full_restore_read_handler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_
  5. #define COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_
  6. #include <memory>
  7. #include <utility>
  8. #include "base/callback.h"
  9. #include "base/component_export.h"
  10. #include "base/containers/flat_map.h"
  11. #include "base/files/file_path.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/scoped_multi_source_observation.h"
  14. #include "base/scoped_observation.h"
  15. #include "base/time/time.h"
  16. #include "components/app_restore/app_restore_arc_info.h"
  17. #include "components/app_restore/arc_read_handler.h"
  18. #include "components/app_restore/full_restore_utils.h"
  19. #include "ui/aura/env.h"
  20. #include "ui/aura/env_observer.h"
  21. #include "ui/aura/window.h"
  22. #include "ui/aura/window_observer.h"
  23. namespace app_restore {
  24. struct AppLaunchInfo;
  25. class LacrosReadHandler;
  26. class RestoreData;
  27. struct WindowInfo;
  28. } // namespace app_restore
  29. namespace ash {
  30. namespace full_restore {
  31. class FullRestoreAppLaunchHandlerBrowserTest;
  32. class FullRestoreAppLaunchHandlerSystemWebAppsBrowserTest;
  33. class FullRestoreServiceTestHavingFullRestoreFile;
  34. } // namespace full_restore
  35. } // namespace ash
  36. namespace full_restore {
  37. class FullRestoreFileHandler;
  38. // FullRestoreReadHandler is responsible for reading |RestoreData| from the full
  39. // restore data file. FullRestoreReadHandler runs on the main thread and creates
  40. // FullRestoreFileHandler (which runs on a background task runner) for the
  41. // actual reading.
  42. class COMPONENT_EXPORT(APP_RESTORE) FullRestoreReadHandler
  43. : public aura::EnvObserver,
  44. public aura::WindowObserver,
  45. public app_restore::ArcReadHandler::Delegate,
  46. public app_restore::AppRestoreArcInfo::Observer {
  47. public:
  48. // The callback function to get the restore data when the reading operation is
  49. // done.
  50. using Callback =
  51. base::OnceCallback<void(std::unique_ptr<app_restore::RestoreData>)>;
  52. static FullRestoreReadHandler* GetInstance();
  53. FullRestoreReadHandler();
  54. FullRestoreReadHandler(const FullRestoreReadHandler&) = delete;
  55. FullRestoreReadHandler& operator=(const FullRestoreReadHandler&) = delete;
  56. ~FullRestoreReadHandler() override;
  57. app_restore::ArcReadHandler* arc_read_handler() {
  58. return arc_read_handler_.get();
  59. }
  60. // aura::EnvObserver:
  61. void OnWindowInitialized(aura::Window* window) override;
  62. // aura::WindowObserver:
  63. void OnWindowAddedToRootWindow(aura::Window* window) override;
  64. void OnWindowDestroyed(aura::Window* window) override;
  65. // app_restore::ArcReadHandler::Delegate:
  66. std::unique_ptr<app_restore::AppLaunchInfo> GetAppLaunchInfo(
  67. const base::FilePath& profile_path,
  68. const std::string& app_id,
  69. int32_t restore_window_id) override;
  70. std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(
  71. const base::FilePath& profile_path,
  72. const std::string& app_id,
  73. int32_t restore_window_id) override;
  74. void RemoveAppRestoreData(const base::FilePath& profile_path,
  75. const std::string& app_id,
  76. int32_t restore_window_id) override;
  77. // app_restore::AppRestoreArcInfo::Observer:
  78. void OnTaskCreated(const std::string& app_id,
  79. int32_t task_id,
  80. int32_t session_id) override;
  81. void OnTaskDestroyed(int32_t task_id) override;
  82. // Invoked when an Chrome app Lacros window is created. `app_id` is the
  83. // AppService id, and `window_id` is the wayland app_id property for the
  84. // window.
  85. void OnLacrosChromeAppWindowAdded(const std::string& app_id,
  86. const std::string& window_id);
  87. // Invoked when an Chrome app Lacros window is removed. `app_id` is the
  88. // AppService id, and `window_id` is the wayland app_id property for the
  89. // window.
  90. void OnLacrosChromeAppWindowRemoved(const std::string& app_id,
  91. const std::string& window_id);
  92. void SetPrimaryProfilePath(const base::FilePath& profile_path);
  93. void SetActiveProfilePath(const base::FilePath& profile_path);
  94. // Sets whether we should check the restore data for `profile_path`. If the
  95. // user selects `Restore`, then we should check the restore data for restored
  96. // windows. Otherwise, we don't need to to check the restore data.
  97. void SetCheckRestoreData(const base::FilePath& profile_path);
  98. // Reads the restore data from |profile_path| on a background task runner, and
  99. // calls |callback| when the reading operation is done.
  100. void ReadFromFile(const base::FilePath& profile_path, Callback callback);
  101. // Modifies the restore data from |profile_path| to set the next restore
  102. // window id for the given chrome app |app_id|.
  103. void SetNextRestoreWindowIdForChromeApp(const base::FilePath& profile_path,
  104. const std::string& app_id);
  105. // Removes app launching and app windows for an app with the given |app_id|
  106. // from |profile_path_to_restore_data_| for |profile_path| .
  107. void RemoveApp(const base::FilePath& profile_path, const std::string& app_id);
  108. // Returns true if there are app type browsers from the full restore file.
  109. // Otherwise, returns false.
  110. bool HasAppTypeBrowser(const base::FilePath& profile_path);
  111. // Returns true if there are normal browsers (non app type browser) from the
  112. // full restore file. Otherwise, returns false.
  113. bool HasBrowser(const base::FilePath& profile_path);
  114. // Returns true if there is a window info for |restore_window_id| from the
  115. // full restore file. Otherwise, returns false. This interface can't be used
  116. // for Arc app windows.
  117. bool HasWindowInfo(int32_t restore_window_id);
  118. // Gets the window information for |window|.
  119. std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(aura::Window* window);
  120. // Gets the window information for the active profile associated with
  121. // `restore_window_id`.
  122. std::unique_ptr<app_restore::WindowInfo> GetWindowInfoForActiveProfile(
  123. int32_t restore_window_id);
  124. // Gets the ARC app launch information from the full restore file for `app_id`
  125. // and `session_id`.
  126. std::unique_ptr<app_restore::AppLaunchInfo> GetArcAppLaunchInfo(
  127. const std::string& app_id,
  128. int32_t session_id);
  129. // Fetches the restore id for the window from RestoreData for the given
  130. // |app_id|. |app_id| should be a Chrome app id.
  131. int32_t FetchRestoreWindowId(const std::string& app_id);
  132. // Returns the restore window id for the ARC app's |task_id|.
  133. int32_t GetArcRestoreWindowIdForTaskId(int32_t task_id);
  134. // Returns the restore window id for the ARC app's |session_id|.
  135. int32_t GetArcRestoreWindowIdForSessionId(int32_t session_id);
  136. // Returns the restore window id for the Lacros window with
  137. // `lacros_window_id`.
  138. int32_t GetLacrosRestoreWindowId(const std::string& lacros_window_id) const;
  139. // Sets |arc session id| for |window_id| to |arc_session_id_to_window_id_|.
  140. // |arc session id| is assigned when ARC apps are restored.
  141. void SetArcSessionIdForWindowId(int32_t arc_session_id, int32_t window_id);
  142. // Called when full restore launching is about to begin. Saves the start time
  143. // in `profile_path_to_start_time_data_`.
  144. void SetStartTimeForProfile(const base::FilePath& profile_path);
  145. // Returns true if full restore launching is thought to be underway on
  146. // `active_profile_path_`.
  147. bool IsFullRestoreRunning() const;
  148. void AddChromeBrowserLaunchInfoForTesting(const base::FilePath& profile_path);
  149. private:
  150. friend class ash::full_restore::FullRestoreAppLaunchHandlerBrowserTest;
  151. friend class ash::full_restore::
  152. FullRestoreAppLaunchHandlerSystemWebAppsBrowserTest;
  153. friend class ash::full_restore::FullRestoreServiceTestHavingFullRestoreFile;
  154. friend class FullRestoreReadHandlerTestApi;
  155. // Gets the window information for |restore_window_id| for browser windows and
  156. // Chrome app windows only. This interface can't be used for ARC app windows.
  157. std::unique_ptr<app_restore::WindowInfo> GetWindowInfo(
  158. int32_t restore_window_id);
  159. // Returns true if ARC restore launching is thought to be underway on
  160. // `primary_profile_path_`.
  161. bool IsArcRestoreRunning() const;
  162. // Returns true if Lacros restore launching is thought to be underway on
  163. // `primary_profile_path_`.
  164. bool IsLacrosRestoreRunning() const;
  165. // Invoked when reading the restore data from |profile_path| is finished, and
  166. // calls |callback| to notify that the reading operation is done.
  167. void OnGetRestoreData(const base::FilePath& profile_path,
  168. Callback callback,
  169. std::unique_ptr<app_restore::RestoreData>);
  170. // Removes AppRestoreData for |restore_window_id|.
  171. void RemoveAppRestoreData(int32_t restore_window_id);
  172. app_restore::RestoreData* GetRestoreData(const base::FilePath& profile_path);
  173. // The primary user profile path.
  174. base::FilePath primary_profile_path_;
  175. // The current active user profile path.
  176. base::FilePath active_profile_path_;
  177. // The restore data read from the full restore files.
  178. base::flat_map<base::FilePath, std::unique_ptr<app_restore::RestoreData>>
  179. profile_path_to_restore_data_;
  180. // The map from the window id to the full restore file path and the
  181. // app id. The window id is saved in the window property
  182. // |kRestoreWindowIdKey|. This map is used to find the file path and the app
  183. // id when get the window info. This map is not used for ARC app windows.
  184. base::flat_map<int32_t, std::pair<base::FilePath, std::string>>
  185. window_id_to_app_restore_info_;
  186. // The start time of full restore for each profile. There won't be an entry if
  187. // full restore hasn't started for the profile.
  188. base::flat_map<base::FilePath, base::TimeTicks>
  189. profile_path_to_start_time_data_;
  190. std::unique_ptr<app_restore::ArcReadHandler> arc_read_handler_;
  191. std::unique_ptr<app_restore::LacrosReadHandler> lacros_read_handler_;
  192. // Records whether we need to check the restore data for the profile path. If
  193. // the profile path is recorded, we should check the restore data. Otherwise,
  194. // we don't need to check the restore data, because the restore process hasn't
  195. // started yet.
  196. std::set<base::FilePath> should_check_restore_data_;
  197. base::ScopedObservation<aura::Env, aura::EnvObserver> env_observer_{this};
  198. base::ScopedMultiSourceObservation<aura::Window, aura::WindowObserver>
  199. observed_windows_{this};
  200. base::ScopedObservation<app_restore::AppRestoreArcInfo,
  201. app_restore::AppRestoreArcInfo::Observer>
  202. arc_info_observer_{this};
  203. base::WeakPtrFactory<FullRestoreReadHandler> weak_factory_{this};
  204. };
  205. } // namespace full_restore
  206. #endif // COMPONENTS_APP_RESTORE_FULL_RESTORE_READ_HANDLER_H_