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