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