Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/chromeos/extensions/file_manager_util.cc

Issue 11377054: Packaged Files app: open files from Downloads tab (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/background.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "chrome/browser/chromeos/extensions/file_manager_util.h" 4 #include "chrome/browser/chromeos/extensions/file_manager_util.h"
5 5
6 #include "ash/shell.h" 6 #include "ash/shell.h"
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h"
8 #include "base/file_util.h" 9 #include "base/file_util.h"
9 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h" 11 #include "base/json/json_writer.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
13 #include "base/path_service.h" 14 #include "base/path_service.h"
14 #include "base/string_util.h" 15 #include "base/string_util.h"
15 #include "base/utf_string_conversions.h" 16 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 17 #include "base/values.h"
17 #include "chrome/browser/chromeos/drive/drive.pb.h" 18 #include "chrome/browser/chromeos/drive/drive.pb.h"
(...skipping 13 matching lines...) Expand all
31 #include "chrome/browser/ui/browser_finder.h" 32 #include "chrome/browser/ui/browser_finder.h"
32 #include "chrome/browser/ui/browser_list.h" 33 #include "chrome/browser/ui/browser_list.h"
33 #include "chrome/browser/ui/browser_tabstrip.h" 34 #include "chrome/browser/ui/browser_tabstrip.h"
34 #include "chrome/browser/ui/browser_window.h" 35 #include "chrome/browser/ui/browser_window.h"
35 #include "chrome/browser/ui/extensions/application_launch.h" 36 #include "chrome/browser/ui/extensions/application_launch.h"
36 #include "chrome/browser/ui/host_desktop.h" 37 #include "chrome/browser/ui/host_desktop.h"
37 #include "chrome/browser/ui/simple_message_box.h" 38 #include "chrome/browser/ui/simple_message_box.h"
38 #include "chrome/browser/ui/tab_contents/tab_contents.h" 39 #include "chrome/browser/ui/tab_contents/tab_contents.h"
39 #include "chrome/browser/ui/tabs/tab_strip_model.h" 40 #include "chrome/browser/ui/tabs/tab_strip_model.h"
40 #include "chrome/common/chrome_paths.h" 41 #include "chrome/common/chrome_paths.h"
42 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/extensions/file_browser_handler.h" 43 #include "chrome/common/extensions/file_browser_handler.h"
42 #include "chrome/common/url_constants.h" 44 #include "chrome/common/url_constants.h"
43 #include "content/public/browser/browser_thread.h" 45 #include "content/public/browser/browser_thread.h"
44 #include "content/public/browser/plugin_service.h" 46 #include "content/public/browser/plugin_service.h"
45 #include "content/public/browser/storage_partition.h" 47 #include "content/public/browser/storage_partition.h"
46 #include "content/public/browser/user_metrics.h" 48 #include "content/public/browser/user_metrics.h"
47 #include "content/public/browser/web_contents.h" 49 #include "content/public/browser/web_contents.h"
48 #include "content/public/common/pepper_plugin_info.h" 50 #include "content/public/common/pepper_plugin_info.h"
49 #include "grit/generated_resources.h" 51 #include "grit/generated_resources.h"
50 #include "net/base/escape.h" 52 #include "net/base/escape.h"
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 } 442 }
441 return true; 443 return true;
442 } 444 }
443 } 445 }
444 } 446 }
445 } 447 }
446 448
447 return false; 449 return false;
448 } 450 }
449 451
452 bool IsFileManagerPackaged() {
453 const CommandLine* command_line = CommandLine::ForCurrentProcess();
454 return command_line->HasSwitch(switches::kFileManagerPackaged);
455 }
456
457 void ExecuteHandler(Profile* profile,
458 std::string extension_id,
459 std::string action_id,
460 const GURL& url) {
461 // We are executing the task on behalf of File Browser extension.
462 const GURL source_url(kBaseFileBrowserUrl);
463
464 // If File Browser has not been open yet then it did not request access
465 // to the file system. Do it now.
466 fileapi::ExternalFileSystemMountPointProvider* external_provider =
467 BrowserContext::GetDefaultStoragePartition(
468 profile)->GetFileSystemContext()->external_provider();
469 if (!external_provider)
470 return;
471 external_provider->GrantFullAccessToExtension(source_url.host());
472
473 std::vector<GURL> urls;
474 urls.push_back(url);
475 scoped_refptr<FileTaskExecutor> executor = FileTaskExecutor::Create(profile,
476 source_url, 0 /* no tab id */, extension_id,
477 file_handler_util::kTaskFile, action_id);
478 executor->Execute(urls);
479 }
480
450 void OpenFileBrowser(const FilePath& path, 481 void OpenFileBrowser(const FilePath& path,
451 TAB_REUSE_MODE mode, 482 TAB_REUSE_MODE mode,
452 const std::string& flag_name) { 483 const std::string& action_id) {
484 content::RecordAction(UserMetricsAction("ShowFileBrowserFullTab"));
485
453 if (FileManageTabExists(path, mode)) 486 if (FileManageTabExists(path, mode))
454 return; 487 return;
455 488
456 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 489 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
457 490
491 if (IsFileManagerPackaged() && !path.value().empty()) {
492 GURL url;
493 if (!ConvertFileToFileSystemUrl(profile, path,
494 GetFileBrowserExtensionUrl().GetOrigin(), &url))
495 return;
496
497 // Some values of |action_id| are not listed in the manifest and are used
498 // to parametrize the behavior when opening the Files app window.
499 ExecuteHandler(profile, kFileBrowserDomain, action_id, url);
500 return;
501 }
502
458 std::string url = chrome::kChromeUIFileManagerURL; 503 std::string url = chrome::kChromeUIFileManagerURL;
459 if (flag_name.size()) { 504 if (action_id.size()) {
460 DictionaryValue arg_value; 505 DictionaryValue arg_value;
461 arg_value.SetBoolean(flag_name, "true"); 506 arg_value.SetString("action", action_id);
462 std::string query; 507 std::string query;
463 base::JSONWriter::Write(&arg_value, &query); 508 base::JSONWriter::Write(&arg_value, &query);
464 url += "?" + net::EscapeUrlEncodedData(query, false); 509 url += "?" + net::EscapeUrlEncodedData(query, false);
465 } 510 }
466 if (!path.empty()) { 511 if (!path.empty()) {
467 FilePath virtual_path; 512 FilePath virtual_path;
468 if (!ConvertFileToRelativeFileSystemPath(profile, path, &virtual_path)) 513 if (!ConvertFileToRelativeFileSystemPath(profile, path, &virtual_path))
469 return; 514 return;
470 url += "#/" + net::EscapeUrlEncodedData(virtual_path.value(), false); 515 url += "#/" + net::EscapeUrlEncodedData(virtual_path.value(), false);
471 } 516 }
472 517
473 ExtensionService* service = profile->GetExtensionService(); 518 ExtensionService* service = profile->GetExtensionService();
474 if (!service) 519 if (!service)
475 return; 520 return;
476 521
477 const extensions::Extension* extension = 522 const extensions::Extension* extension =
478 service->GetExtensionById(kFileBrowserDomain, false); 523 service->GetExtensionById(kFileBrowserDomain, false);
479 if (!extension) 524 if (!extension)
480 return; 525 return;
481 526
482 content::RecordAction(UserMetricsAction("ShowFileBrowserFullTab"));
483 application_launch::LaunchParams params(profile, extension, 527 application_launch::LaunchParams params(profile, extension,
484 extension_misc::LAUNCH_WINDOW, 528 extension_misc::LAUNCH_WINDOW,
485 NEW_FOREGROUND_TAB); 529 NEW_FOREGROUND_TAB);
486 params.override_url = GURL(url); 530 params.override_url = GURL(url);
487 application_launch::OpenApplication(params); 531 application_launch::OpenApplication(params);
488 } 532 }
489 533
490 Browser* GetBrowserForUrl(GURL target_url) { 534 Browser* GetBrowserForUrl(GURL target_url) {
491 for (BrowserList::const_iterator browser_iterator = BrowserList::begin(); 535 for (BrowserList::const_iterator browser_iterator = BrowserList::begin();
492 browser_iterator != BrowserList::end(); ++browser_iterator) { 536 browser_iterator != BrowserList::end(); ++browser_iterator) {
493 Browser* browser = *browser_iterator; 537 Browser* browser = *browser_iterator;
494 TabStripModel* tab_strip = browser->tab_strip_model(); 538 TabStripModel* tab_strip = browser->tab_strip_model();
495 for (int idx = 0; idx < tab_strip->count(); idx++) { 539 for (int idx = 0; idx < tab_strip->count(); idx++) {
496 content::WebContents* web_contents = 540 content::WebContents* web_contents =
497 tab_strip->GetTabContentsAt(idx)->web_contents(); 541 tab_strip->GetTabContentsAt(idx)->web_contents();
498 const GURL& url = web_contents->GetURL(); 542 const GURL& url = web_contents->GetURL();
499 if (url == target_url) 543 if (url == target_url)
500 return browser; 544 return browser;
501 } 545 }
502 } 546 }
503 return NULL; 547 return NULL;
504 } 548 }
505 549
506 void ViewRemovableDrive(const FilePath& path) { 550 void ViewRemovableDrive(const FilePath& path) {
507 OpenFileBrowser(path, REUSE_ANY_FILE_MANAGER, "mountTriggered"); 551 OpenFileBrowser(path, REUSE_ANY_FILE_MANAGER, "auto-open");
508 } 552 }
509 553
510 void OpenActionChoiceDialog(const FilePath& path) { 554 void OpenActionChoiceDialog(const FilePath& path) {
511 const int kDialogWidth = 394; 555 const int kDialogWidth = 394;
512 // TODO(dgozman): remove 50, which is a title height once popup window 556 // TODO(dgozman): remove 50, which is a title height once popup window
513 // will have no title. 557 // will have no title.
514 const int kDialogHeight = 316 + 50; 558 const int kDialogHeight = 316 + 50;
515 559
516 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 560 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
517 561
(...skipping 20 matching lines...) Expand all
538 profile)); 582 profile));
539 583
540 chrome::AddSelectedTabWithURL(browser, dialog_url, 584 chrome::AddSelectedTabWithURL(browser, dialog_url,
541 content::PAGE_TRANSITION_LINK); 585 content::PAGE_TRANSITION_LINK);
542 } 586 }
543 browser->window()->Show(); 587 browser->window()->Show();
544 } 588 }
545 589
546 void ShowFileInFolder(const FilePath& path) { 590 void ShowFileInFolder(const FilePath& path) {
547 // This action changes the selection so we do not reuse existing tabs. 591 // This action changes the selection so we do not reuse existing tabs.
548 OpenFileBrowser(path, REUSE_NEVER, "selectOnly"); 592 OpenFileBrowser(path, REUSE_NEVER, "select");
549 } 593 }
550 594
551 void ViewFolder(const FilePath& path) { 595 void ViewFolder(const FilePath& path) {
552 OpenFileBrowser(path, REUSE_SAME_PATH, std::string()); 596 OpenFileBrowser(path, REUSE_SAME_PATH, "open");
553 } 597 }
554 598
555 void OpenApplication() { 599 void OpenApplication() {
556 OpenFileBrowser(FilePath(), REUSE_NEVER, std::string()); 600 OpenFileBrowser(FilePath(), REUSE_NEVER, "");
557 } 601 }
558 602
559 bool ExecuteDefaultHandler(Profile* profile, const FilePath& path) { 603 bool ExecuteDefaultHandler(Profile* profile, const FilePath& path) {
560 GURL url; 604 GURL url;
561 if (!ConvertFileToFileSystemUrl(profile, path, 605 if (!ConvertFileToFileSystemUrl(profile, path,
562 GetFileBrowserExtensionUrl().GetOrigin(), &url)) 606 GetFileBrowserExtensionUrl().GetOrigin(), &url))
563 return false; 607 return false;
564 608
565 const FileBrowserHandler* handler; 609 const FileBrowserHandler* handler;
566 if (!file_handler_util::GetTaskForURL(profile, url, &handler)) 610 if (!file_handler_util::GetTaskForURL(profile, url, &handler))
567 return false; 611 return false;
568 612
569 std::string extension_id = handler->extension_id(); 613 std::string extension_id = handler->extension_id();
570 std::string action_id = handler->id(); 614 std::string action_id = handler->id();
571 Browser* browser = browser::FindLastActiveWithProfile(profile); 615 Browser* browser = browser::FindLastActiveWithProfile(profile);
572 616
573 // If there is no browsers for the profile, bail out. Return true so warning 617 // If there is no browsers for the profile, bail out. Return true so warning
574 // about file type not being supported is not displayed. 618 // about file type not being supported is not displayed.
575 if (!browser) 619 if (!browser)
576 return true; 620 return true;
577 621
578 if (extension_id == kFileBrowserDomain) { 622 if (extension_id == kFileBrowserDomain) {
623 if (IsFileManagerPackaged()) {
624 if (action_id == kFileBrowserGalleryTaskId ||
625 action_id == kFileBrowserMountArchiveTaskId ||
626 action_id == kFileBrowserPlayTaskId ||
627 action_id == kFileBrowserWatchTaskId) {
628 ExecuteHandler(profile, extension_id, action_id, url);
629 return true;
630 }
631 return ExecuteBuiltinHandler(browser, path, action_id);
632 }
633
579 // Only two of the built-in File Browser tasks require opening the File 634 // Only two of the built-in File Browser tasks require opening the File
580 // Browser tab. 635 // Browser tab.
581 if (action_id == kFileBrowserGalleryTaskId || 636 if (action_id == kFileBrowserGalleryTaskId ||
582 action_id == kFileBrowserMountArchiveTaskId) { 637 action_id == kFileBrowserMountArchiveTaskId) {
583 // Tab reuse currently does not work for these two tasks. 638 // Tab reuse currently does not work for these two tasks.
584 // |gallery| tries to put the file url into the tab url but it does not 639 // |gallery| tries to put the file url into the tab url but it does not
585 // work on Chrome OS. 640 // work on Chrome OS.
586 // |mount-archive| does not even try. 641 // |mount-archive| does not even try.
587 OpenFileBrowser(path, REUSE_SAME_PATH, ""); 642 OpenFileBrowser(path, REUSE_SAME_PATH, "");
588 return true; 643 return true;
589 } else {
590 return ExecuteBuiltinHandler(browser, path, action_id);
591 } 644 }
592 } else { 645 return ExecuteBuiltinHandler(browser, path, action_id);
593 // We are executing the task on behalf of File Browser extension. 646 }
594 const GURL source_url(kBaseFileBrowserUrl);
595 647
596 // If File Browser has not been open yet then it did not request access 648 ExecuteHandler(profile, extension_id, action_id, url);
597 // to the file system. Do it now. 649 return true;
598 fileapi::ExternalFileSystemMountPointProvider* external_provider =
599 BrowserContext::GetDefaultStoragePartition(
600 profile)->GetFileSystemContext()->external_provider();
601 if (!external_provider)
602 return false;
603 external_provider->GrantFullAccessToExtension(source_url.host());
604
605 std::vector<GURL> urls;
606 urls.push_back(url);
607 scoped_refptr<FileTaskExecutor> executor = FileTaskExecutor::Create(profile,
608 source_url, 0 /* no tab id */, extension_id,
609 file_handler_util::kTaskFile, action_id);
610 executor->Execute(urls);
611 return true;
612 }
613 return ExecuteBuiltinHandler(browser, path, std::string());
614 } 650 }
615 651
616 void ViewFile(const FilePath& path) { 652 void ViewFile(const FilePath& path) {
617 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 653 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
618 if (!ExecuteDefaultHandler(profile, path)) 654 if (!ExecuteDefaultHandler(profile, path))
619 ShowWarningMessageBox(profile, path); 655 ShowWarningMessageBox(profile, path);
620 } 656 }
621 657
622 // Reads an entire file into a string. Fails is the file is 4K or longer. 658 // Reads an entire file into a string. Fails is the file is 4K or longer.
623 bool ReadSmallFileToString(const FilePath& path, std::string* contents) { 659 bool ReadSmallFileToString(const FilePath& path, std::string* contents) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 drive::HOSTED_DOCUMENT)); 742 drive::HOSTED_DOCUMENT));
707 } else { 743 } else {
708 // The file is local (downloaded from an attachment or otherwise copied). 744 // The file is local (downloaded from an attachment or otherwise copied).
709 // Parse the file to extract the Docs url and open this url. 745 // Parse the file to extract the Docs url and open this url.
710 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 746 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
711 base::Bind(&ReadUrlFromGDocOnFileThread, path)); 747 base::Bind(&ReadUrlFromGDocOnFileThread, path));
712 } 748 }
713 return true; 749 return true;
714 } 750 }
715 751
716 if (internal_task_id == kFileBrowserPlayTaskId) { 752 if (!IsFileManagerPackaged()) {
717 GURL url; 753 if (internal_task_id == kFileBrowserPlayTaskId) {
718 if (!ConvertFileToFileSystemUrl(profile, path, 754 GURL url;
719 GetFileBrowserExtensionUrl().GetOrigin(), &url)) 755 if (!ConvertFileToFileSystemUrl(profile, path,
720 return false; 756 GetFileBrowserExtensionUrl().GetOrigin(), &url))
721 MediaPlayer* mediaplayer = MediaPlayer::GetInstance(); 757 return false;
722 mediaplayer->PopupMediaPlayer(); 758 MediaPlayer* mediaplayer = MediaPlayer::GetInstance();
723 mediaplayer->ForcePlayMediaURL(url); 759 mediaplayer->PopupMediaPlayer();
724 return true; 760 mediaplayer->ForcePlayMediaURL(url);
725 } 761 return true;
726 if (internal_task_id == kFileBrowserWatchTaskId) { 762 }
727 GURL url; 763 if (internal_task_id == kFileBrowserWatchTaskId) {
728 if (!ConvertFileToFileSystemUrl(profile, path, 764 GURL url;
729 GetFileBrowserExtensionUrl().GetOrigin(), &url)) 765 if (!ConvertFileToFileSystemUrl(profile, path,
730 return false; 766 GetFileBrowserExtensionUrl().GetOrigin(), &url))
767 return false;
731 768
732 ExtensionService* service = profile->GetExtensionService(); 769 ExtensionService* service = profile->GetExtensionService();
733 if (!service) 770 if (!service)
734 return false; 771 return false;
735 772
736 const extensions::Extension* extension = 773 const extensions::Extension* extension =
737 service->GetExtensionById(kFileBrowserDomain, false); 774 service->GetExtensionById(kFileBrowserDomain, false);
738 if (!extension) 775 if (!extension)
739 return false; 776 return false;
740 777
741 application_launch::LaunchParams params(profile, extension, 778 application_launch::LaunchParams params(profile, extension,
742 extension_misc::LAUNCH_WINDOW, 779 extension_misc::LAUNCH_WINDOW,
743 NEW_FOREGROUND_TAB); 780 NEW_FOREGROUND_TAB);
744 params.override_url = GetVideoPlayerUrl(url); 781 params.override_url = GetVideoPlayerUrl(url);
745 application_launch::OpenApplication(params); 782 application_launch::OpenApplication(params);
746 return true; 783 return true;
784 }
747 } 785 }
748 786
749 if (IsCRXFile(file_extension.data())) { 787 if (IsCRXFile(file_extension.data())) {
750 if (drive::util::IsUnderDriveMountPoint(path)) { 788 if (drive::util::IsUnderDriveMountPoint(path)) {
751 drive::DriveSystemService* system_service = 789 drive::DriveSystemService* system_service =
752 drive::DriveSystemServiceFactory::GetForProfile(profile); 790 drive::DriveSystemServiceFactory::GetForProfile(profile);
753 if (!system_service) 791 if (!system_service)
754 return false; 792 return false;
755 system_service->file_system()->GetFileByPath( 793 system_service->file_system()->GetFileByPath(
756 drive::util::ExtractDrivePath(path), 794 drive::util::ExtractDrivePath(path),
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
815 for (google_apis::OperationProgressStatusList::const_iterator iter = 853 for (google_apis::OperationProgressStatusList::const_iterator iter =
816 list.begin(); 854 list.begin();
817 iter != list.end(); ++iter) { 855 iter != list.end(); ++iter) {
818 result_list->Append( 856 result_list->Append(
819 ProgessStatusToDictionaryValue(profile, origin_url, *iter)); 857 ProgessStatusToDictionaryValue(profile, origin_url, *iter));
820 } 858 }
821 return result_list.release(); 859 return result_list.release();
822 } 860 }
823 861
824 } // namespace file_manager_util 862 } // namespace file_manager_util
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/file_manager/js/background.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698