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.
5 #include "gin/modules/file_module_provider.h"
8 #include "base/files/file_util.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_split.h"
11 #include "gin/converter.h"
17 void AttempToLoadModule(const base::WeakPtr
<Runner
>& runner
,
18 const std::vector
<base::FilePath
>& search_paths
,
19 const std::string
& id
) {
23 std::vector
<std::string
> components
= base::SplitString(
24 id
, "/", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
27 for (size_t i
= 0; i
< components
.size(); ++i
) {
28 // TODO(abarth): Technically the path components can be UTF-8. We don't
29 // handle that case correctly yet.
30 path
= path
.AppendASCII(components
[i
]);
32 path
= path
.AddExtension(FILE_PATH_LITERAL("js"));
34 for (size_t i
= 0; i
< search_paths
.size(); ++i
) {
36 if (!ReadFileToString(search_paths
[i
].Append(path
), &source
))
39 Runner::Scope
scope(runner
.get());
40 runner
->Run(source
, id
);
43 LOG(ERROR
) << "Failed to load module from disk: " << id
;
48 FileModuleProvider::FileModuleProvider(
49 const std::vector
<base::FilePath
>& search_paths
)
50 : search_paths_(search_paths
) {
53 FileModuleProvider::~FileModuleProvider() {
56 void FileModuleProvider::AttempToLoadModules(
57 Runner
* runner
, const std::set
<std::string
>& ids
) {
58 std::set
<std::string
> modules
= ids
;
59 for (std::set
<std::string
>::const_iterator it
= modules
.begin();
60 it
!= modules
.end(); ++it
) {
61 const std::string
& id
= *it
;
62 if (attempted_ids_
.count(id
))
64 attempted_ids_
.insert(id
);
65 base::MessageLoop::current()->PostTask(FROM_HERE
, base::Bind(
66 AttempToLoadModule
, runner
->GetWeakPtr(), search_paths_
, id
));