OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/modules/module_runner_delegate.h" | 5 #include "gin/modules/module_runner_delegate.h" |
6 | 6 |
7 #include "gin/modules/module_registry.h" | 7 #include "gin/modules/module_registry.h" |
8 #include "gin/object_template_builder.h" | 8 #include "gin/object_template_builder.h" |
| 9 #include "gin/public/context_holder.h" |
9 | 10 |
10 namespace gin { | 11 namespace gin { |
11 | 12 |
| 13 namespace { |
| 14 |
| 15 // DefaultModuleLoader is a ModuleLoader that forwards to the ModuleRegistry. |
| 16 class DefaultModuleLoader : public ModuleLoader { |
| 17 public: |
| 18 explicit DefaultModuleLoader(v8::Handle<v8::Context> context); |
| 19 virtual ~DefaultModuleLoader(); |
| 20 |
| 21 static void RegisterGlobals(v8::Isolate* isolate, |
| 22 v8::Handle<v8::ObjectTemplate> templ); |
| 23 |
| 24 private: |
| 25 // ModuleLoader overrides: |
| 26 virtual void Load(v8::Handle<v8::Context> context, |
| 27 scoped_ptr<PendingModule> pending) OVERRIDE; |
| 28 |
| 29 DISALLOW_COPY_AND_ASSIGN(DefaultModuleLoader); |
| 30 }; |
| 31 |
| 32 DefaultModuleLoader::~DefaultModuleLoader() { |
| 33 } |
| 34 |
| 35 // static |
| 36 void DefaultModuleLoader::RegisterGlobals( |
| 37 v8::Isolate* isolate, |
| 38 v8::Handle<v8::ObjectTemplate> templ) { |
| 39 ModuleLoader::RegisterGlobals(isolate, templ); |
| 40 } |
| 41 |
| 42 DefaultModuleLoader::DefaultModuleLoader(v8::Handle<v8::Context> context) |
| 43 : ModuleLoader(context) { |
| 44 } |
| 45 |
| 46 void DefaultModuleLoader::Load(v8::Handle<v8::Context> context, |
| 47 scoped_ptr<PendingModule> pending) { |
| 48 ModuleRegistry::From(context)->AddPendingModule(context->GetIsolate(), |
| 49 pending.Pass()); |
| 50 } |
| 51 |
| 52 } // namespace |
| 53 |
12 ModuleRunnerDelegate::ModuleRunnerDelegate( | 54 ModuleRunnerDelegate::ModuleRunnerDelegate( |
13 const std::vector<base::FilePath>& search_paths) | 55 const std::vector<base::FilePath>& search_paths) |
14 : module_provider_(search_paths) { | 56 : module_provider_(search_paths) { |
15 } | 57 } |
16 | 58 |
17 ModuleRunnerDelegate::~ModuleRunnerDelegate() { | 59 ModuleRunnerDelegate::~ModuleRunnerDelegate() { |
18 } | 60 } |
19 | 61 |
20 void ModuleRunnerDelegate::AddBuiltinModule(const std::string& id, | 62 void ModuleRunnerDelegate::AddBuiltinModule(const std::string& id, |
21 ModuleGetter getter) { | 63 ModuleGetter getter) { |
22 builtin_modules_[id] = getter; | 64 builtin_modules_[id] = getter; |
23 } | 65 } |
24 | 66 |
25 void ModuleRunnerDelegate::AttemptToLoadMoreModules(Runner* runner) { | 67 void ModuleRunnerDelegate::AttemptToLoadMoreModules(Runner* runner) { |
26 ModuleRegistry* registry = ModuleRegistry::From(runner->context()); | 68 ModuleRegistry* registry = ModuleRegistry::From( |
27 registry->AttemptToLoadMoreModules(runner->isolate()); | 69 runner->GetContextHolder()->context()); |
| 70 registry->AttemptToLoadMoreModules(runner->GetContextHolder()->isolate()); |
28 module_provider_.AttempToLoadModules( | 71 module_provider_.AttempToLoadModules( |
29 runner, registry->unsatisfied_dependencies()); | 72 runner, registry->unsatisfied_dependencies()); |
30 } | 73 } |
31 | 74 |
32 v8::Handle<v8::ObjectTemplate> ModuleRunnerDelegate::GetGlobalTemplate( | 75 v8::Handle<v8::ObjectTemplate> ModuleRunnerDelegate::GetGlobalTemplate( |
33 Runner* runner) { | 76 DefaultRunner* runner, |
34 v8::Handle<v8::ObjectTemplate> templ = | 77 v8::Isolate* isolate) { |
35 ObjectTemplateBuilder(runner->isolate()).Build(); | 78 v8::Handle<v8::ObjectTemplate> templ = ObjectTemplateBuilder(isolate).Build(); |
36 ModuleRegistry::RegisterGlobals(runner->isolate(), templ); | 79 DefaultModuleLoader::RegisterGlobals(isolate, templ); |
37 return templ; | 80 return templ; |
38 } | 81 } |
39 | 82 |
40 void ModuleRunnerDelegate::DidCreateContext(Runner* runner) { | 83 void ModuleRunnerDelegate::DidCreateContext(DefaultRunner* runner) { |
41 RunnerDelegate::DidCreateContext(runner); | 84 DefaultRunnerDelegate::DidCreateContext(runner); |
42 | 85 |
43 v8::Handle<v8::Context> context = runner->context(); | 86 v8::Handle<v8::Context> context = runner->GetContextHolder()->context(); |
| 87 // DefaultModuleLoader is owned with the context. |
| 88 new DefaultModuleLoader(runner->GetContextHolder()->context()); |
44 ModuleRegistry* registry = ModuleRegistry::From(context); | 89 ModuleRegistry* registry = ModuleRegistry::From(context); |
45 | 90 |
| 91 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); |
46 for (BuiltinModuleMap::const_iterator it = builtin_modules_.begin(); | 92 for (BuiltinModuleMap::const_iterator it = builtin_modules_.begin(); |
47 it != builtin_modules_.end(); ++it) { | 93 it != builtin_modules_.end(); ++it) { |
48 registry->AddBuiltinModule(runner->isolate(), it->first, | 94 registry->AddBuiltinModule(isolate, it->first, it->second(isolate)); |
49 it->second(runner->isolate())); | |
50 } | 95 } |
51 } | 96 } |
52 | 97 |
53 void ModuleRunnerDelegate::DidRunScript(Runner* runner) { | 98 void ModuleRunnerDelegate::DidRunScript(DefaultRunner* runner) { |
54 AttemptToLoadMoreModules(runner); | 99 AttemptToLoadMoreModules(runner); |
55 } | 100 } |
56 | 101 |
57 } // namespace gin | 102 } // namespace gin |
OLD | NEW |