blob: 08f736981573b04c9055c896d878c7cca74e0acc [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]f1675202012-07-09 15:18:0015#include "base/message_loop.h"
[email protected]7286e3fc2011-07-19 22:13:2416#include "base/stl_util.h"
[email protected]eda58402011-09-21 19:32:0217#include "base/stringprintf.h"
18#include "base/synchronization/lock.h"
19#include "base/sys_string_conversions.h"
[email protected]d2a8fb72010-01-21 05:31:4220#include "build/build_config.h"
[email protected]2bddd2072012-06-18 16:16:5121#include "content/browser/download/byte_stream.h"
[email protected]71bf3f5e2011-08-15 21:05:2222#include "content/browser/download/download_create_info.h"
23#include "content/browser/download/download_file_manager.h"
[email protected]c09a8fd2011-11-21 19:54:5024#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0625#include "content/browser/download/download_stats.h"
[email protected]b3c41c0b2012-03-06 15:48:3226#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]ea114722012-03-12 01:11:2527#include "content/browser/renderer_host/resource_dispatcher_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2928#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1129#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4930#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4431#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3432#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1733#include "content/public/browser/download_manager_delegate.h"
[email protected]8da82ea2012-03-11 22:16:5234#include "content/public/browser/download_persistent_store_info.h"
[email protected]c5a5c0842012-05-04 20:05:1435#include "content/public/browser/download_url_parameters.h"
[email protected]ad50def52011-10-19 23:17:0736#include "content/public/browser/notification_service.h"
[email protected]0d6e9bd2011-10-18 04:29:1637#include "content/public/browser/notification_types.h"
[email protected]f3b1a082011-11-18 00:34:3038#include "content/public/browser/render_process_host.h"
[email protected]94e2bbe2012-06-22 15:26:1339#include "content/public/browser/resource_context.h"
[email protected]0bfbf882011-12-22 18:19:2740#include "content/public/browser/web_contents_delegate.h"
[email protected]c5a5c0842012-05-04 20:05:1441#include "net/base/load_flags.h"
[email protected]27678b2a2012-02-04 22:09:1442#include "net/base/upload_data.h"
[email protected]f859eba2012-05-30 17:22:4943#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2944
[email protected]631bb742011-11-02 11:29:3945using content::BrowserThread;
[email protected]98e814062012-01-27 00:35:4246using content::DownloadId;
[email protected]e582fdd2011-12-20 16:48:1747using content::DownloadItem;
[email protected]8da82ea2012-03-11 22:16:5248using content::DownloadPersistentStoreInfo;
[email protected]ea114722012-03-12 01:11:2549using content::ResourceDispatcherHostImpl;
[email protected]2a6bc3e2011-12-28 23:51:3350using content::WebContents;
[email protected]631bb742011-11-02 11:29:3951
[email protected]a0ce3282011-08-19 20:49:5252namespace {
53
[email protected]99229b22012-07-11 17:38:5054// This is just used to remember which DownloadItems come from SavePage.
55class SavePageExternalData : public DownloadItem::ExternalData {
56 public:
57 // A spoonful of syntactic sugar.
58 static bool Get(DownloadItem* item) {
59 return item->GetExternalData(kKey) != NULL;
60 }
61
62 explicit SavePageExternalData(DownloadItem* item) {
63 item->SetExternalData(kKey, this);
64 }
65
66 virtual ~SavePageExternalData() {}
67
68 private:
69 static const char kKey[];
70
71 DISALLOW_COPY_AND_ASSIGN(SavePageExternalData);
72};
73
74const char SavePageExternalData::kKey[] = "DownloadItem SavePageExternalData";
75
[email protected]c5a5c0842012-05-04 20:05:1476void BeginDownload(content::DownloadUrlParameters* params) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
78 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
79 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
80 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]c5a5c0842012-05-04 20:05:1481 scoped_ptr<net::URLRequest> request(new net::URLRequest(
[email protected]94e2bbe2012-06-22 15:26:1382 params->url(),
83 NULL,
84 params->resource_context()->GetRequestContext()));
[email protected]f859eba2012-05-30 17:22:4985 request->set_referrer(params->referrer().url.spec());
86 webkit_glue::ConfigureURLRequestForReferrerPolicy(
87 request.get(), params->referrer().policy);
[email protected]c5a5c0842012-05-04 20:05:1488 request->set_load_flags(request->load_flags() | params->load_flags());
89 request->set_method(params->method());
90 if (!params->post_body().empty())
91 request->AppendBytesToUpload(params->post_body().data(),
92 params->post_body().size());
93 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1494 // The POST in this case does not have an actual body, and only works
95 // when retrieving data from cache. This is done because we don't want
96 // to do a re-POST without user consent, and currently don't have a good
97 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1498 DCHECK(params->prefer_cache());
99 DCHECK(params->method() == "POST");
[email protected]27678b2a2012-02-04 22:09:14100 scoped_refptr<net::UploadData> upload_data = new net::UploadData();
[email protected]c5a5c0842012-05-04 20:05:14101 upload_data->set_identifier(params->post_id());
[email protected]27678b2a2012-02-04 22:09:14102 request->set_upload(upload_data);
103 }
[email protected]c5a5c0842012-05-04 20:05:14104 for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter
105 = params->request_headers_begin();
106 iter != params->request_headers_end();
107 ++iter) {
108 request->SetExtraRequestHeaderByName(
109 iter->first, iter->second, false/*overwrite*/);
110 }
[email protected]56f0b082012-06-14 07:12:32111 params->resource_dispatcher_host()->BeginDownload(
[email protected]ea114722012-03-12 01:11:25112 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:06113 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:14114 params->resource_context(),
115 params->render_process_host_id(),
116 params->render_view_host_routing_id(),
117 params->prefer_cache(),
118 params->save_info(),
119 params->callback());
[email protected]a0ce3282011-08-19 20:49:52120}
121
[email protected]33d22102012-01-25 17:46:53122class MapValueIteratorAdapter {
123 public:
124 explicit MapValueIteratorAdapter(
125 base::hash_map<int64, DownloadItem*>::const_iterator iter)
126 : iter_(iter) {
127 }
128 ~MapValueIteratorAdapter() {}
129
130 DownloadItem* operator*() { return iter_->second; }
131
132 MapValueIteratorAdapter& operator++() {
133 ++iter_;
134 return *this;
135 }
136
137 bool operator!=(const MapValueIteratorAdapter& that) const {
138 return iter_ != that.iter_;
139 }
140
141 private:
142 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
143 // Allow copy and assign.
144};
145
[email protected]5948e1a2012-03-10 00:19:18146void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm,
147 bool* result) {
148 if (dfm->NumberOfActiveDownloads())
149 *result = false;
150 BrowserThread::PostTask(
151 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
152}
153
[email protected]0a5c9112012-03-12 17:49:02154void EnsureNoPendingDownloadJobsOnIO(bool* result) {
[email protected]5948e1a2012-03-10 00:19:18155 scoped_refptr<DownloadFileManager> download_file_manager =
[email protected]ea114722012-03-12 01:11:25156 ResourceDispatcherHostImpl::Get()->download_file_manager();
[email protected]5948e1a2012-03-10 00:19:18157 BrowserThread::PostTask(
158 BrowserThread::FILE, FROM_HERE,
159 base::Bind(&EnsureNoPendingDownloadsOnFile,
160 download_file_manager, result));
161}
162
[email protected]d25fda12012-06-12 17:05:03163class DownloadItemFactoryImpl : public content::DownloadItemFactory {
164 public:
165 DownloadItemFactoryImpl() {}
166 virtual ~DownloadItemFactoryImpl() {}
167
[email protected]db1d8f72012-07-13 19:23:16168 virtual DownloadItemImpl* CreatePersistedItem(
169 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03170 content::DownloadId download_id,
171 const content::DownloadPersistentStoreInfo& info,
172 const net::BoundNetLog& bound_net_log) OVERRIDE {
173 return new DownloadItemImpl(delegate, download_id, info, bound_net_log);
174 }
175
[email protected]db1d8f72012-07-13 19:23:16176 virtual DownloadItemImpl* CreateActiveItem(
177 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03178 const DownloadCreateInfo& info,
[email protected]94d841cf2012-06-12 20:58:20179 scoped_ptr<DownloadRequestHandleInterface> request_handle,
[email protected]d25fda12012-06-12 17:05:03180 bool is_otr,
181 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]94d841cf2012-06-12 20:58:20182 return new DownloadItemImpl(delegate, info, request_handle.Pass(),
183 is_otr, bound_net_log);
[email protected]d25fda12012-06-12 17:05:03184 }
185
[email protected]db1d8f72012-07-13 19:23:16186 virtual DownloadItemImpl* CreateSavePageItem(
187 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03188 const FilePath& path,
189 const GURL& url,
190 bool is_otr,
191 content::DownloadId download_id,
192 const std::string& mime_type,
193 const net::BoundNetLog& bound_net_log) OVERRIDE {
194 return new DownloadItemImpl(delegate, path, url, is_otr, download_id,
195 mime_type, bound_net_log);
196 }
197};
198
[email protected]a0ce3282011-08-19 20:49:52199} // namespace
200
[email protected]99907362012-01-11 05:41:40201namespace content {
202
[email protected]5948e1a2012-03-10 00:19:18203bool DownloadManager::EnsureNoPendingDownloadsForTesting() {
204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
205 bool result = true;
206 BrowserThread::PostTask(
207 BrowserThread::IO, FROM_HERE,
[email protected]0a5c9112012-03-12 17:49:02208 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result));
[email protected]5948e1a2012-03-10 00:19:18209 MessageLoop::current()->Run();
210 return result;
211}
212
[email protected]99907362012-01-11 05:41:40213} // namespace content
214
[email protected]d25fda12012-06-12 17:05:03215DownloadManagerImpl::DownloadManagerImpl(
216 DownloadFileManager* file_manager,
217 scoped_ptr<content::DownloadItemFactory> factory,
218 net::NetLog* net_log)
219 : factory_(factory.Pass()),
[email protected]09ea72c7422012-07-02 20:35:40220 history_size_(0),
[email protected]d25fda12012-06-12 17:05:03221 shutdown_needed_(false),
[email protected]b441a8492012-06-06 14:55:57222 browser_context_(NULL),
[email protected]d25fda12012-06-12 17:05:03223 file_manager_(file_manager),
[email protected]33c20cd92012-06-14 22:02:50224 delegate_(NULL),
[email protected]b441a8492012-06-06 14:55:57225 net_log_(net_log) {
[email protected]d25fda12012-06-12 17:05:03226 DCHECK(file_manager);
227 if (!factory_.get())
228 factory_.reset(new DownloadItemFactoryImpl());
initial.commit09911bf2008-07-26 23:55:29229}
230
[email protected]5656f8a2011-11-17 16:12:58231DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13232 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29233}
234
[email protected]5656f8a2011-11-17 16:12:58235DownloadId DownloadManagerImpl::GetNextId() {
[email protected]491aaa62012-06-07 03:50:18236 DownloadId id;
237 if (delegate_)
238 id = delegate_->GetNextId();
239 if (!id.IsValid()) {
240 static int next_id;
241 id = DownloadId(browser_context_, ++next_id);
242 }
243
244 return id;
[email protected]2909e342011-10-29 00:46:53245}
246
[email protected]db1d8f72012-07-13 19:23:16247bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) {
[email protected]491aaa62012-06-07 03:50:18248 if (!delegate_)
249 return true;
250
[email protected]fc03de22011-12-06 23:28:12251 return delegate_->ShouldOpenDownload(item);
252}
253
254bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18255 if (!delegate_)
256 return false;
257
[email protected]fc03de22011-12-06 23:28:12258 return delegate_->ShouldOpenFileBasedOnExtension(path);
259}
260
[email protected]b441a8492012-06-06 14:55:57261void DownloadManagerImpl::SetDelegate(
262 content::DownloadManagerDelegate* delegate) {
263 delegate_ = delegate;
264}
265
[email protected]b488b5a52012-06-06 17:01:28266content::DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
267 return delegate_;
268}
269
[email protected]5656f8a2011-11-17 16:12:58270void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50271 VLOG(20) << __FUNCTION__ << "()"
272 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13273 if (!shutdown_needed_)
274 return;
275 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29276
[email protected]75e51b52012-02-04 16:57:54277 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08278 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13279
[email protected]d25fda12012-06-12 17:05:03280 DCHECK(file_manager_);
281 BrowserThread::PostTask(
282 BrowserThread::FILE, FROM_HERE,
283 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown,
284 file_manager_, make_scoped_refptr(this)));
initial.commit09911bf2008-07-26 23:55:29285
[email protected]f04182f32010-12-10 19:12:07286 AssertContainersConsistent();
287
288 // Go through all downloads in downloads_. Dangerous ones we need to
289 // remove on disk, and in progress ones we need to cancel.
[email protected]ba7895d2012-06-22 19:02:52290 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]db1d8f72012-07-13 19:23:16291 DownloadItemImpl* download = it->second;
[email protected]f04182f32010-12-10 19:12:07292
293 // Save iterator from potential erases in this set done by called code.
294 // Iterators after an erasure point are still valid for lists and
295 // associative containers such as sets.
296 it++;
297
[email protected]c09a8fd2011-11-21 19:54:50298 if (download->GetSafetyState() == DownloadItem::DANGEROUS &&
[email protected]48837962011-04-19 17:03:29299 download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07300 // The user hasn't accepted it, so we need to remove it
301 // from the disk. This may or may not result in it being
302 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12303 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07304 // removes and deletes it if it's known to the history service)
305 // so the only thing we know after calling this function is that
306 // the download was deleted if-and-only-if it was removed
307 // from all queues.
[email protected]303077002011-04-19 23:21:01308 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26309 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17310 download->Cancel(false);
[email protected]491aaa62012-06-07 03:50:18311 if (delegate_)
312 delegate_->UpdateItemInPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29313 }
314 }
315
[email protected]f04182f32010-12-10 19:12:07316 // At this point, all dangerous downloads have had their files removed
317 // and all in progress downloads have been cancelled. We can now delete
318 // anything left.
[email protected]9ccbb372008-10-10 18:50:32319
[email protected]5cd11b6e2011-06-10 20:30:59320 // Copy downloads_ to separate container so as not to set off checks
321 // in DownloadItem destruction.
[email protected]ba7895d2012-06-22 19:02:52322 DownloadMap downloads_to_delete;
[email protected]5cd11b6e2011-06-10 20:30:59323 downloads_to_delete.swap(downloads_);
324
[email protected]70850c72011-01-11 17:31:27325 active_downloads_.clear();
[email protected]ba7895d2012-06-22 19:02:52326 STLDeleteValues(&downloads_to_delete);
initial.commit09911bf2008-07-26 23:55:29327
[email protected]41f558fb2012-01-09 22:59:58328 // We'll have nothing more to report to the observers after this point.
329 observers_.Clear();
330
initial.commit09911bf2008-07-26 23:55:29331 file_manager_ = NULL;
[email protected]b441a8492012-06-06 14:55:57332 if (delegate_)
333 delegate_->Shutdown();
initial.commit09911bf2008-07-26 23:55:29334}
335
[email protected]5656f8a2011-11-17 16:12:58336void DownloadManagerImpl::GetTemporaryDownloads(
[email protected]6d0146c2011-08-04 19:13:04337 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57338 DCHECK(result);
[email protected]6aa4a1c02010-01-15 18:49:58339
[email protected]09ea72c7422012-07-02 20:35:40340 for (DownloadMap::iterator it = downloads_.begin();
341 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16342 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:40343 // TODO(benjhayden): Don't check IsPersisted().
344 if (item->IsTemporary() &&
345 item->IsPersisted() &&
[email protected]3d833de2012-05-30 23:32:06346 (dir_path.empty() ||
[email protected]09ea72c7422012-07-02 20:35:40347 item->GetTargetFilePath().DirName() == dir_path))
348 result->push_back(item);
[email protected]6aa4a1c02010-01-15 18:49:58349 }
[email protected]6aa4a1c02010-01-15 18:49:58350}
351
[email protected]5656f8a2011-11-17 16:12:58352void DownloadManagerImpl::GetAllDownloads(
[email protected]6d0146c2011-08-04 19:13:04353 const FilePath& dir_path, DownloadVector* result) {
[email protected]82f37b02010-07-29 22:04:57354 DCHECK(result);
[email protected]8ddbd66a2010-05-21 16:38:34355
[email protected]09ea72c7422012-07-02 20:35:40356 for (DownloadMap::iterator it = downloads_.begin();
357 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16358 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:40359 // TODO(benjhayden): Don't check IsPersisted().
360 if (!item->IsTemporary() &&
361 item->IsPersisted() &&
[email protected]3d833de2012-05-30 23:32:06362 (dir_path.empty() ||
[email protected]09ea72c7422012-07-02 20:35:40363 item->GetTargetFilePath().DirName() == dir_path))
364 result->push_back(item);
[email protected]8ddbd66a2010-05-21 16:38:34365 }
[email protected]8ddbd66a2010-05-21 16:38:34366}
367
[email protected]5656f8a2011-11-17 16:12:58368void DownloadManagerImpl::SearchDownloads(const string16& query,
369 DownloadVector* result) {
[email protected]503d03872011-05-06 08:36:26370 string16 query_lower(base::i18n::ToLower(query));
[email protected]d3b12902010-08-16 23:39:42371
[email protected]09ea72c7422012-07-02 20:35:40372 for (DownloadMap::iterator it = downloads_.begin();
373 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16374 DownloadItemImpl* download_item = it->second;
[email protected]d3b12902010-08-16 23:39:42375 // Display Incognito downloads only in Incognito window, and vice versa.
376 // The Incognito Downloads page will get the list of non-Incognito downloads
377 // from its parent profile.
[email protected]09ea72c7422012-07-02 20:35:40378 // TODO(benjhayden): Don't check IsPersisted().
379 if (!download_item->IsTemporary() &&
380 download_item->IsPersisted() &&
381 (browser_context_->IsOffTheRecord() == download_item->IsOtr()) &&
382 download_item->MatchesQuery(query_lower)) {
[email protected]d3b12902010-08-16 23:39:42383 result->push_back(download_item);
[email protected]09ea72c7422012-07-02 20:35:40384 }
[email protected]d3b12902010-08-16 23:39:42385 }
[email protected]d3b12902010-08-16 23:39:42386}
387
[email protected]5656f8a2011-11-17 16:12:58388bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03389 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29390 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
391 shutdown_needed_ = true;
392
[email protected]6d0c9fb2011-08-22 19:26:03393 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46394
initial.commit09911bf2008-07-26 23:55:29395 return true;
396}
397
[email protected]aa9881c2011-08-15 18:01:12398// We have received a message from DownloadFileManager about a new download.
[email protected]2bddd2072012-06-18 16:16:51399content::DownloadId DownloadManagerImpl::StartDownload(
400 scoped_ptr<DownloadCreateInfo> info,
401 scoped_ptr<content::ByteStreamReader> stream) {
[email protected]ca4b5fa32010-10-09 12:42:18402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35403
[email protected]2bddd2072012-06-18 16:16:51404 // |bound_net_log| will be used for logging both the download item's and
405 // the download file's events.
406 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get());
407
408 // If info->download_id was unknown on entry to this function, it was
409 // assigned in CreateDownloadItem.
410 DownloadId download_id = info->download_id;
411
412 DownloadFileManager::CreateDownloadFileCallback callback(
413 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated,
414 this, download_id.local()));
415
416 BrowserThread::PostTask(
417 BrowserThread::FILE, FROM_HERE,
418 base::Bind(&DownloadFileManager::CreateDownloadFile,
419 file_manager_, base::Passed(info.Pass()),
420 base::Passed(stream.Pass()),
421 make_scoped_refptr(this),
422 GenerateFileHash(), bound_net_log,
423 callback));
424
425 return download_id;
426}
427
428void DownloadManagerImpl::OnDownloadFileCreated(
429 int32 download_id, content::DownloadInterruptReason reason) {
430 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) {
[email protected]b36cad502012-07-13 23:19:14431 OnDownloadInterrupted(download_id, reason);
[email protected]2bddd2072012-06-18 16:16:51432 // TODO(rdsmith): It makes no sense to continue along the
433 // regular download path after we've gotten an error. But it's
434 // the way the code has historically worked, and this allows us
435 // to get the download persisted and observers of the download manager
436 // notified, so tests work. When we execute all side effects of cancel
437 // (including queue removal) immedately rather than waiting for
438 // persistence we should replace this comment with a "return;".
439 }
440
[email protected]491aaa62012-06-07 03:50:18441 if (!delegate_ || delegate_->ShouldStartDownload(download_id))
[email protected]aa9881c2011-08-15 18:01:12442 RestartDownload(download_id);
[email protected]287b86b2011-02-26 00:11:35443}
444
[email protected]5656f8a2011-11-17 16:12:58445void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48446 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]09ea72c7422012-07-02 20:35:40447 for (DownloadMap::iterator it = downloads_.begin();
448 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16449 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:40450 if (item->IsPersisted())
451 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48452 }
453}
454
[email protected]db1d8f72012-07-13 19:23:16455void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
[email protected]9fc114672011-06-15 08:17:48456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
457 if (download_item->IsComplete() &&
[email protected]c09a8fd2011-11-21 19:54:50458 !download_item->GetFileExternallyRemoved()) {
[email protected]9fc114672011-06-15 08:17:48459 BrowserThread::PostTask(
460 BrowserThread::FILE, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58461 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread,
[email protected]09ea72c7422012-07-02 20:35:40462 this, download_item->GetId(),
[email protected]fabf36d22011-10-28 21:50:17463 download_item->GetTargetFilePath()));
[email protected]9fc114672011-06-15 08:17:48464 }
465}
466
[email protected]5656f8a2011-11-17 16:12:58467void DownloadManagerImpl::CheckForFileRemovalOnFileThread(
[email protected]09ea72c7422012-07-02 20:35:40468 int32 download_id, const FilePath& path) {
[email protected]9fc114672011-06-15 08:17:48469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
470 if (!file_util::PathExists(path)) {
471 BrowserThread::PostTask(
472 BrowserThread::UI, FROM_HERE,
[email protected]5656f8a2011-11-17 16:12:58473 base::Bind(&DownloadManagerImpl::OnFileRemovalDetected,
474 this,
[email protected]09ea72c7422012-07-02 20:35:40475 download_id));
[email protected]9fc114672011-06-15 08:17:48476 }
477}
478
[email protected]09ea72c7422012-07-02 20:35:40479void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) {
[email protected]9fc114672011-06-15 08:17:48480 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]db1d8f72012-07-13 19:23:16481 if (ContainsKey(downloads_, download_id))
482 downloads_[download_id]->OnDownloadedFileRemoved();
[email protected]9fc114672011-06-15 08:17:48483}
484
[email protected]443853c62011-12-22 19:22:41485void DownloadManagerImpl::RestartDownload(int32 download_id) {
[email protected]ca4b5fa32010-10-09 12:42:18486 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
initial.commit09911bf2008-07-26 23:55:29487
[email protected]db1d8f72012-07-13 19:23:16488 if (!ContainsKey(active_downloads_, download_id))
[email protected]4cd82f72011-05-23 19:15:01489 return;
[email protected]db1d8f72012-07-13 19:23:16490 DownloadItemImpl* download = active_downloads_[download_id];
[email protected]4cd82f72011-05-23 19:15:01491
492 VLOG(20) << __FUNCTION__ << "()"
493 << " download = " << download->DebugString(true);
494
[email protected]3d833de2012-05-30 23:32:06495 if (download->GetTargetDisposition() ==
496 DownloadItem::TARGET_DISPOSITION_PROMPT) {
initial.commit09911bf2008-07-26 23:55:29497 // We must ask the user for the place to put the download.
[email protected]491aaa62012-06-07 03:50:18498 if (delegate_) {
[email protected]31ee43f2012-06-08 23:05:36499 delegate_->ChooseDownloadPath(download);
[email protected]491aaa62012-06-07 03:50:18500 FOR_EACH_OBSERVER(Observer, observers_,
501 SelectFileDialogDisplayed(this, download_id));
502 } else {
503 FileSelectionCanceled(download_id);
504 }
initial.commit09911bf2008-07-26 23:55:29505 } else {
[email protected]3d833de2012-05-30 23:32:06506 // No prompting for download, just continue with the current target path.
507 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29508 }
509}
510
[email protected]37757c62011-12-20 20:07:12511content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58512 return browser_context_;
513}
514
515FilePath DownloadManagerImpl::LastDownloadPath() {
516 return last_download_path_;
517}
518
[email protected]ef17c9a2012-02-09 05:08:09519net::BoundNetLog DownloadManagerImpl::CreateDownloadItem(
[email protected]2bddd2072012-06-18 16:16:51520 DownloadCreateInfo* info) {
[email protected]c2e76012010-12-23 21:10:29521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
522
[email protected]ef17c9a2012-02-09 05:08:09523 net::BoundNetLog bound_net_log =
524 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]d25fda12012-06-12 17:05:03525 if (!info->download_id.IsValid())
526 info->download_id = GetNextId();
[email protected]db1d8f72012-07-13 19:23:16527 DownloadItemImpl* download = factory_->CreateActiveItem(
[email protected]2bddd2072012-06-18 16:16:51528 this, *info,
529 scoped_ptr<DownloadRequestHandleInterface>(
530 new DownloadRequestHandle(info->request_handle)).Pass(),
[email protected]ef17c9a2012-02-09 05:08:09531 browser_context_->IsOffTheRecord(), bound_net_log);
[email protected]d8472d92011-08-26 20:15:20532
[email protected]ba7895d2012-06-22 19:02:52533 DCHECK(!ContainsKey(downloads_, download->GetId()));
534 downloads_[download->GetId()] = download;
535 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
536 active_downloads_[download->GetId()] = download;
[email protected]6a1a59a12012-07-26 17:26:40537 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]ef17c9a2012-02-09 05:08:09538
539 return bound_net_log;
[email protected]c2e76012010-12-23 21:10:29540}
541
[email protected]db1d8f72012-07-13 19:23:16542DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]fc03de22011-12-06 23:28:12543 const FilePath& main_file_path,
544 const GURL& page_url,
545 bool is_otr,
[email protected]6474b112012-05-04 19:35:27546 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12547 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09548 net::BoundNetLog bound_net_log =
549 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]db1d8f72012-07-13 19:23:16550 DownloadItemImpl* download = factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27551 this,
552 main_file_path,
553 page_url,
554 is_otr,
555 GetNextId(),
556 mime_type,
557 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12558
559 download->AddObserver(observer);
560
[email protected]ba7895d2012-06-22 19:02:52561 DCHECK(!ContainsKey(downloads_, download->GetId()));
562 downloads_[download->GetId()] = download;
[email protected]99229b22012-07-11 17:38:50563 DCHECK(!SavePageExternalData::Get(download));
564 new SavePageExternalData(download);
565 DCHECK(SavePageExternalData::Get(download));
[email protected]fc03de22011-12-06 23:28:12566
[email protected]6a1a59a12012-07-26 17:26:40567 // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when
568 // we're comfortable with the user interacting with them.
569 // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
570
[email protected]fc03de22011-12-06 23:28:12571 // Will notify the observer in the callback.
[email protected]491aaa62012-06-07 03:50:18572 if (delegate_)
573 delegate_->AddItemToPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12574
575 return download;
576}
577
[email protected]3d833de2012-05-30 23:32:06578// The target path for the download item is now valid. We proceed with the
579// determination of an intermediate path.
[email protected]db1d8f72012-07-13 19:23:16580void DownloadManagerImpl::OnTargetPathAvailable(DownloadItemImpl* download) {
[email protected]ca4b5fa32010-10-09 12:42:18581 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01582 DCHECK(download);
[email protected]ba7895d2012-06-22 19:02:52583 DCHECK(ContainsKey(downloads_, download->GetId()));
584 DCHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]70850c72011-01-11 17:31:27585
[email protected]4cd82f72011-05-23 19:15:01586 VLOG(20) << __FUNCTION__ << "()"
587 << " download = " << download->DebugString(true);
588
[email protected]adb2f3d12011-01-23 16:24:54589 // Rename to intermediate name.
[email protected]3d833de2012-05-30 23:32:06590 // TODO(asanka): Skip this rename if download->AllDataSaved() is true. This
591 // avoids a spurious rename when we can just rename to the final
592 // filename. Unnecessary renames may cause bugs like
593 // http://crbug.com/74187.
[email protected]491aaa62012-06-07 03:50:18594 FilePath intermediate_path;
[email protected]11be6a682012-07-02 22:32:16595 if (delegate_)
596 intermediate_path = delegate_->GetIntermediatePath(*download);
597 else
[email protected]491aaa62012-06-07 03:50:18598 intermediate_path = download->GetTargetFilePath();
[email protected]11be6a682012-07-02 22:32:16599
[email protected]3d833de2012-05-30 23:32:06600 // We want the intermediate and target paths to refer to the same directory so
601 // that they are both on the same device and subject to same
602 // space/permission/availability constraints.
603 DCHECK(intermediate_path.DirName() ==
604 download->GetTargetFilePath().DirName());
[email protected]11be6a682012-07-02 22:32:16605 download->OnIntermediatePathDetermined(file_manager_, intermediate_path);
initial.commit09911bf2008-07-26 23:55:29606}
607
[email protected]443853c62011-12-22 19:22:41608void DownloadManagerImpl::UpdateDownload(int32 download_id,
609 int64 bytes_so_far,
610 int64 bytes_per_sec,
[email protected]0afff032012-01-06 20:55:00611 const std::string& hash_state) {
[email protected]70850c72011-01-11 17:31:27612 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
613 DownloadMap::iterator it = active_downloads_.find(download_id);
614 if (it != active_downloads_.end()) {
[email protected]db1d8f72012-07-13 19:23:16615 DownloadItemImpl* download = it->second;
[email protected]bf68a00b2011-04-07 17:28:26616 if (download->IsInProgress()) {
[email protected]443853c62011-12-22 19:22:41617 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state);
[email protected]491aaa62012-06-07 03:50:18618 if (delegate_)
619 delegate_->UpdateItemInPersistentStore(download);
[email protected]70850c72011-01-11 17:31:27620 }
initial.commit09911bf2008-07-26 23:55:29621 }
622}
623
[email protected]5656f8a2011-11-17 16:12:58624void DownloadManagerImpl::OnResponseCompleted(int32 download_id,
625 int64 size,
626 const std::string& hash) {
[email protected]33c6d3f12011-09-04 00:00:54627 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]da6e3922010-11-24 21:45:50628 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id
629 << " size = " << size;
[email protected]9d7ef802011-02-25 19:03:35630 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]9ccbb372008-10-10 18:50:32631
[email protected]c4f02c42011-01-24 21:55:06632 // If it's not in active_downloads_, that means it was cancelled; just
633 // ignore the notification.
634 if (active_downloads_.count(download_id) == 0)
635 return;
636
[email protected]db1d8f72012-07-13 19:23:16637 DownloadItemImpl* download = active_downloads_[download_id];
[email protected]ac4af82f2011-11-10 19:09:37638 download->OnAllDataSaved(size, hash);
[email protected]da4cd4262012-05-18 20:42:35639 MaybeCompleteDownload(download);
[email protected]adb2f3d12011-01-23 16:24:54640}
[email protected]9ccbb372008-10-10 18:50:32641
[email protected]db1d8f72012-07-13 19:23:16642void DownloadManagerImpl::AssertStateConsistent(
643 DownloadItemImpl* download) const {
[email protected]c09a8fd2011-11-21 19:54:50644 if (download->GetState() == DownloadItem::REMOVING) {
[email protected]ba7895d2012-06-22 19:02:52645 DCHECK(!ContainsKey(downloads_, download->GetId()));
[email protected]0634626a2012-05-03 19:04:26646 DCHECK(!ContainsKey(active_downloads_, download->GetId()));
[email protected]7d413112011-06-16 18:50:17647 return;
648 }
649
650 // Should be in downloads_ if we're not REMOVING.
[email protected]ba7895d2012-06-22 19:02:52651 CHECK(ContainsKey(downloads_, download->GetId()));
[email protected]7d413112011-06-16 18:50:17652
[email protected]c09a8fd2011-11-21 19:54:50653 int64 state = download->GetState();
[email protected]61b75a52011-08-29 16:34:34654 base::debug::Alias(&state);
[email protected]c09a8fd2011-11-21 19:54:50655 if (ContainsKey(active_downloads_, download->GetId())) {
[email protected]5009b7a2012-02-21 18:47:03656 if (download->IsPersisted())
[email protected]c09a8fd2011-11-21 19:54:50657 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState());
658 if (DownloadItem::IN_PROGRESS != download->GetState())
659 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle());
[email protected]f9a2997f2011-09-23 16:54:07660 }
[email protected]c09a8fd2011-11-21 19:54:50661 if (DownloadItem::IN_PROGRESS == download->GetState())
662 CHECK(ContainsKey(active_downloads_, download->GetId()));
[email protected]5cd11b6e2011-06-10 20:30:59663}
664
[email protected]db1d8f72012-07-13 19:23:16665bool DownloadManagerImpl::IsDownloadReadyForCompletion(
666 DownloadItemImpl* download) {
[email protected]adb2f3d12011-01-23 16:24:54667 // If we don't have all the data, the download is not ready for
668 // completion.
[email protected]c09a8fd2011-11-21 19:54:50669 if (!download->AllDataSaved())
[email protected]adb2f3d12011-01-23 16:24:54670 return false;
[email protected]6a7fb042010-02-01 16:30:47671
[email protected]9d7ef802011-02-25 19:03:35672 // If the download is dangerous, but not yet validated, it's not ready for
673 // completion.
[email protected]c09a8fd2011-11-21 19:54:50674 if (download->GetSafetyState() == DownloadItem::DANGEROUS)
[email protected]9d7ef802011-02-25 19:03:35675 return false;
676
[email protected]adb2f3d12011-01-23 16:24:54677 // If the download isn't active (e.g. has been cancelled) it's not
678 // ready for completion.
[email protected]c09a8fd2011-11-21 19:54:50679 if (active_downloads_.count(download->GetId()) == 0)
[email protected]adb2f3d12011-01-23 16:24:54680 return false;
681
682 // If the download hasn't been inserted into the history system
683 // (which occurs strictly after file name determination, intermediate
684 // file rename, and UI display) then it's not ready for completion.
[email protected]5009b7a2012-02-21 18:47:03685 if (!download->IsPersisted())
[email protected]7054b592011-06-22 14:46:42686 return false;
687
688 return true;
[email protected]adb2f3d12011-01-23 16:24:54689}
690
[email protected]6474b112012-05-04 19:35:27691// When SavePackage downloads MHTML to GData (see
692// SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it
693// does for non-SavePackage downloads, but SavePackage downloads never satisfy
694// IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls
695// DownloadItem::UpdateObservers() when the upload completes so that SavePackage
696// notices that the upload has completed and runs its normal Finish() pathway.
697// MaybeCompleteDownload() is never the mechanism by which SavePackage completes
698// downloads. SavePackage always uses its own Finish() to mark downloads
699// complete.
700
[email protected]db1d8f72012-07-13 19:23:16701void DownloadManagerImpl::MaybeCompleteDownload(
702 DownloadItemImpl* download) {
[email protected]adb2f3d12011-01-23 16:24:54703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
704 VLOG(20) << __FUNCTION__ << "()" << " download = "
705 << download->DebugString(false);
706
707 if (!IsDownloadReadyForCompletion(download))
[email protected]9ccbb372008-10-10 18:50:32708 return;
[email protected]9ccbb372008-10-10 18:50:32709
[email protected]adb2f3d12011-01-23 16:24:54710 // TODO(rdsmith): DCHECK that we only pass through this point
711 // once per download. The natural way to do this is by a state
712 // transition on the DownloadItem.
[email protected]594cd7d2010-07-21 03:23:56713
[email protected]adb2f3d12011-01-23 16:24:54714 // Confirm we're in the proper set of states to be here;
[email protected]550520f2012-05-14 20:58:48715 // have all data, have a history handle, (validated or safe).
716 DCHECK(download->IsInProgress());
[email protected]c09a8fd2011-11-21 19:54:50717 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState());
[email protected]c09a8fd2011-11-21 19:54:50718 DCHECK(download->AllDataSaved());
[email protected]5009b7a2012-02-21 18:47:03719 DCHECK(download->IsPersisted());
[email protected]adb2f3d12011-01-23 16:24:54720
[email protected]da4cd4262012-05-18 20:42:35721 // Give the delegate a chance to override. It's ok to keep re-setting the
722 // delegate's |complete_callback| cb as long as there isn't another call-point
723 // trying to set it to a different cb. TODO(benjhayden): Change the callback
724 // to point directly to the item instead of |this| when DownloadItem supports
725 // weak-ptrs.
[email protected]491aaa62012-06-07 03:50:18726 if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind(
[email protected]da4cd4262012-05-18 20:42:35727 &DownloadManagerImpl::MaybeCompleteDownloadById,
728 this, download->GetId())))
[email protected]c2918652011-11-01 18:50:23729 return;
730
[email protected]adb2f3d12011-01-23 16:24:54731 VLOG(20) << __FUNCTION__ << "()" << " executing: download = "
732 << download->DebugString(false);
733
[email protected]491aaa62012-06-07 03:50:18734 if (delegate_)
735 delegate_->UpdateItemInPersistentStore(download);
[email protected]48837962011-04-19 17:03:29736 download->OnDownloadCompleting(file_manager_);
[email protected]9ccbb372008-10-10 18:50:32737}
738
[email protected]da4cd4262012-05-18 20:42:35739void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) {
[email protected]db1d8f72012-07-13 19:23:16740 if (ContainsKey(active_downloads_, download_id))
741 MaybeCompleteDownload(active_downloads_[download_id]);
[email protected]da4cd4262012-05-18 20:42:35742}
743
[email protected]db1d8f72012-07-13 19:23:16744void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) {
[email protected]70850c72011-01-11 17:31:27745 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]cc3c7c092011-05-09 18:40:21746 DCHECK(download);
[email protected]491aaa62012-06-07 03:50:18747 if (delegate_)
748 delegate_->UpdateItemInPersistentStore(download);
[email protected]fc03de22011-12-06 23:28:12749 active_downloads_.erase(download->GetId());
750 AssertStateConsistent(download);
[email protected]70850c72011-01-11 17:31:27751}
752
[email protected]5656f8a2011-11-17 16:12:58753void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]93af2272011-09-21 18:29:17754 // A cancel at the right time could remove the download from the
755 // |active_downloads_| map before we get here.
[email protected]db1d8f72012-07-13 19:23:16756 if (ContainsKey(active_downloads_, download_id))
757 active_downloads_[download_id]->Cancel(true);
[email protected]93af2272011-09-21 18:29:17758}
759
[email protected]db1d8f72012-07-13 19:23:16760void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) {
[email protected]d8472d92011-08-26 20:15:20761 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d8472d92011-08-26 20:15:20762
763 VLOG(20) << __FUNCTION__ << "()"
[email protected]da6e3922010-11-24 21:45:50764 << " download = " << download->DebugString(true);
765
[email protected]93af2272011-09-21 18:29:17766 RemoveFromActiveList(download);
[email protected]47a881b2011-08-29 22:59:21767 // This function is called from the DownloadItem, so DI state
768 // should already have been updated.
[email protected]fc03de22011-12-06 23:28:12769 AssertStateConsistent(download);
initial.commit09911bf2008-07-26 23:55:29770
[email protected]d25fda12012-06-12 17:05:03771 DCHECK(file_manager_);
772 download->OffThreadCancel(file_manager_);
initial.commit09911bf2008-07-26 23:55:29773}
774
[email protected]bf3b08a2012-03-08 01:52:34775void DownloadManagerImpl::OnDownloadInterrupted(
776 int32 download_id,
[email protected]bf3b08a2012-03-08 01:52:34777 content::DownloadInterruptReason reason) {
[email protected]47a881b2011-08-29 22:59:21778 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
779
[email protected]db1d8f72012-07-13 19:23:16780 if (!ContainsKey(active_downloads_, download_id))
[email protected]47a881b2011-08-29 22:59:21781 return;
[email protected]b36cad502012-07-13 23:19:14782 active_downloads_[download_id]->Interrupt(reason);
[email protected]47a881b2011-08-29 22:59:21783}
784
[email protected]db1d8f72012-07-13 19:23:16785void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) {
[email protected]93af2272011-09-21 18:29:17786 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
787 DCHECK(download);
788
789 // Clean up will happen when the history system create callback runs if we
790 // don't have a valid db_handle yet.
[email protected]5009b7a2012-02-21 18:47:03791 if (download->IsPersisted()) {
[email protected]c09a8fd2011-11-21 19:54:50792 active_downloads_.erase(download->GetId());
[email protected]491aaa62012-06-07 03:50:18793 if (delegate_)
794 delegate_->UpdateItemInPersistentStore(download);
[email protected]93af2272011-09-21 18:29:17795 }
796}
797
[email protected]fd3a82832012-01-19 20:35:12798bool DownloadManagerImpl::GenerateFileHash() {
[email protected]491aaa62012-06-07 03:50:18799 return delegate_ && delegate_->GenerateFileHash();
[email protected]fd3a82832012-01-19 20:35:12800}
801
[email protected]5656f8a2011-11-17 16:12:58802int DownloadManagerImpl::RemoveDownloadItems(
[email protected]db1d8f72012-07-13 19:23:16803 const DownloadItemImplVector& pending_deletes) {
[email protected]6d0146c2011-08-04 19:13:04804 if (pending_deletes.empty())
805 return 0;
806
807 // Delete from internal maps.
[email protected]db1d8f72012-07-13 19:23:16808 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
[email protected]6d0146c2011-08-04 19:13:04809 it != pending_deletes.end();
810 ++it) {
[email protected]db1d8f72012-07-13 19:23:16811 DownloadItemImpl* download = *it;
[email protected]6d0146c2011-08-04 19:13:04812 DCHECK(download);
[email protected]ba7895d2012-06-22 19:02:52813 downloads_.erase(download->GetId());
[email protected]6d0146c2011-08-04 19:13:04814 }
815
816 // Tell observers to refresh their views.
817 NotifyModelChanged();
818
819 // Delete the download items themselves.
820 const int num_deleted = static_cast<int>(pending_deletes.size());
821 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end());
822 return num_deleted;
823}
824
[email protected]db1d8f72012-07-13 19:23:16825void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]09ea72c7422012-07-02 20:35:40826 if (!download ||
827 downloads_.find(download->GetId()) == downloads_.end())
828 return;
829
830 // TODO(benjhayden,rdsmith): Remove this.
831 if (!download->IsPersisted())
[email protected]93af2272011-09-21 18:29:17832 return;
833
834 // Make history update.
[email protected]491aaa62012-06-07 03:50:18835 if (delegate_)
836 delegate_->RemoveItemFromPersistentStore(download);
initial.commit09911bf2008-07-26 23:55:29837
838 // Remove from our tables and delete.
[email protected]db1d8f72012-07-13 19:23:16839 int downloads_count =
840 RemoveDownloadItems(DownloadItemImplVector(1, download));
[email protected]f04182f32010-12-10 19:12:07841 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29842}
843
[email protected]fd3a82832012-01-19 20:35:12844int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
845 base::Time remove_end) {
[email protected]491aaa62012-06-07 03:50:18846 if (delegate_)
847 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end);
initial.commit09911bf2008-07-26 23:55:29848
[email protected]a312a442010-12-15 23:40:33849 // All downloads visible to the user will be in the history,
850 // so scan that map.
[email protected]db1d8f72012-07-13 19:23:16851 DownloadItemImplVector pending_deletes;
[email protected]09ea72c7422012-07-02 20:35:40852 for (DownloadMap::const_iterator it = downloads_.begin();
853 it != downloads_.end();
[email protected]6d0146c2011-08-04 19:13:04854 ++it) {
[email protected]db1d8f72012-07-13 19:23:16855 DownloadItemImpl* download = it->second;
[email protected]09ea72c7422012-07-02 20:35:40856 if (download->IsPersisted() &&
857 download->GetStartTime() >= remove_begin &&
[email protected]c09a8fd2011-11-21 19:54:50858 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04859 (download->IsComplete() || download->IsCancelled())) {
[email protected]fc03de22011-12-06 23:28:12860 AssertStateConsistent(download);
[email protected]78b8fcc92009-03-31 17:36:28861 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29862 }
initial.commit09911bf2008-07-26 23:55:29863 }
[email protected]6d0146c2011-08-04 19:13:04864 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29865}
866
[email protected]fd3a82832012-01-19 20:35:12867int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59868 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29869}
870
[email protected]5656f8a2011-11-17 16:12:58871int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]d41355e6f2009-04-07 21:21:12872 // The null times make the date range unbounded.
[email protected]09ea72c7422012-07-02 20:35:40873 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
874 download_stats::RecordClearAllSize(num_deleted);
875 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12876}
877
[email protected]0d4e30c2012-01-28 00:47:53878void DownloadManagerImpl::DownloadUrl(
[email protected]c5a5c0842012-05-04 20:05:14879 scoped_ptr<content::DownloadUrlParameters> params) {
880 if (params->post_id() >= 0) {
881 // Check this here so that the traceback is more useful.
882 DCHECK(params->prefer_cache());
883 DCHECK(params->method() == "POST");
884 }
885 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
886 &BeginDownload, base::Owned(params.release())));
initial.commit09911bf2008-07-26 23:55:29887}
888
[email protected]5656f8a2011-11-17 16:12:58889void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29890 observers_.AddObserver(observer);
[email protected]a1e41e72012-02-22 17:41:25891 // TODO: It is the responsibility of the observers to query the
892 // DownloadManager. Remove the following call from here and update all
893 // observers.
[email protected]75e51b52012-02-04 16:57:54894 observer->ModelChanged(this);
initial.commit09911bf2008-07-26 23:55:29895}
896
[email protected]5656f8a2011-11-17 16:12:58897void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29898 observers_.RemoveObserver(observer);
899}
900
[email protected]84d57412012-03-03 08:59:55901void DownloadManagerImpl::FileSelected(const FilePath& path,
902 int32 download_id) {
[email protected]4cd82f72011-05-23 19:15:01903 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]3d833de2012-05-30 23:32:06904 DCHECK(!path.empty());
[email protected]4cd82f72011-05-23 19:15:01905
[email protected]db1d8f72012-07-13 19:23:16906 if (!ContainsKey(active_downloads_, download_id))
[email protected]4cd82f72011-05-23 19:15:01907 return;
[email protected]db1d8f72012-07-13 19:23:16908 DownloadItemImpl* download = active_downloads_[download_id];
909
[email protected]4cd82f72011-05-23 19:15:01910 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\""
[email protected]3d833de2012-05-30 23:32:06911 << " download = " << download->DebugString(true);
[email protected]4cd82f72011-05-23 19:15:01912
[email protected]3d833de2012-05-30 23:32:06913 // Retain the last directory. Exclude temporary downloads since the path
914 // likely points at the location of a temporary file.
915 if (!download->IsTemporary())
[email protected]7ae7c2cb2009-01-06 23:31:41916 last_download_path_ = path.DirName();
[email protected]287b86b2011-02-26 00:11:35917
[email protected]4cd82f72011-05-23 19:15:01918 // Make sure the initial file name is set only once.
[email protected]3d833de2012-05-30 23:32:06919 download->OnTargetPathSelected(path);
920 OnTargetPathAvailable(download);
initial.commit09911bf2008-07-26 23:55:29921}
922
[email protected]84d57412012-03-03 08:59:55923void DownloadManagerImpl::FileSelectionCanceled(int32 download_id) {
initial.commit09911bf2008-07-26 23:55:29924 // The user didn't pick a place to save the file, so need to cancel the
925 // download that's already in progress to the temporary location.
[email protected]4cd82f72011-05-23 19:15:01926 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4cd82f72011-05-23 19:15:01927
[email protected]db1d8f72012-07-13 19:23:16928 if (!ContainsKey(active_downloads_, download_id))
[email protected]4cd82f72011-05-23 19:15:01929 return;
[email protected]db1d8f72012-07-13 19:23:16930 DownloadItemImpl* download = active_downloads_[download_id];
[email protected]4cd82f72011-05-23 19:15:01931
932 VLOG(20) << __FUNCTION__ << "()"
933 << " download = " << download->DebugString(true);
934
[email protected]8f8bc112012-02-22 12:36:31935 download->Cancel(true);
[email protected]4cd82f72011-05-23 19:15:01936}
937
initial.commit09911bf2008-07-26 23:55:29938// Operations posted to us from the history service ----------------------------
939
940// The history service has retrieved all download entries. 'entries' contains
[email protected]bb1a4212011-08-22 22:38:25941// 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time).
[email protected]5656f8a2011-11-17 16:12:58942void DownloadManagerImpl::OnPersistentStoreQueryComplete(
[email protected]bb1a4212011-08-22 22:38:25943 std::vector<DownloadPersistentStoreInfo>* entries) {
[email protected]09ea72c7422012-07-02 20:35:40944 history_size_ = entries->size();
initial.commit09911bf2008-07-26 23:55:29945 for (size_t i = 0; i < entries->size(); ++i) {
[email protected]deb40832012-02-23 15:41:37946 int64 db_handle = entries->at(i).db_handle;
947 base::debug::Alias(&db_handle);
[email protected]deb40832012-02-23 15:41:37948
[email protected]ef17c9a2012-02-09 05:08:09949 net::BoundNetLog bound_net_log =
950 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]db1d8f72012-07-13 19:23:16951 DownloadItemImpl* download = factory_->CreatePersistedItem(
[email protected]ef17c9a2012-02-09 05:08:09952 this, GetNextId(), entries->at(i), bound_net_log);
[email protected]ba7895d2012-06-22 19:02:52953 DCHECK(!ContainsKey(downloads_, download->GetId()));
954 downloads_[download->GetId()] = download;
[email protected]6a1a59a12012-07-26 17:26:40955 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]da6e3922010-11-24 21:45:50956 VLOG(20) << __FUNCTION__ << "()" << i << ">"
957 << " download = " << download->DebugString(true);
initial.commit09911bf2008-07-26 23:55:29958 }
[email protected]b0ab1d42010-02-24 19:29:28959 NotifyModelChanged();
[email protected]9fc114672011-06-15 08:17:48960 CheckForHistoryFilesRemoval();
initial.commit09911bf2008-07-26 23:55:29961}
962
[email protected]db1d8f72012-07-13 19:23:16963void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download,
[email protected]5656f8a2011-11-17 16:12:58964 int64 db_handle) {
[email protected]70850c72011-01-11 17:31:27965 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]0634626a2012-05-03 19:04:26966 DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle);
[email protected]5009b7a2012-02-21 18:47:03967 DCHECK(!download->IsPersisted());
[email protected]c09a8fd2011-11-21 19:54:50968 download->SetDbHandle(db_handle);
[email protected]5009b7a2012-02-21 18:47:03969 download->SetIsPersisted();
[email protected]5bcd73eb2011-03-23 21:14:02970
[email protected]09ea72c7422012-07-02 20:35:40971 download_stats::RecordHistorySize(history_size_);
972 // Not counting |download|.
973 ++history_size_;
[email protected]6d0146c2011-08-04 19:13:04974
975 // Show in the appropriate browser UI.
976 // This includes buttons to save or cancel, for a dangerous download.
977 ShowDownloadInBrowser(download);
978
979 // Inform interested objects about the new download.
980 NotifyModelChanged();
[email protected]f9a45b02011-06-30 23:49:19981}
982
[email protected]2588ea9d2011-08-22 20:59:53983
[email protected]5656f8a2011-11-17 16:12:58984void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id,
985 int64 db_handle) {
[email protected]2588ea9d2011-08-22 20:59:53986 // It's valid that we don't find a matching item, i.e. on shutdown.
[email protected]db1d8f72012-07-13 19:23:16987 if (!ContainsKey(downloads_, download_id))
[email protected]99229b22012-07-11 17:38:50988 return;
[email protected]db1d8f72012-07-13 19:23:16989
990 DownloadItemImpl* item = downloads_[download_id];
[email protected]99229b22012-07-11 17:38:50991 AddDownloadItemToHistory(item, db_handle);
992 if (SavePageExternalData::Get(item)) {
993 OnSavePageItemAddedToPersistentStore(item);
994 } else {
995 OnDownloadItemAddedToPersistentStore(item);
996 }
[email protected]2588ea9d2011-08-22 20:59:53997}
998
[email protected]09ea72c7422012-07-02 20:35:40999// Once the new DownloadItem has been committed to the persistent store,
1000// associate it with its db_handle (TODO(benjhayden) merge db_handle with id),
1001// show it in the browser (TODO(benjhayden) the ui should observe us instead),
1002// and notify observers (TODO(benjhayden) observers should be able to see the
1003// item when it's created so they can observe it directly. Are there any
1004// clients that actually need to know when the item is added to the history?).
[email protected]5656f8a2011-11-17 16:12:581005void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore(
[email protected]db1d8f72012-07-13 19:23:161006 DownloadItemImpl* item) {
[email protected]f9a45b02011-06-30 23:49:191007 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]99229b22012-07-11 17:38:501008 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle()
1009 << " download_id = " << item->GetId()
1010 << " download = " << item->DebugString(true);
initial.commit09911bf2008-07-26 23:55:291011
[email protected]93af2272011-09-21 18:29:171012 // If the download is still in progress, try to complete it.
1013 //
1014 // Otherwise, download has been cancelled or interrupted before we've
1015 // received the DB handle. We post one final message to the history
1016 // service so that it can be properly in sync with the DownloadItem's
1017 // completion status, and also inform any observers so that they get
1018 // more than just the start notification.
[email protected]99229b22012-07-11 17:38:501019 if (item->IsInProgress()) {
1020 MaybeCompleteDownload(item);
[email protected]93af2272011-09-21 18:29:171021 } else {
[email protected]99229b22012-07-11 17:38:501022 DCHECK(item->IsCancelled());
1023 active_downloads_.erase(item->GetId());
[email protected]491aaa62012-06-07 03:50:181024 if (delegate_)
[email protected]99229b22012-07-11 17:38:501025 delegate_->UpdateItemInPersistentStore(item);
1026 item->UpdateObservers();
[email protected]93af2272011-09-21 18:29:171027 }
initial.commit09911bf2008-07-26 23:55:291028}
1029
[email protected]db1d8f72012-07-13 19:23:161030void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) {
[email protected]a29e4f22012-04-12 21:22:031031 // The 'contents' may no longer exist if the user closed the contents before
1032 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:381033 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:261034
1035 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:031036 // contents.
[email protected]491aaa62012-06-07 03:50:181037 if (!content && delegate_)
[email protected]ef9572e2012-01-04 22:14:121038 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:531039
[email protected]0bfbf882011-12-22 18:19:271040 if (content && content->GetDelegate())
1041 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:531042}
1043
[email protected]5656f8a2011-11-17 16:12:581044int DownloadManagerImpl::InProgressCount() const {
[email protected]550520f2012-05-14 20:58:481045 // Don't use active_downloads_.count() because Cancel() leaves items in
1046 // active_downloads_ if they haven't made it into the persistent store yet.
[email protected]007e7412012-03-13 20:10:561047 // Need to actually look at each item's state.
1048 int count = 0;
[email protected]550520f2012-05-14 20:58:481049 for (DownloadMap::const_iterator it = active_downloads_.begin();
1050 it != active_downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:161051 DownloadItemImpl* item = it->second;
[email protected]007e7412012-03-13 20:10:561052 if (item->IsInProgress())
1053 ++count;
1054 }
1055 return count;
[email protected]5656f8a2011-11-17 16:12:581056}
1057
[email protected]6cade212008-12-03 00:32:221058// Clears the last download path, used to initialize "save as" dialogs.
[email protected]5656f8a2011-11-17 16:12:581059void DownloadManagerImpl::ClearLastDownloadPath() {
[email protected]7ae7c2cb2009-01-06 23:31:411060 last_download_path_ = FilePath();
[email protected]eea46622009-07-15 20:49:381061}
[email protected]b0ab1d42010-02-24 19:29:281062
[email protected]5656f8a2011-11-17 16:12:581063void DownloadManagerImpl::NotifyModelChanged() {
[email protected]75e51b52012-02-04 16:57:541064 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this));
[email protected]b0ab1d42010-02-24 19:29:281065}
1066
[email protected]5656f8a2011-11-17 16:12:581067DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) {
[email protected]09ea72c7422012-07-02 20:35:401068 DownloadItem* download = GetDownload(download_id);
1069 return (download && download->IsPersisted()) ? download : NULL;
[email protected]2e030682010-07-23 19:45:361070}
1071
[email protected]ba7895d2012-06-22 19:02:521072DownloadItem* DownloadManagerImpl::GetDownload(int download_id) {
1073 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
1074}
1075
[email protected]5656f8a2011-11-17 16:12:581076DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) {
[email protected]5d3e83642011-12-16 01:14:361077 if (ContainsKey(active_downloads_, download_id))
1078 return active_downloads_[download_id];
1079 return NULL;
[email protected]4cd82f72011-05-23 19:15:011080}
1081
[email protected]57fd1252010-12-23 17:24:091082// Confirm that everything in all maps is also in |downloads_|, and that
1083// everything in |downloads_| is also in some other map.
[email protected]5656f8a2011-11-17 16:12:581084void DownloadManagerImpl::AssertContainersConsistent() const {
[email protected]f04182f32010-12-10 19:12:071085#if !defined(NDEBUG)
[email protected]57fd1252010-12-23 17:24:091086 // Turn everything into sets.
[email protected]99229b22012-07-11 17:38:501087 const DownloadMap* input_maps[] = {&active_downloads_};
1088 DownloadSet active_set;
1089 DownloadSet* all_sets[] = {&active_set};
[email protected]6d0146c2011-08-04 19:13:041090 DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets));
[email protected]57fd1252010-12-23 17:24:091091 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) {
1092 for (DownloadMap::const_iterator it = input_maps[i]->begin();
[email protected]6d0146c2011-08-04 19:13:041093 it != input_maps[i]->end(); ++it) {
1094 all_sets[i]->insert(&*it->second);
[email protected]f04182f32010-12-10 19:12:071095 }
1096 }
[email protected]57fd1252010-12-23 17:24:091097
[email protected]ba7895d2012-06-22 19:02:521098 DownloadSet all_downloads;
1099 for (DownloadMap::const_iterator it = downloads_.begin();
1100 it != downloads_.end(); ++it) {
1101 all_downloads.insert(it->second);
1102 }
1103
[email protected]57fd1252010-12-23 17:24:091104 // Check if each set is fully present in downloads, and create a union.
[email protected]57fd1252010-12-23 17:24:091105 for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) {
1106 DownloadSet remainder;
1107 std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin());
1108 std::set_difference(all_sets[i]->begin(), all_sets[i]->end(),
[email protected]ba7895d2012-06-22 19:02:521109 all_downloads.begin(), all_downloads.end(),
[email protected]57fd1252010-12-23 17:24:091110 insert_it);
1111 DCHECK(remainder.empty());
[email protected]57fd1252010-12-23 17:24:091112 }
[email protected]f04182f32010-12-10 19:12:071113#endif
1114}
1115
[email protected]6d0146c2011-08-04 19:13:041116// SavePackage will call SavePageDownloadFinished upon completion/cancellation.
[email protected]2588ea9d2011-08-22 20:59:531117// The history callback will call OnSavePageItemAddedToPersistentStore.
[email protected]6d0146c2011-08-04 19:13:041118// If the download finishes before the history callback,
[email protected]2588ea9d2011-08-22 20:59:531119// OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring
1120// that the history event is update regardless of the order in which these two
1121// events complete.
[email protected]6d0146c2011-08-04 19:13:041122// If something removes the download item from the download manager (Remove,
1123// Shutdown) the result will be that the SavePage system will not be able to
1124// properly update the download item (which no longer exists) or the download
1125// history, but the action will complete properly anyway. This may lead to the
1126// history entry being wrong on a reload of chrome (specifically in the case of
1127// Initiation -> History Callback -> Removal -> Completion), but there's no way
1128// to solve that without canceling on Remove (which would then update the DB).
1129
[email protected]5656f8a2011-11-17 16:12:581130void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore(
[email protected]db1d8f72012-07-13 19:23:161131 DownloadItemImpl* item) {
[email protected]6d0146c2011-08-04 19:13:041132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1133
[email protected]6d0146c2011-08-04 19:13:041134 // Finalize this download if it finished before the history callback.
[email protected]99229b22012-07-11 17:38:501135 if (!item->IsInProgress())
1136 SavePageDownloadFinished(item);
[email protected]6d0146c2011-08-04 19:13:041137}
1138
[email protected]db1d8f72012-07-13 19:23:161139void DownloadManagerImpl::SavePageDownloadFinished(
1140 content::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]6d0146c2011-08-04 19:13:041144 if (download->IsComplete())
[email protected]ad50def52011-10-19 23:17:071145 content::NotificationService::current()->Notify(
[email protected]6d0146c2011-08-04 19:13:041146 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
[email protected]6c2381d2011-10-19 02:52:531147 content::Source<DownloadManager>(this),
1148 content::Details<DownloadItem>(download));
[email protected]6d0146c2011-08-04 19:13:041149 }
1150}
[email protected]da4a5582011-10-17 19:08:061151
[email protected]db1d8f72012-07-13 19:23:161152void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) {
[email protected]491aaa62012-06-07 03:50:181153 if (delegate_)
1154 delegate_->UpdateItemInPersistentStore(download);
[email protected]da4a5582011-10-17 19:08:061155 int num_unopened = 0;
[email protected]09ea72c7422012-07-02 20:35:401156 for (DownloadMap::iterator it = downloads_.begin();
1157 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:161158 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:401159 if (item->IsComplete() &&
1160 !item->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(
[email protected]db1d8f72012-07-13 19:23:161167 DownloadItemImpl* download) {
[email protected]3d833de2012-05-30 23:32:061168 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(
[email protected]db1d8f72012-07-13 19:23:161176 DownloadItemImpl* download) {
[email protected]3d833de2012-05-30 23:32:061177 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}