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 <Carbon/Carbon.h> | |
7 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
8 | 9 |
9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
11 #include "base/mac/bundle_locations.h" | 12 #include "base/mac/bundle_locations.h" |
12 #include "base/mac/foundation_util.h" | 13 #include "base/mac/foundation_util.h" |
13 #include "base/mac/mac_logging.h" | 14 #include "base/mac/mac_logging.h" |
14 #include "base/mac/mac_util.h" | 15 #include "base/mac/mac_util.h" |
15 #include "base/mac/scoped_cftyperef.h" | 16 #include "base/mac/scoped_cftyperef.h" |
16 #include "base/memory/scoped_nsobject.h" | 17 #include "base/memory/scoped_nsobject.h" |
17 #include "base/strings/sys_string_conversions.h" | 18 #include "base/strings/sys_string_conversions.h" |
18 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
19 #include "chrome/browser/web_applications/web_app.h" | 20 #include "chrome/browser/web_applications/web_app.h" |
20 #include "chrome/common/chrome_paths_internal.h" | 21 #include "chrome/common/chrome_paths_internal.h" |
21 #include "chrome/common/mac/app_mode_common.h" | 22 #include "chrome/common/mac/app_mode_common.h" |
22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
23 #include "grit/chromium_strings.h" | 24 #include "grit/chromium_strings.h" |
24 #include "skia/ext/skia_utils_mac.h" | 25 #include "skia/ext/skia_utils_mac.h" |
25 #include "third_party/icon_family/IconFamily.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
27 #include "third_party/skia/include/core/SkColor.h" | |
26 #include "ui/base/l10n/l10n_util_mac.h" | 28 #include "ui/base/l10n/l10n_util_mac.h" |
27 #include "ui/gfx/image/image_family.h" | 29 #include "ui/gfx/image/image_family.h" |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 // Get the 100% image representation for |image|. | 33 class ScopedCarbonHandle { |
32 // This returns the representation with the same width and height as |image| | 34 public: |
33 // itself. If there is no such representation, returns nil. | 35 ScopedCarbonHandle(size_t initial_size) : handle_(NewHandle(initial_size)) {} |
34 NSBitmapImageRep* NSImageGet100PRepresentation(NSImage* image) { | 36 ~ScopedCarbonHandle() { DisposeHandle(handle_); } |
35 NSSize image_size = [image size]; | 37 |
36 for (NSBitmapImageRep* image_rep in [image representations]) { | 38 Handle Get() { return handle_; } |
37 NSSize image_rep_size = [image_rep size]; | 39 char* Data() { return *handle_; } |
38 if (image_rep_size.width == image_size.width && | 40 size_t HandleSize() const { return GetHandleSize(handle_); } |
39 image_rep_size.height == image_size.height) { | 41 |
40 return image_rep; | 42 template <class GetAsType> |
43 GetAsType GetAs() { | |
44 return reinterpret_cast<GetAsType>(handle_); | |
45 } | |
46 | |
47 bool WriteDataToFile(const base::FilePath& path) { | |
48 NSData* data = [NSData dataWithBytes:Data() | |
49 length:HandleSize()]; | |
50 return [data writeToFile:base::mac::FilePathToNSString(path) | |
51 atomically:NO]; | |
52 } | |
53 | |
54 private: | |
55 Handle handle_; | |
56 }; | |
57 | |
58 void ConvertSkiaToARGB(const SkBitmap& bitmap, ScopedCarbonHandle* handle) { | |
59 CHECK_EQ(handle->HandleSize(), bitmap.getSize()); | |
60 CHECK_EQ(4u * bitmap.width(), bitmap.rowBytes()); | |
61 CHECK_EQ(bitmap.rowBytes() * bitmap.height(), bitmap.getSize()); | |
benwells
2013/05/23 04:15:24
Maybe just CHECK_EQ(bitmap.width() * bitmap.height
tapted
2013/05/23 04:30:02
Done.
| |
62 | |
63 char* argb = handle->Data(); | |
64 SkAutoLockPixels lock(bitmap); | |
65 for (int y = 0; y < bitmap.height(); ++y) { | |
66 for (int x = 0; x < bitmap.width(); ++x) { | |
67 SkColor pixel = bitmap.getColor(x, y); // Unpremultiplies. | |
benwells
2013/05/23 04:15:24
Nit: I think the comment is unnecessary and implie
tapted
2013/05/23 04:30:02
Done.
| |
68 argb[0] = SkColorGetA(pixel); | |
69 argb[1] = SkColorGetR(pixel); | |
70 argb[2] = SkColorGetG(pixel); | |
71 argb[3] = SkColorGetB(pixel); | |
72 argb += 4; | |
41 } | 73 } |
42 } | 74 } |
43 return nil; | |
44 } | 75 } |
45 | 76 |
46 // Adds |image_rep| to |icon_family|. Returns true on success, false on failure. | 77 // Adds |image| to |icon_family|. Returns true on success, false on failure. |
47 bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, | 78 bool AddGfxImageToIconFamily(IconFamilyHandle icon_family, |
48 NSBitmapImageRep* image_rep) { | 79 const gfx::Image& image) { |
49 NSSize size = [image_rep size]; | 80 // When called via ShowCreateChromeAppShortcutsDialog the ImageFamily will |
50 if (size.width != size.height) | 81 // have all the representations desired here for mac, from the kDesiredSizes |
82 // array in web_app_ui.cc. | |
83 SkBitmap bitmap = image.AsBitmap(); | |
84 if (bitmap.config() != SkBitmap::kARGB_8888_Config || | |
85 bitmap.width() != bitmap.height()) { | |
51 return false; | 86 return false; |
87 } | |
52 | 88 |
53 switch (static_cast<int>(size.width)) { | 89 OSType icon_type; |
90 switch (bitmap.width()) { | |
54 case 512: | 91 case 512: |
55 return [icon_family setIconFamilyElement:kIconServices512PixelDataARGB | 92 icon_type = kIconServices512PixelDataARGB; |
56 fromBitmapImageRep:image_rep]; | 93 break; |
57 case 256: | 94 case 256: |
58 return [icon_family setIconFamilyElement:kIconServices256PixelDataARGB | 95 icon_type = kIconServices256PixelDataARGB; |
59 fromBitmapImageRep:image_rep]; | 96 break; |
60 case 128: | 97 case 128: |
61 return [icon_family setIconFamilyElement:kThumbnail32BitData | 98 icon_type = kIconServices128PixelDataARGB; |
62 fromBitmapImageRep:image_rep] && | 99 break; |
63 [icon_family setIconFamilyElement:kThumbnail8BitMask | 100 case 48: |
64 fromBitmapImageRep:image_rep]; | 101 icon_type = kIconServices48PixelDataARGB; |
102 break; | |
65 case 32: | 103 case 32: |
66 return [icon_family setIconFamilyElement:kLarge32BitData | 104 icon_type = kIconServices32PixelDataARGB; |
67 fromBitmapImageRep:image_rep] && | 105 break; |
68 [icon_family setIconFamilyElement:kLarge8BitData | |
69 fromBitmapImageRep:image_rep] && | |
70 [icon_family setIconFamilyElement:kLarge8BitMask | |
71 fromBitmapImageRep:image_rep] && | |
72 [icon_family setIconFamilyElement:kLarge1BitMask | |
73 fromBitmapImageRep:image_rep]; | |
74 case 16: | 106 case 16: |
75 return [icon_family setIconFamilyElement:kSmall32BitData | 107 icon_type = kIconServices16PixelDataARGB; |
76 fromBitmapImageRep:image_rep] && | 108 break; |
77 [icon_family setIconFamilyElement:kSmall8BitData | |
78 fromBitmapImageRep:image_rep] && | |
79 [icon_family setIconFamilyElement:kSmall8BitMask | |
80 fromBitmapImageRep:image_rep] && | |
81 [icon_family setIconFamilyElement:kSmall1BitMask | |
82 fromBitmapImageRep:image_rep]; | |
83 default: | 109 default: |
84 return false; | 110 return false; |
85 } | 111 } |
112 | |
113 ScopedCarbonHandle raw_data(bitmap.getSize()); | |
114 ConvertSkiaToARGB(bitmap, &raw_data); | |
115 return SetIconFamilyData(icon_family, icon_type, raw_data.Get()) == noErr; | |
86 } | 116 } |
87 | 117 |
88 base::FilePath GetWritableApplicationsDirectory() { | 118 base::FilePath GetWritableApplicationsDirectory() { |
89 base::FilePath path; | 119 base::FilePath path; |
90 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | 120 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
91 file_util::PathIsWritable(path)) { | 121 file_util::PathIsWritable(path)) { |
92 return path; | 122 return path; |
93 } | 123 } |
94 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | 124 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
95 return path; | 125 return path; |
96 return base::FilePath(); | 126 return base::FilePath(); |
97 } | 127 } |
98 | 128 |
99 } // namespace | 129 } // namespace |
100 | 130 |
101 | |
102 namespace web_app { | 131 namespace web_app { |
103 | 132 |
104 const char kChromeAppDirName[] = "Chrome Apps.localized"; | 133 const char kChromeAppDirName[] = "Chrome Apps.localized"; |
105 | 134 |
106 WebAppShortcutCreator::WebAppShortcutCreator( | 135 WebAppShortcutCreator::WebAppShortcutCreator( |
107 const base::FilePath& user_data_dir, | 136 const base::FilePath& user_data_dir, |
108 const ShellIntegration::ShortcutInfo& shortcut_info, | 137 const ShellIntegration::ShortcutInfo& shortcut_info, |
109 const string16& chrome_bundle_id) | 138 const string16& chrome_bundle_id) |
110 : user_data_dir_(user_data_dir), | 139 : user_data_dir_(user_data_dir), |
111 info_(shortcut_info), | 140 info_(shortcut_info), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 forKey:app_mode::kCrAppModeUserDataDirKey]; | 259 forKey:app_mode::kCrAppModeUserDataDirKey]; |
231 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) | 260 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) |
232 forKey:app_mode::kCrAppModeProfileDirKey]; | 261 forKey:app_mode::kCrAppModeProfileDirKey]; |
233 return [plist writeToFile:plist_path atomically:YES]; | 262 return [plist writeToFile:plist_path atomically:YES]; |
234 } | 263 } |
235 | 264 |
236 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { | 265 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { |
237 if (info_.favicon.empty()) | 266 if (info_.favicon.empty()) |
238 return true; | 267 return true; |
239 | 268 |
240 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | 269 ScopedCarbonHandle icon_family(0); |
241 bool image_added = false; | 270 bool image_added = false; |
242 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); | 271 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); |
243 it != info_.favicon.end(); ++it) { | 272 it != info_.favicon.end(); ++it) { |
244 if (it->IsEmpty()) | 273 if (it->IsEmpty()) |
245 continue; | 274 continue; |
246 NSBitmapImageRep* image_rep = NSImageGet100PRepresentation(it->ToNSImage()); | |
247 if (!image_rep) | |
248 continue; | |
249 | 275 |
250 // Missing an icon size is not fatal so don't fail if adding the bitmap | 276 // Missing an icon size is not fatal so don't fail if adding the bitmap |
251 // doesn't work. | 277 // doesn't work. |
252 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) | 278 if (!AddGfxImageToIconFamily(icon_family.GetAs<IconFamilyHandle>(), *it)) |
253 continue; | 279 continue; |
254 | 280 |
255 image_added = true; | 281 image_added = true; |
256 } | 282 } |
257 | 283 |
258 if (!image_added) | 284 if (!image_added) |
259 return false; | 285 return false; |
260 | 286 |
261 base::FilePath resources_path = | 287 base::FilePath resources_path = |
262 app_path.Append("Contents").Append("Resources"); | 288 app_path.Append("Contents").Append("Resources"); |
263 if (!file_util::CreateDirectory(resources_path)) | 289 if (!file_util::CreateDirectory(resources_path)) |
264 return false; | 290 return false; |
265 base::FilePath icon_path = resources_path.Append("app.icns"); | 291 |
266 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 292 return icon_family.WriteDataToFile(resources_path.Append("app.icns")); |
267 } | 293 } |
268 | 294 |
269 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const | 295 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const |
270 { | 296 { |
271 NSString* bundle_id_template = | 297 NSString* bundle_id_template = |
272 base::mac::ObjCCast<NSString>( | 298 base::mac::ObjCCast<NSString>( |
273 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); | 299 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); |
274 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); | 300 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); |
275 NSString* placeholder = | 301 NSString* placeholder = |
276 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; | 302 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 void UpdatePlatformShortcuts( | 370 void UpdatePlatformShortcuts( |
345 const base::FilePath& web_app_path, | 371 const base::FilePath& web_app_path, |
346 const ShellIntegration::ShortcutInfo& shortcut_info) { | 372 const ShellIntegration::ShortcutInfo& shortcut_info) { |
347 // TODO(benwells): Implement this when shortcuts / weblings are enabled on | 373 // TODO(benwells): Implement this when shortcuts / weblings are enabled on |
348 // mac. | 374 // mac. |
349 } | 375 } |
350 | 376 |
351 } // namespace internals | 377 } // namespace internals |
352 | 378 |
353 } // namespace web_app | 379 } // namespace web_app |
OLD | NEW |