| OLD | NEW |
| 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 | 14 |
| 15 namespace plugin { | 15 namespace plugin { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/"; | 19 static const char kPnaclBaseUrl[] = "chrome://pnacl-translator/"; |
| 20 | 20 |
| 21 nacl::string GetFullUrl(const nacl::string& partial_url) { | 21 nacl::string GetFullUrl(const nacl::string& partial_url) { |
| 22 return nacl::string(kPnaclBaseUrl) + GetNaClInterface()->GetSandboxArch() + | 22 return nacl::string(kPnaclBaseUrl) + GetNaClInterface()->GetSandboxArch() + |
| 23 "/" + partial_url; | 23 "/" + partial_url; |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 // Determine if a URL is for a pnacl-component file, or if it is some other | |
| 29 // type of URL (e.g., http://, https://, chrome-extension://). | |
| 30 // The URL could be one of the other variants for shared libraries | |
| 31 // served from the web. | |
| 32 bool PnaclUrls::IsPnaclComponent(const nacl::string& full_url) { | |
| 33 return full_url.find(kPnaclBaseUrl, 0) == 0; | |
| 34 } | |
| 35 | |
| 36 // Convert a URL to a filename accepted by GetReadonlyPnaclFd. | |
| 37 // Must be kept in sync with chrome/browser/nacl_host/nacl_file_host. | |
| 38 nacl::string PnaclUrls::PnaclComponentURLToFilename( | |
| 39 const nacl::string& full_url) { | |
| 40 // strip component scheme. | |
| 41 nacl::string r = full_url.substr(nacl::string(kPnaclBaseUrl).length()); | |
| 42 | |
| 43 // Use white-listed-chars. | |
| 44 size_t replace_pos; | |
| 45 static const char* white_list = "abcdefghijklmnopqrstuvwxyz0123456789_"; | |
| 46 replace_pos = r.find_first_not_of(white_list); | |
| 47 while(replace_pos != nacl::string::npos) { | |
| 48 r = r.replace(replace_pos, 1, "_"); | |
| 49 replace_pos = r.find_first_not_of(white_list); | |
| 50 } | |
| 51 return r; | |
| 52 } | |
| 53 | |
| 54 ////////////////////////////////////////////////////////////////////// | |
| 55 | |
| 56 PnaclResources::~PnaclResources() { | 28 PnaclResources::~PnaclResources() { |
| 57 if (llc_file_handle_ != PP_kInvalidFileHandle) | 29 if (llc_file_handle_ != PP_kInvalidFileHandle) |
| 58 CloseFileHandle(llc_file_handle_); | 30 CloseFileHandle(llc_file_handle_); |
| 59 if (ld_file_handle_ != PP_kInvalidFileHandle) | 31 if (ld_file_handle_ != PP_kInvalidFileHandle) |
| 60 CloseFileHandle(ld_file_handle_); | 32 CloseFileHandle(ld_file_handle_); |
| 61 } | 33 } |
| 62 | 34 |
| 63 void PnaclResources::ReadResourceInfo( | 35 void PnaclResources::ReadResourceInfo( |
| 64 const pp::CompletionCallback& resource_info_read_cb) { | 36 const pp::CompletionCallback& resource_info_read_cb) { |
| 65 nacl::string full_url = "chrome://pnacl-translator/pnacl.json"; | |
| 66 nacl::string resource_info_filename = | |
| 67 PnaclUrls::PnaclComponentURLToFilename(full_url); | |
| 68 | |
| 69 PLUGIN_PRINTF(("Pnacl-converted resources info url: %s\n", | |
| 70 resource_info_filename.c_str())); | |
| 71 PP_Var pp_llc_tool_name_var; | 37 PP_Var pp_llc_tool_name_var; |
| 72 PP_Var pp_ld_tool_name_var; | 38 PP_Var pp_ld_tool_name_var; |
| 73 if (!plugin_->nacl_interface()->GetPnaclResourceInfo( | 39 if (!plugin_->nacl_interface()->GetPnaclResourceInfo( |
| 74 plugin_->pp_instance(), | 40 plugin_->pp_instance(), |
| 75 resource_info_filename.c_str(), | 41 "chrome://pnacl-translator/pnacl.json", |
| 76 &pp_llc_tool_name_var, | 42 &pp_llc_tool_name_var, |
| 77 &pp_ld_tool_name_var)) { | 43 &pp_ld_tool_name_var)) { |
| 78 coordinator_->ExitWithError(); | 44 coordinator_->ExitWithError(); |
| 79 } | 45 } |
| 80 | 46 |
| 81 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var); | 47 pp::Var llc_tool_name(pp::PASS_REF, pp_llc_tool_name_var); |
| 82 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var); | 48 pp::Var ld_tool_name(pp::PASS_REF, pp_ld_tool_name_var); |
| 83 llc_tool_name_ = GetFullUrl(llc_tool_name.AsString()); | 49 llc_tool_name_ = GetFullUrl(llc_tool_name.AsString()); |
| 84 ld_tool_name_ = GetFullUrl(ld_tool_name.AsString()); | 50 ld_tool_name_ = GetFullUrl(ld_tool_name.AsString()); |
| 85 pp::Module::Get()->core()->CallOnMainThread(0, resource_info_read_cb, PP_OK); | 51 pp::Module::Get()->core()->CallOnMainThread(0, resource_info_read_cb, PP_OK); |
| 86 } | 52 } |
| 87 | 53 |
| 88 PP_FileHandle PnaclResources::TakeLlcFileHandle() { | 54 PP_FileHandle PnaclResources::TakeLlcFileHandle() { |
| 89 PP_FileHandle to_return = llc_file_handle_; | 55 PP_FileHandle to_return = llc_file_handle_; |
| 90 llc_file_handle_ = PP_kInvalidFileHandle; | 56 llc_file_handle_ = PP_kInvalidFileHandle; |
| 91 return to_return; | 57 return to_return; |
| 92 } | 58 } |
| 93 | 59 |
| 94 PP_FileHandle PnaclResources::TakeLdFileHandle() { | 60 PP_FileHandle PnaclResources::TakeLdFileHandle() { |
| 95 PP_FileHandle to_return = ld_file_handle_; | 61 PP_FileHandle to_return = ld_file_handle_; |
| 96 ld_file_handle_ = PP_kInvalidFileHandle; | 62 ld_file_handle_ = PP_kInvalidFileHandle; |
| 97 return to_return; | 63 return to_return; |
| 98 } | 64 } |
| 99 | 65 |
| 100 void PnaclResources::StartLoad( | 66 void PnaclResources::StartLoad( |
| 101 const pp::CompletionCallback& all_loaded_callback) { | 67 const pp::CompletionCallback& all_loaded_callback) { |
| 102 PLUGIN_PRINTF(("PnaclResources::StartLoad\n")); | 68 PLUGIN_PRINTF(("PnaclResources::StartLoad\n")); |
| 103 | 69 |
| 104 // Do a blocking load of each of the resources. | 70 // Do a blocking load of each of the resources. |
| 105 nacl::string llc_filename = | |
| 106 PnaclUrls::PnaclComponentURLToFilename(llc_tool_name_); | |
| 107 llc_file_handle_ = | 71 llc_file_handle_ = |
| 108 plugin_->nacl_interface()->GetReadonlyPnaclFd(llc_filename.c_str()); | 72 plugin_->nacl_interface()->GetReadonlyPnaclFd(llc_tool_name_.c_str()); |
| 109 | |
| 110 nacl::string ld_filename = | |
| 111 PnaclUrls::PnaclComponentURLToFilename(ld_tool_name_); | |
| 112 ld_file_handle_ = | 73 ld_file_handle_ = |
| 113 plugin_->nacl_interface()->GetReadonlyPnaclFd(ld_filename.c_str()); | 74 plugin_->nacl_interface()->GetReadonlyPnaclFd(ld_tool_name_.c_str()); |
| 114 | 75 |
| 115 int32_t result = PP_OK; | 76 int32_t result = PP_OK; |
| 116 if (llc_file_handle_ == PP_kInvalidFileHandle || | 77 if (llc_file_handle_ == PP_kInvalidFileHandle || |
| 117 ld_file_handle_ == PP_kInvalidFileHandle) { | 78 ld_file_handle_ == PP_kInvalidFileHandle) { |
| 118 // File-open failed. Assume this means that the file is | 79 // File-open failed. Assume this means that the file is |
| 119 // not actually installed. This shouldn't actually occur since | 80 // not actually installed. This shouldn't actually occur since |
| 120 // ReadResourceInfo() fail first. | 81 // ReadResourceInfo() fail first. |
| 121 coordinator_->ReportNonPpapiError( | 82 coordinator_->ReportNonPpapiError( |
| 122 PP_NACL_ERROR_PNACL_RESOURCE_FETCH, | 83 PP_NACL_ERROR_PNACL_RESOURCE_FETCH, |
| 123 nacl::string("The Portable Native Client (pnacl) component is not " | 84 nacl::string("The Portable Native Client (pnacl) component is not " |
| 124 "installed. Please consult chrome://components for more " | 85 "installed. Please consult chrome://components for more " |
| 125 "information.")); | 86 "information.")); |
| 126 result = PP_ERROR_FILENOTFOUND; | 87 result = PP_ERROR_FILENOTFOUND; |
| 127 } | 88 } |
| 128 pp::Module::Get()->core()->CallOnMainThread(0, all_loaded_callback, result); | 89 pp::Module::Get()->core()->CallOnMainThread(0, all_loaded_callback, result); |
| 129 } | 90 } |
| 130 | 91 |
| 131 } // namespace plugin | 92 } // namespace plugin |
| OLD | NEW |