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/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/browser_actions_container.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // TODO(mpcomplete): remove this after users are upgraded to 5.0. | 120 // TODO(mpcomplete): remove this after users are upgraded to 5.0. |
121 int predefined_width = | 121 int predefined_width = |
122 profile_->GetPrefs()->GetInteger(prefs::kBrowserActionContainerWidth); | 122 profile_->GetPrefs()->GetInteger(prefs::kBrowserActionContainerWidth); |
123 if (predefined_width != 0) | 123 if (predefined_width != 0) |
124 model_->SetVisibleIconCount(WidthToIconCount(predefined_width)); | 124 model_->SetVisibleIconCount(WidthToIconCount(predefined_width)); |
125 } | 125 } |
126 if (model_ && model_->extensions_initialized()) | 126 if (model_ && model_->extensions_initialized()) |
127 SetContainerWidth(); | 127 SetContainerWidth(); |
128 } | 128 } |
129 | 129 |
130 int BrowserActionsContainer::GetCurrentTabId() const { | |
131 TabContents* tab = chrome::GetActiveTabContents(browser_); | |
132 return tab ? tab->restore_tab_helper()->session_id().id() : -1; | |
133 } | |
134 | |
135 BrowserActionView* BrowserActionsContainer::GetBrowserActionView( | 130 BrowserActionView* BrowserActionsContainer::GetBrowserActionView( |
136 ExtensionAction* action) { | 131 ExtensionAction* action) { |
137 for (BrowserActionViews::iterator i(browser_action_views_.begin()); | 132 for (BrowserActionViews::iterator i(browser_action_views_.begin()); |
138 i != browser_action_views_.end(); ++i) { | 133 i != browser_action_views_.end(); ++i) { |
139 if ((*i)->button()->browser_action() == action) | 134 if ((*i)->button()->browser_action() == action) |
140 return *i; | 135 return *i; |
141 } | 136 } |
142 return NULL; | 137 return NULL; |
143 } | 138 } |
144 | 139 |
145 void BrowserActionsContainer::RefreshBrowserActionViews() { | 140 void BrowserActionsContainer::RefreshBrowserActionViews() { |
146 for (size_t i = 0; i < browser_action_views_.size(); ++i) | 141 for (size_t i = 0; i < browser_action_views_.size(); ++i) |
147 browser_action_views_[i]->button()->UpdateState(); | 142 browser_action_views_[i]->button()->UpdateState(); |
148 } | 143 } |
149 | 144 |
150 void BrowserActionsContainer::CreateBrowserActionViews() { | 145 void BrowserActionsContainer::CreateBrowserActionViews() { |
151 DCHECK(browser_action_views_.empty()); | 146 DCHECK(browser_action_views_.empty()); |
152 if (!model_) | 147 if (!model_) |
153 return; | 148 return; |
154 | 149 |
155 for (extensions::ExtensionList::iterator i(model_->begin()); | 150 for (extensions::ExtensionList::iterator i(model_->begin()); |
156 i != model_->end(); ++i) { | 151 i != model_->end(); ++i) { |
157 if (!ShouldDisplayBrowserAction(*i)) | 152 if (!ShouldDisplayBrowserAction(*i)) |
158 continue; | 153 continue; |
159 | 154 |
160 BrowserActionView* view = new BrowserActionView(*i, this); | 155 BrowserActionView* view = new BrowserActionView(*i, browser_, this); |
161 browser_action_views_.push_back(view); | 156 browser_action_views_.push_back(view); |
162 AddChildView(view); | 157 AddChildView(view); |
163 } | 158 } |
164 } | 159 } |
165 | 160 |
166 void BrowserActionsContainer::DeleteBrowserActionViews() { | 161 void BrowserActionsContainer::DeleteBrowserActionViews() { |
167 HidePopup(); | 162 HidePopup(); |
168 STLDeleteElements(&browser_action_views_); | 163 STLDeleteElements(&browser_action_views_); |
169 } | 164 } |
170 | 165 |
171 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { | |
172 SetVisible(!browser_action_views_.empty()); | |
173 owner_view_->Layout(); | |
174 owner_view_->SchedulePaint(); | |
175 } | |
176 | |
177 size_t BrowserActionsContainer::VisibleBrowserActions() const { | 166 size_t BrowserActionsContainer::VisibleBrowserActions() const { |
178 size_t visible_actions = 0; | 167 size_t visible_actions = 0; |
179 for (size_t i = 0; i < browser_action_views_.size(); ++i) { | 168 for (size_t i = 0; i < browser_action_views_.size(); ++i) { |
180 if (browser_action_views_[i]->visible()) | 169 if (browser_action_views_[i]->visible()) |
181 ++visible_actions; | 170 ++visible_actions; |
182 } | 171 } |
183 return visible_actions; | 172 return visible_actions; |
184 } | 173 } |
185 | 174 |
186 void BrowserActionsContainer::OnBrowserActionExecuted( | |
187 BrowserActionButton* button) { | |
188 const Extension* extension = button->extension(); | |
189 GURL popup_url; | |
190 switch (model_->ExecuteBrowserAction(extension, browser_, &popup_url)) { | |
191 case ExtensionToolbarModel::ACTION_NONE: | |
192 break; | |
193 case ExtensionToolbarModel::ACTION_SHOW_POPUP: | |
194 ShowPopup(button, popup_url); | |
195 break; | |
196 } | |
197 } | |
198 | |
199 gfx::Size BrowserActionsContainer::GetPreferredSize() { | 175 gfx::Size BrowserActionsContainer::GetPreferredSize() { |
200 if (browser_action_views_.empty()) | 176 if (browser_action_views_.empty()) |
201 return gfx::Size(ToolbarView::kStandardSpacing, 0); | 177 return gfx::Size(ToolbarView::kStandardSpacing, 0); |
202 | 178 |
203 // We calculate the size of the view by taking the current width and | 179 // We calculate the size of the view by taking the current width and |
204 // subtracting resize_amount_ (the latter represents how far the user is | 180 // subtracting resize_amount_ (the latter represents how far the user is |
205 // resizing the view or, if animating the snapping, how far to animate it). | 181 // resizing the view or, if animating the snapping, how far to animate it). |
206 // But we also clamp it to a minimum size and the maximum size, so that the | 182 // But we also clamp it to a minimum size and the maximum size, so that the |
207 // container can never shrink too far or take up more space than it needs. In | 183 // container can never shrink too far or take up more space than it needs. In |
208 // other words: ContainerMinSize() < width() - resize < ClampTo(MAX). | 184 // other words: ContainerMinSize() < width() - resize < ClampTo(MAX). |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 DCHECK_EQ(popup_->GetWidget(), widget); | 446 DCHECK_EQ(popup_->GetWidget(), widget); |
471 popup_->GetWidget()->RemoveObserver(this); | 447 popup_->GetWidget()->RemoveObserver(this); |
472 popup_ = NULL; | 448 popup_ = NULL; |
473 // |popup_button_| is NULL if the extension has been removed. | 449 // |popup_button_| is NULL if the extension has been removed. |
474 if (popup_button_) { | 450 if (popup_button_) { |
475 popup_button_->SetButtonNotPushed(); | 451 popup_button_->SetButtonNotPushed(); |
476 popup_button_ = NULL; | 452 popup_button_ = NULL; |
477 } | 453 } |
478 } | 454 } |
479 | 455 |
| 456 Browser* BrowserActionsContainer::GetBrowser() const { |
| 457 return browser_; |
| 458 } |
| 459 |
| 460 int BrowserActionsContainer::GetCurrentTabId() const { |
| 461 TabContents* tab = chrome::GetActiveTabContents(browser_); |
| 462 return tab ? tab->restore_tab_helper()->session_id().id() : -1; |
| 463 } |
| 464 |
| 465 void BrowserActionsContainer::OnBrowserActionExecuted( |
| 466 BrowserActionButton* button) { |
| 467 const Extension* extension = button->extension(); |
| 468 GURL popup_url; |
| 469 if (model_->ExecuteBrowserAction(extension, browser_, &popup_url) == |
| 470 ExtensionToolbarModel::ACTION_SHOW_POPUP) |
| 471 ShowPopup(button, popup_url); |
| 472 } |
| 473 |
| 474 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { |
| 475 SetVisible(!browser_action_views_.empty()); |
| 476 owner_view_->Layout(); |
| 477 owner_view_->SchedulePaint(); |
| 478 } |
| 479 |
| 480 gfx::Size BrowserActionsContainer::GetViewContentOffset() const { |
| 481 return gfx::Size(0, ToolbarView::kVertSpacing); |
| 482 } |
| 483 |
480 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, | 484 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, |
481 size_t new_index) { | 485 size_t new_index) { |
482 ExtensionService* service = profile_->GetExtensionService(); | 486 ExtensionService* service = profile_->GetExtensionService(); |
483 if (service) { | 487 if (service) { |
484 const Extension* extension = service->GetExtensionById(extension_id, false); | 488 const Extension* extension = service->GetExtensionById(extension_id, false); |
485 model_->MoveBrowserAction(extension, new_index); | 489 model_->MoveBrowserAction(extension, new_index); |
486 SchedulePaint(); | 490 SchedulePaint(); |
487 } | 491 } |
488 } | 492 } |
489 | 493 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 CloseOverflowMenu(); | 590 CloseOverflowMenu(); |
587 | 591 |
588 if (!ShouldDisplayBrowserAction(extension)) | 592 if (!ShouldDisplayBrowserAction(extension)) |
589 return; | 593 return; |
590 | 594 |
591 size_t visible_actions = VisibleBrowserActions(); | 595 size_t visible_actions = VisibleBrowserActions(); |
592 | 596 |
593 // Add the new browser action to the vector and the view hierarchy. | 597 // Add the new browser action to the vector and the view hierarchy. |
594 if (profile_->IsOffTheRecord()) | 598 if (profile_->IsOffTheRecord()) |
595 index = model_->OriginalIndexToIncognito(index); | 599 index = model_->OriginalIndexToIncognito(index); |
596 BrowserActionView* view = new BrowserActionView(extension, this); | 600 BrowserActionView* view = new BrowserActionView(extension, browser_, this); |
597 browser_action_views_.insert(browser_action_views_.begin() + index, view); | 601 browser_action_views_.insert(browser_action_views_.begin() + index, view); |
598 AddChildViewAt(view, index); | 602 AddChildViewAt(view, index); |
599 | 603 |
600 // If we are still initializing the container, don't bother animating. | 604 // If we are still initializing the container, don't bother animating. |
601 if (!model_->extensions_initialized()) | 605 if (!model_->extensions_initialized()) |
602 return; | 606 return; |
603 | 607 |
604 // Enlarge the container if it was already at maximum size and we're not in | 608 // Enlarge the container if it was already at maximum size and we're not in |
605 // the middle of upgrading. | 609 // the middle of upgrading. |
606 if ((model_->GetVisibleIconCount() < 0) && | 610 if ((model_->GetVisibleIconCount() < 0) && |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
806 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 810 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
807 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 811 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
808 popup_ = ExtensionPopup::ShowPopup(popup_url, | 812 popup_ = ExtensionPopup::ShowPopup(popup_url, |
809 browser_, | 813 browser_, |
810 reference_view, | 814 reference_view, |
811 arrow_location); | 815 arrow_location); |
812 popup_->GetWidget()->AddObserver(this); | 816 popup_->GetWidget()->AddObserver(this); |
813 popup_button_ = button; | 817 popup_button_ = button; |
814 popup_button_->SetButtonPushed(); | 818 popup_button_->SetButtonPushed(); |
815 } | 819 } |
OLD | NEW |