| 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 "chrome/browser/download/download_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" | |
| 12 #include "base/rand_util.h" | |
| 13 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/sys_string_conversions.h" | |
| 16 #include "base/task.h" | 12 #include "base/task.h" |
| 17 #include "base/utf_string_conversions.h" | |
| 18 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 19 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/download/download_create_info.h" | 15 #include "chrome/browser/download/download_create_info.h" |
| 21 #include "chrome/browser/download/download_extensions.h" | |
| 22 #include "chrome/browser/download/download_file_manager.h" | 16 #include "chrome/browser/download/download_file_manager.h" |
| 23 #include "chrome/browser/download/download_history.h" | 17 #include "chrome/browser/download/download_history.h" |
| 24 #include "chrome/browser/download/download_item.h" | 18 #include "chrome/browser/download/download_item.h" |
| 25 #include "chrome/browser/download/download_manager_delegate.h" | 19 #include "chrome/browser/download/download_manager_delegate.h" |
| 26 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
| 27 #include "chrome/browser/download/download_request_handle.h" | 21 #include "chrome/browser/download/download_request_handle.h" |
| 28 #include "chrome/browser/download/download_safe_browsing_client.h" | |
| 29 #include "chrome/browser/download/download_status_updater.h" | 22 #include "chrome/browser/download/download_status_updater.h" |
| 30 #include "chrome/browser/download/download_util.h" | 23 #include "chrome/browser/download/download_util.h" |
| 31 #include "chrome/browser/extensions/extension_service.h" | |
| 32 #include "chrome/browser/history/download_history_info.h" | 24 #include "chrome/browser/history/download_history_info.h" |
| 33 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 34 #include "chrome/browser/tab_contents/tab_util.h" | |
| 35 #include "chrome/common/chrome_paths.h" | |
| 36 #include "chrome/common/pref_names.h" | |
| 37 #include "content/browser/browser_thread.h" | 26 #include "content/browser/browser_thread.h" |
| 38 #include "content/browser/renderer_host/render_process_host.h" | 27 #include "content/browser/renderer_host/render_process_host.h" |
| 39 #include "content/browser/renderer_host/render_view_host.h" | 28 #include "content/browser/renderer_host/render_view_host.h" |
| 40 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 29 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 41 #include "content/browser/tab_contents/tab_contents.h" | 30 #include "content/browser/tab_contents/tab_contents.h" |
| 42 #include "content/common/content_notification_types.h" | 31 #include "content/common/content_notification_types.h" |
| 43 #include "content/common/notification_service.h" | 32 #include "content/common/notification_service.h" |
| 44 #include "googleurl/src/gurl.h" | |
| 45 #include "grit/generated_resources.h" | |
| 46 #include "grit/theme_resources.h" | |
| 47 #include "net/base/mime_util.h" | |
| 48 #include "net/base/net_util.h" | |
| 49 #include "ui/base/l10n/l10n_util.h" | |
| 50 #include "ui/base/resource/resource_bundle.h" | |
| 51 | 33 |
| 52 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, | 34 DownloadManager::DownloadManager(DownloadManagerDelegate* delegate, |
| 53 DownloadStatusUpdater* status_updater) | 35 DownloadStatusUpdater* status_updater) |
| 54 : shutdown_needed_(false), | 36 : shutdown_needed_(false), |
| 55 profile_(NULL), | 37 profile_(NULL), |
| 56 file_manager_(NULL), | 38 file_manager_(NULL), |
| 57 status_updater_(status_updater->AsWeakPtr()), | 39 status_updater_(status_updater->AsWeakPtr()), |
| 58 next_save_page_id_(0), | 40 next_save_page_id_(0), |
| 59 delegate_(delegate) { | 41 delegate_(delegate) { |
| 60 if (status_updater_) | 42 if (status_updater_) |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 file_manager_ = rdh->download_file_manager(); | 221 file_manager_ = rdh->download_file_manager(); |
| 240 DCHECK(file_manager_); | 222 DCHECK(file_manager_); |
| 241 } | 223 } |
| 242 | 224 |
| 243 other_download_manager_observer_.reset( | 225 other_download_manager_observer_.reset( |
| 244 new OtherDownloadManagerObserver(this)); | 226 new OtherDownloadManagerObserver(this)); |
| 245 | 227 |
| 246 return true; | 228 return true; |
| 247 } | 229 } |
| 248 | 230 |
| 249 // We have received a message from DownloadFileManager about a new download. We | 231 // We have received a message from DownloadFileManager about a new download. |
| 250 // create a download item and store it in our download map, and inform the | |
| 251 // history system of a new download. Since this method can be called while the | |
| 252 // history service thread is still reading the persistent state, we do not | |
| 253 // insert the new DownloadItem into 'history_downloads_' or inform our | |
| 254 // observers at this point. OnCreateDownloadEntryComplete() handles that | |
| 255 // finalization of the the download creation as a callback from the | |
| 256 // history thread. | |
| 257 void DownloadManager::StartDownload(int32 download_id) { | 232 void DownloadManager::StartDownload(int32 download_id) { |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 259 | 234 |
| 260 DownloadItem* download = GetActiveDownloadItem(download_id); | 235 if (!delegate_->ShouldStartDownload(download_id)) |
| 261 if (!download) | 236 RestartDownload(download_id); |
| 262 return; | |
| 263 | |
| 264 #if defined(ENABLE_SAFE_BROWSING) | |
| 265 // Create a client to verify download URL with safebrowsing. | |
| 266 // It deletes itself after the callback. | |
| 267 scoped_refptr<DownloadSBClient> sb_client = new DownloadSBClient( | |
| 268 download_id, download->url_chain(), download->referrer_url(), | |
| 269 profile_->GetPrefs()->GetBoolean(prefs::kSafeBrowsingEnabled)); | |
| 270 sb_client->CheckDownloadUrl( | |
| 271 NewCallback(this, &DownloadManager::CheckDownloadUrlDone)); | |
| 272 #else | |
| 273 CheckDownloadUrlDone(download_id, false); | |
| 274 #endif | |
| 275 } | 237 } |
| 276 | 238 |
| 277 void DownloadManager::CheckForHistoryFilesRemoval() { | 239 void DownloadManager::CheckForHistoryFilesRemoval() { |
| 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 279 for (DownloadMap::iterator it = history_downloads_.begin(); | 241 for (DownloadMap::iterator it = history_downloads_.begin(); |
| 280 it != history_downloads_.end(); ++it) { | 242 it != history_downloads_.end(); ++it) { |
| 281 CheckForFileRemoval(it->second); | 243 CheckForFileRemoval(it->second); |
| 282 } | 244 } |
| 283 } | 245 } |
| 284 | 246 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 309 | 271 |
| 310 void DownloadManager::OnFileRemovalDetected(int64 db_handle) { | 272 void DownloadManager::OnFileRemovalDetected(int64 db_handle) { |
| 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 312 DownloadMap::iterator it = history_downloads_.find(db_handle); | 274 DownloadMap::iterator it = history_downloads_.find(db_handle); |
| 313 if (it != history_downloads_.end()) { | 275 if (it != history_downloads_.end()) { |
| 314 DownloadItem* download_item = it->second; | 276 DownloadItem* download_item = it->second; |
| 315 download_item->OnDownloadedFileRemoved(); | 277 download_item->OnDownloadedFileRemoved(); |
| 316 } | 278 } |
| 317 } | 279 } |
| 318 | 280 |
| 319 void DownloadManager::CheckDownloadUrlDone(int32 download_id, | 281 void DownloadManager::RestartDownload( |
| 320 bool is_dangerous_url) { | 282 int32 download_id) { |
| 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 322 | |
| 323 DownloadItem* download = GetActiveDownloadItem(download_id); | |
| 324 if (!download) | |
| 325 return; | |
| 326 | |
| 327 if (is_dangerous_url) | |
| 328 download->MarkUrlDangerous(); | |
| 329 | |
| 330 download_history_->CheckVisitedReferrerBefore(download_id, | |
| 331 download->referrer_url(), | |
| 332 NewCallback(this, &DownloadManager::CheckVisitedReferrerBeforeDone)); | |
| 333 } | |
| 334 | |
| 335 void DownloadManager::CheckVisitedReferrerBeforeDone( | |
| 336 int32 download_id, | |
| 337 bool visited_referrer_before) { | |
| 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 339 | |
| 340 DownloadItem* download = GetActiveDownloadItem(download_id); | |
| 341 if (!download) | |
| 342 return; | |
| 343 | |
| 344 // Check whether this download is for an extension install or not. | |
| 345 // Allow extensions to be explicitly saved. | |
| 346 DownloadStateInfo state = download->state_info(); | |
| 347 if (!state.prompt_user_for_save_location) { | |
| 348 if (UserScript::IsURLUserScript(download->GetURL(), | |
| 349 download->mime_type()) || | |
| 350 (download->mime_type() == Extension::kMimeType)) { | |
| 351 state.is_extension_install = true; | |
| 352 } | |
| 353 } | |
| 354 | |
| 355 if (state.force_file_name.empty()) { | |
| 356 FilePath generated_name; | |
| 357 download_util::GenerateFileNameFromRequest(*download, | |
| 358 &generated_name); | |
| 359 | |
| 360 // Freeze the user's preference for showing a Save As dialog. We're going | |
| 361 // to bounce around a bunch of threads and we don't want to worry about race | |
| 362 // conditions where the user changes this pref out from under us. | |
| 363 if (download_prefs_->PromptForDownload()) { | |
| 364 // But ignore the user's preference for the following scenarios: | |
| 365 // 1) Extension installation. Note that we only care here about the case | |
| 366 // where an extension is installed, not when one is downloaded with | |
| 367 // "save as...". | |
| 368 // 2) Filetypes marked "always open." If the user just wants this file | |
| 369 // opened, don't bother asking where to keep it. | |
| 370 if (!state.is_extension_install && | |
| 371 !ShouldOpenFileBasedOnExtension(generated_name)) | |
| 372 state.prompt_user_for_save_location = true; | |
| 373 } | |
| 374 if (download_prefs_->IsDownloadPathManaged()) { | |
| 375 state.prompt_user_for_save_location = false; | |
| 376 } | |
| 377 | |
| 378 // Determine the proper path for a download, by either one of the following: | |
| 379 // 1) using the default download directory. | |
| 380 // 2) prompting the user. | |
| 381 if (state.prompt_user_for_save_location && !last_download_path_.empty()) { | |
| 382 state.suggested_path = last_download_path_; | |
| 383 } else { | |
| 384 state.suggested_path = download_prefs_->download_path(); | |
| 385 } | |
| 386 state.suggested_path = state.suggested_path.Append(generated_name); | |
| 387 } else { | |
| 388 state.suggested_path = state.force_file_name; | |
| 389 } | |
| 390 | |
| 391 if (!state.prompt_user_for_save_location && state.force_file_name.empty()) { | |
| 392 state.is_dangerous_file = | |
| 393 IsDangerousFile(*download, state, visited_referrer_before); | |
| 394 } | |
| 395 | |
| 396 // We need to move over to the download thread because we don't want to stat | |
| 397 // the suggested path on the UI thread. | |
| 398 // We can only access preferences on the UI thread, so check the download path | |
| 399 // now and pass the value to the FILE thread. | |
| 400 BrowserThread::PostTask( | |
| 401 BrowserThread::FILE, FROM_HERE, | |
| 402 NewRunnableMethod( | |
| 403 this, | |
| 404 &DownloadManager::CheckIfSuggestedPathExists, | |
| 405 download->id(), | |
| 406 state, | |
| 407 download_prefs()->download_path())); | |
| 408 } | |
| 409 | |
| 410 void DownloadManager::CheckIfSuggestedPathExists(int32 download_id, | |
| 411 DownloadStateInfo state, | |
| 412 const FilePath& default_path) { | |
| 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 414 | |
| 415 // Make sure the default download directory exists. | |
| 416 // TODO(phajdan.jr): only create the directory when we're sure the user | |
| 417 // is going to save there and not to another directory of his choice. | |
| 418 file_util::CreateDirectory(default_path); | |
| 419 | |
| 420 // Check writability of the suggested path. If we can't write to it, default | |
| 421 // to the user's "My Documents" directory. We'll prompt them in this case. | |
| 422 FilePath dir = state.suggested_path.DirName(); | |
| 423 FilePath filename = state.suggested_path.BaseName(); | |
| 424 if (!file_util::PathIsWritable(dir)) { | |
| 425 VLOG(1) << "Unable to write to directory \"" << dir.value() << "\""; | |
| 426 state.prompt_user_for_save_location = true; | |
| 427 PathService::Get(chrome::DIR_USER_DOCUMENTS, &state.suggested_path); | |
| 428 state.suggested_path = state.suggested_path.Append(filename); | |
| 429 } | |
| 430 | |
| 431 // If the download is deemed dangerous, we'll use a temporary name for it. | |
| 432 if (state.IsDangerous()) { | |
| 433 state.target_name = FilePath(state.suggested_path).BaseName(); | |
| 434 // Create a temporary file to hold the file until the user approves its | |
| 435 // download. | |
| 436 FilePath::StringType file_name; | |
| 437 FilePath path; | |
| 438 #if defined(OS_WIN) | |
| 439 string16 unconfirmed_prefix = | |
| 440 l10n_util::GetStringUTF16(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | |
| 441 #else | |
| 442 std::string unconfirmed_prefix = | |
| 443 l10n_util::GetStringUTF8(IDS_DOWNLOAD_UNCONFIRMED_PREFIX); | |
| 444 #endif | |
| 445 | |
| 446 while (path.empty()) { | |
| 447 base::SStringPrintf( | |
| 448 &file_name, | |
| 449 unconfirmed_prefix.append( | |
| 450 FILE_PATH_LITERAL(" %d.crdownload")).c_str(), | |
| 451 base::RandInt(0, 100000)); | |
| 452 path = dir.Append(file_name); | |
| 453 if (file_util::PathExists(path)) | |
| 454 path = FilePath(); | |
| 455 } | |
| 456 state.suggested_path = path; | |
| 457 } else { | |
| 458 // Do not add the path uniquifier if we are saving to a specific path as in | |
| 459 // the drag-out case. | |
| 460 if (state.force_file_name.empty()) { | |
| 461 state.path_uniquifier = download_util::GetUniquePathNumberWithCrDownload( | |
| 462 state.suggested_path); | |
| 463 } | |
| 464 // We know the final path, build it if necessary. | |
| 465 if (state.path_uniquifier > 0) { | |
| 466 download_util::AppendNumberToPath(&(state.suggested_path), | |
| 467 state.path_uniquifier); | |
| 468 // Setting path_uniquifier to 0 to make sure we don't try to unique it | |
| 469 // later on. | |
| 470 state.path_uniquifier = 0; | |
| 471 } else if (state.path_uniquifier == -1) { | |
| 472 // We failed to find a unique path. We have to prompt the user. | |
| 473 VLOG(1) << "Unable to find a unique path for suggested path \"" | |
| 474 << state.suggested_path.value() << "\""; | |
| 475 state.prompt_user_for_save_location = true; | |
| 476 } | |
| 477 } | |
| 478 | |
| 479 // Create an empty file at the suggested path so that we don't allocate the | |
| 480 // same "non-existant" path to multiple downloads. | |
| 481 // See: http://code.google.com/p/chromium/issues/detail?id=3662 | |
| 482 if (!state.prompt_user_for_save_location && | |
| 483 state.force_file_name.empty()) { | |
| 484 if (state.IsDangerous()) | |
| 485 file_util::WriteFile(state.suggested_path, "", 0); | |
| 486 else | |
| 487 file_util::WriteFile(download_util::GetCrDownloadPath( | |
| 488 state.suggested_path), "", 0); | |
| 489 } | |
| 490 | |
| 491 BrowserThread::PostTask( | |
| 492 BrowserThread::UI, FROM_HERE, | |
| 493 NewRunnableMethod(this, | |
| 494 &DownloadManager::OnPathExistenceAvailable, | |
| 495 download_id, | |
| 496 state)); | |
| 497 } | |
| 498 | |
| 499 void DownloadManager::OnPathExistenceAvailable( | |
| 500 int32 download_id, const DownloadStateInfo& new_state) { | |
| 501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 502 | 284 |
| 503 DownloadItem* download = GetActiveDownloadItem(download_id); | 285 DownloadItem* download = GetActiveDownloadItem(download_id); |
| 504 if (!download) | 286 if (!download) |
| 505 return; | 287 return; |
| 506 | 288 |
| 507 VLOG(20) << __FUNCTION__ << "()" | 289 VLOG(20) << __FUNCTION__ << "()" |
| 508 << " download = " << download->DebugString(true); | 290 << " download = " << download->DebugString(true); |
| 509 | 291 |
| 510 download->SetFileCheckResults(new_state); | |
| 511 | |
| 512 FilePath suggested_path = download->suggested_path(); | 292 FilePath suggested_path = download->suggested_path(); |
| 513 | 293 |
| 514 if (download->prompt_user_for_save_location()) { | 294 if (download->prompt_user_for_save_location()) { |
| 515 // We must ask the user for the place to put the download. | 295 // We must ask the user for the place to put the download. |
| 516 DownloadRequestHandle request_handle = download->request_handle(); | 296 DownloadRequestHandle request_handle = download->request_handle(); |
| 517 TabContents* contents = request_handle.GetTabContents(); | 297 TabContents* contents = request_handle.GetTabContents(); |
| 518 | 298 |
| 519 // |id_ptr| will be deleted in either FileSelected() or | 299 // |id_ptr| will be deleted in either FileSelected() or |
| 520 // FileSelectionCancelled(). | 300 // FileSelectionCancelled(). |
| 521 int32* id_ptr = new int32; | 301 int32* id_ptr = new int32; |
| 522 *id_ptr = download_id; | 302 *id_ptr = download_id; |
| 523 | 303 |
| 524 delegate_->ChooseDownloadPath( | 304 delegate_->ChooseDownloadPath( |
| 525 this, contents, suggested_path, reinterpret_cast<void*>(id_ptr)); | 305 contents, suggested_path, reinterpret_cast<void*>(id_ptr)); |
| 526 | 306 |
| 527 FOR_EACH_OBSERVER(Observer, observers_, | 307 FOR_EACH_OBSERVER(Observer, observers_, |
| 528 SelectFileDialogDisplayed(download_id)); | 308 SelectFileDialogDisplayed(download_id)); |
| 529 } else { | 309 } else { |
| 530 // No prompting for download, just continue with the suggested name. | 310 // No prompting for download, just continue with the suggested name. |
| 531 ContinueDownloadWithPath(download, suggested_path); | 311 ContinueDownloadWithPath(download, suggested_path); |
| 532 } | 312 } |
| 533 } | 313 } |
| 534 | 314 |
| 535 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { | 315 void DownloadManager::CreateDownloadItem(DownloadCreateInfo* info) { |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 635 | 415 |
| 636 // If it's not in active_downloads_, that means it was cancelled; just | 416 // If it's not in active_downloads_, that means it was cancelled; just |
| 637 // ignore the notification. | 417 // ignore the notification. |
| 638 if (active_downloads_.count(download_id) == 0) | 418 if (active_downloads_.count(download_id) == 0) |
| 639 return; | 419 return; |
| 640 | 420 |
| 641 DownloadItem* download = active_downloads_[download_id]; | 421 DownloadItem* download = active_downloads_[download_id]; |
| 642 download->OnAllDataSaved(size); | 422 download->OnAllDataSaved(size); |
| 643 | 423 |
| 644 // When hash is not available, it means either it is not calculated | |
| 645 // or there is error while it is calculated. We will skip the download hash | |
| 646 // check in that case. | |
| 647 if (!hash.empty()) { | |
| 648 #if defined(ENABLE_SAFE_BROWSING) | |
| 649 scoped_refptr<DownloadSBClient> sb_client = | |
| 650 new DownloadSBClient(download_id, | |
| 651 download->url_chain(), | |
| 652 download->referrer_url(), | |
| 653 profile_->GetPrefs()->GetBoolean( | |
| 654 prefs::kSafeBrowsingEnabled)); | |
| 655 sb_client->CheckDownloadHash( | |
| 656 hash, NewCallback(this, &DownloadManager::CheckDownloadHashDone)); | |
| 657 #else | |
| 658 CheckDownloadHashDone(download_id, false); | |
| 659 #endif | |
| 660 } | |
| 661 MaybeCompleteDownload(download); | 424 MaybeCompleteDownload(download); |
| 662 } | 425 } |
| 663 | 426 |
| 664 // TODO(lzheng): This function currently works as a callback place holder. | |
| 665 // Once we decide the hash check is reliable, we could move the | |
| 666 // MaybeCompleteDownload in OnAllDataSaved to this function. | |
| 667 void DownloadManager::CheckDownloadHashDone(int32 download_id, | |
| 668 bool is_dangerous_hash) { | |
| 669 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 670 DVLOG(1) << "CheckDownloadHashDone, download_id: " << download_id | |
| 671 << " is dangerous_hash: " << is_dangerous_hash; | |
| 672 | |
| 673 // If it's not in active_downloads_, that means it was cancelled or | |
| 674 // the download already finished. | |
| 675 if (active_downloads_.count(download_id) == 0) | |
| 676 return; | |
| 677 | |
| 678 DVLOG(1) << "CheckDownloadHashDone, url: " | |
| 679 << active_downloads_[download_id]->GetURL().spec(); | |
| 680 } | |
| 681 | |
| 682 void DownloadManager::AssertQueueStateConsistent(DownloadItem* download) { | 427 void DownloadManager::AssertQueueStateConsistent(DownloadItem* download) { |
| 683 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 428 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
| 684 if (download->state() == DownloadItem::REMOVING) { | 429 if (download->state() == DownloadItem::REMOVING) { |
| 685 CHECK(!ContainsKey(downloads_, download)); | 430 CHECK(!ContainsKey(downloads_, download)); |
| 686 CHECK(!ContainsKey(active_downloads_, download->id())); | 431 CHECK(!ContainsKey(active_downloads_, download->id())); |
| 687 CHECK(!ContainsKey(in_progress_, download->id())); | 432 CHECK(!ContainsKey(in_progress_, download->id())); |
| 688 CHECK(!ContainsKey(history_downloads_, download->db_handle())); | 433 CHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 689 return; | 434 return; |
| 690 } | 435 } |
| 691 | 436 |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 | 731 |
| 987 void DownloadManager::AddObserver(Observer* observer) { | 732 void DownloadManager::AddObserver(Observer* observer) { |
| 988 observers_.AddObserver(observer); | 733 observers_.AddObserver(observer); |
| 989 observer->ModelChanged(); | 734 observer->ModelChanged(); |
| 990 } | 735 } |
| 991 | 736 |
| 992 void DownloadManager::RemoveObserver(Observer* observer) { | 737 void DownloadManager::RemoveObserver(Observer* observer) { |
| 993 observers_.RemoveObserver(observer); | 738 observers_.RemoveObserver(observer); |
| 994 } | 739 } |
| 995 | 740 |
| 996 bool DownloadManager::ShouldOpenFileBasedOnExtension( | |
| 997 const FilePath& path) const { | |
| 998 FilePath::StringType extension = path.Extension(); | |
| 999 if (extension.empty()) | |
| 1000 return false; | |
| 1001 if (Extension::IsExtension(path)) | |
| 1002 return false; | |
| 1003 DCHECK(extension[0] == FilePath::kExtensionSeparator); | |
| 1004 extension.erase(0, 1); | |
| 1005 return download_prefs_->IsAutoOpenEnabledForExtension(extension); | |
| 1006 } | |
| 1007 | |
| 1008 bool DownloadManager::IsDownloadProgressKnown() { | 741 bool DownloadManager::IsDownloadProgressKnown() { |
| 1009 for (DownloadMap::iterator i = in_progress_.begin(); | 742 for (DownloadMap::iterator i = in_progress_.begin(); |
| 1010 i != in_progress_.end(); ++i) { | 743 i != in_progress_.end(); ++i) { |
| 1011 if (i->second->total_bytes() <= 0) | 744 if (i->second->total_bytes() <= 0) |
| 1012 return false; | 745 return false; |
| 1013 } | 746 } |
| 1014 | 747 |
| 1015 return true; | 748 return true; |
| 1016 } | 749 } |
| 1017 | 750 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1072 DownloadItem* download = GetActiveDownloadItem(download_id); | 805 DownloadItem* download = GetActiveDownloadItem(download_id); |
| 1073 if (!download) | 806 if (!download) |
| 1074 return; | 807 return; |
| 1075 | 808 |
| 1076 VLOG(20) << __FUNCTION__ << "()" | 809 VLOG(20) << __FUNCTION__ << "()" |
| 1077 << " download = " << download->DebugString(true); | 810 << " download = " << download->DebugString(true); |
| 1078 | 811 |
| 1079 DownloadCancelledInternal(download_id, download->request_handle()); | 812 DownloadCancelledInternal(download_id, download->request_handle()); |
| 1080 } | 813 } |
| 1081 | 814 |
| 1082 // TODO(phajdan.jr): This is apparently not being exercised in tests. | |
| 1083 bool DownloadManager::IsDangerousFile(const DownloadItem& download, | |
| 1084 const DownloadStateInfo& state, | |
| 1085 bool visited_referrer_before) { | |
| 1086 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 1087 | |
| 1088 bool auto_open = ShouldOpenFileBasedOnExtension(state.suggested_path); | |
| 1089 download_util::DownloadDangerLevel danger_level = | |
| 1090 download_util::GetFileDangerLevel(state.suggested_path.BaseName()); | |
| 1091 | |
| 1092 if (danger_level == download_util::Dangerous) | |
| 1093 return !(auto_open && state.has_user_gesture); | |
| 1094 | |
| 1095 if (danger_level == download_util::AllowOnUserGesture && | |
| 1096 (!state.has_user_gesture || !visited_referrer_before)) | |
| 1097 return true; | |
| 1098 | |
| 1099 if (state.is_extension_install) { | |
| 1100 // Extensions that are not from the gallery are considered dangerous. | |
| 1101 ExtensionService* service = profile()->GetExtensionService(); | |
| 1102 if (!service || !service->IsDownloadFromGallery(download.GetURL(), | |
| 1103 download.referrer_url())) | |
| 1104 return true; | |
| 1105 } | |
| 1106 return false; | |
| 1107 } | |
| 1108 | |
| 1109 // Operations posted to us from the history service ---------------------------- | 815 // Operations posted to us from the history service ---------------------------- |
| 1110 | 816 |
| 1111 // The history service has retrieved all download entries. 'entries' contains | 817 // The history service has retrieved all download entries. 'entries' contains |
| 1112 // 'DownloadHistoryInfo's in sorted order (by ascending start_time). | 818 // 'DownloadHistoryInfo's in sorted order (by ascending start_time). |
| 1113 void DownloadManager::OnQueryDownloadEntriesComplete( | 819 void DownloadManager::OnQueryDownloadEntriesComplete( |
| 1114 std::vector<DownloadHistoryInfo>* entries) { | 820 std::vector<DownloadHistoryInfo>* entries) { |
| 1115 for (size_t i = 0; i < entries->size(); ++i) { | 821 for (size_t i = 0; i < entries->size(); ++i) { |
| 1116 DownloadItem* download = new DownloadItem(this, entries->at(i)); | 822 DownloadItem* download = new DownloadItem(this, entries->at(i)); |
| 1117 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); | 823 DCHECK(!ContainsKey(history_downloads_, download->db_handle())); |
| 1118 downloads_.insert(download); | 824 downloads_.insert(download); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1191 | 897 |
| 1192 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { | 898 void DownloadManager::ShowDownloadInBrowser(DownloadItem* download) { |
| 1193 // The 'contents' may no longer exist if the user closed the tab before we | 899 // The 'contents' may no longer exist if the user closed the tab before we |
| 1194 // get this start completion event. | 900 // get this start completion event. |
| 1195 DownloadRequestHandle request_handle = download->request_handle(); | 901 DownloadRequestHandle request_handle = download->request_handle(); |
| 1196 TabContents* content = request_handle.GetTabContents(); | 902 TabContents* content = request_handle.GetTabContents(); |
| 1197 | 903 |
| 1198 // If the contents no longer exists, we ask the embedder to suggest another | 904 // If the contents no longer exists, we ask the embedder to suggest another |
| 1199 // tab. | 905 // tab. |
| 1200 if (!content) | 906 if (!content) |
| 1201 content = delegate_->GetAlternativeTabContentsToNotifyForDownload(this); | 907 content = delegate_->GetAlternativeTabContentsToNotifyForDownload(); |
| 1202 | 908 |
| 1203 if (content) | 909 if (content) |
| 1204 content->OnStartDownload(download); | 910 content->OnStartDownload(download); |
| 1205 } | 911 } |
| 1206 | 912 |
| 1207 // Clears the last download path, used to initialize "save as" dialogs. | 913 // Clears the last download path, used to initialize "save as" dialogs. |
| 1208 void DownloadManager::ClearLastDownloadPath() { | 914 void DownloadManager::ClearLastDownloadPath() { |
| 1209 last_download_path_ = FilePath(); | 915 last_download_path_ = FilePath(); |
| 1210 } | 916 } |
| 1211 | 917 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1366 Source<DownloadManager>(this), | 1072 Source<DownloadManager>(this), |
| 1367 Details<DownloadItem>(download)); | 1073 Details<DownloadItem>(download)); |
| 1368 } | 1074 } |
| 1369 } | 1075 } |
| 1370 | 1076 |
| 1371 int32 DownloadManager::GetNextSavePageId() { | 1077 int32 DownloadManager::GetNextSavePageId() { |
| 1372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1078 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1373 return next_save_page_id_++; | 1079 return next_save_page_id_++; |
| 1374 } | 1080 } |
| 1375 | 1081 |
| OLD | NEW |