lacros_read_handler.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "components/app_restore/lacros_read_handler.h"
  5. #include "ash/constants/app_types.h"
  6. #include "components/app_restore/app_restore_info.h"
  7. #include "components/app_restore/app_restore_utils.h"
  8. #include "components/app_restore/full_restore_read_handler.h"
  9. #include "components/app_restore/window_info.h"
  10. #include "components/app_restore/window_properties.h"
  11. #include "components/sessions/core/session_id.h"
  12. #include "ui/aura/client/aura_constants.h"
  13. #include "ui/aura/window.h"
  14. namespace app_restore {
  15. LacrosReadHandler::LacrosReadHandler(const base::FilePath& profile_path)
  16. : profile_path_(profile_path) {}
  17. LacrosReadHandler::~LacrosReadHandler() = default;
  18. void LacrosReadHandler::OnWindowInitialized(aura::Window* window) {
  19. // TODO(sophiewen): Test this in full_restore_read_and_save_unittest.
  20. if (!IsLacrosWindow(window))
  21. return;
  22. int32_t restored_browser_session_id =
  23. window->GetProperty(app_restore::kRestoreWindowIdKey);
  24. auto it = window_to_window_data_.find(window);
  25. if (it != window_to_window_data_.end() &&
  26. it->second.restore_window_id == restored_browser_session_id) {
  27. // `window` has been restored.
  28. return;
  29. }
  30. window_to_window_data_[window].restore_window_id =
  31. restored_browser_session_id;
  32. // If there is a restore data, set the app id to restore `window`.
  33. auto restore_it =
  34. restore_window_id_to_app_id_.find(restored_browser_session_id);
  35. if (restore_it != restore_window_id_to_app_id_.end())
  36. window_to_window_data_[window].app_id = restore_it->second;
  37. // If `window` is added to a hidden container, call UpdateWindow to restore
  38. // and remove `window` from the hidden container.
  39. if (base::Contains(window_candidates_, window))
  40. UpdateWindow(window);
  41. }
  42. void LacrosReadHandler::AddRestoreData(const std::string& app_id,
  43. int32_t window_id) {
  44. restore_window_id_to_app_id_[window_id] = app_id;
  45. }
  46. void LacrosReadHandler::OnAppWindowAdded(const std::string& app_id,
  47. const std::string& lacros_window_id) {
  48. lacros_window_id_to_app_id_[lacros_window_id] = app_id;
  49. auto window_it =
  50. std::find_if(window_candidates_.begin(), window_candidates_.end(),
  51. [lacros_window_id](aura::Window* window) {
  52. return GetLacrosWindowId(window) == lacros_window_id;
  53. });
  54. if (window_it == window_candidates_.end())
  55. return;
  56. SetWindowData(
  57. *window_it, app_id,
  58. full_restore::FullRestoreReadHandler::GetInstance()->FetchRestoreWindowId(
  59. app_id));
  60. UpdateWindow(*window_it);
  61. }
  62. void LacrosReadHandler::OnAppWindowRemoved(
  63. const std::string& app_id,
  64. const std::string& lacros_window_id) {
  65. lacros_window_id_to_app_id_.erase(lacros_window_id);
  66. }
  67. void LacrosReadHandler::OnWindowAddedToRootWindow(aura::Window* window) {
  68. if (!window->GetProperty(app_restore::kParentToHiddenContainerKey)) {
  69. // If `window` has been removed from the hidden container, we don't need to
  70. // restore it, because it has been restored.
  71. return;
  72. }
  73. auto window_it = window_to_window_data_.find(window);
  74. if (window_it != window_to_window_data_.end()) {
  75. // We have received the restore window, so restore and remove `window` from
  76. // the hidden container.
  77. UpdateWindow(window);
  78. return;
  79. }
  80. const auto lacros_window_id = GetLacrosWindowId(window);
  81. auto it = lacros_window_id_to_app_id_.find(lacros_window_id);
  82. if (it != lacros_window_id_to_app_id_.end()) {
  83. // We have received the app id for the Chrome app window, so restore and
  84. // remove `window` from the hidden container.
  85. SetWindowData(window, it->second,
  86. full_restore::FullRestoreReadHandler::GetInstance()
  87. ->FetchRestoreWindowId(it->second));
  88. UpdateWindow(window);
  89. return;
  90. }
  91. // We haven't received the restore window id, add `window` to
  92. // `window_candidates_` to wait for the restore window id.
  93. window_candidates_.insert(window);
  94. }
  95. void LacrosReadHandler::OnWindowDestroyed(aura::Window* window) {
  96. window_candidates_.erase(window);
  97. window_to_window_data_.erase(window);
  98. }
  99. int32_t LacrosReadHandler::GetLacrosRestoreWindowId(
  100. const std::string& lacros_window_id) const {
  101. auto it = lacros_window_id_to_app_id_.find(lacros_window_id);
  102. // Set restore window id as 0 to prevent the window is added to the hidden
  103. // container. Windows restoration will be done by exo with another method.
  104. return it == lacros_window_id_to_app_id_.end()
  105. ? 0
  106. : full_restore::FullRestoreReadHandler::GetInstance()
  107. ->FetchRestoreWindowId(it->second);
  108. }
  109. void LacrosReadHandler::SetWindowData(aura::Window* const window,
  110. const std::string& app_id,
  111. int32_t restore_window_id) {
  112. if (base::Contains(restore_window_id_to_app_id_, restore_window_id))
  113. window_to_window_data_[window].app_id = app_id;
  114. window_to_window_data_[window].restore_window_id = restore_window_id;
  115. }
  116. void LacrosReadHandler::UpdateWindow(aura::Window* const window) {
  117. auto it = window_to_window_data_.find(window);
  118. if (it != window_to_window_data_.end() && !it->second.app_id.empty()) {
  119. // `window` is restored, so set the window property `kRestoreWindowIdKey`
  120. // and `kWindowInfoKey` to restore `window`.
  121. window->SetProperty(kRestoreWindowIdKey, it->second.restore_window_id);
  122. auto window_info =
  123. full_restore::FullRestoreReadHandler::GetInstance()->GetWindowInfo(
  124. profile_path_, it->second.app_id, it->second.restore_window_id);
  125. if (window_info)
  126. ApplyProperties(window_info.get(), window);
  127. restore_window_id_to_app_id_.erase(it->second.restore_window_id);
  128. }
  129. // Remove the window from the hidden container.
  130. app_restore::AppRestoreInfo::GetInstance()->OnParentWindowToValidContainer(
  131. window);
  132. window_candidates_.erase(window);
  133. }
  134. } // namespace app_restore