[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 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 | #include "content/renderer/mouse_lock_dispatcher.h" | ||||
6 | |||||
[email protected] | 8905450 | 2012-06-03 10:29:24 | [diff] [blame] | 7 | #include "base/logging.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 8 | #include "third_party/blink/public/platform/web_input_event.h" |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 9 | |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 10 | namespace content { |
11 | |||||
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 12 | MouseLockDispatcher::MouseLockDispatcher() |
13 | : mouse_locked_(false), | ||||
14 | pending_lock_request_(false), | ||||
15 | pending_unlock_request_(false), | ||||
16 | target_(nullptr) {} | ||||
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 17 | |
18 | MouseLockDispatcher::~MouseLockDispatcher() { | ||||
19 | } | ||||
20 | |||||
21 | bool MouseLockDispatcher::LockMouse(LockTarget* target) { | ||||
22 | if (MouseLockedOrPendingAction()) | ||||
23 | return false; | ||||
24 | |||||
25 | pending_lock_request_ = true; | ||||
26 | target_ = target; | ||||
27 | |||||
chongz | ba34097 | 2017-06-07 14:39:56 | [diff] [blame] | 28 | SendLockMouseRequest(); |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 29 | return true; |
30 | } | ||||
31 | |||||
32 | void MouseLockDispatcher::UnlockMouse(LockTarget* target) { | ||||
33 | if (target && target == target_ && !pending_unlock_request_) { | ||||
34 | pending_unlock_request_ = true; | ||||
[email protected] | 275c0fdf | 2012-06-05 20:28:21 | [diff] [blame] | 35 | |
[email protected] | 8905450 | 2012-06-03 10:29:24 | [diff] [blame] | 36 | SendUnlockMouseRequest(); |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 37 | } |
38 | } | ||||
39 | |||||
40 | void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) { | ||||
41 | if (target == target_) { | ||||
42 | UnlockMouse(target); | ||||
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 43 | target_ = nullptr; |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 44 | } |
45 | } | ||||
46 | |||||
Dave Tapuska | 54c97c7 | 2018-01-03 15:44:58 | [diff] [blame] | 47 | void MouseLockDispatcher::ClearLockTarget() { |
48 | OnLockTargetDestroyed(target_); | ||||
49 | } | ||||
50 | |||||
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 51 | bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) { |
52 | return mouse_locked_ && target_ == target; | ||||
53 | } | ||||
54 | |||||
55 | bool MouseLockDispatcher::WillHandleMouseEvent( | ||||
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 56 | const blink::WebMouseEvent& event) { |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 57 | if (mouse_locked_ && target_) |
58 | return target_->HandleMouseLockedInputEvent(event); | ||||
59 | return false; | ||||
60 | } | ||||
61 | |||||
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 62 | void 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 Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 76 | target_ = nullptr; |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 77 | |
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] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 83 | } |
84 | |||||
85 | void 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 Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 92 | target_ = nullptr; |
[email protected] | 217690d | 2012-01-27 07:33:11 | [diff] [blame] | 93 | |
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] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 100 | |
101 | } // namespace content |