OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 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 shared by more than one child process. |
| 10 |
| 11 #ifndef CHROME_COMMON_COMMON_PARAM_TRAITS_H_ |
| 12 #define CHROME_COMMON_COMMON_PARAM_TRAITS_H_ |
| 13 |
| 14 #include "base/gfx/native_widget_types.h" |
| 15 #include "chrome/common/thumbnail_score.h" |
| 16 #include "chrome/common/transport_dib.h" |
| 17 #include "ipc/ipc_message_utils.h" |
| 18 #include "net/url_request/url_request_status.h" |
| 19 #include "webkit/glue/webcursor.h" |
| 20 #include "webkit/glue/window_open_disposition.h" |
| 21 |
| 22 // Forward declarations. |
| 23 class GURL; |
| 24 class SkBitmap; |
| 25 class DictionaryValue; |
| 26 class ListValue; |
| 27 |
| 28 namespace gfx { |
| 29 class Point; |
| 30 class Rect; |
| 31 class Size; |
| 32 } // namespace gfx |
| 33 |
| 34 namespace webkit_glue { |
| 35 struct WebApplicationInfo; |
| 36 } // namespace webkit_glue |
| 37 |
| 38 namespace IPC { |
| 39 |
| 40 template <> |
| 41 struct ParamTraits<SkBitmap> { |
| 42 typedef SkBitmap param_type; |
| 43 static void Write(Message* m, const param_type& p); |
| 44 |
| 45 // Note: This function expects parameter |r| to be of type &SkBitmap since |
| 46 // r->SetConfig() and r->SetPixels() are called. |
| 47 static bool Read(const Message* m, void** iter, param_type* r); |
| 48 |
| 49 static void Log(const param_type& p, std::wstring* l); |
| 50 }; |
| 51 |
| 52 |
| 53 template <> |
| 54 struct ParamTraits<GURL> { |
| 55 typedef GURL param_type; |
| 56 static void Write(Message* m, const param_type& p); |
| 57 static bool Read(const Message* m, void** iter, param_type* p); |
| 58 static void Log(const param_type& p, std::wstring* l); |
| 59 }; |
| 60 |
| 61 |
| 62 template <> |
| 63 struct ParamTraits<gfx::Point> { |
| 64 typedef gfx::Point param_type; |
| 65 static void Write(Message* m, const param_type& p); |
| 66 static bool Read(const Message* m, void** iter, param_type* r); |
| 67 static void Log(const param_type& p, std::wstring* l); |
| 68 }; |
| 69 |
| 70 template <> |
| 71 struct ParamTraits<gfx::Rect> { |
| 72 typedef gfx::Rect param_type; |
| 73 static void Write(Message* m, const param_type& p); |
| 74 static bool Read(const Message* m, void** iter, param_type* r); |
| 75 static void Log(const param_type& p, std::wstring* l); |
| 76 }; |
| 77 |
| 78 template <> |
| 79 struct ParamTraits<gfx::Size> { |
| 80 typedef gfx::Size param_type; |
| 81 static void Write(Message* m, const param_type& p); |
| 82 static bool Read(const Message* m, void** iter, param_type* r); |
| 83 static void Log(const param_type& p, std::wstring* l); |
| 84 }; |
| 85 |
| 86 template <> |
| 87 struct ParamTraits<gfx::NativeWindow> { |
| 88 typedef gfx::NativeWindow param_type; |
| 89 static void Write(Message* m, const param_type& p) { |
| 90 m->WriteIntPtr(reinterpret_cast<intptr_t>(p)); |
| 91 } |
| 92 static bool Read(const Message* m, void** iter, param_type* r) { |
| 93 DCHECK_EQ(sizeof(param_type), sizeof(intptr_t)); |
| 94 return m->ReadIntPtr(iter, reinterpret_cast<intptr_t*>(r)); |
| 95 } |
| 96 static void Log(const param_type& p, std::wstring* l) { |
| 97 l->append(StringPrintf(L"0x%X", p)); |
| 98 } |
| 99 }; |
| 100 |
| 101 |
| 102 template <> |
| 103 struct ParamTraits<WindowOpenDisposition> { |
| 104 typedef WindowOpenDisposition param_type; |
| 105 static void Write(Message* m, const param_type& p) { |
| 106 m->WriteInt(p); |
| 107 } |
| 108 static bool Read(const Message* m, void** iter, param_type* r) { |
| 109 int temp; |
| 110 bool res = m->ReadInt(iter, &temp); |
| 111 *r = static_cast<WindowOpenDisposition>(temp); |
| 112 return res; |
| 113 } |
| 114 static void Log(const param_type& p, std::wstring* l) { |
| 115 l->append(StringPrintf(L"%d", p)); |
| 116 } |
| 117 }; |
| 118 |
| 119 |
| 120 template <> |
| 121 struct ParamTraits<WebCursor> { |
| 122 typedef WebCursor param_type; |
| 123 static void Write(Message* m, const param_type& p) { |
| 124 p.Serialize(m); |
| 125 } |
| 126 static bool Read(const Message* m, void** iter, param_type* r) { |
| 127 return r->Deserialize(m, iter); |
| 128 } |
| 129 static void Log(const param_type& p, std::wstring* l) { |
| 130 l->append(L"<WebCursor>"); |
| 131 } |
| 132 }; |
| 133 |
| 134 |
| 135 template <> |
| 136 struct ParamTraits<webkit_glue::WebApplicationInfo> { |
| 137 typedef webkit_glue::WebApplicationInfo param_type; |
| 138 static void Write(Message* m, const param_type& p); |
| 139 static bool Read(const Message* m, void** iter, param_type* r); |
| 140 static void Log(const param_type& p, std::wstring* l); |
| 141 }; |
| 142 |
| 143 |
| 144 #if defined(OS_WIN) |
| 145 template<> |
| 146 struct ParamTraits<TransportDIB::Id> { |
| 147 typedef TransportDIB::Id param_type; |
| 148 static void Write(Message* m, const param_type& p) { |
| 149 WriteParam(m, p.handle); |
| 150 WriteParam(m, p.sequence_num); |
| 151 } |
| 152 static bool Read(const Message* m, void** iter, param_type* r) { |
| 153 return (ReadParam(m, iter, &r->handle) && |
| 154 ReadParam(m, iter, &r->sequence_num)); |
| 155 } |
| 156 static void Log(const param_type& p, std::wstring* l) { |
| 157 l->append(L"TransportDIB("); |
| 158 LogParam(p.handle, l); |
| 159 l->append(L", "); |
| 160 LogParam(p.sequence_num, l); |
| 161 l->append(L")"); |
| 162 } |
| 163 }; |
| 164 #endif |
| 165 |
| 166 // Traits for URLRequestStatus |
| 167 template <> |
| 168 struct ParamTraits<URLRequestStatus> { |
| 169 typedef URLRequestStatus param_type; |
| 170 static void Write(Message* m, const param_type& p) { |
| 171 WriteParam(m, static_cast<int>(p.status())); |
| 172 WriteParam(m, p.os_error()); |
| 173 } |
| 174 static bool Read(const Message* m, void** iter, param_type* r) { |
| 175 int status, os_error; |
| 176 if (!ReadParam(m, iter, &status) || |
| 177 !ReadParam(m, iter, &os_error)) |
| 178 return false; |
| 179 r->set_status(static_cast<URLRequestStatus::Status>(status)); |
| 180 r->set_os_error(os_error); |
| 181 return true; |
| 182 } |
| 183 static void Log(const param_type& p, std::wstring* l) { |
| 184 std::wstring status; |
| 185 switch (p.status()) { |
| 186 case URLRequestStatus::SUCCESS: |
| 187 status = L"SUCCESS"; |
| 188 break; |
| 189 case URLRequestStatus::IO_PENDING: |
| 190 status = L"IO_PENDING "; |
| 191 break; |
| 192 case URLRequestStatus::HANDLED_EXTERNALLY: |
| 193 status = L"HANDLED_EXTERNALLY"; |
| 194 break; |
| 195 case URLRequestStatus::CANCELED: |
| 196 status = L"CANCELED"; |
| 197 break; |
| 198 case URLRequestStatus::FAILED: |
| 199 status = L"FAILED"; |
| 200 break; |
| 201 default: |
| 202 status = L"UNKNOWN"; |
| 203 break; |
| 204 } |
| 205 if (p.status() == URLRequestStatus::FAILED) |
| 206 l->append(L"("); |
| 207 |
| 208 LogParam(status, l); |
| 209 |
| 210 if (p.status() == URLRequestStatus::FAILED) { |
| 211 l->append(L", "); |
| 212 LogParam(p.os_error(), l); |
| 213 l->append(L")"); |
| 214 } |
| 215 } |
| 216 }; |
| 217 |
| 218 template<> |
| 219 struct ParamTraits<ThumbnailScore> { |
| 220 typedef ThumbnailScore param_type; |
| 221 static void Write(Message* m, const param_type& p) { |
| 222 IPC::ParamTraits<double>::Write(m, p.boring_score); |
| 223 IPC::ParamTraits<bool>::Write(m, p.good_clipping); |
| 224 IPC::ParamTraits<bool>::Write(m, p.at_top); |
| 225 IPC::ParamTraits<base::Time>::Write(m, p.time_at_snapshot); |
| 226 } |
| 227 static bool Read(const Message* m, void** iter, param_type* r) { |
| 228 double boring_score; |
| 229 bool good_clipping, at_top; |
| 230 base::Time time_at_snapshot; |
| 231 if (!IPC::ParamTraits<double>::Read(m, iter, &boring_score) || |
| 232 !IPC::ParamTraits<bool>::Read(m, iter, &good_clipping) || |
| 233 !IPC::ParamTraits<bool>::Read(m, iter, &at_top) || |
| 234 !IPC::ParamTraits<base::Time>::Read(m, iter, &time_at_snapshot)) |
| 235 return false; |
| 236 |
| 237 r->boring_score = boring_score; |
| 238 r->good_clipping = good_clipping; |
| 239 r->at_top = at_top; |
| 240 r->time_at_snapshot = time_at_snapshot; |
| 241 return true; |
| 242 } |
| 243 static void Log(const param_type& p, std::wstring* l) { |
| 244 l->append(StringPrintf(L"(%f, %d, %d)", |
| 245 p.boring_score, p.good_clipping, p.at_top)); |
| 246 } |
| 247 }; |
| 248 |
| 249 } // namespace IPC |
| 250 |
| 251 #endif // CHROME_COMMON_COMMON_PARAM_TRAITS_H_ |
OLD | NEW |