OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_ | 5 #ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_ |
6 #define MEDIA_BASE_USER_INPUT_MONITOR_H_ | 6 #define MEDIA_BASE_USER_INPUT_MONITOR_H_ |
7 | 7 |
| 8 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
10 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
11 #include "media/base/media_export.h" | 12 #include "media/base/media_export.h" |
12 | 13 |
13 struct SkIPoint; | 14 struct SkIPoint; |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class SingleThreadTaskRunner; | 17 class SingleThreadTaskRunner; |
17 } // namespace base | 18 } // namespace base |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 | 21 |
21 // Monitors and notifies about mouse movements and keyboard events. | 22 // Monitors and notifies about mouse movements and keyboard events. |
22 // Thread safe. The thread on which the listenters are called is not guaranteed. | 23 // Thread safe. The thread on which the listenters are called is not guaranteed. |
23 // The callers should not perform expensive/blocking tasks in the callback since | 24 // The callers should not perform expensive/blocking tasks in the callback since |
24 // it might be called on the browser UI/IO threads. | 25 // it might be called on the browser UI/IO threads. |
25 // The object must outlive the browser UI/IO threads to make sure the callbacks | 26 // The object must outlive the browser UI/IO threads to make sure the callbacks |
26 // will not access a deleted object. | 27 // will not access a deleted object. |
27 class MEDIA_EXPORT UserInputMonitor { | 28 class MEDIA_EXPORT UserInputMonitor |
| 29 : public base::RefCountedThreadSafe<UserInputMonitor> { |
28 public: | 30 public: |
29 // The interface to receive mouse movement events. | 31 // The interface to receive mouse movement events. |
30 class MEDIA_EXPORT MouseEventListener { | 32 class MEDIA_EXPORT MouseEventListener { |
31 public: | 33 public: |
32 // |position| is the new mouse position. | 34 // |position| is the new mouse position. |
33 virtual void OnMouseMoved(const SkIPoint& position) = 0; | 35 virtual void OnMouseMoved(const SkIPoint& position) = 0; |
34 | 36 |
35 protected: | 37 protected: |
36 virtual ~MouseEventListener() {} | 38 virtual ~MouseEventListener() {} |
37 }; | 39 }; |
38 | 40 |
39 UserInputMonitor(); | 41 UserInputMonitor(); |
40 virtual ~UserInputMonitor(); | |
41 | 42 |
42 // Creates a platform-specific instance of UserInputMonitor. | 43 // Creates a platform-specific instance of UserInputMonitor. |
43 // |io_task_runner| is the task runner for an IO thread. | 44 // |io_task_runner| is the task runner for an IO thread. |
44 // |ui_task_runner| is the task runner for a UI thread. | 45 // |ui_task_runner| is the task runner for a UI thread. |
45 static scoped_ptr<UserInputMonitor> Create( | 46 static scoped_refptr<UserInputMonitor> Create( |
46 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, | 47 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner, |
47 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); | 48 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner); |
48 | 49 |
49 // The same |listener| should only be added once. | 50 // The same |listener| should only be added once. |
50 // The clients should make sure to call Remove*Listener before |listener| is | 51 // The clients should make sure to call Remove*Listener before |listener| is |
51 // destroyed. | 52 // destroyed. |
52 void AddMouseListener(MouseEventListener* listener); | 53 void AddMouseListener(MouseEventListener* listener); |
53 void RemoveMouseListener(MouseEventListener* listener); | 54 void RemoveMouseListener(MouseEventListener* listener); |
54 | 55 |
55 // A caller must call EnableKeyPressMonitoring and | 56 // A caller must call EnableKeyPressMonitoring and |
56 // DisableKeyPressMonitoring in pair. | 57 // DisableKeyPressMonitoring in pair. |
57 void EnableKeyPressMonitoring(); | 58 void EnableKeyPressMonitoring(); |
58 void DisableKeyPressMonitoring(); | 59 void DisableKeyPressMonitoring(); |
59 | 60 |
60 // Returns the number of keypresses. The starting point from when it is | 61 // Returns the number of keypresses. The starting point from when it is |
61 // counted is not guaranteed, but consistent within the pair of calls of | 62 // counted is not guaranteed, but consistent within the pair of calls of |
62 // EnableKeyPressMonitoring and DisableKeyPressMonitoring. So a caller can | 63 // EnableKeyPressMonitoring and DisableKeyPressMonitoring. So a caller can |
63 // use the difference between the values returned at two times to get the | 64 // use the difference between the values returned at two times to get the |
64 // number of keypresses happened within that time period, but should not make | 65 // number of keypresses happened within that time period, but should not make |
65 // any assumption on the initial value. | 66 // any assumption on the initial value. |
66 virtual size_t GetKeyPressCount() const = 0; | 67 virtual size_t GetKeyPressCount() const = 0; |
67 | 68 |
68 protected: | 69 protected: |
| 70 friend class base::RefCountedThreadSafe<UserInputMonitor>; |
| 71 virtual ~UserInputMonitor(); |
| 72 |
69 // Called by the platform-specific sub-classes to propagate the events to the | 73 // Called by the platform-specific sub-classes to propagate the events to the |
70 // listeners. | 74 // listeners. |
71 void OnMouseEvent(const SkIPoint& position); | 75 void OnMouseEvent(const SkIPoint& position); |
72 | 76 |
73 private: | 77 private: |
74 virtual void StartMouseMonitoring() = 0; | 78 virtual void StartMouseMonitoring() = 0; |
75 virtual void StopMouseMonitoring() = 0; | 79 virtual void StopMouseMonitoring() = 0; |
76 virtual void StartKeyboardMonitoring() = 0; | 80 virtual void StartKeyboardMonitoring() = 0; |
77 virtual void StopKeyboardMonitoring() = 0; | 81 virtual void StopKeyboardMonitoring() = 0; |
78 | 82 |
79 base::Lock lock_; | 83 base::Lock lock_; |
80 ObserverList<MouseEventListener, true> mouse_listeners_; | 84 ObserverList<MouseEventListener, true> mouse_listeners_; |
81 bool monitoring_mouse_; | 85 bool monitoring_mouse_; |
82 size_t key_press_counter_references_; | 86 size_t key_press_counter_references_; |
83 | 87 |
84 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); | 88 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor); |
85 }; | 89 }; |
86 | 90 |
87 } // namespace media | 91 } // namespace media |
88 | 92 |
89 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ | 93 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_ |
OLD | NEW |