arc_read_handler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2021 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_ARC_READ_HANDLER_H_
  5. #define COMPONENTS_APP_RESTORE_ARC_READ_HANDLER_H_
  6. #include <map>
  7. #include <set>
  8. #include <utility>
  9. #include "base/component_export.h"
  10. #include "base/files/file_path.h"
  11. #include "components/app_restore/app_restore_utils.h"
  12. namespace aura {
  13. class Window;
  14. }
  15. namespace full_restore {
  16. class FullRestoreReadHandlerTestApi;
  17. }
  18. namespace app_restore {
  19. struct AppLaunchInfo;
  20. struct WindowInfo;
  21. // ArcReadHandler is a helper class for a Delegate to read the app and window
  22. // info of ARC apps, which have special cases, e.g. ARC task creation, ARC
  23. // session id, etc.
  24. //
  25. // Android is responsible for restoring ARC window bounds, so full restore won't
  26. // restore window bounds, but restore activation_index, desk_id, window_state,
  27. // etc, when the widget has initialized by calling OnWidgetInitialized. For
  28. // ghost windows, when the ghost window is created, all window properties are
  29. // restored by ghost window.
  30. //
  31. // Task id is saved as the window id. So only when the task id is received, the
  32. // window can be restored. Task id and window init are two separate threads, so
  33. // there could be 2 scenarios:
  34. // 1. Window is initialized first, then `window` is added to the hidden
  35. // container, and saved in `arc_window_candidates_`. When the task is created,
  36. // OnTaskCreated callback applies the restore window properties, and remove the
  37. // window from the hidden container.
  38. // 2. Task is created first, then Window is initialized. So before `window` is
  39. // initialized, ModifyWidgetParams is called to apply the restore window
  40. // properties, and when `window` is initialized, it can be restored directly.
  41. class COMPONENT_EXPORT(APP_RESTORE) ArcReadHandler {
  42. public:
  43. // A delegate class which allows an owner of ArcReadHandler to have some
  44. // specific behavior.
  45. class Delegate {
  46. public:
  47. virtual ~Delegate() = default;
  48. // Gets the app launch information from `profile_path` for `app_id` and
  49. // `restore_window_id`.
  50. virtual std::unique_ptr<AppLaunchInfo> GetAppLaunchInfo(
  51. const base::FilePath& profile_path,
  52. const std::string& app_id,
  53. int32_t restore_window_id) = 0;
  54. // Gets the window information from `profile_path` for `app_id` and
  55. // `restore_window_id`.
  56. virtual std::unique_ptr<WindowInfo> GetWindowInfo(
  57. const base::FilePath& profile_path,
  58. const std::string& app_id,
  59. int32_t restore_window_id) = 0;
  60. // Removes AppRestoreData from `profile_path` for `app_id` and
  61. // `restore_window_id`.
  62. virtual void RemoveAppRestoreData(const base::FilePath& profile_path,
  63. const std::string& app_id,
  64. int32_t restore_window_id) = 0;
  65. };
  66. ArcReadHandler(const base::FilePath& profile_path, Delegate* delegate);
  67. ArcReadHandler(const ArcReadHandler&) = delete;
  68. ArcReadHandler& operator=(const ArcReadHandler&) = delete;
  69. ~ArcReadHandler();
  70. // Sets |app_id| and |window_id| to |window_id_to_app_id_| to record that
  71. // there is a restore data for |app_id| and |window_id|.
  72. void AddRestoreData(const std::string& app_id, int32_t window_id);
  73. // Add |window| to |arc_window_candidates_|.
  74. void AddArcWindowCandidate(aura::Window* window);
  75. // Invoked when |window| is destroyed.
  76. void OnWindowDestroyed(aura::Window* window);
  77. // Invoked when the task is created for an ARC app.
  78. void OnTaskCreated(const std::string& app_id,
  79. int32_t task_id,
  80. int32_t session_id);
  81. // Invoked when the task is destroyed for an ARC app.
  82. void OnTaskDestroyed(int32_t task_id);
  83. // Returns true if there is restore data for |window_id|, otherwise returns
  84. // false.
  85. bool HasRestoreData(int32_t window_id);
  86. // Gets the ARC app launch information from the full restore file for `app_id`
  87. // and `session_id`.
  88. std::unique_ptr<AppLaunchInfo> GetArcAppLaunchInfo(const std::string& app_id,
  89. int32_t session_id);
  90. // Gets the window information for |restore_window_id|.
  91. std::unique_ptr<WindowInfo> GetWindowInfo(int32_t restore_window_id);
  92. // Returns the restore window id for the ARC app's |task_id|.
  93. int32_t GetArcRestoreWindowIdForTaskId(int32_t task_id);
  94. // Returns the restore window id for the ARC app's `session_id`.
  95. int32_t GetArcRestoreWindowIdForSessionId(int32_t session_id);
  96. // Sets |session_id| for |window_id| to |session_id_to_window_id_|.
  97. // |session_id| is assigned when ARC apps are restored.
  98. void SetArcSessionIdForWindowId(int32_t session_id, int32_t window_id);
  99. private:
  100. friend class full_restore::FullRestoreReadHandlerTestApi;
  101. // Removes AppRestoreData for |restore_window_id|.
  102. void RemoveAppRestoreData(int32_t restore_window_id);
  103. // Finds the window from `arc_window_candidates_` for `task_id`, and remove
  104. // the window from `arc_window_candidates_`.
  105. void UpdateWindowCandidates(int32_t task_id, int32_t restore_window_id);
  106. // The user profile path for ARC app windows.
  107. base::FilePath profile_path_;
  108. // The map from the window id to the app id for ARC app windows. The window id
  109. // is saved in the window property |kRestoreWindowIdKey|.
  110. std::map<int32_t, std::string> window_id_to_app_id_;
  111. // The map from the arc session id to the window id.
  112. std::map<int32_t, int32_t> session_id_to_window_id_;
  113. // The map from the arc task id to the window id.
  114. std::map<int32_t, int32_t> task_id_to_window_id_;
  115. // ARC app tasks could be created after the window initialized.
  116. // |arc_window_candidates_| is used to record those initialized ARC app
  117. // windows, whose tasks have not been created. Once the task for the window is
  118. // created, the window is removed from |arc_window_candidates_|.
  119. std::set<aura::Window*> arc_window_candidates_;
  120. // ARC app tasks could be created before the window initialized.
  121. // `not_restored_task_ids_` is used to record tasks not created by the restore
  122. // process. Once the window is created for the task, the window can be removed
  123. // from the hidden container.
  124. std::set<int32_t> not_restored_task_ids_;
  125. Delegate* delegate_;
  126. };
  127. } // namespace app_restore
  128. #endif // COMPONENTS_APP_RESTORE_ARC_READ_HANDLER_H_