OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
3 // LICENSE file. | 3 // LICENSE file. |
4 // | 4 // |
5 // This file contains ParamTraits templates to support serialization of WebKit | 5 // This file contains ParamTraits templates to support serialization of WebKit |
6 // data types over IPC. | 6 // data types over IPC. |
| 7 // |
| 8 // NOTE: IT IS IMPORTANT THAT ONLY POD (plain old data) TYPES ARE SERIALIZED. |
| 9 // |
| 10 // There are several reasons for this restrictions: |
| 11 // |
| 12 // o We don't want inclusion of this file to imply linking to WebKit code. |
| 13 // |
| 14 // o Many WebKit structures are not thread-safe. WebString, for example, |
| 15 // contains a reference counted buffer, which does not use thread-safe |
| 16 // reference counting. If we allowed serializing WebString, then we may ru
n |
| 17 // the risk of introducing subtle thread-safety bugs if people passed a |
| 18 // WebString across threads via PostTask(NewRunnableMethod(...)). |
| 19 // |
| 20 // o The WebKit API has redundant types for strings, and we should avoid usin
g |
| 21 // those beyond code that interfaces with the WebKit API. |
7 | 22 |
8 #ifndef CHROME_COMMON_WEBKIT_PARAM_TRAITS_H_ | 23 #ifndef CHROME_COMMON_WEBKIT_PARAM_TRAITS_H_ |
9 #define CHROME_COMMON_WEBKIT_PARAM_TRAITS_H_ | 24 #define CHROME_COMMON_WEBKIT_PARAM_TRAITS_H_ |
10 | 25 |
11 #include "chrome/common/ipc_message_utils.h" | 26 #include "chrome/common/ipc_message_utils.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebCache.h" | 27 #include "third_party/WebKit/WebKit/chromium/public/WebCache.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebConsoleMessage.h" | 28 #include "third_party/WebKit/WebKit/chromium/public/WebConsoleMessage.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebFindInPageRequest.h" | 29 #include "third_party/WebKit/WebKit/chromium/public/WebFindOptions.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h" | 30 #include "third_party/WebKit/WebKit/chromium/public/WebInputEvent.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebScreenInfo.h" | 31 #include "third_party/WebKit/WebKit/chromium/public/WebScreenInfo.h" |
17 | 32 |
18 namespace IPC { | 33 namespace IPC { |
19 | 34 |
20 template <> | 35 template <> |
21 struct ParamTraits<WebKit::WebRect> { | 36 struct ParamTraits<WebKit::WebRect> { |
22 typedef WebKit::WebRect param_type; | 37 typedef WebKit::WebRect param_type; |
23 static void Write(Message* m, const param_type& p) { | 38 static void Write(Message* m, const param_type& p) { |
24 WriteParam(m, p.x); | 39 WriteParam(m, p.x); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 LogParam(p.isMonochrome, l); | 88 LogParam(p.isMonochrome, l); |
74 l->append(L", "); | 89 l->append(L", "); |
75 LogParam(p.rect, l); | 90 LogParam(p.rect, l); |
76 l->append(L", "); | 91 l->append(L", "); |
77 LogParam(p.availableRect, l); | 92 LogParam(p.availableRect, l); |
78 l->append(L")"); | 93 l->append(L")"); |
79 } | 94 } |
80 }; | 95 }; |
81 | 96 |
82 template <> | 97 template <> |
83 struct ParamTraits<WebKit::WebString> { | |
84 typedef WebKit::WebString param_type; | |
85 static void Write(Message* m, const param_type& p) { | |
86 m->WriteData(reinterpret_cast<const char*>(p.data()), | |
87 static_cast<int>(p.length() * sizeof(WebKit::WebUChar))); | |
88 } | |
89 static bool Read(const Message* m, void** iter, param_type* p) { | |
90 const char* data; | |
91 int data_len; | |
92 if (!m->ReadData(iter, &data, &data_len)) | |
93 return false; | |
94 p->assign(reinterpret_cast<const WebKit::WebUChar*>(data), | |
95 static_cast<size_t>(data_len / sizeof(WebKit::WebUChar))); | |
96 return true; | |
97 } | |
98 static void Log(const param_type& p, std::wstring* l) { | |
99 l->append(UTF16ToWideHack(p)); | |
100 } | |
101 }; | |
102 | |
103 template <> | |
104 struct ParamTraits<WebKit::WebConsoleMessage::Level> { | 98 struct ParamTraits<WebKit::WebConsoleMessage::Level> { |
105 typedef WebKit::WebConsoleMessage::Level param_type; | 99 typedef WebKit::WebConsoleMessage::Level param_type; |
106 static void Write(Message* m, const param_type& p) { | 100 static void Write(Message* m, const param_type& p) { |
107 WriteParam(m, static_cast<int>(p)); | 101 WriteParam(m, static_cast<int>(p)); |
108 } | 102 } |
109 static bool Read(const Message* m, void** iter, param_type* r) { | 103 static bool Read(const Message* m, void** iter, param_type* r) { |
110 int value; | 104 int value; |
111 if (!ReadParam(m, iter, &value)) | 105 if (!ReadParam(m, iter, &value)) |
112 return false; | 106 return false; |
113 *r = static_cast<param_type>(value); | 107 *r = static_cast<param_type>(value); |
114 return true; | 108 return true; |
115 } | 109 } |
116 static void Log(const param_type& p, std::wstring* l) { | 110 static void Log(const param_type& p, std::wstring* l) { |
117 LogParam(static_cast<int>(p), l); | 111 LogParam(static_cast<int>(p), l); |
118 } | 112 } |
119 }; | 113 }; |
120 | 114 |
121 template <> | 115 template <> |
122 struct ParamTraits<WebKit::WebConsoleMessage> { | 116 struct ParamTraits<WebKit::WebFindOptions> { |
123 typedef WebKit::WebConsoleMessage param_type; | 117 typedef WebKit::WebFindOptions param_type; |
124 static void Write(Message* m, const param_type& p) { | 118 static void Write(Message* m, const param_type& p) { |
125 WriteParam(m, p.level); | |
126 WriteParam(m, p.text); | |
127 } | |
128 static bool Read(const Message* m, void** iter, param_type* r) { | |
129 return | |
130 ReadParam(m, iter, &r->level) && | |
131 ReadParam(m, iter, &r->text); | |
132 } | |
133 static void Log(const param_type& p, std::wstring* l) { | |
134 l->append(L"("); | |
135 LogParam(p.level, l); | |
136 l->append(L", "); | |
137 LogParam(p.text, l); | |
138 l->append(L")"); | |
139 } | |
140 }; | |
141 | |
142 template <> | |
143 struct ParamTraits<WebKit::WebFindInPageRequest> { | |
144 typedef WebKit::WebFindInPageRequest param_type; | |
145 static void Write(Message* m, const param_type& p) { | |
146 WriteParam(m, p.identifier); | |
147 WriteParam(m, p.text); | |
148 WriteParam(m, p.forward); | 119 WriteParam(m, p.forward); |
149 WriteParam(m, p.matchCase); | 120 WriteParam(m, p.matchCase); |
150 WriteParam(m, p.findNext); | 121 WriteParam(m, p.findNext); |
151 } | 122 } |
152 static bool Read(const Message* m, void** iter, param_type* p) { | 123 static bool Read(const Message* m, void** iter, param_type* p) { |
153 return | 124 return |
154 ReadParam(m, iter, &p->identifier) && | |
155 ReadParam(m, iter, &p->text) && | |
156 ReadParam(m, iter, &p->forward) && | 125 ReadParam(m, iter, &p->forward) && |
157 ReadParam(m, iter, &p->matchCase) && | 126 ReadParam(m, iter, &p->matchCase) && |
158 ReadParam(m, iter, &p->findNext); | 127 ReadParam(m, iter, &p->findNext); |
159 } | 128 } |
160 static void Log(const param_type& p, std::wstring* l) { | 129 static void Log(const param_type& p, std::wstring* l) { |
161 l->append(L"<FindInPageRequest>"); | 130 l->append(L"("); |
| 131 LogParam(p.forward, l); |
| 132 l->append(L", "); |
| 133 LogParam(p.matchCase, l); |
| 134 l->append(L", "); |
| 135 LogParam(p.findNext, l); |
| 136 l->append(L")"); |
162 } | 137 } |
163 }; | 138 }; |
164 | 139 |
165 template <> | 140 template <> |
166 struct ParamTraits<WebKit::WebInputEvent::Type> { | 141 struct ParamTraits<WebKit::WebInputEvent::Type> { |
167 typedef WebKit::WebInputEvent::Type param_type; | 142 typedef WebKit::WebInputEvent::Type param_type; |
168 static void Write(Message* m, const param_type& p) { | 143 static void Write(Message* m, const param_type& p) { |
169 m->WriteInt(p); | 144 m->WriteInt(p); |
170 } | 145 } |
171 static bool Read(const Message* m, void** iter, param_type* p) { | 146 static bool Read(const Message* m, void** iter, param_type* p) { |
172 int type; | 147 int type; |
173 if (!m->ReadInt(iter, &type)) | 148 if (!m->ReadInt(iter, &type)) |
174 return false; | 149 return false; |
175 *p = static_cast<WebKit::WebInputEvent::Type>(type); | 150 *p = static_cast<WebKit::WebInputEvent::Type>(type); |
176 return true; | 151 return true; |
177 } | 152 } |
178 static void Log(const param_type& p, std::wstring* l) { | 153 static void Log(const param_type& p, std::wstring* l) { |
179 std::wstring type; | 154 const wchar_t* type; |
180 switch (p) { | 155 switch (p) { |
181 case WebKit::WebInputEvent::MouseDown: | 156 case WebKit::WebInputEvent::MouseDown: |
182 type = L"MouseDown"; | 157 type = L"MouseDown"; |
183 break; | 158 break; |
184 case WebKit::WebInputEvent::MouseUp: | 159 case WebKit::WebInputEvent::MouseUp: |
185 type = L"MouseUp"; | 160 type = L"MouseUp"; |
186 break; | 161 break; |
187 case WebKit::WebInputEvent::MouseMove: | 162 case WebKit::WebInputEvent::MouseMove: |
188 type = L"MouseMove"; | 163 type = L"MouseMove"; |
189 break; | 164 break; |
(...skipping 12 matching lines...) Expand all Loading... |
202 case WebKit::WebInputEvent::KeyDown: | 177 case WebKit::WebInputEvent::KeyDown: |
203 type = L"KeyDown"; | 178 type = L"KeyDown"; |
204 break; | 179 break; |
205 case WebKit::WebInputEvent::KeyUp: | 180 case WebKit::WebInputEvent::KeyUp: |
206 type = L"KeyUp"; | 181 type = L"KeyUp"; |
207 break; | 182 break; |
208 default: | 183 default: |
209 type = L"None"; | 184 type = L"None"; |
210 break; | 185 break; |
211 } | 186 } |
212 LogParam(type, l); | 187 LogParam(std::wstring(type), l); |
213 } | 188 } |
214 }; | 189 }; |
215 | 190 |
216 // Traits for WebKit::WebCache::UsageStats | |
217 template <> | 191 template <> |
218 struct ParamTraits<WebKit::WebCache::UsageStats> { | 192 struct ParamTraits<WebKit::WebCache::UsageStats> { |
219 typedef WebKit::WebCache::UsageStats param_type; | 193 typedef WebKit::WebCache::UsageStats param_type; |
220 static void Write(Message* m, const param_type& p) { | 194 static void Write(Message* m, const param_type& p) { |
221 WriteParam(m, p.minDeadCapacity); | 195 WriteParam(m, p.minDeadCapacity); |
222 WriteParam(m, p.maxDeadCapacity); | 196 WriteParam(m, p.maxDeadCapacity); |
223 WriteParam(m, p.capacity); | 197 WriteParam(m, p.capacity); |
224 WriteParam(m, p.liveSize); | 198 WriteParam(m, p.liveSize); |
225 WriteParam(m, p.deadSize); | 199 WriteParam(m, p.deadSize); |
226 } | 200 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 LogParam(p.scripts, l); | 260 LogParam(p.scripts, l); |
287 LogParam(p.xslStyleSheets, l); | 261 LogParam(p.xslStyleSheets, l); |
288 LogParam(p.fonts, l); | 262 LogParam(p.fonts, l); |
289 l->append(L"</WebCoreStats>"); | 263 l->append(L"</WebCoreStats>"); |
290 } | 264 } |
291 }; | 265 }; |
292 | 266 |
293 } // namespace IPC | 267 } // namespace IPC |
294 | 268 |
295 #endif // CHROME_COMMON_WEBKIT_PARAM_TRAITS_H_ | 269 #endif // CHROME_COMMON_WEBKIT_PARAM_TRAITS_H_ |
OLD | NEW |