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