File: borealis_window_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 6,162 bytes parent folder | download | duplicates (6)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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 CHROME_BROWSER_ASH_BOREALIS_BOREALIS_WINDOW_MANAGER_H_
#define CHROME_BROWSER_ASH_BOREALIS_BOREALIS_WINDOW_MANAGER_H_

#include <string>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "components/services/app_service/public/cpp/instance_registry.h"

class Profile;

namespace aura {
class Window;
}

namespace borealis {

// Base64-encoded shell application id of borealis client when it is in full-
// screen mode.
extern const char kFullscreenClientShellId[];

// Base64-encoded application id suffix for borealis client windows.
extern const char kBorealisClientSuffix[];

// Anonymous apps do not have a CrOS-standard app_id (i.e. one registered with
// the GuestOsRegistryService), so to identify them we prepend this.
extern const char kBorealisAnonymousPrefix[];

// The borealis window manager keeps track of the association of windows to
// borealis apps. This includes determining which windows belong to a borealis
// app, what the lifetime of the app is relative to its windows, and the
// presence of borealis windows with an unknown app (see go/anonymous-apps).
class BorealisWindowManager : public apps::InstanceRegistry::Observer {
 public:

  // Whether this window belongs to a Steam game within the Borealis VM.
  static bool IsSteamGameWindow(Profile* profile, const aura::Window* window);

  // Returns true when the given |app_id| is for an anonymous borealis app.
  static bool IsAnonymousAppId(const std::string& app_id);

  // An observer for tracking the creation and deletion of anonymous windows.
  class AnonymousAppObserver : public base::CheckedObserver {
   public:
    // Called when a new App ID was detected that we do not know the app it
    // belongs too. The |shelf_app_name| represents the system's best-guess for
    // what the app should be called. This us usually not a localized string but
    // something we read from the window's properties.
    virtual void OnAnonymousAppAdded(const std::string& shelf_app_id,
                                     const std::string& shelf_app_name) = 0;

    // Called when the last window for the anonymous app with |shelf_app_id| is
    // closed, and the app is no longer relevant.
    virtual void OnAnonymousAppRemoved(const std::string& shelf_app_id) = 0;

    // Called when the window manager is being deleted. Observers should
    // unregister themselves from it.
    virtual void OnWindowManagerDeleted(
        BorealisWindowManager* window_manager) = 0;
  };

  // An observer for tracking window/app lifetimes. The key concepts are:
  //  - "Session", which refers to all borealis windows.
  //  - "App", which refers to the subset of windows belonging to a single
  //    identified app.
  //  - "Window", which refers to single windows.
  // These concepts are nested, all apps belong to one session, and each window
  // belongs to a single app.
  class AppWindowLifetimeObserver : public base::CheckedObserver {
   public:
    // Called when the first UI element of any borealis app becomes visible.
    virtual void OnSessionStarted() {}

    // Called when the last UI element of any borealis app disappears. This
    // implies that there are no more borealis windows until the next
    // OnSessionStarted() is called.
    virtual void OnSessionFinished() {}

    // Called when the first window for an app with this |app_id| becomes
    // visible.
    virtual void OnAppStarted(const std::string& app_id) {}

    // Called when the last window for |app_id|'s app goes away, implying the
    // app has no visible windows until OnAppStarted() is called again.
    virtual void OnAppFinished(const std::string& app_id,
                               aura::Window* last_window) {}

    // Called when a window associated with |app_id|'s app comes into existence.
    // Note that this has nothing to do with the visible state of |window|,
    // only that it exists in memory.
    virtual void OnWindowStarted(const std::string& app_id,
                                 aura::Window* window) {}

    // Called when |window|, associated with |app_id|'s app, is about to be
    // closed.
    virtual void OnWindowFinished(const std::string& app_id,
                                  aura::Window* window) {}

    // Called when the window manager is being deleted. Observers should
    // unregister themselves from it.
    virtual void OnWindowManagerDeleted(
        BorealisWindowManager* window_manager) = 0;
  };

  explicit BorealisWindowManager(Profile* profile);

  ~BorealisWindowManager() override;

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

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

  // Returns the application ID for the given window, or "" if the window does
  // not belong to borealis.
  std::string GetShelfAppId(aura::Window* window);

  // apps::InstanceRegistry::Observer overrides.
  void OnInstanceUpdate(const apps::InstanceUpdate& update) override;
  void OnInstanceRegistryWillBeDestroyed(
      apps::InstanceRegistry* cache) override;

 private:
  void HandleWindowDestruction(aura::Window* window, const std::string& app_id);
  void HandleWindowCreation(aura::Window* window, const std::string& app_id);

  const raw_ptr<Profile, DanglingUntriaged> profile_;
  base::ScopedObservation<apps::InstanceRegistry,
                          apps::InstanceRegistry::Observer>
      instance_registry_observation_;
  base::flat_map<std::string,
                 base::flat_set<raw_ptr<aura::Window, CtnExperimental>>>
      ids_to_windows_;
  base::ObserverList<AnonymousAppObserver> anon_observers_;
  base::ObserverList<AppWindowLifetimeObserver> lifetime_observers_;
};

}  // namespace borealis

#endif  // CHROME_BROWSER_ASH_BOREALIS_BOREALIS_WINDOW_MANAGER_H_