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