Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5756)

Unified Diff: chrome/common/extensions/extension_action.cc

Issue 10905005: Change browser/page action default icon defined in manifest to support hidpi. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/common/extensions/extension_action.cc
diff --git a/chrome/common/extensions/extension_action.cc b/chrome/common/extensions/extension_action.cc
index bab5dfbd563094bd9399a0c64bbb4539d0c1440e..982a5a8fa5eded6562f52add7e9114fee4f3e714 100644
--- a/chrome/common/extensions/extension_action.cc
+++ b/chrome/common/extensions/extension_action.cc
@@ -10,6 +10,7 @@
#include "base/logging.h"
#include "base/message_loop.h"
#include "chrome/common/badge_util.h"
+#include "chrome/common/extensions/extension_constants.h"
#include "googleurl/src/gurl.h"
#include "grit/theme_resources.h"
#include "grit/ui_resources.h"
@@ -61,6 +62,36 @@ const int kMaxTextWidth = 23;
// The minimum width for center-aligning the badge.
const int kCenterAlignThreshold = 20;
+void CopyExtensionIconSet(const ExtensionIconSet* from, ExtensionIconSet* to) {
Jeffrey Yasskin 2012/09/13 00:23:36 If we want this function, I suspect ExtensionIconS
tbarzic 2012/09/13 02:01:01 I don't think we want this functionality in produc
+ for (ExtensionIconSet::IconMap::const_iterator iter = from->map().begin();
+ iter != from->map().end();
+ ++iter) {
+ to->Add(iter->first, iter->second);
+ }
+}
+
+int GetDesiredIconSizeForActionType(ExtensionAction::Type type) {
+ switch (type) {
+ case ExtensionAction::TYPE_BROWSER:
+ case ExtensionAction::TYPE_PAGE:
+ return extension_misc::EXTENSION_ICON_ACTION;
+ case ExtensionAction::TYPE_SCRIPT_BADGE:
+ return extension_misc::EXTENSION_ICON_BITTY;
+ default:
+ NOTREACHED();
+ return 0;
+ }
+}
+
+gfx::ImageSkia GetIconFromIconSet(const ExtensionIconSet* icon_set,
Jeffrey Yasskin 2012/09/13 00:23:36 This function doesn't seem to pull its weight. I'd
tbarzic 2012/09/13 02:01:01 Done.
+ ExtensionIconFactoryDelegate* icon_factory,
+ ExtensionAction::Type type) {
+ if (!icon_set || !icon_factory)
+ return gfx::ImageSkia();
+
+ return icon_factory->GetIcon(icon_set, GetDesiredIconSizeForActionType(type));
+}
+
class GetAttentionImageSource : public gfx::ImageSkiaSource {
public:
explicit GetAttentionImageSource(const gfx::ImageSkia& icon)
@@ -254,12 +285,31 @@ scoped_ptr<ExtensionAction> ExtensionAction::CopyForTest() const {
copy->badge_text_color_ = badge_text_color_;
copy->appearance_ = appearance_;
copy->icon_animation_ = icon_animation_;
- copy->default_icon_path_ = default_icon_path_;
copy->id_ = id_;
- copy->icon_paths_ = icon_paths_;
+
+ if (default_icon_.get()) {
+ scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet());
+ CopyExtensionIconSet(default_icon_.get(), default_icon.get());
+ copy->default_icon_ = default_icon.Pass();
+ }
+
+ for (size_t i = 0; i < page_action_icons_.size(); i++) {
+ scoped_ptr<ExtensionIconSet> page_action_icon(new ExtensionIconSet());
+ CopyExtensionIconSet(page_action_icons_[i], page_action_icon.get());
+ copy->AddPageActionIcon(page_action_icon.Pass());
+ }
+
return copy.Pass();
}
+void ExtensionAction::AddPageActionIcon(scoped_ptr<ExtensionIconSet> icon_set) {
+ page_action_icons_.push_back(icon_set.release());
+}
+
+bool ExtensionAction::IsValidIconIndex(int index) const {
+ return index >= 0 && (static_cast<size_t>(index) < page_action_icons_.size());
+}
+
void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) {
// We store |url| even if it is empty, rather than removing a URL from the
// map. If an extension has a default popup, and removes it for a tab via
@@ -277,36 +327,29 @@ GURL ExtensionAction::GetPopupUrl(int tab_id) const {
return GetValue(&popup_url_, tab_id);
}
-void ExtensionAction::CacheIcon(const std::string& path,
- const gfx::Image& icon) {
- if (!icon.IsEmpty())
- path_to_icon_cache_.insert(std::make_pair(path, *icon.ToImageSkia()));
-}
-
void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) {
SetValue(&icon_, tab_id, image.AsImageSkia());
}
-gfx::Image ExtensionAction::GetIcon(int tab_id) const {
+gfx::Image ExtensionAction::GetIcon(
+ int tab_id,
+ ExtensionIconFactoryDelegate* icon_factory) const {
// Check if a specific icon is set for this tab.
gfx::ImageSkia icon = GetExplicitlySetIcon(tab_id);
if (icon.isNull()) {
- // Need to find an icon from a path.
- const std::string* path = NULL;
// Check if one of the elements of icon_path() was selected.
int icon_index = GetIconIndex(tab_id);
+ const ExtensionIconSet* icon_set = NULL;
if (icon_index >= 0) {
- path = &icon_paths()->at(icon_index);
+ icon_set = page_action_icons_[icon_index];
} else {
- // Otherwise, use the default icon.
- path = &default_icon_path();
+ icon_set = default_icon_.get();
}
- std::map<std::string, gfx::ImageSkia>::const_iterator cached_icon =
- path_to_icon_cache_.find(*path);
- if (cached_icon != path_to_icon_cache_.end()) {
- icon = cached_icon->second;
- } else {
+ icon = GetIconFromIconSet(icon_set, icon_factory, action_type());
+
+ // Extension favicon is our last resort.
+ if (icon.isNull()) {
icon = *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
IDR_EXTENSIONS_FAVICON);
}
@@ -323,7 +366,7 @@ gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const {
}
void ExtensionAction::SetIconIndex(int tab_id, int index) {
- if (static_cast<size_t>(index) >= icon_paths_.size()) {
+ if (static_cast<size_t>(index) >= page_action_icons_.size()) {
NOTREACHED();
return;
}
@@ -336,6 +379,7 @@ bool ExtensionAction::SetAppearance(int tab_id, Appearance new_appearance) {
if (old_appearance == new_appearance)
return false;
+ // Remeber icon width so it can be used for badge painting.
Jeffrey Yasskin 2012/09/13 00:23:36 This doesn't look like the width being rembered.
tbarzic 2012/09/13 02:01:01 Done.
SetValue(&appearance_, tab_id, new_appearance);
// When showing a badge for the first time on a web page, fade it
@@ -368,7 +412,7 @@ void ExtensionAction::PaintBadge(gfx::Canvas* canvas,
GetBadgeText(tab_id),
GetBadgeTextColor(tab_id),
GetBadgeBackgroundColor(tab_id),
- GetValue(&icon_, tab_id).size().width());
+ GetIconWidth(tab_id));
}
gfx::ImageSkia ExtensionAction::GetIconWithBadge(
@@ -387,6 +431,24 @@ gfx::ImageSkia ExtensionAction::GetIconWithBadge(
icon.size());
}
+// Determines which icon would be returned by |GetIcon|, and returns its width.
+int ExtensionAction:: GetIconWidth(int tab_id) const {
Jeffrey Yasskin 2012/09/13 00:23:36 No space between :: and the method name.
tbarzic 2012/09/13 02:01:01 Done.
+ // If icon has been set, return its width.
+ gfx::ImageSkia icon = GetValue(&icon_, tab_id);
+ if (!icon.isNull())
+ return icon.width();
+ // If page action icon has been set, or there is a default icon, the icon
+ // the icon width will be set depending on our action type.
Jeffrey Yasskin 2012/09/13 00:23:36 typo: "the icon the icon width"
tbarzic 2012/09/13 02:01:01 Done.
+ int icon_index = GetIconIndex(tab_id);
+ if (icon_index >= 0 || default_icon_.get())
+ return GetDesiredIconSizeForActionType(action_type());
+
+ // If no icon has been set and there is no default icon, we need favicon
+ // width.
+ return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
+ IDR_EXTENSIONS_FAVICON).ToImageSkia()->width();
+}
+
// static
void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas,
const gfx::Rect& bounds,

Powered by Google App Engine
This is Rietveld 408576698