OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "gin/modules/file_module_provider.h" | 5 #include "gin/modules/file_module_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 #include "gin/converter.h" | 11 #include "gin/converter.h" |
12 | 12 |
13 namespace gin { | 13 namespace gin { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 void AttempToLoadModule(const base::WeakPtr<Runner>& runner, | 17 void AttemptToLoadModule(const base::WeakPtr<Runner>& runner, |
18 const std::vector<base::FilePath>& search_paths, | 18 const std::vector<base::FilePath>& search_paths, |
19 const std::string& id) { | 19 const std::string& id, |
20 if (!runner) | 20 base::Callback<void(bool)> callback) { |
21 if (!runner) { | |
22 callback.Run(false); | |
21 return; | 23 return; |
24 } | |
22 | 25 |
23 std::vector<std::string> components; | 26 std::vector<std::string> components; |
24 base::SplitString(id, '/', &components); | 27 base::SplitString(id, '/', &components); |
25 | 28 |
26 base::FilePath path; | 29 base::FilePath path; |
27 for (size_t i = 0; i < components.size(); ++i) { | 30 for (size_t i = 0; i < components.size(); ++i) { |
28 // TODO(abarth): Technically the path components can be UTF-8. We don't | 31 // TODO(abarth): Technically the path components can be UTF-8. We don't |
29 // handle that case correctly yet. | 32 // handle that case correctly yet. |
30 path = path.AppendASCII(components[i]); | 33 path = path.AppendASCII(components[i]); |
31 } | 34 } |
32 path = path.AddExtension(FILE_PATH_LITERAL("js")); | 35 path = path.AddExtension(FILE_PATH_LITERAL("js")); |
33 | 36 |
34 for (size_t i = 0; i < search_paths.size(); ++i) { | 37 for (size_t i = 0; i < search_paths.size(); ++i) { |
35 std::string source; | 38 std::string source; |
36 if (!ReadFileToString(search_paths[i].Append(path), &source)) | 39 if (!ReadFileToString(search_paths[i].Append(path), &source)) |
37 continue; | 40 continue; |
38 | 41 |
39 Runner::Scope scope(runner.get()); | 42 Runner::Scope scope(runner.get()); |
40 runner->Run(source, id); | 43 runner->Run(source, id); |
44 callback.Run(true); | |
41 return; | 45 return; |
42 } | 46 } |
47 callback.Run(false); | |
abarth-chromium
2014/07/22 22:16:15
Maybe we should just log something at this point a
Matt Perry
2014/07/22 22:24:40
Huh.. I thought I tried that and it resulted in fa
| |
43 } | 48 } |
44 | 49 |
45 } // namespace | 50 } // namespace |
46 | 51 |
47 FileModuleProvider::FileModuleProvider( | 52 FileModuleProvider::FileModuleProvider( |
48 const std::vector<base::FilePath>& search_paths) | 53 const std::vector<base::FilePath>& search_paths) |
49 : search_paths_(search_paths) { | 54 : weak_factory_(this), search_paths_(search_paths) { |
50 } | 55 } |
51 | 56 |
52 FileModuleProvider::~FileModuleProvider() { | 57 FileModuleProvider::~FileModuleProvider() { |
53 } | 58 } |
54 | 59 |
55 void FileModuleProvider::AttempToLoadModules( | 60 void FileModuleProvider::AttemptToLoadModules( |
56 Runner* runner, const std::set<std::string>& ids) { | 61 Runner* runner, const std::set<std::string>& ids) { |
57 std::set<std::string> modules = ids; | 62 std::set<std::string> modules = ids; |
58 for (std::set<std::string>::const_iterator it = modules.begin(); | 63 for (std::set<std::string>::const_iterator it = modules.begin(); |
59 it != modules.end(); ++it) { | 64 it != modules.end(); ++it) { |
60 const std::string& id = *it; | 65 const std::string& id = *it; |
61 if (attempted_ids_.count(id)) | 66 if (attempted_ids_.count(id)) |
62 continue; | 67 continue; |
63 attempted_ids_.insert(id); | 68 attempted_ids_.insert(id); |
64 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 69 base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
65 AttempToLoadModule, runner->GetWeakPtr(), search_paths_, id)); | 70 AttemptToLoadModule, runner->GetWeakPtr(), search_paths_, id, |
71 base::Bind(&FileModuleProvider::OnReadCompleted, | |
72 weak_factory_.GetWeakPtr(), id))); | |
66 } | 73 } |
67 } | 74 } |
68 | 75 |
76 void FileModuleProvider::OnReadCompleted(const std::string& id, bool success) { | |
77 if (success) { | |
78 successful_ids_.insert(id); | |
79 } else { | |
80 failed_ids_.insert(id); | |
81 } | |
82 | |
83 if (failed_ids_.size() + successful_ids_.size() == attempted_ids_.size()) { | |
84 if (!completion_callback_.is_null()) | |
85 completion_callback_.Run(successful_ids_); | |
86 } | |
87 } | |
88 | |
69 } // namespace gin | 89 } // namespace gin |
OLD | NEW |