summaryrefslogtreecommitdiffstats
path: root/gin
diff options
context:
space:
mode:
authorabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-22 07:30:55 +0000
committerabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-22 07:30:55 +0000
commit467834b30f40d1aff0031fbb7660fb211c8112e4 (patch)
tree8e1f09b420bfda00a238ed6613642ac656720d2e /gin
parent3ac7e07966540fe5b41bcc60ef14ff6b18fe6057 (diff)
downloadchromium_src-467834b30f40d1aff0031fbb7660fb211c8112e4.zip
chromium_src-467834b30f40d1aff0031fbb7660fb211c8112e4.tar.gz
chromium_src-467834b30f40d1aff0031fbb7660fb211c8112e4.tar.bz2
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
Diffstat (limited to 'gin')
-rw-r--r--gin/modules/file_module_provider.cc33
-rw-r--r--gin/modules/file_module_provider.h6
-rw-r--r--gin/modules/module_runner_delegate.cc5
-rw-r--r--gin/modules/module_runner_delegate.h3
-rw-r--r--gin/shell/gin_main.cc8
-rw-r--r--gin/test/file_runner.cc12
6 files changed, 39 insertions, 28 deletions
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>& runner,
- const base::FilePath& base,
+ const std::vector<base::FilePath>& search_paths,
const std::string& id) {
if (!runner)
return;
std::vector<std::string> 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>& 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<v8::Script> script = v8::Script::New(
- StringToV8(runner->isolate(), source),
- StringToV8(runner->isolate(), id));
- runner->Run(script);
+ Runner::Scope scope(runner.get());
+ v8::Handle<v8::Script> 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<base::FilePath>& 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 <set>
#include <string>
+#include <vector>
#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<base::FilePath>& search_paths);
~FileModuleProvider();
void AttempToLoadModules(Runner* runner, const std::set<std::string>& ids);
private:
- base::FilePath base_;
+ std::vector<base::FilePath> search_paths_;
std::set<std::string> 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<base::FilePath>& 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<v8::ObjectTemplate> (*ModuleTemplateGetter)(
class ModuleRunnerDelegate : public RunnerDelegate {
public:
- explicit ModuleRunnerDelegate(const base::FilePath& module_base);
+ explicit ModuleRunnerDelegate(
+ const std::vector<base::FilePath>& search_paths);
virtual ~ModuleRunnerDelegate();
void AddBuiltinModule(const std::string& id, ModuleTemplateGetter templ);
diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc
index e1d95bb..4e18d71 100644
--- a/gin/shell/gin_main.cc
+++ b/gin/shell/gin_main.cc
@@ -31,15 +31,15 @@ void Run(base::WeakPtr<Runner> runner, const std::string& source) {
runner->Run(source);
}
-base::FilePath GetModuleBase() {
- base::FilePath module_base;
- CHECK(file_util::GetCurrentDirectory(&module_base));
+std::vector<base::FilePath> GetModuleSearchPaths() {
+ std::vector<base::FilePath> module_base(1);
+ CHECK(file_util::GetCurrentDirectory(&module_base[0]));
return module_base;
}
class ShellRunnerDelegate : public ModuleRunnerDelegate {
public:
- ShellRunnerDelegate() : ModuleRunnerDelegate(GetModuleBase()) {
+ ShellRunnerDelegate() : ModuleRunnerDelegate(GetModuleSearchPaths()) {
AddBuiltinModule(Console::kModuleName, Console::GetTemplate);
}
diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc
index e26d1d1..ff5ac5d 100644
--- a/gin/test/file_runner.cc
+++ b/gin/test/file_runner.cc
@@ -19,16 +19,18 @@ namespace gin {
namespace {
-base::FilePath GetModuleBase() {
- base::FilePath path;
- PathService::Get(base::DIR_SOURCE_ROOT, &path);
- return path;
+std::vector<base::FilePath> GetModuleSearchPaths() {
+ std::vector<base::FilePath> search_paths(2);
+ PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
+ PathService::Get(base::DIR_EXE, &search_paths[1]);
+ search_paths[1] = search_paths[1].AppendASCII("gen");
+ return search_paths;
}
} // namespace
FileRunnerDelegate::FileRunnerDelegate()
- : ModuleRunnerDelegate(GetModuleBase()) {
+ : ModuleRunnerDelegate(GetModuleSearchPaths()) {
AddBuiltinModule(Console::kModuleName, Console::GetTemplate);
AddBuiltinModule(GTest::kModuleName, GTest::GetTemplate);
}