Chromium Code Reviews| Index: gin/modules/file_module_provider.cc |
| diff --git a/gin/modules/file_module_provider.cc b/gin/modules/file_module_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6e62d5a357e8e61757e87447971cfaa7a6154f2f |
| --- /dev/null |
| +++ b/gin/modules/file_module_provider.cc |
| @@ -0,0 +1,65 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "gin/modules/file_module_provider.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/file_util.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/strings/string_split.h" |
| +#include "gin/converter.h" |
| + |
| +namespace gin { |
| + |
| +namespace { |
| + |
| +void AttempToLoadModule(base::WeakPtr<Runner> runner, |
|
jochen (gone - plz use gerrit)
2013/11/18 12:46:05
should all these parameters be const references?
abarth-chromium
2013/11/18 15:33:05
Done.
|
| + base::FilePath base, |
| + std::string id) { |
| + if (!runner.get()) |
|
jochen (gone - plz use gerrit)
2013/11/18 12:46:05
no get() needed
abarth-chromium
2013/11/18 15:33:05
Done.
|
| + return; |
| + |
| + std::vector<std::string> components; |
| + base::SplitString(id, '/', &components); |
| + base::FilePath path = base; |
| + for (size_t i = 0; i < components.size(); ++i) { |
|
jochen (gone - plz use gerrit)
2013/11/18 12:46:05
no { } needed
|
| + path = path.AppendASCII(components[i]); |
|
jochen (gone - plz use gerrit)
2013/11/18 12:46:05
the id is actually utf8, isn't it?
abarth-chromium
2013/11/18 15:33:05
I've added a TODO about this issue.
|
| + } |
| + path = path.AddExtension(FILE_PATH_LITERAL("js")); |
| + |
| + std::string source; |
| + if (!ReadFileToString(path, &source)) |
| + return; |
| + |
| + Runner::Scope scope(runner.get()); |
| + v8::Handle<v8::Script> script = v8::Script::New( |
| + StringToV8(runner->isolate(), source), |
| + StringToV8(runner->isolate(), id)); |
| + runner->Run(script); |
| +} |
| + |
| +} |
|
jochen (gone - plz use gerrit)
2013/11/18 12:46:05
add // namespace
abarth-chromium
2013/11/18 15:33:05
Done.
|
| + |
| +FileModuleProvider::FileModuleProvider(const base::FilePath& base) |
| + : base_(base) { |
| +} |
| + |
| +FileModuleProvider::~FileModuleProvider() { |
| +} |
| + |
| +void FileModuleProvider::AttempToLoadModules( |
| + Runner* runner, const std::set<std::string>& ids) { |
| + std::set<std::string> modules = ids; |
| + for (std::set<std::string>::const_iterator it = modules.begin(); |
| + it != modules.end(); ++it) { |
| + const std::string& id = *it; |
| + if (attempted_ids_.count(id)) |
| + continue; |
| + attempted_ids_.insert(id); |
| + base::MessageLoop::current()->PostTask(FROM_HERE, |
| + base::Bind(AttempToLoadModule, runner->GetWeakPtr(), base_, id)); |
| + } |
| +} |
| + |
| +} // namespace gin |