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/web_applications/web_app_mac.h" | 5 #import "chrome/browser/web_applications/web_app_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/mac/bundle_locations.h" | 10 #include "base/mac/bundle_locations.h" |
| 11 #include "base/mac/foundation_util.h" | 11 #include "base/mac/foundation_util.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/sys_string_conversions.h" | 13 #include "base/sys_string_conversions.h" |
| 14 #include "chrome/browser/web_applications/web_app.h" | 14 #include "chrome/browser/web_applications/web_app.h" |
| 15 #include "chrome/common/chrome_paths_internal.h" | |
| 15 #include "chrome/common/mac/app_mode_common.h" | 16 #include "chrome/common/mac/app_mode_common.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "grit/chromium_strings.h" | 18 #include "grit/chromium_strings.h" |
| 18 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
| 19 | 20 |
| 20 namespace web_app { | 21 namespace web_app { |
| 21 | 22 |
| 22 WebAppShortcutCreator::WebAppShortcutCreator( | 23 WebAppShortcutCreator::WebAppShortcutCreator( |
| 23 const ShellIntegration::ShortcutInfo& shortcut_info) | 24 const ShellIntegration::ShortcutInfo& shortcut_info) |
| 24 : info_(shortcut_info) { | 25 : info_(shortcut_info) { |
| 25 } | 26 } |
| 26 | 27 |
| 27 WebAppShortcutCreator::~WebAppShortcutCreator() { | 28 WebAppShortcutCreator::~WebAppShortcutCreator() { |
| 28 } | 29 } |
| 29 | 30 |
| 30 bool WebAppShortcutCreator::CreateShortcut() { | 31 bool WebAppShortcutCreator::CreateShortcut(NSString* chrome_bundle_id) { |
| 31 FilePath app_name = internals::GetSanitizedFileName(info_.title); | 32 FilePath app_name = internals::GetSanitizedFileName(info_.title); |
| 32 FilePath app_file_name = app_name.ReplaceExtension("app"); | 33 FilePath app_file_name = app_name.ReplaceExtension("app"); |
| 33 ScopedTempDir scoped_temp_dir; | 34 ScopedTempDir scoped_temp_dir; |
| 34 if (!scoped_temp_dir.CreateUniqueTempDir()) | 35 if (!scoped_temp_dir.CreateUniqueTempDir()) |
| 35 return false; | 36 return false; |
| 36 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name); | 37 FilePath staging_path = scoped_temp_dir.path().Append(app_file_name); |
| 37 | 38 |
| 38 // Update the app's plist and icon in a temp directory. This works around | 39 // Update the app's plist and icon in a temp directory. This works around |
| 39 // a Finder bug where the app's icon doesn't properly update. | 40 // a Finder bug where the app's icon doesn't properly update. |
| 40 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) { | 41 if (!file_util::CopyDirectory(GetAppLoaderPath(), staging_path, true)) { |
| 41 LOG(ERROR) << "Copying app to staging path: " << staging_path.value() | 42 LOG(ERROR) << "Copying app to staging path: " << staging_path.value() |
| 42 << " failed"; | 43 << " failed"; |
| 43 return false; | 44 return false; |
| 44 } | 45 } |
| 45 | 46 |
| 46 if (!UpdatePlist(staging_path)) | 47 if (!UpdatePlist(staging_path, chrome_bundle_id)) |
| 47 return false; | 48 return false; |
| 48 | 49 |
| 49 if (!UpdateIcon(staging_path)) | 50 if (!UpdateIcon(staging_path)) |
| 50 return false; | 51 return false; |
| 51 | 52 |
| 52 FilePath dst_path = GetDestinationPath(app_file_name); | 53 FilePath dst_path = GetDestinationPath(app_file_name); |
| 53 if (!file_util::CopyDirectory(staging_path, dst_path, true)) { | 54 if (!file_util::CopyDirectory(staging_path, dst_path, true)) { |
| 54 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; | 55 LOG(ERROR) << "Copying app to dst path: " << dst_path.value() << " failed"; |
| 55 return false; | 56 return false; |
| 56 } | 57 } |
| 57 | 58 |
| 58 [[NSWorkspace sharedWorkspace] | 59 [[NSWorkspace sharedWorkspace] |
| 59 selectFile:base::mac::FilePathToNSString(dst_path) | 60 selectFile:base::mac::FilePathToNSString(dst_path) |
| 60 inFileViewerRootedAtPath:nil]; | 61 inFileViewerRootedAtPath:nil]; |
| 61 return true; | 62 return true; |
| 62 } | 63 } |
| 63 | 64 |
| 64 FilePath WebAppShortcutCreator::GetAppLoaderPath() const { | 65 FilePath WebAppShortcutCreator::GetAppLoaderPath() const { |
| 65 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) | |
| 66 stringByAppendingString:@" App Mode Loader.app"]; | |
| 67 return base::mac::PathForFrameworkBundleResource( | 66 return base::mac::PathForFrameworkBundleResource( |
| 68 base::mac::NSToCFCast(app_loader)); | 67 base::mac::NSToCFCast(@"app_mode_loader.app")); |
| 69 } | 68 } |
| 70 | 69 |
| 71 FilePath WebAppShortcutCreator::GetDestinationPath( | 70 FilePath WebAppShortcutCreator::GetDestinationPath( |
| 72 const FilePath& app_file_name) const { | 71 const FilePath& app_file_name) const { |
| 73 FilePath path; | 72 FilePath path; |
| 74 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | 73 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
| 75 file_util::PathIsWritable(path)) { | 74 file_util::PathIsWritable(path)) { |
| 76 return path; | 75 return path; |
| 77 } | 76 } |
| 78 | 77 |
| 79 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | 78 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
| 80 return path; | 79 return path; |
| 81 | 80 |
| 82 return FilePath(); | 81 return FilePath(); |
| 83 } | 82 } |
| 84 | 83 |
| 85 bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const { | 84 bool WebAppShortcutCreator::UpdatePlist( |
| 85 const FilePath& app_path, NSString* chrome_bundle_id) const { | |
| 86 NSString* const kCFBundleIdentifer = @"CFBundleIdentifier"; | |
|
sail
2012/02/17 16:42:09
you could also use kCFBundleIdentifierKey here
jeremy
2012/02/19 14:36:41
Done.
| |
| 87 | |
| 86 NSString* plist_path = base::mac::FilePathToNSString( | 88 NSString* plist_path = base::mac::FilePathToNSString( |
| 87 app_path.Append("Contents").Append("Info.plist")); | 89 app_path.Append("Contents").Append("Info.plist")); |
| 88 NSMutableDictionary* dict = | 90 NSMutableDictionary* dict = |
| 89 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; | 91 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; |
| 90 | 92 |
| 91 [dict setObject:base::SysUTF8ToNSString(info_.extension_id) | 93 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); |
| 94 | |
| 95 // Set the bundle signature. | |
| 96 NSString* bundle_id_template = | |
| 97 base::mac::ObjCCast<NSString>([dict objectForKey:kCFBundleIdentifer]); | |
| 98 NSString* bundle_id = | |
| 99 [bundle_id_template | |
| 100 stringByReplacingOccurrencesOfString:app_mode::kShortcutIdPlaceholder | |
|
sail
2012/02/17 16:42:09
this function is becoming a bit long, could you ch
jeremy
2012/02/19 14:36:41
Done.
| |
| 101 withString:extension_id]; | |
| 102 [dict setObject:bundle_id | |
| 103 forKey:kCFBundleIdentifer]; | |
| 104 | |
| 105 | |
| 106 [dict setObject:extension_id | |
| 92 forKey:app_mode::kCrAppModeShortcutIDKey]; | 107 forKey:app_mode::kCrAppModeShortcutIDKey]; |
| 93 [dict setObject:base::SysUTF16ToNSString(info_.title) | 108 [dict setObject:base::SysUTF16ToNSString(info_.title) |
| 94 forKey:app_mode::kCrAppModeShortcutNameKey]; | 109 forKey:app_mode::kCrAppModeShortcutNameKey]; |
| 95 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) | 110 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) |
| 96 forKey:app_mode::kCrAppModeShortcutURLKey]; | 111 forKey:app_mode::kCrAppModeShortcutURLKey]; |
| 112 [dict setObject:chrome_bundle_id | |
| 113 forKey:app_mode::kBrowserBundleIDKey]; | |
| 97 return [dict writeToFile:plist_path atomically:YES]; | 114 return [dict writeToFile:plist_path atomically:YES]; |
| 98 } | 115 } |
| 99 | 116 |
| 100 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { | 117 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { |
| 101 // TODO:(sail) Need to implement this. | 118 // TODO:(sail) Need to implement this. |
| 102 return true; | 119 return true; |
| 103 } | 120 } |
| 104 | 121 |
| 105 } // namespace | 122 } // namespace |
| 106 | 123 |
| 107 namespace web_app { | 124 namespace web_app { |
| 108 namespace internals { | 125 namespace internals { |
| 109 | 126 |
| 110 void CreateShortcutTask(const FilePath& web_app_path, | 127 void CreateShortcutTask(const FilePath& web_app_path, |
| 111 const FilePath& profile_path, | 128 const FilePath& profile_path, |
| 112 const ShellIntegration::ShortcutInfo& shortcut_info) { | 129 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 130 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 114 WebAppShortcutCreator shortcut_creator(shortcut_info); | 131 WebAppShortcutCreator shortcut_creator(shortcut_info); |
| 115 shortcut_creator.CreateShortcut(); | 132 shortcut_creator.CreateShortcut([chrome::OuterAppBundle() bundleIdentifier]); |
|
sail
2012/02/17 16:42:09
bug 24842 seems to imply that NSBundle isn't threa
jeremy
2012/02/19 14:36:41
Good catch! Thanks!
| |
| 116 } | 133 } |
| 117 | 134 |
| 118 } // namespace internals | 135 } // namespace internals |
| 119 } // namespace web_app | 136 } // namespace web_app |
| OLD | NEW |