| 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" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.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" | 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" |
| 16 #include "chrome/browser/extensions/extension_host.h" | 17 #include "chrome/browser/extensions/extension_host.h" |
| 18 #include "chrome/browser/extensions/extension_prefs.h" |
| 17 #include "chrome/browser/extensions/extension_process_manager.h" | 19 #include "chrome/browser/extensions/extension_process_manager.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
| 19 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 22 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
| 22 #include "chrome/common/extensions/extension_messages.h" | 25 #include "chrome/common/extensions/extension_messages.h" |
| 23 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/child_process_security_policy.h" | 27 #include "content/public/browser/child_process_security_policy.h" |
| 25 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
| 26 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 27 #include "net/base/mime_util.h" | 30 #include "net/base/mime_util.h" |
| 28 #include "net/base/net_util.h" | 31 #include "net/base/net_util.h" |
| 29 #include "webkit/fileapi/file_system_types.h" | 32 #include "webkit/fileapi/file_system_types.h" |
| 30 #include "webkit/fileapi/isolated_context.h" | 33 #include "webkit/fileapi/isolated_context.h" |
| 31 | 34 |
| 32 using content::BrowserThread; | 35 using content::BrowserThread; |
| 33 using extensions::app_file_handler_util::FileHandlerForId; | 36 using extensions::app_file_handler_util::FileHandlerForId; |
| 34 using extensions::app_file_handler_util::FileHandlerCanHandleFileWithMimeType; | 37 using extensions::app_file_handler_util::FileHandlerCanHandleFileWithMimeType; |
| 35 using extensions::app_file_handler_util::FirstFileHandlerForMimeType; | 38 using extensions::app_file_handler_util::FirstFileHandlerForMimeType; |
| 39 using extensions::app_file_handler_util::CreateFileEntry; |
| 40 using extensions::app_file_handler_util::GrantedFileEntry; |
| 41 using extensions::app_file_handler_util::SavedFileEntry; |
| 36 | 42 |
| 37 namespace extensions { | 43 namespace extensions { |
| 38 | 44 |
| 39 namespace { | 45 namespace { |
| 40 | 46 |
| 41 bool MakePathAbsolute(const base::FilePath& current_directory, | 47 bool MakePathAbsolute(const base::FilePath& current_directory, |
| 42 base::FilePath* file_path) { | 48 base::FilePath* file_path) { |
| 43 DCHECK(file_path); | 49 DCHECK(file_path); |
| 44 if (file_path->IsAbsolute()) | 50 if (file_path->IsAbsolute()) |
| 45 return true; | 51 return true; |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 // The extension providing the app. | 238 // The extension providing the app. |
| 233 const Extension* extension_; | 239 const Extension* extension_; |
| 234 // The path to be passed through to the app. | 240 // The path to be passed through to the app. |
| 235 const base::FilePath file_path_; | 241 const base::FilePath file_path_; |
| 236 // The ID of the file handler used to launch the app. | 242 // The ID of the file handler used to launch the app. |
| 237 std::string handler_id_; | 243 std::string handler_id_; |
| 238 | 244 |
| 239 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); | 245 DISALLOW_COPY_AND_ASSIGN(PlatformAppPathLauncher); |
| 240 }; | 246 }; |
| 241 | 247 |
| 248 class SavedFileEntryLauncher |
| 249 : public base::RefCountedThreadSafe<SavedFileEntryLauncher> { |
| 250 public: |
| 251 SavedFileEntryLauncher( |
| 252 Profile* profile, |
| 253 const Extension* extension, |
| 254 const std::vector<SavedFileEntry>& file_entries) |
| 255 : profile_(profile), |
| 256 extension_(extension), |
| 257 file_entries_(file_entries) {} |
| 258 |
| 259 void Launch() { |
| 260 // Access needs to be granted to the file or filesystem for the process |
| 261 // associated with the extension. To do this the ExtensionHost is needed. |
| 262 // This might not be available, or it might be in the process of being |
| 263 // unloaded, in which case the lazy background task queue is used to load |
| 264 // he extension and then call back to us. |
| 265 extensions::LazyBackgroundTaskQueue* queue = |
| 266 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); |
| 267 if (queue->ShouldEnqueueTask(profile_, extension_)) { |
| 268 queue->AddPendingTask(profile_, extension_->id(), base::Bind( |
| 269 &SavedFileEntryLauncher::GrantAccessToFilesAndLaunch, |
| 270 this)); |
| 271 return; |
| 272 } |
| 273 ExtensionProcessManager* process_manager = |
| 274 ExtensionSystem::Get(profile_)->process_manager(); |
| 275 extensions::ExtensionHost* host = |
| 276 process_manager->GetBackgroundHostForExtension(extension_->id()); |
| 277 DCHECK(host); |
| 278 GrantAccessToFilesAndLaunch(host); |
| 279 } |
| 280 |
| 281 private: |
| 282 friend class base::RefCountedThreadSafe<SavedFileEntryLauncher>; |
| 283 ~SavedFileEntryLauncher() {} |
| 284 void GrantAccessToFilesAndLaunch(ExtensionHost* host) { |
| 285 int renderer_id = host->render_process_host()->GetID(); |
| 286 std::vector<GrantedFileEntry> granted_file_entries; |
| 287 for (std::vector<SavedFileEntry>::const_iterator it = |
| 288 file_entries_.begin(); it != file_entries_.end(); ++it) { |
| 289 GrantedFileEntry file_entry = CreateFileEntry( |
| 290 profile_, extension_->id(), renderer_id, it->path, it->writable); |
| 291 file_entry.id = it->id; |
| 292 granted_file_entries.push_back(file_entry); |
| 293 |
| 294 // Record that we have granted this file permission. |
| 295 ExtensionPrefs* extension_prefs = ExtensionSystem::Get(profile_)-> |
| 296 extension_service()->extension_prefs(); |
| 297 extension_prefs->AddSavedFileEntry( |
| 298 host->extension()->id(), it->id, it->path, it->writable); |
| 299 } |
| 300 extensions::AppEventRouter::DispatchOnRestartedEvent( |
| 301 profile_, extension_, granted_file_entries); |
| 302 } |
| 303 |
| 304 // The profile the app should be run in. |
| 305 Profile* profile_; |
| 306 // The extension providing the app. |
| 307 const Extension* extension_; |
| 308 |
| 309 std::vector<SavedFileEntry> file_entries_; |
| 310 }; |
| 311 |
| 242 } // namespace | 312 } // namespace |
| 243 | 313 |
| 244 void LaunchPlatformApp(Profile* profile, | 314 void LaunchPlatformApp(Profile* profile, |
| 245 const Extension* extension, | 315 const Extension* extension, |
| 246 const CommandLine* command_line, | 316 const CommandLine* command_line, |
| 247 const base::FilePath& current_directory) { | 317 const base::FilePath& current_directory) { |
| 248 base::FilePath path; | 318 base::FilePath path; |
| 249 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { | 319 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { |
| 250 LaunchPlatformAppWithNoData(profile, extension); | 320 LaunchPlatformAppWithNoData(profile, extension); |
| 251 return; | 321 return; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 268 | 338 |
| 269 void LaunchPlatformAppWithFileHandler(Profile* profile, | 339 void LaunchPlatformAppWithFileHandler(Profile* profile, |
| 270 const Extension* extension, | 340 const Extension* extension, |
| 271 const std::string& handler_id, | 341 const std::string& handler_id, |
| 272 const base::FilePath& file_path) { | 342 const base::FilePath& file_path) { |
| 273 scoped_refptr<PlatformAppPathLauncher> launcher = | 343 scoped_refptr<PlatformAppPathLauncher> launcher = |
| 274 new PlatformAppPathLauncher(profile, extension, file_path); | 344 new PlatformAppPathLauncher(profile, extension, file_path); |
| 275 launcher->LaunchWithHandler(handler_id); | 345 launcher->LaunchWithHandler(handler_id); |
| 276 } | 346 } |
| 277 | 347 |
| 348 void RestartPlatformAppWithFileEntries( |
| 349 Profile* profile, |
| 350 const Extension* extension, |
| 351 const std::vector<SavedFileEntry>& file_entries) { |
| 352 scoped_refptr<SavedFileEntryLauncher> launcher = new SavedFileEntryLauncher( |
| 353 profile, extension, file_entries); |
| 354 launcher->Launch(); |
| 355 } |
| 356 |
| 278 } // namespace extensions | 357 } // namespace extensions |
| OLD | NEW |