Home | History | Annotate | Download | only in renderer
      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 "third_party/WebKit/public/web/WebFrame.h"
     13 #include "third_party/WebKit/public/web/WebScriptSource.h"
     14 
     15 using v8::Context;
     16 using v8::HandleScope;
     17 using v8::Isolate;
     18 using v8::Object;
     19 using v8::ObjectTemplate;
     20 using v8::Script;
     21 
     22 namespace content {
     23 
     24 WebUIRunner::WebUIRunner(blink::WebFrame* frame,
     25                          gin::ContextHolder* context_holder)
     26     : frame_(frame),
     27       context_holder_(context_holder) {
     28   DCHECK(frame_);
     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);
     34 }
     35 
     36 WebUIRunner::~WebUIRunner() {
     37 }
     38 
     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()));
     50 }
     51 
     52 void WebUIRunner::Run(const std::string& source,
     53                       const std::string& resource_name) {
     54   frame_->executeScript(
     55       blink::WebScriptSource(blink::WebString::fromUTF8(source)));
     56 }
     57 
     58 v8::Handle<v8::Value> WebUIRunner::Call(v8::Handle<v8::Function> function,
     59                                         v8::Handle<v8::Value> receiver,
     60                                         int argc,
     61                                         v8::Handle<v8::Value> argv[]) {
     62   return frame_->callFunctionEvenIfScriptDisabled(function, receiver, argc,
     63                                                   argv);
     64 }
     65 
     66 gin::ContextHolder* WebUIRunner::GetContextHolder() {
     67   return context_holder_;
     68 }
     69 
     70 }  // namespace content
     71