OLD | NEW |
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/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/common/badge_util.h" | 10 #include "chrome/common/badge_util.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 const int kPadding = 2; | 52 const int kPadding = 2; |
53 // The padding between the top of the badge and the top of the text. | 53 // The padding between the top of the badge and the top of the text. |
54 const int kTopTextPadding = -1; | 54 const int kTopTextPadding = -1; |
55 #endif | 55 #endif |
56 | 56 |
57 const int kBadgeHeight = 11; | 57 const int kBadgeHeight = 11; |
58 const int kMaxTextWidth = 23; | 58 const int kMaxTextWidth = 23; |
59 // The minimum width for center-aligning the badge. | 59 // The minimum width for center-aligning the badge. |
60 const int kCenterAlignThreshold = 20; | 60 const int kCenterAlignThreshold = 20; |
61 | 61 |
| 62 void CopyExtensionIconSet(const ExtensionIconSet* from, ExtensionIconSet* to) { |
| 63 for (ExtensionIconSet::IconMap::const_iterator iter = from->map().begin(); |
| 64 iter != from->map().end(); |
| 65 ++iter) { |
| 66 to->Add(iter->first, iter->second); |
| 67 } |
| 68 } |
| 69 |
| 70 gfx::ImageSkia GetIconFromIconSet(const ExtensionIconSet* icon_set, |
| 71 ExtensionAction::IconFactory* icon_factory, |
| 72 ExtensionAction::Type type) { |
| 73 if (!icon_set || !icon_factory) |
| 74 return gfx::ImageSkia(); |
| 75 |
| 76 return icon_factory->GetIcon(icon_set, type); |
| 77 } |
| 78 |
62 class GetAttentionImageSource : public gfx::ImageSkiaSource { | 79 class GetAttentionImageSource : public gfx::ImageSkiaSource { |
63 public: | 80 public: |
64 explicit GetAttentionImageSource(const gfx::ImageSkia& icon) | 81 explicit GetAttentionImageSource(const gfx::ImageSkia& icon) |
65 : icon_(icon) {} | 82 : icon_(icon) {} |
66 | 83 |
67 // gfx::ImageSkiaSource overrides: | 84 // gfx::ImageSkiaSource overrides: |
68 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) | 85 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale_factor) |
69 OVERRIDE { | 86 OVERRIDE { |
70 gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor); | 87 gfx::ImageSkiaRep icon_rep = icon_.GetRepresentation(scale_factor); |
71 color_utils::HSL shift = {-1, 0, 0.5}; | 88 color_utils::HSL shift = {-1, 0, 0.5}; |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 new ExtensionAction(extension_id_, action_type_)); | 279 new ExtensionAction(extension_id_, action_type_)); |
263 copy->popup_url_ = popup_url_; | 280 copy->popup_url_ = popup_url_; |
264 copy->title_ = title_; | 281 copy->title_ = title_; |
265 copy->icon_ = icon_; | 282 copy->icon_ = icon_; |
266 copy->icon_index_ = icon_index_; | 283 copy->icon_index_ = icon_index_; |
267 copy->badge_text_ = badge_text_; | 284 copy->badge_text_ = badge_text_; |
268 copy->badge_background_color_ = badge_background_color_; | 285 copy->badge_background_color_ = badge_background_color_; |
269 copy->badge_text_color_ = badge_text_color_; | 286 copy->badge_text_color_ = badge_text_color_; |
270 copy->appearance_ = appearance_; | 287 copy->appearance_ = appearance_; |
271 copy->icon_animation_ = icon_animation_; | 288 copy->icon_animation_ = icon_animation_; |
272 copy->default_icon_path_ = default_icon_path_; | |
273 copy->id_ = id_; | 289 copy->id_ = id_; |
274 copy->icon_paths_ = icon_paths_; | 290 |
| 291 if (default_icon_.get()) { |
| 292 scoped_ptr<ExtensionIconSet> default_icon(new ExtensionIconSet()); |
| 293 CopyExtensionIconSet(default_icon_.get(), default_icon.get()); |
| 294 copy->default_icon_ = default_icon.Pass(); |
| 295 } |
| 296 |
| 297 for (size_t i = 0; i < page_action_icons_.size(); i++) { |
| 298 scoped_ptr<ExtensionIconSet> page_action_icon(new ExtensionIconSet()); |
| 299 CopyExtensionIconSet(page_action_icons_[i], page_action_icon.get()); |
| 300 copy->AddPageActionIcon(page_action_icon.release()); |
| 301 } |
| 302 |
275 return copy.Pass(); | 303 return copy.Pass(); |
276 } | 304 } |
277 | 305 |
| 306 void ExtensionAction::AddPageActionIcon(const ExtensionIconSet* icon_set) { |
| 307 page_action_icons_.push_back(icon_set); |
| 308 } |
| 309 |
| 310 bool ExtensionAction::IsValidIconIndex(int index) const { |
| 311 return index >= 0 && (static_cast<size_t>(index) < page_action_icons_.size()); |
| 312 } |
| 313 |
278 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) { | 314 void ExtensionAction::SetPopupUrl(int tab_id, const GURL& url) { |
279 // We store |url| even if it is empty, rather than removing a URL from the | 315 // We store |url| even if it is empty, rather than removing a URL from the |
280 // map. If an extension has a default popup, and removes it for a tab via | 316 // map. If an extension has a default popup, and removes it for a tab via |
281 // the API, we must remember that there is no popup for that specific tab. | 317 // the API, we must remember that there is no popup for that specific tab. |
282 // If we removed the tab's URL, GetPopupURL would incorrectly return the | 318 // If we removed the tab's URL, GetPopupURL would incorrectly return the |
283 // default URL. | 319 // default URL. |
284 SetValue(&popup_url_, tab_id, url); | 320 SetValue(&popup_url_, tab_id, url); |
285 } | 321 } |
286 | 322 |
287 bool ExtensionAction::HasPopup(int tab_id) const { | 323 bool ExtensionAction::HasPopup(int tab_id) const { |
288 return !GetPopupUrl(tab_id).is_empty(); | 324 return !GetPopupUrl(tab_id).is_empty(); |
289 } | 325 } |
290 | 326 |
291 GURL ExtensionAction::GetPopupUrl(int tab_id) const { | 327 GURL ExtensionAction::GetPopupUrl(int tab_id) const { |
292 return GetValue(&popup_url_, tab_id); | 328 return GetValue(&popup_url_, tab_id); |
293 } | 329 } |
294 | 330 |
295 void ExtensionAction::CacheIcon(const std::string& path, | |
296 const gfx::Image& icon) { | |
297 if (!icon.IsEmpty()) | |
298 path_to_icon_cache_.insert(std::make_pair(path, *icon.ToImageSkia())); | |
299 } | |
300 | |
301 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) { | 331 void ExtensionAction::SetIcon(int tab_id, const gfx::Image& image) { |
302 SetValue(&icon_, tab_id, image.AsImageSkia()); | 332 SetValue(&icon_, tab_id, image.AsImageSkia()); |
303 } | 333 } |
304 | 334 |
305 gfx::Image ExtensionAction::GetIcon(int tab_id) const { | 335 gfx::Image ExtensionAction::GetIcon(int tab_id, |
| 336 IconFactory* icon_factory) const { |
306 // Check if a specific icon is set for this tab. | 337 // Check if a specific icon is set for this tab. |
307 gfx::ImageSkia icon = GetValue(&icon_, tab_id); | 338 gfx::ImageSkia icon = GetValue(&icon_, tab_id); |
308 if (icon.isNull()) { | 339 if (icon.isNull()) { |
309 // Need to find an icon from a path. | |
310 const std::string* path = NULL; | |
311 // Check if one of the elements of icon_path() was selected. | 340 // Check if one of the elements of icon_path() was selected. |
312 int icon_index = GetIconIndex(tab_id); | 341 int icon_index = GetIconIndex(tab_id); |
| 342 const ExtensionIconSet* icon_set = NULL; |
313 if (icon_index >= 0) { | 343 if (icon_index >= 0) { |
314 path = &icon_paths()->at(icon_index); | 344 icon_set = page_action_icons_[icon_index]; |
315 } else { | 345 } else { |
316 // Otherwise, use the default icon. | 346 icon_set = default_icon_.get(); |
317 path = &default_icon_path(); | |
318 } | 347 } |
319 | 348 |
320 std::map<std::string, gfx::ImageSkia>::const_iterator cached_icon = | 349 icon = GetIconFromIconSet(icon_set, icon_factory, action_type()); |
321 path_to_icon_cache_.find(*path); | 350 |
322 if (cached_icon != path_to_icon_cache_.end()) { | 351 // Extension favicon is our last resort. |
323 icon = cached_icon->second; | 352 if (icon.isNull()) { |
324 } else { | |
325 icon = *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 353 icon = *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
326 IDR_EXTENSIONS_FAVICON); | 354 IDR_EXTENSIONS_FAVICON); |
327 } | 355 } |
328 } | 356 } |
329 | 357 |
330 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION) | 358 if (GetValue(&appearance_, tab_id) == WANTS_ATTENTION) |
331 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size()); | 359 icon = gfx::ImageSkia(new GetAttentionImageSource(icon), icon.size()); |
332 | 360 |
333 return gfx::Image(ApplyIconAnimation(tab_id, icon)); | 361 return gfx::Image(ApplyIconAnimation(tab_id, icon)); |
334 } | 362 } |
335 | 363 |
336 void ExtensionAction::SetIconIndex(int tab_id, int index) { | 364 void ExtensionAction::SetIconIndex(int tab_id, int index) { |
337 if (static_cast<size_t>(index) >= icon_paths_.size()) { | 365 if (static_cast<size_t>(index) >= page_action_icons_.size()) { |
338 NOTREACHED(); | 366 NOTREACHED(); |
339 return; | 367 return; |
340 } | 368 } |
341 SetValue(&icon_index_, tab_id, index); | 369 SetValue(&icon_index_, tab_id, index); |
342 } | 370 } |
343 | 371 |
344 bool ExtensionAction::SetAppearance(int tab_id, Appearance new_appearance) { | 372 bool ExtensionAction::SetAppearance(int tab_id, Appearance new_appearance) { |
345 const Appearance old_appearance = GetValue(&appearance_, tab_id); | 373 const Appearance old_appearance = GetValue(&appearance_, tab_id); |
346 | 374 |
347 if (old_appearance == new_appearance) | 375 if (old_appearance == new_appearance) |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 new AnimatedIconImageSource(icon, animation_wrapper->animation()), | 549 new AnimatedIconImageSource(icon, animation_wrapper->animation()), |
522 icon.size()); | 550 icon.size()); |
523 } | 551 } |
524 | 552 |
525 void ExtensionAction::RunIconAnimation(int tab_id) { | 553 void ExtensionAction::RunIconAnimation(int tab_id) { |
526 IconAnimationWrapper* icon_animation = | 554 IconAnimationWrapper* icon_animation = |
527 new IconAnimationWrapper(); | 555 new IconAnimationWrapper(); |
528 icon_animation_[tab_id] = icon_animation->AsWeakPtr(); | 556 icon_animation_[tab_id] = icon_animation->AsWeakPtr(); |
529 icon_animation->animation()->Start(); | 557 icon_animation->animation()->Start(); |
530 } | 558 } |
OLD | NEW |