Pass the WebFrame to closeHelperPlugin().
[chromium-blink-merge.git] / apps / launcher.cc
blobe5990a50a5ed8e8d49f48e426a74b16591a3830f
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.
5 #include "apps/launcher.h"
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
15 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
16 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
17 #include "chrome/browser/extensions/event_router.h"
18 #include "chrome/browser/extensions/extension_host.h"
19 #include "chrome/browser/extensions/extension_prefs.h"
20 #include "chrome/browser/extensions/extension_process_manager.h"
21 #include "chrome/browser/extensions/extension_service.h"
22 #include "chrome/browser/extensions/extension_system.h"
23 #include "chrome/browser/extensions/lazy_background_task_queue.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/apps/app_metro_infobar_delegate_win.h"
26 #include "chrome/common/extensions/api/app_runtime.h"
27 #include "chrome/common/extensions/extension.h"
28 #include "chrome/common/extensions/extension_messages.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/render_process_host.h"
31 #include "content/public/browser/web_contents.h"
32 #include "net/base/mime_util.h"
33 #include "net/base/net_util.h"
35 #if defined(OS_CHROMEOS)
36 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
37 #include "chrome/browser/chromeos/drive/file_errors.h"
38 #include "chrome/browser/chromeos/drive/file_system_interface.h"
39 #include "chrome/browser/chromeos/drive/file_system_util.h"
40 #endif
42 #if defined(OS_WIN)
43 #include "win8/util/win8_util.h"
44 #endif
46 namespace app_runtime = extensions::api::app_runtime;
48 using content::BrowserThread;
49 using extensions::app_file_handler_util::FileHandlerForId;
50 using extensions::app_file_handler_util::FileHandlerCanHandleFile;
51 using extensions::app_file_handler_util::FirstFileHandlerForFile;
52 using extensions::app_file_handler_util::CreateFileEntry;
53 using extensions::app_file_handler_util::GrantedFileEntry;
54 using extensions::Extension;
55 using extensions::ExtensionHost;
56 using extensions::ExtensionSystem;
58 namespace apps {
60 namespace {
62 const char kFallbackMimeType[] = "application/octet-stream";
64 bool MakePathAbsolute(const base::FilePath& current_directory,
65 base::FilePath* file_path) {
66 DCHECK(file_path);
67 if (file_path->IsAbsolute())
68 return true;
70 if (current_directory.empty()) {
71 *file_path = base::MakeAbsoluteFilePath(*file_path);
72 return !file_path->empty();
75 if (!current_directory.IsAbsolute())
76 return false;
78 *file_path = current_directory.Append(*file_path);
79 return true;
82 bool GetAbsolutePathFromCommandLine(const CommandLine* command_line,
83 const base::FilePath& current_directory,
84 base::FilePath* path) {
85 if (!command_line || !command_line->GetArgs().size())
86 return false;
88 base::FilePath relative_path(command_line->GetArgs()[0]);
89 base::FilePath absolute_path(relative_path);
90 if (!MakePathAbsolute(current_directory, &absolute_path)) {
91 LOG(WARNING) << "Cannot make absolute path from " << relative_path.value();
92 return false;
94 *path = absolute_path;
95 return true;
98 // Helper method to launch the platform app |extension| with no data. This
99 // should be called in the fallback case, where it has been impossible to
100 // load or obtain file launch data.
101 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
103 extensions::AppEventRouter::DispatchOnLaunchedEvent(profile, extension);
106 // Class to handle launching of platform apps to open a specific path.
107 // An instance of this class is created for each launch. The lifetime of these
108 // instances is managed by reference counted pointers. As long as an instance
109 // has outstanding tasks on a message queue it will be retained; once all
110 // outstanding tasks are completed it will be deleted.
111 class PlatformAppPathLauncher
112 : public base::RefCountedThreadSafe<PlatformAppPathLauncher> {
113 public:
114 PlatformAppPathLauncher(Profile* profile,
115 const Extension* extension,
116 const base::FilePath& file_path)
117 : profile_(profile), extension_(extension), file_path_(file_path) {}
119 void Launch() {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
121 if (file_path_.empty()) {
122 LaunchPlatformAppWithNoData(profile_, extension_);
123 return;
126 DCHECK(file_path_.IsAbsolute());
128 #if defined(OS_CHROMEOS)
129 if (drive::util::IsUnderDriveMountPoint(file_path_)) {
130 GetMimeTypeAndLaunchForDriveFile();
131 return;
133 #endif
135 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
136 &PlatformAppPathLauncher::GetMimeTypeAndLaunch, this));
139 void LaunchWithHandler(const std::string& handler_id) {
140 handler_id_ = handler_id;
141 Launch();
144 private:
145 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>;
147 virtual ~PlatformAppPathLauncher() {}
149 void GetMimeTypeAndLaunch() {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
152 // If the file doesn't exist, or is a directory, launch with no launch data.
153 if (!base::PathExists(file_path_) ||
154 base::DirectoryExists(file_path_)) {
155 LOG(WARNING) << "No file exists with path " << file_path_.value();
156 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
157 &PlatformAppPathLauncher::LaunchWithNoLaunchData, this));
158 return;
161 std::string mime_type;
162 if (!net::GetMimeTypeFromFile(file_path_, &mime_type))
163 mime_type = kFallbackMimeType;
165 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
166 &PlatformAppPathLauncher::LaunchWithMimeType, this, mime_type));
169 #if defined(OS_CHROMEOS)
170 void GetMimeTypeAndLaunchForDriveFile() {
171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
173 drive::DriveIntegrationService* service =
174 drive::DriveIntegrationServiceFactory::FindForProfile(profile_);
175 if (!service) {
176 LaunchWithNoLaunchData();
177 return;
180 service->file_system()->GetFileByPath(
181 drive::util::ExtractDrivePath(file_path_),
182 base::Bind(&PlatformAppPathLauncher::OnGotDriveFile, this));
185 void OnGotDriveFile(drive::FileError error,
186 const base::FilePath& file_path,
187 scoped_ptr<drive::ResourceEntry> entry) {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 if (error != drive::FILE_ERROR_OK ||
191 !entry || entry->file_specific_info().is_hosted_document()) {
192 LaunchWithNoLaunchData();
193 return;
196 const std::string& mime_type =
197 entry->file_specific_info().content_mime_type();
198 LaunchWithMimeType(mime_type.empty() ? kFallbackMimeType : mime_type);
200 #endif // defined(OS_CHROMEOS)
202 void LaunchWithNoLaunchData() {
203 // This method is required as an entry point on the UI thread.
204 LaunchPlatformAppWithNoData(profile_, extension_);
207 void LaunchWithMimeType(const std::string& mime_type) {
208 // Find file handler from the platform app for the file being opened.
209 const extensions::FileHandlerInfo* handler = NULL;
210 if (!handler_id_.empty())
211 handler = FileHandlerForId(*extension_, handler_id_);
212 else
213 handler = FirstFileHandlerForFile(*extension_, mime_type, file_path_);
214 if (handler && !FileHandlerCanHandleFile(*handler, mime_type, file_path_)) {
215 LOG(WARNING) << "Extension does not provide a valid file handler for "
216 << file_path_.value();
217 LaunchWithNoLaunchData();
218 return;
221 // If this app doesn't have a file handler that supports the file, launch
222 // with no launch data.
223 if (!handler) {
224 LOG(WARNING) << "Extension does not provide a valid file handler for "
225 << file_path_.value();
226 LaunchWithNoLaunchData();
227 return;
230 if (handler_id_.empty())
231 handler_id_ = handler->id;
233 // Access needs to be granted to the file for the process associated with
234 // the extension. To do this the ExtensionHost is needed. This might not be
235 // available, or it might be in the process of being unloaded, in which case
236 // the lazy background task queue is used to load the extension and then
237 // call back to us.
238 extensions::LazyBackgroundTaskQueue* queue =
239 ExtensionSystem::Get(profile_)->lazy_background_task_queue();
240 if (queue->ShouldEnqueueTask(profile_, extension_)) {
241 queue->AddPendingTask(profile_, extension_->id(), base::Bind(
242 &PlatformAppPathLauncher::GrantAccessToFileAndLaunch,
243 this, mime_type));
244 return;
247 ExtensionProcessManager* process_manager =
248 ExtensionSystem::Get(profile_)->process_manager();
249 ExtensionHost* host =
250 process_manager->GetBackgroundHostForExtension(extension_->id());
251 DCHECK(host);
252 GrantAccessToFileAndLaunch(mime_type, host);
255 void GrantAccessToFileAndLaunch(const std::string& mime_type,
256 ExtensionHost* host) {
257 // If there was an error loading the app page, |host| will be NULL.
258 if (!host) {
259 LOG(ERROR) << "Could not load app page for " << extension_->id();
260 return;
263 GrantedFileEntry file_entry = CreateFileEntry(
264 profile_,
265 extension_->id(),
266 host->render_process_host()->GetID(),
267 file_path_,
268 false);
269 extensions::AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
270 profile_, extension_, handler_id_, mime_type, file_entry);
273 // The profile the app should be run in.
274 Profile* profile_;
275 // The extension providing the app.
276 const Extension* extension_;
277 // The path to be passed through to the app.
278 const base::FilePath file_path_;
279 // The ID of the file handler used to launch the app.
280 std::string handler_id_;
282 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher);
285 } // namespace
287 void LaunchPlatformAppWithCommandLine(Profile* profile,
288 const Extension* extension,
289 const CommandLine* command_line,
290 const base::FilePath& current_directory) {
291 #if defined(OS_WIN)
292 // On Windows 8's single window Metro mode we can not launch platform apps.
293 // Offer to switch Chrome to desktop mode.
294 if (win8::IsSingleWindowMetroMode()) {
295 AppMetroInfoBarDelegateWin::Create(
296 profile, AppMetroInfoBarDelegateWin::LAUNCH_PACKAGED_APP,
297 extension->id());
298 return;
300 #endif
302 base::FilePath path;
303 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) {
304 LaunchPlatformAppWithNoData(profile, extension);
305 return;
308 // TODO(benwells): add a command-line argument to provide a handler ID.
309 LaunchPlatformAppWithPath(profile, extension, path);
312 void LaunchPlatformAppWithPath(Profile* profile,
313 const Extension* extension,
314 const base::FilePath& file_path) {
315 // launcher will be freed when nothing has a reference to it. The message
316 // queue will retain a reference for any outstanding task, so when the
317 // launcher has finished it will be freed.
318 scoped_refptr<PlatformAppPathLauncher> launcher =
319 new PlatformAppPathLauncher(profile, extension, file_path);
320 launcher->Launch();
323 void LaunchPlatformApp(Profile* profile, const Extension* extension) {
324 LaunchPlatformAppWithCommandLine(profile, extension, NULL, base::FilePath());
327 void LaunchPlatformAppWithFileHandler(Profile* profile,
328 const Extension* extension,
329 const std::string& handler_id,
330 const base::FilePath& file_path) {
331 scoped_refptr<PlatformAppPathLauncher> launcher =
332 new PlatformAppPathLauncher(profile, extension, file_path);
333 launcher->LaunchWithHandler(handler_id);
336 void RestartPlatformApp(Profile* profile, const Extension* extension) {
337 #if defined(OS_WIN)
338 // On Windows 8's single window Metro mode we can not launch platform apps.
339 // In restart we are just making sure launch doesn't slip through.
340 if (win8::IsSingleWindowMetroMode())
341 return;
342 #endif
343 extensions::EventRouter* event_router =
344 ExtensionSystem::Get(profile)->event_router();
345 bool listening_to_restart = event_router->
346 ExtensionHasEventListener(extension->id(),
347 app_runtime::OnRestarted::kEventName);
349 if (listening_to_restart) {
350 extensions::AppEventRouter::DispatchOnRestartedEvent(profile, extension);
351 return;
354 extensions::ExtensionPrefs* extension_prefs = ExtensionSystem::Get(profile)->
355 extension_service()->extension_prefs();
356 bool had_windows = extension_prefs->IsActive(extension->id());
357 extension_prefs->SetIsActive(extension->id(), false);
358 bool listening_to_launch = event_router->
359 ExtensionHasEventListener(extension->id(),
360 app_runtime::OnLaunched::kEventName);
362 if (listening_to_launch && had_windows)
363 LaunchPlatformAppWithNoData(profile, extension);
366 } // namespace apps