File: app_restore_info.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,490 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_
#define COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_

#include <set>

#include "base/component_export.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"

class AccountId;

namespace aura {
class Window;
}

namespace views {
class Widget;
}

namespace app_restore {

// AppRestoreInfo is responsible for providing the information for
// AppRestoreInfo::Observer, including:
// 1. Whether we should restore apps and browser windows for |account_id|.
// 2. Notifies when the restore pref is changed for |account_id|.
// 3. Notifies when |window| is ready to be restored, after we have the app
// launch information, e.g. a task id for an ARC app
class COMPONENT_EXPORT(APP_RESTORE) AppRestoreInfo {
 public:
  class Observer : public base::CheckedObserver {
   public:
    // Notifies when the restore pref is changed. If the restore pref is 'Do not
    // restore', `could_restore` is false. Otherwise, `could_restore` is true,
    // for the pref 'Always' and 'Ask every time'.
    virtual void OnRestorePrefChanged(const AccountId& account_id,
                                      bool could_restore) {}

    // Notifies when |window| is ready to save the window info.
    //
    // When |window| is created, we might not have the app launch info yet. For
    // example, if the ARC task is not created, we don't have the launch info.
    // When the task is created, OnAppLaunched is called to notify observers to
    // save the window info.
    virtual void OnAppLaunched(aura::Window* window) {}

    // If |window| is restored, notifies observers to restore |window|, when
    // |window| has been initialized.
    //
    // For ARC app windows, when |window| is initialized, the task might not be
    // created yet, so we don't have the window info, |window| might be parent
    // to a hidden container based on the property kParentToHiddenContainerKey.
    virtual void OnWindowInitialized(aura::Window* window) {}

    // Called once the widget associated with an app restored window is
    // initialized. This is called sometime after OnWindowInitialized, and the
    // ARC task also may not be created yet at this point.
    virtual void OnWidgetInitialized(views::Widget* widget) {}

    // Called when `window` is ready to be parented to a valid desk container.
    //
    // For ARC app windows, called once a window which was created without an
    // associated task is now associated with a ARC task.
    virtual void OnParentWindowToValidContainer(aura::Window* window) {}

   protected:
    ~Observer() override = default;
  };

  static AppRestoreInfo* GetInstance();

  AppRestoreInfo();
  AppRestoreInfo(const AppRestoreInfo&) = delete;
  AppRestoreInfo& operator=(const AppRestoreInfo&) = delete;
  ~AppRestoreInfo();

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Returns true if the restore pref is 'Always' or 'Ask every time', as we
  // could restore apps and pages based on the user's choice from the
  // notification for `account_id`. Otherwise, returns false, when the restore
  // pref is 'Do not restore'.
  bool CanPerformRestore(const AccountId& account_id);

  // Sets whether we could restore apps and pages, based on the restore pref
  // setting for `account_id`.
  void SetRestorePref(const AccountId& account_id, bool could_restore);

  // Notifies observers to observe |window| and restore or save the window info
  // for |window|.
  void OnAppLaunched(aura::Window* window);

  // Notifies observers that |window| has been initialized.
  void OnWindowInitialized(aura::Window* window);

  // Notifies observers that |widget| has been initialized.
  void OnWidgetInitialized(views::Widget* widget);

  // Notifies observers that `window` is ready to be parented to a valid desk
  // container..
  void OnParentWindowToValidContainer(aura::Window* window);

 private:
  base::ObserverList<Observer> observers_;

  // Records the restore pref. If the account id is not added, that means the
  // restore pref is 'Do not restore' for the account id. Otherwise, the restore
  // pref is 'Always' or 'Ask every time', and we could restore for the account
  // id.
  std::set<AccountId> restore_prefs_;
};

}  // namespace app_restore

#endif  // COMPONENTS_APP_RESTORE_APP_RESTORE_INFO_H_