platformKeys: Add per-extension sign permissions.
[chromium-blink-merge.git] / ash / wm / video_detector.cc
blob2d919e1f34fcd732c9107010d37bd60b92020542
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/wm/video_detector.h"
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/wm/window_state.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/wm/core/window_util.h"
16 namespace ash {
18 const int VideoDetector::kMinUpdateWidth = 333;
19 const int VideoDetector::kMinUpdateHeight = 250;
20 const int VideoDetector::kMinFramesPerSecond = 15;
21 const double VideoDetector::kNotifyIntervalSec = 1.0;
23 // Stores information about updates to a window and determines whether it's
24 // likely that a video is playing in it.
25 class VideoDetector::WindowInfo {
26 public:
27 WindowInfo() : buffer_start_(0), buffer_size_(0) {}
29 // Handles an update within a window, returning true if it appears that
30 // video is currently playing in the window.
31 bool RecordUpdateAndCheckForVideo(const gfx::Rect& region,
32 base::TimeTicks now) {
33 if (region.width() < kMinUpdateWidth || region.height() < kMinUpdateHeight)
34 return false;
36 // If the buffer is full, drop the first timestamp.
37 if (buffer_size_ == static_cast<size_t>(kMinFramesPerSecond)) {
38 buffer_start_ = (buffer_start_ + 1) % kMinFramesPerSecond;
39 buffer_size_--;
42 update_times_[(buffer_start_ + buffer_size_) % kMinFramesPerSecond] = now;
43 buffer_size_++;
45 return buffer_size_ == static_cast<size_t>(kMinFramesPerSecond) &&
46 (now - update_times_[buffer_start_]).InSecondsF() <= 1.0;
49 private:
50 // Circular buffer containing update times of the last (up to
51 // |kMinFramesPerSecond|) video-sized updates to this window.
52 base::TimeTicks update_times_[kMinFramesPerSecond];
54 // Index into |update_times_| of the oldest update.
55 size_t buffer_start_;
57 // Number of updates stored in |update_times_|.
58 size_t buffer_size_;
60 DISALLOW_COPY_AND_ASSIGN(WindowInfo);
63 VideoDetector::VideoDetector()
64 : observer_manager_(this),
65 is_shutting_down_(false) {
66 aura::Env::GetInstance()->AddObserver(this);
67 Shell::GetInstance()->AddShellObserver(this);
70 VideoDetector::~VideoDetector() {
71 Shell::GetInstance()->RemoveShellObserver(this);
72 aura::Env::GetInstance()->RemoveObserver(this);
75 void VideoDetector::AddObserver(VideoDetectorObserver* observer) {
76 observers_.AddObserver(observer);
79 void VideoDetector::RemoveObserver(VideoDetectorObserver* observer) {
80 observers_.RemoveObserver(observer);
83 void VideoDetector::OnWindowInitialized(aura::Window* window) {
84 observer_manager_.Add(window);
87 void VideoDetector::OnDelegatedFrameDamage(
88 aura::Window* window,
89 const gfx::Rect& damage_rect_in_dip) {
90 if (is_shutting_down_)
91 return;
92 linked_ptr<WindowInfo>& info = window_infos_[window];
93 if (!info.get())
94 info.reset(new WindowInfo);
96 base::TimeTicks now =
97 !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
98 if (info->RecordUpdateAndCheckForVideo(damage_rect_in_dip, now))
99 MaybeNotifyObservers(window, now);
102 void VideoDetector::OnWindowDestroyed(aura::Window* window) {
103 window_infos_.erase(window);
104 observer_manager_.Remove(window);
107 void VideoDetector::OnAppTerminating() {
108 // Stop checking video activity once the shutdown
109 // process starts. crbug.com/231696.
110 is_shutting_down_ = true;
113 void VideoDetector::MaybeNotifyObservers(aura::Window* window,
114 base::TimeTicks now) {
115 if (!last_observer_notification_time_.is_null() &&
116 (now - last_observer_notification_time_).InSecondsF() <
117 kNotifyIntervalSec)
118 return;
120 if (!window->IsVisible())
121 return;
123 gfx::Rect root_bounds = window->GetRootWindow()->bounds();
124 if (!window->GetBoundsInRootWindow().Intersects(root_bounds))
125 return;
127 // As a relatively-cheap way to avoid flipping back and forth between
128 // fullscreen and non-fullscreen notifications when one video is playing in a
129 // fullscreen window and a second video is playing in a non-fullscreen window,
130 // report fullscreen video whenever a fullscreen window exists on any desktop
131 // regardless of whether the video is actually playing in that window:
132 // http://crbug.com/340666
133 bool fullscreen_window_exists = false;
134 std::vector<aura::Window*> containers =
135 Shell::GetContainersFromAllRootWindows(kShellWindowId_DefaultContainer,
136 NULL);
137 for (std::vector<aura::Window*>::const_iterator container =
138 containers.begin(); container != containers.end(); ++container) {
139 const aura::Window::Windows& windows = (*container)->children();
140 for (aura::Window::Windows::const_iterator window = windows.begin();
141 window != windows.end(); ++window) {
142 if (wm::GetWindowState(*window)->IsFullscreen()) {
143 fullscreen_window_exists = true;
144 break;
149 FOR_EACH_OBSERVER(VideoDetectorObserver,
150 observers_,
151 OnVideoDetected(fullscreen_window_exists));
152 last_observer_notification_time_ = now;
155 } // namespace ash