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

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: icon_factory: NULL check on the observer 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 int GetIconWidthForActionType(ExtensionAction::Type type) {
Jeffrey Yasskin 2012/09/14 23:47:12 You also have this function as GetIconSizeForType(
tbarzic 2012/09/15 00:29:10 Done.
66 switch (type) {
67 case ExtensionAction::TYPE_BROWSER:
68 case ExtensionAction::TYPE_PAGE:
69 return extension_misc::EXTENSION_ICON_ACTION;
70 case ExtensionAction::TYPE_SCRIPT_BADGE:
71 return extension_misc::EXTENSION_ICON_BITTY;
72 default:
73 NOTREACHED();
74 return 0;
75 }
76 }
77
64 class GetAttentionImageSource : public gfx::ImageSkiaSource { 78 class GetAttentionImageSource : public gfx::ImageSkiaSource {
65 public: 79 public:
66 explicit GetAttentionImageSource(const gfx::ImageSkia& icon) 80 explicit GetAttentionImageSource(const gfx::ImageSkia& icon)
67 : icon_(icon) {} 81 : icon_(icon) {}
68 82
69 // gfx::ImageSkiaSource overrides: 83 // gfx::ImageSkiaSource overrides:
70 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) 84 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor)
71 OVERRIDE { 85 OVERRIDE {
72 gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor); 86 gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor);
73 color_utils::HSL shift = {-1, 0, 0.5}; 87 color_utils::HSL shift = {-1, 0, 0.5};
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 scoped_ptr<ExtensionAction> copy( 260 scoped_ptr<ExtensionAction> copy(
247 new ExtensionAction(extension_id_, action_type_)); 261 new ExtensionAction(extension_id_, action_type_));
248 copy->popup_url_ = popup_url_; 262 copy->popup_url_ = popup_url_;
249 copy->title_ = title_; 263 copy->title_ = title_;
250 copy->icon_ = icon_; 264 copy->icon_ = icon_;
251 copy->badge_text_ = badge_text_; 265 copy->badge_text_ = badge_text_;
252 copy->badge_background_color_ = badge_background_color_; 266 copy->badge_background_color_ = badge_background_color_;
253 copy->badge_text_color_ = badge_text_color_; 267 copy->badge_text_color_ = badge_text_color_;
254 copy->appearance_ = appearance_; 268 copy->appearance_ = appearance_;
255 copy->icon_animation_ = icon_animation_; 269 copy->icon_animation_ = icon_animation_;
256 copy->default_icon_path_ = default_icon_path_;
257 copy->id_ = id_; 270 copy->id_ = id_;
271
272 if (default_icon_.get()) {
273 scoped_ptr<ExtensionIconSet> default_icon_copy(new ExtensionIconSet());
274 default_icon_->CopyForTest(default_icon_copy.get());
275 copy->default_icon_ = default_icon_copy.Pass();
276 }
277
258 return copy.Pass(); 278 return copy.Pass();
259 } 279 }
260 280
261 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) { 281 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) {
262 // We store |url| even if it is empty, rather than removing a URL from the 282 // We store |url| even if it is empty, rather than removing a URL from the
263 // map. If an extension has a default popup, and removes it for a tab via 283 // map. If an extension has a default popup, and removes it for a tab via
264 // the API, we must remember that there is no popup for that specific tab. 284 // the API, we must remember that there is no popup for that specific tab.
265 // If we removed the tab's URL, GetPopupURL would incorrectly return the 285 // If we removed the tab's URL, GetPopupURL would incorrectly return the
266 // default URL. 286 // default URL.
267 SetValue(&popup_url_, tab_id, url); 287 SetValue(&popup_url_, tab_id, url);
268 } 288 }
269 289
270 bool ExtensionAction::HasPopup(int tab_id) const { 290 bool ExtensionAction::HasPopup(int tab_id) const {
271 return !GetPopupUrl(tab_id).is_empty(); 291 return !GetPopupUrl(tab_id).is_empty();
272 } 292 }
273 293
274 GURL ExtensionAction::GetPopupUrl(int tab_id) const { 294 GURL ExtensionAction::GetPopupUrl(int tab_id) const {
275 return GetValue(&popup_url_, tab_id); 295 return GetValue(&popup_url_, tab_id);
276 } 296 }
277 297
278 void ExtensionAction::CacheIcon(const gfx::Image& icon) {
279 if (!icon.IsEmpty())
280 cached_icon_.reset(new gfx::ImageSkia(*icon.ToImageSkia()));
281 }
282
283 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) { 298 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) {
284 SetValue(&icon_, tab_id, image.AsImageSkia()); 299 SetValue(&icon_, tab_id, image.AsImageSkia());
285 } 300 }
286 301
287 gfx::Image ExtensionAction::GetIcon(int tab_id) const { 302 gfx::Image ExtensionAction::ApplyAttentionAndAnimation(
Jeffrey Yasskin 2012/09/14 23:47:12 Somewhere in this sequence of refactorings, the po
tbarzic 2012/09/15 00:29:10 Initially I wanted to make all of this to return I
288 // Check if a specific icon is set for this tab. 303 const gfx::ImageSkia& original_icon,
289 gfx::ImageSkia icon = GetExplicitlySetIcon(tab_id); 304 int tab_id) const {
290 if (icon.isNull()) { 305 gfx::ImageSkia icon = original_icon;
291 if (cached_icon_.get()) {
292 icon = *cached_icon_;
293 } else {
294 icon = *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
295 IDR_EXTENSIONS_FAVICON);
296 }
297 }
298
299 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION) 306 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION)
300 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size()); 307 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size());
301 308
302 return gfx::Image(ApplyIconAnimation(tab_id, icon)); 309 return gfx::Image(ApplyIconAnimation(tab_id, icon));
303 } 310 }
304 311
305 gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const { 312 gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const {
306 return GetValue(&icon_, tab_id); 313 return GetValue(&icon_, tab_id);
307 } 314 }
308 315
(...skipping 27 matching lines...) Expand all
336 343
337 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, 344 void ExtensionAction::PaintBadge(gfx::Canvas* canvas,
338 const gfx::Rect& bounds, 345 const gfx::Rect& bounds,
339 int tab_id) { 346 int tab_id) {
340 ExtensionAction::DoPaintBadge( 347 ExtensionAction::DoPaintBadge(
341 canvas, 348 canvas,
342 bounds, 349 bounds,
343 GetBadgeText(tab_id), 350 GetBadgeText(tab_id),
344 GetBadgeTextColor(tab_id), 351 GetBadgeTextColor(tab_id),
345 GetBadgeBackgroundColor(tab_id), 352 GetBadgeBackgroundColor(tab_id),
346 GetValue(&icon_, tab_id).size().width()); 353 GetIconWidth(tab_id));
347 } 354 }
348 355
349 gfx::ImageSkia ExtensionAction::GetIconWithBadge( 356 gfx::ImageSkia ExtensionAction::GetIconWithBadge(
350 const gfx::ImageSkia& icon, 357 const gfx::ImageSkia& icon,
351 int tab_id, 358 int tab_id,
352 const gfx::Size& spacing) const { 359 const gfx::Size& spacing) const {
353 if (tab_id < 0) 360 if (tab_id < 0)
354 return icon; 361 return icon;
355 362
356 return gfx::ImageSkia( 363 return gfx::ImageSkia(
357 new IconWithBadgeImageSource(icon, 364 new IconWithBadgeImageSource(icon,
358 spacing, 365 spacing,
359 GetBadgeText(tab_id), 366 GetBadgeText(tab_id),
360 GetBadgeTextColor(tab_id), 367 GetBadgeTextColor(tab_id),
361 GetBadgeBackgroundColor(tab_id)), 368 GetBadgeBackgroundColor(tab_id)),
362 icon.size()); 369 icon.size());
363 } 370 }
364 371
372 // Determines which icon would be returned by |GetIcon|, and returns its width.
373 int ExtensionAction::GetIconWidth(int tab_id) const {
374 // If icon has been set, return its width.
375 gfx::ImageSkia icon = GetValue(&icon_, tab_id);
376 if (!icon.isNull())
377 return icon.width();
378 // If there is a default icon, the icon width will be set depending on our
379 // action type.
380 if (default_icon_.get())
381 return GetIconWidthForActionType(action_type());
382
383 // If no icon has been set and there is no default icon, we need favicon
384 // width.
385 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
386 IDR_EXTENSIONS_FAVICON).ToImageSkia()->width();
387 }
388
365 // static 389 // static
366 void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas, 390 void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas,
367 const gfx::Rect& bounds, 391 const gfx::Rect& bounds,
368 const std::string& text, 392 const std::string& text,
369 const SkColor& text_color_in, 393 const SkColor& text_color_in,
370 const SkColor& background_color_in, 394 const SkColor& background_color_in,
371 int icon_width) { 395 int icon_width) {
372 if (text.empty()) 396 if (text.empty())
373 return; 397 return;
374 398
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 icon_animation->Start(); 514 icon_animation->Start();
491 // After the icon is finished fading in (plus some padding to handle random 515 // After the icon is finished fading in (plus some padding to handle random
492 // timer delays), destroy it. We use a delayed task so that the Animation is 516 // timer delays), destroy it. We use a delayed task so that the Animation is
493 // deleted even if it hasn't finished by the time the MessageLoop is 517 // deleted even if it hasn't finished by the time the MessageLoop is
494 // destroyed. 518 // destroyed.
495 MessageLoop::current()->PostDelayedTask( 519 MessageLoop::current()->PostDelayedTask(
496 FROM_HERE, 520 FROM_HERE,
497 base::Bind(&DestroyIconAnimation, base::Passed(icon_animation.Pass())), 521 base::Bind(&DestroyIconAnimation, base::Passed(icon_animation.Pass())),
498 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2)); 522 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2));
499 } 523 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698