| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/resource_provider/public/cpp/resource_loader.h" | 5 #include "components/resource_provider/public/cpp/resource_loader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/files/file.h" | 11 #include "base/files/file.h" |
| 11 #include "mojo/application/public/cpp/application_impl.h" | 12 #include "mojo/application/public/cpp/application_impl.h" |
| 12 #include "mojo/application/public/cpp/connect.h" | 13 #include "mojo/application/public/cpp/connect.h" |
| 13 #include "mojo/application/public/interfaces/service_provider.mojom.h" | 14 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
| 14 #include "mojo/application/public/interfaces/shell.mojom.h" | 15 #include "mojo/application/public/interfaces/shell.mojom.h" |
| 15 #include "mojo/common/common_type_converters.h" | 16 #include "mojo/common/common_type_converters.h" |
| 16 #include "mojo/platform_handle/platform_handle_functions.h" | 17 #include "mojo/platform_handle/platform_handle_functions.h" |
| 17 | 18 |
| 18 namespace resource_provider { | 19 namespace resource_provider { |
| 19 namespace { | 20 namespace { |
| 20 base::File GetFileFromHandle(mojo::ScopedHandle handle) { | 21 base::File GetFileFromHandle(mojo::ScopedHandle handle) { |
| 21 CHECK(handle.is_valid()); | 22 CHECK(handle.is_valid()); |
| 22 MojoPlatformHandle platform_handle; | 23 MojoPlatformHandle platform_handle; |
| 23 CHECK(MojoExtractPlatformHandle(handle.release().value(), | 24 CHECK(MojoExtractPlatformHandle(handle.release().value(), |
| 24 &platform_handle) == MOJO_RESULT_OK); | 25 &platform_handle) == MOJO_RESULT_OK); |
| 25 return base::File(platform_handle).Pass(); | 26 return base::File(platform_handle); |
| 26 } | 27 } |
| 27 } | 28 } |
| 28 | 29 |
| 29 ResourceLoader::ResourceLoader(mojo::ApplicationImpl* app, | 30 ResourceLoader::ResourceLoader(mojo::ApplicationImpl* app, |
| 30 const std::set<std::string>& paths) | 31 const std::set<std::string>& paths) |
| 31 : loaded_(false), did_block_(false) { | 32 : loaded_(false), did_block_(false) { |
| 32 app->ConnectToService("mojo:resource_provider", &resource_provider_); | 33 app->ConnectToService("mojo:resource_provider", &resource_provider_); |
| 33 std::vector<std::string> paths_vector(paths.begin(), paths.end()); | 34 std::vector<std::string> paths_vector(paths.begin(), paths.end()); |
| 34 resource_provider_->GetResources( | 35 resource_provider_->GetResources( |
| 35 mojo::Array<mojo::String>::From(paths_vector), | 36 mojo::Array<mojo::String>::From(paths_vector), |
| 36 base::Bind(&ResourceLoader::OnGotResources, base::Unretained(this), | 37 base::Bind(&ResourceLoader::OnGotResources, base::Unretained(this), |
| 37 paths_vector)); | 38 paths_vector)); |
| 38 } | 39 } |
| 39 | 40 |
| 40 ResourceLoader::~ResourceLoader() { | 41 ResourceLoader::~ResourceLoader() { |
| 41 } | 42 } |
| 42 | 43 |
| 43 bool ResourceLoader::BlockUntilLoaded() { | 44 bool ResourceLoader::BlockUntilLoaded() { |
| 44 if (did_block_ || loaded_) | 45 if (did_block_ || loaded_) |
| 45 return loaded_; | 46 return loaded_; |
| 46 | 47 |
| 47 did_block_ = true; | 48 did_block_ = true; |
| 48 resource_provider_.WaitForIncomingResponse(); | 49 resource_provider_.WaitForIncomingResponse(); |
| 49 return loaded_; | 50 return loaded_; |
| 50 } | 51 } |
| 51 | 52 |
| 52 base::File ResourceLoader::ReleaseFile(const std::string& path) { | 53 base::File ResourceLoader::ReleaseFile(const std::string& path) { |
| 53 CHECK(resource_map_.count(path)); | 54 CHECK(resource_map_.count(path)); |
| 54 scoped_ptr<base::File> file_wrapper(resource_map_[path].Pass()); | 55 scoped_ptr<base::File> file_wrapper(std::move(resource_map_[path])); |
| 55 resource_map_.erase(path); | 56 resource_map_.erase(path); |
| 56 return file_wrapper->Pass(); | 57 return std::move(*file_wrapper); |
| 57 } | 58 } |
| 58 | 59 |
| 59 void ResourceLoader::OnGotResources(const std::vector<std::string>& paths, | 60 void ResourceLoader::OnGotResources(const std::vector<std::string>& paths, |
| 60 mojo::Array<mojo::ScopedHandle> resources) { | 61 mojo::Array<mojo::ScopedHandle> resources) { |
| 61 | 62 |
| 62 CHECK(!resources.is_null()); | 63 CHECK(!resources.is_null()); |
| 63 CHECK_EQ(resources.size(), paths.size()); | 64 CHECK_EQ(resources.size(), paths.size()); |
| 64 for (size_t i = 0; i < resources.size(); ++i) { | 65 for (size_t i = 0; i < resources.size(); ++i) { |
| 65 resource_map_[paths[i]].reset( | 66 resource_map_[paths[i]].reset( |
| 66 new base::File(GetFileFromHandle(resources[i].Pass()))); | 67 new base::File(GetFileFromHandle(std::move(resources[i])))); |
| 67 } | 68 } |
| 68 loaded_ = true; | 69 loaded_ = true; |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // namespace resource_provider | 72 } // namespace resource_provider |
| OLD | NEW |