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

Side by Side Diff: chrome/browser/extensions/script_badge_controller.cc

Issue 10559054: Animate the script badges. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: really sending for review... Created 8 years, 6 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
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/browser/extensions/script_badge_controller.h" 5 #include "chrome/browser/extensions/script_badge_controller.h"
6 6
7 #include "base/compiler_specific.h"
7 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_system.h" 9 #include "chrome/browser/extensions/extension_system.h"
9 #include "chrome/browser/ui/tab_contents/tab_contents.h" 10 #include "chrome/browser/ui/tab_contents/tab_contents.h"
10 #include "chrome/common/extensions/extension.h" 11 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/extension_action.h" 12 #include "chrome/common/extensions/extension_action.h"
12 #include "chrome/common/extensions/extension_messages.h" 13 #include "chrome/common/extensions/extension_messages.h"
13 #include "chrome/common/extensions/extension_set.h" 14 #include "chrome/common/extensions/extension_set.h"
14 #include "chrome/common/extensions/extension_switch_utils.h" 15 #include "chrome/common/extensions/extension_switch_utils.h"
15 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
16 #include "content/public/browser/navigation_controller.h" 17 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_entry.h" 18 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/notification_service.h" 19 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
20 #include "ipc/ipc_message.h" 21 #include "ipc/ipc_message.h"
21 #include "ipc/ipc_message_macros.h" 22 #include "ipc/ipc_message_macros.h"
23 #include "ui/base/animation/animation_delegate.h"
22 24
23 namespace extensions { 25 namespace extensions {
24 26
27 // Wraps an IconAnimation and implements its ui::AnimationDelegate to erase the
28 // animation from a map when the animation ends or is cancelled, causing itself
29 // and its owned IconAnimation to be deleted.
30 class ScriptBadgeController::IconAnimationWrapper
31 : public ui::AnimationDelegate {
32 public:
33 IconAnimationWrapper(ScriptBadgeController* owner, const std::string& key)
34 : owner_(owner),
35 key_(key),
36 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(owner, this)) {}
37
38 virtual ~IconAnimationWrapper() {}
39
40 IconAnimation* animation() {
41 return &animation_;
42 }
43
44 private:
45 virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE {
46 Done();
47 }
48
49 virtual void AnimationCanceled(const ui::Animation* animation) OVERRIDE {
50 Done();
51 }
52
53 void Done() {
54 owner_->icon_animations_.erase(key_);
55 // this will now have been deleted.
56 }
57
58 ScriptBadgeController* owner_;
59 std::string key_;
60 IconAnimation animation_;
61 };
62
25 ScriptBadgeController::ScriptBadgeController(TabContents* tab_contents) 63 ScriptBadgeController::ScriptBadgeController(TabContents* tab_contents)
26 : content::WebContentsObserver(tab_contents->web_contents()), 64 : content::WebContentsObserver(tab_contents->web_contents()),
27 script_executor_(tab_contents->web_contents()), 65 script_executor_(tab_contents->web_contents()),
28 tab_contents_(tab_contents) { 66 tab_contents_(tab_contents) {
29 registrar_.Add(this, 67 registrar_.Add(this,
30 chrome::NOTIFICATION_EXTENSION_UNLOADED, 68 chrome::NOTIFICATION_EXTENSION_UNLOADED,
31 content::Source<Profile>(tab_contents->profile())); 69 content::Source<Profile>(tab_contents->profile()));
32 } 70 }
33 71
34 ScriptBadgeController::~ScriptBadgeController() {} 72 ScriptBadgeController::~ScriptBadgeController() {}
35 73
36 std::vector<ExtensionAction*> ScriptBadgeController::GetCurrentActions() { 74 std::vector<ExtensionAction*> ScriptBadgeController::GetCurrentActions() const {
37 return current_actions_; 75 return current_actions_;
38 } 76 }
39 77
78 base::WeakPtr<LocationBarController::IconAnimation>
79 ScriptBadgeController::GetIconAnimation(
80 const ExtensionAction* action) const {
81 IconAnimationMap::const_iterator it =
82 icon_animations_.find(action->extension_id());
83 return (it != icon_animations_.end()) ? it->second->animation()->AsWeakPtr()
84 : base::WeakPtr<IconAnimation>();
85 }
86
40 LocationBarController::Action ScriptBadgeController::OnClicked( 87 LocationBarController::Action ScriptBadgeController::OnClicked(
41 const std::string& extension_id, int mouse_button) { 88 const std::string& extension_id, int mouse_button) {
42 ExtensionService* service = GetExtensionService(); 89 ExtensionService* service = GetExtensionService();
43 if (!service) 90 if (!service)
44 return ACTION_NONE; 91 return ACTION_NONE;
45 92
46 const Extension* extension = service->extensions()->GetByID(extension_id); 93 const Extension* extension = service->extensions()->GetByID(extension_id);
47 CHECK(extension); 94 CHECK(extension);
48 95
49 switch (mouse_button) { 96 switch (mouse_button) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 207
161 ExtensionService* service = GetExtensionService(); 208 ExtensionService* service = GetExtensionService();
162 if (!service) 209 if (!service)
163 return false; 210 return false;
164 211
165 const Extension* extension = service->extensions()->GetByID(extension_id); 212 const Extension* extension = service->extensions()->GetByID(extension_id);
166 if (!extension) 213 if (!extension)
167 return false; 214 return false;
168 215
169 current_actions_.push_back(extension->script_badge()); 216 current_actions_.push_back(extension->script_badge());
217
218 IconAnimationWrapper* icon_animation =
219 new IconAnimationWrapper(this, extension_id);
220 icon_animations_[extension_id] = make_linked_ptr(icon_animation);
221 icon_animation->animation()->Start();
222
170 return true; 223 return true;
171 } 224 }
172 225
173
174 bool ScriptBadgeController::EraseExtension(const Extension* extension) { 226 bool ScriptBadgeController::EraseExtension(const Extension* extension) {
175 if (extensions_executing_scripts_.erase(extension->id()) == 0) 227 if (extensions_executing_scripts_.erase(extension->id()) == 0)
176 return false; 228 return false;
177 229
178 size_t size_before = current_actions_.size(); 230 size_t size_before = current_actions_.size();
179 231
180 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin(); 232 for (std::vector<ExtensionAction*>::iterator it = current_actions_.begin();
181 it != current_actions_.end(); ++it) { 233 it != current_actions_.end(); ++it) {
182 // Safe to -> the extension action because we still have a handle to the 234 // Safe to -> the extension action because we still have a handle to the
183 // owner Extension. 235 // owner Extension.
184 // 236 //
185 // Also note that this means that when extensions are uninstalled their 237 // Also note that this means that when extensions are uninstalled their
186 // script badges will disappear, even though they're still acting on the 238 // script badges will disappear, even though they're still acting on the
187 // page (since they would have already acted). 239 // page (since they would have already acted).
188 if ((*it)->extension_id() == extension->id()) { 240 if ((*it)->extension_id() == extension->id()) {
189 current_actions_.erase(it); 241 current_actions_.erase(it);
190 break; 242 break;
191 } 243 }
192 } 244 }
193 245
194 CHECK_EQ(size_before, current_actions_.size() + 1); 246 CHECK_EQ(size_before, current_actions_.size() + 1);
247
248 icon_animations_.erase(extension->id());
249
195 return true; 250 return true;
196 } 251 }
197 252
198 } // namespace extensions 253 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698