File: game_mode_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (217 lines) | stat: -rw-r--r-- 7,514 bytes parent folder | download | duplicates (7)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 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.

#include "chromeos/ash/components/game_mode/game_mode_controller.h"

#include "ash/public/cpp/window_properties.h"
#include "ash/shell.h"
#include "base/metrics/histogram_functions.h"
#include "base/time/time.h"
#include "chromeos/ash/components/borealis/borealis_util.h"
#include "chromeos/ash/components/dbus/resourced/resourced_client.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "ui/views/widget/widget.h"

namespace game_mode {

namespace {

constexpr int kRefreshSec = 60;
constexpr int kTimeoutSec = kRefreshSec + 10;

}  // namespace

GameModeController::GameModeController() {
  if (!ash::Shell::HasInstance())
    return;
  aura::client::FocusClient* focus_client =
      aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow());
  focus_client->AddObserver(this);
  // In case a window is already focused when this is constructed.
  OnWindowFocused(focus_client->GetFocusedWindow(), nullptr);
}

GameModeController::~GameModeController() {
  if (ash::Shell::HasInstance())
    aura::client::GetFocusClient(ash::Shell::GetPrimaryRootWindow())
        ->RemoveObserver(this);
}

void GameModeController::OnWindowFocused(aura::Window* gained_focus,
                                         aura::Window* lost_focus) {
  auto maybe_keep_focused = std::move(focused_);

  if (!gained_focus)
    return;

  auto* widget = views::Widget::GetTopLevelWidgetForNativeView(gained_focus);
  // |widget| can be nullptr in tests.
  if (!widget)
    return;

  aura::Window* window = widget->GetNativeWindow();
  auto* window_state = ash::WindowState::Get(window);

  if (!window_state)
    return;

  if (ash::borealis::IsBorealisWindow(window)) {
    focused_ = std::make_unique<WindowTracker>(
        window_state, std::move(maybe_keep_focused),
        base::BindRepeating(
            &GameModeController::NotifySetGameMode,
            // This is safe because the callback is only used by WindowTracker
            // and GameModeEnabler, which are owned by (or transitively owned
            // by) GameModeController. Therefore |this| cannot be stale.
            base::Unretained(this)));
  }
}

GameModeController::WindowTracker::WindowTracker(
    ash::WindowState* window_state,
    std::unique_ptr<WindowTracker> previous_focus,
    NotifySetGameModeCallback notify_set_game_mode_callback)
    : notify_set_game_mode_callback_(notify_set_game_mode_callback) {
  auto* window = window_state->window();

  // Maintain game mode state without dropping momentarily out of it
  // when switching between full-screen game windows.
  if (previous_focus) {
    // Enabler will be null if game mode is not on before focus switch.
    game_mode_enabler_ = std::move(previous_focus->game_mode_enabler_);
  }

  window_state_observer_.Observe(window_state);
  window_observer_.Observe(window);

  // If maintaining game mode state, update the window reference on the reused
  // enabler. Must occur after observers are setup.
  if (game_mode_enabler_) {
    game_mode_enabler_->SetWindowState(window_state);
  }

  // This possibly turns OFF as well as turns on game mode.
  UpdateGameModeStatus(window_state);
}

GameModeController::WindowTracker::~WindowTracker() {}

void GameModeController::WindowTracker::OnPostWindowStateTypeChange(
    ash::WindowState* window_state,
    chromeos::WindowStateType old_type) {
  UpdateGameModeStatus(window_state);
}

void GameModeController::WindowTracker::UpdateGameModeStatus(
    ash::WindowState* window_state) {
  auto* window = window_state->window();
  CHECK(ash::borealis::IsBorealisWindow(window));

  if (!window_state->IsFullscreen()) {
    game_mode_enabler_.reset();
    return;
  }

  if (game_mode_enabler_) {
    // No need to create a new enabler. The existing one is already valid for
    // this window.
    return;
  }

  VLOG(2) << "Initializing GameModeEnabler for Borealis";

  // Borealis has no further criteria than the window being fullscreen and
  // focused, already guaranteed by WindowTracker existing.
  DCHECK_EQ(window_state, window_state_observer_.GetSource());
  game_mode_enabler_ = std::make_unique<GameModeEnabler>(
      window_state, notify_set_game_mode_callback_);
}

