summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/app_base.gypi2
-rw-r--r--app/mac/nsimage_cache.h35
-rw-r--r--app/mac/nsimage_cache.mm75
-rw-r--r--app/mac/nsimage_cache_unittest.cc.README3
4 files changed, 115 insertions, 0 deletions
diff --git a/app/app_base.gypi b/app/app_base.gypi
index 3715498..cc2c0e6 100644
--- a/app/app_base.gypi
+++ b/app/app_base.gypi
@@ -168,6 +168,8 @@
'l10n_util_win.h',
'linear_animation.cc',
'linear_animation.h',
+ 'mac/nsimage_cache.h',
+ 'mac/nsimage_cache.mm',
'mac/scoped_nsdisable_screen_updates.h',
'menus/accelerator.h',
'menus/accelerator_gtk.h',
diff --git a/app/mac/nsimage_cache.h b/app/mac/nsimage_cache.h
new file mode 100644
index 0000000..ed1de06
--- /dev/null
+++ b/app/mac/nsimage_cache.h
@@ -0,0 +1,35 @@
+// Copyright (c) 2010 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.
+
+#ifndef APP_MAC_NSIMAGE_CACHE_H_
+#define APP_MAC_NSIMAGE_CACHE_H_
+#pragma once
+
+#ifdef __OBJC__
+@class NSImage;
+@class NSString;
+#else
+class NSImage;
+class NSString;
+#endif
+
+namespace app {
+namespace mac {
+
+// Returns an autoreleased image from the main app bundle
+// (mac_util::MainAppBundle()) with the given name, and keeps it in memory so
+// future fetches are fast.
+// NOTE:
+// - This should only be called on the main thread.
+// - The caller should retain the image if they want to keep it around, as
+// the cache could have limit on size/lifetime, etc.
+NSImage* GetCachedImageWithName(NSString* name);
+
+// Clears the image cache.
+void ClearCachedImages(void);
+
+} // namespace mac
+} // namespace app
+
+#endif // APP_MAC_NSIMAGE_CACHE_H_
diff --git a/app/mac/nsimage_cache.mm b/app/mac/nsimage_cache.mm
new file mode 100644
index 0000000..a5d645e
--- /dev/null
+++ b/app/mac/nsimage_cache.mm
@@ -0,0 +1,75 @@
+// Copyright (c) 2010 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 "app/mac/nsimage_cache.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 app {
+namespace mac {
+
+static NSMutableDictionary* image_cache = nil;
+
+NSImage* GetCachedImageWithName(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) {
+ DVLOG_IF(1, [[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) {
+ // Auto-template images with names ending in "Template".
+ NSString* extensionlessName = [name stringByDeletingPathExtension];
+ if ([extensionlessName hasSuffix:@"Template"])
+ [result setTemplate:YES];
+
+ [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] UTF8String];
+ result = nil;
+ }
+ }
+ }
+
+ // TODO: if we ever limit the cache size, this should retain & autorelease
+ // the image.
+ return result;
+}
+
+void ClearCachedImages(void) {
+ // NOTE: to make this thread safe, we'd have to sync on the cache.
+ [image_cache removeAllObjects];
+}
+
+} // namespace mac
+} // namespace app
diff --git a/app/mac/nsimage_cache_unittest.cc.README b/app/mac/nsimage_cache_unittest.cc.README
new file mode 100644
index 0000000..558426f
--- /dev/null
+++ b/app/mac/nsimage_cache_unittest.cc.README
@@ -0,0 +1,3 @@
+The unit test for this file is in
+chrome/browser/ui/cocoa/nsimage_cache_unittest.mm since it uses certain Chrome
+resources for the test.