[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / common / content_param_traits.cc
blobbc786599714d67f6130280bb330de75c837dbef5
1 // Copyright (c) 2012 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/content_param_traits.h"
7 #include "base/string_number_conversions.h"
8 #include "net/base/ip_endpoint.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
10 #include "webkit/glue/npruntime_util.h"
11 #include "webkit/plugins/npapi/plugin_host.h"
12 #include "ui/base/range/range.h"
14 namespace {
16 int WebInputEventSizeForType(WebKit::WebInputEvent::Type type) {
17 if (WebKit::WebInputEvent::isMouseEventType(type))
18 return sizeof(WebKit::WebMouseEvent);
19 if (type == WebKit::WebInputEvent::MouseWheel)
20 return sizeof(WebKit::WebMouseWheelEvent);
21 if (WebKit::WebInputEvent::isKeyboardEventType(type))
22 return sizeof(WebKit::WebKeyboardEvent);
23 if (WebKit::WebInputEvent::isTouchEventType(type))
24 return sizeof(WebKit::WebTouchEvent);
25 if (WebKit::WebInputEvent::isGestureEventType(type))
26 return sizeof(WebKit::WebGestureEvent);
27 NOTREACHED() << "Unknown webkit event type " << type;
28 return 0;
31 } // namespace
33 namespace content {
35 NPIdentifier_Param::NPIdentifier_Param()
36 : identifier() {
39 NPIdentifier_Param::~NPIdentifier_Param() {
42 NPVariant_Param::NPVariant_Param()
43 : type(NPVARIANT_PARAM_VOID),
44 bool_value(false),
45 int_value(0),
46 double_value(0),
47 npobject_routing_id(-1) {
50 NPVariant_Param::~NPVariant_Param() {
53 } // namespace content
55 using content::NPIdentifier_Param;
56 using content::NPVariant_Param;
58 namespace IPC {
60 void ParamTraits<net::IPEndPoint>::Write(Message* m, const param_type& p) {
61 WriteParam(m, p.address());
62 WriteParam(m, p.port());
65 bool ParamTraits<net::IPEndPoint>::Read(const Message* m, PickleIterator* iter,
66 param_type* p) {
67 net::IPAddressNumber address;
68 int port;
69 if (!ReadParam(m, iter, &address) || !ReadParam(m, iter, &port))
70 return false;
71 *p = net::IPEndPoint(address, port);
72 return true;
75 void ParamTraits<net::IPEndPoint>::Log(const param_type& p, std::string* l) {
76 LogParam("IPEndPoint:" + p.ToString(), l);
79 void ParamTraits<NPVariant_Param>::Write(Message* m, const param_type& p) {
80 WriteParam(m, static_cast<int>(p.type));
81 if (p.type == content::NPVARIANT_PARAM_BOOL) {
82 WriteParam(m, p.bool_value);
83 } else if (p.type == content::NPVARIANT_PARAM_INT) {
84 WriteParam(m, p.int_value);
85 } else if (p.type == content::NPVARIANT_PARAM_DOUBLE) {
86 WriteParam(m, p.double_value);
87 } else if (p.type == content::NPVARIANT_PARAM_STRING) {
88 WriteParam(m, p.string_value);
89 } else if (p.type == content::NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID ||
90 p.type == content::NPVARIANT_PARAM_RECEIVER_OBJECT_ROUTING_ID) {
91 // This is the routing id used to connect NPObjectProxy in the other
92 // process with NPObjectStub in this process or to identify the raw
93 // npobject pointer to be used in the callee process.
94 WriteParam(m, p.npobject_routing_id);
95 } else {
96 DCHECK(p.type == content::NPVARIANT_PARAM_VOID ||
97 p.type == content::NPVARIANT_PARAM_NULL);
101 bool ParamTraits<NPVariant_Param>::Read(const Message* m,
102 PickleIterator* iter,
103 param_type* r) {
104 int type;
105 if (!ReadParam(m, iter, &type))
106 return false;
108 bool result = false;
109 r->type = static_cast<content::NPVariant_ParamEnum>(type);
110 if (r->type == content::NPVARIANT_PARAM_BOOL) {
111 result = ReadParam(m, iter, &r->bool_value);
112 } else if (r->type == content::NPVARIANT_PARAM_INT) {
113 result = ReadParam(m, iter, &r->int_value);
114 } else if (r->type == content::NPVARIANT_PARAM_DOUBLE) {
115 result = ReadParam(m, iter, &r->double_value);
116 } else if (r->type == content::NPVARIANT_PARAM_STRING) {
117 result = ReadParam(m, iter, &r->string_value);
118 } else if (r->type == content::NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID ||
119 r->type == content::NPVARIANT_PARAM_RECEIVER_OBJECT_ROUTING_ID) {
120 result = ReadParam(m, iter, &r->npobject_routing_id);
121 } else if ((r->type == content::NPVARIANT_PARAM_VOID) ||
122 (r->type == content::NPVARIANT_PARAM_NULL)) {
123 result = true;
124 } else {
125 NOTREACHED();
128 return result;
131 void ParamTraits<NPVariant_Param>::Log(const param_type& p, std::string* l) {
132 l->append(StringPrintf("NPVariant_Param(%d, ", static_cast<int>(p.type)));
133 if (p.type == content::NPVARIANT_PARAM_BOOL) {
134 LogParam(p.bool_value, l);
135 } else if (p.type == content::NPVARIANT_PARAM_INT) {
136 LogParam(p.int_value, l);
137 } else if (p.type == content::NPVARIANT_PARAM_DOUBLE) {
138 LogParam(p.double_value, l);
139 } else if (p.type == content::NPVARIANT_PARAM_STRING) {
140 LogParam(p.string_value, l);
141 } else if (p.type == content::NPVARIANT_PARAM_SENDER_OBJECT_ROUTING_ID ||
142 p.type == content::NPVARIANT_PARAM_RECEIVER_OBJECT_ROUTING_ID) {
143 LogParam(p.npobject_routing_id, l);
144 } else {
145 l->append("<none>");
147 l->append(")");
150 void ParamTraits<NPIdentifier_Param>::Write(Message* m, const param_type& p) {
151 webkit_glue::SerializeNPIdentifier(p.identifier, m);
154 bool ParamTraits<NPIdentifier_Param>::Read(const Message* m,
155 PickleIterator* iter,
156 param_type* r) {
157 return webkit_glue::DeserializeNPIdentifier(iter, &r->identifier);
160 void ParamTraits<NPIdentifier_Param>::Log(const param_type& p, std::string* l) {
161 if (WebKit::WebBindings::identifierIsString(p.identifier)) {
162 NPUTF8* str = WebKit::WebBindings::utf8FromIdentifier(p.identifier);
163 l->append(str);
164 webkit::npapi::PluginHost::Singleton()->host_functions()->memfree(str);
165 } else {
166 l->append(base::IntToString(
167 WebKit::WebBindings::intFromIdentifier(p.identifier)));
171 void ParamTraits<ui::Range>::Write(Message* m, const ui::Range& r) {
172 m->WriteUInt64(r.start());
173 m->WriteUInt64(r.end());
176 bool ParamTraits<ui::Range>::Read(const Message* m,
177 PickleIterator* iter,
178 ui::Range* r) {
179 uint64 start, end;
180 if (!m->ReadUInt64(iter, &start) || !m->ReadUInt64(iter, &end))
181 return false;
182 r->set_start(start);
183 r->set_end(end);
184 return true;
187 void ParamTraits<ui::Range>::Log(const ui::Range& r, std::string* l) {
188 l->append(base::StringPrintf("(%"PRIuS", %"PRIuS")", r.start(), r.end()));
191 void ParamTraits<WebInputEventPointer>::Write(Message* m, const param_type& p) {
192 m->WriteData(reinterpret_cast<const char*>(p), p->size);
195 bool ParamTraits<WebInputEventPointer>::Read(const Message* m,
196 PickleIterator* iter,
197 param_type* r) {
198 const char* data;
199 int data_length;
200 if (!m->ReadData(iter, &data, &data_length)) {
201 NOTREACHED();
202 return false;
204 if (data_length < static_cast<int>(sizeof(WebKit::WebInputEvent))) {
205 NOTREACHED();
206 return false;
208 param_type event = reinterpret_cast<param_type>(data);
209 // Check that the data size matches that of the event.
210 if (data_length != static_cast<int>(event->size)) {
211 NOTREACHED();
212 return false;
214 if (data_length != WebInputEventSizeForType(event->type)) {
215 NOTREACHED();
216 return false;
218 *r = event;
219 return true;
222 void ParamTraits<WebInputEventPointer>::Log(const param_type& p,
223 std::string* l) {
224 l->append("(");
225 LogParam(p->size, l);
226 l->append(", ");
227 LogParam(p->type, l);
228 l->append(", ");
229 LogParam(p->timeStampSeconds, l);
230 l->append(")");
233 } // namespace IPC
235 // Generate param traits write methods.
236 #include "ipc/param_traits_write_macros.h"
237 namespace IPC {
238 #undef CONTENT_COMMON_CONTENT_PARAM_TRAITS_MACROS_H_
239 #include "content/common/content_param_traits_macros.h"
240 } // namespace IPC
242 // Generate param traits read methods.
243 #include "ipc/param_traits_read_macros.h"
244 namespace IPC {
245 #undef CONTENT_COMMON_CONTENT_PARAM_TRAITS_MACROS_H_
246 #include "content/common/content_param_traits_macros.h"
247 } // namespace IPC
249 // Generate param traits log methods.
250 #include "ipc/param_traits_log_macros.h"
251 namespace IPC {
252 #undef CONTENT_COMMON_CONTENT_PARAM_TRAITS_MACROS_H_
253 #include "content/common/content_param_traits_macros.h"
254 } // namespace IPC