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/ui/views/location_bar/page_action_image_view.h" | 5 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
10 #include "chrome/browser/extensions/extension_browser_event_router.h" | 10 #include "chrome/browser/extensions/extension_browser_event_router.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 | 38 |
39 PageActionImageView::PageActionImageView(LocationBarView* owner, | 39 PageActionImageView::PageActionImageView(LocationBarView* owner, |
40 ExtensionAction* page_action, | 40 ExtensionAction* page_action, |
41 Browser* browser) | 41 Browser* browser) |
42 : owner_(owner), | 42 : owner_(owner), |
43 page_action_(page_action), | 43 page_action_(page_action), |
44 browser_(browser), | 44 browser_(browser), |
45 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 45 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
46 current_tab_id_(-1), | 46 current_tab_id_(-1), |
47 preview_enabled_(false), | 47 preview_enabled_(false), |
48 popup_(NULL) { | 48 popup_(NULL), |
| 49 ALLOW_THIS_IN_INITIALIZER_LIST(scoped_icon_animation_observer_( |
| 50 page_action->GetIconAnimation( |
| 51 owner->GetTabContents()->extension_tab_helper()->tab_id()), |
| 52 this)) { |
49 const Extension* extension = owner_->profile()->GetExtensionService()-> | 53 const Extension* extension = owner_->profile()->GetExtensionService()-> |
50 GetExtensionById(page_action->extension_id(), false); | 54 GetExtensionById(page_action->extension_id(), false); |
51 DCHECK(extension); | 55 DCHECK(extension); |
52 | 56 |
53 // Load all the icons declared in the manifest. This is the contents of the | 57 // Load all the icons declared in the manifest. This is the contents of the |
54 // icons array, plus the default_icon property, if any. | 58 // icons array, plus the default_icon property, if any. |
55 std::vector<std::string> icon_paths(*page_action->icon_paths()); | 59 std::vector<std::string> icon_paths(*page_action->icon_paths()); |
56 if (!page_action_->default_icon_path().empty()) | 60 if (!page_action_->default_icon_path().empty()) |
57 icon_paths.push_back(page_action_->default_icon_path()); | 61 icon_paths.push_back(page_action_->default_icon_path()); |
58 | 62 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 // Map the index of the loaded image back to its name. If we ever get an | 199 // Map the index of the loaded image back to its name. If we ever get an |
196 // index greater than the number of icons, it must be the default icon. | 200 // index greater than the number of icons, it must be the default icon. |
197 if (!image.IsEmpty()) { | 201 if (!image.IsEmpty()) { |
198 const SkBitmap* bitmap = image.ToSkBitmap(); | 202 const SkBitmap* bitmap = image.ToSkBitmap(); |
199 if (index < static_cast<int>(page_action_->icon_paths()->size())) | 203 if (index < static_cast<int>(page_action_->icon_paths()->size())) |
200 page_action_icons_[page_action_->icon_paths()->at(index)] = *bitmap; | 204 page_action_icons_[page_action_->icon_paths()->at(index)] = *bitmap; |
201 else | 205 else |
202 page_action_icons_[page_action_->default_icon_path()] = *bitmap; | 206 page_action_icons_[page_action_->default_icon_path()] = *bitmap; |
203 } | 207 } |
204 | 208 |
205 // During object construction (before the parent has been set) we are already | 209 // During object construction owner_ will be NULL. |
206 // in a UpdatePageActions call, so we don't need to start another one (and | 210 TabContents* tab_contents = owner_ ? owner_->GetTabContents() : NULL; |
207 // doing so causes crash described in http://crbug.com/57333). | 211 if (tab_contents) |
208 if (parent()) | 212 UpdateVisibility(tab_contents->web_contents(), current_url_); |
209 owner_->UpdatePageActions(); | |
210 } | 213 } |
211 | 214 |
212 bool PageActionImageView::AcceleratorPressed( | 215 bool PageActionImageView::AcceleratorPressed( |
213 const ui::Accelerator& accelerator) { | 216 const ui::Accelerator& accelerator) { |
214 DCHECK(visible()); // Should not have happened due to CanHandleAccelerator. | 217 DCHECK(visible()); // Should not have happened due to CanHandleAccelerator. |
215 | 218 |
216 ExecuteAction(1); // 1 means left-click. | 219 ExecuteAction(1); // 1 means left-click. |
217 return true; | 220 return true; |
218 } | 221 } |
219 | 222 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 int icon_index = page_action_->GetIconIndex(current_tab_id_); | 258 int icon_index = page_action_->GetIconIndex(current_tab_id_); |
256 std::string icon_path = (icon_index < 0) ? | 259 std::string icon_path = (icon_index < 0) ? |
257 page_action_->default_icon_path() : | 260 page_action_->default_icon_path() : |
258 page_action_->icon_paths()->at(icon_index); | 261 page_action_->icon_paths()->at(icon_index); |
259 if (!icon_path.empty()) { | 262 if (!icon_path.empty()) { |
260 PageActionMap::iterator iter = page_action_icons_.find(icon_path); | 263 PageActionMap::iterator iter = page_action_icons_.find(icon_path); |
261 if (iter != page_action_icons_.end()) | 264 if (iter != page_action_icons_.end()) |
262 icon = iter->second; | 265 icon = iter->second; |
263 } | 266 } |
264 } | 267 } |
265 if (!icon.isNull()) | 268 |
| 269 if (!icon.isNull()) { |
| 270 const ExtensionAction::IconAnimation* icon_animation = |
| 271 scoped_icon_animation_observer_.icon_animation(); |
| 272 if (icon_animation) |
| 273 icon = icon_animation->Apply(icon); |
266 SetImage(icon); | 274 SetImage(icon); |
| 275 } |
267 | 276 |
268 SetVisible(true); | 277 SetVisible(true); |
269 } | 278 } |
270 | 279 |
271 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { | 280 void PageActionImageView::OnWidgetClosing(views::Widget* widget) { |
272 DCHECK_EQ(popup_->GetWidget(), widget); | 281 DCHECK_EQ(popup_->GetWidget(), widget); |
273 popup_->GetWidget()->RemoveObserver(this); | 282 popup_->GetWidget()->RemoveObserver(this); |
274 popup_ = NULL; | 283 popup_ = NULL; |
275 } | 284 } |
276 | 285 |
277 void PageActionImageView::Observe(int type, | 286 void PageActionImageView::Observe(int type, |
278 const content::NotificationSource& source, | 287 const content::NotificationSource& source, |
279 const content::NotificationDetails& details) { | 288 const content::NotificationDetails& details) { |
280 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); | 289 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNLOADED, type); |
281 const Extension* unloaded_extension = | 290 const Extension* unloaded_extension = |
282 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; | 291 content::Details<extensions::UnloadedExtensionInfo>(details)->extension; |
283 if (page_action_ == unloaded_extension ->page_action()) | 292 if (page_action_ == unloaded_extension ->page_action()) |
284 owner_->UpdatePageActions(); | 293 owner_->UpdatePageActions(); |
285 } | 294 } |
286 | 295 |
| 296 void PageActionImageView::OnIconChanged( |
| 297 const ExtensionAction::IconAnimation& animation) { |
| 298 TabContents* tab_contents = owner_->GetTabContents(); |
| 299 if (tab_contents) |
| 300 UpdateVisibility(tab_contents->web_contents(), current_url_); |
| 301 } |
| 302 |
287 void PageActionImageView::ShowPopupWithURL(const GURL& popup_url) { | 303 void PageActionImageView::ShowPopupWithURL(const GURL& popup_url) { |
288 bool popup_showing = popup_ != NULL; | 304 bool popup_showing = popup_ != NULL; |
289 | 305 |
290 // Always hide the current popup. Only one popup at a time. | 306 // Always hide the current popup. Only one popup at a time. |
291 HidePopup(); | 307 HidePopup(); |
292 | 308 |
293 // If we were already showing, then treat this click as a dismiss. | 309 // If we were already showing, then treat this click as a dismiss. |
294 if (popup_showing) | 310 if (popup_showing) |
295 return; | 311 return; |
296 | 312 |
297 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 313 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
298 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 314 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
299 | 315 |
300 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location); | 316 popup_ = ExtensionPopup::ShowPopup(popup_url, browser_, this, arrow_location); |
301 popup_->GetWidget()->AddObserver(this); | 317 popup_->GetWidget()->AddObserver(this); |
302 } | 318 } |
303 | 319 |
304 void PageActionImageView::HidePopup() { | 320 void PageActionImageView::HidePopup() { |
305 if (popup_) | 321 if (popup_) |
306 popup_->GetWidget()->Close(); | 322 popup_->GetWidget()->Close(); |
307 } | 323 } |
OLD | NEW |