Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/launcher.h" | 5 #include "apps/launcher.h" |
| 6 | 6 |
| 7 #include "apps/browser/api/app_runtime/app_runtime_api.h" | 7 #include "apps/browser/api/app_runtime/app_runtime_api.h" |
| 8 #include "apps/browser/file_handler_util.h" | 8 #include "apps/browser/file_handler_util.h" |
| 9 #include "apps/common/api/app_runtime.h" | 9 #include "apps/common/api/app_runtime.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 56 using extensions::Extension; | 56 using extensions::Extension; |
| 57 using extensions::ExtensionHost; | 57 using extensions::ExtensionHost; |
| 58 using extensions::ExtensionSystem; | 58 using extensions::ExtensionSystem; |
| 59 | 59 |
| 60 namespace apps { | 60 namespace apps { |
| 61 | 61 |
| 62 namespace { | 62 namespace { |
| 63 | 63 |
| 64 const char kFallbackMimeType[] = "application/octet-stream"; | 64 const char kFallbackMimeType[] = "application/octet-stream"; |
| 65 | 65 |
| 66 bool MakePathAbsolute(const base::FilePath& current_directory, | 66 bool DoMakePathAbsolute(const base::FilePath& current_directory, |
| 67 base::FilePath* file_path) { | 67 base::FilePath* file_path) { |
| 68 DCHECK(file_path); | 68 DCHECK(file_path); |
| 69 if (file_path->IsAbsolute()) | 69 if (file_path->IsAbsolute()) |
| 70 return true; | 70 return true; |
| 71 | 71 |
| 72 if (current_directory.empty()) { | 72 if (current_directory.empty()) { |
| 73 *file_path = base::MakeAbsoluteFilePath(*file_path); | 73 *file_path = base::MakeAbsoluteFilePath(*file_path); |
| 74 return !file_path->empty(); | 74 return !file_path->empty(); |
| 75 } | 75 } |
| 76 | 76 |
| 77 if (!current_directory.IsAbsolute()) | 77 if (!current_directory.IsAbsolute()) |
| 78 return false; | 78 return false; |
| 79 | 79 |
| 80 *file_path = current_directory.Append(*file_path); | 80 *file_path = current_directory.Append(*file_path); |
| 81 return true; | 81 return true; |
| 82 } | 82 } |
| 83 | 83 |
| 84 bool GetAbsolutePathFromCommandLine(const CommandLine& command_line, | |
| 85 const base::FilePath& current_directory, | |
| 86 base::FilePath* path) { | |
| 87 if (!command_line.GetArgs().size()) | |
| 88 return false; | |
| 89 | |
| 90 base::FilePath relative_path(command_line.GetArgs()[0]); | |
| 91 base::FilePath absolute_path(relative_path); | |
| 92 if (!MakePathAbsolute(current_directory, &absolute_path)) { | |
| 93 LOG(WARNING) << "Cannot make absolute path from " << relative_path.value(); | |
| 94 return false; | |
| 95 } | |
| 96 *path = absolute_path; | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 // Helper method to launch the platform app |extension| with no data. This | 84 // Helper method to launch the platform app |extension| with no data. This |
| 101 // should be called in the fallback case, where it has been impossible to | 85 // should be called in the fallback case, where it has been impossible to |
| 102 // load or obtain file launch data. | 86 // load or obtain file launch data. |
| 103 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { | 87 void LaunchPlatformAppWithNoData(Profile* profile, const Extension* extension) { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 105 AppEventRouter::DispatchOnLaunchedEvent(profile, extension); | 89 AppEventRouter::DispatchOnLaunchedEvent(profile, extension); |
| 106 } | 90 } |
| 107 | 91 |
| 108 // Class to handle launching of platform apps to open a specific path. | 92 // Class to handle launching of platform apps to open a specific path. |
| 109 // An instance of this class is created for each launch. The lifetime of these | 93 // An instance of this class is created for each launch. The lifetime of these |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 140 } | 124 } |
| 141 | 125 |
| 142 OnFileValid(); | 126 OnFileValid(); |
| 143 } | 127 } |
| 144 | 128 |
| 145 void LaunchWithHandler(const std::string& handler_id) { | 129 void LaunchWithHandler(const std::string& handler_id) { |
| 146 handler_id_ = handler_id; | 130 handler_id_ = handler_id; |
| 147 Launch(); | 131 Launch(); |
| 148 } | 132 } |
| 149 | 133 |
| 134 void LaunchWithRelativePath(const base::FilePath& current_directory) { | |
| 135 BrowserThread::PostTask( | |
| 136 BrowserThread::FILE, | |
| 137 FROM_HERE, | |
| 138 base::Bind(&PlatformAppPathLauncher::MakePathAbsolute, | |
| 139 this, | |
| 140 current_directory)); | |
| 141 } | |
| 142 | |
| 150 private: | 143 private: |
| 151 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; | 144 friend class base::RefCountedThreadSafe<PlatformAppPathLauncher>; |
| 152 | 145 |
| 153 virtual ~PlatformAppPathLauncher() {} | 146 virtual ~PlatformAppPathLauncher() {} |
| 154 | 147 |
| 148 void MakePathAbsolute(const base::FilePath& current_directory) { | |
| 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
|
tapted
2014/05/12 00:27:45
oh! I forgot - there's now also DCHECK_CURRENTLY_O
benwells
2014/05/12 05:51:05
Nice, I didn't know about that.
| |
| 150 | |
| 151 if (!DoMakePathAbsolute(current_directory, &file_path_)) { | |
| 152 LOG(WARNING) << "Cannot make absolute path from " << file_path_.value(); | |
| 153 file_path_ = base::FilePath(); | |
| 154 } | |
| 155 | |
| 156 BrowserThread::PostTask(BrowserThread::UI, | |
| 157 FROM_HERE, | |
| 158 base::Bind(&PlatformAppPathLauncher::Launch, this)); | |
| 159 } | |
| 160 | |
| 155 void OnFileValid() { | 161 void OnFileValid() { |
| 156 #if defined(OS_CHROMEOS) | 162 #if defined(OS_CHROMEOS) |
| 157 if (drive::util::IsUnderDriveMountPoint(file_path_)) { | 163 if (drive::util::IsUnderDriveMountPoint(file_path_)) { |
| 158 PlatformAppPathLauncher::GetMimeTypeAndLaunchForDriveFile(); | 164 PlatformAppPathLauncher::GetMimeTypeAndLaunchForDriveFile(); |
| 159 return; | 165 return; |
| 160 } | 166 } |
| 161 #endif | 167 #endif |
| 162 | 168 |
| 163 BrowserThread::PostTask( | 169 BrowserThread::PostTask( |
| 164 BrowserThread::FILE, | 170 BrowserThread::FILE, |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 host->render_process_host()->GetID(), | 309 host->render_process_host()->GetID(), |
| 304 file_path_, | 310 file_path_, |
| 305 false); | 311 false); |
| 306 AppEventRouter::DispatchOnLaunchedEventWithFileEntry( | 312 AppEventRouter::DispatchOnLaunchedEventWithFileEntry( |
| 307 profile_, extension_, handler_id_, mime_type, file_entry); | 313 profile_, extension_, handler_id_, mime_type, file_entry); |
| 308 } | 314 } |
| 309 | 315 |
| 310 // The profile the app should be run in. | 316 // The profile the app should be run in. |
| 311 Profile* profile_; | 317 Profile* profile_; |
| 312 // The extension providing the app. | 318 // The extension providing the app. |
| 313 const Extension* extension_; | 319 const Extension* extension_; |
|
tapted
2014/05/12 00:07:51
should this be a scoped_refptr?
benwells
2014/05/12 00:18:51
I don't think so. My understanding is that extensi
benwells
2014/05/12 05:51:05
As discussed, added a TODO for this.
| |
| 314 // The path to be passed through to the app. | 320 // The path to be passed through to the app. |
| 315 const base::FilePath file_path_; | 321 base::FilePath file_path_; |
| 316 // The ID of the file handler used to launch the app. | 322 // The ID of the file handler used to launch the app. |
| 317 std::string handler_id_; | 323 std::string handler_id_; |
| 318 | 324 |
| 319 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); | 325 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
| 320 }; | 326 }; |
| 321 | 327 |
| 322 } // namespace | 328 } // namespace |
| 323 | 329 |
| 324 void LaunchPlatformAppWithCommandLine(Profile* profile, | 330 void LaunchPlatformAppWithCommandLine(Profile* profile, |
| 325 const Extension* extension, | 331 const Extension* extension, |
| 326 const CommandLine& command_line, | 332 const CommandLine& command_line, |
| 327 const base::FilePath& current_directory) { | 333 const base::FilePath& current_directory) { |
| 328 // An app with "kiosk_only" should not be installed and launched | 334 // An app with "kiosk_only" should not be installed and launched |
| 329 // outside of ChromeOS kiosk mode in the first place. This is a defensive | 335 // outside of ChromeOS kiosk mode in the first place. This is a defensive |
| 330 // check in case this scenario does occur. | 336 // check in case this scenario does occur. |
| 331 if (extensions::KioskModeInfo::IsKioskOnly(extension)) { | 337 if (extensions::KioskModeInfo::IsKioskOnly(extension)) { |
| 332 bool in_kiosk_mode = false; | 338 bool in_kiosk_mode = false; |
| 333 #if defined(OS_CHROMEOS) | 339 #if defined(OS_CHROMEOS) |
| 334 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); | 340 chromeos::UserManager* user_manager = chromeos::UserManager::Get(); |
| 335 in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp(); | 341 in_kiosk_mode = user_manager && user_manager->IsLoggedInAsKioskApp(); |
| 336 #endif | 342 #endif |
| 337 if (!in_kiosk_mode) { | 343 if (!in_kiosk_mode) { |
| 338 LOG(ERROR) << "App with 'kiosk_only' attribute must be run in " | 344 LOG(ERROR) << "App with 'kiosk_only' attribute must be run in " |
| 339 << " ChromeOS kiosk mode."; | 345 << " ChromeOS kiosk mode."; |
| 340 NOTREACHED(); | 346 NOTREACHED(); |
| 341 return; | 347 return; |
| 342 } | 348 } |
| 343 } | 349 } |
| 344 | 350 |
| 345 base::FilePath path; | 351 if (!command_line.GetArgs().size()) { |
|
tapted
2014/05/12 00:07:51
nit: if (command_line.GetArgs().empty()) {
benwells
2014/05/12 05:51:05
Done.
| |
| 346 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { | |
| 347 LaunchPlatformAppWithNoData(profile, extension); | 352 LaunchPlatformAppWithNoData(profile, extension); |
| 348 return; | 353 return; |
| 349 } | 354 } |
| 350 | 355 |
| 351 // TODO(benwells): add a command-line argument to provide a handler ID. | 356 base::FilePath file_path(command_line.GetArgs()[0]); |
| 352 LaunchPlatformAppWithPath(profile, extension, path); | 357 // launcher will be freed when nothing has a reference to it. The message |
|
tapted
2014/05/12 00:07:51
nit: this comment seems like it should be on the c
benwells
2014/05/12 00:18:51
Yep, I'll move it. I remember adding this comment
benwells
2014/05/12 05:51:05
Actually there is already a comment there :)
| |
| 358 // queue will retain a reference for any outstanding task, so when the | |
| 359 // launcher has finished it will be freed. | |
| 360 scoped_refptr<PlatformAppPathLauncher> launcher = | |
| 361 new PlatformAppPathLauncher(profile, extension, file_path); | |
| 362 launcher->LaunchWithRelativePath(current_directory); | |
|
tapted
2014/05/12 00:07:51
You could also do
base::PostTaskAndReplyWithRes
| |
| 353 } | 363 } |
| 354 | 364 |
| 355 void LaunchPlatformAppWithPath(Profile* profile, | 365 void LaunchPlatformAppWithPath(Profile* profile, |
| 356 const Extension* extension, | 366 const Extension* extension, |
| 357 const base::FilePath& file_path) { | 367 const base::FilePath& file_path) { |
| 358 // launcher will be freed when nothing has a reference to it. The message | 368 // launcher will be freed when nothing has a reference to it. The message |
| 359 // queue will retain a reference for any outstanding task, so when the | 369 // queue will retain a reference for any outstanding task, so when the |
| 360 // launcher has finished it will be freed. | 370 // launcher has finished it will be freed. |
| 361 scoped_refptr<PlatformAppPathLauncher> launcher = | 371 scoped_refptr<PlatformAppPathLauncher> launcher = |
| 362 new PlatformAppPathLauncher(profile, extension, file_path); | 372 new PlatformAppPathLauncher(profile, extension, file_path); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 void LaunchPlatformAppWithUrl(Profile* profile, | 415 void LaunchPlatformAppWithUrl(Profile* profile, |
| 406 const Extension* extension, | 416 const Extension* extension, |
| 407 const std::string& handler_id, | 417 const std::string& handler_id, |
| 408 const GURL& url, | 418 const GURL& url, |
| 409 const GURL& referrer_url) { | 419 const GURL& referrer_url) { |
| 410 AppEventRouter::DispatchOnLaunchedEventWithUrl( | 420 AppEventRouter::DispatchOnLaunchedEventWithUrl( |
| 411 profile, extension, handler_id, url, referrer_url); | 421 profile, extension, handler_id, url, referrer_url); |
| 412 } | 422 } |
| 413 | 423 |
| 414 } // namespace apps | 424 } // namespace apps |
| OLD | NEW |