OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "services/catalog/public/cpp/resource_loader.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/files/file.h" | |
12 #include "components/filesystem/public/interfaces/directory.mojom.h" | |
13 #include "mojo/platform_handle/platform_handle_functions.h" | |
14 #include "services/shell/public/cpp/connector.h" | |
15 #include "services/shell/public/interfaces/connector.mojom.h" | |
16 #include "services/shell/public/interfaces/interface_provider.mojom.h" | |
17 | |
18 namespace catalog { | |
19 namespace { | |
20 base::File GetFileFromHandle(mojo::ScopedHandle handle) { | |
21 CHECK(handle.is_valid()); | |
22 MojoPlatformHandle platform_handle; | |
23 CHECK(MojoExtractPlatformHandle(handle.release().value(), | |
24 &platform_handle) == MOJO_RESULT_OK); | |
25 return base::File(platform_handle); | |
26 } | |
27 } | |
28 | |
29 ResourceLoader::ResourceLoader(shell::Connector* connector, | |
sky
2016/05/03 04:13:23
This is a lot of expensive operations for the cons
| |
30 const std::set<std::string>& paths) { | |
31 filesystem::DirectoryPtr directory; | |
32 connector->ConnectToInterface("mojo:catalog", &directory); | |
33 | |
34 mojo::Array<filesystem::FileOpenDetailsPtr> deets( | |
sky
2016/05/03 04:13:23
deets? You sure are going for obscure names lately
| |
35 mojo::Array<filesystem::FileOpenDetailsPtr>::New(paths.size())); | |
36 size_t i = 0; | |
37 for (const auto& path : paths) { | |
38 filesystem::FileOpenDetailsPtr open_details( | |
39 filesystem::FileOpenDetails::New()); | |
40 open_details->path = path; | |
41 open_details->open_flags = filesystem::kFlagOpen | filesystem::kFlagRead; | |
42 deets[i++] = std::move(open_details); | |
43 } | |
44 | |
45 mojo::Array<filesystem::FileOpenResultPtr> results( | |
46 mojo::Array<filesystem::FileOpenResultPtr>::New(paths.size())); | |
47 directory->OpenFileHandles(std::move(deets), &results); | |
sky
2016/05/03 04:13:23
You need to check the return value. I don't think
| |
48 | |
49 CHECK_EQ(results.size(), paths.size()); | |
50 for (const auto& result : results) { | |
51 resource_map_[result->path].reset( | |
52 new base::File(GetFileFromHandle(std::move(result->file_handle)))); | |
53 } | |
54 } | |
55 | |
56 ResourceLoader::~ResourceLoader() { | |
57 } | |
58 | |
59 base::File ResourceLoader::TakeFile(const std::string& path) { | |
60 CHECK(resource_map_.count(path)); | |
61 std::unique_ptr<base::File> file_wrapper(std::move(resource_map_[path])); | |
62 resource_map_.erase(path); | |
63 return std::move(*file_wrapper); | |
64 } | |
65 | |
66 } // namespace catalog | |
OLD | NEW |