Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / web_ui_runner.cc
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/web_ui_runner.h"
6
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 "mojo/bindings/js/unicode.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/WebKit/public/web/WebScriptSource.h"
15
16 using v8::Context;
17 using v8::HandleScope;
18 using v8::Isolate;
19 using v8::Object;
20 using v8::ObjectTemplate;
21 using v8::Script;
22
23 namespace content {
24
25 WebUIRunner::WebUIRunner(blink::WebFrame* frame,
26                          gin::ContextHolder* context_holder)
27     : frame_(frame),
28       context_holder_(context_holder) {
29   DCHECK(frame_);
30   v8::Isolate::Scope isolate_scope(context_holder->isolate());
31   HandleScope handle_scope(context_holder->isolate());
32   // Note: this installs the runner globally. If we need to support more than
33   // one runner at a time we'll have to revisit this.
34   gin::PerContextData::From(context_holder->context())->set_runner(this);
35 }
36
37 WebUIRunner::~WebUIRunner() {
38 }
39
40 void WebUIRunner::RegisterBuiltinModules() {
41   gin::ModuleRegistry* registry =
42       gin::ModuleRegistry::From(context_holder_->context());
43   registry->AddBuiltinModule(context_holder_->isolate(),
44                              mojo::js::Core::kModuleName,
45                              mojo::js::Core::GetModule(
46                                  context_holder_->isolate()));
47   registry->AddBuiltinModule(context_holder_->isolate(),
48                              mojo::js::Support::kModuleName,
49                              mojo::js::Support::GetModule(
50                                  context_holder_->isolate()));
51   registry->AddBuiltinModule(context_holder_->isolate(),
52                              mojo::js::Unicode::kModuleName,
53                              mojo::js::Unicode::GetModule(
54                                  context_holder_->isolate()));
55 }
56
57 void WebUIRunner::Run(const std::string& source,
58                       const std::string& resource_name) {
59   frame_->executeScript(
60       blink::WebScriptSource(blink::WebString::fromUTF8(source)));
61 }
62
63 v8::Handle<v8::Value> WebUIRunner::Call(v8::Handle<v8::Function> function,
64                                         v8::Handle<v8::Value> receiver,
65                                         int argc,
66                                         v8::Handle<v8::Value> argv[]) {
67   return frame_->callFunctionEvenIfScriptDisabled(function, receiver, argc,
68                                                   argv);
69 }
70
71 gin::ContextHolder* WebUIRunner::GetContextHolder() {
72   return context_holder_;
73 }
74
75 }  // namespace content