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