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"
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
;
35 NPIdentifier_Param::NPIdentifier_Param()
39 NPIdentifier_Param::~NPIdentifier_Param() {
42 NPVariant_Param::NPVariant_Param()
43 : type(NPVARIANT_PARAM_VOID
),
47 npobject_routing_id(-1) {
50 NPVariant_Param::~NPVariant_Param() {
53 } // namespace content
55 using content::NPIdentifier_Param
;
56 using content::NPVariant_Param
;
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
,
67 net::IPAddressNumber address
;
69 if (!ReadParam(m
, iter
, &address
) || !ReadParam(m
, iter
, &port
))
71 *p
= net::IPEndPoint(address
, port
);
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
);
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
,
105 if (!ReadParam(m
, iter
, &type
))
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
)) {
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
);
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
,
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
);
164 webkit::npapi::PluginHost::Singleton()->host_functions()->memfree(str
);
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
,
180 if (!m
->ReadUInt64(iter
, &start
) || !m
->ReadUInt64(iter
, &end
))
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
,
200 if (!m
->ReadData(iter
, &data
, &data_length
)) {
204 if (data_length
< static_cast<int>(sizeof(WebKit::WebInputEvent
))) {
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
)) {
214 if (data_length
!= WebInputEventSizeForType(event
->type
)) {
222 void ParamTraits
<WebInputEventPointer
>::Log(const param_type
& p
,
225 LogParam(p
->size
, l
);
227 LogParam(p
->type
, l
);
229 LogParam(p
->timeStampSeconds
, l
);
235 // Generate param traits write methods.
236 #include "ipc/param_traits_write_macros.h"
238 #undef CONTENT_COMMON_CONTENT_PARAM_TRAITS_MACROS_H_
239 #include "content/common/content_param_traits_macros.h"
242 // Generate param traits read methods.
243 #include "ipc/param_traits_read_macros.h"
245 #undef CONTENT_COMMON_CONTENT_PARAM_TRAITS_MACROS_H_
246 #include "content/common/content_param_traits_macros.h"
249 // Generate param traits log methods.
250 #include "ipc/param_traits_log_macros.h"
252 #undef CONTENT_COMMON_CONTENT_PARAM_TRAITS_MACROS_H_
253 #include "content/common/content_param_traits_macros.h"