| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 return posix_desc; | 50 return posix_desc; |
| 51 #else | 51 #else |
| 52 return handle; | 52 return handle; |
| 53 #endif | 53 #endif |
| 54 } | 54 } |
| 55 | 55 |
| 56 } // namespace | 56 } // namespace |
| 57 | 57 |
| 58 namespace plugin { | 58 namespace plugin { |
| 59 | 59 |
| 60 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser() |
| 61 : info_(NoFileInfo()) {} |
| 62 |
| 63 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo* pass_ownership) |
| 64 : info_(*pass_ownership) { |
| 65 *pass_ownership = NoFileInfo(); |
| 66 } |
| 67 |
| 68 void NaClFileInfoAutoCloser::FreeResources() { |
| 69 if (-1 != get_desc()) { |
| 70 PLUGIN_PRINTF(("NaClFileInfoAutoCloser::FreeResources close(%d)\n", |
| 71 get_desc())); |
| 72 close(get_desc()); |
| 73 } |
| 74 info_.desc = -1; |
| 75 } |
| 76 |
| 77 void NaClFileInfoAutoCloser::TakeOwnership(NaClFileInfo* pass_ownership) { |
| 78 PLUGIN_PRINTF(("NaClFileInfoAutoCloser::set: taking ownership of %d\n", |
| 79 pass_ownership->desc)); |
| 80 CHECK(pass_ownership->desc == -1 || pass_ownership->desc != get_desc()); |
| 81 FreeResources(); |
| 82 info_ = *pass_ownership; |
| 83 *pass_ownership = NoFileInfo(); |
| 84 } |
| 85 |
| 86 NaClFileInfo NaClFileInfoAutoCloser::Release() { |
| 87 NaClFileInfo info_to_return = info_; |
| 88 info_ = NoFileInfo(); |
| 89 return info_to_return; |
| 90 } |
| 91 |
| 60 void FileDownloader::Initialize(Plugin* instance) { | 92 void FileDownloader::Initialize(Plugin* instance) { |
| 61 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", | 93 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", |
| 62 static_cast<void*>(this))); | 94 static_cast<void*>(this))); |
| 63 CHECK(instance != NULL); | 95 CHECK(instance != NULL); |
| 64 CHECK(instance_ == NULL); // Can only initialize once. | 96 CHECK(instance_ == NULL); // Can only initialize once. |
| 65 instance_ = instance; | 97 instance_ = instance; |
| 66 callback_factory_.Initialize(this); | 98 callback_factory_.Initialize(this); |
| 67 file_io_private_interface_ = static_cast<const PPB_FileIO_Private*>( | 99 file_io_private_interface_ = static_cast<const PPB_FileIO_Private*>( |
| 68 pp::Module::Get()->GetBrowserInterface(PPB_FILEIO_PRIVATE_INTERFACE)); | 100 pp::Module::Get()->GetBrowserInterface(PPB_FILEIO_PRIVATE_INTERFACE)); |
| 69 url_loader_trusted_interface_ = static_cast<const PPB_URLLoaderTrusted*>( | 101 url_loader_trusted_interface_ = static_cast<const PPB_URLLoaderTrusted*>( |
| 70 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE)); | 102 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE)); |
| 71 temp_buffer_.resize(kTempBufferSize); | 103 temp_buffer_.resize(kTempBufferSize); |
| 72 cached_file_info_ = NoFileInfo(); | 104 file_info_.FreeResources(); |
| 73 } | 105 } |
| 74 | 106 |
| 75 bool FileDownloader::OpenStream( | 107 bool FileDownloader::OpenStream( |
| 76 const nacl::string& url, | 108 const nacl::string& url, |
| 77 const pp::CompletionCallback& callback, | 109 const pp::CompletionCallback& callback, |
| 78 StreamCallbackSource* stream_callback_source) { | 110 StreamCallbackSource* stream_callback_source) { |
| 79 open_and_stream_ = false; | 111 open_and_stream_ = false; |
| 80 data_stream_callback_source_ = stream_callback_source; | 112 data_stream_callback_source_ = stream_callback_source; |
| 81 return Open(url, DOWNLOAD_STREAM, callback, true, NULL); | 113 return Open(url, DOWNLOAD_STREAM, callback, true, NULL); |
| 82 } | 114 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 return false; | 126 return false; |
| 95 | 127 |
| 96 CHECK(instance_ != NULL); | 128 CHECK(instance_ != NULL); |
| 97 open_time_ = NaClGetTimeOfDayMicroseconds(); | 129 open_time_ = NaClGetTimeOfDayMicroseconds(); |
| 98 status_code_ = -1; | 130 status_code_ = -1; |
| 99 url_to_open_ = url; | 131 url_to_open_ = url; |
| 100 url_ = url; | 132 url_ = url; |
| 101 file_open_notify_callback_ = callback; | 133 file_open_notify_callback_ = callback; |
| 102 mode_ = mode; | 134 mode_ = mode; |
| 103 buffer_.clear(); | 135 buffer_.clear(); |
| 104 cached_file_info_ = NoFileInfo(); | 136 file_info_.FreeResources(); |
| 105 pp::URLRequestInfo url_request(instance_); | 137 pp::URLRequestInfo url_request(instance_); |
| 106 | 138 |
| 107 // Allow CORS. | 139 // Allow CORS. |
| 108 // Note that "SetAllowCrossOriginRequests" (currently) has the side effect of | 140 // Note that "SetAllowCrossOriginRequests" (currently) has the side effect of |
| 109 // preventing credentials from being sent on same-origin requests. We | 141 // preventing credentials from being sent on same-origin requests. We |
| 110 // therefore avoid setting this flag unless we know for sure it is a | 142 // therefore avoid setting this flag unless we know for sure it is a |
| 111 // cross-origin request, resulting in behavior similar to XMLHttpRequest. | 143 // cross-origin request, resulting in behavior similar to XMLHttpRequest. |
| 112 if (!instance_->DocumentCanRequest(url)) | 144 if (!instance_->DocumentCanRequest(url)) |
| 113 url_request.SetAllowCrossOriginRequests(true); | 145 url_request.SetAllowCrossOriginRequests(true); |
| 114 | 146 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 pp_error)); | 205 pp_error)); |
| 174 CHECK(pp_error == PP_OK_COMPLETIONPENDING); | 206 CHECK(pp_error == PP_OK_COMPLETIONPENDING); |
| 175 return true; | 207 return true; |
| 176 } | 208 } |
| 177 | 209 |
| 178 void FileDownloader::OpenFast(const nacl::string& url, | 210 void FileDownloader::OpenFast(const nacl::string& url, |
| 179 PP_FileHandle file_handle, | 211 PP_FileHandle file_handle, |
| 180 uint64_t file_token_lo, uint64_t file_token_hi) { | 212 uint64_t file_token_lo, uint64_t file_token_hi) { |
| 181 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); | 213 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); |
| 182 | 214 |
| 183 cached_file_info_ = NoFileInfo(); | 215 file_info_.FreeResources(); |
| 184 CHECK(instance_ != NULL); | 216 CHECK(instance_ != NULL); |
| 185 open_time_ = NaClGetTimeOfDayMicroseconds(); | 217 open_time_ = NaClGetTimeOfDayMicroseconds(); |
| 186 status_code_ = NACL_HTTP_STATUS_OK; | 218 status_code_ = NACL_HTTP_STATUS_OK; |
| 187 url_to_open_ = url; | 219 url_to_open_ = url; |
| 188 url_ = url; | 220 url_ = url; |
| 189 mode_ = DOWNLOAD_NONE; | 221 mode_ = DOWNLOAD_NONE; |
| 190 file_handle_ = file_handle; | 222 if (not_streaming() && file_handle != PP_kInvalidFileHandle) { |
| 191 file_token_.lo = file_token_lo; | 223 NaClFileInfo tmp_info = NoFileInfo(); |
| 192 file_token_.hi = file_token_hi; | 224 tmp_info.desc = ConvertFileDescriptor(file_handle); |
| 225 tmp_info.file_token.lo = file_token_lo; |
| 226 tmp_info.file_token.hi = file_token_hi; |
| 227 file_info_.TakeOwnership(&tmp_info); |
| 228 } |
| 193 } | 229 } |
| 194 | 230 |
| 195 struct NaClFileInfo FileDownloader::GetFileInfo() { | 231 NaClFileInfo FileDownloader::GetFileInfo() { |
| 196 PLUGIN_PRINTF(("FileDownloader::GetFileInfo\n")); | 232 NaClFileInfo info_to_return = NoFileInfo(); |
| 197 if (cached_file_info_.desc != -1) { | 233 |
| 198 return cached_file_info_; | 234 PLUGIN_PRINTF(("FileDownloader::GetFileInfo, this %p\n", this)); |
| 199 } else if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { | 235 if (file_info_.get_desc() != -1) { |
| 200 cached_file_info_.desc = ConvertFileDescriptor(file_handle_); | 236 info_to_return = file_info_.Release(); |
| 201 if (cached_file_info_.desc != -1) | |
| 202 cached_file_info_.file_token = file_token_; | |
| 203 return cached_file_info_; | |
| 204 } | 237 } |
| 205 return NoFileInfo(); | 238 PLUGIN_PRINTF(("FileDownloader::GetFileInfo -- returning %d\n", |
| 239 info_to_return.desc)); |
| 240 return info_to_return; |
| 206 } | 241 } |
| 207 | 242 |
| 208 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { | 243 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { |
| 209 int64_t now = NaClGetTimeOfDayMicroseconds(); | 244 int64_t now = NaClGetTimeOfDayMicroseconds(); |
| 210 // If Open() wasn't called or we somehow return an earlier time now, just | 245 // If Open() wasn't called or we somehow return an earlier time now, just |
| 211 // return the 0 rather than worse nonsense values. | 246 // return the 0 rather than worse nonsense values. |
| 212 if (open_time_ < 0 || now < open_time_) | 247 if (open_time_ < 0 || now < open_time_) |
| 213 return 0; | 248 return 0; |
| 214 return (now - open_time_) / NACL_MICROS_PER_MILLI; | 249 return (now - open_time_) / NACL_MICROS_PER_MILLI; |
| 215 } | 250 } |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 480 |
| 446 bool FileDownloader::not_streaming() const { | 481 bool FileDownloader::not_streaming() const { |
| 447 return mode_ == DOWNLOAD_NONE; | 482 return mode_ == DOWNLOAD_NONE; |
| 448 } | 483 } |
| 449 | 484 |
| 450 void FileDownloader::GotFileHandleNotify(int32_t pp_error, | 485 void FileDownloader::GotFileHandleNotify(int32_t pp_error, |
| 451 PP_FileHandle handle) { | 486 PP_FileHandle handle) { |
| 452 PLUGIN_PRINTF(( | 487 PLUGIN_PRINTF(( |
| 453 "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n", | 488 "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n", |
| 454 pp_error)); | 489 pp_error)); |
| 455 if (pp_error == PP_OK) | 490 if (pp_error == PP_OK) { |
| 456 cached_file_info_.desc = ConvertFileDescriptor(handle); | 491 NaClFileInfo tmp_info = NoFileInfo(); |
| 492 tmp_info.desc = ConvertFileDescriptor(handle); |
| 493 file_info_.TakeOwnership(&tmp_info); |
| 494 } |
| 457 | 495 |
| 458 stream_finish_callback_.RunAndClear(pp_error); | 496 stream_finish_callback_.RunAndClear(pp_error); |
| 459 } | 497 } |
| 460 | 498 |
| 461 } // namespace plugin | 499 } // namespace plugin |
| OLD | NEW |