| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/views/browser_actions_container.h" | 5 #include "chrome/browser/views/browser_actions_container.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "app/slide_animation.h" | 9 #include "app/slide_animation.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 bool BrowserActionsContainer::disable_animations_during_testing_ = false; | 91 bool BrowserActionsContainer::disable_animations_during_testing_ = false; |
| 92 | 92 |
| 93 //////////////////////////////////////////////////////////////////////////////// | 93 //////////////////////////////////////////////////////////////////////////////// |
| 94 // BrowserActionButton | 94 // BrowserActionButton |
| 95 | 95 |
| 96 BrowserActionButton::BrowserActionButton(Extension* extension, | 96 BrowserActionButton::BrowserActionButton(Extension* extension, |
| 97 BrowserActionsContainer* panel) | 97 BrowserActionsContainer* panel) |
| 98 : ALLOW_THIS_IN_INITIALIZER_LIST(MenuButton(this, L"", NULL, false)), | 98 : ALLOW_THIS_IN_INITIALIZER_LIST(MenuButton(this, L"", NULL, false)), |
| 99 browser_action_(extension->browser_action()), | 99 browser_action_(extension->browser_action()), |
| 100 extension_(extension), | 100 extension_(extension), |
| 101 tracker_(NULL), | 101 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
| 102 showing_context_menu_(false), | 102 showing_context_menu_(false), |
| 103 panel_(panel) { | 103 panel_(panel) { |
| 104 set_alignment(TextButton::ALIGN_CENTER); | 104 set_alignment(TextButton::ALIGN_CENTER); |
| 105 | 105 |
| 106 // No UpdateState() here because View hierarchy not setup yet. Our parent | 106 // No UpdateState() here because View hierarchy not setup yet. Our parent |
| 107 // should call UpdateState() after creation. | 107 // should call UpdateState() after creation. |
| 108 | 108 |
| 109 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 109 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 110 Source<ExtensionAction>(browser_action_)); | 110 Source<ExtensionAction>(browser_action_)); |
| 111 | 111 |
| 112 // The Browser Action API does not allow the default icon path to be changed | 112 // The Browser Action API does not allow the default icon path to be changed |
| 113 // at runtime, so we can load this now and cache it. | 113 // at runtime, so we can load this now and cache it. |
| 114 std::string relative_path = browser_action_->default_icon_path(); | 114 std::string relative_path = browser_action_->default_icon_path(); |
| 115 if (relative_path.empty()) | 115 if (relative_path.empty()) |
| 116 return; | 116 return; |
| 117 | 117 |
| 118 // This is a bit sketchy because if ImageLoadingTracker calls | 118 // This is a bit sketchy because if ImageLoadingTracker calls |
| 119 // ::OnImageLoaded() before our creator appends up to the view hierarchy, we | 119 // ::OnImageLoaded() before our creator appends up to the view hierarchy, we |
| 120 // will crash. But since we know that ImageLoadingTracker is asynchronous, | 120 // will crash. But since we know that ImageLoadingTracker is asynchronous, |
| 121 // this should be OK. And doing this in the constructor means that we don't | 121 // this should be OK. And doing this in the constructor means that we don't |
| 122 // have to protect against it getting done multiple times. | 122 // have to protect against it getting done multiple times. |
| 123 tracker_ = new ImageLoadingTracker(this, 1); | 123 tracker_.LoadImage(extension->GetResource(relative_path), |
| 124 tracker_->PostLoadImageTask( | 124 gfx::Size(Extension::kBrowserActionIconMaxSize, |
| 125 extension->GetResource(relative_path), | 125 Extension::kBrowserActionIconMaxSize)); |
| 126 gfx::Size(Extension::kBrowserActionIconMaxSize, | |
| 127 Extension::kBrowserActionIconMaxSize)); | |
| 128 } | 126 } |
| 129 | 127 |
| 130 BrowserActionButton::~BrowserActionButton() { | 128 BrowserActionButton::~BrowserActionButton() { |
| 131 if (tracker_) | |
| 132 tracker_->StopTrackingImageLoad(); | |
| 133 } | 129 } |
| 134 | 130 |
| 135 gfx::Insets BrowserActionButton::GetInsets() const { | 131 gfx::Insets BrowserActionButton::GetInsets() const { |
| 136 static gfx::Insets zero_inset; | 132 static gfx::Insets zero_inset; |
| 137 return zero_inset; | 133 return zero_inset; |
| 138 } | 134 } |
| 139 | 135 |
| 140 void BrowserActionButton::ButtonPressed(views::Button* sender, | 136 void BrowserActionButton::ButtonPressed(views::Button* sender, |
| 141 const views::Event& event) { | 137 const views::Event& event) { |
| 142 panel_->OnBrowserActionExecuted(this, false); // inspect_with_devtools | 138 panel_->OnBrowserActionExecuted(this, false); // inspect_with_devtools |
| 143 } | 139 } |
| 144 | 140 |
| 145 void BrowserActionButton::OnImageLoaded(SkBitmap* image, size_t index) { | 141 void BrowserActionButton::OnImageLoaded( |
| 142 SkBitmap* image, ExtensionResource resource, int index) { |
| 146 if (image) | 143 if (image) |
| 147 default_icon_ = *image; | 144 default_icon_ = *image; |
| 148 | 145 |
| 149 tracker_ = NULL; // The tracker object will delete itself when we return. | |
| 150 | |
| 151 // Call back to UpdateState() because a more specific icon might have been set | 146 // Call back to UpdateState() because a more specific icon might have been set |
| 152 // while the load was outstanding. | 147 // while the load was outstanding. |
| 153 UpdateState(); | 148 UpdateState(); |
| 154 } | 149 } |
| 155 | 150 |
| 156 void BrowserActionButton::UpdateState() { | 151 void BrowserActionButton::UpdateState() { |
| 157 int tab_id = panel_->GetCurrentTabId(); | 152 int tab_id = panel_->GetCurrentTabId(); |
| 158 if (tab_id < 0) | 153 if (tab_id < 0) |
| 159 return; | 154 return; |
| 160 | 155 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 if (tab_id < 0) { | 195 if (tab_id < 0) { |
| 201 NOTREACHED() << "Button is not on a specific tab."; | 196 NOTREACHED() << "Button is not on a specific tab."; |
| 202 GURL empty_url; | 197 GURL empty_url; |
| 203 return empty_url; | 198 return empty_url; |
| 204 } | 199 } |
| 205 return browser_action_->GetPopupUrl(tab_id); | 200 return browser_action_->GetPopupUrl(tab_id); |
| 206 } | 201 } |
| 207 | 202 |
| 208 bool BrowserActionButton::Activate() { | 203 bool BrowserActionButton::Activate() { |
| 209 if (IsPopup()) { | 204 if (IsPopup()) { |
| 210 panel_->OnBrowserActionExecuted(this, false); // inspect_with_devtools | 205 panel_->OnBrowserActionExecuted(this, false); // |inspect_with_devtools|. |
| 211 | 206 |
| 212 // TODO(erikkay): Run a nested modal loop while the mouse is down to | 207 // TODO(erikkay): Run a nested modal loop while the mouse is down to |
| 213 // enable menu-like drag-select behavior. | 208 // enable menu-like drag-select behavior. |
| 214 | 209 |
| 215 // The return value of this method is returned via OnMousePressed. | 210 // The return value of this method is returned via OnMousePressed. |
| 216 // We need to return false here since we're handing off focus to another | 211 // We need to return false here since we're handing off focus to another |
| 217 // widget/view, and true will grab it right back and try to send events | 212 // widget/view, and true will grab it right back and try to send events |
| 218 // to us. | 213 // to us. |
| 219 return false; | 214 return false; |
| 220 } | 215 } |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 owner_view_->SchedulePaint(); | 499 owner_view_->SchedulePaint(); |
| 505 } | 500 } |
| 506 | 501 |
| 507 void BrowserActionsContainer::HidePopup() { | 502 void BrowserActionsContainer::HidePopup() { |
| 508 if (popup_) | 503 if (popup_) |
| 509 popup_->Close(); | 504 popup_->Close(); |
| 510 } | 505 } |
| 511 | 506 |
| 512 void BrowserActionsContainer::TestExecuteBrowserAction(int index) { | 507 void BrowserActionsContainer::TestExecuteBrowserAction(int index) { |
| 513 BrowserActionButton* button = browser_action_views_[index]->button(); | 508 BrowserActionButton* button = browser_action_views_[index]->button(); |
| 514 OnBrowserActionExecuted(button, false); // inspect_with_devtools | 509 OnBrowserActionExecuted(button, false); // |inspect_with_devtools|. |
| 515 } | 510 } |
| 516 | 511 |
| 517 void BrowserActionsContainer::TestSetIconVisibilityCount(size_t icons) { | 512 void BrowserActionsContainer::TestSetIconVisibilityCount(size_t icons) { |
| 518 chevron_->SetVisible(icons < browser_action_views_.size()); | 513 chevron_->SetVisible(icons < browser_action_views_.size()); |
| 519 container_size_.set_width(IconCountToWidth(icons)); | 514 container_size_.set_width(IconCountToWidth(icons)); |
| 520 Layout(); | 515 Layout(); |
| 521 SchedulePaint(); | 516 SchedulePaint(); |
| 522 } | 517 } |
| 523 | 518 |
| 524 void BrowserActionsContainer::OnBrowserActionExecuted( | 519 void BrowserActionsContainer::OnBrowserActionExecuted( |
| (...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1107 | 1102 |
| 1108 void BrowserActionsContainer::NotifyMenuDeleted( | 1103 void BrowserActionsContainer::NotifyMenuDeleted( |
| 1109 BrowserActionOverflowMenuController* controller) { | 1104 BrowserActionOverflowMenuController* controller) { |
| 1110 DCHECK(controller == overflow_menu_); | 1105 DCHECK(controller == overflow_menu_); |
| 1111 overflow_menu_ = NULL; | 1106 overflow_menu_ = NULL; |
| 1112 } | 1107 } |
| 1113 | 1108 |
| 1114 void BrowserActionsContainer::InspectPopup( | 1109 void BrowserActionsContainer::InspectPopup( |
| 1115 ExtensionAction* action) { | 1110 ExtensionAction* action) { |
| 1116 OnBrowserActionExecuted(GetBrowserActionView(action)->button(), | 1111 OnBrowserActionExecuted(GetBrowserActionView(action)->button(), |
| 1117 true); // inspect_with_devtools | 1112 true); // |inspect_with_devtools|. |
| 1118 } | 1113 } |
| 1119 | 1114 |
| 1120 void BrowserActionsContainer::ExtensionPopupClosed(ExtensionPopup* popup) { | 1115 void BrowserActionsContainer::ExtensionPopupClosed(ExtensionPopup* popup) { |
| 1121 // ExtensionPopup is ref-counted, so we don't need to delete it. | 1116 // ExtensionPopup is ref-counted, so we don't need to delete it. |
| 1122 DCHECK_EQ(popup_, popup); | 1117 DCHECK_EQ(popup_, popup); |
| 1123 popup_ = NULL; | 1118 popup_ = NULL; |
| 1124 popup_button_->SetButtonNotPushed(); | 1119 popup_button_->SetButtonNotPushed(); |
| 1125 popup_button_ = NULL; | 1120 popup_button_ = NULL; |
| 1126 } | 1121 } |
| 1127 | 1122 |
| 1128 bool BrowserActionsContainer::ShouldDisplayBrowserAction(Extension* extension) { | 1123 bool BrowserActionsContainer::ShouldDisplayBrowserAction(Extension* extension) { |
| 1129 // Only display incognito-enabled extensions while in incognito mode. | 1124 // Only display incognito-enabled extensions while in incognito mode. |
| 1130 return (!profile_->IsOffTheRecord() || | 1125 return (!profile_->IsOffTheRecord() || |
| 1131 profile_->GetExtensionsService()->IsIncognitoEnabled(extension)); | 1126 profile_->GetExtensionsService()->IsIncognitoEnabled(extension)); |
| 1132 } | 1127 } |
| OLD | NEW |