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 "base/files/file_path.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 | |
12 namespace download { | |
13 | |
14 DownloadDriverImpl::DownloadDriverImpl(content::DownloadManager* manager) | |
15 : download_manager_(manager), observer_(nullptr) { | |
16 DCHECK(download_manager_); | |
17 download_manager_->AddObserver(this); | |
18 } | |
19 | |
20 DownloadDriverImpl::~DownloadDriverImpl() = default; | |
21 | |
22 void DownloadDriverImpl::Start(const DownloadParams& params) { | |
23 DCHECK(!params.request_params.url.is_empty()); | |
24 DCHECK(!params.guid.empty()); | |
25 | |
26 content::StoragePartition* storage_partition = | |
27 content::BrowserContext::GetStoragePartitionForSite( | |
28 download_manager_->GetBrowserContext(), params.request_params.url); | |
29 DCHECK(storage_partition); | |
30 | |
31 std::unique_ptr<content::DownloadUrlParameters> download_url_params( | |
32 new content::DownloadUrlParameters( | |
33 params.request_params.url, | |
34 storage_partition->GetURLRequestContext())); | |
35 | |
36 // 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.
| |
37 // 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.
| |
38 download_url_params->set_guid(params.guid); | |
39 download_url_params->set_transient(true); | |
40 download_url_params->set_method(params.request_params.method); | |
41 | |
42 download_manager_->DownloadUrl(std::move(download_url_params)); | |
43 } | |
44 | |
45 void DownloadDriverImpl::Cancel(const std::string& guid) { | |
46 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
47 // Cancels the download and removes the persisted records in content layer. | |
48 if (item) { | |
49 item->RemoveObserver(this); | |
50 item->Remove(); | |
51 } | |
52 } | |
53 | |
54 void DownloadDriverImpl::Pause(const std::string& guid) { | |
55 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
56 if (item) | |
57 item->Pause(); | |
58 } | |
59 | |
60 void DownloadDriverImpl::Resume(const std::string& guid) { | |
61 content::DownloadItem* item = download_manager_->GetDownloadByGuid(guid); | |
62 if (item) | |
63 item->Resume(); | |
64 } | |
65 | |
66 bool DownloadDriverImpl::IsReady() const { | |
67 return download_manager_->IsManagerInitialized(); | |
68 } | |
69 | |
70 void DownloadDriverImpl::SetObserver(DownloadDriver::Observer* observer) { | |
71 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
| |
72 } | |
73 | |
74 void DownloadDriverImpl::OnDownloadUpdated(content::DownloadItem* item) { | |
75 DCHECK(item); | |
76 if (!observer_) | |
77 return; | |
78 | |
79 using DownloadState = content::DownloadItem::DownloadState; | |
80 DownloadState state = item->GetState(); | |
81 content::DownloadInterruptReason reason = item->GetLastReason(); | |
82 | |
83 if (state == DownloadState::COMPLETE) { | |
84 observer_->OnDownloadSucceeded(item->GetGuid(), item->GetTargetFilePath(), | |
85 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.
| |
86 } else if (state == DownloadState::IN_PROGRESS) { | |
87 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.
| |
88 } else if (reason != | |
89 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
| |
90 observer_->OnDownloadFailed(item->GetGuid(), static_cast<int>(reason)); | |
91 } | |
92 } | |
93 | |
94 void DownloadDriverImpl::OnDownloadCreated(content::DownloadManager* manager, | |
95 content::DownloadItem* item) { | |
96 // Listens to all downloads. | |
97 item->AddObserver(this); | |
98 | |
99 // TODO(xingliu): Pumps the response headers if needed from download network | |
100 // stack. | |
101 if (observer_) | |
102 observer_->OnDownloadCreated(item->GetGuid()); | |
103 } | |
104 | |
105 void DownloadDriverImpl::OnManagerInitialized() { | |
106 if (observer_) | |
107 observer_->OnDriverReady(); | |
108 } | |
109 | |
110 void DownloadDriverImpl::ManagerGoingDown(content::DownloadManager* manager) { | |
111 manager->RemoveObserver(this); | |
112 } | |
113 | |
114 } // namespace download | |
OLD | NEW |