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
3 // found in the LICENSE file.
5 #include "components/resource_provider/public/cpp/resource_loader.h"
8 #include "base/files/file.h"
9 #include "mojo/application/public/cpp/connect.h"
10 #include "mojo/application/public/interfaces/shell.mojom.h"
11 #include "mojo/common/common_type_converters.h"
12 #include "mojo/platform_handle/platform_handle_functions.h"
14 namespace resource_provider
{
16 ResourceLoader::ResourceLoader(mojo::Shell
* shell
,
17 const std::set
<std::string
>& paths
)
18 : loaded_(false), did_block_(false) {
19 mojo::URLRequestPtr
request(mojo::URLRequest::New());
20 request
->url
= mojo::String::From("mojo:resource_provider");
21 shell
->ConnectToApplication(request
.Pass(),
22 GetProxy(&resource_provider_service_provider_
),
24 mojo::ConnectToService(resource_provider_service_provider_
.get(),
26 std::vector
<std::string
> paths_vector(paths
.begin(), paths
.end());
27 resource_provider_
->GetResources(
28 mojo::Array
<mojo::String
>::From(paths_vector
),
29 base::Bind(&ResourceLoader::OnGotResources
, base::Unretained(this),
33 ResourceLoader::~ResourceLoader() {
36 bool ResourceLoader::BlockUntilLoaded() {
37 if (did_block_
|| loaded_
)
41 resource_provider_
.WaitForIncomingResponse();
45 base::File
ResourceLoader::ReleaseFile(const std::string
& path
) {
46 CHECK(resource_map_
.count(path
));
47 scoped_ptr
<base::File
> file_wrapper(resource_map_
[path
].Pass());
48 resource_map_
.erase(path
);
49 return file_wrapper
->Pass();
52 void ResourceLoader::OnGotResources(const std::vector
<std::string
>& paths
,
53 mojo::Array
<mojo::ScopedHandle
> resources
) {
54 // We no longer need the connection to ResourceProvider.
55 resource_provider_
.reset();
56 resource_provider_service_provider_
.reset();
58 CHECK(!resources
.is_null());
59 CHECK_EQ(resources
.size(), paths
.size());
60 for (size_t i
= 0; i
< resources
.size(); ++i
) {
61 CHECK(resources
[i
].is_valid());
62 MojoPlatformHandle platform_handle
;
63 CHECK(MojoExtractPlatformHandle(resources
[i
].release().value(),
64 &platform_handle
) == MOJO_RESULT_OK
);
65 resource_map_
[paths
[i
]].reset(new base::File(platform_handle
));
70 } // namespace resource_provider