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
|
// 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 "ash/session/fullscreen_controller.h"
#include <limits>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/session/fullscreen_notification_bubble.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/wm/window_state.h"
#include "ash/wm/wm_event.h"
#include "base/check.h"
#include "base/strings/string_util.h"
#include "chromeos/dbus/power_manager/backlight.pb.h"
#include "chromeos/dbus/power_manager/idle.pb.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/wm/fullscreen/keep_fullscreen_for_url_checker.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "url/gurl.h"
namespace ash {
namespace {
// Exits full screen to avoid the web page or app mimicking the lock screen if
// the active window is in full screen mode and the shelf is not visible. Do not
// exit fullscreen if the shelf is visible while in fullscreen because the shelf
// makes it harder for a web page or app to mimic the lock screen.
void ExitFullscreenIfActive() {
WindowState* active_window_state = WindowState::ForActiveWindow();
if (!active_window_state || !active_window_state->IsFullscreen())
return;
Shelf* shelf = Shelf::ForWindow(active_window_state->window());
const bool shelf_visible =
shelf->GetVisibilityState() == ShelfVisibilityState::SHELF_VISIBLE;
if (shelf_visible && !active_window_state->GetHideShelfWhenFullscreen())
return;
const WMEvent event(WM_EVENT_TOGGLE_FULLSCREEN);
active_window_state->OnWMEvent(&event);
}
} // namespace
FullscreenController::FullscreenController(
SessionControllerImpl* session_controller)
: session_controller_(session_controller) {
auto* power_manager = chromeos::PowerManagerClient::Get();
// Might be nullptr in tests.
if (power_manager) {
power_manager->AddObserver(this);
}
}
FullscreenController::~FullscreenController() {
auto* power_manager = chromeos::PowerManagerClient::Get();
if (power_manager) {
power_manager->RemoveObserver(this);
}
}
// static
void FullscreenController::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kFullscreenAlertEnabled, true,
PrefRegistry::PUBLIC);
}
void FullscreenController::MaybeExitFullscreenBeforeLock(
base::OnceClosure callback) {
// Check whether it is allowed to keep full screen on unlock.
if (!features::IsFullscreenAfterUnlockAllowed()) {
ExitFullscreenIfActive();
std::move(callback).Run();
return;
}
// Nothing to do if the active window is not in full screen mode.
WindowState* active_window_state = WindowState::ForActiveWindow();
if (!active_window_state || !active_window_state->IsFullscreen()) {
std::move(callback).Run();
return;
}
// Do not exit fullscreen for a Borealis window. We do additional checks here
// to avoid entering a screen lock with a window which has a not-allowed
// property combination. We use CHECKs as those combination should never
// happen.
if (active_window_state->window()->GetProperty(
chromeos::kNoExitFullscreenOnLock)) {
CHECK(active_window_state->window()->GetProperty(
chromeos::kUseOverviewToExitFullscreen))
<< "Property combination not allowed. kUseOverviewToExitFullscreen "
"must be true if kNoExitFullscreenOnLock is true.";
std::move(callback).Run();
return;
}
if (!keep_fullscreen_checker_) {
keep_fullscreen_checker_ =
std::make_unique<chromeos::KeepFullscreenForUrlChecker>(
Shell::Get()->session_controller()->GetPrimaryUserPrefService());
}
// Always exit full screen if the allowlist policy is unset.
if (!keep_fullscreen_checker_
->IsKeepFullscreenWithoutNotificationPolicySet()) {
ExitFullscreenIfActive();
std::move(callback).Run();
return;
}
// Try to get the URL of the active window from the shell delegate.
const GURL& url =
Shell::Get()->shell_delegate()->GetLastCommittedURLForWindowIfAny(
active_window_state->window());
// Check if it is allowed by user pref to keep full screen for the window URL.
if (keep_fullscreen_checker_->ShouldExitFullscreenForUrl(url))
ExitFullscreenIfActive();
std::move(callback).Run();
}
void FullscreenController::MaybeShowNotification() {
if (!features::IsFullscreenAlertBubbleEnabled())
return;
auto* session_controller = Shell::Get()->session_controller();
// Check if a user session is active to exclude OOBE process.
if (session_controller->GetSessionState() !=
session_manager::SessionState::ACTIVE) {
return;
}
auto* prefs = session_controller->GetPrimaryUserPrefService();
if (!prefs->GetBoolean(prefs::kFullscreenAlertEnabled))
return;
// Check if the activate window is fullscreen.
WindowState* active_window_state = WindowState::ForActiveWindow();
if (!active_window_state || !active_window_state->IsFullscreen())
return;
// Check if the shelf is visible.
Shelf* shelf = Shelf::ForWindow(active_window_state->window());
const bool shelf_visible =
shelf->GetVisibilityState() == ShelfVisibilityState::SHELF_VISIBLE;
if (shelf_visible && !active_window_state->GetHideShelfWhenFullscreen())
return;
if (!bubble_)
bubble_ = std::make_unique<FullscreenNotificationBubble>();
bubble_->ShowForWindowState(active_window_state);
}
void FullscreenController::SuspendImminent(
power_manager::SuspendImminent::Reason reason) {
if (session_controller_->login_status() != LoginStatus::GUEST)
return;
ExitFullscreenIfActive();
}
void FullscreenController::ScreenIdleStateChanged(
const power_manager::ScreenIdleState& proto) {
if (session_controller_->login_status() != LoginStatus::GUEST)
return;
if (proto.off() || proto.dimmed())
ExitFullscreenIfActive();
}
void FullscreenController::ScreenBrightnessChanged(
const power_manager::BacklightBrightnessChange& change) {
// Show alert when the device returns from low (epsilon) brightness which
// covers three cases.
// 1. The device returns from sleep.
// 2. The device lid is opended (with sleep on).
// 3. The device returns from low display brightness.
double epsilon = std::numeric_limits<double>::epsilon();
if (change.percent() <= epsilon) {
device_in_dark_ = true;
} else {
if (device_in_dark_)
MaybeShowNotification();
device_in_dark_ = false;
}
}
void FullscreenController::LidEventReceived(
chromeos::PowerManagerClient::LidState state,
base::TimeTicks timestamp) {
// Show alert when the lid is opened. This also covers the case when the user
// turns off "Sleep when cover is closed".
if (state == chromeos::PowerManagerClient::LidState::OPEN)
MaybeShowNotification();
}
} // namespace ash
|