OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "blimp/client/input/blimp_input_manager.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/location.h" |
| 10 #include "ui/events/blink/blink_event_util.h" |
| 11 #include "ui/events/gesture_detection/gesture_provider_config_helper.h" |
| 12 |
| 13 namespace blimp { |
| 14 |
| 15 scoped_ptr<BlimpInputManager> BlimpInputManager::Create( |
| 16 BlimpInputManagerClient* client, |
| 17 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 18 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, |
| 19 const base::WeakPtr<cc::InputHandler>& input_handler) { |
| 20 return make_scoped_ptr(new BlimpInputManager(client, |
| 21 main_task_runner, |
| 22 compositor_task_runner, |
| 23 input_handler)); |
| 24 } |
| 25 |
| 26 BlimpInputManager::BlimpInputManager( |
| 27 BlimpInputManagerClient* client, |
| 28 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 29 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, |
| 30 const base::WeakPtr<cc::InputHandler>& input_handler) |
| 31 : client_(client), |
| 32 gesture_provider_(ui::GetGestureProviderConfig( |
| 33 ui::GestureProviderConfigType::CURRENT_PLATFORM), this), |
| 34 main_task_runner_(main_task_runner), |
| 35 compositor_task_runner_(compositor_task_runner), |
| 36 main_thread_blocked_(false), |
| 37 weak_factory_(this) { |
| 38 DCHECK(IsMainThread()); |
| 39 compositor_task_runner_->PostTask( |
| 40 FROM_HERE, |
| 41 base::Bind( |
| 42 &BlimpInputManager::CreateInputHandlerWrapperOnCompositorThread, |
| 43 base::Unretained(this), weak_factory_.GetWeakPtr(), |
| 44 input_handler)); |
| 45 } |
| 46 |
| 47 BlimpInputManager::~BlimpInputManager() { |
| 48 DCHECK(IsMainThread()); |
| 49 |
| 50 base::WaitableEvent shutdown_event(false /* manual_reset */, |
| 51 false /* initially_signaled */); |
| 52 { |
| 53 base::AutoReset<bool> auto_reset_main_thread_blocked( |
| 54 &main_thread_blocked_, true); |
| 55 compositor_task_runner_->PostTask( |
| 56 FROM_HERE, |
| 57 base::Bind( |
| 58 &BlimpInputManager::ShutdownOnCompositorThread, |
| 59 base::Unretained(this), &shutdown_event)); |
| 60 shutdown_event.Wait(); |
| 61 } |
| 62 } |
| 63 |
| 64 bool BlimpInputManager::OnTouchEvent(const ui::MotionEvent& motion_event) { |
| 65 DCHECK(IsMainThread()); |
| 66 |
| 67 ui::FilteredGestureProvider::TouchHandlingResult result = |
| 68 gesture_provider_.OnTouchEvent(motion_event); |
| 69 if (!result.succeeded) |
| 70 return false; |
| 71 |
| 72 blink::WebTouchEvent touch = |
| 73 ui::CreateWebTouchEventFromMotionEvent(motion_event, |
| 74 result.did_generate_scroll); |
| 75 |
| 76 // Touch events are queued in the Gesture Provider until acknowledged to |
| 77 // allow them to be consumed by the touch event handlers in blink which can |
| 78 // prevent-default on the event. Since we currently do not support touch |
| 79 // handlers the event is always acknowledged as not consumed. |
| 80 gesture_provider_.OnTouchEventAck(touch.uniqueTouchEventId, false); |
| 81 |
| 82 return true; |
| 83 } |
| 84 |
| 85 void BlimpInputManager::OnGestureEvent(const ui::GestureEventData& gesture) { |
| 86 DCHECK(IsMainThread()); |
| 87 |
| 88 blink::WebGestureEvent web_gesture = |
| 89 ui::CreateWebGestureEventFromGestureEventData(gesture); |
| 90 // TODO(khushalsagar): Remove this workaround after Android fixes UiAutomator |
| 91 // to stop providing shift meta values to synthetic MotionEvents. This |
| 92 // prevents unintended shift+click interpretation of all accessibility clicks. |
| 93 // See crbug.com/443247. |
| 94 if (web_gesture.type == blink::WebInputEvent::GestureTap && |
| 95 web_gesture.modifiers == blink::WebInputEvent::ShiftKey) { |
| 96 web_gesture.modifiers = 0; |
| 97 } |
| 98 |
| 99 compositor_task_runner_->PostTask( |
| 100 FROM_HERE, |
| 101 base::Bind( |
| 102 &BlimpInputManager::HandleWebInputEventOnCompositorThread, |
| 103 base::Unretained(this), web_gesture)); |
| 104 } |
| 105 |
| 106 void BlimpInputManager::CreateInputHandlerWrapperOnCompositorThread( |
| 107 base::WeakPtr<BlimpInputManager> input_manager_weak_ptr, |
| 108 const base::WeakPtr<cc::InputHandler>& input_handler) { |
| 109 DCHECK(IsCompositorThread()); |
| 110 |
| 111 // The input_handler might have been destroyed at this point. |
| 112 if (!input_handler) |
| 113 return; |
| 114 |
| 115 DCHECK(!input_handler_wrapper_); |
| 116 input_handler_wrapper_ = make_scoped_ptr( |
| 117 new BlimpInputHandlerWrapper(main_task_runner_, |
| 118 input_manager_weak_ptr, |
| 119 input_handler.get())); |
| 120 } |
| 121 |
| 122 void BlimpInputManager::HandleWebInputEventOnCompositorThread( |
| 123 const blink::WebInputEvent& input_event) { |
| 124 DCHECK(IsCompositorThread()); |
| 125 |
| 126 if (input_handler_wrapper_) |
| 127 input_handler_wrapper_->HandleWebInputEvent(input_event); |
| 128 } |
| 129 |
| 130 void BlimpInputManager::ShutdownOnCompositorThread( |
| 131 base::WaitableEvent* shutdown_event) { |
| 132 DCHECK(IsCompositorThread()); |
| 133 DCHECK(main_thread_blocked_); |
| 134 |
| 135 input_handler_wrapper_.reset(); |
| 136 shutdown_event->Signal(); |
| 137 } |
| 138 |
| 139 void BlimpInputManager::DidHandleWebInputEvent( |
| 140 const blink::WebInputEvent& input_event, bool consumed) { |
| 141 DCHECK(IsMainThread()); |
| 142 |
| 143 if (!consumed) |
| 144 client_->SendWebInputEvent(input_event); |
| 145 } |
| 146 |
| 147 bool BlimpInputManager::IsMainThread() const { |
| 148 return main_task_runner_->BelongsToCurrentThread(); |
| 149 } |
| 150 |
| 151 bool BlimpInputManager::IsCompositorThread() const { |
| 152 return compositor_task_runner_->BelongsToCurrentThread(); |
| 153 } |
| 154 |
| 155 } // namespace blimp |
OLD | NEW |