OLD | NEW |
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 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
12 #include "base/mac/bundle_locations.h" | 12 #include "base/mac/bundle_locations.h" |
13 #include "base/mac/scoped_nsobject.h" | 13 #include "base/mac/scoped_nsobject.h" |
14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "ui/base/material_design/material_design_controller.h" | |
18 #include "ui/base/resource/resource_handle.h" | 17 #include "ui/base/resource/resource_handle.h" |
19 #include "ui/gfx/image/image.h" | 18 #include "ui/gfx/image/image.h" |
20 | 19 |
21 namespace ui { | 20 namespace ui { |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 base::FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { | 24 base::FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { |
26 NSString *resource_path; | 25 NSString *resource_path; |
27 // Some of the helper processes need to be able to fetch resources | 26 // Some of the helper processes need to be able to fetch resources |
(...skipping 25 matching lines...) Expand all Loading... |
53 nil), SCALE_FACTOR_100P); | 52 nil), SCALE_FACTOR_100P); |
54 | 53 |
55 // On Mac we load 1x and 2x resources and we let the UI framework decide | 54 // On Mac we load 1x and 2x resources and we let the UI framework decide |
56 // which one to use. | 55 // which one to use. |
57 if (IsScaleFactorSupported(SCALE_FACTOR_200P)) { | 56 if (IsScaleFactorSupported(SCALE_FACTOR_200P)) { |
58 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil), | 57 AddDataPackFromPath(GetResourcesPakFilePath(@"chrome_200_percent", nil), |
59 SCALE_FACTOR_200P); | 58 SCALE_FACTOR_200P); |
60 } | 59 } |
61 } | 60 } |
62 | 61 |
63 void ResourceBundle::LoadMaterialDesignResources() { | |
64 if (!MaterialDesignController::IsModeMaterial()) { | |
65 return; | |
66 } | |
67 | |
68 // The Material Design data packs contain some of the same asset IDs as in | |
69 // the non-Material Design data packs. Set aside the current packs and add the | |
70 // Material Design packs so that they are searched first when a request for an | |
71 // asset is made. The Material Design packs cannot be loaded in | |
72 // LoadCommonResources() because the MaterialDesignController is not always | |
73 // initialized at the time it is called. | |
74 // TODO(shrike) - remove this method and restore loading of Material Design | |
75 // packs to LoadCommonResources() when the MaterialDesignController goes away. | |
76 std::vector<std::unique_ptr<ResourceHandle>> tmp_packs; | |
77 for (auto it = data_packs_.begin(); it != data_packs_.end(); ++it) { | |
78 std::unique_ptr<ResourceHandle> next_pack(*it); | |
79 tmp_packs.push_back(std::move(next_pack)); | |
80 } | |
81 data_packs_.weak_clear(); | |
82 | |
83 AddMaterialDesignDataPackFromPath( | |
84 GetResourcesPakFilePath(@"chrome_material_100_percent", nil), | |
85 SCALE_FACTOR_100P); | |
86 | |
87 AddOptionalMaterialDesignDataPackFromPath( | |
88 GetResourcesPakFilePath(@"chrome_material_200_percent", nil), | |
89 SCALE_FACTOR_200P); | |
90 | |
91 // Add back the non-Material Design packs so that they serve as a fallback. | |
92 for (auto it = tmp_packs.begin(); it != tmp_packs.end(); ++it) { | |
93 data_packs_.push_back(std::move(*it)); | |
94 } | |
95 } | |
96 | |
97 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, | 62 base::FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, |
98 bool test_file_exists) { | 63 bool test_file_exists) { |
99 NSString* mac_locale = base::SysUTF8ToNSString(app_locale); | 64 NSString* mac_locale = base::SysUTF8ToNSString(app_locale); |
100 | 65 |
101 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value. | 66 // Mac OS X uses "_" instead of "-", so swap to get a Mac-style value. |
102 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-" | 67 mac_locale = [mac_locale stringByReplacingOccurrencesOfString:@"-" |
103 withString:@"_"]; | 68 withString:@"_"]; |
104 | 69 |
105 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578). | 70 // On disk, the "en_US" resources are just "en" (http://crbug.com/25578). |
106 if ([mac_locale isEqual:@"en_US"]) | 71 if ([mac_locale isEqual:@"en_US"]) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 return images_[resource_id]; | 103 return images_[resource_id]; |
139 } | 104 } |
140 } | 105 } |
141 | 106 |
142 gfx::Image image; | 107 gfx::Image image; |
143 if (delegate_) | 108 if (delegate_) |
144 image = delegate_->GetNativeImageNamed(resource_id); | 109 image = delegate_->GetNativeImageNamed(resource_id); |
145 | 110 |
146 if (image.IsEmpty()) { | 111 if (image.IsEmpty()) { |
147 base::scoped_nsobject<NSImage> ns_image; | 112 base::scoped_nsobject<NSImage> ns_image; |
148 // Material Design packs are meant to override the standard packs, so | |
149 // search for the image in those packs first. | |
150 for (size_t i = 0; i < data_packs_.size(); ++i) { | 113 for (size_t i = 0; i < data_packs_.size(); ++i) { |
151 if (!data_packs_[i]->HasOnlyMaterialDesignAssets()) | |
152 continue; | |
153 scoped_refptr<base::RefCountedStaticMemory> data( | 114 scoped_refptr<base::RefCountedStaticMemory> data( |
154 data_packs_[i]->GetStaticMemory(resource_id)); | 115 data_packs_[i]->GetStaticMemory(resource_id)); |
155 if (!data.get()) | 116 if (!data.get()) |
156 continue; | 117 continue; |
157 | 118 |
158 base::scoped_nsobject<NSData> ns_data( | 119 base::scoped_nsobject<NSData> ns_data( |
159 [[NSData alloc] initWithBytes:data->front() length:data->size()]); | 120 [[NSData alloc] initWithBytes:data->front() length:data->size()]); |
160 if (!ns_image.get()) { | 121 if (!ns_image.get()) { |
161 ns_image.reset([[NSImage alloc] initWithData:ns_data]); | 122 ns_image.reset([[NSImage alloc] initWithData:ns_data]); |
162 } else { | 123 } else { |
163 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; | 124 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; |
164 if (image_rep) | 125 if (image_rep) |
165 [ns_image addRepresentation:image_rep]; | 126 [ns_image addRepresentation:image_rep]; |
166 } | 127 } |
167 } | 128 } |
168 | 129 |
169 if (ns_image.get()) { | 130 if (ns_image.get()) { |
170 image = gfx::Image(ns_image.release()); | 131 image = gfx::Image(ns_image.release()); |
171 } | 132 } |
172 } | 133 } |
173 | 134 |
174 if (image.IsEmpty()) { | 135 if (image.IsEmpty()) { |
175 base::scoped_nsobject<NSImage> ns_image; | 136 base::scoped_nsobject<NSImage> ns_image; |
176 for (size_t i = 0; i < data_packs_.size(); ++i) { | 137 for (size_t i = 0; i < data_packs_.size(); ++i) { |
177 if (data_packs_[i]->HasOnlyMaterialDesignAssets()) | |
178 continue; | |
179 scoped_refptr<base::RefCountedStaticMemory> data( | 138 scoped_refptr<base::RefCountedStaticMemory> data( |
180 data_packs_[i]->GetStaticMemory(resource_id)); | 139 data_packs_[i]->GetStaticMemory(resource_id)); |
181 if (!data.get()) | 140 if (!data.get()) |
182 continue; | 141 continue; |
183 | 142 |
184 base::scoped_nsobject<NSData> ns_data( | 143 base::scoped_nsobject<NSData> ns_data( |
185 [[NSData alloc] initWithBytes:data->front() length:data->size()]); | 144 [[NSData alloc] initWithBytes:data->front() length:data->size()]); |
186 if (!ns_image.get()) { | 145 if (!ns_image.get()) { |
187 ns_image.reset([[NSImage alloc] initWithData:ns_data]); | 146 ns_image.reset([[NSImage alloc] initWithData:ns_data]); |
188 } else { | 147 } else { |
(...skipping 16 matching lines...) Expand all Loading... |
205 | 164 |
206 // Another thread raced the load and has already cached the image. | 165 // Another thread raced the load and has already cached the image. |
207 if (images_.count(resource_id)) | 166 if (images_.count(resource_id)) |
208 return images_[resource_id]; | 167 return images_[resource_id]; |
209 | 168 |
210 images_[resource_id] = image; | 169 images_[resource_id] = image; |
211 return images_[resource_id]; | 170 return images_[resource_id]; |
212 } | 171 } |
213 | 172 |
214 } // namespace ui | 173 } // namespace ui |
OLD | NEW |