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

Unified Diff: content/browser/download/download_file_impl.cc

Issue 10074001: Initial implementation of the ByteStream refactor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Checkpoint and merge to LKGR. Created 8 years, 8 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 | « content/browser/download/download_file_impl.h ('k') | content/browser/download/download_file_manager.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/download/download_file_impl.cc
diff --git a/content/browser/download/download_file_impl.cc b/content/browser/download/download_file_impl.cc
index ae8d4ec4395ddb88e5b5c1875f0c9b247055c5c5..40cec3f08e5c9ba37aeabe3582e4098fcd182cef 100644
--- a/content/browser/download/download_file_impl.cc
+++ b/content/browser/download/download_file_impl.cc
@@ -6,11 +6,17 @@
#include <string>
+#include "base/bind.h"
#include "base/file_util.h"
+#include "base/message_loop_proxy.h"
#include "content/browser/download/download_create_info.h"
+#include "content/browser/download/download_interrupt_reasons_impl.h"
+#include "content/browser/download/download_net_log_parameters.h"
#include "content/browser/power_save_blocker.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
+#include "content/browser/download/download_stats.h"
+#include "net/base/io_buffer.h"
using content::BrowserThread;
using content::DownloadId;
@@ -33,21 +39,42 @@ DownloadFileImpl::DownloadFileImpl(
info->save_info.hash_state,
info->save_info.file_stream,
bound_net_log),
+ input_pipe_(info->pipe),
id_(info->download_id),
request_handle_(request_handle),
download_manager_(download_manager),
+ bound_net_log_(bound_net_log),
+ weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
power_save_blocker_(power_save_blocker.Pass()) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
}
DownloadFileImpl::~DownloadFileImpl() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ // Prevent any future callbacks from reaching us.
+ if (input_pipe_.get())
+ input_pipe_->RegisterSinkCallback(scoped_refptr<base::TaskRunner>(),
+ base::Closure(), 0);
}
// BaseFile delegated functions.
net::Error DownloadFileImpl::Initialize() {
update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>());
- return file_.Initialize();
+ net::Error result = file_.Initialize();
+ if (result != net::OK)
+ return result;
+
+ input_pipe_->RegisterSinkCallback(
+ base::MessageLoopProxy::current(),
+ // Unretained is safe because the callback is nulled in the
+ // destructor.
+ base::Bind(&DownloadFileImpl::PipeActive, base::Unretained(this)),
+ 33);
+
+ // Initial pull from the straw.
+ PipeActive();
+
+ return result;
}
net::Error DownloadFileImpl::AppendDataToFile(const char* data,
@@ -72,11 +99,6 @@ void DownloadFileImpl::Cancel() {
file_.Cancel();
}
-void DownloadFileImpl::Finish() {
- file_.Finish();
- update_timer_.reset();
-}
-
void DownloadFileImpl::AnnotateWithSourceInformation() {
file_.AnnotateWithSourceInformation();
}
@@ -135,6 +157,99 @@ std::string DownloadFileImpl::DebugString() const {
file_.DebugString().c_str());
}
+void DownloadFileImpl::PipeActive() {
+ base::Time start(base::Time::Now());
+ base::Time now;
+ scoped_refptr<net::IOBuffer> incoming_data;
+ size_t length = 0;
+ size_t total_length = 0;
+ size_t num_buffers = 0;
+ content::ByteStream::StreamState state(content::ByteStream::STREAM_EMPTY);
+ content::DownloadInterruptReason reason =
+ content::DOWNLOAD_INTERRUPT_REASON_NONE;
+ base::TimeDelta delta(base::TimeDelta::FromMilliseconds(1000));
+
+ // Take care of any file local activity required.
+ do {
+ state = input_pipe_->GetData(&incoming_data, &length);
+
+ net::Error result = net::OK;
+ switch (state) {
+ case content::ByteStream::STREAM_EMPTY:
+ break;
+ case content::ByteStream::STREAM_HAS_DATA:
+ ++num_buffers;
+ result = AppendDataToFile(incoming_data.get()->data(), length);
+ total_length += length;
+ reason = content::ConvertNetErrorToInterruptReason(
+ result, content::DOWNLOAD_INTERRUPT_FROM_DISK);
+ break;
+ case content::ByteStream::STREAM_COMPLETE:
+ reason = input_pipe_->GetSourceResult();
+ SendUpdate();
+ file_.Finish();
+ update_timer_.reset();
+ break;
+ default:
+ NOTREACHED();
+ break;
+ }
+ now = base::Time::Now();
+ } while (state == content::ByteStream::STREAM_HAS_DATA &&
+ reason == content::DOWNLOAD_INTERRUPT_REASON_NONE &&
+ now - start <= delta);
+
+ // If we're stopping to yield the thread, post a task so we come back.
+ if (state == content::ByteStream::STREAM_HAS_DATA && now - start > delta) {
+ BrowserThread::PostTask(
+ BrowserThread::FILE, FROM_HERE,
+ base::Bind(&DownloadFileImpl::PipeActive, weak_factory_.GetWeakPtr()));
+ }
+
+ if (total_length)
+ download_stats::RecordFileThreadReceiveBuffers(total_length);
+
+ download_stats::RecordContiguousWriteTime(now - start);
+
+ // Take care of communication with our controller.
+ if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) {
+ // Error case for both upstream source and file write.
+ // Shut down processing and signal an error to our controller.
+ // Our controller will clean us up.
+ input_pipe_->RegisterSinkCallback(scoped_refptr<base::TaskRunner>(),
+ base::Closure(), 0);
+ weak_factory_.InvalidateWeakPtrs();
+ if (download_manager_.get()) {
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&DownloadManager::OnDownloadInterrupted,
+ download_manager_, id_.local(),
+ BytesSoFar(), GetHashState(), reason));
+ }
+ } else if (state == content::ByteStream::STREAM_COMPLETE) {
+ // Signal successful completion and shut down processing.
+ input_pipe_->RegisterSinkCallback(scoped_refptr<base::TaskRunner>(),
+ base::Closure(), 0);
+ weak_factory_.InvalidateWeakPtrs();
+ if (download_manager_.get()) {
+ std::string hash;
+ if (!GetHash(&hash) || file_.IsEmptyHash(hash))
+ hash.clear();
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&DownloadManager::OnResponseCompleted,
+ download_manager_, id_.local(),
+ BytesSoFar(), hash));
+ }
+ }
+ if (bound_net_log_.IsLoggingAllEvents()) {
+ bound_net_log_.AddEvent(
+ net::NetLog::TYPE_DOWNLOAD_PIPE_DRAINED,
+ make_scoped_refptr(new download_net_logs::FilePipeDrainedParameters(
+ total_length, num_buffers)));
+ }
+}
+
void DownloadFileImpl::SendUpdate() {
if (download_manager_.get()) {
BrowserThread::PostTask(
« no previous file with comments | « content/browser/download/download_file_impl.h ('k') | content/browser/download/download_file_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698