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) { | 35 void PnaclResources::AddResourceUrl(const nacl::string& url) { |
40 // Use previously loaded resources if available. | 36 // Use previously loaded resources if available. |
41 if (resource_wrappers_.find(url) != resource_wrappers_.end()) { | 37 if (resource_wrappers_.find(url) != resource_wrappers_.end()) { |
42 return; | 38 return; |
43 } | 39 } |
44 all_loaded_ = false; | 40 all_loaded_ = false; |
robertm
2011/12/13 17:06:39
maybe having a counter of how many have been (succ
sehr (please use chromium)
2011/12/13 20:05:04
There is such a counter in DelayedCallback. I hav
| |
45 resource_urls_.push_back(url); | 41 resource_urls_.push_back(url); |
46 } | 42 } |
47 | 43 |
48 void PnaclResources::StartDownloads() { | 44 void PnaclResources::StartDownloads() { |
49 // If there are no resources to be loaded, report all loaded to invoke | 45 // If there are no resources to be loaded, report all loaded to invoke |
50 // client callbacks as needed. | 46 // client callbacks as needed. |
51 if (all_loaded_) { | 47 if (all_loaded_) { |
52 AllLoaded(PP_OK); | 48 AllLoaded(PP_OK); |
53 return; | 49 return; |
54 } | 50 } |
55 pp::CompletionCallback all_loaded_callback = | 51 pp::CompletionCallback all_loaded_callback = |
56 callback_factory_.NewCallback(&PnaclResources::AllLoaded); | 52 callback_factory_.NewCallback(&PnaclResources::AllLoaded); |
57 // Create a counter (barrier) callback to track when all of the resources | 53 // Create a counter (barrier) callback to track when all of the resources |
58 // are loaded. | 54 // are loaded. |
59 uint32_t resource_count = static_cast<uint32_t>(resource_urls_.size()); | 55 uint32_t resource_count = static_cast<uint32_t>(resource_urls_.size()); |
60 delayed_callback_.reset( | 56 delayed_callback_.reset( |
robertm
2011/12/13 17:06:39
not sure whether the DelayedCallback adds all this
sehr (please use chromium)
2011/12/13 20:05:04
It is needed to implement the barrier callback for
| |
61 new DelayedCallback(all_loaded_callback, resource_count)); | 57 new DelayedCallback(all_loaded_callback, resource_count)); |
62 | 58 |
63 // All resource URLs are relative to the coordinator's resource_base_url(). | |
64 nacl::string resource_base_url = coordinator_->resource_base_url(); | |
65 | |
66 // Schedule the downloads. | 59 // Schedule the downloads. |
67 CHECK(resource_urls_.size() > 0); | 60 CHECK(resource_urls_.size() > 0); |
68 for (size_t i = 0; i < resource_urls_.size(); ++i) { | 61 for (size_t i = 0; i < resource_urls_.size(); ++i) { |
69 const nacl::string& full_url = resource_base_url + resource_urls_[i]; | 62 const nacl::string& full_url = resource_base_url_ + resource_urls_[i]; |
63 PLUGIN_PRINTF(("PnaclResources::StartDownloads === creating callback\n")); | |
70 pp::CompletionCallback ready_callback = | 64 pp::CompletionCallback ready_callback = |
71 callback_factory_.NewCallback(&PnaclResources::ResourceReady, | 65 callback_factory_.NewCallback(&PnaclResources::ResourceReady, |
72 resource_urls_[i], | 66 resource_urls_[i], |
73 full_url); | 67 full_url); |
68 PLUGIN_PRINTF(("PnaclResources::StartDownloads === created callback\n")); | |
74 if (!plugin_->StreamAsFile(full_url, | 69 if (!plugin_->StreamAsFile(full_url, |
75 ready_callback.pp_completion_callback())) { | 70 ready_callback.pp_completion_callback())) { |
76 ErrorInfo error_info; | 71 coordinator_->ReportNonPpapiError( |
77 error_info.SetReport(ERROR_UNKNOWN, | 72 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; | 73 break; |
83 } | 74 } |
75 PLUGIN_PRINTF(("PnaclResources::StartDownloads === invoked SAF\n")); | |
84 } | 76 } |
85 resource_urls_.clear(); | 77 resource_urls_.clear(); |
86 } | 78 } |
87 | 79 |
88 void PnaclResources::ResourceReady(int32_t pp_error, | 80 void PnaclResources::ResourceReady(int32_t pp_error, |
89 const nacl::string& url, | 81 const nacl::string& url, |
90 const nacl::string& full_url) { | 82 const nacl::string& full_url) { |
91 PLUGIN_PRINTF(("PnaclResources::ResourceReady (pp_error=%" | 83 PLUGIN_PRINTF(("PnaclResources::ResourceReady (pp_error=%" |
92 NACL_PRId32", url=%s)\n", pp_error, url.c_str())); | 84 NACL_PRId32", url=%s)\n", pp_error, url.c_str())); |
93 // pp_error is checked by GetLoadedFileDesc. | 85 // pp_error is checked by GetLoadedFileDesc. |
94 int32_t fd = coordinator_->GetLoadedFileDesc(pp_error, | 86 int32_t fd = coordinator_->GetLoadedFileDesc(pp_error, |
95 full_url, | 87 full_url, |
96 "resource " + url); | 88 "resource " + url); |
97 if (fd < 0) { | 89 if (fd < 0) { |
98 coordinator_->PnaclPpapiError(pp_error); | 90 coordinator_->ReportPpapiError(pp_error, |
91 "PnaclResources::ResourceReady failed.\n"); | |
99 } else { | 92 } else { |
100 resource_wrappers_[url] = | 93 resource_wrappers_[url] = |
101 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY); | 94 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY); |
102 delayed_callback_->RunIfTime(); | 95 delayed_callback_->RunIfTime(); |
robertm
2011/12/13 17:06:39
do you need delayed_callback_ or could you make th
sehr (please use chromium)
2011/12/13 20:05:04
I think using DelayedCallback and removing the oth
| |
103 } | 96 } |
104 } | 97 } |
105 | 98 |
106 void PnaclResources::AllLoaded(int32_t pp_error) { | 99 void PnaclResources::AllLoaded(int32_t pp_error) { |
107 PLUGIN_PRINTF(("PnaclResources::AllLoaded (pp_error=%"NACL_PRId32")\n", | 100 PLUGIN_PRINTF(("PnaclResources::AllLoaded (pp_error=%"NACL_PRId32")\n", |
108 pp_error)); | 101 pp_error)); |
109 all_loaded_ = true; | 102 all_loaded_ = true; |
110 // Run the client-specified callback if one was set. | 103 // Run the client-specified callback if one was set. |
111 if (client_callback_is_valid_) { | 104 if (client_callback_is_valid_) { |
112 pp::Core* core = pp::Module::Get()->core(); | 105 pp::Core* core = pp::Module::Get()->core(); |
113 core->CallOnMainThread(0, client_callback_, PP_OK); | 106 core->CallOnMainThread(0, client_callback_, PP_OK); |
114 } | 107 } |
115 } | 108 } |
116 | 109 |
117 void PnaclResources::RunWhenAllLoaded(pp::CompletionCallback& client_callback) { | 110 void PnaclResources::RunWhenAllLoaded(pp::CompletionCallback& client_callback) { |
118 if (all_loaded_) { | 111 if (all_loaded_) { |
119 pp::Core* core = pp::Module::Get()->core(); | 112 pp::Core* core = pp::Module::Get()->core(); |
120 core->CallOnMainThread(0, client_callback, PP_OK); | 113 core->CallOnMainThread(0, client_callback, PP_OK); |
121 } | 114 } |
122 client_callback_ = client_callback; | 115 client_callback_ = client_callback; |
123 client_callback_is_valid_ = true; | 116 client_callback_is_valid_ = true; |
124 } | 117 } |
125 | 118 |
126 } // namespace plugin | 119 } // namespace plugin |
OLD | NEW |