| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/platform_app_launcher.h" | 5 #include "chrome/browser/extensions/platform_app_launcher.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
| 44 #include "win8/util/win8_util.h" | 44 #include "win8/util/win8_util.h" |
| 45 #endif | 45 #endif |
| 46 | 46 |
| 47 using content::BrowserThread; | 47 using content::BrowserThread; |
| 48 using extensions::app_file_handler_util::FileHandlerForId; | 48 using extensions::app_file_handler_util::FileHandlerForId; |
| 49 using extensions::app_file_handler_util::FileHandlerCanHandleFile; | 49 using extensions::app_file_handler_util::FileHandlerCanHandleFile; |
| 50 using extensions::app_file_handler_util::FirstFileHandlerForFile; | 50 using extensions::app_file_handler_util::FirstFileHandlerForFile; |
| 51 using extensions::app_file_handler_util::CreateFileEntry; | 51 using extensions::app_file_handler_util::CreateFileEntry; |
| 52 using extensions::app_file_handler_util::GrantedFileEntry; | 52 using extensions::app_file_handler_util::GrantedFileEntry; |
| 53 using extensions::app_file_handler_util::SavedFileEntry; | |
| 54 | 53 |
| 55 namespace extensions { | 54 namespace extensions { |
| 56 | 55 |
| 57 namespace { | 56 namespace { |
| 58 | 57 |
| 59 const char kFallbackMimeType[] = "application/octet-stream"; | 58 const char kFallbackMimeType[] = "application/octet-stream"; |
| 60 | 59 |
| 61 bool MakePathAbsolute(const base::FilePath& current_directory, | 60 bool MakePathAbsolute(const base::FilePath& current_directory, |
| 62 base::FilePath* file_path) { | 61 base::FilePath* file_path) { |
| 63 DCHECK(file_path); | 62 DCHECK(file_path); |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // The extension providing the app. | 271 // The extension providing the app. |
| 273 const Extension* extension_; | 272 const Extension* extension_; |
| 274 // The path to be passed through to the app. | 273 // The path to be passed through to the app. |
| 275 const base::FilePath file_path_; | 274 const base::FilePath file_path_; |
| 276 // The ID of the file handler used to launch the app. | 275 // The ID of the file handler used to launch the app. |
| 277 std::string handler_id_; | 276 std::string handler_id_; |
| 278 | 277 |
| 279 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); | 278 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
| 280 }; | 279 }; |
| 281 | 280 |
| 282 class SavedFileEntryLauncher | |
| 283 : public base::RefCountedThreadSafe<SavedFileEntryLauncher> { | |
| 284 public: | |
| 285 SavedFileEntryLauncher( | |
| 286 Profile* profile, | |
| 287 const Extension* extension, | |
| 288 const std::vector<SavedFileEntry>& file_entries) | |
| 289 : profile_(profile), | |
| 290 extension_(extension), | |
| 291 file_entries_(file_entries) {} | |
| 292 | |
| 293 void Launch() { | |
| 294 // Access needs to be granted to the file or filesystem for the process | |
| 295 // associated with the extension. To do this the ExtensionHost is needed. | |
| 296 // This might not be available, or it might be in the process of being | |
| 297 // unloaded, in which case the lazy background task queue is used to load | |
| 298 // he extension and then call back to us. | |
| 299 extensions::LazyBackgroundTaskQueue* queue = | |
| 300 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); | |
| 301 if (queue->ShouldEnqueueTask(profile_, extension_)) { | |
| 302 queue->AddPendingTask(profile_, extension_->id(), base::Bind( | |
| 303 &SavedFileEntryLauncher::GrantAccessToFilesAndLaunch, | |
| 304 this)); | |
| 305 return; | |
| 306 } | |
| 307 ExtensionProcessManager* process_manager = | |
| 308 ExtensionSystem::Get(profile_)->process_manager(); | |
| 309 extensions::ExtensionHost* host = | |
| 310 process_manager->GetBackgroundHostForExtension(extension_->id()); | |
| 311 DCHECK(host); | |
| 312 GrantAccessToFilesAndLaunch(host); | |
| 313 } | |
| 314 | |
| 315 private: | |
| 316 friend class base::RefCountedThreadSafe<SavedFileEntryLauncher>; | |
| 317 ~SavedFileEntryLauncher() {} | |
| 318 | |
| 319 void GrantAccessToFilesAndLaunch(ExtensionHost* host) { | |
| 320 // If there was an error loading the app page, |host| will be NULL. | |
| 321 if (!host) { | |
| 322 LOG(ERROR) << "Could not load app page for " << extension_->id(); | |
| 323 return; | |
| 324 } | |
| 325 | |
| 326 int renderer_id = host->render_process_host()->GetID(); | |
| 327 std::vector<GrantedFileEntry> granted_file_entries; | |
| 328 for (std::vector<SavedFileEntry>::const_iterator it = | |
| 329 file_entries_.begin(); it != file_entries_.end(); ++it) { | |
| 330 GrantedFileEntry file_entry = CreateFileEntry( | |
| 331 profile_, extension_->id(), renderer_id, it->path, it->writable); | |
| 332 file_entry.id = it->id; | |
| 333 granted_file_entries.push_back(file_entry); | |
| 334 | |
| 335 // Record that we have granted this file permission. | |
| 336 app_file_handler_util::AddSavedFileEntry( | |
| 337 ExtensionPrefs::Get(profile_), | |
| 338 host->extension()->id(), | |
| 339 it->id, | |
| 340 it->path, | |
| 341 it->writable); | |
| 342 } | |
| 343 extensions::AppEventRouter::DispatchOnRestartedEvent( | |
| 344 profile_, extension_, granted_file_entries); | |
| 345 } | |
| 346 | |
| 347 // The profile the app should be run in. | |
| 348 Profile* profile_; | |
| 349 // The extension providing the app. | |
| 350 const Extension* extension_; | |
| 351 | |
| 352 std::vector<SavedFileEntry> file_entries_; | |
| 353 }; | |
| 354 | |
| 355 } // namespace | 281 } // namespace |
| 356 | 282 |
| 357 void LaunchPlatformApp(Profile* profile, | 283 void LaunchPlatformApp(Profile* profile, |
| 358 const Extension* extension, | 284 const Extension* extension, |
| 359 const CommandLine* command_line, | 285 const CommandLine* command_line, |
| 360 const base::FilePath& current_directory) { | 286 const base::FilePath& current_directory) { |
| 361 #if defined(OS_WIN) | 287 #if defined(OS_WIN) |
| 362 // On Windows 8's single window Metro mode we can not launch platform apps. | 288 // On Windows 8's single window Metro mode we can not launch platform apps. |
| 363 // Offer to switch Chrome to desktop mode. | 289 // Offer to switch Chrome to desktop mode. |
| 364 if (win8::IsSingleWindowMetroMode()) { | 290 if (win8::IsSingleWindowMetroMode()) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 393 | 319 |
| 394 void LaunchPlatformAppWithFileHandler(Profile* profile, | 320 void LaunchPlatformAppWithFileHandler(Profile* profile, |
| 395 const Extension* extension, | 321 const Extension* extension, |
| 396 const std::string& handler_id, | 322 const std::string& handler_id, |
| 397 const base::FilePath& file_path) { | 323 const base::FilePath& file_path) { |
| 398 scoped_refptr<PlatformAppPathLauncher> launcher = | 324 scoped_refptr<PlatformAppPathLauncher> launcher = |
| 399 new PlatformAppPathLauncher(profile, extension, file_path); | 325 new PlatformAppPathLauncher(profile, extension, file_path); |
| 400 launcher->LaunchWithHandler(handler_id); | 326 launcher->LaunchWithHandler(handler_id); |
| 401 } | 327 } |
| 402 | 328 |
| 403 void RestartPlatformAppWithFileEntries( | 329 void RestartPlatformApp(Profile* profile, const Extension* extension) { |
| 404 Profile* profile, | |
| 405 const Extension* extension, | |
| 406 const std::vector<SavedFileEntry>& file_entries) { | |
| 407 extensions::EventRouter* event_router = | 330 extensions::EventRouter* event_router = |
| 408 ExtensionSystem::Get(profile)->event_router(); | 331 ExtensionSystem::Get(profile)->event_router(); |
| 409 bool listening_to_restart = event_router-> | 332 bool listening_to_restart = event_router-> |
| 410 ExtensionHasEventListener(extension->id(), event_names::kOnRestarted); | 333 ExtensionHasEventListener(extension->id(), event_names::kOnRestarted); |
| 411 | 334 |
| 412 if (listening_to_restart) { | 335 if (listening_to_restart) { |
| 413 scoped_refptr<SavedFileEntryLauncher> launcher = new SavedFileEntryLauncher( | 336 extensions::AppEventRouter::DispatchOnRestartedEvent(profile, extension); |
| 414 profile, extension, file_entries); | |
| 415 launcher->Launch(); | |
| 416 return; | 337 return; |
| 417 } | 338 } |
| 418 | 339 |
| 419 ExtensionPrefs* extension_prefs = ExtensionSystem::Get(profile)-> | 340 ExtensionPrefs* extension_prefs = ExtensionSystem::Get(profile)-> |
| 420 extension_service()->extension_prefs(); | 341 extension_service()->extension_prefs(); |
| 421 bool had_windows = extension_prefs->HasWindows(extension->id()); | 342 bool had_windows = extension_prefs->HasWindows(extension->id()); |
| 422 extension_prefs->SetHasWindows(extension->id(), false); | 343 extension_prefs->SetHasWindows(extension->id(), false); |
| 423 bool listening_to_launch = event_router-> | 344 bool listening_to_launch = event_router-> |
| 424 ExtensionHasEventListener(extension->id(), event_names::kOnLaunched); | 345 ExtensionHasEventListener(extension->id(), event_names::kOnLaunched); |
| 425 | 346 |
| 426 if (listening_to_launch && had_windows) | 347 if (listening_to_launch && had_windows) |
| 427 LaunchPlatformAppWithNoData(profile, extension); | 348 LaunchPlatformAppWithNoData(profile, extension); |
| 428 } | 349 } |
| 429 | 350 |
| 430 } // namespace extensions | 351 } // namespace extensions |
| OLD | NEW |