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 #ifndef GIN_MODULES_MODULE_RUNNER_DELEGATE_H_ 6 #define GIN_MODULES_MODULE_RUNNER_DELEGATE_H_ 7 8 #include <map> 9 10 #include "base/compiler_specific.h" 11 #include "gin/gin_export.h" 12 #include "gin/modules/file_module_provider.h" 13 #include "gin/runner.h" 14 15 namespace gin { 16 17 typedef v8::Local<v8::ObjectTemplate> (*ModuleTemplateGetter)( 18 v8::Isolate* isolate); 19 20 // Emebedders that use AMD modules will probably want to use a RunnerDelegate 21 // that inherits from ModuleRunnerDelegate. ModuleRunnerDelegate lets embedders 22 // register built-in modules and routes module requests to FileModuleProvider. 23 class GIN_EXPORT ModuleRunnerDelegate : public RunnerDelegate { 24 public: 25 explicit ModuleRunnerDelegate( 26 const std::vector<base::FilePath>& search_paths); 27 virtual ~ModuleRunnerDelegate(); 28 29 // Lets you register a built-in module. Built-in modules are instantiated by 30 // creating a new instance of a v8::ObjectTemplate rather than by executing 31 // code. This function takes a ModuleTemplateGetter rather than a 32 // v8::ObjectTemplate directly so that embedders can create object templates 33 // lazily. 34 void AddBuiltinModule(const std::string& id, ModuleTemplateGetter templ); 35 36 protected: 37 void AttemptToLoadMoreModules(Runner* runner); 38 39 private: 40 typedef std::map<std::string, ModuleTemplateGetter> BuiltinModuleMap; 41 42 // From RunnerDelegate: 43 virtual v8::Handle<v8::ObjectTemplate> GetGlobalTemplate( 44 Runner* runner) OVERRIDE; 45 virtual void DidCreateContext(Runner* runner) OVERRIDE; 46 virtual void DidRunScript(Runner* runner) OVERRIDE; 47 48 BuiltinModuleMap builtin_modules_; 49 FileModuleProvider module_provider_; 50 51 DISALLOW_COPY_AND_ASSIGN(ModuleRunnerDelegate); 52 }; 53 54 } // namespace gin 55 56 #endif // GIN_MODULES_MODULE_RUNNER_DELEGATE_H_ 57