ProfileReset.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsIToolkitProfileService.h"
  6. #include "nsIFile.h"
  7. #include "nsThreadUtils.h"
  8. static bool gProfileResetCleanupCompleted = false;
  9. static const char kResetProgressURL[] = "chrome://global/content/resetProfileProgress.xul";
  10. nsresult CreateResetProfile(nsIToolkitProfileService* aProfileSvc,
  11. const nsACString& aOldProfileName,
  12. nsIToolkitProfile* *aNewProfile);
  13. nsresult ProfileResetCleanup(nsIToolkitProfile* aOldProfile);
  14. class ProfileResetCleanupResultTask : public mozilla::Runnable
  15. {
  16. public:
  17. ProfileResetCleanupResultTask()
  18. : mWorkerThread(do_GetCurrentThread())
  19. {
  20. MOZ_ASSERT(!NS_IsMainThread());
  21. }
  22. NS_IMETHOD Run() override {
  23. MOZ_ASSERT(NS_IsMainThread());
  24. mWorkerThread->Shutdown();
  25. return NS_OK;
  26. }
  27. private:
  28. nsCOMPtr<nsIThread> mWorkerThread;
  29. };
  30. class ProfileResetCleanupAsyncTask : public mozilla::Runnable
  31. {
  32. public:
  33. ProfileResetCleanupAsyncTask(nsIFile* aProfileDir, nsIFile* aProfileLocalDir,
  34. nsIFile* aTargetDir, const nsAString &aLeafName)
  35. : mProfileDir(aProfileDir)
  36. , mProfileLocalDir(aProfileLocalDir)
  37. , mTargetDir(aTargetDir)
  38. , mLeafName(aLeafName)
  39. { }
  40. /**
  41. * Copy a root profile to a backup folder before deleting it. Then delete the local profile dir.
  42. */
  43. NS_IMETHOD Run() override
  44. {
  45. // Copy to the destination then delete the profile. A move doesn't follow links.
  46. nsresult rv = mProfileDir->CopyToFollowingLinks(mTargetDir, mLeafName);
  47. if (NS_SUCCEEDED(rv))
  48. rv = mProfileDir->Remove(true);
  49. if (NS_WARN_IF(NS_FAILED(rv))) {
  50. NS_WARNING("Could not backup the root profile directory");
  51. }
  52. // If we have a separate local cache profile directory, just delete it.
  53. // Don't return an error if this fails so that reset can proceed if it can't be deleted.
  54. bool sameDir;
  55. nsresult rvLocal = mProfileDir->Equals(mProfileLocalDir, &sameDir);
  56. if (NS_SUCCEEDED(rvLocal) && !sameDir) {
  57. rvLocal = mProfileLocalDir->Remove(true);
  58. if (NS_FAILED(rvLocal)) NS_WARNING("Could not remove the old local profile directory (cache)");
  59. }
  60. gProfileResetCleanupCompleted = true;
  61. nsCOMPtr<nsIRunnable> resultRunnable = new ProfileResetCleanupResultTask();
  62. NS_DispatchToMainThread(resultRunnable);
  63. return NS_OK;
  64. }
  65. private:
  66. nsCOMPtr<nsIFile> mProfileDir;
  67. nsCOMPtr<nsIFile> mProfileLocalDir;
  68. nsCOMPtr<nsIFile> mTargetDir;
  69. nsString mLeafName;
  70. };