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/chromeos/extensions/file_manager/file_browser_private_a
pi.h" | 5 #include "chrome/browser/chromeos/extensions/file_manager/file_browser_private_a
pi.h" |
6 | 6 |
7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
8 #include <sys/statvfs.h> | 8 #include <sys/statvfs.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 #include <utime.h> | 10 #include <utime.h> |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 // Returns a task id for the web app with |app_id|. | 447 // Returns a task id for the web app with |app_id|. |
448 std::string MakeWebAppTaskId(const std::string& app_id) { | 448 std::string MakeWebAppTaskId(const std::string& app_id) { |
449 // TODO(gspencer): For now, the action id is always "open-with", but we | 449 // TODO(gspencer): For now, the action id is always "open-with", but we |
450 // could add any actions that the drive app supports. | 450 // could add any actions that the drive app supports. |
451 return file_handler_util::MakeTaskID( | 451 return file_handler_util::MakeTaskID( |
452 app_id, file_handler_util::kTaskDrive, "open-with"); | 452 app_id, file_handler_util::kTaskDrive, "open-with"); |
453 } | 453 } |
454 | 454 |
455 } // namespace | 455 } // namespace |
456 | 456 |
457 class RequestLocalFileSystemFunction::LocalFileSystemCallbackDispatcher { | |
458 public: | |
459 static fileapi::FileSystemContext::OpenFileSystemCallback CreateCallback( | |
460 RequestLocalFileSystemFunction* function, | |
461 scoped_refptr<fileapi::FileSystemContext> file_system_context, | |
462 int child_id, | |
463 scoped_refptr<const Extension> extension) { | |
464 return base::Bind( | |
465 &LocalFileSystemCallbackDispatcher::DidOpenFileSystem, | |
466 base::Owned(new LocalFileSystemCallbackDispatcher( | |
467 function, file_system_context, child_id, extension))); | |
468 } | |
469 | |
470 void DidOpenFileSystem(base::PlatformFileError result, | |
471 const std::string& name, | |
472 const GURL& root_path) { | |
473 if (result != base::PLATFORM_FILE_OK) { | |
474 DidFail(result); | |
475 return; | |
476 } | |
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
478 // Set up file permission access. | |
479 if (!SetupFileSystemAccessPermissions()) { | |
480 DidFail(base::PLATFORM_FILE_ERROR_SECURITY); | |
481 return; | |
482 } | |
483 | |
484 BrowserThread::PostTask( | |
485 BrowserThread::UI, FROM_HERE, | |
486 base::Bind( | |
487 &RequestLocalFileSystemFunction::RespondSuccessOnUIThread, | |
488 function_, | |
489 name, | |
490 root_path)); | |
491 } | |
492 | |
493 void DidFail(base::PlatformFileError error_code) { | |
494 BrowserThread::PostTask( | |
495 BrowserThread::UI, FROM_HERE, | |
496 base::Bind( | |
497 &RequestLocalFileSystemFunction::RespondFailedOnUIThread, | |
498 function_, | |
499 error_code)); | |
500 } | |
501 | |
502 private: | |
503 LocalFileSystemCallbackDispatcher( | |
504 RequestLocalFileSystemFunction* function, | |
505 scoped_refptr<fileapi::FileSystemContext> file_system_context, | |
506 int child_id, | |
507 scoped_refptr<const Extension> extension) | |
508 : function_(function), | |
509 file_system_context_(file_system_context), | |
510 child_id_(child_id), | |
511 extension_(extension) { | |
512 DCHECK(function_); | |
513 } | |
514 | |
515 // Grants file system access permissions to file browser component. | |
516 bool SetupFileSystemAccessPermissions() { | |
517 if (!extension_.get()) | |
518 return false; | |
519 | |
520 // Make sure that only component extension can access the entire | |
521 // local file system. | |
522 if (extension_->location() != extensions::Manifest::COMPONENT) { | |
523 NOTREACHED() << "Private method access by non-component extension " | |
524 << extension_->id(); | |
525 return false; | |
526 } | |
527 | |
528 fileapi::ExternalFileSystemMountPointProvider* provider = | |
529 file_system_context_->external_provider(); | |
530 if (!provider) | |
531 return false; | |
532 | |
533 // Grant full access to File API from this component extension. | |
534 provider->GrantFullAccessToExtension(extension_->id()); | |
535 | |
536 // Grant R/W file permissions to the renderer hosting component | |
537 // extension for all paths exposed by our local file system provider. | |
538 std::vector<base::FilePath> root_dirs = provider->GetRootDirectories(); | |
539 for (size_t i = 0; i < root_dirs.size(); ++i) { | |
540 ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( | |
541 child_id_, root_dirs[i], | |
542 file_handler_util::GetReadWritePermissions()); | |
543 } | |
544 return true; | |
545 } | |
546 | |
547 RequestLocalFileSystemFunction* function_; | |
548 scoped_refptr<fileapi::FileSystemContext> file_system_context_; | |
549 // Renderer process id. | |
550 int child_id_; | |
551 // Extension source URL. | |
552 scoped_refptr<const Extension> extension_; | |
553 DISALLOW_COPY_AND_ASSIGN(LocalFileSystemCallbackDispatcher); | |
554 }; | |
555 | |
556 FileBrowserPrivateAPI::FileBrowserPrivateAPI(Profile* profile) | 457 FileBrowserPrivateAPI::FileBrowserPrivateAPI(Profile* profile) |
557 : event_router_(new FileManagerEventRouter(profile)) { | 458 : event_router_(new FileManagerEventRouter(profile)) { |
558 (new FileBrowserHandlerParser)->Register(); | 459 (new FileBrowserHandlerParser)->Register(); |
559 | 460 |
560 ExtensionFunctionRegistry* registry = | 461 ExtensionFunctionRegistry* registry = |
561 ExtensionFunctionRegistry::GetInstance(); | 462 ExtensionFunctionRegistry::GetInstance(); |
562 registry->RegisterFunction<LogoutUserFunction>(); | 463 registry->RegisterFunction<LogoutUserFunction>(); |
563 registry->RegisterFunction<CancelFileDialogFunction>(); | 464 registry->RegisterFunction<CancelFileDialogFunction>(); |
564 registry->RegisterFunction<ExecuteTasksFileBrowserFunction>(); | 465 registry->RegisterFunction<ExecuteTasksFileBrowserFunction>(); |
565 registry->RegisterFunction<SetDefaultTaskFileBrowserFunction>(); | 466 registry->RegisterFunction<SetDefaultTaskFileBrowserFunction>(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 | 504 |
604 void FileBrowserPrivateAPI::Shutdown() { | 505 void FileBrowserPrivateAPI::Shutdown() { |
605 event_router_->Shutdown(); | 506 event_router_->Shutdown(); |
606 } | 507 } |
607 | 508 |
608 // static | 509 // static |
609 FileBrowserPrivateAPI* FileBrowserPrivateAPI::Get(Profile* profile) { | 510 FileBrowserPrivateAPI* FileBrowserPrivateAPI::Get(Profile* profile) { |
610 return FileBrowserPrivateAPIFactory::GetForProfile(profile); | 511 return FileBrowserPrivateAPIFactory::GetForProfile(profile); |
611 } | 512 } |
612 | 513 |
613 void RequestLocalFileSystemFunction::RequestOnFileThread( | |
614 scoped_refptr<fileapi::FileSystemContext> file_system_context, | |
615 const GURL& source_url, | |
616 int child_id) { | |
617 GURL origin_url = source_url.GetOrigin(); | |
618 file_system_context->OpenFileSystem( | |
619 origin_url, fileapi::kFileSystemTypeExternal, | |
620 fileapi::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
621 LocalFileSystemCallbackDispatcher::CreateCallback( | |
622 this, | |
623 file_system_context, | |
624 child_id, | |
625 GetExtension())); | |
626 } | |
627 | |
628 bool LogoutUserFunction::RunImpl() { | 514 bool LogoutUserFunction::RunImpl() { |
629 chrome::AttemptUserExit(); | 515 chrome::AttemptUserExit(); |
630 return true; | 516 return true; |
631 } | 517 } |
632 | 518 |
| 519 void RequestLocalFileSystemFunction::RequestOnFileThread( |
| 520 scoped_refptr<fileapi::FileSystemContext> file_system_context, |
| 521 const GURL& source_url, |
| 522 int child_id) { |
| 523 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 524 |
| 525 GURL origin_url = source_url.GetOrigin(); |
| 526 file_system_context->OpenFileSystem( |
| 527 origin_url, |
| 528 fileapi::kFileSystemTypeExternal, |
| 529 fileapi::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, |
| 530 base::Bind(&RequestLocalFileSystemFunction::DidOpenFileSystem, |
| 531 this, |
| 532 file_system_context, |
| 533 child_id, |
| 534 GetExtension())); |
| 535 } |
| 536 |
| 537 void RequestLocalFileSystemFunction::DidOpenFileSystem( |
| 538 scoped_refptr<fileapi::FileSystemContext> file_system_context, |
| 539 int child_id, |
| 540 scoped_refptr<const extensions::Extension> extension, |
| 541 base::PlatformFileError result, |
| 542 const std::string& name, |
| 543 const GURL& root_path) { |
| 544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 545 |
| 546 if (result != base::PLATFORM_FILE_OK) { |
| 547 DidFail(result); |
| 548 return; |
| 549 } |
| 550 // Set up file permission access. |
| 551 if (!SetupFileSystemAccessPermissions(file_system_context, |
| 552 child_id, |
| 553 extension)) { |
| 554 DidFail(base::PLATFORM_FILE_ERROR_SECURITY); |
| 555 return; |
| 556 } |
| 557 |
| 558 BrowserThread::PostTask( |
| 559 BrowserThread::UI, FROM_HERE, |
| 560 base::Bind( |
| 561 &RequestLocalFileSystemFunction::RespondSuccessOnUIThread, |
| 562 this, |
| 563 name, |
| 564 root_path)); |
| 565 } |
| 566 |
| 567 void RequestLocalFileSystemFunction::DidFail( |
| 568 base::PlatformFileError error_code) { |
| 569 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 570 |
| 571 BrowserThread::PostTask( |
| 572 BrowserThread::UI, FROM_HERE, |
| 573 base::Bind( |
| 574 &RequestLocalFileSystemFunction::RespondFailedOnUIThread, |
| 575 this, |
| 576 error_code)); |
| 577 } |
| 578 |
| 579 bool RequestLocalFileSystemFunction::SetupFileSystemAccessPermissions( |
| 580 scoped_refptr<fileapi::FileSystemContext> file_system_context, |
| 581 int child_id, |
| 582 scoped_refptr<const extensions::Extension> extension) { |
| 583 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 584 |
| 585 if (!extension.get()) |
| 586 return false; |
| 587 |
| 588 // Make sure that only component extension can access the entire |
| 589 // local file system. |
| 590 if (extension_->location() != extensions::Manifest::COMPONENT) { |
| 591 NOTREACHED() << "Private method access by non-component extension " |
| 592 << extension->id(); |
| 593 return false; |
| 594 } |
| 595 |
| 596 fileapi::ExternalFileSystemMountPointProvider* provider = |
| 597 file_system_context->external_provider(); |
| 598 if (!provider) |
| 599 return false; |
| 600 |
| 601 // Grant full access to File API from this component extension. |
| 602 provider->GrantFullAccessToExtension(extension_->id()); |
| 603 |
| 604 // Grant R/W file permissions to the renderer hosting component |
| 605 // extension for all paths exposed by our local file system provider. |
| 606 std::vector<base::FilePath> root_dirs = provider->GetRootDirectories(); |
| 607 for (size_t i = 0; i < root_dirs.size(); ++i) { |
| 608 ChildProcessSecurityPolicy::GetInstance()->GrantPermissionsForFile( |
| 609 child_id, root_dirs[i], |
| 610 file_handler_util::GetReadWritePermissions()); |
| 611 } |
| 612 return true; |
| 613 } |
| 614 |
633 bool RequestLocalFileSystemFunction::RunImpl() { | 615 bool RequestLocalFileSystemFunction::RunImpl() { |
634 if (!dispatcher() || !render_view_host() || !render_view_host()->GetProcess()) | 616 if (!dispatcher() || !render_view_host() || !render_view_host()->GetProcess()) |
635 return false; | 617 return false; |
636 | 618 |
637 set_log_on_completion(true); | 619 set_log_on_completion(true); |
638 | 620 |
639 content::SiteInstance* site_instance = render_view_host()->GetSiteInstance(); | 621 content::SiteInstance* site_instance = render_view_host()->GetSiteInstance(); |
640 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 622 scoped_refptr<fileapi::FileSystemContext> file_system_context = |
641 BrowserContext::GetStoragePartition(profile_, site_instance)-> | 623 BrowserContext::GetStoragePartition(profile_, site_instance)-> |
642 GetFileSystemContext(); | 624 GetFileSystemContext(); |
(...skipping 2544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3187 OpenNewWindowFunction::OpenNewWindowFunction() {} | 3169 OpenNewWindowFunction::OpenNewWindowFunction() {} |
3188 | 3170 |
3189 OpenNewWindowFunction::~OpenNewWindowFunction() {} | 3171 OpenNewWindowFunction::~OpenNewWindowFunction() {} |
3190 | 3172 |
3191 bool OpenNewWindowFunction::RunImpl() { | 3173 bool OpenNewWindowFunction::RunImpl() { |
3192 std::string url; | 3174 std::string url; |
3193 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 3175 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
3194 file_manager_util::OpenNewWindow(profile_, GURL(url)); | 3176 file_manager_util::OpenNewWindow(profile_, GURL(url)); |
3195 return true; | 3177 return true; |
3196 } | 3178 } |
OLD | NEW |