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_
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"
31 class ASH_EXPORT VideoDetectorObserver
{
33 // Invoked periodically while a video is being played onscreen.
34 virtual void OnVideoDetected(bool is_fullscreen
) = 0;
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
{
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
58 static const double kNotifyIntervalSec
;
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
;
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
);
109 #endif // ASH_WM_VIDEO_DETECTOR_H_