1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/resource/resource_bundle.h"
7 #import <QuartzCore/QuartzCore.h>
8 #import <UIKit/UIKit.h>
10 #include "base/basictypes.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/mac/bundle_locations.h"
14 #include "base/mac/foundation_util.h"
15 #include "base/mac/scoped_nsobject.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "base/synchronization/lock.h"
19 #include "ui/base/resource/resource_handle.h"
20 #include "ui/gfx/image/image.h"
26 base::FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) {
27 NSString *resource_path;
28 if ([mac_locale length]) {
29 resource_path = [base::mac::FrameworkBundle() pathForResource:name
32 forLocalization:mac_locale];
34 resource_path = [base::mac::FrameworkBundle() pathForResource:name
38 // Return just the name of the pak file.
39 return base::FilePath(base::SysNSStringToUTF8(name) + ".pak");
41 return base::FilePath([resource_path fileSystemRepresentation]);
46 void ResourceBundle::LoadCommonResources() {
47 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome", nil),
48 ui::SCALE_FACTOR_NONE);
50 if (IsScaleFactorSupported(SCALE_FACTOR_100P)) {
51 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_100_percent", nil),
55 if (IsScaleFactorSupported(SCALE_FACTOR_200P)) {
56 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil),
61 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale,
62 bool test_file_exists) {
63 NSString* mac_locale = base::SysUTF8ToNSString(app_locale);
65 // iOS uses "_" instead of "-", so swap to get a iOS-style value.
66 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-"
69 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578).
70 if ([mac_locale isEqual:@"en_US"])
73 base::FilePath locale_file_path =
74 GetResourcesPakFilePath(@"locale", mac_locale);
78 delegate_->GetPathForLocalePack(locale_file_path, app_locale);
81 // Don't try to load empty values or values that are not absolute paths.
82 if (locale_file_path.empty() || !locale_file_path.IsAbsolute())
83 return base::FilePath();
85 if (test_file_exists && !base::PathExists(locale_file_path))
86 return base::FilePath();
88 return locale_file_path;
91 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
92 // Flipped images are not used on iOS.
93 DCHECK_EQ(rtl, RTL_DISABLED);
95 // Check to see if the image is already in the cache.
97 base::AutoLock lock(*images_and_fonts_lock_);
98 ImageMap::iterator found = images_.find(resource_id);
99 if (found != images_.end()) {
100 return found->second;
106 image = delegate_->GetNativeImageNamed(resource_id, rtl);
108 if (image.IsEmpty()) {
109 // Load the raw data from the resource pack at the current supported scale
110 // factor. This code assumes that only one of the possible scale factors is
111 // supported at runtime, based on the device resolution.
112 ui::ScaleFactor scale_factor = GetMaxScaleFactor();
114 scoped_refptr<base::RefCountedStaticMemory> data(
115 LoadDataResourceBytesForScale(resource_id, scale_factor));
118 LOG(WARNING) << "Unable to load image with id " << resource_id;
119 return GetEmptyImage();
122 // Create a data object from the raw bytes.
123 base::scoped_nsobject<NSData> ns_data(
124 [[NSData alloc] initWithBytes:data->front() length:data->size()]);
126 bool is_fallback = PNGContainsFallbackMarker(data->front(), data->size());
127 // Create the image from the data.
128 CGFloat target_scale = ui::GetScaleForScaleFactor(scale_factor);
129 CGFloat source_scale = is_fallback ? 1.0 : target_scale;
130 base::scoped_nsobject<UIImage> ui_image(
131 [[UIImage alloc] initWithData:ns_data scale:source_scale]);
133 // If the image is a 1x fallback, scale it up to a full-size representation.
135 CGSize source_size = [ui_image size];
136 CGSize target_size = CGSizeMake(source_size.width * target_scale,
137 source_size.height * target_scale);
138 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
139 CGColorSpaceCreateDeviceRGB());
140 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
145 target_size.width * 4,
147 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
149 CGRect target_rect = CGRectMake(0, 0,
150 target_size.width, target_size.height);
151 CGContextSetBlendMode(context, kCGBlendModeCopy);
152 CGContextDrawImage(context, target_rect, [ui_image CGImage]);
154 base::ScopedCFTypeRef<CGImageRef> cg_image(
155 CGBitmapContextCreateImage(context));
156 ui_image.reset([[UIImage alloc] initWithCGImage:cg_image
158 orientation:UIImageOrientationUp]);
161 if (!ui_image.get()) {
162 LOG(WARNING) << "Unable to load image with id " << resource_id;
163 NOTREACHED(); // Want to assert in debug mode.
164 return GetEmptyImage();
167 // The gfx::Image takes ownership.
168 image = gfx::Image(ui_image.release());
171 base::AutoLock lock(*images_and_fonts_lock_);
173 // Another thread raced the load and has already cached the image.
174 if (images_.count(resource_id))
175 return images_[resource_id];
177 images_[resource_id] = image;
178 return images_[resource_id];