blob: 05957ea68adfdbf42a982602c5119a5439c808a5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/cocoa/nsimage_cache.h"
#include "base/logging.h"
#include "base/mac_util.h"
namespace nsimage_cache {
static NSMutableDictionary *image_cache = nil;
NSImage *ImageNamed(NSString *name) {
DCHECK(name);
// NOTE: to make this thread safe, we'd have to sync on the cache and
// also force all the bundle calls on the main thread.
if (!image_cache) {
image_cache = [[NSMutableDictionary alloc] init];
DCHECK(image_cache);
}
NSImage *result = [image_cache objectForKey:name];
if (!result) {
DLOG_IF(INFO, [[name pathExtension] length] == 0)
<< "Suggest including the extension in the image name";
NSString *path = [mac_util::MainAppBundle() pathForImageResource:name];
if (path) {
@try {
result = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
if (result)
[image_cache setObject:result forKey:name];
}
@catch (id err) {
DLOG(ERROR) << "Failed to load the image for name '"
<< [name UTF8String] << "' from path '" << [path UTF8String]
<< "', error: " << [err description];
result = nil;
}
}
}
// TODO: if we ever limit the cache size, this should retain & autorelease
// the image.
return result;
}
void Clear(void) {
// NOTE: to make this thread safe, we'd have to sync on the cache.
[image_cache removeAllObjects];
}
} // nsimage_cache
|