diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-22 20:56:29 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-22 20:56:29 +0000 |
commit | 930511ae0f8ab6bc43d5e1972c884e72020bd85e (patch) | |
tree | 2a5c3eccec5a7b632ff2360b9332f55700e88ddd /base | |
parent | d8cca99869c465f0567a61e43a5e69e616a26286 (diff) | |
download | chromium_src-930511ae0f8ab6bc43d5e1972c884e72020bd85e.zip chromium_src-930511ae0f8ab6bc43d5e1972c884e72020bd85e.tar.gz chromium_src-930511ae0f8ab6bc43d5e1972c884e72020bd85e.tar.bz2 |
Revert accidental delete of compat_execinfo.
TEST=it compiles
BUG=none
Review URL: http://codereview.chromium.org/5961007
TBR=brettw@chromium.org
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69977 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/compat_execinfo.h | 34 | ||||
-rw-r--r-- | base/nsimage_cache_mac.h | 33 | ||||
-rw-r--r-- | base/nsimage_cache_mac.mm | 73 |
3 files changed, 140 insertions, 0 deletions
diff --git a/base/compat_execinfo.h b/base/compat_execinfo.h new file mode 100644 index 0000000..3d4cc43 --- /dev/null +++ b/base/compat_execinfo.h @@ -0,0 +1,34 @@ +// Copyright (c) 2006-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. + +// A file you can include instead of <execinfo.h> if your project might need +// to run on Mac OS X 10.4. + +#ifndef BASE_COMPAT_EXECINFO_H_ +#define BASE_COMPAT_EXECINFO_H_ +#pragma once + +#include "build/build_config.h" + +#if defined(OS_MACOSX) +#include <AvailabilityMacros.h> +#endif + +#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 +// Manually define these here as weak imports, rather than including execinfo.h. +// This lets us launch on 10.4 which does not have these calls. +extern "C" { + +extern int backtrace(void**, int) __attribute__((weak_import)); +extern char** backtrace_symbols(void* const*, int) + __attribute__((weak_import)); +extern void backtrace_symbols_fd(void* const*, int, int) + __attribute__((weak_import)); + +} // extern "C" +#else +#include <execinfo.h> +#endif + +#endif // BASE_COMPAT_EXECINFO_H_ diff --git a/base/nsimage_cache_mac.h b/base/nsimage_cache_mac.h new file mode 100644 index 0000000..b13eac9 --- /dev/null +++ b/base/nsimage_cache_mac.h @@ -0,0 +1,33 @@ +// 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. + +#ifndef BASE_NSIMAGE_CACHE_MAC_H_ +#define BASE_NSIMAGE_CACHE_MAC_H_ +#pragma once + +#ifdef __OBJC__ +@class NSImage; +@class NSString; +#else +class NSImage; +class NSString; +#endif + +namespace nsimage_cache { + +// 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* ImageNamed(NSString* name); + +// Clears the cache. +void Clear(void); + +} // namespace nsimage_cache + +#endif // BASE_NSIMAGE_CACHE_MAC_H_ diff --git a/base/nsimage_cache_mac.mm b/base/nsimage_cache_mac.mm new file mode 100644 index 0000000..e693ed4 --- /dev/null +++ b/base/nsimage_cache_mac.mm @@ -0,0 +1,73 @@ +// 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 "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) { + 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 Clear(void) { + // NOTE: to make this thread safe, we'd have to sync on the cache. + [image_cache removeAllObjects]; +} + +} // namespace nsimage_cache |