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 #ifndef COMPONENTS_RESOURCE_PROVIDER_PUBLIC_CPP_RESOURCE_LOADER_H_ | 5 #ifndef COMPONENTS_RESOURCE_PROVIDER_PUBLIC_CPP_RESOURCE_LOADER_H_ |
6 #define COMPONENTS_RESOURCE_PROVIDER_PUBLIC_CPP_RESOURCE_LOADER_H_ | 6 #define COMPONENTS_RESOURCE_PROVIDER_PUBLIC_CPP_RESOURCE_LOADER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/scoped_ptr.h" |
14 #include "components/resource_provider/public/interfaces/resource_provider.mojom
.h" | 15 #include "components/resource_provider/public/interfaces/resource_provider.mojom
.h" |
15 #include "mojo/platform_handle/platform_handle.h" | 16 #include "mojo/platform_handle/platform_handle.h" |
16 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | 17 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" |
17 #include "third_party/mojo/src/mojo/public/cpp/system/handle.h" | 18 #include "third_party/mojo/src/mojo/public/cpp/system/handle.h" |
18 #include "third_party/mojo/src/mojo/public/interfaces/application/service_provid
er.mojom.h" | 19 #include "third_party/mojo/src/mojo/public/interfaces/application/service_provid
er.mojom.h" |
19 | 20 |
| 21 namespace base { |
| 22 class File; |
| 23 } |
| 24 |
20 namespace mojo { | 25 namespace mojo { |
21 class Shell; | 26 class Shell; |
22 } | 27 } |
23 | 28 |
24 namespace resource_provider { | 29 namespace resource_provider { |
25 | 30 |
26 // ResourceLoader handles making a request to ResourceProvider and blocking | 31 // ResourceLoader handles making a request to ResourceProvider and blocking |
27 // until the resources are available. Expected use is to create a ResourceLoader | 32 // until the resources are available. Expected use is to create a ResourceLoader |
28 // and shortly thereafter call BlockUntilLoaded() to wait until the resources | 33 // and shortly thereafter call BlockUntilLoaded() to wait until the resources |
29 // have been obtained. | 34 // have been obtained. |
30 class ResourceLoader { | 35 class ResourceLoader { |
31 public: | 36 public: |
32 using ResourceMap = std::map<std::string, MojoPlatformHandle>; | |
33 | |
34 ResourceLoader(mojo::Shell* shell, const std::set<std::string>& paths); | 37 ResourceLoader(mojo::Shell* shell, const std::set<std::string>& paths); |
35 ~ResourceLoader(); | 38 ~ResourceLoader(); |
36 | 39 |
37 // Uses WaitForIncomingMessage() to block until the results are available, or | 40 // Uses WaitForIncomingMessage() to block until the results are available, or |
38 // an error occurs. Returns true if the resources were obtained, false on | 41 // an error occurs. Returns true if the resources were obtained, false on |
39 // error. | 42 // error. This immediately returns if the resources have already been |
| 43 // obtained. |
40 bool BlockUntilLoaded(); | 44 bool BlockUntilLoaded(); |
41 | 45 |
42 const ResourceMap& resource_map() const { return resource_map_; } | 46 // Releases and returns the file wrapping the handle. |
| 47 base::File ReleaseFile(const std::string& path); |
43 | 48 |
44 bool loaded() const { return loaded_; } | 49 bool loaded() const { return loaded_; } |
45 | 50 |
46 private: | 51 private: |
| 52 using ResourceMap = std::map<std::string, scoped_ptr<base::File>>; |
| 53 |
47 // Callback when resources have loaded. | 54 // Callback when resources have loaded. |
48 void OnGotResources(const std::vector<std::string>& paths, | 55 void OnGotResources(const std::vector<std::string>& paths, |
49 mojo::Array<mojo::ScopedHandle> resources); | 56 mojo::Array<mojo::ScopedHandle> resources); |
50 | 57 |
51 mojo::ServiceProviderPtr resource_provider_service_provider_; | 58 mojo::ServiceProviderPtr resource_provider_service_provider_; |
52 | 59 |
53 ResourceProviderPtr resource_provider_; | 60 ResourceProviderPtr resource_provider_; |
54 | 61 |
55 ResourceMap resource_map_; | 62 ResourceMap resource_map_; |
56 | 63 |
57 bool loaded_; | 64 bool loaded_; |
| 65 bool did_block_; |
58 | 66 |
59 DISALLOW_COPY_AND_ASSIGN(ResourceLoader); | 67 DISALLOW_COPY_AND_ASSIGN(ResourceLoader); |
60 }; | 68 }; |
61 | 69 |
62 } // namespace resource_provider | 70 } // namespace resource_provider |
63 | 71 |
64 #endif // COMPONENTS_RESOURCE_PROVIDER_PUBLIC_CPP_RESOURCE_LOADER_H_ | 72 #endif // COMPONENTS_RESOURCE_PROVIDER_PUBLIC_CPP_RESOURCE_LOADER_H_ |
OLD | NEW |