File: full_restore_service.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 (222 lines) | stat: -rw-r--r-- 8,438 bytes parent folder | download | duplicates (5)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2020 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_APP_RESTORE_FULL_RESTORE_SERVICE_H_
#define CHROME_BROWSER_ASH_APP_RESTORE_FULL_RESTORE_SERVICE_H_

#include <memory>
#include <optional>

#include "ash/public/cpp/session/session_observer.h"
#include "ash/wm/window_restore/informed_restore_contents_data.h"
#include "base/callback_list.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/sessions/exit_type_service.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/sessions/core/session_types.h"

class Profile;

namespace app_restore {
class RestoreData;
}  // namespace app_restore

namespace ash::full_restore {

class FullRestoreAppLaunchHandler;
class FullRestoreDataHandler;
class NewUserRestorePrefHandler;

// The restore notification button index.
enum class RestoreNotificationButtonIndex {
  kRestore = 0,
  kCancel,
};

// This is used to record histograms, so do not remove or reorder existing
// entries.
enum class RestoreAction {
  kRestore = 0,
  kCancel = 1,
  kCloseByUser = 2,
  kCloseNotByUser = 3,

  // Add any new values above this one, and update kMaxValue to the highest
  // enumerator value.
  kMaxValue = kCloseNotByUser,
};

// The FullRestoreService class calls AppService and Window Management
// interfaces to restore the app launchings and app windows.
class FullRestoreService : public KeyedService,
                           public SessionObserver {
 public:
  // Delegate class that talks to ash shell. Ash shell is not created in
  // unit tests so this should be mocked out for testing those behaviors.
  class Delegate {
   public:
    virtual ~Delegate() = default;
    // Starts overview with the informed restore dialog unless overview is
    // already active.
    virtual void MaybeStartInformedRestoreOverviewSession(
        std::unique_ptr<InformedRestoreContentsData> contents_data) = 0;
    virtual void MaybeEndInformedRestoreOverviewSession() = 0;
    virtual InformedRestoreContentsData* GetInformedRestoreContentData() = 0;
    virtual void OnInformedRestoreContentsDataUpdated() = 0;
  };

  explicit FullRestoreService(Profile* profile);
  FullRestoreService(const FullRestoreService&) = delete;
  FullRestoreService& operator=(const FullRestoreService&) = delete;
  ~FullRestoreService() override;

  // If the last session was sanitized, skip showing any full restore UI. It is
  // a static function since the pref gets reset before a `FullRestoreService`
  // is created.
  static void SetLastSessionSanitized();

  FullRestoreAppLaunchHandler* app_launch_handler() {
    return app_launch_handler_.get();
  }

  // Initialize the full restore service. |show_notification| indicates whether
  // a full restore notification has been shown.
  void Init(bool& show_notification);

  void OnTransitionedToNewActiveUser(Profile* profile);

  // Launches the browser, When the restore data is loaded, and the user chooses
  // to restore.
  void LaunchBrowserWhenReady();

  void MaybeCloseNotification(bool allow_save = true);

  // Implement the restoration.
  void Restore();

  // SessionObserver:
  void OnSessionStateChanged(session_manager::SessionState state) override;

  void SetAppLaunchHandlerForTesting(
      std::unique_ptr<FullRestoreAppLaunchHandler> app_launch_handler);

 private:
  friend class FullRestoreTestHelper;
  FRIEND_TEST_ALL_PREFIXES(FullRestoreAppLaunchHandlerChromeAppBrowserTest,
                           RestoreChromeApp);
  FRIEND_TEST_ALL_PREFIXES(FullRestoreAppLaunchHandlerArcAppBrowserTest,
                           RestoreArcApp);
  using SessionWindows = std::vector<std::unique_ptr<sessions::SessionWindow>>;
  // Maps window id to an associated session window. We use a map at certain
  // points because:
  //   - The data from the full restore file is in a 2 dimensional vector. The
  //     first one is for apps, and the second one is for windows.
  //   - The data from session restore is a single vector.
  // We build a map to avoid doing a O(n) search each loop of the former.
  using SessionWindowsMap = base::flat_map<int, sessions::SessionWindow*>;

  // KeyedService:
  void Shutdown() override;

  // Returns true if `Init` can be called to show the notification or restore
  // apps. Otherwise, returns false.
  bool CanBeInited() const;

  void InitInformedRestoreContentsData(
      InformedRestoreContentsData::DialogType dialog_type);

  // Shows the restore notification or the informed restore dialog on startup.
  void MaybeShowRestoreDialog(
      InformedRestoreContentsData::DialogType dialog_type,
      bool& out_show_notification);

  // Callback used when the pref |kRestoreAppsAndPagesPrefName| changes.
  void OnPreferenceChanged(const std::string& pref_name);

  void OnAppTerminating();

  // Callbacks for the informed restore dialog buttons.
  void OnDialogRestore();
  void OnDialogCancel();

  // Callbacks run after querying for data from the session service(s).
  // `OnGotSessionAsh` is run after receiving data from either the normal
  // session service or app session service. `OnGotAllSessionsAsh` is run after
  // receiving data from both.
  void OnGotSessionAsh(base::OnceCallback<void(SessionWindows)> callback,
                       SessionWindows session_windows,
                       SessionID active_window_id,
                       bool read_error);
  void OnGotAllSessionsAsh(
      const std::vector<SessionWindows>& all_session_windows);

  // Called when session information is ready to be processed. Constructs the
  // object needed to show the informed restore dialog. It will be passed to ash
  // which will then use its contents to create and display the dialog.
  // `session_windows_map` is the browser info retrieved from session restore.
  void OnSessionInformationReceived(
      const SessionWindowsMap& session_windows_map);

  // Shows the informed restore onboarding dialog when there is no restore data.
  void MaybeShowInformedRestoreOnboarding(bool restore_on);

  raw_ptr<Profile> profile_ = nullptr;
  PrefChangeRegistrar pref_change_registrar_;

  // If the user of `profile_` is not the primary user, and hasn't been the
  // active user yet, don't init to restore. Because if the restore setting is
  // 'Always', the app could be launched directly after restart, and the app
  // windows could be added to the primary user's profile path. This may cause
  // the non-primary user lost some restored windows data.
  //
  // If the non primary user becomes the active user, set `can_be_inited_` as
  // true to init and restore app. Otherwise, if `can_be_inited_` is false for
  // the non primary user, defer the init and app restoration.
  bool can_be_inited_ = false;

  bool is_shut_down_ = false;
  bool close_notification_ = false;

  // Specifies whether it is the first time to run the full restore feature.
  bool first_run_full_restore_ = false;

  std::unique_ptr<NewUserRestorePrefHandler> new_user_pref_handler_;

  // |app_launch_handler_| is responsible for launching apps based on the
  // restore data.
  std::unique_ptr<FullRestoreAppLaunchHandler> app_launch_handler_;

  std::unique_ptr<FullRestoreDataHandler> restore_data_handler_;

  // The contents data that will be presented in the informed restore dialog.
  // Will pass the ownership to post-login controller when start post-login
  // session.
  std::unique_ptr<InformedRestoreContentsData> contents_data_;

  std::unique_ptr<Delegate> delegate_;

  base::CallbackListSubscription on_app_terminating_subscription_;

  // Browser session restore exit type service lock. This is created when the
  // system is restored from crash to help set the browser saving flag.
  std::unique_ptr<ExitTypeService::CrashedLock> crashed_lock_;

  base::WeakPtrFactory<FullRestoreService> weak_ptr_factory_{this};
};

class ScopedRestoreForTesting {
 public:
  ScopedRestoreForTesting();
  ScopedRestoreForTesting(const ScopedRestoreForTesting&) = delete;
  ScopedRestoreForTesting& operator=(const ScopedRestoreForTesting&) = delete;
  ~ScopedRestoreForTesting();
};

}  // namespace ash::full_restore

#endif  // CHROME_BROWSER_ASH_APP_RESTORE_FULL_RESTORE_SERVICE_H_