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