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 <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/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
11 #include "base/mac/bundle_locations.h" | 11 #include "base/mac/bundle_locations.h" |
12 #include "base/mac/foundation_util.h" | 12 #include "base/mac/foundation_util.h" |
13 #include "base/mac/mac_logging.h" | 13 #include "base/mac/mac_logging.h" |
14 #include "base/mac/mac_util.h" | 14 #include "base/mac/mac_util.h" |
15 #include "base/mac/scoped_cftyperef.h" | 15 #include "base/mac/scoped_cftyperef.h" |
16 #include "base/memory/scoped_nsobject.h" | 16 #include "base/memory/scoped_nsobject.h" |
17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "chrome/browser/web_applications/web_app.h" | 19 #include "chrome/browser/web_applications/web_app.h" |
20 #include "chrome/common/chrome_paths_internal.h" | 20 #include "chrome/common/chrome_paths_internal.h" |
21 #include "chrome/common/mac/app_mode_common.h" | 21 #include "chrome/common/mac/app_mode_common.h" |
22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
23 #include "grit/chromium_strings.h" | 23 #include "grit/chromium_strings.h" |
24 #include "skia/ext/skia_utils_mac.h" | 24 #include "skia/ext/skia_utils_mac.h" |
25 #include "third_party/icon_family/IconFamily.h" | 25 #include "third_party/icon_family/IconFamily.h" |
26 #include "ui/base/l10n/l10n_util_mac.h" | 26 #include "ui/base/l10n/l10n_util_mac.h" |
27 #include "ui/gfx/image/image_skia.h" | 27 #include "ui/gfx/image/image_family.h" |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 // Creates a NSBitmapImageRep from |bitmap|. | 31 // Get the 100% image representation for |image|. |
32 NSBitmapImageRep* SkBitmapToImageRep(const SkBitmap& bitmap) { | 32 // This returns the representation with the same width and height as |image| |
33 base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( | 33 // itself. If there is no such representation, returns nil. |
34 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | 34 NSBitmapImageRep* NSImageGet100PRepresentation(NSImage* image) { |
35 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace( | 35 NSSize image_size = [image size]; |
36 bitmap, color_space.get()); | 36 for (NSBitmapImageRep* image_rep in [image representations]) { |
37 return base::mac::ObjCCast<NSBitmapImageRep>( | 37 NSSize image_rep_size = [image_rep size]; |
38 [[image representations] lastObject]); | 38 if (image_rep_size.width == image_size.width && |
| 39 image_rep_size.height == image_size.height) { |
| 40 return image_rep; |
| 41 } |
| 42 } |
| 43 return nil; |
39 } | 44 } |
40 | 45 |
41 // Adds |image_rep| to |icon_family|. Returns true on success, false on failure. | 46 // Adds |image_rep| to |icon_family|. Returns true on success, false on failure. |
42 bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, | 47 bool AddBitmapImageRepToIconFamily(IconFamily* icon_family, |
43 NSBitmapImageRep* image_rep) { | 48 NSBitmapImageRep* image_rep) { |
44 NSSize size = [image_rep size]; | 49 NSSize size = [image_rep size]; |
45 if (size.width != size.height) | 50 if (size.width != size.height) |
46 return false; | 51 return false; |
47 | 52 |
48 switch (static_cast<int>(size.width)) { | 53 switch (static_cast<int>(size.width)) { |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 [plist setObject:GetBundleIdentifier(plist) | 218 [plist setObject:GetBundleIdentifier(plist) |
214 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; | 219 forKey:base::mac::CFToNSCast(kCFBundleIdentifierKey)]; |
215 [plist setObject:base::mac::FilePathToNSString(user_data_dir_) | 220 [plist setObject:base::mac::FilePathToNSString(user_data_dir_) |
216 forKey:app_mode::kCrAppModeUserDataDirKey]; | 221 forKey:app_mode::kCrAppModeUserDataDirKey]; |
217 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) | 222 [plist setObject:base::mac::FilePathToNSString(info_.profile_path.BaseName()) |
218 forKey:app_mode::kCrAppModeProfileDirKey]; | 223 forKey:app_mode::kCrAppModeProfileDirKey]; |
219 return [plist writeToFile:plist_path atomically:YES]; | 224 return [plist writeToFile:plist_path atomically:YES]; |
220 } | 225 } |
221 | 226 |
222 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { | 227 bool WebAppShortcutCreator::UpdateIcon(const base::FilePath& app_path) const { |
223 if (info_.favicon.IsEmpty()) | 228 if (info_.favicon.empty()) |
224 return true; | 229 return true; |
225 | 230 |
226 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); | 231 scoped_nsobject<IconFamily> icon_family([[IconFamily alloc] init]); |
227 bool image_added = false; | 232 bool image_added = false; |
228 info_.favicon.ToImageSkia()->EnsureRepsForSupportedScaleFactors(); | 233 for (gfx::ImageFamily::const_iterator it = info_.favicon.begin(); |
229 std::vector<gfx::ImageSkiaRep> image_reps = | 234 it != info_.favicon.end(); ++it) { |
230 info_.favicon.ToImageSkia()->image_reps(); | 235 if (it->IsEmpty()) |
231 for (size_t i = 0; i < image_reps.size(); ++i) { | 236 continue; |
232 NSBitmapImageRep* image_rep = SkBitmapToImageRep( | 237 NSBitmapImageRep* image_rep = NSImageGet100PRepresentation(it->ToNSImage()); |
233 image_reps[i].sk_bitmap()); | |
234 if (!image_rep) | 238 if (!image_rep) |
235 continue; | 239 continue; |
236 | 240 |
237 // Missing an icon size is not fatal so don't fail if adding the bitmap | 241 // Missing an icon size is not fatal so don't fail if adding the bitmap |
238 // doesn't work. | 242 // doesn't work. |
239 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) | 243 if (!AddBitmapImageRepToIconFamily(icon_family, image_rep)) |
240 continue; | 244 continue; |
241 | 245 |
242 image_added = true; | 246 image_added = true; |
243 } | 247 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 void UpdatePlatformShortcuts( | 327 void UpdatePlatformShortcuts( |
324 const base::FilePath& web_app_path, | 328 const base::FilePath& web_app_path, |
325 const ShellIntegration::ShortcutInfo& shortcut_info) { | 329 const ShellIntegration::ShortcutInfo& shortcut_info) { |
326 // TODO(benwells): Implement this when shortcuts / weblings are enabled on | 330 // TODO(benwells): Implement this when shortcuts / weblings are enabled on |
327 // mac. | 331 // mac. |
328 } | 332 } |
329 | 333 |
330 } // namespace internals | 334 } // namespace internals |
331 | 335 |
332 } // namespace web_app | 336 } // namespace web_app |
OLD | NEW |