// 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(const base::WeakPtr& runner, const std::vector& search_paths, const std::string& id) { if (!runner) return; std::vector components; base::SplitString(id, '/', &components); 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. 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()); v8::Handle script = v8::Script::New( StringToV8(runner->isolate(), source), StringToV8(runner->isolate(), id)); runner->Run(script); return; } } } // namespace FileModuleProvider::FileModuleProvider( const std::vector& search_paths) : search_paths_(search_paths) { } FileModuleProvider::~FileModuleProvider() { } void FileModuleProvider::AttempToLoadModules( Runner* runner, const std::set& ids) { std::set modules = ids; for (std::set::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)); } } } // namespace gin