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