| Index: services/catalog/public/cpp/resource_loader.cc
|
| diff --git a/services/catalog/public/cpp/resource_loader.cc b/services/catalog/public/cpp/resource_loader.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..dc7fdc202df1662537efb75919a2e08089855a55
|
| --- /dev/null
|
| +++ b/services/catalog/public/cpp/resource_loader.cc
|
| @@ -0,0 +1,62 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "services/catalog/public/cpp/resource_loader.h"
|
| +
|
| +#include <stddef.h>
|
| +#include <utility>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/files/file.h"
|
| +#include "components/filesystem/public/interfaces/directory.mojom.h"
|
| +#include "mojo/platform_handle/platform_handle_functions.h"
|
| +#include "services/shell/public/cpp/connector.h"
|
| +#include "services/shell/public/interfaces/interface_provider.mojom.h"
|
| +
|
| +namespace catalog {
|
| +namespace {
|
| +base::File GetFileFromHandle(mojo::ScopedHandle handle) {
|
| + CHECK(handle.is_valid());
|
| + MojoPlatformHandle platform_handle;
|
| + CHECK(MojoExtractPlatformHandle(handle.release().value(),
|
| + &platform_handle) == MOJO_RESULT_OK);
|
| + return base::File(platform_handle);
|
| +}
|
| +}
|
| +
|
| +ResourceLoader::ResourceLoader() {}
|
| +ResourceLoader::~ResourceLoader() {}
|
| +
|
| +bool ResourceLoader::OpenFiles(filesystem::DirectoryPtr directory,
|
| + const std::set<std::string>& paths) {
|
| + mojo::Array<filesystem::FileOpenDetailsPtr> details(
|
| + mojo::Array<filesystem::FileOpenDetailsPtr>::New(paths.size()));
|
| + size_t i = 0;
|
| + for (const auto& path : paths) {
|
| + filesystem::FileOpenDetailsPtr open_details(
|
| + filesystem::FileOpenDetails::New());
|
| + open_details->path = path;
|
| + open_details->open_flags = filesystem::kFlagOpen | filesystem::kFlagRead;
|
| + details[i++] = std::move(open_details);
|
| + }
|
| +
|
| + mojo::Array<filesystem::FileOpenResultPtr> results(
|
| + mojo::Array<filesystem::FileOpenResultPtr>::New(paths.size()));
|
| + if (!directory->OpenFileHandles(std::move(details), &results))
|
| + return false;
|
| +
|
| + for (const auto& result : results) {
|
| + resource_map_[result->path].reset(
|
| + new base::File(GetFileFromHandle(std::move(result->file_handle))));
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +base::File ResourceLoader::TakeFile(const std::string& path) {
|
| + std::unique_ptr<base::File> file_wrapper(std::move(resource_map_[path]));
|
| + resource_map_.erase(path);
|
| + return std::move(*file_wrapper);
|
| +}
|
| +
|
| +} // namespace catalog
|
|
|