Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 5 | #ifndef COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_ |
| 6 | #define COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_ |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 7 | |
| 8 | #include <set> |
| 9 | |
| 10 | #include "base/component_export.h" |
| 11 | #include "base/observer_list.h" |
| 12 | #include "base/observer_list_types.h" |
| 13 | |
| 14 | class AccountId; |
| 15 | |
| 16 | namespace aura { |
| 17 | class Window; |
| 18 | } |
| 19 | |
Sammie Quon | 38b3e50 | 2021-03-22 19:34:33 | [diff] [blame] | 20 | namespace views { |
| 21 | class Widget; |
| 22 | } |
| 23 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 24 | namespace app_restore { |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 25 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 26 | // AppRestoreInfo is responsible for providing the information for |
| 27 | // AppRestoreInfo::Observer, including: |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 28 | // 1. Whether we should restore apps and browser windows for |account_id|. |
Nancy Wang | 9e15659 | 2021-05-19 04:16:43 | [diff] [blame] | 29 | // 2. Notifies when the restore pref is changed for |account_id|. |
| 30 | // 3. Notifies when |window| is ready to be restored, after we have the app |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 31 | // launch information, e.g. a task id for an ARC app |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 32 | class COMPONENT_EXPORT(APP_RESTORE) AppRestoreInfo { |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 33 | public: |
| 34 | class Observer : public base::CheckedObserver { |
| 35 | public: |
Nancy Wang | 9e15659 | 2021-05-19 04:16:43 | [diff] [blame] | 36 | // Notifies when the restore pref is changed. If the restore pref is 'Do not |
| 37 | // restore', `could_restore` is false. Otherwise, `could_restore` is true, |
| 38 | // for the pref 'Always' and 'Ask every time'. |
| 39 | virtual void OnRestorePrefChanged(const AccountId& account_id, |
| 40 | bool could_restore) {} |
| 41 | |
Nancy Wang | 4049470 | 2021-03-16 00:24:47 | [diff] [blame] | 42 | // Notifies when |window| is ready to save the window info. |
| 43 | // |
| 44 | // When |window| is created, we might not have the app launch info yet. For |
| 45 | // example, if the ARC task is not created, we don't have the launch info. |
| 46 | // When the task is created, OnAppLaunched is called to notify observers to |
| 47 | // save the window info. |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 48 | virtual void OnAppLaunched(aura::Window* window) {} |
| 49 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 50 | // If |window| is restored, notifies observers to restore |window|, when |
| 51 | // |window| has been initialized. |
Nancy Wang | 4049470 | 2021-03-16 00:24:47 | [diff] [blame] | 52 | // |
| 53 | // For ARC app windows, when |window| is initialized, the task might not be |
| 54 | // created yet, so we don't have the window info, |window| might be parent |
| 55 | // to a hidden container based on the property kParentToHiddenContainerKey. |
Sammie Quon | 9084127 | 2021-03-05 01:42:03 | [diff] [blame] | 56 | virtual void OnWindowInitialized(aura::Window* window) {} |
| 57 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 58 | // Called once the widget associated with an app restored window is |
Sammie Quon | 38b3e50 | 2021-03-22 19:34:33 | [diff] [blame] | 59 | // initialized. This is called sometime after OnWindowInitialized, and the |
| 60 | // ARC task also may not be created yet at this point. |
| 61 | virtual void OnWidgetInitialized(views::Widget* widget) {} |
| 62 | |
Nancy Wang | 38289d7f | 2022-01-19 00:33:57 | [diff] [blame] | 63 | // Called when `window` is ready to be parented to a valid desk container. |
| 64 | // |
Nancy Wang | 38289d7f | 2022-01-19 00:33:57 | [diff] [blame] | 65 | // For ARC app windows, called once a window which was created without an |
| 66 | // associated task is now associated with a ARC task. |
| 67 | virtual void OnParentWindowToValidContainer(aura::Window* window) {} |
Sammie Quon | ce1a32d | 2021-05-10 19:46:13 | [diff] [blame] | 68 | |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 69 | protected: |
| 70 | ~Observer() override = default; |
| 71 | }; |
| 72 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 73 | static AppRestoreInfo* GetInstance(); |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 74 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 75 | AppRestoreInfo(); |
| 76 | AppRestoreInfo(const AppRestoreInfo&) = delete; |
| 77 | AppRestoreInfo& operator=(const AppRestoreInfo&) = delete; |
| 78 | ~AppRestoreInfo(); |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 79 | |
| 80 | void AddObserver(Observer* observer); |
| 81 | void RemoveObserver(Observer* observer); |
| 82 | |
Nancy Wang | 9e15659 | 2021-05-19 04:16:43 | [diff] [blame] | 83 | // Returns true if the restore pref is 'Always' or 'Ask every time', as we |
| 84 | // could restore apps and pages based on the user's choice from the |
| 85 | // notification for `account_id`. Otherwise, returns false, when the restore |
| 86 | // pref is 'Do not restore'. |
| 87 | bool CanPerformRestore(const AccountId& account_id); |
| 88 | |
| 89 | // Sets whether we could restore apps and pages, based on the restore pref |
| 90 | // setting for `account_id`. |
| 91 | void SetRestorePref(const AccountId& account_id, bool could_restore); |
| 92 | |
Nancy Wang | b2fca268b | 2021-01-27 03:13:18 | [diff] [blame] | 93 | // Notifies observers to observe |window| and restore or save the window info |
| 94 | // for |window|. |
| 95 | void OnAppLaunched(aura::Window* window); |
| 96 | |
Sammie Quon | 9084127 | 2021-03-05 01:42:03 | [diff] [blame] | 97 | // Notifies observers that |window| has been initialized. |
| 98 | void OnWindowInitialized(aura::Window* window); |
| 99 | |
Sammie Quon | 38b3e50 | 2021-03-22 19:34:33 | [diff] [blame] | 100 | // Notifies observers that |widget| has been initialized. |
| 101 | void OnWidgetInitialized(views::Widget* widget); |
| 102 | |
Nancy Wang | 38289d7f | 2022-01-19 00:33:57 | [diff] [blame] | 103 | // Notifies observers that `window` is ready to be parented to a valid desk |
| 104 | // container.. |
| 105 | void OnParentWindowToValidContainer(aura::Window* window); |
Sammie Quon | ce1a32d | 2021-05-10 19:46:13 | [diff] [blame] | 106 | |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 107 | private: |
| 108 | base::ObserverList<Observer> observers_; |
| 109 | |
Nancy Wang | 9e15659 | 2021-05-19 04:16:43 | [diff] [blame] | 110 | // Records the restore pref. If the account id is not added, that means the |
| 111 | // restore pref is 'Do not restore' for the account id. Otherwise, the restore |
| 112 | // pref is 'Always' or 'Ask every time', and we could restore for the account |
| 113 | // id. |
| 114 | std::set<AccountId> restore_prefs_; |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 115 | }; |
| 116 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 117 | } // namespace app_restore |
Nancy Wang | 6089d34 | 2020-12-18 02:43:49 | [diff] [blame] | 118 | |
Sammie Quon | d540170e | 2022-03-21 16:59:46 | [diff] [blame] | 119 | #endif // COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_ |