• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 "gin/modules/module_runner_delegate.h"
6 
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "gin/modules/module_registry.h"
10 #include "gin/object_template_builder.h"
11 #include "gin/public/context_holder.h"
12 
13 namespace gin {
14 
ModuleRunnerDelegate(const std::vector<base::FilePath> & search_paths)15 ModuleRunnerDelegate::ModuleRunnerDelegate(
16   const std::vector<base::FilePath>& search_paths)
17     : module_provider_(search_paths) {
18 }
19 
~ModuleRunnerDelegate()20 ModuleRunnerDelegate::~ModuleRunnerDelegate() {
21 }
22 
AddBuiltinModule(const std::string & id,ModuleGetter getter)23 void ModuleRunnerDelegate::AddBuiltinModule(const std::string& id,
24                                             ModuleGetter getter) {
25   builtin_modules_[id] = base::Bind(getter);
26 }
27 
AddBuiltinModule(const std::string & id,const ModuleGetterCallback & getter)28 void ModuleRunnerDelegate::AddBuiltinModule(const std::string& id,
29     const ModuleGetterCallback& getter) {
30   builtin_modules_[id] = getter;
31 }
32 
AttemptToLoadMoreModules(Runner * runner)33 void ModuleRunnerDelegate::AttemptToLoadMoreModules(Runner* runner) {
34   ModuleRegistry* registry = ModuleRegistry::From(
35       runner->GetContextHolder()->context());
36   registry->AttemptToLoadMoreModules(runner->GetContextHolder()->isolate());
37   module_provider_.AttempToLoadModules(
38       runner, registry->unsatisfied_dependencies());
39 }
40 
GetGlobalTemplate(ShellRunner * runner,v8::Isolate * isolate)41 v8::Handle<v8::ObjectTemplate> ModuleRunnerDelegate::GetGlobalTemplate(
42     ShellRunner* runner,
43     v8::Isolate* isolate) {
44   v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate).Build();
45   ModuleRegistry::RegisterGlobals(isolate, templ);
46   return templ;
47 }
48 
DidCreateContext(ShellRunner * runner)49 void ModuleRunnerDelegate::DidCreateContext(ShellRunner* runner) {
50   ShellRunnerDelegate::DidCreateContext(runner);
51 
52   v8::Handle<v8::Context> context = runner->GetContextHolder()->context();
53   ModuleRegistry* registry = ModuleRegistry::From(context);
54 
55   v8::Isolate* isolate = runner->GetContextHolder()->isolate();
56 
57   for (BuiltinModuleMap::const_iterator it = builtin_modules_.begin();
58        it != builtin_modules_.end(); ++it) {
59     registry->AddBuiltinModule(isolate, it->first, it->second.Run(isolate));
60   }
61 }
62 
DidRunScript(ShellRunner * runner)63 void ModuleRunnerDelegate::DidRunScript(ShellRunner* runner) {
64   AttemptToLoadMoreModules(runner);
65 }
66 
67 }  // namespace gin
68