Chromium Code Reviews| 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 "native_client/src/trusted/plugin/file_downloader.h" | 5 #include "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> | 9 #include <string> |
| 9 | 10 |
| 10 #include "native_client/src/include/portability_io.h" | 11 #include "native_client/src/include/portability_io.h" |
| 11 #include "native_client/src/shared/platform/nacl_check.h" | 12 #include "native_client/src/shared/platform/nacl_check.h" |
| 12 #include "native_client/src/shared/platform/nacl_time.h" | 13 #include "native_client/src/shared/platform/nacl_time.h" |
| 13 #include "native_client/src/trusted/plugin/callback_source.h" | 14 #include "native_client/src/trusted/plugin/callback_source.h" |
| 14 #include "native_client/src/trusted/plugin/plugin.h" | 15 #include "native_client/src/trusted/plugin/plugin.h" |
| 15 #include "native_client/src/trusted/plugin/utility.h" | 16 #include "native_client/src/trusted/plugin/utility.h" |
| 16 #include "ppapi/c/pp_errors.h" | 17 #include "ppapi/c/pp_errors.h" |
| 17 #include "ppapi/c/ppb_file_io.h" | 18 #include "ppapi/c/ppb_file_io.h" |
| 18 #include "ppapi/cpp/file_io.h" | 19 #include "ppapi/cpp/file_io.h" |
| 19 #include "ppapi/cpp/file_ref.h" | 20 #include "ppapi/cpp/file_ref.h" |
| 20 #include "ppapi/cpp/url_request_info.h" | 21 #include "ppapi/cpp/url_request_info.h" |
| 21 #include "ppapi/cpp/url_response_info.h" | 22 #include "ppapi/cpp/url_response_info.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
|
Mark Seaborn
2013/05/16 23:01:47
Add empty line after this
Nick Bray (chromium)
2013/05/21 20:09:06
Done.
| |
| 24 const int32_t kExtensionUrlRequestStatusOk = 200; | 25 const int32_t kExtensionUrlRequestStatusOk = 200; |
| 25 const int32_t kDataUriRequestStatusOk = 0; | 26 const int32_t kDataUriRequestStatusOk = 0; |
| 27 | |
| 28 struct NaClFileInfo NoFileInfo() { | |
|
Mark Seaborn
2013/05/16 23:01:47
'struct NaClFileInfo' -> 'NaClFileInfo' in C++? S
Nick Bray (chromium)
2013/05/21 20:09:06
Staying consistent with the C code base.
| |
| 29 struct NaClFileInfo info; | |
| 30 memset(&info, 0, sizeof(info)); | |
| 31 info.desc = -1; | |
| 32 return info; | |
| 33 } | |
| 34 | |
| 26 } | 35 } |
| 27 | 36 |
| 28 namespace plugin { | 37 namespace plugin { |
| 29 | 38 |
| 30 void FileDownloader::Initialize(Plugin* instance) { | 39 void FileDownloader::Initialize(Plugin* instance) { |
| 31 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", | 40 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", |
| 32 static_cast<void*>(this))); | 41 static_cast<void*>(this))); |
| 33 CHECK(instance != NULL); | 42 CHECK(instance != NULL); |
| 34 CHECK(instance_ == NULL); // Can only initialize once. | 43 CHECK(instance_ == NULL); // Can only initialize once. |
| 35 instance_ = instance; | 44 instance_ = instance; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 // invocation of the user callback. The user code will not be reentered. | 147 // invocation of the user callback. The user code will not be reentered. |
| 139 pp::CompletionCallback onload_callback = | 148 pp::CompletionCallback onload_callback = |
| 140 callback_factory_.NewCallback(start_notify); | 149 callback_factory_.NewCallback(start_notify); |
| 141 int32_t pp_error = url_loader_.Open(url_request, onload_callback); | 150 int32_t pp_error = url_loader_.Open(url_request, onload_callback); |
| 142 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%"NACL_PRId32")\n", pp_error)); | 151 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%"NACL_PRId32")\n", pp_error)); |
| 143 CHECK(pp_error == PP_OK_COMPLETIONPENDING); | 152 CHECK(pp_error == PP_OK_COMPLETIONPENDING); |
| 144 return true; | 153 return true; |
| 145 } | 154 } |
| 146 | 155 |
| 147 void FileDownloader::OpenFast(const nacl::string& url, | 156 void FileDownloader::OpenFast(const nacl::string& url, |
| 148 PP_FileHandle file_handle) { | 157 PP_FileHandle file_handle, |
| 158 uint64_t nonce) { | |
| 149 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); | 159 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); |
| 150 CHECK(instance_ != NULL); | 160 CHECK(instance_ != NULL); |
| 151 open_time_ = NaClGetTimeOfDayMicroseconds(); | 161 open_time_ = NaClGetTimeOfDayMicroseconds(); |
| 152 status_code_ = NACL_HTTP_STATUS_OK; | 162 status_code_ = NACL_HTTP_STATUS_OK; |
| 153 url_to_open_ = url; | 163 url_to_open_ = url; |
| 154 url_ = url; | 164 url_ = url; |
| 155 mode_ = DOWNLOAD_NONE; | 165 mode_ = DOWNLOAD_NONE; |
| 156 file_handle_ = file_handle; | 166 file_handle_ = file_handle; |
| 167 nonce_ = nonce; | |
| 157 } | 168 } |
| 158 | 169 |
| 159 int32_t FileDownloader::GetPOSIXFileDescriptor() { | 170 struct NaClFileInfo FileDownloader::GetFileInfo() { |
| 171 struct NaClFileInfo info = NoFileInfo(); | |
| 160 int32_t file_desc = NACL_NO_FILE_DESC; | 172 int32_t file_desc = NACL_NO_FILE_DESC; |
| 161 if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { | 173 if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { |
| 162 #if NACL_WINDOWS | 174 #if NACL_WINDOWS |
| 163 // On Windows, valid handles are 32 bit unsigned integers so this is safe. | 175 // On Windows, valid handles are 32 bit unsigned integers so this is safe. |
| 164 file_desc = reinterpret_cast<uintptr_t>(file_handle_); | 176 file_desc = reinterpret_cast<uintptr_t>(file_handle_); |
| 165 #else | 177 #else |
| 166 file_desc = file_handle_; | 178 file_desc = file_handle_; |
| 167 #endif | 179 #endif |
| 180 info.nonce = nonce_; | |
| 168 } else { | 181 } else { |
| 169 if (!streaming_to_file()) { | 182 if (!streaming_to_file()) { |
| 170 return NACL_NO_FILE_DESC; | 183 return NoFileInfo(); |
| 171 } | 184 } |
| 172 // Use the trusted interface to get the file descriptor. | 185 // Use the trusted interface to get the file descriptor. |
| 173 if (file_io_trusted_interface_ == NULL) { | 186 if (file_io_trusted_interface_ == NULL) { |
| 174 return NACL_NO_FILE_DESC; | 187 return NoFileInfo(); |
| 175 } | 188 } |
| 176 file_desc = file_io_trusted_interface_->GetOSFileDescriptor( | 189 file_desc = file_io_trusted_interface_->GetOSFileDescriptor( |
| 177 file_reader_.pp_resource()); | 190 file_reader_.pp_resource()); |
| 178 } | 191 } |
| 179 | 192 |
| 180 #if NACL_WINDOWS | 193 #if NACL_WINDOWS |
| 181 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. | 194 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. |
| 182 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); | 195 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); |
| 183 if (posix_desc == -1) { | 196 if (posix_desc == -1) { |
| 184 // Close the Windows HANDLE if it can't be converted. | 197 // Close the Windows HANDLE if it can't be converted. |
| 185 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); | 198 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); |
| 186 return NACL_NO_FILE_DESC; | 199 return NoFileInfo(); |
| 187 } | 200 } |
| 188 file_desc = posix_desc; | 201 file_desc = posix_desc; |
| 189 #endif | 202 #endif |
| 190 | 203 |
| 191 return file_desc; | 204 info.desc = file_desc; |
| 205 return info; | |
| 192 } | 206 } |
| 193 | 207 |
| 194 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { | 208 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { |
| 195 int64_t now = NaClGetTimeOfDayMicroseconds(); | 209 int64_t now = NaClGetTimeOfDayMicroseconds(); |
| 196 // If Open() wasn't called or we somehow return an earlier time now, just | 210 // If Open() wasn't called or we somehow return an earlier time now, just |
| 197 // return the 0 rather than worse nonsense values. | 211 // return the 0 rather than worse nonsense values. |
| 198 if (open_time_ < 0 || now < open_time_) | 212 if (open_time_ < 0 || now < open_time_) |
| 199 return 0; | 213 return 0; |
| 200 return (now - open_time_) / NACL_MICROS_PER_MILLI; | 214 return (now - open_time_) / NACL_MICROS_PER_MILLI; |
| 201 } | 215 } |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 | 412 |
| 399 bool FileDownloader::streaming_to_user() const { | 413 bool FileDownloader::streaming_to_user() const { |
| 400 return mode_ == DOWNLOAD_STREAM; | 414 return mode_ == DOWNLOAD_STREAM; |
| 401 } | 415 } |
| 402 | 416 |
| 403 bool FileDownloader::not_streaming() const { | 417 bool FileDownloader::not_streaming() const { |
| 404 return mode_ == DOWNLOAD_NONE; | 418 return mode_ == DOWNLOAD_NONE; |
| 405 } | 419 } |
| 406 | 420 |
| 407 } // namespace plugin | 421 } // namespace plugin |
| OLD | NEW |