| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file.h" |
| 8 #include "mojo/common/common_type_converters.h" | 9 #include "mojo/common/common_type_converters.h" |
| 9 #include "mojo/platform_handle/platform_handle_functions.h" | 10 #include "mojo/platform_handle/platform_handle_functions.h" |
| 10 #include "third_party/mojo/src/mojo/public/cpp/application/connect.h" | 11 #include "third_party/mojo/src/mojo/public/cpp/application/connect.h" |
| 11 #include "third_party/mojo/src/mojo/public/interfaces/application/shell.mojom.h" | 12 #include "third_party/mojo/src/mojo/public/interfaces/application/shell.mojom.h" |
| 12 | 13 |
| 13 namespace resource_provider { | 14 namespace resource_provider { |
| 14 | 15 |
| 15 ResourceLoader::ResourceLoader(mojo::Shell* shell, | 16 ResourceLoader::ResourceLoader(mojo::Shell* shell, |
| 16 const std::set<std::string>& paths) | 17 const std::set<std::string>& paths) |
| 17 : loaded_(false) { | 18 : loaded_(false), did_block_(false) { |
| 18 shell->ConnectToApplication("mojo:resource_provider", | 19 shell->ConnectToApplication("mojo:resource_provider", |
| 19 GetProxy(&resource_provider_service_provider_), | 20 GetProxy(&resource_provider_service_provider_), |
| 20 nullptr); | 21 nullptr); |
| 21 mojo::ConnectToService(resource_provider_service_provider_.get(), | 22 mojo::ConnectToService(resource_provider_service_provider_.get(), |
| 22 &resource_provider_); | 23 &resource_provider_); |
| 23 std::vector<std::string> paths_vector(paths.begin(), paths.end()); | 24 std::vector<std::string> paths_vector(paths.begin(), paths.end()); |
| 24 resource_provider_->GetResources( | 25 resource_provider_->GetResources( |
| 25 mojo::Array<mojo::String>::From(paths_vector), | 26 mojo::Array<mojo::String>::From(paths_vector), |
| 26 base::Bind(&ResourceLoader::OnGotResources, base::Unretained(this), | 27 base::Bind(&ResourceLoader::OnGotResources, base::Unretained(this), |
| 27 paths_vector)); | 28 paths_vector)); |
| 28 } | 29 } |
| 29 | 30 |
| 30 ResourceLoader::~ResourceLoader() { | 31 ResourceLoader::~ResourceLoader() { |
| 31 } | 32 } |
| 32 | 33 |
| 33 bool ResourceLoader::BlockUntilLoaded() { | 34 bool ResourceLoader::BlockUntilLoaded() { |
| 34 CHECK(!loaded_); | 35 if (did_block_) |
| 36 return loaded_; |
| 37 |
| 38 did_block_ = true; |
| 35 resource_provider_.WaitForIncomingMethodCall(); | 39 resource_provider_.WaitForIncomingMethodCall(); |
| 36 return loaded_; | 40 return loaded_; |
| 37 } | 41 } |
| 38 | 42 |
| 43 base::File ResourceLoader::ReleaseFile(const std::string& path) { |
| 44 CHECK(resource_map_.count(path)); |
| 45 scoped_ptr<base::File> file_wrapper(resource_map_[path].Pass()); |
| 46 resource_map_.erase(path); |
| 47 return file_wrapper->Pass(); |
| 48 } |
| 49 |
| 39 void ResourceLoader::OnGotResources(const std::vector<std::string>& paths, | 50 void ResourceLoader::OnGotResources(const std::vector<std::string>& paths, |
| 40 mojo::Array<mojo::ScopedHandle> resources) { | 51 mojo::Array<mojo::ScopedHandle> resources) { |
| 41 // We no longer need the connection to ResourceProvider. | 52 // We no longer need the connection to ResourceProvider. |
| 42 resource_provider_.reset(); | 53 resource_provider_.reset(); |
| 43 resource_provider_service_provider_.reset(); | 54 resource_provider_service_provider_.reset(); |
| 44 | 55 |
| 45 CHECK(!resources.is_null()); | 56 CHECK(!resources.is_null()); |
| 46 CHECK_EQ(resources.size(), paths.size()); | 57 CHECK_EQ(resources.size(), paths.size()); |
| 47 for (size_t i = 0; i < resources.size(); ++i) { | 58 for (size_t i = 0; i < resources.size(); ++i) { |
| 48 CHECK(resources[i].is_valid()); | 59 CHECK(resources[i].is_valid()); |
| 49 MojoPlatformHandle platform_handle; | 60 MojoPlatformHandle platform_handle; |
| 50 CHECK(MojoExtractPlatformHandle(resources[i].release().value(), | 61 CHECK(MojoExtractPlatformHandle(resources[i].release().value(), |
| 51 &platform_handle) == MOJO_RESULT_OK); | 62 &platform_handle) == MOJO_RESULT_OK); |
| 52 resource_map_[paths[i]] = platform_handle; | 63 resource_map_[paths[i]].reset(new base::File(platform_handle)); |
| 53 } | 64 } |
| 54 loaded_ = true; | 65 loaded_ = true; |
| 55 } | 66 } |
| 56 | 67 |
| 57 } // namespace resource_provider | 68 } // namespace resource_provider |
| OLD | NEW |