OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/base/resource/resource_bundle.h" | |
6 | |
7 #import <QuartzCore/QuartzCore.h> | |
8 #import <UIKit/UIKit.h> | |
9 | |
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" | |
21 | |
22 namespace ui { | |
23 | |
24 namespace { | |
25 | |
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 | |
30 ofType:@"pak" | |
31 inDirectory:@"" | |
32 forLocalization:mac_locale]; | |
33 } else { | |
34 resource_path = [base::mac::FrameworkBundle() pathForResource:name | |
35 ofType:@"pak"]; | |
36 } | |
37 if (!resource_path) { | |
38 // Return just the name of the pak file. | |
39 return base::FilePath(base::SysNSStringToUTF8(name) + ".pak"); | |
40 } | |
41 return base::FilePath([resource_path fileSystemRepresentation]); | |
42 } | |
43 | |
44 } // namespace | |
45 | |
46 void ResourceBundle::LoadCommonResources() { | |
47 if (IsScaleFactorSupported(SCALE_FACTOR_100P)) { | |
48 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_100_percent", nil), | |
49 SCALE_FACTOR_100P); | |
50 } | |
51 | |
52 if (IsScaleFactorSupported(SCALE_FACTOR_200P)) { | |
53 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil), | |
54 SCALE_FACTOR_200P); | |
55 } | |
56 | |
57 // TODO(rohitrao): Add a chrome_300_percent file and load it here. For now, | |
58 // we are simply falling back to the 200P resources. http://crbug.com/413300. | |
59 if (IsScaleFactorSupported(SCALE_FACTOR_300P)) { | |
60 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil), | |
61 SCALE_FACTOR_200P); | |
62 } | |
63 } | |
64 | |
65 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, | |
66 bool test_file_exists) { | |
67 NSString* mac_locale = base::SysUTF8ToNSString(app_locale); | |
68 | |
69 // iOS uses "_" instead of "-", so swap to get a iOS-style value. | |
70 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-" | |
71 withString:@"_"]; | |
72 | |
73 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578). | |
74 if ([mac_locale isEqual:@"en_US"]) | |
75 mac_locale = @"en"; | |
76 | |
77 base::FilePath locale_file_path = | |
78 GetResourcesPakFilePath(@"locale", mac_locale); | |
79 | |
80 if (delegate_) { | |
81 locale_file_path = | |
82 delegate_->GetPathForLocalePack(locale_file_path, app_locale); | |
83 } | |
84 | |
85 // Don't try to load empty values or values that are not absolute paths. | |
86 if (locale_file_path.empty() || !locale_file_path.IsAbsolute()) | |
87 return base::FilePath(); | |
88 | |
89 if (test_file_exists && !base::PathExists(locale_file_path)) | |
90 return base::FilePath(); | |
91 | |
92 return locale_file_path; | |
93 } | |
94 | |
95 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { | |
96 // Flipped images are not used on iOS. | |
97 DCHECK_EQ(rtl, RTL_DISABLED); | |
98 | |
99 // Check to see if the image is already in the cache. | |
100 { | |
101 base::AutoLock lock(*images_and_fonts_lock_); | |
102 ImageMap::iterator found = images_.find(resource_id); | |
103 if (found != images_.end()) { | |
104 return found->second; | |
105 } | |
106 } | |
107 | |
108 gfx::Image image; | |
109 if (delegate_) | |
110 image = delegate_->GetNativeImageNamed(resource_id, rtl); | |
111 | |
112 if (image.IsEmpty()) { | |
113 // Load the raw data from the resource pack at the current supported scale | |
114 // factor. This code assumes that only one of the possible scale factors is | |
115 // supported at runtime, based on the device resolution. | |
116 ui::ScaleFactor scale_factor = GetMaxScaleFactor(); | |
117 | |
118 scoped_refptr<base::RefCountedStaticMemory> data( | |
119 LoadDataResourceBytesForScale(resource_id, scale_factor)); | |
120 | |
121 if (!data.get()) { | |
122 LOG(WARNING) << "Unable to load image with id " << resource_id; | |
123 return GetEmptyImage(); | |
124 } | |
125 | |
126 // Create a data object from the raw bytes. | |
127 base::scoped_nsobject<NSData> ns_data( | |
128 [[NSData alloc] initWithBytes:data->front() length:data->size()]); | |
129 | |
130 bool is_fallback = PNGContainsFallbackMarker(data->front(), data->size()); | |
131 // Create the image from the data. | |
132 CGFloat target_scale = ui::GetScaleForScaleFactor(scale_factor); | |
133 // Hack: The 200P pak file is the only pak file loaded on iOS devices with | |
134 // an @3x scale factor. Force |source_scale| to be 2.0 to handle this case, | |
135 // since it cannot be anything else. http://crbug.com/413300. | |
136 // TODO(rohitrao): Support proper fallback by using the actual scale factor | |
137 // of the source image, rather than assuming it is 1.0 or 2.0. | |
138 CGFloat source_scale = target_scale; | |
139 if (is_fallback) { | |
140 source_scale = 1.0; | |
141 } else if (scale_factor == SCALE_FACTOR_300P) { | |
142 source_scale = 2.0; | |
143 } | |
144 base::scoped_nsobject<UIImage> ui_image( | |
145 [[UIImage alloc] initWithData:ns_data scale:source_scale]); | |
146 | |
147 // If the image is a 1x fallback, scale it up to a full-size representation. | |
148 if (is_fallback) { | |
149 CGSize source_size = [ui_image size]; | |
150 CGSize target_size = CGSizeMake(source_size.width * target_scale, | |
151 source_size.height * target_scale); | |
152 base::ScopedCFTypeRef<CGColorSpaceRef> color_space( | |
153 CGColorSpaceCreateDeviceRGB()); | |
154 base::ScopedCFTypeRef<CGContextRef> context(CGBitmapContextCreate( | |
155 NULL, | |
156 target_size.width, | |
157 target_size.height, | |
158 8, | |
159 target_size.width * 4, | |
160 color_space, | |
161 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host)); | |
162 | |
163 CGRect target_rect = CGRectMake(0, 0, | |
164 target_size.width, target_size.height); | |
165 CGContextSetBlendMode(context, kCGBlendModeCopy); | |
166 CGContextDrawImage(context, target_rect, [ui_image CGImage]); | |
167 | |
168 base::ScopedCFTypeRef<CGImageRef> cg_image( | |
169 CGBitmapContextCreateImage(context)); | |
170 ui_image.reset([[UIImage alloc] initWithCGImage:cg_image | |
171 scale:target_scale | |
172 orientation:UIImageOrientationUp]); | |
173 } | |
174 | |
175 if (!ui_image.get()) { | |
176 LOG(WARNING) << "Unable to load image with id " << resource_id; | |
177 NOTREACHED(); // Want to assert in debug mode. | |
178 return GetEmptyImage(); | |
179 } | |
180 | |
181 // The gfx::Image takes ownership. | |
182 image = gfx::Image(ui_image.release()); | |
183 } | |
184 | |
185 base::AutoLock lock(*images_and_fonts_lock_); | |
186 | |
187 // Another thread raced the load and has already cached the image. | |
188 if (images_.count(resource_id)) | |
189 return images_[resource_id]; | |
190 | |
191 images_[resource_id] = image; | |
192 return images_[resource_id]; | |
193 } | |
194 | |
195 } // namespace ui | |
OLD | NEW |