diff options
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_popup_view_mac.mm | 19 | ||||
-rw-r--r-- | chrome/browser/cocoa/blocked_popup_container_controller.mm | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/nsimage_cache.h | 26 | ||||
-rw-r--r-- | chrome/browser/cocoa/nsimage_cache.mm | 55 | ||||
-rw-r--r-- | chrome/browser/cocoa/nsimage_cache_unittest.mm | 55 | ||||
-rw-r--r-- | chrome/browser/cocoa/sad_tab_view.mm | 3 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_controller.mm | 3 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.mm | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_view.mm | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/toolbar_controller.mm | 13 | ||||
-rw-r--r-- | chrome/chrome.gyp | 3 |
11 files changed, 169 insertions, 23 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm index ca4c8ec..f2a33d8 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm @@ -8,6 +8,7 @@ #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" +#include "chrome/browser/cocoa/nsimage_cache.h" namespace { @@ -40,28 +41,28 @@ static const NSColor* DescriptionTextColor() { // gtk code. NSImage* MatchIcon(const AutocompleteMatch& match) { if (match.starred) { - return [NSImage imageNamed:@"o2_star.png"]; + return nsimage_cache::ImageNamed(@"o2_star.png"); } switch (match.type) { case AutocompleteMatch::URL_WHAT_YOU_TYPED: case AutocompleteMatch::NAVSUGGEST: { - return [NSImage imageNamed:@"o2_globe.png"]; + return nsimage_cache::ImageNamed(@"o2_globe.png"); } case AutocompleteMatch::HISTORY_URL: case AutocompleteMatch::HISTORY_TITLE: case AutocompleteMatch::HISTORY_BODY: case AutocompleteMatch::HISTORY_KEYWORD: { - return [NSImage imageNamed:@"o2_history.png"]; + return nsimage_cache::ImageNamed(@"o2_history.png"); } case AutocompleteMatch::SEARCH_WHAT_YOU_TYPED: case AutocompleteMatch::SEARCH_HISTORY: case AutocompleteMatch::SEARCH_SUGGEST: case AutocompleteMatch::SEARCH_OTHER_ENGINE: { - return [NSImage imageNamed:@"o2_search.png"]; + return nsimage_cache::ImageNamed(@"o2_search.png"); } case AutocompleteMatch::OPEN_HISTORY_PAGE: { - return [NSImage imageNamed:@"o2_more.png"]; + return nsimage_cache::ImageNamed(@"o2_more.png"); } default: NOTREACHED(); @@ -141,11 +142,11 @@ NSAttributedString* AutocompletePopupViewMac::MatchText( NSAttributedString* emDash = [[[NSAttributedString alloc] initWithString:rawEmDash attributes:attributes] autorelease]; - + NSAttributedString* description = DecorateMatchedString(match.description, match.description_class, DescriptionTextColor(), font); - + [as appendAttributedString:emDash]; [as appendAttributedString:description]; } @@ -277,7 +278,7 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() { // because the popup doesn't need any of the field-like aspects of // field_. The edit view could expose helper methods for attaching // the window to the field. - + // Locate |field_| on the screen. NSRect r = [field_ convertRect:[field_ bounds] toView:nil]; r.origin = [[field_ window] convertBaseToScreen:r.origin]; @@ -306,7 +307,7 @@ void AutocompletePopupViewMac::UpdatePopupAppearance() { [[field_ window] addChildWindow:popup_ ordered:NSWindowAbove]; } } - + // This is only called by model in SetSelectedLine() after updating // everything. Popup should already be visible. void AutocompletePopupViewMac::PaintUpdatesNow() { diff --git a/chrome/browser/cocoa/blocked_popup_container_controller.mm b/chrome/browser/cocoa/blocked_popup_container_controller.mm index 1d0182f..4df3dba 100644 --- a/chrome/browser/cocoa/blocked_popup_container_controller.mm +++ b/chrome/browser/cocoa/blocked_popup_container_controller.mm @@ -6,6 +6,7 @@ #include "app/l10n_util.h" #include "base/sys_string_conversions.h" +#include "chrome/browser/cocoa/nsimage_cache.h" #include "chrome/browser/tab_contents/tab_contents.h" #include "chrome/browser/tab_contents/tab_contents_view.h" #include "grit/generated_resources.h" @@ -109,8 +110,8 @@ class BlockedPopupContainerViewBridge : public BlockedPopupContainerView { kCloseBoxSize); NSButton* close = [[[NSButton alloc] initWithFrame:closeFrame] autorelease]; [close setAutoresizingMask:NSViewMinXMargin]; - [close setImage:[NSImage imageNamed:@"close_bar"]]; - [close setAlternateImage:[NSImage imageNamed:@"close_bar_p"]]; + [close setImage:nsimage_cache::ImageNamed(@"close_bar.pdf")]; + [close setAlternateImage:nsimage_cache::ImageNamed(@"close_bar_p.pdf")]; [close setBordered:NO]; [close setTarget:self]; [close setAction:@selector(closePopup:)]; diff --git a/chrome/browser/cocoa/nsimage_cache.h b/chrome/browser/cocoa/nsimage_cache.h new file mode 100644 index 0000000..2d2511e --- /dev/null +++ b/chrome/browser/cocoa/nsimage_cache.h @@ -0,0 +1,26 @@ +// 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 CHROME_BROWSER_COCOA_IMAGE_CACHE_H_ +#define CHROME_BROWSER_COCOA_IMAGE_CACHE_H_ + +#import <Cocoa/Cocoa.h> + +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); + +} + +#endif // CHROME_BROWSER_COCOA_IMAGE_CACHE_H_ 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 diff --git a/chrome/browser/cocoa/nsimage_cache_unittest.mm b/chrome/browser/cocoa/nsimage_cache_unittest.mm new file mode 100644 index 0000000..149fb60 --- /dev/null +++ b/chrome/browser/cocoa/nsimage_cache_unittest.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. + +#import <Cocoa/Cocoa.h> + +#include "base/file_path.h" +#include "base/mac_util.h" +#include "base/path_service.h" +#include "chrome/browser/cocoa/nsimage_cache.h" +#include "chrome/common/mac_app_names.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class NSImageCacheTest : public testing::Test { + public: + NSImageCacheTest() { + // Look in the Chromium app bundle for resources. + FilePath path; + PathService::Get(base::DIR_EXE, &path); + path = path.AppendASCII(MAC_BROWSER_APP_NAME); + mac_util::SetOverrideAppBundlePath(path); + } + virtual ~NSImageCacheTest() { + mac_util::SetOverrideAppBundle(nil); + } +}; + +TEST_F(NSImageCacheTest, LookupFound) { + EXPECT_TRUE(nsimage_cache::ImageNamed(@"back.pdf") != nil) + << "Failed to find the toolbar image?"; +} + +TEST_F(NSImageCacheTest, LookupCached) { + EXPECT_EQ(nsimage_cache::ImageNamed(@"back.pdf"), + nsimage_cache::ImageNamed(@"back.pdf")) + << "Didn't get the same NSImage back?"; +} + +TEST_F(NSImageCacheTest, LookupMiss) { + EXPECT_TRUE(nsimage_cache::ImageNamed(@"should_not.exist") == nil) + << "There shouldn't be an image with this name?"; +} + +TEST_F(NSImageCacheTest, LookupFoundAndClear) { + NSImage *first = nsimage_cache::ImageNamed(@"back.pdf"); + EXPECT_TRUE(first != nil) + << "Failed to find the toolbar image?"; + nsimage_cache::Clear(); + EXPECT_NE(first, nsimage_cache::ImageNamed(@"back.pdf")) + << "how'd we get the same image after a cache clear?"; +} + +} // namespace diff --git a/chrome/browser/cocoa/sad_tab_view.mm b/chrome/browser/cocoa/sad_tab_view.mm index 78d4793..a6ae4a9 100644 --- a/chrome/browser/cocoa/sad_tab_view.mm +++ b/chrome/browser/cocoa/sad_tab_view.mm @@ -6,6 +6,7 @@ #include "app/l10n_util.h" #include "base/sys_string_conversions.h" +#include "chrome/browser/cocoa/nsimage_cache.h" #include "grit/generated_resources.h" static const int kSadTabOffset = -64; @@ -15,7 +16,7 @@ static const int kTitleMessageSpacing = 15; @implementation SadTabView - (void)drawRect:(NSRect)dirtyRect { - NSImage* sadTabImage = [NSImage imageNamed:@"sadtab"]; + NSImage* sadTabImage = nsimage_cache::ImageNamed(@"sadtab.png"); NSString* title = base::SysWideToNSString(l10n_util::GetString(IDS_SAD_TAB_TITLE)); NSString* message = diff --git a/chrome/browser/cocoa/tab_controller.mm b/chrome/browser/cocoa/tab_controller.mm index e2eb3b5..897312a 100644 --- a/chrome/browser/cocoa/tab_controller.mm +++ b/chrome/browser/cocoa/tab_controller.mm @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/mac_util.h" +#include "chrome/browser/cocoa/nsimage_cache.h" #import "chrome/browser/cocoa/tab_controller.h" #import "chrome/browser/cocoa/tab_controller_target.h" @@ -41,7 +42,7 @@ // Called when the tab's nib is done loading and all outlets are hooked up. - (void)awakeFromNib { - [(id)iconView_ setImage:[NSImage imageNamed:@"nav"]]; + [(id)iconView_ setImage:nsimage_cache::ImageNamed(@"nav.pdf")]; [[self view] addSubview:backgroundButton_ positioned:NSWindowBelow relativeTo:nil]; diff --git a/chrome/browser/cocoa/tab_strip_controller.mm b/chrome/browser/cocoa/tab_strip_controller.mm index 14704ca..4ad5f2c 100644 --- a/chrome/browser/cocoa/tab_strip_controller.mm +++ b/chrome/browser/cocoa/tab_strip_controller.mm @@ -9,6 +9,7 @@ #include "base/sys_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" #include "chrome/browser/browser.h" +#include "chrome/browser/cocoa/nsimage_cache.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/profile.h" #import "chrome/browser/cocoa/tab_strip_view.h" @@ -487,9 +488,9 @@ NSString* const kTabStripNumberOfTabsChanged = @"kTabStripNumberOfTabsChanged"; // over. if (contents) { static NSImage* throbberWaitingImage = - [[NSImage imageNamed:@"throbber_waiting"] retain]; + [nsimage_cache::ImageNamed(@"throbber_waiting.png") retain]; static NSImage* throbberLoadingImage = - [[NSImage imageNamed:@"throbber"] retain]; + [nsimage_cache::ImageNamed(@"throbber.png") retain]; TabController* tabController = [tabArray_ objectAtIndex:index]; diff --git a/chrome/browser/cocoa/tab_view.mm b/chrome/browser/cocoa/tab_view.mm index 5b215fe..44d43ac 100644 --- a/chrome/browser/cocoa/tab_view.mm +++ b/chrome/browser/cocoa/tab_view.mm @@ -2,6 +2,7 @@ // 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 "chrome/browser/cocoa/tab_controller.h" #include "chrome/browser/cocoa/tab_view.h" #include "chrome/browser/cocoa/tab_window_controller.h" @@ -47,13 +48,13 @@ - (void)mouseEntered:(NSEvent *)theEvent { // We only set up one tracking area, so we know any mouseEntered: // messages are for close button mouseovers. - [closeButton_ setImage:[NSImage imageNamed:@"close_bar_h"]]; + [closeButton_ setImage:nsimage_cache::ImageNamed(@"close_bar_h.pdf")]; } - (void)mouseExited:(NSEvent *)theEvent { // We only set up one tracking area, so we know any mouseExited: // messages are for close button mouseovers. - [closeButton_ setImage:[NSImage imageNamed:@"close_bar"]]; + [closeButton_ setImage:nsimage_cache::ImageNamed(@"close_bar.pdf")]; } // Determines which view a click in our frame actually hit. It's either this diff --git a/chrome/browser/cocoa/toolbar_controller.mm b/chrome/browser/cocoa/toolbar_controller.mm index eba4d89..d7cd131 100644 --- a/chrome/browser/cocoa/toolbar_controller.mm +++ b/chrome/browser/cocoa/toolbar_controller.mm @@ -8,6 +8,7 @@ #include "base/sys_string_conversions.h" #include "chrome/app/chrome_dll_resource.h" #import "chrome/browser/cocoa/location_bar_view_mac.h" +#include "chrome/browser/cocoa/nsimage_cache.h" #include "chrome/browser/profile.h" #include "chrome/browser/toolbar_model.h" #include "chrome/common/notification_details.h" @@ -17,8 +18,8 @@ #include "chrome/common/pref_service.h" // Names of images in the bundle for the star icon (normal and 'starred'). -static NSString* const kStarImageName = @"star"; -static NSString* const kStarredImageName = @"starred"; +static NSString* const kStarImageName = @"star.pdf"; +static NSString* const kStarredImageName = @"starred.pdf"; @implementation LocationBarFieldEditor - (void)copy:(id)sender { @@ -175,17 +176,17 @@ class PrefObserverBridge : public NotificationObserver { NSString* starImageName = kStarImageName; if (isStarred) starImageName = kStarredImageName; - [starButton_ setImage:[NSImage imageNamed:starImageName]]; + [starButton_ setImage:nsimage_cache::ImageNamed(starImageName)]; } - (void)setIsLoading:(BOOL)isLoading { - NSString* imageName = @"go"; + NSString* imageName = @"go.pdf"; NSInteger tag = IDC_GO; if (isLoading) { - imageName = @"stop"; + imageName = @"stop.pdf"; tag = IDC_STOP; } - [goButton_ setImage:[NSImage imageNamed:imageName]]; + [goButton_ setImage:nsimage_cache::ImageNamed(imageName)]; [goButton_ setTag:tag]; } diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index ade7104..ab62c41 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -787,6 +787,8 @@ 'browser/cocoa/location_bar_view_mac.mm', 'browser/cocoa/menu_localizer.h', 'browser/cocoa/menu_localizer.mm', + 'browser/cocoa/nsimage_cache.h', + 'browser/cocoa/nsimage_cache.mm', 'browser/cocoa/page_info_window_controller.h', 'browser/cocoa/page_info_window_controller.mm', 'browser/cocoa/page_info_window_mac.h', @@ -3478,6 +3480,7 @@ 'browser/cocoa/location_bar_view_mac_unittest.mm', 'browser/cocoa/location_bar_fieldeditor_unittest.mm', 'browser/cocoa/gradient_button_cell_unittest.mm', + 'browser/cocoa/nsimage_cache_unittest.mm', 'browser/cocoa/preferences_window_controller_unittest.mm', 'browser/cocoa/rwhvm_editcommand_helper_unittest.mm', 'browser/cocoa/sad_tab_view_unittest.mm', |