chromeos: dbus: add Bluetooth properties support
[chromium-blink-merge.git] / content / renderer / mouse_lock_dispatcher.cc
blob41ed654c42329bf7ada5f8775348105c28aff7e7
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.
5 #include "content/renderer/mouse_lock_dispatcher.h"
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"
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) {
21 MouseLockDispatcher::~MouseLockDispatcher() {
24 bool MouseLockDispatcher::LockMouse(LockTarget* target) {
25 if (MouseLockedOrPendingAction())
26 return false;
28 pending_lock_request_ = true;
29 target_ = target;
31 Send(new ViewHostMsg_LockMouse(routing_id()));
32 return true;
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()));
42 void MouseLockDispatcher::OnLockTargetDestroyed(LockTarget* target) {
43 if (target == target_) {
44 UnlockMouse(target);
45 target_ = NULL;
49 bool MouseLockDispatcher::IsMouseLockedTo(LockTarget* target) {
50 return mouse_locked_ && target_ == target;
53 bool MouseLockDispatcher::WillHandleMouseEvent(
54 const WebKit::WebMouseEvent& event) {
55 if (mouse_locked_ && target_)
56 return target_->HandleMouseLockedInputEvent(event);
57 return false;
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;
70 void MouseLockDispatcher::OnLockMouseACK(bool succeeded) {
71 DCHECK(!mouse_locked_ && pending_lock_request_);
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;
82 LockTarget* last_target = target_;
83 if (!succeeded)
84 target_ = NULL;
86 // Callbacks made after all state modification to prevent reentrant errors
87 // such as OnLockMouseACK() synchronously calling LockMouse().
89 if (last_target)
90 last_target->OnLockMouseACK(succeeded);
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();
102 void MouseLockDispatcher::OnMouseLockLost() {
103 DCHECK(mouse_locked_ && !pending_lock_request_);
105 mouse_locked_ = false;
106 pending_unlock_request_ = false;
108 LockTarget* last_target = target_;
109 target_ = NULL;
111 // Callbacks made after all state modification to prevent reentrant errors
112 // such as OnMouseLockLost() synchronously calling LockMouse().
114 if (last_target)
115 last_target->OnMouseLockLost();