| 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/mac/mac_util.h" | 12 #include "base/mac/mac_util.h" |
| 13 #include "base/memory/scoped_nsobject.h" | 13 #include "base/memory/scoped_nsobject.h" |
| 14 #include "base/scoped_temp_dir.h" | 14 #include "base/scoped_temp_dir.h" |
| 15 #include "base/sys_string_conversions.h" | 15 #include "base/sys_string_conversions.h" |
| 16 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
| 18 #include "chrome/common/chrome_paths_internal.h" |
| 17 #include "chrome/common/mac/app_mode_common.h" | 19 #include "chrome/common/mac/app_mode_common.h" |
| 18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 19 #include "grit/chromium_strings.h" | 21 #include "grit/chromium_strings.h" |
| 20 #include "skia/ext/skia_utils_mac.h" | 22 #include "skia/ext/skia_utils_mac.h" |
| 21 #include "third_party/icon_family/IconFamily.h" | 23 #include "third_party/icon_family/IconFamily.h" |
| 22 #include "ui/base/l10n/l10n_util_mac.h" | 24 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 // Creates a NSBitmapImageRep from |bitmap|. | 28 // Creates a NSBitmapImageRep from |bitmap|. |
| 27 NSBitmapImageRep* SkBitmapToImageRep(const SkBitmap& bitmap) { | 29 NSBitmapImageRep* SkBitmapToImageRep(const SkBitmap& bitmap) { |
| 28 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( | 30 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
| 29 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | 31 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| 30 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace( | 32 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace( |
| 31 bitmap, color_space.get()); | 33 bitmap, color_space.get()); |
| 32 return base::mac::ObjCCast<NSBitmapImageRep>( | 34 return base::mac::ObjCCast<NSBitmapImageRep>( |
| 33 [[image representations] lastObject]); | 35 [[image representations] lastObject]); |
| 34 } | 36 } |
| 35 | 37 |
| 36 } // namespace | 38 } // namespace |
| 37 | 39 |
| 38 | 40 |
| 39 namespace web_app { | 41 namespace web_app { |
| 40 | 42 |
| 41 WebAppShortcutCreator::WebAppShortcutCreator( | 43 WebAppShortcutCreator::WebAppShortcutCreator( |
| 42 const FilePath& user_data_dir, | 44 const FilePath& user_data_dir, |
| 43 const ShellIntegration::ShortcutInfo& shortcut_info) | 45 const ShellIntegration::ShortcutInfo& shortcut_info, |
| 46 const string16& chrome_bundle_id) |
| 44 : user_data_dir_(user_data_dir), | 47 : user_data_dir_(user_data_dir), |
| 45 info_(shortcut_info) { | 48 info_(shortcut_info), |
| 49 chrome_bundle_id_(chrome_bundle_id) { |
| 46 } | 50 } |
| 47 | 51 |
| 48 WebAppShortcutCreator::~WebAppShortcutCreator() { | 52 WebAppShortcutCreator::~WebAppShortcutCreator() { |
| 49 } | 53 } |
| 50 | 54 |
| 51 bool WebAppShortcutCreator::CreateShortcut() { | 55 bool WebAppShortcutCreator::CreateShortcut() { |
| 52 FilePath app_name = internals::GetSanitizedFileName(info_.title); | 56 FilePath app_name = internals::GetSanitizedFileName(info_.title); |
| 53 FilePath app_file_name = app_name.ReplaceExtension("app"); | 57 FilePath app_file_name = app_name.ReplaceExtension("app"); |
| 54 ScopedTempDir scoped_temp_dir; | 58 ScopedTempDir scoped_temp_dir; |
| 55 if (!scoped_temp_dir.CreateUniqueTempDir()) | 59 if (!scoped_temp_dir.CreateUniqueTempDir()) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 76 return false; | 80 return false; |
| 77 } | 81 } |
| 78 | 82 |
| 79 [[NSWorkspace sharedWorkspace] | 83 [[NSWorkspace sharedWorkspace] |
| 80 selectFile:base::mac::FilePathToNSString(dst_path) | 84 selectFile:base::mac::FilePathToNSString(dst_path) |
| 81 inFileViewerRootedAtPath:nil]; | 85 inFileViewerRootedAtPath:nil]; |
| 82 return true; | 86 return true; |
| 83 } | 87 } |
| 84 | 88 |
| 85 FilePath WebAppShortcutCreator::GetAppLoaderPath() const { | 89 FilePath WebAppShortcutCreator::GetAppLoaderPath() const { |
| 86 NSString* app_loader = [l10n_util::GetNSString(IDS_PRODUCT_NAME) | |
| 87 stringByAppendingString:@" App Mode Loader.app"]; | |
| 88 return base::mac::PathForFrameworkBundleResource( | 90 return base::mac::PathForFrameworkBundleResource( |
| 89 base::mac::NSToCFCast(app_loader)); | 91 base::mac::NSToCFCast(@"app_mode_loader.app")); |
| 90 } | 92 } |
| 91 | 93 |
| 92 FilePath WebAppShortcutCreator::GetDestinationPath( | 94 FilePath WebAppShortcutCreator::GetDestinationPath( |
| 93 const FilePath& app_file_name) const { | 95 const FilePath& app_file_name) const { |
| 94 FilePath path; | 96 FilePath path; |
| 95 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | 97 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
| 96 file_util::PathIsWritable(path)) { | 98 file_util::PathIsWritable(path)) { |
| 97 return path; | 99 return path; |
| 98 } | 100 } |
| 99 | 101 |
| 100 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | 102 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
| 101 return path; | 103 return path; |
| 102 | 104 |
| 103 return FilePath(); | 105 return FilePath(); |
| 104 } | 106 } |
| 105 | 107 |
| 106 bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const { | 108 bool WebAppShortcutCreator::UpdatePlist(const FilePath& app_path) const { |
| 107 NSString* plist_path = base::mac::FilePathToNSString( | 109 NSString* plist_path = base::mac::FilePathToNSString( |
| 108 app_path.Append("Contents").Append("Info.plist")); | 110 app_path.Append("Contents").Append("Info.plist")); |
| 109 NSMutableDictionary* dict = | 111 NSMutableDictionary* dict = |
| 110 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; | 112 [NSMutableDictionary dictionaryWithContentsOfFile:plist_path]; |
| 111 | 113 |
| 114 [dict setObject:GetBundleIdentifier(dict) |
| 115 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; |
| 112 [dict setObject:base::SysUTF8ToNSString(info_.extension_id) | 116 [dict setObject:base::SysUTF8ToNSString(info_.extension_id) |
| 113 forKey:app_mode::kCrAppModeShortcutIDKey]; | 117 forKey:app_mode::kCrAppModeShortcutIDKey]; |
| 114 [dict setObject:base::SysUTF16ToNSString(info_.title) | 118 [dict setObject:base::SysUTF16ToNSString(info_.title) |
| 115 forKey:app_mode::kCrAppModeShortcutNameKey]; | 119 forKey:app_mode::kCrAppModeShortcutNameKey]; |
| 116 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) | 120 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) |
| 117 forKey:app_mode::kCrAppModeShortcutURLKey]; | 121 forKey:app_mode::kCrAppModeShortcutURLKey]; |
| 118 [dict setObject:base::mac::FilePathToNSString(user_data_dir_) | 122 [dict setObject:base::mac::FilePathToNSString(user_data_dir_) |
| 119 forKey:app_mode::kCrAppModeUserDataDirKey]; | 123 forKey:app_mode::kCrAppModeUserDataDirKey]; |
| 120 [dict setObject:base::mac::FilePathToNSString(info_.extension_path) | 124 [dict setObject:base::mac::FilePathToNSString(info_.extension_path) |
| 121 forKey:app_mode::kCrAppModeExtensionPathKey]; | 125 forKey:app_mode::kCrAppModeExtensionPathKey]; |
| 126 [dict setObject:base::SysUTF16ToNSString(chrome_bundle_id_) |
| 127 forKey:app_mode::kBrowserBundleIDKey]; |
| 122 return [dict writeToFile:plist_path atomically:YES]; | 128 return [dict writeToFile:plist_path atomically:YES]; |
| 123 } | 129 } |
| 124 | 130 |
| 125 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { | 131 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { |
| 126 // TODO(sail): Add support for multiple icon sizes. | 132 // TODO(sail): Add support for multiple icon sizes. |
| 127 if (info_.favicon.empty() || info_.favicon.width() != 32 || | 133 if (info_.favicon.empty() || info_.favicon.width() != 32 || |
| 128 info_.favicon.height() != 32) { | 134 info_.favicon.height() != 32) { |
| 129 return true; | 135 return true; |
| 130 } | 136 } |
| 131 | 137 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 if (!success) | 151 if (!success) |
| 146 return false; | 152 return false; |
| 147 | 153 |
| 148 FilePath resources_path = app_path.Append("Contents").Append("Resources"); | 154 FilePath resources_path = app_path.Append("Contents").Append("Resources"); |
| 149 if (!file_util::CreateDirectory(resources_path)) | 155 if (!file_util::CreateDirectory(resources_path)) |
| 150 return false; | 156 return false; |
| 151 FilePath icon_path = resources_path.Append("app.icns"); | 157 FilePath icon_path = resources_path.Append("app.icns"); |
| 152 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 158 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; |
| 153 } | 159 } |
| 154 | 160 |
| 161 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const |
| 162 { |
| 163 NSString* bundle_id_template = |
| 164 base::mac::ObjCCast<NSString>( |
| 165 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); |
| 166 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); |
| 167 NSString* placeholder = |
| 168 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; |
| 169 NSString* bundle_id = |
| 170 [bundle_id_template |
| 171 stringByReplacingOccurrencesOfString:placeholder |
| 172 withString:extension_id]; |
| 173 return bundle_id; |
| 174 } |
| 175 |
| 155 } // namespace | 176 } // namespace |
| 156 | 177 |
| 157 namespace web_app { | 178 namespace web_app { |
| 158 namespace internals { | 179 namespace internals { |
| 159 | 180 |
| 160 void CreateShortcutTask(const FilePath& web_app_path, | 181 void CreateShortcutTask(const FilePath& web_app_path, |
| 161 const FilePath& profile_path, | 182 const FilePath& profile_path, |
| 162 const ShellIntegration::ShortcutInfo& shortcut_info) { | 183 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 164 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info); | 185 string16 bundle_id = UTF8ToUTF16(base::mac::BaseBundleID()); |
| 186 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info, |
| 187 bundle_id); |
| 165 shortcut_creator.CreateShortcut(); | 188 shortcut_creator.CreateShortcut(); |
| 166 } | 189 } |
| 167 | 190 |
| 168 } // namespace internals | 191 } // namespace internals |
| 169 } // namespace web_app | 192 } // namespace web_app |
| OLD | NEW |