OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/plugins/pepper_var.h" | 5 #include "webkit/glue/plugins/pepper_var.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "third_party/ppapi/c/pp_var.h" | 10 #include "third_party/ppapi/c/pp_var.h" |
11 #include "third_party/ppapi/c/ppb_var.h" | 11 #include "third_party/ppapi/c/ppb_var.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" |
13 #include "webkit/glue/plugins/pepper_string.h" | 13 #include "webkit/glue/plugins/pepper_string.h" |
| 14 #include "v8/include/v8.h" |
| 15 |
| 16 // Uncomment to enable catching JS exceptions |
| 17 // #define HAVE_WEBBINDINGS_EXCEPTION_HANDLER 1 |
14 | 18 |
15 using WebKit::WebBindings; | 19 using WebKit::WebBindings; |
16 | 20 |
17 namespace pepper { | 21 namespace pepper { |
18 | 22 |
19 namespace { | 23 namespace { |
20 | 24 |
| 25 PP_Var VarFromUtf8(const char* data, uint32_t len); |
| 26 |
| 27 // --------------------------------------------------------------------------- |
| 28 // Exceptions |
| 29 |
| 30 class TryCatch { |
| 31 public: |
| 32 TryCatch(PP_Var* exception) : exception_(exception) { |
| 33 #ifdef HAVE_WEBBINDINGS_EXCEPTION_HANDLER |
| 34 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); |
| 35 #endif |
| 36 } |
| 37 |
| 38 ~TryCatch() { |
| 39 #ifdef HAVE_WEBBINDINGS_EXCEPTION_HANDLER |
| 40 WebBindings::popExceptionHandler(); |
| 41 #endif |
| 42 } |
| 43 |
| 44 bool HasException() const { |
| 45 return exception_ && exception_->type != PP_VarType_Void; |
| 46 } |
| 47 |
| 48 void SetException(const char* message) { |
| 49 DCHECK(!HasException()); |
| 50 if (exception_) |
| 51 *exception_ = VarFromUtf8(message, strlen(message)); |
| 52 } |
| 53 |
| 54 private: |
| 55 static void Catch(void* self, const NPUTF8* message) { |
| 56 static_cast<TryCatch*>(self)->SetException(message); |
| 57 } |
| 58 |
| 59 // May be null if the consumer isn't interesting in catching exceptions. |
| 60 PP_Var* exception_; |
| 61 }; |
| 62 |
| 63 const char kInvalidObjectException[] = "Error: Invalid object"; |
| 64 const char kInvalidPropertyException[] = "Error: Invalid property"; |
| 65 const char kUnableToGetPropertyException[] = "Error: Unable to get property"; |
| 66 const char kUnableToSetPropertyException[] = "Error: Unable to set property"; |
| 67 const char kUnableToRemovePropertyException[] = |
| 68 "Error: Unable to remove property"; |
| 69 const char kUnableToGetAllPropertiesException[] = |
| 70 "Error: Unable to get all properties"; |
| 71 const char kUnableToCallMethodException[] = "Error: Unable to call method"; |
| 72 const char kUnableToConstructException[] = "Error: Unable to construct"; |
| 73 |
| 74 // --------------------------------------------------------------------------- |
| 75 // Utilities |
| 76 |
21 String* GetStringUnchecked(PP_Var var) { | 77 String* GetStringUnchecked(PP_Var var) { |
22 return reinterpret_cast<String*>(var.value.as_id); | 78 return reinterpret_cast<String*>(var.value.as_id); |
23 } | 79 } |
24 | 80 |
25 NPObject* GetNPObjectUnchecked(PP_Var var) { | 81 NPObject* GetNPObjectUnchecked(PP_Var var) { |
26 return reinterpret_cast<NPObject*>(var.value.as_id); | 82 return reinterpret_cast<NPObject*>(var.value.as_id); |
27 } | 83 } |
28 | 84 |
29 NPObject* GetNPObject(PP_Var var) { | 85 NPObject* GetNPObject(PP_Var var) { |
30 if (var.type != PP_VarType_Object) | 86 if (var.type != PP_VarType_Object) |
31 return NULL; | 87 return NULL; |
32 return GetNPObjectUnchecked(var); | 88 return GetNPObjectUnchecked(var); |
33 } | 89 } |
34 | 90 |
| 91 // Returns a PP_Var that corresponds to the given NPVariant. The contents of |
| 92 // the NPVariant will be copied unless the NPVariant corresponds to an object. |
| 93 PP_Var NPVariantToPPVar(NPVariant* variant) { |
| 94 switch (variant->type) { |
| 95 case NPVariantType_Void: |
| 96 return PP_MakeVoid(); |
| 97 case NPVariantType_Null: |
| 98 return PP_MakeNull(); |
| 99 case NPVariantType_Bool: |
| 100 return PP_MakeBool(NPVARIANT_TO_BOOLEAN(*variant)); |
| 101 case NPVariantType_Int32: |
| 102 return PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); |
| 103 case NPVariantType_Double: |
| 104 return PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); |
| 105 case NPVariantType_String: |
| 106 return VarFromUtf8(NPVARIANT_TO_STRING(*variant).UTF8Characters, |
| 107 NPVARIANT_TO_STRING(*variant).UTF8Length); |
| 108 case NPVariantType_Object: |
| 109 return NPObjectToPPVar(NPVARIANT_TO_OBJECT(*variant)); |
| 110 } |
| 111 NOTREACHED(); |
| 112 return PP_MakeVoid(); |
| 113 } |
| 114 |
| 115 // Returns a NPVariant that corresponds to the given PP_Var. The contents of |
| 116 // the PP_Var will be copied unless the PP_Var corresponds to an object. |
| 117 NPVariant PPVarToNPVariant(PP_Var var) { |
| 118 NPVariant ret; |
| 119 switch (var.type) { |
| 120 case PP_VarType_Void: |
| 121 VOID_TO_NPVARIANT(ret); |
| 122 break; |
| 123 case PP_VarType_Null: |
| 124 NULL_TO_NPVARIANT(ret); |
| 125 break; |
| 126 case PP_VarType_Bool: |
| 127 BOOLEAN_TO_NPVARIANT(var.value.as_bool, ret); |
| 128 break; |
| 129 case PP_VarType_Int32: |
| 130 INT32_TO_NPVARIANT(var.value.as_int, ret); |
| 131 break; |
| 132 case PP_VarType_Double: |
| 133 DOUBLE_TO_NPVARIANT(var.value.as_double, ret); |
| 134 break; |
| 135 case PP_VarType_String: { |
| 136 const std::string& value = GetStringUnchecked(var)->value(); |
| 137 STRINGN_TO_NPVARIANT(base::strdup(value.c_str()), value.size(), ret); |
| 138 break; |
| 139 } |
| 140 case PP_VarType_Object: { |
| 141 NPObject* object = GetNPObjectUnchecked(var); |
| 142 OBJECT_TO_NPVARIANT(WebBindings::retainObject(object), ret); |
| 143 break; |
| 144 } |
| 145 } |
| 146 return ret; |
| 147 } |
| 148 |
| 149 // Returns a NPVariant that corresponds to the given PP_Var. The contents of |
| 150 // the PP_Var will NOT be copied, so you need to ensure that the PP_Var remains |
| 151 // valid while the resultant NPVariant is in use. |
| 152 NPVariant PPVarToNPVariantNoCopy(PP_Var var) { |
| 153 NPVariant ret; |
| 154 switch (var.type) { |
| 155 case PP_VarType_Void: |
| 156 VOID_TO_NPVARIANT(ret); |
| 157 break; |
| 158 case PP_VarType_Null: |
| 159 NULL_TO_NPVARIANT(ret); |
| 160 break; |
| 161 case PP_VarType_Bool: |
| 162 BOOLEAN_TO_NPVARIANT(var.value.as_bool, ret); |
| 163 break; |
| 164 case PP_VarType_Int32: |
| 165 INT32_TO_NPVARIANT(var.value.as_int, ret); |
| 166 break; |
| 167 case PP_VarType_Double: |
| 168 DOUBLE_TO_NPVARIANT(var.value.as_double, ret); |
| 169 break; |
| 170 case PP_VarType_String: { |
| 171 const std::string& value = GetStringUnchecked(var)->value(); |
| 172 STRINGN_TO_NPVARIANT(value.c_str(), value.size(), ret); |
| 173 break; |
| 174 } |
| 175 case PP_VarType_Object: { |
| 176 OBJECT_TO_NPVARIANT(GetNPObjectUnchecked(var), ret); |
| 177 break; |
| 178 } |
| 179 } |
| 180 return ret; |
| 181 } |
| 182 |
| 183 // Returns a NPIdentifier that corresponds to the given PP_Var. The contents |
| 184 // of the PP_Var will be copied. Returns NULL if the given PP_Var is not a a |
| 185 // string or integer type. |
| 186 NPIdentifier PPVarToNPIdentifier(PP_Var var) { |
| 187 switch (var.type) { |
| 188 case PP_VarType_String: |
| 189 return WebBindings::getStringIdentifier( |
| 190 GetStringUnchecked(var)->value().c_str()); |
| 191 case PP_VarType_Int32: |
| 192 return WebBindings::getIntIdentifier(var.value.as_int); |
| 193 default: |
| 194 return NULL; |
| 195 } |
| 196 } |
| 197 |
| 198 // --------------------------------------------------------------------------- |
| 199 // PPB_Var methods |
| 200 |
35 void AddRef(PP_Var var) { | 201 void AddRef(PP_Var var) { |
36 if (var.type == PP_VarType_String) { | 202 if (var.type == PP_VarType_String) { |
37 GetStringUnchecked(var)->AddRef(); | 203 GetStringUnchecked(var)->AddRef(); |
38 } else if (var.type == PP_VarType_Object) { | 204 } else if (var.type == PP_VarType_Object) { |
39 // TODO(darin): Add thread safety check | 205 // TODO(darin): Add thread safety check |
40 WebBindings::retainObject(GetNPObjectUnchecked(var)); | 206 WebBindings::retainObject(GetNPObjectUnchecked(var)); |
41 } | 207 } |
42 } | 208 } |
43 | 209 |
44 void Release(PP_Var var) { | 210 void Release(PP_Var var) { |
(...skipping 22 matching lines...) Expand all Loading... |
67 const std::string& str = GetStringUnchecked(var)->value(); | 233 const std::string& str = GetStringUnchecked(var)->value(); |
68 *len = static_cast<uint32_t>(str.size()); | 234 *len = static_cast<uint32_t>(str.size()); |
69 if (str.empty()) | 235 if (str.empty()) |
70 return ""; // Don't return NULL on success. | 236 return ""; // Don't return NULL on success. |
71 return str.data(); | 237 return str.data(); |
72 } | 238 } |
73 | 239 |
74 bool HasProperty(PP_Var var, | 240 bool HasProperty(PP_Var var, |
75 PP_Var name, | 241 PP_Var name, |
76 PP_Var* exception) { | 242 PP_Var* exception) { |
77 if (exception && exception->type != PP_VarType_Void) | 243 TryCatch try_catch(exception); |
| 244 if (try_catch.HasException()) |
78 return false; | 245 return false; |
79 | 246 |
80 NPObject* object = GetNPObject(var); | 247 NPObject* object = GetNPObject(var); |
81 if (!object) { | 248 if (!object) { |
82 // TODO(darin): raise exception | 249 try_catch.SetException(kInvalidObjectException); |
83 return false; | 250 return false; |
84 } | 251 } |
85 | 252 |
86 NPIdentifier identifier = PPVarToNPIdentifier(name); | 253 NPIdentifier identifier = PPVarToNPIdentifier(name); |
87 if (!identifier) { | 254 if (!identifier) { |
88 // TODO(darin): raise exception | 255 try_catch.SetException(kInvalidPropertyException); |
89 return false; | 256 return false; |
90 } | 257 } |
91 | 258 |
92 return WebBindings::hasProperty(NULL, object, identifier); | 259 return WebBindings::hasProperty(NULL, object, identifier); |
93 } | 260 } |
94 | 261 |
95 PP_Var GetProperty(PP_Var var, | 262 PP_Var GetProperty(PP_Var var, |
96 PP_Var name, | 263 PP_Var name, |
97 PP_Var* exception) { | 264 PP_Var* exception) { |
98 if (exception && exception->type != PP_VarType_Void) | 265 TryCatch try_catch(exception); |
| 266 if (try_catch.HasException()) |
99 return PP_MakeVoid(); | 267 return PP_MakeVoid(); |
100 | 268 |
101 NPObject* object = GetNPObject(var); | 269 NPObject* object = GetNPObject(var); |
102 if (!object) { | 270 if (!object) { |
103 // TODO(darin): raise exception | 271 try_catch.SetException(kInvalidObjectException); |
104 return PP_MakeVoid(); | 272 return PP_MakeVoid(); |
105 } | 273 } |
106 | 274 |
107 NPIdentifier identifier = PPVarToNPIdentifier(name); | 275 NPIdentifier identifier = PPVarToNPIdentifier(name); |
108 if (!identifier) { | 276 if (!identifier) { |
109 // TODO(darin): raise exception | 277 try_catch.SetException(kInvalidPropertyException); |
110 return PP_MakeVoid(); | 278 return PP_MakeVoid(); |
111 } | 279 } |
112 | 280 |
113 NPVariant result; | 281 NPVariant result; |
114 if (!WebBindings::getProperty(NULL, object, identifier, &result)) { | 282 if (!WebBindings::getProperty(NULL, object, identifier, &result)) { |
115 // TODO(darin): raise exception | 283 // An exception may have been raised. |
| 284 if (!try_catch.HasException()) |
| 285 try_catch.SetException(kUnableToGetPropertyException); |
116 return PP_MakeVoid(); | 286 return PP_MakeVoid(); |
117 } | 287 } |
118 | 288 |
119 PP_Var ret = NPVariantToPPVar(&result); | 289 PP_Var ret = NPVariantToPPVar(&result); |
120 WebBindings::releaseVariantValue(&result); | 290 WebBindings::releaseVariantValue(&result); |
121 return ret; | 291 return ret; |
122 } | 292 } |
123 | 293 |
124 void GetAllPropertyNames(PP_Var var, | 294 void GetAllPropertyNames(PP_Var var, |
125 uint32_t* property_count, | 295 uint32_t* property_count, |
126 PP_Var** properties, | 296 PP_Var** properties, |
127 PP_Var* exception) { | 297 PP_Var* exception) { |
128 if (exception && exception->type != PP_VarType_Void) | 298 TryCatch try_catch(exception); |
| 299 if (try_catch.HasException()) |
129 return; | 300 return; |
130 | 301 |
131 // TODO(darin) | 302 // TODO(darin): Implement this method! |
| 303 try_catch.SetException(kUnableToGetAllPropertiesException); |
132 } | 304 } |
133 | 305 |
134 void SetProperty(PP_Var var, | 306 void SetProperty(PP_Var var, |
135 PP_Var name, | 307 PP_Var name, |
136 PP_Var value, | 308 PP_Var value, |
137 PP_Var* exception) { | 309 PP_Var* exception) { |
138 if (exception && exception->type != PP_VarType_Void) | 310 TryCatch try_catch(exception); |
| 311 if (try_catch.HasException()) |
139 return; | 312 return; |
140 | 313 |
141 NPObject* object = GetNPObject(var); | 314 NPObject* object = GetNPObject(var); |
142 if (!object) { | 315 if (!object) { |
143 // TODO(darin): raise exception | 316 try_catch.SetException(kInvalidObjectException); |
144 return; | 317 return; |
145 } | 318 } |
146 | 319 |
147 NPIdentifier identifier = PPVarToNPIdentifier(name); | 320 NPIdentifier identifier = PPVarToNPIdentifier(name); |
148 if (!identifier) { | 321 if (!identifier) { |
149 // TODO(darin): raise exception | 322 try_catch.SetException(kInvalidPropertyException); |
150 return; | 323 return; |
151 } | 324 } |
152 | 325 |
153 NPVariant variant = PPVarToNPVariantNoCopy(value); | 326 NPVariant variant = PPVarToNPVariantNoCopy(value); |
154 WebBindings::setProperty(NULL, object, identifier, &variant); | 327 if (!WebBindings::setProperty(NULL, object, identifier, &variant)) { |
| 328 if (!try_catch.HasException()) |
| 329 try_catch.SetException(kUnableToSetPropertyException); |
| 330 } |
155 } | 331 } |
156 | 332 |
157 void RemoveProperty(PP_Var var, | 333 void RemoveProperty(PP_Var var, |
158 PP_Var name, | 334 PP_Var name, |
159 PP_Var* exception) { | 335 PP_Var* exception) { |
160 if (exception && exception->type != PP_VarType_Void) | 336 TryCatch try_catch(exception); |
| 337 if (try_catch.HasException()) |
161 return; | 338 return; |
162 | 339 |
163 NPObject* object = GetNPObject(var); | 340 NPObject* object = GetNPObject(var); |
164 if (!object) { | 341 if (!object) { |
165 // TODO(darin): raise exception | 342 try_catch.SetException(kInvalidObjectException); |
166 return; | 343 return; |
167 } | 344 } |
168 | 345 |
169 NPIdentifier identifier = PPVarToNPIdentifier(name); | 346 NPIdentifier identifier = PPVarToNPIdentifier(name); |
170 if (!identifier) { | 347 if (!identifier) { |
171 // TODO(darin): raise exception | 348 try_catch.SetException(kInvalidPropertyException); |
172 return; | 349 return; |
173 } | 350 } |
174 | 351 |
175 WebBindings::removeProperty(NULL, object, identifier); | 352 if (!WebBindings::removeProperty(NULL, object, identifier)) { |
| 353 if (!try_catch.HasException()) |
| 354 try_catch.SetException(kUnableToRemovePropertyException); |
| 355 } |
176 } | 356 } |
177 | 357 |
178 PP_Var Call(PP_Var var, | 358 PP_Var Call(PP_Var var, |
179 PP_Var method_name, | 359 PP_Var method_name, |
180 int32_t argc, | 360 int32_t argc, |
181 PP_Var* argv, | 361 PP_Var* argv, |
182 PP_Var* exception) { | 362 PP_Var* exception) { |
183 if (exception && exception->type != PP_VarType_Void) | 363 TryCatch try_catch(exception); |
| 364 if (try_catch.HasException()) |
184 return PP_MakeVoid(); | 365 return PP_MakeVoid(); |
185 | 366 |
186 NPObject* object = GetNPObject(var); | 367 NPObject* object = GetNPObject(var); |
187 if (!object) { | 368 if (!object) { |
188 // TODO(darin): raise exception | 369 try_catch.SetException(kInvalidObjectException); |
189 return PP_MakeVoid(); | 370 return PP_MakeVoid(); |
190 } | 371 } |
191 | 372 |
192 NPIdentifier identifier; | 373 NPIdentifier identifier; |
193 if (method_name.type == PP_VarType_Void) { | 374 if (method_name.type == PP_VarType_Void) { |
194 identifier = NULL; | 375 identifier = NULL; |
195 } else { | 376 } else { |
196 identifier = PPVarToNPIdentifier(method_name); | 377 identifier = PPVarToNPIdentifier(method_name); |
197 if (!identifier) { | 378 if (!identifier) { |
198 // TODO(darin): raise exception | 379 try_catch.SetException(kInvalidPropertyException); |
199 return PP_MakeVoid(); | 380 return PP_MakeVoid(); |
200 } | 381 } |
201 } | 382 } |
202 | 383 |
203 scoped_array<NPVariant> args; | 384 scoped_array<NPVariant> args; |
204 if (argc) { | 385 if (argc) { |
205 args.reset(new NPVariant[argc]); | 386 args.reset(new NPVariant[argc]); |
206 for (int32_t i = 0; i < argc; ++i) | 387 for (int32_t i = 0; i < argc; ++i) |
207 args[i] = PPVarToNPVariantNoCopy(argv[i]); | 388 args[i] = PPVarToNPVariantNoCopy(argv[i]); |
208 } | 389 } |
209 | 390 |
210 bool ok; | 391 bool ok; |
211 | 392 |
212 NPVariant result; | 393 NPVariant result; |
213 if (identifier) { | 394 if (identifier) { |
214 ok = WebBindings::invoke(NULL, object, identifier, args.get(), argc, | 395 ok = WebBindings::invoke(NULL, object, identifier, args.get(), argc, |
215 &result); | 396 &result); |
216 } else { | 397 } else { |
217 ok = WebBindings::invokeDefault(NULL, object, args.get(), argc, &result); | 398 ok = WebBindings::invokeDefault(NULL, object, args.get(), argc, &result); |
218 } | 399 } |
219 | 400 |
220 if (!ok) { | 401 if (!ok) { |
221 // TODO(darin): raise exception | 402 // An exception may have been raised. |
| 403 if (!try_catch.HasException()) |
| 404 try_catch.SetException(kUnableToCallMethodException); |
222 return PP_MakeVoid(); | 405 return PP_MakeVoid(); |
223 } | 406 } |
224 | 407 |
225 PP_Var ret = NPVariantToPPVar(&result); | 408 PP_Var ret = NPVariantToPPVar(&result); |
226 WebBindings::releaseVariantValue(&result); | 409 WebBindings::releaseVariantValue(&result); |
227 return ret; | 410 return ret; |
228 } | 411 } |
229 | 412 |
230 PP_Var Construct(PP_Var var, | 413 PP_Var Construct(PP_Var var, |
231 int32_t argc, | 414 int32_t argc, |
232 PP_Var* argv, | 415 PP_Var* argv, |
233 PP_Var* exception) { | 416 PP_Var* exception) { |
234 if (exception && exception->type != PP_VarType_Void) | 417 TryCatch try_catch(exception); |
| 418 if (try_catch.HasException()) |
235 return PP_MakeVoid(); | 419 return PP_MakeVoid(); |
236 | 420 |
237 NPObject* object = GetNPObject(var); | 421 NPObject* object = GetNPObject(var); |
238 if (!object) { | 422 if (!object) { |
239 // TODO(darin): raise exception | 423 try_catch.SetException(kInvalidObjectException); |
240 return PP_MakeVoid(); | 424 return PP_MakeVoid(); |
241 } | 425 } |
242 | 426 |
243 scoped_array<NPVariant> args; | 427 scoped_array<NPVariant> args; |
244 if (argc) { | 428 if (argc) { |
245 args.reset(new NPVariant[argc]); | 429 args.reset(new NPVariant[argc]); |
246 for (int32_t i = 0; i < argc; ++i) | 430 for (int32_t i = 0; i < argc; ++i) |
247 args[i] = PPVarToNPVariantNoCopy(argv[i]); | 431 args[i] = PPVarToNPVariantNoCopy(argv[i]); |
248 } | 432 } |
249 | 433 |
250 NPVariant result; | 434 NPVariant result; |
251 if (!WebBindings::construct(NULL, object, args.get(), argc, &result)) { | 435 if (!WebBindings::construct(NULL, object, args.get(), argc, &result)) { |
252 // TODO(darin): raise exception | 436 // An exception may have been raised. |
| 437 if (!try_catch.HasException()) |
| 438 try_catch.SetException(kUnableToConstructException); |
253 return PP_MakeVoid(); | 439 return PP_MakeVoid(); |
254 } | 440 } |
255 | 441 |
256 PP_Var ret = NPVariantToPPVar(&result); | 442 PP_Var ret = NPVariantToPPVar(&result); |
257 WebBindings::releaseVariantValue(&result); | 443 WebBindings::releaseVariantValue(&result); |
258 return ret; | 444 return ret; |
259 } | 445 } |
260 | 446 |
261 PP_Var CreateObject(const PPP_Class* object_class, | 447 PP_Var CreateObject(const PPP_Class* object_class, |
262 void* object_data) { | 448 void* object_data) { |
263 return PP_MakeVoid(); // TODO(darin) | 449 return PP_MakeVoid(); // TODO(darin): Implement this method! |
264 } | 450 } |
265 | 451 |
266 const PPB_Var var_interface = { | 452 const PPB_Var var_interface = { |
267 &AddRef, | 453 &AddRef, |
268 &Release, | 454 &Release, |
269 &VarFromUtf8, | 455 &VarFromUtf8, |
270 &VarToUtf8, | 456 &VarToUtf8, |
271 &HasProperty, | 457 &HasProperty, |
272 &GetProperty, | 458 &GetProperty, |
273 &GetAllPropertyNames, | 459 &GetAllPropertyNames, |
(...skipping 11 matching lines...) Expand all Loading... |
285 } | 471 } |
286 | 472 |
287 PP_Var NPObjectToPPVar(NPObject* object) { | 473 PP_Var NPObjectToPPVar(NPObject* object) { |
288 PP_Var ret; | 474 PP_Var ret; |
289 ret.type = PP_VarType_Object; | 475 ret.type = PP_VarType_Object; |
290 ret.value.as_id = reinterpret_cast<intptr_t>(object); | 476 ret.value.as_id = reinterpret_cast<intptr_t>(object); |
291 WebBindings::retainObject(object); | 477 WebBindings::retainObject(object); |
292 return ret; | 478 return ret; |
293 } | 479 } |
294 | 480 |
295 PP_Var NPVariantToPPVar(NPVariant* variant) { | |
296 switch (variant->type) { | |
297 case NPVariantType_Void: | |
298 return PP_MakeVoid(); | |
299 case NPVariantType_Null: | |
300 return PP_MakeNull(); | |
301 case NPVariantType_Bool: | |
302 return PP_MakeBool(NPVARIANT_TO_BOOLEAN(*variant)); | |
303 case NPVariantType_Int32: | |
304 return PP_MakeInt32(NPVARIANT_TO_INT32(*variant)); | |
305 case NPVariantType_Double: | |
306 return PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); | |
307 case NPVariantType_String: | |
308 return VarFromUtf8(NPVARIANT_TO_STRING(*variant).UTF8Characters, | |
309 NPVARIANT_TO_STRING(*variant).UTF8Length); | |
310 case NPVariantType_Object: | |
311 return NPObjectToPPVar(NPVARIANT_TO_OBJECT(*variant)); | |
312 } | |
313 NOTREACHED(); | |
314 return PP_MakeVoid(); | |
315 } | |
316 | |
317 NPVariant PPVarToNPVariant(PP_Var var) { | |
318 NPVariant ret; | |
319 switch (var.type) { | |
320 case PP_VarType_Void: | |
321 VOID_TO_NPVARIANT(ret); | |
322 break; | |
323 case PP_VarType_Null: | |
324 NULL_TO_NPVARIANT(ret); | |
325 break; | |
326 case PP_VarType_Bool: | |
327 BOOLEAN_TO_NPVARIANT(var.value.as_bool, ret); | |
328 break; | |
329 case PP_VarType_Int32: | |
330 INT32_TO_NPVARIANT(var.value.as_int, ret); | |
331 break; | |
332 case PP_VarType_Double: | |
333 DOUBLE_TO_NPVARIANT(var.value.as_double, ret); | |
334 break; | |
335 case PP_VarType_String: { | |
336 const std::string& value = GetStringUnchecked(var)->value(); | |
337 STRINGN_TO_NPVARIANT(base::strdup(value.c_str()), value.size(), ret); | |
338 break; | |
339 } | |
340 case PP_VarType_Object: { | |
341 NPObject* object = GetNPObjectUnchecked(var); | |
342 OBJECT_TO_NPVARIANT(WebBindings::retainObject(object), ret); | |
343 break; | |
344 } | |
345 } | |
346 return ret; | |
347 } | |
348 | |
349 NPVariant PPVarToNPVariantNoCopy(PP_Var var) { | |
350 NPVariant ret; | |
351 switch (var.type) { | |
352 case PP_VarType_Void: | |
353 VOID_TO_NPVARIANT(ret); | |
354 break; | |
355 case PP_VarType_Null: | |
356 NULL_TO_NPVARIANT(ret); | |
357 break; | |
358 case PP_VarType_Bool: | |
359 BOOLEAN_TO_NPVARIANT(var.value.as_bool, ret); | |
360 break; | |
361 case PP_VarType_Int32: | |
362 INT32_TO_NPVARIANT(var.value.as_int, ret); | |
363 break; | |
364 case PP_VarType_Double: | |
365 DOUBLE_TO_NPVARIANT(var.value.as_double, ret); | |
366 break; | |
367 case PP_VarType_String: { | |
368 const std::string& value = GetStringUnchecked(var)->value(); | |
369 STRINGN_TO_NPVARIANT(value.c_str(), value.size(), ret); | |
370 break; | |
371 } | |
372 case PP_VarType_Object: { | |
373 OBJECT_TO_NPVARIANT(GetNPObjectUnchecked(var), ret); | |
374 break; | |
375 } | |
376 } | |
377 return ret; | |
378 } | |
379 | |
380 NPIdentifier PPVarToNPIdentifier(PP_Var var) { | |
381 switch (var.type) { | |
382 case PP_VarType_String: | |
383 return WebBindings::getStringIdentifier( | |
384 GetStringUnchecked(var)->value().c_str()); | |
385 case PP_VarType_Int32: | |
386 return WebBindings::getIntIdentifier(var.value.as_int); | |
387 default: | |
388 return NULL; | |
389 } | |
390 } | |
391 | |
392 } // namespace pepper | 481 } // namespace pepper |
OLD | NEW |