lacros_save_handler.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_save_handler.h"
  5. #include <memory>
  6. #include "components/app_constants/constants.h"
  7. #include "components/app_restore/app_launch_info.h"
  8. #include "components/app_restore/app_restore_utils.h"
  9. #include "components/app_restore/full_restore_save_handler.h"
  10. #include "components/app_restore/window_info.h"
  11. #include "components/app_restore/window_properties.h"
  12. #include "ui/aura/window.h"
  13. namespace full_restore {
  14. LacrosSaveHandler::LacrosSaveHandler(const base::FilePath& profile_path)
  15. : profile_path_(profile_path) {}
  16. LacrosSaveHandler::~LacrosSaveHandler() = default;
  17. void LacrosSaveHandler::OnWindowInitialized(aura::Window* window) {
  18. const std::string lacros_window_id = app_restore::GetLacrosWindowId(window);
  19. std::string app_id;
  20. int32_t window_id = ++window_id_;
  21. std::unique_ptr<app_restore::AppLaunchInfo> app_launch_info;
  22. auto it = lacros_window_id_to_app_id_.find(lacros_window_id);
  23. if (it != lacros_window_id_to_app_id_.end()) {
  24. app_id = it->second;
  25. app_launch_info = FullRestoreSaveHandler::GetInstance()->FetchAppLaunchInfo(
  26. profile_path_, app_id);
  27. if (!app_launch_info)
  28. return;
  29. app_launch_info->window_id = window_id;
  30. } else {
  31. app_id = app_constants::kLacrosAppId;
  32. app_launch_info =
  33. std::make_unique<app_restore::AppLaunchInfo>(app_id, window_id);
  34. }
  35. window_candidates_[lacros_window_id].app_id = app_id;
  36. window_candidates_[lacros_window_id].window_id = window_id;
  37. // Don't overwrite window info if `window_id` was already saved by
  38. // OnAppWindowAdded.
  39. if (it == lacros_window_id_to_app_id_.end()) {
  40. // TODO(sophiewen): Move logic to OnWindowInitialized instead of calling
  41. // OnBrowserWindowAdded.
  42. OnBrowserWindowAdded(window, false);
  43. return;
  44. }
  45. FullRestoreSaveHandler::GetInstance()->AddAppLaunchInfo(
  46. profile_path_, std::move(app_launch_info));
  47. }
  48. void LacrosSaveHandler::OnWindowDestroyed(aura::Window* window) {
  49. const std::string lacros_window_id = app_restore::GetLacrosWindowId(window);
  50. lacros_window_id_to_app_id_.erase(lacros_window_id);
  51. auto it = window_candidates_.find(lacros_window_id);
  52. if (it == window_candidates_.end())
  53. return;
  54. FullRestoreSaveHandler::GetInstance()->RemoveAppRestoreData(
  55. profile_path_, it->second.app_id, it->second.window_id);
  56. window_candidates_.erase(it);
  57. }
  58. void LacrosSaveHandler::OnBrowserWindowAdded(aura::Window* const window,
  59. bool is_browser_app) {
  60. const std::string lacros_window_id = app_restore::GetLacrosWindowId(window);
  61. std::unique_ptr<app_restore::WindowInfo> window_info;
  62. auto* save_handler = FullRestoreSaveHandler::GetInstance();
  63. DCHECK(save_handler);
  64. uint32_t browser_session_id =
  65. static_cast<uint32_t>(window->GetProperty(app_restore::kWindowIdKey));
  66. auto it = window_candidates_.find(lacros_window_id);
  67. if (it != window_candidates_.end() &&
  68. it->second.window_id != browser_session_id) {
  69. // If the window has been created and saved using different window id, get
  70. // the current window info, then remove the restore data for the old window
  71. // id, and re-save the restore data with the new `browser_session_id`.
  72. window_info = save_handler->GetWindowInfo(profile_path_, it->second.app_id,
  73. it->second.window_id);
  74. save_handler->RemoveAppRestoreData(profile_path_, it->second.app_id,
  75. it->second.window_id);
  76. }
  77. window_candidates_[lacros_window_id].app_id = app_constants::kLacrosAppId;
  78. window_candidates_[lacros_window_id].window_id = browser_session_id;
  79. std::unique_ptr<app_restore::AppLaunchInfo> app_launch_info =
  80. std::make_unique<app_restore::AppLaunchInfo>(app_constants::kLacrosAppId,
  81. browser_session_id);
  82. app_launch_info->app_type_browser = is_browser_app;
  83. save_handler->AddAppLaunchInfo(profile_path_, std::move(app_launch_info));
  84. if (window_info) {
  85. save_handler->ModifyWindowInfo(profile_path_, app_constants::kLacrosAppId,
  86. browser_session_id, *window_info);
  87. }
  88. }
  89. void LacrosSaveHandler::OnAppWindowAdded(const std::string& app_id,
  90. const std::string& lacros_window_id) {
  91. auto it = window_candidates_.find(lacros_window_id);
  92. if (it == window_candidates_.end()) {
  93. // If the window is not created yet, save the app id to
  94. // `lacros_window_id_to_app_id_` to wait for the window.
  95. lacros_window_id_to_app_id_[lacros_window_id] = app_id;
  96. return;
  97. }
  98. // If the window has been created, get the app launch info and the current
  99. // window info, then remove the restore data for lacros browser app id, and
  100. // re-save the restore data for the Chrome app id.
  101. auto* save_handler = FullRestoreSaveHandler::GetInstance();
  102. DCHECK(save_handler);
  103. auto window_info = save_handler->GetWindowInfo(
  104. profile_path_, it->second.app_id, it->second.window_id);
  105. save_handler->RemoveAppRestoreData(profile_path_, it->second.app_id,
  106. it->second.window_id);
  107. it->second.app_id = app_id;
  108. auto app_launch_info =
  109. save_handler->FetchAppLaunchInfo(profile_path_, app_id);
  110. if (!app_launch_info)
  111. return;
  112. app_launch_info->window_id = it->second.window_id;
  113. save_handler->AddAppLaunchInfo(profile_path_, std::move(app_launch_info));
  114. save_handler->ModifyWindowInfo(profile_path_, app_id, it->second.window_id,
  115. *window_info);
  116. }
  117. void LacrosSaveHandler::OnAppWindowRemoved(
  118. const std::string& app_id,
  119. const std::string& lacros_window_id) {
  120. lacros_window_id_to_app_id_.erase(lacros_window_id);
  121. }
  122. void LacrosSaveHandler::ModifyWindowInfo(
  123. const app_restore::WindowInfo& window_info) {
  124. auto it = window_candidates_.find(
  125. app_restore::GetLacrosWindowId(window_info.window));
  126. if (it != window_candidates_.end()) {
  127. FullRestoreSaveHandler::GetInstance()->ModifyWindowInfo(
  128. profile_path_, it->second.app_id, it->second.window_id, window_info);
  129. }
  130. }
  131. std::string LacrosSaveHandler::GetAppId(aura::Window* window) {
  132. auto it = window_candidates_.find(app_restore::GetLacrosWindowId(window));
  133. return it != window_candidates_.end() ? it->second.app_id : std::string();
  134. }
  135. int LacrosSaveHandler::GetLacrosChromeAppWindowId(aura::Window* window) const {
  136. auto it = window_candidates_.find(app_restore::GetLacrosWindowId(window));
  137. // Window ids start at 0. If we cannot find a corresponding window, return -1
  138. // so we don't fetch the wrong window data.
  139. return it != window_candidates_.end() ? it->second.window_id : -1;
  140. }
  141. } // namespace full_restore