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

Side by Side 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: review feedback 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/extensions/extension_action.h" 5 #include "chrome/common/extensions/extension_action.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "chrome/common/badge_util.h" 12 #include "chrome/common/badge_util.h"
13 #include "chrome/common/extensions/extension_constants.h"
13 #include "googleurl/src/gurl.h" 14 #include "googleurl/src/gurl.h"
14 #include "grit/theme_resources.h" 15 #include "grit/theme_resources.h"
15 #include "grit/ui_resources.h" 16 #include "grit/ui_resources.h"
16 #include "third_party/skia/include/core/SkBitmap.h" 17 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "third_party/skia/include/core/SkCanvas.h" 18 #include "third_party/skia/include/core/SkCanvas.h"
18 #include "third_party/skia/include/core/SkDevice.h" 19 #include "third_party/skia/include/core/SkDevice.h"
19 #include "third_party/skia/include/core/SkPaint.h" 20 #include "third_party/skia/include/core/SkPaint.h"
20 #include "third_party/skia/include/effects/SkGradientShader.h" 21 #include "third_party/skia/include/effects/SkGradientShader.h"
21 #include "ui/base/animation/animation_delegate.h" 22 #include "ui/base/animation/animation_delegate.h"
22 #include "ui/base/resource/resource_bundle.h" 23 #include "ui/base/resource/resource_bundle.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 const int kPadding = 2; 55 const int kPadding = 2;
55 // The padding between the top of the badge and the top of the text. 56 // The padding between the top of the badge and the top of the text.
56 const int kTopTextPadding = -1; 57 const int kTopTextPadding = -1;
57 #endif 58 #endif
58 59
59 const int kBadgeHeight = 11; 60 const int kBadgeHeight = 11;
60 const int kMaxTextWidth = 23; 61 const int kMaxTextWidth = 23;
61 // The minimum width for center-aligning the badge. 62 // The minimum width for center-aligning the badge.
62 const int kCenterAlignThreshold = 20; 63 const int kCenterAlignThreshold = 20;
63 64
65 void CopyExtensionIconSetForTest(const ExtensionIconSet* from,
66 ExtensionIconSet* to) {
67 for (ExtensionIconSet::IconMap::const_iterator iter = from->map().begin();
68 iter != from->map().end();
69 ++iter) {
70 to->Add(iter->first, iter->second);
71 }
72 }
73
74 gfx::Size GetIconSizeForActionType(ExtensionAction::Type type) {
75 switch (type) {
76 case ExtensionAction::TYPE_BROWSER:
77 case ExtensionAction::TYPE_PAGE:
78 return gfx::Size(extension_misc::EXTENSION_ICON_ACTION,
79 extension_misc::EXTENSION_ICON_ACTION);
80 case ExtensionAction::TYPE_SCRIPT_BADGE:
81 return gfx::Size(extension_misc::EXTENSION_ICON_BITTY,
82 extension_misc::EXTENSION_ICON_BITTY);
83 default:
84 NOTREACHED();
85 return gfx::Size();
86 }
87 }
88
64 class GetAttentionImageSource : public gfx::ImageSkiaSource { 89 class GetAttentionImageSource : public gfx::ImageSkiaSource {
65 public: 90 public:
66 explicit GetAttentionImageSource(const gfx::ImageSkia& icon) 91 explicit GetAttentionImageSource(const gfx::ImageSkia& icon)
67 : icon_(icon) {} 92 : icon_(icon) {}
68 93
69 // gfx::ImageSkiaSource overrides: 94 // gfx::ImageSkiaSource overrides:
70 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) 95 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor)
71 OVERRIDE { 96 OVERRIDE {
72 gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor); 97 gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor);
73 color_utils::HSL shift = {-1, 0, 0.5}; 98 color_utils::HSL shift = {-1, 0, 0.5};
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 new ExtensionAction(extension_id_, action_type_)); 272 new ExtensionAction(extension_id_, action_type_));
248 copy->popup_url_ = popup_url_; 273 copy->popup_url_ = popup_url_;
249 copy->title_ = title_; 274 copy->title_ = title_;
250 copy->icon_ = icon_; 275 copy->icon_ = icon_;
251 copy->icon_index_ = icon_index_; 276 copy->icon_index_ = icon_index_;
252 copy->badge_text_ = badge_text_; 277 copy->badge_text_ = badge_text_;
253 copy->badge_background_color_ = badge_background_color_; 278 copy->badge_background_color_ = badge_background_color_;
254 copy->badge_text_color_ = badge_text_color_; 279 copy->badge_text_color_ = badge_text_color_;
255 copy->appearance_ = appearance_; 280 copy->appearance_ = appearance_;
256 copy->icon_animation_ = icon_animation_; 281 copy->icon_animation_ = icon_animation_;
257 copy->default_icon_path_ = default_icon_path_;
258 copy->id_ = id_; 282 copy->id_ = id_;
259 copy->icon_paths_ = icon_paths_; 283
284 if (default_icon_.get()) {
285 scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet());
286 CopyExtensionIconSetForTest(default_icon_.get(), default_icon.get());
287 copy->default_icon_ = default_icon.Pass();
288 }
289
290 for (size_t i = 0; i < page_action_icons_.size(); i++) {
291 scoped_ptr<ExtensionIconSet> page_action_icon(new ExtensionIconSet());
292 CopyExtensionIconSetForTest(page_action_icons_[i], page_action_icon.get());
293 copy->AddPageActionIcon(page_action_icon.Pass());
294 }
295
260 return copy.Pass(); 296 return copy.Pass();
261 } 297 }
262 298
299 void ExtensionAction::AddPageActionIcon(scoped_ptr<ExtensionIconSet> icon_set) {
300 page_action_icons_.push_back(icon_set.release());
301 }
302
303 bool ExtensionAction::IsValidIconIndex(int index) const {
304 return index >= 0 && (static_cast<size_t>(index) < page_action_icons_.size());
305 }
306
263 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) { 307 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) {
264 // We store |url| even if it is empty, rather than removing a URL from the 308 // We store |url| even if it is empty, rather than removing a URL from the
265 // map. If an extension has a default popup, and removes it for a tab via 309 // map. If an extension has a default popup, and removes it for a tab via
266 // the API, we must remember that there is no popup for that specific tab. 310 // the API, we must remember that there is no popup for that specific tab.
267 // If we removed the tab's URL, GetPopupURL would incorrectly return the 311 // If we removed the tab's URL, GetPopupURL would incorrectly return the
268 // default URL. 312 // default URL.
269 SetValue(&popup_url_, tab_id, url); 313 SetValue(&popup_url_, tab_id, url);
270 } 314 }
271 315
272 bool ExtensionAction::HasPopup(int tab_id) const { 316 bool ExtensionAction::HasPopup(int tab_id) const {
273 return !GetPopupUrl(tab_id).is_empty(); 317 return !GetPopupUrl(tab_id).is_empty();
274 } 318 }
275 319
276 GURL ExtensionAction::GetPopupUrl(int tab_id) const { 320 GURL ExtensionAction::GetPopupUrl(int tab_id) const {
277 return GetValue(&popup_url_, tab_id); 321 return GetValue(&popup_url_, tab_id);
278 } 322 }
279 323
280 void ExtensionAction::CacheIcon(const std::string& path,
281 const gfx::Image& icon) {
282 if (!icon.IsEmpty())
283 path_to_icon_cache_.insert(std::make_pair(path, *icon.ToImageSkia()));
284 }
285
286 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) { 324 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) {
287 SetValue(&icon_, tab_id, image.AsImageSkia()); 325 SetValue(&icon_, tab_id, image.AsImageSkia());
288 } 326 }
289 327
290 gfx::Image ExtensionAction::GetIcon(int tab_id) const { 328 gfx::Image ExtensionAction::GetIcon(
329 int tab_id,
330 ExtensionIconFactoryDelegate* icon_factory) const {
291 // Check if a specific icon is set for this tab. 331 // Check if a specific icon is set for this tab.
292 gfx::ImageSkia icon = GetExplicitlySetIcon(tab_id); 332 gfx::ImageSkia icon = GetExplicitlySetIcon(tab_id);
293 if (icon.isNull()) { 333 if (icon.isNull()) {
294 // Need to find an icon from a path.
295 const std::string* path = NULL;
296 // Check if one of the elements of icon_path() was selected. 334 // Check if one of the elements of icon_path() was selected.
297 int icon_index = GetIconIndex(tab_id); 335 int icon_index = GetIconIndex(tab_id);
336 const ExtensionIconSet* icon_set = NULL;
298 if (icon_index >= 0) { 337 if (icon_index >= 0) {
299 path = &icon_paths()->at(icon_index); 338 icon_set = page_action_icons_[icon_index];
300 } else { 339 } else {
301 // Otherwise, use the default icon. 340 icon_set = default_icon_.get();
302 path = &default_icon_path();
303 } 341 }
304 342
305 std::map<std::string, gfx::ImageSkia>::const_iterator cached_icon = 343 if (icon_set && icon_factory) {
306 path_to_icon_cache_.find(*path); 344 icon = icon_factory->GetIcon(icon_set,
307 if (cached_icon != path_to_icon_cache_.end()) { 345 GetIconSizeForActionType(action_type()));
308 icon = cached_icon->second; 346 }
309 } else { 347
348 // Extension favicon is our last resort.
349 if (icon.isNull()) {
310 icon = *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 350 icon = *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
311 IDR_EXTENSIONS_FAVICON); 351 IDR_EXTENSIONS_FAVICON);
312 } 352 }
313 } 353 }
314 354
315 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION) 355 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION)
316 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size()); 356 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size());
317 357
318 return gfx::Image(ApplyIconAnimation(tab_id, icon)); 358 return gfx::Image(ApplyIconAnimation(tab_id, icon));
319 } 359 }
320 360
321 gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const { 361 gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const {
322 return GetValue(&icon_, tab_id); 362 return GetValue(&icon_, tab_id);
323 } 363 }
324 364
325 void ExtensionAction::SetIconIndex(int tab_id, int index) { 365 void ExtensionAction::SetIconIndex(int tab_id, int index) {
326 if (static_cast<size_t>(index) >= icon_paths_.size()) { 366 if (static_cast<size_t>(index) >= page_action_icons_.size()) {
327 NOTREACHED(); 367 NOTREACHED();
328 return; 368 return;
329 } 369 }
330 SetValue(&icon_index_, tab_id, index); 370 SetValue(&icon_index_, tab_id, index);
331 } 371 }
332 372
333 bool ExtensionAction::SetAppearance(int tab_id, Appearance new_appearance) { 373 bool ExtensionAction::SetAppearance(int tab_id, Appearance new_appearance) {
334 const Appearance old_appearance = GetValue(&appearance_, tab_id); 374 const Appearance old_appearance = GetValue(&appearance_, tab_id);
335 375
336 if (old_appearance == new_appearance) 376 if (old_appearance == new_appearance)
(...skipping 24 matching lines...) Expand all
361 401
362 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, 402 void ExtensionAction::PaintBadge(gfx::Canvas* canvas,
363 const gfx::Rect& bounds, 403 const gfx::Rect& bounds,
364 int tab_id) { 404 int tab_id) {
365 ExtensionAction::DoPaintBadge( 405 ExtensionAction::DoPaintBadge(
366 canvas, 406 canvas,
367 bounds, 407 bounds,
368 GetBadgeText(tab_id), 408 GetBadgeText(tab_id),
369 GetBadgeTextColor(tab_id), 409 GetBadgeTextColor(tab_id),
370 GetBadgeBackgroundColor(tab_id), 410 GetBadgeBackgroundColor(tab_id),
371 GetValue(&icon_, tab_id).size().width()); 411 GetIconWidth(tab_id));
372 } 412 }
373 413
374 gfx::ImageSkia ExtensionAction::GetIconWithBadge( 414 gfx::ImageSkia ExtensionAction::GetIconWithBadge(
375 const gfx::ImageSkia& icon, 415 const gfx::ImageSkia& icon,
376 int tab_id, 416 int tab_id,
377 const gfx::Size& spacing) const { 417 const gfx::Size& spacing) const {
378 if (tab_id < 0) 418 if (tab_id < 0)
379 return icon; 419 return icon;
380 420
381 return gfx::ImageSkia( 421 return gfx::ImageSkia(
382 new IconWithBadgeImageSource(icon, 422 new IconWithBadgeImageSource(icon,
383 spacing, 423 spacing,
384 GetBadgeText(tab_id), 424 GetBadgeText(tab_id),
385 GetBadgeTextColor(tab_id), 425 GetBadgeTextColor(tab_id),
386 GetBadgeBackgroundColor(tab_id)), 426 GetBadgeBackgroundColor(tab_id)),
387 icon.size()); 427 icon.size());
388 } 428 }
389 429
430 // Determines which icon would be returned by |GetIcon|, and returns its width.
431 int ExtensionAction::GetIconWidth(int tab_id) const {
432 // If icon has been set, return its width.
433 gfx::ImageSkia icon = GetValue(&icon_, tab_id);
434 if (!icon.isNull())
435 return icon.width();
436 // If page action icon has been set, or there is a default icon, the icon
437 // width will be set depending on our action type.
438 int icon_index = GetIconIndex(tab_id);
439 if (icon_index >= 0 || default_icon_.get())
440 return GetIconSizeForActionType(action_type()).width();
441
442 // If no icon has been set and there is no default icon, we need favicon
443 // width.
444 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
445 IDR_EXTENSIONS_FAVICON).ToImageSkia()->width();
446 }
447
390 // static 448 // static
391 void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas, 449 void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas,
392 const gfx::Rect& bounds, 450 const gfx::Rect& bounds,
393 const std::string& text, 451 const std::string& text,
394 const SkColor& text_color_in, 452 const SkColor& text_color_in,
395 const SkColor& background_color_in, 453 const SkColor& background_color_in,
396 int icon_width) { 454 int icon_width) {
397 if (text.empty()) 455 if (text.empty())
398 return; 456 return;
399 457
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 icon_animation->Start(); 573 icon_animation->Start();
516 // After the icon is finished fading in (plus some padding to handle random 574 // After the icon is finished fading in (plus some padding to handle random
517 // timer delays), destroy it. We use a delayed task so that the Animation is 575 // timer delays), destroy it. We use a delayed task so that the Animation is
518 // deleted even if it hasn't finished by the time the MessageLoop is 576 // deleted even if it hasn't finished by the time the MessageLoop is
519 // destroyed. 577 // destroyed.
520 MessageLoop::current()->PostDelayedTask( 578 MessageLoop::current()->PostDelayedTask(
521 FROM_HERE, 579 FROM_HERE,
522 base::Bind(&DestroyIconAnimation, base::Passed(icon_animation.Pass())), 580 base::Bind(&DestroyIconAnimation, base::Passed(icon_animation.Pass())),
523 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2)); 581 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2));
524 } 582 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698