blob: 0749c9c9c2176bfc94324618ba1f74434c4b0254 [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"
Blink Reformata30d4232018-04-07 15:31:068#include "third_party/blink/public/platform/web_input_event.h"
[email protected]217690d2012-01-27 07:33:119
[email protected]e9ff79c2012-10-19 21:31:2610namespace content {
11
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2812MouseLockDispatcher::MouseLockDispatcher()
13 : mouse_locked_(false),
14 pending_lock_request_(false),
15 pending_unlock_request_(false),
16 target_(nullptr) {}
[email protected]217690d2012-01-27 07:33:1117
18MouseLockDispatcher::~MouseLockDispatcher() {
19}
20
21bool MouseLockDispatcher::LockMouse(LockTarget* target) {
22 if (MouseLockedOrPendingAction())
23 return false;
24
25 pending_lock_request_ = true;
26 target_ = target;
27
chongzba340972017-06-07 14:39:5628 SendLockMouseRequest();
[email protected]217690d2012-01-27 07:33:1129 return true;
30}
31
32void MouseLockDispatcher::UnlockMouse(LockTarget* target) {
33 if (target && target == target_ && !pending_unlock_request_) {
34 pending_unlock_request_ = true;
[email protected]275c0fdf2012-06-05 20:28:2135
[email protected]89054502012-06-03 10:29:2436 SendUnlockMouseRequest();
[email protected]217690d2012-01-27 07:33:1137 }
38}
39
40void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) {
41 if (target == target_) {
42 UnlockMouse(target);
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2843 target_ = nullptr;
[email protected]217690d2012-01-27 07:33:1144 }
45}
46
Dave Tapuska54c97c72018-01-03 15:44:5847void MouseLockDispatcher::ClearLockTarget() {
48 OnLockTargetDestroyed(target_);
49}
50
[email protected]217690d2012-01-27 07:33:1151bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) {
52 return mouse_locked_ && target_ == target;
53}
54
55bool MouseLockDispatcher::WillHandleMouseEvent(
[email protected]180ef242013-11-07 06:50:4656 const blink::WebMouseEvent& event) {
[email protected]217690d2012-01-27 07:33:1157 if (mouse_locked_ && target_)
58 return target_->HandleMouseLockedInputEvent(event);
59 return false;
60}
61
[email protected]217690d2012-01-27 07:33:1162void MouseLockDispatcher::OnLockMouseACK(bool succeeded) {
63 DCHECK(!mouse_locked_ && pending_lock_request_);
64
65 mouse_locked_ = succeeded;
66 pending_lock_request_ = false;
67 if (pending_unlock_request_ && !succeeded) {
68 // We have sent an unlock request after the lock request. However, since
69 // the lock request has failed, the unlock request will be ignored by the
70 // browser side and there won't be any response to it.
71 pending_unlock_request_ = false;
72 }
73
74 LockTarget* last_target = target_;
75 if (!succeeded)
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2876 target_ = nullptr;
[email protected]217690d2012-01-27 07:33:1177
78 // Callbacks made after all state modification to prevent reentrant errors
79 // such as OnLockMouseACK() synchronously calling LockMouse().
80
81 if (last_target)
82 last_target->OnLockMouseACK(succeeded);
[email protected]217690d2012-01-27 07:33:1183}
84
85void MouseLockDispatcher::OnMouseLockLost() {
86 DCHECK(mouse_locked_ && !pending_lock_request_);
87
88 mouse_locked_ = false;
89 pending_unlock_request_ = false;
90
91 LockTarget* last_target = target_;
Ivan Kotenkov2c0d2bb32017-11-01 15:41:2892 target_ = nullptr;
[email protected]217690d2012-01-27 07:33:1193
94 // Callbacks made after all state modification to prevent reentrant errors
95 // such as OnMouseLockLost() synchronously calling LockMouse().
96
97 if (last_target)
98 last_target->OnMouseLockLost();
99}
[email protected]e9ff79c2012-10-19 21:31:26100
101} // namespace content