lacros_read_handler.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2022 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_LACROS_READ_HANDLER_H_
  5. #define COMPONENTS_APP_RESTORE_LACROS_READ_HANDLER_H_
  6. #include <map>
  7. #include <set>
  8. #include "base/component_export.h"
  9. #include "base/files/file_path.h"
  10. namespace aura {
  11. class Window;
  12. }
  13. namespace app_restore {
  14. // LacrosSaveHandler is a helper class for FullRestoreReadHandler to restore
  15. // Lacros windows.
  16. //
  17. // For Lacros browser window, the restored browser session id is used as the
  18. // restore window id. So only when the restored browser session id is received
  19. // via mojom calls, the window can be restored. OnLacrosBrowserWindowAdded is
  20. // called when both `window` is initialized, and the restored browser session id
  21. // is received. So there could be 2 scenarios:
  22. //
  23. // 1. `window` is initialized first, then `window` is added to the hidden
  24. // container, and OnWindowAddedToRootWindow is called to save `window` in
  25. // `window_candidates_` to wait for the restored browser session id. When
  26. // OnLacrosBrowserWindowAdded is called, call UpdateWindow to apply the restore
  27. // window properties, and remove `window` from the hidden container.
  28. //
  29. // 2. The restored browser session id is received first, then
  30. // OnLacrosBrowserWindowAdded is called when `window` is initialized. We have to
  31. // wait for the OnWindowAddedToRootWindow callback, because `window`'s root is
  32. // not set yet in the OnWindowInitialized/OnLacrosBrowserWindowAdded callback,
  33. // and we can't remove `window` from the hidden container. When
  34. // OnWindowAddedToRootWindow is called, `window` can be restored and removed
  35. // from the hidden container.
  36. //
  37. // TODO(crbug.com/1239984): Restore Lacros windows.
  38. class COMPONENT_EXPORT(APP_RESTORE) LacrosReadHandler {
  39. public:
  40. LacrosReadHandler(const base::FilePath& profile_path);
  41. LacrosReadHandler(const LacrosReadHandler&) = delete;
  42. LacrosReadHandler& operator=(const LacrosReadHandler&) = delete;
  43. ~LacrosReadHandler();
  44. // Invoked when `window` is initialized.
  45. void OnWindowInitialized(aura::Window* window);
  46. // Sets `app_id` and `window_id` to `restore_window_id_to_app_id_` to record
  47. // that there is a restore data for `app_id` and `window_id`.
  48. void AddRestoreData(const std::string& app_id, int32_t window_id);
  49. // Invoked when an Chrome app Lacros window is created. `app_id` is the
  50. // AppService id, and `window_id` is the wayland app_id property for the
  51. // window.
  52. void OnAppWindowAdded(const std::string& app_id,
  53. const std::string& lacros_window_id);
  54. // Invoked when an Chrome app Lacros window is removed. `app_id` is the
  55. // AppService id, and `window_id` is the wayland app_id property for the
  56. // window.
  57. void OnAppWindowRemoved(const std::string& app_id,
  58. const std::string& lacros_window_id);
  59. // Invoked when `window` is added to the root window.
  60. void OnWindowAddedToRootWindow(aura::Window* window);
  61. // Invoked when `window` is destroyed.
  62. void OnWindowDestroyed(aura::Window* window);
  63. // Returns the restore window id for the Lacros window with
  64. // `lacros_window_id`.
  65. int32_t GetLacrosRestoreWindowId(const std::string& lacros_window_id) const;
  66. private:
  67. struct WindowData {
  68. std::string app_id;
  69. int32_t restore_window_id = -1;
  70. };
  71. // Sets `app_id` and `restore_window_id` for `window` in
  72. // `window_to_window_data_`. If there is no restore data for
  73. // `restore_window_id`, `app_id` won't be set to skip setting the restore data
  74. // for `window`.
  75. void SetWindowData(aura::Window* const window,
  76. const std::string& app_id,
  77. int32_t restore_window_id);
  78. // Sets `kRestoreWindowIdKey` and `kWindowInfoKey` to restore and remove
  79. // `window from the hidden container`.
  80. void UpdateWindow(aura::Window* const window);
  81. // The user profile path for Lacros windows.
  82. base::FilePath profile_path_;
  83. // The map from the restore window id to the app id for Lacros windows.
  84. std::map<int32_t, std::string> restore_window_id_to_app_id_;
  85. // The map from the window to the app id and the restore window id.
  86. std::map<aura::Window*, WindowData> window_to_window_data_;
  87. // The mojom call to forward the restore window id could be received later
  88. // than the OnWindowAddedToRootWindow callback. So add windows to
  89. // `window_candidates_` to record window candidates. Once the restore window
  90. // id is received, the window can be restored and removed from the hidden
  91. // container.
  92. std::set<aura::Window*> window_candidates_;
  93. // The map from the lacros window id to the app id for Chrome app windows.
  94. std::map<std::string, std::string> lacros_window_id_to_app_id_;
  95. };
  96. } // namespace app_restore
  97. #endif // COMPONENTS_APP_RESTORE_LACROS_READ_HANDLER_H_