blob: f72e1e05889b77de226ecc13bf2b861ef1c00b2f [file] [log] [blame]
[email protected]217690d2012-01-27 07:33:111// 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#include "content/renderer/mouse_lock_dispatcher.h"
6
[email protected]89054502012-06-03 10:29:247#include "base/logging.h"
[email protected]2255a9332013-06-17 05:12:318#include "third_party/WebKit/public/web/WebInputEvent.h"
[email protected]217690d2012-01-27 07:33:119
[email protected]e9ff79c2012-10-19 21:31:2610namespace content {
11
[email protected]89054502012-06-03 10:29:2412MouseLockDispatcher::MouseLockDispatcher() : mouse_locked_(false),
13 pending_lock_request_(false),
14 pending_unlock_request_(false),
15 unlocked_by_target_(false),
16 target_(NULL) {
[email protected]217690d2012-01-27 07:33:1117}
18
19MouseLockDispatcher::~MouseLockDispatcher() {
20}
21
22bool MouseLockDispatcher::LockMouse(LockTarget* target) {
23 if (MouseLockedOrPendingAction())
24 return false;
25
26 pending_lock_request_ = true;
27 target_ = target;
28
[email protected]89054502012-06-03 10:29:2429 SendLockMouseRequest(unlocked_by_target_);
[email protected]a9c81f02012-06-01 00:15:4430 unlocked_by_target_ = false;
[email protected]217690d2012-01-27 07:33:1131 return true;
32}
33
34void MouseLockDispatcher::UnlockMouse(LockTarget* target) {
35 if (target && target == target_ && !pending_unlock_request_) {
36 pending_unlock_request_ = true;
[email protected]275c0fdf2012-06-05 20:28:2137
38 // When a target application voluntarily unlocks the mouse we permit
39 // relocking the mouse silently and with no user gesture requirement.
40 // Check that the lock request is not currently pending and not yet
41 // accepted by the browser process before setting |unlocked_by_target_|.
42 if (!pending_lock_request_)
43 unlocked_by_target_ = true;
44
[email protected]89054502012-06-03 10:29:2445 SendUnlockMouseRequest();
[email protected]217690d2012-01-27 07:33:1146 }
47}
48
49void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) {
50 if (target == target_) {
51 UnlockMouse(target);
52 target_ = NULL;
53 }
54}
55
56bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) {
57 return mouse_locked_ && target_ == target;
58}
59
60bool MouseLockDispatcher::WillHandleMouseEvent(
[email protected]180ef242013-11-07 06:50:4661 const blink::WebMouseEvent& event) {
[email protected]217690d2012-01-27 07:33:1162 if (mouse_locked_ && target_)
63 return target_->HandleMouseLockedInputEvent(event);
64 return false;
65}
66
[email protected]217690d2012-01-27 07:33:1167void MouseLockDispatcher::OnLockMouseACK(bool succeeded) {
68 DCHECK(!mouse_locked_ && pending_lock_request_);
69
70 mouse_locked_ = succeeded;
71 pending_lock_request_ = false;
72 if (pending_unlock_request_ && !succeeded) {
73 // We have sent an unlock request after the lock request. However, since
74 // the lock request has failed, the unlock request will be ignored by the
75 // browser side and there won't be any response to it.
76 pending_unlock_request_ = false;
77 }
78
79 LockTarget* last_target = target_;
80 if (!succeeded)
81 target_ = NULL;
82
83 // Callbacks made after all state modification to prevent reentrant errors
84 // such as OnLockMouseACK() synchronously calling LockMouse().
85
86 if (last_target)
87 last_target->OnLockMouseACK(succeeded);
[email protected]217690d2012-01-27 07:33:1188}
89
90void MouseLockDispatcher::OnMouseLockLost() {
91 DCHECK(mouse_locked_ && !pending_lock_request_);
92
93 mouse_locked_ = false;
94 pending_unlock_request_ = false;
95
96 LockTarget* last_target = target_;
97 target_ = NULL;
98
99 // Callbacks made after all state modification to prevent reentrant errors
100 // such as OnMouseLockLost() synchronously calling LockMouse().
101
102 if (last_target)
103 last_target->OnMouseLockLost();
104}
[email protected]e9ff79c2012-10-19 21:31:26105
106} // namespace content