nsDeleteDir.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 et cindent: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef nsDeleteDir_h__
  7. #define nsDeleteDir_h__
  8. #include "nsCOMPtr.h"
  9. #include "nsCOMArray.h"
  10. #include "mozilla/Mutex.h"
  11. #include "mozilla/CondVar.h"
  12. class nsIFile;
  13. class nsIThread;
  14. class nsITimer;
  15. class nsDeleteDir {
  16. public:
  17. nsDeleteDir();
  18. ~nsDeleteDir();
  19. static nsresult Init();
  20. static nsresult Shutdown(bool finishDeleting);
  21. /**
  22. * This routine attempts to delete a directory that may contain some files
  23. * that are still in use. This latter point is only an issue on Windows and
  24. * a few other systems.
  25. *
  26. * If the moveToTrash parameter is true we first rename the given directory
  27. * "foo.Trash123" (where "foo" is the original directory name, and "123" is
  28. * a random number, in order to support multiple concurrent deletes). The
  29. * directory is then deleted, file-by-file, on a background thread.
  30. *
  31. * If the moveToTrash parameter is false, then the given directory is deleted
  32. * directly.
  33. *
  34. * If 'delay' is non-zero, the directory will not be deleted until the
  35. * specified number of milliseconds have passed. (The directory is still
  36. * renamed immediately if 'moveToTrash' is passed, so upon return it is safe
  37. * to create a directory with the same name).
  38. */
  39. static nsresult DeleteDir(nsIFile *dir, bool moveToTrash, uint32_t delay = 0);
  40. /**
  41. * Returns the trash directory corresponding to the given directory.
  42. */
  43. static nsresult GetTrashDir(nsIFile *dir, nsCOMPtr<nsIFile> *result);
  44. /**
  45. * Remove all trashes left from previous run. This function does nothing when
  46. * called second and more times.
  47. */
  48. static nsresult RemoveOldTrashes(nsIFile *cacheDir);
  49. static void TimerCallback(nsITimer *aTimer, void *arg);
  50. private:
  51. friend class nsBlockOnBackgroundThreadEvent;
  52. friend class nsDestroyThreadEvent;
  53. nsresult InitThread();
  54. void DestroyThread();
  55. nsresult PostTimer(void *arg, uint32_t delay);
  56. nsresult RemoveDir(nsIFile *file, bool *stopDeleting);
  57. static nsDeleteDir * gInstance;
  58. mozilla::Mutex mLock;
  59. mozilla::CondVar mCondVar;
  60. bool mNotified;
  61. nsCOMArray<nsITimer> mTimers;
  62. nsCOMPtr<nsIThread> mThread;
  63. bool mShutdownPending;
  64. bool mStopDeleting;
  65. };
  66. #endif // nsDeleteDir_h__