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