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