OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 #include "chrome/common/common_param_traits.h" |
| 6 |
| 7 #include "base/gfx/rect.h" |
| 8 #include "googleurl/src/gurl.h" |
| 9 #ifndef EXCLUDE_SKIA_DEPENDENCIES |
| 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #endif |
| 12 #include "webkit/glue/dom_operations.h" |
| 13 |
| 14 namespace IPC { |
| 15 |
| 16 #ifndef EXCLUDE_SKIA_DEPENDENCIES |
| 17 |
| 18 namespace { |
| 19 |
| 20 struct SkBitmap_Data { |
| 21 // The configuration for the bitmap (bits per pixel, etc). |
| 22 SkBitmap::Config fConfig; |
| 23 |
| 24 // The width of the bitmap in pixels. |
| 25 uint32 fWidth; |
| 26 |
| 27 // The height of the bitmap in pixels. |
| 28 uint32 fHeight; |
| 29 |
| 30 // The number of bytes between subsequent rows of the bitmap. |
| 31 uint32 fRowBytes; |
| 32 |
| 33 void InitSkBitmapDataForTransfer(const SkBitmap& bitmap) { |
| 34 fConfig = bitmap.config(); |
| 35 fWidth = bitmap.width(); |
| 36 fHeight = bitmap.height(); |
| 37 fRowBytes = bitmap.rowBytes(); |
| 38 } |
| 39 |
| 40 // Returns whether |bitmap| successfully initialized. |
| 41 bool InitSkBitmapFromData(SkBitmap* bitmap, const char* pixels, |
| 42 size_t total_pixels) const { |
| 43 if (total_pixels) { |
| 44 bitmap->setConfig(fConfig, fWidth, fHeight, fRowBytes); |
| 45 if (!bitmap->allocPixels()) |
| 46 return false; |
| 47 if (total_pixels > bitmap->getSize()) |
| 48 return false; |
| 49 memcpy(bitmap->getPixels(), pixels, total_pixels); |
| 50 } |
| 51 return true; |
| 52 } |
| 53 }; |
| 54 |
| 55 } // namespace |
| 56 |
| 57 |
| 58 void ParamTraits<SkBitmap>::Write(Message* m, const SkBitmap& p) { |
| 59 size_t fixed_size = sizeof(SkBitmap_Data); |
| 60 SkBitmap_Data bmp_data; |
| 61 bmp_data.InitSkBitmapDataForTransfer(p); |
| 62 m->WriteData(reinterpret_cast<const char*>(&bmp_data), |
| 63 static_cast<int>(fixed_size)); |
| 64 size_t pixel_size = p.getSize(); |
| 65 SkAutoLockPixels p_lock(p); |
| 66 m->WriteData(reinterpret_cast<const char*>(p.getPixels()), |
| 67 static_cast<int>(pixel_size)); |
| 68 } |
| 69 |
| 70 bool ParamTraits<SkBitmap>::Read(const Message* m, void** iter, SkBitmap* r) { |
| 71 const char* fixed_data; |
| 72 int fixed_data_size = 0; |
| 73 if (!m->ReadData(iter, &fixed_data, &fixed_data_size) || |
| 74 (fixed_data_size <= 0)) { |
| 75 NOTREACHED(); |
| 76 return false; |
| 77 } |
| 78 if (fixed_data_size != sizeof(SkBitmap_Data)) |
| 79 return false; // Message is malformed. |
| 80 |
| 81 const char* variable_data; |
| 82 int variable_data_size = 0; |
| 83 if (!m->ReadData(iter, &variable_data, &variable_data_size) || |
| 84 (variable_data_size < 0)) { |
| 85 NOTREACHED(); |
| 86 return false; |
| 87 } |
| 88 const SkBitmap_Data* bmp_data = |
| 89 reinterpret_cast<const SkBitmap_Data*>(fixed_data); |
| 90 return bmp_data->InitSkBitmapFromData(r, variable_data, variable_data_size); |
| 91 } |
| 92 |
| 93 void ParamTraits<SkBitmap>::Log(const SkBitmap& p, std::wstring* l) { |
| 94 l->append(StringPrintf(L"<SkBitmap>")); |
| 95 } |
| 96 |
| 97 #endif // EXCLUDE_SKIA_DEPENDENCIES |
| 98 |
| 99 void ParamTraits<GURL>::Write(Message* m, const GURL& p) { |
| 100 m->WriteString(p.possibly_invalid_spec()); |
| 101 // TODO(brettw) bug 684583: Add encoding for query params. |
| 102 } |
| 103 |
| 104 bool ParamTraits<GURL>::Read(const Message* m, void** iter, GURL* p) { |
| 105 std::string s; |
| 106 if (!m->ReadString(iter, &s)) { |
| 107 *p = GURL(); |
| 108 return false; |
| 109 } |
| 110 *p = GURL(s); |
| 111 return true; |
| 112 } |
| 113 |
| 114 void ParamTraits<GURL>::Log(const GURL& p, std::wstring* l) { |
| 115 l->append(UTF8ToWide(p.spec())); |
| 116 } |
| 117 |
| 118 void ParamTraits<gfx::Point>::Write(Message* m, const gfx::Point& p) { |
| 119 m->WriteInt(p.x()); |
| 120 m->WriteInt(p.y()); |
| 121 } |
| 122 |
| 123 bool ParamTraits<gfx::Point>::Read(const Message* m, void** iter, |
| 124 gfx::Point* r) { |
| 125 int x, y; |
| 126 if (!m->ReadInt(iter, &x) || |
| 127 !m->ReadInt(iter, &y)) |
| 128 return false; |
| 129 r->set_x(x); |
| 130 r->set_y(y); |
| 131 return true; |
| 132 } |
| 133 |
| 134 void ParamTraits<gfx::Point>::Log(const gfx::Point& p, std::wstring* l) { |
| 135 l->append(StringPrintf(L"(%d, %d)", p.x(), p.y())); |
| 136 } |
| 137 |
| 138 |
| 139 void ParamTraits<gfx::Rect>::Write(Message* m, const gfx::Rect& p) { |
| 140 m->WriteInt(p.x()); |
| 141 m->WriteInt(p.y()); |
| 142 m->WriteInt(p.width()); |
| 143 m->WriteInt(p.height()); |
| 144 } |
| 145 |
| 146 bool ParamTraits<gfx::Rect>::Read(const Message* m, void** iter, gfx::Rect* r) { |
| 147 int x, y, w, h; |
| 148 if (!m->ReadInt(iter, &x) || |
| 149 !m->ReadInt(iter, &y) || |
| 150 !m->ReadInt(iter, &w) || |
| 151 !m->ReadInt(iter, &h)) |
| 152 return false; |
| 153 r->set_x(x); |
| 154 r->set_y(y); |
| 155 r->set_width(w); |
| 156 r->set_height(h); |
| 157 return true; |
| 158 } |
| 159 |
| 160 void ParamTraits<gfx::Rect>::Log(const gfx::Rect& p, std::wstring* l) { |
| 161 l->append(StringPrintf(L"(%d, %d, %d, %d)", p.x(), p.y(), |
| 162 p.width(), p.height())); |
| 163 } |
| 164 |
| 165 |
| 166 void ParamTraits<gfx::Size>::Write(Message* m, const gfx::Size& p) { |
| 167 m->WriteInt(p.width()); |
| 168 m->WriteInt(p.height()); |
| 169 } |
| 170 |
| 171 bool ParamTraits<gfx::Size>::Read(const Message* m, void** iter, gfx::Size* r) { |
| 172 int w, h; |
| 173 if (!m->ReadInt(iter, &w) || |
| 174 !m->ReadInt(iter, &h)) |
| 175 return false; |
| 176 r->set_width(w); |
| 177 r->set_height(h); |
| 178 return true; |
| 179 } |
| 180 |
| 181 void ParamTraits<gfx::Size>::Log(const gfx::Size& p, std::wstring* l) { |
| 182 l->append(StringPrintf(L"(%d, %d)", p.width(), p.height())); |
| 183 } |
| 184 |
| 185 void ParamTraits<webkit_glue::WebApplicationInfo>::Write( |
| 186 Message* m, const webkit_glue::WebApplicationInfo& p) { |
| 187 WriteParam(m, p.title); |
| 188 WriteParam(m, p.description); |
| 189 WriteParam(m, p.app_url); |
| 190 WriteParam(m, p.icons.size()); |
| 191 for (size_t i = 0; i < p.icons.size(); ++i) { |
| 192 WriteParam(m, p.icons[i].url); |
| 193 WriteParam(m, p.icons[i].width); |
| 194 WriteParam(m, p.icons[i].height); |
| 195 } |
| 196 } |
| 197 |
| 198 bool ParamTraits<webkit_glue::WebApplicationInfo>::Read( |
| 199 const Message* m, void** iter, webkit_glue::WebApplicationInfo* r) { |
| 200 size_t icon_count; |
| 201 bool result = |
| 202 ReadParam(m, iter, &r->title) && |
| 203 ReadParam(m, iter, &r->description) && |
| 204 ReadParam(m, iter, &r->app_url) && |
| 205 ReadParam(m, iter, &icon_count); |
| 206 if (!result) |
| 207 return false; |
| 208 for (size_t i = 0; i < icon_count && result; ++i) { |
| 209 param_type::IconInfo icon_info; |
| 210 result = |
| 211 ReadParam(m, iter, &icon_info.url) && |
| 212 ReadParam(m, iter, &icon_info.width) && |
| 213 ReadParam(m, iter, &icon_info.height); |
| 214 r->icons.push_back(icon_info); |
| 215 } |
| 216 return result; |
| 217 } |
| 218 |
| 219 void ParamTraits<webkit_glue::WebApplicationInfo>::Log( |
| 220 const webkit_glue::WebApplicationInfo& p, std::wstring* l) { |
| 221 l->append(L"<WebApplicationInfo>"); |
| 222 } |
| 223 |
| 224 } // namespace IPC |
| 225 |
OLD | NEW |