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 #include "native_client/src/trusted/plugin/pnacl_resources.h" | 5 #include "native_client/src/trusted/plugin/pnacl_resources.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "native_client/src/include/portability_io.h" | 10 #include "native_client/src/include/portability_io.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 PnaclResources::~PnaclResources() { | 25 PnaclResources::~PnaclResources() { |
26 for (std::map<nacl::string, nacl::DescWrapper*>::iterator | 26 for (std::map<nacl::string, nacl::DescWrapper*>::iterator |
27 i = resource_wrappers_.begin(), e = resource_wrappers_.end(); | 27 i = resource_wrappers_.begin(), e = resource_wrappers_.end(); |
28 i != e; | 28 i != e; |
29 ++i) { | 29 ++i) { |
30 delete i->second; | 30 delete i->second; |
31 } | 31 } |
32 resource_wrappers_.clear(); | 32 resource_wrappers_.clear(); |
33 } | 33 } |
34 | 34 |
35 void PnaclResources::Initialize() { | |
36 callback_factory_.Initialize(this); | |
37 } | |
38 | |
39 void PnaclResources::AddResourceUrl(const nacl::string& url) { | |
40 // Use previously loaded resources if available. | |
41 if (resource_wrappers_.find(url) != resource_wrappers_.end()) { | |
42 return; | |
43 } | |
44 all_loaded_ = false; | |
45 resource_urls_.push_back(url); | |
46 } | |
47 | |
48 void PnaclResources::StartDownloads() { | 35 void PnaclResources::StartDownloads() { |
49 // If there are no resources to be loaded, report all loaded to invoke | 36 PLUGIN_PRINTF(("PnaclResources::StartDownloads\n")); |
50 // client callbacks as needed. | |
51 if (all_loaded_) { | |
52 AllLoaded(PP_OK); | |
53 return; | |
54 } | |
55 pp::CompletionCallback all_loaded_callback = | |
56 callback_factory_.NewCallback(&PnaclResources::AllLoaded); | |
57 // Create a counter (barrier) callback to track when all of the resources | 37 // Create a counter (barrier) callback to track when all of the resources |
58 // are loaded. | 38 // are loaded. |
59 uint32_t resource_count = static_cast<uint32_t>(resource_urls_.size()); | 39 uint32_t resource_count = static_cast<uint32_t>(resource_urls_.size()); |
60 delayed_callback_.reset( | 40 delayed_callback_.reset( |
61 new DelayedCallback(all_loaded_callback, resource_count)); | 41 new DelayedCallback(all_loaded_callback_, resource_count)); |
62 | |
63 // All resource URLs are relative to the coordinator's resource_base_url(). | |
64 nacl::string resource_base_url = coordinator_->resource_base_url(); | |
65 | 42 |
66 // Schedule the downloads. | 43 // Schedule the downloads. |
67 CHECK(resource_urls_.size() > 0); | 44 CHECK(resource_urls_.size() > 0); |
68 for (size_t i = 0; i < resource_urls_.size(); ++i) { | 45 for (size_t i = 0; i < resource_urls_.size(); ++i) { |
69 const nacl::string& full_url = resource_base_url + resource_urls_[i]; | 46 const nacl::string& full_url = resource_base_url_ + resource_urls_[i]; |
70 pp::CompletionCallback ready_callback = | 47 pp::CompletionCallback ready_callback = |
71 callback_factory_.NewCallback(&PnaclResources::ResourceReady, | 48 callback_factory_.NewCallback(&PnaclResources::ResourceReady, |
72 resource_urls_[i], | 49 resource_urls_[i], |
73 full_url); | 50 full_url); |
74 if (!plugin_->StreamAsFile(full_url, | 51 if (!plugin_->StreamAsFile(full_url, |
75 ready_callback.pp_completion_callback())) { | 52 ready_callback.pp_completion_callback())) { |
76 ErrorInfo error_info; | 53 coordinator_->ReportNonPpapiError( |
77 error_info.SetReport(ERROR_UNKNOWN, | 54 nacl::string("failed to download ") + resource_urls_[i] + "\n"); |
78 "PnaclCoordinator: Failed to download file: " + | |
79 resource_urls_[i] + "\n"); | |
80 coordinator_->ReportLoadError(error_info); | |
81 coordinator_->PnaclNonPpapiError(); | |
82 break; | 55 break; |
83 } | 56 } |
84 } | 57 } |
85 resource_urls_.clear(); | |
86 } | 58 } |
87 | 59 |
88 void PnaclResources::ResourceReady(int32_t pp_error, | 60 void PnaclResources::ResourceReady(int32_t pp_error, |
89 const nacl::string& url, | 61 const nacl::string& url, |
90 const nacl::string& full_url) { | 62 const nacl::string& full_url) { |
91 PLUGIN_PRINTF(("PnaclResources::ResourceReady (pp_error=%" | 63 PLUGIN_PRINTF(("PnaclResources::ResourceReady (pp_error=%" |
92 NACL_PRId32", url=%s)\n", pp_error, url.c_str())); | 64 NACL_PRId32", url=%s)\n", pp_error, url.c_str())); |
93 // pp_error is checked by GetLoadedFileDesc. | 65 // pp_error is checked by GetLoadedFileDesc. |
94 int32_t fd = coordinator_->GetLoadedFileDesc(pp_error, | 66 int32_t fd = coordinator_->GetLoadedFileDesc(pp_error, |
95 full_url, | 67 full_url, |
96 "resource " + url); | 68 "resource " + url); |
97 if (fd < 0) { | 69 if (fd < 0) { |
98 coordinator_->PnaclPpapiError(pp_error); | 70 coordinator_->ReportPpapiError(pp_error, |
| 71 "PnaclResources::ResourceReady failed.\n"); |
99 } else { | 72 } else { |
100 resource_wrappers_[url] = | 73 resource_wrappers_[url] = |
101 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY); | 74 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY); |
102 delayed_callback_->RunIfTime(); | 75 delayed_callback_->RunIfTime(); |
103 } | 76 } |
104 } | 77 } |
105 | 78 |
106 void PnaclResources::AllLoaded(int32_t pp_error) { | |
107 PLUGIN_PRINTF(("PnaclResources::AllLoaded (pp_error=%"NACL_PRId32")\n", | |
108 pp_error)); | |
109 all_loaded_ = true; | |
110 // Run the client-specified callback if one was set. | |
111 if (client_callback_is_valid_) { | |
112 pp::Core* core = pp::Module::Get()->core(); | |
113 core->CallOnMainThread(0, client_callback_, PP_OK); | |
114 } | |
115 } | |
116 | |
117 void PnaclResources::RunWhenAllLoaded(pp::CompletionCallback& client_callback) { | |
118 if (all_loaded_) { | |
119 pp::Core* core = pp::Module::Get()->core(); | |
120 core->CallOnMainThread(0, client_callback, PP_OK); | |
121 } | |
122 client_callback_ = client_callback; | |
123 client_callback_is_valid_ = true; | |
124 } | |
125 | |
126 } // namespace plugin | 79 } // namespace plugin |
OLD | NEW |