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 #include "base/bind.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_split.h" |
| 11 #include "gin/converter.h" |
| 12 |
| 13 namespace gin { |
| 14 |
| 15 namespace { |
| 16 |
| 17 void AttempToLoadModule(const base::WeakPtr<Runner>& runner, |
| 18 const base::FilePath& base, |
| 19 const std::string& id) { |
| 20 if (!runner) |
| 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 // TODO(abarth): Technically the path components can be UTF-8. We don't |
| 28 // handle that case correctly yet. |
| 29 path = path.AppendASCII(components[i]); |
| 30 } |
| 31 path = path.AddExtension(FILE_PATH_LITERAL("js")); |
| 32 |
| 33 std::string source; |
| 34 if (!ReadFileToString(path, &source)) |
| 35 return; |
| 36 |
| 37 Runner::Scope scope(runner.get()); |
| 38 v8::Handle<v8::Script> script = v8::Script::New( |
| 39 StringToV8(runner->isolate(), source), |
| 40 StringToV8(runner->isolate(), id)); |
| 41 runner->Run(script); |
| 42 } |
| 43 |
| 44 } // namespace |
| 45 |
| 46 FileModuleProvider::FileModuleProvider(const base::FilePath& base) |
| 47 : base_(base) { |
| 48 } |
| 49 |
| 50 FileModuleProvider::~FileModuleProvider() { |
| 51 } |
| 52 |
| 53 void FileModuleProvider::AttempToLoadModules( |
| 54 Runner* runner, const std::set<std::string>& ids) { |
| 55 std::set<std::string> modules = ids; |
| 56 for (std::set<std::string>::const_iterator it = modules.begin(); |
| 57 it != modules.end(); ++it) { |
| 58 const std::string& id = *it; |
| 59 if (attempted_ids_.count(id)) |
| 60 continue; |
| 61 attempted_ids_.insert(id); |
| 62 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 63 base::Bind(AttempToLoadModule, runner->GetWeakPtr(), base_, id)); |
| 64 } |
| 65 } |
| 66 |
| 67 } // namespace gin |
OLD | NEW |