From 467834b30f40d1aff0031fbb7660fb211c8112e4 Mon Sep 17 00:00:00 2001 From: "abarth@chromium.org" Date: Fri, 22 Nov 2013 07:30:55 +0000 Subject: Teach mojo_js_bindings_unittests to load generated JS files This CL generalizes gin::FileModuleProvider to be able to load modules from several places in the file system. I've then made use of this facility in mojo_js_bindings_unittests to load generated JavaScript modules created by mojom_js_generator.py, similar to how C++ files can #include from the generated mojom directory. I've deleted sample_service.js now that we can load it from the generated directory. BUG=317398 Review URL: https://codereview.chromium.org/81673002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236711 0039d316-1c4b-4281-b951-d872f2087c98 --- gin/modules/file_module_provider.cc | 33 +++++++++++++++++++-------------- gin/modules/file_module_provider.h | 6 ++++-- gin/modules/module_runner_delegate.cc | 5 +++-- gin/modules/module_runner_delegate.h | 3 ++- 4 files changed, 28 insertions(+), 19 deletions(-) (limited to 'gin/modules') diff --git a/gin/modules/file_module_provider.cc b/gin/modules/file_module_provider.cc index 0a3e24c..990dcd0 100644 --- a/gin/modules/file_module_provider.cc +++ b/gin/modules/file_module_provider.cc @@ -15,14 +15,15 @@ namespace gin { namespace { void AttempToLoadModule(const base::WeakPtr& runner, - const base::FilePath& base, + const std::vector& search_paths, const std::string& id) { if (!runner) return; std::vector components; base::SplitString(id, '/', &components); - base::FilePath path = base; + + base::FilePath path; for (size_t i = 0; i < components.size(); ++i) { // TODO(abarth): Technically the path components can be UTF-8. We don't // handle that case correctly yet. @@ -30,21 +31,25 @@ void AttempToLoadModule(const base::WeakPtr& runner, } path = path.AddExtension(FILE_PATH_LITERAL("js")); - std::string source; - if (!ReadFileToString(path, &source)) - return; + for (size_t i = 0; i < search_paths.size(); ++i) { + std::string source; + if (!ReadFileToString(search_paths[i].Append(path), &source)) + continue; - Runner::Scope scope(runner.get()); - v8::Handle script = v8::Script::New( - StringToV8(runner->isolate(), source), - StringToV8(runner->isolate(), id)); - runner->Run(script); + Runner::Scope scope(runner.get()); + v8::Handle script = v8::Script::New( + StringToV8(runner->isolate(), source), + StringToV8(runner->isolate(), id)); + runner->Run(script); + return; + } } } // namespace -FileModuleProvider::FileModuleProvider(const base::FilePath& base) - : base_(base) { +FileModuleProvider::FileModuleProvider( + const std::vector& search_paths) + : search_paths_(search_paths) { } FileModuleProvider::~FileModuleProvider() { @@ -59,8 +64,8 @@ void FileModuleProvider::AttempToLoadModules( if (attempted_ids_.count(id)) continue; attempted_ids_.insert(id); - base::MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(AttempToLoadModule, runner->GetWeakPtr(), base_, id)); + base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( + AttempToLoadModule, runner->GetWeakPtr(), search_paths_, id)); } } diff --git a/gin/modules/file_module_provider.h b/gin/modules/file_module_provider.h index 1e6fb88e..e82d8e3 100644 --- a/gin/modules/file_module_provider.h +++ b/gin/modules/file_module_provider.h @@ -7,6 +7,7 @@ #include #include +#include #include "base/files/file_path.h" #include "gin/runner.h" @@ -15,13 +16,14 @@ namespace gin { class FileModuleProvider { public: - explicit FileModuleProvider(const base::FilePath& base); + explicit FileModuleProvider( + const std::vector& search_paths); ~FileModuleProvider(); void AttempToLoadModules(Runner* runner, const std::set& ids); private: - base::FilePath base_; + std::vector search_paths_; std::set attempted_ids_; DISALLOW_COPY_AND_ASSIGN(FileModuleProvider); diff --git a/gin/modules/module_runner_delegate.cc b/gin/modules/module_runner_delegate.cc index 50b9f5e..3f2422c 100644 --- a/gin/modules/module_runner_delegate.cc +++ b/gin/modules/module_runner_delegate.cc @@ -8,8 +8,9 @@ namespace gin { -ModuleRunnerDelegate::ModuleRunnerDelegate(const base::FilePath& module_base) - : module_provider_(module_base) { +ModuleRunnerDelegate::ModuleRunnerDelegate( + const std::vector& search_paths) + : module_provider_(search_paths) { } ModuleRunnerDelegate::~ModuleRunnerDelegate() { diff --git a/gin/modules/module_runner_delegate.h b/gin/modules/module_runner_delegate.h index 4d821d2..a66695f 100644 --- a/gin/modules/module_runner_delegate.h +++ b/gin/modules/module_runner_delegate.h @@ -18,7 +18,8 @@ typedef v8::Local (*ModuleTemplateGetter)( class ModuleRunnerDelegate : public RunnerDelegate { public: - explicit ModuleRunnerDelegate(const base::FilePath& module_base); + explicit ModuleRunnerDelegate( + const std::vector& search_paths); virtual ~ModuleRunnerDelegate(); void AddBuiltinModule(const std::string& id, ModuleTemplateGetter templ); -- cgit v1.1