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 "extensions/renderer/v8_context_native_handler.h"
8 #include "extensions/common/features/feature.h"
9 #include "extensions/renderer/dispatcher.h"
10 #include "extensions/renderer/script_context.h"
11 #include "third_party/WebKit/public/web/WebLocalFrame.h"
13 namespace extensions
{
15 V8ContextNativeHandler::V8ContextNativeHandler(ScriptContext
* context
,
16 Dispatcher
* dispatcher
)
17 : ObjectBackedNativeHandler(context
),
19 dispatcher_(dispatcher
) {
20 RouteFunction("GetAvailability",
21 base::Bind(&V8ContextNativeHandler::GetAvailability
,
22 base::Unretained(this)));
23 RouteFunction("GetModuleSystem",
24 base::Bind(&V8ContextNativeHandler::GetModuleSystem
,
25 base::Unretained(this)));
27 "RunWithNativesEnabled",
28 base::Bind(&V8ContextNativeHandler::RunWithNativesEnabled
,
29 base::Unretained(this)));
32 void V8ContextNativeHandler::GetAvailability(
33 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
34 CHECK_EQ(args
.Length(), 1);
35 v8::Isolate
* isolate
= args
.GetIsolate();
36 std::string api_name
= *v8::String::Utf8Value(args
[0]);
37 Feature::Availability availability
= context_
->GetAvailability(api_name
);
39 v8::Local
<v8::Object
> ret
= v8::Object::New(isolate
);
40 ret
->Set(v8::String::NewFromUtf8(isolate
, "is_available"),
41 v8::Boolean::New(isolate
, availability
.is_available()));
42 ret
->Set(v8::String::NewFromUtf8(isolate
, "message"),
43 v8::String::NewFromUtf8(isolate
, availability
.message().c_str()));
44 ret
->Set(v8::String::NewFromUtf8(isolate
, "result"),
45 v8::Integer::New(isolate
, availability
.result()));
46 args
.GetReturnValue().Set(ret
);
49 void V8ContextNativeHandler::GetModuleSystem(
50 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
51 CHECK_EQ(args
.Length(), 1);
52 CHECK(args
[0]->IsObject());
53 v8::Local
<v8::Context
> v8_context
=
54 v8::Local
<v8::Object
>::Cast(args
[0])->CreationContext();
55 ScriptContext
* context
=
56 dispatcher_
->script_context_set().GetByV8Context(v8_context
);
57 if (blink::WebFrame::scriptCanAccess(context
->web_frame()))
58 args
.GetReturnValue().Set(context
->module_system()->NewInstance());
61 void V8ContextNativeHandler::RunWithNativesEnabled(
62 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
63 CHECK_EQ(args
.Length(), 1);
64 CHECK(args
[0]->IsFunction());
65 ModuleSystem::NativesEnabledScope
natives_enabled(context()->module_system());
66 context()->CallFunction(v8::Local
<v8::Function
>::Cast(args
[0]));
69 } // namespace extensions