summaryrefslogtreecommitdiffstats
path: root/base/nsimage_cache_mac.mm
diff options
context:
space:
mode:
authormark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-05 19:07:34 +0000
committermark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-05 19:07:34 +0000
commit3075a7fdc642d705bec27b857d284b5b12c6dff0 (patch)
treedecd2068a70a330e44dae9b7a5f4e26f73ea33cc /base/nsimage_cache_mac.mm
parenta3416f9ba02ce9ca1e52b033fafabeffab50dceb (diff)
downloadchromium_src-3075a7fdc642d705bec27b857d284b5b12c6dff0.zip
chromium_src-3075a7fdc642d705bec27b857d284b5b12c6dff0.tar.gz
chromium_src-3075a7fdc642d705bec27b857d284b5b12c6dff0.tar.bz2
Move nsimage_cache into base so that it can be accessed from outside of chrome.
This is in support of the next round of rebundling. TEST=does it still build, do the unit tests still pass, does it still work? BUG=14610 Review URL: http://codereview.chromium.org/243100 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28016 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/nsimage_cache_mac.mm')
-rw-r--r--base/nsimage_cache_mac.mm67
1 files changed, 67 insertions, 0 deletions
diff --git a/base/nsimage_cache_mac.mm b/base/nsimage_cache_mac.mm
new file mode 100644
index 0000000..7ca05be
--- /dev/null
+++ b/base/nsimage_cache_mac.mm
@@ -0,0 +1,67 @@
+// 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 "base/nsimage_cache_mac.h"
+
+#import <AppKit/AppKit.h>
+
+#include "base/logging.h"
+#include "base/mac_util.h"
+
+// When C++ exceptions are disabled, the C++ library defines |try| and
+// |catch| so as to allow exception-expecting C++ code to build properly when
+// language support for exceptions is not present. These macros interfere
+// with the use of |@try| and |@catch| in Objective-C files such as this one.
+// Undefine these macros here, after everything has been #included, since
+// there will be no C++ uses and only Objective-C uses from this point on.
+#undef try
+#undef catch
+
+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];
+}
+
+} // namespace nsimage_cache