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 |
11 #include "native_client/src/include/portability_io.h" | 11 #include "native_client/src/include/portability_io.h" |
12 #include "native_client/src/shared/platform/nacl_check.h" | 12 #include "native_client/src/shared/platform/nacl_check.h" |
13 #include "native_client/src/shared/platform/nacl_time.h" | 13 #include "native_client/src/shared/platform/nacl_time.h" |
14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/c/ppb_file_io.h" | 15 #include "ppapi/c/ppb_file_io.h" |
16 #include "ppapi/cpp/file_io.h" | 16 #include "ppapi/cpp/file_io.h" |
17 #include "ppapi/cpp/file_ref.h" | 17 #include "ppapi/cpp/file_ref.h" |
18 #include "ppapi/cpp/url_request_info.h" | 18 #include "ppapi/cpp/url_request_info.h" |
19 #include "ppapi/cpp/url_response_info.h" | 19 #include "ppapi/cpp/url_response_info.h" |
20 #include "ppapi/native_client/src/trusted/plugin/callback_source.h" | 20 #include "ppapi/native_client/src/trusted/plugin/callback_source.h" |
| 21 #include "ppapi/native_client/src/trusted/plugin/file_utils.h" |
21 #include "ppapi/native_client/src/trusted/plugin/plugin.h" | 22 #include "ppapi/native_client/src/trusted/plugin/plugin.h" |
22 #include "ppapi/native_client/src/trusted/plugin/utility.h" | 23 #include "ppapi/native_client/src/trusted/plugin/utility.h" |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 const int32_t kExtensionUrlRequestStatusOk = 200; | 27 const int32_t kExtensionUrlRequestStatusOk = 200; |
27 const int32_t kDataUriRequestStatusOk = 0; | 28 const int32_t kDataUriRequestStatusOk = 0; |
28 | 29 |
29 struct NaClFileInfo NoFileInfo() { | 30 struct NaClFileInfo NoFileInfo() { |
30 struct NaClFileInfo info; | 31 struct NaClFileInfo info; |
31 memset(&info, 0, sizeof(info)); | 32 memset(&info, 0, sizeof(info)); |
32 info.desc = -1; | 33 info.desc = -1; |
33 return info; | 34 return info; |
34 } | 35 } |
35 | 36 |
36 // Converts a PP_FileHandle to a POSIX file descriptor. | |
37 int32_t ConvertFileDescriptor(PP_FileHandle handle) { | |
38 PLUGIN_PRINTF(("ConvertFileDescriptor, handle=%d\n", handle)); | |
39 #if NACL_WINDOWS | |
40 int32_t file_desc = NACL_NO_FILE_DESC; | |
41 // On Windows, valid handles are 32 bit unsigned integers so this is safe. | |
42 file_desc = reinterpret_cast<uintptr_t>(handle); | |
43 // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. | |
44 int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); | |
45 if (posix_desc == -1) { | |
46 // Close the Windows HANDLE if it can't be converted. | |
47 CloseHandle(reinterpret_cast<HANDLE>(file_desc)); | |
48 return -1; | |
49 } | |
50 return posix_desc; | |
51 #else | |
52 return handle; | |
53 #endif | |
54 } | |
55 | |
56 } // namespace | 37 } // namespace |
57 | 38 |
58 namespace plugin { | 39 namespace plugin { |
59 | 40 |
60 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser() | 41 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser() |
61 : info_(NoFileInfo()) {} | 42 : info_(NoFileInfo()) {} |
62 | 43 |
63 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo* pass_ownership) | 44 NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo* pass_ownership) |
64 : info_(*pass_ownership) { | 45 : info_(*pass_ownership) { |
65 *pass_ownership = NoFileInfo(); | 46 *pass_ownership = NoFileInfo(); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); | 191 PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); |
211 | 192 |
212 file_info_.FreeResources(); | 193 file_info_.FreeResources(); |
213 CHECK(instance_ != NULL); | 194 CHECK(instance_ != NULL); |
214 open_time_ = NaClGetTimeOfDayMicroseconds(); | 195 open_time_ = NaClGetTimeOfDayMicroseconds(); |
215 status_code_ = NACL_HTTP_STATUS_OK; | 196 status_code_ = NACL_HTTP_STATUS_OK; |
216 url_ = url; | 197 url_ = url; |
217 mode_ = DOWNLOAD_NONE; | 198 mode_ = DOWNLOAD_NONE; |
218 if (file_handle != PP_kInvalidFileHandle) { | 199 if (file_handle != PP_kInvalidFileHandle) { |
219 NaClFileInfo tmp_info = NoFileInfo(); | 200 NaClFileInfo tmp_info = NoFileInfo(); |
220 tmp_info.desc = ConvertFileDescriptor(file_handle); | 201 tmp_info.desc = file_utils::ConvertFileDescriptor(file_handle); |
221 tmp_info.file_token.lo = file_token_lo; | 202 tmp_info.file_token.lo = file_token_lo; |
222 tmp_info.file_token.hi = file_token_hi; | 203 tmp_info.file_token.hi = file_token_hi; |
223 file_info_.TakeOwnership(&tmp_info); | 204 file_info_.TakeOwnership(&tmp_info); |
224 } | 205 } |
225 } | 206 } |
226 | 207 |
227 NaClFileInfo FileDownloader::GetFileInfo() { | 208 NaClFileInfo FileDownloader::GetFileInfo() { |
228 NaClFileInfo info_to_return = NoFileInfo(); | 209 NaClFileInfo info_to_return = NoFileInfo(); |
229 | 210 |
230 PLUGIN_PRINTF(("FileDownloader::GetFileInfo, this %p\n", this)); | 211 PLUGIN_PRINTF(("FileDownloader::GetFileInfo, this %p\n", this)); |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 file_reader_.pp_resource(), cb.output(), cb.pp_completion_callback()); | 443 file_reader_.pp_resource(), cb.output(), cb.pp_completion_callback()); |
463 } | 444 } |
464 | 445 |
465 void FileDownloader::GotFileHandleNotify(int32_t pp_error, | 446 void FileDownloader::GotFileHandleNotify(int32_t pp_error, |
466 PP_FileHandle handle) { | 447 PP_FileHandle handle) { |
467 PLUGIN_PRINTF(( | 448 PLUGIN_PRINTF(( |
468 "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n", | 449 "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n", |
469 pp_error)); | 450 pp_error)); |
470 if (pp_error == PP_OK) { | 451 if (pp_error == PP_OK) { |
471 NaClFileInfo tmp_info = NoFileInfo(); | 452 NaClFileInfo tmp_info = NoFileInfo(); |
472 tmp_info.desc = ConvertFileDescriptor(handle); | 453 tmp_info.desc = file_utils::ConvertFileDescriptor(handle); |
473 file_info_.TakeOwnership(&tmp_info); | 454 file_info_.TakeOwnership(&tmp_info); |
474 } | 455 } |
475 | 456 |
476 stream_finish_callback_.RunAndClear(pp_error); | 457 stream_finish_callback_.RunAndClear(pp_error); |
477 } | 458 } |
478 | 459 |
479 } // namespace plugin | 460 } // namespace plugin |
OLD | NEW |