OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "gin/modules/file_module_provider.h" |
| 6 |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_split.h" |
| 12 #include "gin/converter.h" |
| 13 namespace gin { |
| 14 |
| 15 namespace { |
| 16 |
| 17 void AttempToLoadModule(base::WeakPtr<Runner> runner, |
| 18 base::FilePath base, |
| 19 std::string id) { |
| 20 if (!runner.get()) |
| 21 return; |
| 22 |
| 23 std::vector<std::string> components; |
| 24 base::SplitString(id, '/', &components); |
| 25 base::FilePath path = base; |
| 26 for (size_t i = 0; i < components.size(); ++i) { |
| 27 path = path.AppendASCII(components[i]); |
| 28 } |
| 29 path = path.AddExtension(FILE_PATH_LITERAL("js")); |
| 30 |
| 31 std::string source; |
| 32 if (!ReadFileToString(path, &source)) |
| 33 return; |
| 34 |
| 35 Runner::Scope scope(runner.get()); |
| 36 v8::Handle<v8::Script> script = v8::Script::New( |
| 37 StringToV8(runner->isolate(), source), StringToV8(runner->isolate(), id)); |
| 38 runner->Run(script); |
| 39 } |
| 40 |
| 41 } |
| 42 |
| 43 FileModuleProvider::FileModuleProvider(const base::FilePath& base) |
| 44 : base_(base) { |
| 45 } |
| 46 |
| 47 FileModuleProvider::~FileModuleProvider() { |
| 48 } |
| 49 |
| 50 void FileModuleProvider::AttempToLoadModules( |
| 51 Runner* runner, const std::set<std::string>& ids) { |
| 52 std::set<std::string> modules = ids; |
| 53 for (std::set<std::string>::const_iterator it = modules.begin(); |
| 54 it != modules.end(); ++it) { |
| 55 const std::string& id = *it; |
| 56 if (attempted_ids_.count(id)) |
| 57 continue; |
| 58 attempted_ids_.insert(id); |
| 59 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 60 base::Bind(AttempToLoadModule, runner->GetWeakPtr(), base_, id)); |
| 61 } |
| 62 } |
| 63 |
| 64 } // namespace gin |
OLD | NEW |