OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/ui/ws/event_targeter.h" | 5 #include "services/ui/ws/event_targeter.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/task_scheduler/post_task.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
7 #include "services/ui/ws/event_targeter_delegate.h" | 11 #include "services/ui/ws/event_targeter_delegate.h" |
8 | 12 |
9 namespace ui { | 13 namespace ui { |
10 namespace ws { | 14 namespace ws { |
11 | 15 |
| 16 EventTargeter::HitTestRequest::HitTestRequest(const gfx::Point& location, |
| 17 int64_t display_id, |
| 18 HitTestCallback callback) |
| 19 : location(location), |
| 20 display_id(display_id), |
| 21 callback(std::move(callback)) {} |
| 22 |
| 23 EventTargeter::HitTestRequest::~HitTestRequest() {} |
| 24 |
12 EventTargeter::EventTargeter(EventTargeterDelegate* event_targeter_delegate) | 25 EventTargeter::EventTargeter(EventTargeterDelegate* event_targeter_delegate) |
13 : event_targeter_delegate_(event_targeter_delegate) {} | 26 : event_targeter_delegate_(event_targeter_delegate), |
| 27 hit_test_in_flight_(false), |
| 28 weak_ptr_factory_(this) {} |
14 | 29 |
15 EventTargeter::~EventTargeter() {} | 30 EventTargeter::~EventTargeter() {} |
16 | 31 |
17 LocationTarget EventTargeter::FindTargetForLocation(const gfx::Point& location, | 32 void EventTargeter::FindTargetForLocation(const gfx::Point& location, |
18 int64_t display_id) { | 33 int64_t display_id, |
| 34 HitTestCallback callback) { |
| 35 if (IsHitTestInFlight()) { |
| 36 std::unique_ptr<HitTestRequest> hittest_request = |
| 37 base::MakeUnique<HitTestRequest>(location, display_id, |
| 38 std::move(callback)); |
| 39 hit_test_request_queue_.push(std::move(hittest_request)); |
| 40 return; |
| 41 } |
| 42 |
| 43 ProcessFindTarget(location, display_id, std::move(callback)); |
| 44 } |
| 45 |
| 46 bool EventTargeter::IsHitTestInFlight() const { |
| 47 return hit_test_in_flight_ || !hit_test_request_queue_.empty(); |
| 48 } |
| 49 |
| 50 void EventTargeter::ProcessFindTarget(const gfx::Point& location, |
| 51 int64_t display_id, |
| 52 HitTestCallback callback) { |
| 53 // TODO(riajiang): After HitTestComponent is implemented, do synchronous |
| 54 // hit-test for most cases using shared memory and only ask Blink |
| 55 // asynchronously for hard cases. For now, assume all synchronous hit-tests |
| 56 // failed if the "enable-async-event-targeting" flag is turned on. |
| 57 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 58 "enable-async-event-targeting")) { |
| 59 DCHECK(!hit_test_in_flight_); |
| 60 hit_test_in_flight_ = true; |
| 61 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 62 FROM_HERE, base::BindOnce(&EventTargeter::FindTargetForLocationNow, |
| 63 weak_ptr_factory_.GetWeakPtr(), location, |
| 64 display_id, base::Passed(&callback))); |
| 65 } else { |
| 66 FindTargetForLocationNow(location, display_id, std::move(callback)); |
| 67 } |
| 68 } |
| 69 |
| 70 void EventTargeter::FindTargetForLocationNow(const gfx::Point& location, |
| 71 int64_t display_id, |
| 72 HitTestCallback callback) { |
19 LocationTarget location_target; | 73 LocationTarget location_target; |
20 location_target.location_in_root = location; | 74 location_target.location_in_root = location; |
21 location_target.display_id = display_id; | 75 location_target.display_id = display_id; |
22 ServerWindow* root = event_targeter_delegate_->GetRootWindowContaining( | 76 ServerWindow* root = event_targeter_delegate_->GetRootWindowContaining( |
23 &location_target.location_in_root, &location_target.display_id); | 77 &location_target.location_in_root, &location_target.display_id); |
24 if (root) { | 78 if (root) { |
25 location_target.deepest_window = | 79 location_target.deepest_window = |
26 ui::ws::FindDeepestVisibleWindowForLocation( | 80 ui::ws::FindDeepestVisibleWindowForLocation( |
27 root, location_target.location_in_root); | 81 root, location_target.location_in_root); |
28 } | 82 } |
29 return location_target; | 83 std::move(callback).Run(location_target); |
| 84 ProcessNextHitTestRequestFromQueue(); |
| 85 } |
| 86 |
| 87 void EventTargeter::ProcessNextHitTestRequestFromQueue() { |
| 88 hit_test_in_flight_ = false; |
| 89 if (hit_test_request_queue_.empty()) { |
| 90 event_targeter_delegate_->ProcessNextAvailableEvent(); |
| 91 return; |
| 92 } |
| 93 |
| 94 std::unique_ptr<HitTestRequest> hittest_request = |
| 95 std::move(hit_test_request_queue_.front()); |
| 96 hit_test_request_queue_.pop(); |
| 97 ProcessFindTarget(hittest_request->location, hittest_request->display_id, |
| 98 std::move(hittest_request->callback)); |
30 } | 99 } |
31 | 100 |
32 } // namespace ws | 101 } // namespace ws |
33 } // namespace ui | 102 } // namespace ui |
OLD | NEW |