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 DCHECK(handle_); |
35 NSSize image_size = [image size]; | 37 DCHECK_EQ(noErr, MemError()); |
36 for (NSBitmapImageRep* image_rep in [image representations]) { | 38 } |
37 NSSize image_rep_size = [image_rep size]; | 39 ~ScopedCarbonHandle() { DisposeHandle(handle_); } |
38 if (image_rep_size.width == image_size.width && | 40 |
39 image_rep_size.height == image_size.height) { | 41 Handle Get() { return handle_; } |
40 return image_rep; | 42 char* Data() { return *handle_; } |
| 43 size_t HandleSize() const { return GetHandleSize(handle_); } |
| 44 |
| 45 IconFamilyHandle GetAsIconFamilyHandle() { |
| 46 return reinterpret_cast<IconFamilyHandle>(handle_); |
| 47 } |
| 48 |
| 49 bool WriteDataToFile(const base::FilePath& path) { |
| 50 NSData* data = [NSData dataWithBytes:Data() |
| 51 length:HandleSize()]; |
| 52 return [data writeToFile:base::mac::FilePathToNSString(path) |
| 53 atomically:NO]; |
| 54 } |
| 55 |
| 56 private: |
| 57 Handle handle_; |
| 58 }; |
| 59 |
| 60 void ConvertSkiaToARGB(const SkBitmap& bitmap, ScopedCarbonHandle* handle) { |
| 61 CHECK_EQ(4u * bitmap.width() * bitmap.height(), handle->HandleSize()); |
| 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); |
| 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 OSErr result = SetIconFamilyData(icon_family, icon_type, raw_data.Get()); |
| 116 DCHECK_EQ(noErr, result); |
| 117 return result == noErr; |
86 } | 118 } |
87 | 119 |
88 base::FilePath GetWritableApplicationsDirectory() { | 120 base::FilePath GetWritableApplicationsDirectory() { |
89 base::FilePath path; | 121 base::FilePath path; |
90 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && | 122 if (base::mac::GetLocalDirectory(NSApplicationDirectory, &path) && |
91 file_util::PathIsWritable(path)) { | 123 file_util::PathIsWritable(path)) { |
92 return path; | 124 return path; |
93 } | 125 } |
94 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) | 126 if (base::mac::GetUserDirectory(NSApplicationDirectory, &path)) |
95 return path; | 127 return path; |
96 return base::FilePath(); | 128 return base::FilePath(); |
97 } | 129 } |
98 | 130 |
99 } // namespace | 131 } // namespace |
100 | 132 |
101 | |
102 namespace web_app { | 133 namespace web_app { |
103 | 134 |
104 const char kChromeAppDirName[] = "Chrome Apps.localized"; | 135 const char kChromeAppDirName[] = "Chrome Apps.localized"; |
105 | 136 |
106 WebAppShortcutCreator::WebAppShortcutCreator( | 137 WebAppShortcutCreator::WebAppShortcutCreator( |
107 const base::FilePath& user_data_dir, | 138 const base::FilePath& user_data_dir, |
108 const ShellIntegration::ShortcutInfo& shortcut_info, | 139 const ShellIntegration::ShortcutInfo& shortcut_info, |
109 const string16& chrome_bundle_id) | 140 const string16& chrome_bundle_id) |
110 : user_data_dir_(user_data_dir), | 141 : user_data_dir_(user_data_dir), |
111 info_(shortcut_info), | 142 info_(shortcut_info), |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 forKey:app_mode::kCrAppModeUserDataDirKey]; | 261 forKey:app_mode::kCrAppModeUserDataDirKey]; |
231 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) | 262 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) |
232 forKey:app_mode::kCrAppModeProfileDirKey]; | 263 forKey:app_mode::kCrAppModeProfileDirKey]; |
233 return [plist writeToFile:plist_path atomically:YES]; | 264 return [plist writeToFile:plist_path atomically:YES]; |
234 } | 265 } |
235 | 266 |
236 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { | 267 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { |
237 if (info_.favicon.empty()) | 268 if (info_.favicon.empty()) |
238 return true; | 269 return true; |
239 | 270 |
240 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | 271 ScopedCarbonHandle icon_family(0); |
241 bool image_added = false; | 272 bool image_added = false; |
242 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); | 273 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); |
243 it != info_.favicon.end(); ++it) { | 274 it != info_.favicon.end(); ++it) { |
244 if (it->IsEmpty()) | 275 if (it->IsEmpty()) |
245 continue; | 276 continue; |
246 NSBitmapImageRep* image_rep = NSImageGet100PRepresentation(it->ToNSImage()); | |
247 if (!image_rep) | |
248 continue; | |
249 | 277 |
250 // Missing an icon size is not fatal so don't fail if adding the bitmap | 278 // Missing an icon size is not fatal so don't fail if adding the bitmap |
251 // doesn't work. | 279 // doesn't work. |
252 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) | 280 if (!AddGfxImageToIconFamily(icon_family.GetAsIconFamilyHandle(), *it)) |
253 continue; | 281 continue; |
254 | 282 |
255 image_added = true; | 283 image_added = true; |
256 } | 284 } |
257 | 285 |
258 if (!image_added) | 286 if (!image_added) |
259 return false; | 287 return false; |
260 | 288 |
261 base::FilePath resources_path = | 289 base::FilePath resources_path = |
262 app_path.Append("Contents").Append("Resources"); | 290 app_path.Append("Contents").Append("Resources"); |
263 if (!file_util::CreateDirectory(resources_path)) | 291 if (!file_util::CreateDirectory(resources_path)) |
264 return false; | 292 return false; |
265 base::FilePath icon_path = resources_path.Append("app.icns"); | 293 |
266 return [icon_family writeToFile:base::mac::FilePathToNSString(icon_path)]; | 294 return icon_family.WriteDataToFile(resources_path.Append("app.icns")); |
267 } | 295 } |
268 | 296 |
269 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const | 297 NSString* WebAppShortcutCreator::GetBundleIdentifier(NSDictionary* plist) const |
270 { | 298 { |
271 NSString* bundle_id_template = | 299 NSString* bundle_id_template = |
272 base::mac::ObjCCast<NSString>( | 300 base::mac::ObjCCast<NSString>( |
273 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); | 301 [plist objectForKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]); |
274 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); | 302 NSString* extension_id = base::SysUTF8ToNSString(info_.extension_id); |
275 NSString* placeholder = | 303 NSString* placeholder = |
276 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; | 304 [NSString stringWithFormat:@"@%@@", app_mode::kShortcutIdPlaceholder]; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 void UpdatePlatformShortcuts( | 372 void UpdatePlatformShortcuts( |
345 const base::FilePath& web_app_path, | 373 const base::FilePath& web_app_path, |
346 const ShellIntegration::ShortcutInfo& shortcut_info) { | 374 const ShellIntegration::ShortcutInfo& shortcut_info) { |
347 // TODO(benwells): Implement this when shortcuts / weblings are enabled on | 375 // TODO(benwells): Implement this when shortcuts / weblings are enabled on |
348 // mac. | 376 // mac. |
349 } | 377 } |
350 | 378 |
351 } // namespace internals | 379 } // namespace internals |
352 | 380 |
353 } // namespace web_app | 381 } // namespace web_app |
OLD | NEW |