1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/extension_action.h"
9 #include "base/base64.h"
10 #include "base/logging.h"
11 #include "chrome/common/badge_util.h"
12 #include "chrome/common/icon_with_badge_image_source.h"
13 #include "extensions/browser/extension_icon_image.h"
14 #include "extensions/common/constants.h"
15 #include "extensions/common/extension_icon_set.h"
16 #include "extensions/common/manifest_handlers/icons_handler.h"
17 #include "grit/theme_resources.h"
18 #include "grit/ui_resources.h"
19 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_message_utils.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
22 #include "third_party/skia/include/core/SkCanvas.h"
23 #include "third_party/skia/include/core/SkPaint.h"
24 #include "third_party/skia/include/effects/SkGradientShader.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/gfx/animation/animation_delegate.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/gfx/color_utils.h"
29 #include "ui/gfx/geometry/rect.h"
30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/image/image.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/image/image_skia_source.h"
34 #include "ui/gfx/ipc/gfx_param_traits.h"
35 #include "ui/gfx/skbitmap_operations.h"
40 // Returns the default icon image for extensions.
41 gfx::Image
GetDefaultIcon() {
42 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
43 IDR_EXTENSIONS_FAVICON
);
46 // Given the extension action type, returns the size the extension action icon
47 // should have. The icon should be square, so only one dimension is
49 int GetIconSizeForType(extensions::ActionInfo::Type type
) {
51 case extensions::ActionInfo::TYPE_BROWSER
:
52 case extensions::ActionInfo::TYPE_PAGE
:
53 case extensions::ActionInfo::TYPE_SYSTEM_INDICATOR
:
54 // TODO(dewittj) Report the actual icon size of the system
56 return extension_misc::EXTENSION_ICON_ACTION
;
63 class GetAttentionImageSource
: public gfx::ImageSkiaSource
{
65 explicit GetAttentionImageSource(const gfx::ImageSkia
& icon
)
68 // gfx::ImageSkiaSource overrides:
69 gfx::ImageSkiaRep
GetImageForScale(float scale
) override
{
70 gfx::ImageSkiaRep icon_rep
= icon_
.GetRepresentation(scale
);
71 color_utils::HSL shift
= {-1, 0, 0.5};
72 return gfx::ImageSkiaRep(
73 SkBitmapOperations::CreateHSLShiftedBitmap(icon_rep
.sk_bitmap(), shift
),
78 const gfx::ImageSkia icon_
;
81 struct IconRepresentationInfo
{
82 // Size as a string that will be used to retrieve a representation value from
83 // SetIcon function arguments.
84 const char* size_string
;
85 // Scale factor for which the represantion should be used.
86 ui::ScaleFactor scale
;
89 const IconRepresentationInfo kIconSizes
[] = {{"19", ui::SCALE_FACTOR_100P
},
90 {"38", ui::SCALE_FACTOR_200P
}};
93 bool HasValue(const std::map
<int, T
>& map
, int tab_id
) {
94 return map
.find(tab_id
) != map
.end();
99 const int ExtensionAction::kDefaultTabId
= -1;
100 const int ExtensionAction::kPageActionIconMaxSize
=
101 extension_misc::EXTENSION_ICON_ACTION
;
103 ExtensionAction::ExtensionAction(const extensions::Extension
& extension
,
104 extensions::ActionInfo::Type action_type
,
105 const extensions::ActionInfo
& manifest_data
)
106 : extension_id_(extension
.id()), action_type_(action_type
) {
107 // Page/script actions are hidden/disabled by default, and browser actions are
108 // visible/enabled by default.
109 SetIsVisible(kDefaultTabId
,
110 action_type
== extensions::ActionInfo::TYPE_BROWSER
);
111 Populate(extension
, manifest_data
);
114 ExtensionAction::~ExtensionAction() {
117 void ExtensionAction::SetPopupUrl(int tab_id
, const GURL
& url
) {
118 // We store |url| even if it is empty, rather than removing a URL from the
119 // map. If an extension has a default popup, and removes it for a tab via
120 // the API, we must remember that there is no popup for that specific tab.
121 // If we removed the tab's URL, GetPopupURL would incorrectly return the
123 SetValue(&popup_url_
, tab_id
, url
);
126 bool ExtensionAction::HasPopup(int tab_id
) const {
127 return !GetPopupUrl(tab_id
).is_empty();
130 GURL
ExtensionAction::GetPopupUrl(int tab_id
) const {
131 return GetValue(&popup_url_
, tab_id
);
134 void ExtensionAction::SetIcon(int tab_id
, const gfx::Image
& image
) {
135 SetValue(&icon_
, tab_id
, image
);
138 bool ExtensionAction::ParseIconFromCanvasDictionary(
139 const base::DictionaryValue
& dict
,
140 gfx::ImageSkia
* icon
) {
141 // Try to extract an icon for each known scale.
142 for (size_t i
= 0; i
< arraysize(kIconSizes
); i
++) {
143 const base::BinaryValue
* image_data
;
144 std::string binary_string64
;
146 if (dict
.GetBinary(kIconSizes
[i
].size_string
, &image_data
)) {
147 pickle
= IPC::Message(image_data
->GetBuffer(), image_data
->GetSize());
148 } else if (dict
.GetString(kIconSizes
[i
].size_string
, &binary_string64
)) {
149 std::string binary_string
;
150 if (!base::Base64Decode(binary_string64
, &binary_string
))
152 pickle
= IPC::Message(binary_string
.c_str(), binary_string
.length());
156 PickleIterator
iter(pickle
);
158 if (!IPC::ReadParam(&pickle
, &iter
, &bitmap
))
160 CHECK(!bitmap
.isNull());
161 float scale
= ui::GetScaleForScaleFactor(kIconSizes
[i
].scale
);
162 icon
->AddRepresentation(gfx::ImageSkiaRep(bitmap
, scale
));
167 gfx::Image
ExtensionAction::GetExplicitlySetIcon(int tab_id
) const {
168 return GetValue(&icon_
, tab_id
);
171 bool ExtensionAction::SetIsVisible(int tab_id
, bool new_visibility
) {
172 const bool old_visibility
= GetValue(&is_visible_
, tab_id
);
174 if (old_visibility
== new_visibility
)
177 SetValue(&is_visible_
, tab_id
, new_visibility
);
182 void ExtensionAction::DeclarativeShow(int tab_id
) {
183 DCHECK_NE(tab_id
, kDefaultTabId
);
184 ++declarative_show_count_
[tab_id
]; // Use default initialization to 0.
187 void ExtensionAction::UndoDeclarativeShow(int tab_id
) {
188 int& show_count
= declarative_show_count_
[tab_id
];
189 DCHECK_GT(show_count
, 0);
190 if (--show_count
== 0)
191 declarative_show_count_
.erase(tab_id
);
194 void ExtensionAction::DeclarativeSetIcon(int tab_id
,
196 const gfx::Image
& icon
) {
197 DCHECK_NE(tab_id
, kDefaultTabId
);
198 declarative_icon_
[tab_id
][priority
].push_back(icon
);
201 void ExtensionAction::UndoDeclarativeSetIcon(int tab_id
,
203 const gfx::Image
& icon
) {
204 std::vector
<gfx::Image
>& icons
= declarative_icon_
[tab_id
][priority
];
205 for (std::vector
<gfx::Image
>::iterator it
= icons
.begin(); it
!= icons
.end();
207 if (it
->AsImageSkia().BackedBySameObjectAs(icon
.AsImageSkia())) {
214 const gfx::Image
ExtensionAction::GetDeclarativeIcon(int tab_id
) const {
215 if (declarative_icon_
.find(tab_id
) != declarative_icon_
.end() &&
216 !declarative_icon_
.find(tab_id
)->second
.rbegin()->second
.empty()) {
217 return declarative_icon_
.find(tab_id
)->second
.rbegin()->second
.back();
222 void ExtensionAction::ClearAllValuesForTab(int tab_id
) {
223 popup_url_
.erase(tab_id
);
224 title_
.erase(tab_id
);
226 badge_text_
.erase(tab_id
);
227 badge_text_color_
.erase(tab_id
);
228 badge_background_color_
.erase(tab_id
);
229 is_visible_
.erase(tab_id
);
230 // TODO(jyasskin): Erase the element from declarative_show_count_
231 // when the tab's closed. There's a race between the
232 // LocationBarController and the ContentRulesRegistry on navigation,
233 // which prevents me from cleaning everything up now.
236 void ExtensionAction::PaintBadge(gfx::Canvas
* canvas
,
237 const gfx::Rect
& bounds
,
239 badge_util::PaintBadge(
242 GetBadgeText(tab_id
),
243 GetBadgeTextColor(tab_id
),
244 GetBadgeBackgroundColor(tab_id
),
245 GetIconWidth(tab_id
),
249 gfx::ImageSkia
ExtensionAction::GetIconWithBadge(
250 const gfx::ImageSkia
& icon
,
252 const gfx::Size
& spacing
) const {
256 return gfx::ImageSkia(
257 new IconWithBadgeImageSource(icon
,
260 GetBadgeText(tab_id
),
261 GetBadgeTextColor(tab_id
),
262 GetBadgeBackgroundColor(tab_id
),
267 extensions::IconImage
* ExtensionAction::LoadDefaultIconImage(
268 const extensions::Extension
& extension
,
269 content::BrowserContext
* browser_context
) {
270 if (default_icon_
&& !default_icon_image_
) {
271 default_icon_image_
.reset(new extensions::IconImage(
275 GetIconSizeForType(action_type_
),
276 *GetDefaultIcon().ToImageSkia(),
279 return default_icon_image_
.get();
282 gfx::Image
ExtensionAction::GetDefaultIconImage() const {
283 // If we have a default icon, it should be loaded before trying to use it.
284 DCHECK(!default_icon_image_
== !default_icon_
);
285 return default_icon_image_
? default_icon_image_
->image() : GetDefaultIcon();
288 bool ExtensionAction::HasPopupUrl(int tab_id
) const {
289 return HasValue(popup_url_
, tab_id
);
292 bool ExtensionAction::HasTitle(int tab_id
) const {
293 return HasValue(title_
, tab_id
);
296 bool ExtensionAction::HasBadgeText(int tab_id
) const {
297 return HasValue(badge_text_
, tab_id
);
300 bool ExtensionAction::HasBadgeBackgroundColor(int tab_id
) const {
301 return HasValue(badge_background_color_
, tab_id
);
304 bool ExtensionAction::HasBadgeTextColor(int tab_id
) const {
305 return HasValue(badge_text_color_
, tab_id
);
308 bool ExtensionAction::HasIsVisible(int tab_id
) const {
309 return HasValue(is_visible_
, tab_id
);
312 bool ExtensionAction::HasIcon(int tab_id
) const {
313 return HasValue(icon_
, tab_id
);
316 void ExtensionAction::SetDefaultIconForTest(
317 scoped_ptr
<ExtensionIconSet
> default_icon
) {
318 default_icon_
= default_icon
.Pass();
321 void ExtensionAction::Populate(const extensions::Extension
& extension
,
322 const extensions::ActionInfo
& manifest_data
) {
323 // If the manifest doesn't specify a title, set it to |extension|'s name.
324 const std::string
& title
=
325 !manifest_data
.default_title
.empty() ? manifest_data
.default_title
:
327 SetTitle(kDefaultTabId
, title
);
328 SetPopupUrl(kDefaultTabId
, manifest_data
.default_popup_url
);
329 set_id(manifest_data
.id
);
331 // Initialize the specified icon set.
332 if (!manifest_data
.default_icon
.empty())
333 default_icon_
.reset(new ExtensionIconSet(manifest_data
.default_icon
));
335 const ExtensionIconSet
& extension_icons
=
336 extensions::IconsInfo::GetIcons(&extension
);
337 // Look for any other icons.
338 std::string largest_icon
= extension_icons
.Get(
339 extension_misc::EXTENSION_ICON_GIGANTOR
, ExtensionIconSet::MATCH_SMALLER
);
341 if (!largest_icon
.empty()) {
342 // We found an icon to use, so create an icon set if one doesn't exist.
344 default_icon_
.reset(new ExtensionIconSet());
345 int largest_icon_size
= extension_icons
.GetIconSizeFromPath(largest_icon
);
346 // Replace any missing extension action icons with the largest icon
347 // retrieved from |extension|'s manifest so long as the largest icon is
348 // larger than the current key.
349 for (int i
= extension_misc::kNumExtensionActionIconSizes
- 1; i
>= 0;
351 int size
= extension_misc::kExtensionActionIconSizes
[i
].size
;
352 if (default_icon_
->Get(size
, ExtensionIconSet::MATCH_BIGGER
).empty() &&
353 largest_icon_size
> size
) {
354 default_icon_
->Add(size
, largest_icon
);
361 // Determines which icon would be returned by |GetIcon|, and returns its width.
362 int ExtensionAction::GetIconWidth(int tab_id
) const {
363 // If icon has been set, return its width.
364 gfx::Image icon
= GetValue(&icon_
, tab_id
);
367 // If there is a default icon, the icon width will be set depending on our
370 return GetIconSizeForType(action_type());
372 // If no icon has been set and there is no default icon, we need favicon
374 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
375 IDR_EXTENSIONS_FAVICON
).Width();