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 #import "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" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 data[index + 0] = SkMulDiv255Round(SkColorGetR(color), alpha); | 59 data[index + 0] = SkMulDiv255Round(SkColorGetR(color), alpha); |
60 data[index + 1] = SkMulDiv255Round(SkColorGetG(color), alpha); | 60 data[index + 1] = SkMulDiv255Round(SkColorGetG(color), alpha); |
61 data[index + 2] = SkMulDiv255Round(SkColorGetB(color), alpha); | 61 data[index + 2] = SkMulDiv255Round(SkColorGetB(color), alpha); |
62 data[index + 3] = alpha; | 62 data[index + 3] = alpha; |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
66 return [image_rep.release() autorelease]; | 66 return [image_rep.release() autorelease]; |
67 } | 67 } |
68 | 68 |
| 69 // Adds |image_rep| to |icon_family|. Returns true on success, false on failure. |
| 70 bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, |
| 71 NSBitmapImageRep* image_rep) { |
| 72 NSSize size = [image_rep size]; |
| 73 if (size.width != size.height) |
| 74 return false; |
| 75 |
| 76 switch ((int)size.width) { |
| 77 case 512: |
| 78 return [icon_family setIconFamilyElement:kIconServices512PixelDataARGB |
| 79 fromBitmapImageRep:image_rep]; |
| 80 case 256: |
| 81 return [icon_family setIconFamilyElement:kIconServices256PixelDataARGB |
| 82 fromBitmapImageRep:image_rep]; |
| 83 case 128: |
| 84 return [icon_family setIconFamilyElement:kThumbnail32BitData |
| 85 fromBitmapImageRep:image_rep] && |
| 86 [icon_family setIconFamilyElement:kThumbnail8BitMask |
| 87 fromBitmapImageRep:image_rep]; |
| 88 case 32: |
| 89 return [icon_family setIconFamilyElement:kLarge32BitData |
| 90 fromBitmapImageRep:image_rep] && |
| 91 [icon_family setIconFamilyElement:kLarge8BitData |
| 92 fromBitmapImageRep:image_rep] && |
| 93 [icon_family setIconFamilyElement:kLarge8BitMask |
| 94 fromBitmapImageRep:image_rep] && |
| 95 [icon_family setIconFamilyElement:kLarge1BitMask |
| 96 fromBitmapImageRep:image_rep]; |
| 97 case 16: |
| 98 return [icon_family setIconFamilyElement:kSmall32BitData |
| 99 fromBitmapImageRep:image_rep] && |
| 100 [icon_family setIconFamilyElement:kSmall8BitData |
| 101 fromBitmapImageRep:image_rep] && |
| 102 [icon_family setIconFamilyElement:kSmall8BitMask |
| 103 fromBitmapImageRep:image_rep] && |
| 104 [icon_family setIconFamilyElement:kSmall1BitMask |
| 105 fromBitmapImageRep:image_rep]; |
| 106 default: |
| 107 return false; |
| 108 } |
| 109 } |
| 110 |
69 } // namespace | 111 } // namespace |
70 | 112 |
71 | 113 |
72 namespace web_app { | 114 namespace web_app { |
73 | 115 |
74 WebAppShortcutCreator::WebAppShortcutCreator( | 116 WebAppShortcutCreator::WebAppShortcutCreator( |
75 const FilePath& web_app_path, | 117 const FilePath& web_app_path, |
76 const ShellIntegration::ShortcutInfo& shortcut_info, | 118 const ShellIntegration::ShortcutInfo& shortcut_info, |
77 const string16& chrome_bundle_id) | 119 const string16& chrome_bundle_id) |
78 : web_app_path_(web_app_path), | 120 : web_app_path_(web_app_path), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; | 217 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; |
176 [plist setObject:base::mac::FilePathToNSString(web_app_path_) | 218 [plist setObject:base::mac::FilePathToNSString(web_app_path_) |
177 forKey:app_mode::kCrAppModeUserDataDirKey]; | 219 forKey:app_mode::kCrAppModeUserDataDirKey]; |
178 [plist setObject:base::mac::FilePathToNSString(info_.extension_path) | 220 [plist setObject:base::mac::FilePathToNSString(info_.extension_path) |
179 forKey:app_mode::kCrAppModeExtensionPathKey]; | 221 forKey:app_mode::kCrAppModeExtensionPathKey]; |
180 | 222 |
181 return [plist writeToFile:plist_path atomically:YES]; | 223 return [plist writeToFile:plist_path atomically:YES]; |
182 } | 224 } |
183 | 225 |
184 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { | 226 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { |
185 // TODO(sail): Add support for multiple icon sizes. | 227 if (info_.favicon.IsEmpty()) |
186 if (info_.favicon.empty() || info_.favicon.width() != 32 || | |
187 info_.favicon.height() != 32) { | |
188 return true; | 228 return true; |
| 229 |
| 230 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); |
| 231 bool image_added = false; |
| 232 for (size_t i = 0; i < info_.favicon.GetNumberOfSkBitmaps(); ++i) { |
| 233 NSBitmapImageRep* image_rep = |
| 234 SkBitmapToRGBAImageRep(*info_.favicon.GetSkBitmapAtIndex(i)); |
| 235 if (!image_rep) |
| 236 continue; |
| 237 |
| 238 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) |
| 239 continue; |
| 240 |
| 241 image_added = true; |
189 } | 242 } |
190 | 243 |
191 NSBitmapImageRep* image_rep = SkBitmapToRGBAImageRep(info_.favicon); | 244 if (!image_added) |
192 if (!image_rep) | |
193 return false; | |
194 | |
195 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | |
196 bool success = [icon_family setIconFamilyElement:kLarge32BitData | |
197 fromBitmapImageRep:image_rep] && | |
198 [icon_family setIconFamilyElement:kLarge8BitData | |
199 fromBitmapImageRep:image_rep] && | |
200 [icon_family setIconFamilyElement:kLarge8BitMask | |
201 fromBitmapImageRep:image_rep] && | |
202 [icon_family setIconFamilyElement:kLarge1BitMask | |
203 fromBitmapImageRep:image_rep]; | |
204 if (!success) | |
205 return false; | 245 return false; |
206 | 246 |
207 FilePath resources_path = app_path.Append("Contents").Append("Resources"); | 247 FilePath resources_path = app_path.Append("Contents").Append("Resources"); |
208 if (!file_util::CreateDirectory(resources_path)) | 248 if (!file_util::CreateDirectory(resources_path)) |
209 return false; | 249 return false; |
| 250 |
210 FilePath icon_path = resources_path.Append("app.icns"); | 251 FilePath icon_path = resources_path.Append("app.icns"); |
211 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 252 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; |
212 } | 253 } |
213 | 254 |
214 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const | 255 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const |
215 { | 256 { |
216 NSString* bundle_id_template = | 257 NSString* bundle_id_template = |
217 base::mac::ObjCCast<NSString>( | 258 base::mac::ObjCCast<NSString>( |
218 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); | 259 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); |
219 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); | 260 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); |
(...skipping 21 matching lines...) Expand all Loading... |
241 const ShellIntegration::ShortcutInfo& shortcut_info) { | 282 const ShellIntegration::ShortcutInfo& shortcut_info) { |
242 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 283 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
243 string16 bundle_id = UTF8ToUTF16(base::mac::BaseBundleID()); | 284 string16 bundle_id = UTF8ToUTF16(base::mac::BaseBundleID()); |
244 WebAppShortcutCreator shortcut_creator( | 285 WebAppShortcutCreator shortcut_creator( |
245 web_app_path, shortcut_info, bundle_id); | 286 web_app_path, shortcut_info, bundle_id); |
246 shortcut_creator.CreateShortcut(); | 287 shortcut_creator.CreateShortcut(); |
247 } | 288 } |
248 | 289 |
249 } // namespace internals | 290 } // namespace internals |
250 } // namespace web_app | 291 } // namespace web_app |
OLD | NEW |