Chromium Code Reviews| 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 mouse_lock_webwidget_owner_(NULL), | |
| 19 mouse_lock_plugin_owner_(NULL) { | |
| 20 } | |
| 21 | |
| 22 MouseLockDispatcher::~MouseLockDispatcher() { | |
| 23 } | |
| 24 | |
| 25 bool MouseLockDispatcher::LockMouse(WebKit::WebWidget* webwidget, | |
| 26 webkit::ppapi::PluginInstance* plugin) { | |
|
yzshen1
2012/01/17 23:39:25
According to our style guide, it should align with
scheib
2012/01/18 17:54:58
Done.
| |
| 27 DCHECK(!(webwidget && plugin)); // Only one mouse lock target at a time. | |
| 28 | |
| 29 if (MouseLockedOrPendingAction()) | |
| 30 return false; | |
| 31 | |
| 32 pending_lock_request_ = true; | |
| 33 DCHECK(!mouse_lock_webwidget_owner_ && !mouse_lock_plugin_owner_); | |
| 34 mouse_lock_webwidget_owner_ = webwidget; | |
| 35 mouse_lock_plugin_owner_ = plugin; | |
| 36 | |
| 37 Send(new ViewHostMsg_LockMouse(routing_id())); | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 void MouseLockDispatcher::UnlockMouse(WebKit::WebWidget* webwidget, | |
| 42 webkit::ppapi::PluginInstance* plugin) { | |
|
yzshen1
2012/01/17 23:39:25
Ditto.
scheib
2012/01/18 17:54:58
Done.
| |
| 43 if (!MouseLockedOrPendingAction() || pending_unlock_request_) | |
| 44 return; | |
| 45 | |
| 46 // Ignore unless the unlock request is for the current lock holder. | |
| 47 if (webwidget == mouse_lock_webwidget_owner_ || | |
| 48 plugin == mouse_lock_plugin_owner_) { | |
| 49 pending_unlock_request_ = true; | |
| 50 Send(new ViewHostMsg_UnlockMouse(routing_id())); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 bool MouseLockDispatcher::IsPointerLockedTo(WebKit::WebWidget* webwidget) { | |
| 55 return mouse_locked_ && mouse_lock_webwidget_owner_ == webwidget; | |
| 56 } | |
| 57 | |
| 58 bool MouseLockDispatcher::IsMouseLockedTo( | |
| 59 webkit::ppapi::PluginInstance* plugin) { | |
| 60 return mouse_locked_ && mouse_lock_plugin_owner_ == plugin; | |
| 61 } | |
| 62 | |
| 63 bool MouseLockDispatcher::WillHandleMouseEvent( | |
| 64 const WebKit::WebMouseEvent& event) { | |
| 65 if (mouse_locked_ && mouse_lock_plugin_owner_) { | |
| 66 DCHECK(!mouse_lock_webwidget_owner_); | |
| 67 mouse_lock_plugin_owner_->HandleMouseLockedInputEvent(event); | |
| 68 | |
| 69 // mouse_lock_webwidget_owner_ handles mouse lock in handleInputEvent(). | |
| 70 | |
| 71 // If the mouse is locked, only the current owner of the mouse lock can | |
| 72 // process mouse events. | |
| 73 return true; | |
| 74 } | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 bool MouseLockDispatcher::OnMessageReceived(const IPC::Message& message) { | |
| 79 bool handled = true; | |
| 80 IPC_BEGIN_MESSAGE_MAP(MouseLockDispatcher, message) | |
| 81 IPC_MESSAGE_HANDLER(ViewMsg_LockMouse_ACK, OnLockMouseACK) | |
| 82 IPC_MESSAGE_HANDLER(ViewMsg_MouseLockLost, OnMouseLockLost) | |
| 83 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 84 IPC_END_MESSAGE_MAP() | |
| 85 return handled; | |
| 86 } | |
| 87 | |
| 88 void MouseLockDispatcher::OnLockMouseACK(bool succeeded) { | |
| 89 // Mouse Lock removes the system cursor and provides all mouse motion as | |
| 90 // .movementX/Y values on events all sent to a fixed target. This requires | |
| 91 // content to specifically request the mode to be entered. | |
| 92 // Mouse Capture is implicitly given for the duration of a drag event, and | |
| 93 // sends all mouse events to the initial target of the drag. | |
| 94 // If Lock is entered it supercedes any in progress Capture. | |
| 95 if (succeeded && render_view_impl_->webwidget()) | |
| 96 render_view_impl_->webwidget()->mouseCaptureLost(); | |
| 97 | |
| 98 DCHECK(!mouse_locked_ && pending_lock_request_); | |
| 99 | |
| 100 mouse_locked_ = succeeded; | |
| 101 pending_lock_request_ = false; | |
| 102 if (pending_unlock_request_ && !succeeded) { | |
| 103 // We have sent an unlock request after the lock request. However, since | |
| 104 // the lock request has failed, the unlock request will be ignored by the | |
| 105 // browser side and there won't be any response to it. | |
| 106 pending_unlock_request_ = false; | |
| 107 } | |
| 108 | |
| 109 if (mouse_lock_webwidget_owner_ || mouse_lock_plugin_owner_) { | |
| 110 WebKit::WebWidget* last_webwidget_owner = mouse_lock_webwidget_owner_; | |
| 111 webkit::ppapi::PluginInstance* last_plugin_owner = | |
| 112 mouse_lock_plugin_owner_; | |
| 113 | |
| 114 if (!succeeded) { | |
| 115 // Reset mouse lock owner to NULL before calling OnLockMouseACK(), so | |
| 116 // that if OnLockMouseACK() results in calls to any mouse lock method | |
| 117 // (e.g., LockMouse()), the method will see consistent internal state. | |
| 118 mouse_lock_webwidget_owner_ = NULL; | |
| 119 mouse_lock_plugin_owner_ = NULL; | |
| 120 } | |
| 121 | |
| 122 DCHECK(!mouse_lock_webwidget_owner_ || !mouse_lock_plugin_owner_); | |
| 123 if (last_webwidget_owner) { | |
| 124 // TODO(scheib): Enable after WebKit pointer lock API has landed. | |
| 125 // | |
| 126 // if (succeeded) | |
| 127 // last_webwidget_owner->didCompletePointerLock(); | |
| 128 // else | |
| 129 // last_webwidget_owner->didNotCompletePointerLock(); | |
| 130 } | |
| 131 if (last_plugin_owner) | |
| 132 last_plugin_owner->OnLockMouseACK(succeeded); | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 void MouseLockDispatcher::OnMouseLockLost() { | |
| 137 DCHECK(mouse_locked_ && !pending_lock_request_); | |
| 138 | |
| 139 mouse_locked_ = false; | |
| 140 pending_unlock_request_ = false; | |
| 141 if (mouse_lock_webwidget_owner_ || mouse_lock_plugin_owner_) { | |
| 142 // TODO(scheib): Enable after WebKit pointer lock API has landed. | |
| 143 // | |
| 144 // WebKit::WebWidget* last_webwidget_owner = mouse_lock_webwidget_owner_; | |
| 145 webkit::ppapi::PluginInstance* last_plugin_owner = | |
| 146 mouse_lock_plugin_owner_; | |
| 147 | |
| 148 // Reset mouse lock owner to NULL before calling | |
| 149 // didLosePointerLock/OnMouseLockLost(), so | |
| 150 // that a reentrant call (e.g. to, LockMouse()) will see consistent | |
| 151 // internal state. | |
| 152 mouse_lock_webwidget_owner_ = NULL; | |
| 153 mouse_lock_plugin_owner_ = NULL; | |
| 154 | |
| 155 DCHECK(!mouse_lock_webwidget_owner_ || !mouse_lock_plugin_owner_); | |
| 156 // TODO(scheib): Enable after WebKit pointer lock API has landed. | |
| 157 // | |
| 158 // if (last_webwidget_owner) | |
| 159 // last_webwidget_owner->didLosePointerLock(); | |
| 160 if (last_plugin_owner) | |
| 161 last_plugin_owner->OnMouseLockLost(); | |
| 162 } | |
| 163 } | |
| 164 | |
| OLD | NEW |