|
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 "nsIToolkitProfileService.h" |
|
7 #include "nsIFile.h" |
|
8 #include "nsThreadUtils.h" |
|
9 |
|
10 static bool gProfileResetCleanupCompleted = false; |
|
11 static const char kResetProgressURL[] = "chrome://global/content/resetProfileProgress.xul"; |
|
12 |
|
13 nsresult CreateResetProfile(nsIToolkitProfileService* aProfileSvc, |
|
14 nsIToolkitProfile* *aNewProfile); |
|
15 |
|
16 nsresult ProfileResetCleanup(nsIToolkitProfile* aOldProfile); |
|
17 |
|
18 class ProfileResetCleanupResultTask : public nsRunnable |
|
19 { |
|
20 public: |
|
21 ProfileResetCleanupResultTask() |
|
22 : mWorkerThread(do_GetCurrentThread()) |
|
23 { |
|
24 MOZ_ASSERT(!NS_IsMainThread()); |
|
25 } |
|
26 |
|
27 NS_IMETHOD Run() { |
|
28 MOZ_ASSERT(NS_IsMainThread()); |
|
29 mWorkerThread->Shutdown(); |
|
30 return NS_OK; |
|
31 } |
|
32 |
|
33 private: |
|
34 nsCOMPtr<nsIThread> mWorkerThread; |
|
35 }; |
|
36 |
|
37 class ProfileResetCleanupAsyncTask : public nsRunnable |
|
38 { |
|
39 public: |
|
40 ProfileResetCleanupAsyncTask(nsIFile* aProfileDir, nsIFile* aProfileLocalDir, |
|
41 nsIFile* aTargetDir, const nsAString &aLeafName) |
|
42 : mProfileDir(aProfileDir) |
|
43 , mProfileLocalDir(aProfileLocalDir) |
|
44 , mTargetDir(aTargetDir) |
|
45 , mLeafName(aLeafName) |
|
46 { } |
|
47 |
|
48 /** |
|
49 * Copy a root profile to a backup folder before deleting it. Then delete the local profile dir. |
|
50 */ |
|
51 NS_IMETHOD Run() |
|
52 { |
|
53 // Copy to the destination then delete the profile. A move doesn't follow links. |
|
54 nsresult rv = mProfileDir->CopyToFollowingLinks(mTargetDir, mLeafName); |
|
55 if (NS_SUCCEEDED(rv)) |
|
56 rv = mProfileDir->Remove(true); |
|
57 else |
|
58 NS_WARNING("Could not backup the root profile directory"); |
|
59 |
|
60 // If we have a separate local cache profile directory, just delete it. |
|
61 // Don't return an error if this fails so that reset can proceed if it can't be deleted. |
|
62 bool sameDir; |
|
63 nsresult rvLocal = mProfileDir->Equals(mProfileLocalDir, &sameDir); |
|
64 if (NS_SUCCEEDED(rvLocal) && !sameDir) { |
|
65 rvLocal = mProfileLocalDir->Remove(true); |
|
66 if (NS_FAILED(rvLocal)) NS_WARNING("Could not remove the old local profile directory (cache)"); |
|
67 } |
|
68 gProfileResetCleanupCompleted = true; |
|
69 |
|
70 nsCOMPtr<nsIRunnable> resultRunnable = new ProfileResetCleanupResultTask(); |
|
71 NS_DispatchToMainThread(resultRunnable); |
|
72 return NS_OK; |
|
73 } |
|
74 |
|
75 private: |
|
76 nsCOMPtr<nsIFile> mProfileDir; |
|
77 nsCOMPtr<nsIFile> mProfileLocalDir; |
|
78 nsCOMPtr<nsIFile> mTargetDir; |
|
79 nsAutoString mLeafName; |
|
80 }; |