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/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/browser_process.h" 13 #include "chrome/browser/chromeos/drive/file_system_util.h" 14 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" 15 #include "chrome/browser/plugins/plugin_prefs.h" 16 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile_manager.h" 18 #include "chrome/browser/ui/browser.h" 19 #include "chrome/browser/ui/browser_tabstrip.h" 20 #include "chrome/browser/ui/browser_window.h" 21 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" 22 #include "chrome/browser/ui/simple_message_box.h" 23 #include "chrome/common/chrome_paths.h" 24 #include "chrome/common/chrome_switches.h" 25 #include "chromeos/chromeos_switches.h" 26 #include "content/public/browser/browser_thread.h" 27 #include "content/public/browser/plugin_service.h" 28 #include "content/public/common/pepper_plugin_info.h" 29 #include "content/public/common/webplugininfo.h" 30 #include "net/base/net_util.h" 31 32 using content::BrowserThread; 33 using content::PluginService; 34 35 namespace file_manager { 36 namespace util { 37 namespace { 38 39 const base::FilePath::CharType kPdfExtension[] = FILE_PATH_LITERAL(".pdf"); 40 const base::FilePath::CharType kSwfExtension[] = FILE_PATH_LITERAL(".swf"); 41 42 // List of file extensions viewable in the browser. 43 const base::FilePath::CharType* kFileExtensionsViewableInBrowser[] = { 44 #if defined(GOOGLE_CHROME_BUILD) 45 FILE_PATH_LITERAL(".pdf"), 46 FILE_PATH_LITERAL(".swf"), 47 #endif 48 FILE_PATH_LITERAL(".bmp"), 49 FILE_PATH_LITERAL(".ico"), 50 FILE_PATH_LITERAL(".jpg"), 51 FILE_PATH_LITERAL(".jpeg"), 52 FILE_PATH_LITERAL(".png"), 53 FILE_PATH_LITERAL(".webp"), 54 FILE_PATH_LITERAL(".gif"), 55 FILE_PATH_LITERAL(".txt"), 56 FILE_PATH_LITERAL(".html"), 57 FILE_PATH_LITERAL(".htm"), 58 FILE_PATH_LITERAL(".mhtml"), 59 FILE_PATH_LITERAL(".mht"), 60 FILE_PATH_LITERAL(".svg"), 61 }; 62 63 // Returns true if |file_path| is viewable in the browser (ex. HTML file). 64 bool IsViewableInBrowser(const base::FilePath& file_path) { 65 for (size_t i = 0; i < arraysize(kFileExtensionsViewableInBrowser); i++) { 66 if (file_path.MatchesExtension(kFileExtensionsViewableInBrowser[i])) 67 return true; 68 } 69 return false; 70 } 71 72 bool IsPepperPluginEnabled(Profile* profile, 73 const base::FilePath& plugin_path) { 74 DCHECK(profile); 75 76 content::PepperPluginInfo* pepper_info = 77 PluginService::GetInstance()->GetRegisteredPpapiPluginInfo(plugin_path); 78 if (!pepper_info) 79 return false; 80 81 scoped_refptr<PluginPrefs> plugin_prefs = PluginPrefs::GetForProfile(profile); 82 if (!plugin_prefs.get()) 83 return false; 84 85 return plugin_prefs->IsPluginEnabled(pepper_info->ToWebPluginInfo()); 86 } 87 88 bool IsPdfPluginEnabled(Profile* profile) { 89 DCHECK(profile); 90 91 base::FilePath plugin_path; 92 PathService::Get(chrome::FILE_PDF_PLUGIN, &plugin_path); 93 return IsPepperPluginEnabled(profile, plugin_path); 94 } 95 96 bool IsFlashPluginEnabled(Profile* profile) { 97 DCHECK(profile); 98 99 base::FilePath plugin_path( 100 CommandLine::ForCurrentProcess()->GetSwitchValueNative( 101 switches::kPpapiFlashPath)); 102 if (plugin_path.empty()) 103 PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &plugin_path); 104 return IsPepperPluginEnabled(profile, plugin_path); 105 } 106 107 void OpenNewTab(Profile* profile, const GURL& url) { 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 109 110 // Check the validity of the pointer so that the closure from 111 // base::Bind(&OpenNewTab, profile) can be passed between threads. 112 if (!g_browser_process->profile_manager()->IsValidProfile(profile)) 113 return; 114 115 chrome::ScopedTabbedBrowserDisplayer displayer( 116 profile, chrome::HOST_DESKTOP_TYPE_ASH); 117 chrome::AddSelectedTabWithURL(displayer.browser(), url, 118 content::PAGE_TRANSITION_LINK); 119 } 120 121 // Reads the alternate URL from a GDoc file. When it fails, returns a file URL 122 // for |file_path| as fallback. 123 // Note that an alternate url is a URL to open a hosted document. 124 GURL ReadUrlFromGDocOnBlockingPool(const base::FilePath& file_path) { 125 GURL url = drive::util::ReadUrlFromGDocFile(file_path); 126 if (url.is_empty()) 127 url = net::FilePathToFileURL(file_path); 128 return url; 129 } 130 131 } // namespace 132 133 bool OpenFileWithBrowser(Profile* profile, const base::FilePath& file_path) { 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 135 DCHECK(profile); 136 137 // For things supported natively by the browser, we should open it 138 // in a tab. 139 if (IsViewableInBrowser(file_path) || 140 ShouldBeOpenedWithPlugin(profile, file_path.Extension())) { 141 GURL page_url = net::FilePathToFileURL(file_path); 142 // Override drive resource to point to internal handler instead of file URL. 143 if (drive::util::IsUnderDriveMountPoint(file_path)) { 144 page_url = drive::util::FilePathToDriveURL( 145 drive::util::ExtractDrivePath(file_path)); 146 } 147 OpenNewTab(profile, page_url); 148 return true; 149 } 150 151 if (drive::util::HasGDocFileExtension(file_path)) { 152 if (drive::util::IsUnderDriveMountPoint(file_path)) { 153 // The file is on Google Docs. Open with drive URL. 154 GURL url = drive::util::FilePathToDriveURL( 155 drive::util::ExtractDrivePath(file_path)); 156 OpenNewTab(profile, url); 157 } else { 158 // The file is local (downloaded from an attachment or otherwise copied). 159 // Parse the file to extract the Docs url and open this url. 160 base::PostTaskAndReplyWithResult( 161 BrowserThread::GetBlockingPool(), 162 FROM_HERE, 163 base::Bind(&ReadUrlFromGDocOnBlockingPool, file_path), 164 base::Bind(&OpenNewTab, profile)); 165 } 166 return true; 167 } 168 169 // Failed to open the file of unknown type. 170 LOG(WARNING) << "Unknown file type: " << file_path.value(); 171 return false; 172 } 173 174 // If a bundled plugin is enabled, we should open pdf/swf files in a tab. 175 bool ShouldBeOpenedWithPlugin( 176 Profile* profile, 177 const base::FilePath::StringType& file_extension) { 178 DCHECK(profile); 179 180 const base::FilePath file_path = 181 base::FilePath::FromUTF8Unsafe("dummy").AddExtension(file_extension); 182 if (file_path.MatchesExtension(kPdfExtension)) 183 return IsPdfPluginEnabled(profile); 184 if (file_path.MatchesExtension(kSwfExtension)) 185 return IsFlashPluginEnabled(profile); 186 return false; 187 } 188 189 } // namespace util 190 } // namespace file_manager 191