Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: ppapi/native_client/src/trusted/plugin/pnacl_resources.cc

Issue 10689012: Add ability to load pnacl resources from DIR_PNACL_COMPONENT. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: PP_FileHandle Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "native_client/src/include/portability_io.h" 7 #include "native_client/src/include/portability_io.h"
8 #include "native_client/src/shared/platform/nacl_check.h" 8 #include "native_client/src/shared/platform/nacl_check.h"
9 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" 9 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
10 #include "native_client/src/trusted/plugin/manifest.h" 10 #include "native_client/src/trusted/plugin/manifest.h"
11 #include "native_client/src/trusted/plugin/plugin.h" 11 #include "native_client/src/trusted/plugin/plugin.h"
12 #include "native_client/src/trusted/plugin/pnacl_coordinator.h" 12 #include "native_client/src/trusted/plugin/pnacl_coordinator.h"
13 #include "native_client/src/trusted/plugin/utility.h" 13 #include "native_client/src/trusted/plugin/utility.h"
14 14
15 #include "ppapi/c/pp_errors.h" 15 #include "ppapi/c/pp_errors.h"
16 16
17 namespace plugin { 17 namespace plugin {
18 18
19 const char PnaclUrls::kExtensionOrigin[] = 19 const char PnaclUrls::kExtensionOrigin[] =
20 "chrome-extension://gcodniebolpnpaiggndmcmmfpldlknih/"; 20 "chrome-extension://gcodniebolpnpaiggndmcmmfpldlknih/";
21 const char PnaclUrls::kPnaclComponentID[] =
22 "pnacl-component://";
21 const char PnaclUrls::kLlcUrl[] = "llc"; 23 const char PnaclUrls::kLlcUrl[] = "llc";
22 const char PnaclUrls::kLdUrl[] = "ld"; 24 const char PnaclUrls::kLdUrl[] = "ld";
23 25
26 bool PnaclUrls::UsePnaclExtension(const Plugin* plugin) {
27 // Use the chrome webstore extension for now, if --enable-pnacl is not
28 // explicitly requested. Eventually we will always behave as if
29 // --enable-pnacl is used and not use the chrome extension.
30 // Some UI work, etc. remains before --enable-pnacl is usable:
31 // http://code.google.com/p/nativeclient/issues/detail?id=2813
32 return !(plugin->nacl_interface()->IsPnaclEnabled());
33 }
34
35 nacl::string PnaclUrls::GetBaseUrl(bool use_extension) {
36 if (use_extension) {
37 return nacl::string(kExtensionOrigin) + GetSandboxISA() + "/";
38 } else {
39 return nacl::string(kPnaclComponentID) + GetSandboxISA() + "/";
40 }
41 }
42
43 bool PnaclUrls::IsPnaclComponent(const nacl::string& full_url) {
44 return full_url.find(kPnaclComponentID, 0) == 0;
45 }
46
47 nacl::string PnaclUrls::StripPnaclComponentPrefix(const nacl::string& full_url) {
48 return full_url.substr(nacl::string(kPnaclComponentID).length());
49 }
50
51 //////////////////////////////////////////////////////////////////////
52
24 PnaclResources::~PnaclResources() { 53 PnaclResources::~PnaclResources() {
25 for (std::map<nacl::string, nacl::DescWrapper*>::iterator 54 for (std::map<nacl::string, nacl::DescWrapper*>::iterator
26 i = resource_wrappers_.begin(), e = resource_wrappers_.end(); 55 i = resource_wrappers_.begin(), e = resource_wrappers_.end();
27 i != e; 56 i != e;
28 ++i) { 57 ++i) {
29 delete i->second; 58 delete i->second;
30 } 59 }
31 resource_wrappers_.clear(); 60 resource_wrappers_.clear();
32 } 61 }
33 62
34 void PnaclResources::StartDownloads() { 63 // static
35 PLUGIN_PRINTF(("PnaclResources::StartDownloads\n")); 64 int32_t PnaclResources::GetPnaclFD(Plugin* plugin, const char* filename) {
36 // Create a counter (barrier) callback to track when all of the resources 65 PP_FileHandle file_handle =
37 // are loaded. 66 plugin->nacl_interface()->GetReadonlyPnaclFd(filename);
38 uint32_t resource_count = static_cast<uint32_t>(resource_urls_.size()); 67 if (file_handle == PP_kInvalidFileHandle)
39 delayed_callback_.reset( 68 return -1;
40 new DelayedCallback(all_loaded_callback_, resource_count));
41 69
42 // Schedule the downloads. 70 #if NACL_WINDOWS
43 CHECK(resource_urls_.size() > 0); 71 //////// Now try the posix view.
44 for (size_t i = 0; i < resource_urls_.size(); ++i) { 72 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(file_handle),
45 nacl::string full_url; 73 _O_RDONLY | _O_BINARY);
46 ErrorInfo error_info; 74 if (posix_desc == -1) {
47 if (!manifest_->ResolveURL(resource_urls_[i], &full_url, &error_info)) { 75 PLUGIN_PRINTF((
48 coordinator_->ReportNonPpapiError(nacl::string("failed to resolve ") + 76 "PnaclResources::GetPnaclFD failed to convert HANDLE to posix\n"));
49 resource_urls_[i] + ": " + 77 // Close the Windows HANDLE if it can't be converted.
50 error_info.message() + "."); 78 CloseHandle(file_handle);
51 break; 79 }
80 return posix_desc;
81 #else
82 return file_handle;
83 #endif
84 }
85
86 void PnaclResources::StartLoad() {
87 PLUGIN_PRINTF(("PnaclResources::StartLoad\n"));
88
89 if (PnaclUrls::UsePnaclExtension(plugin_)) {
90 PLUGIN_PRINTF(("PnaclResources::StartLoad -- PNaCl chrome extension.\n"));
91 // Do a URL fetch.
92 // Create a counter (barrier) callback to track when all of the resources
93 // are loaded.
94 uint32_t resource_count = static_cast<uint32_t>(resource_urls_.size());
95 delayed_callback_.reset(
96 new DelayedCallback(all_loaded_callback_, resource_count));
97
98 // Schedule the downloads.
99 CHECK(resource_urls_.size() > 0);
100 for (size_t i = 0; i < resource_urls_.size(); ++i) {
101 nacl::string full_url;
102 ErrorInfo error_info;
103 if (!manifest_->ResolveURL(resource_urls_[i], &full_url, &error_info)) {
104 coordinator_->ReportNonPpapiError(nacl::string("failed to resolve ") +
105 resource_urls_[i] + ": " +
106 error_info.message() + ".");
107 break;
108 }
109 pp::CompletionCallback ready_callback =
110 callback_factory_.NewCallback(
111 &PnaclResources::ResourceReady,
112 resource_urls_[i],
113 full_url);
114 if (!plugin_->StreamAsFile(full_url,
115 ready_callback.pp_completion_callback())) {
116 coordinator_->ReportNonPpapiError(nacl::string("failed to download ") +
117 resource_urls_[i] + ".");
118 break;
119 }
52 } 120 }
53 pp::CompletionCallback ready_callback = 121 } else {
54 callback_factory_.NewCallback(&PnaclResources::ResourceReady, 122 PLUGIN_PRINTF(("PnaclResources::StartLoad -- local install of PNaCl.\n"));
55 resource_urls_[i], 123 // Do a blocking load of each of the resources.
56 full_url); 124 CHECK(resource_urls_.size() > 0);
57 if (!plugin_->StreamAsFile(full_url, 125 for (size_t i = 0; i < resource_urls_.size(); ++i) {
58 ready_callback.pp_completion_callback())) { 126 const nacl::string& url = resource_urls_[i];
59 coordinator_->ReportNonPpapiError(nacl::string("failed to download ") + 127 nacl::string full_url;
60 resource_urls_[i] + "."); 128 ErrorInfo error_info;
61 break; 129 if (!manifest_->ResolveURL(resource_urls_[i], &full_url, &error_info)) {
130 coordinator_->ReportNonPpapiError(nacl::string("failed to resolve ") +
131 url + ": " +
132 error_info.message() + ".");
133 break;
134 }
135 full_url = PnaclUrls::StripPnaclComponentPrefix(full_url);
136
137 int32_t fd = PnaclResources::GetPnaclFD(plugin_, full_url.c_str());
138 if (fd < 0) {
139 coordinator_->ReportNonPpapiError(
140 nacl::string("PnaclLocalResources::StartLoad failed for: ") +
141 full_url);
robertm 2012/08/08 21:51:11 why not follow the model above and just break here
jvoung - send to chromium... 2012/08/08 22:47:04 Oops, yeah it could.
142 } else {
143 resource_wrappers_[url] =
144 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY);
145 }
62 } 146 }
147 // We're done! Queue the callback.
148 pp::Core* core = pp::Module::Get()->core();
149 core->CallOnMainThread(0, all_loaded_callback_, 0);
63 } 150 }
64 } 151 }
65 152
66 void PnaclResources::ResourceReady(int32_t pp_error, 153 void PnaclResources::ResourceReady(int32_t pp_error,
67 const nacl::string& url, 154 const nacl::string& url,
68 const nacl::string& full_url) { 155 const nacl::string& full_url) {
69 PLUGIN_PRINTF(("PnaclResources::ResourceReady (pp_error=%" 156 PLUGIN_PRINTF(("PnaclResources::ResourceReady (pp_error=%"
70 NACL_PRId32", url=%s)\n", pp_error, url.c_str())); 157 NACL_PRId32", url=%s)\n", pp_error, url.c_str()));
71 // pp_error is checked by GetLoadedFileDesc. 158 // pp_error is checked by GetLoadedFileDesc.
72 int32_t fd = coordinator_->GetLoadedFileDesc(pp_error, 159 int32_t fd = coordinator_->GetLoadedFileDesc(pp_error,
73 full_url, 160 full_url,
74 "resource " + url); 161 "resource " + url);
75 if (fd < 0) { 162 if (fd < 0) {
76 coordinator_->ReportPpapiError(pp_error, 163 coordinator_->ReportPpapiError(pp_error,
77 "PnaclResources::ResourceReady failed."); 164 "PnaclResources::ResourceReady failed.");
78 } else { 165 } else {
79 resource_wrappers_[url] = 166 resource_wrappers_[url] =
80 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY); 167 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY);
81 delayed_callback_->RunIfTime(); 168 delayed_callback_->RunIfTime();
82 } 169 }
83 } 170 }
84 171
85 } // namespace plugin 172 } // namespace plugin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698