Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / common / common_param_traits.h
blob52d7a2f8c9b7f37af21ec992a6d99b9d329cac50
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 // This file is used to define IPC::ParamTraits<> specializations for a number
6 // of types so that they can be serialized over IPC. IPC::ParamTraits<>
7 // specializations for basic types (like int and std::string) and types in the
8 // 'base' project can be found in ipc/ipc_message_utils.h. This file contains
9 // specializations for types that are used by the content code, and which need
10 // manual serialization code. This is usually because they're not structs with
11 // public members, or because the same type is being used in multiple
12 // *_messages.h headers.
14 #ifndef CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_H_
15 #define CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_H_
17 #include <string>
19 #include "base/memory/ref_counted.h"
20 #include "content/common/content_export.h"
21 #include "content/public/common/common_param_traits_macros.h"
22 #include "ipc/ipc_message_utils.h"
23 #include "ui/gfx/native_widget_types.h"
24 #include "ui/surface/transport_dib.h"
25 #include "url/gurl.h"
26 #include "url/origin.h"
28 namespace content {
29 class PageState;
32 namespace net {
33 class HostPortPair;
34 class IPEndPoint;
37 namespace IPC {
39 template <>
40 struct CONTENT_EXPORT ParamTraits<GURL> {
41 typedef GURL param_type;
42 static void Write(Message* m, const param_type& p);
43 static bool Read(const Message* m, PickleIterator* iter, param_type* p);
44 static void Log(const param_type& p, std::string* l);
47 template <>
48 struct CONTENT_EXPORT ParamTraits<url::Origin> {
49 typedef url::Origin param_type;
50 static void Write(Message* m, const param_type& p);
51 static bool Read(const Message* m, PickleIterator* iter, param_type* p);
52 static void Log(const param_type& p, std::string* l);
55 template<>
56 struct CONTENT_EXPORT ParamTraits<net::HostPortPair> {
57 typedef net::HostPortPair param_type;
58 static void Write(Message* m, const param_type& p);
59 static bool Read(const Message* m, PickleIterator* iter, param_type* r);
60 static void Log(const param_type& p, std::string* l);
63 template <>
64 struct CONTENT_EXPORT ParamTraits<net::IPEndPoint> {
65 typedef net::IPEndPoint param_type;
66 static void Write(Message* m, const param_type& p);
67 static bool Read(const Message* m, PickleIterator* iter, param_type* p);
68 static void Log(const param_type& p, std::string* l);
71 template <>
72 struct CONTENT_EXPORT ParamTraits<content::PageState> {
73 typedef content::PageState param_type;
74 static void Write(Message* m, const param_type& p);
75 static bool Read(const Message* m, PickleIterator* iter, param_type* p);
76 static void Log(const param_type& p, std::string* l);
79 template <>
80 struct ParamTraits<gfx::NativeWindow> {
81 typedef gfx::NativeWindow param_type;
82 static void Write(Message* m, const param_type& p) {
83 #if defined(OS_WIN)
84 // HWNDs are always 32 bits on Windows, even on 64 bit systems.
85 m->WriteUInt32(reinterpret_cast<uint32>(p));
86 #else
87 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(p));
88 #endif
90 static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
91 #if defined(OS_WIN)
92 return iter->ReadUInt32(reinterpret_cast<uint32*>(r));
93 #else
94 const char *data;
95 int data_size = 0;
96 bool result = iter->ReadData(&data, &data_size);
97 if (result && data_size == sizeof(gfx::NativeWindow)) {
98 memcpy(r, data, sizeof(gfx::NativeWindow));
99 } else {
100 result = false;
101 NOTREACHED();
103 return result;
104 #endif
106 static void Log(const param_type& p, std::string* l) {
107 l->append("<gfx::NativeWindow>");
111 #if defined(OS_WIN)
112 template<>
113 struct ParamTraits<TransportDIB::Id> {
114 typedef TransportDIB::Id param_type;
115 static void Write(Message* m, const param_type& p) {
116 WriteParam(m, p.handle);
117 WriteParam(m, p.sequence_num);
119 static bool Read(const Message* m, PickleIterator* iter, param_type* r) {
120 return (ReadParam(m, iter, &r->handle) &&
121 ReadParam(m, iter, &r->sequence_num));
123 static void Log(const param_type& p, std::string* l) {
124 l->append("TransportDIB(");
125 LogParam(p.handle, l);
126 l->append(", ");
127 LogParam(p.sequence_num, l);
128 l->append(")");
131 #endif
133 } // namespace IPC
135 #endif // CONTENT_PUBLIC_COMMON_COMMON_PARAM_TRAITS_H_