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