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