blob: f8dc40e3b1a1d85d688ab80126ef93407501cc19 [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"
[email protected]503d03872011-05-06 08:36:2612#include "base/i18n/case_conversion.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/logging.h"
[email protected]f1675202012-07-09 15:18:0014#include "base/message_loop.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"
[email protected]d59d40c2012-08-08 18:18:3717#include "base/supports_user_data.h"
[email protected]eda58402011-09-21 19:32:0218#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"
[email protected]53ac00e82012-10-18 20:59:2023#include "content/browser/download/download_file_factory.h"
24#include "content/browser/download/download_item_factory.h"
[email protected]c09a8fd2011-11-21 19:54:5025#include "content/browser/download/download_item_impl.h"
[email protected]da4a5582011-10-17 19:08:0626#include "content/browser/download/download_stats.h"
[email protected]678c0362012-12-05 08:02:4427#include "content/browser/loader/resource_dispatcher_host_impl.h"
[email protected]b3c41c0b2012-03-06 15:48:3228#include "content/browser/renderer_host/render_view_host_impl.h"
[email protected]93ddb3c2012-04-11 21:44:2929#include "content/browser/web_contents/web_contents_impl.h"
[email protected]ccb797302011-12-15 16:55:1130#include "content/public/browser/browser_context.h"
[email protected]c38831a12011-10-28 12:44:4931#include "content/public/browser/browser_thread.h"
[email protected]87f3c082011-10-19 18:07:4432#include "content/public/browser/content_browser_client.h"
[email protected]bf3b08a2012-03-08 01:52:3433#include "content/public/browser/download_interrupt_reasons.h"
[email protected]1bd0ef12011-10-20 05:24:1734#include "content/public/browser/download_manager_delegate.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]8d68a3e02013-01-12 15:57:1041#include "content/public/common/referrer.h"
[email protected]c5a5c0842012-05-04 20:05:1442#include "net/base/load_flags.h"
[email protected]f288ef02012-12-15 20:28:2843#include "net/base/upload_bytes_element_reader.h"
44#include "net/base/upload_data_stream.h"
[email protected]eb795632012-09-01 00:19:3145#include "net/url_request/url_request_context.h"
[email protected]f859eba2012-05-30 17:22:4946#include "webkit/glue/webkit_glue.h"
initial.commit09911bf2008-07-26 23:55:2947
[email protected]35869622012-10-26 23:23:5548namespace content {
[email protected]a0ce3282011-08-19 20:49:5249namespace {
50
[email protected]8d68a3e02013-01-12 15:57:1051void BeginDownload(scoped_ptr<DownloadUrlParameters> params,
52 DownloadId download_id) {
[email protected]c5a5c0842012-05-04 20:05:1453 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
54 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and
55 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so
56 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4.
[email protected]eb795632012-09-01 00:19:3157 scoped_ptr<net::URLRequest> request(
58 params->resource_context()->GetRequestContext()->CreateRequest(
59 params->url(), NULL));
[email protected]f859eba2012-05-30 17:22:4960 request->set_referrer(params->referrer().url.spec());
61 webkit_glue::ConfigureURLRequestForReferrerPolicy(
62 request.get(), params->referrer().policy);
[email protected]c5a5c0842012-05-04 20:05:1463 request->set_load_flags(request->load_flags() | params->load_flags());
64 request->set_method(params->method());
[email protected]fdae8bf2012-11-20 04:02:4565 if (!params->post_body().empty()) {
[email protected]f288ef02012-12-15 20:28:2866 const std::string& body = params->post_body();
67 scoped_ptr<net::UploadElementReader> reader(
68 net::UploadOwnedBytesElementReader::CreateWithString(body));
69 request->set_upload(make_scoped_ptr(
70 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)));
[email protected]fdae8bf2012-11-20 04:02:4571 }
[email protected]c5a5c0842012-05-04 20:05:1472 if (params->post_id() >= 0) {
[email protected]27678b2a2012-02-04 22:09:1473 // The POST in this case does not have an actual body, and only works
74 // when retrieving data from cache. This is done because we don't want
75 // to do a re-POST without user consent, and currently don't have a good
76 // plan on how to display the UI for that.
[email protected]c5a5c0842012-05-04 20:05:1477 DCHECK(params->prefer_cache());
78 DCHECK(params->method() == "POST");
[email protected]f288ef02012-12-15 20:28:2879 ScopedVector<net::UploadElementReader> element_readers;
80 request->set_upload(make_scoped_ptr(
81 new net::UploadDataStream(&element_readers, params->post_id())));
[email protected]27678b2a2012-02-04 22:09:1482 }
[email protected]8d68a3e02013-01-12 15:57:1083
84 // If we're not at the beginning of the file, retrieve only the remaining
85 // portion.
86 if (params->offset() > 0) {
87 request->SetExtraRequestHeaderByName(
88 "Range",
89 StringPrintf("bytes=%" PRId64 "-", params->offset()),
90 true);
91
92 // If we've asked for a range, we want to make sure that we only
93 // get that range if our current copy of the information is good.
94 if (!params->last_modified().empty()) {
95 request->SetExtraRequestHeaderByName("If-Unmodified-Since",
96 params->last_modified(),
97 true);
98 }
99 if (!params->etag().empty()) {
100 request->SetExtraRequestHeaderByName("If-Match", params->etag(), true);
101 }
102 }
103
[email protected]35869622012-10-26 23:23:55104 for (DownloadUrlParameters::RequestHeadersType::const_iterator iter
[email protected]c5a5c0842012-05-04 20:05:14105 = 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]49d812e2012-11-06 21:18:12111
112 scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo());
113 save_info->file_path = params->file_path();
114 save_info->suggested_name = params->suggested_name();
115 save_info->offset = params->offset();
116 save_info->hash_state = params->hash_state();
117 save_info->prompt_for_save_location = params->prompt();
118 save_info->file_stream = params->GetFileStream();
119
[email protected]56f0b082012-06-14 07:12:32120 params->resource_dispatcher_host()->BeginDownload(
[email protected]ea114722012-03-12 01:11:25121 request.Pass(),
[email protected]a53e2f92012-05-15 15:27:06122 params->content_initiated(),
[email protected]c5a5c0842012-05-04 20:05:14123 params->resource_context(),
124 params->render_process_host_id(),
125 params->render_view_host_routing_id(),
126 params->prefer_cache(),
[email protected]49d812e2012-11-06 21:18:12127 save_info.Pass(),
[email protected]8d68a3e02013-01-12 15:57:10128 download_id,
[email protected]c5a5c0842012-05-04 20:05:14129 params->callback());
[email protected]a0ce3282011-08-19 20:49:52130}
131
[email protected]33d22102012-01-25 17:46:53132class MapValueIteratorAdapter {
133 public:
134 explicit MapValueIteratorAdapter(
135 base::hash_map<int64, DownloadItem*>::const_iterator iter)
136 : iter_(iter) {
137 }
138 ~MapValueIteratorAdapter() {}
139
140 DownloadItem* operator*() { return iter_->second; }
141
142 MapValueIteratorAdapter& operator++() {
143 ++iter_;
144 return *this;
145 }
146
147 bool operator!=(const MapValueIteratorAdapter& that) const {
148 return iter_ != that.iter_;
149 }
150
151 private:
152 base::hash_map<int64, DownloadItem*>::const_iterator iter_;
153 // Allow copy and assign.
154};
155
[email protected]53ac00e82012-10-18 20:59:20156void EnsureNoPendingDownloadJobsOnFile(bool* result) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]35869622012-10-26 23:23:55158 *result = (DownloadFile::GetNumberOfDownloadFiles() == 0);
[email protected]5948e1a2012-03-10 00:19:18159 BrowserThread::PostTask(
160 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure());
161}
162
[email protected]35869622012-10-26 23:23:55163class DownloadItemFactoryImpl : public DownloadItemFactory {
[email protected]d25fda12012-06-12 17:05:03164 public:
165 DownloadItemFactoryImpl() {}
166 virtual ~DownloadItemFactoryImpl() {}
167
[email protected]db1d8f72012-07-13 19:23:16168 virtual DownloadItemImpl* CreatePersistedItem(
169 DownloadItemImplDelegate* delegate,
[email protected]35869622012-10-26 23:23:55170 DownloadId download_id,
[email protected]876774692013-02-03 17:20:15171 const FilePath& current_path,
172 const FilePath& target_path,
173 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08174 const GURL& referrer_url,
175 const base::Time& start_time,
176 const base::Time& end_time,
177 int64 received_bytes,
178 int64 total_bytes,
179 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15180 DownloadDangerType danger_type,
181 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08182 bool opened,
[email protected]d25fda12012-06-12 17:05:03183 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]3d95e542012-11-20 00:52:08184 return new DownloadItemImpl(
185 delegate,
186 download_id,
[email protected]876774692013-02-03 17:20:15187 current_path,
188 target_path,
189 url_chain,
[email protected]3d95e542012-11-20 00:52:08190 referrer_url,
191 start_time,
192 end_time,
193 received_bytes,
194 total_bytes,
195 state,
[email protected]876774692013-02-03 17:20:15196 danger_type,
197 interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08198 opened,
199 bound_net_log);
[email protected]d25fda12012-06-12 17:05:03200 }
201
[email protected]db1d8f72012-07-13 19:23:16202 virtual DownloadItemImpl* CreateActiveItem(
203 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03204 const DownloadCreateInfo& info,
[email protected]d25fda12012-06-12 17:05:03205 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]8d68a3e02013-01-12 15:57:10206 return new DownloadItemImpl(delegate, info, bound_net_log);
[email protected]d25fda12012-06-12 17:05:03207 }
208
[email protected]db1d8f72012-07-13 19:23:16209 virtual DownloadItemImpl* CreateSavePageItem(
210 DownloadItemImplDelegate* delegate,
[email protected]d25fda12012-06-12 17:05:03211 const FilePath& path,
212 const GURL& url,
[email protected]35869622012-10-26 23:23:55213 DownloadId download_id,
[email protected]d25fda12012-06-12 17:05:03214 const std::string& mime_type,
215 const net::BoundNetLog& bound_net_log) OVERRIDE {
[email protected]66521d02012-08-14 17:51:54216 return new DownloadItemImpl(delegate, path, url, download_id,
[email protected]d25fda12012-06-12 17:05:03217 mime_type, bound_net_log);
218 }
219};
220
[email protected]a0ce3282011-08-19 20:49:52221} // namespace
222
[email protected]d25fda12012-06-12 17:05:03223DownloadManagerImpl::DownloadManagerImpl(
[email protected]d25fda12012-06-12 17:05:03224 net::NetLog* net_log)
[email protected]53ac00e82012-10-18 20:59:20225 : item_factory_(new DownloadItemFactoryImpl()),
[email protected]35869622012-10-26 23:23:55226 file_factory_(new DownloadFileFactory()),
[email protected]09ea72c7422012-07-02 20:35:40227 history_size_(0),
[email protected]d25fda12012-06-12 17:05:03228 shutdown_needed_(false),
[email protected]b441a8492012-06-06 14:55:57229 browser_context_(NULL),
[email protected]33c20cd92012-06-14 22:02:50230 delegate_(NULL),
[email protected]0baf5922013-01-30 13:55:18231 net_log_(net_log) {
initial.commit09911bf2008-07-26 23:55:29232}
233
[email protected]5656f8a2011-11-17 16:12:58234DownloadManagerImpl::~DownloadManagerImpl() {
[email protected]326a6a92010-09-10 20:21:13235 DCHECK(!shutdown_needed_);
initial.commit09911bf2008-07-26 23:55:29236}
237
[email protected]5656f8a2011-11-17 16:12:58238DownloadId DownloadManagerImpl::GetNextId() {
[email protected]491aaa62012-06-07 03:50:18239 DownloadId id;
240 if (delegate_)
241 id = delegate_->GetNextId();
242 if (!id.IsValid()) {
243 static int next_id;
244 id = DownloadId(browser_context_, ++next_id);
245 }
246
247 return id;
[email protected]2909e342011-10-29 00:46:53248}
249
[email protected]53ac00e82012-10-18 20:59:20250void DownloadManagerImpl::DetermineDownloadTarget(
251 DownloadItemImpl* item, const DownloadTargetCallback& callback) {
252 // Note that this next call relies on
253 // DownloadItemImplDelegate::DownloadTargetCallback and
254 // DownloadManagerDelegate::DownloadTargetCallback having the same
255 // type. If the types ever diverge, gasket code will need to
256 // be written here.
257 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) {
258 FilePath target_path = item->GetForcedFilePath();
259 // TODO(asanka): Determine a useful path if |target_path| is empty.
260 callback.Run(target_path,
261 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
[email protected]35869622012-10-26 23:23:55262 DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
[email protected]53ac00e82012-10-18 20:59:20263 target_path);
264 }
[email protected]47665442012-07-27 02:31:22265}
266
[email protected]98299622013-01-03 22:26:50267bool DownloadManagerImpl::ShouldCompleteDownload(
[email protected]18710a42a2012-10-17 19:50:43268 DownloadItemImpl* item, const base::Closure& complete_callback) {
269 if (!delegate_ ||
270 delegate_->ShouldCompleteDownload(item, complete_callback)) {
[email protected]98299622013-01-03 22:26:50271 return true;
[email protected]18710a42a2012-10-17 19:50:43272 }
273 // Otherwise, the delegate has accepted responsibility to run the
274 // callback when the download is ready for completion.
[email protected]98299622013-01-03 22:26:50275 return false;
[email protected]fc03de22011-12-06 23:28:12276}
277
278bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) {
[email protected]491aaa62012-06-07 03:50:18279 if (!delegate_)
280 return false;
281
[email protected]fc03de22011-12-06 23:28:12282 return delegate_->ShouldOpenFileBasedOnExtension(path);
283}
284
[email protected]bde0e4f2012-11-08 22:49:59285bool DownloadManagerImpl::ShouldOpenDownload(
286 DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) {
[email protected]18710a42a2012-10-17 19:50:43287 if (!delegate_)
288 return true;
289
[email protected]bde0e4f2012-11-08 22:49:59290 // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and
291 // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening"
292 // to have the same type :-}.
293 return delegate_->ShouldOpenDownload(item, callback);
[email protected]18710a42a2012-10-17 19:50:43294}
295
[email protected]35869622012-10-26 23:23:55296void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) {
[email protected]b441a8492012-06-06 14:55:57297 delegate_ = delegate;
298}
299
[email protected]35869622012-10-26 23:23:55300DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const {
[email protected]b488b5a52012-06-06 17:01:28301 return delegate_;
302}
303
[email protected]5656f8a2011-11-17 16:12:58304void DownloadManagerImpl::Shutdown() {
[email protected]da6e3922010-11-24 21:45:50305 VLOG(20) << __FUNCTION__ << "()"
306 << " shutdown_needed_ = " << shutdown_needed_;
[email protected]326a6a92010-09-10 20:21:13307 if (!shutdown_needed_)
308 return;
309 shutdown_needed_ = false;
initial.commit09911bf2008-07-26 23:55:29310
[email protected]75e51b52012-02-04 16:57:54311 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this));
[email protected]fb4f8d902011-09-16 06:07:08312 // TODO(benjhayden): Consider clearing observers_.
[email protected]326a6a92010-09-10 20:21:13313
[email protected]f04182f32010-12-10 19:12:07314 // Go through all downloads in downloads_. Dangerous ones we need to
315 // remove on disk, and in progress ones we need to cancel.
[email protected]ba7895d2012-06-22 19:02:52316 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) {
[email protected]db1d8f72012-07-13 19:23:16317 DownloadItemImpl* download = it->second;
[email protected]f04182f32010-12-10 19:12:07318
319 // Save iterator from potential erases in this set done by called code.
320 // Iterators after an erasure point are still valid for lists and
321 // associative containers such as sets.
322 it++;
323
[email protected]cda79062013-01-17 01:50:52324 if (download->IsDangerous() && download->IsPartialDownload()) {
[email protected]f04182f32010-12-10 19:12:07325 // The user hasn't accepted it, so we need to remove it
326 // from the disk. This may or may not result in it being
327 // removed from the DownloadManager queues and deleted
[email protected]fc03de22011-12-06 23:28:12328 // (specifically, DownloadManager::DownloadRemoved only
[email protected]f04182f32010-12-10 19:12:07329 // removes and deletes it if it's known to the history service)
330 // so the only thing we know after calling this function is that
331 // the download was deleted if-and-only-if it was removed
332 // from all queues.
[email protected]303077002011-04-19 23:21:01333 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
[email protected]bf68a00b2011-04-07 17:28:26334 } else if (download->IsPartialDownload()) {
[email protected]93af2272011-09-21 18:29:17335 download->Cancel(false);
initial.commit09911bf2008-07-26 23:55:29336 }
337 }
338
[email protected]f04182f32010-12-10 19:12:07339 // At this point, all dangerous downloads have had their files removed
340 // and all in progress downloads have been cancelled. We can now delete
341 // anything left.
[email protected]9ccbb372008-10-10 18:50:32342
[email protected]7745ff02012-10-01 00:09:24343 STLDeleteValues(&downloads_);
[email protected]7e834f02012-08-09 20:38:56344 downloads_.clear();
initial.commit09911bf2008-07-26 23:55:29345
[email protected]41f558fb2012-01-09 22:59:58346 // We'll have nothing more to report to the observers after this point.
347 observers_.Clear();
348
[email protected]b441a8492012-06-06 14:55:57349 if (delegate_)
350 delegate_->Shutdown();
[email protected]47665442012-07-27 02:31:22351 delegate_ = NULL;
initial.commit09911bf2008-07-26 23:55:29352}
353
[email protected]35869622012-10-26 23:23:55354bool DownloadManagerImpl::Init(BrowserContext* browser_context) {
[email protected]6d0c9fb2011-08-22 19:26:03355 DCHECK(browser_context);
initial.commit09911bf2008-07-26 23:55:29356 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
357 shutdown_needed_ = true;
358
[email protected]6d0c9fb2011-08-22 19:26:03359 browser_context_ = browser_context;
[email protected]024f2f02010-04-30 22:51:46360
initial.commit09911bf2008-07-26 23:55:29361 return true;
362}
363
[email protected]3d43eef2012-10-09 23:17:56364DownloadItem* DownloadManagerImpl::StartDownload(
[email protected]2bddd2072012-06-18 16:16:51365 scoped_ptr<DownloadCreateInfo> info,
[email protected]35869622012-10-26 23:23:55366 scoped_ptr<ByteStreamReader> stream) {
[email protected]ca4b5fa32010-10-09 12:42:18367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]287b86b2011-02-26 00:11:35368
[email protected]53ac00e82012-10-18 20:59:20369 FilePath default_download_directory;
[email protected]96bfed052012-09-15 22:21:01370 if (delegate_) {
371 FilePath website_save_directory; // Unused
372 bool skip_dir_check = false; // Unused
373 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory,
[email protected]53ac00e82012-10-18 20:59:20374 &default_download_directory, &skip_dir_check);
[email protected]96bfed052012-09-15 22:21:01375 }
376
[email protected]53ac00e82012-10-18 20:59:20377 // We create the DownloadItem before the DownloadFile because the
378 // DownloadItem already needs to handle a state in which there is
379 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads)
[email protected]8d68a3e02013-01-12 15:57:10380 DownloadItemImpl* download = GetOrCreateDownloadItem(info.get());
[email protected]35869622012-10-26 23:23:55381 scoped_ptr<DownloadFile> download_file(
[email protected]53ac00e82012-10-18 20:59:20382 file_factory_->CreateFile(
383 info->save_info.Pass(), default_download_directory,
384 info->url(), info->referrer_url,
[email protected]3a2b2b5c2012-11-01 23:55:30385 delegate_->GenerateFileHash(),
[email protected]8d68a3e02013-01-12 15:57:10386 stream.Pass(), download->GetBoundNetLog(),
[email protected]53ac00e82012-10-18 20:59:20387 download->DestinationObserverAsWeakPtr()));
[email protected]8d68a3e02013-01-12 15:57:10388 scoped_ptr<DownloadRequestHandleInterface> req_handle(
389 new DownloadRequestHandle(info->request_handle));
390 download->Start(download_file.Pass(), req_handle.Pass());
[email protected]96c3bdbaa2012-08-31 16:39:54391
[email protected]53ac00e82012-10-18 20:59:20392 // Delay notification until after Start() so that download_file is bound
393 // to download and all the usual setters (e.g. Cancel) work.
394 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download));
[email protected]96c3bdbaa2012-08-31 16:39:54395
[email protected]53ac00e82012-10-18 20:59:20396 return download;
[email protected]287b86b2011-02-26 00:11:35397}
398
[email protected]5656f8a2011-11-17 16:12:58399void DownloadManagerImpl::CheckForHistoryFilesRemoval() {
[email protected]9fc114672011-06-15 08:17:48400 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]09ea72c7422012-07-02 20:35:40401 for (DownloadMap::iterator it = downloads_.begin();
402 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16403 DownloadItemImpl* item = it->second;
[email protected]3d95e542012-11-20 00:52:08404 CheckForFileRemoval(item);
[email protected]9fc114672011-06-15 08:17:48405 }
406}
407
[email protected]db1d8f72012-07-13 19:23:16408void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) {
[email protected]9fc114672011-06-15 08:17:48409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
410 if (download_item->IsComplete() &&
[email protected]4c544cd2013-01-25 07:54:16411 !download_item->GetFileExternallyRemoved() &&
412 delegate_) {
413 delegate_->CheckForFileExistence(
414 download_item,
415 base::Bind(&DownloadManagerImpl::OnFileExistenceChecked,
416 this, download_item->GetId()));
[email protected]9fc114672011-06-15 08:17:48417 }
418}
419
[email protected]4c544cd2013-01-25 07:54:16420void DownloadManagerImpl::OnFileExistenceChecked(int32 download_id,
421 bool result) {
[email protected]9fc114672011-06-15 08:17:48422 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]4c544cd2013-01-25 07:54:16423 if (!result) { // File does not exist.
424 if (ContainsKey(downloads_, download_id))
425 downloads_[download_id]->OnDownloadedFileRemoved();
426 }
[email protected]9fc114672011-06-15 08:17:48427}
428
[email protected]35869622012-10-26 23:23:55429BrowserContext* DownloadManagerImpl::GetBrowserContext() const {
[email protected]5656f8a2011-11-17 16:12:58430 return browser_context_;
431}
432
[email protected]8d68a3e02013-01-12 15:57:10433DownloadItemImpl* DownloadManagerImpl::GetOrCreateDownloadItem(
434 DownloadCreateInfo* info) {
[email protected]c2e76012010-12-23 21:10:29435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
436
[email protected]d25fda12012-06-12 17:05:03437 if (!info->download_id.IsValid())
438 info->download_id = GetNextId();
[email protected]d8472d92011-08-26 20:15:20439
[email protected]8d68a3e02013-01-12 15:57:10440 DownloadItemImpl* download = NULL;
441 if (ContainsKey(downloads_, info->download_id.local())) {
442 // Resuming an existing download.
443 download = downloads_[info->download_id.local()];
444 DCHECK(download->IsInterrupted());
445 } else {
446 // New download
447 net::BoundNetLog bound_net_log =
448 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
449 download = item_factory_->CreateActiveItem(this, *info, bound_net_log);
450 downloads_[download->GetId()] = download;
451 }
452
[email protected]53ac00e82012-10-18 20:59:20453 return download;
[email protected]c2e76012010-12-23 21:10:29454}
455
[email protected]db1d8f72012-07-13 19:23:16456DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem(
[email protected]fc03de22011-12-06 23:28:12457 const FilePath& main_file_path,
458 const GURL& page_url,
[email protected]6474b112012-05-04 19:35:27459 const std::string& mime_type,
[email protected]fc03de22011-12-06 23:28:12460 DownloadItem::Observer* observer) {
[email protected]ef17c9a2012-02-09 05:08:09461 net::BoundNetLog bound_net_log =
462 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD);
[email protected]3d95e542012-11-20 00:52:08463 DownloadItemImpl* download_item = item_factory_->CreateSavePageItem(
[email protected]6474b112012-05-04 19:35:27464 this,
465 main_file_path,
466 page_url,
[email protected]6474b112012-05-04 19:35:27467 GetNextId(),
468 mime_type,
469 bound_net_log);
[email protected]fc03de22011-12-06 23:28:12470
[email protected]3d95e542012-11-20 00:52:08471 download_item->AddObserver(observer);
472 DCHECK(!ContainsKey(downloads_, download_item->GetId()));
473 downloads_[download_item->GetId()] = download_item;
474 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(
475 this, download_item));
[email protected]fc03de22011-12-06 23:28:12476
[email protected]3d95e542012-11-20 00:52:08477 // TODO(asanka): Make the ui an observer.
478 ShowDownloadInBrowser(download_item);
[email protected]fc03de22011-12-06 23:28:12479
[email protected]3d95e542012-11-20 00:52:08480 return download_item;
[email protected]fc03de22011-12-06 23:28:12481}
482
[email protected]df58aa82013-01-24 21:54:00483void DownloadManagerImpl::OnSavePackageSuccessfullyFinished(
484 DownloadItem* download_item) {
485 FOR_EACH_OBSERVER(Observer, observers_,
486 OnSavePackageSuccessfullyFinished(this, download_item));
487}
488
[email protected]5656f8a2011-11-17 16:12:58489void DownloadManagerImpl::CancelDownload(int32 download_id) {
[email protected]fd6ddb82012-11-27 00:38:43490 DownloadItem* download = GetDownload(download_id);
491 if (!download || !download->IsInProgress())
492 return;
493 download->Cancel(true);
[email protected]93af2272011-09-21 18:29:17494}
495
[email protected]8d68a3e02013-01-12 15:57:10496// Resume a download of a specific URL. We send the request to the
497// ResourceDispatcherHost, and let it send us responses like a regular
498// download.
499void DownloadManagerImpl::ResumeInterruptedDownload(
500 scoped_ptr<content::DownloadUrlParameters> params,
501 content::DownloadId id) {
502 BrowserThread::PostTask(
503 BrowserThread::IO,
504 FROM_HERE,
505 base::Bind(&BeginDownload, base::Passed(params.Pass()), id));
506}
507
[email protected]53ac00e82012-10-18 20:59:20508void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
[email protected]35869622012-10-26 23:23:55509 scoped_ptr<DownloadItemFactory> item_factory) {
[email protected]53ac00e82012-10-18 20:59:20510 item_factory_ = item_factory.Pass();
511}
512
513void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
[email protected]35869622012-10-26 23:23:55514 scoped_ptr<DownloadFileFactory> file_factory) {
[email protected]53ac00e82012-10-18 20:59:20515 file_factory_ = file_factory.Pass();
516}
517
[email protected]35869622012-10-26 23:23:55518DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() {
[email protected]53ac00e82012-10-18 20:59:20519 return file_factory_.get();
520}
521
[email protected]5656f8a2011-11-17 16:12:58522int DownloadManagerImpl::RemoveDownloadItems(
[email protected]db1d8f72012-07-13 19:23:16523 const DownloadItemImplVector& pending_deletes) {
[email protected]6d0146c2011-08-04 19:13:04524 if (pending_deletes.empty())
525 return 0;
526
527 // Delete from internal maps.
[email protected]db1d8f72012-07-13 19:23:16528 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin();
[email protected]7e834f02012-08-09 20:38:56529 it != pending_deletes.end();
530 ++it) {
[email protected]db1d8f72012-07-13 19:23:16531 DownloadItemImpl* download = *it;
[email protected]6d0146c2011-08-04 19:13:04532 DCHECK(download);
[email protected]7e834f02012-08-09 20:38:56533 int32 download_id = download->GetId();
534 delete download;
535 downloads_.erase(download_id);
[email protected]6d0146c2011-08-04 19:13:04536 }
[email protected]7e834f02012-08-09 20:38:56537 return static_cast<int>(pending_deletes.size());
[email protected]6d0146c2011-08-04 19:13:04538}
539
[email protected]db1d8f72012-07-13 19:23:16540void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) {
[email protected]09ea72c7422012-07-02 20:35:40541 if (!download ||
542 downloads_.find(download->GetId()) == downloads_.end())
543 return;
544
initial.commit09911bf2008-07-26 23:55:29545 // Remove from our tables and delete.
[email protected]db1d8f72012-07-13 19:23:16546 int downloads_count =
547 RemoveDownloadItems(DownloadItemImplVector(1, download));
[email protected]f04182f32010-12-10 19:12:07548 DCHECK_EQ(1, downloads_count);
initial.commit09911bf2008-07-26 23:55:29549}
550
[email protected]fd3a82832012-01-19 20:35:12551int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin,
552 base::Time remove_end) {
[email protected]db1d8f72012-07-13 19:23:16553 DownloadItemImplVector pending_deletes;
[email protected]09ea72c7422012-07-02 20:35:40554 for (DownloadMap::const_iterator it = downloads_.begin();
555 it != downloads_.end();
[email protected]6d0146c2011-08-04 19:13:04556 ++it) {
[email protected]db1d8f72012-07-13 19:23:16557 DownloadItemImpl* download = it->second;
[email protected]3d95e542012-11-20 00:52:08558 if (download->GetStartTime() >= remove_begin &&
[email protected]c09a8fd2011-11-21 19:54:50559 (remove_end.is_null() || download->GetStartTime() < remove_end) &&
[email protected]6d0146c2011-08-04 19:13:04560 (download->IsComplete() || download->IsCancelled())) {
[email protected]7e834f02012-08-09 20:38:56561 download->NotifyRemoved();
[email protected]78b8fcc92009-03-31 17:36:28562 pending_deletes.push_back(download);
initial.commit09911bf2008-07-26 23:55:29563 }
initial.commit09911bf2008-07-26 23:55:29564 }
[email protected]6d0146c2011-08-04 19:13:04565 return RemoveDownloadItems(pending_deletes);
initial.commit09911bf2008-07-26 23:55:29566}
567
[email protected]fd3a82832012-01-19 20:35:12568int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) {
[email protected]e93d2822009-01-30 05:59:59569 return RemoveDownloadsBetween(remove_begin, base::Time());
initial.commit09911bf2008-07-26 23:55:29570}
571
[email protected]5656f8a2011-11-17 16:12:58572int DownloadManagerImpl::RemoveAllDownloads() {
[email protected]d41355e6f2009-04-07 21:21:12573 // The null times make the date range unbounded.
[email protected]09ea72c7422012-07-02 20:35:40574 int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time());
[email protected]35869622012-10-26 23:23:55575 RecordClearAllSize(num_deleted);
[email protected]09ea72c7422012-07-02 20:35:40576 return num_deleted;
[email protected]d41355e6f2009-04-07 21:21:12577}
578
[email protected]0d4e30c2012-01-28 00:47:53579void DownloadManagerImpl::DownloadUrl(
[email protected]35869622012-10-26 23:23:55580 scoped_ptr<DownloadUrlParameters> params) {
[email protected]c5a5c0842012-05-04 20:05:14581 if (params->post_id() >= 0) {
582 // Check this here so that the traceback is more useful.
583 DCHECK(params->prefer_cache());
584 DCHECK(params->method() == "POST");
585 }
586 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
[email protected]8d68a3e02013-01-12 15:57:10587 &BeginDownload, base::Passed(&params), DownloadId()));
initial.commit09911bf2008-07-26 23:55:29588}
589
[email protected]5656f8a2011-11-17 16:12:58590void DownloadManagerImpl::AddObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29591 observers_.AddObserver(observer);
initial.commit09911bf2008-07-26 23:55:29592}
593
[email protected]5656f8a2011-11-17 16:12:58594void DownloadManagerImpl::RemoveObserver(Observer* observer) {
initial.commit09911bf2008-07-26 23:55:29595 observers_.RemoveObserver(observer);
596}
597
[email protected]3d95e542012-11-20 00:52:08598DownloadItem* DownloadManagerImpl::CreateDownloadItem(
[email protected]876774692013-02-03 17:20:15599 const FilePath& current_path,
600 const FilePath& target_path,
601 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08602 const GURL& referrer_url,
603 const base::Time& start_time,
604 const base::Time& end_time,
605 int64 received_bytes,
606 int64 total_bytes,
607 DownloadItem::DownloadState state,
[email protected]876774692013-02-03 17:20:15608 DownloadDangerType danger_type,
609 DownloadInterruptReason interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08610 bool opened) {
611 DownloadItemImpl* item = item_factory_->CreatePersistedItem(
612 this,
613 GetNextId(),
[email protected]876774692013-02-03 17:20:15614 current_path,
615 target_path,
616 url_chain,
[email protected]3d95e542012-11-20 00:52:08617 referrer_url,
618 start_time,
619 end_time,
620 received_bytes,
621 total_bytes,
622 state,
[email protected]876774692013-02-03 17:20:15623 danger_type,
624 interrupt_reason,
[email protected]3d95e542012-11-20 00:52:08625 opened,
626 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD));
627 DCHECK(!ContainsKey(downloads_, item->GetId()));
628 downloads_[item->GetId()] = item;
629 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item));
630 VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true);
631 return item;
initial.commit09911bf2008-07-26 23:55:29632}
633
[email protected]3d95e542012-11-20 00:52:08634// TODO(asanka) Move into an observer.
[email protected]db1d8f72012-07-13 19:23:16635void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) {
[email protected]a29e4f22012-04-12 21:22:03636 // The 'contents' may no longer exist if the user closed the contents before
637 // we get this start completion event.
[email protected]a62d42902012-01-24 17:24:38638 WebContents* content = download->GetWebContents();
[email protected]99cb7f82011-07-28 17:27:26639
640 // If the contents no longer exists, we ask the embedder to suggest another
[email protected]a29e4f22012-04-12 21:22:03641 // contents.
[email protected]491aaa62012-06-07 03:50:18642 if (!content && delegate_)
[email protected]ef9572e2012-01-04 22:14:12643 content = delegate_->GetAlternativeWebContentsToNotifyForDownload();
[email protected]5e595482009-05-06 20:16:53644
[email protected]0bfbf882011-12-22 18:19:27645 if (content && content->GetDelegate())
646 content->GetDelegate()->OnStartDownload(content, download);
[email protected]5e595482009-05-06 20:16:53647}
648
[email protected]5656f8a2011-11-17 16:12:58649int DownloadManagerImpl::InProgressCount() const {
[email protected]007e7412012-03-13 20:10:56650 int count = 0;
[email protected]fd6ddb82012-11-27 00:38:43651 for (DownloadMap::const_iterator it = downloads_.begin();
652 it != downloads_.end(); ++it) {
653 if (it->second->IsInProgress())
[email protected]007e7412012-03-13 20:10:56654 ++count;
655 }
656 return count;
[email protected]5656f8a2011-11-17 16:12:58657}
658
[email protected]ba7895d2012-06-22 19:02:52659DownloadItem* DownloadManagerImpl::GetDownload(int download_id) {
660 return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL;
661}
662
[email protected]9f1f4092012-09-10 21:18:04663void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) {
664 for (DownloadMap::iterator it = downloads_.begin();
665 it != downloads_.end(); ++it) {
666 downloads->push_back(it->second);
667 }
668}
669
[email protected]ffc0c2d2013-01-21 15:32:12670void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) {
[email protected]da4a5582011-10-17 19:08:06671 int num_unopened = 0;
[email protected]09ea72c7422012-07-02 20:35:40672 for (DownloadMap::iterator it = downloads_.begin();
673 it != downloads_.end(); ++it) {
[email protected]db1d8f72012-07-13 19:23:16674 DownloadItemImpl* item = it->second;
[email protected]09ea72c7422012-07-02 20:35:40675 if (item->IsComplete() &&
676 !item->GetOpened())
[email protected]da4a5582011-10-17 19:08:06677 ++num_unopened;
678 }
[email protected]35869622012-10-26 23:23:55679 RecordOpensOutstanding(num_unopened);
[email protected]ffc0c2d2013-01-21 15:32:12680
[email protected]0baf5922013-01-30 13:55:18681 if (delegate_)
[email protected]ffc0c2d2013-01-21 15:32:12682 delegate_->OpenDownload(download);
683}
684
685void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) {
686 if (delegate_)
687 delegate_->ShowDownloadInShell(download);
[email protected]da4a5582011-10-17 19:08:06688}
[email protected]5656f8a2011-11-17 16:12:58689
[email protected]35869622012-10-26 23:23:55690} // namespace content