diff options
author | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-09 20:59:32 +0000 |
---|---|---|
committer | thomasvl@chromium.org <thomasvl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-09 20:59:32 +0000 |
commit | 655345e0991a974109e0f2656f6e00afbe8d9e59 (patch) | |
tree | 7fcca7714b4957f9efed3496e863c4f15b48941a /chrome/browser/cocoa/nsimage_cache.mm | |
parent | 5ce8cbcbf65f801bc7f52cc035027d0eb907add5 (diff) | |
download | chromium_src-655345e0991a974109e0f2656f6e00afbe8d9e59.zip chromium_src-655345e0991a974109e0f2656f6e00afbe8d9e59.tar.gz chromium_src-655345e0991a974109e0f2656f6e00afbe8d9e59.tar.bz2 |
Added an helper namespace that provides fetches images from the app bundle and caches them, line NSImage imageNamed: but lets us control the bundle that's checked.
Unittest for the helper.
Updated the current places that use +[NSImage imageNamed:] to use our cache helper.
BUG=none
TEST=All the ui images still load.
Review URL: http://codereview.chromium.org/149393
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20309 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa/nsimage_cache.mm')
-rw-r--r-- | chrome/browser/cocoa/nsimage_cache.mm | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/nsimage_cache.mm b/chrome/browser/cocoa/nsimage_cache.mm new file mode 100644 index 0000000..48e34ed --- /dev/null +++ b/chrome/browser/cocoa/nsimage_cache.mm @@ -0,0 +1,55 @@ +// 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]; + [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 put 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 |