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

Side by Side Diff: components/download/content/download_driver_impl.cc

Issue 2880933002: Download driver for components/download. (Closed)
Patch Set: DEPS polish. 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 unified diff | Download patch
OLDNEW
(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 DCHECK(item);
43 DriverEntry entry;
44 entry.guid = item->GetGuid();
45 entry.state = ToDriverEntryState(item->GetState());
46 entry.paused = item->IsPaused();
47 entry.bytes_downloaded = item->GetReceivedBytes();
48 entry.expected_total_size = item->GetTotalBytes();
49 entry.response_headers = item->GetResponseHeaders();
50 return entry;
51 }
52
53 DownloadDriverImpl::DownloadDriverImpl(content::DownloadManager* manager,
54 const base::FilePath& dir)
55 : download_manager_(manager), file_dir_(dir), client_(nullptr) {
56 DCHECK(download_manager_);
57 }
58
59 DownloadDriverImpl::~DownloadDriverImpl() = default;
60
61 void DownloadDriverImpl::Initialize(DownloadDriver::Client* client) {
62 DCHECK(!client_);
63 client_ = client;
64 DCHECK(client_);
65 download_manager_->AddObserver(this);
66 if (download_manager_->IsManagerInitialized())
67 client_->OnDriverReady();
68 }
69
70 void DownloadDriverImpl::Start(const DownloadParams& params) {
71 DCHECK(!params.request_params.url.is_empty());
72 DCHECK(!params.guid.empty());
73
74 content::StoragePartition* storage_partition =
75 content::BrowserContext::GetStoragePartitionForSite(
76 download_manager_->GetBrowserContext(), params.request_params.url);
77 DCHECK(storage_partition);
78
79 std::unique_ptr<content::DownloadUrlParameters> download_url_params(
80 new content::DownloadUrlParameters(
81 params.request_params.url,
82 storage_partition->GetURLRequestContext()));
83
84 // TODO(xingliu): Handle the request headers from |params|, need to tweak
85 // download network stack.
86 download_url_params->set_guid(params.guid);
87 download_url_params->set_transient(true);
88 download_url_params->set_method(params.request_params.method);
89 download_url_params->set_file_path(file_dir_.AppendASCII(params.guid));
90
91 download_manager_->DownloadUrl(std::move(download_url_params));
92 }
93
94 void DownloadDriverImpl::Cancel(const std::string& guid) {
95 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
96 // Cancels the download and removes the persisted records in content layer.
97 if (item) {
98 item->RemoveObserver(this);
99 item->Remove();
100 }
101 }
102
103 void DownloadDriverImpl::Pause(const std::string& guid) {
104 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
105 if (item)
106 item->Pause();
107 }
108
109 void DownloadDriverImpl::Resume(const std::string& guid) {
110 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
111 if (item)
112 item->Resume();
113 }
114
115 base::Optional<DriverEntry> DownloadDriverImpl::Find(const std::string& guid) {
116 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid);
117 if (item)
118 return CreateDriverEntry(item);
119 return base::nullopt;
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);
qinmin 2017/05/22 17:16:41 is this needed?
xingliu 2017/05/22 18:22:07 Done, move this call to destructor. This is a goo
158 }
159
160 } // namespace download
OLDNEW
« no previous file with comments | « components/download/content/download_driver_impl.h ('k') | components/download/content/download_driver_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698