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 #include "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
| 6 | 6 |
| 7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 37 ofType:@"pak"]; | 37 ofType:@"pak"]; |
| 38 } | 38 } |
| 39 if (!resource_path) | 39 if (!resource_path) |
| 40 return FilePath(); | 40 return FilePath(); |
| 41 return FilePath([resource_path fileSystemRepresentation]); | 41 return FilePath([resource_path fileSystemRepresentation]); |
| 42 } | 42 } |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 void ResourceBundle::LoadCommonResources() { | 46 void ResourceBundle::LoadCommonResources() { |
| 47 AddDataPack(GetResourcesPakFilePath(@"chrome", nil)); | 47 AddCommonDataPack("chrome", GetResourcesPakFilePath(@"chrome", nil)); |
| 48 AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard", nil)); | 48 AddCommonDataPack("theme_resources_standard", |
| 49 AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard", nil)); | 49 GetResourcesPakFilePath(@"theme_resources_standard", nil)); |
| 50 AddCommonDataPack("ui_resources_standard", | |
| 51 GetResourcesPakFilePath(@"ui_resources_standard", nil)); | |
| 50 | 52 |
| 51 // On Windows and ChromeOS we load either the 1x resource or the 2x resource. | 53 // On Windows and ChromeOS we load either the 1x resource or the 2x resource. |
| 52 // On Mac we load both and let the UI framework decide which one to use. | 54 // On Mac we load both and let the UI framework decide which one to use. |
| 53 #if defined(ENABLE_HIDPI) | 55 #if defined(ENABLE_HIDPI) |
| 54 if (base::mac::IsOSLionOrLater()) { | 56 if (base::mac::IsOSLionOrLater()) { |
| 55 AddDataPack(GetResourcesPakFilePath(@"theme_resources_2x", nil)); | 57 AddCommonDataPack("theme_resources_2x", |
| 56 AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard_2x", nil)); | 58 GetResourcesPakFilePath(@"theme_resources_2x", nil)); |
| 57 AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard_2x", nil)); | 59 AddCommonDataPack("theme_resources_standard_2x", |
| 60 GetResourcesPakFilePath(@"theme_resources_standard_2x", nil)); | |
| 61 AddCommonDataPack("ui_resources_standard_2x", | |
| 62 GetResourcesPakFilePath(@"ui_resources_standard_2x", nil)); | |
| 58 } | 63 } |
| 59 #endif | 64 #endif |
| 60 } | 65 } |
| 61 | 66 |
| 62 // static | 67 // static |
| 63 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | 68 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
| 64 NSString* mac_locale = base::SysUTF8ToNSString(app_locale); | 69 NSString* mac_locale = base::SysUTF8ToNSString(app_locale); |
| 65 | 70 |
| 66 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value. | 71 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value. |
| 67 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-" | 72 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-" |
| 68 withString:@"_"]; | 73 withString:@"_"]; |
| 69 | 74 |
| 70 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578). | 75 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578). |
| 71 if ([mac_locale isEqual:@"en_US"]) | 76 if ([mac_locale isEqual:@"en_US"]) |
| 72 mac_locale = @"en"; | 77 mac_locale = @"en"; |
| 73 | 78 |
| 74 return GetResourcesPakFilePath(@"locale", mac_locale); | 79 FilePath locale_file_path = GetResourcesPakFilePath(@"locale", mac_locale); |
| 80 | |
| 81 if (delegate_ && | |
| 82 !delegate_->GetPathForLocalePack(app_locale, &locale_file_path)) { | |
| 83 return FilePath(); | |
| 84 } | |
| 85 | |
| 86 return locale_file_path; | |
| 75 } | 87 } |
| 76 | 88 |
| 77 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { | 89 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { |
| 78 // Flipped images are not used on Mac. | 90 // Flipped images are not used on Mac. |
| 79 DCHECK_EQ(rtl, RTL_DISABLED); | 91 DCHECK_EQ(rtl, RTL_DISABLED); |
| 80 | 92 |
| 81 // Check to see if the image is already in the cache. | 93 // Check to see if the image is already in the cache. |
| 82 { | 94 { |
| 83 base::AutoLock lock(*images_and_fonts_lock_); | 95 base::AutoLock lock(*images_and_fonts_lock_); |
| 84 ImageMap::const_iterator found = images_.find(resource_id); | 96 ImageMap::const_iterator found = images_.find(resource_id); |
| 85 if (found != images_.end()) { | 97 if (found != images_.end()) { |
| 86 if (!found->second->HasRepresentation(gfx::Image::kImageRepCocoa)) { | 98 if (!found->second->HasRepresentation(gfx::Image::kImageRepCocoa)) { |
| 87 DLOG(WARNING) << "ResourceBundle::GetNativeImageNamed() is returning a" | 99 DLOG(WARNING) << "ResourceBundle::GetNativeImageNamed() is returning a" |
| 88 << " cached gfx::Image that isn't backed by an NSImage. The image" | 100 << " cached gfx::Image that isn't backed by an NSImage. The image" |
| 89 << " will be converted, rather than going through the NSImage loader." | 101 << " will be converted, rather than going through the NSImage loader." |
| 90 << " resource_id = " << resource_id; | 102 << " resource_id = " << resource_id; |
| 91 } | 103 } |
| 92 return *found->second; | 104 return *found->second; |
| 93 } | 105 } |
| 94 } | 106 } |
| 95 | 107 |
| 96 scoped_nsobject<NSImage> ns_image; | 108 gfx::Image* image = NULL; |
| 97 for (size_t i = 0; i < data_packs_.size(); ++i) { | 109 if (delegate_) |
| 98 scoped_refptr<base::RefCountedStaticMemory> data( | 110 image = delegate_->GetNativeImageNamed(resource_id, rtl); |
| 99 data_packs_[i]->GetStaticMemory(resource_id)); | |
| 100 if (!data.get()) | |
| 101 continue; | |
| 102 | 111 |
| 103 scoped_nsobject<NSData> ns_data( | 112 if (!image) { |
| 104 [[NSData alloc] initWithBytes:data->front() | 113 scoped_nsobject<NSImage> ns_image; |
| 105 length:data->size()]); | 114 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 115 scoped_refptr<base::RefCountedStaticMemory> data( | |
| 116 data_packs_[i]->GetStaticMemory(resource_id)); | |
| 117 if (!data.get()) | |
| 118 continue; | |
| 119 | |
| 120 scoped_nsobject<NSData> ns_data( | |
| 121 [[NSData alloc] initWithBytes:data->front() | |
| 122 length:data->size()]); | |
| 123 if (!ns_image.get()) { | |
| 124 ns_image.reset([[NSImage alloc] initWithData:ns_data]); | |
| 125 } else { | |
| 126 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; | |
| 127 if (image_rep) | |
| 128 [ns_image addRepresentation:image_rep]; | |
| 129 } | |
| 130 } | |
| 131 | |
| 106 if (!ns_image.get()) { | 132 if (!ns_image.get()) { |
| 107 ns_image.reset([[NSImage alloc] initWithData:ns_data]); | 133 LOG(WARNING) << "Unable to load image with id " << resource_id; |
| 108 } else { | 134 NOTREACHED(); // Want to assert in debug mode. |
| 109 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; | 135 return *GetEmptyImage(); |
| 110 if (image_rep) | |
| 111 [ns_image addRepresentation:image_rep]; | |
| 112 } | 136 } |
| 113 } | |
| 114 | 137 |
| 115 if (!ns_image.get()) { | 138 image = new gfx::Image(ns_image.release()); |
| 116 LOG(WARNING) << "Unable to load image with id " << resource_id; | |
| 117 NOTREACHED(); // Want to assert in debug mode. | |
| 118 return *GetEmptyImage(); | |
| 119 } | 139 } |
| 120 | 140 |
| 121 base::AutoLock lock(*images_and_fonts_lock_); | 141 base::AutoLock lock(*images_and_fonts_lock_); |
| 122 | 142 |
| 123 // Another thread raced the load and has already cached the image. | 143 // Another thread raced the load and has already cached the image. |
| 124 if (images_.count(resource_id)) { | 144 if (images_.count(resource_id)) { |
| 145 delete image; | |
|
sail
2012/04/30 21:14:43
same, should avoid this if possible
Marshall
2012/04/30 21:50:10
Done.
| |
| 125 return *images_[resource_id]; | 146 return *images_[resource_id]; |
| 126 } | 147 } |
| 127 | 148 |
| 128 gfx::Image* image = new gfx::Image(ns_image.release()); | |
| 129 images_[resource_id] = image; | 149 images_[resource_id] = image; |
| 130 return *image; | 150 return *image; |
| 131 } | 151 } |
| 132 | 152 |
| 133 } // namespace ui | 153 } // namespace ui |
| OLD | NEW |