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/files/file_path.h"
12 #include "base/files/file_util.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 if (IsScaleFactorSupported(SCALE_FACTOR_100P)) {
48 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_100_percent", nil),
52 if (IsScaleFactorSupported(SCALE_FACTOR_200P)) {
53 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil),
57 if (IsScaleFactorSupported(SCALE_FACTOR_300P)) {
58 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_300_percent", nil),
63 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale,
64 bool test_file_exists) {
65 NSString* mac_locale = base::SysUTF8ToNSString(app_locale);
67 // iOS uses "_" instead of "-", so swap to get a iOS-style value.
68 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-"
71 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578).
72 if ([mac_locale isEqual:@"en_US"])
75 base::FilePath locale_file_path =
76 GetResourcesPakFilePath(@"locale", mac_locale);
80 delegate_->GetPathForLocalePack(locale_file_path, app_locale);
83 // Don't try to load empty values or values that are not absolute paths.
84 if (locale_file_path.empty() || !locale_file_path.IsAbsolute())
85 return base::FilePath();
87 if (test_file_exists && !base::PathExists(locale_file_path))
88 return base::FilePath();
90 return locale_file_path;
93 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
94 // Flipped images are not used on iOS.
95 DCHECK_EQ(rtl, RTL_DISABLED);
97 // Check to see if the image is already in the cache.
99 base::AutoLock lock(*images_and_fonts_lock_);
100 ImageMap::iterator found = images_.find(resource_id);
101 if (found != images_.end()) {
102 return found->second;
108 image = delegate_->GetNativeImageNamed(resource_id, rtl);
110 if (image.IsEmpty()) {
111 // Load the raw data from the resource pack at the current supported scale
112 // factor. This code assumes that only one of the possible scale factors is
113 // supported at runtime, based on the device resolution.
114 ui::ScaleFactor scale_factor = GetMaxScaleFactor();
116 scoped_refptr<base::RefCountedStaticMemory> data(
117 LoadDataResourceBytesForScale(resource_id, scale_factor));
120 LOG(WARNING) << "Unable to load image with id " << resource_id;
121 return GetEmptyImage();
124 // Create a data object from the raw bytes.
125 base::scoped_nsobject<NSData> ns_data(
126 [[NSData alloc] initWithBytes:data->front() length:data->size()]);
128 bool is_fallback = PNGContainsFallbackMarker(data->front(), data->size());
129 // Create the image from the data.
130 CGFloat target_scale = ui::GetScaleForScaleFactor(scale_factor);
131 CGFloat source_scale = is_fallback ? 1.0 : target_scale;
132 base::scoped_nsobject<UIImage> ui_image(
133 [[UIImage alloc] initWithData:ns_data scale:source_scale]);
135 // If the image is a 1x fallback, scale it up to a full-size representation.
137 CGSize source_size = [ui_image size];
138 CGSize target_size = CGSizeMake(source_size.width * target_scale,
139 source_size.height * target_scale);
140 base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
141 CGColorSpaceCreateDeviceRGB());
142 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate(
147 target_size.width * 4,
149 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host));
151 CGRect target_rect = CGRectMake(0, 0,
152 target_size.width, target_size.height);
153 CGContextSetBlendMode(context, kCGBlendModeCopy);
154 CGContextDrawImage(context, target_rect, [ui_image CGImage]);
156 base::ScopedCFTypeRef<CGImageRef> cg_image(
157 CGBitmapContextCreateImage(context));
158 ui_image.reset([[UIImage alloc] initWithCGImage:cg_image
160 orientation:UIImageOrientationUp]);
163 if (!ui_image.get()) {
164 LOG(WARNING) << "Unable to load image with id " << resource_id;
165 NOTREACHED(); // Want to assert in debug mode.
166 return GetEmptyImage();
169 // The gfx::Image takes ownership.
170 image = gfx::Image(ui_image.release());
173 base::AutoLock lock(*images_and_fonts_lock_);
175 // Another thread raced the load and has already cached the image.
176 if (images_.count(resource_id))
177 return images_[resource_id];
179 images_[resource_id] = image;
180 return images_[resource_id];