File: migration_notification_manager.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 (145 lines) | stat: -rw-r--r-- 5,588 bytes parent folder | download | duplicates (3)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2024 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_ASH_POLICY_SKYVAULT_MIGRATION_NOTIFICATION_MANAGER_H_
#define CHROME_BROWSER_ASH_POLICY_SKYVAULT_MIGRATION_NOTIFICATION_MANAGER_H_

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "base/callback_list.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "chrome/browser/ash/policy/skyvault/policy_utils.h"
#include "chrome/browser/profiles/profile_keyed_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"

namespace base {
template <typename T>
class NoDestructor;
}  // namespace base

namespace content {
class BrowserContext;
}  // namespace content

namespace policy::local_user_files {

inline constexpr char kSkyVaultMigrationNotificationId[] = "skyvault-migration";

// Shows notifications and dialogs related to SkyVault migration status.
class MigrationNotificationManager : public KeyedService {
 public:
  using SignInCallback = base::OnceCallback<void(base::File::Error)>;
  using SignInCallbacks = base::OnceCallbackList<void(base::File::Error)>;

  explicit MigrationNotificationManager(content::BrowserContext* context);
  ~MigrationNotificationManager() override;

  // Shows a dialog informing the user that the migration will happen at
  // `migration_start_time`, e.g. 24 h or 1 h from now. From the dialog, the
  // user can select to start the migration immediately which executes the
  // `migration_callback`.
  // Virtual to override in tests.
  virtual void ShowMigrationInfoDialog(MigrationDestination destination,
                                       base::Time migration_start_time,
                                       base::OnceClosure migration_callback);

  // Shows the migration in progress notification.
  void ShowMigrationProgressNotification(MigrationDestination destination);

  // Shows the migration completed successfully notification with a button to
  // open the folder specified by `destination_path`.
  void ShowMigrationCompletedNotification(
      MigrationDestination destination,
      const base::FilePath& destination_path);

  // Shows a notification that the user's files were successfully removed.
  // Virtual to override in tests.
  virtual void ShowDeletionCompletedNotification();

  // Shows a notification that migration completed with errors.
  void ShowMigrationErrorNotification(MigrationDestination destination,
                                      const std::string& folder_name,
                                      const base::FilePath& error_log_path);

  // Shows the policy configuration error notification.
  // Virtual to override in tests.
  virtual void ShowConfigurationErrorNotification(
      MigrationDestination destination);

  // Displays a single notification prompting the user to sign in to OneDrive.
  // Queues any subsequent sign-in requests until the user responds which
  // executes all queued callbacks with the result of the sign-in process.
  base::CallbackListSubscription ShowOneDriveSignInNotification(
      SignInCallback callback);

  // Closes any open notification.
  void CloseNotifications();

  // Closes the migration dialog. No-op if dialog isn't opened.
  void CloseDialog();

 private:
  Profile* profile();

  // Callback invoked when the user responds to the OneDrive sign-in
  // notification.
  // Shows a progress notification if the setup and sign in completed
  // successfully (base::File::Error::FILE_ERROR_OK).
  void OnSignInResponse(base::File::Error error);

  // Context for which this instance was created.
  raw_ptr<content::BrowserContext> context_;

  // List of sign-in result callbacks, mapped to a single sign-in notification.
  // This ensures that all callers who requested the sign-in receive the final
  // result (success or error) once the notification is dismissed.
  SignInCallbacks sign_in_callbacks_ GUARDED_BY_CONTEXT(sequence_checker_);

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<MigrationNotificationManager> weak_factory_{this};
};

// Manages all MigrationNotificationManager instances and associates them with
// Profiles.
class MigrationNotificationManagerFactory : public ProfileKeyedServiceFactory {
 public:
  MigrationNotificationManagerFactory(
      const MigrationNotificationManagerFactory&) = delete;
  MigrationNotificationManagerFactory& operator=(
      const MigrationNotificationManagerFactory&) = delete;

  // Gets the singleton instance of the factory.
  static MigrationNotificationManagerFactory* GetInstance();

  // Gets the LocalFilesMigrationManager instance associated with the given
  // BrowserContext.
  static MigrationNotificationManager* GetForBrowserContext(
      content::BrowserContext* context);

 private:
  friend base::NoDestructor<MigrationNotificationManagerFactory>;

  MigrationNotificationManagerFactory();
  ~MigrationNotificationManagerFactory() override;

  // BrowserContextKeyedServiceFactory overrides:
  bool ServiceIsNULLWhileTesting() const override;
  std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
      content::BrowserContext* context) const override;
};

}  // namespace policy::local_user_files

#endif  // CHROME_BROWSER_ASH_POLICY_SKYVAULT_MIGRATION_NOTIFICATION_MANAGER_H_