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/web_ui_runner.h"
7 #include "gin/modules/module_registry.h"
8 #include "gin/per_context_data.h"
9 #include "gin/public/context_holder.h"
10 #include "mojo/bindings/js/core.h"
11 #include "mojo/bindings/js/support.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebScriptSource.h"
16 using v8::HandleScope
;
19 using v8::ObjectTemplate
;
24 WebUIRunner::WebUIRunner(blink::WebFrame
* frame
,
25 gin::ContextHolder
* context_holder
)
27 context_holder_(context_holder
) {
29 v8::Isolate::Scope
isolate_scope(context_holder
->isolate());
30 HandleScope
handle_scope(context_holder
->isolate());
31 // Note: this installs the runner globally. If we need to support more than
32 // one runner at a time we'll have to revisit this.
33 gin::PerContextData::From(context_holder
->context())->set_runner(this);
36 WebUIRunner::~WebUIRunner() {
39 void WebUIRunner::RegisterBuiltinModules() {
40 gin::ModuleRegistry
* registry
=
41 gin::ModuleRegistry::From(context_holder_
->context());
42 registry
->AddBuiltinModule(context_holder_
->isolate(),
43 mojo::js::Core::kModuleName
,
44 mojo::js::Core::GetModule(
45 context_holder_
->isolate()));
46 registry
->AddBuiltinModule(context_holder_
->isolate(),
47 mojo::js::Support::kModuleName
,
48 mojo::js::Support::GetModule(
49 context_holder_
->isolate()));
52 void WebUIRunner::Run(const std::string
& source
,
53 const std::string
& resource_name
) {
54 frame_
->executeScript(
55 blink::WebScriptSource(blink::WebString::fromUTF8(source
)));
58 v8::Handle
<v8::Value
> WebUIRunner::Call(v8::Handle
<v8::Function
> function
,
59 v8::Handle
<v8::Value
> receiver
,
61 v8::Handle
<v8::Value
> argv
[]) {
62 return frame_
->callFunctionEvenIfScriptDisabled(function
, receiver
, argc
,
66 gin::ContextHolder
* WebUIRunner::GetContextHolder() {
67 return context_holder_
;
70 } // namespace content