| 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/file_downloader.h" | 5 #include "ppapi/native_client/src/trusted/plugin/file_downloader.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 struct NaClFileInfo NoFileInfo() { | 26 struct NaClFileInfo NoFileInfo() { |
| 27 struct NaClFileInfo info; | 27 struct NaClFileInfo info; |
| 28 memset(&info, 0, sizeof(info)); | 28 memset(&info, 0, sizeof(info)); |
| 29 info.desc = -1; | 29 info.desc = -1; |
| 30 return info; | 30 return info; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Converts a PP_FileHandle to a POSIX file descriptor. | |
| 34 int32_t ConvertFileDescriptor(PP_FileHandle handle) { | |
| 35 PLUGIN_PRINTF(("ConvertFileDescriptor, handle=%d\n", handle)); | |
| 36 #if NACL_WINDOWS | |
| 37 int32_t file_desc = NACL_NO_FILE_DESC; | |
| 38 // On Windows, valid handles are 32 bit unsigned integers so this is safe. | |
| 39 file_desc = reinterpret_cast<uintptr_t>(handle); | |
| 40 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. | |
| 41 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); | |
| 42 if (posix_desc == -1) { | |
| 43 // Close the Windows HANDLE if it can't be converted. | |
| 44 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); | |
| 45 return -1; | |
| 46 } | |
| 47 return posix_desc; | |
| 48 #else | |
| 49 return handle; | |
| 50 #endif | |
| 51 } | |
| 52 | |
| 53 } // namespace | 33 } // namespace |
| 54 | 34 |
| 55 namespace plugin { | 35 namespace plugin { |
| 56 | 36 |
| 57 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser() | 37 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser() |
| 58 : info_(NoFileInfo()) {} | 38 : info_(NoFileInfo()) {} |
| 59 | 39 |
| 60 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo* pass_ownership) | 40 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo* pass_ownership) |
| 61 : info_(*pass_ownership) { | 41 : info_(*pass_ownership) { |
| 62 *pass_ownership = NoFileInfo(); | 42 *pass_ownership = NoFileInfo(); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 // invocation of the user callback. The user code will not be reentered. | 148 // invocation of the user callback. The user code will not be reentered. |
| 169 pp::CompletionCallback onload_callback = | 149 pp::CompletionCallback onload_callback = |
| 170 callback_factory_.NewCallback(&FileDownloader::URLLoadStartNotify); | 150 callback_factory_.NewCallback(&FileDownloader::URLLoadStartNotify); |
| 171 int32_t pp_error = url_loader_.Open(url_request, onload_callback); | 151 int32_t pp_error = url_loader_.Open(url_request, onload_callback); |
| 172 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%" NACL_PRId32 ")\n", | 152 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%" NACL_PRId32 ")\n", |
| 173 pp_error)); | 153 pp_error)); |
| 174 CHECK(pp_error == PP_OK_COMPLETIONPENDING); | 154 CHECK(pp_error == PP_OK_COMPLETIONPENDING); |
| 175 return true; | 155 return true; |
| 176 } | 156 } |
| 177 | 157 |
| 178 void FileDownloader::OpenFast(const nacl::string& url, | |
| 179 PP_FileHandle file_handle, | |
| 180 uint64_t file_token_lo, uint64_t file_token_hi) { | |
| 181 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); | |
| 182 | |
| 183 file_info_.FreeResources(); | |
| 184 CHECK(instance_ != NULL); | |
| 185 status_code_ = NACL_HTTP_STATUS_OK; | |
| 186 url_ = url; | |
| 187 mode_ = DOWNLOAD_NONE; | |
| 188 if (file_handle != PP_kInvalidFileHandle) { | |
| 189 NaClFileInfo tmp_info = NoFileInfo(); | |
| 190 tmp_info.desc = ConvertFileDescriptor(file_handle, true); | |
| 191 tmp_info.file_token.lo = file_token_lo; | |
| 192 tmp_info.file_token.hi = file_token_hi; | |
| 193 file_info_.TakeOwnership(&tmp_info); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 NaClFileInfo FileDownloader::GetFileInfo() { | |
| 198 NaClFileInfo info_to_return = NoFileInfo(); | |
| 199 | |
| 200 PLUGIN_PRINTF(("FileDownloader::GetFileInfo, this %p\n", this)); | |
| 201 if (file_info_.get_desc() != -1) { | |
| 202 info_to_return = file_info_.Release(); | |
| 203 } | |
| 204 PLUGIN_PRINTF(("FileDownloader::GetFileInfo -- returning %d\n", | |
| 205 info_to_return.desc)); | |
| 206 return info_to_return; | |
| 207 } | |
| 208 | |
| 209 bool FileDownloader::InitialResponseIsValid() { | 158 bool FileDownloader::InitialResponseIsValid() { |
| 210 // Process the response, validating the headers to confirm successful loading. | 159 // Process the response, validating the headers to confirm successful loading. |
| 211 url_response_ = url_loader_.GetResponseInfo(); | 160 url_response_ = url_loader_.GetResponseInfo(); |
| 212 if (url_response_.is_null()) { | 161 if (url_response_.is_null()) { |
| 213 PLUGIN_PRINTF(( | 162 PLUGIN_PRINTF(( |
| 214 "FileDownloader::InitialResponseIsValid (url_response_=NULL)\n")); | 163 "FileDownloader::InitialResponseIsValid (url_response_=NULL)\n")); |
| 215 return false; | 164 return false; |
| 216 } | 165 } |
| 217 | 166 |
| 218 pp::Var full_url = url_response_.GetURL(); | 167 pp::Var full_url = url_response_.GetURL(); |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 if (pp_error == PP_OK) { | 357 if (pp_error == PP_OK) { |
| 409 NaClFileInfo tmp_info = NoFileInfo(); | 358 NaClFileInfo tmp_info = NoFileInfo(); |
| 410 tmp_info.desc = ConvertFileDescriptor(handle, false); | 359 tmp_info.desc = ConvertFileDescriptor(handle, false); |
| 411 file_info_.TakeOwnership(&tmp_info); | 360 file_info_.TakeOwnership(&tmp_info); |
| 412 } | 361 } |
| 413 | 362 |
| 414 stream_finish_callback_.RunAndClear(pp_error); | 363 stream_finish_callback_.RunAndClear(pp_error); |
| 415 } | 364 } |
| 416 | 365 |
| 417 } // namespace plugin | 366 } // namespace plugin |
| OLD | NEW |