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 bool MouseLockDispatcher::LockMouse(LockTarget* target) { |
| 25 if (MouseLockedOrPendingAction()) |
| 26 return false; |
| 27 |
| 28 pending_lock_request_ = true; |
| 29 target_ = target; |
| 30 |
| 31 Send(new ViewHostMsg_LockMouse(routing_id())); |
| 32 return true; |
| 33 } |
| 34 |
| 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())); |
| 39 } |
| 40 } |
| 41 |
| 42 void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) { |
| 43 if (target == target_) { |
| 44 UnlockMouse(target); |
| 45 target_ = NULL; |
| 46 } |
| 47 } |
| 48 |
| 49 bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) { |
| 50 return mouse_locked_ && target_ == target; |
| 51 } |
| 52 |
| 53 bool MouseLockDispatcher::WillHandleMouseEvent( |
| 54 const WebKit::WebMouseEvent& event) { |
| 55 if (mouse_locked_ && target_) |
| 56 return target_->HandleMouseLockedInputEvent(event); |
| 57 return false; |
| 58 } |
| 59 |
| 60 bool MouseLockDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 61 bool handled = true; |
| 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) |
| 66 IPC_END_MESSAGE_MAP() |
| 67 return handled; |
| 68 } |
| 69 |
| 70 void MouseLockDispatcher::OnLockMouseACK(bool succeeded) { |
| 71 DCHECK(!mouse_locked_ && pending_lock_request_); |
| 72 |
| 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; |
| 80 } |
| 81 |
| 82 LockTarget* last_target = target_; |
| 83 if (!succeeded) |
| 84 target_ = NULL; |
| 85 |
| 86 // Callbacks made after all state modification to prevent reentrant errors |
| 87 // such as OnLockMouseACK() synchronously calling LockMouse(). |
| 88 |
| 89 if (last_target) |
| 90 last_target->OnLockMouseACK(succeeded); |
| 91 |
| 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(); |
| 100 } |
| 101 |
| 102 void MouseLockDispatcher::OnMouseLockLost() { |
| 103 DCHECK(mouse_locked_ && !pending_lock_request_); |
| 104 |
| 105 mouse_locked_ = false; |
| 106 pending_unlock_request_ = false; |
| 107 |
| 108 LockTarget* last_target = target_; |
| 109 target_ = NULL; |
| 110 |
| 111 // Callbacks made after all state modification to prevent reentrant errors |
| 112 // such as OnMouseLockLost() synchronously calling LockMouse(). |
| 113 |
| 114 if (last_target) |
| 115 last_target->OnMouseLockLost(); |
| 116 } |
| 117 |
OLD | NEW |