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

Side by Side Diff: chrome/browser/web_applications/web_app_mac.mm

Issue 64803005: File association for app shims. (Mac) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Change FileHandlersInfo to hold a copy. Created 6 years, 8 months 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
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 4
5 #import "chrome/browser/web_applications/web_app_mac.h" 5 #import "chrome/browser/web_applications/web_app_mac.h"
6 6
7 #import <Carbon/Carbon.h> 7 #import <Carbon/Carbon.h>
8 #import <Cocoa/Cocoa.h> 8 #import <Cocoa/Cocoa.h>
9 9
10 #include "apps/app_shim/app_shim_mac.h" 10 #include "apps/app_shim/app_shim_mac.h"
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 base::FilePath profile_base_name = base::mac::NSStringToFilePath( 410 base::FilePath profile_base_name = base::mac::NSStringToFilePath(
411 [plist valueForKey:app_mode::kCrAppModeProfileDirKey]); 411 [plist valueForKey:app_mode::kCrAppModeProfileDirKey]);
412 if (user_data_dir.DirName().DirName().BaseName() == profile_base_name) 412 if (user_data_dir.DirName().DirName().BaseName() == profile_base_name)
413 shortcut_info.profile_path = user_data_dir.DirName().DirName(); 413 shortcut_info.profile_path = user_data_dir.DirName().DirName();
414 else 414 else
415 shortcut_info.profile_path = user_data_dir.Append(profile_base_name); 415 shortcut_info.profile_path = user_data_dir.Append(profile_base_name);
416 416
417 return shortcut_info; 417 return shortcut_info;
418 } 418 }
419 419
420 void UpdateFileTypes(NSMutableDictionary* plist,
421 const extensions::FileHandlersInfo& file_handlers_info) {
422 const std::vector<extensions::FileHandlerInfo>& handlers =
423 file_handlers_info.handlers;
424 NSMutableArray* document_types =
425 [NSMutableArray arrayWithCapacity:handlers.size()];
426
427 for (std::vector<extensions::FileHandlerInfo>::const_iterator info_it =
428 handlers.begin();
429 info_it != handlers.end();
430 ++info_it) {
431 const extensions::FileHandlerInfo& info = *info_it;
432
433 NSMutableArray* file_extensions =
434 [NSMutableArray arrayWithCapacity:info.extensions.size()];
435 for (std::set<std::string>::iterator it = info.extensions.begin();
436 it != info.extensions.end(); ++it) {
437 [file_extensions addObject:base::SysUTF8ToNSString(*it)];
438 }
439
440 NSMutableArray* mime_types =
441 [NSMutableArray arrayWithCapacity:info.types.size()];
442 for (std::set<std::string>::iterator it = info.types.begin();
443 it != info.types.end(); ++it) {
444 [mime_types addObject:base::SysUTF8ToNSString(*it)];
445 }
446
447 NSDictionary* type_dictionary = @{
448 // TODO(jackhou): Add the type name and and icon file once the manifest
449 // supports these.
450 // app_mode::kCFBundleTypeNameKey : ,
451 // app_mode::kCFBundleTypeIconFileKey : ,
452 app_mode::kCFBundleTypeExtensionsKey : file_extensions,
453 app_mode::kCFBundleTypeMIMETypesKey : mime_types,
454 app_mode::kCFBundleTypeRoleKey : app_mode::kBundleTypeRoleViewer
455 };
456 [document_types addObject:type_dictionary];
457 }
458
459 [plist setObject:document_types
460 forKey:app_mode::kCFBundleDocumentTypesKey];
461 }
462
420 } // namespace 463 } // namespace
421 464
422 namespace chrome { 465 namespace chrome {
423 466
424 void ShowCreateChromeAppShortcutsDialog(gfx::NativeWindow /*parent_window*/, 467 void ShowCreateChromeAppShortcutsDialog(gfx::NativeWindow /*parent_window*/,
425 Profile* profile, 468 Profile* profile,
426 const extensions::Extension* app, 469 const extensions::Extension* app,
427 const base::Closure& close_callback) { 470 const base::Closure& close_callback) {
428 // Normally we would show a dialog, but since we always create the app 471 // Normally we would show a dialog, but since we always create the app
429 // shortcut in ~/Applications there are no options for the user to choose. 472 // shortcut in ~/Applications there are no options for the user to choose.
430 web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER, 473 web_app::CreateShortcuts(web_app::SHORTCUT_CREATION_BY_USER,
431 ShellIntegration::ShortcutLocations(), 474 ShellIntegration::ShortcutLocations(),
432 profile, 475 profile,
433 app); 476 app);
434 if (!close_callback.is_null()) 477 if (!close_callback.is_null())
435 close_callback.Run(); 478 close_callback.Run();
436 } 479 }
437 480
438 } // namespace chrome 481 } // namespace chrome
439 482
440 namespace web_app { 483 namespace web_app {
441 484
442 WebAppShortcutCreator::WebAppShortcutCreator( 485 WebAppShortcutCreator::WebAppShortcutCreator(
443 const base::FilePath& app_data_dir, 486 const base::FilePath& app_data_dir,
444 const ShellIntegration::ShortcutInfo& shortcut_info) 487 const ShellIntegration::ShortcutInfo& shortcut_info,
488 const extensions::FileHandlersInfo& file_handlers_info)
445 : app_data_dir_(app_data_dir), 489 : app_data_dir_(app_data_dir),
446 info_(shortcut_info) {} 490 info_(shortcut_info),
491 file_handlers_info_(file_handlers_info) {}
447 492
448 WebAppShortcutCreator::~WebAppShortcutCreator() {} 493 WebAppShortcutCreator::~WebAppShortcutCreator() {}
449 494
450 base::FilePath WebAppShortcutCreator::GetApplicationsShortcutPath() const { 495 base::FilePath WebAppShortcutCreator::GetApplicationsShortcutPath() const {
451 base::FilePath applications_dir = GetApplicationsDirname(); 496 base::FilePath applications_dir = GetApplicationsDirname();
452 return applications_dir.empty() ? 497 return applications_dir.empty() ?
453 base::FilePath() : applications_dir.Append(GetShortcutBasename()); 498 base::FilePath() : applications_dir.Append(GetShortcutBasename());
454 } 499 }
455 500
456 base::FilePath WebAppShortcutCreator::GetInternalShortcutPath() const { 501 base::FilePath WebAppShortcutCreator::GetInternalShortcutPath() const {
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 if (info_.extension_id == app_mode::kAppListModeId) { 716 if (info_.extension_id == app_mode::kAppListModeId) {
672 // Prevent the app list from bouncing in the dock, and getting a run light. 717 // Prevent the app list from bouncing in the dock, and getting a run light.
673 [plist setObject:[NSNumber numberWithBool:YES] 718 [plist setObject:[NSNumber numberWithBool:YES]
674 forKey:kLSUIElement]; 719 forKey:kLSUIElement];
675 } 720 }
676 721
677 base::FilePath app_name = app_path.BaseName().RemoveExtension(); 722 base::FilePath app_name = app_path.BaseName().RemoveExtension();
678 [plist setObject:base::mac::FilePathToNSString(app_name) 723 [plist setObject:base::mac::FilePathToNSString(app_name)
679 forKey:base::mac::CFToNSCast(kCFBundleNameKey)]; 724 forKey:base::mac::CFToNSCast(kCFBundleNameKey)];
680 725
726 if (CommandLine::ForCurrentProcess()->HasSwitch(
727 switches::kEnableAppsFileAssociations)) {
728 UpdateFileTypes(plist, file_handlers_info_);
729 }
730
681 return [plist writeToFile:plist_path 731 return [plist writeToFile:plist_path
682 atomically:YES]; 732 atomically:YES];
683 } 733 }
684 734
685 bool WebAppShortcutCreator::UpdateDisplayName( 735 bool WebAppShortcutCreator::UpdateDisplayName(
686 const base::FilePath& app_path) const { 736 const base::FilePath& app_path) const {
687 // OSX searches for the best language in the order of preferred languages. 737 // OSX searches for the best language in the order of preferred languages.
688 // Since we only have one localization directory, it will choose this one. 738 // Since we only have one localization directory, it will choose this one.
689 base::FilePath localized_dir = GetResourcesPath(app_path).Append("en.lproj"); 739 base::FilePath localized_dir = GetResourcesPath(app_path).Append("en.lproj");
690 if (!base::CreateDirectory(localized_dir)) 740 if (!base::CreateDirectory(localized_dir))
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 if (app_path.empty()) 838 if (app_path.empty())
789 return; 839 return;
790 840
791 [[NSWorkspace sharedWorkspace] 841 [[NSWorkspace sharedWorkspace]
792 selectFile:base::mac::FilePathToNSString(app_path) 842 selectFile:base::mac::FilePathToNSString(app_path)
793 inFileViewerRootedAtPath:nil]; 843 inFileViewerRootedAtPath:nil];
794 } 844 }
795 845
796 base::FilePath GetAppInstallPath( 846 base::FilePath GetAppInstallPath(
797 const ShellIntegration::ShortcutInfo& shortcut_info) { 847 const ShellIntegration::ShortcutInfo& shortcut_info) {
798 WebAppShortcutCreator shortcut_creator(base::FilePath(), shortcut_info); 848 WebAppShortcutCreator shortcut_creator(
849 base::FilePath(), shortcut_info, extensions::FileHandlersInfo(NULL));
799 return shortcut_creator.GetApplicationsShortcutPath(); 850 return shortcut_creator.GetApplicationsShortcutPath();
800 } 851 }
801 852
802 void MaybeLaunchShortcut(const ShellIntegration::ShortcutInfo& shortcut_info) { 853 void MaybeLaunchShortcut(const ShellIntegration::ShortcutInfo& shortcut_info) {
803 if (!apps::IsAppShimsEnabled()) 854 if (!apps::IsAppShimsEnabled())
804 return; 855 return;
805 856
806 content::BrowserThread::PostTask( 857 content::BrowserThread::PostTask(
807 content::BrowserThread::FILE, FROM_HERE, 858 content::BrowserThread::FILE, FROM_HERE,
808 base::Bind(&LaunchShimOnFileThread, shortcut_info)); 859 base::Bind(&LaunchShimOnFileThread, shortcut_info));
809 } 860 }
810 861
811 namespace internals { 862 namespace internals {
812 863
813 bool CreatePlatformShortcuts( 864 bool CreatePlatformShortcuts(
814 const base::FilePath& app_data_path, 865 const base::FilePath& app_data_path,
815 const ShellIntegration::ShortcutInfo& shortcut_info, 866 const ShellIntegration::ShortcutInfo& shortcut_info,
816 const extensions::FileHandlersInfo& file_handlers_info, 867 const extensions::FileHandlersInfo& file_handlers_info,
817 const ShellIntegration::ShortcutLocations& creation_locations, 868 const ShellIntegration::ShortcutLocations& creation_locations,
818 ShortcutCreationReason creation_reason) { 869 ShortcutCreationReason creation_reason) {
819 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 870 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
820 WebAppShortcutCreator shortcut_creator(app_data_path, shortcut_info); 871 WebAppShortcutCreator shortcut_creator(
872 app_data_path, shortcut_info, file_handlers_info);
821 return shortcut_creator.CreateShortcuts(creation_reason, creation_locations); 873 return shortcut_creator.CreateShortcuts(creation_reason, creation_locations);
822 } 874 }
823 875
824 void DeletePlatformShortcuts( 876 void DeletePlatformShortcuts(
825 const base::FilePath& app_data_path, 877 const base::FilePath& app_data_path,
826 const ShellIntegration::ShortcutInfo& shortcut_info) { 878 const ShellIntegration::ShortcutInfo& shortcut_info) {
827 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 879 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
828 WebAppShortcutCreator shortcut_creator(app_data_path, shortcut_info); 880 WebAppShortcutCreator shortcut_creator(
881 app_data_path, shortcut_info, extensions::FileHandlersInfo(NULL));
829 shortcut_creator.DeleteShortcuts(); 882 shortcut_creator.DeleteShortcuts();
830 } 883 }
831 884
832 void UpdatePlatformShortcuts( 885 void UpdatePlatformShortcuts(
833 const base::FilePath& app_data_path, 886 const base::FilePath& app_data_path,
834 const base::string16& old_app_title, 887 const base::string16& old_app_title,
835 const ShellIntegration::ShortcutInfo& shortcut_info, 888 const ShellIntegration::ShortcutInfo& shortcut_info,
836 const extensions::FileHandlersInfo& file_handlers_info) { 889 const extensions::FileHandlersInfo& file_handlers_info) {
837 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 890 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
838 WebAppShortcutCreator shortcut_creator(app_data_path, shortcut_info); 891 WebAppShortcutCreator shortcut_creator(
892 app_data_path, shortcut_info, file_handlers_info);
839 shortcut_creator.UpdateShortcuts(); 893 shortcut_creator.UpdateShortcuts();
840 } 894 }
841 895
842 void DeleteAllShortcutsForProfile(const base::FilePath& profile_path) { 896 void DeleteAllShortcutsForProfile(const base::FilePath& profile_path) {
843 const std::string profile_base_name = profile_path.BaseName().value(); 897 const std::string profile_base_name = profile_path.BaseName().value();
844 std::vector<base::FilePath> bundles = GetAllAppBundlesInPath( 898 std::vector<base::FilePath> bundles = GetAllAppBundlesInPath(
845 profile_path.Append(chrome::kWebAppDirname), profile_base_name); 899 profile_path.Append(chrome::kWebAppDirname), profile_base_name);
846 900
847 for (std::vector<base::FilePath>::const_iterator it = bundles.begin(); 901 for (std::vector<base::FilePath>::const_iterator it = bundles.begin();
848 it != bundles.end(); ++it) { 902 it != bundles.end(); ++it) {
849 ShellIntegration::ShortcutInfo shortcut_info = 903 ShellIntegration::ShortcutInfo shortcut_info =
850 BuildShortcutInfoFromBundle(*it); 904 BuildShortcutInfoFromBundle(*it);
851 WebAppShortcutCreator shortcut_creator(it->DirName(), shortcut_info); 905 WebAppShortcutCreator shortcut_creator(
906 it->DirName(), shortcut_info, extensions::FileHandlersInfo(NULL));
852 shortcut_creator.DeleteShortcuts(); 907 shortcut_creator.DeleteShortcuts();
853 } 908 }
854 } 909 }
855 910
856 } // namespace internals 911 } // namespace internals
857 912
858 } // namespace web_app 913 } // namespace web_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698