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 { |
| 25 |
24 const int32_t kExtensionUrlRequestStatusOk = 200; | 26 const int32_t kExtensionUrlRequestStatusOk = 200; |
25 const int32_t kDataUriRequestStatusOk = 0; | 27 const int32_t kDataUriRequestStatusOk = 0; |
| 28 |
| 29 struct NaClFileInfo NoFileInfo() { |
| 30 struct NaClFileInfo info; |
| 31 memset(&info, 0, sizeof(info)); |
| 32 info.desc = -1; |
| 33 return info; |
| 34 } |
| 35 |
26 } | 36 } |
27 | 37 |
28 namespace plugin { | 38 namespace plugin { |
29 | 39 |
30 void FileDownloader::Initialize(Plugin* instance) { | 40 void FileDownloader::Initialize(Plugin* instance) { |
31 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", | 41 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", |
32 static_cast<void*>(this))); | 42 static_cast<void*>(this))); |
33 CHECK(instance != NULL); | 43 CHECK(instance != NULL); |
34 CHECK(instance_ == NULL); // Can only initialize once. | 44 CHECK(instance_ == NULL); // Can only initialize once. |
35 instance_ = instance; | 45 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. | 148 // invocation of the user callback. The user code will not be reentered. |
139 pp::CompletionCallback onload_callback = | 149 pp::CompletionCallback onload_callback = |
140 callback_factory_.NewCallback(start_notify); | 150 callback_factory_.NewCallback(start_notify); |
141 int32_t pp_error = url_loader_.Open(url_request, onload_callback); | 151 int32_t pp_error = url_loader_.Open(url_request, onload_callback); |
142 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%"NACL_PRId32")\n", pp_error)); | 152 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%"NACL_PRId32")\n", pp_error)); |
143 CHECK(pp_error == PP_OK_COMPLETIONPENDING); | 153 CHECK(pp_error == PP_OK_COMPLETIONPENDING); |
144 return true; | 154 return true; |
145 } | 155 } |
146 | 156 |
147 void FileDownloader::OpenFast(const nacl::string& url, | 157 void FileDownloader::OpenFast(const nacl::string& url, |
148 PP_FileHandle file_handle) { | 158 PP_FileHandle file_handle, |
| 159 uint64_t file_token_lo, uint64_t file_token_hi) { |
149 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); | 160 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); |
150 CHECK(instance_ != NULL); | 161 CHECK(instance_ != NULL); |
151 open_time_ = NaClGetTimeOfDayMicroseconds(); | 162 open_time_ = NaClGetTimeOfDayMicroseconds(); |
152 status_code_ = NACL_HTTP_STATUS_OK; | 163 status_code_ = NACL_HTTP_STATUS_OK; |
153 url_to_open_ = url; | 164 url_to_open_ = url; |
154 url_ = url; | 165 url_ = url; |
155 mode_ = DOWNLOAD_NONE; | 166 mode_ = DOWNLOAD_NONE; |
156 file_handle_ = file_handle; | 167 file_handle_ = file_handle; |
| 168 file_token_.lo = file_token_lo; |
| 169 file_token_.hi = file_token_hi; |
157 } | 170 } |
158 | 171 |
159 int32_t FileDownloader::GetPOSIXFileDescriptor() { | 172 struct NaClFileInfo FileDownloader::GetFileInfo() { |
| 173 struct NaClFileInfo info = NoFileInfo(); |
160 int32_t file_desc = NACL_NO_FILE_DESC; | 174 int32_t file_desc = NACL_NO_FILE_DESC; |
161 if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { | 175 if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { |
162 #if NACL_WINDOWS | 176 #if NACL_WINDOWS |
163 // On Windows, valid handles are 32 bit unsigned integers so this is safe. | 177 // On Windows, valid handles are 32 bit unsigned integers so this is safe. |
164 file_desc = reinterpret_cast<uintptr_t>(file_handle_); | 178 file_desc = reinterpret_cast<uintptr_t>(file_handle_); |
165 #else | 179 #else |
166 file_desc = file_handle_; | 180 file_desc = file_handle_; |
167 #endif | 181 #endif |
| 182 info.file_token = file_token_; |
168 } else { | 183 } else { |
169 if (!streaming_to_file()) { | 184 if (!streaming_to_file()) { |
170 return NACL_NO_FILE_DESC; | 185 return NoFileInfo(); |
171 } | 186 } |
172 // Use the trusted interface to get the file descriptor. | 187 // Use the trusted interface to get the file descriptor. |
173 if (file_io_trusted_interface_ == NULL) { | 188 if (file_io_trusted_interface_ == NULL) { |
174 return NACL_NO_FILE_DESC; | 189 return NoFileInfo(); |
175 } | 190 } |
176 file_desc = file_io_trusted_interface_->GetOSFileDescriptor( | 191 file_desc = file_io_trusted_interface_->GetOSFileDescriptor( |
177 file_reader_.pp_resource()); | 192 file_reader_.pp_resource()); |
178 } | 193 } |
179 | 194 |
180 #if NACL_WINDOWS | 195 #if NACL_WINDOWS |
181 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. | 196 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. |
182 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); | 197 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); |
183 if (posix_desc == -1) { | 198 if (posix_desc == -1) { |
184 // Close the Windows HANDLE if it can't be converted. | 199 // Close the Windows HANDLE if it can't be converted. |
185 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); | 200 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); |
186 return NACL_NO_FILE_DESC; | 201 return NoFileInfo(); |
187 } | 202 } |
188 file_desc = posix_desc; | 203 file_desc = posix_desc; |
189 #endif | 204 #endif |
190 | 205 |
191 return file_desc; | 206 info.desc = file_desc; |
| 207 return info; |
192 } | 208 } |
193 | 209 |
194 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { | 210 int64_t FileDownloader::TimeSinceOpenMilliseconds() const { |
195 int64_t now = NaClGetTimeOfDayMicroseconds(); | 211 int64_t now = NaClGetTimeOfDayMicroseconds(); |
196 // If Open() wasn't called or we somehow return an earlier time now, just | 212 // If Open() wasn't called or we somehow return an earlier time now, just |
197 // return the 0 rather than worse nonsense values. | 213 // return the 0 rather than worse nonsense values. |
198 if (open_time_ < 0 || now < open_time_) | 214 if (open_time_ < 0 || now < open_time_) |
199 return 0; | 215 return 0; |
200 return (now - open_time_) / NACL_MICROS_PER_MILLI; | 216 return (now - open_time_) / NACL_MICROS_PER_MILLI; |
201 } | 217 } |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 | 414 |
399 bool FileDownloader::streaming_to_user() const { | 415 bool FileDownloader::streaming_to_user() const { |
400 return mode_ == DOWNLOAD_STREAM; | 416 return mode_ == DOWNLOAD_STREAM; |
401 } | 417 } |
402 | 418 |
403 bool FileDownloader::not_streaming() const { | 419 bool FileDownloader::not_streaming() const { |
404 return mode_ == DOWNLOAD_NONE; | 420 return mode_ == DOWNLOAD_NONE; |
405 } | 421 } |
406 | 422 |
407 } // namespace plugin | 423 } // namespace plugin |
OLD | NEW |