OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/download/download_commands.h" |
| 6 |
| 7 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/download/download_crx_util.h" |
| 9 #include "chrome/browser/download/download_item_model.h" |
| 10 #include "chrome/browser/download/download_prefs.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 13 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 14 #include "chrome/browser/ui/browser_finder.h" |
| 15 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 16 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/grit/generated_resources.h" |
| 18 #include "grit/theme_resources.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 #include "ui/base/resource/resource_bundle.h" |
| 21 |
| 22 #if defined(OS_WIN) |
| 23 #include "chrome/browser/download/download_target_determiner.h" |
| 24 #include "chrome/browser/ui/pdf/adobe_reader_info_win.h" |
| 25 #endif |
| 26 |
| 27 DownloadCommands::DownloadCommands(content::DownloadItem* download_item) |
| 28 : download_item_(download_item) { |
| 29 DCHECK(download_item); |
| 30 } |
| 31 |
| 32 int DownloadCommands::GetCommandIconId(Command command) { |
| 33 switch (command) { |
| 34 case PAUSE: |
| 35 return IDR_DOWNLOAD_NOTIFICATION_MENU_PAUSE; |
| 36 case RESUME: |
| 37 return IDR_DOWNLOAD_NOTIFICATION_MENU_RESUME; |
| 38 case SHOW_IN_FOLDER: |
| 39 return IDR_DOWNLOAD_NOTIFICATION_MENU_FOLDER; |
| 40 case RETRY: |
| 41 case KEEP: |
| 42 return IDR_DOWNLOAD_NOTIFICATION_MENU_DOWNLOAD; |
| 43 case DISCARD: |
| 44 return IDR_DOWNLOAD_NOTIFICATION_MENU_DELETE; |
| 45 case OPEN_WHEN_COMPLETE: |
| 46 case ALWAYS_OPEN_TYPE: |
| 47 case PLATFORM_OPEN: |
| 48 case CANCEL: |
| 49 case LEARN_MORE_SCANNING: |
| 50 case LEARN_MORE_INTERRUPTED: |
| 51 return -1; |
| 52 } |
| 53 NOTREACHED(); |
| 54 return -1; |
| 55 } |
| 56 |
| 57 gfx::Image DownloadCommands::GetCommandIcon(Command command) { |
| 58 ResourceBundle& bundle = ResourceBundle::GetSharedInstance(); |
| 59 return bundle.GetImageNamed(GetCommandIconId(command)); |
| 60 } |
| 61 |
| 62 bool DownloadCommands::IsCommandEnabled(Command command) const { |
| 63 switch (command) { |
| 64 case SHOW_IN_FOLDER: |
| 65 return download_item_->CanShowInFolder(); |
| 66 case OPEN_WHEN_COMPLETE: |
| 67 case PLATFORM_OPEN: |
| 68 return download_item_->CanOpenDownload() && |
| 69 !download_crx_util::IsExtensionDownload(*download_item_); |
| 70 case ALWAYS_OPEN_TYPE: |
| 71 // For temporary downloads, the target filename might be a temporary |
| 72 // filename. Don't base an "Always open" decision based on it. Also |
| 73 // exclude extensions. |
| 74 return download_item_->CanOpenDownload() && |
| 75 !download_crx_util::IsExtensionDownload(*download_item_); |
| 76 case CANCEL: |
| 77 return !download_item_->IsDone(); |
| 78 case PAUSE: |
| 79 return !download_item_->IsDone() && !download_item_->IsPaused() && |
| 80 download_item_->GetState() == content::DownloadItem::IN_PROGRESS; |
| 81 case RESUME: |
| 82 return !download_item_->CanResume() && |
| 83 (download_item_->IsPaused() || |
| 84 download_item_->GetState() != content::DownloadItem::IN_PROGRESS); |
| 85 case DISCARD: |
| 86 case KEEP: |
| 87 case LEARN_MORE_SCANNING: |
| 88 case LEARN_MORE_INTERRUPTED: |
| 89 case RETRY: |
| 90 return true; |
| 91 } |
| 92 NOTREACHED(); |
| 93 return false; |
| 94 } |
| 95 |
| 96 bool DownloadCommands::IsCommandChecked(Command command) const { |
| 97 switch (command) { |
| 98 case OPEN_WHEN_COMPLETE: |
| 99 return download_item_->GetOpenWhenComplete() || |
| 100 download_crx_util::IsExtensionDownload(*download_item_); |
| 101 case ALWAYS_OPEN_TYPE: |
| 102 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 103 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 104 if (CanOpenPdfInSystemViewer()) { |
| 105 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( |
| 106 download_item_->GetBrowserContext()); |
| 107 return prefs->ShouldOpenPdfInSystemReader(); |
| 108 } |
| 109 #endif |
| 110 return download_item_->ShouldOpenFileBasedOnExtension(); |
| 111 case PAUSE: |
| 112 case RESUME: |
| 113 return download_item_->IsPaused(); |
| 114 case SHOW_IN_FOLDER: |
| 115 case PLATFORM_OPEN: |
| 116 case CANCEL: |
| 117 case DISCARD: |
| 118 case KEEP: |
| 119 case RETRY: |
| 120 case LEARN_MORE_SCANNING: |
| 121 case LEARN_MORE_INTERRUPTED: |
| 122 return false; |
| 123 } |
| 124 return false; |
| 125 } |
| 126 |
| 127 bool DownloadCommands::IsCommandVisible(Command command) const { |
| 128 if (command == PLATFORM_OPEN) |
| 129 return (DownloadItemModel(download_item_).ShouldPreferOpeningInBrowser()); |
| 130 |
| 131 return true; |
| 132 } |
| 133 |
| 134 void DownloadCommands::ExecuteCommand(Command command) { |
| 135 switch (command) { |
| 136 case SHOW_IN_FOLDER: |
| 137 download_item_->ShowDownloadInShell(); |
| 138 break; |
| 139 case OPEN_WHEN_COMPLETE: |
| 140 download_item_->OpenDownload(); |
| 141 break; |
| 142 case ALWAYS_OPEN_TYPE: { |
| 143 bool is_checked = IsCommandChecked(ALWAYS_OPEN_TYPE); |
| 144 DownloadPrefs* prefs = DownloadPrefs::FromBrowserContext( |
| 145 download_item_->GetBrowserContext()); |
| 146 #if defined(OS_WIN) || defined(OS_LINUX) || \ |
| 147 (defined(OS_MACOSX) && !defined(OS_IOS)) |
| 148 if (CanOpenPdfInSystemViewer()) { |
| 149 prefs->SetShouldOpenPdfInSystemReader(!is_checked); |
| 150 DownloadItemModel(download_item_) |
| 151 .SetShouldPreferOpeningInBrowser(is_checked); |
| 152 break; |
| 153 } |
| 154 #endif |
| 155 base::FilePath path = download_item_->GetTargetFilePath(); |
| 156 if (is_checked) |
| 157 prefs->DisableAutoOpenBasedOnExtension(path); |
| 158 else |
| 159 prefs->EnableAutoOpenBasedOnExtension(path); |
| 160 break; |
| 161 } |
| 162 case PLATFORM_OPEN: |
| 163 DownloadItemModel(download_item_).OpenUsingPlatformHandler(); |
| 164 break; |
| 165 case CANCEL: |
| 166 download_item_->Cancel(true /* Cancelled by user */); |
| 167 break; |
| 168 case DISCARD: |
| 169 download_item_->Remove(); |
| 170 break; |
| 171 case KEEP: |
| 172 download_item_->ValidateDangerousDownload(); |
| 173 break; |
| 174 case LEARN_MORE_SCANNING: { |
| 175 #if defined(FULL_SAFE_BROWSING) |
| 176 using safe_browsing::DownloadProtectionService; |
| 177 |
| 178 SafeBrowsingService* sb_service = |
| 179 g_browser_process->safe_browsing_service(); |
| 180 DownloadProtectionService* protection_service = |
| 181 (sb_service ? sb_service->download_protection_service() : nullptr); |
| 182 if (protection_service) |
| 183 protection_service->ShowDetailsForDownload(*download_item_, |
| 184 GetBrowser()); |
| 185 #else |
| 186 // Should only be getting invoked if we are using safe browsing. |
| 187 NOTREACHED(); |
| 188 #endif |
| 189 break; |
| 190 } |
| 191 case LEARN_MORE_INTERRUPTED: |
| 192 GetBrowser()->OpenURL(content::OpenURLParams( |
| 193 GURL(chrome::kDownloadInterruptedLearnMoreURL), content::Referrer(), |
| 194 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, false)); |
| 195 break; |
| 196 case PAUSE: |
| 197 download_item_->Pause(); |
| 198 break; |
| 199 case RESUME: |
| 200 download_item_->Resume(); |
| 201 break; |
| 202 case RETRY: |
| 203 if (download_item_->CanResume()) { |
| 204 download_item_->Resume(); |
| 205 } else { |
| 206 // TODO(yoshiki): Implement retry logic. |
| 207 } |
| 208 break; |
| 209 } |
| 210 } |
| 211 |
| 212 Browser* DownloadCommands::GetBrowser() const { |
| 213 Profile* profile = |
| 214 Profile::FromBrowserContext(download_item_->GetBrowserContext()); |
| 215 chrome::ScopedTabbedBrowserDisplayer browser_displayer( |
| 216 profile, chrome::GetActiveDesktop()); |
| 217 DCHECK(browser_displayer.browser()); |
| 218 return browser_displayer.browser(); |
| 219 } |
| 220 |
| 221 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
| 222 bool DownloadCommands::IsDownloadPdf() const { |
| 223 base::FilePath path = download_item_->GetTargetFilePath(); |
| 224 return path.MatchesExtension(FILE_PATH_LITERAL(".pdf")); |
| 225 } |
| 226 #endif |
| 227 |
| 228 bool DownloadCommands::CanOpenPdfInSystemViewer() const { |
| 229 #if defined(OS_WIN) |
| 230 bool is_adobe_pdf_reader_up_to_date = false; |
| 231 if (IsDownloadPdf() && IsAdobeReaderDefaultPDFViewer()) { |
| 232 is_adobe_pdf_reader_up_to_date = |
| 233 DownloadTargetDeterminer::IsAdobeReaderUpToDate(); |
| 234 } |
| 235 return IsDownloadPdf() && |
| 236 (IsAdobeReaderDefaultPDFViewer() ? is_adobe_pdf_reader_up_to_date |
| 237 : true); |
| 238 #elif defined(OS_MACOSX) || defined(OS_LINUX) |
| 239 return IsDownloadPdf(); |
| 240 #endif |
| 241 } |
OLD | NEW |