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 "components/download/internal/driver_entry.h" | |
| 8 #include "content/public/browser/download_interrupt_reasons.h" | |
| 9 #include "content/public/browser/download_url_parameters.h" | |
| 10 #include "content/public/browser/storage_partition.h" | |
| 11 #include "net/http/http_response_headers.h" | |
| 12 | |
| 13 namespace download { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Converts a content::DownloadItem::DownloadState to DriverEntry::State. | |
| 18 DriverEntry::State ToDriverEntryState( | |
| 19 content::DownloadItem::DownloadState state) { | |
| 20 switch (state) { | |
| 21 case content::DownloadItem::IN_PROGRESS: | |
| 22 return DriverEntry::State::IN_PROGRESS; | |
| 23 case content::DownloadItem::COMPLETE: | |
| 24 return DriverEntry::State::COMPLETE; | |
| 25 case content::DownloadItem::CANCELLED: | |
| 26 return DriverEntry::State::CANCELLED; | |
| 27 case content::DownloadItem::INTERRUPTED: | |
| 28 return DriverEntry::State::INTERRUPTED; | |
| 29 case content::DownloadItem::MAX_DOWNLOAD_STATE: | |
| 30 return DriverEntry::State::UNKNOWN; | |
| 31 default: | |
| 32 NOTREACHED(); | |
| 33 return DriverEntry::State::UNKNOWN; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 // static | |
| 40 DriverEntry DownloadDriverImpl::CreateDriverEntry( | |
| 41 const content::DownloadItem* item) { | |
| 42 DriverEntry entry; | |
| 43 if (!item) | |
|
David Trainor- moved to gerrit
2017/05/18 23:51:19
DCHECK item instead?
xingliu
2017/05/22 16:41:03
Done.
| |
| 44 return entry; | |
| 45 entry.guid = item->GetGuid(); | |
| 46 entry.state = ToDriverEntryState(item->GetState()); | |
| 47 entry.paused = item->IsPaused(); | |
| 48 entry.bytes_downloaded = item->GetReceivedBytes(); | |
| 49 entry.expected_total_size = item->GetTotalBytes(); | |
| 50 entry.response_headers = item->GetResponseHeaders(); | |
| 51 return entry; | |
| 52 } | |
| 53 | |
| 54 DownloadDriverImpl::DownloadDriverImpl(content::DownloadManager* manager, | |
| 55 const base::FilePath& dir) | |
| 56 : download_manager_(manager), file_dir_(dir), client_(nullptr) { | |
| 57 DCHECK(download_manager_); | |
| 58 } | |
| 59 | |
| 60 DownloadDriverImpl::~DownloadDriverImpl() = default; | |
| 61 | |
| 62 void DownloadDriverImpl::Initialize(DownloadDriver::Client* client) { | |
| 63 DCHECK(!client_); | |
| 64 client_ = client; | |
| 65 DCHECK(client_); | |
| 66 download_manager_->AddObserver(this); | |
| 67 if (download_manager_->IsManagerInitialized()) | |
| 68 client_->OnDriverReady(); | |
| 69 } | |
| 70 | |
| 71 void DownloadDriverImpl::Start(const DownloadParams& params) { | |
| 72 DCHECK(!params.request_params.url.is_empty()); | |
| 73 DCHECK(!params.guid.empty()); | |
| 74 | |
| 75 content::StoragePartition* storage_partition = | |
| 76 content::BrowserContext::GetStoragePartitionForSite( | |
| 77 download_manager_->GetBrowserContext(), params.request_params.url); | |
| 78 DCHECK(storage_partition); | |
| 79 | |
| 80 std::unique_ptr<content::DownloadUrlParameters> download_url_params( | |
| 81 new content::DownloadUrlParameters( | |
| 82 params.request_params.url, | |
| 83 storage_partition->GetURLRequestContext())); | |
| 84 | |
| 85 // TODO(xingliu): Handle the request headers from |params|, need to tweak | |
| 86 // download network stack. | |
| 87 download_url_params->set_guid(params.guid); | |
| 88 download_url_params->set_transient(true); | |
| 89 download_url_params->set_method(params.request_params.method); | |
| 90 download_url_params->set_file_path(file_dir_.Append(params.guid)); | |
| 91 | |
| 92 download_manager_->DownloadUrl(std::move(download_url_params)); | |
| 93 } | |
| 94 | |
| 95 void DownloadDriverImpl::Cancel(const std::string& guid) { | |
| 96 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 97 // Cancels the download and removes the persisted records in content layer. | |
| 98 if (item) { | |
| 99 item->RemoveObserver(this); | |
| 100 item->Remove(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void DownloadDriverImpl::Pause(const std::string& guid) { | |
| 105 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 106 if (item) | |
| 107 item->Pause(); | |
| 108 } | |
| 109 | |
| 110 void DownloadDriverImpl::Resume(const std::string& guid) { | |
| 111 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 112 if (item) | |
| 113 item->Resume(); | |
| 114 } | |
| 115 | |
| 116 DriverEntry DownloadDriverImpl::Find(const std::string& guid) { | |
|
David Trainor- moved to gerrit
2017/05/18 23:51:19
base::Optional<DriverEntry>
xingliu
2017/05/22 16:41:03
Done.
| |
| 117 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
| 118 DriverEntry entry; | |
| 119 return item ? CreateDriverEntry(item) : entry; | |
| 120 } | |
| 121 | |
| 122 void DownloadDriverImpl::OnDownloadUpdated(content::DownloadItem* item) { | |
| 123 DCHECK(client_); | |
| 124 | |
| 125 using DownloadState = content::DownloadItem::DownloadState; | |
| 126 DownloadState state = item->GetState(); | |
| 127 content::DownloadInterruptReason reason = item->GetLastReason(); | |
| 128 DriverEntry entry = CreateDriverEntry(item); | |
| 129 | |
| 130 if (state == DownloadState::COMPLETE) { | |
| 131 client_->OnDownloadSucceeded(entry, item->GetTargetFilePath()); | |
| 132 item->RemoveObserver(this); | |
| 133 } else if (state == DownloadState::IN_PROGRESS) { | |
| 134 client_->OnDownloadUpdated(entry); | |
| 135 } else if (reason != | |
| 136 content::DownloadInterruptReason::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
| 137 client_->OnDownloadFailed(entry, static_cast<int>(reason)); | |
| 138 item->RemoveObserver(this); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void DownloadDriverImpl::OnDownloadCreated(content::DownloadManager* manager, | |
| 143 content::DownloadItem* item) { | |
| 144 // Listens to all downloads. | |
| 145 item->AddObserver(this); | |
| 146 DCHECK(client_); | |
| 147 DriverEntry entry = CreateDriverEntry(item); | |
| 148 client_->OnDownloadCreated(entry); | |
| 149 } | |
| 150 | |
| 151 void DownloadDriverImpl::OnManagerInitialized() { | |
| 152 DCHECK(client_); | |
| 153 client_->OnDriverReady(); | |
| 154 } | |
| 155 | |
| 156 void DownloadDriverImpl::ManagerGoingDown(content::DownloadManager* manager) { | |
| 157 manager->RemoveObserver(this); | |
| 158 } | |
| 159 | |
| 160 } // namespace download | |
| OLD | NEW |