void GameModeController::WindowTracker::OnWindowDestroying(
    aura::Window* window) {
  window_state_observer_.Reset();
  window_observer_.Reset();
  game_mode_enabler_.reset();
}

bool GameModeController::GameModeEnabler::should_record_failure;

GameModeController::GameModeEnabler::GameModeEnabler(
    ash::WindowState* window_state,
    NotifySetGameModeCallback notify_set_game_mode_callback)
    : window_state_(window_state),
      notify_set_game_mode_callback_(notify_set_game_mode_callback) {
  notify_set_game_mode_callback_.Run(GameMode::BOREALIS, window_state_);

  GameModeEnabler::should_record_failure = true;
  base::UmaHistogramEnumeration(kGameModeResultHistogramName,
                                GameModeResult::kAttempted);
  if (ash::ResourcedClient::Get()) {
    ash::ResourcedClient::Get()->SetGameModeWithTimeout(
        GameMode::BOREALIS, kTimeoutSec,
        base::BindOnce(&GameModeEnabler::OnSetGameMode,
                       /*refresh_of=*/std::nullopt));
  }
  timer_.Start(FROM_HERE, base::Seconds(kRefreshSec), this,
               &GameModeEnabler::RefreshGameMode);
}

GameModeController::GameModeEnabler::~GameModeEnabler() {
  auto time_in_mode = began_.Elapsed();
  base::UmaHistogramLongTimes100(kTimeInGameModeHistogramName, time_in_mode);

  notify_set_game_mode_callback_.Run(GameMode::OFF, window_state_);

  timer_.Stop();
  VLOG(1) << "Turning off game mode";
  if (ash::ResourcedClient::Get()) {
    ash::ResourcedClient::Get()->SetGameModeWithTimeout(
        GameMode::OFF, 0,
        base::BindOnce(&GameModeEnabler::OnSetGameMode, GameMode::BOREALIS));
  }
}

void GameModeController::GameModeEnabler::SetWindowState(
    ash::WindowState* window_state) {
  if (window_state_ == window_state) {
    return;
  }

  window_state_ = window_state;
  notify_set_game_mode_callback_.Run(GameMode::BOREALIS, window_state_);
}

void GameModeController::GameModeEnabler::RefreshGameMode() {
  if (ash::ResourcedClient::Get()) {
    ash::ResourcedClient::Get()->SetGameModeWithTimeout(
        GameMode::BOREALIS, kTimeoutSec,
        base::BindOnce(&GameModeEnabler::OnSetGameMode, GameMode::BOREALIS));
  }
}

// Previous is whether game mode was enabled previous to this call.
void GameModeController::GameModeEnabler::OnSetGameMode(
    std::optional<GameMode> refresh_of,
    std::optional<GameMode> previous) {
  if (!previous.has_value()) {
    LOG(ERROR) << "Failed to set Game Mode";
  } else if (GameModeEnabler::should_record_failure && refresh_of.has_value() &&
             previous.value() != refresh_of.value()) {
    // If game mode was not on and it was not the initial call,
    // it means the previous call failed/timed out.
    base::UmaHistogramEnumeration(kGameModeResultHistogramName,
                                  GameModeResult::kFailed);
    // Only record failures once per entry into game mode.
    GameModeEnabler::should_record_failure = false;
  }
}

void GameModeController::NotifySetGameMode(GameMode game_mode,
                                           ash::WindowState* window_state) {
  if (!callback_.is_null()) {
    callback_.Run(window_state->window(), game_mode);
  }
}

}  // namespace game_mode