Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / components / resource_provider / public / cpp / resource_loader.cc
blobba31461e545c452574ad148206c46cff64bf310a
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"
7 #include "base/bind.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_),
23 nullptr);
24 mojo::ConnectToService(resource_provider_service_provider_.get(),
25 &resource_provider_);
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),
30 paths_vector));
33 ResourceLoader::~ResourceLoader() {
36 bool ResourceLoader::BlockUntilLoaded() {
37 if (did_block_ || loaded_)
38 return loaded_;
40 did_block_ = true;
41 resource_provider_.WaitForIncomingResponse();
42 return loaded_;
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));
67 loaded_ = true;
70 } // namespace resource_provider