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

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: forgot to update tests 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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 scoped_ptr<ExtensionAction> copy( 247 scoped_ptr<ExtensionAction> copy(
247 new ExtensionAction(extension_id_, action_type_)); 248 new ExtensionAction(extension_id_, action_type_));
248 copy->popup_url_ = popup_url_; 249 copy->popup_url_ = popup_url_;
249 copy->title_ = title_; 250 copy->title_ = title_;
250 copy->icon_ = icon_; 251 copy->icon_ = icon_;
251 copy->badge_text_ = badge_text_; 252 copy->badge_text_ = badge_text_;
252 copy->badge_background_color_ = badge_background_color_; 253 copy->badge_background_color_ = badge_background_color_;
253 copy->badge_text_color_ = badge_text_color_; 254 copy->badge_text_color_ = badge_text_color_;
254 copy->appearance_ = appearance_; 255 copy->appearance_ = appearance_;
255 copy->icon_animation_ = icon_animation_; 256 copy->icon_animation_ = icon_animation_;
256 copy->default_icon_path_ = default_icon_path_;
257 copy->id_ = id_; 257 copy->id_ = id_;
258
259 if (default_icon_.get()) {
260 scoped_ptr<ExtensionIconSet> default_icon_copy(new ExtensionIconSet());
261 default_icon_->CopyForTest(default_icon_copy.get());
262 copy->default_icon_ = default_icon_copy.Pass();
263 }
264
258 return copy.Pass(); 265 return copy.Pass();
259 } 266 }
260 267
268 // static
269 int ExtensionAction::GetIconSizeForType(ExtensionAction::Type type) {
270 switch (type) {
271 case ExtensionAction::TYPE_BROWSER:
272 case ExtensionAction::TYPE_PAGE:
273 return extension_misc::EXTENSION_ICON_ACTION;
274 case ExtensionAction::TYPE_SCRIPT_BADGE:
275 return extension_misc::EXTENSION_ICON_BITTY;
276 default:
277 NOTREACHED();
278 return 0;
279 }
280 }
281
261 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) { 282 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 283 // 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 284 // 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. 285 // 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 286 // If we removed the tab's URL, GetPopupURL would incorrectly return the
266 // default URL. 287 // default URL.
267 SetValue(&popup_url_, tab_id, url); 288 SetValue(&popup_url_, tab_id, url);
268 } 289 }
269 290
270 bool ExtensionAction::HasPopup(int tab_id) const { 291 bool ExtensionAction::HasPopup(int tab_id) const {
271 return !GetPopupUrl(tab_id).is_empty(); 292 return !GetPopupUrl(tab_id).is_empty();
272 } 293 }
273 294
274 GURL ExtensionAction::GetPopupUrl(int tab_id) const { 295 GURL ExtensionAction::GetPopupUrl(int tab_id) const {
275 return GetValue(&popup_url_, tab_id); 296 return GetValue(&popup_url_, tab_id);
276 } 297 }
277 298
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) { 299 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) {
284 SetValue(&icon_, tab_id, image.AsImageSkia()); 300 SetValue(&icon_, tab_id, image.AsImageSkia());
285 } 301 }
286 302
287 gfx::Image ExtensionAction::GetIcon(int tab_id) const { 303 gfx::Image ExtensionAction::ApplyAttentionAndAnimation(
288 // Check if a specific icon is set for this tab. 304 const gfx::ImageSkia& original_icon,
289 gfx::ImageSkia icon = GetExplicitlySetIcon(tab_id); 305 int tab_id) const {
290 if (icon.isNull()) { 306 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) 307 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION)
300 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size()); 308 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size());
301 309
302 return gfx::Image(ApplyIconAnimation(tab_id, icon)); 310 return gfx::Image(ApplyIconAnimation(tab_id, icon));
303 } 311 }
304 312
305 gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const { 313 gfx::ImageSkia ExtensionAction::GetExplicitlySetIcon(int tab_id) const {
306 return GetValue(&icon_, tab_id); 314 return GetValue(&icon_, tab_id);
307 } 315 }
308 316
(...skipping 27 matching lines...) Expand all
336 344
337 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, 345 void ExtensionAction::PaintBadge(gfx::Canvas* canvas,
338 const gfx::Rect& bounds, 346 const gfx::Rect& bounds,
339 int tab_id) { 347 int tab_id) {
340 ExtensionAction::DoPaintBadge( 348 ExtensionAction::DoPaintBadge(
341 canvas, 349 canvas,
342 bounds, 350 bounds,
343 GetBadgeText(tab_id), 351 GetBadgeText(tab_id),
344 GetBadgeTextColor(tab_id), 352 GetBadgeTextColor(tab_id),
345 GetBadgeBackgroundColor(tab_id), 353 GetBadgeBackgroundColor(tab_id),
346 GetValue(&icon_, tab_id).size().width()); 354 GetIconWidth(tab_id));
347 } 355 }
348 356
349 gfx::ImageSkia ExtensionAction::GetIconWithBadge( 357 gfx::ImageSkia ExtensionAction::GetIconWithBadge(
350 const gfx::ImageSkia& icon, 358 const gfx::ImageSkia& icon,
351 int tab_id, 359 int tab_id,
352 const gfx::Size& spacing) const { 360 const gfx::Size& spacing) const {
353 if (tab_id < 0) 361 if (tab_id < 0)
354 return icon; 362 return icon;
355 363
356 return gfx::ImageSkia( 364 return gfx::ImageSkia(
357 new IconWithBadgeImageSource(icon, 365 new IconWithBadgeImageSource(icon,
358 spacing, 366 spacing,
359 GetBadgeText(tab_id), 367 GetBadgeText(tab_id),
360 GetBadgeTextColor(tab_id), 368 GetBadgeTextColor(tab_id),
361 GetBadgeBackgroundColor(tab_id)), 369 GetBadgeBackgroundColor(tab_id)),
362 icon.size()); 370 icon.size());
363 } 371 }
364 372
373 // Determines which icon would be returned by |GetIcon|, and returns its width.
374 int ExtensionAction::GetIconWidth(int tab_id) const {
375 // If icon has been set, return its width.
376 gfx::ImageSkia icon = GetValue(&icon_, tab_id);
377 if (!icon.isNull())
378 return icon.width();
379 // If there is a default icon, the icon width will be set depending on our
380 // action type.
381 if (default_icon_.get())
382 return GetIconSizeForType(action_type());
383
384 // If no icon has been set and there is no default icon, we need favicon
385 // width.
386 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(
387 IDR_EXTENSIONS_FAVICON).ToImageSkia()->width();
388 }
389
365 // static 390 // static
366 void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas, 391 void ExtensionAction::DoPaintBadge(gfx::Canvas* canvas,
367 const gfx::Rect& bounds, 392 const gfx::Rect& bounds,
368 const std::string& text, 393 const std::string& text,
369 const SkColor& text_color_in, 394 const SkColor& text_color_in,
370 const SkColor& background_color_in, 395 const SkColor& background_color_in,
371 int icon_width) { 396 int icon_width) {
372 if (text.empty()) 397 if (text.empty())
373 return; 398 return;
374 399
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 icon_animation->Start(); 515 icon_animation->Start();
491 // After the icon is finished fading in (plus some padding to handle random 516 // 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 517 // 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 518 // deleted even if it hasn't finished by the time the MessageLoop is
494 // destroyed. 519 // destroyed.
495 MessageLoop::current()->PostDelayedTask( 520 MessageLoop::current()->PostDelayedTask(
496 FROM_HERE, 521 FROM_HERE,
497 base::Bind(&DestroyIconAnimation, base::Passed(icon_animation.Pass())), 522 base::Bind(&DestroyIconAnimation, base::Passed(icon_animation.Pass())),
498 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2)); 523 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2));
499 } 524 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698