Make developerPrivate API available in stable.
[chromium-blink-merge.git] / content / common / input / input_param_traits.cc
blobe3aaf670ffd54318260017b960db918cf4111c56
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"
13 namespace IPC {
14 namespace {
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>();
23 } // namespace
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,
31 PickleIterator* iter,
32 param_type* p) {
33 int64 id;
34 content::EventPacket::InputEvents events;
35 if (!ReadParam(m, iter, &id) ||
36 !ReadParam(m, iter, &events))
37 return false;
39 p->set_id(id);
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)
45 continue;
46 if (!p->Add(event.Pass()))
47 events_added_successfully = false;
49 events.weak_clear();
50 return events_added_successfully;
53 void ParamTraits<content::EventPacket>::Log(const param_type& p,
54 std::string* l) {
55 l->append("EventPacket((");
56 LogParam(p.id(), l);
57 l->append("), Events(");
58 LogParam(p.events(), l);
59 l->append("))");
62 void ParamTraits<content::InputEvent>::Write(Message* m, const param_type& p) {
63 WriteParam(m, p.id());
64 WriteParam(m, !!p.payload());
65 if (!p.valid())
66 return;
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()));
73 break;
74 case content::InputEvent::Payload::WEB_INPUT_EVENT:
75 WriteParam(m, *content::WebInputEventPayload::Cast(p.payload()));
76 break;
77 default:
78 break;
82 bool ParamTraits<content::InputEvent>::Read(const Message* m,
83 PickleIterator* iter,
84 param_type* p) {
85 int64 id;
86 bool has_payload = false;
87 content::InputEvent::Payload::Type payload_type;
88 if (!ReadParam(m, iter, &id) ||
89 !ReadParam(m, iter, &has_payload) ||
90 !id ||
91 !has_payload ||
92 !ReadParam(m, iter, &payload_type))
93 return false;
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);
99 break;
100 case content::InputEvent::Payload::WEB_INPUT_EVENT:
101 payload = ReadPayload<content::WebInputEventPayload>(m, iter);
102 break;
103 default:
104 NOTREACHED() << "Invalid InputEvent::Payload type.";
105 return false;
107 return p->Initialize(id, payload.Pass());
110 void ParamTraits<content::InputEvent>::Log(const param_type& p,
111 std::string* l) {
112 l->append("InputEvent((");
113 LogParam(p.id(), l);
114 l->append("), Payload (");
115 const content::InputEvent::Payload* payload = p.payload();
116 if (payload) {
117 switch (payload->GetType()) {
118 case content::InputEvent::Payload::IPC_MESSAGE:
119 LogParam(*content::IPCInputEventPayload::Cast(payload), l);
120 break;
121 case content::InputEvent::Payload::WEB_INPUT_EVENT:
122 LogParam(*content::WebInputEventPayload::Cast(payload), l);
123 break;
124 default:
125 NOTREACHED() << "Invalid InputEvent::Payload type.";
126 l->append("INVALID");
127 break;
129 } else {
130 l->append("NULL");
132 l->append("))");
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);
139 if (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,
147 param_type* p) {
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) ||
153 !valid_web_event ||
154 !ReadParam(m, iter, &web_input_event_pointer) ||
155 !ReadParam(m, iter, &latency_info) ||
156 !ReadParam(m, iter, &is_keyboard_shortcut))
157 return false;
159 p->Initialize(*web_input_event_pointer, latency_info, is_keyboard_shortcut);
160 return true;
163 void ParamTraits<content::WebInputEventPayload>::Log(const param_type& p,
164 std::string* l) {
165 l->append("WebInputEventPayload(");
166 LogParam(p.web_event(), l);
167 l->append(", ");
168 LogParam(p.latency_info(), l);
169 l->append(", ");
170 LogParam(p.is_keyboard_shortcut(), l);
171 l->append(")");
174 } // namespace IPC