#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 {
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);
}
}
FullscreenController::FullscreenController(
SessionControllerImpl* session_controller)
: session_controller_(session_controller) {
auto* power_manager = chromeos::PowerManagerClient::Get();
if (power_manager) {
power_manager->AddObserver(this);
}
}
FullscreenController::~FullscreenController() {
auto* power_manager = chromeos::PowerManagerClient::Get();
if (power_manager) {
power_manager->RemoveObserver(this);
}
}
void FullscreenController::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kFullscreenAlertEnabled, true,
PrefRegistry::PUBLIC);
}
void FullscreenController::MaybeExitFullscreenBeforeLock(
base::OnceClosure callback) {
WindowState* active_window_state = WindowState::ForActiveWindow();
if (!active_window_state || !active_window_state->IsFullscreen()) {
std::move(callback).Run();
return;
}
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());
}
if (!keep_fullscreen_checker_
->IsKeepFullscreenWithoutNotificationPolicySet()) {
ExitFullscreenIfActive();
std::move(callback).Run();
return;
}
const GURL& url =
Shell::Get()->shell_delegate()->GetLastCommittedURLForWindowIfAny(
active_window_state->window());
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();
if (session_controller->GetSessionState() !=
session_manager::SessionState::ACTIVE) {
return;
}
auto* prefs = session_controller->GetPrimaryUserPrefService();
if (!prefs->GetBoolean(prefs::kFullscreenAlertEnabled))
return;
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;
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) {
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) {
if (state == chromeos::PowerManagerClient::LidState::OPEN)
MaybeShowNotification();
}
}