[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | cdaa865 | 2008-09-13 02:48:59 | [diff] [blame] | 5 | #include "chrome/browser/download/download_manager.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 7 | #include "base/callback.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/file_util.h" |
| 9 | #include "base/logging.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | 1b5044d | 2009-02-24 00:04:14 | [diff] [blame] | 11 | #include "base/rand_util.h" |
[email protected] | 92926d9 | 2010-09-02 18:35:06 | [diff] [blame] | 12 | #include "base/stl_util-inl.h" |
[email protected] | 2594c2b | 2010-11-08 23:04:26 | [diff] [blame] | 13 | #include "base/stringprintf.h" |
[email protected] | 1b5044d | 2009-02-24 00:04:14 | [diff] [blame] | 14 | #include "base/sys_string_conversions.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/task.h" |
[email protected] | ce7f62e3 | 2010-08-10 23:43:59 | [diff] [blame] | 16 | #include "base/utf_string_conversions.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 17 | #include "build/build_config.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "chrome/browser/browser_list.h" |
| 19 | #include "chrome/browser/browser_process.h" |
[email protected] | facac153 | 2010-10-11 22:59:50 | [diff] [blame] | 20 | #include "chrome/browser/browser_thread.h" |
[email protected] | cd44809 | 2010-12-06 23:49:13 | [diff] [blame] | 21 | #include "chrome/browser/download/download_extensions.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 22 | #include "chrome/browser/download/download_file_manager.h" |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 23 | #include "chrome/browser/download/download_history.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame] | 24 | #include "chrome/browser/download/download_item.h" |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 25 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 26 | #include "chrome/browser/download/download_status_updater.h" |
[email protected] | e9ef0a6 | 2009-08-11 22:50:13 | [diff] [blame] | 27 | #include "chrome/browser/download/download_util.h" |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 28 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | a0835ac | 2010-09-13 19:40:08 | [diff] [blame] | 29 | #include "chrome/browser/history/download_create_info.h" |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 30 | #include "chrome/browser/net/chrome_url_request_context.h" |
[email protected] | 14a000d | 2010-04-29 21:44:24 | [diff] [blame] | 31 | #include "chrome/browser/platform_util.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 32 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 33 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | 6524b5f9 | 2009-01-22 17:48:25 | [diff] [blame] | 34 | #include "chrome/browser/renderer_host/render_view_host.h" |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 35 | #include "chrome/browser/renderer_host/resource_dispatcher_host.h" |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 36 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 37 | #include "chrome/browser/tab_contents/tab_util.h" |
[email protected] | 7b5dc00 | 2010-11-16 23:08:10 | [diff] [blame] | 38 | #include "chrome/browser/ui/browser.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | #include "chrome/common/chrome_paths.h" |
[email protected] | 91e1bd8 | 2009-09-03 22:04:40 | [diff] [blame] | 40 | #include "chrome/common/notification_type.h" |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 41 | #include "googleurl/src/gurl.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 42 | #include "grit/generated_resources.h" |
[email protected] | 21ca982c | 2010-01-26 22:49:55 | [diff] [blame] | 43 | #include "grit/theme_resources.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | #include "net/base/mime_util.h" |
| 45 | #include "net/base/net_util.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 46 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 47 | #include "ui/base/resource/resource_bundle.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 49 | DownloadManager::DownloadManager(DownloadStatusUpdater* status_updater) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | : shutdown_needed_(false), |
| 51 | profile_(NULL), |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 52 | file_manager_(NULL), |
[email protected] | a3d2bc4 | 2010-10-06 14:08:49 | [diff] [blame] | 53 | status_updater_(status_updater->AsWeakPtr()) { |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 54 | if (status_updater_) |
| 55 | status_updater_->AddDelegate(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | DownloadManager::~DownloadManager() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 59 | DCHECK(!shutdown_needed_); |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 60 | if (status_updater_) |
| 61 | status_updater_->RemoveDelegate(this); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void DownloadManager::Shutdown() { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 65 | VLOG(20) << __FUNCTION__ << "()" |
| 66 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 67 | if (!shutdown_needed_) |
| 68 | return; |
| 69 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 71 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
| 72 | |
| 73 | if (file_manager_) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 74 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 75 | NewRunnableMethod(file_manager_, |
| 76 | &DownloadFileManager::OnDownloadManagerShutdown, |
[email protected] | dc7cdcb9 | 2010-12-14 06:40:54 | [diff] [blame] | 77 | make_scoped_refptr(this))); |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 78 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 80 | AssertContainersConsistent(); |
| 81 | |
| 82 | // Go through all downloads in downloads_. Dangerous ones we need to |
| 83 | // remove on disk, and in progress ones we need to cancel. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 84 | for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 85 | DownloadItem* download = *it; |
| 86 | |
| 87 | // Save iterator from potential erases in this set done by called code. |
| 88 | // Iterators after an erasure point are still valid for lists and |
| 89 | // associative containers such as sets. |
| 90 | it++; |
| 91 | |
| 92 | if (download->safety_state() == DownloadItem::DANGEROUS && |
| 93 | (download->state() == DownloadItem::IN_PROGRESS || |
| 94 | download->state() == DownloadItem::COMPLETE)) { |
| 95 | // The user hasn't accepted it, so we need to remove it |
| 96 | // from the disk. This may or may not result in it being |
| 97 | // removed from the DownloadManager queues and deleted |
| 98 | // (specifically, DownloadManager::RemoveDownload only |
| 99 | // removes and deletes it if it's known to the history service) |
| 100 | // so the only thing we know after calling this function is that |
| 101 | // the download was deleted if-and-only-if it was removed |
| 102 | // from all queues. |
| 103 | download->Remove(true); |
| 104 | } else if (download->state() == DownloadItem::IN_PROGRESS) { |
| 105 | download->Cancel(false); |
| 106 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 110 | // At this point, all dangerous downloads have had their files removed |
| 111 | // and all in progress downloads have been cancelled. We can now delete |
| 112 | // anything left. |
| 113 | STLDeleteElements(&downloads_); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 114 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 115 | // And clear all non-owning containers. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | in_progress_.clear(); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 117 | active_downloads_.clear(); |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 118 | #if !defined(NDEBUG) |
| 119 | save_page_as_downloads_.clear(); |
| 120 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | |
| 122 | file_manager_ = NULL; |
| 123 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | // Make sure the save as dialog doesn't notify us back if we're gone before |
| 125 | // it returns. |
| 126 | if (select_file_dialog_.get()) |
| 127 | select_file_dialog_->ListenerDestroyed(); |
| 128 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 129 | download_history_.reset(); |
[email protected] | 68a49e5 | 2011-01-28 10:39:51 | [diff] [blame] | 130 | download_prefs_.reset(); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 131 | |
[email protected] | 2c93f6f | 2010-11-08 20:41:17 | [diff] [blame] | 132 | request_context_getter_ = NULL; |
| 133 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | shutdown_needed_ = false; |
| 135 | } |
| 136 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 137 | void DownloadManager::GetTemporaryDownloads( |
| 138 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 139 | DCHECK(result); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 140 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 141 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 142 | it != history_downloads_.end(); ++it) { |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 143 | if (it->second->is_temporary() && |
| 144 | it->second->full_path().DirName() == dir_path) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 145 | result->push_back(it->second); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 146 | } |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 149 | void DownloadManager::GetAllDownloads( |
| 150 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 151 | DCHECK(result); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 152 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 153 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 154 | it != history_downloads_.end(); ++it) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 155 | if (!it->second->is_temporary() && |
| 156 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 157 | result->push_back(it->second); |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 158 | } |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 161 | void DownloadManager::GetCurrentDownloads( |
| 162 | const FilePath& dir_path, std::vector<DownloadItem*>* result) { |
| 163 | DCHECK(result); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 164 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 165 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 166 | it != history_downloads_.end(); ++it) { |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 167 | if (!it->second->is_temporary() && |
| 168 | (it->second->state() == DownloadItem::IN_PROGRESS || |
| 169 | it->second->safety_state() == DownloadItem::DANGEROUS) && |
| 170 | (dir_path.empty() || it->second->full_path().DirName() == dir_path)) |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 171 | result->push_back(it->second); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 172 | } |
[email protected] | f7e9fd6 | 2010-09-28 15:45:06 | [diff] [blame] | 173 | |
| 174 | // If we have a parent profile, let it add its downloads to the results. |
| 175 | Profile* original_profile = profile_->GetOriginalProfile(); |
| 176 | if (original_profile != profile_) |
| 177 | original_profile->GetDownloadManager()->GetCurrentDownloads(dir_path, |
| 178 | result); |
[email protected] | c4a530b | 2010-03-08 17:33:03 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 181 | void DownloadManager::SearchDownloads(const string16& query, |
| 182 | std::vector<DownloadItem*>* result) { |
| 183 | DCHECK(result); |
| 184 | |
| 185 | string16 query_lower(l10n_util::ToLower(query)); |
| 186 | |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 187 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 188 | it != history_downloads_.end(); ++it) { |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 189 | DownloadItem* download_item = it->second; |
| 190 | |
| 191 | if (download_item->is_temporary() || download_item->is_extension_install()) |
| 192 | continue; |
| 193 | |
| 194 | // Display Incognito downloads only in Incognito window, and vice versa. |
| 195 | // The Incognito Downloads page will get the list of non-Incognito downloads |
| 196 | // from its parent profile. |
| 197 | if (profile_->IsOffTheRecord() != download_item->is_otr()) |
| 198 | continue; |
| 199 | |
| 200 | if (download_item->MatchesQuery(query_lower)) |
| 201 | result->push_back(download_item); |
| 202 | } |
| 203 | |
| 204 | // If we have a parent profile, let it add its downloads to the results. |
| 205 | Profile* original_profile = profile_->GetOriginalProfile(); |
| 206 | if (original_profile != profile_) |
| 207 | original_profile->GetDownloadManager()->SearchDownloads(query, result); |
| 208 | } |
| 209 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | // Query the history service for information about all persisted downloads. |
| 211 | bool DownloadManager::Init(Profile* profile) { |
| 212 | DCHECK(profile); |
| 213 | DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 214 | shutdown_needed_ = true; |
| 215 | |
| 216 | profile_ = profile; |
[email protected] | be180c80 | 2009-10-23 06:33:31 | [diff] [blame] | 217 | request_context_getter_ = profile_->GetRequestContext(); |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 218 | download_history_.reset(new DownloadHistory(profile)); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 219 | download_history_->Load( |
| 220 | NewCallback(this, &DownloadManager::OnQueryDownloadEntriesComplete)); |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 221 | |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 222 | download_prefs_.reset(new DownloadPrefs(profile_->GetPrefs())); |
| 223 | |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 224 | // In test mode, there may be no ResourceDispatcherHost. In this case it's |
| 225 | // safe to avoid setting |file_manager_| because we only call a small set of |
| 226 | // functions, none of which need it. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 227 | ResourceDispatcherHost* rdh = g_browser_process->resource_dispatcher_host(); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 228 | if (rdh) { |
| 229 | file_manager_ = rdh->download_file_manager(); |
| 230 | DCHECK(file_manager_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 233 | other_download_manager_observer_.reset( |
| 234 | new OtherDownloadManagerObserver(this)); |
| 235 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 236 | return true; |
| 237 | } |
| 238 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 239 | // We have received a message from DownloadFileManager about a new download. We |
| 240 | // create a download item and store it in our download map, and inform the |
| 241 | // history system of a new download. Since this method can be called while the |
| 242 | // history service thread is still reading the persistent state, we do not |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 243 | // insert the new DownloadItem into 'history_downloads_' or inform our |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 244 | // observers at this point. OnCreateDownloadEntryComplete() handles that |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 245 | // finalization of the the download creation as a callback from the |
| 246 | // history thread. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | void DownloadManager::StartDownload(DownloadCreateInfo* info) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 248 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 249 | DCHECK(info); |
| 250 | |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 251 | // Check whether this download is for an extension install or not. |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 252 | // Allow extensions to be explicitly saved. |
| 253 | if (!info->prompt_user_for_save_location) { |
[email protected] | a60c8ae | 2009-12-25 06:50:57 | [diff] [blame] | 254 | if (UserScript::HasUserScriptFileExtension(info->url) || |
| 255 | info->mime_type == Extension::kMimeType) |
| 256 | info->is_extension_install = true; |
| 257 | } |
| 258 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 259 | if (info->save_info.file_path.empty()) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 260 | FilePath generated_name; |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 261 | download_util::GenerateFileNameFromInfo(info, &generated_name); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 262 | |
| 263 | // Freeze the user's preference for showing a Save As dialog. We're going |
| 264 | // to bounce around a bunch of threads and we don't want to worry about race |
| 265 | // conditions where the user changes this pref out from under us. |
[email protected] | 0e9b607 | 2011-02-17 12:42:05 | [diff] [blame] | 266 | if (download_prefs_->PromptForDownload()) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 267 | // But ignore the user's preference for the following scenarios: |
| 268 | // 1) Extension installation. Note that we only care here about the case |
| 269 | // where an extension is installed, not when one is downloaded with |
| 270 | // "save as...". |
| 271 | // 2) Filetypes marked "always open." If the user just wants this file |
| 272 | // opened, don't bother asking where to keep it. |
| 273 | if (!info->is_extension_install && |
| 274 | !ShouldOpenFileBasedOnExtension(generated_name)) |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 275 | info->prompt_user_for_save_location = true; |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 276 | } |
[email protected] | 14e0a10 | 2011-02-22 14:04:01 | [diff] [blame] | 277 | if (download_prefs_->IsDownloadPathManaged()) { |
| 278 | info->prompt_user_for_save_location = false; |
| 279 | } |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 280 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 281 | // Determine the proper path for a download, by either one of the following: |
| 282 | // 1) using the default download directory. |
| 283 | // 2) prompting the user. |
[email protected] | ccea03c | 2010-12-17 03:31:50 | [diff] [blame] | 284 | if (info->prompt_user_for_save_location && !last_download_path_.empty()) { |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 285 | info->suggested_path = last_download_path_; |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 286 | } else { |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 287 | info->suggested_path = download_prefs_->download_path(); |
[email protected] | 80dc361 | 2010-07-27 19:35:08 | [diff] [blame] | 288 | } |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 289 | info->suggested_path = info->suggested_path.Append(generated_name); |
| 290 | } else { |
| 291 | info->suggested_path = info->save_info.file_path; |
| 292 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 293 | |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 294 | if (!info->prompt_user_for_save_location && |
| 295 | info->save_info.file_path.empty()) { |
[email protected] | b580f25e | 2011-01-29 05:06:17 | [diff] [blame] | 296 | info->is_dangerous = download_util::IsDangerous( |
| 297 | info, profile(), ShouldOpenFileBasedOnExtension(info->suggested_path)); |
[email protected] | e9ebf3fc | 2008-10-17 22:06:58 | [diff] [blame] | 298 | } |
| 299 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 300 | // We need to move over to the download thread because we don't want to stat |
| 301 | // the suggested path on the UI thread. |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 302 | // We can only access preferences on the UI thread, so check the download path |
| 303 | // now and pass the value to the FILE thread. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 304 | BrowserThread::PostTask( |
| 305 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 306 | NewRunnableMethod( |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 307 | this, |
| 308 | &DownloadManager::CheckIfSuggestedPathExists, |
| 309 | info, |
| 310 | download_prefs()->download_path())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 313 | void DownloadManager::CheckIfSuggestedPathExists(DownloadCreateInfo* info, |
| 314 | const FilePath& default_path) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 315 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 316 | DCHECK(info); |
| 317 | |
[email protected] | 5a3b97e | 2010-10-05 09:49:11 | [diff] [blame] | 318 | // Make sure the default download directory exists. |
| 319 | // TODO(phajdan.jr): only create the directory when we're sure the user |
| 320 | // is going to save there and not to another directory of his choice. |
| 321 | file_util::CreateDirectory(default_path); |
| 322 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 323 | // Check writability of the suggested path. If we can't write to it, default |
| 324 | // to the user's "My Documents" directory. We'll prompt them in this case. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 325 | FilePath dir = info->suggested_path.DirName(); |
| 326 | FilePath filename = info->suggested_path.BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 327 | if (!file_util::PathIsWritable(dir)) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 328 | VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 329 | info->prompt_user_for_save_location = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 331 | info->suggested_path = info->suggested_path.Append(filename); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 334 | // If the download is deemed dangerous, we'll use a temporary name for it. |
[email protected] | e9ebf3fc | 2008-10-17 22:06:58 | [diff] [blame] | 335 | if (info->is_dangerous) { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 336 | info->original_name = FilePath(info->suggested_path).BaseName(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 337 | // Create a temporary file to hold the file until the user approves its |
| 338 | // download. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 339 | FilePath::StringType file_name; |
| 340 | FilePath path; |
[email protected] | 463e1b43 | 2011-01-21 22:05:51 | [diff] [blame] | 341 | #if defined(OS_WIN) |
| 342 | string16 unconfirmed_prefix = |
| 343 | l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 344 | #else |
| 345 | std::string unconfirmed_prefix = |
| 346 | l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); |
| 347 | #endif |
| 348 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 349 | while (path.empty()) { |
[email protected] | 2594c2b | 2010-11-08 23:04:26 | [diff] [blame] | 350 | base::SStringPrintf( |
| 351 | &file_name, |
[email protected] | 463e1b43 | 2011-01-21 22:05:51 | [diff] [blame] | 352 | unconfirmed_prefix.append( |
| 353 | FILE_PATH_LITERAL(" %d.crdownload")).c_str(), |
[email protected] | 2594c2b | 2010-11-08 23:04:26 | [diff] [blame] | 354 | base::RandInt(0, 100000)); |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 355 | path = dir.Append(file_name); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 356 | if (file_util::PathExists(path)) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 357 | path = FilePath(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 358 | } |
| 359 | info->suggested_path = path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 360 | } else { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 361 | // Do not add the path uniquifier if we are saving to a specific path as in |
| 362 | // the drag-out case. |
| 363 | if (info->save_info.file_path.empty()) { |
| 364 | info->path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( |
| 365 | info->suggested_path); |
| 366 | } |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 367 | // We know the final path, build it if necessary. |
| 368 | if (info->path_uniquifier > 0) { |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 369 | download_util::AppendNumberToPath(&(info->suggested_path), |
| 370 | info->path_uniquifier); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 371 | // Setting path_uniquifier to 0 to make sure we don't try to unique it |
| 372 | // later on. |
| 373 | info->path_uniquifier = 0; |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 374 | } else if (info->path_uniquifier == -1) { |
| 375 | // We failed to find a unique path. We have to prompt the user. |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 376 | VLOG(1) << "Unable to find a unique path for suggested path \"" |
| 377 | << info->suggested_path.value() << "\""; |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 378 | info->prompt_user_for_save_location = true; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 379 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 380 | } |
| 381 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 382 | // Create an empty file at the suggested path so that we don't allocate the |
| 383 | // same "non-existant" path to multiple downloads. |
| 384 | // See: http://code.google.com/p/chromium/issues/detail?id=3662 |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 385 | if (!info->prompt_user_for_save_location && |
| 386 | info->save_info.file_path.empty()) { |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 387 | if (info->is_dangerous) |
| 388 | file_util::WriteFile(info->suggested_path, "", 0); |
| 389 | else |
| 390 | file_util::WriteFile(download_util::GetCrDownloadPath( |
| 391 | info->suggested_path), "", 0); |
[email protected] | 7d3851d8 | 2008-12-12 03:26:07 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 394 | BrowserThread::PostTask( |
| 395 | BrowserThread::UI, FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | NewRunnableMethod(this, |
| 397 | &DownloadManager::OnPathExistenceAvailable, |
| 398 | info)); |
| 399 | } |
| 400 | |
| 401 | void DownloadManager::OnPathExistenceAvailable(DownloadCreateInfo* info) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 402 | VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 403 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | DCHECK(info); |
| 405 | |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 406 | if (info->prompt_user_for_save_location) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | // We must ask the user for the place to put the download. |
| 408 | if (!select_file_dialog_.get()) |
| 409 | select_file_dialog_ = SelectFileDialog::Create(this); |
| 410 | |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 411 | TabContents* contents = tab_util::GetTabContentsByID(info->child_id, |
| 412 | info->render_view_id); |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 413 | SelectFileDialog::FileTypeInfo file_type_info; |
| 414 | file_type_info.extensions.resize(1); |
| 415 | file_type_info.extensions[0].push_back(info->suggested_path.Extension()); |
[email protected] | 15bc805 | 2009-04-17 19:57:24 | [diff] [blame] | 416 | if (!file_type_info.extensions[0][0].empty()) |
| 417 | file_type_info.extensions[0][0].erase(0, 1); // drop the . |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 418 | file_type_info.include_all_files = true; |
[email protected] | 076700e6 | 2009-04-01 18:41:23 | [diff] [blame] | 419 | gfx::NativeWindow owning_window = |
| 420 | contents ? platform_util::GetTopLevel(contents->GetNativeView()) : NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 422 | string16(), |
| 423 | info->suggested_path, |
[email protected] | b949f111 | 2009-04-12 20:03:08 | [diff] [blame] | 424 | &file_type_info, 0, FILE_PATH_LITERAL(""), |
[email protected] | 0f44d3e | 2009-03-12 23:36:30 | [diff] [blame] | 425 | owning_window, info); |
[email protected] | ac167ce | 2010-11-24 17:59:48 | [diff] [blame] | 426 | FOR_EACH_OBSERVER(Observer, observers_, SelectFileDialogDisplayed()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 427 | } else { |
| 428 | // No prompting for download, just continue with the suggested name. |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 429 | AttachDownloadItem(info, info->suggested_path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 433 | void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 434 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 435 | |
| 436 | DownloadItem* download = new DownloadItem(this, *info, |
| 437 | profile_->IsOffTheRecord()); |
| 438 | DCHECK(!ContainsKey(in_progress_, info->download_id)); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 439 | DCHECK(!ContainsKey(active_downloads_, info->download_id)); |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 440 | downloads_.insert(download); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 441 | active_downloads_[info->download_id] = download; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void DownloadManager::AttachDownloadItem(DownloadCreateInfo* info, |
[email protected] | a850ba4 | 2010-09-10 22:00:30 | [diff] [blame] | 445 | const FilePath& target_path) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 446 | VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
| 447 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 448 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 449 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | scoped_ptr<DownloadCreateInfo> infop(info); |
| 451 | info->path = target_path; |
| 452 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 453 | // NOTE(ahendrickson) Eventually |active_downloads_| will replace |
| 454 | // |in_progress_|, but we don't want to change the semantics yet. |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 455 | DCHECK(!ContainsKey(in_progress_, info->download_id)); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 456 | DCHECK(ContainsKey(active_downloads_, info->download_id)); |
| 457 | DownloadItem* download = active_downloads_[info->download_id]; |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 458 | DCHECK(download != NULL); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 459 | DCHECK(ContainsKey(downloads_, download)); |
| 460 | |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 461 | download->SetFileCheckResults(info->path, |
| 462 | info->is_dangerous, |
| 463 | info->path_uniquifier, |
| 464 | info->prompt_user_for_save_location, |
| 465 | info->is_extension_install, |
| 466 | info->original_name); |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 467 | in_progress_[info->download_id] = download; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 468 | UpdateAppIcon(); // Reflect entry into in_progress_. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 470 | // Rename to intermediate name. |
| 471 | if (info->is_dangerous) { |
| 472 | // The download is not safe. We can now rename the file to its |
| 473 | // tentative name using OnFinalDownloadName (the actual final |
| 474 | // name after user confirmation will be set in |
| 475 | // ProceedWithFinishedDangerousDownload). |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 476 | BrowserThread::PostTask( |
| 477 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 478 | NewRunnableMethod( |
[email protected] | 0d7e79fa | 2010-10-08 23:35:47 | [diff] [blame] | 479 | file_manager_, &DownloadFileManager::OnFinalDownloadName, |
[email protected] | 5b1c5dfa | 2011-02-15 15:51:44 | [diff] [blame] | 480 | download->id(), target_path, make_scoped_refptr(this))); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 481 | } else { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 482 | // The download is a safe download. We need to |
| 483 | // rename it to its intermediate '.crdownload' path. The final |
| 484 | // name after user confirmation will be set from |
| 485 | // DownloadItem::OnSafeDownloadFinished. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 486 | FilePath download_path = download_util::GetCrDownloadPath(target_path); |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 487 | BrowserThread::PostTask( |
| 488 | BrowserThread::FILE, FROM_HERE, |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 489 | NewRunnableMethod( |
[email protected] | 0d7e79fa | 2010-10-08 23:35:47 | [diff] [blame] | 490 | file_manager_, &DownloadFileManager::OnIntermediateDownloadName, |
[email protected] | dc7cdcb9 | 2010-12-14 06:40:54 | [diff] [blame] | 491 | download->id(), download_path, make_scoped_refptr(this))); |
[email protected] | 44b94b8 | 2010-11-08 20:49:18 | [diff] [blame] | 492 | download->Rename(download_path); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 495 | download_history_->AddEntry(*info, download, |
| 496 | NewCallback(this, &DownloadManager::OnCreateDownloadEntryComplete)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | } |
| 498 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 499 | void DownloadManager::UpdateDownload(int32 download_id, int64 size) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 500 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 501 | DownloadMap::iterator it = active_downloads_.find(download_id); |
| 502 | if (it != active_downloads_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | DownloadItem* download = it->second; |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 504 | if (download->state() == DownloadItem::IN_PROGRESS) { |
| 505 | download->Update(size); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 506 | UpdateAppIcon(); // Reflect size updates. |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 507 | download_history_->UpdateEntry(download); |
| 508 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
[email protected] | a850ba4 | 2010-09-10 22:00:30 | [diff] [blame] | 512 | void DownloadManager::OnAllDataSaved(int32 download_id, int64 size) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 513 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 514 | << " size = " << size; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 515 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 516 | |
[email protected] | c4f02c4 | 2011-01-24 21:55:06 | [diff] [blame] | 517 | // If it's not in active_downloads_, that means it was cancelled; just |
| 518 | // ignore the notification. |
| 519 | if (active_downloads_.count(download_id) == 0) |
| 520 | return; |
| 521 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 522 | DownloadItem* download = active_downloads_[download_id]; |
[email protected] | a850ba4 | 2010-09-10 22:00:30 | [diff] [blame] | 523 | download->OnAllDataSaved(size); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 524 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 525 | MaybeCompleteDownload(download); |
| 526 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 527 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 528 | bool DownloadManager::IsDownloadReadyForCompletion(DownloadItem* download) { |
| 529 | // If we don't have all the data, the download is not ready for |
| 530 | // completion. |
| 531 | if (!download->all_data_saved()) |
| 532 | return false; |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 533 | |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 534 | // If the download is dangerous, but not yet validated, it's not ready for |
| 535 | // completion. |
| 536 | if (download->safety_state() == DownloadItem::DANGEROUS) |
| 537 | return false; |
| 538 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 539 | // If the download isn't active (e.g. has been cancelled) it's not |
| 540 | // ready for completion. |
| 541 | if (active_downloads_.count(download->id()) == 0) |
| 542 | return false; |
| 543 | |
| 544 | // If the download hasn't been inserted into the history system |
| 545 | // (which occurs strictly after file name determination, intermediate |
| 546 | // file rename, and UI display) then it's not ready for completion. |
| 547 | return (download->db_handle() != DownloadHistory::kUninitializedHandle); |
| 548 | } |
| 549 | |
| 550 | void DownloadManager::MaybeCompleteDownload(DownloadItem* download) { |
| 551 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 552 | VLOG(20) << __FUNCTION__ << "()" << " download = " |
| 553 | << download->DebugString(false); |
| 554 | |
| 555 | if (!IsDownloadReadyForCompletion(download)) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 556 | return; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 557 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 558 | // TODO(rdsmith): DCHECK that we only pass through this point |
| 559 | // once per download. The natural way to do this is by a state |
| 560 | // transition on the DownloadItem. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 561 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 562 | // Confirm we're in the proper set of states to be here; |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 563 | // in in_progress_, have all data, have a history handle, (validated or safe). |
| 564 | DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state()); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 565 | DCHECK_EQ(1u, in_progress_.count(download->id())); |
| 566 | DCHECK(download->all_data_saved()); |
| 567 | DCHECK(download->db_handle() != DownloadHistory::kUninitializedHandle); |
| 568 | DCHECK_EQ(1u, history_downloads_.count(download->db_handle())); |
| 569 | |
| 570 | VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
| 571 | << download->DebugString(false); |
| 572 | |
| 573 | // Remove the id from in_progress |
| 574 | in_progress_.erase(download->id()); |
| 575 | UpdateAppIcon(); // Reflect removal from in_progress_. |
| 576 | |
| 577 | // Final update of download item and history. |
| 578 | download->MarkAsComplete(); |
| 579 | download_history_->UpdateEntry(download); |
| 580 | |
| 581 | switch (download->safety_state()) { |
| 582 | case DownloadItem::DANGEROUS: |
| 583 | // If this a dangerous download not yet validated by the user, don't do |
| 584 | // anything. When the user notifies us, it will trigger a call to |
| 585 | // ProceedWithFinishedDangerousDownload. |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 586 | NOTREACHED(); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 587 | return; |
| 588 | case DownloadItem::DANGEROUS_BUT_VALIDATED: |
| 589 | // The dangerous download has been validated by the user. We first |
| 590 | // need to rename the downloaded file from its temporary name to |
| 591 | // its final name. We will continue the download processing in the |
| 592 | // callback. |
| 593 | BrowserThread::PostTask( |
| 594 | BrowserThread::FILE, FROM_HERE, |
| 595 | NewRunnableMethod( |
| 596 | this, &DownloadManager::ProceedWithFinishedDangerousDownload, |
| 597 | download->db_handle(), |
| 598 | download->full_path(), download->target_name())); |
| 599 | return; |
| 600 | case DownloadItem::SAFE: |
| 601 | // The download is safe; just finish it. |
| 602 | download->OnSafeDownloadFinished(file_manager_); |
| 603 | return; |
| 604 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 605 | } |
| 606 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 607 | void DownloadManager::RemoveFromActiveList(int32 download_id) { |
| 608 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 609 | active_downloads_.erase(download_id); |
| 610 | } |
| 611 | |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 612 | void DownloadManager::DownloadRenamedToFinalName(int download_id, |
| 613 | const FilePath& full_path) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 614 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 615 | << " full_path = \"" << full_path.value() << "\""; |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 616 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 617 | DownloadItem* item = GetDownloadItem(download_id); |
| 618 | if (!item) |
| 619 | return; |
[email protected] | 9246de3 | 2010-11-10 22:58:47 | [diff] [blame] | 620 | item->OnDownloadRenamedToFinalName(full_path); |
[email protected] | 8f78375 | 2009-04-01 23:33:45 | [diff] [blame] | 621 | } |
| 622 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 623 | // Called on the file thread. Renames the downloaded file to its original name. |
| 624 | void DownloadManager::ProceedWithFinishedDangerousDownload( |
| 625 | int64 download_handle, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 626 | const FilePath& path, |
| 627 | const FilePath& original_name) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 628 | bool success = false; |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 629 | FilePath new_path; |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 630 | int uniquifier = 0; |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 631 | if (file_util::PathExists(path)) { |
[email protected] | 889ed35c | 2009-01-21 00:07:24 | [diff] [blame] | 632 | new_path = path.DirName().Append(original_name); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 633 | // Make our name unique at this point, as if a dangerous file is downloading |
| 634 | // and a 2nd download is started for a file with the same name, they would |
| 635 | // have the same path. This is because we uniquify the name on download |
| 636 | // start, and at that time the first file does not exists yet, so the second |
| 637 | // file gets the same name. |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 638 | uniquifier = download_util::GetUniquePathNumber(new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 639 | if (uniquifier > 0) |
[email protected] | 5a2388a | 2010-03-26 16:13:39 | [diff] [blame] | 640 | download_util::AppendNumberToPath(&new_path, uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 641 | success = file_util::Move(path, new_path); |
| 642 | } else { |
| 643 | NOTREACHED(); |
| 644 | } |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 645 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 646 | BrowserThread::PostTask( |
| 647 | BrowserThread::UI, FROM_HERE, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 648 | NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 649 | download_handle, success, new_path, uniquifier)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // Call from the file thread when the finished dangerous download was renamed. |
| 653 | void DownloadManager::DangerousDownloadRenamed(int64 download_handle, |
| 654 | bool success, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 655 | const FilePath& new_path, |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 656 | int new_path_uniquifier) { |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 657 | VLOG(20) << __FUNCTION__ << "()" << " download_handle = " << download_handle |
| 658 | << " success = " << success |
| 659 | << " new_path = \"" << new_path.value() << "\"" |
| 660 | << " new_path_uniquifier = " << new_path_uniquifier; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 661 | DownloadMap::iterator it = history_downloads_.find(download_handle); |
| 662 | if (it == history_downloads_.end()) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 663 | NOTREACHED(); |
| 664 | return; |
| 665 | } |
| 666 | |
| 667 | DownloadItem* download = it->second; |
| 668 | // If we failed to rename the file, we'll just keep the name as is. |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 669 | if (success) { |
| 670 | // We need to update the path uniquifier so that the UI shows the right |
[email protected] | 44b94b8 | 2010-11-08 20:49:18 | [diff] [blame] | 671 | // name when calling GetFileNameToReportUser(). |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 672 | download->set_path_uniquifier(new_path_uniquifier); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 673 | RenameDownload(download, new_path); |
[email protected] | 7a256ea | 2008-10-17 17:34:16 | [diff] [blame] | 674 | } |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 675 | |
| 676 | // Continue the download finished sequence. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 677 | download->Finished(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 678 | } |
| 679 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 680 | void DownloadManager::DownloadCancelled(int32 download_id) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 681 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 682 | DownloadMap::iterator it = in_progress_.find(download_id); |
| 683 | if (it == in_progress_.end()) |
| 684 | return; |
| 685 | DownloadItem* download = it->second; |
| 686 | |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 687 | VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 688 | << " download = " << download->DebugString(true); |
| 689 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 690 | // Clean up will happen when the history system create callback runs if we |
| 691 | // don't have a valid db_handle yet. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 692 | if (download->db_handle() != DownloadHistory::kUninitializedHandle) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 693 | in_progress_.erase(it); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 694 | active_downloads_.erase(download_id); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 695 | UpdateAppIcon(); // Reflect removal from in_progress_. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 696 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 697 | } |
| 698 | |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 699 | DownloadCancelledInternal(download_id, |
| 700 | download->render_process_id(), |
| 701 | download->request_id()); |
| 702 | } |
| 703 | |
| 704 | void DownloadManager::DownloadCancelledInternal(int download_id, |
| 705 | int render_process_id, |
| 706 | int request_id) { |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 707 | // Cancel the network request. RDH is guaranteed to outlive the IO thread. |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 708 | BrowserThread::PostTask( |
| 709 | BrowserThread::IO, FROM_HERE, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 710 | NewRunnableFunction(&download_util::CancelDownloadRequest, |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 711 | g_browser_process->resource_dispatcher_host(), |
| 712 | render_process_id, |
| 713 | request_id)); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 714 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 715 | BrowserThread::PostTask( |
| 716 | BrowserThread::FILE, FROM_HERE, |
[email protected] | d83d03aa | 2009-11-02 21:44:37 | [diff] [blame] | 717 | NewRunnableMethod( |
| 718 | file_manager_, &DownloadFileManager::CancelDownload, download_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void DownloadManager::PauseDownload(int32 download_id, bool pause) { |
| 722 | DownloadMap::iterator it = in_progress_.find(download_id); |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 723 | if (it == in_progress_.end()) |
| 724 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 725 | |
[email protected] | d85cf07 | 2009-10-27 03:59:31 | [diff] [blame] | 726 | DownloadItem* download = it->second; |
| 727 | if (pause == download->is_paused()) |
| 728 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 729 | |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 730 | BrowserThread::PostTask( |
| 731 | BrowserThread::IO, FROM_HERE, |
[email protected] | e2abdeb2 | 2010-09-03 19:56:15 | [diff] [blame] | 732 | NewRunnableMethod(this, |
| 733 | &DownloadManager::PauseDownloadRequest, |
| 734 | g_browser_process->resource_dispatcher_host(), |
[email protected] | 7ba53e1 | 2010-08-05 17:14:00 | [diff] [blame] | 735 | download->render_process_id(), |
| 736 | download->request_id(), |
| 737 | pause)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 738 | } |
| 739 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 740 | void DownloadManager::UpdateAppIcon() { |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 741 | if (status_updater_) |
| 742 | status_updater_->Update(); |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 743 | } |
| 744 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 745 | void DownloadManager::RenameDownload(DownloadItem* download, |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 746 | const FilePath& new_path) { |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 747 | download->Rename(new_path); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 748 | download_history_->UpdateDownloadPath(download, new_path); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 749 | } |
| 750 | |
[email protected] | e2abdeb2 | 2010-09-03 19:56:15 | [diff] [blame] | 751 | void DownloadManager::PauseDownloadRequest(ResourceDispatcherHost* rdh, |
| 752 | int render_process_id, |
| 753 | int request_id, |
| 754 | bool pause) { |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 755 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | e2abdeb2 | 2010-09-03 19:56:15 | [diff] [blame] | 756 | rdh->PauseRequest(render_process_id, request_id, pause); |
| 757 | } |
| 758 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 759 | void DownloadManager::RemoveDownload(int64 download_handle) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 760 | DownloadMap::iterator it = history_downloads_.find(download_handle); |
| 761 | if (it == history_downloads_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 762 | return; |
| 763 | |
| 764 | // Make history update. |
| 765 | DownloadItem* download = it->second; |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 766 | download_history_->RemoveEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 767 | |
| 768 | // Remove from our tables and delete. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 769 | history_downloads_.erase(it); |
| 770 | int downloads_count = downloads_.erase(download); |
| 771 | DCHECK_EQ(1, downloads_count); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 772 | |
| 773 | // Tell observers to refresh their views. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 774 | NotifyModelChanged(); |
[email protected] | 6f71287 | 2008-11-07 00:35:36 | [diff] [blame] | 775 | |
| 776 | delete download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 777 | } |
| 778 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 779 | int DownloadManager::RemoveDownloadsBetween(const base::Time remove_begin, |
| 780 | const base::Time remove_end) { |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 781 | download_history_->RemoveEntriesBetween(remove_begin, remove_end); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 782 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 783 | // All downloads visible to the user will be in the history, |
| 784 | // so scan that map. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 785 | DownloadMap::iterator it = history_downloads_.begin(); |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 786 | std::vector<DownloadItem*> pending_deletes; |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 787 | while (it != history_downloads_.end()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 788 | DownloadItem* download = it->second; |
| 789 | DownloadItem::DownloadState state = download->state(); |
| 790 | if (download->start_time() >= remove_begin && |
| 791 | (remove_end.is_null() || download->start_time() < remove_end) && |
| 792 | (state == DownloadItem::COMPLETE || |
| 793 | state == DownloadItem::CANCELLED)) { |
| 794 | // Remove from the map and move to the next in the list. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 795 | history_downloads_.erase(it++); |
[email protected] | a6604d9 | 2008-10-30 00:58:58 | [diff] [blame] | 796 | |
| 797 | // Also remove it from any completed dangerous downloads. |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 798 | pending_deletes.push_back(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 799 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 800 | continue; |
| 801 | } |
| 802 | |
| 803 | ++it; |
| 804 | } |
| 805 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 806 | // If we aren't deleting anything, we're done. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 807 | if (pending_deletes.empty()) |
| 808 | return 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 809 | |
[email protected] | a312a44 | 2010-12-15 23:40:33 | [diff] [blame] | 810 | // Remove the chosen downloads from the main owning container. |
| 811 | for (std::vector<DownloadItem*>::iterator it = pending_deletes.begin(); |
| 812 | it != pending_deletes.end(); it++) { |
| 813 | downloads_.erase(*it); |
| 814 | } |
| 815 | |
| 816 | // Tell observers to refresh their views. |
| 817 | NotifyModelChanged(); |
| 818 | |
| 819 | // Delete the download items themselves. |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 820 | int num_deleted = static_cast<int>(pending_deletes.size()); |
| 821 | |
[email protected] | 78b8fcc9 | 2009-03-31 17:36:28 | [diff] [blame] | 822 | STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 823 | pending_deletes.clear(); |
| 824 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 825 | return num_deleted; |
| 826 | } |
| 827 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 828 | int DownloadManager::RemoveDownloads(const base::Time remove_begin) { |
| 829 | return RemoveDownloadsBetween(remove_begin, base::Time()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 830 | } |
| 831 | |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 832 | int DownloadManager::RemoveAllDownloads() { |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 833 | if (this != profile_->GetOriginalProfile()->GetDownloadManager()) { |
| 834 | // This is an incognito downloader. Clear All should clear main download |
| 835 | // manager as well. |
| 836 | profile_->GetOriginalProfile()->GetDownloadManager()->RemoveAllDownloads(); |
| 837 | } |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 838 | // The null times make the date range unbounded. |
| 839 | return RemoveDownloadsBetween(base::Time(), base::Time()); |
| 840 | } |
| 841 | |
[email protected] | ec4826a | 2010-09-21 09:15:59 | [diff] [blame] | 842 | void DownloadManager::SavePageAsDownloadStarted(DownloadItem* download_item) { |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 843 | #if !defined(NDEBUG) |
| 844 | save_page_as_downloads_.insert(download_item); |
| 845 | #endif |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 846 | downloads_.insert(download_item); |
[email protected] | ec4826a | 2010-09-21 09:15:59 | [diff] [blame] | 847 | } |
| 848 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 849 | // Initiate a download of a specific URL. We send the request to the |
| 850 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 851 | // download. |
| 852 | void DownloadManager::DownloadUrl(const GURL& url, |
| 853 | const GURL& referrer, |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 854 | const std::string& referrer_charset, |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 855 | TabContents* tab_contents) { |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 856 | DownloadUrlToFile(url, referrer, referrer_charset, DownloadSaveInfo(), |
| 857 | tab_contents); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | void DownloadManager::DownloadUrlToFile(const GURL& url, |
| 861 | const GURL& referrer, |
| 862 | const std::string& referrer_charset, |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 863 | const DownloadSaveInfo& save_info, |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 864 | TabContents* tab_contents) { |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 865 | DCHECK(tab_contents); |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 866 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
[email protected] | ae894519 | 2010-07-20 16:56:26 | [diff] [blame] | 867 | NewRunnableFunction(&download_util::DownloadUrl, |
| 868 | url, |
| 869 | referrer, |
| 870 | referrer_charset, |
| 871 | save_info, |
| 872 | g_browser_process->resource_dispatcher_host(), |
| 873 | tab_contents->GetRenderProcessHost()->id(), |
| 874 | tab_contents->render_view_host()->routing_id(), |
| 875 | request_context_getter_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 876 | } |
| 877 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 878 | void DownloadManager::AddObserver(Observer* observer) { |
| 879 | observers_.AddObserver(observer); |
| 880 | observer->ModelChanged(); |
| 881 | } |
| 882 | |
| 883 | void DownloadManager::RemoveObserver(Observer* observer) { |
| 884 | observers_.RemoveObserver(observer); |
| 885 | } |
| 886 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 887 | bool DownloadManager::ShouldOpenFileBasedOnExtension( |
| 888 | const FilePath& path) const { |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 889 | FilePath::StringType extension = path.Extension(); |
| 890 | if (extension.empty()) |
| 891 | return false; |
[email protected] | 92e11c8 | 2010-01-13 06:39:56 | [diff] [blame] | 892 | if (Extension::IsExtension(path)) |
| 893 | return false; |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 894 | DCHECK(extension[0] == FilePath::kExtensionSeparator); |
| 895 | extension.erase(0, 1); |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 896 | return download_prefs_->IsAutoOpenEnabledForExtension(extension); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 897 | } |
| 898 | |
[email protected] | 073ed7b | 2010-09-27 09:20:02 | [diff] [blame] | 899 | bool DownloadManager::IsDownloadProgressKnown() { |
| 900 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 901 | i != in_progress_.end(); ++i) { |
| 902 | if (i->second->total_bytes() <= 0) |
| 903 | return false; |
| 904 | } |
| 905 | |
| 906 | return true; |
| 907 | } |
| 908 | |
| 909 | int64 DownloadManager::GetInProgressDownloadCount() { |
| 910 | return in_progress_.size(); |
| 911 | } |
| 912 | |
| 913 | int64 DownloadManager::GetReceivedDownloadBytes() { |
| 914 | DCHECK(IsDownloadProgressKnown()); |
| 915 | int64 received_bytes = 0; |
| 916 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 917 | i != in_progress_.end(); ++i) { |
| 918 | received_bytes += i->second->received_bytes(); |
| 919 | } |
| 920 | return received_bytes; |
| 921 | } |
| 922 | |
| 923 | int64 DownloadManager::GetTotalDownloadBytes() { |
| 924 | DCHECK(IsDownloadProgressKnown()); |
| 925 | int64 total_bytes = 0; |
| 926 | for (DownloadMap::iterator i = in_progress_.begin(); |
| 927 | i != in_progress_.end(); ++i) { |
| 928 | total_bytes += i->second->total_bytes(); |
| 929 | } |
| 930 | return total_bytes; |
| 931 | } |
| 932 | |
[email protected] | 561abe6 | 2009-04-06 18:08:34 | [diff] [blame] | 933 | void DownloadManager::FileSelected(const FilePath& path, |
[email protected] | 23b357b | 2009-03-30 20:02:36 | [diff] [blame] | 934 | int index, void* params) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 935 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | e6875c1 | 2010-07-18 11:14:13 | [diff] [blame] | 936 | if (info->prompt_user_for_save_location) |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 937 | last_download_path_ = path.DirName(); |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 938 | AttachDownloadItem(info, path); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | void DownloadManager::FileSelectionCanceled(void* params) { |
| 942 | // The user didn't pick a place to save the file, so need to cancel the |
| 943 | // download that's already in progress to the temporary location. |
| 944 | DownloadCreateInfo* info = reinterpret_cast<DownloadCreateInfo*>(params); |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 945 | DownloadCancelledInternal(info->download_id, |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 946 | info->child_id, |
[email protected] | d7d1c5c | 2009-08-05 23:52:50 | [diff] [blame] | 947 | info->request_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 948 | } |
| 949 | |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 950 | void DownloadManager::DangerousDownloadValidated(DownloadItem* download) { |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 951 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 952 | DCHECK_EQ(DownloadItem::DANGEROUS, download->safety_state()); |
| 953 | download->set_safety_state(DownloadItem::DANGEROUS_BUT_VALIDATED); |
| 954 | download->UpdateObservers(); |
| 955 | |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 956 | MaybeCompleteDownload(download); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 957 | } |
| 958 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 959 | // Operations posted to us from the history service ---------------------------- |
| 960 | |
| 961 | // The history service has retrieved all download entries. 'entries' contains |
| 962 | // 'DownloadCreateInfo's in sorted order (by ascending start_time). |
| 963 | void DownloadManager::OnQueryDownloadEntriesComplete( |
| 964 | std::vector<DownloadCreateInfo>* entries) { |
| 965 | for (size_t i = 0; i < entries->size(); ++i) { |
[email protected] | aa033af | 2010-07-27 18:16:39 | [diff] [blame] | 966 | DownloadItem* download = new DownloadItem(this, entries->at(i)); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 967 | DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 968 | downloads_.insert(download); |
| 969 | history_downloads_[download->db_handle()] = download; |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 970 | VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 971 | << " download = " << download->DebugString(true); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 972 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 973 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 974 | } |
| 975 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 976 | // Once the new DownloadItem's creation info has been committed to the history |
| 977 | // service, we associate the DownloadItem with the db handle, update our |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 978 | // 'history_downloads_' map and inform observers. |
[email protected] | 9f5cb56 | 2010-11-08 22:06:59 | [diff] [blame] | 979 | void DownloadManager::OnCreateDownloadEntryComplete( |
[email protected] | 76d8b62 | 2010-11-11 19:50:05 | [diff] [blame] | 980 | DownloadCreateInfo info, |
[email protected] | 9f5cb56 | 2010-11-08 22:06:59 | [diff] [blame] | 981 | int64 db_handle) { |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 982 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 983 | DownloadMap::iterator it = in_progress_.find(info.download_id); |
| 984 | DCHECK(it != in_progress_.end()); |
| 985 | |
| 986 | DownloadItem* download = it->second; |
[email protected] | da6e392 | 2010-11-24 21:45:50 | [diff] [blame] | 987 | VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle |
| 988 | << " download_id = " << info.download_id |
| 989 | << " download = " << download->DebugString(true); |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 990 | |
| 991 | // It's not immediately obvious, but HistoryBackend::CreateDownload() can |
| 992 | // call this function with an invalid |db_handle|. For instance, this can |
| 993 | // happen when the history database is offline. We cannot have multiple |
| 994 | // DownloadItems with the same invalid db_handle, so we need to assign a |
| 995 | // unique |db_handle| here. |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 996 | if (db_handle == DownloadHistory::kUninitializedHandle) |
| 997 | db_handle = download_history_->GetNextFakeDbHandle(); |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 998 | |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 999 | DCHECK(download->db_handle() == DownloadHistory::kUninitializedHandle); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1000 | download->set_db_handle(db_handle); |
| 1001 | |
| 1002 | // Insert into our full map. |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1003 | DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1004 | history_downloads_[download->db_handle()] = download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1005 | |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1006 | // Show in the appropriate browser UI. |
[email protected] | 9d7ef80 | 2011-02-25 19:03:35 | [diff] [blame^] | 1007 | // This includes buttons to save or cancel, for a dangerous download. |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1008 | ShowDownloadInBrowser(info, download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1009 | |
| 1010 | // Inform interested objects about the new download. |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1011 | NotifyModelChanged(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1012 | |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1013 | // If this download has been cancelled before we've received the DB handle, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1014 | // post one final message to the history service so that it can be properly |
| 1015 | // in sync with the DownloadItem's completion status, and also inform any |
| 1016 | // observers so that they get more than just the start notification. |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1017 | // |
| 1018 | // Otherwise, try to complete the download |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1019 | if (download->state() != DownloadItem::IN_PROGRESS) { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1020 | DCHECK_EQ(DownloadItem::CANCELLED, download->state()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1021 | in_progress_.erase(it); |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1022 | active_downloads_.erase(info.download_id); |
[email protected] | 82f37b0 | 2010-07-29 22:04:57 | [diff] [blame] | 1023 | download_history_->UpdateEntry(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1024 | download->UpdateObservers(); |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1025 | } else { |
| 1026 | MaybeCompleteDownload(download); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1030 | void DownloadManager::ShowDownloadInBrowser(const DownloadCreateInfo& info, |
| 1031 | DownloadItem* download) { |
[email protected] | 8ddbd66a | 2010-05-21 16:38:34 | [diff] [blame] | 1032 | // The 'contents' may no longer exist if the user closed the tab before we |
| 1033 | // get this start completion event. If it does, tell the origin TabContents |
| 1034 | // to display its download shelf. |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 1035 | TabContents* contents = tab_util::GetTabContentsByID(info.child_id, |
| 1036 | info.render_view_id); |
[email protected] | 5e59548 | 2009-05-06 20:16:53 | [diff] [blame] | 1037 | |
| 1038 | // If the contents no longer exists, we start the download in the last active |
| 1039 | // browser. This is not ideal but better than fully hiding the download from |
| 1040 | // the user. |
| 1041 | if (!contents) { |
| 1042 | Browser* last_active = BrowserList::GetLastActive(); |
| 1043 | if (last_active) |
| 1044 | contents = last_active->GetSelectedTabContents(); |
| 1045 | } |
| 1046 | |
| 1047 | if (contents) |
| 1048 | contents->OnStartDownload(download); |
| 1049 | } |
| 1050 | |
[email protected] | 6cade21 | 2008-12-03 00:32:22 | [diff] [blame] | 1051 | // Clears the last download path, used to initialize "save as" dialogs. |
[email protected] | 905a08d | 2008-11-19 07:24:12 | [diff] [blame] | 1052 | void DownloadManager::ClearLastDownloadPath() { |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 1053 | last_download_path_ = FilePath(); |
[email protected] | eea4662 | 2009-07-15 20:49:38 | [diff] [blame] | 1054 | } |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1055 | |
| 1056 | void DownloadManager::NotifyModelChanged() { |
| 1057 | FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 1058 | } |
| 1059 | |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1060 | DownloadItem* DownloadManager::GetDownloadItem(int id) { |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1061 | for (DownloadMap::iterator it = history_downloads_.begin(); |
| 1062 | it != history_downloads_.end(); ++it) { |
[email protected] | 2e03068 | 2010-07-23 19:45:36 | [diff] [blame] | 1063 | DownloadItem* item = it->second; |
| 1064 | if (item->id() == id) |
| 1065 | return item; |
| 1066 | } |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1070 | // Confirm that everything in all maps is also in |downloads_|, and that |
| 1071 | // everything in |downloads_| is also in some other map. |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1072 | void DownloadManager::AssertContainersConsistent() const { |
| 1073 | #if !defined(NDEBUG) |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1074 | // Turn everything into sets. |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1075 | DownloadSet active_set, history_set; |
[email protected] | 70850c7 | 2011-01-11 17:31:27 | [diff] [blame] | 1076 | const DownloadMap* input_maps[] = {&active_downloads_, &history_downloads_}; |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1077 | DownloadSet* local_sets[] = {&active_set, &history_set}; |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1078 | DCHECK_EQ(ARRAYSIZE_UNSAFE(input_maps), ARRAYSIZE_UNSAFE(local_sets)); |
| 1079 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_maps); i++) { |
| 1080 | for (DownloadMap::const_iterator it = input_maps[i]->begin(); |
| 1081 | it != input_maps[i]->end(); it++) { |
| 1082 | local_sets[i]->insert(&*it->second); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1083 | } |
| 1084 | } |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1085 | |
| 1086 | // Check if each set is fully present in downloads, and create a union. |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 1087 | const DownloadSet* all_sets[] = {&active_set, &history_set, |
[email protected] | 57fd125 | 2010-12-23 17:24:09 | [diff] [blame] | 1088 | &save_page_as_downloads_}; |
| 1089 | DownloadSet downloads_union; |
| 1090 | for (int i = 0; i < static_cast<int>(ARRAYSIZE_UNSAFE(all_sets)); i++) { |
| 1091 | DownloadSet remainder; |
| 1092 | std::insert_iterator<DownloadSet> insert_it(remainder, remainder.begin()); |
| 1093 | std::set_difference(all_sets[i]->begin(), all_sets[i]->end(), |
| 1094 | downloads_.begin(), downloads_.end(), |
| 1095 | insert_it); |
| 1096 | DCHECK(remainder.empty()); |
| 1097 | std::insert_iterator<DownloadSet> |
| 1098 | insert_union(downloads_union, downloads_union.end()); |
| 1099 | std::set_union(downloads_union.begin(), downloads_union.end(), |
| 1100 | all_sets[i]->begin(), all_sets[i]->end(), |
| 1101 | insert_union); |
| 1102 | } |
| 1103 | |
| 1104 | // Is everything in downloads_ present in one of the other sets? |
| 1105 | DownloadSet remainder; |
| 1106 | std::insert_iterator<DownloadSet> |
| 1107 | insert_remainder(remainder, remainder.begin()); |
| 1108 | std::set_difference(downloads_.begin(), downloads_.end(), |
| 1109 | downloads_union.begin(), downloads_union.end(), |
| 1110 | insert_remainder); |
| 1111 | DCHECK(remainder.empty()); |
[email protected] | f04182f3 | 2010-12-10 19:12:07 | [diff] [blame] | 1112 | #endif |
| 1113 | } |
| 1114 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1115 | // DownloadManager::OtherDownloadManagerObserver implementation ---------------- |
| 1116 | |
| 1117 | DownloadManager::OtherDownloadManagerObserver::OtherDownloadManagerObserver( |
| 1118 | DownloadManager* observing_download_manager) |
| 1119 | : observing_download_manager_(observing_download_manager), |
| 1120 | observed_download_manager_(NULL) { |
| 1121 | if (observing_download_manager->profile_->GetOriginalProfile() == |
| 1122 | observing_download_manager->profile_) { |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | observed_download_manager_ = observing_download_manager_-> |
| 1127 | profile_->GetOriginalProfile()->GetDownloadManager(); |
| 1128 | observed_download_manager_->AddObserver(this); |
| 1129 | } |
| 1130 | |
| 1131 | DownloadManager::OtherDownloadManagerObserver::~OtherDownloadManagerObserver() { |
| 1132 | if (observed_download_manager_) |
| 1133 | observed_download_manager_->RemoveObserver(this); |
| 1134 | } |
| 1135 | |
| 1136 | void DownloadManager::OtherDownloadManagerObserver::ModelChanged() { |
| 1137 | observing_download_manager_->NotifyModelChanged(); |
| 1138 | } |
| 1139 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 1140 | void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
| 1141 | observed_download_manager_ = NULL; |
| 1142 | } |