|
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 "nsIAppStartup.h" |
|
7 #include "nsIDOMWindow.h" |
|
8 #include "nsIFile.h" |
|
9 #include "nsIStringBundle.h" |
|
10 #include "nsIToolkitProfile.h" |
|
11 #include "nsIWindowWatcher.h" |
|
12 |
|
13 #include "ProfileReset.h" |
|
14 |
|
15 #include "nsDirectoryServiceDefs.h" |
|
16 #include "nsDirectoryServiceUtils.h" |
|
17 #include "nsPrintfCString.h" |
|
18 #include "nsToolkitCompsCID.h" |
|
19 #include "nsXPCOMCIDInternal.h" |
|
20 #include "nsXREAppData.h" |
|
21 |
|
22 #include "mozilla/Services.h" |
|
23 #include "prtime.h" |
|
24 |
|
25 extern const nsXREAppData* gAppData; |
|
26 |
|
27 static const char kProfileProperties[] = |
|
28 "chrome://mozapps/locale/profile/profileSelection.properties"; |
|
29 |
|
30 /** |
|
31 * Creates a new profile with a timestamp in the name to use for profile reset. |
|
32 */ |
|
33 nsresult |
|
34 CreateResetProfile(nsIToolkitProfileService* aProfileSvc, nsIToolkitProfile* *aNewProfile) |
|
35 { |
|
36 NS_ABORT_IF_FALSE(aProfileSvc, "NULL profile service"); |
|
37 |
|
38 nsCOMPtr<nsIToolkitProfile> newProfile; |
|
39 // Make the new profile "default-" + the time in seconds since epoch for uniqueness. |
|
40 nsAutoCString newProfileName("default-"); |
|
41 newProfileName.Append(nsPrintfCString("%lld", PR_Now() / 1000)); |
|
42 nsresult rv = aProfileSvc->CreateProfile(nullptr, // choose a default dir for us |
|
43 newProfileName, |
|
44 getter_AddRefs(newProfile)); |
|
45 if (NS_FAILED(rv)) return rv; |
|
46 |
|
47 rv = aProfileSvc->Flush(); |
|
48 if (NS_FAILED(rv)) return rv; |
|
49 |
|
50 newProfile.swap(*aNewProfile); |
|
51 |
|
52 return NS_OK; |
|
53 } |
|
54 |
|
55 /** |
|
56 * Delete the profile directory being reset after a backup and delete the local profile directory. |
|
57 */ |
|
58 nsresult |
|
59 ProfileResetCleanup(nsIToolkitProfile* aOldProfile) |
|
60 { |
|
61 nsresult rv; |
|
62 nsCOMPtr<nsIFile> profileDir; |
|
63 rv = aOldProfile->GetRootDir(getter_AddRefs(profileDir)); |
|
64 if (NS_FAILED(rv)) return rv; |
|
65 |
|
66 nsCOMPtr<nsIFile> profileLocalDir; |
|
67 rv = aOldProfile->GetLocalDir(getter_AddRefs(profileLocalDir)); |
|
68 if (NS_FAILED(rv)) return rv; |
|
69 |
|
70 // Get the friendly name for the backup directory. |
|
71 nsCOMPtr<nsIStringBundleService> sbs = mozilla::services::GetStringBundleService(); |
|
72 if (!sbs) return NS_ERROR_FAILURE; |
|
73 |
|
74 nsCOMPtr<nsIStringBundle> sb; |
|
75 rv = sbs->CreateBundle(kProfileProperties, getter_AddRefs(sb)); |
|
76 if (!sb) return NS_ERROR_FAILURE; |
|
77 |
|
78 NS_ConvertUTF8toUTF16 appName(gAppData->name); |
|
79 const char16_t* params[] = {appName.get(), appName.get()}; |
|
80 |
|
81 nsXPIDLString resetBackupDirectoryName; |
|
82 |
|
83 static const char16_t* kResetBackupDirectory = MOZ_UTF16("resetBackupDirectory"); |
|
84 rv = sb->FormatStringFromName(kResetBackupDirectory, params, 2, |
|
85 getter_Copies(resetBackupDirectoryName)); |
|
86 |
|
87 // Get info to copy the old root profile dir to the desktop as a backup. |
|
88 nsCOMPtr<nsIFile> backupDest, containerDest, profileDest; |
|
89 rv = NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(backupDest)); |
|
90 if (NS_FAILED(rv)) { |
|
91 // Fall back to the home directory if the desktop is not available. |
|
92 rv = NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(backupDest)); |
|
93 if (NS_FAILED(rv)) return rv; |
|
94 } |
|
95 |
|
96 // Try to create a directory for all the backups |
|
97 backupDest->Clone(getter_AddRefs(containerDest)); |
|
98 containerDest->Append(resetBackupDirectoryName); |
|
99 rv = containerDest->Create(nsIFile::DIRECTORY_TYPE, 0700); |
|
100 // It's OK if it already exists, if and only if it is a directory |
|
101 if (rv == NS_ERROR_FILE_ALREADY_EXISTS) { |
|
102 bool containerIsDir; |
|
103 rv = containerDest->IsDirectory(&containerIsDir); |
|
104 if (NS_FAILED(rv) || !containerIsDir) { |
|
105 return rv; |
|
106 } |
|
107 } else if (NS_FAILED(rv)) { |
|
108 return rv; |
|
109 } |
|
110 |
|
111 // Get the name of the profile |
|
112 nsAutoString leafName; |
|
113 rv = profileDir->GetLeafName(leafName); |
|
114 if (NS_FAILED(rv)) return rv; |
|
115 |
|
116 // Try to create a unique directory for the profile: |
|
117 containerDest->Clone(getter_AddRefs(profileDest)); |
|
118 profileDest->Append(leafName); |
|
119 rv = profileDest->CreateUnique(nsIFile::DIRECTORY_TYPE, 0700); |
|
120 if (NS_FAILED(rv)) return rv; |
|
121 |
|
122 // Get the unique profile name |
|
123 rv = profileDest->GetLeafName(leafName); |
|
124 if (NS_FAILED(rv)) return rv; |
|
125 |
|
126 // Delete the empty directory that CreateUnique just created. |
|
127 rv = profileDest->Remove(false); |
|
128 if (NS_FAILED(rv)) return rv; |
|
129 |
|
130 // Show a progress window while the cleanup happens since the disk I/O can take time. |
|
131 nsCOMPtr<nsIWindowWatcher> windowWatcher(do_GetService(NS_WINDOWWATCHER_CONTRACTID)); |
|
132 if (!windowWatcher) return NS_ERROR_FAILURE; |
|
133 |
|
134 nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID)); |
|
135 if (!appStartup) return NS_ERROR_FAILURE; |
|
136 |
|
137 nsCOMPtr<nsIDOMWindow> progressWindow; |
|
138 rv = windowWatcher->OpenWindow(nullptr, |
|
139 kResetProgressURL, |
|
140 "_blank", |
|
141 "centerscreen,chrome,titlebar", |
|
142 nullptr, |
|
143 getter_AddRefs(progressWindow)); |
|
144 if (NS_FAILED(rv)) return rv; |
|
145 |
|
146 // Create a new thread to do the bulk of profile cleanup to stay responsive. |
|
147 nsCOMPtr<nsIThreadManager> tm = do_GetService(NS_THREADMANAGER_CONTRACTID); |
|
148 nsCOMPtr<nsIThread> cleanupThread; |
|
149 rv = tm->NewThread(0, 0, getter_AddRefs(cleanupThread)); |
|
150 if (NS_SUCCEEDED(rv)) { |
|
151 nsCOMPtr<nsIRunnable> runnable = new ProfileResetCleanupAsyncTask(profileDir, profileLocalDir, |
|
152 containerDest, leafName); |
|
153 cleanupThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL); |
|
154 // The result callback will shut down the worker thread. |
|
155 |
|
156 nsIThread *thread = NS_GetCurrentThread(); |
|
157 // Wait for the cleanup thread to complete. |
|
158 while(!gProfileResetCleanupCompleted) { |
|
159 NS_ProcessNextEvent(thread); |
|
160 } |
|
161 } else { |
|
162 gProfileResetCleanupCompleted = true; |
|
163 NS_WARNING("Cleanup thread creation failed"); |
|
164 return rv; |
|
165 } |
|
166 // Close the progress window now that the cleanup thread is done. |
|
167 progressWindow->Close(); |
|
168 |
|
169 // Delete the old profile from profiles.ini. The folder was already deleted by the thread above. |
|
170 rv = aOldProfile->Remove(false); |
|
171 if (NS_FAILED(rv)) NS_WARNING("Could not remove the profile"); |
|
172 |
|
173 return rv; |
|
174 } |