Chromium Code Reviews| 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 "gin/converter.h" | |
| 10 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 11 #include "ppapi/shared_impl/var_tracker.h" | |
| 12 | |
| 13 namespace content { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 const char kConversionException[] = | |
| 18 "Error: Failed conversion between PP_Var and V8 value"; | |
| 19 const char kInvalidException[] = "Error: An invalid exception was thrown."; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 PepperTryCatch::PepperTryCatch(PepperPluginInstanceImpl* instance, | |
| 24 bool convert_objects) | |
| 25 : instance_(instance), | |
| 26 convert_objects_(convert_objects) {} | |
| 27 | |
| 28 PepperTryCatch::~PepperTryCatch() {} | |
| 29 | |
| 30 v8::Handle<v8::Context> PepperTryCatch::GetContext() { | |
| 31 return instance_->GetContext(); | |
| 32 } | |
| 33 | |
| 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); | |
| 38 if (!success) { | |
| 39 SetException(kConversionException); | |
| 40 return v8::Handle<v8::Value>(); | |
| 41 } | |
| 42 return result; | |
| 43 } | |
| 44 | |
| 45 ppapi::ScopedPPVar PepperTryCatch::FromV8(v8::Handle<v8::Value> v8_value) { | |
| 46 if (v8_value.IsEmpty()) { | |
| 47 SetException(kConversionException); | |
| 48 return ppapi::ScopedPPVar(); | |
| 49 } | |
| 50 ppapi::ScopedPPVar result; | |
| 51 V8VarConverter converter(instance_->pp_instance(), convert_objects_); | |
| 52 bool success = converter.FromV8ValueSync(v8_value, GetContext(), &result); | |
| 53 if (!success) { | |
| 54 SetException(kConversionException); | |
| 55 return ppapi::ScopedPPVar(); | |
| 56 } | |
| 57 return result; | |
| 58 } | |
| 59 | |
| 60 PepperTryCatchV8::PepperTryCatchV8(PepperPluginInstanceImpl* instance, | |
| 61 bool convert_objects, | |
| 62 v8::Isolate* isolate) | |
| 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()); | |
| 70 } | |
| 71 | |
| 72 PepperTryCatchV8::~PepperTryCatchV8() { | |
| 73 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); | |
| 74 } | |
| 75 | |
| 76 bool PepperTryCatchV8::HasException() { | |
| 77 return exception_.type != PP_VARTYPE_UNDEFINED; | |
| 78 } | |
| 79 | |
| 80 bool PepperTryCatchV8::ThrowException() { | |
| 81 if (!HasException()) | |
| 82 return false; | |
| 83 | |
| 84 std::string message(kInvalidException); | |
| 85 ppapi::StringVar* message_var = ppapi::StringVar::FromPPVar(exception_); | |
| 86 if (message_var) | |
| 87 message = message_var->value(); | |
| 88 instance_->GetIsolate()->ThrowException(v8::Exception::Error( | |
| 89 gin::StringToV8(instance_->GetIsolate(), message))); | |
| 90 | |
| 91 ppapi::PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(exception_); | |
| 92 exception_ = PP_MakeUndefined(); | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 void PepperTryCatchV8::ThrowException(const char* message) { | |
| 97 SetException(message); | |
| 98 ThrowException(); | |
| 99 } | |
| 100 | |
| 101 void PepperTryCatchV8::SetException(const char* message) { | |
| 102 if (HasException()) { | |
| 103 NOTREACHED(); | |
| 104 return; | |
| 105 } | |
| 106 exception_ = ppapi::StringVar::StringToPPVar(message); | |
| 107 } | |
| 108 | |
| 109 PepperTryCatchVar::PepperTryCatchVar(PepperPluginInstanceImpl* instance, | |
| 110 bool convert_objects, | |
| 111 PP_Var* exception) | |
| 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 if (!GetContext().IsEmpty()) | |
| 118 GetContext()->Enter(); | |
| 119 } | |
| 120 | |
| 121 PepperTryCatchVar::~PepperTryCatchVar() { | |
| 122 if (!GetContext().IsEmpty()) | |
|
dmichael (off chromium)
2014/07/30 19:34:14
This should never be empty, right? Maybe you can j
raymes
2014/08/06 03:22:52
Done.
dmichael (off chromium)
2014/08/06 15:41:45
Hmm, I might have been wrong about this, sorry. Th
raymes
2014/08/11 00:26:33
Good to know, let's leave the check out for now an
| |
| 123 GetContext()->Exit(); | |
| 124 } | |
| 125 | |
| 126 bool PepperTryCatchVar::HasException() { | |
| 127 // Check if a v8 exception was caught. | |
| 128 if (!exception_is_set_ && try_catch_.HasCaught()) { | |
| 129 v8::String::Utf8Value utf8(try_catch_.Message()->Get()); | |
| 130 if (exception_) { | |
|
dmichael (off chromium)
2014/07/30 19:34:13
Is this so you can allow a user of this class to j
raymes
2014/08/06 03:22:52
There are two cases when we use this class:
-One i
dmichael (off chromium)
2014/08/06 15:41:45
I was trying to say it might be clearer if you jus
raymes
2014/08/11 00:26:33
Ah I see. I think that this would make the intende
| |
| 131 *exception_ = ppapi::StringVar::StringToPPVar( | |
| 132 std::string(*utf8, utf8.length())); | |
| 133 } | |
| 134 exception_is_set_ = true; | |
| 135 } | |
| 136 | |
| 137 return exception_is_set_; | |
| 138 } | |
| 139 | |
| 140 void PepperTryCatchVar::SetException(const char* message) { | |
| 141 if (exception_is_set_) { | |
| 142 NOTREACHED(); | |
| 143 return; | |
| 144 } | |
| 145 if (exception_) | |
| 146 *exception_ = ppapi::StringVar::StringToPPVar(message, strlen(message)); | |
| 147 exception_is_set_ = true; | |
| 148 } | |
| 149 | |
| 150 } // namespace content | |
| OLD | NEW |