| 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 namespace client { | |
| 15 | |
| 16 scoped_ptr<BlimpInputManager> BlimpInputManager::Create( | |
| 17 BlimpInputManagerClient* client, | |
| 18 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
| 19 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, | |
| 20 const base::WeakPtr<cc::InputHandler>& input_handler) { | |
| 21 return make_scoped_ptr(new BlimpInputManager(client, | |
| 22 main_task_runner, | |
| 23 compositor_task_runner, | |
| 24 input_handler)); | |
| 25 } | |
| 26 | |
| 27 BlimpInputManager::BlimpInputManager( | |
| 28 BlimpInputManagerClient* client, | |
| 29 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
| 30 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner, | |
| 31 const base::WeakPtr<cc::InputHandler>& input_handler) | |
| 32 : client_(client), | |
| 33 gesture_provider_(ui::GetGestureProviderConfig( | |
| 34 ui::GestureProviderConfigType::CURRENT_PLATFORM), this), | |
| 35 main_task_runner_(main_task_runner), | |
| 36 compositor_task_runner_(compositor_task_runner), | |
| 37 main_thread_blocked_(false), | |
| 38 weak_factory_(this) { | |
| 39 DCHECK(IsMainThread()); | |
| 40 compositor_task_runner_->PostTask( | |
| 41 FROM_HERE, | |
| 42 base::Bind( | |
| 43 &BlimpInputManager::CreateInputHandlerWrapperOnCompositorThread, | |
| 44 base::Unretained(this), weak_factory_.GetWeakPtr(), | |
| 45 input_handler)); | |
| 46 } | |
| 47 | |
| 48 BlimpInputManager::~BlimpInputManager() { | |
| 49 DCHECK(IsMainThread()); | |
| 50 | |
| 51 base::WaitableEvent shutdown_event(false /* manual_reset */, | |
| 52 false /* initially_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.did_generate_scroll); | |
| 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 scoped_ptr<blink::WebGestureEvent> gesture_event; | |
| 101 gesture_event.reset(new blink::WebGestureEvent); | |
| 102 memcpy(gesture_event.get(), &web_gesture, sizeof(blink::WebGestureEvent)); | |
| 103 | |
| 104 compositor_task_runner_->PostTask( | |
| 105 FROM_HERE, | |
| 106 base::Bind( | |
| 107 &BlimpInputManager::HandleWebInputEventOnCompositorThread, | |
| 108 base::Unretained(this), base::Passed(&gesture_event))); | |
| 109 } | |
| 110 | |
| 111 void BlimpInputManager::CreateInputHandlerWrapperOnCompositorThread( | |
| 112 base::WeakPtr<BlimpInputManager> input_manager_weak_ptr, | |
| 113 const base::WeakPtr<cc::InputHandler>& input_handler) { | |
| 114 DCHECK(IsCompositorThread()); | |
| 115 | |
| 116 // The input_handler might have been destroyed at this point. | |
| 117 if (!input_handler) | |
| 118 return; | |
| 119 | |
| 120 DCHECK(!input_handler_wrapper_); | |
| 121 input_handler_wrapper_ = make_scoped_ptr( | |
| 122 new BlimpInputHandlerWrapper(main_task_runner_, | |
| 123 input_manager_weak_ptr, | |
| 124 input_handler.get())); | |
| 125 } | |
| 126 | |
| 127 void BlimpInputManager::HandleWebInputEventOnCompositorThread( | |
| 128 scoped_ptr<blink::WebInputEvent> input_event) { | |
| 129 DCHECK(IsCompositorThread()); | |
| 130 | |
| 131 if (input_handler_wrapper_) | |
| 132 input_handler_wrapper_->HandleWebInputEvent(std::move(input_event)); | |
| 133 } | |
| 134 | |
| 135 void BlimpInputManager::ShutdownOnCompositorThread( | |
| 136 base::WaitableEvent* shutdown_event) { | |
| 137 DCHECK(IsCompositorThread()); | |
| 138 DCHECK(main_thread_blocked_); | |
| 139 | |
| 140 input_handler_wrapper_.reset(); | |
| 141 shutdown_event->Signal(); | |
| 142 } | |
| 143 | |
| 144 void BlimpInputManager::DidHandleWebInputEvent( | |
| 145 scoped_ptr<blink::WebInputEvent> input_event, bool consumed) { | |
| 146 DCHECK(IsMainThread()); | |
| 147 | |
| 148 if (!consumed) | |
| 149 client_->SendWebInputEvent(*input_event); | |
| 150 } | |
| 151 | |
| 152 bool BlimpInputManager::IsMainThread() const { | |
| 153 return main_task_runner_->BelongsToCurrentThread(); | |
| 154 } | |
| 155 | |
| 156 bool BlimpInputManager::IsCompositorThread() const { | |
| 157 return compositor_task_runner_->BelongsToCurrentThread(); | |
| 158 } | |
| 159 | |
| 160 } // namespace client | |
| 161 } // namespace blimp | |
| OLD | NEW |