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