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 #include "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 15 matching lines...) Expand all Loading... | |
26 // Creates a NSBitmapImageRep from |bitmap|. | 26 // Creates a NSBitmapImageRep from |bitmap|. |
27 NSBitmapImageRep* SkBitmapToImageRep(const SkBitmap& bitmap) { | 27 NSBitmapImageRep* SkBitmapToImageRep(const SkBitmap& bitmap) { |
28 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( | 28 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
29 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | 29 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
30 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace( | 30 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace( |
31 bitmap, color_space.get()); | 31 bitmap, color_space.get()); |
32 return base::mac::ObjCCast<NSBitmapImageRep>( | 32 return base::mac::ObjCCast<NSBitmapImageRep>( |
33 [[image representations] lastObject]); | 33 [[image representations] lastObject]); |
34 } | 34 } |
35 | 35 |
36 // Adds |image_rep| to |icon_family|. Returns true on success, false on failure. | |
37 bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, | |
38 NSBitmapImageRep* image_rep) { | |
39 NSSize size = [image_rep size]; | |
40 if (size.width != size.height) | |
41 return false; | |
42 | |
43 switch ((int)size.width) { | |
Finnur
2012/02/28 10:26:51
Style guide frowns upon this casting style: (int)
sail
2012/02/28 23:25:36
Done.
| |
44 case 512: | |
45 return [icon_family setIconFamilyElement:kIconServices512PixelDataARGB | |
46 fromBitmapImageRep:image_rep]; | |
47 case 256: | |
48 return [icon_family setIconFamilyElement:kIconServices256PixelDataARGB | |
49 fromBitmapImageRep:image_rep]; | |
50 case 128: | |
51 return [icon_family setIconFamilyElement:kThumbnail32BitData | |
52 fromBitmapImageRep:image_rep] && | |
53 [icon_family setIconFamilyElement:kThumbnail8BitMask | |
54 fromBitmapImageRep:image_rep]; | |
55 case 32: | |
56 return [icon_family setIconFamilyElement:kLarge32BitData | |
57 fromBitmapImageRep:image_rep] && | |
58 [icon_family setIconFamilyElement:kLarge8BitData | |
59 fromBitmapImageRep:image_rep] && | |
60 [icon_family setIconFamilyElement:kLarge8BitMask | |
61 fromBitmapImageRep:image_rep] && | |
62 [icon_family setIconFamilyElement:kLarge1BitMask | |
63 fromBitmapImageRep:image_rep]; | |
64 case 16: | |
65 return [icon_family setIconFamilyElement:kSmall32BitData | |
66 fromBitmapImageRep:image_rep] && | |
67 [icon_family setIconFamilyElement:kSmall8BitData | |
68 fromBitmapImageRep:image_rep] && | |
69 [icon_family setIconFamilyElement:kSmall8BitMask | |
70 fromBitmapImageRep:image_rep] && | |
71 [icon_family setIconFamilyElement:kSmall1BitMask | |
72 fromBitmapImageRep:image_rep]; | |
73 default: | |
74 return false; | |
75 } | |
76 } | |
77 | |
36 } // namespace | 78 } // namespace |
37 | 79 |
38 | 80 |
39 namespace web_app { | 81 namespace web_app { |
40 | 82 |
41 WebAppShortcutCreator::WebAppShortcutCreator( | 83 WebAppShortcutCreator::WebAppShortcutCreator( |
42 const FilePath& user_data_dir, | 84 const FilePath& user_data_dir, |
43 const ShellIntegration::ShortcutInfo& shortcut_info) | 85 const ShellIntegration::ShortcutInfo& shortcut_info) |
44 : user_data_dir_(user_data_dir), | 86 : user_data_dir_(user_data_dir), |
45 info_(shortcut_info) { | 87 info_(shortcut_info) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) | 158 [dict setObject:base::SysUTF8ToNSString(info_.url.spec()) |
117 forKey:app_mode::kCrAppModeShortcutURLKey]; | 159 forKey:app_mode::kCrAppModeShortcutURLKey]; |
118 [dict setObject:base::mac::FilePathToNSString(user_data_dir_) | 160 [dict setObject:base::mac::FilePathToNSString(user_data_dir_) |
119 forKey:app_mode::kCrAppModeUserDataDirKey]; | 161 forKey:app_mode::kCrAppModeUserDataDirKey]; |
120 [dict setObject:base::mac::FilePathToNSString(info_.extension_path) | 162 [dict setObject:base::mac::FilePathToNSString(info_.extension_path) |
121 forKey:app_mode::kCrAppModeExtensionPathKey]; | 163 forKey:app_mode::kCrAppModeExtensionPathKey]; |
122 return [dict writeToFile:plist_path atomically:YES]; | 164 return [dict writeToFile:plist_path atomically:YES]; |
123 } | 165 } |
124 | 166 |
125 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { | 167 bool WebAppShortcutCreator::UpdateIcon(const FilePath& app_path) const { |
126 // TODO(sail): Add support for multiple icon sizes. | 168 if (info_.favicon.IsEmpty()) |
127 if (info_.favicon.empty() || info_.favicon.width() != 32 || | |
128 info_.favicon.height() != 32) { | |
129 return true; | 169 return true; |
130 } | |
131 | |
132 NSBitmapImageRep* image_rep = SkBitmapToImageRep(info_.favicon); | |
133 if (!image_rep) | |
134 return false; | |
135 | 170 |
136 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | 171 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); |
137 bool success = [icon_family setIconFamilyElement:kLarge32BitData | 172 bool image_added = false; |
138 fromBitmapImageRep:image_rep] && | 173 for (size_t i = 0; i < info_.favicon.GetNumberOfSkBitmaps(); ++i) { |
139 [icon_family setIconFamilyElement:kLarge8BitData | 174 NSBitmapImageRep* image_rep = |
140 fromBitmapImageRep:image_rep] && | 175 SkBitmapToImageRep(*info_.favicon.GetSkBitmapAtIndex(i)); |
141 [icon_family setIconFamilyElement:kLarge8BitMask | 176 if (!image_rep) |
142 fromBitmapImageRep:image_rep] && | 177 continue; |
143 [icon_family setIconFamilyElement:kLarge1BitMask | 178 |
144 fromBitmapImageRep:image_rep]; | 179 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) |
145 if (!success) | 180 continue; |
146 return false; | 181 |
182 image_added = true; | |
183 } | |
147 | 184 |
148 FilePath resources_path = app_path.Append("Contents").Append("Resources"); | 185 FilePath resources_path = app_path.Append("Contents").Append("Resources"); |
149 if (!file_util::CreateDirectory(resources_path)) | 186 if (!file_util::CreateDirectory(resources_path)) |
150 return false; | 187 return false; |
151 FilePath icon_path = resources_path.Append("app.icns"); | 188 FilePath icon_path = resources_path.Append("app.icns"); |
152 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 189 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; |
153 } | 190 } |
154 | 191 |
155 } // namespace | 192 } // namespace |
156 | 193 |
157 namespace web_app { | 194 namespace web_app { |
158 namespace internals { | 195 namespace internals { |
159 | 196 |
160 void CreateShortcutTask(const FilePath& web_app_path, | 197 void CreateShortcutTask(const FilePath& web_app_path, |
161 const FilePath& profile_path, | 198 const FilePath& profile_path, |
162 const ShellIntegration::ShortcutInfo& shortcut_info) { | 199 const ShellIntegration::ShortcutInfo& shortcut_info) { |
163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 200 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
164 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info); | 201 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info); |
165 shortcut_creator.CreateShortcut(); | 202 shortcut_creator.CreateShortcut(); |
166 } | 203 } |
167 | 204 |
168 } // namespace internals | 205 } // namespace internals |
169 } // namespace web_app | 206 } // namespace web_app |
OLD | NEW |