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

Unified Diff: components/download/content/download_driver_impl.cc

Issue 2880933002: Download driver for components/download. (Closed)
Patch Set: Created 3 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
Index: components/download/content/download_driver_impl.cc
diff --git a/components/download/content/download_driver_impl.cc b/components/download/content/download_driver_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5cd0531a68682a11cf28a8c3403cfbbcf7c62c26
--- /dev/null
+++ b/components/download/content/download_driver_impl.cc
@@ -0,0 +1,114 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/download/content/download_driver_impl.h"
+
+#include "base/files/file_path.h"
+#include "content/public/browser/download_interrupt_reasons.h"
+#include "content/public/browser/download_url_parameters.h"
+#include "content/public/browser/storage_partition.h"
+
+namespace download {
+
+DownloadDriverImpl::DownloadDriverImpl(content::DownloadManager* manager)
+ : download_manager_(manager), observer_(nullptr) {
+ DCHECK(download_manager_);
+ download_manager_->AddObserver(this);
+}
+
+DownloadDriverImpl::~DownloadDriverImpl() = default;
+
+void DownloadDriverImpl::Start(const DownloadParams& params) {
+ DCHECK(!params.request_params.url.is_empty());
+ DCHECK(!params.guid.empty());
+
+ content::StoragePartition* storage_partition =
+ content::BrowserContext::GetStoragePartitionForSite(
+ download_manager_->GetBrowserContext(), params.request_params.url);
+ DCHECK(storage_partition);
+
+ std::unique_ptr<content::DownloadUrlParameters> download_url_params(
+ new content::DownloadUrlParameters(
+ params.request_params.url,
+ storage_partition->GetURLRequestContext()));
+
+ // TODO(xingliu): Handles the request headers from |params|, needs to tweak
David Trainor- moved to gerrit 2017/05/15 20:08:14 Handles -> Handle, needs -> need Passes -> Pass
xingliu 2017/05/17 19:34:52 Done.
+ // download network stack. Passes the file path and file name.
David Trainor- moved to gerrit 2017/05/15 20:08:14 We should make the file name the guid or something
xingliu 2017/05/17 19:34:52 Done.
+ download_url_params->set_guid(params.guid);
+ download_url_params->set_transient(true);
+ download_url_params->set_method(params.request_params.method);
+
+ download_manager_->DownloadUrl(std::move(download_url_params));
+}
+
+void DownloadDriverImpl::Cancel(const std::string& guid) {
+ content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
+ // Cancels the download and removes the persisted records in content layer.
+ if (item) {
+ item->RemoveObserver(this);
+ item->Remove();
+ }
+}
+
+void DownloadDriverImpl::Pause(const std::string& guid) {
+ content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
+ if (item)
+ item->Pause();
+}
+
+void DownloadDriverImpl::Resume(const std::string& guid) {
+ content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
+ if (item)
+ item->Resume();
+}
+
+bool DownloadDriverImpl::IsReady() const {
+ return download_manager_->IsManagerInitialized();
+}
+
+void DownloadDriverImpl::SetObserver(DownloadDriver::Observer* observer) {
+ observer_ = observer;
David Trainor- moved to gerrit 2017/05/15 20:08:14 Do we need to notify the observer that we're initi
xingliu 2017/05/17 19:34:52 Done, change to pass a Client* through Initialize
+}
+
+void DownloadDriverImpl::OnDownloadUpdated(content::DownloadItem* item) {
+ DCHECK(item);
+ if (!observer_)
+ return;
+
+ using DownloadState = content::DownloadItem::DownloadState;
+ DownloadState state = item->GetState();
+ content::DownloadInterruptReason reason = item->GetLastReason();
+
+ if (state == DownloadState::COMPLETE) {
+ observer_->OnDownloadSucceeded(item->GetGuid(), item->GetTargetFilePath(),
+ item->GetReceivedBytes());
David Trainor- moved to gerrit 2017/05/15 20:08:14 Remove observer on the item here and/or when it fa
xingliu 2017/05/17 19:34:52 Done, good catch.
+ } else if (state == DownloadState::IN_PROGRESS) {
+ observer_->OnDownloadUpdated(item->GetGuid(), item->GetReceivedBytes());
David Trainor- moved to gerrit 2017/05/15 20:08:14 We'll need estimated progress as well. Would it j
xingliu 2017/05/17 19:34:52 Done.
+ } else if (reason !=
+ content::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) {
David Trainor- moved to gerrit 2017/05/15 20:08:14 We probably need all of the paused/resumed informa
xingliu 2017/05/17 19:34:52 Done. added the is_paused, if we need more thing w
+ observer_->OnDownloadFailed(item->GetGuid(), static_cast<int>(reason));
+ }
+}
+
+void DownloadDriverImpl::OnDownloadCreated(content::DownloadManager* manager,
+ content::DownloadItem* item) {
+ // Listens to all downloads.
+ item->AddObserver(this);
+
+ // TODO(xingliu): Pumps the response headers if needed from download network
+ // stack.
+ if (observer_)
+ observer_->OnDownloadCreated(item->GetGuid());
+}
+
+void DownloadDriverImpl::OnManagerInitialized() {
+ if (observer_)
+ observer_->OnDriverReady();
+}
+
+void DownloadDriverImpl::ManagerGoingDown(content::DownloadManager* manager) {
+ manager->RemoveObserver(this);
+}
+
+} // namespace download

Powered by Google App Engine
This is Rietveld 408576698