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