OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "content/renderer/pepper/pepper_try_catch.h" |
| 6 |
| 7 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 8 #include "content/renderer/pepper/v8_var_converter.h" |
| 9 #include "ppapi/shared_impl/ppapi_globals.h" |
| 10 #include "ppapi/shared_impl/var_tracker.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kConversionException[] = "Error: Failed conversion between PP_Var " |
| 17 "and V8 value"; |
| 18 const char kInvalidException[] = "Error: An invalid exception was thrown."; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 PepperTryCatch::PepperTryCatch(PepperPluginInstanceImpl* instance, |
| 23 bool convert_objects) |
| 24 : instance_(instance), |
| 25 convert_objects_(convert_objects) {} |
| 26 |
| 27 PepperTryCatch::~PepperTryCatch() {} |
| 28 |
| 29 v8::Handle<v8::Context> PepperTryCatch::GetContext() { |
| 30 return instance_->GetPluginContext(); |
| 31 } |
| 32 |
| 33 v8::Handle<v8::Value> PepperTryCatch::ToV8(PP_Var var) { |
| 34 V8VarConverter converter(instance_->pp_instance(), convert_objects_); |
| 35 v8::Handle<v8::Value> result; |
| 36 bool success = converter.ToV8Value(var, GetContext(), &result); |
| 37 if (!success) { |
| 38 SetException(kConversionException); |
| 39 return v8::Handle<v8::Value>(); |
| 40 } |
| 41 return result; |
| 42 } |
| 43 |
| 44 ppapi::ScopedPPVar PepperTryCatch::FromV8(v8::Handle<v8::Value> v8_value) { |
| 45 if (v8_value.IsEmpty()) { |
| 46 SetException(kConversionException); |
| 47 return ppapi::ScopedPPVar(); |
| 48 } |
| 49 ppapi::ScopedPPVar result; |
| 50 V8VarConverter converter(instance_->pp_instance(), convert_objects_); |
| 51 bool success = converter.FromV8ValueSync(v8_value, GetContext(), &result); |
| 52 if (!success) { |
| 53 SetException(kConversionException); |
| 54 return ppapi::ScopedPPVar(); |
| 55 } |
| 56 return result; |
| 57 } |
| 58 |
| 59 PepperTryCatchV8::PepperTryCatchV8(PepperPluginInstanceImpl* instance, |
| 60 bool convert_objects, |
| 61 v8::Isolate* isolate) |
| 62 : PepperTryCatch(instance, convert_objects), |
| 63 exception_(PP_MakeUndefined()) { |
| 64 // Typically when using PepperTryCatchV8 we are passed an isolate. We verify |
| 65 // that this isolate is the same as the plugin isolate. |
| 66 DCHECK(isolate == instance_->GetIsolate()); |
| 67 // We assume we are already in the plugin context for PepperTryCatchV8. |
| 68 DCHECK(GetContext() == isolate->GetCurrentContext()); |
| 69 } |
| 70 |
| 71 PepperTryCatchV8::~PepperTryCatchV8() { |
| 72 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); |
| 73 } |
| 74 |
| 75 bool PepperTryCatchV8::HasException() { |
| 76 return exception_.type != PP_VARTYPE_UNDEFINED; |
| 77 } |
| 78 |
| 79 bool PepperTryCatchV8::ThrowException() { |
| 80 if (!HasException()) |
| 81 return false; |
| 82 |
| 83 std::string message(kInvalidException); |
| 84 ppapi::StringVar* message_var = ppapi::StringVar::FromPPVar(exception_); |
| 85 if (message_var) |
| 86 message = message_var->value(); |
| 87 instance_->GetIsolate()->ThrowException(v8::Exception::Error( |
| 88 gin::StringToV8(instance_->GetIsolate(), message))); |
| 89 |
| 90 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); |
| 91 exception_ = PP_MakeUndefined(); |
| 92 return true; |
| 93 } |
| 94 |
| 95 void PepperTryCatchV8::ThrowException(const char* message) { |
| 96 SetException(message); |
| 97 ThrowException(); |
| 98 } |
| 99 |
| 100 void PepperTryCatchV8::SetException(const char* message) { |
| 101 if (HasException()) { |
| 102 NOTREACHED(); |
| 103 return; |
| 104 } |
| 105 exception_ = ppapi::StringVar::StringToPPVar(message); |
| 106 } |
| 107 |
| 108 PepperTryCatchVar::PepperTryCatchVar(PepperPluginInstanceImpl* instance, |
| 109 bool convert_objects, |
| 110 PP_Var* exception) |
| 111 : PepperTryCatch(instance, convert_objects), |
| 112 handle_scope_(instance_->GetIsolate()), |
| 113 exception_(exception), |
| 114 exception_is_set_(false) { |
| 115 // We switch to the plugin context. |
| 116 if (!GetContext().IsEmpty()) |
| 117 GetContext()->Enter(); |
| 118 } |
| 119 |
| 120 PepperTryCatchVar::~PepperTryCatchVar() { |
| 121 if (!GetContext().IsEmpty()) |
| 122 GetContext()->Exit(); |
| 123 } |
| 124 |
| 125 bool PepperTryCatchVar::HasException() { |
| 126 // Check if a v8 exception was caught. |
| 127 if (!exception_is_set_ && try_catch_.HasCaught()) { |
| 128 v8::String::Utf8Value utf8(try_catch_.Message()->Get()); |
| 129 if (exception_) { |
| 130 *exception_ = ppapi::StringVar::StringToPPVar( |
| 131 std::string(*utf8, utf8.length())); |
| 132 } |
| 133 exception_is_set_ = true; |
| 134 } |
| 135 |
| 136 return exception_is_set_; |
| 137 } |
| 138 |
| 139 void PepperTryCatchVar::SetException(const char* message) { |
| 140 if (exception_is_set_) { |
| 141 NOTREACHED(); |
| 142 return; |
| 143 } |
| 144 if (exception_) |
| 145 *exception_ = ppapi::StringVar::StringToPPVar(message, strlen(message)); |
| 146 exception_is_set_ = true; |
| 147 } |
| 148 |
| 149 } // namespace content |
OLD | NEW |