blob: b4179ab4fcab692b8197a61fcb5d7949ed4b80a0 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <memory>
#include <set>
#include "ash/ash_export.h"
#include "ash/session/session_observer.h"
#include "ash/shell_observer.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/scoped_observer.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/viz/public/interfaces/compositing/video_detector_observer.mojom.h"
#include "ui/aura/env_observer.h"
#include "ui/aura/window_observer.h"
namespace aura {
class Window;
namespace ash {
// Receives notifications from viz::VideoDetector about whether it is likely
// that a video is being played on screen. If video activity is detected, this
// class will classify it as full screen or windowed.
class ASH_EXPORT VideoDetector : public aura::EnvObserver,
public aura::WindowObserver,
public SessionObserver,
public ShellObserver,
public viz::mojom::VideoDetectorObserver {
// State of detected video activity.
enum class State {
// Video activity has been detected recently and there are no fullscreen
// windows.
// Video activity has been detected recently and there is at least one
// fullscreen window.
// Video activity has not been detected recently.
class Observer {
// Invoked when the video playback state has changed.
virtual void OnVideoStateChanged(VideoDetector::State state) = 0;
virtual ~Observer() {}
~VideoDetector() override;
State state() const { return state_; }
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// EnvObserver overrides.
void OnWindowInitialized(aura::Window* window) override;
// aura::WindowObserver overrides.
void OnWindowDestroyed(aura::Window* window) override;
void OnWindowDestroying(aura::Window* window) override;
// SessionStateController overrides.
void OnChromeTerminating() override;
// ShellObserver overrides.
void OnFullscreenStateChanged(bool is_fullscreen,
aura::Window* root_window) override;
// viz::mojom::VideoDetectorObserver implementation.
void OnVideoActivityStarted() override;
void OnVideoActivityEnded() override;
// Updates |state_| and notifies |observers_| if it changed.
void UpdateState();
// Connects to Viz and starts observing video activities.
void EstablishConnectionToViz();
// Called when connection to Viz is lost. The connection will be
// re-established after a short delay.
void OnConnectionError();
// Current playback state.
State state_;
// True if video has been observed in the last |kVideoTimeoutMs|.
bool video_is_playing_;
// Currently-fullscreen root windows.
std::set<aura::Window*> fullscreen_root_windows_;
base::ObserverList<Observer>::Unchecked observers_;
ScopedObserver<aura::Window, aura::WindowObserver> window_observer_manager_;
ScopedSessionObserver scoped_session_observer_;
bool is_shutting_down_;
mojo::Binding<viz::mojom::VideoDetectorObserver> binding_;
base::WeakPtrFactory<VideoDetector> weak_factory_;
} // namespace ash