1 // Copyright 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.
5 #include "content/common/input/input_param_traits.h"
7 #include "content/common/content_param_traits.h"
8 #include "content/common/input/input_event_disposition.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"
15 template <typename PayloadType
>
16 scoped_ptr
<content::InputEvent::Payload
> ReadPayload(const Message
* m
,
17 PickleIterator
* iter
) {
18 scoped_ptr
<PayloadType
> event
= PayloadType::Create();
19 if (!ReadParam(m
, iter
, event
.get()))
20 return scoped_ptr
<content::InputEvent::Payload
>();
21 return event
.template PassAs
<content::InputEvent::Payload
>();
25 void ParamTraits
<content::EventPacket
>::Write(Message
* m
, const param_type
& p
) {
26 WriteParam(m
, p
.id());
27 WriteParam(m
, p
.events());
30 bool ParamTraits
<content::EventPacket
>::Read(const Message
* m
,
34 content::EventPacket::InputEvents events
;
35 if (!ReadParam(m
, iter
, &id
) ||
36 !ReadParam(m
, iter
, &events
))
40 bool events_added_successfully
= true;
41 for (size_t i
= 0; i
< events
.size(); ++i
) {
42 // Take ownership of all events.
43 scoped_ptr
<content::InputEvent
> event(events
[i
]);
44 if (!events_added_successfully
)
46 if (!p
->Add(event
.Pass()))
47 events_added_successfully
= false;
50 return events_added_successfully
;
53 void ParamTraits
<content::EventPacket
>::Log(const param_type
& p
,
55 l
->append("EventPacket((");
57 l
->append("), Events(");
58 LogParam(p
.events(), l
);
62 void ParamTraits
<content::InputEvent
>::Write(Message
* m
, const param_type
& p
) {
63 WriteParam(m
, p
.id());
64 WriteParam(m
, !!p
.payload());
68 content::InputEvent::Payload::Type payload_type
= p
.payload()->GetType();
69 WriteParam(m
, payload_type
);
70 switch (payload_type
) {
71 case content::InputEvent::Payload::IPC_MESSAGE
:
72 WriteParam(m
, *content::IPCInputEventPayload::Cast(p
.payload()));
74 case content::InputEvent::Payload::WEB_INPUT_EVENT
:
75 WriteParam(m
, *content::WebInputEventPayload::Cast(p
.payload()));
82 bool ParamTraits
<content::InputEvent
>::Read(const Message
* m
,
86 bool has_payload
= false;
87 content::InputEvent::Payload::Type payload_type
;
88 if (!ReadParam(m
, iter
, &id
) ||
89 !ReadParam(m
, iter
, &has_payload
) ||
92 !ReadParam(m
, iter
, &payload_type
))
95 scoped_ptr
<content::InputEvent::Payload
> payload
;
96 switch (payload_type
) {
97 case content::InputEvent::Payload::IPC_MESSAGE
:
98 payload
= ReadPayload
<content::IPCInputEventPayload
>(m
, iter
);
100 case content::InputEvent::Payload::WEB_INPUT_EVENT
:
101 payload
= ReadPayload
<content::WebInputEventPayload
>(m
, iter
);
104 NOTREACHED() << "Invalid InputEvent::Payload type.";
107 return p
->Initialize(id
, payload
.Pass());
110 void ParamTraits
<content::InputEvent
>::Log(const param_type
& p
,
112 l
->append("InputEvent((");
114 l
->append("), Payload (");
115 const content::InputEvent::Payload
* payload
= p
.payload();
117 switch (payload
->GetType()) {
118 case content::InputEvent::Payload::IPC_MESSAGE
:
119 LogParam(*content::IPCInputEventPayload::Cast(payload
), l
);
121 case content::InputEvent::Payload::WEB_INPUT_EVENT
:
122 LogParam(*content::WebInputEventPayload::Cast(payload
), l
);
125 NOTREACHED() << "Invalid InputEvent::Payload type.";
126 l
->append("INVALID");
135 void ParamTraits
<content::WebInputEventPayload
>::Write(Message
* m
,
136 const param_type
& p
) {
137 bool valid_web_event
= !!p
.web_event();
138 WriteParam(m
, valid_web_event
);
140 WriteParam(m
, p
.web_event());
141 WriteParam(m
, p
.latency_info());
142 WriteParam(m
, p
.is_keyboard_shortcut());
145 bool ParamTraits
<content::WebInputEventPayload
>::Read(const Message
* m
,
146 PickleIterator
* iter
,
148 bool valid_web_event
;
149 WebInputEventPointer web_input_event_pointer
;
150 ui::LatencyInfo latency_info
;
151 bool is_keyboard_shortcut
;
152 if (!ReadParam(m
, iter
, &valid_web_event
) ||
154 !ReadParam(m
, iter
, &web_input_event_pointer
) ||
155 !ReadParam(m
, iter
, &latency_info
) ||
156 !ReadParam(m
, iter
, &is_keyboard_shortcut
))
159 p
->Initialize(*web_input_event_pointer
, latency_info
, is_keyboard_shortcut
);
163 void ParamTraits
<content::WebInputEventPayload
>::Log(const param_type
& p
,
165 l
->append("WebInputEventPayload(");
166 LogParam(p
.web_event(), l
);
168 LogParam(p
.latency_info(), l
);
170 LogParam(p
.is_keyboard_shortcut(), l
);