[email protected] | 0afff03 | 2012-01-06 20:55:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 5 | #include "content/browser/download/download_manager_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 7 | #include <iterator> |
| 8 | |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 11 | #include "base/debug/alias.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 12 | #include "base/i18n/case_conversion.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 14 | #include "base/message_loop.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 16 | #include "base/stringprintf.h" |
[email protected] | 40d11e0 | 2013-03-28 17:43:14 | [diff] [blame] | 17 | #include "base/strings/sys_string_conversions.h" |
[email protected] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 18 | #include "base/supports_user_data.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 19 | #include "base/synchronization/lock.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 20 | #include "build/build_config.h" |
[email protected] | f8e92b5d | 2013-03-21 18:35:46 | [diff] [blame] | 21 | #include "content/browser/byte_stream.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 22 | #include "content/browser/download/download_create_info.h" |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 23 | #include "content/browser/download/download_file_factory.h" |
| 24 | #include "content/browser/download/download_item_factory.h" |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 25 | #include "content/browser/download/download_item_impl.h" |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 26 | #include "content/browser/download/download_stats.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 27 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 28 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 29 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 30 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 31 | #include "content/public/browser/browser_thread.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 32 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 33 | #include "content/public/browser/download_interrupt_reasons.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 34 | #include "content/public/browser/download_manager_delegate.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 35 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 36 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 37 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 38 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 39 | #include "content/public/browser/resource_context.h" |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 40 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 41 | #include "content/public/common/referrer.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 42 | #include "net/base/load_flags.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 43 | #include "net/base/upload_bytes_element_reader.h" |
| 44 | #include "net/base/upload_data_stream.h" |
[email protected] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 45 | #include "net/url_request/url_request_context.h" |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 46 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 48 | namespace content { |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 49 | namespace { |
| 50 | |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 51 | void BeginDownload(scoped_ptr<DownloadUrlParameters> params, |
| 52 | DownloadId download_id) { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 53 | 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] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 57 | scoped_ptr<net::URLRequest> request( |
| 58 | params->resource_context()->GetRequestContext()->CreateRequest( |
| 59 | params->url(), NULL)); |
[email protected] | dd18a1f | 2013-03-16 17:34:07 | [diff] [blame] | 60 | if (params->referrer().url.is_valid()) |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 61 | request->SetReferrer(params->referrer().url.spec()); |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 62 | webkit_glue::ConfigureURLRequestForReferrerPolicy( |
| 63 | request.get(), params->referrer().policy); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 64 | request->set_load_flags(request->load_flags() | params->load_flags()); |
| 65 | request->set_method(params->method()); |
[email protected] | fdae8bf | 2012-11-20 04:02:45 | [diff] [blame] | 66 | if (!params->post_body().empty()) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 67 | const std::string& body = params->post_body(); |
| 68 | scoped_ptr<net::UploadElementReader> reader( |
| 69 | net::UploadOwnedBytesElementReader::CreateWithString(body)); |
| 70 | request->set_upload(make_scoped_ptr( |
| 71 | net::UploadDataStream::CreateWithReader(reader.Pass(), 0))); |
[email protected] | fdae8bf | 2012-11-20 04:02:45 | [diff] [blame] | 72 | } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 73 | if (params->post_id() >= 0) { |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 74 | // The POST in this case does not have an actual body, and only works |
| 75 | // when retrieving data from cache. This is done because we don't want |
| 76 | // to do a re-POST without user consent, and currently don't have a good |
| 77 | // plan on how to display the UI for that. |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 78 | DCHECK(params->prefer_cache()); |
| 79 | DCHECK(params->method() == "POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 80 | ScopedVector<net::UploadElementReader> element_readers; |
| 81 | request->set_upload(make_scoped_ptr( |
| 82 | new net::UploadDataStream(&element_readers, params->post_id()))); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 83 | } |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 84 | |
| 85 | // If we're not at the beginning of the file, retrieve only the remaining |
| 86 | // portion. |
[email protected] | fc179dd1 | 2013-03-16 09:35:19 | [diff] [blame] | 87 | bool has_last_modified = !params->last_modified().empty(); |
| 88 | bool has_etag = !params->etag().empty(); |
| 89 | |
| 90 | // If we've asked for a range, we want to make sure that we only |
| 91 | // get that range if our current copy of the information is good. |
| 92 | // We shouldn't be asked to continue if we don't have a verifier. |
| 93 | DCHECK(params->offset() == 0 || has_etag || has_last_modified); |
| 94 | |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 95 | if (params->offset() > 0) { |
| 96 | request->SetExtraRequestHeaderByName( |
| 97 | "Range", |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 98 | base::StringPrintf("bytes=%" PRId64 "-", params->offset()), |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 99 | true); |
| 100 | |
[email protected] | fc179dd1 | 2013-03-16 09:35:19 | [diff] [blame] | 101 | if (has_last_modified) { |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 102 | request->SetExtraRequestHeaderByName("If-Unmodified-Since", |
| 103 | params->last_modified(), |
| 104 | true); |
| 105 | } |
[email protected] | fc179dd1 | 2013-03-16 09:35:19 | [diff] [blame] | 106 | if (has_etag) { |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 107 | request->SetExtraRequestHeaderByName("If-Match", params->etag(), true); |
| 108 | } |
| 109 | } |
| 110 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 111 | for (DownloadUrlParameters::RequestHeadersType::const_iterator iter |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 112 | = params->request_headers_begin(); |
| 113 | iter != params->request_headers_end(); |
| 114 | ++iter) { |
| 115 | request->SetExtraRequestHeaderByName( |
| 116 | iter->first, iter->second, false/*overwrite*/); |
| 117 | } |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 118 | |
| 119 | scoped_ptr<DownloadSaveInfo> save_info(new DownloadSaveInfo()); |
| 120 | save_info->file_path = params->file_path(); |
| 121 | save_info->suggested_name = params->suggested_name(); |
| 122 | save_info->offset = params->offset(); |
| 123 | save_info->hash_state = params->hash_state(); |
| 124 | save_info->prompt_for_save_location = params->prompt(); |
| 125 | save_info->file_stream = params->GetFileStream(); |
| 126 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 127 | params->resource_dispatcher_host()->BeginDownload( |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 128 | request.Pass(), |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 129 | params->content_initiated(), |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 130 | params->resource_context(), |
| 131 | params->render_process_host_id(), |
| 132 | params->render_view_host_routing_id(), |
| 133 | params->prefer_cache(), |
[email protected] | 49d812e | 2012-11-06 21:18:12 | [diff] [blame] | 134 | save_info.Pass(), |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 135 | download_id, |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 136 | params->callback()); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 33d2210 | 2012-01-25 17:46:53 | [diff] [blame] | 139 | class MapValueIteratorAdapter { |
| 140 | public: |
| 141 | explicit MapValueIteratorAdapter( |
| 142 | base::hash_map<int64, DownloadItem*>::const_iterator iter) |
| 143 | : iter_(iter) { |
| 144 | } |
| 145 | ~MapValueIteratorAdapter() {} |
| 146 | |
| 147 | DownloadItem* operator*() { return iter_->second; } |
| 148 | |
| 149 | MapValueIteratorAdapter& operator++() { |
| 150 | ++iter_; |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | bool operator!=(const MapValueIteratorAdapter& that) const { |
| 155 | return iter_ != that.iter_; |
| 156 | } |
| 157 | |
| 158 | private: |
| 159 | base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
| 160 | // Allow copy and assign. |
| 161 | }; |
| 162 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 163 | void EnsureNoPendingDownloadJobsOnFile(bool* result) { |
| 164 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 165 | *result = (DownloadFile::GetNumberOfDownloadFiles() == 0); |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 166 | BrowserThread::PostTask( |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame^] | 167 | BrowserThread::UI, FROM_HERE, base::MessageLoop::QuitClosure()); |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 170 | class DownloadItemFactoryImpl : public DownloadItemFactory { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 171 | public: |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 172 | DownloadItemFactoryImpl() {} |
| 173 | virtual ~DownloadItemFactoryImpl() {} |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 174 | |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 175 | virtual DownloadItemImpl* CreatePersistedItem( |
| 176 | DownloadItemImplDelegate* delegate, |
| 177 | DownloadId download_id, |
| 178 | const base::FilePath& current_path, |
| 179 | const base::FilePath& target_path, |
| 180 | const std::vector<GURL>& url_chain, |
| 181 | const GURL& referrer_url, |
| 182 | const base::Time& start_time, |
| 183 | const base::Time& end_time, |
| 184 | int64 received_bytes, |
| 185 | int64 total_bytes, |
| 186 | DownloadItem::DownloadState state, |
| 187 | DownloadDangerType danger_type, |
| 188 | DownloadInterruptReason interrupt_reason, |
| 189 | bool opened, |
| 190 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 191 | return new DownloadItemImpl( |
| 192 | delegate, |
| 193 | download_id, |
| 194 | current_path, |
| 195 | target_path, |
| 196 | url_chain, |
| 197 | referrer_url, |
| 198 | start_time, |
| 199 | end_time, |
| 200 | received_bytes, |
| 201 | total_bytes, |
| 202 | state, |
| 203 | danger_type, |
| 204 | interrupt_reason, |
| 205 | opened, |
| 206 | bound_net_log); |
| 207 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 208 | |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 209 | virtual DownloadItemImpl* CreateActiveItem( |
| 210 | DownloadItemImplDelegate* delegate, |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 211 | DownloadId download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 212 | const DownloadCreateInfo& info, |
| 213 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 214 | return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 215 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 216 | |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 217 | virtual DownloadItemImpl* CreateSavePageItem( |
| 218 | DownloadItemImplDelegate* delegate, |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 219 | DownloadId download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 220 | const base::FilePath& path, |
| 221 | const GURL& url, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 222 | const std::string& mime_type, |
| 223 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 224 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 225 | return new DownloadItemImpl(delegate, download_id, path, url, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 226 | mime_type, request_handle.Pass(), |
| 227 | bound_net_log); |
| 228 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 229 | }; |
| 230 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 231 | } // namespace |
| 232 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 233 | DownloadManagerImpl::DownloadManagerImpl( |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 234 | net::NetLog* net_log, |
| 235 | BrowserContext* browser_context) |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 236 | : item_factory_(new DownloadItemFactoryImpl()), |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 237 | file_factory_(new DownloadFileFactory()), |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 238 | history_size_(0), |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 239 | shutdown_needed_(true), |
| 240 | browser_context_(browser_context), |
[email protected] | 33c20cd9 | 2012-06-14 22:02:50 | [diff] [blame] | 241 | delegate_(NULL), |
[email protected] | 0baf592 | 2013-01-30 13:55:18 | [diff] [blame] | 242 | net_log_(net_log) { |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 243 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 246 | DownloadManagerImpl::~DownloadManagerImpl() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 247 | DCHECK(!shutdown_needed_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 250 | void DownloadManagerImpl::CreateActiveItem( |
| 251 | DownloadId id, const DownloadCreateInfo& info) { |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 252 | net::BoundNetLog bound_net_log = |
| 253 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 254 | downloads_[id.local()] = |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 255 | item_factory_->CreateActiveItem(this, id, info, bound_net_log); |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 258 | DownloadId DownloadManagerImpl::GetNextId() { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 259 | DownloadId id; |
| 260 | if (delegate_) |
| 261 | id = delegate_->GetNextId(); |
| 262 | if (!id.IsValid()) { |
| 263 | static int next_id; |
| 264 | id = DownloadId(browser_context_, ++next_id); |
| 265 | } |
| 266 | |
| 267 | return id; |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 270 | void DownloadManagerImpl::DetermineDownloadTarget( |
| 271 | DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| 272 | // Note that this next call relies on |
| 273 | // DownloadItemImplDelegate::DownloadTargetCallback and |
| 274 | // DownloadManagerDelegate::DownloadTargetCallback having the same |
| 275 | // type. If the types ever diverge, gasket code will need to |
| 276 | // be written here. |
| 277 | if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 278 | base::FilePath target_path = item->GetForcedFilePath(); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 279 | // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 280 | callback.Run(target_path, |
| 281 | DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 282 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 283 | target_path); |
| 284 | } |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 285 | } |
| 286 | |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 287 | bool DownloadManagerImpl::ShouldCompleteDownload( |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 288 | DownloadItemImpl* item, const base::Closure& complete_callback) { |
| 289 | if (!delegate_ || |
| 290 | delegate_->ShouldCompleteDownload(item, complete_callback)) { |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 291 | return true; |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 292 | } |
| 293 | // Otherwise, the delegate has accepted responsibility to run the |
| 294 | // callback when the download is ready for completion. |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 295 | return false; |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 298 | bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension( |
| 299 | const base::FilePath& path) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 300 | if (!delegate_) |
| 301 | return false; |
| 302 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 303 | return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 304 | } |
| 305 | |
[email protected] | bde0e4f | 2012-11-08 22:49:59 | [diff] [blame] | 306 | bool DownloadManagerImpl::ShouldOpenDownload( |
| 307 | DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) { |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 308 | if (!delegate_) |
| 309 | return true; |
| 310 | |
[email protected] | bde0e4f | 2012-11-08 22:49:59 | [diff] [blame] | 311 | // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and |
| 312 | // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening" |
| 313 | // to have the same type :-}. |
| 314 | return delegate_->ShouldOpenDownload(item, callback); |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 317 | void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) { |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 318 | delegate_ = delegate; |
| 319 | } |
| 320 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 321 | DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const { |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 322 | return delegate_; |
| 323 | } |
| 324 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 325 | void DownloadManagerImpl::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 326 | VLOG(20) << __FUNCTION__ << "()" |
| 327 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 328 | if (!shutdown_needed_) |
| 329 | return; |
| 330 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 332 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
[email protected] | fb4f8d90 | 2011-09-16 06:07:08 | [diff] [blame] | 333 | // TODO(benjhayden): Consider clearing observers_. |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 334 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 335 | // Go through all downloads in downloads_. Dangerous ones we need to |
| 336 | // remove on disk, and in progress ones we need to cancel. |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 337 | for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 338 | DownloadItemImpl* download = it->second; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 339 | |
| 340 | // Save iterator from potential erases in this set done by called code. |
| 341 | // Iterators after an erasure point are still valid for lists and |
| 342 | // associative containers such as sets. |
| 343 | it++; |
| 344 | |
[email protected] | cda7906 | 2013-01-17 01:50:52 | [diff] [blame] | 345 | if (download->IsDangerous() && download->IsPartialDownload()) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 346 | // The user hasn't accepted it, so we need to remove it |
| 347 | // from the disk. This may or may not result in it being |
| 348 | // removed from the DownloadManager queues and deleted |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 349 | // (specifically, DownloadManager::DownloadRemoved only |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 350 | // removes and deletes it if it's known to the history service) |
| 351 | // so the only thing we know after calling this function is that |
| 352 | // the download was deleted if-and-only-if it was removed |
| 353 | // from all queues. |
[email protected] | 30307700 | 2011-04-19 23:21:01 | [diff] [blame] | 354 | download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 355 | } else if (download->IsPartialDownload()) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 356 | download->Cancel(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 360 | // At this point, all dangerous downloads have had their files removed |
| 361 | // and all in progress downloads have been cancelled. We can now delete |
| 362 | // anything left. |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 363 | |
[email protected] | 7745ff0 | 2012-10-01 00:09:24 | [diff] [blame] | 364 | STLDeleteValues(&downloads_); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 365 | downloads_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | |
[email protected] | 41f558fb | 2012-01-09 22:59:58 | [diff] [blame] | 367 | // We'll have nothing more to report to the observers after this point. |
| 368 | observers_.Clear(); |
| 369 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 370 | if (delegate_) |
| 371 | delegate_->Shutdown(); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 372 | delegate_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | 3d43eef | 2012-10-09 23:17:56 | [diff] [blame] | 375 | DownloadItem* DownloadManagerImpl::StartDownload( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 376 | scoped_ptr<DownloadCreateInfo> info, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 377 | scoped_ptr<ByteStreamReader> stream) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 378 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 379 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 380 | base::FilePath default_download_directory; |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 381 | if (delegate_) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 382 | base::FilePath website_save_directory; // Unused |
| 383 | bool skip_dir_check = false; // Unused |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 384 | delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 385 | &default_download_directory, &skip_dir_check); |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 388 | // If we don't have a valid id, that's a signal to generate one. |
| 389 | DownloadId id(info->download_id); |
| 390 | if (!id.IsValid()) |
| 391 | id = GetNextId(); |
| 392 | |
| 393 | // Create a new download item if this isn't a resumption. |
| 394 | bool new_download(!ContainsKey(downloads_, id.local())); |
| 395 | if (new_download) |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 396 | CreateActiveItem(id, *info); |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 397 | |
| 398 | DownloadItemImpl* download(downloads_[id.local()]); |
| 399 | DCHECK(download); |
| 400 | DCHECK(new_download || download->IsInterrupted()); |
| 401 | |
| 402 | // Create the download file and start the download. |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 403 | scoped_ptr<DownloadFile> download_file( |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 404 | file_factory_->CreateFile( |
| 405 | info->save_info.Pass(), default_download_directory, |
| 406 | info->url(), info->referrer_url, |
[email protected] | 3a2b2b5c | 2012-11-01 23:55:30 | [diff] [blame] | 407 | delegate_->GenerateFileHash(), |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 408 | stream.Pass(), download->GetBoundNetLog(), |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 409 | download->DestinationObserverAsWeakPtr())); |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 410 | scoped_ptr<DownloadRequestHandleInterface> req_handle( |
| 411 | new DownloadRequestHandle(info->request_handle)); |
| 412 | download->Start(download_file.Pass(), req_handle.Pass()); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 413 | |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 414 | // For interrupted downloads, Start() will transition the state to |
| 415 | // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). |
| 416 | // For new downloads, we notify here, rather than earlier, so that |
| 417 | // the download_file is bound to download and all the usual |
| 418 | // setters (e.g. Cancel) work. |
| 419 | if (new_download) |
| 420 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 421 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 422 | return download; |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 423 | } |
| 424 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 425 | void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 426 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 427 | for (DownloadMap::iterator it = downloads_.begin(); |
| 428 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 429 | DownloadItemImpl* item = it->second; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 430 | CheckForFileRemoval(item); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 434 | void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 435 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 436 | if (download_item->IsComplete() && |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 437 | !download_item->GetFileExternallyRemoved() && |
| 438 | delegate_) { |
| 439 | delegate_->CheckForFileExistence( |
| 440 | download_item, |
| 441 | base::Bind(&DownloadManagerImpl::OnFileExistenceChecked, |
| 442 | this, download_item->GetId())); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 446 | void DownloadManagerImpl::OnFileExistenceChecked(int32 download_id, |
| 447 | bool result) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 448 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 449 | if (!result) { // File does not exist. |
| 450 | if (ContainsKey(downloads_, download_id)) |
| 451 | downloads_[download_id]->OnDownloadedFileRemoved(); |
| 452 | } |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 455 | BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 456 | return browser_context_; |
| 457 | } |
| 458 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 459 | DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 460 | const base::FilePath& main_file_path, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 461 | const GURL& page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 462 | const std::string& mime_type, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 463 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 464 | DownloadItem::Observer* observer) { |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 465 | net::BoundNetLog bound_net_log = |
| 466 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 467 | DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 468 | this, |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 469 | GetNextId(), |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 470 | main_file_path, |
| 471 | page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 472 | mime_type, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 473 | request_handle.Pass(), |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 474 | bound_net_log); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 475 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 476 | download_item->AddObserver(observer); |
| 477 | DCHECK(!ContainsKey(downloads_, download_item->GetId())); |
| 478 | downloads_[download_item->GetId()] = download_item; |
| 479 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
| 480 | this, download_item)); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 481 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 482 | return download_item; |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | df58aa8 | 2013-01-24 21:54:00 | [diff] [blame] | 485 | void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( |
| 486 | DownloadItem* download_item) { |
| 487 | FOR_EACH_OBSERVER(Observer, observers_, |
| 488 | OnSavePackageSuccessfullyFinished(this, download_item)); |
| 489 | } |
| 490 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 491 | void DownloadManagerImpl::CancelDownload(int32 download_id) { |
[email protected] | fd6ddb8 | 2012-11-27 00:38:43 | [diff] [blame] | 492 | DownloadItem* download = GetDownload(download_id); |
| 493 | if (!download || !download->IsInProgress()) |
| 494 | return; |
| 495 | download->Cancel(true); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 496 | } |
| 497 | |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 498 | // Resume a download of a specific URL. We send the request to the |
| 499 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 500 | // download. |
| 501 | void DownloadManagerImpl::ResumeInterruptedDownload( |
| 502 | scoped_ptr<content::DownloadUrlParameters> params, |
| 503 | content::DownloadId id) { |
| 504 | BrowserThread::PostTask( |
| 505 | BrowserThread::IO, |
| 506 | FROM_HERE, |
[email protected] | c02087b51 | 2013-02-04 03:09:20 | [diff] [blame] | 507 | base::Bind(&BeginDownload, base::Passed(¶ms), id)); |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 508 | } |
| 509 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 510 | void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 511 | scoped_ptr<DownloadItemFactory> item_factory) { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 512 | item_factory_ = item_factory.Pass(); |
| 513 | } |
| 514 | |
| 515 | void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 516 | scoped_ptr<DownloadFileFactory> file_factory) { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 517 | file_factory_ = file_factory.Pass(); |
| 518 | } |
| 519 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 520 | DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 521 | return file_factory_.get(); |
| 522 | } |
| 523 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 524 | void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 525 | if (!download) |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 526 | return; |
| 527 | |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 528 | int32 download_id = download->GetId(); |
| 529 | if (downloads_.find(download_id) == downloads_.end()) |
| 530 | return; |
| 531 | |
| 532 | delete download; |
| 533 | downloads_.erase(download_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 534 | } |
| 535 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 536 | int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
| 537 | base::Time remove_end) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 538 | int count = 0; |
| 539 | DownloadMap::const_iterator it = downloads_.begin(); |
| 540 | while (it != downloads_.end()) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 541 | DownloadItemImpl* download = it->second; |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 542 | |
| 543 | // Increment done here to protect against invalidation below. |
| 544 | ++it; |
| 545 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 546 | if (download->GetStartTime() >= remove_begin && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 547 | (remove_end.is_null() || download->GetStartTime() < remove_end) && |
[email protected] | fb48765b | 2013-03-24 09:29:19 | [diff] [blame] | 548 | !download->IsInProgress()) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 549 | // Erases the download from downloads_. |
| 550 | download->Remove(); |
| 551 | count++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 553 | } |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 554 | return count; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 555 | } |
| 556 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 557 | int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 558 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 559 | } |
| 560 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 561 | int DownloadManagerImpl::RemoveAllDownloads() { |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 562 | // The null times make the date range unbounded. |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 563 | int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time()); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 564 | RecordClearAllSize(num_deleted); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 565 | return num_deleted; |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 566 | } |
| 567 | |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 568 | void DownloadManagerImpl::DownloadUrl( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 569 | scoped_ptr<DownloadUrlParameters> params) { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 570 | if (params->post_id() >= 0) { |
| 571 | // Check this here so that the traceback is more useful. |
| 572 | DCHECK(params->prefer_cache()); |
| 573 | DCHECK(params->method() == "POST"); |
| 574 | } |
| 575 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 576 | &BeginDownload, base::Passed(¶ms), DownloadId())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 577 | } |
| 578 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 579 | void DownloadManagerImpl::AddObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 580 | observers_.AddObserver(observer); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 581 | } |
| 582 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 583 | void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 584 | observers_.RemoveObserver(observer); |
| 585 | } |
| 586 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 587 | DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 588 | const base::FilePath& current_path, |
| 589 | const base::FilePath& target_path, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 590 | const std::vector<GURL>& url_chain, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 591 | const GURL& referrer_url, |
| 592 | const base::Time& start_time, |
| 593 | const base::Time& end_time, |
| 594 | int64 received_bytes, |
| 595 | int64 total_bytes, |
| 596 | DownloadItem::DownloadState state, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 597 | DownloadDangerType danger_type, |
| 598 | DownloadInterruptReason interrupt_reason, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 599 | bool opened) { |
| 600 | DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
| 601 | this, |
| 602 | GetNextId(), |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 603 | current_path, |
| 604 | target_path, |
| 605 | url_chain, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 606 | referrer_url, |
| 607 | start_time, |
| 608 | end_time, |
| 609 | received_bytes, |
| 610 | total_bytes, |
| 611 | state, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 612 | danger_type, |
| 613 | interrupt_reason, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 614 | opened, |
| 615 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); |
| 616 | DCHECK(!ContainsKey(downloads_, item->GetId())); |
| 617 | downloads_[item->GetId()] = item; |
| 618 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
| 619 | VLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); |
| 620 | return item; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 621 | } |
| 622 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 623 | int DownloadManagerImpl::InProgressCount() const { |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 624 | int count = 0; |
[email protected] | fd6ddb8 | 2012-11-27 00:38:43 | [diff] [blame] | 625 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 626 | it != downloads_.end(); ++it) { |
| 627 | if (it->second->IsInProgress()) |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 628 | ++count; |
| 629 | } |
| 630 | return count; |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 633 | DownloadItem* DownloadManagerImpl::GetDownload(int download_id) { |
| 634 | return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; |
| 635 | } |
| 636 | |
[email protected] | 9f1f409 | 2012-09-10 21:18:04 | [diff] [blame] | 637 | void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { |
| 638 | for (DownloadMap::iterator it = downloads_.begin(); |
| 639 | it != downloads_.end(); ++it) { |
| 640 | downloads->push_back(it->second); |
| 641 | } |
| 642 | } |
| 643 | |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 644 | void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) { |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 645 | int num_unopened = 0; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 646 | for (DownloadMap::iterator it = downloads_.begin(); |
| 647 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 648 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 649 | if (item->IsComplete() && |
| 650 | !item->GetOpened()) |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 651 | ++num_unopened; |
| 652 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 653 | RecordOpensOutstanding(num_unopened); |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 654 | |
[email protected] | 0baf592 | 2013-01-30 13:55:18 | [diff] [blame] | 655 | if (delegate_) |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 656 | delegate_->OpenDownload(download); |
| 657 | } |
| 658 | |
| 659 | void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 660 | if (delegate_) |
| 661 | delegate_->ShowDownloadInShell(download); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 662 | } |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 663 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 664 | } // namespace content |