Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_target_determiner.h" | 5 #include "chrome/browser/download/download_target_determiner.h" |
| 6 | 6 |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 11 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 12 #include "chrome/browser/download/download_crx_util.h" | 12 #include "chrome/browser/download/download_crx_util.h" |
| 13 #include "chrome/browser/download/download_extensions.h" | 13 #include "chrome/browser/download/download_extensions.h" |
| 14 #include "chrome/browser/download/download_prefs.h" | 14 #include "chrome/browser/download/download_prefs.h" |
| 15 #include "chrome/browser/extensions/webstore_installer.h" | 15 #include "chrome/browser/extensions/webstore_installer.h" |
| 16 #include "chrome/browser/history/history_service.h" | 16 #include "chrome/browser/history/history_service.h" |
| 17 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/download_interrupt_reasons.h" | 22 #include "content/public/browser/download_interrupt_reasons.h" |
| 23 #include "extensions/common/constants.h" | 23 #include "extensions/common/constants.h" |
| 24 #include "extensions/common/feature_switch.h" | 24 #include "extensions/common/feature_switch.h" |
| 25 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
| 26 #include "net/base/filename_util.h" | 26 #include "net/base/filename_util.h" |
| 27 #include "net/base/mime_util.h" | 27 #include "net/base/mime_util.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
| 29 | 29 |
| 30 #if defined(OS_WIN) | |
| 31 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" | |
| 32 #endif | |
| 33 | |
| 30 #if defined(ENABLE_PLUGINS) | 34 #if defined(ENABLE_PLUGINS) |
| 31 #include "chrome/browser/plugins/plugin_prefs.h" | 35 #include "chrome/browser/plugins/plugin_prefs.h" |
| 32 #include "content/public/browser/plugin_service.h" | 36 #include "content/public/browser/plugin_service.h" |
| 33 #include "content/public/common/webplugininfo.h" | 37 #include "content/public/common/webplugininfo.h" |
| 34 #endif | 38 #endif |
| 35 | 39 |
| 36 using content::BrowserThread; | 40 using content::BrowserThread; |
| 37 using content::DownloadItem; | 41 using content::DownloadItem; |
| 38 | 42 |
| 39 namespace { | 43 namespace { |
| 40 | 44 |
| 41 const base::FilePath::CharType kCrdownloadSuffix[] = | 45 const base::FilePath::CharType kCrdownloadSuffix[] = |
| 42 FILE_PATH_LITERAL(".crdownload"); | 46 FILE_PATH_LITERAL(".crdownload"); |
| 43 | 47 |
| 44 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a | 48 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a |
| 45 // single bool. A host is considered visited before if prior visible visits were | 49 // single bool. A host is considered visited before if prior visible visits were |
| 46 // found in history and the first such visit was earlier than the most recent | 50 // found in history and the first such visit was earlier than the most recent |
| 47 // midnight. | 51 // midnight. |
| 48 void VisitCountsToVisitedBefore( | 52 void VisitCountsToVisitedBefore( |
| 49 const base::Callback<void(bool)>& callback, | 53 const base::Callback<void(bool)>& callback, |
| 50 HistoryService::Handle unused_handle, | 54 HistoryService::Handle unused_handle, |
| 51 bool found_visits, | 55 bool found_visits, |
| 52 int count, | 56 int count, |
| 53 base::Time first_visit) { | 57 base::Time first_visit) { |
| 54 callback.Run( | 58 callback.Run( |
| 55 found_visits && count > 0 && | 59 found_visits && count > 0 && |
| 56 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | 60 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
| 57 } | 61 } |
| 58 | 62 |
| 59 } // namespace | 63 #if defined(OS_WIN) |
| 64 // Keeps track of whether Adobe Reader is up to date. | |
| 65 bool g_is_adobe_reader_up_to_date_ = false; | |
| 66 #endif | |
| 67 | |
| 68 } // namespace | |
| 60 | 69 |
| 61 DownloadTargetInfo::DownloadTargetInfo() | 70 DownloadTargetInfo::DownloadTargetInfo() |
| 62 : is_filetype_handled_safely(false) {} | 71 : is_filetype_handled_safely(false) {} |
| 63 | 72 |
| 64 DownloadTargetInfo::~DownloadTargetInfo() {} | 73 DownloadTargetInfo::~DownloadTargetInfo() {} |
| 65 | 74 |
| 66 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { | 75 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { |
| 67 } | 76 } |
| 68 | 77 |
| 69 DownloadTargetDeterminer::DownloadTargetDeterminer( | 78 DownloadTargetDeterminer::DownloadTargetDeterminer( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 break; | 133 break; |
| 125 case STATE_DETERMINE_LOCAL_PATH: | 134 case STATE_DETERMINE_LOCAL_PATH: |
| 126 result = DoDetermineLocalPath(); | 135 result = DoDetermineLocalPath(); |
| 127 break; | 136 break; |
| 128 case STATE_DETERMINE_MIME_TYPE: | 137 case STATE_DETERMINE_MIME_TYPE: |
| 129 result = DoDetermineMimeType(); | 138 result = DoDetermineMimeType(); |
| 130 break; | 139 break; |
| 131 case STATE_DETERMINE_IF_HANDLED_SAFELY_BY_BROWSER: | 140 case STATE_DETERMINE_IF_HANDLED_SAFELY_BY_BROWSER: |
| 132 result = DoDetermineIfHandledSafely(); | 141 result = DoDetermineIfHandledSafely(); |
| 133 break; | 142 break; |
| 143 case STATE_DETERMINE_IF_ADOBE_READER_UP_TO_DATE: | |
| 144 result = DoDetermineIfAdobeReaderUpToDate(); | |
| 145 break; | |
| 134 case STATE_CHECK_DOWNLOAD_URL: | 146 case STATE_CHECK_DOWNLOAD_URL: |
| 135 result = DoCheckDownloadUrl(); | 147 result = DoCheckDownloadUrl(); |
| 136 break; | 148 break; |
| 137 case STATE_DETERMINE_INTERMEDIATE_PATH: | 149 case STATE_DETERMINE_INTERMEDIATE_PATH: |
| 138 result = DoDetermineIntermediatePath(); | 150 result = DoDetermineIntermediatePath(); |
| 139 break; | 151 break; |
| 140 case STATE_CHECK_VISITED_REFERRER_BEFORE: | 152 case STATE_CHECK_VISITED_REFERRER_BEFORE: |
| 141 result = DoCheckVisitedReferrerBefore(); | 153 result = DoCheckVisitedReferrerBefore(); |
| 142 break; | 154 break; |
| 143 case STATE_NONE: | 155 case STATE_NONE: |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 // In practice, we assume that retrying once is enough. | 452 // In practice, we assume that retrying once is enough. |
| 441 DCHECK(!is_stale); | 453 DCHECK(!is_stale); |
| 442 bool is_handled_safely = | 454 bool is_handled_safely = |
| 443 plugin_found && | 455 plugin_found && |
| 444 (plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS || | 456 (plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS || |
| 445 plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS); | 457 plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS); |
| 446 BrowserThread::PostTask( | 458 BrowserThread::PostTask( |
| 447 BrowserThread::UI, FROM_HERE, base::Bind(callback, is_handled_safely)); | 459 BrowserThread::UI, FROM_HERE, base::Bind(callback, is_handled_safely)); |
| 448 } | 460 } |
| 449 | 461 |
| 450 } // namespace | 462 } // namespace |
| 451 #endif // ENABLE_PLUGINS | 463 #endif // defined(ENABLE_PLUGINS) |
| 452 | 464 |
| 453 DownloadTargetDeterminer::Result | 465 DownloadTargetDeterminer::Result |
| 454 DownloadTargetDeterminer::DoDetermineIfHandledSafely() { | 466 DownloadTargetDeterminer::DoDetermineIfHandledSafely() { |
| 455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 467 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 456 DCHECK(!virtual_path_.empty()); | 468 DCHECK(!virtual_path_.empty()); |
| 457 DCHECK(!local_path_.empty()); | 469 DCHECK(!local_path_.empty()); |
| 458 DCHECK(!is_filetype_handled_safely_); | 470 DCHECK(!is_filetype_handled_safely_); |
| 459 | 471 |
| 460 next_state_ = STATE_CHECK_DOWNLOAD_URL; | 472 next_state_ = STATE_DETERMINE_IF_ADOBE_READER_UP_TO_DATE; |
| 461 | 473 |
| 462 if (mime_type_.empty()) | 474 if (mime_type_.empty()) |
| 463 return CONTINUE; | 475 return CONTINUE; |
| 464 | 476 |
| 465 if (net::IsSupportedMimeType(mime_type_)) { | 477 if (net::IsSupportedMimeType(mime_type_)) { |
| 466 is_filetype_handled_safely_ = true; | 478 is_filetype_handled_safely_ = true; |
| 467 return CONTINUE; | 479 return CONTINUE; |
| 468 } | 480 } |
| 469 | 481 |
| 470 #if defined(ENABLE_PLUGINS) | 482 #if defined(ENABLE_PLUGINS) |
| 471 BrowserThread::PostTask( | 483 BrowserThread::PostTask( |
| 472 BrowserThread::IO, | 484 BrowserThread::IO, |
| 473 FROM_HERE, | 485 FROM_HERE, |
| 474 base::Bind( | 486 base::Bind( |
| 475 &IsHandledBySafePlugin, | 487 &IsHandledBySafePlugin, |
| 476 GetProfile()->GetResourceContext(), | 488 GetProfile()->GetResourceContext(), |
| 477 net::FilePathToFileURL(local_path_), | 489 net::FilePathToFileURL(local_path_), |
| 478 mime_type_, | 490 mime_type_, |
| 479 RETRY_IF_STALE_PLUGIN_LIST, | 491 RETRY_IF_STALE_PLUGIN_LIST, |
| 480 base::Bind(&DownloadTargetDeterminer::DetermineIfHandledSafelyDone, | 492 base::Bind(&DownloadTargetDeterminer::DetermineIfHandledSafelyDone, |
| 481 weak_ptr_factory_.GetWeakPtr()))); | 493 weak_ptr_factory_.GetWeakPtr()))); |
| 482 return QUIT_DOLOOP; | 494 return QUIT_DOLOOP; |
| 483 #else | 495 #else |
| 484 return CONTINUE; | 496 return CONTINUE; |
| 485 #endif | 497 #endif |
| 486 } | 498 } |
| 487 | 499 |
| 500 #if defined(ENABLE_PLUGINS) | |
| 488 void DownloadTargetDeterminer::DetermineIfHandledSafelyDone( | 501 void DownloadTargetDeterminer::DetermineIfHandledSafelyDone( |
| 489 bool is_handled_safely) { | 502 bool is_handled_safely) { |
| 490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 503 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 491 DVLOG(20) << "Is file type handled safely: " << is_filetype_handled_safely_; | 504 DVLOG(20) << "Is file type handled safely: " << is_filetype_handled_safely_; |
| 492 DCHECK_EQ(STATE_CHECK_DOWNLOAD_URL, next_state_); | 505 DCHECK_EQ(STATE_DETERMINE_IF_ADOBE_READER_UP_TO_DATE, next_state_); |
| 493 is_filetype_handled_safely_ = is_handled_safely; | 506 is_filetype_handled_safely_ = is_handled_safely; |
| 494 DoLoop(); | 507 DoLoop(); |
| 495 } | 508 } |
| 509 #endif | |
| 510 | |
| 511 DownloadTargetDeterminer::Result | |
| 512 DownloadTargetDeterminer::DoDetermineIfAdobeReaderUpToDate() { | |
| 513 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 514 | |
| 515 next_state_ = STATE_CHECK_DOWNLOAD_URL; | |
| 516 | |
| 517 #if defined(OS_WIN) | |
| 518 if (local_path_.Extension() == FILE_PATH_LITERAL(".pdf")) | |
|
asanka
2014/07/01 19:55:14
Also, local_path_.MatchesExtension(...) rather tha
asanka
2014/07/01 19:55:14
Why skip the check if we are looking at a .pdf?
Lei Zhang
2014/07/01 20:47:56
Whoops, meant to only check for PDFs.
Lei Zhang
2014/07/01 20:47:57
Done.
| |
| 519 return CONTINUE; | |
| 520 if (!IsAdobeReaderDefaultPDFViewer()) | |
| 521 return CONTINUE; | |
| 522 | |
| 523 base::PostTaskAndReplyWithResult( | |
| 524 BrowserThread::GetBlockingPool(), | |
| 525 FROM_HERE, | |
| 526 base::Bind(&::IsAdobeReaderUpToDate), | |
| 527 base::Bind(&DownloadTargetDeterminer::DetermineIfAdobeReaderUpToDateDone, | |
| 528 weak_ptr_factory_.GetWeakPtr())); | |
| 529 return QUIT_DOLOOP; | |
| 530 #else | |
| 531 return CONTINUE; | |
| 532 #endif | |
| 533 } | |
| 534 | |
| 535 #if defined(OS_WIN) | |
| 536 void DownloadTargetDeterminer::DetermineIfAdobeReaderUpToDateDone( | |
| 537 bool adobe_reader_up_to_date) { | |
| 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 539 DVLOG(20) << "Is Adobe Reader Up To Date: " << adobe_reader_up_to_date; | |
| 540 DCHECK_EQ(STATE_CHECK_DOWNLOAD_URL, next_state_); | |
| 541 g_is_adobe_reader_up_to_date_ = adobe_reader_up_to_date; | |
| 542 DoLoop(); | |
| 543 } | |
| 544 #endif | |
| 496 | 545 |
| 497 DownloadTargetDeterminer::Result | 546 DownloadTargetDeterminer::Result |
| 498 DownloadTargetDeterminer::DoCheckDownloadUrl() { | 547 DownloadTargetDeterminer::DoCheckDownloadUrl() { |
| 499 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 500 DCHECK(!virtual_path_.empty()); | 549 DCHECK(!virtual_path_.empty()); |
| 501 next_state_ = STATE_CHECK_VISITED_REFERRER_BEFORE; | 550 next_state_ = STATE_CHECK_VISITED_REFERRER_BEFORE; |
| 502 delegate_->CheckDownloadUrl( | 551 delegate_->CheckDownloadUrl( |
| 503 download_, | 552 download_, |
| 504 virtual_path_, | 553 virtual_path_, |
| 505 base::Bind(&DownloadTargetDeterminer::CheckDownloadUrlDone, | 554 base::Bind(&DownloadTargetDeterminer::CheckDownloadUrlDone, |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 814 // asynchronously. | 863 // asynchronously. |
| 815 new DownloadTargetDeterminer(download, initial_virtual_path, download_prefs, | 864 new DownloadTargetDeterminer(download, initial_virtual_path, download_prefs, |
| 816 delegate, callback); | 865 delegate, callback); |
| 817 } | 866 } |
| 818 | 867 |
| 819 // static | 868 // static |
| 820 base::FilePath DownloadTargetDeterminer::GetCrDownloadPath( | 869 base::FilePath DownloadTargetDeterminer::GetCrDownloadPath( |
| 821 const base::FilePath& suggested_path) { | 870 const base::FilePath& suggested_path) { |
| 822 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); | 871 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); |
| 823 } | 872 } |
| 873 | |
| 874 #if defined(OS_WIN) | |
| 875 // static | |
| 876 bool DownloadTargetDeterminer::IsAdobeReaderUpToDate() { | |
| 877 return g_is_adobe_reader_up_to_date_; | |
| 878 } | |
| 879 #endif | |
| OLD | NEW |