Chromium Code Reviews| Index: content/renderer/mouse_lock_dispatcher.cc |
| diff --git a/content/renderer/mouse_lock_dispatcher.cc b/content/renderer/mouse_lock_dispatcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0c8715824c5d96121bb04a74358f1a07e8d1e8bc |
| --- /dev/null |
| +++ b/content/renderer/mouse_lock_dispatcher.cc |
| @@ -0,0 +1,164 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/mouse_lock_dispatcher.h" |
| + |
| +#include "content/common/view_messages.h" |
| +#include "content/renderer/render_view_impl.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" |
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebWidget.h" |
| + |
| +MouseLockDispatcher::MouseLockDispatcher(RenderViewImpl* render_view_impl) |
| + : content::RenderViewObserver(render_view_impl), |
| + render_view_impl_(render_view_impl), |
| + mouse_locked_(false), |
| + pending_lock_request_(false), |
| + pending_unlock_request_(false), |
| + mouse_lock_webwidget_owner_(NULL), |
| + mouse_lock_plugin_owner_(NULL) { |
| +} |
| + |
| +MouseLockDispatcher::~MouseLockDispatcher() { |
| +} |
| + |
| +bool MouseLockDispatcher::LockMouse(WebKit::WebWidget* webwidget, |
| + webkit::ppapi::PluginInstance* plugin) { |
| + DCHECK(!(webwidget && plugin)); // Only one mouse lock target at a time. |
|
piman
2012/01/18 19:00:59
Is it valid to lock with no owner at all?
scheib
2012/01/24 01:51:42
Refactoring has removed this issue, there is only
|
| + |
| + if (MouseLockedOrPendingAction()) |
| + return false; |
| + |
| + pending_lock_request_ = true; |
| + DCHECK(!mouse_lock_webwidget_owner_ && !mouse_lock_plugin_owner_); |
| + mouse_lock_webwidget_owner_ = webwidget; |
| + mouse_lock_plugin_owner_ = plugin; |
| + |
| + Send(new ViewHostMsg_LockMouse(routing_id())); |
| + return true; |
| +} |
| + |
| +void MouseLockDispatcher::UnlockMouse(WebKit::WebWidget* webwidget, |
| + webkit::ppapi::PluginInstance* plugin) { |
|
piman
2012/01/18 19:00:59
This gets called when the plugin instance gets del
scheib
2012/01/24 01:51:42
Addressed by creating a new method UnlockMouseAndC
|
| + if (!MouseLockedOrPendingAction() || pending_unlock_request_) |
| + return; |
| + |
| + // Ignore unless the unlock request is for the current lock holder. |
| + if (webwidget == mouse_lock_webwidget_owner_ || |
| + plugin == mouse_lock_plugin_owner_) { |
|
piman
2012/01/18 19:00:59
This test should be stricter. E.g. if I do LockMou
scheib
2012/01/24 01:51:42
Done.
|
| + pending_unlock_request_ = true; |
| + Send(new ViewHostMsg_UnlockMouse(routing_id())); |
| + } |
| +} |
| + |
| +bool MouseLockDispatcher::IsPointerLockedTo(WebKit::WebWidget* webwidget) { |
| + return mouse_locked_ && mouse_lock_webwidget_owner_ == webwidget; |
| +} |
| + |
| +bool MouseLockDispatcher::IsMouseLockedTo( |
| + webkit::ppapi::PluginInstance* plugin) { |
| + return mouse_locked_ && mouse_lock_plugin_owner_ == plugin; |
| +} |
| + |
| +bool MouseLockDispatcher::WillHandleMouseEvent( |
| + const WebKit::WebMouseEvent& event) { |
| + if (mouse_locked_ && mouse_lock_plugin_owner_) { |
| + DCHECK(!mouse_lock_webwidget_owner_); |
| + mouse_lock_plugin_owner_->HandleMouseLockedInputEvent(event); |
| + |
| + // mouse_lock_webwidget_owner_ handles mouse lock in handleInputEvent(). |
| + |
| + // If the mouse is locked, only the current owner of the mouse lock can |
| + // process mouse events. |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +bool MouseLockDispatcher::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(MouseLockDispatcher, message) |
| + IPC_MESSAGE_HANDLER(ViewMsg_LockMouse_ACK, OnLockMouseACK) |
| + IPC_MESSAGE_HANDLER(ViewMsg_MouseLockLost, OnMouseLockLost) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void MouseLockDispatcher::OnLockMouseACK(bool succeeded) { |
| + // Mouse Lock removes the system cursor and provides all mouse motion as |
| + // .movementX/Y values on events all sent to a fixed target. This requires |
| + // content to specifically request the mode to be entered. |
| + // Mouse Capture is implicitly given for the duration of a drag event, and |
| + // sends all mouse events to the initial target of the drag. |
| + // If Lock is entered it supercedes any in progress Capture. |
| + if (succeeded && render_view_impl_->webwidget()) |
| + render_view_impl_->webwidget()->mouseCaptureLost(); |
|
piman
2012/01/18 19:00:59
could this callback re-enter and cause, say, the c
scheib
2012/01/24 01:51:42
Moved to after all state changes to prevent reentr
|
| + |
| + DCHECK(!mouse_locked_ && pending_lock_request_); |
| + |
| + mouse_locked_ = succeeded; |
| + pending_lock_request_ = false; |
| + if (pending_unlock_request_ && !succeeded) { |
| + // We have sent an unlock request after the lock request. However, since |
| + // the lock request has failed, the unlock request will be ignored by the |
| + // browser side and there won't be any response to it. |
| + pending_unlock_request_ = false; |
| + } |
| + |
| + if (mouse_lock_webwidget_owner_ || mouse_lock_plugin_owner_) { |
|
piman
2012/01/18 19:00:59
(similar question as above) can this be false?
scheib
2012/01/24 01:51:42
Now with UnlockAndClearPointer it is possible for
|
| + WebKit::WebWidget* last_webwidget_owner = mouse_lock_webwidget_owner_; |
| + webkit::ppapi::PluginInstance* last_plugin_owner = |
| + mouse_lock_plugin_owner_; |
| + |
| + if (!succeeded) { |
| + // Reset mouse lock owner to NULL before calling OnLockMouseACK(), so |
| + // that if OnLockMouseACK() results in calls to any mouse lock method |
| + // (e.g., LockMouse()), the method will see consistent internal state. |
| + mouse_lock_webwidget_owner_ = NULL; |
| + mouse_lock_plugin_owner_ = NULL; |
| + } |
| + |
| + DCHECK(!mouse_lock_webwidget_owner_ || !mouse_lock_plugin_owner_); |
| + if (last_webwidget_owner) { |
| + // TODO(scheib): Enable after WebKit pointer lock API has landed. |
| + // |
| + // if (succeeded) |
| + // last_webwidget_owner->didCompletePointerLock(); |
| + // else |
| + // last_webwidget_owner->didNotCompletePointerLock(); |
| + } |
| + if (last_plugin_owner) |
| + last_plugin_owner->OnLockMouseACK(succeeded); |
| + } |
| +} |
| + |
| +void MouseLockDispatcher::OnMouseLockLost() { |
| + DCHECK(mouse_locked_ && !pending_lock_request_); |
| + |
| + mouse_locked_ = false; |
| + pending_unlock_request_ = false; |
| + if (mouse_lock_webwidget_owner_ || mouse_lock_plugin_owner_) { |
| + // TODO(scheib): Enable after WebKit pointer lock API has landed. |
| + // |
| + // WebKit::WebWidget* last_webwidget_owner = mouse_lock_webwidget_owner_; |
| + webkit::ppapi::PluginInstance* last_plugin_owner = |
| + mouse_lock_plugin_owner_; |
| + |
| + // Reset mouse lock owner to NULL before calling |
| + // didLosePointerLock/OnMouseLockLost(), so |
| + // that a reentrant call (e.g. to, LockMouse()) will see consistent |
| + // internal state. |
| + mouse_lock_webwidget_owner_ = NULL; |
| + mouse_lock_plugin_owner_ = NULL; |
| + |
| + DCHECK(!mouse_lock_webwidget_owner_ || !mouse_lock_plugin_owner_); |
| + // TODO(scheib): Enable after WebKit pointer lock API has landed. |
| + // |
| + // if (last_webwidget_owner) |
| + // last_webwidget_owner->didLosePointerLock(); |
| + if (last_plugin_owner) |
| + last_plugin_owner->OnMouseLockLost(); |
| + } |
| +} |
| + |