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 "gin/converter.h" | 8 #include "gin/converter.h" |
9 #include "ppapi/shared_impl/ppapi_globals.h" | 9 #include "ppapi/shared_impl/ppapi_globals.h" |
10 #include "ppapi/shared_impl/var_tracker.h" | 10 #include "ppapi/shared_impl/var_tracker.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 const char kConversionException[] = | 16 const char kConversionException[] = |
17 "Error: Failed conversion between PP_Var and V8 value"; | 17 "Error: Failed conversion between PP_Var and V8 value"; |
18 const char kInvalidException[] = "Error: An invalid exception was thrown."; | 18 const char kInvalidException[] = "Error: An invalid exception was thrown."; |
19 | 19 |
20 } // namespace | 20 } // namespace |
21 | 21 |
22 PepperTryCatch::PepperTryCatch(PepperPluginInstanceImpl* instance, | 22 PepperTryCatch::PepperTryCatch(PepperPluginInstanceImpl* instance, |
23 V8VarConverter::AllowObjectVars convert_objects) | 23 V8VarConverter::AllowObjectVars convert_objects) |
24 : instance_(instance), | 24 : instance_(instance), |
25 convert_objects_(convert_objects) {} | 25 convert_objects_(convert_objects) {} |
26 | 26 |
27 PepperTryCatch::~PepperTryCatch() {} | 27 PepperTryCatch::~PepperTryCatch() {} |
28 | 28 |
29 v8::Handle<v8::Context> PepperTryCatch::GetContext() { | |
30 return instance_->GetContext(); | |
31 } | |
32 | |
33 v8::Handle<v8::Value> PepperTryCatch::ToV8(PP_Var var) { | 29 v8::Handle<v8::Value> PepperTryCatch::ToV8(PP_Var var) { |
34 if (HasException()) { | 30 if (HasException()) { |
35 SetException(kConversionException); | 31 SetException(kConversionException); |
36 return v8::Handle<v8::Value>(); | 32 return v8::Handle<v8::Value>(); |
37 } | 33 } |
38 | 34 |
39 V8VarConverter converter(instance_->pp_instance(), convert_objects_); | 35 V8VarConverter converter(instance_->pp_instance(), convert_objects_); |
40 v8::Handle<v8::Value> result; | 36 v8::Handle<v8::Value> result; |
41 bool success = converter.ToV8Value(var, GetContext(), &result); | 37 bool success = converter.ToV8Value(var, GetContext(), &result); |
42 if (!success) { | 38 if (!success) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 } | 74 } |
79 | 75 |
80 PepperTryCatchV8::~PepperTryCatchV8() { | 76 PepperTryCatchV8::~PepperTryCatchV8() { |
81 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); | 77 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); |
82 } | 78 } |
83 | 79 |
84 bool PepperTryCatchV8::HasException() { | 80 bool PepperTryCatchV8::HasException() { |
85 return GetContext().IsEmpty() || exception_.type != PP_VARTYPE_UNDEFINED; | 81 return GetContext().IsEmpty() || exception_.type != PP_VARTYPE_UNDEFINED; |
86 } | 82 } |
87 | 83 |
| 84 v8::Handle<v8::Context> PepperTryCatchV8::GetContext() { |
| 85 // When calling from JS into the plugin always use the current context. |
| 86 return instance_->GetIsolate()->GetCurrentContext(); |
| 87 } |
| 88 |
88 bool PepperTryCatchV8::ThrowException() { | 89 bool PepperTryCatchV8::ThrowException() { |
89 if (!HasException()) | 90 if (!HasException()) |
90 return false; | 91 return false; |
91 | 92 |
92 // If the plugin context is gone, then we have an exception but we don't try | 93 // If there is no context then we have an exception but we don't try to throw |
93 // to throw it into v8. | 94 // it into v8. |
94 if (GetContext().IsEmpty()) | 95 if (GetContext().IsEmpty()) |
95 return true; | 96 return true; |
96 | 97 |
97 std::string message(kInvalidException); | 98 std::string message(kInvalidException); |
98 ppapi::StringVar* message_var = ppapi::StringVar::FromPPVar(exception_); | 99 ppapi::StringVar* message_var = ppapi::StringVar::FromPPVar(exception_); |
99 if (message_var) | 100 if (message_var) |
100 message = message_var->value(); | 101 message = message_var->value(); |
101 instance_->GetIsolate()->ThrowException(v8::Exception::Error( | 102 instance_->GetIsolate()->ThrowException(v8::Exception::Error( |
102 gin::StringToV8(instance_->GetIsolate(), message))); | 103 gin::StringToV8(instance_->GetIsolate(), message))); |
103 | 104 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 | 150 |
150 if (!exception_message.empty()) { | 151 if (!exception_message.empty()) { |
151 exception_is_set_ = true; | 152 exception_is_set_ = true; |
152 if (exception_) | 153 if (exception_) |
153 *exception_ = ppapi::StringVar::StringToPPVar(exception_message); | 154 *exception_ = ppapi::StringVar::StringToPPVar(exception_message); |
154 } | 155 } |
155 | 156 |
156 return exception_is_set_; | 157 return exception_is_set_; |
157 } | 158 } |
158 | 159 |
| 160 v8::Handle<v8::Context> PepperTryCatchVar::GetContext() { |
| 161 // When calling into JS from the plugin, always use the plugin context. |
| 162 return instance_->GetMainWorldContext(); |
| 163 } |
| 164 |
159 void PepperTryCatchVar::SetException(const char* message) { | 165 void PepperTryCatchVar::SetException(const char* message) { |
160 if (exception_is_set_) | 166 if (exception_is_set_) |
161 return; | 167 return; |
162 | 168 |
163 if (exception_) | 169 if (exception_) |
164 *exception_ = ppapi::StringVar::StringToPPVar(message, strlen(message)); | 170 *exception_ = ppapi::StringVar::StringToPPVar(message, strlen(message)); |
165 exception_is_set_ = true; | 171 exception_is_set_ = true; |
166 } | 172 } |
167 | 173 |
168 } // namespace content | 174 } // namespace content |
OLD | NEW |