diff options
author | teravest@chromium.org <teravest@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-03 03:34:58 +0000 |
---|---|---|
committer | teravest@chromium.org <teravest@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-03 03:34:58 +0000 |
commit | a7855d0a8bb7405ed0f548a6d806e522928e8816 (patch) | |
tree | 953ae508ff0dbdb323a054c4740399d2fe9f8f79 /ppapi | |
parent | 4f291ec744c7a3f5c7f04b5d53caf04911dbbd83 (diff) | |
download | chromium_src-a7855d0a8bb7405ed0f548a6d806e522928e8816.zip chromium_src-a7855d0a8bb7405ed0f548a6d806e522928e8816.tar.gz chromium_src-a7855d0a8bb7405ed0f548a6d806e522928e8816.tar.bz2 |
NaCl: FileDownloader now uses FileIO_Private.
This changes the implementation of FileDownloader to use FileIO_Private instead
of FileIOTrusted. This allows us to get rid of the FileIOTrusted interface.
BUG=246396
Review URL: https://codereview.chromium.org/25258004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@226677 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/native_client/src/trusted/plugin/file_downloader.cc | 104 | ||||
-rw-r--r-- | ppapi/native_client/src/trusted/plugin/file_downloader.h | 9 |
2 files changed, 70 insertions, 43 deletions
diff --git a/ppapi/native_client/src/trusted/plugin/file_downloader.cc b/ppapi/native_client/src/trusted/plugin/file_downloader.cc index 21f7075..4bc1d4a 100644 --- a/ppapi/native_client/src/trusted/plugin/file_downloader.cc +++ b/ppapi/native_client/src/trusted/plugin/file_downloader.cc @@ -33,6 +33,26 @@ struct NaClFileInfo NoFileInfo() { return info; } +// Converts a PP_FileHandle to a POSIX file descriptor. +int32_t ConvertFileDescriptor(PP_FileHandle handle) { + PLUGIN_PRINTF(("ConvertFileDescriptor, handle=%d\n", handle)); +#if NACL_WINDOWS + int32_t file_desc = NACL_NO_FILE_DESC; + // On Windows, valid handles are 32 bit unsigned integers so this is safe. + file_desc = reinterpret_cast<uintptr_t>(handle); + // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. + int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); + if (posix_desc == -1) { + // Close the Windows HANDLE if it can't be converted. + CloseHandle(reinterpret_cast<HANDLE>(file_desc)); + return -1; + } + return posix_desc; +#else + return handle; +#endif +} + } // namespace namespace plugin { @@ -44,11 +64,12 @@ void FileDownloader::Initialize(Plugin* instance) { CHECK(instance_ == NULL); // Can only initialize once. instance_ = instance; callback_factory_.Initialize(this); - file_io_trusted_interface_ = static_cast<const PPB_FileIOTrusted*>( - pp::Module::Get()->GetBrowserInterface(PPB_FILEIOTRUSTED_INTERFACE)); + file_io_private_interface_ = static_cast<const PPB_FileIO_Private*>( + pp::Module::Get()->GetBrowserInterface(PPB_FILEIO_PRIVATE_INTERFACE)); url_loader_trusted_interface_ = static_cast<const PPB_URLLoaderTrusted*>( pp::Module::Get()->GetBrowserInterface(PPB_URLLOADERTRUSTED_INTERFACE)); temp_buffer_.resize(kTempBufferSize); + cached_file_info_ = NoFileInfo(); } bool FileDownloader::OpenStream( @@ -69,7 +90,7 @@ bool FileDownloader::Open( PLUGIN_PRINTF(("FileDownloader::Open (url=%s)\n", url.c_str())); if (callback.pp_completion_callback().func == NULL || instance_ == NULL || - file_io_trusted_interface_ == NULL) + file_io_private_interface_ == NULL) return false; CHECK(instance_ != NULL); @@ -80,6 +101,7 @@ bool FileDownloader::Open( file_open_notify_callback_ = callback; mode_ = mode; buffer_.clear(); + cached_file_info_ = NoFileInfo(); pp::URLRequestInfo url_request(instance_); // Allow CORS. @@ -154,6 +176,8 @@ void FileDownloader::OpenFast(const nacl::string& url, PP_FileHandle file_handle, uint64_t file_token_lo, uint64_t file_token_hi) { PLUGIN_PRINTF(("FileDownloader::OpenFast (url=%s)\n", url.c_str())); + + cached_file_info_ = NoFileInfo(); CHECK(instance_ != NULL); open_time_ = NaClGetTimeOfDayMicroseconds(); status_code_ = NACL_HTTP_STATUS_OK; @@ -166,41 +190,16 @@ void FileDownloader::OpenFast(const nacl::string& url, } struct NaClFileInfo FileDownloader::GetFileInfo() { - struct NaClFileInfo info = NoFileInfo(); - int32_t file_desc = NACL_NO_FILE_DESC; - if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { -#if NACL_WINDOWS - // On Windows, valid handles are 32 bit unsigned integers so this is safe. - file_desc = reinterpret_cast<uintptr_t>(file_handle_); -#else - file_desc = file_handle_; -#endif - info.file_token = file_token_; - } else { - if (!streaming_to_file()) { - return NoFileInfo(); - } - // Use the trusted interface to get the file descriptor. - if (file_io_trusted_interface_ == NULL) { - return NoFileInfo(); - } - file_desc = file_io_trusted_interface_->GetOSFileDescriptor( - file_reader_.pp_resource()); + PLUGIN_PRINTF(("FileDownloader::GetFileInfo\n")); + if (cached_file_info_.desc != -1) { + return cached_file_info_; + } else if (not_streaming() && file_handle_ != PP_kInvalidFileHandle) { + cached_file_info_.desc = ConvertFileDescriptor(file_handle_); + if (cached_file_info_.desc != -1) + cached_file_info_.file_token = file_token_; + return cached_file_info_; } - -#if NACL_WINDOWS - // Convert the Windows HANDLE from Pepper to a POSIX file descriptor. - int32_t posix_desc = _open_osfhandle(file_desc, _O_RDWR | _O_BINARY); - if (posix_desc == -1) { - // Close the Windows HANDLE if it can't be converted. - CloseHandle(reinterpret_cast<HANDLE>(file_desc)); - return NoFileInfo(); - } - file_desc = posix_desc; -#endif - - info.desc = file_desc; - return info; + return NoFileInfo(); } int64_t FileDownloader::TimeSinceOpenMilliseconds() const { @@ -268,8 +267,10 @@ void FileDownloader::URLLoadStartNotify(int32_t pp_error) { return; } - if (open_and_stream_) - return FinishStreaming(file_open_notify_callback_); + if (open_and_stream_) { + FinishStreaming(file_open_notify_callback_); + return; + } file_open_notify_callback_.RunAndClear(PP_OK); } @@ -412,7 +413,19 @@ void FileDownloader::StreamFinishNotify(int32_t pp_error) { PLUGIN_PRINTF(( "FileDownloader::StreamFinishNotify (pp_error=%" NACL_PRId32 ")\n", pp_error)); - stream_finish_callback_.RunAndClear(pp_error); + + // Run the callback if we have an error, or if we don't have a file_reader_ + // to get a file handle for. + if (pp_error != PP_OK || file_reader_.pp_resource() == 0) { + stream_finish_callback_.RunAndClear(pp_error); + return; + } + + pp::CompletionCallbackWithOutput<PP_FileHandle> cb = + callback_factory_.NewCallbackWithOutput( + &FileDownloader::GotFileHandleNotify); + file_io_private_interface_->RequestOSFileHandle( + file_reader_.pp_resource(), cb.output(), cb.pp_completion_callback()); } bool FileDownloader::streaming_to_file() const { @@ -431,4 +444,15 @@ bool FileDownloader::not_streaming() const { return mode_ == DOWNLOAD_NONE; } +void FileDownloader::GotFileHandleNotify(int32_t pp_error, + PP_FileHandle handle) { + PLUGIN_PRINTF(( + "FileDownloader::GotFileHandleNotify (pp_error=%" NACL_PRId32 ")\n", + pp_error)); + if (pp_error == PP_OK) + cached_file_info_.desc = ConvertFileDescriptor(handle); + + stream_finish_callback_.RunAndClear(pp_error); +} + } // namespace plugin diff --git a/ppapi/native_client/src/trusted/plugin/file_downloader.h b/ppapi/native_client/src/trusted/plugin/file_downloader.h index c0045ed..028b5c9 100644 --- a/ppapi/native_client/src/trusted/plugin/file_downloader.h +++ b/ppapi/native_client/src/trusted/plugin/file_downloader.h @@ -11,7 +11,7 @@ #include "native_client/src/include/nacl_string.h" #include "native_client/src/trusted/validator/nacl_file_info.h" #include "ppapi/c/private/pp_file_handle.h" -#include "ppapi/c/trusted/ppb_file_io_trusted.h" +#include "ppapi/c/private/ppb_file_io_private.h" #include "ppapi/c/trusted/ppb_url_loader_trusted.h" #include "ppapi/cpp/file_io.h" #include "ppapi/cpp/instance.h" @@ -52,7 +52,7 @@ class FileDownloader { file_open_notify_callback_(pp::BlockUntilComplete()), stream_finish_callback_(pp::BlockUntilComplete()), file_handle_(PP_kInvalidFileHandle), - file_io_trusted_interface_(NULL), + file_io_private_interface_(NULL), url_loader_trusted_interface_(NULL), open_time_(-1), mode_(DOWNLOAD_NONE), @@ -171,6 +171,8 @@ class FileDownloader { void URLReadBodyNotify(int32_t pp_error); void StreamFinishNotify(int32_t pp_error); + void GotFileHandleNotify(int32_t pp_error, PP_FileHandle handle); + Plugin* instance_; nacl::string url_to_open_; nacl::string url_; @@ -180,7 +182,7 @@ class FileDownloader { pp::FileIO file_reader_; PP_FileHandle file_handle_; struct NaClFileToken file_token_; - const PPB_FileIOTrusted* file_io_trusted_interface_; + const PPB_FileIO_Private* file_io_private_interface_; const PPB_URLLoaderTrusted* url_loader_trusted_interface_; pp::URLLoader url_loader_; pp::CompletionCallbackFactory<FileDownloader> callback_factory_; @@ -193,6 +195,7 @@ class FileDownloader { std::deque<char> buffer_; UrlSchemeType url_scheme_; StreamCallbackSource* data_stream_callback_source_; + NaClFileInfo cached_file_info_; }; } // namespace plugin; #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ |