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() {} |
| 30 ResourceLoader::~ResourceLoader() {} |
| 31 |
| 32 bool ResourceLoader::OpenFiles(filesystem::DirectoryPtr directory, |
| 33 const std::set<std::string>& paths) { |
| 34 mojo::Array<filesystem::FileOpenDetailsPtr> details( |
| 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 details[i++] = std::move(open_details); |
| 43 } |
| 44 |
| 45 mojo::Array<filesystem::FileOpenResultPtr> results( |
| 46 mojo::Array<filesystem::FileOpenResultPtr>::New(paths.size())); |
| 47 if (!directory->OpenFileHandles(std::move(details), &results)) |
| 48 return false; |
| 49 |
| 50 for (const auto& result : results) { |
| 51 resource_map_[result->path].reset( |
| 52 new base::File(GetFileFromHandle(std::move(result->file_handle)))); |
| 53 } |
| 54 return true; |
| 55 } |
| 56 |
| 57 base::File ResourceLoader::TakeFile(const std::string& path) { |
| 58 std::unique_ptr<base::File> file_wrapper(std::move(resource_map_[path])); |
| 59 resource_map_.erase(path); |
| 60 return std::move(*file_wrapper); |
| 61 } |
| 62 |
| 63 } // namespace catalog |
OLD | NEW |