OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/toolbar/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "chrome/browser/extensions/extension_action_manager.h" | 9 #include "chrome/browser/extensions/extension_action_manager.h" |
10 #include "chrome/browser/extensions/extension_util.h" | 10 #include "chrome/browser/extensions/extension_util.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // The maximum number of icons to show per row when in overflow mode (showing | 59 // The maximum number of icons to show per row when in overflow mode (showing |
60 // icons in the application menu). | 60 // icons in the application menu). |
61 // TODO(devlin): Compute the right number of icons to show, depending on the | 61 // TODO(devlin): Compute the right number of icons to show, depending on the |
62 // menu width. | 62 // menu width. |
63 #if defined(OS_LINUX) | 63 #if defined(OS_LINUX) |
64 const int kIconsPerMenuRow = 8; // The menu on Linux is wider. | 64 const int kIconsPerMenuRow = 8; // The menu on Linux is wider. |
65 #else | 65 #else |
66 const int kIconsPerMenuRow = 7; | 66 const int kIconsPerMenuRow = 7; |
67 #endif | 67 #endif |
68 | 68 |
69 // Returns the ExtensionAction for the given |extension|. | |
70 ExtensionAction* GetExtensionAction(const Extension& extension, | |
71 Profile* profile) { | |
72 extensions::ExtensionActionManager* action_manager = | |
73 extensions::ExtensionActionManager::Get(profile); | |
74 ExtensionAction* action = action_manager->GetBrowserAction(extension); | |
75 return action ? action : action_manager->GetPageAction(extension); | |
76 } | |
77 | |
78 // A version of MenuButton with almost empty insets to fit properly on the | 69 // A version of MenuButton with almost empty insets to fit properly on the |
79 // toolbar. | 70 // toolbar. |
80 class ChevronMenuButton : public views::MenuButton { | 71 class ChevronMenuButton : public views::MenuButton { |
81 public: | 72 public: |
82 ChevronMenuButton(views::ButtonListener* listener, | 73 ChevronMenuButton(views::ButtonListener* listener, |
83 const base::string16& text, | 74 const base::string16& text, |
84 views::MenuButtonListener* menu_button_listener, | 75 views::MenuButtonListener* menu_button_listener, |
85 bool show_menu_marker) | 76 bool show_menu_marker) |
86 : views::MenuButton(listener, | 77 : views::MenuButton(listener, |
87 text, | 78 text, |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 void BrowserActionsContainer::RefreshBrowserActionViews() { | 210 void BrowserActionsContainer::RefreshBrowserActionViews() { |
220 for (size_t i = 0; i < browser_action_views_.size(); ++i) | 211 for (size_t i = 0; i < browser_action_views_.size(); ++i) |
221 browser_action_views_[i]->UpdateState(); | 212 browser_action_views_[i]->UpdateState(); |
222 } | 213 } |
223 | 214 |
224 void BrowserActionsContainer::CreateBrowserActionViews() { | 215 void BrowserActionsContainer::CreateBrowserActionViews() { |
225 DCHECK(browser_action_views_.empty()); | 216 DCHECK(browser_action_views_.empty()); |
226 if (!model_) | 217 if (!model_) |
227 return; | 218 return; |
228 | 219 |
| 220 extensions::ExtensionActionManager* action_manager = |
| 221 extensions::ExtensionActionManager::Get(profile_); |
229 const extensions::ExtensionList& toolbar_items = model_->toolbar_items(); | 222 const extensions::ExtensionList& toolbar_items = model_->toolbar_items(); |
230 for (extensions::ExtensionList::const_iterator i(toolbar_items.begin()); | 223 for (extensions::ExtensionList::const_iterator i(toolbar_items.begin()); |
231 i != toolbar_items.end(); ++i) { | 224 i != toolbar_items.end(); ++i) { |
232 if (!ShouldDisplayBrowserAction(i->get())) | 225 if (!ShouldDisplayBrowserAction(i->get())) |
233 continue; | 226 continue; |
234 | 227 |
235 BrowserActionView* view = | 228 BrowserActionView* view = |
236 new BrowserActionView(i->get(), | 229 new BrowserActionView(i->get(), |
237 GetExtensionAction(*i->get(), profile_), | 230 action_manager->GetExtensionAction(**i), |
238 browser_, | 231 browser_, |
239 this); | 232 this); |
240 browser_action_views_.push_back(view); | 233 browser_action_views_.push_back(view); |
241 AddChildView(view); | 234 AddChildView(view); |
242 } | 235 } |
243 } | 236 } |
244 | 237 |
245 void BrowserActionsContainer::DeleteBrowserActionViews() { | 238 void BrowserActionsContainer::DeleteBrowserActionViews() { |
246 HideActivePopup(); | 239 HideActivePopup(); |
247 if (overflow_menu_) | 240 if (overflow_menu_) |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 CloseOverflowMenu(); | 798 CloseOverflowMenu(); |
806 | 799 |
807 if (!ShouldDisplayBrowserAction(extension)) | 800 if (!ShouldDisplayBrowserAction(extension)) |
808 return; | 801 return; |
809 | 802 |
810 // Add the new browser action to the vector and the view hierarchy. | 803 // Add the new browser action to the vector and the view hierarchy. |
811 if (profile_->IsOffTheRecord()) | 804 if (profile_->IsOffTheRecord()) |
812 index = model_->OriginalIndexToIncognito(index); | 805 index = model_->OriginalIndexToIncognito(index); |
813 BrowserActionView* view = | 806 BrowserActionView* view = |
814 new BrowserActionView(extension, | 807 new BrowserActionView(extension, |
815 GetExtensionAction(*extension, profile_), | 808 extensions::ExtensionActionManager::Get(profile_)-> |
| 809 GetExtensionAction(*extension), |
816 browser_, | 810 browser_, |
817 this); | 811 this); |
818 browser_action_views_.insert(browser_action_views_.begin() + index, view); | 812 browser_action_views_.insert(browser_action_views_.begin() + index, view); |
819 AddChildViewAt(view, index); | 813 AddChildViewAt(view, index); |
820 | 814 |
821 // If we are still initializing the container, don't bother animating. | 815 // If we are still initializing the container, don't bother animating. |
822 if (!model_->extensions_initialized()) | 816 if (!model_->extensions_initialized()) |
823 return; | 817 return; |
824 | 818 |
825 // Enlarge the container if it was already at maximum size and we're not in | 819 // Enlarge the container if it was already at maximum size and we're not in |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 } | 896 } |
903 | 897 |
904 void BrowserActionsContainer::ToolbarExtensionUpdated( | 898 void BrowserActionsContainer::ToolbarExtensionUpdated( |
905 const Extension* extension) { | 899 const Extension* extension) { |
906 BrowserActionView* view = GetViewForExtension(extension); | 900 BrowserActionView* view = GetViewForExtension(extension); |
907 if (view) | 901 if (view) |
908 view->UpdateState(); | 902 view->UpdateState(); |
909 } | 903 } |
910 | 904 |
911 bool BrowserActionsContainer::ShowExtensionActionPopup( | 905 bool BrowserActionsContainer::ShowExtensionActionPopup( |
912 const Extension* extension) { | 906 const Extension* extension, |
913 return ShowPopupForExtension(extension, false, false); | 907 bool grant_active_tab) { |
| 908 // Don't override another popup, and only show in the active window. |
| 909 if (popup_owner_ || !browser_->window()->IsActive()) |
| 910 return false; |
| 911 |
| 912 BrowserActionView* view = GetViewForExtension(extension); |
| 913 return view && view->view_controller()->ExecuteAction(ExtensionPopup::SHOW, |
| 914 grant_active_tab); |
914 } | 915 } |
915 | 916 |
916 void BrowserActionsContainer::ToolbarVisibleCountChanged() { | 917 void BrowserActionsContainer::ToolbarVisibleCountChanged() { |
917 int old_container_width = container_width_; | 918 int old_container_width = container_width_; |
918 SetContainerWidth(); | 919 SetContainerWidth(); |
919 if (old_container_width != container_width_) | 920 if (old_container_width != container_width_) |
920 Animate(gfx::Tween::EASE_OUT, GetIconCount()); | 921 Animate(gfx::Tween::EASE_OUT, GetIconCount()); |
921 } | 922 } |
922 | 923 |
923 void BrowserActionsContainer::ToolbarHighlightModeChanged( | 924 void BrowserActionsContainer::ToolbarHighlightModeChanged( |
924 bool is_highlighting) { | 925 bool is_highlighting) { |
925 // The visual highlighting is done in OnPaint(). It's a bit of a pain that | 926 // The visual highlighting is done in OnPaint(). It's a bit of a pain that |
926 // we delete and recreate everything here, but given everything else going on | 927 // we delete and recreate everything here, but given everything else going on |
927 // (the lack of highlight, n more extensions appearing, etc), it's not worth | 928 // (the lack of highlight, n more extensions appearing, etc), it's not worth |
928 // the extra complexity to create and insert only the new extensions. | 929 // the extra complexity to create and insert only the new extensions. |
929 DeleteBrowserActionViews(); | 930 DeleteBrowserActionViews(); |
930 CreateBrowserActionViews(); | 931 CreateBrowserActionViews(); |
931 Animate(gfx::Tween::LINEAR, browser_action_views_.size()); | 932 Animate(gfx::Tween::LINEAR, browser_action_views_.size()); |
932 } | 933 } |
933 | 934 |
| 935 Browser* BrowserActionsContainer::GetBrowser() { |
| 936 return browser_; |
| 937 } |
| 938 |
934 void BrowserActionsContainer::LoadImages() { | 939 void BrowserActionsContainer::LoadImages() { |
935 ui::ThemeProvider* tp = GetThemeProvider(); | 940 ui::ThemeProvider* tp = GetThemeProvider(); |
936 if (!tp || !chevron_) | 941 if (!tp || !chevron_) |
937 return; | 942 return; |
938 | 943 |
939 chevron_->SetImage(views::Button::STATE_NORMAL, | 944 chevron_->SetImage(views::Button::STATE_NORMAL, |
940 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); | 945 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); |
941 | 946 |
942 const int kImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT); | 947 const int kImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT); |
943 highlight_painter_.reset(views::Painter::CreateImageGridPainter(kImages)); | 948 highlight_painter_.reset(views::Painter::CreateImageGridPainter(kImages)); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1046 } | 1051 } |
1047 } | 1052 } |
1048 | 1053 |
1049 bool BrowserActionsContainer::ShouldDisplayBrowserAction( | 1054 bool BrowserActionsContainer::ShouldDisplayBrowserAction( |
1050 const Extension* extension) const { | 1055 const Extension* extension) const { |
1051 // Only display incognito-enabled extensions while in incognito mode. | 1056 // Only display incognito-enabled extensions while in incognito mode. |
1052 return !profile_->IsOffTheRecord() || | 1057 return !profile_->IsOffTheRecord() || |
1053 extensions::util::IsIncognitoEnabled(extension->id(), profile_); | 1058 extensions::util::IsIncognitoEnabled(extension->id(), profile_); |
1054 } | 1059 } |
1055 | 1060 |
1056 bool BrowserActionsContainer::ShowPopupForExtension( | |
1057 const extensions::Extension* extension, | |
1058 bool grant_tab_permissions, | |
1059 bool can_override) { | |
1060 // If the popup cannot override other views, then no other popups can be | |
1061 // showing, and it must be shown in the active widow with a visible toolbar. | |
1062 // TODO(justinlin): Remove toolbar check when http://crbug.com/308645 is | |
1063 // fixed. | |
1064 if (!can_override && | |
1065 (popup_owner_ || | |
1066 !browser_->window()->IsActive() || | |
1067 !browser_->window()->IsToolbarVisible())) { | |
1068 return false; | |
1069 } | |
1070 | |
1071 BrowserActionView* view = GetViewForExtension(extension); | |
1072 return view ? view->view_controller()->ExecuteAction( | |
1073 ExtensionPopup::SHOW, grant_tab_permissions) : false; | |
1074 } | |
1075 | |
1076 BrowserActionView* BrowserActionsContainer::GetViewForExtension( | 1061 BrowserActionView* BrowserActionsContainer::GetViewForExtension( |
1077 const Extension* extension) { | 1062 const Extension* extension) { |
1078 for (BrowserActionViews::iterator view = browser_action_views_.begin(); | 1063 for (BrowserActionViews::iterator view = browser_action_views_.begin(); |
1079 view != browser_action_views_.end(); ++view) { | 1064 view != browser_action_views_.end(); ++view) { |
1080 if ((*view)->extension() == extension) | 1065 if ((*view)->extension() == extension) |
1081 return *view; | 1066 return *view; |
1082 } | 1067 } |
1083 | 1068 |
1084 return NULL; | 1069 return NULL; |
1085 } | 1070 } |
(...skipping 13 matching lines...) Expand all Loading... |
1099 size_t absolute_model_size = | 1084 size_t absolute_model_size = |
1100 model_size == -1 ? extensions.size() : model_size; | 1085 model_size == -1 ? extensions.size() : model_size; |
1101 | 1086 |
1102 // The main container will try to show |model_size| icons, but reduce if there | 1087 // The main container will try to show |model_size| icons, but reduce if there |
1103 // aren't enough displayable icons to do so. | 1088 // aren't enough displayable icons to do so. |
1104 size_t main_displayed = std::min(displayable_icon_count, absolute_model_size); | 1089 size_t main_displayed = std::min(displayable_icon_count, absolute_model_size); |
1105 // The overflow will display the extras, if any. | 1090 // The overflow will display the extras, if any. |
1106 return in_overflow_mode() ? | 1091 return in_overflow_mode() ? |
1107 displayable_icon_count - main_displayed : main_displayed; | 1092 displayable_icon_count - main_displayed : main_displayed; |
1108 } | 1093 } |
OLD | NEW |