Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1270)

Unified Diff: content/renderer/mouse_lock_dispatcher.cc

Issue 8970016: refactoring mouse lock to support pepper and WebKit (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated webkit API names. Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..885150fd83234a4074d58c7dbc592b59fb5404e4
--- /dev/null
+++ b/content/renderer/mouse_lock_dispatcher.cc
@@ -0,0 +1,157 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
yzshen1 2012/01/16 07:50:35 2012. :)
scheib 2012/01/17 22:05:36 Done.
+// 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/WebWidget.h"
+#include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
yzshen1 2012/01/16 07:50:35 Alphabetically, please.
scheib 2012/01/17 22:05:36 Done.
+
+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_pinstance_owner_(NULL) {
+}
+
+MouseLockDispatcher::~MouseLockDispatcher() {
+}
+
+bool MouseLockDispatcher::LockMouse(WebKit::WebWidget* webwidget,
+ webkit::ppapi::PluginInstance* pinstance) {
yzshen1 2012/01/16 07:50:35 Wrong indent.
scheib 2012/01/17 22:05:36 Done.
+ DCHECK(!(webwidget && pinstance)); // Only one mouse lock target at a time.
+
+ if (MouseLockedOrPendingAction())
+ return false;
+
+ pending_lock_request_ = true;
+ DCHECK(!mouse_lock_webwidget_owner_ && !mouse_lock_pinstance_owner_);
+ mouse_lock_webwidget_owner_ = webwidget;
+ mouse_lock_pinstance_owner_ = pinstance;
+
+ Send(new ViewHostMsg_LockMouse(routing_id()));
+ return true;
+}
+
+void MouseLockDispatcher::UnlockMouse(WebKit::WebWidget* webwidget,
+ webkit::ppapi::PluginInstance* pinstance) {
yzshen1 2012/01/16 07:50:35 Wrong indent.
scheib 2012/01/17 22:05:36 Done.
+ if (!MouseLockedOrPendingAction() || pending_unlock_request_)
+ return;
+
+ // Ignore unless the unlock request is for the current lock holder.
+ if (webwidget == mouse_lock_webwidget_owner_ ||
+ pinstance == mouse_lock_pinstance_owner_) {
+ 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* pinstance) {
+ return mouse_locked_ && mouse_lock_pinstance_owner_ == pinstance;
+}
+
+bool MouseLockDispatcher::WillHandleMouseEvent(
+ const WebKit::WebMouseEvent& event) {
+ if (mouse_locked_ && mouse_lock_pinstance_owner_) {
+ DCHECK(!mouse_lock_webwidget_owner_);
+ if (mouse_lock_pinstance_owner_)
yzshen1 2012/01/16 07:50:35 you don't need this check, right?
scheib 2012/01/17 22:05:36 Done.
+ mouse_lock_pinstance_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;
+}
yzshen1 2012/01/16 07:50:35 Empty line after it, please.
scheib 2012/01/17 22:05:36 Done.
+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();
+
+ 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_pinstance_owner_) {
+ WebKit::WebWidget* last_webwidget_owner = mouse_lock_webwidget_owner_;
yzshen1 2012/01/16 07:50:35 [Just want to double check.] Is it possible that m
scheib 2012/01/17 22:05:36 It is not possible, the render view impl will call
+ webkit::ppapi::PluginInstance* last_pinstance_owner =
+ mouse_lock_pinstance_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_pinstance_owner_ = NULL;
+ }
+
+ DCHECK(!mouse_lock_webwidget_owner_ || !mouse_lock_pinstance_owner_);
+ if (last_webwidget_owner) {
+ if (succeeded)
+ last_webwidget_owner->didCompletePointerLock();
+ else
+ last_webwidget_owner->didNotCompletePointerLock();
+ }
+ if (last_pinstance_owner)
+ last_pinstance_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_pinstance_owner_) {
+ WebKit::WebWidget* last_webwidget_owner = mouse_lock_webwidget_owner_;
+ webkit::ppapi::PluginInstance* last_pinstance_owner =
+ mouse_lock_pinstance_owner_;
+
+ // Reset mouse lock owner to NULL before calling OnLockMouseACK(), so
yzshen1 2012/01/16 07:50:35 OnLockMouseACK -> OnMouseLockLost. (And the same f
scheib 2012/01/17 22:05:36 Done.
+ // 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_pinstance_owner_ = NULL;
+
+ DCHECK(!mouse_lock_webwidget_owner_ || !mouse_lock_pinstance_owner_);
+ if (last_webwidget_owner)
+ last_webwidget_owner->didLosePointerLock();
+ if (last_pinstance_owner)
+ last_pinstance_owner->OnMouseLockLost();
+ }
+}
+

Powered by Google App Engine
This is Rietveld 408576698