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)) {} |
sail
2013/05/29 00:31:37
Add a DCHECK(handle_) and DCHECK(!MemError()) here
tapted
2013/05/29 01:37:36
Done.
| |
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(4u * bitmap.width() * bitmap.height(), handle->HandleSize()); | |
60 | |
61 char* argb = handle->Data(); | |
62 SkAutoLockPixels lock(bitmap); | |
63 for (int y = 0; y < bitmap.height(); ++y) { | |
64 for (int x = 0; x < bitmap.width(); ++x) { | |
65 SkColor pixel = bitmap.getColor(x, y); | |
66 argb[0] = SkColorGetA(pixel); | |
67 argb[1] = SkColorGetR(pixel); | |
68 argb[2] = SkColorGetG(pixel); | |
69 argb[3] = SkColorGetB(pixel); | |
70 argb += 4; | |
41 } | 71 } |
42 } | 72 } |
43 return nil; | |
44 } | 73 } |
45 | 74 |
46 // Adds |image_rep| to |icon_family|. Returns true on success, false on failure. | 75 // Adds |image| to |icon_family|. Returns true on success, false on failure. |
47 bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, | 76 bool AddGfxImageToIconFamily(IconFamilyHandle icon_family, |
48 NSBitmapImageRep* image_rep) { | 77 const gfx::Image& image) { |
49 NSSize size = [image_rep size]; | 78 // When called via ShowCreateChromeAppShortcutsDialog the ImageFamily will |
50 if (size.width != size.height) | 79 // have all the representations desired here for mac, from the kDesiredSizes |
80 // array in web_app_ui.cc. | |
81 SkBitmap bitmap = image.AsBitmap(); | |
82 if (bitmap.config() != SkBitmap::kARGB_8888_Config || | |
83 bitmap.width() != bitmap.height()) { | |
51 return false; | 84 return false; |
85 } | |
52 | 86 |
53 switch (static_cast<int>(size.width)) { | 87 OSType icon_type; |
88 switch (bitmap.width()) { | |
54 case 512: | 89 case 512: |
55 return [icon_family setIconFamilyElement:kIconServices512PixelDataARGB | 90 icon_type = kIconServices512PixelDataARGB; |
56 fromBitmapImageRep:image_rep]; | 91 break; |
57 case 256: | 92 case 256: |
58 return [icon_family setIconFamilyElement:kIconServices256PixelDataARGB | 93 icon_type = kIconServices256PixelDataARGB; |
59 fromBitmapImageRep:image_rep]; | 94 break; |
60 case 128: | 95 case 128: |
61 return [icon_family setIconFamilyElement:kThumbnail32BitData | 96 icon_type = kIconServices128PixelDataARGB; |
62 fromBitmapImageRep:image_rep] && | 97 break; |
63 [icon_family setIconFamilyElement:kThumbnail8BitMask | 98 case 48: |
64 fromBitmapImageRep:image_rep]; | 99 icon_type = kIconServices48PixelDataARGB; |
100 break; | |
65 case 32: | 101 case 32: |
66 return [icon_family setIconFamilyElement:kLarge32BitData | 102 icon_type = kIconServices32PixelDataARGB; |
67 fromBitmapImageRep:image_rep] && | 103 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: | 104 case 16: |
75 return [icon_family setIconFamilyElement:kSmall32BitData | 105 icon_type = kIconServices16PixelDataARGB; |
76 fromBitmapImageRep:image_rep] && | 106 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: | 107 default: |
84 return false; | 108 return false; |
85 } | 109 } |
110 | |
111 ScopedCarbonHandle raw_data(bitmap.getSize()); | |
112 ConvertSkiaToARGB(bitmap, &raw_data); | |
113 return SetIconFamilyData(icon_family, icon_type, raw_data.Get()) == noErr; | |
sail
2013/05/29 00:31:37
DCHECK !error here?
tapted
2013/05/29 01:37:36
Done.
| |
86 } | 114 } |
87 | 115 |
88 base::FilePath GetWritableApplicationsDirectory() { | 116 base::FilePath GetWritableApplicationsDirectory() { |
89 base::FilePath path; | 117 base::FilePath path; |
90 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | 118 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
91 file_util::PathIsWritable(path)) { | 119 file_util::PathIsWritable(path)) { |
92 return path; | 120 return path; |
93 } | 121 } |
94 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | 122 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
95 return path; | 123 return path; |
96 return base::FilePath(); | 124 return base::FilePath(); |
97 } | 125 } |
98 | 126 |
99 } // namespace | 127 } // namespace |
100 | 128 |
101 | |
102 namespace web_app { | 129 namespace web_app { |
103 | 130 |
104 const char kChromeAppDirName[] = "Chrome Apps.localized"; | 131 const char kChromeAppDirName[] = "Chrome Apps.localized"; |
105 | 132 |
106 WebAppShortcutCreator::WebAppShortcutCreator( | 133 WebAppShortcutCreator::WebAppShortcutCreator( |
107 const base::FilePath& user_data_dir, | 134 const base::FilePath& user_data_dir, |
108 const ShellIntegration::ShortcutInfo& shortcut_info, | 135 const ShellIntegration::ShortcutInfo& shortcut_info, |
109 const string16& chrome_bundle_id) | 136 const string16& chrome_bundle_id) |
110 : user_data_dir_(user_data_dir), | 137 : user_data_dir_(user_data_dir), |
111 info_(shortcut_info), | 138 info_(shortcut_info), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 forKey:app_mode::kCrAppModeUserDataDirKey]; | 257 forKey:app_mode::kCrAppModeUserDataDirKey]; |
231 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) | 258 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) |
232 forKey:app_mode::kCrAppModeProfileDirKey]; | 259 forKey:app_mode::kCrAppModeProfileDirKey]; |
233 return [plist writeToFile:plist_path atomically:YES]; | 260 return [plist writeToFile:plist_path atomically:YES]; |
234 } | 261 } |
235 | 262 |
236 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { | 263 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { |
237 if (info_.favicon.empty()) | 264 if (info_.favicon.empty()) |
238 return true; | 265 return true; |
239 | 266 |
240 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | 267 ScopedCarbonHandle icon_family(0); |
241 bool image_added = false; | 268 bool image_added = false; |
242 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); | 269 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); |
243 it != info_.favicon.end(); ++it) { | 270 it != info_.favicon.end(); ++it) { |
244 if (it->IsEmpty()) | 271 if (it->IsEmpty()) |
245 continue; | 272 continue; |
246 NSBitmapImageRep* image_rep = NSImageGet100PRepresentation(it->ToNSImage()); | |
247 if (!image_rep) | |
248 continue; | |
249 | 273 |
250 // Missing an icon size is not fatal so don't fail if adding the bitmap | 274 // Missing an icon size is not fatal so don't fail if adding the bitmap |
251 // doesn't work. | 275 // doesn't work. |
252 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) | 276 if (!AddGfxImageToIconFamily(icon_family.GetAs<IconFamilyHandle>(), *it)) |
253 continue; | 277 continue; |
254 | 278 |
255 image_added = true; | 279 image_added = true; |
256 } | 280 } |
257 | 281 |
258 if (!image_added) | 282 if (!image_added) |
259 return false; | 283 return false; |
260 | 284 |
261 base::FilePath resources_path = | 285 base::FilePath resources_path = |
262 app_path.Append("Contents").Append("Resources"); | 286 app_path.Append("Contents").Append("Resources"); |
263 if (!file_util::CreateDirectory(resources_path)) | 287 if (!file_util::CreateDirectory(resources_path)) |
264 return false; | 288 return false; |
265 base::FilePath icon_path = resources_path.Append("app.icns"); | 289 |
266 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 290 return icon_family.WriteDataToFile(resources_path.Append("app.icns")); |
267 } | 291 } |
268 | 292 |
269 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const | 293 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const |
270 { | 294 { |
271 NSString* bundle_id_template = | 295 NSString* bundle_id_template = |
272 base::mac::ObjCCast<NSString>( | 296 base::mac::ObjCCast<NSString>( |
273 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); | 297 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); |
274 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); | 298 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); |
275 NSString* placeholder = | 299 NSString* placeholder = |
276 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; | 300 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
344 void UpdatePlatformShortcuts( | 368 void UpdatePlatformShortcuts( |
345 const base::FilePath& web_app_path, | 369 const base::FilePath& web_app_path, |
346 const ShellIntegration::ShortcutInfo& shortcut_info) { | 370 const ShellIntegration::ShortcutInfo& shortcut_info) { |
347 // TODO(benwells): Implement this when shortcuts / weblings are enabled on | 371 // TODO(benwells): Implement this when shortcuts / weblings are enabled on |
348 // mac. | 372 // mac. |
349 } | 373 } |
350 | 374 |
351 } // namespace internals | 375 } // namespace internals |
352 | 376 |
353 } // namespace web_app | 377 } // namespace web_app |
OLD | NEW |