[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] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 28 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 29 | #include "content/browser/renderer_host/resource_dispatcher_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] | 8da82ea | 2012-03-11 22:16:52 | [diff] [blame] | 36 | #include "content/public/browser/download_persistent_store_info.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 37 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 38 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 39 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 40 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 41 | #include "content/public/browser/resource_context.h" |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 42 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 43 | #include "net/base/load_flags.h" |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 44 | #include "net/base/upload_data.h" |
[email protected] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 45 | #include "net/url_request/url_request_context.h" |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 46 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 48 | namespace content { |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 49 | namespace { |
| 50 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 51 | void BeginDownload(scoped_ptr<DownloadUrlParameters> params) { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 52 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 53 | // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
| 54 | // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
| 55 | // 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] | 56 | scoped_ptr<net::URLRequest> request( |
| 57 | params->resource_context()->GetRequestContext()->CreateRequest( |
| 58 | params->url(), NULL)); |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 59 | request->set_referrer(params->referrer().url.spec()); |
| 60 | webkit_glue::ConfigureURLRequestForReferrerPolicy( |
| 61 | request.get(), params->referrer().policy); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 62 | request->set_load_flags(request->load_flags() | params->load_flags()); |
| 63 | request->set_method(params->method()); |
| 64 | if (!params->post_body().empty()) |
| 65 | request->AppendBytesToUpload(params->post_body().data(), |
| 66 | params->post_body().size()); |
| 67 | if (params->post_id() >= 0) { |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 68 | // The POST in this case does not have an actual body, and only works |
| 69 | // when retrieving data from cache. This is done because we don't want |
| 70 | // to do a re-POST without user consent, and currently don't have a good |
| 71 | // plan on how to display the UI for that. |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 72 | DCHECK(params->prefer_cache()); |
| 73 | DCHECK(params->method() == "POST"); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 74 | scoped_refptr<net::UploadData> upload_data = new net::UploadData(); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 75 | upload_data->set_identifier(params->post_id()); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 76 | request->set_upload(upload_data); |
| 77 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 78 | for (DownloadUrlParameters::RequestHeadersType::const_iterator iter |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 79 | = params->request_headers_begin(); |
| 80 | iter != params->request_headers_end(); |
| 81 | ++iter) { |
| 82 | request->SetExtraRequestHeaderByName( |
| 83 | iter->first, iter->second, false/*overwrite*/); |
| 84 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 85 | params->resource_dispatcher_host()->BeginDownload( |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 86 | request.Pass(), |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 87 | params->content_initiated(), |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 88 | params->resource_context(), |
| 89 | params->render_process_host_id(), |
| 90 | params->render_view_host_routing_id(), |
| 91 | params->prefer_cache(), |
[email protected] | c873c86 | 2012-10-17 15:32:12 | [diff] [blame] | 92 | params->GetSaveInfo(), |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 93 | params->callback()); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 33d2210 | 2012-01-25 17:46:53 | [diff] [blame] | 96 | class MapValueIteratorAdapter { |
| 97 | public: |
| 98 | explicit MapValueIteratorAdapter( |
| 99 | base::hash_map<int64, DownloadItem*>::const_iterator iter) |
| 100 | : iter_(iter) { |
| 101 | } |
| 102 | ~MapValueIteratorAdapter() {} |
| 103 | |
| 104 | DownloadItem* operator*() { return iter_->second; } |
| 105 | |
| 106 | MapValueIteratorAdapter& operator++() { |
| 107 | ++iter_; |
| 108 | return *this; |
| 109 | } |
| 110 | |
| 111 | bool operator!=(const MapValueIteratorAdapter& that) const { |
| 112 | return iter_ != that.iter_; |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
| 117 | // Allow copy and assign. |
| 118 | }; |
| 119 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 120 | void EnsureNoPendingDownloadJobsOnFile(bool* result) { |
| 121 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 122 | *result = (DownloadFile::GetNumberOfDownloadFiles() == 0); |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 123 | BrowserThread::PostTask( |
| 124 | BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 125 | } |
| 126 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 127 | class DownloadItemFactoryImpl : public DownloadItemFactory { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 128 | public: |
| 129 | DownloadItemFactoryImpl() {} |
| 130 | virtual ~DownloadItemFactoryImpl() {} |
| 131 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 132 | virtual DownloadItemImpl* CreatePersistedItem( |
| 133 | DownloadItemImplDelegate* delegate, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 134 | DownloadId download_id, |
| 135 | const DownloadPersistentStoreInfo& info, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 136 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 137 | return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
| 138 | } |
| 139 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 140 | virtual DownloadItemImpl* CreateActiveItem( |
| 141 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 142 | const DownloadCreateInfo& info, |
[email protected] | 94d841cf | 2012-06-12 20:58:20 | [diff] [blame] | 143 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 144 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 94d841cf | 2012-06-12 20:58:20 | [diff] [blame] | 145 | return new DownloadItemImpl(delegate, info, request_handle.Pass(), |
[email protected] | 66521d0 | 2012-08-14 17:51:54 | [diff] [blame] | 146 | bound_net_log); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 149 | virtual DownloadItemImpl* CreateSavePageItem( |
| 150 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 151 | const FilePath& path, |
| 152 | const GURL& url, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 153 | DownloadId download_id, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 154 | const std::string& mime_type, |
| 155 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 66521d0 | 2012-08-14 17:51:54 | [diff] [blame] | 156 | return new DownloadItemImpl(delegate, path, url, download_id, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 157 | mime_type, bound_net_log); |
| 158 | } |
| 159 | }; |
| 160 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 161 | } // namespace |
| 162 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 163 | DownloadManagerImpl::DownloadManagerImpl( |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 164 | net::NetLog* net_log) |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 165 | : item_factory_(new DownloadItemFactoryImpl()), |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 166 | file_factory_(new DownloadFileFactory()), |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 167 | history_size_(0), |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 168 | shutdown_needed_(false), |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 169 | browser_context_(NULL), |
[email protected] | 33c20cd9 | 2012-06-14 22:02:50 | [diff] [blame] | 170 | delegate_(NULL), |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 171 | net_log_(net_log) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 174 | DownloadManagerImpl::~DownloadManagerImpl() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 175 | DCHECK(!shutdown_needed_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 178 | DownloadId DownloadManagerImpl::GetNextId() { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 179 | DownloadId id; |
| 180 | if (delegate_) |
| 181 | id = delegate_->GetNextId(); |
| 182 | if (!id.IsValid()) { |
| 183 | static int next_id; |
| 184 | id = DownloadId(browser_context_, ++next_id); |
| 185 | } |
| 186 | |
| 187 | return id; |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 190 | void DownloadManagerImpl::DetermineDownloadTarget( |
| 191 | DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| 192 | // Note that this next call relies on |
| 193 | // DownloadItemImplDelegate::DownloadTargetCallback and |
| 194 | // DownloadManagerDelegate::DownloadTargetCallback having the same |
| 195 | // type. If the types ever diverge, gasket code will need to |
| 196 | // be written here. |
| 197 | if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
| 198 | FilePath target_path = item->GetForcedFilePath(); |
| 199 | // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 200 | callback.Run(target_path, |
| 201 | DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 202 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 203 | target_path); |
| 204 | } |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 207 | void DownloadManagerImpl::ReadyForDownloadCompletion( |
| 208 | DownloadItemImpl* item, const base::Closure& complete_callback) { |
| 209 | if (!delegate_ || |
| 210 | delegate_->ShouldCompleteDownload(item, complete_callback)) { |
| 211 | complete_callback.Run(); |
| 212 | } |
| 213 | // Otherwise, the delegate has accepted responsibility to run the |
| 214 | // callback when the download is ready for completion. |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 218 | if (!delegate_) |
| 219 | return false; |
| 220 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 221 | return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 222 | } |
| 223 | |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 224 | bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
| 225 | if (!delegate_) |
| 226 | return true; |
| 227 | |
| 228 | return delegate_->ShouldOpenDownload(item); |
| 229 | } |
| 230 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 231 | void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) { |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 232 | delegate_ = delegate; |
| 233 | } |
| 234 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 235 | DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const { |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 236 | return delegate_; |
| 237 | } |
| 238 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 239 | void DownloadManagerImpl::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 240 | VLOG(20) << __FUNCTION__ << "()" |
| 241 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 242 | if (!shutdown_needed_) |
| 243 | return; |
| 244 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 245 | |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 246 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
[email protected] | fb4f8d90 | 2011-09-16 06:07:08 | [diff] [blame] | 247 | // TODO(benjhayden): Consider clearing observers_. |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 248 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 249 | AssertContainersConsistent(); |
| 250 | |
| 251 | // Go through all downloads in downloads_. Dangerous ones we need to |
| 252 | // remove on disk, and in progress ones we need to cancel. |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 253 | for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 254 | DownloadItemImpl* download = it->second; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 255 | |
| 256 | // Save iterator from potential erases in this set done by called code. |
| 257 | // Iterators after an erasure point are still valid for lists and |
| 258 | // associative containers such as sets. |
| 259 | it++; |
| 260 | |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 261 | if (download->GetSafetyState() == DownloadItem::DANGEROUS && |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 262 | download->IsPartialDownload()) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 263 | // The user hasn't accepted it, so we need to remove it |
| 264 | // from the disk. This may or may not result in it being |
| 265 | // removed from the DownloadManager queues and deleted |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 266 | // (specifically, DownloadManager::DownloadRemoved only |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 267 | // removes and deletes it if it's known to the history service) |
| 268 | // so the only thing we know after calling this function is that |
| 269 | // the download was deleted if-and-only-if it was removed |
| 270 | // from all queues. |
[email protected] | 30307700 | 2011-04-19 23:21:01 | [diff] [blame] | 271 | download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 272 | } else if (download->IsPartialDownload()) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 273 | download->Cancel(false); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 274 | if (delegate_) |
| 275 | delegate_->UpdateItemInPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 279 | // At this point, all dangerous downloads have had their files removed |
| 280 | // and all in progress downloads have been cancelled. We can now delete |
| 281 | // anything left. |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 282 | |
[email protected] | bd466f30 | 2012-10-08 17:11:19 | [diff] [blame] | 283 | // We delete the downloads before clearing the active_downloads_ map |
| 284 | // so that downloads in the COMPLETING_INTERNAL state (which will have |
| 285 | // ignored the Cancel() above) will still show up in active_downloads_ |
| 286 | // in order to satisfy the invariants enforced in AssertStateConsistent(). |
[email protected] | 7745ff0 | 2012-10-01 00:09:24 | [diff] [blame] | 287 | STLDeleteValues(&downloads_); |
[email protected] | bd466f30 | 2012-10-08 17:11:19 | [diff] [blame] | 288 | active_downloads_.clear(); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 289 | downloads_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | |
[email protected] | 41f558fb | 2012-01-09 22:59:58 | [diff] [blame] | 291 | // We'll have nothing more to report to the observers after this point. |
| 292 | observers_.Clear(); |
| 293 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 294 | if (delegate_) |
| 295 | delegate_->Shutdown(); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 296 | delegate_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 299 | bool DownloadManagerImpl::Init(BrowserContext* browser_context) { |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 300 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 302 | shutdown_needed_ = true; |
| 303 | |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 304 | browser_context_ = browser_context; |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 305 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 306 | return true; |
| 307 | } |
| 308 | |
[email protected] | 3d43eef | 2012-10-09 23:17:56 | [diff] [blame] | 309 | DownloadItem* DownloadManagerImpl::StartDownload( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 310 | scoped_ptr<DownloadCreateInfo> info, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 311 | scoped_ptr<ByteStreamReader> stream) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 312 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 313 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 314 | net::BoundNetLog bound_net_log = |
| 315 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 316 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 317 | FilePath default_download_directory; |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 318 | if (delegate_) { |
| 319 | FilePath website_save_directory; // Unused |
| 320 | bool skip_dir_check = false; // Unused |
| 321 | delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 322 | &default_download_directory, &skip_dir_check); |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 325 | // We create the DownloadItem before the DownloadFile because the |
| 326 | // DownloadItem already needs to handle a state in which there is |
| 327 | // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
| 328 | DownloadItemImpl* download = |
| 329 | CreateDownloadItem(info.get(), bound_net_log); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 330 | scoped_ptr<DownloadFile> download_file( |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 331 | file_factory_->CreateFile( |
| 332 | info->save_info.Pass(), default_download_directory, |
| 333 | info->url(), info->referrer_url, |
[email protected] | 3a2b2b5c | 2012-11-01 23:55:30 | [diff] [blame^] | 334 | delegate_->GenerateFileHash(), |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 335 | stream.Pass(), bound_net_log, |
| 336 | download->DestinationObserverAsWeakPtr())); |
| 337 | download->Start(download_file.Pass()); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 338 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 339 | // Delay notification until after Start() so that download_file is bound |
| 340 | // to download and all the usual setters (e.g. Cancel) work. |
| 341 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 342 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 343 | return download; |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 346 | void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 347 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 348 | for (DownloadMap::iterator it = downloads_.begin(); |
| 349 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 350 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 351 | if (item->IsPersisted()) |
| 352 | CheckForFileRemoval(item); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 356 | void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 357 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 358 | if (download_item->IsComplete() && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 359 | !download_item->GetFileExternallyRemoved()) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 360 | BrowserThread::PostTask( |
| 361 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 362 | base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 363 | this, download_item->GetId(), |
[email protected] | fabf36d2 | 2011-10-28 21:50:17 | [diff] [blame] | 364 | download_item->GetTargetFilePath())); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 368 | void DownloadManagerImpl::CheckForFileRemovalOnFileThread( |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 369 | int32 download_id, const FilePath& path) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 370 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 371 | if (!file_util::PathExists(path)) { |
| 372 | BrowserThread::PostTask( |
| 373 | BrowserThread::UI, FROM_HERE, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 374 | base::Bind(&DownloadManagerImpl::OnFileRemovalDetected, |
| 375 | this, |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 376 | download_id)); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 380 | void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 381 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 382 | if (ContainsKey(downloads_, download_id)) |
| 383 | downloads_[download_id]->OnDownloadedFileRemoved(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 384 | } |
| 385 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 386 | BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 387 | return browser_context_; |
| 388 | } |
| 389 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 390 | DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
| 391 | DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 392 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 393 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 394 | if (!info->download_id.IsValid()) |
| 395 | info->download_id = GetNextId(); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 396 | DownloadItemImpl* download = item_factory_->CreateActiveItem( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 397 | this, *info, |
| 398 | scoped_ptr<DownloadRequestHandleInterface>( |
| 399 | new DownloadRequestHandle(info->request_handle)).Pass(), |
[email protected] | 66521d0 | 2012-08-14 17:51:54 | [diff] [blame] | 400 | bound_net_log); |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 401 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 402 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 403 | downloads_[download->GetId()] = download; |
| 404 | DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 405 | active_downloads_[download->GetId()] = download; |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 406 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 407 | return download; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 410 | DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 411 | const FilePath& main_file_path, |
| 412 | const GURL& page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 413 | const std::string& mime_type, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 414 | DownloadItem::Observer* observer) { |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 415 | net::BoundNetLog bound_net_log = |
| 416 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 417 | DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 418 | this, |
| 419 | main_file_path, |
| 420 | page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 421 | GetNextId(), |
| 422 | mime_type, |
| 423 | bound_net_log); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 424 | |
| 425 | download->AddObserver(observer); |
| 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] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 429 | |
[email protected] | 8a5ccda | 2012-08-22 23:55:41 | [diff] [blame] | 430 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame] | 431 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 432 | // Will notify the observer in the callback. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 433 | if (delegate_) |
| 434 | delegate_->AddItemToPersistentStore(download); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 435 | |
| 436 | return download; |
| 437 | } |
| 438 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 439 | void DownloadManagerImpl::AssertStateConsistent( |
| 440 | DownloadItemImpl* download) const { |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 441 | CHECK(ContainsKey(downloads_, download->GetId())); |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 442 | |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 443 | int64 state = download->GetState(); |
[email protected] | 61b75a5 | 2011-08-29 16:34:34 | [diff] [blame] | 444 | base::debug::Alias(&state); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 445 | if (ContainsKey(active_downloads_, download->GetId())) { |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 446 | if (download->IsPersisted()) |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 447 | CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 448 | if (DownloadItem::IN_PROGRESS != download->GetState()) |
| 449 | CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); |
[email protected] | f9a2997f | 2011-09-23 16:54:07 | [diff] [blame] | 450 | } |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 451 | if (DownloadItem::IN_PROGRESS == download->GetState()) |
| 452 | CHECK(ContainsKey(active_downloads_, download->GetId())); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 453 | } |
| 454 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 455 | void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 456 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 457 | DCHECK(download); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 458 | if (delegate_) |
| 459 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 460 | active_downloads_.erase(download->GetId()); |
| 461 | AssertStateConsistent(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 464 | void DownloadManagerImpl::CancelDownload(int32 download_id) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 465 | // A cancel at the right time could remove the download from the |
| 466 | // |active_downloads_| map before we get here. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 467 | if (ContainsKey(active_downloads_, download_id)) |
| 468 | active_downloads_[download_id]->Cancel(true); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 469 | } |
| 470 | |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 471 | void DownloadManagerImpl::UpdatePersistence(DownloadItemImpl* download) { |
| 472 | if (delegate_) |
| 473 | delegate_->UpdateItemInPersistentStore(download); |
| 474 | } |
| 475 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 476 | void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 477 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 478 | |
| 479 | VLOG(20) << __FUNCTION__ << "()" |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 480 | << " download = " << download->DebugString(true); |
| 481 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 482 | RemoveFromActiveList(download); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 483 | // This function is called from the DownloadItem, so DI state |
| 484 | // should already have been updated. |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 485 | AssertStateConsistent(download); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 486 | } |
| 487 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 488 | void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 489 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 490 | DCHECK(download); |
| 491 | |
| 492 | // Clean up will happen when the history system create callback runs if we |
| 493 | // don't have a valid db_handle yet. |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 494 | if (download->IsPersisted()) { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 495 | active_downloads_.erase(download->GetId()); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 496 | if (delegate_) |
| 497 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 501 | void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 502 | scoped_ptr<DownloadItemFactory> item_factory) { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 503 | item_factory_ = item_factory.Pass(); |
| 504 | } |
| 505 | |
| 506 | void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 507 | scoped_ptr<DownloadFileFactory> file_factory) { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 508 | file_factory_ = file_factory.Pass(); |
| 509 | } |
| 510 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 511 | DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 512 | return file_factory_.get(); |
| 513 | } |
| 514 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 515 | int DownloadManagerImpl::RemoveDownloadItems( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 516 | const DownloadItemImplVector& pending_deletes) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 517 | if (pending_deletes.empty()) |
| 518 | return 0; |
| 519 | |
| 520 | // Delete from internal maps. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 521 | for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 522 | it != pending_deletes.end(); |
| 523 | ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 524 | DownloadItemImpl* download = *it; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 525 | DCHECK(download); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 526 | int32 download_id = download->GetId(); |
| 527 | delete download; |
| 528 | downloads_.erase(download_id); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 529 | } |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 530 | NotifyModelChanged(); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 531 | return static_cast<int>(pending_deletes.size()); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 532 | } |
| 533 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 534 | void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 535 | if (!download || |
| 536 | downloads_.find(download->GetId()) == downloads_.end()) |
| 537 | return; |
| 538 | |
| 539 | // TODO(benjhayden,rdsmith): Remove this. |
| 540 | if (!download->IsPersisted()) |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 541 | return; |
| 542 | |
| 543 | // Make history update. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 544 | if (delegate_) |
| 545 | delegate_->RemoveItemFromPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 546 | |
| 547 | // Remove from our tables and delete. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 548 | int downloads_count = |
| 549 | RemoveDownloadItems(DownloadItemImplVector(1, download)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 550 | DCHECK_EQ(1, downloads_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 551 | } |
| 552 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 553 | int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
| 554 | base::Time remove_end) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 555 | if (delegate_) |
| 556 | delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 557 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 558 | DownloadItemImplVector pending_deletes; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 559 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 560 | it != downloads_.end(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 561 | ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 562 | DownloadItemImpl* download = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 563 | if (download->IsPersisted() && |
| 564 | download->GetStartTime() >= remove_begin && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 565 | (remove_end.is_null() || download->GetStartTime() < remove_end) && |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 566 | (download->IsComplete() || download->IsCancelled())) { |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 567 | AssertStateConsistent(download); |
[email protected] | 7e834f0 | 2012-08-09 20:38:56 | [diff] [blame] | 568 | download->NotifyRemoved(); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 569 | pending_deletes.push_back(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 570 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | } |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 572 | return RemoveDownloadItems(pending_deletes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 573 | } |
| 574 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 575 | int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 576 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 577 | } |
| 578 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 579 | int DownloadManagerImpl::RemoveAllDownloads() { |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 580 | // The null times make the date range unbounded. |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 581 | int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time()); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 582 | RecordClearAllSize(num_deleted); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 583 | return num_deleted; |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 584 | } |
| 585 | |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 586 | void DownloadManagerImpl::DownloadUrl( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 587 | scoped_ptr<DownloadUrlParameters> params) { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 588 | if (params->post_id() >= 0) { |
| 589 | // Check this here so that the traceback is more useful. |
| 590 | DCHECK(params->prefer_cache()); |
| 591 | DCHECK(params->method() == "POST"); |
| 592 | } |
| 593 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
[email protected] | c873c86 | 2012-10-17 15:32:12 | [diff] [blame] | 594 | &BeginDownload, base::Passed(params.Pass()))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 595 | } |
| 596 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 597 | void DownloadManagerImpl::AddObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | observers_.AddObserver(observer); |
[email protected] | a1e41e7 | 2012-02-22 17:41:25 | [diff] [blame] | 599 | // TODO: It is the responsibility of the observers to query the |
| 600 | // DownloadManager. Remove the following call from here and update all |
| 601 | // observers. |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 602 | observer->ModelChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 603 | } |
| 604 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 605 | void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | observers_.RemoveObserver(observer); |
| 607 | } |
| 608 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 609 | // Operations posted to us from the history service ---------------------------- |
| 610 | |
| 611 | // The history service has retrieved all download entries. 'entries' contains |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame] | 612 | // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 613 | void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame] | 614 | std::vector<DownloadPersistentStoreInfo>* entries) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 615 | history_size_ = entries->size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 616 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | deb4083 | 2012-02-23 15:41:37 | [diff] [blame] | 617 | int64 db_handle = entries->at(i).db_handle; |
| 618 | base::debug::Alias(&db_handle); |
[email protected] | deb4083 | 2012-02-23 15:41:37 | [diff] [blame] | 619 | |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 620 | net::BoundNetLog bound_net_log = |
| 621 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 622 | DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 623 | this, GetNextId(), entries->at(i), bound_net_log); |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 624 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 625 | downloads_[download->GetId()] = download; |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame] | 626 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 627 | VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 628 | << " download = " << download->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 629 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 630 | NotifyModelChanged(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 631 | CheckForHistoryFilesRemoval(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 632 | } |
| 633 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 634 | void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 635 | int64 db_handle) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 636 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 0634626a | 2012-05-03 19:04:26 | [diff] [blame] | 637 | DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 638 | DCHECK(!download->IsPersisted()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 639 | download->SetDbHandle(db_handle); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 640 | download->SetIsPersisted(); |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 641 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 642 | RecordHistorySize(history_size_); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 643 | // Not counting |download|. |
| 644 | ++history_size_; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 645 | |
| 646 | // Show in the appropriate browser UI. |
| 647 | // This includes buttons to save or cancel, for a dangerous download. |
| 648 | ShowDownloadInBrowser(download); |
| 649 | |
| 650 | // Inform interested objects about the new download. |
| 651 | NotifyModelChanged(); |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 652 | } |
| 653 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 654 | void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
| 655 | int64 db_handle) { |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 656 | // It's valid that we don't find a matching item, i.e. on shutdown. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 657 | if (!ContainsKey(downloads_, download_id)) |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 658 | return; |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 659 | |
| 660 | DownloadItemImpl* item = downloads_[download_id]; |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 661 | AddDownloadItemToHistory(item, db_handle); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 662 | if (item->IsSavePackageDownload()) { |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 663 | OnSavePageItemAddedToPersistentStore(item); |
| 664 | } else { |
| 665 | OnDownloadItemAddedToPersistentStore(item); |
| 666 | } |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 667 | } |
| 668 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 669 | // Once the new DownloadItem has been committed to the persistent store, |
| 670 | // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
| 671 | // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
| 672 | // and notify observers (TODO(benjhayden) observers should be able to see the |
| 673 | // item when it's created so they can observe it directly. Are there any |
| 674 | // clients that actually need to know when the item is added to the history?). |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 675 | void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 676 | DownloadItemImpl* item) { |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 677 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 678 | VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle() |
| 679 | << " download_id = " << item->GetId() |
| 680 | << " download = " << item->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 681 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 682 | // If the download is still in progress, try to complete it. |
| 683 | // |
| 684 | // Otherwise, download has been cancelled or interrupted before we've |
| 685 | // received the DB handle. We post one final message to the history |
| 686 | // service so that it can be properly in sync with the DownloadItem's |
| 687 | // completion status, and also inform any observers so that they get |
| 688 | // more than just the start notification. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 689 | if (item->IsInProgress()) { |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 690 | item->MaybeCompleteDownload(); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 691 | } else { |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 692 | DCHECK(item->IsCancelled()); |
| 693 | active_downloads_.erase(item->GetId()); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 694 | if (delegate_) |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 695 | delegate_->UpdateItemInPersistentStore(item); |
| 696 | item->UpdateObservers(); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 697 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 700 | void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
[email protected] | a29e4f2 | 2012-04-12 21:22:03 | [diff] [blame] | 701 | // The 'contents' may no longer exist if the user closed the contents before |
| 702 | // we get this start completion event. |
[email protected] | a62d4290 | 2012-01-24 17:24:38 | [diff] [blame] | 703 | WebContents* content = download->GetWebContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 704 | |
| 705 | // If the contents no longer exists, we ask the embedder to suggest another |
[email protected] | a29e4f2 | 2012-04-12 21:22:03 | [diff] [blame] | 706 | // contents. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 707 | if (!content && delegate_) |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 708 | content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 709 | |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 710 | if (content && content->GetDelegate()) |
| 711 | content->GetDelegate()->OnStartDownload(content, download); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 712 | } |
| 713 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 714 | int DownloadManagerImpl::InProgressCount() const { |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 715 | // Don't use active_downloads_.count() because Cancel() leaves items in |
| 716 | // active_downloads_ if they haven't made it into the persistent store yet. |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 717 | // Need to actually look at each item's state. |
| 718 | int count = 0; |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 719 | for (DownloadMap::const_iterator it = active_downloads_.begin(); |
| 720 | it != active_downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 721 | DownloadItemImpl* item = it->second; |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 722 | if (item->IsInProgress()) |
| 723 | ++count; |
| 724 | } |
| 725 | return count; |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 726 | } |
| 727 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 728 | void DownloadManagerImpl::NotifyModelChanged() { |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 729 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this)); |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 730 | } |
| 731 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 732 | DownloadItem* DownloadManagerImpl::GetDownload(int download_id) { |
| 733 | return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; |
| 734 | } |
| 735 | |
[email protected] | 9f1f409 | 2012-09-10 21:18:04 | [diff] [blame] | 736 | void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { |
| 737 | for (DownloadMap::iterator it = downloads_.begin(); |
| 738 | it != downloads_.end(); ++it) { |
| 739 | downloads->push_back(it->second); |
| 740 | } |
| 741 | } |
| 742 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 743 | // Confirm that everything in all maps is also in |downloads_|, and that |
| 744 | // everything in |downloads_| is also in some other map. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 745 | void DownloadManagerImpl::AssertContainersConsistent() const { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 746 | #if !defined(NDEBUG) |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 747 | // Turn everything into sets. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 748 | const DownloadMap* input_maps[] = {&active_downloads_}; |
| 749 | DownloadSet active_set; |
| 750 | DownloadSet* all_sets[] = {&active_set}; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 751 | DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets)); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 752 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 753 | for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 754 | it != input_maps[i]->end(); ++it) { |
| 755 | all_sets[i]->insert(&*it->second); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 756 | } |
| 757 | } |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 758 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 759 | DownloadSet all_downloads; |
| 760 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 761 | it != downloads_.end(); ++it) { |
| 762 | all_downloads.insert(it->second); |
| 763 | } |
| 764 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 765 | // Check if each set is fully present in downloads, and create a union. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 766 | for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) { |
| 767 | DownloadSet remainder; |
| 768 | std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
| 769 | std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 770 | all_downloads.begin(), all_downloads.end(), |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 771 | insert_it); |
| 772 | DCHECK(remainder.empty()); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 773 | } |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 774 | #endif |
| 775 | } |
| 776 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 777 | // SavePackage will call SavePageDownloadFinished upon completion/cancellation. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 778 | // The history callback will call OnSavePageItemAddedToPersistentStore. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 779 | // If the download finishes before the history callback, |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 780 | // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring |
| 781 | // that the history event is update regardless of the order in which these two |
| 782 | // events complete. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 783 | // If something removes the download item from the download manager (Remove, |
| 784 | // Shutdown) the result will be that the SavePage system will not be able to |
| 785 | // properly update the download item (which no longer exists) or the download |
| 786 | // history, but the action will complete properly anyway. This may lead to the |
| 787 | // history entry being wrong on a reload of chrome (specifically in the case of |
| 788 | // Initiation -> History Callback -> Removal -> Completion), but there's no way |
| 789 | // to solve that without canceling on Remove (which would then update the DB). |
| 790 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 791 | void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 792 | DownloadItemImpl* item) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 793 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 794 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 795 | // Finalize this download if it finished before the history callback. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 796 | if (!item->IsInProgress()) |
| 797 | SavePageDownloadFinished(item); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 798 | } |
| 799 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 800 | void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) { |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 801 | if (download->IsPersisted()) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 802 | if (delegate_) |
| 803 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 804 | } |
| 805 | } |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 806 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 807 | void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 808 | if (delegate_) |
| 809 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 810 | int num_unopened = 0; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 811 | for (DownloadMap::iterator it = downloads_.begin(); |
| 812 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 813 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 814 | if (item->IsComplete() && |
| 815 | !item->GetOpened()) |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 816 | ++num_unopened; |
| 817 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 818 | RecordOpensOutstanding(num_unopened); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 819 | } |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 820 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 821 | void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 822 | DownloadItemImpl* download) { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 823 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 824 | // download->GetFullPath() is only expected to be meaningful after this |
| 825 | // callback is received. Therefore we can now add the download to a persistent |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 826 | // store. If the rename failed, we processed an interrupt |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 827 | // before we receive the DownloadRenamedToIntermediateName() call. |
| 828 | if (delegate_) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 829 | delegate_->AddItemToPersistentStore(download); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 830 | } else { |
| 831 | OnItemAddedToPersistentStore(download->GetId(), |
| 832 | DownloadItem::kUninitializedHandle); |
| 833 | } |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | void DownloadManagerImpl::DownloadRenamedToFinalName( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 837 | DownloadItemImpl* download) { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 838 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 839 | // If the rename failed, we processed an interrupt before we get here. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 840 | if (delegate_) { |
| 841 | delegate_->UpdatePathForItemInPersistentStore( |
| 842 | download, download->GetFullPath()); |
| 843 | } |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 844 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 845 | |
| 846 | } // namespace content |