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 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/mac/bundle_locations.h" | 12 #include "base/mac/bundle_locations.h" |
13 #include "base/mac/mac_util.h" | 13 #include "base/mac/mac_util.h" |
| 14 #include "base/mac/scoped_nsobject.h" |
14 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
15 #include "base/memory/scoped_nsobject.h" | |
16 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "ui/base/resource/resource_handle.h" | 18 #include "ui/base/resource/resource_handle.h" |
19 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
20 | 20 |
21 namespace ui { | 21 namespace ui { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 base::FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { | 25 base::FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 112 } |
113 return images_[resource_id]; | 113 return images_[resource_id]; |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 gfx::Image image; | 117 gfx::Image image; |
118 if (delegate_) | 118 if (delegate_) |
119 image = delegate_->GetNativeImageNamed(resource_id, rtl); | 119 image = delegate_->GetNativeImageNamed(resource_id, rtl); |
120 | 120 |
121 if (image.IsEmpty()) { | 121 if (image.IsEmpty()) { |
122 scoped_nsobject<NSImage> ns_image; | 122 base::scoped_nsobject<NSImage> ns_image; |
123 for (size_t i = 0; i < data_packs_.size(); ++i) { | 123 for (size_t i = 0; i < data_packs_.size(); ++i) { |
124 scoped_refptr<base::RefCountedStaticMemory> data( | 124 scoped_refptr<base::RefCountedStaticMemory> data( |
125 data_packs_[i]->GetStaticMemory(resource_id)); | 125 data_packs_[i]->GetStaticMemory(resource_id)); |
126 if (!data.get()) | 126 if (!data.get()) |
127 continue; | 127 continue; |
128 | 128 |
129 scoped_nsobject<NSData> ns_data( | 129 base::scoped_nsobject<NSData> ns_data( |
130 [[NSData alloc] initWithBytes:data->front() | 130 [[NSData alloc] initWithBytes:data->front() length:data->size()]); |
131 length:data->size()]); | |
132 if (!ns_image.get()) { | 131 if (!ns_image.get()) { |
133 ns_image.reset([[NSImage alloc] initWithData:ns_data]); | 132 ns_image.reset([[NSImage alloc] initWithData:ns_data]); |
134 } else { | 133 } else { |
135 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; | 134 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; |
136 if (image_rep) | 135 if (image_rep) |
137 [ns_image addRepresentation:image_rep]; | 136 [ns_image addRepresentation:image_rep]; |
138 } | 137 } |
139 } | 138 } |
140 | 139 |
141 if (!ns_image.get()) { | 140 if (!ns_image.get()) { |
142 LOG(WARNING) << "Unable to load image with id " << resource_id; | 141 LOG(WARNING) << "Unable to load image with id " << resource_id; |
143 NOTREACHED(); // Want to assert in debug mode. | 142 NOTREACHED(); // Want to assert in debug mode. |
144 return GetEmptyImage(); | 143 return GetEmptyImage(); |
145 } | 144 } |
146 | 145 |
147 image = gfx::Image(ns_image.release()); | 146 image = gfx::Image(ns_image.release()); |
148 } | 147 } |
149 | 148 |
150 base::AutoLock lock(*images_and_fonts_lock_); | 149 base::AutoLock lock(*images_and_fonts_lock_); |
151 | 150 |
152 // Another thread raced the load and has already cached the image. | 151 // Another thread raced the load and has already cached the image. |
153 if (images_.count(resource_id)) | 152 if (images_.count(resource_id)) |
154 return images_[resource_id]; | 153 return images_[resource_id]; |
155 | 154 |
156 images_[resource_id] = image; | 155 images_[resource_id] = image; |
157 return images_[resource_id]; | 156 return images_[resource_id]; |
158 } | 157 } |
159 | 158 |
160 } // namespace ui | 159 } // namespace ui |
OLD | NEW |