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

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

Issue 291973002: Pepper: DescWrapper cleanup in PnaclResources. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nits Created 6 years, 7 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 "ppapi/native_client/src/trusted/plugin/pnacl_resources.h" 5 #include "ppapi/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 "ppapi/c/pp_errors.h" 10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/native_client/src/trusted/plugin/plugin.h" 11 #include "ppapi/native_client/src/trusted/plugin/plugin.h"
12 #include "ppapi/native_client/src/trusted/plugin/pnacl_coordinator.h" 12 #include "ppapi/native_client/src/trusted/plugin/pnacl_coordinator.h"
13 #include "ppapi/native_client/src/trusted/plugin/utility.h" 13 #include "ppapi/native_client/src/trusted/plugin/utility.h"
14 #include "third_party/jsoncpp/source/include/json/reader.h" 14 #include "third_party/jsoncpp/source/include/json/reader.h"
15 #include "third_party/jsoncpp/source/include/json/value.h" 15 #include "third_party/jsoncpp/source/include/json/value.h"
16 16
17 namespace plugin { 17 namespace plugin {
18 18
19 namespace {
20
21 nacl::string GetFullUrl(const nacl::string& partial_url) {
22 return PnaclUrls::GetBaseUrl() + GetNaClInterface()->GetSandboxArch() + "/" +
23 partial_url;
24 }
25
26 } // namespace
27
19 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/"; 28 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/";
20 29
21 nacl::string PnaclUrls::GetBaseUrl() { 30 nacl::string PnaclUrls::GetBaseUrl() {
22 return nacl::string(kPnaclBaseUrl); 31 return nacl::string(kPnaclBaseUrl);
23 } 32 }
24 33
25 // Determine if a URL is for a pnacl-component file, or if it is some other 34 // Determine if a URL is for a pnacl-component file, or if it is some other
26 // type of URL (e.g., http://, https://, chrome-extension://). 35 // type of URL (e.g., http://, https://, chrome-extension://).
27 // The URL could be one of the other variants for shared libraries 36 // The URL could be one of the other variants for shared libraries
28 // served from the web. 37 // served from the web.
(...skipping 19 matching lines...) Expand all
48 return r; 57 return r;
49 } 58 }
50 59
51 nacl::string PnaclUrls::GetResourceInfoUrl() { 60 nacl::string PnaclUrls::GetResourceInfoUrl() {
52 return "pnacl.json"; 61 return "pnacl.json";
53 } 62 }
54 63
55 ////////////////////////////////////////////////////////////////////// 64 //////////////////////////////////////////////////////////////////////
56 65
57 PnaclResources::~PnaclResources() { 66 PnaclResources::~PnaclResources() {
58 for (std::map<nacl::string, nacl::DescWrapper*>::iterator 67 if (llc_file_handle_ != PP_kInvalidFileHandle)
59 i = resource_wrappers_.begin(), e = resource_wrappers_.end(); 68 CloseFileHandle(llc_file_handle_);
60 i != e; 69 if (ld_file_handle_ != PP_kInvalidFileHandle)
61 ++i) { 70 CloseFileHandle(ld_file_handle_);
62 delete i->second;
63 }
64 resource_wrappers_.clear();
65 }
66
67 // static
68 int32_t PnaclResources::GetPnaclFD(Plugin* plugin, const char* filename) {
69 PP_FileHandle file_handle =
70 plugin->nacl_interface()->GetReadonlyPnaclFd(filename);
71 if (file_handle == PP_kInvalidFileHandle)
72 return -1;
73
74 #if NACL_WINDOWS
75 //////// Now try the posix view.
76 int32_t posix_desc = _open_osfhandle(reinterpret_cast<intptr_t>(file_handle),
77 _O_RDONLY | _O_BINARY);
78 if (posix_desc == -1) {
79 PLUGIN_PRINTF((
80 "PnaclResources::GetPnaclFD failed to convert HANDLE to posix\n"));
81 // Close the Windows HANDLE if it can't be converted.
82 CloseHandle(file_handle);
83 }
84 return posix_desc;
85 #else
86 return file_handle;
87 #endif
88 }
89
90 nacl::DescWrapper* PnaclResources::WrapperForUrl(const nacl::string& url) {
91 CHECK(resource_wrappers_.find(url) != resource_wrappers_.end());
92 return resource_wrappers_[url];
93 } 71 }
94 72
95 void PnaclResources::ReadResourceInfo( 73 void PnaclResources::ReadResourceInfo(
96 const nacl::string& resource_info_url, 74 const nacl::string& resource_info_url,
97 const pp::CompletionCallback& resource_info_read_cb) { 75 const pp::CompletionCallback& resource_info_read_cb) {
98 PLUGIN_PRINTF(("PnaclResources::ReadResourceInfo\n")); 76 PLUGIN_PRINTF(("PnaclResources::ReadResourceInfo\n"));
99 77
100 nacl::string full_url = PnaclUrls::GetBaseUrl() + resource_info_url; 78 nacl::string full_url = PnaclUrls::GetBaseUrl() + resource_info_url;
101 PLUGIN_PRINTF(("Resolved resources info url: %s\n", full_url.c_str())); 79 PLUGIN_PRINTF(("Resolved resources info url: %s\n", full_url.c_str()));
102 nacl::string resource_info_filename = 80 nacl::string resource_info_filename =
103 PnaclUrls::PnaclComponentURLToFilename(full_url); 81 PnaclUrls::PnaclComponentURLToFilename(full_url);
104 82
105 PLUGIN_PRINTF(("Pnacl-converted resources info url: %s\n", 83 PLUGIN_PRINTF(("Pnacl-converted resources info url: %s\n",
106 resource_info_filename.c_str())); 84 resource_info_filename.c_str()));
107 85
108 PP_Var pp_llc_tool_name_var; 86 PP_Var pp_llc_tool_name_var;
109 PP_Var pp_ld_tool_name_var; 87 PP_Var pp_ld_tool_name_var;
110 if (!plugin_->nacl_interface()->GetPnaclResourceInfo( 88 if (!plugin_->nacl_interface()->GetPnaclResourceInfo(
111 plugin_->pp_instance(), 89 plugin_->pp_instance(),
112 resource_info_filename.c_str(), 90 resource_info_filename.c_str(),
113 &pp_llc_tool_name_var, 91 &pp_llc_tool_name_var,
114 &pp_ld_tool_name_var)) { 92 &pp_ld_tool_name_var)) {
115 coordinator_->ExitWithError(); 93 coordinator_->ExitWithError();
116 } 94 }
117 95
118 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var); 96 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var);
119 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var); 97 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var);
120 llc_tool_name_ = llc_tool_name.AsString(); 98 llc_tool_name_ = GetFullUrl(llc_tool_name.AsString());
121 ld_tool_name_ = ld_tool_name.AsString(); 99 ld_tool_name_ = GetFullUrl(ld_tool_name.AsString());
122 pp::Core* core = pp::Module::Get()->core(); 100 pp::Module::Get()->core()->CallOnMainThread(0, resource_info_read_cb, PP_OK);
123 core->CallOnMainThread(0, resource_info_read_cb, PP_OK);
124 } 101 }
125 102
126 nacl::string PnaclResources::GetFullUrl( 103 PP_FileHandle PnaclResources::TakeLlcFileHandle() {
127 const nacl::string& partial_url, const nacl::string& sandbox_arch) const { 104 PP_FileHandle to_return = llc_file_handle_;
128 return PnaclUrls::GetBaseUrl() + sandbox_arch + "/" + partial_url; 105 llc_file_handle_ = PP_kInvalidFileHandle;
106 return to_return;
107 }
108
109 PP_FileHandle PnaclResources::TakeLdFileHandle() {
110 PP_FileHandle to_return = ld_file_handle_;
111 ld_file_handle_ = PP_kInvalidFileHandle;
112 return to_return;
129 } 113 }
130 114
131 void PnaclResources::StartLoad( 115 void PnaclResources::StartLoad(
132 const pp::CompletionCallback& all_loaded_callback) { 116 const pp::CompletionCallback& all_loaded_callback) {
133 PLUGIN_PRINTF(("PnaclResources::StartLoad\n")); 117 PLUGIN_PRINTF(("PnaclResources::StartLoad\n"));
134 118
135 std::vector<nacl::string> resource_urls; 119 // Do a blocking load of each of the resources.
136 resource_urls.push_back(llc_tool_name_); 120 nacl::string llc_filename =
137 resource_urls.push_back(ld_tool_name_); 121 PnaclUrls::PnaclComponentURLToFilename(llc_tool_name_);
122 llc_file_handle_ =
123 plugin_->nacl_interface()->GetReadonlyPnaclFd(llc_filename.c_str());
138 124
139 PLUGIN_PRINTF(("PnaclResources::StartLoad -- local install of PNaCl.\n")); 125 nacl::string ld_filename =
140 // Do a blocking load of each of the resources. 126 PnaclUrls::PnaclComponentURLToFilename(ld_tool_name_);
127 ld_file_handle_ =
128 plugin_->nacl_interface()->GetReadonlyPnaclFd(ld_filename.c_str());
129
141 int32_t result = PP_OK; 130 int32_t result = PP_OK;
142 for (size_t i = 0; i < resource_urls.size(); ++i) { 131 if (llc_file_handle_ == PP_kInvalidFileHandle ||
143 nacl::string full_url = GetFullUrl( 132 ld_file_handle_ == PP_kInvalidFileHandle) {
144 resource_urls[i], plugin_->nacl_interface()->GetSandboxArch()); 133 // File-open failed. Assume this means that the file is
145 nacl::string filename = PnaclUrls::PnaclComponentURLToFilename(full_url); 134 // not actually installed. This shouldn't actually occur since
146 135 // ReadResourceInfo() fail first.
147 int32_t fd = PnaclResources::GetPnaclFD(plugin_, filename.c_str()); 136 coordinator_->ReportNonPpapiError(
148 if (fd < 0) { 137 PP_NACL_ERROR_PNACL_RESOURCE_FETCH,
149 // File-open failed. Assume this means that the file is 138 nacl::string("The Portable Native Client (pnacl) component is not "
150 // not actually installed. This shouldn't actually occur since 139 "installed. Please consult chrome://components for more "
151 // ReadResourceInfo() should happen first, and error out. 140 "information."));
152 coordinator_->ReportNonPpapiError( 141 result = PP_ERROR_FILENOTFOUND;
153 PP_NACL_ERROR_PNACL_RESOURCE_FETCH,
154 nacl::string("The Portable Native Client (pnacl) component is not "
155 "installed. Please consult chrome://components for more "
156 "information."));
157 result = PP_ERROR_FILENOTFOUND;
158 break;
159 } else {
160 resource_wrappers_[resource_urls[i]] =
161 plugin_->wrapper_factory()->MakeFileDesc(fd, O_RDONLY);
162 }
163 } 142 }
164 // We're done! Queue the callback. 143 pp::Module::Get()->core()->CallOnMainThread(0, all_loaded_callback, result);
165 pp::Core* core = pp::Module::Get()->core();
166 core->CallOnMainThread(0, all_loaded_callback, result);
167 } 144 }
168 145
169 } // namespace plugin 146 } // namespace plugin
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698