blob: 5e8c80f25bf8b495192bc39701fc7ecd39523b10 [file] [log] [blame]
[email protected]0afff032012-01-06 20:55:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]5656f8a2011-11-17 16:12:585#include "content/browser/download/download_manager_impl.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]e7557f172011-08-19 23:42:017#include <iterator>
8
[email protected]eda58402011-09-21 19:32:029#include "base/bind.h"
[email protected]2041cf342010-02-19 03:15:5910#include "base/callback.h"
[email protected]c6944272012-01-06 22:12:2811#include "base/debug/alias.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/file_util.h"
[email protected]503d03872011-05-06 08:36:2613#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/logging.h"
[email protected]7286e3fc2011-07-19 22:13:2415#include "base/stl_util.h"
[email protected]eda58402011-09-21 19:32:0216#include "base/stringprintf.h"
17#include "base/synchronization/lock.h"
18#include "base/sys_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4219#include "build/build_config.h"
[email protected]71bf3f5e2011-08-15 21:05:2220#include "content/browser/download/download_create_info.h"
21#include "content/browser/download/download_file_manager.h"
[email protected]c09a8fd2011-11-21 19:54:5022#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0623#include "content/browser/download/download_stats.h"
[email protected]b3c41c0b2012-03-06 15:48:3224#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]ea114722012-03-12 01:11:2525#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2926#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1127#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4928#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4429#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3430#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1731#include "content/public/browser/download_manager_delegate.h"
[email protected]8da82ea2012-03-11 22:16:5232#include "content/public/browser/download_persistent_store_info.h"
[email protected]c5a5c0842012-05-04 20:05:1433#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0734#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1635#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3036#include "content/public/browser/render_process_host.h"
[email protected]0bfbf882011-12-22 18:19:2737#include "content/public/browser/web_contents_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1438#include "net/base/load_flags.h"
[email protected]27678b2a2012-02-04 22:09:1439#include "net/base/upload_data.h"
[email protected]f859eba2012-05-30 17:22:4940#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2941
[email protected]631bb742011-11-02 11:29:3942using content::BrowserThread;
[email protected]98e814062012-01-27 00:35:4243using content::DownloadId;
[email protected]e582fdd2011-12-20 16:48:1744using content::DownloadItem;
[email protected]8da82ea2012-03-11 22:16:5245using content::DownloadPersistentStoreInfo;
[email protected]ea114722012-03-12 01:11:2546using content::ResourceDispatcherHostImpl;
[email protected]2a6bc3e2011-12-28 23:51:3347using content::WebContents;
[email protected]631bb742011-11-02 11:29:3948
[email protected]a0ce3282011-08-19 20:49:5249namespace {
50
[email protected]c5a5c0842012-05-04 20:05:1451void BeginDownload(content::DownloadUrlParameters* params) {
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
53 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
54 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
55 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]c5a5c0842012-05-04 20:05:1456 scoped_ptr<net::URLRequest> request(new net::URLRequest(
[email protected]56f0b082012-06-14 07:12:3257 params->url(), NULL));
[email protected]f859eba2012-05-30 17:22:4958 request->set_referrer(params->referrer().url.spec());
59 webkit_glue::ConfigureURLRequestForReferrerPolicy(
60 request.get(), params->referrer().policy);
[email protected]c5a5c0842012-05-04 20:05:1461 request->set_load_flags(request->load_flags() | params->load_flags());
62 request->set_method(params->method());
63 if (!params->post_body().empty())
64 request->AppendBytesToUpload(params->post_body().data(),
65 params->post_body().size());
66 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1467 // The POST in this case does not have an actual body, and only works
68 // when retrieving data from cache. This is done because we don't want
69 // to do a re-POST without user consent, and currently don't have a good
70 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1471 DCHECK(params->prefer_cache());
72 DCHECK(params->method() == "POST");
[email protected]27678b2a2012-02-04 22:09:1473 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
[email protected]c5a5c0842012-05-04 20:05:1474 upload_data->set_identifier(params->post_id());
[email protected]27678b2a2012-02-04 22:09:1475 request->set_upload(upload_data);
76 }
[email protected]c5a5c0842012-05-04 20:05:1477 for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter
78 = params->request_headers_begin();
79 iter != params->request_headers_end();
80 ++iter) {
81 request->SetExtraRequestHeaderByName(
82 iter->first, iter->second, false/*overwrite*/);
83 }
[email protected]56f0b082012-06-14 07:12:3284 params->resource_dispatcher_host()->BeginDownload(
[email protected]ea114722012-03-12 01:11:2585 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:0686 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:1487 params->resource_context(),
88 params->render_process_host_id(),
89 params->render_view_host_routing_id(),
90 params->prefer_cache(),
91 params->save_info(),
92 params->callback());
[email protected]a0ce3282011-08-19 20:49:5293}
94
[email protected]33d22102012-01-25 17:46:5395class MapValueIteratorAdapter {
96 public:
97 explicit MapValueIteratorAdapter(
98 base::hash_map<int64, DownloadItem*>::const_iterator iter)
99 : iter_(iter) {
100 }
101 ~MapValueIteratorAdapter() {}
102
103 DownloadItem* operator*() { return iter_->second; }
104
105 MapValueIteratorAdapter& operator++() {
106 ++iter_;
107 return *this;
108 }
109
110 bool operator!=(const MapValueIteratorAdapter& that) const {
111 return iter_ != that.iter_;
112 }
113
114 private:
115 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
116 // Allow copy and assign.
117};
118
[email protected]5948e1a2012-03-10 00:19:18119void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm,
120 bool* result) {
121 if (dfm->NumberOfActiveDownloads())
122 *result = false;
123 BrowserThread::PostTask(
124 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
125}
126
[email protected]0a5c9112012-03-12 17:49:02127void EnsureNoPendingDownloadJobsOnIO(bool* result) {
[email protected]5948e1a2012-03-10 00:19:18128 scoped_refptr<DownloadFileManager> download_file_manager =
[email protected]ea114722012-03-12 01:11:25129 ResourceDispatcherHostImpl::Get()->download_file_manager();
[email protected]5948e1a2012-03-10 00:19:18130 BrowserThread::PostTask(
131 BrowserThread::FILE, FROM_HERE,
132 base::Bind(&EnsureNoPendingDownloadsOnFile,
133 download_file_manager, result));
134}
135
[email protected]d25fda12012-06-12 17:05:03136class DownloadItemFactoryImpl : public content::DownloadItemFactory {
137 public:
138 DownloadItemFactoryImpl() {}
139 virtual ~DownloadItemFactoryImpl() {}
140
141 virtual content::DownloadItem* CreatePersistedItem(
142 DownloadItemImpl::Delegate* delegate,
143 content::DownloadId download_id,
144 const content::DownloadPersistentStoreInfo& info,
145 const net::BoundNetLog& bound_net_log) OVERRIDE {
146 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
147 }
148
149 virtual content::DownloadItem* CreateActiveItem(
150 DownloadItemImpl::Delegate* delegate,
151 const DownloadCreateInfo& info,
[email protected]94d841cf2012-06-12 20:58:20152 scoped_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]d25fda12012-06-12 17:05:03153 bool is_otr,
154 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]94d841cf2012-06-12 20:58:20155 return new DownloadItemImpl(delegate, info, request_handle.Pass(),
156 is_otr, bound_net_log);
[email protected]d25fda12012-06-12 17:05:03157 }
158
159 virtual content::DownloadItem* CreateSavePageItem(
160 DownloadItemImpl::Delegate* delegate,
161 const FilePath& path,
162 const GURL& url,
163 bool is_otr,
164 content::DownloadId download_id,
165 const std::string& mime_type,
166 const net::BoundNetLog& bound_net_log) OVERRIDE {
167 return new DownloadItemImpl(delegate, path, url, is_otr, download_id,
168 mime_type, bound_net_log);
169 }
170};
171
[email protected]a0ce3282011-08-19 20:49:52172} // namespace
173
[email protected]99907362012-01-11 05:41:40174namespace content {
175
[email protected]5948e1a2012-03-10 00:19:18176bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
178 bool result = true;
179 BrowserThread::PostTask(
180 BrowserThread::IO, FROM_HERE,
[email protected]0a5c9112012-03-12 17:49:02181 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result));
[email protected]5948e1a2012-03-10 00:19:18182 MessageLoop::current()->Run();
183 return result;
184}
185
[email protected]99907362012-01-11 05:41:40186} // namespace content
187
[email protected]d25fda12012-06-12 17:05:03188DownloadManagerImpl::DownloadManagerImpl(
189 DownloadFileManager* file_manager,
190 scoped_ptr<content::DownloadItemFactory> factory,
191 net::NetLog* net_log)
192 : factory_(factory.Pass()),
193 shutdown_needed_(false),
[email protected]b441a8492012-06-06 14:55:57194 browser_context_(NULL),
[email protected]d25fda12012-06-12 17:05:03195 file_manager_(file_manager),
[email protected]33c20cd92012-06-14 22:02:50196 delegate_(NULL),
[email protected]b441a8492012-06-06 14:55:57197 net_log_(net_log) {
[email protected]d25fda12012-06-12 17:05:03198 DCHECK(file_manager);
199 if (!factory_.get())
200 factory_.reset(new DownloadItemFactoryImpl());
initial.commit09911bf2008-07-26 23:55:29201}
202
[email protected]5656f8a2011-11-17 16:12:58203DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13204 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29205}
206
[email protected]5656f8a2011-11-17 16:12:58207DownloadId DownloadManagerImpl::GetNextId() {
[email protected]491aaa62012-06-07 03:50:18208 DownloadId id;
209 if (delegate_)
210 id = delegate_->GetNextId();
211 if (!id.IsValid()) {
212 static int next_id;
213 id = DownloadId(browser_context_, ++next_id);
214 }
215
216 return id;
[email protected]2909e342011-10-29 00:46:53217}
218
[email protected]fc03de22011-12-06 23:28:12219bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
[email protected]491aaa62012-06-07 03:50:18220 if (!delegate_)
221 return true;
222
[email protected]fc03de22011-12-06 23:28:12223 return delegate_->ShouldOpenDownload(item);
224}
225
226bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18227 if (!delegate_)
228 return false;
229
[email protected]fc03de22011-12-06 23:28:12230 return delegate_->ShouldOpenFileBasedOnExtension(path);
231}
232
[email protected]b441a8492012-06-06 14:55:57233void DownloadManagerImpl::SetDelegate(
234 content::DownloadManagerDelegate* delegate) {
235 delegate_ = delegate;
236}
237
[email protected]b488b5a52012-06-06 17:01:28238content::DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
239 return delegate_;
240}
241
[email protected]5656f8a2011-11-17 16:12:58242void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50243 VLOG(20) << __FUNCTION__ << "()"
244 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13245 if (!shutdown_needed_)
246 return;
247 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29248
[email protected]75e51b52012-02-04 16:57:54249 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08250 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13251
[email protected]d25fda12012-06-12 17:05:03252 DCHECK(file_manager_);
253 BrowserThread::PostTask(
254 BrowserThread::FILE, FROM_HERE,
255 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
256 file_manager_, make_scoped_refptr(this)));
initial.commit09911bf2008-07-26 23:55:29257
[email protected]f04182f32010-12-10 19:12:07258 AssertContainersConsistent();
259
260 // Go through all downloads in downloads_. Dangerous ones we need to
261 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09262 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07263 DownloadItem* download = *it;
264
265 // Save iterator from potential erases in this set done by called code.
266 // Iterators after an erasure point are still valid for lists and
267 // associative containers such as sets.
268 it++;
269
[email protected]c09a8fd2011-11-21 19:54:50270 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29271 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07272 // The user hasn't accepted it, so we need to remove it
273 // from the disk. This may or may not result in it being
274 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12275 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07276 // removes and deletes it if it's known to the history service)
277 // so the only thing we know after calling this function is that
278 // the download was deleted if-and-only-if it was removed
279 // from all queues.
[email protected]303077002011-04-19 23:21:01280 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26281 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17282 download->Cancel(false);
[email protected]491aaa62012-06-07 03:50:18283 if (delegate_)
284 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29285 }
286 }
287
[email protected]f04182f32010-12-10 19:12:07288 // At this point, all dangerous downloads have had their files removed
289 // and all in progress downloads have been cancelled. We can now delete
290 // anything left.
[email protected]9ccbb372008-10-10 18:50:32291
[email protected]5cd11b6e2011-06-10 20:30:59292 // Copy downloads_ to separate container so as not to set off checks
293 // in DownloadItem destruction.
294 DownloadSet downloads_to_delete;
295 downloads_to_delete.swap(downloads_);
296
[email protected]70850c72011-01-11 17:31:27297 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59298 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59299 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29300
[email protected]41f558fb2012-01-09 22:59:58301 // We'll have nothing more to report to the observers after this point.
302 observers_.Clear();
303
[email protected]6d0146c2011-08-04 19:13:04304 DCHECK(save_page_downloads_.empty());
305
initial.commit09911bf2008-07-26 23:55:29306 file_manager_ = NULL;
[email protected]b441a8492012-06-06 14:55:57307 if (delegate_)
308 delegate_->Shutdown();
initial.commit09911bf2008-07-26 23:55:29309}
310
[email protected]5656f8a2011-11-17 16:12:58311void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04312 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57313 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58314
[email protected]f04182f32010-12-10 19:12:07315 for (DownloadMap::iterator it = history_downloads_.begin();
316 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50317 if (it->second->IsTemporary() &&
[email protected]3d833de2012-05-30 23:32:06318 (dir_path.empty() ||
319 it->second->GetTargetFilePath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57320 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58321 }
[email protected]6aa4a1c02010-01-15 18:49:58322}
323
[email protected]5656f8a2011-11-17 16:12:58324void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04325 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57326 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34327
[email protected]f04182f32010-12-10 19:12:07328 for (DownloadMap::iterator it = history_downloads_.begin();
329 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50330 if (!it->second->IsTemporary() &&
[email protected]3d833de2012-05-30 23:32:06331 (dir_path.empty() ||
332 it->second->GetTargetFilePath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57333 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34334 }
[email protected]8ddbd66a2010-05-21 16:38:34335}
336
[email protected]5656f8a2011-11-17 16:12:58337void DownloadManagerImpl::SearchDownloads(const string16& query,
338 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26339 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42340
[email protected]f04182f32010-12-10 19:12:07341 for (DownloadMap::iterator it = history_downloads_.begin();
342 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42343 DownloadItem* download_item = it->second;
344
[email protected]c09a8fd2011-11-21 19:54:50345 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42346 continue;
347
348 // Display Incognito downloads only in Incognito window, and vice versa.
349 // The Incognito Downloads page will get the list of non-Incognito downloads
350 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50351 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42352 continue;
353
354 if (download_item->MatchesQuery(query_lower))
355 result->push_back(download_item);
356 }
[email protected]d3b12902010-08-16 23:39:42357}
358
[email protected]5656f8a2011-11-17 16:12:58359bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03360 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29361 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
362 shutdown_needed_ = true;
363
[email protected]6d0c9fb2011-08-22 19:26:03364 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46365
initial.commit09911bf2008-07-26 23:55:29366 return true;
367}
368
[email protected]aa9881c2011-08-15 18:01:12369// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58370void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18371 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35372
[email protected]491aaa62012-06-07 03:50:18373 if (!delegate_ || delegate_->ShouldStartDownload(download_id))
[email protected]aa9881c2011-08-15 18:01:12374 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35375}
376
[email protected]5656f8a2011-11-17 16:12:58377void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48378 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
379 for (DownloadMap::iterator it = history_downloads_.begin();
380 it != history_downloads_.end(); ++it) {
381 CheckForFileRemoval(it->second);
382 }
383}
384
[email protected]5656f8a2011-11-17 16:12:58385void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48386 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
387 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50388 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48389 BrowserThread::PostTask(
390 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58391 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50392 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17393 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48394 }
395}
396
[email protected]5656f8a2011-11-17 16:12:58397void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48398 int64 db_handle, const FilePath& path) {
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
400 if (!file_util::PathExists(path)) {
401 BrowserThread::PostTask(
402 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58403 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
404 this,
405 db_handle));
[email protected]9fc114672011-06-15 08:17:48406 }
407}
408
[email protected]5656f8a2011-11-17 16:12:58409void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
411 DownloadMap::iterator it = history_downloads_.find(db_handle);
412 if (it != history_downloads_.end()) {
413 DownloadItem* download_item = it->second;
414 download_item->OnDownloadedFileRemoved();
415 }
416}
417
[email protected]443853c62011-12-22 19:22:41418void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29420
[email protected]4cd82f72011-05-23 19:15:01421 DownloadItem* download = GetActiveDownloadItem(download_id);
422 if (!download)
423 return;
424
425 VLOG(20) << __FUNCTION__ << "()"
426 << " download = " << download->DebugString(true);
427
[email protected]3d833de2012-05-30 23:32:06428 if (download->GetTargetDisposition() ==
429 DownloadItem::TARGET_DISPOSITION_PROMPT) {
initial.commit09911bf2008-07-26 23:55:29430 // We must ask the user for the place to put the download.
[email protected]491aaa62012-06-07 03:50:18431 if (delegate_) {
[email protected]31ee43f2012-06-08 23:05:36432 delegate_->ChooseDownloadPath(download);
[email protected]491aaa62012-06-07 03:50:18433 FOR_EACH_OBSERVER(Observer, observers_,
434 SelectFileDialogDisplayed(this, download_id));
435 } else {
436 FileSelectionCanceled(download_id);
437 }
initial.commit09911bf2008-07-26 23:55:29438 } else {
[email protected]3d833de2012-05-30 23:32:06439 // No prompting for download, just continue with the current target path.
440 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29441 }
442}
443
[email protected]37757c62011-12-20 20:07:12444content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58445 return browser_context_;
446}
447
448FilePath DownloadManagerImpl::LastDownloadPath() {
449 return last_download_path_;
450}
451
[email protected]ef17c9a2012-02-09 05:08:09452net::BoundNetLog DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41453 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29454 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
455
[email protected]ef17c9a2012-02-09 05:08:09456 net::BoundNetLog bound_net_log =
457 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03458 if (!info->download_id.IsValid())
459 info->download_id = GetNextId();
460 DownloadItem* download = factory_->CreateActiveItem(
[email protected]94d841cf2012-06-12 20:58:20461 this, *info, scoped_ptr<DownloadRequestHandleInterface>(
462 new DownloadRequestHandle(request_handle)).Pass(),
[email protected]ef17c9a2012-02-09 05:08:09463 browser_context_->IsOffTheRecord(), bound_net_log);
[email protected]2909e342011-10-29 00:46:53464 int32 download_id = info->download_id.local();
[email protected]d8472d92011-08-26 20:15:20465
[email protected]0634626a2012-05-03 19:04:26466 DCHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29467 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01468 active_downloads_[download_id] = download;
[email protected]ef17c9a2012-02-09 05:08:09469
470 return bound_net_log;
[email protected]c2e76012010-12-23 21:10:29471}
472
[email protected]fc03de22011-12-06 23:28:12473DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
474 const FilePath& main_file_path,
475 const GURL& page_url,
476 bool is_otr,
[email protected]6474b112012-05-04 19:35:27477 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12478 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09479 net::BoundNetLog bound_net_log =
480 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03481 DownloadItem* download = factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27482 this,
483 main_file_path,
484 page_url,
485 is_otr,
486 GetNextId(),
487 mime_type,
488 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12489
490 download->AddObserver(observer);
491
492 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
493 downloads_.insert(download);
494 save_page_downloads_[download->GetId()] = download;
495
496 // Will notify the observer in the callback.
[email protected]491aaa62012-06-07 03:50:18497 if (delegate_)
498 delegate_->AddItemToPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12499
500 return download;
501}
502
[email protected]3d833de2012-05-30 23:32:06503// The target path for the download item is now valid. We proceed with the
504// determination of an intermediate path.
505void DownloadManagerImpl::OnTargetPathAvailable(DownloadItem* download) {
[email protected]ca4b5fa32010-10-09 12:42:18506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01507 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39508
[email protected]c09a8fd2011-11-21 19:54:50509 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29510
[email protected]70850c72011-01-11 17:31:27511 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01512 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27513
[email protected]4cd82f72011-05-23 19:15:01514 VLOG(20) << __FUNCTION__ << "()"
515 << " download = " << download->DebugString(true);
516
[email protected]adb2f3d12011-01-23 16:24:54517 // Rename to intermediate name.
[email protected]3d833de2012-05-30 23:32:06518 // TODO(asanka): Skip this rename if download->AllDataSaved() is true. This
519 // avoids a spurious rename when we can just rename to the final
520 // filename. Unnecessary renames may cause bugs like
521 // http://crbug.com/74187.
522 bool ok_to_overwrite = true;
[email protected]491aaa62012-06-07 03:50:18523 FilePath intermediate_path;
524 if (delegate_) {
525 intermediate_path =
526 delegate_->GetIntermediatePath(*download, &ok_to_overwrite);
527 } else {
528 intermediate_path = download->GetTargetFilePath();
529 }
[email protected]3d833de2012-05-30 23:32:06530 // We want the intermediate and target paths to refer to the same directory so
531 // that they are both on the same device and subject to same
532 // space/permission/availability constraints.
533 DCHECK(intermediate_path.DirName() ==
534 download->GetTargetFilePath().DirName());
535 download->OnIntermediatePathDetermined(file_manager_, intermediate_path,
536 ok_to_overwrite);
initial.commit09911bf2008-07-26 23:55:29537}
538
[email protected]443853c62011-12-22 19:22:41539void DownloadManagerImpl::UpdateDownload(int32 download_id,
540 int64 bytes_so_far,
541 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00542 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27543 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
544 DownloadMap::iterator it = active_downloads_.find(download_id);
545 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29546 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26547 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41548 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]491aaa62012-06-07 03:50:18549 if (delegate_)
550 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27551 }
initial.commit09911bf2008-07-26 23:55:29552 }
553}
554
[email protected]5656f8a2011-11-17 16:12:58555void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
556 int64 size,
557 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54558 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50559 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
560 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32562
[email protected]c4f02c42011-01-24 21:55:06563 // If it's not in active_downloads_, that means it was cancelled; just
564 // ignore the notification.
565 if (active_downloads_.count(download_id) == 0)
566 return;
567
[email protected]adb2f3d12011-01-23 16:24:54568 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37569 download->OnAllDataSaved(size, hash);
[email protected]da4cd4262012-05-18 20:42:35570 MaybeCompleteDownload(download);
[email protected]adb2f3d12011-01-23 16:24:54571}
[email protected]9ccbb372008-10-10 18:50:32572
[email protected]fc03de22011-12-06 23:28:12573void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
[email protected]c09a8fd2011-11-21 19:54:50574 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]0634626a2012-05-03 19:04:26575 DCHECK(!ContainsKey(downloads_, download));
576 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
[email protected]0634626a2012-05-03 19:04:26577 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17578 return;
579 }
580
581 // Should be in downloads_ if we're not REMOVING.
582 CHECK(ContainsKey(downloads_, download));
583
584 // Check history_downloads_ consistency.
[email protected]5009b7a2012-02-21 18:47:03585 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50586 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17587 } else {
[email protected]fc03de22011-12-06 23:28:12588 for (DownloadMap::const_iterator it = history_downloads_.begin();
[email protected]7d413112011-06-16 18:50:17589 it != history_downloads_.end(); ++it) {
[email protected]0634626a2012-05-03 19:04:26590 DCHECK(it->second != download);
[email protected]7d413112011-06-16 18:50:17591 }
592 }
593
[email protected]c09a8fd2011-11-21 19:54:50594 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34595 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50596 if (ContainsKey(active_downloads_, download->GetId())) {
[email protected]5009b7a2012-02-21 18:47:03597 if (download->IsPersisted())
[email protected]c09a8fd2011-11-21 19:54:50598 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
599 if (DownloadItem::IN_PROGRESS != download->GetState())
600 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07601 }
[email protected]c09a8fd2011-11-21 19:54:50602 if (DownloadItem::IN_PROGRESS == download->GetState())
603 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59604}
605
[email protected]5656f8a2011-11-17 16:12:58606bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54607 // If we don't have all the data, the download is not ready for
608 // completion.
[email protected]c09a8fd2011-11-21 19:54:50609 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54610 return false;
[email protected]6a7fb042010-02-01 16:30:47611
[email protected]9d7ef802011-02-25 19:03:35612 // If the download is dangerous, but not yet validated, it's not ready for
613 // completion.
[email protected]c09a8fd2011-11-21 19:54:50614 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35615 return false;
616
[email protected]adb2f3d12011-01-23 16:24:54617 // If the download isn't active (e.g. has been cancelled) it's not
618 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50619 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54620 return false;
621
622 // If the download hasn't been inserted into the history system
623 // (which occurs strictly after file name determination, intermediate
624 // file rename, and UI display) then it's not ready for completion.
[email protected]5009b7a2012-02-21 18:47:03625 if (!download->IsPersisted())
[email protected]7054b592011-06-22 14:46:42626 return false;
627
628 return true;
[email protected]adb2f3d12011-01-23 16:24:54629}
630
[email protected]6474b112012-05-04 19:35:27631// When SavePackage downloads MHTML to GData (see
632// SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it
633// does for non-SavePackage downloads, but SavePackage downloads never satisfy
634// IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls
635// DownloadItem::UpdateObservers() when the upload completes so that SavePackage
636// notices that the upload has completed and runs its normal Finish() pathway.
637// MaybeCompleteDownload() is never the mechanism by which SavePackage completes
638// downloads. SavePackage always uses its own Finish() to mark downloads
639// complete.
640
[email protected]5656f8a2011-11-17 16:12:58641void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54642 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
643 VLOG(20) << __FUNCTION__ << "()" << " download = "
644 << download->DebugString(false);
645
646 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32647 return;
[email protected]9ccbb372008-10-10 18:50:32648
[email protected]adb2f3d12011-01-23 16:24:54649 // TODO(rdsmith): DCHECK that we only pass through this point
650 // once per download. The natural way to do this is by a state
651 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56652
[email protected]adb2f3d12011-01-23 16:24:54653 // Confirm we're in the proper set of states to be here;
[email protected]550520f2012-05-14 20:58:48654 // have all data, have a history handle, (validated or safe).
655 DCHECK(download->IsInProgress());
[email protected]c09a8fd2011-11-21 19:54:50656 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
[email protected]c09a8fd2011-11-21 19:54:50657 DCHECK(download->AllDataSaved());
[email protected]5009b7a2012-02-21 18:47:03658 DCHECK(download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50659 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54660
[email protected]da4cd4262012-05-18 20:42:35661 // Give the delegate a chance to override. It's ok to keep re-setting the
662 // delegate's |complete_callback| cb as long as there isn't another call-point
663 // trying to set it to a different cb. TODO(benjhayden): Change the callback
664 // to point directly to the item instead of |this| when DownloadItem supports
665 // weak-ptrs.
[email protected]491aaa62012-06-07 03:50:18666 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind(
[email protected]da4cd4262012-05-18 20:42:35667 &DownloadManagerImpl::MaybeCompleteDownloadById,
668 this, download->GetId())))
[email protected]c2918652011-11-01 18:50:23669 return;
670
[email protected]adb2f3d12011-01-23 16:24:54671 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
672 << download->DebugString(false);
673
[email protected]491aaa62012-06-07 03:50:18674 if (delegate_)
675 delegate_->UpdateItemInPersistentStore(download);
[email protected]48837962011-04-19 17:03:29676 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32677}
678
[email protected]da4cd4262012-05-18 20:42:35679void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) {
680 DownloadItem* download_item = GetActiveDownload(download_id);
681 if (download_item != NULL)
682 MaybeCompleteDownload(download_item);
683}
684
[email protected]fc03de22011-12-06 23:28:12685void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
[email protected]70850c72011-01-11 17:31:27686 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21687 DCHECK(download);
[email protected]491aaa62012-06-07 03:50:18688 if (delegate_)
689 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12690 active_downloads_.erase(download->GetId());
691 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27692}
693
[email protected]5656f8a2011-11-17 16:12:58694void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17695 DownloadItem* download = GetActiveDownload(download_id);
696 // A cancel at the right time could remove the download from the
697 // |active_downloads_| map before we get here.
698 if (!download)
699 return;
700
701 download->Cancel(true);
702}
703
[email protected]fc03de22011-12-06 23:28:12704void DownloadManagerImpl::DownloadCancelled(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20705 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20706
707 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50708 << " download = " << download->DebugString(true);
709
[email protected]93af2272011-09-21 18:29:17710 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21711 // This function is called from the DownloadItem, so DI state
712 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12713 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29714
[email protected]d25fda12012-06-12 17:05:03715 DCHECK(file_manager_);
716 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29717}
718
[email protected]bf3b08a2012-03-08 01:52:34719void DownloadManagerImpl::OnDownloadInterrupted(
720 int32 download_id,
721 int64 size,
722 const std::string& hash_state,
723 content::DownloadInterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21724 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
725
726 DownloadItem* download = GetActiveDownload(download_id);
727 if (!download)
728 return;
729
[email protected]be76b7e2011-10-13 12:57:57730 VLOG(20) << __FUNCTION__ << "()"
731 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50732 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21733 << " size = " << size
734 << " download = " << download->DebugString(true);
735
[email protected]93af2272011-09-21 18:29:17736 RemoveFromActiveList(download);
[email protected]443853c62011-12-22 19:22:41737 download->Interrupted(size, hash_state, reason);
[email protected]93af2272011-09-21 18:29:17738 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21739}
740
[email protected]5656f8a2011-11-17 16:12:58741DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26742 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
743 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26744 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21745 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26746
747 DownloadItem* download = it->second;
748
[email protected]47a881b2011-08-29 22:59:21749 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50750 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01751
[email protected]47a881b2011-08-29 22:59:21752 return download;
753}
[email protected]54610672011-07-18 18:24:43754
[email protected]5656f8a2011-11-17 16:12:58755void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
757 DCHECK(download);
758
759 // Clean up will happen when the history system create callback runs if we
760 // don't have a valid db_handle yet.
[email protected]5009b7a2012-02-21 18:47:03761 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50762 active_downloads_.erase(download->GetId());
[email protected]491aaa62012-06-07 03:50:18763 if (delegate_)
764 delegate_->UpdateItemInPersistentStore(download);
[email protected]93af2272011-09-21 18:29:17765 }
766}
767
[email protected]fd3a82832012-01-19 20:35:12768bool DownloadManagerImpl::GenerateFileHash() {
[email protected]491aaa62012-06-07 03:50:18769 return delegate_ && delegate_->GenerateFileHash();
[email protected]fd3a82832012-01-19 20:35:12770}
771
[email protected]5656f8a2011-11-17 16:12:58772int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04773 const DownloadVector& pending_deletes) {
774 if (pending_deletes.empty())
775 return 0;
776
777 // Delete from internal maps.
778 for (DownloadVector::const_iterator it = pending_deletes.begin();
779 it != pending_deletes.end();
780 ++it) {
781 DownloadItem* download = *it;
782 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50783 history_downloads_.erase(download->GetDbHandle());
784 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04785 downloads_.erase(download);
786 }
787
788 // Tell observers to refresh their views.
789 NotifyModelChanged();
790
791 // Delete the download items themselves.
792 const int num_deleted = static_cast<int>(pending_deletes.size());
793 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
794 return num_deleted;
795}
796
[email protected]fc03de22011-12-06 23:28:12797void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) {
798 if (history_downloads_.find(download->GetDbHandle()) ==
799 history_downloads_.end())
[email protected]93af2272011-09-21 18:29:17800 return;
801
802 // Make history update.
[email protected]491aaa62012-06-07 03:50:18803 if (delegate_)
804 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29805
806 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04807 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07808 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29809}
810
[email protected]fd3a82832012-01-19 20:35:12811int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
812 base::Time remove_end) {
[email protected]491aaa62012-06-07 03:50:18813 if (delegate_)
814 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29815
[email protected]a312a442010-12-15 23:40:33816 // All downloads visible to the user will be in the history,
817 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04818 DownloadVector pending_deletes;
819 for (DownloadMap::const_iterator it = history_downloads_.begin();
820 it != history_downloads_.end();
821 ++it) {
initial.commit09911bf2008-07-26 23:55:29822 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50823 if (download->GetStartTime() >= remove_begin &&
824 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04825 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12826 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28827 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29828 }
initial.commit09911bf2008-07-26 23:55:29829 }
[email protected]6d0146c2011-08-04 19:13:04830 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29831}
832
[email protected]fd3a82832012-01-19 20:35:12833int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59834 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29835}
836
[email protected]5656f8a2011-11-17 16:12:58837int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06838 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12839 // The null times make the date range unbounded.
840 return RemoveDownloadsBetween(base::Time(), base::Time());
841}
842
[email protected]0d4e30c2012-01-28 00:47:53843void DownloadManagerImpl::DownloadUrl(
[email protected]c5a5c0842012-05-04 20:05:14844 scoped_ptr<content::DownloadUrlParameters> params) {
845 if (params->post_id() >= 0) {
846 // Check this here so that the traceback is more useful.
847 DCHECK(params->prefer_cache());
848 DCHECK(params->method() == "POST");
849 }
850 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
851 &BeginDownload, base::Owned(params.release())));
initial.commit09911bf2008-07-26 23:55:29852}
853
[email protected]5656f8a2011-11-17 16:12:58854void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29855 observers_.AddObserver(observer);
[email protected]a1e41e72012-02-22 17:41:25856 // TODO: It is the responsibility of the observers to query the
857 // DownloadManager. Remove the following call from here and update all
858 // observers.
[email protected]75e51b52012-02-04 16:57:54859 observer->ModelChanged(this);
initial.commit09911bf2008-07-26 23:55:29860}
861
[email protected]5656f8a2011-11-17 16:12:58862void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29863 observers_.RemoveObserver(observer);
864}
865
[email protected]84d57412012-03-03 08:59:55866void DownloadManagerImpl::FileSelected(const FilePath& path,
867 int32 download_id) {
[email protected]4cd82f72011-05-23 19:15:01868 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3d833de2012-05-30 23:32:06869 DCHECK(!path.empty());
[email protected]4cd82f72011-05-23 19:15:01870
[email protected]4cd82f72011-05-23 19:15:01871 DownloadItem* download = GetActiveDownloadItem(download_id);
872 if (!download)
873 return;
874 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
[email protected]3d833de2012-05-30 23:32:06875 << " download = " << download->DebugString(true);
[email protected]4cd82f72011-05-23 19:15:01876
[email protected]3d833de2012-05-30 23:32:06877 // Retain the last directory. Exclude temporary downloads since the path
878 // likely points at the location of a temporary file.
879 if (!download->IsTemporary())
[email protected]7ae7c2cb2009-01-06 23:31:41880 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35881
[email protected]4cd82f72011-05-23 19:15:01882 // Make sure the initial file name is set only once.
[email protected]3d833de2012-05-30 23:32:06883 download->OnTargetPathSelected(path);
884 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29885}
886
[email protected]84d57412012-03-03 08:59:55887void DownloadManagerImpl::FileSelectionCanceled(int32 download_id) {
initial.commit09911bf2008-07-26 23:55:29888 // The user didn't pick a place to save the file, so need to cancel the
889 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01890 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01891
892 DownloadItem* download = GetActiveDownloadItem(download_id);
893 if (!download)
894 return;
895
896 VLOG(20) << __FUNCTION__ << "()"
897 << " download = " << download->DebugString(true);
898
[email protected]8f8bc112012-02-22 12:36:31899 download->Cancel(true);
[email protected]4cd82f72011-05-23 19:15:01900}
901
initial.commit09911bf2008-07-26 23:55:29902// Operations posted to us from the history service ----------------------------
903
904// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25905// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58906void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25907 std::vector<DownloadPersistentStoreInfo>* entries) {
initial.commit09911bf2008-07-26 23:55:29908 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]deb40832012-02-23 15:41:37909 int64 db_handle = entries->at(i).db_handle;
910 base::debug::Alias(&db_handle);
[email protected]0634626a2012-05-03 19:04:26911 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]deb40832012-02-23 15:41:37912
[email protected]ef17c9a2012-02-09 05:08:09913 net::BoundNetLog bound_net_log =
914 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03915 DownloadItem* download = factory_->CreatePersistedItem(
[email protected]ef17c9a2012-02-09 05:08:09916 this, GetNextId(), entries->at(i), bound_net_log);
[email protected]f04182f32010-12-10 19:12:07917 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50918 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50919 VLOG(20) << __FUNCTION__ << "()" << i << ">"
920 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29921 }
[email protected]b0ab1d42010-02-24 19:29:28922 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48923 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29924}
925
[email protected]5656f8a2011-11-17 16:12:58926void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
927 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27928 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0634626a2012-05-03 19:04:26929 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33930
[email protected]da4a5582011-10-17 19:08:06931 download_stats::RecordHistorySize(history_downloads_.size());
932
[email protected]5009b7a2012-02-21 18:47:03933 DCHECK(!download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50934 download->SetDbHandle(db_handle);
[email protected]5009b7a2012-02-21 18:47:03935 download->SetIsPersisted();
[email protected]5bcd73eb2011-03-23 21:14:02936
[email protected]0634626a2012-05-03 19:04:26937 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]c09a8fd2011-11-21 19:54:50938 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04939
940 // Show in the appropriate browser UI.
941 // This includes buttons to save or cancel, for a dangerous download.
942 ShowDownloadInBrowser(download);
943
944 // Inform interested objects about the new download.
945 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19946}
947
[email protected]2588ea9d2011-08-22 20:59:53948
[email protected]5656f8a2011-11-17 16:12:58949void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
950 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53951 if (save_page_downloads_.count(download_id)) {
952 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
953 } else if (active_downloads_.count(download_id)) {
954 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
955 }
956 // It's valid that we don't find a matching item, i.e. on shutdown.
957}
958
[email protected]f9a45b02011-06-30 23:49:19959// Once the new DownloadItem's creation info has been committed to the history
960// service, we associate the DownloadItem with the db handle, update our
961// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58962void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
963 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19964 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45965 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17966 if (!download)
[email protected]19420cc2011-07-18 17:43:45967 return;
[email protected]54610672011-07-18 18:24:43968
969 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
970 << " download_id = " << download_id
971 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19972
[email protected]e5107ce2011-09-19 20:36:13973 int32 matching_item_download_id
974 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50975 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13976 base::debug::Alias(&matching_item_download_id);
977
[email protected]0634626a2012-05-03 19:04:26978 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20979
[email protected]f9a45b02011-06-30 23:49:19980 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29981
[email protected]93af2272011-09-21 18:29:17982 // If the download is still in progress, try to complete it.
983 //
984 // Otherwise, download has been cancelled or interrupted before we've
985 // received the DB handle. We post one final message to the history
986 // service so that it can be properly in sync with the DownloadItem's
987 // completion status, and also inform any observers so that they get
988 // more than just the start notification.
989 if (download->IsInProgress()) {
990 MaybeCompleteDownload(download);
991 } else {
[email protected]0634626a2012-05-03 19:04:26992 DCHECK(download->IsCancelled());
[email protected]93af2272011-09-21 18:29:17993 active_downloads_.erase(download_id);
[email protected]491aaa62012-06-07 03:50:18994 if (delegate_)
995 delegate_->UpdateItemInPersistentStore(download);
[email protected]93af2272011-09-21 18:29:17996 download->UpdateObservers();
997 }
initial.commit09911bf2008-07-26 23:55:29998}
999
[email protected]5656f8a2011-11-17 16:12:581000void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]a29e4f22012-04-12 21:22:031001 // The 'contents' may no longer exist if the user closed the contents before
1002 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:381003 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:261004
1005 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:031006 // contents.
[email protected]491aaa62012-06-07 03:50:181007 if (!content && delegate_)
[email protected]ef9572e2012-01-04 22:14:121008 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:531009
[email protected]0bfbf882011-12-22 18:19:271010 if (content && content->GetDelegate())
1011 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:531012}
1013
[email protected]5656f8a2011-11-17 16:12:581014int DownloadManagerImpl::InProgressCount() const {
[email protected]550520f2012-05-14 20:58:481015 // Don't use active_downloads_.count() because Cancel() leaves items in
1016 // active_downloads_ if they haven't made it into the persistent store yet.
[email protected]007e7412012-03-13 20:10:561017 // Need to actually look at each item's state.
1018 int count = 0;
[email protected]550520f2012-05-14 20:58:481019 for (DownloadMap::const_iterator it = active_downloads_.begin();
1020 it != active_downloads_.end(); ++it) {
[email protected]007e7412012-03-13 20:10:561021 DownloadItem* item = it->second;
1022 if (item->IsInProgress())
1023 ++count;
1024 }
1025 return count;
[email protected]5656f8a2011-11-17 16:12:581026}
1027
[email protected]6cade212008-12-03 00:32:221028// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:581029void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411030 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381031}
[email protected]b0ab1d42010-02-24 19:29:281032
[email protected]5656f8a2011-11-17 16:12:581033void DownloadManagerImpl::NotifyModelChanged() {
[email protected]75e51b52012-02-04 16:57:541034 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this));
[email protected]b0ab1d42010-02-24 19:29:281035}
1036
[email protected]5656f8a2011-11-17 16:12:581037DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:011038 // The |history_downloads_| map is indexed by the download's db_handle,
1039 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071040 for (DownloadMap::iterator it = history_downloads_.begin();
1041 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361042 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:501043 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:361044 return item;
1045 }
1046 return NULL;
1047}
1048
[email protected]5656f8a2011-11-17 16:12:581049DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:361050 if (ContainsKey(active_downloads_, download_id))
1051 return active_downloads_[download_id];
1052 return NULL;
[email protected]4cd82f72011-05-23 19:15:011053}
1054
[email protected]57fd1252010-12-23 17:24:091055// Confirm that everything in all maps is also in |downloads_|, and that
1056// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581057void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071058#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091059 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:041060 const DownloadMap* input_maps[] = {&active_downloads_,
1061 &history_downloads_,
1062 &save_page_downloads_};
1063 DownloadSet active_set, history_set, save_page_set;
1064 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
1065 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091066 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1067 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041068 it != input_maps[i]->end(); ++it) {
1069 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071070 }
1071 }
[email protected]57fd1252010-12-23 17:24:091072
1073 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091074 DownloadSet downloads_union;
1075 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1076 DownloadSet remainder;
1077 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1078 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1079 downloads_.begin(), downloads_.end(),
1080 insert_it);
1081 DCHECK(remainder.empty());
1082 std::insert_iterator<DownloadSet>
1083 insert_union(downloads_union, downloads_union.end());
1084 std::set_union(downloads_union.begin(), downloads_union.end(),
1085 all_sets[i]->begin(), all_sets[i]->end(),
1086 insert_union);
1087 }
1088
1089 // Is everything in downloads_ present in one of the other sets?
1090 DownloadSet remainder;
1091 std::insert_iterator<DownloadSet>
1092 insert_remainder(remainder, remainder.begin());
1093 std::set_difference(downloads_.begin(), downloads_.end(),
1094 downloads_union.begin(), downloads_union.end(),
1095 insert_remainder);
1096 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071097#endif
1098}
1099
[email protected]6d0146c2011-08-04 19:13:041100// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531101// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041102// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531103// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1104// that the history event is update regardless of the order in which these two
1105// events complete.
[email protected]6d0146c2011-08-04 19:13:041106// If something removes the download item from the download manager (Remove,
1107// Shutdown) the result will be that the SavePage system will not be able to
1108// properly update the download item (which no longer exists) or the download
1109// history, but the action will complete properly anyway. This may lead to the
1110// history entry being wrong on a reload of chrome (specifically in the case of
1111// Initiation -> History Callback -> Removal -> Completion), but there's no way
1112// to solve that without canceling on Remove (which would then update the DB).
1113
[email protected]5656f8a2011-11-17 16:12:581114void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1115 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1117
1118 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1119 // This can happen if the download manager is shutting down and all maps
1120 // have been cleared.
1121 if (it == save_page_downloads_.end())
1122 return;
1123
1124 DownloadItem* download = it->second;
1125 if (!download) {
1126 NOTREACHED();
1127 return;
1128 }
1129
[email protected]0634626a2012-05-03 19:04:261130 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201131
[email protected]6d0146c2011-08-04 19:13:041132 AddDownloadItemToHistory(download, db_handle);
1133
1134 // Finalize this download if it finished before the history callback.
1135 if (!download->IsInProgress())
1136 SavePageDownloadFinished(download);
1137}
1138
[email protected]5656f8a2011-11-17 16:12:581139void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]5009b7a2012-02-21 18:47:031140 if (download->IsPersisted()) {
[email protected]491aaa62012-06-07 03:50:181141 if (delegate_)
1142 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501143 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1144 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041145
1146 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071147 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041148 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531149 content::Source<DownloadManager>(this),
1150 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041151 }
1152}
[email protected]da4a5582011-10-17 19:08:061153
[email protected]fc03de22011-12-06 23:28:121154void DownloadManagerImpl::DownloadOpened(DownloadItem* download) {
[email protected]491aaa62012-06-07 03:50:181155 if (delegate_)
1156 delegate_->UpdateItemInPersistentStore(download);
[email protected]da4a5582011-10-17 19:08:061157 int num_unopened = 0;
1158 for (DownloadMap::iterator it = history_downloads_.begin();
1159 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501160 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061161 ++num_unopened;
1162 }
1163 download_stats::RecordOpensOutstanding(num_unopened);
1164}
[email protected]5656f8a2011-11-17 16:12:581165
[email protected]3d833de2012-05-30 23:32:061166void DownloadManagerImpl::DownloadRenamedToIntermediateName(
1167 DownloadItem* download) {
1168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1169 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1170 // receive the DownloadRenamedToIntermediateName() call.
[email protected]491aaa62012-06-07 03:50:181171 if (delegate_)
1172 delegate_->AddItemToPersistentStore(download);
[email protected]3d833de2012-05-30 23:32:061173}
1174
1175void DownloadManagerImpl::DownloadRenamedToFinalName(
1176 DownloadItem* download) {
1177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1178 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1179 // receive the DownloadRenamedToFinalName() call.
[email protected]491aaa62012-06-07 03:50:181180 if (delegate_) {
1181 delegate_->UpdatePathForItemInPersistentStore(
1182 download, download->GetFullPath());
1183 }
[email protected]3d833de2012-05-30 23:32:061184}