window_restore_controller.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ASH_WM_WINDOW_RESTORE_WINDOW_RESTORE_CONTROLLER_H_
  5. #define ASH_WM_WINDOW_RESTORE_WINDOW_RESTORE_CONTROLLER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/public/cpp/tablet_mode_observer.h"
  8. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  9. #include "base/cancelable_callback.h"
  10. #include "base/containers/flat_set.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/scoped_multi_source_observation.h"
  13. #include "base/scoped_observation.h"
  14. #include "components/account_id/account_id.h"
  15. #include "components/app_restore/app_restore_info.h"
  16. #include "components/app_restore/window_info.h"
  17. #include "ui/aura/window_observer.h"
  18. namespace aura {
  19. class Window;
  20. }
  21. namespace views {
  22. class Widget;
  23. }
  24. namespace ash {
  25. class WindowState;
  26. class ASH_EXPORT WindowRestoreController
  27. : public TabletModeObserver,
  28. public app_restore::AppRestoreInfo::Observer,
  29. public aura::WindowObserver {
  30. public:
  31. using SaveWindowCallback =
  32. base::RepeatingCallback<void(const app_restore::WindowInfo&)>;
  33. WindowRestoreController();
  34. WindowRestoreController(const WindowRestoreController&) = delete;
  35. WindowRestoreController& operator=(const WindowRestoreController&) = delete;
  36. ~WindowRestoreController() override;
  37. // Convenience function to get the controller which is created and owned by
  38. // Shell.
  39. static WindowRestoreController* Get();
  40. // Returns whether a app restored window can be activated. Only ghost
  41. // windows, windows without the `app_restore::kLaunchedFromAppRestoreKey`,
  42. // and topmost app restored windows return true.
  43. static bool CanActivateRestoredWindow(const aura::Window* window);
  44. // When windows are restored, they're restored inactive so during tablet mode
  45. // a window may be restored above the app list while the app list is still
  46. // active. To prevent this situation, the app list is deactivated and this
  47. // function should be called when determining the next focus target to prevent
  48. // the app list from being reactivated. Returns true if we're in tablet mode,
  49. // |window| is the window for the app list, and the topmost window of any
  50. // active desk container is a restored window.
  51. static bool CanActivateAppList(const aura::Window* window);
  52. // Given a set of windows, ordered from LRU to MRU, returns where `window`
  53. // should be inserted. The insertion point is determined by iterating from LRU
  54. // to MRU, returning the an iter to the first window that has no activation
  55. // index or a lower activation index.
  56. static std::vector<aura::Window*>::const_iterator GetWindowToInsertBefore(
  57. aura::Window* window,
  58. const std::vector<aura::Window*>& windows);
  59. // Calls SaveWindowImpl for |window_state|. The activation index will be
  60. // calculated in SaveWindowImpl.
  61. void SaveWindow(WindowState* window_state);
  62. // Gets all windows on all desk in the MRU window tracker and saves them to
  63. // file.
  64. void SaveAllWindows();
  65. // Called from MruWindowTracker when |gained_active| gets activation. This is
  66. // not done as an observer to ensure changes to the MRU list get handled first
  67. // before this is called.
  68. void OnWindowActivated(aura::Window* gained_active);
  69. // Stacks the window according to its activation index.
  70. void StackWindow(aura::Window* window);
  71. // Returns true if `window` is currently in the process of being restored by
  72. // `this`.
  73. bool IsRestoringWindow(aura::Window* window) const;
  74. // TabletModeObserver:
  75. void OnTabletModeStarted() override;
  76. void OnTabletModeEnded() override;
  77. void OnTabletControllerDestroyed() override;
  78. // app_restore::AppRestoreInfo::Observer:
  79. void OnRestorePrefChanged(const AccountId& account_id,
  80. bool could_restore) override;
  81. void OnAppLaunched(aura::Window* window) override;
  82. void OnWidgetInitialized(views::Widget* widget) override;
  83. void OnParentWindowToValidContainer(aura::Window* window) override;
  84. // aura::WindowObserver:
  85. void OnWindowPropertyChanged(aura::Window* window,
  86. const void* key,
  87. intptr_t old) override;
  88. void OnWindowVisibilityChanged(aura::Window* window, bool visible) override;
  89. void OnWindowDestroying(aura::Window* window) override;
  90. const aura::Window* to_be_snapped_window() const {
  91. return to_be_snapped_window_;
  92. }
  93. private:
  94. friend class WindowRestoreControllerTest;
  95. // Updates the window state, activation and stacking of `window`. Also
  96. // observes `window` as we need to do further updates when the window is
  97. // shown.
  98. void UpdateAndObserveWindow(aura::Window* window);
  99. // Calls full_restore::FullRestoreSaveHandler to save to file. The handler has
  100. // timer to prevent too many writes, but we should limit calls regardless if
  101. // possible. Optionally passes |activation_index|, which is calculated with
  102. // respect to the MRU tracker. Calling SaveAllWindows will iterate through
  103. // the MRU tracker list, so we can pass the activation index during that loop
  104. // instead of building the MRU list again for each window.
  105. void SaveWindowImpl(WindowState* window_state,
  106. absl::optional<int> activation_index);
  107. // Retrieves the saved `WindowInfo` of `window` and restores its
  108. // `WindowStateType`. Also creates a post task to clear `window`s
  109. // `app_restore::kLaunchedFromAppRestoreKey`.
  110. void RestoreStateTypeAndClearLaunchedKey(aura::Window* window);
  111. // Calls `CancelAndRemoveRestorePropertyClearCallback()`. Also sets the
  112. // `window`'s `app_restore::kLaunchedFromAppRestoreKey` to false if the
  113. // window is not destroying.
  114. void ClearLaunchedKey(aura::Window* window);
  115. // Cancels and removes the App Restore property clear callback for `window`
  116. // from `restore_property_clear_callbacks_`.
  117. void CancelAndRemoveRestorePropertyClearCallback(aura::Window* window);
  118. // Sets a callback for testing that will be fired immediately when
  119. // SaveWindowImpl is about to notify the window restore component we want to
  120. // write to file.
  121. void SetSaveWindowCallbackForTesting(SaveWindowCallback callback);
  122. // True whenever we are attempting to restore snap state.
  123. // The window that is about to be snapped by window restore. Reset to nullptr
  124. // if we aren't directly working with the window anymore.
  125. aura::Window* to_be_snapped_window_ = nullptr;
  126. // The set of windows that have had their widgets initialized and will be
  127. // shown later.
  128. base::flat_set<aura::Window*> to_be_shown_windows_;
  129. // When a window is restored, we post a task to clear its
  130. // `app_restore::kLaunchedFromAppRestoreKey` property. However, a window can
  131. // be closed before this task occurs, deleting the window. This map keeps
  132. // track of these posted tasks so we can cancel them upon window deletion.
  133. std::map<aura::Window*, base::CancelableOnceClosure>
  134. restore_property_clear_callbacks_;
  135. base::ScopedObservation<TabletModeController, TabletModeObserver>
  136. tablet_mode_observation_{this};
  137. base::ScopedObservation<app_restore::AppRestoreInfo,
  138. app_restore::AppRestoreInfo::Observer>
  139. app_restore_info_observation_{this};
  140. // Observes windows launched by window restore.
  141. base::ScopedMultiSourceObservation<aura::Window, aura::WindowObserver>
  142. windows_observation_{this};
  143. base::WeakPtrFactory<WindowRestoreController> weak_ptr_factory_{this};
  144. };
  145. } // namespace ash
  146. #endif // ASH_WM_WINDOW_RESTORE_WINDOW_RESTORE_CONTROLLER_H_