| 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/feature/compositor/blimp_input_manager.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "ui/events/blink/blink_event_util.h" | |
| 12 #include "ui/events/gesture_detection/gesture_provider_config_helper.h" | |
| 13 | |
| 14 namespace blimp { | |
| 15 namespace client { | |
| 16 | |
| 17 std::unique_ptr<BlimpInputManager> BlimpInputManager::Create( | |
| 18 BlimpInputManagerClient* client, | |
| 19 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
| 20 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, | |
| 21 const base::WeakPtr<cc::InputHandler>& input_handler) { | |
| 22 return base::WrapUnique(new BlimpInputManager( | |
| 23 client, main_task_runner, compositor_task_runner, 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( | |
| 51 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 52 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 53 { | |
| 54 base::AutoReset<bool> auto_reset_main_thread_blocked( | |
| 55 &main_thread_blocked_, true); | |
| 56 compositor_task_runner_->PostTask( | |
| 57 FROM_HERE, | |
| 58 base::Bind( | |
| 59 &BlimpInputManager::ShutdownOnCompositorThread, | |
| 60 base::Unretained(this), &shutdown_event)); | |
| 61 shutdown_event.Wait(); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 bool BlimpInputManager::OnTouchEvent(const ui::MotionEvent& motion_event) { | |
| 66 DCHECK(IsMainThread()); | |
| 67 | |
| 68 ui::FilteredGestureProvider::TouchHandlingResult result = | |
| 69 gesture_provider_.OnTouchEvent(motion_event); | |
| 70 if (!result.succeeded) | |
| 71 return false; | |
| 72 | |
| 73 blink::WebTouchEvent touch = | |
| 74 ui::CreateWebTouchEventFromMotionEvent(motion_event, | |
| 75 result.moved_beyond_slop_region); | |
| 76 | |
| 77 // Touch events are queued in the Gesture Provider until acknowledged to | |
| 78 // allow them to be consumed by the touch event handlers in blink which can | |
| 79 // prevent-default on the event. Since we currently do not support touch | |
| 80 // handlers the event is always acknowledged as not consumed. | |
| 81 gesture_provider_.OnTouchEventAck(touch.uniqueTouchEventId, false); | |
| 82 | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 void BlimpInputManager::OnGestureEvent(const ui::GestureEventData& gesture) { | |
| 87 DCHECK(IsMainThread()); | |
| 88 | |
| 89 blink::WebGestureEvent web_gesture = | |
| 90 ui::CreateWebGestureEventFromGestureEventData(gesture); | |
| 91 // TODO(khushalsagar): Remove this workaround after Android fixes UiAutomator | |
| 92 // to stop providing shift meta values to synthetic MotionEvents. This | |
| 93 // prevents unintended shift+click interpretation of all accessibility clicks. | |
| 94 // See crbug.com/443247. | |
| 95 if (web_gesture.type == blink::WebInputEvent::GestureTap && | |
| 96 web_gesture.modifiers == blink::WebInputEvent::ShiftKey) { | |
| 97 web_gesture.modifiers = 0; | |
| 98 } | |
| 99 | |
| 100 compositor_task_runner_->PostTask( | |
| 101 FROM_HERE, | |
| 102 base::Bind(&BlimpInputManager::HandleWebGestureEventOnCompositorThread, | |
| 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_ = base::MakeUnique<BlimpInputHandlerWrapper>( | |
| 117 main_task_runner_, input_manager_weak_ptr, input_handler.get()); | |
| 118 } | |
| 119 | |
| 120 void BlimpInputManager::HandleWebGestureEventOnCompositorThread( | |
| 121 const blink::WebGestureEvent& gesture_event) { | |
| 122 DCHECK(IsCompositorThread()); | |
| 123 | |
| 124 if (input_handler_wrapper_) | |
| 125 input_handler_wrapper_->HandleWebGestureEvent(gesture_event); | |
| 126 } | |
| 127 | |
| 128 void BlimpInputManager::ShutdownOnCompositorThread( | |
| 129 base::WaitableEvent* shutdown_event) { | |
| 130 DCHECK(IsCompositorThread()); | |
| 131 DCHECK(main_thread_blocked_); | |
| 132 | |
| 133 input_handler_wrapper_.reset(); | |
| 134 shutdown_event->Signal(); | |
| 135 } | |
| 136 | |
| 137 void BlimpInputManager::DidHandleWebGestureEvent( | |
| 138 const blink::WebGestureEvent& gesture_event, | |
| 139 bool consumed) { | |
| 140 DCHECK(IsMainThread()); | |
| 141 | |
| 142 if (!consumed) | |
| 143 client_->SendWebGestureEvent(gesture_event); | |
| 144 } | |
| 145 | |
| 146 bool BlimpInputManager::IsMainThread() const { | |
| 147 return main_task_runner_->BelongsToCurrentThread(); | |
| 148 } | |
| 149 | |
| 150 bool BlimpInputManager::IsCompositorThread() const { | |
| 151 return compositor_task_runner_->BelongsToCurrentThread(); | |
| 152 } | |
| 153 | |
| 154 } // namespace client | |
| 155 } // namespace blimp | |
| OLD | NEW |