| 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_impl.h" | 5 #include "content/browser/download/download_manager_impl.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 "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "content/browser/browser_context.h" | 19 #include "content/browser/browser_context.h" |
| 20 #include "content/browser/download/download_create_info.h" | 20 #include "content/browser/download/download_create_info.h" |
| 21 #include "content/browser/download/download_file_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
| 22 #include "content/browser/download/download_id_factory.h" | 22 #include "content/browser/download/download_id_factory.h" |
| 23 #include "content/browser/download/download_item.h" | 23 #include "content/browser/download/download_item_impl.h" |
| 24 #include "content/browser/download/download_persistent_store_info.h" | 24 #include "content/browser/download/download_persistent_store_info.h" |
| 25 #include "content/browser/download/download_stats.h" | 25 #include "content/browser/download/download_stats.h" |
| 26 #include "content/browser/download/download_status_updater.h" | 26 #include "content/browser/download/download_status_updater.h" |
| 27 #include "content/browser/download/interrupt_reasons.h" | 27 #include "content/browser/download/interrupt_reasons.h" |
| 28 #include "content/browser/renderer_host/render_view_host.h" | 28 #include "content/browser/renderer_host/render_view_host.h" |
| 29 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 29 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 30 #include "content/browser/tab_contents/tab_contents.h" | 30 #include "content/browser/tab_contents/tab_contents.h" |
| 31 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 32 #include "content/public/browser/content_browser_client.h" | 32 #include "content/public/browser/content_browser_client.h" |
| 33 #include "content/public/browser/download_manager_delegate.h" | 33 #include "content/public/browser/download_manager_delegate.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // Go through all downloads in downloads_. Dangerous ones we need to | 122 // Go through all downloads in downloads_. Dangerous ones we need to |
| 123 // remove on disk, and in progress ones we need to cancel. | 123 // remove on disk, and in progress ones we need to cancel. |
| 124 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { | 124 for (DownloadSet::iterator it = downloads_.begin(); it != downloads_.end();) { |
| 125 DownloadItem* download = *it; | 125 DownloadItem* download = *it; |
| 126 | 126 |
| 127 // Save iterator from potential erases in this set done by called code. | 127 // Save iterator from potential erases in this set done by called code. |
| 128 // Iterators after an erasure point are still valid for lists and | 128 // Iterators after an erasure point are still valid for lists and |
| 129 // associative containers such as sets. | 129 // associative containers such as sets. |
| 130 it++; | 130 it++; |
| 131 | 131 |
| 132 if (download->safety_state() == DownloadItem::DANGEROUS && | 132 if (download->GetSafetyState() == DownloadItem::DANGEROUS && |
| 133 download->IsPartialDownload()) { | 133 download->IsPartialDownload()) { |
| 134 // The user hasn't accepted it, so we need to remove it | 134 // The user hasn't accepted it, so we need to remove it |
| 135 // from the disk. This may or may not result in it being | 135 // from the disk. This may or may not result in it being |
| 136 // removed from the DownloadManager queues and deleted | 136 // removed from the DownloadManager queues and deleted |
| 137 // (specifically, DownloadManager::RemoveDownload only | 137 // (specifically, DownloadManager::RemoveDownload only |
| 138 // removes and deletes it if it's known to the history service) | 138 // removes and deletes it if it's known to the history service) |
| 139 // so the only thing we know after calling this function is that | 139 // so the only thing we know after calling this function is that |
| 140 // the download was deleted if-and-only-if it was removed | 140 // the download was deleted if-and-only-if it was removed |
| 141 // from all queues. | 141 // from all queues. |
| 142 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); | 142 download->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 167 | 167 |
| 168 shutdown_needed_ = false; | 168 shutdown_needed_ = false; |
| 169 } | 169 } |
| 170 | 170 |
| 171 void DownloadManagerImpl::GetTemporaryDownloads( | 171 void DownloadManagerImpl::GetTemporaryDownloads( |
| 172 const FilePath& dir_path, DownloadVector* result) { | 172 const FilePath& dir_path, DownloadVector* result) { |
| 173 DCHECK(result); | 173 DCHECK(result); |
| 174 | 174 |
| 175 for (DownloadMap::iterator it = history_downloads_.begin(); | 175 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 176 it != history_downloads_.end(); ++it) { | 176 it != history_downloads_.end(); ++it) { |
| 177 if (it->second->is_temporary() && | 177 if (it->second->IsTemporary() && |
| 178 it->second->full_path().DirName() == dir_path) | 178 it->second->GetFullPath().DirName() == dir_path) |
| 179 result->push_back(it->second); | 179 result->push_back(it->second); |
| 180 } | 180 } |
| 181 } | 181 } |
| 182 | 182 |
| 183 void DownloadManagerImpl::GetAllDownloads( | 183 void DownloadManagerImpl::GetAllDownloads( |
| 184 const FilePath& dir_path, DownloadVector* result) { | 184 const FilePath& dir_path, DownloadVector* result) { |
| 185 DCHECK(result); | 185 DCHECK(result); |
| 186 | 186 |
| 187 for (DownloadMap::iterator it = history_downloads_.begin(); | 187 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 188 it != history_downloads_.end(); ++it) { | 188 it != history_downloads_.end(); ++it) { |
| 189 if (!it->second->is_temporary() && | 189 if (!it->second->IsTemporary() && |
| 190 (dir_path.empty() || it->second->full_path().DirName() == dir_path)) | 190 (dir_path.empty() || it->second->GetFullPath().DirName() == dir_path)) |
| 191 result->push_back(it->second); | 191 result->push_back(it->second); |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 | 194 |
| 195 void DownloadManagerImpl::SearchDownloads(const string16& query, | 195 void DownloadManagerImpl::SearchDownloads(const string16& query, |
| 196 DownloadVector* result) { | 196 DownloadVector* result) { |
| 197 string16 query_lower(base::i18n::ToLower(query)); | 197 string16 query_lower(base::i18n::ToLower(query)); |
| 198 | 198 |
| 199 for (DownloadMap::iterator it = history_downloads_.begin(); | 199 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 200 it != history_downloads_.end(); ++it) { | 200 it != history_downloads_.end(); ++it) { |
| 201 DownloadItem* download_item = it->second; | 201 DownloadItem* download_item = it->second; |
| 202 | 202 |
| 203 if (download_item->is_temporary()) | 203 if (download_item->IsTemporary()) |
| 204 continue; | 204 continue; |
| 205 | 205 |
| 206 // Display Incognito downloads only in Incognito window, and vice versa. | 206 // Display Incognito downloads only in Incognito window, and vice versa. |
| 207 // The Incognito Downloads page will get the list of non-Incognito downloads | 207 // The Incognito Downloads page will get the list of non-Incognito downloads |
| 208 // from its parent profile. | 208 // from its parent profile. |
| 209 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 209 if (browser_context_->IsOffTheRecord() != download_item->IsOtr()) |
| 210 continue; | 210 continue; |
| 211 | 211 |
| 212 if (download_item->MatchesQuery(query_lower)) | 212 if (download_item->MatchesQuery(query_lower)) |
| 213 result->push_back(download_item); | 213 result->push_back(download_item); |
| 214 } | 214 } |
| 215 } | 215 } |
| 216 | 216 |
| 217 // Query the history service for information about all persisted downloads. | 217 // Query the history service for information about all persisted downloads. |
| 218 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 218 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
| 219 DCHECK(browser_context); | 219 DCHECK(browser_context); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 248 for (DownloadMap::iterator it = history_downloads_.begin(); | 248 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 249 it != history_downloads_.end(); ++it) { | 249 it != history_downloads_.end(); ++it) { |
| 250 CheckForFileRemoval(it->second); | 250 CheckForFileRemoval(it->second); |
| 251 } | 251 } |
| 252 } | 252 } |
| 253 | 253 |
| 254 void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) { | 254 void DownloadManagerImpl::CheckForFileRemoval(DownloadItem* download_item) { |
| 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 256 if (download_item->IsComplete() && | 256 if (download_item->IsComplete() && |
| 257 !download_item->file_externally_removed()) { | 257 !download_item->GetFileExternallyRemoved()) { |
| 258 BrowserThread::PostTask( | 258 BrowserThread::PostTask( |
| 259 BrowserThread::FILE, FROM_HERE, | 259 BrowserThread::FILE, FROM_HERE, |
| 260 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, | 260 base::Bind(&DownloadManagerImpl::CheckForFileRemovalOnFileThread, |
| 261 this, download_item->db_handle(), | 261 this, download_item->GetDbHandle(), |
| 262 download_item->GetTargetFilePath())); | 262 download_item->GetTargetFilePath())); |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 | 265 |
| 266 void DownloadManagerImpl::CheckForFileRemovalOnFileThread( | 266 void DownloadManagerImpl::CheckForFileRemovalOnFileThread( |
| 267 int64 db_handle, const FilePath& path) { | 267 int64 db_handle, const FilePath& path) { |
| 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 269 if (!file_util::PathExists(path)) { | 269 if (!file_util::PathExists(path)) { |
| 270 BrowserThread::PostTask( | 270 BrowserThread::PostTask( |
| 271 BrowserThread::UI, FROM_HERE, | 271 BrowserThread::UI, FROM_HERE, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 288 int32 download_id) { | 288 int32 download_id) { |
| 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 290 | 290 |
| 291 DownloadItem* download = GetActiveDownloadItem(download_id); | 291 DownloadItem* download = GetActiveDownloadItem(download_id); |
| 292 if (!download) | 292 if (!download) |
| 293 return; | 293 return; |
| 294 | 294 |
| 295 VLOG(20) << __FUNCTION__ << "()" | 295 VLOG(20) << __FUNCTION__ << "()" |
| 296 << " download = " << download->DebugString(true); | 296 << " download = " << download->DebugString(true); |
| 297 | 297 |
| 298 FilePath suggested_path = download->suggested_path(); | 298 FilePath suggested_path = download->GetSuggestedPath(); |
| 299 | 299 |
| 300 if (download->prompt_user_for_save_location()) { | 300 if (download->PromptUserForSaveLocation()) { |
| 301 // We must ask the user for the place to put the download. | 301 // We must ask the user for the place to put the download. |
| 302 TabContents* contents = download->GetTabContents(); | 302 TabContents* contents = download->GetTabContents(); |
| 303 | 303 |
| 304 // |id_ptr| will be deleted in either FileSelected() or | 304 // |id_ptr| will be deleted in either FileSelected() or |
| 305 // FileSelectionCancelled(). | 305 // FileSelectionCancelled(). |
| 306 int32* id_ptr = new int32; | 306 int32* id_ptr = new int32; |
| 307 *id_ptr = download_id; | 307 *id_ptr = download_id; |
| 308 | 308 |
| 309 delegate_->ChooseDownloadPath( | 309 delegate_->ChooseDownloadPath( |
| 310 contents, suggested_path, reinterpret_cast<void*>(id_ptr)); | 310 contents, suggested_path, reinterpret_cast<void*>(id_ptr)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 322 } | 322 } |
| 323 | 323 |
| 324 FilePath DownloadManagerImpl::LastDownloadPath() { | 324 FilePath DownloadManagerImpl::LastDownloadPath() { |
| 325 return last_download_path_; | 325 return last_download_path_; |
| 326 } | 326 } |
| 327 | 327 |
| 328 void DownloadManagerImpl::CreateDownloadItem( | 328 void DownloadManagerImpl::CreateDownloadItem( |
| 329 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { | 329 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { |
| 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 331 | 331 |
| 332 DownloadItem* download = new DownloadItem( | 332 DownloadItem* download = new DownloadItemImpl( |
| 333 this, *info, new DownloadRequestHandle(request_handle), | 333 this, *info, new DownloadRequestHandle(request_handle), |
| 334 browser_context_->IsOffTheRecord()); | 334 browser_context_->IsOffTheRecord()); |
| 335 int32 download_id = info->download_id.local(); | 335 int32 download_id = info->download_id.local(); |
| 336 DCHECK(!ContainsKey(in_progress_, download_id)); | 336 DCHECK(!ContainsKey(in_progress_, download_id)); |
| 337 | 337 |
| 338 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 338 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
| 339 CHECK(!ContainsKey(active_downloads_, download_id)); | 339 CHECK(!ContainsKey(active_downloads_, download_id)); |
| 340 downloads_.insert(download); | 340 downloads_.insert(download); |
| 341 active_downloads_[download_id] = download; | 341 active_downloads_[download_id] = download; |
| 342 } | 342 } |
| 343 | 343 |
| 344 void DownloadManagerImpl::ContinueDownloadWithPath( | 344 void DownloadManagerImpl::ContinueDownloadWithPath( |
| 345 DownloadItem* download, const FilePath& chosen_file) { | 345 DownloadItem* download, const FilePath& chosen_file) { |
| 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 347 DCHECK(download); | 347 DCHECK(download); |
| 348 | 348 |
| 349 int32 download_id = download->id(); | 349 int32 download_id = download->GetId(); |
| 350 | 350 |
| 351 // NOTE(ahendrickson) Eventually |active_downloads_| will replace | 351 // NOTE(ahendrickson) Eventually |active_downloads_| will replace |
| 352 // |in_progress_|, but we don't want to change the semantics yet. | 352 // |in_progress_|, but we don't want to change the semantics yet. |
| 353 DCHECK(!ContainsKey(in_progress_, download_id)); | 353 DCHECK(!ContainsKey(in_progress_, download_id)); |
| 354 DCHECK(ContainsKey(downloads_, download)); | 354 DCHECK(ContainsKey(downloads_, download)); |
| 355 DCHECK(ContainsKey(active_downloads_, download_id)); | 355 DCHECK(ContainsKey(active_downloads_, download_id)); |
| 356 | 356 |
| 357 // Make sure the initial file name is set only once. | 357 // Make sure the initial file name is set only once. |
| 358 download->OnPathDetermined(chosen_file); | 358 download->OnPathDetermined(chosen_file); |
| 359 | 359 |
| 360 VLOG(20) << __FUNCTION__ << "()" | 360 VLOG(20) << __FUNCTION__ << "()" |
| 361 << " download = " << download->DebugString(true); | 361 << " download = " << download->DebugString(true); |
| 362 | 362 |
| 363 in_progress_[download_id] = download; | 363 in_progress_[download_id] = download; |
| 364 UpdateDownloadProgress(); // Reflect entry into in_progress_. | 364 UpdateDownloadProgress(); // Reflect entry into in_progress_. |
| 365 | 365 |
| 366 // Rename to intermediate name. | 366 // Rename to intermediate name. |
| 367 FilePath download_path; | 367 FilePath download_path; |
| 368 if (!delegate_->OverrideIntermediatePath(download, &download_path)) | 368 if (!delegate_->OverrideIntermediatePath(download, &download_path)) |
| 369 download_path = download->full_path(); | 369 download_path = download->GetFullPath(); |
| 370 | 370 |
| 371 BrowserThread::PostTask( | 371 BrowserThread::PostTask( |
| 372 BrowserThread::FILE, FROM_HERE, | 372 BrowserThread::FILE, FROM_HERE, |
| 373 base::Bind(&DownloadFileManager::RenameInProgressDownloadFile, | 373 base::Bind(&DownloadFileManager::RenameInProgressDownloadFile, |
| 374 file_manager_, download->global_id(), download_path)); | 374 file_manager_, download->GetGlobalId(), download_path)); |
| 375 | 375 |
| 376 download->Rename(download_path); | 376 download->Rename(download_path); |
| 377 | 377 |
| 378 delegate_->AddItemToPersistentStore(download); | 378 delegate_->AddItemToPersistentStore(download); |
| 379 } | 379 } |
| 380 | 380 |
| 381 void DownloadManagerImpl::UpdateDownload(int32 download_id, int64 size) { | 381 void DownloadManagerImpl::UpdateDownload(int32 download_id, int64 size) { |
| 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 383 DownloadMap::iterator it = active_downloads_.find(download_id); | 383 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 384 if (it != active_downloads_.end()) { | 384 if (it != active_downloads_.end()) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 406 | 406 |
| 407 DownloadItem* download = active_downloads_[download_id]; | 407 DownloadItem* download = active_downloads_[download_id]; |
| 408 download->OnAllDataSaved(size, hash); | 408 download->OnAllDataSaved(size, hash); |
| 409 delegate_->OnResponseCompleted(download); | 409 delegate_->OnResponseCompleted(download); |
| 410 | 410 |
| 411 MaybeCompleteDownload(download); | 411 MaybeCompleteDownload(download); |
| 412 } | 412 } |
| 413 | 413 |
| 414 void DownloadManagerImpl::AssertQueueStateConsistent(DownloadItem* download) { | 414 void DownloadManagerImpl::AssertQueueStateConsistent(DownloadItem* download) { |
| 415 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 415 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 416 if (download->state() == DownloadItem::REMOVING) { | 416 if (download->GetState() == DownloadItem::REMOVING) { |
| 417 CHECK(!ContainsKey(downloads_, download)); | 417 CHECK(!ContainsKey(downloads_, download)); |
| 418 CHECK(!ContainsKey(active_downloads_, download->id())); | 418 CHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 419 CHECK(!ContainsKey(in_progress_, download->id())); | 419 CHECK(!ContainsKey(in_progress_, download->GetId())); |
| 420 CHECK(!ContainsKey(history_downloads_, download->db_handle())); | 420 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); |
| 421 return; | 421 return; |
| 422 } | 422 } |
| 423 | 423 |
| 424 // Should be in downloads_ if we're not REMOVING. | 424 // Should be in downloads_ if we're not REMOVING. |
| 425 CHECK(ContainsKey(downloads_, download)); | 425 CHECK(ContainsKey(downloads_, download)); |
| 426 | 426 |
| 427 // Check history_downloads_ consistency. | 427 // Check history_downloads_ consistency. |
| 428 if (download->db_handle() != DownloadItem::kUninitializedHandle) { | 428 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) { |
| 429 CHECK(ContainsKey(history_downloads_, download->db_handle())); | 429 CHECK(ContainsKey(history_downloads_, download->GetDbHandle())); |
| 430 } else { | 430 } else { |
| 431 // TODO(rdsmith): Somewhat painful; make sure to disable in | 431 // TODO(rdsmith): Somewhat painful; make sure to disable in |
| 432 // release builds after resolution of http://crbug.com/85408. | 432 // release builds after resolution of http://crbug.com/85408. |
| 433 for (DownloadMap::iterator it = history_downloads_.begin(); | 433 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 434 it != history_downloads_.end(); ++it) { | 434 it != history_downloads_.end(); ++it) { |
| 435 CHECK(it->second != download); | 435 CHECK(it->second != download); |
| 436 } | 436 } |
| 437 } | 437 } |
| 438 | 438 |
| 439 int64 state = download->state(); | 439 int64 state = download->GetState(); |
| 440 base::debug::Alias(&state); | 440 base::debug::Alias(&state); |
| 441 if (ContainsKey(active_downloads_, download->id())) { | 441 if (ContainsKey(active_downloads_, download->GetId())) { |
| 442 if (download->db_handle() != DownloadItem::kUninitializedHandle) | 442 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) |
| 443 CHECK_EQ(DownloadItem::IN_PROGRESS, download->state()); | 443 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 444 if (DownloadItem::IN_PROGRESS != download->state()) | 444 if (DownloadItem::IN_PROGRESS != download->GetState()) |
| 445 CHECK_EQ(DownloadItem::kUninitializedHandle, download->db_handle()); | 445 CHECK_EQ(DownloadItem::kUninitializedHandle, download->GetDbHandle()); |
| 446 } | 446 } |
| 447 if (DownloadItem::IN_PROGRESS == download->state()) | 447 if (DownloadItem::IN_PROGRESS == download->GetState()) |
| 448 CHECK(ContainsKey(active_downloads_, download->id())); | 448 CHECK(ContainsKey(active_downloads_, download->GetId())); |
| 449 } | 449 } |
| 450 | 450 |
| 451 bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) { | 451 bool DownloadManagerImpl::IsDownloadReadyForCompletion(DownloadItem* download) { |
| 452 // If we don't have all the data, the download is not ready for | 452 // If we don't have all the data, the download is not ready for |
| 453 // completion. | 453 // completion. |
| 454 if (!download->all_data_saved()) | 454 if (!download->AllDataSaved()) |
| 455 return false; | 455 return false; |
| 456 | 456 |
| 457 // If the download is dangerous, but not yet validated, it's not ready for | 457 // If the download is dangerous, but not yet validated, it's not ready for |
| 458 // completion. | 458 // completion. |
| 459 if (download->safety_state() == DownloadItem::DANGEROUS) | 459 if (download->GetSafetyState() == DownloadItem::DANGEROUS) |
| 460 return false; | 460 return false; |
| 461 | 461 |
| 462 // If the download isn't active (e.g. has been cancelled) it's not | 462 // If the download isn't active (e.g. has been cancelled) it's not |
| 463 // ready for completion. | 463 // ready for completion. |
| 464 if (active_downloads_.count(download->id()) == 0) | 464 if (active_downloads_.count(download->GetId()) == 0) |
| 465 return false; | 465 return false; |
| 466 | 466 |
| 467 // If the download hasn't been inserted into the history system | 467 // If the download hasn't been inserted into the history system |
| 468 // (which occurs strictly after file name determination, intermediate | 468 // (which occurs strictly after file name determination, intermediate |
| 469 // file rename, and UI display) then it's not ready for completion. | 469 // file rename, and UI display) then it's not ready for completion. |
| 470 if (download->db_handle() == DownloadItem::kUninitializedHandle) | 470 if (download->GetDbHandle() == DownloadItem::kUninitializedHandle) |
| 471 return false; | 471 return false; |
| 472 | 472 |
| 473 return true; | 473 return true; |
| 474 } | 474 } |
| 475 | 475 |
| 476 void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) { | 476 void DownloadManagerImpl::MaybeCompleteDownload(DownloadItem* download) { |
| 477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 478 VLOG(20) << __FUNCTION__ << "()" << " download = " | 478 VLOG(20) << __FUNCTION__ << "()" << " download = " |
| 479 << download->DebugString(false); | 479 << download->DebugString(false); |
| 480 | 480 |
| 481 if (!IsDownloadReadyForCompletion(download)) | 481 if (!IsDownloadReadyForCompletion(download)) |
| 482 return; | 482 return; |
| 483 | 483 |
| 484 // TODO(rdsmith): DCHECK that we only pass through this point | 484 // TODO(rdsmith): DCHECK that we only pass through this point |
| 485 // once per download. The natural way to do this is by a state | 485 // once per download. The natural way to do this is by a state |
| 486 // transition on the DownloadItem. | 486 // transition on the DownloadItem. |
| 487 | 487 |
| 488 // Confirm we're in the proper set of states to be here; | 488 // Confirm we're in the proper set of states to be here; |
| 489 // in in_progress_, have all data, have a history handle, (validated or safe). | 489 // in in_progress_, have all data, have a history handle, (validated or safe). |
| 490 DCHECK_NE(DownloadItem::DANGEROUS, download->safety_state()); | 490 DCHECK_NE(DownloadItem::DANGEROUS, download->GetSafetyState()); |
| 491 DCHECK_EQ(1u, in_progress_.count(download->id())); | 491 DCHECK_EQ(1u, in_progress_.count(download->GetId())); |
| 492 DCHECK(download->all_data_saved()); | 492 DCHECK(download->AllDataSaved()); |
| 493 DCHECK(download->db_handle() != DownloadItem::kUninitializedHandle); | 493 DCHECK(download->GetDbHandle() != DownloadItem::kUninitializedHandle); |
| 494 DCHECK_EQ(1u, history_downloads_.count(download->db_handle())); | 494 DCHECK_EQ(1u, history_downloads_.count(download->GetDbHandle())); |
| 495 | 495 |
| 496 // Give the delegate a chance to override. | 496 // Give the delegate a chance to override. |
| 497 if (!delegate_->ShouldCompleteDownload(download)) | 497 if (!delegate_->ShouldCompleteDownload(download)) |
| 498 return; | 498 return; |
| 499 | 499 |
| 500 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " | 500 VLOG(20) << __FUNCTION__ << "()" << " executing: download = " |
| 501 << download->DebugString(false); | 501 << download->DebugString(false); |
| 502 | 502 |
| 503 // Remove the id from in_progress | 503 // Remove the id from in_progress |
| 504 in_progress_.erase(download->id()); | 504 in_progress_.erase(download->GetId()); |
| 505 UpdateDownloadProgress(); // Reflect removal from in_progress_. | 505 UpdateDownloadProgress(); // Reflect removal from in_progress_. |
| 506 | 506 |
| 507 delegate_->UpdateItemInPersistentStore(download); | 507 delegate_->UpdateItemInPersistentStore(download); |
| 508 | 508 |
| 509 // Finish the download. | 509 // Finish the download. |
| 510 download->OnDownloadCompleting(file_manager_); | 510 download->OnDownloadCompleting(file_manager_); |
| 511 } | 511 } |
| 512 | 512 |
| 513 void DownloadManagerImpl::DownloadCompleted(int32 download_id) { | 513 void DownloadManagerImpl::DownloadCompleted(int32 download_id) { |
| 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 525 int uniquifier) { | 525 int uniquifier) { |
| 526 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | 526 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id |
| 527 << " full_path = \"" << full_path.value() << "\"" | 527 << " full_path = \"" << full_path.value() << "\"" |
| 528 << " uniquifier = " << uniquifier; | 528 << " uniquifier = " << uniquifier; |
| 529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 529 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 530 | 530 |
| 531 DownloadItem* item = GetDownloadItem(download_id); | 531 DownloadItem* item = GetDownloadItem(download_id); |
| 532 if (!item) | 532 if (!item) |
| 533 return; | 533 return; |
| 534 | 534 |
| 535 if (item->safety_state() == DownloadItem::SAFE) { | 535 if (item->GetSafetyState() == DownloadItem::SAFE) { |
| 536 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; | 536 DCHECK_EQ(0, uniquifier) << "We should not uniquify SAFE downloads twice"; |
| 537 } | 537 } |
| 538 | 538 |
| 539 BrowserThread::PostTask( | 539 BrowserThread::PostTask( |
| 540 BrowserThread::FILE, FROM_HERE, | 540 BrowserThread::FILE, FROM_HERE, |
| 541 base::Bind(&DownloadFileManager::CompleteDownload, | 541 base::Bind(&DownloadFileManager::CompleteDownload, |
| 542 file_manager_, item->global_id())); | 542 file_manager_, item->GetGlobalId())); |
| 543 | 543 |
| 544 if (uniquifier) | 544 if (uniquifier) |
| 545 item->set_path_uniquifier(uniquifier); | 545 item->SetPathUniquifier(uniquifier); |
| 546 | 546 |
| 547 item->OnDownloadRenamedToFinalName(full_path); | 547 item->OnDownloadRenamedToFinalName(full_path); |
| 548 delegate_->UpdatePathForItemInPersistentStore(item, full_path); | 548 delegate_->UpdatePathForItemInPersistentStore(item, full_path); |
| 549 } | 549 } |
| 550 | 550 |
| 551 void DownloadManagerImpl::CancelDownload(int32 download_id) { | 551 void DownloadManagerImpl::CancelDownload(int32 download_id) { |
| 552 DownloadItem* download = GetActiveDownload(download_id); | 552 DownloadItem* download = GetActiveDownload(download_id); |
| 553 // A cancel at the right time could remove the download from the | 553 // A cancel at the right time could remove the download from the |
| 554 // |active_downloads_| map before we get here. | 554 // |active_downloads_| map before we get here. |
| 555 if (!download) | 555 if (!download) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 577 int64 size, | 577 int64 size, |
| 578 InterruptReason reason) { | 578 InterruptReason reason) { |
| 579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 579 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 580 | 580 |
| 581 DownloadItem* download = GetActiveDownload(download_id); | 581 DownloadItem* download = GetActiveDownload(download_id); |
| 582 if (!download) | 582 if (!download) |
| 583 return; | 583 return; |
| 584 | 584 |
| 585 VLOG(20) << __FUNCTION__ << "()" | 585 VLOG(20) << __FUNCTION__ << "()" |
| 586 << " reason " << InterruptReasonDebugString(reason) | 586 << " reason " << InterruptReasonDebugString(reason) |
| 587 << " at offset " << download->received_bytes() | 587 << " at offset " << download->GetReceivedBytes() |
| 588 << " size = " << size | 588 << " size = " << size |
| 589 << " download = " << download->DebugString(true); | 589 << " download = " << download->DebugString(true); |
| 590 | 590 |
| 591 RemoveFromActiveList(download); | 591 RemoveFromActiveList(download); |
| 592 download->Interrupted(size, reason); | 592 download->Interrupted(size, reason); |
| 593 download->OffThreadCancel(file_manager_); | 593 download->OffThreadCancel(file_manager_); |
| 594 } | 594 } |
| 595 | 595 |
| 596 DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) { | 596 DownloadItem* DownloadManagerImpl::GetActiveDownload(int32 download_id) { |
| 597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 598 DownloadMap::iterator it = active_downloads_.find(download_id); | 598 DownloadMap::iterator it = active_downloads_.find(download_id); |
| 599 if (it == active_downloads_.end()) | 599 if (it == active_downloads_.end()) |
| 600 return NULL; | 600 return NULL; |
| 601 | 601 |
| 602 DownloadItem* download = it->second; | 602 DownloadItem* download = it->second; |
| 603 | 603 |
| 604 DCHECK(download); | 604 DCHECK(download); |
| 605 DCHECK_EQ(download_id, download->id()); | 605 DCHECK_EQ(download_id, download->GetId()); |
| 606 | 606 |
| 607 return download; | 607 return download; |
| 608 } | 608 } |
| 609 | 609 |
| 610 void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) { | 610 void DownloadManagerImpl::RemoveFromActiveList(DownloadItem* download) { |
| 611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 612 DCHECK(download); | 612 DCHECK(download); |
| 613 | 613 |
| 614 // Clean up will happen when the history system create callback runs if we | 614 // Clean up will happen when the history system create callback runs if we |
| 615 // don't have a valid db_handle yet. | 615 // don't have a valid db_handle yet. |
| 616 if (download->db_handle() != DownloadItem::kUninitializedHandle) { | 616 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) { |
| 617 in_progress_.erase(download->id()); | 617 in_progress_.erase(download->GetId()); |
| 618 active_downloads_.erase(download->id()); | 618 active_downloads_.erase(download->GetId()); |
| 619 UpdateDownloadProgress(); // Reflect removal from in_progress_. | 619 UpdateDownloadProgress(); // Reflect removal from in_progress_. |
| 620 delegate_->UpdateItemInPersistentStore(download); | 620 delegate_->UpdateItemInPersistentStore(download); |
| 621 } | 621 } |
| 622 } | 622 } |
| 623 | 623 |
| 624 content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const { | 624 content::DownloadManagerDelegate* DownloadManagerImpl::delegate() const { |
| 625 return delegate_; | 625 return delegate_; |
| 626 } | 626 } |
| 627 | 627 |
| 628 void DownloadManagerImpl::SetDownloadManagerDelegate( | 628 void DownloadManagerImpl::SetDownloadManagerDelegate( |
| 629 content::DownloadManagerDelegate* delegate) { | 629 content::DownloadManagerDelegate* delegate) { |
| 630 delegate_ = delegate; | 630 delegate_ = delegate; |
| 631 } | 631 } |
| 632 | 632 |
| 633 void DownloadManagerImpl::UpdateDownloadProgress() { | 633 void DownloadManagerImpl::UpdateDownloadProgress() { |
| 634 delegate_->DownloadProgressUpdated(); | 634 delegate_->DownloadProgressUpdated(); |
| 635 } | 635 } |
| 636 | 636 |
| 637 int DownloadManagerImpl::RemoveDownloadItems( | 637 int DownloadManagerImpl::RemoveDownloadItems( |
| 638 const DownloadVector& pending_deletes) { | 638 const DownloadVector& pending_deletes) { |
| 639 if (pending_deletes.empty()) | 639 if (pending_deletes.empty()) |
| 640 return 0; | 640 return 0; |
| 641 | 641 |
| 642 // Delete from internal maps. | 642 // Delete from internal maps. |
| 643 for (DownloadVector::const_iterator it = pending_deletes.begin(); | 643 for (DownloadVector::const_iterator it = pending_deletes.begin(); |
| 644 it != pending_deletes.end(); | 644 it != pending_deletes.end(); |
| 645 ++it) { | 645 ++it) { |
| 646 DownloadItem* download = *it; | 646 DownloadItem* download = *it; |
| 647 DCHECK(download); | 647 DCHECK(download); |
| 648 history_downloads_.erase(download->db_handle()); | 648 history_downloads_.erase(download->GetDbHandle()); |
| 649 save_page_downloads_.erase(download->id()); | 649 save_page_downloads_.erase(download->GetId()); |
| 650 downloads_.erase(download); | 650 downloads_.erase(download); |
| 651 } | 651 } |
| 652 | 652 |
| 653 // Tell observers to refresh their views. | 653 // Tell observers to refresh their views. |
| 654 NotifyModelChanged(); | 654 NotifyModelChanged(); |
| 655 | 655 |
| 656 // Delete the download items themselves. | 656 // Delete the download items themselves. |
| 657 const int num_deleted = static_cast<int>(pending_deletes.size()); | 657 const int num_deleted = static_cast<int>(pending_deletes.size()); |
| 658 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); | 658 STLDeleteContainerPointers(pending_deletes.begin(), pending_deletes.end()); |
| 659 return num_deleted; | 659 return num_deleted; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 677 const base::Time remove_end) { | 677 const base::Time remove_end) { |
| 678 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); | 678 delegate_->RemoveItemsFromPersistentStoreBetween(remove_begin, remove_end); |
| 679 | 679 |
| 680 // All downloads visible to the user will be in the history, | 680 // All downloads visible to the user will be in the history, |
| 681 // so scan that map. | 681 // so scan that map. |
| 682 DownloadVector pending_deletes; | 682 DownloadVector pending_deletes; |
| 683 for (DownloadMap::const_iterator it = history_downloads_.begin(); | 683 for (DownloadMap::const_iterator it = history_downloads_.begin(); |
| 684 it != history_downloads_.end(); | 684 it != history_downloads_.end(); |
| 685 ++it) { | 685 ++it) { |
| 686 DownloadItem* download = it->second; | 686 DownloadItem* download = it->second; |
| 687 if (download->start_time() >= remove_begin && | 687 if (download->GetStartTime() >= remove_begin && |
| 688 (remove_end.is_null() || download->start_time() < remove_end) && | 688 (remove_end.is_null() || download->GetStartTime() < remove_end) && |
| 689 (download->IsComplete() || download->IsCancelled())) { | 689 (download->IsComplete() || download->IsCancelled())) { |
| 690 AssertQueueStateConsistent(download); | 690 AssertQueueStateConsistent(download); |
| 691 pending_deletes.push_back(download); | 691 pending_deletes.push_back(download); |
| 692 } | 692 } |
| 693 } | 693 } |
| 694 return RemoveDownloadItems(pending_deletes); | 694 return RemoveDownloadItems(pending_deletes); |
| 695 } | 695 } |
| 696 | 696 |
| 697 int DownloadManagerImpl::RemoveDownloads(const base::Time remove_begin) { | 697 int DownloadManagerImpl::RemoveDownloads(const base::Time remove_begin) { |
| 698 return RemoveDownloadsBetween(remove_begin, base::Time()); | 698 return RemoveDownloadsBetween(remove_begin, base::Time()); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 observer->ModelChanged(); | 740 observer->ModelChanged(); |
| 741 } | 741 } |
| 742 | 742 |
| 743 void DownloadManagerImpl::RemoveObserver(Observer* observer) { | 743 void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
| 744 observers_.RemoveObserver(observer); | 744 observers_.RemoveObserver(observer); |
| 745 } | 745 } |
| 746 | 746 |
| 747 bool DownloadManagerImpl::IsDownloadProgressKnown() const { | 747 bool DownloadManagerImpl::IsDownloadProgressKnown() const { |
| 748 for (DownloadMap::const_iterator i = in_progress_.begin(); | 748 for (DownloadMap::const_iterator i = in_progress_.begin(); |
| 749 i != in_progress_.end(); ++i) { | 749 i != in_progress_.end(); ++i) { |
| 750 if (i->second->total_bytes() <= 0) | 750 if (i->second->GetTotalBytes() <= 0) |
| 751 return false; | 751 return false; |
| 752 } | 752 } |
| 753 | 753 |
| 754 return true; | 754 return true; |
| 755 } | 755 } |
| 756 | 756 |
| 757 int64 DownloadManagerImpl::GetInProgressDownloadCount() const { | 757 int64 DownloadManagerImpl::GetInProgressDownloadCount() const { |
| 758 return in_progress_.size(); | 758 return in_progress_.size(); |
| 759 } | 759 } |
| 760 | 760 |
| 761 int64 DownloadManagerImpl::GetReceivedDownloadBytes() const { | 761 int64 DownloadManagerImpl::GetReceivedDownloadBytes() const { |
| 762 DCHECK(IsDownloadProgressKnown()); | 762 DCHECK(IsDownloadProgressKnown()); |
| 763 int64 received_bytes = 0; | 763 int64 received_bytes = 0; |
| 764 for (DownloadMap::const_iterator i = in_progress_.begin(); | 764 for (DownloadMap::const_iterator i = in_progress_.begin(); |
| 765 i != in_progress_.end(); ++i) { | 765 i != in_progress_.end(); ++i) { |
| 766 received_bytes += i->second->received_bytes(); | 766 received_bytes += i->second->GetReceivedBytes(); |
| 767 } | 767 } |
| 768 return received_bytes; | 768 return received_bytes; |
| 769 } | 769 } |
| 770 | 770 |
| 771 int64 DownloadManagerImpl::GetTotalDownloadBytes() const { | 771 int64 DownloadManagerImpl::GetTotalDownloadBytes() const { |
| 772 DCHECK(IsDownloadProgressKnown()); | 772 DCHECK(IsDownloadProgressKnown()); |
| 773 int64 total_bytes = 0; | 773 int64 total_bytes = 0; |
| 774 for (DownloadMap::const_iterator i = in_progress_.begin(); | 774 for (DownloadMap::const_iterator i = in_progress_.begin(); |
| 775 i != in_progress_.end(); ++i) { | 775 i != in_progress_.end(); ++i) { |
| 776 total_bytes += i->second->total_bytes(); | 776 total_bytes += i->second->GetTotalBytes(); |
| 777 } | 777 } |
| 778 return total_bytes; | 778 return total_bytes; |
| 779 } | 779 } |
| 780 | 780 |
| 781 void DownloadManagerImpl::FileSelected(const FilePath& path, void* params) { | 781 void DownloadManagerImpl::FileSelected(const FilePath& path, void* params) { |
| 782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 782 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 783 | 783 |
| 784 int32* id_ptr = reinterpret_cast<int32*>(params); | 784 int32* id_ptr = reinterpret_cast<int32*>(params); |
| 785 DCHECK(id_ptr != NULL); | 785 DCHECK(id_ptr != NULL); |
| 786 int32 download_id = *id_ptr; | 786 int32 download_id = *id_ptr; |
| 787 delete id_ptr; | 787 delete id_ptr; |
| 788 | 788 |
| 789 DownloadItem* download = GetActiveDownloadItem(download_id); | 789 DownloadItem* download = GetActiveDownloadItem(download_id); |
| 790 if (!download) | 790 if (!download) |
| 791 return; | 791 return; |
| 792 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\"" | 792 VLOG(20) << __FUNCTION__ << "()" << " path = \"" << path.value() << "\"" |
| 793 << " download = " << download->DebugString(true); | 793 << " download = " << download->DebugString(true); |
| 794 | 794 |
| 795 if (download->prompt_user_for_save_location()) | 795 if (download->PromptUserForSaveLocation()) |
| 796 last_download_path_ = path.DirName(); | 796 last_download_path_ = path.DirName(); |
| 797 | 797 |
| 798 // Make sure the initial file name is set only once. | 798 // Make sure the initial file name is set only once. |
| 799 ContinueDownloadWithPath(download, path); | 799 ContinueDownloadWithPath(download, path); |
| 800 } | 800 } |
| 801 | 801 |
| 802 void DownloadManagerImpl::FileSelectionCanceled(void* params) { | 802 void DownloadManagerImpl::FileSelectionCanceled(void* params) { |
| 803 // The user didn't pick a place to save the file, so need to cancel the | 803 // The user didn't pick a place to save the file, so need to cancel the |
| 804 // download that's already in progress to the temporary location. | 804 // download that's already in progress to the temporary location. |
| 805 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 805 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 827 | 827 |
| 828 // The history service has retrieved all download entries. 'entries' contains | 828 // The history service has retrieved all download entries. 'entries' contains |
| 829 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 829 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
| 830 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 830 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
| 831 std::vector<DownloadPersistentStoreInfo>* entries) { | 831 std::vector<DownloadPersistentStoreInfo>* entries) { |
| 832 // TODO(rdsmith): Remove this and related logic when | 832 // TODO(rdsmith): Remove this and related logic when |
| 833 // http://crbug.com/85408 is fixed. | 833 // http://crbug.com/85408 is fixed. |
| 834 largest_db_handle_in_history_ = 0; | 834 largest_db_handle_in_history_ = 0; |
| 835 | 835 |
| 836 for (size_t i = 0; i < entries->size(); ++i) { | 836 for (size_t i = 0; i < entries->size(); ++i) { |
| 837 DownloadItem* download = new DownloadItem(this, entries->at(i)); | 837 DownloadItem* download = new DownloadItemImpl(this, entries->at(i)); |
| 838 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 838 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
| 839 CHECK(!ContainsKey(history_downloads_, download->db_handle())); | 839 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); |
| 840 downloads_.insert(download); | 840 downloads_.insert(download); |
| 841 history_downloads_[download->db_handle()] = download; | 841 history_downloads_[download->GetDbHandle()] = download; |
| 842 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 842 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 843 << " download = " << download->DebugString(true); | 843 << " download = " << download->DebugString(true); |
| 844 | 844 |
| 845 if (download->db_handle() > largest_db_handle_in_history_) | 845 if (download->GetDbHandle() > largest_db_handle_in_history_) |
| 846 largest_db_handle_in_history_ = download->db_handle(); | 846 largest_db_handle_in_history_ = download->GetDbHandle(); |
| 847 } | 847 } |
| 848 NotifyModelChanged(); | 848 NotifyModelChanged(); |
| 849 CheckForHistoryFilesRemoval(); | 849 CheckForHistoryFilesRemoval(); |
| 850 } | 850 } |
| 851 | 851 |
| 852 void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download, | 852 void DownloadManagerImpl::AddDownloadItemToHistory(DownloadItem* download, |
| 853 int64 db_handle) { | 853 int64 db_handle) { |
| 854 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 854 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 855 | 855 |
| 856 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408 | 856 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408 |
| 857 // is fixed. | 857 // is fixed. |
| 858 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle); | 858 CHECK_NE(DownloadItem::kUninitializedHandle, db_handle); |
| 859 | 859 |
| 860 download_stats::RecordHistorySize(history_downloads_.size()); | 860 download_stats::RecordHistorySize(history_downloads_.size()); |
| 861 | 861 |
| 862 DCHECK(download->db_handle() == DownloadItem::kUninitializedHandle); | 862 DCHECK(download->GetDbHandle() == DownloadItem::kUninitializedHandle); |
| 863 download->set_db_handle(db_handle); | 863 download->SetDbHandle(db_handle); |
| 864 | 864 |
| 865 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408 | 865 // TODO(rdsmith): Convert to DCHECK() when http://crbug.com/85408 |
| 866 // is fixed. | 866 // is fixed. |
| 867 CHECK(!ContainsKey(history_downloads_, download->db_handle())); | 867 CHECK(!ContainsKey(history_downloads_, download->GetDbHandle())); |
| 868 history_downloads_[download->db_handle()] = download; | 868 history_downloads_[download->GetDbHandle()] = download; |
| 869 | 869 |
| 870 // Show in the appropriate browser UI. | 870 // Show in the appropriate browser UI. |
| 871 // This includes buttons to save or cancel, for a dangerous download. | 871 // This includes buttons to save or cancel, for a dangerous download. |
| 872 ShowDownloadInBrowser(download); | 872 ShowDownloadInBrowser(download); |
| 873 | 873 |
| 874 // Inform interested objects about the new download. | 874 // Inform interested objects about the new download. |
| 875 NotifyModelChanged(); | 875 NotifyModelChanged(); |
| 876 } | 876 } |
| 877 | 877 |
| 878 | 878 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 898 | 898 |
| 899 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle | 899 VLOG(20) << __FUNCTION__ << "()" << " db_handle = " << db_handle |
| 900 << " download_id = " << download_id | 900 << " download_id = " << download_id |
| 901 << " download = " << download->DebugString(true); | 901 << " download = " << download->DebugString(true); |
| 902 | 902 |
| 903 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 903 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
| 904 int64 largest_handle = largest_db_handle_in_history_; | 904 int64 largest_handle = largest_db_handle_in_history_; |
| 905 base::debug::Alias(&largest_handle); | 905 base::debug::Alias(&largest_handle); |
| 906 int32 matching_item_download_id | 906 int32 matching_item_download_id |
| 907 = (ContainsKey(history_downloads_, db_handle) ? | 907 = (ContainsKey(history_downloads_, db_handle) ? |
| 908 history_downloads_[db_handle]->id() : 0); | 908 history_downloads_[db_handle]->GetId() : 0); |
| 909 base::debug::Alias(&matching_item_download_id); | 909 base::debug::Alias(&matching_item_download_id); |
| 910 | 910 |
| 911 CHECK(!ContainsKey(history_downloads_, db_handle)); | 911 CHECK(!ContainsKey(history_downloads_, db_handle)); |
| 912 | 912 |
| 913 AddDownloadItemToHistory(download, db_handle); | 913 AddDownloadItemToHistory(download, db_handle); |
| 914 | 914 |
| 915 // If the download is still in progress, try to complete it. | 915 // If the download is still in progress, try to complete it. |
| 916 // | 916 // |
| 917 // Otherwise, download has been cancelled or interrupted before we've | 917 // Otherwise, download has been cancelled or interrupted before we've |
| 918 // received the DB handle. We post one final message to the history | 918 // received the DB handle. We post one final message to the history |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 void DownloadManagerImpl::NotifyModelChanged() { | 959 void DownloadManagerImpl::NotifyModelChanged() { |
| 960 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); | 960 FOR_EACH_OBSERVER(Observer, observers_, ModelChanged()); |
| 961 } | 961 } |
| 962 | 962 |
| 963 DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) { | 963 DownloadItem* DownloadManagerImpl::GetDownloadItem(int download_id) { |
| 964 // The |history_downloads_| map is indexed by the download's db_handle, | 964 // The |history_downloads_| map is indexed by the download's db_handle, |
| 965 // not its id, so we have to iterate. | 965 // not its id, so we have to iterate. |
| 966 for (DownloadMap::iterator it = history_downloads_.begin(); | 966 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 967 it != history_downloads_.end(); ++it) { | 967 it != history_downloads_.end(); ++it) { |
| 968 DownloadItem* item = it->second; | 968 DownloadItem* item = it->second; |
| 969 if (item->id() == download_id) | 969 if (item->GetId() == download_id) |
| 970 return item; | 970 return item; |
| 971 } | 971 } |
| 972 return NULL; | 972 return NULL; |
| 973 } | 973 } |
| 974 | 974 |
| 975 DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) { | 975 DownloadItem* DownloadManagerImpl::GetActiveDownloadItem(int download_id) { |
| 976 DCHECK(ContainsKey(active_downloads_, download_id)); | 976 DCHECK(ContainsKey(active_downloads_, download_id)); |
| 977 DownloadItem* download = active_downloads_[download_id]; | 977 DownloadItem* download = active_downloads_[download_id]; |
| 978 DCHECK(download != NULL); | 978 DCHECK(download != NULL); |
| 979 return download; | 979 return download; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1018 std::insert_iterator<DownloadSet> | 1018 std::insert_iterator<DownloadSet> |
| 1019 insert_remainder(remainder, remainder.begin()); | 1019 insert_remainder(remainder, remainder.begin()); |
| 1020 std::set_difference(downloads_.begin(), downloads_.end(), | 1020 std::set_difference(downloads_.begin(), downloads_.end(), |
| 1021 downloads_union.begin(), downloads_union.end(), | 1021 downloads_union.begin(), downloads_union.end(), |
| 1022 insert_remainder); | 1022 insert_remainder); |
| 1023 DCHECK(remainder.empty()); | 1023 DCHECK(remainder.empty()); |
| 1024 #endif | 1024 #endif |
| 1025 } | 1025 } |
| 1026 | 1026 |
| 1027 void DownloadManagerImpl::SavePageDownloadStarted(DownloadItem* download) { | 1027 void DownloadManagerImpl::SavePageDownloadStarted(DownloadItem* download) { |
| 1028 DCHECK(!ContainsKey(save_page_downloads_, download->id())); | 1028 DCHECK(!ContainsKey(save_page_downloads_, download->GetId())); |
| 1029 downloads_.insert(download); | 1029 downloads_.insert(download); |
| 1030 save_page_downloads_[download->id()] = download; | 1030 save_page_downloads_[download->GetId()] = download; |
| 1031 | 1031 |
| 1032 // Add this entry to the history service. | 1032 // Add this entry to the history service. |
| 1033 // Additionally, the UI is notified in the callback. | 1033 // Additionally, the UI is notified in the callback. |
| 1034 delegate_->AddItemToPersistentStore(download); | 1034 delegate_->AddItemToPersistentStore(download); |
| 1035 } | 1035 } |
| 1036 | 1036 |
| 1037 // SavePackage will call SavePageDownloadFinished upon completion/cancellation. | 1037 // SavePackage will call SavePageDownloadFinished upon completion/cancellation. |
| 1038 // The history callback will call OnSavePageItemAddedToPersistentStore. | 1038 // The history callback will call OnSavePageItemAddedToPersistentStore. |
| 1039 // If the download finishes before the history callback, | 1039 // If the download finishes before the history callback, |
| 1040 // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring | 1040 // OnSavePageItemAddedToPersistentStore calls SavePageDownloadFinished, ensuring |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1070 CHECK(!ContainsKey(history_downloads_, db_handle)); | 1070 CHECK(!ContainsKey(history_downloads_, db_handle)); |
| 1071 | 1071 |
| 1072 AddDownloadItemToHistory(download, db_handle); | 1072 AddDownloadItemToHistory(download, db_handle); |
| 1073 | 1073 |
| 1074 // Finalize this download if it finished before the history callback. | 1074 // Finalize this download if it finished before the history callback. |
| 1075 if (!download->IsInProgress()) | 1075 if (!download->IsInProgress()) |
| 1076 SavePageDownloadFinished(download); | 1076 SavePageDownloadFinished(download); |
| 1077 } | 1077 } |
| 1078 | 1078 |
| 1079 void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) { | 1079 void DownloadManagerImpl::SavePageDownloadFinished(DownloadItem* download) { |
| 1080 if (download->db_handle() != DownloadItem::kUninitializedHandle) { | 1080 if (download->GetDbHandle() != DownloadItem::kUninitializedHandle) { |
| 1081 delegate_->UpdateItemInPersistentStore(download); | 1081 delegate_->UpdateItemInPersistentStore(download); |
| 1082 DCHECK(ContainsKey(save_page_downloads_, download->id())); | 1082 DCHECK(ContainsKey(save_page_downloads_, download->GetId())); |
| 1083 save_page_downloads_.erase(download->id()); | 1083 save_page_downloads_.erase(download->GetId()); |
| 1084 | 1084 |
| 1085 if (download->IsComplete()) | 1085 if (download->IsComplete()) |
| 1086 content::NotificationService::current()->Notify( | 1086 content::NotificationService::current()->Notify( |
| 1087 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, | 1087 content::NOTIFICATION_SAVE_PACKAGE_SUCCESSFULLY_FINISHED, |
| 1088 content::Source<DownloadManager>(this), | 1088 content::Source<DownloadManager>(this), |
| 1089 content::Details<DownloadItem>(download)); | 1089 content::Details<DownloadItem>(download)); |
| 1090 } | 1090 } |
| 1091 } | 1091 } |
| 1092 | 1092 |
| 1093 void DownloadManagerImpl::MarkDownloadOpened(DownloadItem* download) { | 1093 void DownloadManagerImpl::MarkDownloadOpened(DownloadItem* download) { |
| 1094 delegate_->UpdateItemInPersistentStore(download); | 1094 delegate_->UpdateItemInPersistentStore(download); |
| 1095 int num_unopened = 0; | 1095 int num_unopened = 0; |
| 1096 for (DownloadMap::iterator it = history_downloads_.begin(); | 1096 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 1097 it != history_downloads_.end(); ++it) { | 1097 it != history_downloads_.end(); ++it) { |
| 1098 if (it->second->IsComplete() && !it->second->opened()) | 1098 if (it->second->IsComplete() && !it->second->GetOpened()) |
| 1099 ++num_unopened; | 1099 ++num_unopened; |
| 1100 } | 1100 } |
| 1101 download_stats::RecordOpensOutstanding(num_unopened); | 1101 download_stats::RecordOpensOutstanding(num_unopened); |
| 1102 } | 1102 } |
| 1103 | 1103 |
| 1104 void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) { | 1104 void DownloadManagerImpl::SetFileManager(DownloadFileManager* file_manager) { |
| 1105 file_manager_ = file_manager; | 1105 file_manager_ = file_manager; |
| 1106 } | 1106 } |
| OLD | NEW |