| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 void BrowserActionsContainer::GetAccessibleState( | 356 void BrowserActionsContainer::GetAccessibleState( |
| 381 ui::AccessibleViewState* state) { | 357 ui::AccessibleViewState* state) { |
| 382 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 358 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
| 383 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS); | 359 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_EXTENSIONS); |
| 384 } | 360 } |
| 385 | 361 |
| 386 void BrowserActionsContainer::OnMenuButtonClicked(views::View* source, | 362 void BrowserActionsContainer::OnMenuButtonClicked(views::View* source, |
| 387 const gfx::Point& point) { | 363 const gfx::Point& point) { |
| 388 if (source == chevron_) { | 364 if (source == chevron_) { |
| 389 overflow_menu_ = new BrowserActionOverflowMenuController( | 365 overflow_menu_ = new BrowserActionOverflowMenuController( |
| 390 this, chevron_, browser_action_views_, VisibleBrowserActions()); | 366 this, browser_, chevron_, browser_action_views_, |
| 367 VisibleBrowserActions()); |
| 391 overflow_menu_->set_observer(this); | 368 overflow_menu_->set_observer(this); |
| 392 overflow_menu_->RunMenu(GetWidget(), false); | 369 overflow_menu_->RunMenu(GetWidget(), false); |
| 393 } | 370 } |
| 394 } | 371 } |
| 395 | 372 |
| 396 void BrowserActionsContainer::WriteDragDataForView(View* sender, | 373 void BrowserActionsContainer::WriteDragDataForView(View* sender, |
| 397 const gfx::Point& press_pt, | 374 const gfx::Point& press_pt, |
| 398 OSExchangeData* data) { | 375 OSExchangeData* data) { |
| 399 DCHECK(data); | 376 DCHECK(data); |
| 400 | 377 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 DCHECK_EQ(popup_->GetWidget(), widget); | 447 DCHECK_EQ(popup_->GetWidget(), widget); |
| 471 popup_->GetWidget()->RemoveObserver(this); | 448 popup_->GetWidget()->RemoveObserver(this); |
| 472 popup_ = NULL; | 449 popup_ = NULL; |
| 473 // |popup_button_| is NULL if the extension has been removed. | 450 // |popup_button_| is NULL if the extension has been removed. |
| 474 if (popup_button_) { | 451 if (popup_button_) { |
| 475 popup_button_->SetButtonNotPushed(); | 452 popup_button_->SetButtonNotPushed(); |
| 476 popup_button_ = NULL; | 453 popup_button_ = NULL; |
| 477 } | 454 } |
| 478 } | 455 } |
| 479 | 456 |
| 457 int BrowserActionsContainer::GetCurrentTabId() const { |
| 458 TabContents* tab = chrome::GetActiveTabContents(browser_); |
| 459 return tab ? tab->restore_tab_helper()->session_id().id() : -1; |
| 460 } |
| 461 |
| 462 void BrowserActionsContainer::OnBrowserActionExecuted( |
| 463 BrowserActionButton* button) { |
| 464 const Extension* extension = button->extension(); |
| 465 GURL popup_url; |
| 466 if (model_->ExecuteBrowserAction(extension, browser_, &popup_url) == |
| 467 ExtensionToolbarModel::ACTION_SHOW_POPUP) |
| 468 ShowPopup(button, popup_url); |
| 469 } |
| 470 |
| 471 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { |
| 472 SetVisible(!browser_action_views_.empty()); |
| 473 owner_view_->Layout(); |
| 474 owner_view_->SchedulePaint(); |
| 475 } |
| 476 |
| 477 gfx::Size BrowserActionsContainer::GetViewContentOffset() const { |
| 478 return gfx::Size(0, ToolbarView::kVertSpacing); |
| 479 } |
| 480 |
| 480 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, | 481 void BrowserActionsContainer::MoveBrowserAction(const std::string& extension_id, |
| 481 size_t new_index) { | 482 size_t new_index) { |
| 482 ExtensionService* service = profile_->GetExtensionService(); | 483 ExtensionService* service = profile_->GetExtensionService(); |
| 483 if (service) { | 484 if (service) { |
| 484 const Extension* extension = service->GetExtensionById(extension_id, false); | 485 const Extension* extension = service->GetExtensionById(extension_id, false); |
| 485 model_->MoveBrowserAction(extension, new_index); | 486 model_->MoveBrowserAction(extension, new_index); |
| 486 SchedulePaint(); | 487 SchedulePaint(); |
| 487 } | 488 } |
| 488 } | 489 } |
| 489 | 490 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 CloseOverflowMenu(); | 587 CloseOverflowMenu(); |
| 587 | 588 |
| 588 if (!ShouldDisplayBrowserAction(extension)) | 589 if (!ShouldDisplayBrowserAction(extension)) |
| 589 return; | 590 return; |
| 590 | 591 |
| 591 size_t visible_actions = VisibleBrowserActions(); | 592 size_t visible_actions = VisibleBrowserActions(); |
| 592 | 593 |
| 593 // Add the new browser action to the vector and the view hierarchy. | 594 // Add the new browser action to the vector and the view hierarchy. |
| 594 if (profile_->IsOffTheRecord()) | 595 if (profile_->IsOffTheRecord()) |
| 595 index = model_->OriginalIndexToIncognito(index); | 596 index = model_->OriginalIndexToIncognito(index); |
| 596 BrowserActionView* view = new BrowserActionView(extension, this); | 597 BrowserActionView* view = new BrowserActionView(extension, browser_, this); |
| 597 browser_action_views_.insert(browser_action_views_.begin() + index, view); | 598 browser_action_views_.insert(browser_action_views_.begin() + index, view); |
| 598 AddChildViewAt(view, index); | 599 AddChildViewAt(view, index); |
| 599 | 600 |
| 600 // If we are still initializing the container, don't bother animating. | 601 // If we are still initializing the container, don't bother animating. |
| 601 if (!model_->extensions_initialized()) | 602 if (!model_->extensions_initialized()) |
| 602 return; | 603 return; |
| 603 | 604 |
| 604 // Enlarge the container if it was already at maximum size and we're not in | 605 // Enlarge the container if it was already at maximum size and we're not in |
| 605 // the middle of upgrading. | 606 // the middle of upgrading. |
| 606 if ((model_->GetVisibleIconCount() < 0) && | 607 if ((model_->GetVisibleIconCount() < 0) && |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 FROM_HERE, | 699 FROM_HERE, |
| 699 base::Bind(&BrowserActionsContainer::ShowDropFolder, | 700 base::Bind(&BrowserActionsContainer::ShowDropFolder, |
| 700 show_menu_task_factory_.GetWeakPtr()), | 701 show_menu_task_factory_.GetWeakPtr()), |
| 701 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay())); | 702 base::TimeDelta::FromMilliseconds(views::GetMenuShowDelay())); |
| 702 } | 703 } |
| 703 | 704 |
| 704 void BrowserActionsContainer::ShowDropFolder() { | 705 void BrowserActionsContainer::ShowDropFolder() { |
| 705 DCHECK(!overflow_menu_); | 706 DCHECK(!overflow_menu_); |
| 706 SetDropIndicator(-1); | 707 SetDropIndicator(-1); |
| 707 overflow_menu_ = new BrowserActionOverflowMenuController( | 708 overflow_menu_ = new BrowserActionOverflowMenuController( |
| 708 this, chevron_, browser_action_views_, VisibleBrowserActions()); | 709 this, browser_, chevron_, browser_action_views_, VisibleBrowserActions()); |
| 709 overflow_menu_->set_observer(this); | 710 overflow_menu_->set_observer(this); |
| 710 overflow_menu_->RunMenu(GetWidget(), true); | 711 overflow_menu_->RunMenu(GetWidget(), true); |
| 711 } | 712 } |
| 712 | 713 |
| 713 void BrowserActionsContainer::SetDropIndicator(int x_pos) { | 714 void BrowserActionsContainer::SetDropIndicator(int x_pos) { |
| 714 if (drop_indicator_position_ != x_pos) { | 715 if (drop_indicator_position_ != x_pos) { |
| 715 drop_indicator_position_ = x_pos; | 716 drop_indicator_position_ = x_pos; |
| 716 SchedulePaint(); | 717 SchedulePaint(); |
| 717 } | 718 } |
| 718 } | 719 } |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? | 807 views::BubbleBorder::ArrowLocation arrow_location = base::i18n::IsRTL() ? |
| 807 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; | 808 views::BubbleBorder::TOP_LEFT : views::BubbleBorder::TOP_RIGHT; |
| 808 popup_ = ExtensionPopup::ShowPopup(popup_url, | 809 popup_ = ExtensionPopup::ShowPopup(popup_url, |
| 809 browser_, | 810 browser_, |
| 810 reference_view, | 811 reference_view, |
| 811 arrow_location); | 812 arrow_location); |
| 812 popup_->GetWidget()->AddObserver(this); | 813 popup_->GetWidget()->AddObserver(this); |
| 813 popup_button_ = button; | 814 popup_button_ = button; |
| 814 popup_button_->SetButtonPushed(); | 815 popup_button_->SetButtonPushed(); |
| 815 } | 816 } |
| OLD | NEW |