blob: 7f9d33aa54a04b790fe878d21281ea01f3c419d7 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2020 The Chromium Authors
Nancy Wang6089d342020-12-18 02:43:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sammie Quond540170e2022-03-21 16:59:465#ifndef COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_
6#define COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_
Nancy Wang6089d342020-12-18 02:43:497
8#include <set>
9
10#include "base/component_export.h"
11#include "base/observer_list.h"
12#include "base/observer_list_types.h"
13
14class AccountId;
15
16namespace aura {
17class Window;
18}
19
Sammie Quon38b3e502021-03-22 19:34:3320namespace views {
21class Widget;
22}
23
Sammie Quond540170e2022-03-21 16:59:4624namespace app_restore {
Nancy Wang6089d342020-12-18 02:43:4925
Sammie Quond540170e2022-03-21 16:59:4626// AppRestoreInfo is responsible for providing the information for
27// AppRestoreInfo::Observer, including:
Nancy Wang6089d342020-12-18 02:43:4928// 1. Whether we should restore apps and browser windows for |account_id|.
Nancy Wang9e156592021-05-19 04:16:4329// 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 Wang6089d342020-12-18 02:43:4931// launch information, e.g. a task id for an ARC app
Sammie Quond540170e2022-03-21 16:59:4632class COMPONENT_EXPORT(APP_RESTORE) AppRestoreInfo {
Nancy Wang6089d342020-12-18 02:43:4933 public:
34 class Observer : public base::CheckedObserver {
35 public:
Nancy Wang9e156592021-05-19 04:16:4336 // 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 Wang40494702021-03-16 00:24:4742 // 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 Wang6089d342020-12-18 02:43:4948 virtual void OnAppLaunched(aura::Window* window) {}
49
Sammie Quond540170e2022-03-21 16:59:4650 // If |window| is restored, notifies observers to restore |window|, when
51 // |window| has been initialized.
Nancy Wang40494702021-03-16 00:24:4752 //
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 Quon90841272021-03-05 01:42:0356 virtual void OnWindowInitialized(aura::Window* window) {}
57
Sammie Quond540170e2022-03-21 16:59:4658 // Called once the widget associated with an app restored window is
Sammie Quon38b3e502021-03-22 19:34:3359 // 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 Wang38289d7f2022-01-19 00:33:5763 // Called when `window` is ready to be parented to a valid desk container.
64 //
Nancy Wang38289d7f2022-01-19 00:33:5765 // 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 Quonce1a32d2021-05-10 19:46:1368
Nancy Wang6089d342020-12-18 02:43:4969 protected:
70 ~Observer() override = default;
71 };
72
Sammie Quond540170e2022-03-21 16:59:4673 static AppRestoreInfo* GetInstance();
Nancy Wang6089d342020-12-18 02:43:4974
Sammie Quond540170e2022-03-21 16:59:4675 AppRestoreInfo();
76 AppRestoreInfo(const AppRestoreInfo&) = delete;
77 AppRestoreInfo& operator=(const AppRestoreInfo&) = delete;
78 ~AppRestoreInfo();
Nancy Wang6089d342020-12-18 02:43:4979
80 void AddObserver(Observer* observer);
81 void RemoveObserver(Observer* observer);
82
Nancy Wang9e156592021-05-19 04:16:4383 // 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 Wangb2fca268b2021-01-27 03:13:1893 // Notifies observers to observe |window| and restore or save the window info
94 // for |window|.
95 void OnAppLaunched(aura::Window* window);
96
Sammie Quon90841272021-03-05 01:42:0397 // Notifies observers that |window| has been initialized.
98 void OnWindowInitialized(aura::Window* window);
99
Sammie Quon38b3e502021-03-22 19:34:33100 // Notifies observers that |widget| has been initialized.
101 void OnWidgetInitialized(views::Widget* widget);
102
Nancy Wang38289d7f2022-01-19 00:33:57103 // Notifies observers that `window` is ready to be parented to a valid desk
104 // container..
105 void OnParentWindowToValidContainer(aura::Window* window);
Sammie Quonce1a32d2021-05-10 19:46:13106
Nancy Wang6089d342020-12-18 02:43:49107 private:
108 base::ObserverList<Observer> observers_;
109
Nancy Wang9e156592021-05-19 04:16:43110 // 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 Wang6089d342020-12-18 02:43:49115};
116
Sammie Quond540170e2022-03-21 16:59:46117} // namespace app_restore
Nancy Wang6089d342020-12-18 02:43:49118
Sammie Quond540170e2022-03-21 16:59:46119#endif // COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_