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 (static_cast<int>(size.width)) { |
| 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; |
| 170 |
| 171 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); |
| 172 bool image_added = false; |
| 173 for (size_t i = 0; i < info_.favicon.GetNumberOfSkBitmaps(); ++i) { |
| 174 NSBitmapImageRep* image_rep = |
| 175 SkBitmapToImageRep(*info_.favicon.GetSkBitmapAtIndex(i)); |
| 176 if (!image_rep) |
| 177 continue; |
| 178 |
| 179 // Missing an icon size is not fatal so don't fail if adding the bitmap |
| 180 // doesn't work. |
| 181 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) |
| 182 continue; |
| 183 |
| 184 image_added = true; |
130 } | 185 } |
131 | 186 |
132 NSBitmapImageRep* image_rep = SkBitmapToImageRep(info_.favicon); | 187 if (!image_added) |
133 if (!image_rep) | |
134 return false; | |
135 | |
136 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | |
137 bool success = [icon_family setIconFamilyElement:kLarge32BitData | |
138 fromBitmapImageRep:image_rep] && | |
139 [icon_family setIconFamilyElement:kLarge8BitData | |
140 fromBitmapImageRep:image_rep] && | |
141 [icon_family setIconFamilyElement:kLarge8BitMask | |
142 fromBitmapImageRep:image_rep] && | |
143 [icon_family setIconFamilyElement:kLarge1BitMask | |
144 fromBitmapImageRep:image_rep]; | |
145 if (!success) | |
146 return false; | 188 return false; |
147 | 189 |
148 FilePath resources_path = app_path.Append("Contents").Append("Resources"); | 190 FilePath resources_path = app_path.Append("Contents").Append("Resources"); |
149 if (!file_util::CreateDirectory(resources_path)) | 191 if (!file_util::CreateDirectory(resources_path)) |
150 return false; | 192 return false; |
151 FilePath icon_path = resources_path.Append("app.icns"); | 193 FilePath icon_path = resources_path.Append("app.icns"); |
152 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 194 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; |
153 } | 195 } |
154 | 196 |
155 } // namespace | 197 } // namespace |
156 | 198 |
157 namespace web_app { | 199 namespace web_app { |
158 namespace internals { | 200 namespace internals { |
159 | 201 |
160 void CreateShortcutTask(const FilePath& web_app_path, | 202 void CreateShortcutTask(const FilePath& web_app_path, |
161 const FilePath& profile_path, | 203 const FilePath& profile_path, |
162 const ShellIntegration::ShortcutInfo& shortcut_info) { | 204 const ShellIntegration::ShortcutInfo& shortcut_info) { |
163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 205 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
164 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info); | 206 WebAppShortcutCreator shortcut_creator(web_app_path, shortcut_info); |
165 shortcut_creator.CreateShortcut(); | 207 shortcut_creator.CreateShortcut(); |
166 } | 208 } |
167 | 209 |
168 } // namespace internals | 210 } // namespace internals |
169 } // namespace web_app | 211 } // namespace web_app |
OLD | NEW |