2012-03-20 04:01:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "nsIAppStartup.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2012-03-20 04:01:49 +04:00
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIToolkitProfile.h"
|
|
|
|
#include "nsIWindowWatcher.h"
|
|
|
|
|
|
|
|
#include "ProfileReset.h"
|
|
|
|
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
2015-10-27 00:37:32 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-03-20 04:01:49 +04:00
|
|
|
#include "nsPrintfCString.h"
|
2016-12-02 17:07:24 +03:00
|
|
|
#include "nsString.h"
|
2012-03-20 04:01:49 +04:00
|
|
|
#include "nsToolkitCompsCID.h"
|
|
|
|
#include "nsXPCOMCIDInternal.h"
|
2016-12-02 17:07:24 +03:00
|
|
|
#include "mozilla/XREAppData.h"
|
2012-03-20 04:01:49 +04:00
|
|
|
|
|
|
|
#include "mozilla/Services.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "prtime.h"
|
2012-03-20 04:01:49 +04:00
|
|
|
|
2016-12-02 17:07:24 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
extern const XREAppData* gAppData;
|
2012-03-20 04:01:49 +04:00
|
|
|
|
|
|
|
static const char kProfileProperties[] =
|
|
|
|
"chrome://mozapps/locale/profile/profileSelection.properties";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new profile with a timestamp in the name to use for profile reset.
|
|
|
|
*/
|
|
|
|
nsresult
|
2017-02-10 02:29:58 +03:00
|
|
|
CreateResetProfile(nsIToolkitProfileService* aProfileSvc, const nsACString& aOldProfileName, nsIToolkitProfile* *aNewProfile)
|
2012-03-20 04:01:49 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aProfileSvc, "NULL profile service");
|
2012-03-20 04:01:49 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIToolkitProfile> newProfile;
|
2017-02-10 02:29:58 +03:00
|
|
|
// Make the new profile the old profile (or "default-") + the time in seconds since epoch for uniqueness.
|
|
|
|
nsAutoCString newProfileName;
|
|
|
|
if (!aOldProfileName.IsEmpty()) {
|
|
|
|
newProfileName.Assign(aOldProfileName);
|
|
|
|
newProfileName.Append("-");
|
|
|
|
} else {
|
2017-09-04 08:12:56 +03:00
|
|
|
newProfileName.AssignLiteral("default-");
|
2017-02-10 02:29:58 +03:00
|
|
|
}
|
2016-12-14 19:32:21 +03:00
|
|
|
newProfileName.Append(nsPrintfCString("%" PRId64, PR_Now() / 1000));
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult rv = aProfileSvc->CreateProfile(nullptr, // choose a default dir for us
|
2012-03-20 04:01:49 +04:00
|
|
|
newProfileName,
|
|
|
|
getter_AddRefs(newProfile));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = aProfileSvc->Flush();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
newProfile.swap(*aNewProfile);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the profile directory being reset after a backup and delete the local profile directory.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
ProfileResetCleanup(nsIToolkitProfile* aOldProfile)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> profileDir;
|
2012-03-20 04:01:49 +04:00
|
|
|
rv = aOldProfile->GetRootDir(getter_AddRefs(profileDir));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> profileLocalDir;
|
2012-03-20 04:01:49 +04:00
|
|
|
rv = aOldProfile->GetLocalDir(getter_AddRefs(profileLocalDir));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Get the friendly name for the backup directory.
|
|
|
|
nsCOMPtr<nsIStringBundleService> sbs = mozilla::services::GetStringBundleService();
|
|
|
|
if (!sbs) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> sb;
|
|
|
|
rv = sbs->CreateBundle(kProfileProperties, getter_AddRefs(sb));
|
|
|
|
if (!sb) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 appName(gAppData->name);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* params[] = {appName.get(), appName.get()};
|
2012-03-20 04:01:49 +04:00
|
|
|
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString resetBackupDirectoryName;
|
2012-03-20 04:01:49 +04:00
|
|
|
|
2017-07-12 08:13:37 +03:00
|
|
|
static const char* kResetBackupDirectory = "resetBackupDirectory";
|
2012-03-20 04:01:49 +04:00
|
|
|
rv = sb->FormatStringFromName(kResetBackupDirectory, params, 2,
|
2017-08-04 07:40:52 +03:00
|
|
|
resetBackupDirectoryName);
|
2012-03-20 04:01:49 +04:00
|
|
|
|
|
|
|
// Get info to copy the old root profile dir to the desktop as a backup.
|
2013-04-05 16:31:28 +04:00
|
|
|
nsCOMPtr<nsIFile> backupDest, containerDest, profileDest;
|
2012-03-20 04:01:49 +04:00
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(backupDest));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Fall back to the home directory if the desktop is not available.
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_HOME_DIR, getter_AddRefs(backupDest));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2013-04-05 16:31:28 +04:00
|
|
|
// Try to create a directory for all the backups
|
|
|
|
backupDest->Clone(getter_AddRefs(containerDest));
|
|
|
|
containerDest->Append(resetBackupDirectoryName);
|
|
|
|
rv = containerDest->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
|
|
|
// It's OK if it already exists, if and only if it is a directory
|
|
|
|
if (rv == NS_ERROR_FILE_ALREADY_EXISTS) {
|
|
|
|
bool containerIsDir;
|
|
|
|
rv = containerDest->IsDirectory(&containerIsDir);
|
|
|
|
if (NS_FAILED(rv) || !containerIsDir) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2012-03-20 04:01:49 +04:00
|
|
|
|
2013-04-05 16:31:28 +04:00
|
|
|
// Get the name of the profile
|
2012-03-20 04:01:49 +04:00
|
|
|
nsAutoString leafName;
|
2013-04-05 16:31:28 +04:00
|
|
|
rv = profileDir->GetLeafName(leafName);
|
2012-03-20 04:01:49 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2013-04-05 16:31:28 +04:00
|
|
|
|
|
|
|
// Try to create a unique directory for the profile:
|
|
|
|
containerDest->Clone(getter_AddRefs(profileDest));
|
|
|
|
profileDest->Append(leafName);
|
|
|
|
rv = profileDest->CreateUnique(nsIFile::DIRECTORY_TYPE, 0700);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Get the unique profile name
|
|
|
|
rv = profileDest->GetLeafName(leafName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-03-20 04:01:49 +04:00
|
|
|
// Delete the empty directory that CreateUnique just created.
|
2013-04-05 16:31:28 +04:00
|
|
|
rv = profileDest->Remove(false);
|
2012-03-20 04:01:49 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Show a progress window while the cleanup happens since the disk I/O can take time.
|
|
|
|
nsCOMPtr<nsIWindowWatcher> windowWatcher(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
|
|
|
|
if (!windowWatcher) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID));
|
|
|
|
if (!appStartup) return NS_ERROR_FAILURE;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozIDOMWindowProxy> progressWindow;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = windowWatcher->OpenWindow(nullptr,
|
2012-03-20 04:01:49 +04:00
|
|
|
kResetProgressURL,
|
|
|
|
"_blank",
|
|
|
|
"centerscreen,chrome,titlebar",
|
2013-10-11 00:36:42 +04:00
|
|
|
nullptr,
|
2012-03-20 04:01:49 +04:00
|
|
|
getter_AddRefs(progressWindow));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create a new thread to do the bulk of profile cleanup to stay responsive.
|
|
|
|
nsCOMPtr<nsIThreadManager> tm = do_GetService(NS_THREADMANAGER_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIThread> cleanupThread;
|
|
|
|
rv = tm->NewThread(0, 0, getter_AddRefs(cleanupThread));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ProfileResetCleanupAsyncTask(profileDir, profileLocalDir,
|
2013-04-05 16:31:28 +04:00
|
|
|
containerDest, leafName);
|
2012-03-20 04:01:49 +04:00
|
|
|
cleanupThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL);
|
|
|
|
// The result callback will shut down the worker thread.
|
|
|
|
|
|
|
|
// Wait for the cleanup thread to complete.
|
2017-05-15 16:34:19 +03:00
|
|
|
SpinEventLoopUntil([&]() { return gProfileResetCleanupCompleted; });
|
2012-03-20 04:01:49 +04:00
|
|
|
} else {
|
|
|
|
gProfileResetCleanupCompleted = true;
|
|
|
|
NS_WARNING("Cleanup thread creation failed");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// Close the progress window now that the cleanup thread is done.
|
2016-01-30 20:05:36 +03:00
|
|
|
auto* piWindow = nsPIDOMWindowOuter::From(progressWindow);
|
2015-10-27 00:37:32 +03:00
|
|
|
piWindow->Close();
|
2012-03-20 04:01:49 +04:00
|
|
|
|
2013-04-05 16:31:28 +04:00
|
|
|
// Delete the old profile from profiles.ini. The folder was already deleted by the thread above.
|
2012-03-20 04:01:49 +04:00
|
|
|
rv = aOldProfile->Remove(false);
|
|
|
|
if (NS_FAILED(rv)) NS_WARNING("Could not remove the profile");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|