File: delete_profile_helper.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,927 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PROFILES_DELETE_PROFILE_HELPER_H_
#define CHROME_BROWSER_PROFILES_DELETE_PROFILE_HELPER_H_

#include <optional>

#include "base/functional/callback_forward.h"
#include "base/memory/raw_ref.h"
#include "chrome/browser/profiles/profile_metrics.h"

namespace base {
class FilePath;
}

class Profile;
class ProfileManager;
class ScopedKeepAlive;
class ScopedProfileKeepAlive;

// This class offers a few helper functions for profile deletion. Note that the
// `DeleteProfileHelper` does not delete actual C++ Profile objects, as this is
// done through the `ScopedProfileKeepAlive` mechanism and
// `ProfileManager::RemoveProfile()`.
// The `DeleteProfileHelper` is responsible for:
// - deleting the profile as a user-visible concept: removes it from the
//   `ProfileAttributesStorage` and deletes the user data on disk.
// - creates or loads another profile before the last profile is deleted.
class DeleteProfileHelper {
 public:
  using ProfileLoadedCallback = base::OnceCallback<void(Profile*)>;

  explicit DeleteProfileHelper(ProfileManager& profile_manager);

  ~DeleteProfileHelper();

  DeleteProfileHelper(const DeleteProfileHelper&) = delete;
  DeleteProfileHelper& operator=(const DeleteProfileHelper&) = delete;

  // Schedules the profile at the given path to be deleted on shutdown. If we're
  // deleting the last profile, a new one will be created in its place, and in
  // that case the callback will be called when profile creation is complete.
  // Silently exits if profile is either scheduling or marked for deletion.
  // If the profile is not loaded, this may trigger a reload of the profile so
  // that user data is cleaned up.
  void MaybeScheduleProfileForDeletion(
      const base::FilePath& profile_dir,
      ProfileLoadedCallback callback,
      ProfileMetrics::ProfileDelete deletion_source);

  // Schedules the ephemeral profile at the given path to be deleted. New
  // profiles will not be created. If the profile is not loaded, this may
  // trigger a reload of the profile so that user data is cleaned up.
  void ScheduleEphemeralProfileForDeletion(
      const base::FilePath& profile_dir,
      std::unique_ptr<ScopedProfileKeepAlive> keep_alive);

  // Checks if any profiles are left behind (e.g. because of a browser
  // crash) and schedule them for deletion. Unlike the "Schedule" methods above,
  // these functions only remove the data from disk and do not perform any steps
  // that require the profile to be loaded. They assume that the deleted
  // profiles are not loaded in memory.
  void CleanUpEphemeralProfiles();
  void CleanUpDeletedProfiles();

 private:
  // Continues the scheduled profile deletion after closing all the profile's
  // browsers tabs. Creates a new profile if the profile to be deleted is the
  // last non-supervised profile. In the Mac, loads the next non-supervised
  // profile if the profile to be deleted is the active profile.
  // `profile_keep_alive` is used to avoid unloading the profile during the
  // deletion process and is null if the profile is not loaded.
  void EnsureActiveProfileExistsBeforeDeletion(
      std::unique_ptr<ScopedKeepAlive> keep_alive,
      std::unique_ptr<ScopedProfileKeepAlive> profile_keep_alive,
      ProfileLoadedCallback callback,
      const base::FilePath& profile_dir);

  // Schedules the profile at the given path to be deleted on shutdown,
  // and marks the new profile as active.
  void FinishDeletingProfile(
      const base::FilePath& profile_dir,
      const base::FilePath& new_active_profile_dir,
      std::unique_ptr<ScopedProfileKeepAlive> keep_alive);
  void OnLoadProfileForProfileDeletion(
      const base::FilePath& profile_dir,
      std::unique_ptr<ScopedProfileKeepAlive> keep_alive,
      Profile* profile);

  // If the `loaded_profile` has been loaded successfully and isn't already
  // scheduled for deletion, then finishes adding `profile_to_delete_dir` to the
  // queue of profiles to be deleted, and updates the kProfileLastUsed
  // preference based on `last_non_supervised_profile_path`. `keep_alive` may be
  // null and is used to ensure shutdown does not start. `profile_keep_alive` is
  // used to avoid unloading the profile during the deletion process and is null
  // if the profile is not loaded.
  void OnNewActiveProfileInitialized(
      const base::FilePath& profile_to_delete_path,
      const base::FilePath& last_non_supervised_profile_path,
      ProfileLoadedCallback callback,
      std::unique_ptr<ScopedKeepAlive> keep_alive,
      std::unique_ptr<ScopedProfileKeepAlive> profile_keep_alive,
      Profile* loaded_profile);

  const raw_ref<ProfileManager>
      profile_manager_;  // Owns the `DeleteProfileHelper`.
};

#endif  // CHROME_BROWSER_PROFILES_DELETE_PROFILE_HELPER_H_