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 
6 #include "nsToolkitProfileService.h"
7 #include "nsIFile.h"
8 #include "nsIThread.h"
9 #include "nsThreadUtils.h"
10 
11 static bool gProfileResetCleanupCompleted = false;
12 static const char kResetProgressURL[] =
13     "chrome://global/content/resetProfileProgress.xhtml";
14 
15 nsresult ProfileResetCleanup(nsToolkitProfileService* aService,
16                              nsIToolkitProfile* aOldProfile);
17 
18 class ProfileResetCleanupResultTask : public mozilla::Runnable {
19  public:
ProfileResetCleanupResultTask()20   ProfileResetCleanupResultTask()
21       : mozilla::Runnable("ProfileResetCleanupResultTask"),
22         mWorkerThread(do_GetCurrentThread()) {
23     MOZ_ASSERT(!NS_IsMainThread());
24   }
25 
Run()26   NS_IMETHOD Run() override {
27     MOZ_ASSERT(NS_IsMainThread());
28     mWorkerThread->Shutdown();
29     return NS_OK;
30   }
31 
32  private:
33   nsCOMPtr<nsIThread> mWorkerThread;
34 };
35 
36 class ProfileResetCleanupAsyncTask : public mozilla::Runnable {
37  public:
ProfileResetCleanupAsyncTask(nsIFile * aProfileDir,nsIFile * aProfileLocalDir,nsIFile * aTargetDir,const nsAString & aLeafName)38   ProfileResetCleanupAsyncTask(nsIFile* aProfileDir, nsIFile* aProfileLocalDir,
39                                nsIFile* aTargetDir, const nsAString& aLeafName)
40       : mozilla::Runnable("ProfileResetCleanupAsyncTask"),
41         mProfileDir(aProfileDir),
42         mProfileLocalDir(aProfileLocalDir),
43         mTargetDir(aTargetDir),
44         mLeafName(aLeafName) {}
45 
46   /**
47    * Copy a root profile to a backup folder before deleting it.  Then delete the
48    * local profile dir.
49    */
Run()50   NS_IMETHOD Run() override {
51     // Copy profile's files to the destination. The profile folder will be
52     // removed after the changes to the known profiles have been flushed to disk
53     // in nsToolkitProfileService::ApplyResetProfile which isn't called until
54     // after this thread finishes copying the files.
55     nsresult rv = mProfileDir->CopyToFollowingLinks(mTargetDir, mLeafName);
56     // I guess we just warn if we fail to make the backup?
57     if (NS_WARN_IF(NS_FAILED(rv))) {
58       NS_WARNING("Could not backup the root profile directory");
59     }
60 
61     // If we have a separate local cache profile directory, just delete it.
62     // Don't return an error if this fails so that reset can proceed if it can't
63     // be deleted.
64     bool sameDir;
65     nsresult rvLocal = mProfileDir->Equals(mProfileLocalDir, &sameDir);
66     if (NS_SUCCEEDED(rvLocal) && !sameDir) {
67       rvLocal = mProfileLocalDir->Remove(true);
68       if (NS_FAILED(rvLocal)) {
69         NS_WARNING("Could not remove the old local profile directory (cache)");
70       }
71     }
72     gProfileResetCleanupCompleted = true;
73 
74     nsCOMPtr<nsIRunnable> resultRunnable = new ProfileResetCleanupResultTask();
75     NS_DispatchToMainThread(resultRunnable);
76     return NS_OK;
77   }
78 
79  private:
80   nsCOMPtr<nsIFile> mProfileDir;
81   nsCOMPtr<nsIFile> mProfileLocalDir;
82   nsCOMPtr<nsIFile> mTargetDir;
83   nsString mLeafName;
84 };
85