| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_ | 5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_ |
| 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_ | 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "native_client/src/include/nacl_macros.h" | 11 #include "native_client/src/include/nacl_macros.h" |
| 12 #include "native_client/src/include/nacl_string.h" | 12 #include "native_client/src/include/nacl_string.h" |
| 13 #include "native_client/src/shared/srpc/nacl_srpc.h" | 13 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 14 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" | 14 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" |
| 15 #include "native_client/src/trusted/plugin/delayed_callback.h" | 15 #include "native_client/src/trusted/plugin/delayed_callback.h" |
| 16 #include "native_client/src/trusted/plugin/plugin_error.h" | 16 #include "native_client/src/trusted/plugin/plugin_error.h" |
| 17 | 17 |
| 18 #include "ppapi/cpp/completion_callback.h" | 18 #include "ppapi/cpp/completion_callback.h" |
| 19 | 19 |
| 20 namespace plugin { | 20 namespace plugin { |
| 21 | 21 |
| 22 class Plugin; | 22 class Plugin; |
| 23 class PnaclCoordinator; | 23 class PnaclCoordinator; |
| 24 | 24 |
| 25 |
| 26 // Loads a list of remote resources, providing a way to get file descriptors for |
| 27 // thse resources. All URLs in relative to resource_base_url_. |
| 25 class PnaclResources { | 28 class PnaclResources { |
| 26 public: | 29 public: |
| 27 PnaclResources(Plugin* plugin, PnaclCoordinator* coordinator) | 30 PnaclResources(Plugin* plugin, |
| 31 PnaclCoordinator* coordinator, |
| 32 const nacl::string& resource_base_url, |
| 33 const std::vector<nacl::string>& resource_urls, |
| 34 const pp::CompletionCallback& all_loaded_callback) |
| 28 : plugin_(plugin), | 35 : plugin_(plugin), |
| 29 coordinator_(coordinator), | 36 coordinator_(coordinator), |
| 30 all_loaded_(true), | 37 resource_urls_(resource_urls), |
| 31 client_callback_is_valid_(false) | 38 all_loaded_callback_(all_loaded_callback), |
| 32 { } | 39 resource_base_url_(resource_base_url) { |
| 40 callback_factory_.Initialize(this); |
| 41 } |
| 33 | 42 |
| 34 virtual ~PnaclResources(); | 43 virtual ~PnaclResources(); |
| 35 | 44 |
| 36 void Initialize(); | 45 // Start fetching the URLs. After construction, this is the first step. |
| 37 | 46 void StartDownloads(); |
| 38 // URLs are all relative to the coordinator's resource_base_url(). | |
| 39 | |
| 40 // Get the wrapper for the downloaded resource. | 47 // Get the wrapper for the downloaded resource. |
| 48 // Only valid after all_loaded_callback_ has been run. |
| 41 nacl::DescWrapper* WrapperForUrl(const nacl::string& url) { | 49 nacl::DescWrapper* WrapperForUrl(const nacl::string& url) { |
| 42 return resource_wrappers_[url]; | 50 return resource_wrappers_[url]; |
| 43 } | 51 } |
| 44 | 52 |
| 45 // Add an URL for download. | |
| 46 void AddResourceUrl(const nacl::string& url); | |
| 47 // Start fetching the URLs. | |
| 48 void StartDownloads(); | |
| 49 // Set the callback for what to do when all the resources are available. | |
| 50 void RunWhenAllLoaded(pp::CompletionCallback& client_callback); | |
| 51 | |
| 52 private: | 53 private: |
| 53 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclResources); | 54 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclResources); |
| 54 | 55 |
| 55 // Callback invoked each time one resource has been loaded. | 56 // Callback invoked each time one resource has been loaded. |
| 56 void ResourceReady(int32_t pp_error, | 57 void ResourceReady(int32_t pp_error, |
| 57 const nacl::string& url, | 58 const nacl::string& url, |
| 58 const nacl::string& full_url); | 59 const nacl::string& full_url); |
| 59 // Callback invoked when all resources have been loaded. | |
| 60 void AllLoaded(int32_t pp_error); | |
| 61 | 60 |
| 61 // The plugin requesting the resource loading. |
| 62 Plugin* plugin_; | 62 Plugin* plugin_; |
| 63 // The coordinator responsible for reporting errors, etc. |
| 63 PnaclCoordinator* coordinator_; | 64 PnaclCoordinator* coordinator_; |
| 65 // The base url for looking up resources. |
| 66 nacl::string resource_base_url_; |
| 67 // The list of resource URLs (relative to resource_base_url_) to load. |
| 64 std::vector<nacl::string> resource_urls_; | 68 std::vector<nacl::string> resource_urls_; |
| 69 // Callback to be invoked when all resources can be guaranteed available. |
| 70 pp::CompletionCallback all_loaded_callback_; |
| 71 // The descriptor wrappers for the downloaded URLs. Only valid |
| 72 // once all_loaded_callback_ has been invoked. |
| 65 std::map<nacl::string, nacl::DescWrapper*> resource_wrappers_; | 73 std::map<nacl::string, nacl::DescWrapper*> resource_wrappers_; |
| 74 // Because we may be loading multiple resources, we need a callback that |
| 75 // is invoked each time a resource arrives, and finally invokes |
| 76 // all_loaded_callback_ when done. |
| 66 nacl::scoped_ptr<DelayedCallback> delayed_callback_; | 77 nacl::scoped_ptr<DelayedCallback> delayed_callback_; |
| 67 // Set once all resources have been loaded to indicate that future calls to | |
| 68 // RunWhenAllLoaded should immediately invoke the client callback. This | |
| 69 // simplifies the client while allowing resources to be used for subsequent | |
| 70 // translations. | |
| 71 bool all_loaded_; | |
| 72 // Callback to be invoked when all resources can be guaranteed available. | |
| 73 pp::CompletionCallback client_callback_; | |
| 74 // If RunWhenAllLoaded was called before all resources have been loaded, | |
| 75 // this indicates that the registered client callback should be used when | |
| 76 // the last resource arrives. | |
| 77 bool client_callback_is_valid_; | |
| 78 // Factory for ready callbacks, etc. | 78 // Factory for ready callbacks, etc. |
| 79 pp::CompletionCallbackFactory<PnaclResources> callback_factory_; | 79 pp::CompletionCallbackFactory<PnaclResources> callback_factory_; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 } // namespace plugin; | 82 } // namespace plugin; |
| 83 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_ | 83 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_RESOURCES_H_ |
| OLD | NEW |