toolkit/xre/ProfileReset.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial