Chromium Code Reviews| Index: services/ui/ws/event_targeter.cc |
| diff --git a/services/ui/ws/event_targeter.cc b/services/ui/ws/event_targeter.cc |
| index 1fb3f79e02741cedd283a9456fdb7f32558141e0..1f5f5e8a25f826ab100ebd86f47ad94f9e50e24b 100644 |
| --- a/services/ui/ws/event_targeter.cc |
| +++ b/services/ui/ws/event_targeter.cc |
| @@ -4,18 +4,72 @@ |
| #include "services/ui/ws/event_targeter.h" |
| +#include "base/command_line.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/task_scheduler/post_task.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| #include "services/ui/ws/event_targeter_delegate.h" |
| namespace ui { |
| namespace ws { |
| +EventTargeter::HitTestRequest::HitTestRequest(const gfx::Point& location, |
| + int64_t display_id, |
| + HitTestCallback callback) |
| + : location(location), |
| + display_id(display_id), |
| + callback(std::move(callback)) {} |
| + |
| +EventTargeter::HitTestRequest::~HitTestRequest() {} |
| + |
| EventTargeter::EventTargeter(EventTargeterDelegate* event_targeter_delegate) |
| - : event_targeter_delegate_(event_targeter_delegate) {} |
| + : event_targeter_delegate_(event_targeter_delegate), |
| + hit_test_in_flight_(false), |
| + weak_ptr_factory_(this) {} |
| EventTargeter::~EventTargeter() {} |
| -LocationTarget EventTargeter::FindTargetForLocation(const gfx::Point& location, |
| - int64_t display_id) { |
| +void EventTargeter::FindTargetForLocation(const gfx::Point& location, |
| + int64_t display_id, |
| + HitTestCallback callback) { |
| + if (IsHitTestInFlight()) { |
| + std::unique_ptr<HitTestRequest> hittest_request = |
| + base::MakeUnique<HitTestRequest>(location, display_id, |
| + std::move(callback)); |
| + hit_test_request_queue_.push(std::move(hittest_request)); |
| + return; |
| + } |
| + |
| + FindTargetForLocationImpl(location, display_id, std::move(callback)); |
| +} |
| + |
| +bool EventTargeter::IsHitTestInFlight() const { |
| + return hit_test_in_flight_ || !hit_test_request_queue_.empty(); |
| +} |
| + |
| +void EventTargeter::FindTargetForLocationImpl(const gfx::Point& location, |
|
sky
2017/06/07 17:05:07
These names are rather confusing. I'm hoping that
riajiang
2017/06/07 18:15:04
Sure sg, done.
|
| + int64_t display_id, |
| + HitTestCallback callback) { |
| + // TODO(riajiang): After HitTestComponent is implemented, do synchronous |
| + // hit-test for most cases using shared memory and only ask Blink |
| + // asynchronously for hard cases. For now, assume all synchronous hit-tests |
| + // failed if the "enable-async-event-targeting" flag is turned on. |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + "enable-async-event-targeting")) { |
| + DCHECK(!hit_test_in_flight_); |
| + hit_test_in_flight_ = true; |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::BindOnce(&EventTargeter::FindTargetForLocationAsync, |
| + weak_ptr_factory_.GetWeakPtr(), location, |
| + display_id, base::Passed(&callback))); |
| + } else { |
| + FindTargetForLocationAsync(location, display_id, std::move(callback)); |
| + } |
| +} |
| + |
| +void EventTargeter::FindTargetForLocationAsync(const gfx::Point& location, |
| + int64_t display_id, |
| + HitTestCallback callback) { |
| LocationTarget location_target; |
| location_target.location_in_root = location; |
| location_target.display_id = display_id; |
| @@ -26,7 +80,23 @@ LocationTarget EventTargeter::FindTargetForLocation(const gfx::Point& location, |
| ui::ws::FindDeepestVisibleWindowForLocation( |
| root, location_target.location_in_root); |
| } |
| - return location_target; |
| + base::ResetAndReturn(&callback).Run(location_target); |
|
sky
2017/06/07 17:05:07
std::move(callback).Run(...);
Generally you only n
riajiang
2017/06/07 18:15:04
I see, done.
|
| + ProcessNextHittesetRequestFromQueue(); |
| +} |
| + |
| +void EventTargeter::ProcessNextHittesetRequestFromQueue() { |
| + hit_test_in_flight_ = false; |
| + if (hit_test_request_queue_.empty()) { |
| + event_targeter_delegate_->ProcessNextEventFromQueue(); |
| + return; |
| + } |
| + |
| + std::unique_ptr<HitTestRequest> hittest_request = |
| + std::move(hit_test_request_queue_.front()); |
| + hit_test_request_queue_.pop(); |
| + FindTargetForLocationImpl(hittest_request->location, |
| + hittest_request->display_id, |
| + std::move(hittest_request->callback)); |
| } |
| } // namespace ws |