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" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "chrome/browser/extensions/webstore_installer.h" | 29 #include "chrome/browser/extensions/webstore_installer.h" |
30 #include "extensions/common/feature_switch.h" | 30 #include "extensions/common/feature_switch.h" |
31 #endif | 31 #endif |
32 | 32 |
33 #if defined(ENABLE_PLUGINS) | 33 #if defined(ENABLE_PLUGINS) |
34 #include "chrome/browser/plugins/plugin_prefs.h" | 34 #include "chrome/browser/plugins/plugin_prefs.h" |
35 #include "content/public/browser/plugin_service.h" | 35 #include "content/public/browser/plugin_service.h" |
36 #include "content/public/common/webplugininfo.h" | 36 #include "content/public/common/webplugininfo.h" |
37 #endif | 37 #endif |
38 | 38 |
| 39 #if defined(OS_WIN) |
| 40 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| 41 #endif |
| 42 |
39 using content::BrowserThread; | 43 using content::BrowserThread; |
40 using content::DownloadItem; | 44 using content::DownloadItem; |
41 | 45 |
42 namespace { | 46 namespace { |
43 | 47 |
44 const base::FilePath::CharType kCrdownloadSuffix[] = | 48 const base::FilePath::CharType kCrdownloadSuffix[] = |
45 FILE_PATH_LITERAL(".crdownload"); | 49 FILE_PATH_LITERAL(".crdownload"); |
46 | 50 |
47 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a | 51 // Condenses the results from HistoryService::GetVisibleVisitCountToHost() to a |
48 // single bool. A host is considered visited before if prior visible visits were | 52 // single bool. A host is considered visited before if prior visible visits were |
49 // found in history and the first such visit was earlier than the most recent | 53 // found in history and the first such visit was earlier than the most recent |
50 // midnight. | 54 // midnight. |
51 void VisitCountsToVisitedBefore( | 55 void VisitCountsToVisitedBefore( |
52 const base::Callback<void(bool)>& callback, | 56 const base::Callback<void(bool)>& callback, |
53 HistoryService::Handle unused_handle, | 57 HistoryService::Handle unused_handle, |
54 bool found_visits, | 58 bool found_visits, |
55 int count, | 59 int count, |
56 base::Time first_visit) { | 60 base::Time first_visit) { |
57 callback.Run( | 61 callback.Run( |
58 found_visits && count > 0 && | 62 found_visits && count > 0 && |
59 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | 63 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
60 } | 64 } |
61 | 65 |
62 } // namespace | 66 #if defined(OS_WIN) |
| 67 // Keeps track of whether Adobe Reader is up to date. |
| 68 bool g_is_adobe_reader_up_to_date_ = false; |
| 69 #endif |
| 70 |
| 71 } // namespace |
63 | 72 |
64 DownloadTargetInfo::DownloadTargetInfo() | 73 DownloadTargetInfo::DownloadTargetInfo() |
65 : is_filetype_handled_safely(false) {} | 74 : is_filetype_handled_safely(false) {} |
66 | 75 |
67 DownloadTargetInfo::~DownloadTargetInfo() {} | 76 DownloadTargetInfo::~DownloadTargetInfo() {} |
68 | 77 |
69 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { | 78 DownloadTargetDeterminerDelegate::~DownloadTargetDeterminerDelegate() { |
70 } | 79 } |
71 | 80 |
72 DownloadTargetDeterminer::DownloadTargetDeterminer( | 81 DownloadTargetDeterminer::DownloadTargetDeterminer( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 break; | 136 break; |
128 case STATE_DETERMINE_LOCAL_PATH: | 137 case STATE_DETERMINE_LOCAL_PATH: |
129 result = DoDetermineLocalPath(); | 138 result = DoDetermineLocalPath(); |
130 break; | 139 break; |
131 case STATE_DETERMINE_MIME_TYPE: | 140 case STATE_DETERMINE_MIME_TYPE: |
132 result = DoDetermineMimeType(); | 141 result = DoDetermineMimeType(); |
133 break; | 142 break; |
134 case STATE_DETERMINE_IF_HANDLED_SAFELY_BY_BROWSER: | 143 case STATE_DETERMINE_IF_HANDLED_SAFELY_BY_BROWSER: |
135 result = DoDetermineIfHandledSafely(); | 144 result = DoDetermineIfHandledSafely(); |
136 break; | 145 break; |
| 146 case STATE_DETERMINE_IF_ADOBE_READER_UP_TO_DATE: |
| 147 result = DoDetermineIfAdobeReaderUpToDate(); |
| 148 break; |
137 case STATE_CHECK_DOWNLOAD_URL: | 149 case STATE_CHECK_DOWNLOAD_URL: |
138 result = DoCheckDownloadUrl(); | 150 result = DoCheckDownloadUrl(); |
139 break; | 151 break; |
140 case STATE_DETERMINE_INTERMEDIATE_PATH: | 152 case STATE_DETERMINE_INTERMEDIATE_PATH: |
141 result = DoDetermineIntermediatePath(); | 153 result = DoDetermineIntermediatePath(); |
142 break; | 154 break; |
143 case STATE_CHECK_VISITED_REFERRER_BEFORE: | 155 case STATE_CHECK_VISITED_REFERRER_BEFORE: |
144 result = DoCheckVisitedReferrerBefore(); | 156 result = DoCheckVisitedReferrerBefore(); |
145 break; | 157 break; |
146 case STATE_NONE: | 158 case STATE_NONE: |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
443 // In practice, we assume that retrying once is enough. | 455 // In practice, we assume that retrying once is enough. |
444 DCHECK(!is_stale); | 456 DCHECK(!is_stale); |
445 bool is_handled_safely = | 457 bool is_handled_safely = |
446 plugin_found && | 458 plugin_found && |
447 (plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS || | 459 (plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS || |
448 plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS); | 460 plugin_info.type == WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS); |
449 BrowserThread::PostTask( | 461 BrowserThread::PostTask( |
450 BrowserThread::UI, FROM_HERE, base::Bind(callback, is_handled_safely)); | 462 BrowserThread::UI, FROM_HERE, base::Bind(callback, is_handled_safely)); |
451 } | 463 } |
452 | 464 |
453 } // namespace | 465 } // namespace |
454 #endif // ENABLE_PLUGINS | 466 #endif // defined(ENABLE_PLUGINS) |
455 | 467 |
456 DownloadTargetDeterminer::Result | 468 DownloadTargetDeterminer::Result |
457 DownloadTargetDeterminer::DoDetermineIfHandledSafely() { | 469 DownloadTargetDeterminer::DoDetermineIfHandledSafely() { |
458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 470 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
459 DCHECK(!virtual_path_.empty()); | 471 DCHECK(!virtual_path_.empty()); |
460 DCHECK(!local_path_.empty()); | 472 DCHECK(!local_path_.empty()); |
461 DCHECK(!is_filetype_handled_safely_); | 473 DCHECK(!is_filetype_handled_safely_); |
462 | 474 |
463 next_state_ = STATE_CHECK_DOWNLOAD_URL; | 475 next_state_ = STATE_DETERMINE_IF_ADOBE_READER_UP_TO_DATE; |
464 | 476 |
465 if (mime_type_.empty()) | 477 if (mime_type_.empty()) |
466 return CONTINUE; | 478 return CONTINUE; |
467 | 479 |
468 if (net::IsSupportedMimeType(mime_type_)) { | 480 if (net::IsSupportedMimeType(mime_type_)) { |
469 is_filetype_handled_safely_ = true; | 481 is_filetype_handled_safely_ = true; |
470 return CONTINUE; | 482 return CONTINUE; |
471 } | 483 } |
472 | 484 |
473 #if defined(ENABLE_PLUGINS) | 485 #if defined(ENABLE_PLUGINS) |
474 BrowserThread::PostTask( | 486 BrowserThread::PostTask( |
475 BrowserThread::IO, | 487 BrowserThread::IO, |
476 FROM_HERE, | 488 FROM_HERE, |
477 base::Bind( | 489 base::Bind( |
478 &IsHandledBySafePlugin, | 490 &IsHandledBySafePlugin, |
479 GetProfile()->GetResourceContext(), | 491 GetProfile()->GetResourceContext(), |
480 net::FilePathToFileURL(local_path_), | 492 net::FilePathToFileURL(local_path_), |
481 mime_type_, | 493 mime_type_, |
482 RETRY_IF_STALE_PLUGIN_LIST, | 494 RETRY_IF_STALE_PLUGIN_LIST, |
483 base::Bind(&DownloadTargetDeterminer::DetermineIfHandledSafelyDone, | 495 base::Bind(&DownloadTargetDeterminer::DetermineIfHandledSafelyDone, |
484 weak_ptr_factory_.GetWeakPtr()))); | 496 weak_ptr_factory_.GetWeakPtr()))); |
485 return QUIT_DOLOOP; | 497 return QUIT_DOLOOP; |
486 #else | 498 #else |
487 return CONTINUE; | 499 return CONTINUE; |
488 #endif | 500 #endif |
489 } | 501 } |
490 | 502 |
| 503 #if defined(ENABLE_PLUGINS) |
491 void DownloadTargetDeterminer::DetermineIfHandledSafelyDone( | 504 void DownloadTargetDeterminer::DetermineIfHandledSafelyDone( |
492 bool is_handled_safely) { | 505 bool is_handled_safely) { |
493 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 506 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
494 DVLOG(20) << "Is file type handled safely: " << is_filetype_handled_safely_; | 507 DVLOG(20) << "Is file type handled safely: " << is_filetype_handled_safely_; |
495 DCHECK_EQ(STATE_CHECK_DOWNLOAD_URL, next_state_); | 508 DCHECK_EQ(STATE_DETERMINE_IF_ADOBE_READER_UP_TO_DATE, next_state_); |
496 is_filetype_handled_safely_ = is_handled_safely; | 509 is_filetype_handled_safely_ = is_handled_safely; |
497 DoLoop(); | 510 DoLoop(); |
498 } | 511 } |
| 512 #endif |
| 513 |
| 514 DownloadTargetDeterminer::Result |
| 515 DownloadTargetDeterminer::DoDetermineIfAdobeReaderUpToDate() { |
| 516 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 517 |
| 518 next_state_ = STATE_CHECK_DOWNLOAD_URL; |
| 519 |
| 520 #if defined(OS_WIN) |
| 521 if (!local_path_.MatchesExtension(FILE_PATH_LITERAL(".pdf"))) |
| 522 return CONTINUE; |
| 523 if (!IsAdobeReaderDefaultPDFViewer()) |
| 524 return CONTINUE; |
| 525 |
| 526 base::PostTaskAndReplyWithResult( |
| 527 BrowserThread::GetBlockingPool(), |
| 528 FROM_HERE, |
| 529 base::Bind(&::IsAdobeReaderUpToDate), |
| 530 base::Bind(&DownloadTargetDeterminer::DetermineIfAdobeReaderUpToDateDone, |
| 531 weak_ptr_factory_.GetWeakPtr())); |
| 532 return QUIT_DOLOOP; |
| 533 #else |
| 534 return CONTINUE; |
| 535 #endif |
| 536 } |
| 537 |
| 538 #if defined(OS_WIN) |
| 539 void DownloadTargetDeterminer::DetermineIfAdobeReaderUpToDateDone( |
| 540 bool adobe_reader_up_to_date) { |
| 541 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 542 DVLOG(20) << "Is Adobe Reader Up To Date: " << adobe_reader_up_to_date; |
| 543 DCHECK_EQ(STATE_CHECK_DOWNLOAD_URL, next_state_); |
| 544 g_is_adobe_reader_up_to_date_ = adobe_reader_up_to_date; |
| 545 DoLoop(); |
| 546 } |
| 547 #endif |
499 | 548 |
500 DownloadTargetDeterminer::Result | 549 DownloadTargetDeterminer::Result |
501 DownloadTargetDeterminer::DoCheckDownloadUrl() { | 550 DownloadTargetDeterminer::DoCheckDownloadUrl() { |
502 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
503 DCHECK(!virtual_path_.empty()); | 552 DCHECK(!virtual_path_.empty()); |
504 next_state_ = STATE_CHECK_VISITED_REFERRER_BEFORE; | 553 next_state_ = STATE_CHECK_VISITED_REFERRER_BEFORE; |
505 delegate_->CheckDownloadUrl( | 554 delegate_->CheckDownloadUrl( |
506 download_, | 555 download_, |
507 virtual_path_, | 556 virtual_path_, |
508 base::Bind(&DownloadTargetDeterminer::CheckDownloadUrlDone, | 557 base::Bind(&DownloadTargetDeterminer::CheckDownloadUrlDone, |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 // asynchronously. | 868 // asynchronously. |
820 new DownloadTargetDeterminer(download, initial_virtual_path, download_prefs, | 869 new DownloadTargetDeterminer(download, initial_virtual_path, download_prefs, |
821 delegate, callback); | 870 delegate, callback); |
822 } | 871 } |
823 | 872 |
824 // static | 873 // static |
825 base::FilePath DownloadTargetDeterminer::GetCrDownloadPath( | 874 base::FilePath DownloadTargetDeterminer::GetCrDownloadPath( |
826 const base::FilePath& suggested_path) { | 875 const base::FilePath& suggested_path) { |
827 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); | 876 return base::FilePath(suggested_path.value() + kCrdownloadSuffix); |
828 } | 877 } |
| 878 |
| 879 #if defined(OS_WIN) |
| 880 // static |
| 881 bool DownloadTargetDeterminer::IsAdobeReaderUpToDate() { |
| 882 return g_is_adobe_reader_up_to_date_; |
| 883 } |
| 884 #endif |
OLD | NEW |