This source file includes following definitions.
- AttempToLoadModule
- AttempToLoadModules
#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(const base::WeakPtr<Runner>& runner,
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;
for (size_t i = 0; i < components.size(); ++i) {
path = path.AppendASCII(components[i]);
}
path = path.AddExtension(FILE_PATH_LITERAL("js"));
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());
runner->Run(source, id);
return;
}
}
}
FileModuleProvider::FileModuleProvider(
const std::vector<base::FilePath>& search_paths)
: search_paths_(search_paths) {
}
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(), search_paths_, id));
}
}
}