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