Chromium Code Reviews| 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 void GrantAccessToFilesAndLaunch(ExtensionHost* host) { | |
| 283 int renderer_id = host->render_process_host()->GetID(); | |
| 284 std::vector<GrantedFileEntry> granted_file_entries; | |
| 285 for (std::vector<SavedFileEntry>::const_iterator it = | |
| 286 file_entries_.begin(); it != file_entries_.end(); ++it) { | |
| 287 GrantedFileEntry file_entry = CreateFileEntry( | |
| 288 profile_, extension_->id(), renderer_id, it->path, it->writable); | |
| 289 file_entry.id = it->id; | |
| 290 granted_file_entries.push_back(file_entry); | |
| 291 | |
| 292 // Record that we have granted this file permission. | |
| 293 ExtensionPrefs* extension_prefs = ExtensionSystem::Get(profile_)-> | |
| 294 extension_service()->extension_prefs(); | |
| 295 extension_prefs->AddSavedFileEntry( | |
| 296 host->extension()->id(), it->id, it->path, it->writable); | |
| 297 } | |
| 298 extensions::AppEventRouter::DispatchOnRestartedEvent( | |
| 299 profile_, extension_, granted_file_entries); | |
| 300 } | |
| 301 | |
| 302 // The profile the app should be run in. | |
| 303 Profile* profile_; | |
| 304 // The extension providing the app. | |
| 305 const Extension* extension_; | |
| 306 | |
| 307 std::vector<SavedFileEntry> file_entries_; | |
| 308 }; | |
| 309 | |
| 310 /* | |
| 311 // Class to handle launching of platform apps with WebIntent data. | |
| 312 // An instance of this class is created for each launch. The lifetime of these | |
| 313 // instances is managed by reference counted pointers. As long as an instance | |
| 314 // has outstanding tasks on a message queue it will be retained; once all | |
| 315 // outstanding tasks are completed it will be deleted. | |
| 316 class PlatformAppWebIntentLauncher | |
|
benwells
2013/03/13 03:58:29
This guy has been deleted.
koz (OOO until 15th September)
2013/03/13 05:01:13
Oops, and commented out in my change, too.
Removed
| |
| 317 : public base::RefCountedThreadSafe<PlatformAppWebIntentLauncher> { | |
| 318 public: | |
| 319 PlatformAppWebIntentLauncher( | |
| 320 Profile* profile, | |
| 321 const Extension* extension, | |
| 322 content::WebIntentsDispatcher* intents_dispatcher, | |
| 323 content::WebContents* source) | |
| 324 : profile_(profile), | |
| 325 extension_(extension), | |
| 326 intents_dispatcher_(intents_dispatcher), | |
| 327 source_(source), | |
| 328 data_(intents_dispatcher->GetIntent()) {} | |
| 329 | |
| 330 void Launch() { | |
| 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 332 if (data_.data_type != webkit_glue::WebIntentData::BLOB && | |
| 333 data_.data_type != webkit_glue::WebIntentData::FILESYSTEM) { | |
| 334 InternalLaunch(); | |
| 335 return; | |
| 336 } | |
| 337 | |
| 338 // Access needs to be granted to the file or filesystem for the process | |
| 339 // associated with the extension. To do this the ExtensionHost is needed. | |
| 340 // This might not be available, or it might be in the process of being | |
| 341 // unloaded, in which case the lazy background task queue is used to load | |
| 342 // he extension and then call back to us. | |
| 343 LazyBackgroundTaskQueue* queue = | |
| 344 ExtensionSystem::Get(profile_)->lazy_background_task_queue(); | |
| 345 if (queue->ShouldEnqueueTask(profile_, extension_)) { | |
| 346 queue->AddPendingTask(profile_, extension_->id(), base::Bind( | |
| 347 &PlatformAppWebIntentLauncher::GrantAccessToFileAndLaunch, | |
| 348 this)); | |
| 349 return; | |
| 350 } | |
| 351 ExtensionProcessManager* process_manager = | |
| 352 ExtensionSystem::Get(profile_)->process_manager(); | |
| 353 ExtensionHost* host = | |
| 354 process_manager->GetBackgroundHostForExtension(extension_->id()); | |
| 355 DCHECK(host); | |
| 356 GrantAccessToFileAndLaunch(host); | |
| 357 } | |
| 358 | |
| 359 private: | |
| 360 friend class base::RefCountedThreadSafe<PlatformAppWebIntentLauncher>; | |
| 361 | |
| 362 virtual ~PlatformAppWebIntentLauncher() {} | |
| 363 | |
| 364 void GrantAccessToFileAndLaunch(ExtensionHost* host) { | |
| 365 // If there was an error loading the app page, |host| will be NULL. | |
| 366 if (!host) { | |
| 367 LOG(ERROR) << "Could not load app page for " << extension_->id(); | |
| 368 return; | |
| 369 } | |
| 370 | |
| 371 content::ChildProcessSecurityPolicy* policy = | |
| 372 content::ChildProcessSecurityPolicy::GetInstance(); | |
| 373 int renderer_id = host->render_process_host()->GetID(); | |
| 374 | |
| 375 if (data_.data_type == webkit_glue::WebIntentData::BLOB) { | |
| 376 // Granting read file permission to allow reading file content. | |
| 377 // If the renderer already has permission to read these paths, it is not | |
| 378 // regranted, as this would overwrite any other permissions which the | |
| 379 // renderer may already have. | |
| 380 if (!policy->CanReadFile(renderer_id, data_.blob_file)) | |
| 381 policy->GrantReadFile(renderer_id, data_.blob_file); | |
| 382 } else if (data_.data_type == webkit_glue::WebIntentData::FILESYSTEM) { | |
| 383 // Grant read filesystem and read directory permission to allow reading | |
| 384 // any part of the specified filesystem. | |
| 385 FilePath path; | |
| 386 const bool valid = | |
| 387 fileapi::IsolatedContext::GetInstance()->GetRegisteredPath( | |
| 388 data_.filesystem_id, &path); | |
| 389 DCHECK(valid); | |
| 390 if (!policy->CanReadFile(renderer_id, path)) | |
| 391 policy->GrantReadFile(renderer_id, path); | |
| 392 policy->GrantReadFileSystem(renderer_id, data_.filesystem_id); | |
| 393 } else { | |
| 394 NOTREACHED(); | |
| 395 } | |
| 396 InternalLaunch(); | |
| 397 } | |
| 398 | |
| 399 void InternalLaunch() { | |
| 400 AppEventRouter::DispatchOnLaunchedEventWithWebIntent( | |
| 401 profile_, extension_, intents_dispatcher_, source_); | |
| 402 } | |
| 403 | |
| 404 // The profile the app should be run in. | |
| 405 Profile* profile_; | |
| 406 // The extension providing the app. | |
| 407 const Extension* extension_; | |
| 408 // The dispatcher so that platform apps can respond to this intent. | |
| 409 content::WebIntentsDispatcher* intents_dispatcher_; | |
| 410 // The source of this intent. | |
| 411 content::WebContents* source_; | |
| 412 // The WebIntent data from the dispatcher. | |
| 413 const webkit_glue::WebIntentData data_; | |
| 414 | |
| 415 DISALLOW_COPY_AND_ASSIGN(PlatformAppWebIntentLauncher); | |
| 416 }; | |
| 417 */ | |
| 418 | |
| 242 } // namespace | 419 } // namespace |
| 243 | 420 |
| 244 void LaunchPlatformApp(Profile* profile, | 421 void LaunchPlatformApp(Profile* profile, |
| 245 const Extension* extension, | 422 const Extension* extension, |
| 246 const CommandLine* command_line, | 423 const CommandLine* command_line, |
| 247 const base::FilePath& current_directory) { | 424 const base::FilePath& current_directory) { |
| 248 base::FilePath path; | 425 base::FilePath path; |
| 249 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { | 426 if (!GetAbsolutePathFromCommandLine(command_line, current_directory, &path)) { |
| 250 LaunchPlatformAppWithNoData(profile, extension); | 427 LaunchPlatformAppWithNoData(profile, extension); |
| 251 return; | 428 return; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 268 | 445 |
| 269 void LaunchPlatformAppWithFileHandler(Profile* profile, | 446 void LaunchPlatformAppWithFileHandler(Profile* profile, |
| 270 const Extension* extension, | 447 const Extension* extension, |
| 271 const std::string& handler_id, | 448 const std::string& handler_id, |
| 272 const base::FilePath& file_path) { | 449 const base::FilePath& file_path) { |
| 273 scoped_refptr<PlatformAppPathLauncher> launcher = | 450 scoped_refptr<PlatformAppPathLauncher> launcher = |
| 274 new PlatformAppPathLauncher(profile, extension, file_path); | 451 new PlatformAppPathLauncher(profile, extension, file_path); |
| 275 launcher->LaunchWithHandler(handler_id); | 452 launcher->LaunchWithHandler(handler_id); |
| 276 } | 453 } |
| 277 | 454 |
| 455 void RestartPlatformAppWithFileEntries( | |
| 456 Profile* profile, | |
| 457 const Extension* extension, | |
| 458 const std::vector<SavedFileEntry>& file_entries) { | |
| 459 scoped_refptr<SavedFileEntryLauncher> launcher = new SavedFileEntryLauncher( | |
| 460 profile, extension, file_entries); | |
| 461 launcher->Launch(); | |
| 462 } | |
| 463 | |
| 278 } // namespace extensions | 464 } // namespace extensions |
| OLD | NEW |