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
3 // found in the LICENSE file.
5 #include "ui/base/resource/resource_bundle.h"
7 #import <AppKit/AppKit.h>
9 #include "base/basictypes.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/mac/bundle_locations.h"
13 #include "base/mac/scoped_nsobject.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/synchronization/lock.h"
17 #include "ui/base/resource/resource_handle.h"
18 #include "ui/gfx/image/image.h"
24 base::FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) {
25 NSString *resource_path;
26 // Some of the helper processes need to be able to fetch resources
27 // (chrome_main.cc: SubprocessNeedsResourceBundle()). Fetch the same locale
28 // as the already-running browser instead of using what NSBundle might pick
29 // based on values at helper launch time.
30 if ([mac_locale length]) {
31 resource_path = [base::mac::FrameworkBundle() pathForResource:name
34 forLocalization:mac_locale];
36 resource_path = [base::mac::FrameworkBundle() pathForResource:name
41 // Return just the name of the pack file.
42 return base::FilePath(base::SysNSStringToUTF8(name) + ".pak");
45 return base::FilePath([resource_path fileSystemRepresentation]);
50 void ResourceBundle::LoadCommonResources() {
51 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_100_percent",
52 nil), SCALE_FACTOR_100P);
54 // On Mac we load 1x and 2x resources and we let the UI framework decide
56 if (IsScaleFactorSupported(SCALE_FACTOR_200P)) {
57 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil),
62 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale,
63 bool test_file_exists) {
64 NSString* mac_locale = base::SysUTF8ToNSString(app_locale);
66 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value.
67 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-"
70 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578).
71 if ([mac_locale isEqual:@"en_US"])
74 base::FilePath locale_file_path =
75 GetResourcesPakFilePath(@"locale", mac_locale);
79 delegate_->GetPathForLocalePack(locale_file_path, app_locale);
82 // Don't try to load empty values or values that are not absolute paths.
83 if (locale_file_path.empty() || !locale_file_path.IsAbsolute())
84 return base::FilePath();
86 if (test_file_exists && !base::PathExists(locale_file_path))
87 return base::FilePath();
89 return locale_file_path;
92 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
93 // Flipped images are not used on Mac.
94 DCHECK_EQ(rtl, RTL_DISABLED);
96 // Check to see if the image is already in the cache.
98 base::AutoLock lock(*images_and_fonts_lock_);
99 if (images_.count(resource_id)) {
100 if (!images_[resource_id].HasRepresentation(gfx::Image::kImageRepCocoa)) {
101 DLOG(WARNING) << "ResourceBundle::GetNativeImageNamed() is returning a"
102 << " cached gfx::Image that isn't backed by an NSImage. The image"
103 << " will be converted, rather than going through the NSImage loader."
104 << " resource_id = " << resource_id;
106 return images_[resource_id];
112 image = delegate_->GetNativeImageNamed(resource_id, rtl);
114 if (image.IsEmpty()) {
115 base::scoped_nsobject<NSImage> ns_image;
116 for (size_t i = 0; i < data_packs_.size(); ++i) {
117 scoped_refptr<base::RefCountedStaticMemory> data(
118 data_packs_[i]->GetStaticMemory(resource_id));
122 base::scoped_nsobject<NSData> ns_data(
123 [[NSData alloc] initWithBytes:data->front() length:data->size()]);
124 if (!ns_image.get()) {
125 ns_image.reset([[NSImage alloc] initWithData:ns_data]);
127 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data];
129 [ns_image addRepresentation:image_rep];
133 if (!ns_image.get()) {
134 LOG(WARNING) << "Unable to load image with id " << resource_id;
135 NOTREACHED(); // Want to assert in debug mode.
136 return GetEmptyImage();
139 image = gfx::Image(ns_image.release());
142 base::AutoLock lock(*images_and_fonts_lock_);
144 // Another thread raced the load and has already cached the image.
145 if (images_.count(resource_id))
146 return images_[resource_id];
148 images_[resource_id] = image;
149 return images_[resource_id];