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.
5 #include "content/renderer/pepper/pepper_try_catch.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"
10 #include "ppapi/shared_impl/ppapi_globals.h"
11 #include "ppapi/shared_impl/var_tracker.h"
17 const char kConversionException
[] =
18 "Error: Failed conversion between PP_Var and V8 value";
19 const char kInvalidException
[] = "Error: An invalid exception was thrown.";
23 PepperTryCatch::PepperTryCatch(PepperPluginInstanceImpl
* instance
,
25 : instance_(instance
),
26 convert_objects_(convert_objects
) {}
28 PepperTryCatch::~PepperTryCatch() {}
30 v8::Handle
<v8::Context
> PepperTryCatch::GetContext() {
31 return instance_
->GetContext();
34 v8::Handle
<v8::Value
> PepperTryCatch::ToV8(PP_Var var
) {
35 V8VarConverter
converter(instance_
->pp_instance(), convert_objects_
);
36 v8::Handle
<v8::Value
> result
;
37 bool success
= converter
.ToV8Value(var
, GetContext(), &result
);
39 SetException(kConversionException
);
40 return v8::Handle
<v8::Value
>();
45 ppapi::ScopedPPVar
PepperTryCatch::FromV8(v8::Handle
<v8::Value
> v8_value
) {
46 if (v8_value
.IsEmpty()) {
47 SetException(kConversionException
);
48 return ppapi::ScopedPPVar();
50 ppapi::ScopedPPVar result
;
51 V8VarConverter
converter(instance_
->pp_instance(), convert_objects_
);
52 bool success
= converter
.FromV8ValueSync(v8_value
, GetContext(), &result
);
54 SetException(kConversionException
);
55 return ppapi::ScopedPPVar();
60 PepperTryCatchV8::PepperTryCatchV8(PepperPluginInstanceImpl
* instance
,
63 : PepperTryCatch(instance
, convert_objects
),
64 exception_(PP_MakeUndefined()) {
65 // Typically when using PepperTryCatchV8 we are passed an isolate. We verify
66 // that this isolate is the same as the plugin isolate.
67 DCHECK(isolate
== instance_
->GetIsolate());
68 // We assume we are already in the plugin context for PepperTryCatchV8.
69 DCHECK(GetContext() == isolate
->GetCurrentContext());
72 PepperTryCatchV8::~PepperTryCatchV8() {
73 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_
);
76 bool PepperTryCatchV8::HasException() {
77 return exception_
.type
!= PP_VARTYPE_UNDEFINED
;
80 bool PepperTryCatchV8::ThrowException() {
84 std::string
message(kInvalidException
);
85 ppapi::StringVar
* message_var
= ppapi::StringVar::FromPPVar(exception_
);
87 message
= message_var
->value();
88 instance_
->GetIsolate()->ThrowException(v8::Exception::Error(
89 gin::StringToV8(instance_
->GetIsolate(), message
)));
91 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_
);
92 exception_
= PP_MakeUndefined();
96 void PepperTryCatchV8::ThrowException(const char* message
) {
97 SetException(message
);
101 void PepperTryCatchV8::SetException(const char* message
) {
102 if (HasException()) {
106 exception_
= ppapi::StringVar::StringToPPVar(message
);
109 PepperTryCatchVar::PepperTryCatchVar(PepperPluginInstanceImpl
* instance
,
110 bool convert_objects
,
112 : PepperTryCatch(instance
, convert_objects
),
113 handle_scope_(instance_
->GetIsolate()),
114 exception_(exception
),
115 exception_is_set_(false) {
116 // We switch to the plugin context.
117 GetContext()->Enter();
120 PepperTryCatchVar::~PepperTryCatchVar() {
121 GetContext()->Exit();
124 bool PepperTryCatchVar::HasException() {
125 // Check if a v8 exception was caught.
126 if (!exception_is_set_
&& try_catch_
.HasCaught()) {
127 v8::String::Utf8Value
utf8(try_catch_
.Message()->Get());
129 *exception_
= ppapi::StringVar::StringToPPVar(
130 std::string(*utf8
, utf8
.length()));
132 exception_is_set_
= true;
135 return exception_is_set_
;
138 void PepperTryCatchVar::SetException(const char* message
) {
139 if (exception_is_set_
) {
144 *exception_
= ppapi::StringVar::StringToPPVar(message
, strlen(message
));
145 exception_is_set_
= true;
148 } // namespace content