| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/mouse_lock_dispatcher.h" | 5 #include "content/renderer/mouse_lock_dispatcher.h" |
| 6 | 6 |
| 7 #include "content/common/view_messages.h" | 7 #include "base/logging.h" |
| 8 #include "content/renderer/render_view_impl.h" | |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | 8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h" | |
| 12 | 9 |
| 13 MouseLockDispatcher::MouseLockDispatcher(RenderViewImpl* render_view_impl) | 10 MouseLockDispatcher::MouseLockDispatcher() : mouse_locked_(false), |
| 14 : content::RenderViewObserver(render_view_impl), | 11 pending_lock_request_(false), |
| 15 render_view_impl_(render_view_impl), | 12 pending_unlock_request_(false), |
| 16 mouse_locked_(false), | 13 target_(NULL) { |
| 17 pending_lock_request_(false), | |
| 18 pending_unlock_request_(false), | |
| 19 target_(NULL) { | |
| 20 } | 14 } |
| 21 | 15 |
| 22 MouseLockDispatcher::~MouseLockDispatcher() { | 16 MouseLockDispatcher::~MouseLockDispatcher() { |
| 23 } | 17 } |
| 24 | 18 |
| 25 bool MouseLockDispatcher::LockMouse(LockTarget* target) { | 19 bool MouseLockDispatcher::LockMouse(LockTarget* target) { |
| 26 if (MouseLockedOrPendingAction()) | 20 if (MouseLockedOrPendingAction()) |
| 27 return false; | 21 return false; |
| 28 | 22 |
| 29 pending_lock_request_ = true; | 23 pending_lock_request_ = true; |
| 30 target_ = target; | 24 target_ = target; |
| 31 | 25 |
| 32 bool user_gesture = | 26 SendLockMouseRequest(); |
| 33 render_view_impl_->webview() && | |
| 34 render_view_impl_->webview()->mainFrame() && | |
| 35 render_view_impl_->webview()->mainFrame()->isProcessingUserGesture(); | |
| 36 | |
| 37 Send(new ViewHostMsg_LockMouse(routing_id(), user_gesture)); | |
| 38 return true; | 27 return true; |
| 39 } | 28 } |
| 40 | 29 |
| 41 void MouseLockDispatcher::UnlockMouse(LockTarget* target) { | 30 void MouseLockDispatcher::UnlockMouse(LockTarget* target) { |
| 42 if (target && target == target_ && !pending_unlock_request_) { | 31 if (target && target == target_ && !pending_unlock_request_) { |
| 43 pending_unlock_request_ = true; | 32 pending_unlock_request_ = true; |
| 44 Send(new ViewHostMsg_UnlockMouse(routing_id())); | 33 SendUnlockMouseRequest(); |
| 45 } | 34 } |
| 46 } | 35 } |
| 47 | 36 |
| 48 void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) { | 37 void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) { |
| 49 if (target == target_) { | 38 if (target == target_) { |
| 50 UnlockMouse(target); | 39 UnlockMouse(target); |
| 51 target_ = NULL; | 40 target_ = NULL; |
| 52 } | 41 } |
| 53 } | 42 } |
| 54 | 43 |
| 55 bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) { | 44 bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) { |
| 56 return mouse_locked_ && target_ == target; | 45 return mouse_locked_ && target_ == target; |
| 57 } | 46 } |
| 58 | 47 |
| 59 bool MouseLockDispatcher::WillHandleMouseEvent( | 48 bool MouseLockDispatcher::WillHandleMouseEvent( |
| 60 const WebKit::WebMouseEvent& event) { | 49 const WebKit::WebMouseEvent& event) { |
| 61 if (mouse_locked_ && target_) | 50 if (mouse_locked_ && target_) |
| 62 return target_->HandleMouseLockedInputEvent(event); | 51 return target_->HandleMouseLockedInputEvent(event); |
| 63 return false; | 52 return false; |
| 64 } | 53 } |
| 65 | 54 |
| 66 bool MouseLockDispatcher::OnMessageReceived(const IPC::Message& message) { | |
| 67 bool handled = true; | |
| 68 IPC_BEGIN_MESSAGE_MAP(MouseLockDispatcher, message) | |
| 69 IPC_MESSAGE_HANDLER(ViewMsg_LockMouse_ACK, OnLockMouseACK) | |
| 70 IPC_MESSAGE_HANDLER(ViewMsg_MouseLockLost, OnMouseLockLost) | |
| 71 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 72 IPC_END_MESSAGE_MAP() | |
| 73 return handled; | |
| 74 } | |
| 75 | |
| 76 void MouseLockDispatcher::OnLockMouseACK(bool succeeded) { | 55 void MouseLockDispatcher::OnLockMouseACK(bool succeeded) { |
| 77 DCHECK(!mouse_locked_ && pending_lock_request_); | 56 DCHECK(!mouse_locked_ && pending_lock_request_); |
| 78 | 57 |
| 79 mouse_locked_ = succeeded; | 58 mouse_locked_ = succeeded; |
| 80 pending_lock_request_ = false; | 59 pending_lock_request_ = false; |
| 81 if (pending_unlock_request_ && !succeeded) { | 60 if (pending_unlock_request_ && !succeeded) { |
| 82 // We have sent an unlock request after the lock request. However, since | 61 // We have sent an unlock request after the lock request. However, since |
| 83 // the lock request has failed, the unlock request will be ignored by the | 62 // the lock request has failed, the unlock request will be ignored by the |
| 84 // browser side and there won't be any response to it. | 63 // browser side and there won't be any response to it. |
| 85 pending_unlock_request_ = false; | 64 pending_unlock_request_ = false; |
| 86 } | 65 } |
| 87 | 66 |
| 88 LockTarget* last_target = target_; | 67 LockTarget* last_target = target_; |
| 89 if (!succeeded) | 68 if (!succeeded) |
| 90 target_ = NULL; | 69 target_ = NULL; |
| 91 | 70 |
| 92 // Callbacks made after all state modification to prevent reentrant errors | 71 // Callbacks made after all state modification to prevent reentrant errors |
| 93 // such as OnLockMouseACK() synchronously calling LockMouse(). | 72 // such as OnLockMouseACK() synchronously calling LockMouse(). |
| 94 | 73 |
| 95 if (last_target) | 74 if (last_target) |
| 96 last_target->OnLockMouseACK(succeeded); | 75 last_target->OnLockMouseACK(succeeded); |
| 97 | |
| 98 // Mouse Lock removes the system cursor and provides all mouse motion as | |
| 99 // .movementX/Y values on events all sent to a fixed target. This requires | |
| 100 // content to specifically request the mode to be entered. | |
| 101 // Mouse Capture is implicitly given for the duration of a drag event, and | |
| 102 // sends all mouse events to the initial target of the drag. | |
| 103 // If Lock is entered it supercedes any in progress Capture. | |
| 104 if (succeeded && render_view_impl_->webwidget()) | |
| 105 render_view_impl_->webwidget()->mouseCaptureLost(); | |
| 106 } | 76 } |
| 107 | 77 |
| 108 void MouseLockDispatcher::OnMouseLockLost() { | 78 void MouseLockDispatcher::OnMouseLockLost() { |
| 109 DCHECK(mouse_locked_ && !pending_lock_request_); | 79 DCHECK(mouse_locked_ && !pending_lock_request_); |
| 110 | 80 |
| 111 mouse_locked_ = false; | 81 mouse_locked_ = false; |
| 112 pending_unlock_request_ = false; | 82 pending_unlock_request_ = false; |
| 113 | 83 |
| 114 LockTarget* last_target = target_; | 84 LockTarget* last_target = target_; |
| 115 target_ = NULL; | 85 target_ = NULL; |
| 116 | 86 |
| 117 // Callbacks made after all state modification to prevent reentrant errors | 87 // Callbacks made after all state modification to prevent reentrant errors |
| 118 // such as OnMouseLockLost() synchronously calling LockMouse(). | 88 // such as OnMouseLockLost() synchronously calling LockMouse(). |
| 119 | 89 |
| 120 if (last_target) | 90 if (last_target) |
| 121 last_target->OnMouseLockLost(); | 91 last_target->OnMouseLockLost(); |
| 122 } | 92 } |
| 123 | |
| OLD | NEW |