arc_read_handler.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #include "components/app_restore/arc_read_handler.h"
  5. #include <memory>
  6. #include "base/containers/contains.h"
  7. #include "components/app_restore/app_launch_info.h"
  8. #include "components/app_restore/app_restore_info.h"
  9. #include "components/app_restore/app_restore_utils.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 app_restore {
  14. namespace {
  15. bool IsValidRestoreWindowId(int32_t restore_window_id) {
  16. return restore_window_id != 0 &&
  17. restore_window_id != kParentToHiddenContainer;
  18. }
  19. } // namespace
  20. ArcReadHandler::ArcReadHandler(const base::FilePath& profile_path,
  21. Delegate* delegate)
  22. : profile_path_(profile_path), delegate_(delegate) {
  23. DCHECK(delegate_);
  24. }
  25. ArcReadHandler::~ArcReadHandler() = default;
  26. void ArcReadHandler::AddRestoreData(const std::string& app_id,
  27. int32_t window_id) {
  28. window_id_to_app_id_[window_id] = app_id;
  29. }
  30. void ArcReadHandler::AddArcWindowCandidate(aura::Window* window) {
  31. if (!base::Contains(task_id_to_window_id_,
  32. window->GetProperty(kWindowIdKey))) {
  33. // Check `session_id` to see whether this is a ghost window.
  34. int32_t session_id = window->GetProperty(kGhostWindowSessionIdKey);
  35. if (session_id >= kArcSessionIdOffsetForRestoredLaunching)
  36. return;
  37. // If the task hasn't been created, and this is not a ghost window, add
  38. // `window` to `arc_window_candidates_` to wait for the task to be created.
  39. arc_window_candidates_.insert(window);
  40. }
  41. }
  42. void ArcReadHandler::OnWindowDestroyed(aura::Window* window) {
  43. DCHECK(window);
  44. // If |window| is list in |arc_window_candidates_|, |window| is not attached
  45. // to a valid restore window id yet, so we don't need to remove AppRestoreData
  46. // from the restore data.
  47. auto it = arc_window_candidates_.find(window);
  48. if (it != arc_window_candidates_.end()) {
  49. arc_window_candidates_.erase(it);
  50. return;
  51. }
  52. int32_t restore_window_id = window->GetProperty(kRestoreWindowIdKey);
  53. RemoveAppRestoreData(restore_window_id);
  54. }
  55. void ArcReadHandler::OnTaskCreated(const std::string& app_id,
  56. int32_t task_id,
  57. int32_t session_id) {
  58. auto it = session_id_to_window_id_.find(session_id);
  59. if (it == session_id_to_window_id_.end()) {
  60. not_restored_task_ids_.insert(task_id);
  61. UpdateWindowCandidates(task_id, /*restore_window_id=*/-1);
  62. return;
  63. }
  64. int32_t restore_window_id = it->second;
  65. session_id_to_window_id_.erase(it);
  66. task_id_to_window_id_[task_id] = restore_window_id;
  67. UpdateWindowCandidates(task_id, restore_window_id);
  68. }
  69. void ArcReadHandler::OnTaskDestroyed(int32_t task_id) {
  70. not_restored_task_ids_.erase(task_id);
  71. auto it = task_id_to_window_id_.find(task_id);
  72. if (it == task_id_to_window_id_.end())
  73. return;
  74. int32_t window_id = it->second;
  75. task_id_to_window_id_.erase(it);
  76. RemoveAppRestoreData(window_id);
  77. }
  78. bool ArcReadHandler::HasRestoreData(int32_t window_id) {
  79. return base::Contains(window_id_to_app_id_, window_id);
  80. }
  81. std::unique_ptr<AppLaunchInfo> ArcReadHandler::GetArcAppLaunchInfo(
  82. const std::string& app_id,
  83. int32_t session_id) {
  84. int32_t restore_window_id = GetArcRestoreWindowIdForSessionId(session_id);
  85. if (restore_window_id == 0)
  86. return nullptr;
  87. return delegate_->GetAppLaunchInfo(profile_path_, app_id, restore_window_id);
  88. }
  89. std::unique_ptr<WindowInfo> ArcReadHandler::GetWindowInfo(
  90. int32_t restore_window_id) {
  91. if (!IsValidRestoreWindowId(restore_window_id))
  92. return nullptr;
  93. auto it = window_id_to_app_id_.find(restore_window_id);
  94. if (it == window_id_to_app_id_.end())
  95. return nullptr;
  96. std::unique_ptr<WindowInfo> window_info =
  97. delegate_->GetWindowInfo(profile_path_, it->second, restore_window_id);
  98. if (!window_info)
  99. return nullptr;
  100. // For ARC windows, Android can restore window bounds, so remove the window
  101. // bounds from the window info.
  102. window_info->current_bounds.reset();
  103. // For ARC windows, Android can restore window minimized or maximized status,
  104. // so remove the WindowStateType from the window info for the minimized and
  105. // maximized state.
  106. if (window_info->window_state_type.has_value() &&
  107. (chromeos::IsMinimizedWindowStateType(
  108. window_info->window_state_type.value()) ||
  109. window_info->window_state_type.value() ==
  110. chromeos::WindowStateType::kMaximized)) {
  111. window_info->window_state_type.reset();
  112. }
  113. return window_info;
  114. }
  115. int32_t ArcReadHandler::GetArcRestoreWindowIdForTaskId(int32_t task_id) {
  116. auto it = task_id_to_window_id_.find(task_id);
  117. if (it != task_id_to_window_id_.end())
  118. return it->second;
  119. // If |session_id_to_window_id_| is empty, that means there is no ARC apps
  120. // launched. If `not_restored_task_ids_` has `task_id`, that means the ARC app
  121. // window is not restored.
  122. if (session_id_to_window_id_.empty() ||
  123. base::Contains(not_restored_task_ids_, task_id)) {
  124. return 0;
  125. }
  126. // If |session_id_to_window_id_| is not empty, that means there are ARC
  127. // apps launched. Returns -1 to add the ARC app window to the hidden
  128. // container.
  129. return kParentToHiddenContainer;
  130. }
  131. int32_t ArcReadHandler::GetArcRestoreWindowIdForSessionId(int32_t session_id) {
  132. // If `session_id` doesn't exist, that means there is no ARC app restored.
  133. auto it = session_id_to_window_id_.find(session_id);
  134. return it == session_id_to_window_id_.end() ? 0 : it->second;
  135. }
  136. void ArcReadHandler::SetArcSessionIdForWindowId(int32_t session_id,
  137. int32_t window_id) {
  138. DCHECK_GT(session_id, kArcSessionIdOffsetForRestoredLaunching);
  139. session_id_to_window_id_[session_id] = window_id;
  140. }
  141. void ArcReadHandler::RemoveAppRestoreData(int32_t window_id) {
  142. if (!IsValidRestoreWindowId(window_id))
  143. return;
  144. auto it = window_id_to_app_id_.find(window_id);
  145. if (it == window_id_to_app_id_.end())
  146. return;
  147. delegate_->RemoveAppRestoreData(profile_path_, it->second, window_id);
  148. window_id_to_app_id_.erase(it);
  149. }
  150. void ArcReadHandler::UpdateWindowCandidates(int32_t task_id,
  151. int32_t restore_window_id) {
  152. // Go through `arc_window_candidates_`.
  153. auto window_it =
  154. std::find_if(arc_window_candidates_.begin(), arc_window_candidates_.end(),
  155. [task_id](aura::Window* window) {
  156. return window->GetProperty(kWindowIdKey) == task_id;
  157. });
  158. if (window_it == arc_window_candidates_.end())
  159. return;
  160. // If `restore_window_id` is valid, sets the window property
  161. // `kRestoreWindowIdKey` and `kWindowInfoKey`.
  162. if (IsValidRestoreWindowId(restore_window_id)) {
  163. (*window_it)->SetProperty(kRestoreWindowIdKey, restore_window_id);
  164. // When the window was created, there was not any window info due to there
  165. // being no task. Apply properties to the window now that there is window
  166. // info.
  167. std::unique_ptr<WindowInfo> window_info = GetWindowInfo(restore_window_id);
  168. if (window_info)
  169. ApplyProperties(window_info.get(), *window_it);
  170. }
  171. // Remove the window from the hidden container.
  172. if ((*window_it)->GetProperty(kParentToHiddenContainerKey)) {
  173. app_restore::AppRestoreInfo::GetInstance()->OnParentWindowToValidContainer(
  174. *window_it);
  175. }
  176. arc_window_candidates_.erase(*window_it);
  177. }
  178. } // namespace app_restore