Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager/open_with_browser.cc

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

Powered by Google App Engine
This is Rietveld 408576698