Roll src/third_party/skia ff271c2:b679ca8
[chromium-blink-merge.git] / ash / wm / video_detector.h
blob0735b09d6d4da5a82509f95f5bdfeb96a6f78de8
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 #ifndef ASH_WM_VIDEO_DETECTOR_H_
6 #define ASH_WM_VIDEO_DETECTOR_H_
8 #include <map>
10 #include "ash/ash_export.h"
11 #include "ash/shell_observer.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/scoped_observer.h"
17 #include "base/time/time.h"
18 #include "ui/aura/env_observer.h"
19 #include "ui/aura/window_observer.h"
21 namespace aura {
22 class Window;
25 namespace gfx {
26 class Rect;
29 namespace ash {
31 class ASH_EXPORT VideoDetectorObserver {
32 public:
33 // Invoked periodically while a video is being played onscreen.
34 virtual void OnVideoDetected(bool is_fullscreen) = 0;
36 protected:
37 virtual ~VideoDetectorObserver() {}
40 // Watches for updates to windows and tries to detect when a video is playing.
41 // We err on the side of false positives and can be fooled by things like
42 // continuous scrolling of a page.
43 class ASH_EXPORT VideoDetector : public aura::EnvObserver,
44 public aura::WindowObserver,
45 public ShellObserver {
46 public:
47 // Minimum dimensions in pixels that a window update must have to be
48 // considered a potential video frame.
49 static const int kMinUpdateWidth;
50 static const int kMinUpdateHeight;
52 // Number of video-sized updates that we must see within a second in a window
53 // before we assume that a video is playing.
54 static const int kMinFramesPerSecond;
56 // Minimum amount of time between notifications to observers that a video is
57 // playing.
58 static const double kNotifyIntervalSec;
60 VideoDetector();
61 ~VideoDetector() override;
63 void set_now_for_test(base::TimeTicks now) { now_for_test_ = now; }
65 void AddObserver(VideoDetectorObserver* observer);
66 void RemoveObserver(VideoDetectorObserver* observer);
68 // EnvObserver overrides.
69 void OnWindowInitialized(aura::Window* window) override;
71 // WindowObserver overrides.
72 void OnDelegatedFrameDamage(aura::Window* window,
73 const gfx::Rect& region) override;
74 void OnWindowDestroyed(aura::Window* window) override;
76 // ShellObserver overrides.
77 void OnAppTerminating() override;
79 private:
80 class WindowInfo;
81 typedef std::map<aura::Window*, linked_ptr<WindowInfo> > WindowInfoMap;
83 // Possibly notifies observers in response to detection of a video in
84 // |window|. Notifications are rate-limited and don't get sent if the window
85 // is invisible or offscreen.
86 void MaybeNotifyObservers(aura::Window* window, base::TimeTicks now);
88 // Maps from a window that we're tracking to information about it.
89 WindowInfoMap window_infos_;
91 ObserverList<VideoDetectorObserver> observers_;
93 // Last time at which we notified observers that a video was playing.
94 base::TimeTicks last_observer_notification_time_;
96 // If set, used when the current time is needed. This can be set by tests to
97 // simulate the passage of time.
98 base::TimeTicks now_for_test_;
100 ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_;
102 bool is_shutting_down_;
104 DISALLOW_COPY_AND_ASSIGN(VideoDetector);
107 } // namespace ash
109 #endif // ASH_WM_VIDEO_DETECTOR_H_