| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/download/download_manager.h" | 5 #include "content/browser/download/download_manager.h" |
| 6 | 6 |
| 7 #include <iterator> | 7 #include <iterator> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
| 18 #include "base/task.h" | 18 #include "base/task.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "content/browser/browser_context.h" | 20 #include "content/browser/browser_context.h" |
| 21 #include "content/browser/browser_thread.h" | 21 #include "content/browser/browser_thread.h" |
| 22 #include "content/browser/content_browser_client.h" | 22 #include "content/browser/content_browser_client.h" |
| 23 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
| 24 #include "content/browser/download/download_file_manager.h" | 24 #include "content/browser/download/download_file_manager.h" |
| 25 #include "content/browser/download/download_id_factory.h" |
| 25 #include "content/browser/download/download_item.h" | 26 #include "content/browser/download/download_item.h" |
| 26 #include "content/browser/download/download_manager_delegate.h" | 27 #include "content/browser/download/download_manager_delegate.h" |
| 27 #include "content/browser/download/download_persistent_store_info.h" | 28 #include "content/browser/download/download_persistent_store_info.h" |
| 28 #include "content/browser/download/download_stats.h" | 29 #include "content/browser/download/download_stats.h" |
| 29 #include "content/browser/download/download_status_updater.h" | 30 #include "content/browser/download/download_status_updater.h" |
| 30 #include "content/browser/download/interrupt_reasons.h" | 31 #include "content/browser/download/interrupt_reasons.h" |
| 31 #include "content/browser/renderer_host/render_process_host.h" | 32 #include "content/browser/renderer_host/render_process_host.h" |
| 32 #include "content/browser/renderer_host/render_view_host.h" | 33 #include "content/browser/renderer_host/render_view_host.h" |
| 33 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 34 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 34 #include "content/browser/tab_contents/tab_contents.h" | 35 #include "content/browser/tab_contents/tab_contents.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 53 true, | 54 true, |
| 54 DownloadResourceHandler::OnStartedCallback(), | 55 DownloadResourceHandler::OnStartedCallback(), |
| 55 render_process_id, | 56 render_process_id, |
| 56 render_view_id, | 57 render_view_id, |
| 57 *context); | 58 *context); |
| 58 } | 59 } |
| 59 | 60 |
| 60 } // namespace | 61 } // namespace |
| 61 | 62 |
| 62 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 63 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
| 64 DownloadIdFactory* id_factory, |
| 63 DownloadStatusUpdater* status_updater) | 65 DownloadStatusUpdater* status_updater) |
| 64 : shutdown_needed_(false), | 66 : shutdown_needed_(false), |
| 65 browser_context_(NULL), | 67 browser_context_(NULL), |
| 66 next_id_(0), | |
| 67 file_manager_(NULL), | 68 file_manager_(NULL), |
| 68 status_updater_((status_updater != NULL) | 69 status_updater_((status_updater != NULL) |
| 69 ? status_updater->AsWeakPtr() | 70 ? status_updater->AsWeakPtr() |
| 70 : base::WeakPtr<DownloadStatusUpdater>()), | 71 : base::WeakPtr<DownloadStatusUpdater>()), |
| 71 delegate_(delegate), | 72 delegate_(delegate), |
| 73 id_factory_(id_factory), |
| 72 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 74 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
| 73 // NOTE(benjhayden): status_updater may be NULL when using | 75 // NOTE(benjhayden): status_updater may be NULL when using |
| 74 // TestingBrowserProcess. | 76 // TestingBrowserProcess. |
| 75 if (status_updater_.get() != NULL) | 77 if (status_updater_.get() != NULL) |
| 76 status_updater_->AddDelegate(this); | 78 status_updater_->AddDelegate(this); |
| 77 } | 79 } |
| 78 | 80 |
| 79 DownloadManager::~DownloadManager() { | 81 DownloadManager::~DownloadManager() { |
| 80 DCHECK(!shutdown_needed_); | 82 DCHECK(!shutdown_needed_); |
| 81 if (status_updater_.get() != NULL) | 83 if (status_updater_.get() != NULL) |
| 82 status_updater_->RemoveDelegate(this); | 84 status_updater_->RemoveDelegate(this); |
| 83 } | 85 } |
| 84 | 86 |
| 87 DownloadId DownloadManager::GetNextId() { |
| 88 return id_factory_->GetNextId(); |
| 89 } |
| 90 |
| 85 void DownloadManager::Shutdown() { | 91 void DownloadManager::Shutdown() { |
| 86 VLOG(20) << __FUNCTION__ << "()" | 92 VLOG(20) << __FUNCTION__ << "()" |
| 87 << " shutdown_needed_ = " << shutdown_needed_; | 93 << " shutdown_needed_ = " << shutdown_needed_; |
| 88 if (!shutdown_needed_) | 94 if (!shutdown_needed_) |
| 89 return; | 95 return; |
| 90 shutdown_needed_ = false; | 96 shutdown_needed_ = false; |
| 91 | 97 |
| 92 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); | 98 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
| 93 // TODO(benjhayden): Consider clearing observers_. | 99 // TODO(benjhayden): Consider clearing observers_. |
| 94 | 100 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 // The Incognito Downloads page will get the list of non-Incognito downloads | 195 // The Incognito Downloads page will get the list of non-Incognito downloads |
| 190 // from its parent profile. | 196 // from its parent profile. |
| 191 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 197 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
| 192 continue; | 198 continue; |
| 193 | 199 |
| 194 if (download_item->MatchesQuery(query_lower)) | 200 if (download_item->MatchesQuery(query_lower)) |
| 195 result->push_back(download_item); | 201 result->push_back(download_item); |
| 196 } | 202 } |
| 197 } | 203 } |
| 198 | 204 |
| 199 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { | |
| 200 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
| 201 base::AutoLock lock(next_id_lock_); | |
| 202 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
| 203 // = next_id. The '+=' works for now because these ids are not yet persisted | |
| 204 // to the database. GetNextId() can allocate zero or more ids starting from 0, | |
| 205 // then this callback can increment next_id_, and the items with lower ids | |
| 206 // won't clash with any other items even though there may be items loaded from | |
| 207 // the history because items from the history don't have valid ids. | |
| 208 next_id_ += next_id; | |
| 209 } | |
| 210 | |
| 211 DownloadId DownloadManager::GetNextId() { | |
| 212 // May be called on any thread via the GetNextIdThunk. | |
| 213 // TODO(benjhayden) If otr, forward to parent DM. | |
| 214 base::AutoLock lock(next_id_lock_); | |
| 215 return DownloadId(this, next_id_++); | |
| 216 } | |
| 217 | |
| 218 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
| 219 // TODO(benjhayden) If otr, forward to parent DM. | |
| 220 return base::Bind(&DownloadManager::GetNextId, this); | |
| 221 } | |
| 222 | |
| 223 // Query the history service for information about all persisted downloads. | 205 // Query the history service for information about all persisted downloads. |
| 224 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 206 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
| 225 DCHECK(browser_context); | 207 DCHECK(browser_context); |
| 226 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 208 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 227 shutdown_needed_ = true; | 209 shutdown_needed_ = true; |
| 228 | 210 |
| 229 browser_context_ = browser_context; | 211 browser_context_ = browser_context; |
| 230 | 212 |
| 231 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 213 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
| 232 // safe to avoid setting |file_manager_| because we only call a small set of | 214 // safe to avoid setting |file_manager_| because we only call a small set of |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 // No prompting for download, just continue with the suggested name. | 305 // No prompting for download, just continue with the suggested name. |
| 324 ContinueDownloadWithPath(download, suggested_path); | 306 ContinueDownloadWithPath(download, suggested_path); |
| 325 } | 307 } |
| 326 } | 308 } |
| 327 | 309 |
| 328 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 310 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 330 | 312 |
| 331 DownloadItem* download = new DownloadItem(this, *info, | 313 DownloadItem* download = new DownloadItem(this, *info, |
| 332 browser_context_->IsOffTheRecord()); | 314 browser_context_->IsOffTheRecord()); |
| 333 int32 download_id = info->download_id; | 315 int32 download_id = info->download_id.local(); |
| 334 DCHECK(!ContainsKey(in_progress_, download_id)); | 316 DCHECK(!ContainsKey(in_progress_, download_id)); |
| 335 | 317 |
| 336 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 318 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
| 337 CHECK(!ContainsKey(active_downloads_, download_id)); | 319 CHECK(!ContainsKey(active_downloads_, download_id)); |
| 338 downloads_.insert(download); | 320 downloads_.insert(download); |
| 339 active_downloads_[download_id] = download; | 321 active_downloads_[download_id] = download; |
| 340 } | 322 } |
| 341 | 323 |
| 342 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, | 324 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, |
| 343 const FilePath& chosen_file) { | 325 const FilePath& chosen_file) { |
| (...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1083 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { | 1065 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { |
| 1084 delegate_->UpdateItemInPersistentStore(download); | 1066 delegate_->UpdateItemInPersistentStore(download); |
| 1085 int num_unopened = 0; | 1067 int num_unopened = 0; |
| 1086 for (DownloadMap::iterator it = history_downloads_.begin(); | 1068 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 1087 it != history_downloads_.end(); ++it) { | 1069 it != history_downloads_.end(); ++it) { |
| 1088 if (it->second->IsComplete() && !it->second->opened()) | 1070 if (it->second->IsComplete() && !it->second->opened()) |
| 1089 ++num_unopened; | 1071 ++num_unopened; |
| 1090 } | 1072 } |
| 1091 download_stats::RecordOpensOutstanding(num_unopened); | 1073 download_stats::RecordOpensOutstanding(num_unopened); |
| 1092 } | 1074 } |
| OLD | NEW |