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.
4
5#ifndef CONTENT_RENDERER_MOUSE_LOCK_DISPATCHER_H_
6#define CONTENT_RENDERER_MOUSE_LOCK_DISPATCHER_H_
7
8#include "base/basictypes.h"
9#include "content/common/content_export.h"
10
11namespace blink {
12class WebMouseEvent;
13}  // namespace blink
14
15namespace content {
16
17class CONTENT_EXPORT MouseLockDispatcher {
18 public:
19  MouseLockDispatcher();
20  virtual ~MouseLockDispatcher();
21
22  class LockTarget {
23   public:
24    virtual ~LockTarget() {}
25    // A mouse lock request was pending and this reports success or failure.
26    virtual void OnLockMouseACK(bool succeeded) = 0;
27    // A mouse lock was in place, but has been lost.
28    virtual void OnMouseLockLost() = 0;
29    // A mouse lock is enabled and mouse events are being delievered.
30    virtual bool HandleMouseLockedInputEvent(
31        const blink::WebMouseEvent& event) = 0;
32  };
33
34  // Locks the mouse to the |target|. If true is returned, an asynchronous
35  // response to target->OnLockMouseACK() will follow.
36  bool LockMouse(LockTarget* target);
37  // Request to unlock the mouse. An asynchronous response to
38  // target->OnMouseLockLost() will follow.
39  void UnlockMouse(LockTarget* target);
40  // Clears out the reference to the |target| because it has or is being
41  // destroyed. Unlocks if locked. The pointer will not be accessed.
42  void OnLockTargetDestroyed(LockTarget* target);
43  bool IsMouseLockedTo(LockTarget* target);
44
45  // Allow lock target to consumed a mouse event, if it does return true.
46  bool WillHandleMouseEvent(const blink::WebMouseEvent& event);
47
48  // Subclasses or users have to call these methods to report mouse lock events
49  // from the browser.
50  void OnLockMouseACK(bool succeeded);
51  void OnMouseLockLost();
52
53 protected:
54  // Subclasses must implement these methods to send mouse lock requests to the
55  // browser.
56  virtual void SendLockMouseRequest(bool unlocked_by_target) = 0;
57  virtual void SendUnlockMouseRequest() = 0;
58
59 private:
60  bool MouseLockedOrPendingAction() const {
61    return mouse_locked_ || pending_lock_request_ || pending_unlock_request_;
62  }
63
64  bool mouse_locked_;
65  // If both |pending_lock_request_| and |pending_unlock_request_| are true,
66  // it means a lock request was sent before an unlock request and we haven't
67  // received responses for them. The logic in LockMouse() makes sure that a
68  // lock request won't be sent when there is a pending unlock request.
69  bool pending_lock_request_;
70  bool pending_unlock_request_;
71
72  // Used when locking to indicate when a target application has voluntarily
73  // unlocked and desires to relock the mouse. If the mouse is unlocked due
74  // to ESC being pressed by the user, this will be false
75  bool unlocked_by_target_;
76
77  // |target_| is the pending or current owner of mouse lock. We retain a non
78  // owning reference here that must be cleared by |OnLockTargetDestroyed|
79  // when it is destroyed.
80  LockTarget* target_;
81
82  DISALLOW_COPY_AND_ASSIGN(MouseLockDispatcher);
83};
84
85}  // namespace content
86
87#endif  // CONTENT_RENDERER_MOUSE_LOCK_DISPATCHER_H_
88