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/browser/extensions/extension_action.h" | 5 #include "chrome/browser/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" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 base::WeakPtr<ExtensionAction::IconAnimation> animation) | 63 base::WeakPtr<ExtensionAction::IconAnimation> animation) |
64 : image_(image), | 64 : image_(image), |
65 animation_(animation) { | 65 animation_(animation) { |
66 } | 66 } |
67 | 67 |
68 private: | 68 private: |
69 virtual ~AnimatedIconImageSource() {} | 69 virtual ~AnimatedIconImageSource() {} |
70 | 70 |
71 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale) OVERRIDE { | 71 virtual gfx::ImageSkiaRep GetImageForScale(ui::ScaleFactor scale) OVERRIDE { |
72 gfx::ImageSkiaRep original_rep = image_.GetRepresentation(scale); | 72 gfx::ImageSkiaRep original_rep = image_.GetRepresentation(scale); |
73 if (!animation_) | 73 if (!animation_.get()) |
74 return original_rep; | 74 return original_rep; |
75 | 75 |
76 // Original representation's scale factor may be different from scale | 76 // Original representation's scale factor may be different from scale |
77 // factor passed to this method. We want to use the former (since we are | 77 // factor passed to this method. We want to use the former (since we are |
78 // using bitmap for that scale). | 78 // using bitmap for that scale). |
79 return gfx::ImageSkiaRep( | 79 return gfx::ImageSkiaRep( |
80 animation_->Apply(original_rep.sk_bitmap()), | 80 animation_->Apply(original_rep.sk_bitmap()), |
81 original_rep.scale_factor()); | 81 original_rep.scale_factor()); |
82 } | 82 } |
83 | 83 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 142 |
143 void ExtensionAction::IconAnimation::AnimateToState(double state) { | 143 void ExtensionAction::IconAnimation::AnimateToState(double state) { |
144 FOR_EACH_OBSERVER(Observer, observers_, OnIconChanged()); | 144 FOR_EACH_OBSERVER(Observer, observers_, OnIconChanged()); |
145 } | 145 } |
146 | 146 |
147 ExtensionAction::IconAnimation::ScopedObserver::ScopedObserver( | 147 ExtensionAction::IconAnimation::ScopedObserver::ScopedObserver( |
148 const base::WeakPtr<IconAnimation>& icon_animation, | 148 const base::WeakPtr<IconAnimation>& icon_animation, |
149 Observer* observer) | 149 Observer* observer) |
150 : icon_animation_(icon_animation), | 150 : icon_animation_(icon_animation), |
151 observer_(observer) { | 151 observer_(observer) { |
152 if (icon_animation) | 152 if (icon_animation.get()) |
153 icon_animation->AddObserver(observer); | 153 icon_animation->AddObserver(observer); |
154 } | 154 } |
155 | 155 |
156 ExtensionAction::IconAnimation::ScopedObserver::~ScopedObserver() { | 156 ExtensionAction::IconAnimation::ScopedObserver::~ScopedObserver() { |
157 if (icon_animation_) | 157 if (icon_animation_.get()) |
158 icon_animation_->RemoveObserver(observer_); | 158 icon_animation_->RemoveObserver(observer_); |
159 } | 159 } |
160 | 160 |
161 ExtensionAction::ExtensionAction( | 161 ExtensionAction::ExtensionAction( |
162 const std::string& extension_id, | 162 const std::string& extension_id, |
163 extensions::ActionInfo::Type action_type, | 163 extensions::ActionInfo::Type action_type, |
164 const extensions::ActionInfo& manifest_data) | 164 const extensions::ActionInfo& manifest_data) |
165 : extension_id_(extension_id), | 165 : extension_id_(extension_id), |
166 action_type_(action_type), | 166 action_type_(action_type), |
167 has_changed_(false) { | 167 has_changed_(false) { |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 347 return ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
348 IDR_EXTENSIONS_FAVICON).ToImageSkia()->width(); | 348 IDR_EXTENSIONS_FAVICON).ToImageSkia()->width(); |
349 } | 349 } |
350 | 350 |
351 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation( | 351 base::WeakPtr<ExtensionAction::IconAnimation> ExtensionAction::GetIconAnimation( |
352 int tab_id) const { | 352 int tab_id) const { |
353 std::map<int, base::WeakPtr<IconAnimation> >::iterator it = | 353 std::map<int, base::WeakPtr<IconAnimation> >::iterator it = |
354 icon_animation_.find(tab_id); | 354 icon_animation_.find(tab_id); |
355 if (it == icon_animation_.end()) | 355 if (it == icon_animation_.end()) |
356 return base::WeakPtr<ExtensionAction::IconAnimation>(); | 356 return base::WeakPtr<ExtensionAction::IconAnimation>(); |
357 if (it->second) | 357 if (it->second.get()) |
358 return it->second; | 358 return it->second; |
359 | 359 |
360 // Take this opportunity to remove all the NULL IconAnimations from | 360 // Take this opportunity to remove all the NULL IconAnimations from |
361 // icon_animation_. | 361 // icon_animation_. |
362 icon_animation_.erase(it); | 362 icon_animation_.erase(it); |
363 for (it = icon_animation_.begin(); it != icon_animation_.end();) { | 363 for (it = icon_animation_.begin(); it != icon_animation_.end();) { |
364 if (it->second) { | 364 if (it->second.get()) { |
365 ++it; | 365 ++it; |
366 } else { | 366 } else { |
367 // The WeakPtr is null; remove it from the map. | 367 // The WeakPtr is null; remove it from the map. |
368 icon_animation_.erase(it++); | 368 icon_animation_.erase(it++); |
369 } | 369 } |
370 } | 370 } |
371 return base::WeakPtr<ExtensionAction::IconAnimation>(); | 371 return base::WeakPtr<ExtensionAction::IconAnimation>(); |
372 } | 372 } |
373 | 373 |
374 gfx::ImageSkia ExtensionAction::ApplyIconAnimation( | 374 gfx::ImageSkia ExtensionAction::ApplyIconAnimation( |
375 int tab_id, | 375 int tab_id, |
376 const gfx::ImageSkia& icon) const { | 376 const gfx::ImageSkia& icon) const { |
377 base::WeakPtr<IconAnimation> animation = GetIconAnimation(tab_id); | 377 base::WeakPtr<IconAnimation> animation = GetIconAnimation(tab_id); |
378 if (animation == NULL) | 378 if (animation.get() == NULL) |
379 return icon; | 379 return icon; |
380 | 380 |
381 return gfx::ImageSkia(new AnimatedIconImageSource(icon, animation), | 381 return gfx::ImageSkia(new AnimatedIconImageSource(icon, animation), |
382 icon.size()); | 382 icon.size()); |
383 } | 383 } |
384 | 384 |
385 namespace { | 385 namespace { |
386 // Used to create a Callback owning an IconAnimation. | 386 // Used to create a Callback owning an IconAnimation. |
387 void DestroyIconAnimation(scoped_ptr<ExtensionAction::IconAnimation>) {} | 387 void DestroyIconAnimation(scoped_ptr<ExtensionAction::IconAnimation>) {} |
388 } | 388 } |
389 void ExtensionAction::RunIconAnimation(int tab_id) { | 389 void ExtensionAction::RunIconAnimation(int tab_id) { |
390 scoped_ptr<IconAnimation> icon_animation(new IconAnimation()); | 390 scoped_ptr<IconAnimation> icon_animation(new IconAnimation()); |
391 icon_animation_[tab_id] = icon_animation->AsWeakPtr(); | 391 icon_animation_[tab_id] = icon_animation->AsWeakPtr(); |
392 icon_animation->Start(); | 392 icon_animation->Start(); |
393 // After the icon is finished fading in (plus some padding to handle random | 393 // After the icon is finished fading in (plus some padding to handle random |
394 // timer delays), destroy it. We use a delayed task so that the Animation is | 394 // timer delays), destroy it. We use a delayed task so that the Animation is |
395 // deleted even if it hasn't finished by the time the MessageLoop is | 395 // deleted even if it hasn't finished by the time the MessageLoop is |
396 // destroyed. | 396 // destroyed. |
397 base::MessageLoop::current()->PostDelayedTask( | 397 base::MessageLoop::current()->PostDelayedTask( |
398 FROM_HERE, | 398 FROM_HERE, |
399 base::Bind(&DestroyIconAnimation, base::Passed(&icon_animation)), | 399 base::Bind(&DestroyIconAnimation, base::Passed(&icon_animation)), |
400 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2)); | 400 base::TimeDelta::FromMilliseconds(kIconFadeInDurationMs * 2)); |
401 } | 401 } |
OLD | NEW |