[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" |
| 18 | #include "base/synchronization/lock.h" |
| 19 | #include "base/sys_string_conversions.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 20 | #include "build/build_config.h" |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 21 | #include "content/browser/download/byte_stream.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 22 | #include "content/browser/download/download_create_info.h" |
| 23 | #include "content/browser/download/download_file_manager.h" |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 24 | #include "content/browser/download/download_item_impl.h" |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 25 | #include "content/browser/download/download_stats.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 26 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 27 | #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 28 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 29 | #include "content/public/browser/browser_context.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 30 | #include "content/public/browser/browser_thread.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 31 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 32 | #include "content/public/browser/download_interrupt_reasons.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 33 | #include "content/public/browser/download_manager_delegate.h" |
[email protected] | 8da82ea | 2012-03-11 22:16:52 | [diff] [blame] | 34 | #include "content/public/browser/download_persistent_store_info.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 35 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 36 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 37 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 38 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 39 | #include "content/public/browser/resource_context.h" |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 40 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 41 | #include "net/base/load_flags.h" |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 42 | #include "net/base/upload_data.h" |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 43 | #include "webkit/glue/webkit_glue.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 45 | using content::BrowserThread; |
[email protected] | 98e81406 | 2012-01-27 00:35:42 | [diff] [blame] | 46 | using content::DownloadId; |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 47 | using content::DownloadItem; |
[email protected] | 8da82ea | 2012-03-11 22:16:52 | [diff] [blame] | 48 | using content::DownloadPersistentStoreInfo; |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 49 | using content::ResourceDispatcherHostImpl; |
[email protected] | 2a6bc3e | 2011-12-28 23:51:33 | [diff] [blame] | 50 | using content::WebContents; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 51 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 52 | namespace { |
| 53 | |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 54 | // This is just used to remember which DownloadItems come from SavePage. |
| 55 | class SavePageExternalData : public DownloadItem::ExternalData { |
| 56 | public: |
| 57 | // A spoonful of syntactic sugar. |
| 58 | static bool Get(DownloadItem* item) { |
| 59 | return item->GetExternalData(kKey) != NULL; |
| 60 | } |
| 61 | |
| 62 | explicit SavePageExternalData(DownloadItem* item) { |
| 63 | item->SetExternalData(kKey, this); |
| 64 | } |
| 65 | |
| 66 | virtual ~SavePageExternalData() {} |
| 67 | |
| 68 | private: |
| 69 | static const char kKey[]; |
| 70 | |
| 71 | DISALLOW_COPY_AND_ASSIGN(SavePageExternalData); |
| 72 | }; |
| 73 | |
| 74 | const char SavePageExternalData::kKey[] = "DownloadItem SavePageExternalData"; |
| 75 | |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 76 | void BeginDownload(content::DownloadUrlParameters* params) { |
| 77 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 78 | // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
| 79 | // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
| 80 | // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 81 | scoped_ptr<net::URLRequest> request(new net::URLRequest( |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 82 | params->url(), |
| 83 | NULL, |
| 84 | params->resource_context()->GetRequestContext())); |
[email protected] | f859eba | 2012-05-30 17:22:49 | [diff] [blame] | 85 | request->set_referrer(params->referrer().url.spec()); |
| 86 | webkit_glue::ConfigureURLRequestForReferrerPolicy( |
| 87 | request.get(), params->referrer().policy); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 88 | request->set_load_flags(request->load_flags() | params->load_flags()); |
| 89 | request->set_method(params->method()); |
| 90 | if (!params->post_body().empty()) |
| 91 | request->AppendBytesToUpload(params->post_body().data(), |
| 92 | params->post_body().size()); |
| 93 | if (params->post_id() >= 0) { |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 94 | // The POST in this case does not have an actual body, and only works |
| 95 | // when retrieving data from cache. This is done because we don't want |
| 96 | // to do a re-POST without user consent, and currently don't have a good |
| 97 | // plan on how to display the UI for that. |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 98 | DCHECK(params->prefer_cache()); |
| 99 | DCHECK(params->method() == "POST"); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 100 | scoped_refptr<net::UploadData> upload_data = new net::UploadData(); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 101 | upload_data->set_identifier(params->post_id()); |
[email protected] | 27678b2a | 2012-02-04 22:09:14 | [diff] [blame] | 102 | request->set_upload(upload_data); |
| 103 | } |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 104 | for (content::DownloadUrlParameters::RequestHeadersType::const_iterator iter |
| 105 | = params->request_headers_begin(); |
| 106 | iter != params->request_headers_end(); |
| 107 | ++iter) { |
| 108 | request->SetExtraRequestHeaderByName( |
| 109 | iter->first, iter->second, false/*overwrite*/); |
| 110 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 111 | params->resource_dispatcher_host()->BeginDownload( |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 112 | request.Pass(), |
[email protected] | a53e2f9 | 2012-05-15 15:27:06 | [diff] [blame] | 113 | params->content_initiated(), |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 114 | params->resource_context(), |
| 115 | params->render_process_host_id(), |
| 116 | params->render_view_host_routing_id(), |
| 117 | params->prefer_cache(), |
| 118 | params->save_info(), |
| 119 | params->callback()); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 33d2210 | 2012-01-25 17:46:53 | [diff] [blame] | 122 | class MapValueIteratorAdapter { |
| 123 | public: |
| 124 | explicit MapValueIteratorAdapter( |
| 125 | base::hash_map<int64, DownloadItem*>::const_iterator iter) |
| 126 | : iter_(iter) { |
| 127 | } |
| 128 | ~MapValueIteratorAdapter() {} |
| 129 | |
| 130 | DownloadItem* operator*() { return iter_->second; } |
| 131 | |
| 132 | MapValueIteratorAdapter& operator++() { |
| 133 | ++iter_; |
| 134 | return *this; |
| 135 | } |
| 136 | |
| 137 | bool operator!=(const MapValueIteratorAdapter& that) const { |
| 138 | return iter_ != that.iter_; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
| 143 | // Allow copy and assign. |
| 144 | }; |
| 145 | |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 146 | void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, |
| 147 | bool* result) { |
| 148 | if (dfm->NumberOfActiveDownloads()) |
| 149 | *result = false; |
| 150 | BrowserThread::PostTask( |
| 151 | BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
| 152 | } |
| 153 | |
[email protected] | 0a5c911 | 2012-03-12 17:49:02 | [diff] [blame] | 154 | void EnsureNoPendingDownloadJobsOnIO(bool* result) { |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 155 | scoped_refptr<DownloadFileManager> download_file_manager = |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 156 | ResourceDispatcherHostImpl::Get()->download_file_manager(); |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 157 | BrowserThread::PostTask( |
| 158 | BrowserThread::FILE, FROM_HERE, |
| 159 | base::Bind(&EnsureNoPendingDownloadsOnFile, |
| 160 | download_file_manager, result)); |
| 161 | } |
| 162 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 163 | class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
| 164 | public: |
| 165 | DownloadItemFactoryImpl() {} |
| 166 | virtual ~DownloadItemFactoryImpl() {} |
| 167 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 168 | virtual DownloadItemImpl* CreatePersistedItem( |
| 169 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 170 | content::DownloadId download_id, |
| 171 | const content::DownloadPersistentStoreInfo& info, |
| 172 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 173 | return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
| 174 | } |
| 175 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 176 | virtual DownloadItemImpl* CreateActiveItem( |
| 177 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 178 | const DownloadCreateInfo& info, |
[email protected] | 94d841cf | 2012-06-12 20:58:20 | [diff] [blame] | 179 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 180 | bool is_otr, |
| 181 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
[email protected] | 94d841cf | 2012-06-12 20:58:20 | [diff] [blame] | 182 | return new DownloadItemImpl(delegate, info, request_handle.Pass(), |
| 183 | is_otr, bound_net_log); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 186 | virtual DownloadItemImpl* CreateSavePageItem( |
| 187 | DownloadItemImplDelegate* delegate, |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 188 | const FilePath& path, |
| 189 | const GURL& url, |
| 190 | bool is_otr, |
| 191 | content::DownloadId download_id, |
| 192 | const std::string& mime_type, |
| 193 | const net::BoundNetLog& bound_net_log) OVERRIDE { |
| 194 | return new DownloadItemImpl(delegate, path, url, is_otr, download_id, |
| 195 | mime_type, bound_net_log); |
| 196 | } |
| 197 | }; |
| 198 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 199 | } // namespace |
| 200 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 201 | namespace content { |
| 202 | |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 203 | bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
| 204 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 205 | bool result = true; |
| 206 | BrowserThread::PostTask( |
| 207 | BrowserThread::IO, FROM_HERE, |
[email protected] | 0a5c911 | 2012-03-12 17:49:02 | [diff] [blame] | 208 | base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
[email protected] | 5948e1a | 2012-03-10 00:19:18 | [diff] [blame] | 209 | MessageLoop::current()->Run(); |
| 210 | return result; |
| 211 | } |
| 212 | |
[email protected] | 9990736 | 2012-01-11 05:41:40 | [diff] [blame] | 213 | } // namespace content |
| 214 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 215 | DownloadManagerImpl::DownloadManagerImpl( |
| 216 | DownloadFileManager* file_manager, |
| 217 | scoped_ptr<content::DownloadItemFactory> factory, |
| 218 | net::NetLog* net_log) |
| 219 | : factory_(factory.Pass()), |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 220 | history_size_(0), |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 221 | shutdown_needed_(false), |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 222 | browser_context_(NULL), |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 223 | file_manager_(file_manager), |
[email protected] | 33c20cd9 | 2012-06-14 22:02:50 | [diff] [blame] | 224 | delegate_(NULL), |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 225 | net_log_(net_log) { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 226 | DCHECK(file_manager); |
| 227 | if (!factory_.get()) |
| 228 | factory_.reset(new DownloadItemFactoryImpl()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 231 | DownloadManagerImpl::~DownloadManagerImpl() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 232 | DCHECK(!shutdown_needed_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 235 | DownloadId DownloadManagerImpl::GetNextId() { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 236 | DownloadId id; |
| 237 | if (delegate_) |
| 238 | id = delegate_->GetNextId(); |
| 239 | if (!id.IsValid()) { |
| 240 | static int next_id; |
| 241 | id = DownloadId(browser_context_, ++next_id); |
| 242 | } |
| 243 | |
| 244 | return id; |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 247 | bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 248 | if (!delegate_) |
| 249 | return true; |
| 250 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 251 | return delegate_->ShouldOpenDownload(item); |
| 252 | } |
| 253 | |
| 254 | bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 255 | if (!delegate_) |
| 256 | return false; |
| 257 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 258 | return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 259 | } |
| 260 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 261 | void DownloadManagerImpl::SetDelegate( |
| 262 | content::DownloadManagerDelegate* delegate) { |
| 263 | delegate_ = delegate; |
| 264 | } |
| 265 | |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 266 | content::DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const { |
| 267 | return delegate_; |
| 268 | } |
| 269 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 270 | void DownloadManagerImpl::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 271 | VLOG(20) << __FUNCTION__ << "()" |
| 272 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 273 | if (!shutdown_needed_) |
| 274 | return; |
| 275 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 277 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
[email protected] | fb4f8d90 | 2011-09-16 06:07:08 | [diff] [blame] | 278 | // TODO(benjhayden): Consider clearing observers_. |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 279 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 280 | DCHECK(file_manager_); |
| 281 | BrowserThread::PostTask( |
| 282 | BrowserThread::FILE, FROM_HERE, |
| 283 | base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, |
| 284 | file_manager_, make_scoped_refptr(this))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 285 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 286 | AssertContainersConsistent(); |
| 287 | |
| 288 | // Go through all downloads in downloads_. Dangerous ones we need to |
| 289 | // remove on disk, and in progress ones we need to cancel. |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 290 | for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 291 | DownloadItemImpl* download = it->second; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 292 | |
| 293 | // Save iterator from potential erases in this set done by called code. |
| 294 | // Iterators after an erasure point are still valid for lists and |
| 295 | // associative containers such as sets. |
| 296 | it++; |
| 297 | |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 298 | if (download->GetSafetyState() == DownloadItem::DANGEROUS && |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 299 | download->IsPartialDownload()) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 300 | // The user hasn't accepted it, so we need to remove it |
| 301 | // from the disk. This may or may not result in it being |
| 302 | // removed from the DownloadManager queues and deleted |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 303 | // (specifically, DownloadManager::DownloadRemoved only |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 304 | // removes and deletes it if it's known to the history service) |
| 305 | // so the only thing we know after calling this function is that |
| 306 | // the download was deleted if-and-only-if it was removed |
| 307 | // from all queues. |
[email protected] | 30307700 | 2011-04-19 23:21:01 | [diff] [blame] | 308 | download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 309 | } else if (download->IsPartialDownload()) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 310 | download->Cancel(false); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 311 | if (delegate_) |
| 312 | delegate_->UpdateItemInPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 316 | // At this point, all dangerous downloads have had their files removed |
| 317 | // and all in progress downloads have been cancelled. We can now delete |
| 318 | // anything left. |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 319 | |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 320 | // Copy downloads_ to separate container so as not to set off checks |
| 321 | // in DownloadItem destruction. |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 322 | DownloadMap downloads_to_delete; |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 323 | downloads_to_delete.swap(downloads_); |
| 324 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 325 | active_downloads_.clear(); |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 326 | STLDeleteValues(&downloads_to_delete); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 327 | |
[email protected] | 41f558fb | 2012-01-09 22:59:58 | [diff] [blame] | 328 | // We'll have nothing more to report to the observers after this point. |
| 329 | observers_.Clear(); |
| 330 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 331 | file_manager_ = NULL; |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 332 | if (delegate_) |
| 333 | delegate_->Shutdown(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | } |
| 335 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 336 | void DownloadManagerImpl::GetTemporaryDownloads( |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 337 | const FilePath& dir_path, DownloadVector* result) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 338 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 339 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 340 | for (DownloadMap::iterator it = downloads_.begin(); |
| 341 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 342 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 343 | // TODO(benjhayden): Don't check IsPersisted(). |
| 344 | if (item->IsTemporary() && |
| 345 | item->IsPersisted() && |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 346 | (dir_path.empty() || |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 347 | item->GetTargetFilePath().DirName() == dir_path)) |
| 348 | result->push_back(item); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 349 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 350 | } |
| 351 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 352 | void DownloadManagerImpl::GetAllDownloads( |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 353 | const FilePath& dir_path, DownloadVector* result) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 354 | DCHECK(result); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 355 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 356 | for (DownloadMap::iterator it = downloads_.begin(); |
| 357 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 358 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 359 | // TODO(benjhayden): Don't check IsPersisted(). |
| 360 | if (!item->IsTemporary() && |
| 361 | item->IsPersisted() && |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 362 | (dir_path.empty() || |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 363 | item->GetTargetFilePath().DirName() == dir_path)) |
| 364 | result->push_back(item); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 365 | } |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 366 | } |
| 367 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 368 | void DownloadManagerImpl::SearchDownloads(const string16& query, |
| 369 | DownloadVector* result) { |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 370 | string16 query_lower(base::i18n::ToLower(query)); |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 371 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 372 | for (DownloadMap::iterator it = downloads_.begin(); |
| 373 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 374 | DownloadItemImpl* download_item = it->second; |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 375 | // Display Incognito downloads only in Incognito window, and vice versa. |
| 376 | // The Incognito Downloads page will get the list of non-Incognito downloads |
| 377 | // from its parent profile. |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 378 | // TODO(benjhayden): Don't check IsPersisted(). |
| 379 | if (!download_item->IsTemporary() && |
| 380 | download_item->IsPersisted() && |
| 381 | (browser_context_->IsOffTheRecord() == download_item->IsOtr()) && |
| 382 | download_item->MatchesQuery(query_lower)) { |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 383 | result->push_back(download_item); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 384 | } |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 385 | } |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 388 | bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 389 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 391 | shutdown_needed_ = true; |
| 392 | |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 393 | browser_context_ = browser_context; |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 394 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 398 | // We have received a message from DownloadFileManager about a new download. |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 399 | content::DownloadId DownloadManagerImpl::StartDownload( |
| 400 | scoped_ptr<DownloadCreateInfo> info, |
| 401 | scoped_ptr<content::ByteStreamReader> stream) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 402 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 403 | |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 404 | // |bound_net_log| will be used for logging both the download item's and |
| 405 | // the download file's events. |
| 406 | net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); |
| 407 | |
| 408 | // If info->download_id was unknown on entry to this function, it was |
| 409 | // assigned in CreateDownloadItem. |
| 410 | DownloadId download_id = info->download_id; |
| 411 | |
| 412 | DownloadFileManager::CreateDownloadFileCallback callback( |
| 413 | base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, |
| 414 | this, download_id.local())); |
| 415 | |
| 416 | BrowserThread::PostTask( |
| 417 | BrowserThread::FILE, FROM_HERE, |
| 418 | base::Bind(&DownloadFileManager::CreateDownloadFile, |
| 419 | file_manager_, base::Passed(info.Pass()), |
| 420 | base::Passed(stream.Pass()), |
| 421 | make_scoped_refptr(this), |
| 422 | GenerateFileHash(), bound_net_log, |
| 423 | callback)); |
| 424 | |
| 425 | return download_id; |
| 426 | } |
| 427 | |
| 428 | void DownloadManagerImpl::OnDownloadFileCreated( |
| 429 | int32 download_id, content::DownloadInterruptReason reason) { |
| 430 | if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
[email protected] | b36cad50 | 2012-07-13 23:19:14 | [diff] [blame] | 431 | OnDownloadInterrupted(download_id, reason); |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 432 | // TODO(rdsmith): It makes no sense to continue along the |
| 433 | // regular download path after we've gotten an error. But it's |
| 434 | // the way the code has historically worked, and this allows us |
| 435 | // to get the download persisted and observers of the download manager |
| 436 | // notified, so tests work. When we execute all side effects of cancel |
| 437 | // (including queue removal) immedately rather than waiting for |
| 438 | // persistence we should replace this comment with a "return;". |
| 439 | } |
| 440 | |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 441 | if (!delegate_ || delegate_->ShouldStartDownload(download_id)) |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 442 | RestartDownload(download_id); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 445 | void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 446 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 447 | for (DownloadMap::iterator it = downloads_.begin(); |
| 448 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 449 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 450 | if (item->IsPersisted()) |
| 451 | CheckForFileRemoval(item); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 455 | void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 456 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 457 | if (download_item->IsComplete() && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 458 | !download_item->GetFileExternallyRemoved()) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 459 | BrowserThread::PostTask( |
| 460 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 461 | base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 462 | this, download_item->GetId(), |
[email protected] | fabf36d2 | 2011-10-28 21:50:17 | [diff] [blame] | 463 | download_item->GetTargetFilePath())); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 467 | void DownloadManagerImpl::CheckForFileRemovalOnFileThread( |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 468 | int32 download_id, const FilePath& path) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 469 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 470 | if (!file_util::PathExists(path)) { |
| 471 | BrowserThread::PostTask( |
| 472 | BrowserThread::UI, FROM_HERE, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 473 | base::Bind(&DownloadManagerImpl::OnFileRemovalDetected, |
| 474 | this, |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 475 | download_id)); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 479 | void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 480 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 481 | if (ContainsKey(downloads_, download_id)) |
| 482 | downloads_[download_id]->OnDownloadedFileRemoved(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 443853c6 | 2011-12-22 19:22:41 | [diff] [blame] | 485 | void DownloadManagerImpl::RestartDownload(int32 download_id) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 486 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 487 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 488 | if (!ContainsKey(active_downloads_, download_id)) |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 489 | return; |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 490 | DownloadItemImpl* download = active_downloads_[download_id]; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 491 | |
| 492 | VLOG(20) << __FUNCTION__ << "()" |
| 493 | << " download = " << download->DebugString(true); |
| 494 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 495 | if (download->GetTargetDisposition() == |
| 496 | DownloadItem::TARGET_DISPOSITION_PROMPT) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | // We must ask the user for the place to put the download. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 498 | if (delegate_) { |
[email protected] | 31ee43f | 2012-06-08 23:05:36 | [diff] [blame] | 499 | delegate_->ChooseDownloadPath(download); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 500 | FOR_EACH_OBSERVER(Observer, observers_, |
| 501 | SelectFileDialogDisplayed(this, download_id)); |
| 502 | } else { |
| 503 | FileSelectionCanceled(download_id); |
| 504 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 505 | } else { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 506 | // No prompting for download, just continue with the current target path. |
| 507 | OnTargetPathAvailable(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
[email protected] | 37757c6 | 2011-12-20 20:07:12 | [diff] [blame] | 511 | content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 512 | return browser_context_; |
| 513 | } |
| 514 | |
| 515 | FilePath DownloadManagerImpl::LastDownloadPath() { |
| 516 | return last_download_path_; |
| 517 | } |
| 518 | |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 519 | net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 520 | DownloadCreateInfo* info) { |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 521 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 522 | |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 523 | net::BoundNetLog bound_net_log = |
| 524 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 525 | if (!info->download_id.IsValid()) |
| 526 | info->download_id = GetNextId(); |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 527 | DownloadItemImpl* download = factory_->CreateActiveItem( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 528 | this, *info, |
| 529 | scoped_ptr<DownloadRequestHandleInterface>( |
| 530 | new DownloadRequestHandle(info->request_handle)).Pass(), |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 531 | browser_context_->IsOffTheRecord(), bound_net_log); |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 532 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 533 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 534 | downloads_[download->GetId()] = download; |
| 535 | DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 536 | active_downloads_[download->GetId()] = download; |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame^] | 537 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 538 | |
| 539 | return bound_net_log; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 540 | } |
| 541 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 542 | DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 543 | const FilePath& main_file_path, |
| 544 | const GURL& page_url, |
| 545 | bool is_otr, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 546 | const std::string& mime_type, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 547 | DownloadItem::Observer* observer) { |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 548 | net::BoundNetLog bound_net_log = |
| 549 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 550 | DownloadItemImpl* download = factory_->CreateSavePageItem( |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 551 | this, |
| 552 | main_file_path, |
| 553 | page_url, |
| 554 | is_otr, |
| 555 | GetNextId(), |
| 556 | mime_type, |
| 557 | bound_net_log); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 558 | |
| 559 | download->AddObserver(observer); |
| 560 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 561 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 562 | downloads_[download->GetId()] = download; |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 563 | DCHECK(!SavePageExternalData::Get(download)); |
| 564 | new SavePageExternalData(download); |
| 565 | DCHECK(SavePageExternalData::Get(download)); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 566 | |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame^] | 567 | // TODO(benjhayden): Fire OnDownloadCreated for SavePackage downloads when |
| 568 | // we're comfortable with the user interacting with them. |
| 569 | // FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 570 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 571 | // Will notify the observer in the callback. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 572 | if (delegate_) |
| 573 | delegate_->AddItemToPersistentStore(download); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 574 | |
| 575 | return download; |
| 576 | } |
| 577 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 578 | // The target path for the download item is now valid. We proceed with the |
| 579 | // determination of an intermediate path. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 580 | void DownloadManagerImpl::OnTargetPathAvailable(DownloadItemImpl* download) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 581 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 582 | DCHECK(download); |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 583 | DCHECK(ContainsKey(downloads_, download->GetId())); |
| 584 | DCHECK(ContainsKey(active_downloads_, download->GetId())); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 585 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 586 | VLOG(20) << __FUNCTION__ << "()" |
| 587 | << " download = " << download->DebugString(true); |
| 588 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 589 | // Rename to intermediate name. |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 590 | // TODO(asanka): Skip this rename if download->AllDataSaved() is true. This |
| 591 | // avoids a spurious rename when we can just rename to the final |
| 592 | // filename. Unnecessary renames may cause bugs like |
| 593 | // http://crbug.com/74187. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 594 | FilePath intermediate_path; |
[email protected] | 11be6a68 | 2012-07-02 22:32:16 | [diff] [blame] | 595 | if (delegate_) |
| 596 | intermediate_path = delegate_->GetIntermediatePath(*download); |
| 597 | else |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 598 | intermediate_path = download->GetTargetFilePath(); |
[email protected] | 11be6a68 | 2012-07-02 22:32:16 | [diff] [blame] | 599 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 600 | // We want the intermediate and target paths to refer to the same directory so |
| 601 | // that they are both on the same device and subject to same |
| 602 | // space/permission/availability constraints. |
| 603 | DCHECK(intermediate_path.DirName() == |
| 604 | download->GetTargetFilePath().DirName()); |
[email protected] | 11be6a68 | 2012-07-02 22:32:16 | [diff] [blame] | 605 | download->OnIntermediatePathDetermined(file_manager_, intermediate_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | } |
| 607 | |
[email protected] | 443853c6 | 2011-12-22 19:22:41 | [diff] [blame] | 608 | void DownloadManagerImpl::UpdateDownload(int32 download_id, |
| 609 | int64 bytes_so_far, |
| 610 | int64 bytes_per_sec, |
[email protected] | 0afff03 | 2012-01-06 20:55:00 | [diff] [blame] | 611 | const std::string& hash_state) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 612 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 613 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 614 | if (it != active_downloads_.end()) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 615 | DownloadItemImpl* download = it->second; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 616 | if (download->IsInProgress()) { |
[email protected] | 443853c6 | 2011-12-22 19:22:41 | [diff] [blame] | 617 | download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 618 | if (delegate_) |
| 619 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 620 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 624 | void DownloadManagerImpl::OnResponseCompleted(int32 download_id, |
| 625 | int64 size, |
| 626 | const std::string& hash) { |
[email protected] | 33c6d3f1 | 2011-09-04 00:00:54 | [diff] [blame] | 627 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 628 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 629 | << " size = " << size; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 630 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 631 | |
[email protected] | c4f02c4 | 2011-01-24 21:55:06 | [diff] [blame] | 632 | // If it's not in active_downloads_, that means it was cancelled; just |
| 633 | // ignore the notification. |
| 634 | if (active_downloads_.count(download_id) == 0) |
| 635 | return; |
| 636 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 637 | DownloadItemImpl* download = active_downloads_[download_id]; |
[email protected] | ac4af82f | 2011-11-10 19:09:37 | [diff] [blame] | 638 | download->OnAllDataSaved(size, hash); |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 639 | MaybeCompleteDownload(download); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 640 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 641 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 642 | void DownloadManagerImpl::AssertStateConsistent( |
| 643 | DownloadItemImpl* download) const { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 644 | if (download->GetState() == DownloadItem::REMOVING) { |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 645 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
[email protected] | 0634626a | 2012-05-03 19:04:26 | [diff] [blame] | 646 | DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | |
| 650 | // Should be in downloads_ if we're not REMOVING. |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 651 | CHECK(ContainsKey(downloads_, download->GetId())); |
[email protected] | 7d41311 | 2011-06-16 18:50:17 | [diff] [blame] | 652 | |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 653 | int64 state = download->GetState(); |
[email protected] | 61b75a5 | 2011-08-29 16:34:34 | [diff] [blame] | 654 | base::debug::Alias(&state); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 655 | if (ContainsKey(active_downloads_, download->GetId())) { |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 656 | if (download->IsPersisted()) |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 657 | CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 658 | if (DownloadItem::IN_PROGRESS != download->GetState()) |
| 659 | CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); |
[email protected] | f9a2997f | 2011-09-23 16:54:07 | [diff] [blame] | 660 | } |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 661 | if (DownloadItem::IN_PROGRESS == download->GetState()) |
| 662 | CHECK(ContainsKey(active_downloads_, download->GetId())); |
[email protected] | 5cd11b6e | 2011-06-10 20:30:59 | [diff] [blame] | 663 | } |
| 664 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 665 | bool DownloadManagerImpl::IsDownloadReadyForCompletion( |
| 666 | DownloadItemImpl* download) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 667 | // If we don't have all the data, the download is not ready for |
| 668 | // completion. |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 669 | if (!download->AllDataSaved()) |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 670 | return false; |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 671 | |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 672 | // If the download is dangerous, but not yet validated, it's not ready for |
| 673 | // completion. |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 674 | if (download->GetSafetyState() == DownloadItem::DANGEROUS) |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame] | 675 | return false; |
| 676 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 677 | // If the download isn't active (e.g. has been cancelled) it's not |
| 678 | // ready for completion. |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 679 | if (active_downloads_.count(download->GetId()) == 0) |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 680 | return false; |
| 681 | |
| 682 | // If the download hasn't been inserted into the history system |
| 683 | // (which occurs strictly after file name determination, intermediate |
| 684 | // file rename, and UI display) then it's not ready for completion. |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 685 | if (!download->IsPersisted()) |
[email protected] | 7054b59 | 2011-06-22 14:46:42 | [diff] [blame] | 686 | return false; |
| 687 | |
| 688 | return true; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 689 | } |
| 690 | |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 691 | // When SavePackage downloads MHTML to GData (see |
| 692 | // SavePackageFilePickerChromeOS), GData calls MaybeCompleteDownload() like it |
| 693 | // does for non-SavePackage downloads, but SavePackage downloads never satisfy |
| 694 | // IsDownloadReadyForCompletion(). GDataDownloadObserver manually calls |
| 695 | // DownloadItem::UpdateObservers() when the upload completes so that SavePackage |
| 696 | // notices that the upload has completed and runs its normal Finish() pathway. |
| 697 | // MaybeCompleteDownload() is never the mechanism by which SavePackage completes |
| 698 | // downloads. SavePackage always uses its own Finish() to mark downloads |
| 699 | // complete. |
| 700 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 701 | void DownloadManagerImpl::MaybeCompleteDownload( |
| 702 | DownloadItemImpl* download) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 703 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 704 | VLOG(20) << __FUNCTION__ << "()" << " download = " |
| 705 | << download->DebugString(false); |
| 706 | |
| 707 | if (!IsDownloadReadyForCompletion(download)) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 708 | return; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 709 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 710 | // TODO(rdsmith): DCHECK that we only pass through this point |
| 711 | // once per download. The natural way to do this is by a state |
| 712 | // transition on the DownloadItem. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 713 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 714 | // Confirm we're in the proper set of states to be here; |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 715 | // have all data, have a history handle, (validated or safe). |
| 716 | DCHECK(download->IsInProgress()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 717 | DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 718 | DCHECK(download->AllDataSaved()); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 719 | DCHECK(download->IsPersisted()); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 720 | |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 721 | // Give the delegate a chance to override. It's ok to keep re-setting the |
| 722 | // delegate's |complete_callback| cb as long as there isn't another call-point |
| 723 | // trying to set it to a different cb. TODO(benjhayden): Change the callback |
| 724 | // to point directly to the item instead of |this| when DownloadItem supports |
| 725 | // weak-ptrs. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 726 | if (delegate_ && !delegate_->ShouldCompleteDownload(download, base::Bind( |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 727 | &DownloadManagerImpl::MaybeCompleteDownloadById, |
| 728 | this, download->GetId()))) |
[email protected] | c291865 | 2011-11-01 18:50:23 | [diff] [blame] | 729 | return; |
| 730 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 731 | VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
| 732 | << download->DebugString(false); |
| 733 | |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 734 | if (delegate_) |
| 735 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 736 | download->OnDownloadCompleting(file_manager_); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 737 | } |
| 738 | |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 739 | void DownloadManagerImpl::MaybeCompleteDownloadById(int download_id) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 740 | if (ContainsKey(active_downloads_, download_id)) |
| 741 | MaybeCompleteDownload(active_downloads_[download_id]); |
[email protected] | da4cd426 | 2012-05-18 20:42:35 | [diff] [blame] | 742 | } |
| 743 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 744 | void DownloadManagerImpl::DownloadCompleted(DownloadItemImpl* download) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 745 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | cc3c7c09 | 2011-05-09 18:40:21 | [diff] [blame] | 746 | DCHECK(download); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 747 | if (delegate_) |
| 748 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 749 | active_downloads_.erase(download->GetId()); |
| 750 | AssertStateConsistent(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 751 | } |
| 752 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 753 | void DownloadManagerImpl::CancelDownload(int32 download_id) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 754 | // A cancel at the right time could remove the download from the |
| 755 | // |active_downloads_| map before we get here. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 756 | if (ContainsKey(active_downloads_, download_id)) |
| 757 | active_downloads_[download_id]->Cancel(true); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 758 | } |
| 759 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 760 | void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 761 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | d8472d9 | 2011-08-26 20:15:20 | [diff] [blame] | 762 | |
| 763 | VLOG(20) << __FUNCTION__ << "()" |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 764 | << " download = " << download->DebugString(true); |
| 765 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 766 | RemoveFromActiveList(download); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 767 | // This function is called from the DownloadItem, so DI state |
| 768 | // should already have been updated. |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 769 | AssertStateConsistent(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 770 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 771 | DCHECK(file_manager_); |
| 772 | download->OffThreadCancel(file_manager_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 773 | } |
| 774 | |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 775 | void DownloadManagerImpl::OnDownloadInterrupted( |
| 776 | int32 download_id, |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 777 | content::DownloadInterruptReason reason) { |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 778 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 779 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 780 | if (!ContainsKey(active_downloads_, download_id)) |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 781 | return; |
[email protected] | b36cad50 | 2012-07-13 23:19:14 | [diff] [blame] | 782 | active_downloads_[download_id]->Interrupt(reason); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 783 | } |
| 784 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 785 | void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 786 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 787 | DCHECK(download); |
| 788 | |
| 789 | // Clean up will happen when the history system create callback runs if we |
| 790 | // don't have a valid db_handle yet. |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 791 | if (download->IsPersisted()) { |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 792 | active_downloads_.erase(download->GetId()); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 793 | if (delegate_) |
| 794 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 795 | } |
| 796 | } |
| 797 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 798 | bool DownloadManagerImpl::GenerateFileHash() { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 799 | return delegate_ && delegate_->GenerateFileHash(); |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 800 | } |
| 801 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 802 | int DownloadManagerImpl::RemoveDownloadItems( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 803 | const DownloadItemImplVector& pending_deletes) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 804 | if (pending_deletes.empty()) |
| 805 | return 0; |
| 806 | |
| 807 | // Delete from internal maps. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 808 | for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 809 | it != pending_deletes.end(); |
| 810 | ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 811 | DownloadItemImpl* download = *it; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 812 | DCHECK(download); |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 813 | downloads_.erase(download->GetId()); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | // Tell observers to refresh their views. |
| 817 | NotifyModelChanged(); |
| 818 | |
| 819 | // Delete the download items themselves. |
| 820 | const int num_deleted = static_cast<int>(pending_deletes.size()); |
| 821 | STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 822 | return num_deleted; |
| 823 | } |
| 824 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 825 | void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 826 | if (!download || |
| 827 | downloads_.find(download->GetId()) == downloads_.end()) |
| 828 | return; |
| 829 | |
| 830 | // TODO(benjhayden,rdsmith): Remove this. |
| 831 | if (!download->IsPersisted()) |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 832 | return; |
| 833 | |
| 834 | // Make history update. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 835 | if (delegate_) |
| 836 | delegate_->RemoveItemFromPersistentStore(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 837 | |
| 838 | // Remove from our tables and delete. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 839 | int downloads_count = |
| 840 | RemoveDownloadItems(DownloadItemImplVector(1, download)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 841 | DCHECK_EQ(1, downloads_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 842 | } |
| 843 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 844 | int DownloadManagerImpl::RemoveDownloadsBetween(base::Time remove_begin, |
| 845 | base::Time remove_end) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 846 | if (delegate_) |
| 847 | delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 848 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 849 | // All downloads visible to the user will be in the history, |
| 850 | // so scan that map. |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 851 | DownloadItemImplVector pending_deletes; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 852 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 853 | it != downloads_.end(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 854 | ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 855 | DownloadItemImpl* download = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 856 | if (download->IsPersisted() && |
| 857 | download->GetStartTime() >= remove_begin && |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 858 | (remove_end.is_null() || download->GetStartTime() < remove_end) && |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 859 | (download->IsComplete() || download->IsCancelled())) { |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 860 | AssertStateConsistent(download); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 861 | pending_deletes.push_back(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 862 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | } |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 864 | return RemoveDownloadItems(pending_deletes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 865 | } |
| 866 | |
[email protected] | fd3a8283 | 2012-01-19 20:35:12 | [diff] [blame] | 867 | int DownloadManagerImpl::RemoveDownloads(base::Time remove_begin) { |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 868 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 869 | } |
| 870 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 871 | int DownloadManagerImpl::RemoveAllDownloads() { |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 872 | // The null times make the date range unbounded. |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 873 | int num_deleted = RemoveDownloadsBetween(base::Time(), base::Time()); |
| 874 | download_stats::RecordClearAllSize(num_deleted); |
| 875 | return num_deleted; |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 876 | } |
| 877 | |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 878 | void DownloadManagerImpl::DownloadUrl( |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 879 | scoped_ptr<content::DownloadUrlParameters> params) { |
| 880 | if (params->post_id() >= 0) { |
| 881 | // Check this here so that the traceback is more useful. |
| 882 | DCHECK(params->prefer_cache()); |
| 883 | DCHECK(params->method() == "POST"); |
| 884 | } |
| 885 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 886 | &BeginDownload, base::Owned(params.release()))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 887 | } |
| 888 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 889 | void DownloadManagerImpl::AddObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 890 | observers_.AddObserver(observer); |
[email protected] | a1e41e7 | 2012-02-22 17:41:25 | [diff] [blame] | 891 | // TODO: It is the responsibility of the observers to query the |
| 892 | // DownloadManager. Remove the following call from here and update all |
| 893 | // observers. |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 894 | observer->ModelChanged(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 895 | } |
| 896 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 897 | void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 898 | observers_.RemoveObserver(observer); |
| 899 | } |
| 900 | |
[email protected] | 84d5741 | 2012-03-03 08:59:55 | [diff] [blame] | 901 | void DownloadManagerImpl::FileSelected(const FilePath& path, |
| 902 | int32 download_id) { |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 903 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 904 | DCHECK(!path.empty()); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 905 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 906 | if (!ContainsKey(active_downloads_, download_id)) |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 907 | return; |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 908 | DownloadItemImpl* download = active_downloads_[download_id]; |
| 909 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 910 | VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\"" |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 911 | << " download = " << download->DebugString(true); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 912 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 913 | // Retain the last directory. Exclude temporary downloads since the path |
| 914 | // likely points at the location of a temporary file. |
| 915 | if (!download->IsTemporary()) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 916 | last_download_path_ = path.DirName(); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 917 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 918 | // Make sure the initial file name is set only once. |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 919 | download->OnTargetPathSelected(path); |
| 920 | OnTargetPathAvailable(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 921 | } |
| 922 | |
[email protected] | 84d5741 | 2012-03-03 08:59:55 | [diff] [blame] | 923 | void DownloadManagerImpl::FileSelectionCanceled(int32 download_id) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 924 | // The user didn't pick a place to save the file, so need to cancel the |
| 925 | // download that's already in progress to the temporary location. |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 926 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 927 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 928 | if (!ContainsKey(active_downloads_, download_id)) |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 929 | return; |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 930 | DownloadItemImpl* download = active_downloads_[download_id]; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 931 | |
| 932 | VLOG(20) << __FUNCTION__ << "()" |
| 933 | << " download = " << download->DebugString(true); |
| 934 | |
[email protected] | 8f8bc11 | 2012-02-22 12:36:31 | [diff] [blame] | 935 | download->Cancel(true); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 936 | } |
| 937 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 938 | // Operations posted to us from the history service ---------------------------- |
| 939 | |
| 940 | // The history service has retrieved all download entries. 'entries' contains |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame] | 941 | // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 942 | void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
[email protected] | bb1a421 | 2011-08-22 22:38:25 | [diff] [blame] | 943 | std::vector<DownloadPersistentStoreInfo>* entries) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 944 | history_size_ = entries->size(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 945 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | deb4083 | 2012-02-23 15:41:37 | [diff] [blame] | 946 | int64 db_handle = entries->at(i).db_handle; |
| 947 | base::debug::Alias(&db_handle); |
[email protected] | deb4083 | 2012-02-23 15:41:37 | [diff] [blame] | 948 | |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 949 | net::BoundNetLog bound_net_log = |
| 950 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 951 | DownloadItemImpl* download = factory_->CreatePersistedItem( |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 952 | this, GetNextId(), entries->at(i), bound_net_log); |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 953 | DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 954 | downloads_[download->GetId()] = download; |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame^] | 955 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 956 | VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 957 | << " download = " << download->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 958 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 959 | NotifyModelChanged(); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 960 | CheckForHistoryFilesRemoval(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 961 | } |
| 962 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 963 | void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItemImpl* download, |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 964 | int64 db_handle) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 965 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 0634626a | 2012-05-03 19:04:26 | [diff] [blame] | 966 | DCHECK_NE(DownloadItem::kUninitializedHandle, db_handle); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 967 | DCHECK(!download->IsPersisted()); |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 968 | download->SetDbHandle(db_handle); |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 969 | download->SetIsPersisted(); |
[email protected] | 5bcd73eb | 2011-03-23 21:14:02 | [diff] [blame] | 970 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 971 | download_stats::RecordHistorySize(history_size_); |
| 972 | // Not counting |download|. |
| 973 | ++history_size_; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 974 | |
| 975 | // Show in the appropriate browser UI. |
| 976 | // This includes buttons to save or cancel, for a dangerous download. |
| 977 | ShowDownloadInBrowser(download); |
| 978 | |
| 979 | // Inform interested objects about the new download. |
| 980 | NotifyModelChanged(); |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 981 | } |
| 982 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 983 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 984 | void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
| 985 | int64 db_handle) { |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 986 | // 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] | 987 | if (!ContainsKey(downloads_, download_id)) |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 988 | return; |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 989 | |
| 990 | DownloadItemImpl* item = downloads_[download_id]; |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 991 | AddDownloadItemToHistory(item, db_handle); |
| 992 | if (SavePageExternalData::Get(item)) { |
| 993 | OnSavePageItemAddedToPersistentStore(item); |
| 994 | } else { |
| 995 | OnDownloadItemAddedToPersistentStore(item); |
| 996 | } |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 997 | } |
| 998 | |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 999 | // Once the new DownloadItem has been committed to the persistent store, |
| 1000 | // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
| 1001 | // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
| 1002 | // and notify observers (TODO(benjhayden) observers should be able to see the |
| 1003 | // item when it's created so they can observe it directly. Are there any |
| 1004 | // 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] | 1005 | void DownloadManagerImpl::OnDownloadItemAddedToPersistentStore( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1006 | DownloadItemImpl* item) { |
[email protected] | f9a45b0 | 2011-06-30 23:49:19 | [diff] [blame] | 1007 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1008 | VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << item->GetDbHandle() |
| 1009 | << " download_id = " << item->GetId() |
| 1010 | << " download = " << item->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1011 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 1012 | // If the download is still in progress, try to complete it. |
| 1013 | // |
| 1014 | // Otherwise, download has been cancelled or interrupted before we've |
| 1015 | // received the DB handle. We post one final message to the history |
| 1016 | // service so that it can be properly in sync with the DownloadItem's |
| 1017 | // completion status, and also inform any observers so that they get |
| 1018 | // more than just the start notification. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1019 | if (item->IsInProgress()) { |
| 1020 | MaybeCompleteDownload(item); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 1021 | } else { |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1022 | DCHECK(item->IsCancelled()); |
| 1023 | active_downloads_.erase(item->GetId()); |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1024 | if (delegate_) |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1025 | delegate_->UpdateItemInPersistentStore(item); |
| 1026 | item->UpdateObservers(); |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 1027 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1028 | } |
| 1029 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1030 | void DownloadManagerImpl::ShowDownloadInBrowser(DownloadItemImpl* download) { |
[email protected] | a29e4f2 | 2012-04-12 21:22:03 | [diff] [blame] | 1031 | // The 'contents' may no longer exist if the user closed the contents before |
| 1032 | // we get this start completion event. |
[email protected] | a62d4290 | 2012-01-24 17:24:38 | [diff] [blame] | 1033 | WebContents* content = download->GetWebContents(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 1034 | |
| 1035 | // If the contents no longer exists, we ask the embedder to suggest another |
[email protected] | a29e4f2 | 2012-04-12 21:22:03 | [diff] [blame] | 1036 | // contents. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1037 | if (!content && delegate_) |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 1038 | content = delegate_->GetAlternativeWebContentsToNotifyForDownload(); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1039 | |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 1040 | if (content && content->GetDelegate()) |
| 1041 | content->GetDelegate()->OnStartDownload(content, download); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1042 | } |
| 1043 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1044 | int DownloadManagerImpl::InProgressCount() const { |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 1045 | // Don't use active_downloads_.count() because Cancel() leaves items in |
| 1046 | // active_downloads_ if they haven't made it into the persistent store yet. |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 1047 | // Need to actually look at each item's state. |
| 1048 | int count = 0; |
[email protected] | 550520f | 2012-05-14 20:58:48 | [diff] [blame] | 1049 | for (DownloadMap::const_iterator it = active_downloads_.begin(); |
| 1050 | it != active_downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1051 | DownloadItemImpl* item = it->second; |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 1052 | if (item->IsInProgress()) |
| 1053 | ++count; |
| 1054 | } |
| 1055 | return count; |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1056 | } |
| 1057 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1058 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1059 | void DownloadManagerImpl::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1060 | last_download_path_ = FilePath(); |
[email protected] | eea4662 | 2009-07-15 20:49:38 | [diff] [blame] | 1061 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1062 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1063 | void DownloadManagerImpl::NotifyModelChanged() { |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 1064 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged(this)); |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1065 | } |
| 1066 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1067 | DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) { |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 1068 | DownloadItem* download = GetDownload(download_id); |
| 1069 | return (download && download->IsPersisted()) ? download : NULL; |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1070 | } |
| 1071 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 1072 | DownloadItem* DownloadManagerImpl::GetDownload(int download_id) { |
| 1073 | return ContainsKey(downloads_, download_id) ? downloads_[download_id] : NULL; |
| 1074 | } |
| 1075 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1076 | DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) { |
[email protected] | 5d3e8364 | 2011-12-16 01:14:36 | [diff] [blame] | 1077 | if (ContainsKey(active_downloads_, download_id)) |
| 1078 | return active_downloads_[download_id]; |
| 1079 | return NULL; |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 1080 | } |
| 1081 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1082 | // Confirm that everything in all maps is also in |downloads_|, and that |
| 1083 | // everything in |downloads_| is also in some other map. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1084 | void DownloadManagerImpl::AssertContainersConsistent() const { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1085 | #if !defined(NDEBUG) |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1086 | // Turn everything into sets. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1087 | const DownloadMap* input_maps[] = {&active_downloads_}; |
| 1088 | DownloadSet active_set; |
| 1089 | DownloadSet* all_sets[] = {&active_set}; |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1090 | DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(all_sets)); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1091 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 1092 | for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1093 | it != input_maps[i]->end(); ++it) { |
| 1094 | all_sets[i]->insert(&*it->second); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1095 | } |
| 1096 | } |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1097 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 1098 | DownloadSet all_downloads; |
| 1099 | for (DownloadMap::const_iterator it = downloads_.begin(); |
| 1100 | it != downloads_.end(); ++it) { |
| 1101 | all_downloads.insert(it->second); |
| 1102 | } |
| 1103 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1104 | // Check if each set is fully present in downloads, and create a union. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1105 | for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) { |
| 1106 | DownloadSet remainder; |
| 1107 | std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
| 1108 | std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 1109 | all_downloads.begin(), all_downloads.end(), |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1110 | insert_it); |
| 1111 | DCHECK(remainder.empty()); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1112 | } |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1113 | #endif |
| 1114 | } |
| 1115 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1116 | // SavePackage will call SavePageDownloadFinished upon completion/cancellation. |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 1117 | // The history callback will call OnSavePageItemAddedToPersistentStore. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1118 | // If the download finishes before the history callback, |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 1119 | // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring |
| 1120 | // that the history event is update regardless of the order in which these two |
| 1121 | // events complete. |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1122 | // If something removes the download item from the download manager (Remove, |
| 1123 | // Shutdown) the result will be that the SavePage system will not be able to |
| 1124 | // properly update the download item (which no longer exists) or the download |
| 1125 | // history, but the action will complete properly anyway. This may lead to the |
| 1126 | // history entry being wrong on a reload of chrome (specifically in the case of |
| 1127 | // Initiation -> History Callback -> Removal -> Completion), but there's no way |
| 1128 | // to solve that without canceling on Remove (which would then update the DB). |
| 1129 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1130 | void DownloadManagerImpl::OnSavePageItemAddedToPersistentStore( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1131 | DownloadItemImpl* item) { |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1132 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1133 | |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1134 | // Finalize this download if it finished before the history callback. |
[email protected] | 99229b2 | 2012-07-11 17:38:50 | [diff] [blame] | 1135 | if (!item->IsInProgress()) |
| 1136 | SavePageDownloadFinished(item); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1137 | } |
| 1138 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1139 | void DownloadManagerImpl::SavePageDownloadFinished( |
| 1140 | content::DownloadItem* download) { |
[email protected] | 5009b7a | 2012-02-21 18:47:03 | [diff] [blame] | 1141 | if (download->IsPersisted()) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1142 | if (delegate_) |
| 1143 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1144 | if (download->IsComplete()) |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 1145 | content::NotificationService::current()->Notify( |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1146 | content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 1147 | content::Source<DownloadManager>(this), |
| 1148 | content::Details<DownloadItem>(download)); |
[email protected] | 6d0146c | 2011-08-04 19:13:04 | [diff] [blame] | 1149 | } |
| 1150 | } |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 1151 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1152 | void DownloadManagerImpl::DownloadOpened(DownloadItemImpl* download) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1153 | if (delegate_) |
| 1154 | delegate_->UpdateItemInPersistentStore(download); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 1155 | int num_unopened = 0; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 1156 | for (DownloadMap::iterator it = downloads_.begin(); |
| 1157 | it != downloads_.end(); ++it) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1158 | DownloadItemImpl* item = it->second; |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 1159 | if (item->IsComplete() && |
| 1160 | !item->GetOpened()) |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 1161 | ++num_unopened; |
| 1162 | } |
| 1163 | download_stats::RecordOpensOutstanding(num_unopened); |
| 1164 | } |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 1165 | |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1166 | void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1167 | DownloadItemImpl* download) { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1168 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1169 | // If the rename failed, we receive an OnDownloadInterrupted() call before we |
| 1170 | // receive the DownloadRenamedToIntermediateName() call. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1171 | if (delegate_) |
| 1172 | delegate_->AddItemToPersistentStore(download); |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | void DownloadManagerImpl::DownloadRenamedToFinalName( |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 1176 | DownloadItemImpl* download) { |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1177 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1178 | // If the rename failed, we receive an OnDownloadInterrupted() call before we |
| 1179 | // receive the DownloadRenamedToFinalName() call. |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 1180 | if (delegate_) { |
| 1181 | delegate_->UpdatePathForItemInPersistentStore( |
| 1182 | download, download->GetFullPath()); |
| 1183 | } |
[email protected] | 3d833de | 2012-05-30 23:32:06 | [diff] [blame] | 1184 | } |