blob: b374565ae68b6e5efedac2cf2790c57b80b091e8 [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.
56 content::ResourceDispatcherHostImpl* resource_dispatcher_host =
57 static_cast<content::ResourceDispatcherHostImpl*>(
58 params->resource_dispatcher_host());
59 scoped_ptr<net::URLRequest> request(new net::URLRequest(
60 params->url(), resource_dispatcher_host));
[email protected]f859eba2012-05-30 17:22:4961 request->set_referrer(params->referrer().url.spec());
62 webkit_glue::ConfigureURLRequestForReferrerPolicy(
63 request.get(), params->referrer().policy);
[email protected]c5a5c0842012-05-04 20:05:1464 request->set_load_flags(request->load_flags() | params->load_flags());
65 request->set_method(params->method());
66 if (!params->post_body().empty())
67 request->AppendBytesToUpload(params->post_body().data(),
68 params->post_body().size());
69 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1470 // The POST in this case does not have an actual body, and only works
71 // when retrieving data from cache. This is done because we don't want
72 // to do a re-POST without user consent, and currently don't have a good
73 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1474 DCHECK(params->prefer_cache());
75 DCHECK(params->method() == "POST");
[email protected]27678b2a2012-02-04 22:09:1476 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
[email protected]c5a5c0842012-05-04 20:05:1477 upload_data->set_identifier(params->post_id());
[email protected]27678b2a2012-02-04 22:09:1478 request->set_upload(upload_data);
79 }
[email protected]c5a5c0842012-05-04 20:05:1480 for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter
81 = params->request_headers_begin();
82 iter != params->request_headers_end();
83 ++iter) {
84 request->SetExtraRequestHeaderByName(
85 iter->first, iter->second, false/*overwrite*/);
86 }
[email protected]c79a0c02011-08-22 22:37:3787 resource_dispatcher_host->BeginDownload(
[email protected]ea114722012-03-12 01:11:2588 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:0689 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:1490 params->resource_context(),
91 params->render_process_host_id(),
92 params->render_view_host_routing_id(),
93 params->prefer_cache(),
94 params->save_info(),
95 params->callback());
[email protected]a0ce3282011-08-19 20:49:5296}
97
[email protected]33d22102012-01-25 17:46:5398class MapValueIteratorAdapter {
99 public:
100 explicit MapValueIteratorAdapter(
101 base::hash_map<int64, DownloadItem*>::const_iterator iter)
102 : iter_(iter) {
103 }
104 ~MapValueIteratorAdapter() {}
105
106 DownloadItem* operator*() { return iter_->second; }
107
108 MapValueIteratorAdapter& operator++() {
109 ++iter_;
110 return *this;
111 }
112
113 bool operator!=(const MapValueIteratorAdapter& that) const {
114 return iter_ != that.iter_;
115 }
116
117 private:
118 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
119 // Allow copy and assign.
120};
121
[email protected]5948e1a2012-03-10 00:19:18122void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm,
123 bool* result) {
124 if (dfm->NumberOfActiveDownloads())
125 *result = false;
126 BrowserThread::PostTask(
127 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
128}
129
[email protected]0a5c9112012-03-12 17:49:02130void EnsureNoPendingDownloadJobsOnIO(bool* result) {
[email protected]5948e1a2012-03-10 00:19:18131 scoped_refptr<DownloadFileManager> download_file_manager =
[email protected]ea114722012-03-12 01:11:25132 ResourceDispatcherHostImpl::Get()->download_file_manager();
[email protected]5948e1a2012-03-10 00:19:18133 BrowserThread::PostTask(
134 BrowserThread::FILE, FROM_HERE,
135 base::Bind(&EnsureNoPendingDownloadsOnFile,
136 download_file_manager, result));
137}
138
[email protected]d25fda12012-06-12 17:05:03139class DownloadItemFactoryImpl : public content::DownloadItemFactory {
140 public:
141 DownloadItemFactoryImpl() {}
142 virtual ~DownloadItemFactoryImpl() {}
143
144 virtual content::DownloadItem* CreatePersistedItem(
145 DownloadItemImpl::Delegate* delegate,
146 content::DownloadId download_id,
147 const content::DownloadPersistentStoreInfo& info,
148 const net::BoundNetLog& bound_net_log) OVERRIDE {
149 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
150 }
151
152 virtual content::DownloadItem* CreateActiveItem(
153 DownloadItemImpl::Delegate* delegate,
154 const DownloadCreateInfo& info,
155 DownloadRequestHandleInterface* request_handle,
156 bool is_otr,
157 const net::BoundNetLog& bound_net_log) OVERRIDE {
158 return new DownloadItemImpl(delegate, info, request_handle, is_otr,
159 bound_net_log);
160 }
161
162 virtual content::DownloadItem* CreateSavePageItem(
163 DownloadItemImpl::Delegate* delegate,
164 const FilePath& path,
165 const GURL& url,
166 bool is_otr,
167 content::DownloadId download_id,
168 const std::string& mime_type,
169 const net::BoundNetLog& bound_net_log) OVERRIDE {
170 return new DownloadItemImpl(delegate, path, url, is_otr, download_id,
171 mime_type, bound_net_log);
172 }
173};
174
[email protected]a0ce3282011-08-19 20:49:52175} // namespace
176
[email protected]99907362012-01-11 05:41:40177namespace content {
178
[email protected]5948e1a2012-03-10 00:19:18179bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
181 bool result = true;
182 BrowserThread::PostTask(
183 BrowserThread::IO, FROM_HERE,
[email protected]0a5c9112012-03-12 17:49:02184 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result));
[email protected]5948e1a2012-03-10 00:19:18185 MessageLoop::current()->Run();
186 return result;
187}
188
[email protected]99907362012-01-11 05:41:40189} // namespace content
190
[email protected]d25fda12012-06-12 17:05:03191DownloadManagerImpl::DownloadManagerImpl(
192 DownloadFileManager* file_manager,
193 scoped_ptr<content::DownloadItemFactory> factory,
194 net::NetLog* net_log)
195 : factory_(factory.Pass()),
196 shutdown_needed_(false),
[email protected]b441a8492012-06-06 14:55:57197 browser_context_(NULL),
[email protected]d25fda12012-06-12 17:05:03198 file_manager_(file_manager),
[email protected]b441a8492012-06-06 14:55:57199 net_log_(net_log) {
[email protected]d25fda12012-06-12 17:05:03200 DCHECK(file_manager);
201 if (!factory_.get())
202 factory_.reset(new DownloadItemFactoryImpl());
initial.commit09911bf2008-07-26 23:55:29203}
204
[email protected]5656f8a2011-11-17 16:12:58205DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13206 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29207}
208
[email protected]5656f8a2011-11-17 16:12:58209DownloadId DownloadManagerImpl::GetNextId() {
[email protected]491aaa62012-06-07 03:50:18210 DownloadId id;
211 if (delegate_)
212 id = delegate_->GetNextId();
213 if (!id.IsValid()) {
214 static int next_id;
215 id = DownloadId(browser_context_, ++next_id);
216 }
217
218 return id;
[email protected]2909e342011-10-29 00:46:53219}
220
[email protected]fc03de22011-12-06 23:28:12221bool DownloadManagerImpl::ShouldOpenDownload(DownloadItem* item) {
[email protected]491aaa62012-06-07 03:50:18222 if (!delegate_)
223 return true;
224
[email protected]fc03de22011-12-06 23:28:12225 return delegate_->ShouldOpenDownload(item);
226}
227
228bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18229 if (!delegate_)
230 return false;
231
[email protected]fc03de22011-12-06 23:28:12232 return delegate_->ShouldOpenFileBasedOnExtension(path);
233}
234
[email protected]b441a8492012-06-06 14:55:57235void DownloadManagerImpl::SetDelegate(
236 content::DownloadManagerDelegate* delegate) {
237 delegate_ = delegate;
238}
239
[email protected]b488b5a52012-06-06 17:01:28240content::DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
241 return delegate_;
242}
243
[email protected]5656f8a2011-11-17 16:12:58244void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50245 VLOG(20) << __FUNCTION__ << "()"
246 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13247 if (!shutdown_needed_)
248 return;
249 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29250
[email protected]75e51b52012-02-04 16:57:54251 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08252 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13253
[email protected]d25fda12012-06-12 17:05:03254 DCHECK(file_manager_);
255 BrowserThread::PostTask(
256 BrowserThread::FILE, FROM_HERE,
257 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
258 file_manager_, make_scoped_refptr(this)));
initial.commit09911bf2008-07-26 23:55:29259
[email protected]f04182f32010-12-10 19:12:07260 AssertContainersConsistent();
261
262 // Go through all downloads in downloads_. Dangerous ones we need to
263 // remove on disk, and in progress ones we need to cancel.
[email protected]57fd1252010-12-23 17:24:09264 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]f04182f32010-12-10 19:12:07265 DownloadItem* download = *it;
266
267 // Save iterator from potential erases in this set done by called code.
268 // Iterators after an erasure point are still valid for lists and
269 // associative containers such as sets.
270 it++;
271
[email protected]c09a8fd2011-11-21 19:54:50272 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29273 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07274 // The user hasn't accepted it, so we need to remove it
275 // from the disk. This may or may not result in it being
276 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12277 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07278 // removes and deletes it if it's known to the history service)
279 // so the only thing we know after calling this function is that
280 // the download was deleted if-and-only-if it was removed
281 // from all queues.
[email protected]303077002011-04-19 23:21:01282 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26283 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17284 download->Cancel(false);
[email protected]491aaa62012-06-07 03:50:18285 if (delegate_)
286 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29287 }
288 }
289
[email protected]f04182f32010-12-10 19:12:07290 // At this point, all dangerous downloads have had their files removed
291 // and all in progress downloads have been cancelled. We can now delete
292 // anything left.
[email protected]9ccbb372008-10-10 18:50:32293
[email protected]5cd11b6e2011-06-10 20:30:59294 // Copy downloads_ to separate container so as not to set off checks
295 // in DownloadItem destruction.
296 DownloadSet downloads_to_delete;
297 downloads_to_delete.swap(downloads_);
298
[email protected]70850c72011-01-11 17:31:27299 active_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59300 history_downloads_.clear();
[email protected]5cd11b6e2011-06-10 20:30:59301 STLDeleteElements(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29302
[email protected]41f558fb2012-01-09 22:59:58303 // We'll have nothing more to report to the observers after this point.
304 observers_.Clear();
305
[email protected]6d0146c2011-08-04 19:13:04306 DCHECK(save_page_downloads_.empty());
307
initial.commit09911bf2008-07-26 23:55:29308 file_manager_ = NULL;
[email protected]b441a8492012-06-06 14:55:57309 if (delegate_)
310 delegate_->Shutdown();
initial.commit09911bf2008-07-26 23:55:29311}
312
[email protected]5656f8a2011-11-17 16:12:58313void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04314 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57315 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58316
[email protected]f04182f32010-12-10 19:12:07317 for (DownloadMap::iterator it = history_downloads_.begin();
318 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50319 if (it->second->IsTemporary() &&
[email protected]3d833de2012-05-30 23:32:06320 (dir_path.empty() ||
321 it->second->GetTargetFilePath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57322 result->push_back(it->second);
[email protected]6aa4a1c02010-01-15 18:49:58323 }
[email protected]6aa4a1c02010-01-15 18:49:58324}
325
[email protected]5656f8a2011-11-17 16:12:58326void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04327 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57328 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34329
[email protected]f04182f32010-12-10 19:12:07330 for (DownloadMap::iterator it = history_downloads_.begin();
331 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:50332 if (!it->second->IsTemporary() &&
[email protected]3d833de2012-05-30 23:32:06333 (dir_path.empty() ||
334 it->second->GetTargetFilePath().DirName() == dir_path))
[email protected]82f37b02010-07-29 22:04:57335 result->push_back(it->second);
[email protected]8ddbd66a2010-05-21 16:38:34336 }
[email protected]8ddbd66a2010-05-21 16:38:34337}
338
[email protected]5656f8a2011-11-17 16:12:58339void DownloadManagerImpl::SearchDownloads(const string16& query,
340 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26341 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42342
[email protected]f04182f32010-12-10 19:12:07343 for (DownloadMap::iterator it = history_downloads_.begin();
344 it != history_downloads_.end(); ++it) {
[email protected]d3b12902010-08-16 23:39:42345 DownloadItem* download_item = it->second;
346
[email protected]c09a8fd2011-11-21 19:54:50347 if (download_item->IsTemporary())
[email protected]d3b12902010-08-16 23:39:42348 continue;
349
350 // Display Incognito downloads only in Incognito window, and vice versa.
351 // The Incognito Downloads page will get the list of non-Incognito downloads
352 // from its parent profile.
[email protected]c09a8fd2011-11-21 19:54:50353 if (browser_context_->IsOffTheRecord() != download_item->IsOtr())
[email protected]d3b12902010-08-16 23:39:42354 continue;
355
356 if (download_item->MatchesQuery(query_lower))
357 result->push_back(download_item);
358 }
[email protected]d3b12902010-08-16 23:39:42359}
360
[email protected]5656f8a2011-11-17 16:12:58361bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03362 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29363 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
364 shutdown_needed_ = true;
365
[email protected]6d0c9fb2011-08-22 19:26:03366 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46367
initial.commit09911bf2008-07-26 23:55:29368 return true;
369}
370
[email protected]aa9881c2011-08-15 18:01:12371// We have received a message from DownloadFileManager about a new download.
[email protected]5656f8a2011-11-17 16:12:58372void DownloadManagerImpl::StartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35374
[email protected]491aaa62012-06-07 03:50:18375 if (!delegate_ || delegate_->ShouldStartDownload(download_id))
[email protected]aa9881c2011-08-15 18:01:12376 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35377}
378
[email protected]5656f8a2011-11-17 16:12:58379void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48380 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
381 for (DownloadMap::iterator it = history_downloads_.begin();
382 it != history_downloads_.end(); ++it) {
383 CheckForFileRemoval(it->second);
384 }
385}
386
[email protected]5656f8a2011-11-17 16:12:58387void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) {
[email protected]9fc114672011-06-15 08:17:48388 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
389 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50390 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48391 BrowserThread::PostTask(
392 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58393 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]c09a8fd2011-11-21 19:54:50394 this, download_item->GetDbHandle(),
[email protected]fabf36d22011-10-28 21:50:17395 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48396 }
397}
398
[email protected]5656f8a2011-11-17 16:12:58399void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]9fc114672011-06-15 08:17:48400 int64 db_handle, const FilePath& path) {
401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
402 if (!file_util::PathExists(path)) {
403 BrowserThread::PostTask(
404 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58405 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
406 this,
407 db_handle));
[email protected]9fc114672011-06-15 08:17:48408 }
409}
410
[email protected]5656f8a2011-11-17 16:12:58411void DownloadManagerImpl::OnFileRemovalDetected(int64 db_handle) {
[email protected]9fc114672011-06-15 08:17:48412 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
413 DownloadMap::iterator it = history_downloads_.find(db_handle);
414 if (it != history_downloads_.end()) {
415 DownloadItem* download_item = it->second;
416 download_item->OnDownloadedFileRemoved();
417 }
418}
419
[email protected]443853c62011-12-22 19:22:41420void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29422
[email protected]4cd82f72011-05-23 19:15:01423 DownloadItem* download = GetActiveDownloadItem(download_id);
424 if (!download)
425 return;
426
427 VLOG(20) << __FUNCTION__ << "()"
428 << " download = " << download->DebugString(true);
429
[email protected]3d833de2012-05-30 23:32:06430 if (download->GetTargetDisposition() ==
431 DownloadItem::TARGET_DISPOSITION_PROMPT) {
initial.commit09911bf2008-07-26 23:55:29432 // We must ask the user for the place to put the download.
[email protected]491aaa62012-06-07 03:50:18433 if (delegate_) {
[email protected]31ee43f2012-06-08 23:05:36434 delegate_->ChooseDownloadPath(download);
[email protected]491aaa62012-06-07 03:50:18435 FOR_EACH_OBSERVER(Observer, observers_,
436 SelectFileDialogDisplayed(this, download_id));
437 } else {
438 FileSelectionCanceled(download_id);
439 }
initial.commit09911bf2008-07-26 23:55:29440 } else {
[email protected]3d833de2012-05-30 23:32:06441 // No prompting for download, just continue with the current target path.
442 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29443 }
444}
445
[email protected]37757c62011-12-20 20:07:12446content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58447 return browser_context_;
448}
449
450FilePath DownloadManagerImpl::LastDownloadPath() {
451 return last_download_path_;
452}
453
[email protected]ef17c9a2012-02-09 05:08:09454net::BoundNetLog DownloadManagerImpl::CreateDownloadItem(
[email protected]594e66fe2011-10-25 22:49:41455 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
[email protected]c2e76012010-12-23 21:10:29456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
457
[email protected]ef17c9a2012-02-09 05:08:09458 net::BoundNetLog bound_net_log =
459 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03460 if (!info->download_id.IsValid())
461 info->download_id = GetNextId();
462 DownloadItem* download = factory_->CreateActiveItem(
[email protected]ae77da82011-11-01 19:17:29463 this, *info, new DownloadRequestHandle(request_handle),
[email protected]ef17c9a2012-02-09 05:08:09464 browser_context_->IsOffTheRecord(), bound_net_log);
[email protected]2909e342011-10-29 00:46:53465 int32 download_id = info->download_id.local();
[email protected]d8472d92011-08-26 20:15:20466
[email protected]0634626a2012-05-03 19:04:26467 DCHECK(!ContainsKey(active_downloads_, download_id));
[email protected]c2e76012010-12-23 21:10:29468 downloads_.insert(download);
[email protected]4cd82f72011-05-23 19:15:01469 active_downloads_[download_id] = download;
[email protected]ef17c9a2012-02-09 05:08:09470
471 return bound_net_log;
[email protected]c2e76012010-12-23 21:10:29472}
473
[email protected]fc03de22011-12-06 23:28:12474DownloadItem* DownloadManagerImpl::CreateSavePackageDownloadItem(
475 const FilePath& main_file_path,
476 const GURL& page_url,
477 bool is_otr,
[email protected]6474b112012-05-04 19:35:27478 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12479 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09480 net::BoundNetLog bound_net_log =
481 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03482 DownloadItem* download = factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27483 this,
484 main_file_path,
485 page_url,
486 is_otr,
487 GetNextId(),
488 mime_type,
489 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12490
491 download->AddObserver(observer);
492
493 DCHECK(!ContainsKey(save_page_downloads_, download->GetId()));
494 downloads_.insert(download);
495 save_page_downloads_[download->GetId()] = download;
496
497 // Will notify the observer in the callback.
[email protected]491aaa62012-06-07 03:50:18498 if (delegate_)
499 delegate_->AddItemToPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12500
501 return download;
502}
503
[email protected]3d833de2012-05-30 23:32:06504// The target path for the download item is now valid. We proceed with the
505// determination of an intermediate path.
506void DownloadManagerImpl::OnTargetPathAvailable(DownloadItem* download) {
[email protected]ca4b5fa32010-10-09 12:42:18507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01508 DCHECK(download);
[email protected]aa033af2010-07-27 18:16:39509
[email protected]c09a8fd2011-11-21 19:54:50510 int32 download_id = download->GetId();
initial.commit09911bf2008-07-26 23:55:29511
[email protected]70850c72011-01-11 17:31:27512 DCHECK(ContainsKey(downloads_, download));
[email protected]4cd82f72011-05-23 19:15:01513 DCHECK(ContainsKey(active_downloads_, download_id));
[email protected]70850c72011-01-11 17:31:27514
[email protected]4cd82f72011-05-23 19:15:01515 VLOG(20) << __FUNCTION__ << "()"
516 << " download = " << download->DebugString(true);
517
[email protected]adb2f3d12011-01-23 16:24:54518 // Rename to intermediate name.
[email protected]3d833de2012-05-30 23:32:06519 // TODO(asanka): Skip this rename if download->AllDataSaved() is true. This
520 // avoids a spurious rename when we can just rename to the final
521 // filename. Unnecessary renames may cause bugs like
522 // http://crbug.com/74187.
523 bool ok_to_overwrite = true;
[email protected]491aaa62012-06-07 03:50:18524 FilePath intermediate_path;
525 if (delegate_) {
526 intermediate_path =
527 delegate_->GetIntermediatePath(*download, &ok_to_overwrite);
528 } else {
529 intermediate_path = download->GetTargetFilePath();
530 }
[email protected]3d833de2012-05-30 23:32:06531 // We want the intermediate and target paths to refer to the same directory so
532 // that they are both on the same device and subject to same
533 // space/permission/availability constraints.
534 DCHECK(intermediate_path.DirName() ==
535 download->GetTargetFilePath().DirName());
536 download->OnIntermediatePathDetermined(file_manager_, intermediate_path,
537 ok_to_overwrite);
initial.commit09911bf2008-07-26 23:55:29538}
539
[email protected]443853c62011-12-22 19:22:41540void DownloadManagerImpl::UpdateDownload(int32 download_id,
541 int64 bytes_so_far,
542 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00543 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
545 DownloadMap::iterator it = active_downloads_.find(download_id);
546 if (it != active_downloads_.end()) {
initial.commit09911bf2008-07-26 23:55:29547 DownloadItem* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26548 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41549 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]491aaa62012-06-07 03:50:18550 if (delegate_)
551 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27552 }
initial.commit09911bf2008-07-26 23:55:29553 }
554}
555
[email protected]5656f8a2011-11-17 16:12:58556void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
557 int64 size,
558 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50560 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
561 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32563
[email protected]c4f02c42011-01-24 21:55:06564 // If it's not in active_downloads_, that means it was cancelled; just
565 // ignore the notification.
566 if (active_downloads_.count(download_id) == 0)
567 return;
568
[email protected]adb2f3d12011-01-23 16:24:54569 DownloadItem* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37570 download->OnAllDataSaved(size, hash);
[email protected]da4cd4262012-05-18 20:42:35571 MaybeCompleteDownload(download);
[email protected]adb2f3d12011-01-23 16:24:54572}
[email protected]9ccbb372008-10-10 18:50:32573
[email protected]fc03de22011-12-06 23:28:12574void DownloadManagerImpl::AssertStateConsistent(DownloadItem* download) const {
[email protected]c09a8fd2011-11-21 19:54:50575 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]0634626a2012-05-03 19:04:26576 DCHECK(!ContainsKey(downloads_, download));
577 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
[email protected]0634626a2012-05-03 19:04:26578 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17579 return;
580 }
581
582 // Should be in downloads_ if we're not REMOVING.
583 CHECK(ContainsKey(downloads_, download));
584
585 // Check history_downloads_ consistency.
[email protected]5009b7a2012-02-21 18:47:03586 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50587 CHECK(ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]7d413112011-06-16 18:50:17588 } else {
[email protected]fc03de22011-12-06 23:28:12589 for (DownloadMap::const_iterator it = history_downloads_.begin();
[email protected]7d413112011-06-16 18:50:17590 it != history_downloads_.end(); ++it) {
[email protected]0634626a2012-05-03 19:04:26591 DCHECK(it->second != download);
[email protected]7d413112011-06-16 18:50:17592 }
593 }
594
[email protected]c09a8fd2011-11-21 19:54:50595 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34596 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50597 if (ContainsKey(active_downloads_, download->GetId())) {
[email protected]5009b7a2012-02-21 18:47:03598 if (download->IsPersisted())
[email protected]c09a8fd2011-11-21 19:54:50599 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
600 if (DownloadItem::IN_PROGRESS != download->GetState())
601 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07602 }
[email protected]c09a8fd2011-11-21 19:54:50603 if (DownloadItem::IN_PROGRESS == download->GetState())
604 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59605}
606
[email protected]5656f8a2011-11-17 16:12:58607bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54608 // If we don't have all the data, the download is not ready for
609 // completion.
[email protected]c09a8fd2011-11-21 19:54:50610 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54611 return false;
[email protected]6a7fb042010-02-01 16:30:47612
[email protected]9d7ef802011-02-25 19:03:35613 // If the download is dangerous, but not yet validated, it's not ready for
614 // completion.
[email protected]c09a8fd2011-11-21 19:54:50615 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35616 return false;
617
[email protected]adb2f3d12011-01-23 16:24:54618 // If the download isn't active (e.g. has been cancelled) it's not
619 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50620 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54621 return false;
622
623 // If the download hasn't been inserted into the history system
624 // (which occurs strictly after file name determination, intermediate
625 // file rename, and UI display) then it's not ready for completion.
[email protected]5009b7a2012-02-21 18:47:03626 if (!download->IsPersisted())
[email protected]7054b592011-06-22 14:46:42627 return false;
628
629 return true;
[email protected]adb2f3d12011-01-23 16:24:54630}
631
[email protected]6474b112012-05-04 19:35:27632// When SavePackage downloads MHTML to GData (see
633// SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it
634// does for non-SavePackage downloads, but SavePackage downloads never satisfy
635// IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls
636// DownloadItem::UpdateObservers() when the upload completes so that SavePackage
637// notices that the upload has completed and runs its normal Finish() pathway.
638// MaybeCompleteDownload() is never the mechanism by which SavePackage completes
639// downloads. SavePackage always uses its own Finish() to mark downloads
640// complete.
641
[email protected]5656f8a2011-11-17 16:12:58642void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) {
[email protected]adb2f3d12011-01-23 16:24:54643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
644 VLOG(20) << __FUNCTION__ << "()" << " download = "
645 << download->DebugString(false);
646
647 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32648 return;
[email protected]9ccbb372008-10-10 18:50:32649
[email protected]adb2f3d12011-01-23 16:24:54650 // TODO(rdsmith): DCHECK that we only pass through this point
651 // once per download. The natural way to do this is by a state
652 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56653
[email protected]adb2f3d12011-01-23 16:24:54654 // Confirm we're in the proper set of states to be here;
[email protected]550520f2012-05-14 20:58:48655 // have all data, have a history handle, (validated or safe).
656 DCHECK(download->IsInProgress());
[email protected]c09a8fd2011-11-21 19:54:50657 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
[email protected]c09a8fd2011-11-21 19:54:50658 DCHECK(download->AllDataSaved());
[email protected]5009b7a2012-02-21 18:47:03659 DCHECK(download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50660 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle()));
[email protected]adb2f3d12011-01-23 16:24:54661
[email protected]da4cd4262012-05-18 20:42:35662 // Give the delegate a chance to override. It's ok to keep re-setting the
663 // delegate's |complete_callback| cb as long as there isn't another call-point
664 // trying to set it to a different cb. TODO(benjhayden): Change the callback
665 // to point directly to the item instead of |this| when DownloadItem supports
666 // weak-ptrs.
[email protected]491aaa62012-06-07 03:50:18667 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind(
[email protected]da4cd4262012-05-18 20:42:35668 &DownloadManagerImpl::MaybeCompleteDownloadById,
669 this, download->GetId())))
[email protected]c2918652011-11-01 18:50:23670 return;
671
[email protected]adb2f3d12011-01-23 16:24:54672 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
673 << download->DebugString(false);
674
[email protected]491aaa62012-06-07 03:50:18675 if (delegate_)
676 delegate_->UpdateItemInPersistentStore(download);
[email protected]48837962011-04-19 17:03:29677 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32678}
679
[email protected]da4cd4262012-05-18 20:42:35680void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) {
681 DownloadItem* download_item = GetActiveDownload(download_id);
682 if (download_item != NULL)
683 MaybeCompleteDownload(download_item);
684}
685
[email protected]fc03de22011-12-06 23:28:12686void DownloadManagerImpl::DownloadCompleted(DownloadItem* download) {
[email protected]70850c72011-01-11 17:31:27687 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21688 DCHECK(download);
[email protected]491aaa62012-06-07 03:50:18689 if (delegate_)
690 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12691 active_downloads_.erase(download->GetId());
692 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27693}
694
[email protected]5656f8a2011-11-17 16:12:58695void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17696 DownloadItem* download = GetActiveDownload(download_id);
697 // A cancel at the right time could remove the download from the
698 // |active_downloads_| map before we get here.
699 if (!download)
700 return;
701
702 download->Cancel(true);
703}
704
[email protected]fc03de22011-12-06 23:28:12705void DownloadManagerImpl::DownloadCancelled(DownloadItem* download) {
[email protected]d8472d92011-08-26 20:15:20706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20707
708 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50709 << " download = " << download->DebugString(true);
710
[email protected]93af2272011-09-21 18:29:17711 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21712 // This function is called from the DownloadItem, so DI state
713 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12714 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29715
[email protected]d25fda12012-06-12 17:05:03716 DCHECK(file_manager_);
717 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29718}
719
[email protected]bf3b08a2012-03-08 01:52:34720void DownloadManagerImpl::OnDownloadInterrupted(
721 int32 download_id,
722 int64 size,
723 const std::string& hash_state,
724 content::DownloadInterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
726
727 DownloadItem* download = GetActiveDownload(download_id);
728 if (!download)
729 return;
730
[email protected]be76b7e2011-10-13 12:57:57731 VLOG(20) << __FUNCTION__ << "()"
732 << " reason " << InterruptReasonDebugString(reason)
[email protected]c09a8fd2011-11-21 19:54:50733 << " at offset " << download->GetReceivedBytes()
[email protected]47a881b2011-08-29 22:59:21734 << " size = " << size
735 << " download = " << download->DebugString(true);
736
[email protected]93af2272011-09-21 18:29:17737 RemoveFromActiveList(download);
[email protected]443853c62011-12-22 19:22:41738 download->Interrupted(size, hash_state, reason);
[email protected]93af2272011-09-21 18:29:17739 download->OffThreadCancel(file_manager_);
[email protected]47a881b2011-08-29 22:59:21740}
741
[email protected]5656f8a2011-11-17 16:12:58742DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) {
[email protected]bf68a00b2011-04-07 17:28:26743 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
744 DownloadMap::iterator it = active_downloads_.find(download_id);
[email protected]bf68a00b2011-04-07 17:28:26745 if (it == active_downloads_.end())
[email protected]47a881b2011-08-29 22:59:21746 return NULL;
[email protected]bf68a00b2011-04-07 17:28:26747
748 DownloadItem* download = it->second;
749
[email protected]47a881b2011-08-29 22:59:21750 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50751 DCHECK_EQ(download_id, download->GetId());
[email protected]4cd82f72011-05-23 19:15:01752
[email protected]47a881b2011-08-29 22:59:21753 return download;
754}
[email protected]54610672011-07-18 18:24:43755
[email protected]5656f8a2011-11-17 16:12:58756void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) {
[email protected]93af2272011-09-21 18:29:17757 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
758 DCHECK(download);
759
760 // Clean up will happen when the history system create callback runs if we
761 // don't have a valid db_handle yet.
[email protected]5009b7a2012-02-21 18:47:03762 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50763 active_downloads_.erase(download->GetId());
[email protected]491aaa62012-06-07 03:50:18764 if (delegate_)
765 delegate_->UpdateItemInPersistentStore(download);
[email protected]93af2272011-09-21 18:29:17766 }
767}
768
[email protected]fd3a82832012-01-19 20:35:12769bool DownloadManagerImpl::GenerateFileHash() {
[email protected]491aaa62012-06-07 03:50:18770 return delegate_ && delegate_->GenerateFileHash();
[email protected]fd3a82832012-01-19 20:35:12771}
772
[email protected]5656f8a2011-11-17 16:12:58773int DownloadManagerImpl::RemoveDownloadItems(
[email protected]6d0146c2011-08-04 19:13:04774 const DownloadVector& pending_deletes) {
775 if (pending_deletes.empty())
776 return 0;
777
778 // Delete from internal maps.
779 for (DownloadVector::const_iterator it = pending_deletes.begin();
780 it != pending_deletes.end();
781 ++it) {
782 DownloadItem* download = *it;
783 DCHECK(download);
[email protected]c09a8fd2011-11-21 19:54:50784 history_downloads_.erase(download->GetDbHandle());
785 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04786 downloads_.erase(download);
787 }
788
789 // Tell observers to refresh their views.
790 NotifyModelChanged();
791
792 // Delete the download items themselves.
793 const int num_deleted = static_cast<int>(pending_deletes.size());
794 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
795 return num_deleted;
796}
797
[email protected]fc03de22011-12-06 23:28:12798void DownloadManagerImpl::DownloadRemoved(DownloadItem* download) {
799 if (history_downloads_.find(download->GetDbHandle()) ==
800 history_downloads_.end())
[email protected]93af2272011-09-21 18:29:17801 return;
802
803 // Make history update.
[email protected]491aaa62012-06-07 03:50:18804 if (delegate_)
805 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29806
807 // Remove from our tables and delete.
[email protected]6d0146c2011-08-04 19:13:04808 int downloads_count = RemoveDownloadItems(DownloadVector(1, download));
[email protected]f04182f32010-12-10 19:12:07809 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29810}
811
[email protected]fd3a82832012-01-19 20:35:12812int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
813 base::Time remove_end) {
[email protected]491aaa62012-06-07 03:50:18814 if (delegate_)
815 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29816
[email protected]a312a442010-12-15 23:40:33817 // All downloads visible to the user will be in the history,
818 // so scan that map.
[email protected]6d0146c2011-08-04 19:13:04819 DownloadVector pending_deletes;
820 for (DownloadMap::const_iterator it = history_downloads_.begin();
821 it != history_downloads_.end();
822 ++it) {
initial.commit09911bf2008-07-26 23:55:29823 DownloadItem* download = it->second;
[email protected]c09a8fd2011-11-21 19:54:50824 if (download->GetStartTime() >= remove_begin &&
825 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04826 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12827 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28828 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29829 }
initial.commit09911bf2008-07-26 23:55:29830 }
[email protected]6d0146c2011-08-04 19:13:04831 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29832}
833
[email protected]fd3a82832012-01-19 20:35:12834int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59835 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29836}
837
[email protected]5656f8a2011-11-17 16:12:58838int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]da4a5582011-10-17 19:08:06839 download_stats::RecordClearAllSize(history_downloads_.size());
[email protected]d41355e6f2009-04-07 21:21:12840 // The null times make the date range unbounded.
841 return RemoveDownloadsBetween(base::Time(), base::Time());
842}
843
[email protected]0d4e30c2012-01-28 00:47:53844void DownloadManagerImpl::DownloadUrl(
[email protected]c5a5c0842012-05-04 20:05:14845 scoped_ptr<content::DownloadUrlParameters> params) {
846 if (params->post_id() >= 0) {
847 // Check this here so that the traceback is more useful.
848 DCHECK(params->prefer_cache());
849 DCHECK(params->method() == "POST");
850 }
851 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
852 &BeginDownload, base::Owned(params.release())));
initial.commit09911bf2008-07-26 23:55:29853}
854
[email protected]5656f8a2011-11-17 16:12:58855void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29856 observers_.AddObserver(observer);
[email protected]a1e41e72012-02-22 17:41:25857 // TODO: It is the responsibility of the observers to query the
858 // DownloadManager. Remove the following call from here and update all
859 // observers.
[email protected]75e51b52012-02-04 16:57:54860 observer->ModelChanged(this);
initial.commit09911bf2008-07-26 23:55:29861}
862
[email protected]5656f8a2011-11-17 16:12:58863void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29864 observers_.RemoveObserver(observer);
865}
866
[email protected]84d57412012-03-03 08:59:55867void DownloadManagerImpl::FileSelected(const FilePath& path,
868 int32 download_id) {
[email protected]4cd82f72011-05-23 19:15:01869 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3d833de2012-05-30 23:32:06870 DCHECK(!path.empty());
[email protected]4cd82f72011-05-23 19:15:01871
[email protected]4cd82f72011-05-23 19:15:01872 DownloadItem* download = GetActiveDownloadItem(download_id);
873 if (!download)
874 return;
875 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
[email protected]3d833de2012-05-30 23:32:06876 << " download = " << download->DebugString(true);
[email protected]4cd82f72011-05-23 19:15:01877
[email protected]3d833de2012-05-30 23:32:06878 // Retain the last directory. Exclude temporary downloads since the path
879 // likely points at the location of a temporary file.
880 if (!download->IsTemporary())
[email protected]7ae7c2cb2009-01-06 23:31:41881 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35882
[email protected]4cd82f72011-05-23 19:15:01883 // Make sure the initial file name is set only once.
[email protected]3d833de2012-05-30 23:32:06884 download->OnTargetPathSelected(path);
885 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29886}
887
[email protected]84d57412012-03-03 08:59:55888void DownloadManagerImpl::FileSelectionCanceled(int32 download_id) {
initial.commit09911bf2008-07-26 23:55:29889 // The user didn't pick a place to save the file, so need to cancel the
890 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01891 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01892
893 DownloadItem* download = GetActiveDownloadItem(download_id);
894 if (!download)
895 return;
896
897 VLOG(20) << __FUNCTION__ << "()"
898 << " download = " << download->DebugString(true);
899
[email protected]8f8bc112012-02-22 12:36:31900 download->Cancel(true);
[email protected]4cd82f72011-05-23 19:15:01901}
902
initial.commit09911bf2008-07-26 23:55:29903// Operations posted to us from the history service ----------------------------
904
905// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25906// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58907void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25908 std::vector<DownloadPersistentStoreInfo>* entries) {
initial.commit09911bf2008-07-26 23:55:29909 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]deb40832012-02-23 15:41:37910 int64 db_handle = entries->at(i).db_handle;
911 base::debug::Alias(&db_handle);
[email protected]0634626a2012-05-03 19:04:26912 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]deb40832012-02-23 15:41:37913
[email protected]ef17c9a2012-02-09 05:08:09914 net::BoundNetLog bound_net_log =
915 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03916 DownloadItem* download = factory_->CreatePersistedItem(
[email protected]ef17c9a2012-02-09 05:08:09917 this, GetNextId(), entries->at(i), bound_net_log);
[email protected]f04182f32010-12-10 19:12:07918 downloads_.insert(download);
[email protected]c09a8fd2011-11-21 19:54:50919 history_downloads_[download->GetDbHandle()] = download;
[email protected]da6e3922010-11-24 21:45:50920 VLOG(20) << __FUNCTION__ << "()" << i << ">"
921 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29922 }
[email protected]b0ab1d42010-02-24 19:29:28923 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48924 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29925}
926
[email protected]5656f8a2011-11-17 16:12:58927void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download,
928 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27929 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0634626a2012-05-03 19:04:26930 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]1e9fe7ff2011-06-24 17:37:33931
[email protected]da4a5582011-10-17 19:08:06932 download_stats::RecordHistorySize(history_downloads_.size());
933
[email protected]5009b7a2012-02-21 18:47:03934 DCHECK(!download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50935 download->SetDbHandle(db_handle);
[email protected]5009b7a2012-02-21 18:47:03936 download->SetIsPersisted();
[email protected]5bcd73eb2011-03-23 21:14:02937
[email protected]0634626a2012-05-03 19:04:26938 DCHECK(!ContainsKey(history_downloads_, download->GetDbHandle()));
[email protected]c09a8fd2011-11-21 19:54:50939 history_downloads_[download->GetDbHandle()] = download;
[email protected]6d0146c2011-08-04 19:13:04940
941 // Show in the appropriate browser UI.
942 // This includes buttons to save or cancel, for a dangerous download.
943 ShowDownloadInBrowser(download);
944
945 // Inform interested objects about the new download.
946 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19947}
948
[email protected]2588ea9d2011-08-22 20:59:53949
[email protected]5656f8a2011-11-17 16:12:58950void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
951 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53952 if (save_page_downloads_.count(download_id)) {
953 OnSavePageItemAddedToPersistentStore(download_id, db_handle);
954 } else if (active_downloads_.count(download_id)) {
955 OnDownloadItemAddedToPersistentStore(download_id, db_handle);
956 }
957 // It's valid that we don't find a matching item, i.e. on shutdown.
958}
959
[email protected]f9a45b02011-06-30 23:49:19960// Once the new DownloadItem's creation info has been committed to the history
961// service, we associate the DownloadItem with the db handle, update our
962// 'history_downloads_' map and inform observers.
[email protected]5656f8a2011-11-17 16:12:58963void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
964 int32 download_id, int64 db_handle) {
[email protected]f9a45b02011-06-30 23:49:19965 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]19420cc2011-07-18 17:43:45966 DownloadItem* download = GetActiveDownloadItem(download_id);
[email protected]93af2272011-09-21 18:29:17967 if (!download)
[email protected]19420cc2011-07-18 17:43:45968 return;
[email protected]54610672011-07-18 18:24:43969
970 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle
971 << " download_id = " << download_id
972 << " download = " << download->DebugString(true);
[email protected]f9a45b02011-06-30 23:49:19973
[email protected]e5107ce2011-09-19 20:36:13974 int32 matching_item_download_id
975 = (ContainsKey(history_downloads_, db_handle) ?
[email protected]c09a8fd2011-11-21 19:54:50976 history_downloads_[db_handle]->GetId() : 0);
[email protected]e5107ce2011-09-19 20:36:13977 base::debug::Alias(&matching_item_download_id);
978
[email protected]0634626a2012-05-03 19:04:26979 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:20980
[email protected]f9a45b02011-06-30 23:49:19981 AddDownloadItemToHistory(download, db_handle);
initial.commit09911bf2008-07-26 23:55:29982
[email protected]93af2272011-09-21 18:29:17983 // If the download is still in progress, try to complete it.
984 //
985 // Otherwise, download has been cancelled or interrupted before we've
986 // received the DB handle. We post one final message to the history
987 // service so that it can be properly in sync with the DownloadItem's
988 // completion status, and also inform any observers so that they get
989 // more than just the start notification.
990 if (download->IsInProgress()) {
991 MaybeCompleteDownload(download);
992 } else {
[email protected]0634626a2012-05-03 19:04:26993 DCHECK(download->IsCancelled());
[email protected]93af2272011-09-21 18:29:17994 active_downloads_.erase(download_id);
[email protected]491aaa62012-06-07 03:50:18995 if (delegate_)
996 delegate_->UpdateItemInPersistentStore(download);
[email protected]93af2272011-09-21 18:29:17997 download->UpdateObservers();
998 }
initial.commit09911bf2008-07-26 23:55:29999}
1000
[email protected]5656f8a2011-11-17 16:12:581001void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItem* download) {
[email protected]a29e4f22012-04-12 21:22:031002 // The 'contents' may no longer exist if the user closed the contents before
1003 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:381004 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:261005
1006 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:031007 // contents.
[email protected]491aaa62012-06-07 03:50:181008 if (!content && delegate_)
[email protected]ef9572e2012-01-04 22:14:121009 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:531010
[email protected]0bfbf882011-12-22 18:19:271011 if (content && content->GetDelegate())
1012 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:531013}
1014
[email protected]5656f8a2011-11-17 16:12:581015int DownloadManagerImpl::InProgressCount() const {
[email protected]550520f2012-05-14 20:58:481016 // Don't use active_downloads_.count() because Cancel() leaves items in
1017 // active_downloads_ if they haven't made it into the persistent store yet.
[email protected]007e7412012-03-13 20:10:561018 // Need to actually look at each item's state.
1019 int count = 0;
[email protected]550520f2012-05-14 20:58:481020 for (DownloadMap::const_iterator it = active_downloads_.begin();
1021 it != active_downloads_.end(); ++it) {
[email protected]007e7412012-03-13 20:10:561022 DownloadItem* item = it->second;
1023 if (item->IsInProgress())
1024 ++count;
1025 }
1026 return count;
[email protected]5656f8a2011-11-17 16:12:581027}
1028
[email protected]6cade212008-12-03 00:32:221029// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:581030void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411031 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381032}
[email protected]b0ab1d42010-02-24 19:29:281033
[email protected]5656f8a2011-11-17 16:12:581034void DownloadManagerImpl::NotifyModelChanged() {
[email protected]75e51b52012-02-04 16:57:541035 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this));
[email protected]b0ab1d42010-02-24 19:29:281036}
1037
[email protected]5656f8a2011-11-17 16:12:581038DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]4cd82f72011-05-23 19:15:011039 // The |history_downloads_| map is indexed by the download's db_handle,
1040 // not its id, so we have to iterate.
[email protected]f04182f32010-12-10 19:12:071041 for (DownloadMap::iterator it = history_downloads_.begin();
1042 it != history_downloads_.end(); ++it) {
[email protected]2e030682010-07-23 19:45:361043 DownloadItem* item = it->second;
[email protected]c09a8fd2011-11-21 19:54:501044 if (item->GetId() == download_id)
[email protected]2e030682010-07-23 19:45:361045 return item;
1046 }
1047 return NULL;
1048}
1049
[email protected]5656f8a2011-11-17 16:12:581050DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:361051 if (ContainsKey(active_downloads_, download_id))
1052 return active_downloads_[download_id];
1053 return NULL;
[email protected]4cd82f72011-05-23 19:15:011054}
1055
[email protected]57fd1252010-12-23 17:24:091056// Confirm that everything in all maps is also in |downloads_|, and that
1057// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581058void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071059#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091060 // Turn everything into sets.
[email protected]6d0146c2011-08-04 19:13:041061 const DownloadMap* input_maps[] = {&active_downloads_,
1062 &history_downloads_,
1063 &save_page_downloads_};
1064 DownloadSet active_set, history_set, save_page_set;
1065 DownloadSet* all_sets[] = {&active_set, &history_set, &save_page_set};
1066 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091067 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1068 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041069 it != input_maps[i]->end(); ++it) {
1070 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071071 }
1072 }
[email protected]57fd1252010-12-23 17:24:091073
1074 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091075 DownloadSet downloads_union;
1076 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1077 DownloadSet remainder;
1078 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1079 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
1080 downloads_.begin(), downloads_.end(),
1081 insert_it);
1082 DCHECK(remainder.empty());
1083 std::insert_iterator<DownloadSet>
1084 insert_union(downloads_union, downloads_union.end());
1085 std::set_union(downloads_union.begin(), downloads_union.end(),
1086 all_sets[i]->begin(), all_sets[i]->end(),
1087 insert_union);
1088 }
1089
1090 // Is everything in downloads_ present in one of the other sets?
1091 DownloadSet remainder;
1092 std::insert_iterator<DownloadSet>
1093 insert_remainder(remainder, remainder.begin());
1094 std::set_difference(downloads_.begin(), downloads_.end(),
1095 downloads_union.begin(), downloads_union.end(),
1096 insert_remainder);
1097 DCHECK(remainder.empty());
[email protected]f04182f32010-12-10 19:12:071098#endif
1099}
1100
[email protected]6d0146c2011-08-04 19:13:041101// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531102// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041103// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531104// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1105// that the history event is update regardless of the order in which these two
1106// events complete.
[email protected]6d0146c2011-08-04 19:13:041107// If something removes the download item from the download manager (Remove,
1108// Shutdown) the result will be that the SavePage system will not be able to
1109// properly update the download item (which no longer exists) or the download
1110// history, but the action will complete properly anyway. This may lead to the
1111// history entry being wrong on a reload of chrome (specifically in the case of
1112// Initiation -> History Callback -> Removal -> Completion), but there's no way
1113// to solve that without canceling on Remove (which would then update the DB).
1114
[email protected]5656f8a2011-11-17 16:12:581115void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
1116 int32 download_id, int64 db_handle) {
[email protected]6d0146c2011-08-04 19:13:041117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1118
1119 DownloadMap::const_iterator it = save_page_downloads_.find(download_id);
1120 // This can happen if the download manager is shutting down and all maps
1121 // have been cleared.
1122 if (it == save_page_downloads_.end())
1123 return;
1124
1125 DownloadItem* download = it->second;
1126 if (!download) {
1127 NOTREACHED();
1128 return;
1129 }
1130
[email protected]0634626a2012-05-03 19:04:261131 DCHECK(!ContainsKey(history_downloads_, db_handle));
[email protected]d8472d92011-08-26 20:15:201132
[email protected]6d0146c2011-08-04 19:13:041133 AddDownloadItemToHistory(download, db_handle);
1134
1135 // Finalize this download if it finished before the history callback.
1136 if (!download->IsInProgress())
1137 SavePageDownloadFinished(download);
1138}
1139
[email protected]5656f8a2011-11-17 16:12:581140void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) {
[email protected]5009b7a2012-02-21 18:47:031141 if (download->IsPersisted()) {
[email protected]491aaa62012-06-07 03:50:181142 if (delegate_)
1143 delegate_->UpdateItemInPersistentStore(download);
[email protected]c09a8fd2011-11-21 19:54:501144 DCHECK(ContainsKey(save_page_downloads_, download->GetId()));
1145 save_page_downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:041146
1147 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071148 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041149 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531150 content::Source<DownloadManager>(this),
1151 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041152 }
1153}
[email protected]da4a5582011-10-17 19:08:061154
[email protected]fc03de22011-12-06 23:28:121155void DownloadManagerImpl::DownloadOpened(DownloadItem* download) {
[email protected]491aaa62012-06-07 03:50:181156 if (delegate_)
1157 delegate_->UpdateItemInPersistentStore(download);
[email protected]da4a5582011-10-17 19:08:061158 int num_unopened = 0;
1159 for (DownloadMap::iterator it = history_downloads_.begin();
1160 it != history_downloads_.end(); ++it) {
[email protected]c09a8fd2011-11-21 19:54:501161 if (it->second->IsComplete() && !it->second->GetOpened())
[email protected]da4a5582011-10-17 19:08:061162 ++num_unopened;
1163 }
1164 download_stats::RecordOpensOutstanding(num_unopened);
1165}
[email protected]5656f8a2011-11-17 16:12:581166
[email protected]3d833de2012-05-30 23:32:061167void DownloadManagerImpl::DownloadRenamedToIntermediateName(
1168 DownloadItem* download) {
1169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1170 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1171 // receive the DownloadRenamedToIntermediateName() call.
[email protected]491aaa62012-06-07 03:50:181172 if (delegate_)
1173 delegate_->AddItemToPersistentStore(download);
[email protected]3d833de2012-05-30 23:32:061174}
1175
1176void DownloadManagerImpl::DownloadRenamedToFinalName(
1177 DownloadItem* download) {
1178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1179 // If the rename failed, we receive an OnDownloadInterrupted() call before we
1180 // receive the DownloadRenamedToFinalName() call.
[email protected]491aaa62012-06-07 03:50:181181 if (delegate_) {
1182 delegate_->UpdatePathForItemInPersistentStore(
1183 download, download->GetFullPath());
1184 }
[email protected]3d833de2012-05-30 23:32:061185}