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..60d3517cd6f677d5cd7e124fd62739d7a2a0eab0 |
--- /dev/null |
+++ b/components/download/content/download_driver_impl.cc |
@@ -0,0 +1,123 @@ |
+// 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 "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, |
+ const base::FilePath& dir) |
+ : download_manager_(manager), file_dir_(dir), client_(nullptr) { |
+ DCHECK(download_manager_); |
+ download_manager_->AddObserver(this); |
David Trainor- moved to gerrit
2017/05/18 18:29:44
Maybe do this ininitialize? That way we don't hav
xingliu
2017/05/18 22:21:49
Done. Makes sense.
|
+} |
+ |
+DownloadDriverImpl::~DownloadDriverImpl() = default; |
+ |
+void DownloadDriverImpl::Initialize(DownloadDriver::Client* client) { |
+ DCHECK(!client_ && download_manager_); |
David Trainor- moved to gerrit
2017/05/18 18:29:44
Don't need the download_manager_ dcheck here right
xingliu
2017/05/18 22:21:49
Done, forgot we have a DCHECK in the ctor.
|
+ client_ = client; |
+ DCHECK(client_); |
+ if (download_manager_->IsManagerInitialized()) |
+ client_->OnDriverReady(); |
+} |
+ |
+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): Handle the request headers from |params|, need to tweak |
+ // download network stack. |
+ download_url_params->set_guid(params.guid); |
+ download_url_params->set_transient(true); |
+ download_url_params->set_method(params.request_params.method); |
+ download_url_params->set_file_path(file_dir_.Append(params.guid)); |
+ |
+ 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(); |
+} |
+ |
+DriverEntry DownloadDriverImpl::Find(const std::string& guid) { |
+ content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); |
+ DriverEntry entry; |
+ return item ? DriverEntry::Create(item) : entry; |
David Trainor- moved to gerrit
2017/05/18 18:29:44
Your conversion function might have to live somewh
xingliu
2017/05/18 22:21:49
Done. Move the conversion function to this file as
|
+} |
+ |
+void DownloadDriverImpl::OnDownloadUpdated(content::DownloadItem* item) { |
+ DCHECK(item); |
+ if (!client_) |
David Trainor- moved to gerrit
2017/05/18 18:29:43
We should probably still clean up our observer tie
xingliu
2017/05/18 22:21:49
Done. Changed all if check to DCHECK(client_);.
|
+ return; |
+ |
+ using DownloadState = content::DownloadItem::DownloadState; |
+ DownloadState state = item->GetState(); |
+ content::DownloadInterruptReason reason = item->GetLastReason(); |
+ DriverEntry entry = DriverEntry::Create(item); |
+ |
+ if (state == DownloadState::COMPLETE) { |
+ client_->OnDownloadSucceeded(entry, item->GetTargetFilePath()); |
+ item->RemoveObserver(this); |
+ } else if (state == DownloadState::IN_PROGRESS) { |
+ client_->OnDownloadUpdated(entry); |
+ } else if (reason != |
+ content::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { |
+ client_->OnDownloadFailed(entry, static_cast<int>(reason)); |
+ item->RemoveObserver(this); |
+ } |
+} |
+ |
+void DownloadDriverImpl::OnDownloadCreated(content::DownloadManager* manager, |
+ content::DownloadItem* item) { |
+ // Listens to all downloads. |
+ item->AddObserver(this); |
+ |
+ if (client_) { |
+ DriverEntry entry = DriverEntry::Create(item); |
+ client_->OnDownloadCreated(entry); |
+ } |
+} |
+ |
+void DownloadDriverImpl::OnManagerInitialized() { |
+ if (client_) |
+ client_->OnDriverReady(); |
+} |
+ |
+void DownloadDriverImpl::ManagerGoingDown(content::DownloadManager* manager) { |
+ manager->RemoveObserver(this); |
+} |
+ |
+} // namespace download |