OLD | NEW |
---|---|
(Empty) | |
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 | |
7 #include "content/common/view_messages.h" | |
8 #include "content/renderer/render_view_impl.h" | |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h" | |
11 | |
12 MouseLockDispatcher::MouseLockDispatcher(RenderViewImpl* render_view_impl) | |
13 : content::RenderViewObserver(render_view_impl), | |
14 render_view_impl_(render_view_impl), | |
15 mouse_locked_(false), | |
16 pending_lock_request_(false), | |
17 pending_unlock_request_(false), | |
18 target_(NULL) { | |
19 } | |
20 | |
21 MouseLockDispatcher::~MouseLockDispatcher() { | |
22 } | |
23 | |
24 namespace { | |
yzshen1
2012/01/24 18:56:08
It is better to put it on top of the file (and bel
scheib
2012/01/25 00:27:11
Done.
| |
25 | |
26 class PluginInstanceLockTarget : public MouseLockDispatcher::LockTarget { | |
27 public: | |
28 PluginInstanceLockTarget(webkit::ppapi::PluginInstance* plugin) | |
29 : plugin_(plugin) { } | |
piman
2012/01/24 02:27:37
nit: indentation before ':' should be +4 (6 total)
yzshen1
2012/01/24 18:56:08
nit: I think we don't put a space between {}.
(and
scheib
2012/01/25 00:27:11
Done.
| |
30 | |
31 virtual void OnLockMouseACK(bool succeeded) OVERRIDE { | |
32 plugin_->OnLockMouseACK(succeeded); | |
33 } | |
34 | |
35 virtual void OnMouseLockLost() OVERRIDE { | |
36 plugin_->OnMouseLockLost(); | |
37 } | |
38 | |
39 virtual bool HandleMouseLockedInputEvent( | |
40 const WebKit::WebMouseEvent &event) OVERRIDE { | |
41 plugin_->HandleMouseLockedInputEvent(event); | |
42 return true; | |
43 } | |
44 | |
45 webkit::ppapi::PluginInstance* plugin_; | |
46 }; | |
47 | |
48 class WebWidgetLockTarget : public MouseLockDispatcher::LockTarget { | |
49 public: | |
50 WebWidgetLockTarget (WebKit::WebWidget* webwidget) | |
51 : webwidget_(webwidget) { } | |
piman
2012/01/24 02:27:37
nit: indentation before ':' should be +4 (6 total)
scheib
2012/01/25 00:27:11
Done.
| |
52 | |
53 virtual void OnLockMouseACK(bool succeeded) OVERRIDE { | |
54 // TODO(scheib): Enable after WebKit pointer lock API has landed. | |
55 // if (succeeded) | |
56 // webwidget_->didAcquirePointerLock(); | |
57 // else | |
58 // webwidget_->didNotAcquirePointerLock(); | |
59 } | |
60 | |
61 virtual void OnMouseLockLost() OVERRIDE { | |
62 // TODO(scheib): Enable after WebKit pointer lock API has landed. | |
63 // webwidget_->didLosePointerLock(); | |
64 } | |
65 | |
66 virtual bool HandleMouseLockedInputEvent( | |
67 const WebKit::WebMouseEvent &event) OVERRIDE { | |
68 // The WebWidget handles mouse lock in WebKit's handleInputEvent(). | |
69 return false; | |
70 } | |
71 | |
72 WebKit::WebWidget* webwidget_; | |
73 }; | |
piman
2012/01/24 02:27:37
High level comment: do these 2 classes need to be
scheib
2012/01/25 00:27:11
Done.
| |
74 | |
75 } // namespace | |
76 | |
77 // static | |
78 MouseLockDispatcher::LockTarget* MouseLockDispatcher::CreateLockTarget( | |
79 webkit::ppapi::PluginInstance* plugin) { | |
80 return new PluginInstanceLockTarget(plugin); | |
81 } | |
82 | |
83 // static | |
84 MouseLockDispatcher::LockTarget* MouseLockDispatcher::CreateLockTarget( | |
85 WebKit::WebWidget* webwidget) { | |
86 return new WebWidgetLockTarget(webwidget); | |
87 } | |
88 | |
89 bool MouseLockDispatcher::LockMouse(LockTarget* target) { | |
90 if (MouseLockedOrPendingAction()) | |
91 return false; | |
92 | |
93 pending_lock_request_ = true; | |
94 target_ = target; | |
95 | |
96 Send(new ViewHostMsg_LockMouse(routing_id())); | |
97 return true; | |
98 } | |
99 | |
100 void MouseLockDispatcher::UnlockMouse(LockTarget* target) { | |
101 if (target == target_ && !pending_unlock_request_) { | |
piman
2012/01/24 02:27:37
When comparing pointers to compare identity (here
yzshen1
2012/01/24 18:56:08
Please also check !NULL. Otherwise, an unlock requ
scheib
2012/01/25 00:27:11
It's a good consideration. But to add comments eve
scheib
2012/01/25 00:27:11
Done.
| |
102 pending_unlock_request_ = true; | |
103 Send(new ViewHostMsg_UnlockMouse(routing_id())); | |
104 } | |
105 } | |
106 | |
107 void MouseLockDispatcher::UnlockMouseAndClearTarget(LockTarget* target) { | |
yzshen1
2012/01/24 18:56:08
It is a little bit confusing: UnlockMouse() will a
scheib
2012/01/25 00:27:11
Done.
| |
108 if (target == target_) { | |
109 UnlockMouse(target); | |
110 target_ = NULL; | |
111 } | |
112 } | |
113 | |
114 bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) { | |
115 return mouse_locked_ && target_ == target; | |
116 } | |
117 | |
118 bool MouseLockDispatcher::WillHandleMouseEvent( | |
119 const WebKit::WebMouseEvent& event) { | |
120 if (mouse_locked_ && target_) | |
121 return target_->HandleMouseLockedInputEvent(event); | |
122 return false; | |
123 } | |
124 | |
125 bool MouseLockDispatcher::OnMessageReceived(const IPC::Message& message) { | |
126 bool handled = true; | |
127 IPC_BEGIN_MESSAGE_MAP(MouseLockDispatcher, message) | |
128 IPC_MESSAGE_HANDLER(ViewMsg_LockMouse_ACK, OnLockMouseACK) | |
129 IPC_MESSAGE_HANDLER(ViewMsg_MouseLockLost, OnMouseLockLost) | |
130 IPC_MESSAGE_UNHANDLED(handled = false) | |
131 IPC_END_MESSAGE_MAP() | |
132 return handled; | |
133 } | |
134 | |
135 void MouseLockDispatcher::OnLockMouseACK(bool succeeded) { | |
136 DCHECK(!mouse_locked_ && pending_lock_request_); | |
137 | |
138 mouse_locked_ = succeeded; | |
139 pending_lock_request_ = false; | |
140 if (pending_unlock_request_ && !succeeded) { | |
141 // We have sent an unlock request after the lock request. However, since | |
142 // the lock request has failed, the unlock request will be ignored by the | |
143 // browser side and there won't be any response to it. | |
144 pending_unlock_request_ = false; | |
145 } | |
146 | |
147 LockTarget* last_target = target_; | |
148 if (!succeeded) | |
149 target_ = NULL; | |
150 | |
151 // Callbacks made after all state modification to prevent reentrant errors | |
152 // such as OnLockMouseACK() synchronously calling LockMouse(). | |
153 | |
154 if (last_target) | |
155 last_target->OnLockMouseACK(succeeded); | |
156 | |
157 // Mouse Lock removes the system cursor and provides all mouse motion as | |
158 // .movementX/Y values on events all sent to a fixed target. This requires | |
159 // content to specifically request the mode to be entered. | |
160 // Mouse Capture is implicitly given for the duration of a drag event, and | |
161 // sends all mouse events to the initial target of the drag. | |
162 // If Lock is entered it supercedes any in progress Capture. | |
163 if (succeeded && render_view_impl_->webwidget()) | |
164 render_view_impl_->webwidget()->mouseCaptureLost(); | |
165 } | |
166 | |
167 void MouseLockDispatcher::OnMouseLockLost() { | |
168 DCHECK(mouse_locked_ && !pending_lock_request_); | |
169 | |
170 mouse_locked_ = false; | |
171 pending_unlock_request_ = false; | |
172 | |
173 LockTarget* last_target = target_; | |
174 target_ = NULL; | |
175 | |
176 // Callbacks made after all state modification to prevent reentrant errors | |
177 // such as OnMouseLockLost() synchronously calling LockMouse(). | |
178 | |
179 if (last_target) | |
180 last_target->OnMouseLockLost(); | |
181 } | |
182 | |
OLD | NEW |