Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Unified Diff: ppapi/native_client/src/trusted/plugin/file_downloader.cc

Issue 292323007: Pepper: FileDownloader cleanup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test fix Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/native_client/src/trusted/plugin/file_downloader.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ppapi/native_client/src/trusted/plugin/file_downloader.cc
diff --git a/ppapi/native_client/src/trusted/plugin/file_downloader.cc b/ppapi/native_client/src/trusted/plugin/file_downloader.cc
index 4a1b6ad16effbd5d7706cd13ab0d5635da4f565a..7c6c045a5af13f42ed2db46a1778e2d7372cb689 100644
--- a/ppapi/native_client/src/trusted/plugin/file_downloader.cc
+++ b/ppapi/native_client/src/trusted/plugin/file_downloader.cc
@@ -12,60 +12,14 @@
#include "native_client/src/shared/platform/nacl_check.h"
#include "native_client/src/shared/platform/nacl_time.h"
#include "ppapi/c/pp_errors.h"
-#include "ppapi/c/ppb_file_io.h"
-#include "ppapi/cpp/file_io.h"
-#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/url_request_info.h"
#include "ppapi/cpp/url_response_info.h"
#include "ppapi/native_client/src/trusted/plugin/callback_source.h"
#include "ppapi/native_client/src/trusted/plugin/plugin.h"
#include "ppapi/native_client/src/trusted/plugin/utility.h"
-namespace {
-
-struct NaClFileInfo NoFileInfo() {
- struct NaClFileInfo info;
- memset(&info, 0, sizeof(info));
- info.desc = -1;
- return info;
-}
-
-} // namespace
-
namespace plugin {
-NaClFileInfoAutoCloser::NaClFileInfoAutoCloser()
- : info_(NoFileInfo()) {}
-
-NaClFileInfoAutoCloser::NaClFileInfoAutoCloser(NaClFileInfo* pass_ownership)
- : info_(*pass_ownership) {
- *pass_ownership = NoFileInfo();
-}
-
-void NaClFileInfoAutoCloser::FreeResources() {
- if (-1 != get_desc()) {
- PLUGIN_PRINTF(("NaClFileInfoAutoCloser::FreeResources close(%d)\n",
- get_desc()));
- close(get_desc());
- }
- info_.desc = -1;
-}
-
-void NaClFileInfoAutoCloser::TakeOwnership(NaClFileInfo* pass_ownership) {
- PLUGIN_PRINTF(("NaClFileInfoAutoCloser::set: taking ownership of %d\n",
- pass_ownership->desc));
- CHECK(pass_ownership->desc == -1 || pass_ownership->desc != get_desc());
- FreeResources();
- info_ = *pass_ownership;
- *pass_ownership = NoFileInfo();
-}
-
-NaClFileInfo NaClFileInfoAutoCloser::Release() {
- NaClFileInfo info_to_return = info_;
- info_ = NoFileInfo();
- return info_to_return;
-}
-
void FileDownloader::Initialize(Plugin* instance) {
PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n",
static_cast<void*>(this)));
@@ -73,12 +27,7 @@ void FileDownloader::Initialize(Plugin* instance) {
CHECK(instance_ == NULL); // Can only initialize once.
instance_ = instance;
callback_factory_.Initialize(this);
- 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);
- file_info_.FreeResources();
}
bool FileDownloader::OpenStream(
@@ -86,27 +35,14 @@ bool FileDownloader::OpenStream(
const pp::CompletionCallback& callback,
StreamCallbackSource* stream_callback_source) {
data_stream_callback_source_ = stream_callback_source;
- return Open(url, DOWNLOAD_TO_BUFFER_AND_STREAM, callback, true, NULL);
-}
-
-bool FileDownloader::Open(
- const nacl::string& url,
- DownloadMode mode,
- const pp::CompletionCallback& callback,
- bool record_progress,
- PP_URLLoaderTrusted_StatusCallback progress_callback) {
PLUGIN_PRINTF(("FileDownloader::Open (url=%s)\n", url.c_str()));
- if (callback.pp_completion_callback().func == NULL ||
- instance_ == NULL ||
- file_io_private_interface_ == NULL)
+ if (callback.pp_completion_callback().func == NULL || instance_ == NULL)
return false;
- CHECK(instance_ != NULL);
status_code_ = -1;
url_ = url;
file_open_notify_callback_ = callback;
- mode_ = mode;
- file_info_.FreeResources();
+ mode_ = DOWNLOAD_TO_BUFFER_AND_STREAM;
pp::URLRequestInfo url_request(instance_);
// Allow CORS.
@@ -124,24 +60,11 @@ bool FileDownloader::Open(
// Note that we have the only reference to the underlying objects, so
// this will implicitly close any pending IO and destroy them.
url_loader_ = pp::URLLoader(instance_);
-
- url_request.SetRecordDownloadProgress(record_progress);
-
- if (url_loader_trusted_interface_ != NULL) {
- if (progress_callback != NULL) {
- url_loader_trusted_interface_->RegisterStatusCallback(
- url_loader_.pp_resource(), progress_callback);
- }
- }
+ url_request.SetRecordDownloadProgress(true);
// Prepare the url request.
url_request.SetURL(url_);
- if (mode_ == DOWNLOAD_TO_FILE) {
- file_reader_ = pp::FileIO(instance_);
- url_request.SetStreamToFile(true);
- }
-
// Request asynchronous download of the url providing an on-load callback.
// As long as this step is guaranteed to be asynchronous, we can call
// synchronously all other internal callbacks that eventually result in the
@@ -191,11 +114,6 @@ void FileDownloader::URLLoadStartNotify(int32_t pp_error) {
return;
}
- if (mode_ != DOWNLOAD_TO_BUFFER_AND_STREAM) {
- FinishStreaming(file_open_notify_callback_);
- return;
- }
-
file_open_notify_callback_.RunAndClear(PP_OK);
}
@@ -204,81 +122,15 @@ void FileDownloader::FinishStreaming(
stream_finish_callback_ = callback;
// Finish streaming the body providing an optional callback.
- if (mode_ == DOWNLOAD_TO_FILE) {
- pp::CompletionCallback onload_callback =
- callback_factory_.NewOptionalCallback(
- &FileDownloader::URLLoadFinishNotify);
- int32_t pp_error = url_loader_.FinishStreamingToFile(onload_callback);
- bool async_notify_ok = (pp_error == PP_OK_COMPLETIONPENDING);
- PLUGIN_PRINTF(("FileDownloader::FinishStreaming (async_notify_ok=%d)\n",
- async_notify_ok));
- if (!async_notify_ok) {
- // Call manually to free allocated memory and report errors. This calls
- // |stream_finish_callback_| with |pp_error| as the parameter.
- onload_callback.RunAndClear(pp_error);
- }
- } else {
- pp::CompletionCallback onread_callback =
- callback_factory_.NewOptionalCallback(
- &FileDownloader::URLReadBodyNotify);
- int32_t temp_size = static_cast<int32_t>(temp_buffer_.size());
- int32_t pp_error = url_loader_.ReadResponseBody(&temp_buffer_[0],
- temp_size,
- onread_callback);
- bool async_notify_ok = (pp_error == PP_OK_COMPLETIONPENDING);
- PLUGIN_PRINTF((
- "FileDownloader::FinishStreaming (async_notify_ok=%d)\n",
- async_notify_ok));
- if (!async_notify_ok) {
- onread_callback.RunAndClear(pp_error);
- }
- }
-}
-
-void FileDownloader::URLLoadFinishNotify(int32_t pp_error) {
- PLUGIN_PRINTF(("FileDownloader::URLLoadFinishNotify (pp_error=%"
- NACL_PRId32")\n", pp_error));
- if (pp_error != PP_OK) { // Streaming failed.
- stream_finish_callback_.RunAndClear(pp_error);
- return;
- }
-
- // Validate response again on load (though it should be the same
- // as it was during InitialResponseIsValid?).
- url_response_ = url_loader_.GetResponseInfo();
- CHECK(url_response_.GetStatusCode() == NACL_HTTP_STATUS_OK);
-
- // Record the full url from the response.
- pp::Var full_url = url_response_.GetURL();
- PLUGIN_PRINTF(("FileDownloader::URLLoadFinishNotify (full_url=%s)\n",
- full_url.DebugString().c_str()));
- if (!full_url.is_string()) {
- stream_finish_callback_.RunAndClear(PP_ERROR_FAILED);
- return;
- }
- full_url_ = full_url.AsString();
-
- // The file is now fully downloaded.
- pp::FileRef file(url_response_.GetBodyAsFileRef());
- if (file.is_null()) {
- PLUGIN_PRINTF(("FileDownloader::URLLoadFinishNotify (file=NULL)\n"));
- stream_finish_callback_.RunAndClear(PP_ERROR_FAILED);
- return;
- }
-
- // Open the file providing an optional callback.
- pp::CompletionCallback onopen_callback =
+ pp::CompletionCallback onread_callback =
callback_factory_.NewOptionalCallback(
- &FileDownloader::StreamFinishNotify);
- pp_error = file_reader_.Open(file, PP_FILEOPENFLAG_READ, onopen_callback);
- bool async_notify_ok = (pp_error == PP_OK_COMPLETIONPENDING);
- PLUGIN_PRINTF(("FileDownloader::URLLoadFinishNotify (async_notify_ok=%d)\n",
- async_notify_ok));
- if (!async_notify_ok) {
- // Call manually to free allocated memory and report errors. This calls
- // |stream_finish_callback_| with |pp_error| as the parameter.
- onopen_callback.RunAndClear(pp_error);
- }
+ &FileDownloader::URLReadBodyNotify);
+ int32_t temp_size = static_cast<int32_t>(temp_buffer_.size());
+ int32_t pp_error = url_loader_.ReadResponseBody(&temp_buffer_[0],
+ temp_size,
+ onread_callback);
+ if (pp_error != PP_OK_COMPLETIONPENDING)
+ onread_callback.RunAndClear(pp_error);
}
void FileDownloader::URLReadBodyNotify(int32_t pp_error) {
@@ -287,18 +139,15 @@ void FileDownloader::URLReadBodyNotify(int32_t pp_error) {
if (pp_error < PP_OK) {
stream_finish_callback_.RunAndClear(pp_error);
} else if (pp_error == PP_OK) {
- if (mode_ == DOWNLOAD_TO_BUFFER_AND_STREAM) {
- data_stream_callback_source_->GetCallback().RunAndClear(PP_OK);
- }
- StreamFinishNotify(PP_OK);
+ data_stream_callback_source_->GetCallback().RunAndClear(PP_OK);
+ stream_finish_callback_.RunAndClear(PP_OK);
} else {
- if (mode_ == DOWNLOAD_TO_BUFFER_AND_STREAM) {
- PLUGIN_PRINTF(("Running data_stream_callback, temp_buffer_=%p\n",
- &temp_buffer_[0]));
- StreamCallback cb = data_stream_callback_source_->GetCallback();
- *(cb.output()) = &temp_buffer_;
- cb.RunAndClear(pp_error);
- }
+ PLUGIN_PRINTF(("Running data_stream_callback, temp_buffer_=%p\n",
+ &temp_buffer_[0]));
+ StreamCallback cb = data_stream_callback_source_->GetCallback();
+ *(cb.output()) = &temp_buffer_;
+ cb.RunAndClear(pp_error);
+
pp::CompletionCallback onread_callback =
callback_factory_.NewOptionalCallback(
&FileDownloader::URLReadBodyNotify);
@@ -306,10 +155,8 @@ void FileDownloader::URLReadBodyNotify(int32_t pp_error) {
pp_error = url_loader_.ReadResponseBody(&temp_buffer_[0],
temp_size,
onread_callback);
- bool async_notify_ok = (pp_error == PP_OK_COMPLETIONPENDING);
- if (!async_notify_ok) {
+ if (pp_error != PP_OK_COMPLETIONPENDING)
onread_callback.RunAndClear(pp_error);
- }
}
}
@@ -330,37 +177,4 @@ nacl::string FileDownloader::GetResponseHeaders() const {
return headers.AsString();
}
-void FileDownloader::StreamFinishNotify(int32_t pp_error) {
- PLUGIN_PRINTF((
- "FileDownloader::StreamFinishNotify (pp_error=%" NACL_PRId32 ")\n",
- 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());
-}
-
-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) {
- NaClFileInfo tmp_info = NoFileInfo();
- tmp_info.desc = ConvertFileDescriptor(handle, false);
- file_info_.TakeOwnership(&tmp_info);
- }
-
- stream_finish_callback_.RunAndClear(pp_error);
-}
-
} // namespace plugin
« no previous file with comments | « ppapi/native_client/src/trusted/plugin/file_downloader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698