common_param_traits.h revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2011 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#pragma once 14 15#include "app/surface/transport_dib.h" 16#include "base/file_util.h" 17#include "base/ref_counted.h" 18#include "chrome/common/content_settings.h" 19#include "chrome/common/page_zoom.h" 20#include "ipc/ipc_message_utils.h" 21#include "printing/native_metafile.h" 22// !!! WARNING: DO NOT ADD NEW WEBKIT DEPENDENCIES !!! 23// 24// That means don't add #includes to any file in 'webkit/' or 25// 'third_party/WebKit/'. Chrome Frame and NACL build parts of base/ and 26// chrome/common/ for a mini-library that doesn't depend on webkit. 27// 28// TODO(erg): The following headers are historical and only work because 29// their definitions are inlined, which also needs to be fixed. 30#include "ui/gfx/native_widget_types.h" 31#include "webkit/glue/webcursor.h" 32#include "webkit/glue/window_open_disposition.h" 33 34// Forward declarations. 35struct Geoposition; 36class SkBitmap; 37class DictionaryValue; 38class ListValue; 39struct ThumbnailScore; 40struct WebApplicationInfo; 41class WebCursor; 42 43namespace gfx { 44class Point; 45class Rect; 46class Size; 47} // namespace gfx 48 49namespace printing { 50struct PageRange; 51struct PrinterCapsAndDefaults; 52} // namespace printing 53 54namespace webkit_glue { 55struct PasswordForm; 56} // namespace webkit_glue 57 58namespace IPC { 59 60template <> 61struct ParamTraits<SkBitmap> { 62 typedef SkBitmap param_type; 63 static void Write(Message* m, const param_type& p); 64 65 // Note: This function expects parameter |r| to be of type &SkBitmap since 66 // r->SetConfig() and r->SetPixels() are called. 67 static bool Read(const Message* m, void** iter, param_type* r); 68 69 static void Log(const param_type& p, std::string* l); 70}; 71 72template <> 73struct ParamTraits<gfx::Point> { 74 typedef gfx::Point param_type; 75 static void Write(Message* m, const param_type& p); 76 static bool Read(const Message* m, void** iter, param_type* r); 77 static void Log(const param_type& p, std::string* l); 78}; 79 80template <> 81struct ParamTraits<gfx::Rect> { 82 typedef gfx::Rect param_type; 83 static void Write(Message* m, const param_type& p); 84 static bool Read(const Message* m, void** iter, param_type* r); 85 static void Log(const param_type& p, std::string* l); 86}; 87 88template <> 89struct ParamTraits<gfx::Size> { 90 typedef gfx::Size param_type; 91 static void Write(Message* m, const param_type& p); 92 static bool Read(const Message* m, void** iter, param_type* r); 93 static void Log(const param_type& p, std::string* l); 94}; 95 96template <> 97struct ParamTraits<ContentSetting> { 98 typedef ContentSetting param_type; 99 static void Write(Message* m, const param_type& p); 100 static bool Read(const Message* m, void** iter, param_type* r); 101 static void Log(const param_type& p, std::string* l); 102}; 103 104template <> 105struct ParamTraits<ContentSettingsType> { 106 typedef ContentSettingsType param_type; 107 static void Write(Message* m, const param_type& p) { 108 WriteParam(m, static_cast<int>(p)); 109 } 110 static bool Read(const Message* m, void** iter, param_type* r) { 111 int value; 112 if (!ReadParam(m, iter, &value)) 113 return false; 114 if (value < 0 || value >= static_cast<int>(CONTENT_SETTINGS_NUM_TYPES)) 115 return false; 116 *r = static_cast<param_type>(value); 117 return true; 118 } 119 static void Log(const param_type& p, std::string* l) { 120 LogParam(static_cast<int>(p), l); 121 } 122}; 123 124template <> 125struct ParamTraits<ContentSettings> { 126 typedef ContentSettings param_type; 127 static void Write(Message* m, const param_type& p); 128 static bool Read(const Message* m, void** iter, param_type* r); 129 static void Log(const param_type& p, std::string* l); 130}; 131 132template <> 133struct ParamTraits<gfx::NativeWindow> { 134 typedef gfx::NativeWindow param_type; 135 static void Write(Message* m, const param_type& p) { 136#if defined(OS_WIN) 137 // HWNDs are always 32 bits on Windows, even on 64 bit systems. 138 m->WriteUInt32(reinterpret_cast<uint32>(p)); 139#else 140 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(p)); 141#endif 142 } 143 static bool Read(const Message* m, void** iter, param_type* r) { 144#if defined(OS_WIN) 145 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); 146#else 147 const char *data; 148 int data_size = 0; 149 bool result = m->ReadData(iter, &data, &data_size); 150 if (result && data_size == sizeof(gfx::NativeWindow)) { 151 memcpy(r, data, sizeof(gfx::NativeWindow)); 152 } else { 153 result = false; 154 NOTREACHED(); 155 } 156 return result; 157#endif 158 } 159 static void Log(const param_type& p, std::string* l) { 160 l->append("<gfx::NativeWindow>"); 161 } 162}; 163 164template <> 165struct ParamTraits<PageZoom::Function> { 166 typedef PageZoom::Function param_type; 167 static void Write(Message* m, const param_type& p) { 168 WriteParam(m, static_cast<int>(p)); 169 } 170 static bool Read(const Message* m, void** iter, param_type* r) { 171 int value; 172 if (!ReadParam(m, iter, &value)) 173 return false; 174 *r = static_cast<param_type>(value); 175 return true; 176 } 177 static void Log(const param_type& p, std::string* l) { 178 LogParam(static_cast<int>(p), l); 179 } 180}; 181 182 183template <> 184struct ParamTraits<WindowOpenDisposition> { 185 typedef WindowOpenDisposition param_type; 186 static void Write(Message* m, const param_type& p) { 187 WriteParam(m, static_cast<int>(p)); 188 } 189 static bool Read(const Message* m, void** iter, param_type* r) { 190 int value; 191 if (!ReadParam(m, iter, &value)) 192 return false; 193 *r = static_cast<param_type>(value); 194 return true; 195 } 196 static void Log(const param_type& p, std::string* l) { 197 LogParam(static_cast<int>(p), l); 198 } 199}; 200 201 202template <> 203struct ParamTraits<WebCursor> { 204 typedef WebCursor param_type; 205 static void Write(Message* m, const param_type& p) { 206 p.Serialize(m); 207 } 208 static bool Read(const Message* m, void** iter, param_type* r) { 209 return r->Deserialize(m, iter); 210 } 211 static void Log(const param_type& p, std::string* l) { 212 l->append("<WebCursor>"); 213 } 214}; 215 216 217template <> 218struct ParamTraits<WebApplicationInfo> { 219 typedef WebApplicationInfo param_type; 220 static void Write(Message* m, const param_type& p); 221 static bool Read(const Message* m, void** iter, param_type* r); 222 static void Log(const param_type& p, std::string* l); 223}; 224 225 226#if defined(OS_WIN) 227template<> 228struct ParamTraits<TransportDIB::Id> { 229 typedef TransportDIB::Id param_type; 230 static void Write(Message* m, const param_type& p) { 231 WriteParam(m, p.handle); 232 WriteParam(m, p.sequence_num); 233 } 234 static bool Read(const Message* m, void** iter, param_type* r) { 235 return (ReadParam(m, iter, &r->handle) && 236 ReadParam(m, iter, &r->sequence_num)); 237 } 238 static void Log(const param_type& p, std::string* l) { 239 l->append("TransportDIB("); 240 LogParam(p.handle, l); 241 l->append(", "); 242 LogParam(p.sequence_num, l); 243 l->append(")"); 244 } 245}; 246#endif 247 248template<> 249struct ParamTraits<ThumbnailScore> { 250 typedef ThumbnailScore param_type; 251 static void Write(Message* m, const param_type& p); 252 static bool Read(const Message* m, void** iter, param_type* r); 253 static void Log(const param_type& p, std::string* l); 254}; 255 256template <> 257struct ParamTraits<Geoposition> { 258 typedef Geoposition param_type; 259 static void Write(Message* m, const param_type& p); 260 static bool Read(const Message* m, void** iter, param_type* p); 261 static void Log(const param_type& p, std::string* l); 262}; 263 264template <> 265struct ParamTraits<webkit_glue::PasswordForm> { 266 typedef webkit_glue::PasswordForm param_type; 267 static void Write(Message* m, const param_type& p); 268 static bool Read(const Message* m, void** iter, param_type* p); 269 static void Log(const param_type& p, std::string* l); 270}; 271 272template <> 273struct ParamTraits<printing::PageRange> { 274 typedef printing::PageRange param_type; 275 static void Write(Message* m, const param_type& p); 276 static bool Read(const Message* m, void** iter, param_type* r); 277 static void Log(const param_type& p, std::string* l); 278}; 279 280template <> 281struct ParamTraits<printing::NativeMetafile> { 282 typedef printing::NativeMetafile param_type; 283 static void Write(Message* m, const param_type& p); 284 static bool Read(const Message* m, void** iter, param_type* r); 285 static void Log(const param_type& p, std::string* l); 286}; 287 288template <> 289struct ParamTraits<printing::PrinterCapsAndDefaults> { 290 typedef printing::PrinterCapsAndDefaults param_type; 291 static void Write(Message* m, const param_type& p); 292 static bool Read(const Message* m, void** iter, param_type* r); 293 static void Log(const param_type& p, std::string* l); 294}; 295 296} // namespace IPC 297 298#endif // CHROME_COMMON_COMMON_PARAM_TRAITS_H_ 299