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.
4
5#ifndef MEDIA_BASE_USER_INPUT_MONITOR_H_
6#define MEDIA_BASE_USER_INPUT_MONITOR_H_
7
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"
13
14struct SkIPoint;
15
16namespace base {
17class SingleThreadTaskRunner;
18}  // namespace base
19
20namespace media {
21
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.
25class 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;
32
33   protected:
34    virtual ~MouseEventListener() {}
35  };
36  typedef ObserverListThreadSafe<UserInputMonitor::MouseEventListener>
37      MouseListenerList;
38
39  UserInputMonitor();
40  virtual ~UserInputMonitor();
41
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);
48
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);
54
55  // A caller must call EnableKeyPressMonitoring and
56  // DisableKeyPressMonitoring in pair.
57  void EnableKeyPressMonitoring();
58  void DisableKeyPressMonitoring();
59
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;
67
68 protected:
69  scoped_refptr<MouseListenerList> mouse_listeners() {
70    return mouse_listeners_;
71  }
72
73 private:
74  virtual void StartKeyboardMonitoring() = 0;
75  virtual void StopKeyboardMonitoring() = 0;
76  virtual void StartMouseMonitoring() = 0;
77  virtual void StopMouseMonitoring() = 0;
78
79  base::Lock lock_;
80  size_t key_press_counter_references_;
81  size_t mouse_listeners_count_;
82  scoped_refptr<MouseListenerList> mouse_listeners_;
83
84  DISALLOW_COPY_AND_ASSIGN(UserInputMonitor);
85};
86
87}  // namespace media
88
89#endif  // MEDIA_BASE_USER_INPUT_MONITOR_H_
90