| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/extensions/file_manager/open_with_browser.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/threading/sequenced_worker_pool.h" | |
| 12 #include "chrome/browser/chromeos/drive/drive.pb.h" | |
| 13 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | |
| 14 #include "chrome/browser/chromeos/drive/file_system.h" | |
| 15 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
| 16 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" | |
| 17 #include "chrome/browser/extensions/crx_installer.h" | |
| 18 #include "chrome/browser/extensions/extension_install_prompt.h" | |
| 19 #include "chrome/browser/extensions/extension_service.h" | |
| 20 #include "chrome/browser/extensions/extension_system.h" | |
| 21 #include "chrome/browser/plugins/plugin_prefs.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | |
| 23 #include "chrome/browser/profiles/profile_manager.h" | |
| 24 #include "chrome/browser/ui/browser.h" | |
| 25 #include "chrome/browser/ui/browser_finder.h" | |
| 26 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 27 #include "chrome/browser/ui/browser_window.h" | |
| 28 #include "chrome/browser/ui/simple_message_box.h" | |
| 29 #include "chrome/common/chrome_paths.h" | |
| 30 #include "chrome/common/chrome_switches.h" | |
| 31 #include "chromeos/chromeos_switches.h" | |
| 32 #include "content/public/browser/browser_thread.h" | |
| 33 #include "content/public/browser/plugin_service.h" | |
| 34 #include "content/public/common/pepper_plugin_info.h" | |
| 35 #include "content/public/common/webplugininfo.h" | |
| 36 #include "net/base/net_util.h" | |
| 37 | |
| 38 using content::BrowserThread; | |
| 39 using content::PluginService; | |
| 40 | |
| 41 namespace file_manager { | |
| 42 namespace util { | |
| 43 namespace { | |
| 44 | |
| 45 const base::FilePath::CharType kCRXExtension[] = FILE_PATH_LITERAL(".crx"); | |
| 46 const base::FilePath::CharType kPdfExtension[] = FILE_PATH_LITERAL(".pdf"); | |
| 47 const base::FilePath::CharType kSwfExtension[] = FILE_PATH_LITERAL(".swf"); | |
| 48 | |
| 49 // List of file extensions viewable in the browser. | |
| 50 const base::FilePath::CharType* kFileExtensionsViewableInBrowser[] = { | |
| 51 #if defined(GOOGLE_CHROME_BUILD) | |
| 52 FILE_PATH_LITERAL(".pdf"), | |
| 53 FILE_PATH_LITERAL(".swf"), | |
| 54 #endif | |
| 55 FILE_PATH_LITERAL(".bmp"), | |
| 56 FILE_PATH_LITERAL(".jpg"), | |
| 57 FILE_PATH_LITERAL(".jpeg"), | |
| 58 FILE_PATH_LITERAL(".png"), | |
| 59 FILE_PATH_LITERAL(".webp"), | |
| 60 FILE_PATH_LITERAL(".gif"), | |
| 61 FILE_PATH_LITERAL(".txt"), | |
| 62 FILE_PATH_LITERAL(".html"), | |
| 63 FILE_PATH_LITERAL(".htm"), | |
| 64 FILE_PATH_LITERAL(".mhtml"), | |
| 65 FILE_PATH_LITERAL(".mht"), | |
| 66 FILE_PATH_LITERAL(".svg"), | |
| 67 }; | |
| 68 | |
| 69 // Returns true if |file_path| is viewable in the browser (ex. HTML file). | |
| 70 bool IsViewableInBrowser(const base::FilePath& file_path) { | |
| 71 for (size_t i = 0; i < arraysize(kFileExtensionsViewableInBrowser); i++) { | |
| 72 if (file_path.MatchesExtension(kFileExtensionsViewableInBrowser[i])) | |
| 73 return true; | |
| 74 } | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 bool IsPepperPluginEnabled(Profile* profile, | |
| 79 const base::FilePath& plugin_path) { | |
| 80 DCHECK(profile); | |
| 81 | |
| 82 content::PepperPluginInfo* pepper_info = | |
| 83 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(plugin_path); | |
| 84 if (!pepper_info) | |
| 85 return false; | |
| 86 | |
| 87 scoped_refptr<PluginPrefs> plugin_prefs = PluginPrefs::GetForProfile(profile); | |
| 88 if (!plugin_prefs.get()) | |
| 89 return false; | |
| 90 | |
| 91 return plugin_prefs->IsPluginEnabled(pepper_info->ToWebPluginInfo()); | |
| 92 } | |
| 93 | |
| 94 bool IsPdfPluginEnabled(Profile* profile) { | |
| 95 DCHECK(profile); | |
| 96 | |
| 97 base::FilePath plugin_path; | |
| 98 PathService::Get(chrome::FILE_PDF_PLUGIN, &plugin_path); | |
| 99 return IsPepperPluginEnabled(profile, plugin_path); | |
| 100 } | |
| 101 | |
| 102 bool IsFlashPluginEnabled(Profile* profile) { | |
| 103 DCHECK(profile); | |
| 104 | |
| 105 base::FilePath plugin_path( | |
| 106 CommandLine::ForCurrentProcess()->GetSwitchValueNative( | |
| 107 switches::kPpapiFlashPath)); | |
| 108 if (plugin_path.empty()) | |
| 109 PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &plugin_path); | |
| 110 return IsPepperPluginEnabled(profile, plugin_path); | |
| 111 } | |
| 112 | |
| 113 void OpenNewTab(Profile* profile, const GURL& url) { | |
| 114 DCHECK(profile); | |
| 115 | |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 117 Browser* browser = chrome::FindOrCreateTabbedBrowser( | |
| 118 profile, chrome::HOST_DESKTOP_TYPE_ASH); | |
| 119 chrome::AddSelectedTabWithURL(browser, url, content::PAGE_TRANSITION_LINK); | |
| 120 // If the current browser is not tabbed then the new tab will be created | |
| 121 // in a different browser. Make sure it is visible. | |
| 122 browser->window()->Show(); | |
| 123 } | |
| 124 | |
| 125 void InstallCRX(Profile* profile, const base::FilePath& file_path) { | |
| 126 DCHECK(profile); | |
| 127 | |
| 128 ExtensionService* service = | |
| 129 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 130 CHECK(service); | |
| 131 | |
| 132 scoped_refptr<extensions::CrxInstaller> installer( | |
| 133 extensions::CrxInstaller::Create( | |
| 134 service, | |
| 135 scoped_ptr<ExtensionInstallPrompt>(new ExtensionInstallPrompt( | |
| 136 profile, NULL, NULL)))); | |
| 137 installer->set_error_on_unsupported_requirements(true); | |
| 138 installer->set_is_gallery_install(false); | |
| 139 installer->set_allow_silent_install(false); | |
| 140 installer->InstallCrx(file_path); | |
| 141 } | |
| 142 | |
| 143 // Called when a crx file on Drive was downloaded. | |
| 144 void OnCRXDownloadCallback(Profile* profile, | |
| 145 drive::FileError error, | |
| 146 const base::FilePath& file, | |
| 147 scoped_ptr<drive::ResourceEntry> entry) { | |
| 148 DCHECK(profile); | |
| 149 | |
| 150 if (error != drive::FILE_ERROR_OK) | |
| 151 return; | |
| 152 InstallCRX(profile, file); | |
| 153 } | |
| 154 | |
| 155 // Reads the alternate URL from a GDoc file. When it fails, returns a file URL | |
| 156 // for |file_path| as fallback. | |
| 157 // Note that an alternate url is a URL to open a hosted document. | |
| 158 GURL ReadUrlFromGDocOnBlockingPool(const base::FilePath& file_path) { | |
| 159 GURL url = drive::util::ReadUrlFromGDocFile(file_path); | |
| 160 if (url.is_empty()) | |
| 161 url = net::FilePathToFileURL(file_path); | |
| 162 return url; | |
| 163 } | |
| 164 | |
| 165 } // namespace | |
| 166 | |
| 167 bool OpenFileWithBrowser(Profile* profile, const base::FilePath& file_path) { | |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 169 DCHECK(profile); | |
| 170 | |
| 171 // For things supported natively by the browser, we should open it | |
| 172 // in a tab. | |
| 173 if (IsViewableInBrowser(file_path) || | |
| 174 ShouldBeOpenedWithPlugin(profile, file_path.Extension())) { | |
| 175 GURL page_url = net::FilePathToFileURL(file_path); | |
| 176 // Override drive resource to point to internal handler instead of file URL. | |
| 177 if (drive::util::IsUnderDriveMountPoint(file_path)) { | |
| 178 page_url = drive::util::FilePathToDriveURL( | |
| 179 drive::util::ExtractDrivePath(file_path)); | |
| 180 } | |
| 181 OpenNewTab(profile, page_url); | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 if (drive::util::HasGDocFileExtension(file_path)) { | |
| 186 if (drive::util::IsUnderDriveMountPoint(file_path)) { | |
| 187 // The file is on Google Docs. Open with drive URL. | |
| 188 GURL url = drive::util::FilePathToDriveURL( | |
| 189 drive::util::ExtractDrivePath(file_path)); | |
| 190 OpenNewTab(profile, url); | |
| 191 } else { | |
| 192 // The file is local (downloaded from an attachment or otherwise copied). | |
| 193 // Parse the file to extract the Docs url and open this url. | |
| 194 base::PostTaskAndReplyWithResult( | |
| 195 BrowserThread::GetBlockingPool(), | |
| 196 FROM_HERE, | |
| 197 base::Bind(&ReadUrlFromGDocOnBlockingPool, file_path), | |
| 198 base::Bind(&OpenNewTab, static_cast<Profile*>(NULL))); | |
| 199 } | |
| 200 return true; | |
| 201 } | |
| 202 | |
| 203 if (file_path.MatchesExtension(kCRXExtension)) { | |
| 204 if (drive::util::IsUnderDriveMountPoint(file_path)) { | |
| 205 drive::DriveIntegrationService* integration_service = | |
| 206 drive::DriveIntegrationServiceFactory::GetForProfile(profile); | |
| 207 if (!integration_service) | |
| 208 return false; | |
| 209 integration_service->file_system()->GetFileByPath( | |
| 210 drive::util::ExtractDrivePath(file_path), | |
| 211 base::Bind(&OnCRXDownloadCallback, profile)); | |
| 212 } else { | |
| 213 InstallCRX(profile, file_path); | |
| 214 } | |
| 215 return true; | |
| 216 } | |
| 217 | |
| 218 // Failed to open the file of unknown type. | |
| 219 LOG(WARNING) << "Unknown file type: " << file_path.value(); | |
| 220 return false; | |
| 221 } | |
| 222 | |
| 223 // If a bundled plugin is enabled, we should open pdf/swf files in a tab. | |
| 224 bool ShouldBeOpenedWithPlugin( | |
| 225 Profile* profile, | |
| 226 const base::FilePath::StringType& file_extension) { | |
| 227 DCHECK(profile); | |
| 228 | |
| 229 const base::FilePath file_path = | |
| 230 base::FilePath::FromUTF8Unsafe("dummy").AddExtension(file_extension); | |
| 231 if (file_path.MatchesExtension(kPdfExtension)) | |
| 232 return IsPdfPluginEnabled(profile); | |
| 233 if (file_path.MatchesExtension(kSwfExtension)) | |
| 234 return IsFlashPluginEnabled(profile); | |
| 235 return false; | |
| 236 } | |
| 237 | |
| 238 } // namespace util | |
| 239 } // namespace file_manager | |
| OLD | NEW |