| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "content/renderer/pepper/npapi_glue.h" | 5 #include "content/renderer/pepper/npapi_glue.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "content/renderer/pepper/host_array_buffer_var.h" | 10 #include "content/renderer/pepper/host_array_buffer_var.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 } | 58 } |
| 59 scoped_refptr<NPObjectVar> object_var( | 59 scoped_refptr<NPObjectVar> object_var( |
| 60 HostGlobals::Get()->host_var_tracker()->NPObjectVarForNPObject( | 60 HostGlobals::Get()->host_var_tracker()->NPObjectVarForNPObject( |
| 61 instance->pp_instance(), object)); | 61 instance->pp_instance(), object)); |
| 62 if (!object_var.get()) { // No object for this module yet, make a new one. | 62 if (!object_var.get()) { // No object for this module yet, make a new one. |
| 63 object_var = new NPObjectVar(instance->pp_instance(), object); | 63 object_var = new NPObjectVar(instance->pp_instance(), object); |
| 64 } | 64 } |
| 65 return object_var->GetPPVar(); | 65 return object_var->GetPPVar(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 | |
| 69 } // namespace | 68 } // namespace |
| 70 | 69 |
| 71 // Utilities ------------------------------------------------------------------- | 70 // Utilities ------------------------------------------------------------------- |
| 72 | 71 |
| 73 bool PPVarToNPVariant(PP_Var var, NPVariant* result) { | 72 bool PPVarToNPVariant(PP_Var var, NPVariant* result) { |
| 74 switch (var.type) { | 73 switch (var.type) { |
| 75 case PP_VARTYPE_UNDEFINED: | 74 case PP_VARTYPE_UNDEFINED: |
| 76 VOID_TO_NPVARIANT(*result); | 75 VOID_TO_NPVARIANT(*result); |
| 77 break; | 76 break; |
| 78 case PP_VARTYPE_NULL: | 77 case PP_VARTYPE_NULL: |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 203 |
| 205 // PPResultAndExceptionToNPResult ---------------------------------------------- | 204 // PPResultAndExceptionToNPResult ---------------------------------------------- |
| 206 | 205 |
| 207 PPResultAndExceptionToNPResult::PPResultAndExceptionToNPResult( | 206 PPResultAndExceptionToNPResult::PPResultAndExceptionToNPResult( |
| 208 NPObject* object_var, | 207 NPObject* object_var, |
| 209 NPVariant* np_result) | 208 NPVariant* np_result) |
| 210 : object_var_(object_var), | 209 : object_var_(object_var), |
| 211 np_result_(np_result), | 210 np_result_(np_result), |
| 212 exception_(PP_MakeUndefined()), | 211 exception_(PP_MakeUndefined()), |
| 213 success_(false), | 212 success_(false), |
| 214 checked_exception_(false) { | 213 checked_exception_(false) {} |
| 215 } | |
| 216 | 214 |
| 217 PPResultAndExceptionToNPResult::~PPResultAndExceptionToNPResult() { | 215 PPResultAndExceptionToNPResult::~PPResultAndExceptionToNPResult() { |
| 218 // The user should have called SetResult or CheckExceptionForNoResult | 216 // The user should have called SetResult or CheckExceptionForNoResult |
| 219 // before letting this class go out of scope, or the exception will have | 217 // before letting this class go out of scope, or the exception will have |
| 220 // been lost. | 218 // been lost. |
| 221 DCHECK(checked_exception_); | 219 DCHECK(checked_exception_); |
| 222 | 220 |
| 223 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); | 221 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); |
| 224 } | 222 } |
| 225 | 223 |
| 226 // Call this with the return value of the PPAPI function. It will convert | 224 // Call this with the return value of the PPAPI function. It will convert |
| 227 // the result to the NPVariant output parameter and pass any exception on to | 225 // the result to the NPVariant output parameter and pass any exception on to |
| 228 // the JS engine. It will update the success flag and return it. | 226 // the JS engine. It will update the success flag and return it. |
| 229 bool PPResultAndExceptionToNPResult::SetResult(PP_Var result) { | 227 bool PPResultAndExceptionToNPResult::SetResult(PP_Var result) { |
| 230 DCHECK(!checked_exception_); // Don't call more than once. | 228 DCHECK(!checked_exception_); // Don't call more than once. |
| 231 DCHECK(np_result_); // Should be expecting a result. | 229 DCHECK(np_result_); // Should be expecting a result. |
| 232 | 230 |
| 233 checked_exception_ = true; | 231 checked_exception_ = true; |
| 234 | 232 |
| 235 if (has_exception()) { | 233 if (has_exception()) { |
| 236 ThrowException(); | 234 ThrowException(); |
| 237 success_ = false; | 235 success_ = false; |
| 238 } else if (!PPVarToNPVariant(result, np_result_)) { | 236 } else if (!PPVarToNPVariant(result, np_result_)) { |
| 239 WebBindings::setException(object_var_, kInvalidPluginValue); | 237 WebBindings::setException(object_var_, kInvalidPluginValue); |
| 240 success_ = false; | 238 success_ = false; |
| 241 } else { | 239 } else { |
| 242 success_ = true; | 240 success_ = true; |
| 243 } | 241 } |
| 244 | 242 |
| 245 // No matter what happened, we need to release the reference to the | 243 // No matter what happened, we need to release the reference to the |
| 246 // value passed in. On success, a reference to this value will be in | 244 // value passed in. On success, a reference to this value will be in |
| 247 // the np_result_. | 245 // the np_result_. |
| 248 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(result); | 246 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(result); |
| 249 return success_; | 247 return success_; |
| 250 } | 248 } |
| 251 | 249 |
| 252 // Call this after calling a PPAPI function that could have set the | 250 // Call this after calling a PPAPI function that could have set the |
| 253 // exception. It will pass the exception on to the JS engine and update | 251 // exception. It will pass the exception on to the JS engine and update |
| 254 // the success flag. | 252 // the success flag. |
| 255 // | 253 // |
| 256 // The success flag will be returned. | 254 // The success flag will be returned. |
| 257 bool PPResultAndExceptionToNPResult::CheckExceptionForNoResult() { | 255 bool PPResultAndExceptionToNPResult::CheckExceptionForNoResult() { |
| 258 DCHECK(!checked_exception_); // Don't call more than once. | 256 DCHECK(!checked_exception_); // Don't call more than once. |
| 259 DCHECK(!np_result_); // Can't have a result when doing this. | 257 DCHECK(!np_result_); // Can't have a result when doing this. |
| 260 | 258 |
| 261 checked_exception_ = true; | 259 checked_exception_ = true; |
| 262 | 260 |
| 263 if (has_exception()) { | 261 if (has_exception()) { |
| 264 ThrowException(); | 262 ThrowException(); |
| 265 success_ = false; | 263 success_ = false; |
| 266 return false; | 264 return false; |
| 267 } | 265 } |
| 268 success_ = true; | 266 success_ = true; |
| 269 return true; | 267 return true; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 299 PPVarArrayFromNPVariantArray::~PPVarArrayFromNPVariantArray() { | 297 PPVarArrayFromNPVariantArray::~PPVarArrayFromNPVariantArray() { |
| 300 ppapi::VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); | 298 ppapi::VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); |
| 301 for (size_t i = 0; i < size_; i++) | 299 for (size_t i = 0; i < size_; i++) |
| 302 var_tracker->ReleaseVar(array_[i]); | 300 var_tracker->ReleaseVar(array_[i]); |
| 303 } | 301 } |
| 304 | 302 |
| 305 // PPVarFromNPObject ----------------------------------------------------------- | 303 // PPVarFromNPObject ----------------------------------------------------------- |
| 306 | 304 |
| 307 PPVarFromNPObject::PPVarFromNPObject(PepperPluginInstanceImpl* instance, | 305 PPVarFromNPObject::PPVarFromNPObject(PepperPluginInstanceImpl* instance, |
| 308 NPObject* object) | 306 NPObject* object) |
| 309 : var_(NPObjectToPPVar(instance, object)) { | 307 : var_(NPObjectToPPVar(instance, object)) {} |
| 310 } | |
| 311 | 308 |
| 312 PPVarFromNPObject::~PPVarFromNPObject() { | 309 PPVarFromNPObject::~PPVarFromNPObject() { |
| 313 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var_); | 310 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(var_); |
| 314 } | 311 } |
| 315 | 312 |
| 316 // NPObjectAccessorWithIdentifier ---------------------------------------------- | 313 // NPObjectAccessorWithIdentifier ---------------------------------------------- |
| 317 | 314 |
| 318 NPObjectAccessorWithIdentifier::NPObjectAccessorWithIdentifier( | 315 NPObjectAccessorWithIdentifier::NPObjectAccessorWithIdentifier( |
| 319 NPObject* object, | 316 NPObject* object, |
| 320 NPIdentifier identifier, | 317 NPIdentifier identifier, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 333 } | 330 } |
| 334 | 331 |
| 335 // TryCatch -------------------------------------------------------------------- | 332 // TryCatch -------------------------------------------------------------------- |
| 336 | 333 |
| 337 TryCatch::TryCatch(PP_Var* exception) | 334 TryCatch::TryCatch(PP_Var* exception) |
| 338 : has_exception_(exception && exception->type != PP_VARTYPE_UNDEFINED), | 335 : has_exception_(exception && exception->type != PP_VARTYPE_UNDEFINED), |
| 339 exception_(exception) { | 336 exception_(exception) { |
| 340 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); | 337 WebBindings::pushExceptionHandler(&TryCatch::Catch, this); |
| 341 } | 338 } |
| 342 | 339 |
| 343 TryCatch::~TryCatch() { | 340 TryCatch::~TryCatch() { WebBindings::popExceptionHandler(); } |
| 344 WebBindings::popExceptionHandler(); | |
| 345 } | |
| 346 | 341 |
| 347 void TryCatch::SetException(const char* message) { | 342 void TryCatch::SetException(const char* message) { |
| 348 if (!has_exception()) { | 343 if (!has_exception()) { |
| 349 has_exception_ = true; | 344 has_exception_ = true; |
| 350 if (exception_) { | 345 if (exception_) { |
| 351 *exception_ = ppapi::StringVar::StringToPPVar(message, strlen(message)); | 346 *exception_ = ppapi::StringVar::StringToPPVar(message, strlen(message)); |
| 352 } | 347 } |
| 353 } | 348 } |
| 354 } | 349 } |
| 355 | 350 |
| 356 // static | 351 // static |
| 357 void TryCatch::Catch(void* self, const char* message) { | 352 void TryCatch::Catch(void* self, const char* message) { |
| 358 static_cast<TryCatch*>(self)->SetException(message); | 353 static_cast<TryCatch*>(self)->SetException(message); |
| 359 } | 354 } |
| 360 | 355 |
| 361 } // namespace content | 356 } // namespace content |
| OLD | NEW |