File: full_restore_read_handler.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 (240 lines) | stat: -rw-r--r-- 9,679 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// 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 COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_
#define COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_

#include <memory>
#include <utility>

#include "base/component_export.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "components/app_restore/app_restore_arc_info.h"
#include "components/app_restore/arc_read_handler.h"
#include "components/app_restore/full_restore_utils.h"
#include "ui/aura/env.h"
#include "ui/aura/env_observer.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"

namespace app_restore {
struct AppLaunchInfo;
struct AppRestoreData;
class RestoreData;
struct WindowInfo;
}  // namespace app_restore

namespace ash {
class AppLaunchInfoSaveWaiter;
namespace full_restore {
class FullRestoreAppLaunchHandlerTestBase;
class FullRestoreServiceTestHavingFullRestoreFile;
}  // namespace full_restore
}  // namespace ash

namespace full_restore {

class FullRestoreFileHandler;

// FullRestoreReadHandler is responsible for reading |RestoreData| from the full
// restore data file. FullRestoreReadHandler runs on the main thread and creates
// FullRestoreFileHandler (which runs on a background task runner) for the
// actual reading.
class COMPONENT_EXPORT(APP_RESTORE) FullRestoreReadHandler
    : public aura::EnvObserver,
      public aura::WindowObserver,
      public app_restore::ArcReadHandler::Delegate,
      public app_restore::AppRestoreArcInfo::Observer {
 public:
  // The callback function to get the restore data when the reading operation is
  // done.
  using Callback =
      base::OnceCallback<void(std::unique_ptr<app_restore::RestoreData>)>;

  static FullRestoreReadHandler* GetInstance();

  FullRestoreReadHandler();
  FullRestoreReadHandler(const FullRestoreReadHandler&) = delete;
  FullRestoreReadHandler& operator=(const FullRestoreReadHandler&) = delete;
  ~FullRestoreReadHandler() override;

  app_restore::ArcReadHandler* arc_read_handler() {
    return arc_read_handler_.get();
  }

  // aura::EnvObserver:
  void OnWindowInitialized(aura::Window* window) override;

  // aura::WindowObserver:
  void OnWindowDestroyed(aura::Window* window) override;

  // app_restore::ArcReadHandler::Delegate:
  std::unique_ptr<app_restore::AppLaunchInfo> GetAppLaunchInfo(
      const base::FilePath& profile_path,
      const std::string& app_id,
      int32_t restore_window_id) override;
  std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(
      const base::FilePath& profile_path,
      const std::string& app_id,
      int32_t restore_window_id) override;
  void RemoveAppRestoreData(const base::FilePath& profile_path,
                            const std::string& app_id,
                            int32_t restore_window_id) override;

  // app_restore::AppRestoreArcInfo::Observer:
  void OnTaskCreated(const std::string& app_id,
                     int32_t task_id,
                     int32_t session_id) override;
  void OnTaskDestroyed(int32_t task_id) override;

  void SetPrimaryProfilePath(const base::FilePath& profile_path);

  void SetActiveProfilePath(const base::FilePath& profile_path);

  // Sets whether we should check the restore data for `profile_path`. If the
  // user selects `Restore`, then we should check the restore data for restored
  // windows. Otherwise, we don't need to to check the restore data.
  void SetCheckRestoreData(const base::FilePath& profile_path);

  // Reads the restore data from |profile_path| on a background task runner, and
  // calls |callback| when the reading operation is done.
  void ReadFromFile(const base::FilePath& profile_path, Callback callback);

  // Modifies the restore data from |profile_path| to set the next restore
  // window id for the given chrome app |app_id|.
  void SetNextRestoreWindowIdForChromeApp(const base::FilePath& profile_path,
                                          const std::string& app_id);

  // Removes app launching and app windows for an app with the given |app_id|
  // from |profile_path_to_restore_data_| for |profile_path| .
  void RemoveApp(const base::FilePath& profile_path, const std::string& app_id);

  // Returns true if there are app type browsers from the full restore file.
  // Otherwise, returns false.
  bool HasAppTypeBrowser(const base::FilePath& profile_path);

  // Returns true if there are normal browsers (non app type browser) from the
  // full restore file. Otherwise, returns false.
  bool HasBrowser(const base::FilePath& profile_path);

  // Returns true if there is a window info for |restore_window_id| from the
  // full restore file. Otherwise, returns false. This interface can't be used
  // for Arc app windows.
  bool HasWindowInfo(int32_t restore_window_id);

  // Gets the window information for |window|.
  std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(aura::Window* window);

  // Gets the window information for the active profile associated with
  // `restore_window_id`.
  std::unique_ptr<app_restore::WindowInfo> GetWindowInfoForActiveProfile(
      int32_t restore_window_id);

  // Gets the ARC app launch information from the full restore file for `app_id`
  // and `session_id`.
  std::unique_ptr<app_restore::AppLaunchInfo> GetArcAppLaunchInfo(
      const std::string& app_id,
      int32_t session_id);

  // Fetches the restore id for the window from RestoreData for the given
  // |app_id|. |app_id| should be a Chrome app id.
  int32_t FetchRestoreWindowId(const std::string& app_id);

  // Returns the restore window id for the ARC app's |task_id|.
  int32_t GetArcRestoreWindowIdForTaskId(int32_t task_id);

  // Returns the restore window id for the ARC app's |session_id|.
  int32_t GetArcRestoreWindowIdForSessionId(int32_t session_id);

  // Sets |arc session id| for |window_id| to |arc_session_id_to_window_id_|.
  // |arc session id| is assigned when ARC apps are restored.
  void SetArcSessionIdForWindowId(int32_t arc_session_id, int32_t window_id);

  // Called when full restore launching is about to begin. Saves the start time
  // in `profile_path_to_start_time_data_`.
  void SetStartTimeForProfile(const base::FilePath& profile_path);

  // Returns true if full restore launching is thought to be underway on
  // `active_profile_path_`.
  bool IsFullRestoreRunning() const;

  void AddChromeBrowserLaunchInfoForTesting(const base::FilePath& profile_path);

 private:
  friend class ash::AppLaunchInfoSaveWaiter;
  friend class ash::full_restore::FullRestoreAppLaunchHandlerTestBase;
  friend class ash::full_restore::FullRestoreServiceTestHavingFullRestoreFile;
  friend class FullRestoreReadHandlerTestApi;

  // Gets the window information for |restore_window_id| for browser windows and
  // Chrome app windows only. This interface can't be used for ARC app windows.
  std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(
      int32_t restore_window_id);

  // Returns true if ARC restore launching is thought to be underway on
  // `primary_profile_path_`.
  bool IsArcRestoreRunning() const;

  // Invoked when reading the restore data from |profile_path| is finished, and
  // calls |callback| to notify that the reading operation is done.
  void OnGetRestoreData(const base::FilePath& profile_path,
                        Callback callback,
                        std::unique_ptr<app_restore::RestoreData>);

  // Removes AppRestoreData for |restore_window_id|.
  void RemoveAppRestoreData(int32_t restore_window_id);

  app_restore::RestoreData* GetRestoreData(const base::FilePath& profile_path);

  // The primary user profile path.
  base::FilePath primary_profile_path_;

  // The current active user profile path.
  base::FilePath active_profile_path_;

  // The restore data read from the full restore files.
  base::flat_map<base::FilePath, std::unique_ptr<app_restore::RestoreData>>
      profile_path_to_restore_data_;

  // The map from the window id to the full restore file path and the
  // app id. The window id is saved in the window property
  // |kRestoreWindowIdKey|. This map is used to find the file path and the app
  // id when get the window info. This map is not used for ARC app windows.
  base::flat_map<int32_t, std::pair<base::FilePath, std::string>>
      window_id_to_app_restore_info_;

  // The start time of full restore for each profile. There won't be an entry if
  // full restore hasn't started for the profile.
  base::flat_map<base::FilePath, base::TimeTicks>
      profile_path_to_start_time_data_;

  std::unique_ptr<app_restore::ArcReadHandler> arc_read_handler_;

  // Records whether we need to check the restore data for the profile path. If
  // the profile path is recorded, we should check the restore data. Otherwise,
  // we don't need to check the restore data, because the restore process hasn't
  // started yet.
  std::set<base::FilePath> should_check_restore_data_;

  base::ScopedObservation<aura::Env, aura::EnvObserver> env_observer_{this};

  base::ScopedMultiSourceObservation<aura::Window, aura::WindowObserver>
      observed_windows_{this};

  base::ScopedObservation<app_restore::AppRestoreArcInfo,
                          app_restore::AppRestoreArcInfo::Observer>
      arc_info_observer_{this};

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

}  // namespace full_restore

#endif  // COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_