toolkit/xre/ProfileReset.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/xre/ProfileReset.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsIToolkitProfileService.h"
    1.10 +#include "nsIFile.h"
    1.11 +#include "nsThreadUtils.h"
    1.12 +
    1.13 +static bool gProfileResetCleanupCompleted = false;
    1.14 +static const char kResetProgressURL[] = "chrome://global/content/resetProfileProgress.xul";
    1.15 +
    1.16 +nsresult CreateResetProfile(nsIToolkitProfileService* aProfileSvc,
    1.17 +                            nsIToolkitProfile* *aNewProfile);
    1.18 +
    1.19 +nsresult ProfileResetCleanup(nsIToolkitProfile* aOldProfile);
    1.20 +
    1.21 +class ProfileResetCleanupResultTask : public nsRunnable
    1.22 +{
    1.23 +public:
    1.24 +  ProfileResetCleanupResultTask()
    1.25 +    : mWorkerThread(do_GetCurrentThread())
    1.26 +  {
    1.27 +    MOZ_ASSERT(!NS_IsMainThread());
    1.28 +  }
    1.29 +
    1.30 +  NS_IMETHOD Run() {
    1.31 +    MOZ_ASSERT(NS_IsMainThread());
    1.32 +    mWorkerThread->Shutdown();
    1.33 +    return NS_OK;
    1.34 +  }
    1.35 +
    1.36 +private:
    1.37 +  nsCOMPtr<nsIThread> mWorkerThread;
    1.38 +};
    1.39 +
    1.40 +class ProfileResetCleanupAsyncTask : public nsRunnable
    1.41 +{
    1.42 +public:
    1.43 +  ProfileResetCleanupAsyncTask(nsIFile* aProfileDir, nsIFile* aProfileLocalDir,
    1.44 +                               nsIFile* aTargetDir, const nsAString &aLeafName)
    1.45 +    : mProfileDir(aProfileDir)
    1.46 +    , mProfileLocalDir(aProfileLocalDir)
    1.47 +    , mTargetDir(aTargetDir)
    1.48 +    , mLeafName(aLeafName)
    1.49 +  { }
    1.50 +
    1.51 +/**
    1.52 + * Copy a root profile to a backup folder before deleting it.  Then delete the local profile dir.
    1.53 + */
    1.54 +  NS_IMETHOD Run()
    1.55 +  {
    1.56 +    // Copy to the destination then delete the profile. A move doesn't follow links.
    1.57 +    nsresult rv = mProfileDir->CopyToFollowingLinks(mTargetDir, mLeafName);
    1.58 +    if (NS_SUCCEEDED(rv))
    1.59 +      rv = mProfileDir->Remove(true);
    1.60 +    else
    1.61 +      NS_WARNING("Could not backup the root profile directory");
    1.62 +
    1.63 +    // If we have a separate local cache profile directory, just delete it.
    1.64 +    // Don't return an error if this fails so that reset can proceed if it can't be deleted.
    1.65 +    bool sameDir;
    1.66 +    nsresult rvLocal = mProfileDir->Equals(mProfileLocalDir, &sameDir);
    1.67 +    if (NS_SUCCEEDED(rvLocal) && !sameDir) {
    1.68 +      rvLocal = mProfileLocalDir->Remove(true);
    1.69 +      if (NS_FAILED(rvLocal)) NS_WARNING("Could not remove the old local profile directory (cache)");
    1.70 +    }
    1.71 +    gProfileResetCleanupCompleted = true;
    1.72 +
    1.73 +    nsCOMPtr<nsIRunnable> resultRunnable = new ProfileResetCleanupResultTask();
    1.74 +    NS_DispatchToMainThread(resultRunnable);
    1.75 +    return NS_OK;
    1.76 +  }
    1.77 +
    1.78 +private:
    1.79 +  nsCOMPtr<nsIFile> mProfileDir;
    1.80 +  nsCOMPtr<nsIFile> mProfileLocalDir;
    1.81 +  nsCOMPtr<nsIFile> mTargetDir;
    1.82 +  nsAutoString mLeafName;
    1.83 +};

mercurial