summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-16 01:04:48 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-16 01:04:48 +0000
commit807871f0e6c4d5898c1e7431266e387c7521e5e5 (patch)
treeec309d04078ad26d68e1fa52b733acc1741a1046 /chrome
parent2c8b3678a3c61f5508f21f8c2b11ef5a6e3afb89 (diff)
downloadchromium_src-807871f0e6c4d5898c1e7431266e387c7521e5e5.zip
chromium_src-807871f0e6c4d5898c1e7431266e387c7521e5e5.tar.gz
chromium_src-807871f0e6c4d5898c1e7431266e387c7521e5e5.tar.bz2
Factor an ExtensionIconSet class out of Extension. This can
be passed to the IO thread or into renderers, where its functionality can be reused without having to reimplement. This will be used in the fix for bug 53495. BUG=53495 TEST=unit_tests --gtest_filter=Extension* Review URL: http://codereview.chromium.org/3432001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59597 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/cocoa/extensions/extension_infobar_controller.mm10
-rw-r--r--chrome/browser/dom_ui/app_launcher_handler.cc3
-rw-r--r--chrome/browser/extensions/extension_dom_ui.cc4
-rw-r--r--chrome/browser/extensions/extension_icon_manager.cc6
-rw-r--r--chrome/browser/extensions/extension_install_ui.cc4
-rw-r--r--chrome/browser/extensions/extension_management_api.cc4
-rw-r--r--chrome/browser/extensions/extensions_ui.cc13
-rw-r--r--chrome/browser/extensions/image_loading_tracker_unittest.cc7
-rw-r--r--chrome/browser/gtk/extension_infobar_gtk.cc10
-rw-r--r--chrome/browser/renderer_host/browser_render_process_host.cc6
-rw-r--r--chrome/browser/tab_contents/tab_contents.cc4
-rw-r--r--chrome/browser/views/app_launched_animation_win.cc4
-rw-r--r--chrome/browser/views/infobars/extension_infobar.cc10
-rw-r--r--chrome/chrome_common.gypi2
-rw-r--r--chrome/chrome_tests.gypi1
-rw-r--r--chrome/common/extensions/extension.cc56
-rw-r--r--chrome/common/extensions/extension.h30
-rw-r--r--chrome/common/extensions/extension_file_util.cc6
-rw-r--r--chrome/common/extensions/extension_icon_set.cc45
-rw-r--r--chrome/common/extensions/extension_icon_set.h39
-rw-r--r--chrome/common/extensions/extension_icon_set_unittest.cc35
21 files changed, 195 insertions, 104 deletions
diff --git a/chrome/browser/cocoa/extensions/extension_infobar_controller.mm b/chrome/browser/cocoa/extensions/extension_infobar_controller.mm
index 5011a24..1bb1a5f 100644
--- a/chrome/browser/cocoa/extensions/extension_infobar_controller.mm
+++ b/chrome/browser/cocoa/extensions/extension_infobar_controller.mm
@@ -15,6 +15,7 @@
#include "chrome/browser/extensions/extension_infobar_delegate.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "gfx/canvas_skia.h"
#include "grit/theme_resources.h"
@@ -64,12 +65,13 @@ class InfobarBridge : public ExtensionInfoBarDelegate::DelegateObserver,
// Load the Extension's icon image.
void LoadIcon() {
- ExtensionResource icon_resource;
Extension* extension = delegate_->extension_host()->extension();
- Extension::Icons size = extension->GetIconResourceAllowLargerSize(
- &icon_resource, Extension::EXTENSION_ICON_BITTY);
+ ExtensionResource icon_resource = extension->GetIconResource(
+ Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY);
if (!icon_resource.relative_path().empty()) {
- tracker_.LoadImage(extension, icon_resource, gfx::Size(size, size),
+ tracker_.LoadImage(extension, icon_resource,
+ gfx::Size(Extension::EXTENSION_ICON_BITTY,
+ Extension::EXTENSION_ICON_BITTY),
ImageLoadingTracker::DONT_CACHE);
} else {
OnImageLoaded(NULL, icon_resource, 0);
diff --git a/chrome/browser/dom_ui/app_launcher_handler.cc b/chrome/browser/dom_ui/app_launcher_handler.cc
index c63067e..d85c2c5 100644
--- a/chrome/browser/dom_ui/app_launcher_handler.cc
+++ b/chrome/browser/dom_ui/app_launcher_handler.cc
@@ -18,6 +18,7 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
@@ -42,7 +43,7 @@ bool ExtractInt(const ListValue* list, size_t index, int* out_int) {
std::string GetIconURL(Extension* extension, Extension::Icons icon,
const std::string& default_val) {
- GURL url = extension->GetIconURL(icon);
+ GURL url = extension->GetIconURL(icon, ExtensionIconSet::MATCH_EXACTLY);
if (!url.is_empty())
return url.spec();
else
diff --git a/chrome/browser/extensions/extension_dom_ui.cc b/chrome/browser/extensions/extension_dom_ui.cc
index 659eb98..41b86a8 100644
--- a/chrome/browser/extensions/extension_dom_ui.cc
+++ b/chrome/browser/extensions/extension_dom_ui.cc
@@ -24,6 +24,7 @@
#include "chrome/common/page_transition_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/url_constants.h"
#include "gfx/codec/png_codec.h"
@@ -71,7 +72,8 @@ class ExtensionDOMUIImageLoadingTracker : public ImageLoadingTracker::Observer {
void Init() {
if (extension_) {
ExtensionResource icon_resource =
- extension_->GetIconResource(Extension::EXTENSION_ICON_BITTY);
+ extension_->GetIconResource(Extension::EXTENSION_ICON_BITTY,
+ ExtensionIconSet::MATCH_EXACTLY);
tracker_.LoadImage(extension_, icon_resource,
gfx::Size(kFavIconSize, kFavIconSize),
diff --git a/chrome/browser/extensions/extension_icon_manager.cc b/chrome/browser/extensions/extension_icon_manager.cc
index 69d4952..23eeec5 100644
--- a/chrome/browser/extensions/extension_icon_manager.cc
+++ b/chrome/browser/extensions/extension_icon_manager.cc
@@ -8,6 +8,7 @@
#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "gfx/canvas_skia.h"
#include "gfx/color_utils.h"
@@ -45,9 +46,8 @@ ExtensionIconManager::~ExtensionIconManager() {
}
void ExtensionIconManager::LoadIcon(Extension* extension) {
- ExtensionResource icon_resource;
- extension->GetIconResourceAllowLargerSize(&icon_resource,
- Extension::EXTENSION_ICON_BITTY);
+ ExtensionResource icon_resource = extension->GetIconResource(
+ Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_BIGGER);
if (!icon_resource.extension_root().empty()) {
image_tracker_.LoadImage(extension,
icon_resource,
diff --git a/chrome/browser/extensions/extension_install_ui.cc b/chrome/browser/extensions/extension_install_ui.cc
index 80c7279..e7f2ba6 100644
--- a/chrome/browser/extensions/extension_install_ui.cc
+++ b/chrome/browser/extensions/extension_install_ui.cc
@@ -28,6 +28,7 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_action.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/extensions/url_pattern.h"
#include "chrome/common/notification_service.h"
@@ -359,7 +360,8 @@ void ExtensionInstallUI::ShowConfirmation(PromptType prompt_type) {
// Load the image asynchronously. For the response, check OnImageLoaded.
prompt_type_ = prompt_type;
ExtensionResource image =
- extension_->GetIconResource(Extension::EXTENSION_ICON_LARGE);
+ extension_->GetIconResource(Extension::EXTENSION_ICON_LARGE,
+ ExtensionIconSet::MATCH_EXACTLY);
tracker_.LoadImage(extension_, image,
gfx::Size(kIconSize, kIconSize),
ImageLoadingTracker::DONT_CACHE);
diff --git a/chrome/browser/extensions/extension_management_api.cc b/chrome/browser/extensions/extension_management_api.cc
index e51c747..95447a6 100644
--- a/chrome/browser/extensions/extension_management_api.cc
+++ b/chrome/browser/extensions/extension_management_api.cc
@@ -17,8 +17,10 @@
#include "chrome/browser/extensions/extension_updater.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
+#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_error_utils.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
@@ -59,7 +61,7 @@ static DictionaryValue* CreateExtensionInfo(const Extension& extension,
info->SetString(kAppLaunchUrlKey,
extension.GetFullLaunchURL().possibly_invalid_spec());
- const std::map<int, std::string>& icons = extension.icons();
+ const ExtensionIconSet::IconMap& icons = extension.icons().map();
if (!icons.empty()) {
ListValue* icon_list = new ListValue();
std::map<int, std::string>::const_iterator icon_iter;
diff --git a/chrome/browser/extensions/extensions_ui.cc b/chrome/browser/extensions/extensions_ui.cc
index a9fc92b..415a922 100644
--- a/chrome/browser/extensions/extensions_ui.cc
+++ b/chrome/browser/extensions/extensions_ui.cc
@@ -38,6 +38,7 @@
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/user_script.h"
#include "chrome/common/extensions/url_pattern.h"
#include "chrome/common/jstemplate_builder.h"
@@ -404,16 +405,8 @@ void ExtensionsDOMHandler::OnIconsLoaded(DictionaryValue* json) {
ExtensionResource ExtensionsDOMHandler::PickExtensionIcon(
Extension* extension) {
- // Try to fetch the medium sized icon, then (if missing) go for the large one.
- const std::map<int, std::string>& icons = extension->icons();
- std::map<int, std::string>::const_iterator iter =
- icons.find(Extension::EXTENSION_ICON_MEDIUM);
- if (iter == icons.end())
- iter = icons.find(Extension::EXTENSION_ICON_LARGE);
- if (iter != icons.end())
- return extension->GetResource(iter->second);
- else
- return ExtensionResource();
+ return extension->GetIconResource(Extension::EXTENSION_ICON_MEDIUM,
+ ExtensionIconSet::MATCH_BIGGER);
}
ExtensionInstallUI* ExtensionsDOMHandler::GetExtensionInstallUI() {
diff --git a/chrome/browser/extensions/image_loading_tracker_unittest.cc b/chrome/browser/extensions/image_loading_tracker_unittest.cc
index 6c5d329..930d831 100644
--- a/chrome/browser/extensions/image_loading_tracker_unittest.cc
+++ b/chrome/browser/extensions/image_loading_tracker_unittest.cc
@@ -8,6 +8,7 @@
#include "chrome/browser/extensions/image_loading_tracker.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/json_value_serializer.h"
#include "chrome/common/notification_service.h"
@@ -102,7 +103,8 @@ TEST_F(ImageLoadingTrackerTest, Cache) {
ASSERT_TRUE(extension.get() != NULL);
ExtensionResource image_resource =
- extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH);
+ extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::MATCH_EXACTLY);
gfx::Size max_size(Extension::EXTENSION_ICON_SMALLISH,
Extension::EXTENSION_ICON_SMALLISH);
ImageLoadingTracker loader(static_cast<ImageLoadingTracker::Observer*>(this));
@@ -148,7 +150,8 @@ TEST_F(ImageLoadingTrackerTest, DeleteExtensionWhileWaitingForCache) {
ASSERT_TRUE(extension.get() != NULL);
ExtensionResource image_resource =
- extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH);
+ extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::MATCH_EXACTLY);
ImageLoadingTracker loader(static_cast<ImageLoadingTracker::Observer*>(this));
loader.LoadImage(extension.get(),
image_resource,
diff --git a/chrome/browser/gtk/extension_infobar_gtk.cc b/chrome/browser/gtk/extension_infobar_gtk.cc
index 8bf2991..7140afc 100644
--- a/chrome/browser/gtk/extension_infobar_gtk.cc
+++ b/chrome/browser/gtk/extension_infobar_gtk.cc
@@ -9,6 +9,7 @@
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/renderer_host/render_widget_host_view.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "gfx/gtk_util.h"
#include "grit/theme_resources.h"
@@ -46,13 +47,14 @@ void ExtensionInfoBarGtk::OnImageLoaded(
void ExtensionInfoBarGtk::BuildWidgets() {
// Start loading the image for the menu button.
- ExtensionResource icon_resource;
Extension* extension = delegate_->extension_host()->extension();
- Extension::Icons size = extension->GetIconResourceAllowLargerSize(
- &icon_resource, Extension::EXTENSION_ICON_BITTY);
+ ExtensionResource icon_resource = extension->GetIconResource(
+ Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY);
if (!icon_resource.relative_path().empty()) {
// Create a tracker to load the image. It will report back on OnImageLoaded.
- tracker_.LoadImage(extension, icon_resource, gfx::Size(size, size),
+ tracker_.LoadImage(extension, icon_resource,
+ gfx::Size(Extension::EXTENSION_ICON_BITTY,
+ Extension::EXTENSION_ICON_BITTY),
ImageLoadingTracker::DONT_CACHE);
} else {
OnImageLoaded(NULL, icon_resource, 0); // |image|, |index|.
diff --git a/chrome/browser/renderer_host/browser_render_process_host.cc b/chrome/browser/renderer_host/browser_render_process_host.cc
index ce76ff9..7d92c70 100644
--- a/chrome/browser/renderer_host/browser_render_process_host.cc
+++ b/chrome/browser/renderer_host/browser_render_process_host.cc
@@ -46,6 +46,8 @@
#include "chrome/browser/visitedlink_master.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/child_process_info.h"
+#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/gpu_messages.h"
#include "chrome/common/logging_chrome.h"
#include "chrome/common/net/url_request_context_getter.h"
@@ -672,8 +674,8 @@ void BrowserRenderProcessHost::SendExtensionInfo() {
info.web_extent = extension->web_extent();
info.name = extension->name();
info.location = extension->location();
- info.icon_url =
- extension->GetIconURLAllowLargerSize(Extension::EXTENSION_ICON_MEDIUM);
+ info.icon_url = extension->GetIconURL(Extension::EXTENSION_ICON_MEDIUM,
+ ExtensionIconSet::MATCH_BIGGER);
params.extensions.push_back(info);
}
diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc
index eb3826d..67428ce 100644
--- a/chrome/browser/tab_contents/tab_contents.cc
+++ b/chrome/browser/tab_contents/tab_contents.cc
@@ -88,6 +88,7 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_action.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "chrome/common/extensions/url_pattern.h"
#include "chrome/common/navigation_types.h"
@@ -3114,7 +3115,8 @@ void TabContents::UpdateExtensionAppIcon(Extension* extension) {
extension_app_image_loader_.reset(new ImageLoadingTracker(this));
extension_app_image_loader_->LoadImage(
extension,
- extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH),
+ extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
+ ExtensionIconSet::MATCH_EXACTLY),
gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
Extension::EXTENSION_ICON_SMALLISH),
ImageLoadingTracker::CACHE);
diff --git a/chrome/browser/views/app_launched_animation_win.cc b/chrome/browser/views/app_launched_animation_win.cc
index a3d0a57..8a23d43 100644
--- a/chrome/browser/views/app_launched_animation_win.cc
+++ b/chrome/browser/views/app_launched_animation_win.cc
@@ -8,6 +8,7 @@
#include "app/slide_animation.h"
#include "chrome/browser/extensions/image_loading_tracker.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "gfx/rect.h"
#include "views/controls/image_view.h"
@@ -72,7 +73,8 @@ AppLaunchedAnimationWin::AppLaunchedAnimationWin(Extension* extension,
DCHECK(extension);
app_icon_loader_.LoadImage(
extension,
- extension->GetIconResource(Extension::EXTENSION_ICON_LARGE),
+ extension->GetIconResource(Extension::EXTENSION_ICON_LARGE,
+ ExtensionIconSet::MATCH_EXACTLY),
rect_.size(),
ImageLoadingTracker::DONT_CACHE);
}
diff --git a/chrome/browser/views/infobars/extension_infobar.cc b/chrome/browser/views/infobars/extension_infobar.cc
index 3a98740..441a95e 100644
--- a/chrome/browser/views/infobars/extension_infobar.cc
+++ b/chrome/browser/views/infobars/extension_infobar.cc
@@ -12,6 +12,7 @@
#include "chrome/browser/platform_util.h"
#include "chrome/browser/views/frame/browser_view.h"
#include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/extension_resource.h"
#include "gfx/canvas_skia.h"
#include "grit/theme_resources.h"
@@ -163,13 +164,14 @@ void ExtensionInfoBar::SetupIconAndMenu() {
menu_->SetVisible(false);
AddChildView(menu_);
- ExtensionResource icon_resource;
Extension* extension = delegate_->extension_host()->extension();
- Extension::Icons size = extension->GetIconResourceAllowLargerSize(
- &icon_resource, Extension::EXTENSION_ICON_BITTY);
+ ExtensionResource icon_resource = extension->GetIconResource(
+ Extension::EXTENSION_ICON_BITTY, ExtensionIconSet::MATCH_EXACTLY);
if (!icon_resource.relative_path().empty()) {
// Create a tracker to load the image. It will report back on OnImageLoaded.
- tracker_.LoadImage(extension, icon_resource, gfx::Size(size, size),
+ tracker_.LoadImage(extension, icon_resource,
+ gfx::Size(Extension::EXTENSION_ICON_BITTY,
+ Extension::EXTENSION_ICON_BITTY),
ImageLoadingTracker::DONT_CACHE);
} else {
OnImageLoaded(NULL, icon_resource, 0); // |image|, |index|.
diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi
index b076ff6..c16a141 100644
--- a/chrome/chrome_common.gypi
+++ b/chrome/chrome_common.gypi
@@ -188,6 +188,8 @@
'common/extensions/extension_extent.h',
'common/extensions/extension_file_util.cc',
'common/extensions/extension_file_util.h',
+ 'common/extensions/extension_icon_set.cc',
+ 'common/extensions/extension_icon_set.h',
'common/extensions/extension_l10n_util.cc',
'common/extensions/extension_l10n_util.h',
'common/extensions/extension_localization_peer.cc',
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index 32eb1d3..114dcec 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -1309,6 +1309,7 @@
'common/extensions/extension_action_unittest.cc',
'common/extensions/extension_extent_unittest.cc',
'common/extensions/extension_file_util_unittest.cc',
+ 'common/extensions/extension_icon_set_unittest.cc',
'common/extensions/extension_l10n_util_unittest.cc',
'common/extensions/extension_localization_peer_unittest.cc',
'common/extensions/extension_manifests_unittest.cc',
diff --git a/chrome/common/extensions/extension.cc b/chrome/common/extensions/extension.cc
index 3b2de4c..a9cfde1 100644
--- a/chrome/common/extensions/extension.cc
+++ b/chrome/common/extensions/extension.cc
@@ -960,7 +960,8 @@ bool Extension::HasEffectiveBrowsingHistoryPermission() const {
void Extension::DecodeIcon(Extension* extension,
Icons icon_size,
scoped_ptr<SkBitmap>* result) {
- FilePath icon_path = extension->GetIconResource(icon_size).GetFilePath();
+ FilePath icon_path = extension->GetIconResource(
+ icon_size, ExtensionIconSet::MATCH_EXACTLY).GetFilePath();
DecodeIconFromPath(icon_path, icon_size, result);
}
@@ -1146,7 +1147,7 @@ bool Extension::InitFromValue(const DictionaryValue& source, bool require_key,
errors::kInvalidIconPath, key);
return false;
}
- icons_[kIconSizes[i]] = icon_path;
+ icons_.Add(kIconSizes[i], icon_path);
}
}
}
@@ -1653,8 +1654,8 @@ std::set<FilePath> Extension::GetBrowserImages() {
// indicate that we're doing something wrong.
// Extension icons.
- for (std::map<int, std::string>::iterator iter = icons_.begin();
- iter != icons_.end(); ++iter) {
+ for (ExtensionIconSet::IconMap::const_iterator iter = icons_.map().begin();
+ iter != icons_.map().end(); ++iter) {
image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(iter->second)));
}
@@ -1767,59 +1768,22 @@ SkBitmap* Extension::GetCachedImageImpl(const ExtensionResource& source,
return NULL;
}
-std::string Extension::GetIconPath(Icons icon) {
- std::map<int, std::string>::const_iterator iter = icons_.find(icon);
- if (iter == icons_.end())
- return std::string();
- return iter->second;
-}
-
-Extension::Icons Extension::GetIconPathAllowLargerSize(
- std::string* path, Icons icon) {
- *path = GetIconPath(icon);
- if (!path->empty())
- return icon;
- if (icon == EXTENSION_ICON_BITTY)
- return GetIconPathAllowLargerSize(path, EXTENSION_ICON_SMALL);
- if (icon == EXTENSION_ICON_SMALL)
- return GetIconPathAllowLargerSize(path, EXTENSION_ICON_MEDIUM);
- if (icon == EXTENSION_ICON_MEDIUM)
- return GetIconPathAllowLargerSize(path, EXTENSION_ICON_LARGE);
- return EXTENSION_ICON_LARGE;
-}
-
-ExtensionResource Extension::GetIconResource(Icons icon) {
- std::string path = GetIconPath(icon);
+ExtensionResource Extension::GetIconResource(
+ int size, ExtensionIconSet::MatchType match_type) {
+ std::string path = icons_.Get(size, match_type);
if (path.empty())
return ExtensionResource();
return GetResource(path);
}
-Extension::Icons Extension::GetIconResourceAllowLargerSize(
- ExtensionResource* resource, Icons icon) {
- std::string path;
- Extension::Icons ret = GetIconPathAllowLargerSize(&path, icon);
- if (path.empty())
- *resource = ExtensionResource();
- else
- *resource = GetResource(path);
- return ret;
-}
-
-GURL Extension::GetIconURL(Icons icon) {
- std::string path = GetIconPath(icon);
+GURL Extension::GetIconURL(int size, ExtensionIconSet::MatchType match_type) {
+ std::string path = icons_.Get(size, match_type);
if (path.empty())
return GURL();
else
return GetResourceURL(path);
}
-GURL Extension::GetIconURLAllowLargerSize(Icons icon) {
- std::string path;
- GetIconPathAllowLargerSize(&path, icon);
- return GetResourceURL(path);
-}
-
bool Extension::CanAccessURL(const URLPattern pattern) const {
if (pattern.MatchesScheme(chrome::kChromeUIScheme)) {
// Only allow access to chrome://favicon to regular extensions. Component
diff --git a/chrome/common/extensions/extension.h b/chrome/common/extensions/extension.h
index e8c4d0f..47df5a8 100644
--- a/chrome/common/extensions/extension.h
+++ b/chrome/common/extensions/extension.h
@@ -16,6 +16,7 @@
#include "base/scoped_ptr.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_extent.h"
+#include "chrome/common/extensions/extension_icon_set.h"
#include "chrome/common/extensions/user_script.h"
#include "chrome/common/extensions/url_pattern.h"
#include "gfx/size.h"
@@ -306,7 +307,8 @@ class Extension {
bool HasAccessToAllHosts() const;
const GURL& update_url() const { return update_url_; }
- const std::map<int, std::string>& icons() const { return icons_; }
+
+ const ExtensionIconSet& icons() const { return icons_; }
// Returns the Google Gallery URL for this extension, if one exists. For
// third-party extensions, this returns a blank GURL.
@@ -325,19 +327,10 @@ class Extension {
// the browser might load (like themes and page action icons).
std::set<FilePath> GetBrowserImages();
- // Returns an absolute path to the given icon inside of the extension. Returns
- // an empty FilePath if the extension does not have that icon.
- ExtensionResource GetIconResource(Icons icon);
-
- // Looks for an extension icon of dimension |icon|. If not found, checks if
- // the next larger size exists (until one is found or the end is reached). If
- // an icon is found, the path is returned in |resource| and the dimension
- // found is returned to the caller (as function return value).
- // NOTE: |resource| is not guaranteed to be non-empty.
- Icons GetIconResourceAllowLargerSize(ExtensionResource* resource, Icons icon);
-
- GURL GetIconURL(Icons icon);
- GURL GetIconURLAllowLargerSize(Icons icon);
+ // Get an extension icon as a resource or URL.
+ ExtensionResource GetIconResource(int size,
+ ExtensionIconSet::MatchType match_type);
+ GURL GetIconURL(int size, ExtensionIconSet::MatchType match_type);
const DictionaryValue* manifest_value() const {
return manifest_value_.get();
@@ -447,11 +440,6 @@ class Extension {
// kPermissionNames).
bool IsAPIPermission(const std::string& permission);
- // Utility functions to get the icon relative path used to create an
- // ExtensionResource or URL.
- std::string GetIconPath(Icons icon);
- Icons GetIconPathAllowLargerSize(std::string* path, Icons icon);
-
// The absolute path to the directory the extension is stored in.
FilePath path_;
@@ -535,8 +523,8 @@ class Extension {
// The sites this extension has permission to talk to (using XHR, etc).
URLPatternList host_permissions_;
- // The paths to the icons the extension contains mapped by their width.
- std::map<int, std::string> icons_;
+ // The icons for the extension.
+ ExtensionIconSet icons_;
// URL for fetching an update manifest
GURL update_url_;
diff --git a/chrome/common/extensions/extension_file_util.cc b/chrome/common/extensions/extension_file_util.cc
index 4967916e..0c19a10 100644
--- a/chrome/common/extensions/extension_file_util.cc
+++ b/chrome/common/extensions/extension_file_util.cc
@@ -134,8 +134,10 @@ Extension* LoadExtension(const FilePath& extension_path,
bool ValidateExtension(Extension* extension, std::string* error) {
// Validate icons exist.
- for (std::map<int, std::string>::const_iterator iter =
- extension->icons().begin(); iter != extension->icons().end(); ++iter) {
+ for (ExtensionIconSet::IconMap::const_iterator iter =
+ extension->icons().map().begin();
+ iter != extension->icons().map().end();
+ ++iter) {
const FilePath path = extension->GetResource(iter->second).GetFilePath();
if (!file_util::PathExists(path)) {
*error =
diff --git a/chrome/common/extensions/extension_icon_set.cc b/chrome/common/extensions/extension_icon_set.cc
new file mode 100644
index 0000000..0679882
--- /dev/null
+++ b/chrome/common/extensions/extension_icon_set.cc
@@ -0,0 +1,45 @@
+// 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 "chrome/common/extensions/extension_icon_set.h"
+
+#include "base/logging.h"
+
+void ExtensionIconSet::Clear() {
+ map_.clear();
+}
+
+void ExtensionIconSet::Add(int size, const std::string& path) {
+ map_[size] = path;
+}
+
+std::string ExtensionIconSet::Get(int size, MatchType match_type) const {
+ // The searches for MATCH_BIGGER and MATCH_SMALLER below rely on the fact that
+ // std::map is sorted. This is per the spec, so it should be safe to rely on.
+ if (match_type == MATCH_EXACTLY) {
+ IconMap::const_iterator result = map_.find(size);
+ return result == map_.end() ? std::string() : result->second;
+ } else if (match_type == MATCH_SMALLER) {
+ IconMap::const_reverse_iterator result = map_.rend();
+ for (IconMap::const_reverse_iterator iter = map_.rbegin();
+ iter != map_.rend(); ++iter) {
+ if (iter->first <= size) {
+ result = iter;
+ break;
+ }
+ }
+ return result == map_.rend() ? std::string() : result->second;
+ } else {
+ CHECK(match_type == MATCH_BIGGER);
+ IconMap::const_iterator result = map_.end();
+ for (IconMap::const_iterator iter = map_.begin(); iter != map_.end();
+ ++iter) {
+ if (iter->first >= size) {
+ result = iter;
+ break;
+ }
+ }
+ return result == map_.end() ? std::string() : result->second;
+ }
+}
diff --git a/chrome/common/extensions/extension_icon_set.h b/chrome/common/extensions/extension_icon_set.h
new file mode 100644
index 0000000..474a0c1
--- /dev/null
+++ b/chrome/common/extensions/extension_icon_set.h
@@ -0,0 +1,39 @@
+// 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 CHROME_COMMON_EXTENSIONS_EXTENSION_ICON_SET_H_
+#define CHROME_COMMON_EXTENSIONS_EXTENSION_ICON_SET_H_
+#pragma once
+
+#include <map>
+#include <string>
+
+// Represents the set of icons for an extension.
+class ExtensionIconSet {
+ public:
+ // Access to the underlying map from icon size->path.
+ typedef std::map<int, std::string> IconMap;
+ const IconMap& map() const { return map_; }
+
+ // Remove all icons from the set.
+ void Clear();
+
+ // Add an icon to the set. If the specified size is already present, it is
+ // overwritten.
+ void Add(int size, const std::string& path);
+
+ // Get an icon from the set, optionally falling back to a smaller or bigger
+ // size. MatchType is exclusive (do not OR them together).
+ enum MatchType {
+ MATCH_EXACTLY,
+ MATCH_BIGGER,
+ MATCH_SMALLER
+ };
+ std::string Get(int size, MatchType match_type) const;
+
+ private:
+ IconMap map_;
+};
+
+#endif // CHROME_COMMON_EXTENSIONS_EXTENSION_ICON_SET_H_
diff --git a/chrome/common/extensions/extension_icon_set_unittest.cc b/chrome/common/extensions/extension_icon_set_unittest.cc
new file mode 100644
index 0000000..d91206a
--- /dev/null
+++ b/chrome/common/extensions/extension_icon_set_unittest.cc
@@ -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.
+
+#include "chrome/common/extensions/extension_icon_set.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(ExtensionIconSet, Basic) {
+ ExtensionIconSet icons;
+ EXPECT_EQ("", icons.Get(42, ExtensionIconSet::MATCH_EXACTLY));
+ EXPECT_EQ("", icons.Get(42, ExtensionIconSet::MATCH_BIGGER));
+ EXPECT_EQ("", icons.Get(42, ExtensionIconSet::MATCH_SMALLER));
+ EXPECT_TRUE(icons.map().empty());
+
+ icons.Add(42, "42.png");
+ EXPECT_EQ("42.png", icons.Get(42, ExtensionIconSet::MATCH_EXACTLY));
+ EXPECT_EQ("42.png", icons.Get(42, ExtensionIconSet::MATCH_BIGGER));
+ EXPECT_EQ("42.png", icons.Get(42, ExtensionIconSet::MATCH_SMALLER));
+ EXPECT_EQ("42.png", icons.Get(41, ExtensionIconSet::MATCH_BIGGER));
+ EXPECT_EQ("42.png", icons.Get(43, ExtensionIconSet::MATCH_SMALLER));
+ EXPECT_EQ("", icons.Get(41, ExtensionIconSet::MATCH_SMALLER));
+ EXPECT_EQ("", icons.Get(43, ExtensionIconSet::MATCH_BIGGER));
+
+ icons.Add(38, "38.png");
+ icons.Add(40, "40.png");
+ icons.Add(44, "44.png");
+ icons.Add(46, "46.png");
+
+ EXPECT_EQ("", icons.Get(41, ExtensionIconSet::MATCH_EXACTLY));
+ EXPECT_EQ("40.png", icons.Get(41, ExtensionIconSet::MATCH_SMALLER));
+ EXPECT_EQ("42.png", icons.Get(41, ExtensionIconSet::MATCH_BIGGER));
+ EXPECT_EQ("", icons.Get(37, ExtensionIconSet::MATCH_SMALLER));
+ EXPECT_EQ("", icons.Get(47, ExtensionIconSet::MATCH_BIGGER));
+}