Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/xre/ProfileReset.h
Line
Count
Source (jump to first uncovered line)
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 "nsIToolkitProfileService.h"
7
#include "nsIFile.h"
8
#include "nsThreadUtils.h"
9
10
static bool gProfileResetCleanupCompleted = false;
11
static const char kResetProgressURL[] = "chrome://global/content/resetProfileProgress.xul";
12
13
nsresult CreateResetProfile(nsIToolkitProfileService* aProfileSvc,
14
                            const nsACString& aOldProfileName,
15
                            nsIToolkitProfile* *aNewProfile);
16
17
nsresult ProfileResetCleanup(nsIToolkitProfile* aOldProfile);
18
19
class ProfileResetCleanupResultTask : public mozilla::Runnable
20
{
21
public:
22
  ProfileResetCleanupResultTask()
23
    : mozilla::Runnable("ProfileResetCleanupResultTask")
24
    , mWorkerThread(do_GetCurrentThread())
25
0
  {
26
0
    MOZ_ASSERT(!NS_IsMainThread());
27
0
  }
28
29
0
  NS_IMETHOD Run() override {
30
0
    MOZ_ASSERT(NS_IsMainThread());
31
0
    mWorkerThread->Shutdown();
32
0
    return NS_OK;
33
0
  }
34
35
private:
36
  nsCOMPtr<nsIThread> mWorkerThread;
37
};
38
39
class ProfileResetCleanupAsyncTask : public mozilla::Runnable
40
{
41
public:
42
  ProfileResetCleanupAsyncTask(nsIFile* aProfileDir,
43
                               nsIFile* aProfileLocalDir,
44
                               nsIFile* aTargetDir,
45
                               const nsAString& aLeafName)
46
    : mozilla::Runnable("ProfileResetCleanupAsyncTask")
47
    , mProfileDir(aProfileDir)
48
    , mProfileLocalDir(aProfileLocalDir)
49
    , mTargetDir(aTargetDir)
50
    , mLeafName(aLeafName)
51
0
  { }
52
53
/**
54
 * Copy a root profile to a backup folder before deleting it.  Then delete the local profile dir.
55
 */
56
  NS_IMETHOD Run() override
57
0
  {
58
0
    // Copy to the destination then delete the profile. A move doesn't follow links.
59
0
    nsresult rv = mProfileDir->CopyToFollowingLinks(mTargetDir, mLeafName);
60
0
    if (NS_SUCCEEDED(rv))
61
0
      rv = mProfileDir->Remove(true);
62
0
    if (NS_WARN_IF(NS_FAILED(rv))) {
63
0
      NS_WARNING("Could not backup the root profile directory");
64
0
    }
65
0
66
0
    // If we have a separate local cache profile directory, just delete it.
67
0
    // Don't return an error if this fails so that reset can proceed if it can't be deleted.
68
0
    bool sameDir;
69
0
    nsresult rvLocal = mProfileDir->Equals(mProfileLocalDir, &sameDir);
70
0
    if (NS_SUCCEEDED(rvLocal) && !sameDir) {
71
0
      rvLocal = mProfileLocalDir->Remove(true);
72
0
      if (NS_FAILED(rvLocal)) NS_WARNING("Could not remove the old local profile directory (cache)");
73
0
    }
74
0
    gProfileResetCleanupCompleted = true;
75
0
76
0
    nsCOMPtr<nsIRunnable> resultRunnable = new ProfileResetCleanupResultTask();
77
0
    NS_DispatchToMainThread(resultRunnable);
78
0
    return NS_OK;
79
0
  }
80
81
private:
82
  nsCOMPtr<nsIFile> mProfileDir;
83
  nsCOMPtr<nsIFile> mProfileLocalDir;
84
  nsCOMPtr<nsIFile> mTargetDir;
85
  nsString mLeafName;
86
};