Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / user_input_monitor.h
blobab572694ed3351d3e1f648718640d4d76bab03b0
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
3 // found in the LICENSE file.
5 #ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_
6 #define MEDIA_BASE_USER_INPUT_MONITOR_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/observer_list_threadsafe.h"
11 #include "base/synchronization/lock.h"
12 #include "media/base/media_export.h"
14 struct SkIPoint;
16 namespace base {
17 class SingleThreadTaskRunner;
18 } // namespace base
20 namespace media {
22 // Monitors and notifies about mouse movements and keyboard events.
23 // Thread safe. The listeners are called on the thread where the listeners are
24 // added.
25 class MEDIA_EXPORT UserInputMonitor {
26 public:
27 // The interface to receive mouse movement events.
28 class MEDIA_EXPORT MouseEventListener {
29 public:
30 // |position| is the new mouse position.
31 virtual void OnMouseMoved(const SkIPoint& position) = 0;
33 protected:
34 virtual ~MouseEventListener() {}
36 typedef ObserverListThreadSafe<UserInputMonitor::MouseEventListener>
37 MouseListenerList;
39 UserInputMonitor();
40 virtual ~UserInputMonitor();
42 // Creates a platform-specific instance of UserInputMonitor.
43 // |io_task_runner| is the task runner for an IO thread.
44 // |ui_task_runner| is the task runner for a UI thread.
45 static scoped_ptr<UserInputMonitor> Create(
46 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
47 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner);
49 // The same |listener| should only be added once.
50 // The clients should make sure to call Remove*Listener before |listener| is
51 // destroyed.
52 void AddMouseListener(MouseEventListener* listener);
53 void RemoveMouseListener(MouseEventListener* listener);
55 // A caller must call EnableKeyPressMonitoring and
56 // DisableKeyPressMonitoring in pair.
57 void EnableKeyPressMonitoring();
58 void DisableKeyPressMonitoring();
60 // 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 // EnableKeyPressMonitoring and DisableKeyPressMonitoring. So a caller can
63 // 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 // any assumption on the initial value.
66 virtual size_t GetKeyPressCount() const = 0;
68 protected:
69 scoped_refptr<MouseListenerList> mouse_listeners() {
70 return mouse_listeners_;
73 private:
74 virtual void StartKeyboardMonitoring() = 0;
75 virtual void StopKeyboardMonitoring() = 0;
76 virtual void StartMouseMonitoring() = 0;
77 virtual void StopMouseMonitoring() = 0;
79 base::Lock lock_;
80 size_t key_press_counter_references_;
81 size_t mouse_listeners_count_;
82 scoped_refptr<MouseListenerList> mouse_listeners_;
84 DISALLOW_COPY_AND_ASSIGN(UserInputMonitor);
87 } // namespace media
89 #endif // MEDIA_BASE_USER_INPUT_MONITOR_H_