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 "extensions/renderer/v8_context_native_handler.h" | 5 #include "extensions/renderer/v8_context_native_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "extensions/common/features/feature.h" | 8 #include "extensions/common/features/feature.h" |
9 #include "extensions/renderer/dispatcher.h" | 9 #include "extensions/renderer/dispatcher.h" |
10 #include "extensions/renderer/script_context.h" | 10 #include "extensions/renderer/script_context.h" |
| 11 #include "extensions/renderer/v8_maybe_helpers.h" |
11 | 12 |
12 namespace extensions { | 13 namespace extensions { |
13 | 14 |
14 V8ContextNativeHandler::V8ContextNativeHandler(ScriptContext* context, | 15 V8ContextNativeHandler::V8ContextNativeHandler(ScriptContext* context, |
15 Dispatcher* dispatcher) | 16 Dispatcher* dispatcher) |
16 : ObjectBackedNativeHandler(context), | 17 : ObjectBackedNativeHandler(context), |
17 context_(context), | 18 context_(context), |
18 dispatcher_(dispatcher) { | 19 dispatcher_(dispatcher) { |
19 RouteFunction("GetAvailability", | 20 RouteFunction("GetAvailability", |
20 base::Bind(&V8ContextNativeHandler::GetAvailability, | 21 base::Bind(&V8ContextNativeHandler::GetAvailability, |
21 base::Unretained(this))); | 22 base::Unretained(this))); |
22 RouteFunction("GetModuleSystem", | 23 RouteFunction("GetModuleSystem", |
23 base::Bind(&V8ContextNativeHandler::GetModuleSystem, | 24 base::Bind(&V8ContextNativeHandler::GetModuleSystem, |
24 base::Unretained(this))); | 25 base::Unretained(this))); |
25 RouteFunction( | 26 RouteFunction( |
26 "RunWithNativesEnabledModuleSystem", | 27 "RunWithNativesEnabledModuleSystem", |
27 base::Bind(&V8ContextNativeHandler::RunWithNativesEnabledModuleSystem, | 28 base::Bind(&V8ContextNativeHandler::RunWithNativesEnabledModuleSystem, |
28 base::Unretained(this))); | 29 base::Unretained(this))); |
29 } | 30 } |
30 | 31 |
31 void V8ContextNativeHandler::GetAvailability( | 32 void V8ContextNativeHandler::GetAvailability( |
32 const v8::FunctionCallbackInfo<v8::Value>& args) { | 33 const v8::FunctionCallbackInfo<v8::Value>& args) { |
33 CHECK_EQ(args.Length(), 1); | 34 CHECK_EQ(args.Length(), 1); |
34 v8::Isolate* isolate = args.GetIsolate(); | 35 v8::Isolate* isolate = args.GetIsolate(); |
| 36 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
35 std::string api_name = *v8::String::Utf8Value(args[0]); | 37 std::string api_name = *v8::String::Utf8Value(args[0]); |
36 Feature::Availability availability = context_->GetAvailability(api_name); | 38 Feature::Availability availability = context_->GetAvailability(api_name); |
| 39 v8::Local<v8::String> v8_availability = |
| 40 ToV8String(isolate, availability.message().c_str()); |
37 | 41 |
38 v8::Local<v8::Object> ret = v8::Object::New(isolate); | 42 v8::Local<v8::Object> ret = v8::Object::New(isolate); |
39 ret->Set(v8::String::NewFromUtf8(isolate, "is_available"), | 43 SetProperty(context, ret, ToV8String(isolate, "is_available"), |
40 v8::Boolean::New(isolate, availability.is_available())); | 44 v8::Boolean::New(isolate, availability.is_available())); |
41 ret->Set(v8::String::NewFromUtf8(isolate, "message"), | 45 SetProperty(context, ret, ToV8String(isolate, "message"), v8_availability); |
42 v8::String::NewFromUtf8(isolate, availability.message().c_str())); | 46 SetProperty(context, ret, ToV8String(isolate, "result"), |
43 ret->Set(v8::String::NewFromUtf8(isolate, "result"), | 47 v8::Integer::New(isolate, availability.result())); |
44 v8::Integer::New(isolate, availability.result())); | |
45 args.GetReturnValue().Set(ret); | 48 args.GetReturnValue().Set(ret); |
46 } | 49 } |
47 | 50 |
48 void V8ContextNativeHandler::GetModuleSystem( | 51 void V8ContextNativeHandler::GetModuleSystem( |
49 const v8::FunctionCallbackInfo<v8::Value>& args) { | 52 const v8::FunctionCallbackInfo<v8::Value>& args) { |
50 CHECK_EQ(args.Length(), 1); | 53 CHECK_EQ(args.Length(), 1); |
51 CHECK(args[0]->IsObject()); | 54 CHECK(args[0]->IsObject()); |
52 v8::Local<v8::Context> v8_context = | 55 v8::Local<v8::Context> v8_context = |
53 v8::Local<v8::Object>::Cast(args[0])->CreationContext(); | 56 v8::Local<v8::Object>::Cast(args[0])->CreationContext(); |
54 ScriptContext* context = | 57 ScriptContext* context = |
55 dispatcher_->script_context_set().GetByV8Context(v8_context); | 58 dispatcher_->script_context_set().GetByV8Context(v8_context); |
56 args.GetReturnValue().Set(context->module_system()->NewInstance()); | 59 v8::Local<v8::Object> instance; |
| 60 if (context->module_system()->NewInstance().ToLocal(&instance)) |
| 61 args.GetReturnValue().Set(instance); |
57 } | 62 } |
58 | 63 |
59 void V8ContextNativeHandler::RunWithNativesEnabledModuleSystem( | 64 void V8ContextNativeHandler::RunWithNativesEnabledModuleSystem( |
60 const v8::FunctionCallbackInfo<v8::Value>& args) { | 65 const v8::FunctionCallbackInfo<v8::Value>& args) { |
61 CHECK_EQ(args.Length(), 1); | 66 CHECK_EQ(args.Length(), 1); |
62 CHECK(args[0]->IsFunction()); | 67 CHECK(args[0]->IsFunction()); |
63 v8::Local<v8::Value> call_with_args[] = { | 68 v8::Local<v8::Object> instance; |
64 context()->module_system()->NewInstance()}; | 69 if (!context()->module_system()->NewInstance().ToLocal(&instance)) |
| 70 return; |
| 71 v8::Local<v8::Value> call_with_args[] = { instance }; |
65 ModuleSystem::NativesEnabledScope natives_enabled(context()->module_system()); | 72 ModuleSystem::NativesEnabledScope natives_enabled(context()->module_system()); |
66 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 1, | 73 context()->CallFunction(v8::Local<v8::Function>::Cast(args[0]), 1, |
67 call_with_args); | 74 call_with_args); |
68 } | 75 } |
69 | 76 |
70 } // namespace extensions | 77 } // namespace extensions |
OLD | NEW |