michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIToolkitProfileService.h" michael@0: #include "nsIFile.h" michael@0: #include "nsThreadUtils.h" michael@0: michael@0: static bool gProfileResetCleanupCompleted = false; michael@0: static const char kResetProgressURL[] = "chrome://global/content/resetProfileProgress.xul"; michael@0: michael@0: nsresult CreateResetProfile(nsIToolkitProfileService* aProfileSvc, michael@0: nsIToolkitProfile* *aNewProfile); michael@0: michael@0: nsresult ProfileResetCleanup(nsIToolkitProfile* aOldProfile); michael@0: michael@0: class ProfileResetCleanupResultTask : public nsRunnable michael@0: { michael@0: public: michael@0: ProfileResetCleanupResultTask() michael@0: : mWorkerThread(do_GetCurrentThread()) michael@0: { michael@0: MOZ_ASSERT(!NS_IsMainThread()); michael@0: } michael@0: michael@0: NS_IMETHOD Run() { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: mWorkerThread->Shutdown(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsCOMPtr mWorkerThread; michael@0: }; michael@0: michael@0: class ProfileResetCleanupAsyncTask : public nsRunnable michael@0: { michael@0: public: michael@0: ProfileResetCleanupAsyncTask(nsIFile* aProfileDir, nsIFile* aProfileLocalDir, michael@0: nsIFile* aTargetDir, const nsAString &aLeafName) michael@0: : mProfileDir(aProfileDir) michael@0: , mProfileLocalDir(aProfileLocalDir) michael@0: , mTargetDir(aTargetDir) michael@0: , mLeafName(aLeafName) michael@0: { } michael@0: michael@0: /** michael@0: * Copy a root profile to a backup folder before deleting it. Then delete the local profile dir. michael@0: */ michael@0: NS_IMETHOD Run() michael@0: { michael@0: // Copy to the destination then delete the profile. A move doesn't follow links. michael@0: nsresult rv = mProfileDir->CopyToFollowingLinks(mTargetDir, mLeafName); michael@0: if (NS_SUCCEEDED(rv)) michael@0: rv = mProfileDir->Remove(true); michael@0: else michael@0: NS_WARNING("Could not backup the root profile directory"); michael@0: michael@0: // If we have a separate local cache profile directory, just delete it. michael@0: // Don't return an error if this fails so that reset can proceed if it can't be deleted. michael@0: bool sameDir; michael@0: nsresult rvLocal = mProfileDir->Equals(mProfileLocalDir, &sameDir); michael@0: if (NS_SUCCEEDED(rvLocal) && !sameDir) { michael@0: rvLocal = mProfileLocalDir->Remove(true); michael@0: if (NS_FAILED(rvLocal)) NS_WARNING("Could not remove the old local profile directory (cache)"); michael@0: } michael@0: gProfileResetCleanupCompleted = true; michael@0: michael@0: nsCOMPtr resultRunnable = new ProfileResetCleanupResultTask(); michael@0: NS_DispatchToMainThread(resultRunnable); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: nsCOMPtr mProfileDir; michael@0: nsCOMPtr mProfileLocalDir; michael@0: nsCOMPtr mTargetDir; michael@0: nsAutoString mLeafName; michael@0: };