Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/download/content/download_driver_impl.h" | |
| 6 | |
| 7 #include "content/public/browser/download_interrupt_reasons.h" | |
| 8 #include "content/public/browser/download_url_parameters.h" | |
| 9 #include "content/public/browser/storage_partition.h" | |
| 10 | |
| 11 namespace download { | |
| 12 | |
| 13 DownloadDriverImpl::DownloadDriverImpl(content::DownloadManager* manager, | |
| 14 const base::FilePath& dir) | |
| 15 : download_manager_(manager), file_dir_(dir), client_(nullptr) { | |
| 16 DCHECK(download_manager_); | |
| 17 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.
| |
| 18 } | |
| 19 | |
| 20 DownloadDriverImpl::~DownloadDriverImpl() = default; | |
| 21 | |
| 22 void DownloadDriverImpl::Initialize(DownloadDriver::Client* client) { | |
| 23 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.
| |
| 24 client_ = client; | |
| 25 DCHECK(client_); | |
| 26 if (download_manager_->IsManagerInitialized()) | |
| 27 client_->OnDriverReady(); | |
| 28 } | |
| 29 | |
| 30 void DownloadDriverImpl::Start(const DownloadParams& params) { | |
| 31 DCHECK(!params.request_params.url.is_empty()); | |
| 32 DCHECK(!params.guid.empty()); | |
| 33 | |
| 34 content::StoragePartition* storage_partition = | |
| 35 content::BrowserContext::GetStoragePartitionForSite( | |
| 36 download_manager_->GetBrowserContext(), params.request_params.url); | |
| 37 DCHECK(storage_partition); | |
| 38 | |
| 39 std::unique_ptr<content::DownloadUrlParameters> download_url_params( | |
| 40 new content::DownloadUrlParameters( | |
| 41 params.request_params.url, | |
| 42 storage_partition->GetURLRequestContext())); | |
| 43 | |
| 44 // TODO(xingliu): Handle the request headers from |params|, need to tweak | |
| 45 // download network stack. | |
| 46 download_url_params->set_guid(params.guid); | |
| 47 download_url_params->set_transient(true); | |
| 48 download_url_params->set_method(params.request_params.method); | |
| 49 download_url_params->set_file_path(file_dir_.Append(params.guid)); | |
| 50 | |
| 51 download_manager_->DownloadUrl(std::move(download_url_params)); | |
| 52 } | |
| 53 | |
| 54 void DownloadDriverImpl::Cancel(const std::string& guid) { | |
| 55 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 56 // Cancels the download and removes the persisted records in content layer. | |
| 57 if (item) { | |
| 58 item->RemoveObserver(this); | |
| 59 item->Remove(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 void DownloadDriverImpl::Pause(const std::string& guid) { | |
| 64 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 65 if (item) | |
| 66 item->Pause(); | |
| 67 } | |
| 68 | |
| 69 void DownloadDriverImpl::Resume(const std::string& guid) { | |
| 70 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 71 if (item) | |
| 72 item->Resume(); | |
| 73 } | |
| 74 | |
| 75 DriverEntry DownloadDriverImpl::Find(const std::string& guid) { | |
| 76 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 77 DriverEntry entry; | |
| 78 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
| |
| 79 } | |
| 80 | |
| 81 void DownloadDriverImpl::OnDownloadUpdated(content::DownloadItem* item) { | |
| 82 DCHECK(item); | |
| 83 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_);.
| |
| 84 return; | |
| 85 | |
| 86 using DownloadState = content::DownloadItem::DownloadState; | |
| 87 DownloadState state = item->GetState(); | |
| 88 content::DownloadInterruptReason reason = item->GetLastReason(); | |
| 89 DriverEntry entry = DriverEntry::Create(item); | |
| 90 | |
| 91 if (state == DownloadState::COMPLETE) { | |
| 92 client_->OnDownloadSucceeded(entry, item->GetTargetFilePath()); | |
| 93 item->RemoveObserver(this); | |
| 94 } else if (state == DownloadState::IN_PROGRESS) { | |
| 95 client_->OnDownloadUpdated(entry); | |
| 96 } else if (reason != | |
| 97 content::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 98 client_->OnDownloadFailed(entry, static_cast<int>(reason)); | |
| 99 item->RemoveObserver(this); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 void DownloadDriverImpl::OnDownloadCreated(content::DownloadManager* manager, | |
| 104 content::DownloadItem* item) { | |
| 105 // Listens to all downloads. | |
| 106 item->AddObserver(this); | |
| 107 | |
| 108 if (client_) { | |
| 109 DriverEntry entry = DriverEntry::Create(item); | |
| 110 client_->OnDownloadCreated(entry); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void DownloadDriverImpl::OnManagerInitialized() { | |
| 115 if (client_) | |
| 116 client_->OnDriverReady(); | |
| 117 } | |
| 118 | |
| 119 void DownloadDriverImpl::ManagerGoingDown(content::DownloadManager* manager) { | |
| 120 manager->RemoveObserver(this); | |
| 121 } | |
| 122 | |
| 123 } // namespace download | |
| OLD | NEW |