| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "services/catalog/public/cpp/resource_loader.h" | 5 #include "services/catalog/public/cpp/resource_loader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 12 #include "components/filesystem/public/interfaces/directory.mojom.h" | 12 #include "components/filesystem/public/interfaces/directory.mojom.h" |
| 13 #include "mojo/public/cpp/system/platform_handle.h" | 13 #include "mojo/public/cpp/system/platform_handle.h" |
| 14 #include "services/service_manager/public/cpp/connector.h" | 14 #include "services/service_manager/public/cpp/connector.h" |
| 15 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" | 15 #include "services/service_manager/public/interfaces/interface_provider.mojom.h" |
| 16 | 16 |
| 17 namespace catalog { | 17 namespace catalog { |
| 18 | 18 |
| 19 namespace { | |
| 20 | |
| 21 base::File GetFileFromHandle(mojo::ScopedHandle handle) { | |
| 22 CHECK(handle.is_valid()); | |
| 23 base::PlatformFile platform_file; | |
| 24 CHECK_EQ(mojo::UnwrapPlatformFile(std::move(handle), &platform_file), | |
| 25 MOJO_RESULT_OK); | |
| 26 return base::File(platform_file); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 ResourceLoader::ResourceLoader() {} | 19 ResourceLoader::ResourceLoader() {} |
| 32 ResourceLoader::~ResourceLoader() {} | 20 ResourceLoader::~ResourceLoader() {} |
| 33 | 21 |
| 34 bool ResourceLoader::OpenFiles(filesystem::mojom::DirectoryPtr directory, | 22 bool ResourceLoader::OpenFiles(filesystem::mojom::DirectoryPtr directory, |
| 35 const std::set<std::string>& paths) { | 23 const std::set<std::string>& paths) { |
| 36 std::vector<filesystem::mojom::FileOpenDetailsPtr> details(paths.size()); | 24 std::vector<filesystem::mojom::FileOpenDetailsPtr> details(paths.size()); |
| 37 size_t i = 0; | 25 size_t i = 0; |
| 38 for (const auto& path : paths) { | 26 for (const auto& path : paths) { |
| 39 filesystem::mojom::FileOpenDetailsPtr open_details( | 27 filesystem::mojom::FileOpenDetailsPtr open_details( |
| 40 filesystem::mojom::FileOpenDetails::New()); | 28 filesystem::mojom::FileOpenDetails::New()); |
| 41 open_details->path = path; | 29 open_details->path = path; |
| 42 open_details->open_flags = | 30 open_details->open_flags = |
| 43 filesystem::mojom::kFlagOpen | filesystem::mojom::kFlagRead; | 31 filesystem::mojom::kFlagOpen | filesystem::mojom::kFlagRead; |
| 44 details[i++] = std::move(open_details); | 32 details[i++] = std::move(open_details); |
| 45 } | 33 } |
| 46 | 34 |
| 47 std::vector<filesystem::mojom::FileOpenResultPtr> results; | 35 std::vector<filesystem::mojom::FileOpenResultPtr> results; |
| 48 if (!directory->OpenFileHandles(std::move(details), &results)) | 36 if (!directory->OpenFileHandles(std::move(details), &results)) |
| 49 return false; | 37 return false; |
| 50 | 38 |
| 51 for (const auto& result : results) { | 39 for (const auto& result : results) { |
| 52 resource_map_[result->path].reset( | 40 resource_map_[result->path].reset( |
| 53 new base::File(GetFileFromHandle(std::move(result->file_handle)))); | 41 new base::File(std::move(result->file_handle))); |
| 54 } | 42 } |
| 55 return true; | 43 return true; |
| 56 } | 44 } |
| 57 | 45 |
| 58 base::File ResourceLoader::TakeFile(const std::string& path) { | 46 base::File ResourceLoader::TakeFile(const std::string& path) { |
| 59 std::unique_ptr<base::File> file_wrapper(std::move(resource_map_[path])); | 47 std::unique_ptr<base::File> file_wrapper(std::move(resource_map_[path])); |
| 60 resource_map_.erase(path); | 48 resource_map_.erase(path); |
| 61 return std::move(*file_wrapper); | 49 return std::move(*file_wrapper); |
| 62 } | 50 } |
| 63 | 51 |
| 64 } // namespace catalog | 52 } // namespace catalog |
| OLD | NEW |