OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/renderer/gpu/renderer_event_packet.h" |
| 6 |
| 7 #include "content/common/input/event_packet.h" |
| 8 #include "content/common/input/input_event.h" |
| 9 #include "content/common/input/ipc_input_event_payload.h" |
| 10 #include "content/common/input/web_input_event_payload.h" |
| 11 #include "content/common/input_messages.h" |
| 12 |
| 13 namespace content { |
| 14 namespace { |
| 15 |
| 16 class RendererInputEvent { |
| 17 public: |
| 18 // |event| should outlive the created RendererInputEvent. |
| 19 explicit RendererInputEvent(const InputEvent* event) : event_(event) { |
| 20 DCHECK(event_); |
| 21 } |
| 22 |
| 23 InputEventDisposition Dispatch(RendererEventPacket::Dispatcher* dispatcher, |
| 24 int routing_id) { |
| 25 DCHECK(dispatcher); |
| 26 const InputEvent::Payload* payload = event_->payload(); |
| 27 |
| 28 if (payload->GetType() == InputEvent::Payload::IPC_MESSAGE) { |
| 29 return dispatcher->Dispatch( |
| 30 *IPCInputEventPayload::Cast(payload)->message); |
| 31 } |
| 32 |
| 33 if (payload->GetType() == InputEvent::Payload::WEB_INPUT_EVENT) { |
| 34 const WebInputEventPayload* web_payload = |
| 35 WebInputEventPayload::Cast(payload); |
| 36 return dispatcher->Dispatch(routing_id, |
| 37 web_payload->web_event(), |
| 38 web_payload->latency_info(), |
| 39 web_payload->is_keyboard_shortcut()); |
| 40 } |
| 41 |
| 42 NOTREACHED() << "Invalid InputEvent type."; |
| 43 return INPUT_EVENT_UNHANDLED; |
| 44 } |
| 45 |
| 46 bool CanCreateFollowupEvents() const { |
| 47 const InputEvent::Payload* payload = event_->payload(); |
| 48 return payload->GetType() == InputEvent::Payload::WEB_INPUT_EVENT && |
| 49 WebInputEventPayload::Cast(payload)->CanCreateFollowupEvents(); |
| 50 } |
| 51 |
| 52 bool CanDeliverOnThread(RendererEventPacket::DispatchThreadType thread_type) { |
| 53 switch (thread_type) { |
| 54 case RendererEventPacket::THREAD_MAIN: |
| 55 return true; |
| 56 case RendererEventPacket::THREAD_IMPL: |
| 57 return event_->payload()->GetType() == |
| 58 InputEvent::Payload::WEB_INPUT_EVENT; |
| 59 } |
| 60 NOTREACHED() << "Invalid dispatch thread type."; |
| 61 return false; |
| 62 } |
| 63 |
| 64 private: |
| 65 const InputEvent* event_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(RendererInputEvent); |
| 68 }; |
| 69 |
| 70 } // namespace |
| 71 |
| 72 RendererEventPacket::RendererEventPacket( |
| 73 const EventPacket* packet, |
| 74 const InputEventDispositions& dispositions) |
| 75 : packet_(packet), |
| 76 dispositions_(dispositions) { |
| 77 DCHECK(packet_); |
| 78 DCHECK_LE(dispositions_.size(), packet_->size()); |
| 79 dispositions_.resize(packet_->size(), INPUT_EVENT_UNHANDLED); |
| 80 } |
| 81 |
| 82 RendererEventPacket::~RendererEventPacket() {} |
| 83 |
| 84 scoped_ptr<IPC::Message> RendererEventPacket::DispatchWith( |
| 85 Dispatcher* dispatcher, |
| 86 DispatchThreadType thread_type, |
| 87 int routing_id) { |
| 88 DCHECK(dispatcher); |
| 89 DCHECK_EQ(dispositions_.size(), packet_->size()); |
| 90 const EventPacket::InputEvents& events = packet_->events(); |
| 91 for (size_t i = 0; i < events.size(); ++i) { |
| 92 RendererInputEvent event(events[i]); |
| 93 |
| 94 // Ignore previously handled events. |
| 95 InputEventDisposition old_disposition = dispositions_[i]; |
| 96 if (old_disposition != INPUT_EVENT_UNHANDLED && |
| 97 (thread_type == THREAD_IMPL || |
| 98 old_disposition != INPUT_EVENT_IMPL_THREAD_BOUNCE_TO_MAIN)) |
| 99 continue; |
| 100 |
| 101 InputEventDisposition disposition = event.Dispatch(dispatcher, routing_id); |
| 102 dispositions_[i] = disposition; |
| 103 |
| 104 if (disposition == INPUT_EVENT_IMPL_THREAD_BOUNCE_TO_MAIN) { |
| 105 DCHECK_EQ(THREAD_IMPL, thread_type); |
| 106 // Bounce subsequent events to main. Events that should be offered to the |
| 107 // impl thread will prevent further delivery. |
| 108 bool can_continue_delivery = true; |
| 109 while (++i < events.size()) { |
| 110 RendererInputEvent current_event(events[i]); |
| 111 if (current_event.CanDeliverOnThread(thread_type)) |
| 112 can_continue_delivery = false; |
| 113 dispositions_[i] = can_continue_delivery |
| 114 ? INPUT_EVENT_IMPL_THREAD_BOUNCE_TO_MAIN |
| 115 : INPUT_EVENT_COULD_NOT_DELIVER; |
| 116 } |
| 117 return scoped_ptr<IPC::Message>( |
| 118 new InputMsg_HandleEventPacket(routing_id, *packet_, dispositions_)); |
| 119 } |
| 120 |
| 121 // Unconsumed events that create followup will prevent further delivery. |
| 122 bool event_consumed = |
| 123 disposition == INPUT_EVENT_IMPL_THREAD_CONSUMED || |
| 124 disposition == INPUT_EVENT_MAIN_THREAD_CONSUMED || |
| 125 disposition == INPUT_EVENT_MAIN_THREAD_PREVENT_DEFAULTED; |
| 126 if (!event_consumed && event.CanCreateFollowupEvents()) { |
| 127 while (++i < events.size()) |
| 128 dispositions_[i] = INPUT_EVENT_COULD_NOT_DELIVER; |
| 129 } |
| 130 } |
| 131 |
| 132 return scoped_ptr<IPC::Message>(new InputHostMsg_HandleEventPacket_ACK( |
| 133 routing_id, packet_->id(), dispositions_)); |
| 134 } |
| 135 |
| 136 } // namespace content |
OLD | NEW |