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.
5 #include "content/renderer/mouse_lock_dispatcher.h"
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"
12 MouseLockDispatcher::MouseLockDispatcher(RenderViewImpl
* render_view_impl
)
13 : content::RenderViewObserver(render_view_impl
),
14 render_view_impl_(render_view_impl
),
16 pending_lock_request_(false),
17 pending_unlock_request_(false),
21 MouseLockDispatcher::~MouseLockDispatcher() {
24 bool MouseLockDispatcher::LockMouse(LockTarget
* target
) {
25 if (MouseLockedOrPendingAction())
28 pending_lock_request_
= true;
31 Send(new ViewHostMsg_LockMouse(routing_id()));
35 void MouseLockDispatcher::UnlockMouse(LockTarget
* target
) {
36 if (target
&& target
== target_
&& !pending_unlock_request_
) {
37 pending_unlock_request_
= true;
38 Send(new ViewHostMsg_UnlockMouse(routing_id()));
42 void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget
* target
) {
43 if (target
== target_
) {
49 bool MouseLockDispatcher::IsMouseLockedTo(LockTarget
* target
) {
50 return mouse_locked_
&& target_
== target
;
53 bool MouseLockDispatcher::WillHandleMouseEvent(
54 const WebKit::WebMouseEvent
& event
) {
55 if (mouse_locked_
&& target_
)
56 return target_
->HandleMouseLockedInputEvent(event
);
60 bool MouseLockDispatcher::OnMessageReceived(const IPC::Message
& message
) {
62 IPC_BEGIN_MESSAGE_MAP(MouseLockDispatcher
, message
)
63 IPC_MESSAGE_HANDLER(ViewMsg_LockMouse_ACK
, OnLockMouseACK
)
64 IPC_MESSAGE_HANDLER(ViewMsg_MouseLockLost
, OnMouseLockLost
)
65 IPC_MESSAGE_UNHANDLED(handled
= false)
70 void MouseLockDispatcher::OnLockMouseACK(bool succeeded
) {
71 DCHECK(!mouse_locked_
&& pending_lock_request_
);
73 mouse_locked_
= succeeded
;
74 pending_lock_request_
= false;
75 if (pending_unlock_request_
&& !succeeded
) {
76 // We have sent an unlock request after the lock request. However, since
77 // the lock request has failed, the unlock request will be ignored by the
78 // browser side and there won't be any response to it.
79 pending_unlock_request_
= false;
82 LockTarget
* last_target
= target_
;
86 // Callbacks made after all state modification to prevent reentrant errors
87 // such as OnLockMouseACK() synchronously calling LockMouse().
90 last_target
->OnLockMouseACK(succeeded
);
92 // Mouse Lock removes the system cursor and provides all mouse motion as
93 // .movementX/Y values on events all sent to a fixed target. This requires
94 // content to specifically request the mode to be entered.
95 // Mouse Capture is implicitly given for the duration of a drag event, and
96 // sends all mouse events to the initial target of the drag.
97 // If Lock is entered it supercedes any in progress Capture.
98 if (succeeded
&& render_view_impl_
->webwidget())
99 render_view_impl_
->webwidget()->mouseCaptureLost();
102 void MouseLockDispatcher::OnMouseLockLost() {
103 DCHECK(mouse_locked_
&& !pending_lock_request_
);
105 mouse_locked_
= false;
106 pending_unlock_request_
= false;
108 LockTarget
* last_target
= target_
;
111 // Callbacks made after all state modification to prevent reentrant errors
112 // such as OnMouseLockLost() synchronously calling LockMouse().
115 last_target
->OnMouseLockLost();