Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: ui/base/resource/resource_bundle_mac.mm

Issue 10270023: Add new ResourceBundle::Delegate interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/resource/resource_bundle_gtk.cc ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/base/resource/resource_bundle.h" 5 #include "ui/base/resource/resource_bundle.h"
6 6
7 #import <AppKit/AppKit.h> 7 #import <AppKit/AppKit.h>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/file_util.h"
11 #include "base/mac/bundle_locations.h" 12 #include "base/mac/bundle_locations.h"
12 #include "base/mac/mac_util.h" 13 #include "base/mac/mac_util.h"
13 #include "base/memory/ref_counted_memory.h" 14 #include "base/memory/ref_counted_memory.h"
14 #include "base/memory/scoped_nsobject.h" 15 #include "base/memory/scoped_nsobject.h"
15 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
16 #include "base/sys_string_conversions.h" 17 #include "base/sys_string_conversions.h"
17 #include "ui/base/resource/resource_handle.h" 18 #include "ui/base/resource/resource_handle.h"
18 #include "ui/gfx/image/image.h" 19 #include "ui/gfx/image/image.h"
19 20
20 namespace ui { 21 namespace ui {
21 22
22 namespace { 23 namespace {
23 24
24 FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { 25 FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) {
25 NSString *resource_path; 26 NSString *resource_path;
26 // Some of the helper processes need to be able to fetch resources 27 // Some of the helper processes need to be able to fetch resources
27 // (chrome_main.cc: SubprocessNeedsResourceBundle()). Fetch the same locale 28 // (chrome_main.cc: SubprocessNeedsResourceBundle()). Fetch the same locale
28 // as the already-running browser instead of using what NSBundle might pick 29 // as the already-running browser instead of using what NSBundle might pick
29 // based on values at helper launch time. 30 // based on values at helper launch time.
30 if ([mac_locale length]) { 31 if ([mac_locale length]) {
31 resource_path = [base::mac::FrameworkBundle() pathForResource:name 32 resource_path = [base::mac::FrameworkBundle() pathForResource:name
32 ofType:@"pak" 33 ofType:@"pak"
33 inDirectory:@"" 34 inDirectory:@""
34 forLocalization:mac_locale]; 35 forLocalization:mac_locale];
35 } else { 36 } else {
36 resource_path = [base::mac::FrameworkBundle() pathForResource:name 37 resource_path = [base::mac::FrameworkBundle() pathForResource:name
37 ofType:@"pak"]; 38 ofType:@"pak"];
38 } 39 }
39 if (!resource_path) 40
40 return FilePath(); 41 if (!resource_path) {
42 // Return just the name of the pack file.
43 return FilePath(base::SysNSStringToUTF8(name) + ".pak");
44 }
45
41 return FilePath([resource_path fileSystemRepresentation]); 46 return FilePath([resource_path fileSystemRepresentation]);
42 } 47 }
43 48
44 } // namespace 49 } // namespace
45 50
46 void ResourceBundle::LoadCommonResources() { 51 void ResourceBundle::LoadCommonResources() {
47 AddDataPack(GetResourcesPakFilePath(@"chrome", nil), 52 AddDataPack(GetResourcesPakFilePath(@"chrome", nil),
48 ResourceHandle::kScaleFactor100x); 53 ResourceHandle::kScaleFactor100x);
49 AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard", nil), 54 AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard", nil),
50 ResourceHandle::kScaleFactor100x); 55 ResourceHandle::kScaleFactor100x);
51 AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard", nil), 56 AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard", nil),
52 ResourceHandle::kScaleFactor100x); 57 ResourceHandle::kScaleFactor100x);
53 58
54 // On Windows and ChromeOS we load either the 1x resource or the 2x resource. 59 // On Windows and ChromeOS we load either the 1x resource or the 2x resource.
55 // On Mac we load both and let the UI framework decide which one to use. 60 // On Mac we load both and let the UI framework decide which one to use.
56 #if defined(ENABLE_HIDPI) 61 #if defined(ENABLE_HIDPI)
57 if (base::mac::IsOSLionOrLater()) { 62 if (base::mac::IsOSLionOrLater()) {
58 AddDataPack(GetResourcesPakFilePath(@"theme_resources_2x", nil), 63 AddDataPack(GetResourcesPakFilePath(@"theme_resources_2x", nil),
59 ResourceHandle::kScaleFactor200x); 64 ResourceHandle::kScaleFactor200x);
60 AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard_2x", nil), 65 AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard_2x", nil),
61 ResourceHandle::kScaleFactor200x); 66 ResourceHandle::kScaleFactor200x);
62 AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard_2x", nil), 67 AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard_2x", nil),
63 ResourceHandle::kScaleFactor200x); 68 ResourceHandle::kScaleFactor200x);
64 } 69 }
65 #endif 70 #endif
66 } 71 }
67 72
68 // static
69 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { 73 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) {
70 NSString* mac_locale = base::SysUTF8ToNSString(app_locale); 74 NSString* mac_locale = base::SysUTF8ToNSString(app_locale);
71 75
72 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value. 76 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value.
73 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-" 77 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-"
74 withString:@"_"]; 78 withString:@"_"];
75 79
76 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578). 80 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578).
77 if ([mac_locale isEqual:@"en_US"]) 81 if ([mac_locale isEqual:@"en_US"])
78 mac_locale = @"en"; 82 mac_locale = @"en";
79 83
80 return GetResourcesPakFilePath(@"locale", mac_locale); 84 FilePath locale_file_path = GetResourcesPakFilePath(@"locale", mac_locale);
85
86 if (delegate_) {
87 locale_file_path =
88 delegate_->GetPathForLocalePack(locale_file_path, app_locale);
89 }
90
91 // Don't try to load empty values or values that are not absolute paths.
92 if (locale_file_path.empty() || !locale_file_path.IsAbsolute())
93 return FilePath();
94
95 if (!file_util::PathExists(locale_file_path))
96 return FilePath();
97
98 return locale_file_path;
81 } 99 }
82 100
83 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { 101 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) {
84 // Flipped images are not used on Mac. 102 // Flipped images are not used on Mac.
85 DCHECK_EQ(rtl, RTL_DISABLED); 103 DCHECK_EQ(rtl, RTL_DISABLED);
86 104
87 // Check to see if the image is already in the cache. 105 // Check to see if the image is already in the cache.
88 { 106 {
89 base::AutoLock lock(*images_and_fonts_lock_); 107 base::AutoLock lock(*images_and_fonts_lock_);
90 ImageMap::const_iterator found = images_.find(resource_id); 108 if (images_.count(resource_id)) {
91 if (found != images_.end()) { 109 if (!images_[resource_id].HasRepresentation(gfx::Image::kImageRepCocoa)) {
92 if (!found->second->HasRepresentation(gfx::Image::kImageRepCocoa)) {
93 DLOG(WARNING) << "ResourceBundle::GetNativeImageNamed() is returning a" 110 DLOG(WARNING) << "ResourceBundle::GetNativeImageNamed() is returning a"
94 << " cached gfx::Image that isn't backed by an NSImage. The image" 111 << " cached gfx::Image that isn't backed by an NSImage. The image"
95 << " will be converted, rather than going through the NSImage loader." 112 << " will be converted, rather than going through the NSImage loader."
96 << " resource_id = " << resource_id; 113 << " resource_id = " << resource_id;
97 } 114 }
98 return *found->second; 115 return images_[resource_id];
99 } 116 }
100 } 117 }
101 118
102 scoped_nsobject<NSImage> ns_image; 119 gfx::Image image;
103 for (size_t i = 0; i < data_packs_.size(); ++i) { 120 if (delegate_)
104 scoped_refptr<base::RefCountedStaticMemory> data( 121 image = delegate_->GetNativeImageNamed(resource_id, rtl);
105 data_packs_[i]->GetStaticMemory(resource_id));
106 if (!data.get())
107 continue;
108 122
109 scoped_nsobject<NSData> ns_data( 123 if (image.IsEmpty()) {
110 [[NSData alloc] initWithBytes:data->front() 124 scoped_nsobject<NSImage> ns_image;
111 length:data->size()]); 125 for (size_t i = 0; i < data_packs_.size(); ++i) {
126 scoped_refptr<base::RefCountedStaticMemory> data(
127 data_packs_[i]->GetStaticMemory(resource_id));
128 if (!data.get())
129 continue;
130
131 scoped_nsobject<NSData> ns_data(
132 [[NSData alloc] initWithBytes:data->front()
133 length:data->size()]);
134 if (!ns_image.get()) {
135 ns_image.reset([[NSImage alloc] initWithData:ns_data]);
136 } else {
137 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data];
138 if (image_rep)
139 [ns_image addRepresentation:image_rep];
140 }
141 }
142
112 if (!ns_image.get()) { 143 if (!ns_image.get()) {
113 ns_image.reset([[NSImage alloc] initWithData:ns_data]); 144 LOG(WARNING) << "Unable to load image with id " << resource_id;
114 } else { 145 NOTREACHED(); // Want to assert in debug mode.
115 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; 146 return GetEmptyImage();
116 if (image_rep)
117 [ns_image addRepresentation:image_rep];
118 } 147 }
119 }
120 148
121 if (!ns_image.get()) { 149 image = gfx::Image(ns_image.release());
122 LOG(WARNING) << "Unable to load image with id " << resource_id;
123 NOTREACHED(); // Want to assert in debug mode.
124 return *GetEmptyImage();
125 } 150 }
126 151
127 base::AutoLock lock(*images_and_fonts_lock_); 152 base::AutoLock lock(*images_and_fonts_lock_);
128 153
129 // Another thread raced the load and has already cached the image. 154 // Another thread raced the load and has already cached the image.
130 if (images_.count(resource_id)) { 155 if (images_.count(resource_id))
131 return *images_[resource_id]; 156 return images_[resource_id];
132 }
133 157
134 gfx::Image* image = new gfx::Image(ns_image.release());
135 images_[resource_id] = image; 158 images_[resource_id] = image;
136 return *image; 159 return images_[resource_id];
137 } 160 }
138 161
139 } // namespace ui 162 } // namespace ui
OLDNEW
« no previous file with comments | « ui/base/resource/resource_bundle_gtk.cc ('k') | ui/base/resource/resource_bundle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698