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