Refactor android test results logging.
[chromium-blink-merge.git] / ppapi / proxy / serialized_structs.cc
blobfcd41a7b2122711f66e3967c9a653423e4bbff8e
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 #include "ppapi/proxy/serialized_structs.h"
7 #include "base/pickle.h"
8 #include "base/platform_file.h"
9 #include "base/shared_memory.h"
10 #include "build/build_config.h"
11 #include "ipc/ipc_platform_file.h"
12 #include "ppapi/c/dev/ppb_font_dev.h"
13 #include "ppapi/c/pp_file_info.h"
14 #include "ppapi/c/pp_rect.h"
15 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
16 #include "ppapi/shared_impl/var.h"
18 #if defined(OS_NACL)
19 #include <unistd.h>
20 #endif
22 namespace ppapi {
23 namespace proxy {
25 SerializedFontDescription::SerializedFontDescription()
26 : face(),
27 family(0),
28 size(0),
29 weight(0),
30 italic(PP_FALSE),
31 small_caps(PP_FALSE),
32 letter_spacing(0),
33 word_spacing(0) {
36 SerializedFontDescription::~SerializedFontDescription() {}
38 void SerializedFontDescription::SetFromPPFontDescription(
39 const PP_FontDescription_Dev& desc) {
40 StringVar* string_var = StringVar::FromPPVar(desc.face);
41 face = string_var ? string_var->value() : std::string();
43 family = desc.family;
44 size = desc.size;
45 weight = desc.weight;
46 italic = desc.italic;
47 small_caps = desc.small_caps;
48 letter_spacing = desc.letter_spacing;
49 word_spacing = desc.word_spacing;
52 void SerializedFontDescription::SetFromPPBrowserFontDescription(
53 const PP_BrowserFont_Trusted_Description& desc) {
54 StringVar* string_var = StringVar::FromPPVar(desc.face);
55 face = string_var ? string_var->value() : std::string();
57 family = desc.family;
58 size = desc.size;
59 weight = desc.weight;
60 italic = desc.italic;
61 small_caps = desc.small_caps;
62 letter_spacing = desc.letter_spacing;
63 word_spacing = desc.word_spacing;
66 void SerializedFontDescription::SetToPPFontDescription(
67 PP_FontDescription_Dev* desc) const {
68 desc->face = StringVar::StringToPPVar(face);
69 desc->family = static_cast<PP_FontFamily_Dev>(family);
70 desc->size = size;
71 desc->weight = static_cast<PP_FontWeight_Dev>(weight);
72 desc->italic = italic;
73 desc->small_caps = small_caps;
74 desc->letter_spacing = letter_spacing;
75 desc->word_spacing = word_spacing;
78 void SerializedFontDescription::SetToPPBrowserFontDescription(
79 PP_BrowserFont_Trusted_Description* desc) const {
80 desc->face = StringVar::StringToPPVar(face);
81 desc->family = static_cast<PP_BrowserFont_Trusted_Family>(family);
82 desc->size = size;
83 desc->weight = static_cast<PP_BrowserFont_Trusted_Weight>(weight);
84 desc->italic = italic;
85 desc->small_caps = small_caps;
86 desc->letter_spacing = letter_spacing;
87 desc->word_spacing = word_spacing;
90 PPBFlash_DrawGlyphs_Params::PPBFlash_DrawGlyphs_Params()
91 : instance(0),
92 font_desc(),
93 color(0) {
94 clip.point.x = 0;
95 clip.point.y = 0;
96 clip.size.height = 0;
97 clip.size.width = 0;
98 position.x = 0;
99 position.y = 0;
100 allow_subpixel_aa = PP_FALSE;
103 PPBFlash_DrawGlyphs_Params::~PPBFlash_DrawGlyphs_Params() {}
105 SerializedHandle::SerializedHandle()
106 : type_(INVALID),
107 shm_handle_(base::SharedMemory::NULLHandle()),
108 size_(0),
109 descriptor_(IPC::InvalidPlatformFileForTransit()) {
112 SerializedHandle::SerializedHandle(Type type_param)
113 : type_(type_param),
114 shm_handle_(base::SharedMemory::NULLHandle()),
115 size_(0),
116 descriptor_(IPC::InvalidPlatformFileForTransit()) {
119 SerializedHandle::SerializedHandle(const base::SharedMemoryHandle& handle,
120 uint32_t size)
121 : type_(SHARED_MEMORY),
122 shm_handle_(handle),
123 size_(size),
124 descriptor_(IPC::InvalidPlatformFileForTransit()) {
127 SerializedHandle::SerializedHandle(
128 Type type,
129 const IPC::PlatformFileForTransit& socket_descriptor)
130 : type_(type),
131 shm_handle_(base::SharedMemory::NULLHandle()),
132 size_(0),
133 descriptor_(socket_descriptor) {
136 bool SerializedHandle::IsHandleValid() const {
137 switch (type_) {
138 case SHARED_MEMORY:
139 return base::SharedMemory::IsHandleValid(shm_handle_);
140 case SOCKET:
141 case CHANNEL_HANDLE:
142 case FILE:
143 return !(IPC::InvalidPlatformFileForTransit() == descriptor_);
144 case INVALID:
145 return false;
146 // No default so the compiler will warn us if a new type is added.
148 return false;
151 void SerializedHandle::Close() {
152 if (IsHandleValid()) {
153 switch (type_) {
154 case INVALID:
155 NOTREACHED();
156 break;
157 case SHARED_MEMORY:
158 base::SharedMemory::CloseHandle(shm_handle_);
159 break;
160 case SOCKET:
161 case CHANNEL_HANDLE:
162 case FILE:
163 base::PlatformFile file =
164 IPC::PlatformFileForTransitToPlatformFile(descriptor_);
165 #if !defined(OS_NACL)
166 base::ClosePlatformFile(file);
167 #else
168 close(file);
169 #endif
170 break;
171 // No default so the compiler will warn us if a new type is added.
174 *this = SerializedHandle();
177 // static
178 bool SerializedHandle::WriteHeader(const Header& hdr, Pickle* pickle) {
179 if (!pickle->WriteInt(hdr.type))
180 return false;
181 if (hdr.type == SHARED_MEMORY) {
182 if (!pickle->WriteUInt32(hdr.size))
183 return false;
185 return true;
188 // static
189 bool SerializedHandle::ReadHeader(PickleIterator* iter, Header* hdr) {
190 *hdr = Header(INVALID, 0);
191 int type = 0;
192 if (!iter->ReadInt(&type))
193 return false;
194 bool valid_type = false;
195 switch (type) {
196 case SHARED_MEMORY: {
197 uint32_t size = 0;
198 if (!iter->ReadUInt32(&size))
199 return false;
200 hdr->size = size;
201 valid_type = true;
202 break;
204 case SOCKET:
205 case CHANNEL_HANDLE:
206 case FILE:
207 case INVALID:
208 valid_type = true;
209 break;
210 // No default so the compiler will warn us if a new type is added.
212 if (valid_type)
213 hdr->type = Type(type);
214 return valid_type;
217 } // namespace proxy
218 } // namespace ppapi