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