Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_actions_bar.cc

Issue 2606293002: Remove ScopedVector from chrome/browser/ui. (Closed)
Patch Set: one last Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/toolbar/toolbar_actions_bar.h" 5 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
6 6
7 #include <set>
7 #include <utility> 8 #include <utility>
8 9
9 #include "base/auto_reset.h" 10 #include "base/auto_reset.h"
10 #include "base/location.h" 11 #include "base/location.h"
11 #include "base/profiler/scoped_tracker.h" 12 #include "base/profiler/scoped_tracker.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "chrome/browser/extensions/extension_message_bubble_controller.h" 16 #include "chrome/browser/extensions/extension_message_bubble_controller.h"
16 #include "chrome/browser/extensions/extension_util.h" 17 #include "chrome/browser/extensions/extension_util.h"
(...skipping 30 matching lines...) Expand all
47 // Takes a reference vector |reference| of length n, where n is less than or 48 // Takes a reference vector |reference| of length n, where n is less than or
48 // equal to the length of |to_sort|, and rearranges |to_sort| so that 49 // equal to the length of |to_sort|, and rearranges |to_sort| so that
49 // |to_sort|'s first n elements match the n elements of |reference| (the order 50 // |to_sort|'s first n elements match the n elements of |reference| (the order
50 // of any remaining elements in |to_sort| is unspecified). 51 // of any remaining elements in |to_sort| is unspecified).
51 // |equal| is used to compare the elements of |to_sort| and |reference|. 52 // |equal| is used to compare the elements of |to_sort| and |reference|.
52 // This allows us to sort a vector to match another vector of two different 53 // This allows us to sort a vector to match another vector of two different
53 // types without needing to construct a more cumbersome comparator class. 54 // types without needing to construct a more cumbersome comparator class.
54 // |FunctionType| should equate to (something similar to) 55 // |FunctionType| should equate to (something similar to)
55 // bool Equal(const Type1&, const Type2&), but we can't enforce this 56 // bool Equal(const Type1&, const Type2&), but we can't enforce this
56 // because of MSVC compilation limitations. 57 // because of MSVC compilation limitations.
57 template<typename Type1, typename Type2, typename FunctionType> 58 template <typename Type1, typename Type2, typename FunctionType>
58 void SortContainer(std::vector<Type1>* to_sort, 59 void SortContainer(std::vector<std::unique_ptr<Type1>>* to_sort,
59 const std::vector<Type2>& reference, 60 const std::vector<Type2>& reference,
60 FunctionType equal) { 61 FunctionType equal) {
61 CHECK_GE(to_sort->size(), reference.size()) << 62 CHECK_GE(to_sort->size(), reference.size()) <<
62 "|to_sort| must contain all elements in |reference|."; 63 "|to_sort| must contain all elements in |reference|.";
63 if (reference.empty()) 64 if (reference.empty())
64 return; 65 return;
65 // Run through the each element and compare it to the reference. If something 66 // Run through the each element and compare it to the reference. If something
66 // is out of place, find the correct spot for it. 67 // is out of place, find the correct spot for it.
67 for (size_t i = 0; i < reference.size() - 1; ++i) { 68 for (size_t i = 0; i < reference.size() - 1; ++i) {
68 if (!equal(to_sort->at(i), reference[i])) { 69 if (!equal(to_sort->at(i).get(), reference[i])) {
69 // Find the correct index (it's guaranteed to be after our current 70 // Find the correct index (it's guaranteed to be after our current
70 // index, since everything up to this point is correct), and swap. 71 // index, since everything up to this point is correct), and swap.
71 size_t j = i + 1; 72 size_t j = i + 1;
72 while (!equal(to_sort->at(j), reference[i])) { 73 while (!equal(to_sort->at(j).get(), reference[i])) {
73 ++j; 74 ++j;
74 DCHECK_LT(j, to_sort->size()) << 75 DCHECK_LT(j, to_sort->size()) <<
75 "Item in |reference| not found in |to_sort|."; 76 "Item in |reference| not found in |to_sort|.";
76 } 77 }
77 std::swap(to_sort->at(i), to_sort->at(j)); 78 std::swap(to_sort->at(i), to_sort->at(j));
78 } 79 }
79 } 80 }
80 } 81 }
81 82
82 // How long to wait until showing an extension message bubble. 83 // How long to wait until showing an extension message bubble.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 204 }
204 205
205 size_t ToolbarActionsBar::GetIconCount() const { 206 size_t ToolbarActionsBar::GetIconCount() const {
206 if (!model_) 207 if (!model_)
207 return 0; 208 return 0;
208 209
209 int pop_out_modifier = 0; 210 int pop_out_modifier = 0;
210 // If there is a popped out action, it could affect the number of visible 211 // If there is a popped out action, it could affect the number of visible
211 // icons - but only if it wouldn't otherwise be visible. 212 // icons - but only if it wouldn't otherwise be visible.
212 if (popped_out_action_) { 213 if (popped_out_action_) {
213 size_t popped_out_index = 214 size_t popped_out_index = 0;
214 std::find(toolbar_actions_.begin(), 215 for (; popped_out_index < toolbar_actions_.size(); ++popped_out_index) {
215 toolbar_actions_.end(), 216 if (toolbar_actions_[popped_out_index].get() == popped_out_action_)
216 popped_out_action_) - toolbar_actions_.begin(); 217 break;
218 }
219
217 pop_out_modifier = popped_out_index >= model_->visible_icon_count() ? 1 : 0; 220 pop_out_modifier = popped_out_index >= model_->visible_icon_count() ? 1 : 0;
218 } 221 }
219 222
220 // We purposefully do not account for any "popped out" actions in overflow 223 // We purposefully do not account for any "popped out" actions in overflow
221 // mode. This is because the popup cannot be showing while the overflow menu 224 // mode. This is because the popup cannot be showing while the overflow menu
222 // is open, so there's no concern there. Also, if the user has a popped out 225 // is open, so there's no concern there. Also, if the user has a popped out
223 // action, and immediately opens the overflow menu, we *want* the action there 226 // action, and immediately opens the overflow menu, we *want* the action there
224 // (since it will close the popup, but do so asynchronously, and we don't 227 // (since it will close the popup, but do so asynchronously, and we don't
225 // want to "slide" the action back in. 228 // want to "slide" the action back in.
226 size_t visible_icons = in_overflow_mode() ? 229 size_t visible_icons = in_overflow_mode() ?
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 290
288 return gfx::Rect(platform_settings().item_spacing + 291 return gfx::Rect(platform_settings().item_spacing +
289 index_in_row * IconWidth(true), 292 index_in_row * IconWidth(true),
290 row_index * IconHeight(), 293 row_index * IconHeight(),
291 IconWidth(false), 294 IconWidth(false),
292 IconHeight()); 295 IconHeight());
293 } 296 }
294 297
295 std::vector<ToolbarActionViewController*> 298 std::vector<ToolbarActionViewController*>
296 ToolbarActionsBar::GetActions() const { 299 ToolbarActionsBar::GetActions() const {
297 std::vector<ToolbarActionViewController*> actions = toolbar_actions_.get(); 300 std::vector<ToolbarActionViewController*> actions;
301 for (const auto& action : toolbar_actions_)
302 actions.push_back(action.get());
298 303
299 // If there is an action that should be popped out, and it's not visible by 304 // If there is an action that should be popped out, and it's not visible by
300 // default, make it the final action in the list. 305 // default, make it the final action in the list.
301 if (popped_out_action_) { 306 if (popped_out_action_) {
302 size_t index = 307 size_t index =
303 std::find(actions.begin(), actions.end(), popped_out_action_) - 308 std::find(actions.begin(), actions.end(), popped_out_action_) -
304 actions.begin(); 309 actions.begin();
305 DCHECK_NE(actions.size(), index); 310 DCHECK_NE(actions.size(), index);
306 size_t visible = GetIconCount(); 311 size_t visible = GetIconCount();
307 if (index >= visible) { 312 if (index >= visible) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 tracked_objects::ScopedTracker tracking_profile3( 350 tracked_objects::ScopedTracker tracking_profile3(
346 FROM_HERE_WITH_EXPLICIT_FUNCTION( 351 FROM_HERE_WITH_EXPLICIT_FUNCTION(
347 "ToolbarActionsBar::CreateActions3")); 352 "ToolbarActionsBar::CreateActions3"));
348 ReorderActions(); 353 ReorderActions();
349 } 354 }
350 355
351 tracked_objects::ScopedTracker tracking_profile4( 356 tracked_objects::ScopedTracker tracking_profile4(
352 FROM_HERE_WITH_EXPLICIT_FUNCTION("ToolbarActionsBar::CreateActions4")); 357 FROM_HERE_WITH_EXPLICIT_FUNCTION("ToolbarActionsBar::CreateActions4"));
353 358
354 for (size_t i = 0; i < toolbar_actions_.size(); ++i) 359 for (size_t i = 0; i < toolbar_actions_.size(); ++i)
355 delegate_->AddViewForAction(toolbar_actions_[i], i); 360 delegate_->AddViewForAction(toolbar_actions_[i].get(), i);
356 } 361 }
357 362
358 // Once the actions are created, we should animate the changes. 363 // Once the actions are created, we should animate the changes.
359 suppress_animation_ = false; 364 suppress_animation_ = false;
360 365
361 // CreateActions() can be called multiple times, so we need to make sure we 366 // CreateActions() can be called multiple times, so we need to make sure we
362 // haven't already shown the bubble. 367 // haven't already shown the bubble.
363 // Extension bubbles can also highlight a subset of actions, so don't show the 368 // Extension bubbles can also highlight a subset of actions, so don't show the
364 // bubble if the toolbar is already highlighting a different set. 369 // bubble if the toolbar is already highlighting a different set.
365 if (should_check_extension_bubble_ && !is_highlighting()) { 370 if (should_check_extension_bubble_ && !is_highlighting()) {
(...skipping 12 matching lines...) Expand all
378 toolbar_actions_.clear(); 383 toolbar_actions_.clear();
379 } 384 }
380 385
381 void ToolbarActionsBar::Update() { 386 void ToolbarActionsBar::Update() {
382 if (toolbar_actions_.empty()) 387 if (toolbar_actions_.empty())
383 return; // Nothing to do. 388 return; // Nothing to do.
384 389
385 { 390 {
386 // Don't layout until the end. 391 // Don't layout until the end.
387 base::AutoReset<bool> layout_resetter(&suppress_layout_, true); 392 base::AutoReset<bool> layout_resetter(&suppress_layout_, true);
388 for (ToolbarActionViewController* action : toolbar_actions_) 393 for (const auto& action : toolbar_actions_)
389 action->UpdateState(); 394 action->UpdateState();
390 } 395 }
391 396
392 ReorderActions(); // Also triggers a draw. 397 ReorderActions(); // Also triggers a draw.
393 } 398 }
394 399
395 bool ToolbarActionsBar::ShowToolbarActionPopup(const std::string& action_id, 400 bool ToolbarActionsBar::ShowToolbarActionPopup(const std::string& action_id,
396 bool grant_active_tab) { 401 bool grant_active_tab) {
397 // Don't override another popup, and only show in the active window. 402 // Don't override another popup, and only show in the active window.
398 if (popup_owner() || !browser_->window()->IsActive()) 403 if (popup_owner() || !browser_->window()->IsActive())
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 481
477 void ToolbarActionsBar::OnBubbleClosed() { 482 void ToolbarActionsBar::OnBubbleClosed() {
478 is_showing_bubble_ = false; 483 is_showing_bubble_ = false;
479 } 484 }
480 485
481 bool ToolbarActionsBar::IsActionVisibleOnMainBar( 486 bool ToolbarActionsBar::IsActionVisibleOnMainBar(
482 const ToolbarActionViewController* action) const { 487 const ToolbarActionViewController* action) const {
483 if (in_overflow_mode()) 488 if (in_overflow_mode())
484 return main_bar_->IsActionVisibleOnMainBar(action); 489 return main_bar_->IsActionVisibleOnMainBar(action);
485 490
486 size_t index = std::find(toolbar_actions_.begin(), 491 if (action == popped_out_action_)
487 toolbar_actions_.end(), 492 return true;
488 action) - toolbar_actions_.begin(); 493
489 return index < GetIconCount() || action == popped_out_action_; 494 size_t visible_icon_count = std::min(toolbar_actions_.size(), GetIconCount());
495 for (size_t index = 0; index < visible_icon_count; ++index)
496 if (toolbar_actions_[index].get() == action)
497 return true;
498
499 return false;
490 } 500 }
491 501
492 void ToolbarActionsBar::PopOutAction(ToolbarActionViewController* controller, 502 void ToolbarActionsBar::PopOutAction(ToolbarActionViewController* controller,
493 bool is_sticky, 503 bool is_sticky,
494 const base::Closure& closure) { 504 const base::Closure& closure) {
495 DCHECK(!in_overflow_mode()) << "Only the main bar can pop out actions."; 505 DCHECK(!in_overflow_mode()) << "Only the main bar can pop out actions.";
496 DCHECK(!popped_out_action_) << "Only one action can be popped out at a time!"; 506 DCHECK(!popped_out_action_) << "Only one action can be popped out at a time!";
497 bool needs_redraw = !IsActionVisibleOnMainBar(controller); 507 bool needs_redraw = !IsActionVisibleOnMainBar(controller);
498 popped_out_action_ = controller; 508 popped_out_action_ = controller;
499 is_popped_out_sticky_ = is_sticky; 509 is_popped_out_sticky_ = is_sticky;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 void ToolbarActionsBar::OnToolbarActionAdded( 623 void ToolbarActionsBar::OnToolbarActionAdded(
614 const ToolbarActionsModel::ToolbarItem& item, 624 const ToolbarActionsModel::ToolbarItem& item,
615 int index) { 625 int index) {
616 CHECK(model_->actions_initialized()); 626 CHECK(model_->actions_initialized());
617 CHECK(GetActionForId(item.id) == nullptr) 627 CHECK(GetActionForId(item.id) == nullptr)
618 << "Asked to add a toolbar action view for an action that already " 628 << "Asked to add a toolbar action view for an action that already "
619 "exists"; 629 "exists";
620 630
621 toolbar_actions_.insert(toolbar_actions_.begin() + index, 631 toolbar_actions_.insert(toolbar_actions_.begin() + index,
622 model_->CreateActionForItem(browser_, this, item)); 632 model_->CreateActionForItem(browser_, this, item));
623 delegate_->AddViewForAction(toolbar_actions_[index], index); 633 delegate_->AddViewForAction(toolbar_actions_[index].get(), index);
624 634
625 // We may need to resize (e.g. to show the new icon, or the chevron). We don't 635 // We may need to resize (e.g. to show the new icon, or the chevron). We don't
626 // need to check if an extension is upgrading here, because ResizeDelegate() 636 // need to check if an extension is upgrading here, because ResizeDelegate()
627 // checks to see if the container is already the proper size, and because 637 // checks to see if the container is already the proper size, and because
628 // if the action is newly incognito enabled, even though it's a reload, it's 638 // if the action is newly incognito enabled, even though it's a reload, it's
629 // a new extension to this toolbar. 639 // a new extension to this toolbar.
630 // We suppress the chevron during animation because, if we're expanding to 640 // We suppress the chevron during animation because, if we're expanding to
631 // show a new icon, we don't want to have the chevron visible only for the 641 // show a new icon, we don't want to have the chevron visible only for the
632 // duration of the animation. 642 // duration of the animation.
633 ResizeDelegate(gfx::Tween::LINEAR, true); 643 ResizeDelegate(gfx::Tween::LINEAR, true);
634 } 644 }
635 645
636 void ToolbarActionsBar::OnToolbarActionRemoved(const std::string& action_id) { 646 void ToolbarActionsBar::OnToolbarActionRemoved(const std::string& action_id) {
637 ToolbarActions::iterator iter = toolbar_actions_.begin(); 647 ToolbarActions::iterator iter = toolbar_actions_.begin();
638 while (iter != toolbar_actions_.end() && (*iter)->GetId() != action_id) 648 while (iter != toolbar_actions_.end() && (*iter)->GetId() != action_id)
639 ++iter; 649 ++iter;
640 650
641 if (iter == toolbar_actions_.end()) 651 if (iter == toolbar_actions_.end())
642 return; 652 return;
643 653
644 // The action should outlive the UI element (which is owned by the delegate), 654 // The action should outlive the UI element (which is owned by the delegate),
645 // so we can't delete it just yet. But we should remove it from the list of 655 // so we can't delete it just yet. But we should remove it from the list of
646 // actions so that any width calculations are correct. 656 // actions so that any width calculations are correct.
647 std::unique_ptr<ToolbarActionViewController> removed_action(*iter); 657 std::unique_ptr<ToolbarActionViewController> removed_action =
648 toolbar_actions_.weak_erase(iter); 658 std::move(*iter);
659 toolbar_actions_.erase(iter);
649 delegate_->RemoveViewForAction(removed_action.get()); 660 delegate_->RemoveViewForAction(removed_action.get());
650 if (popped_out_action_ == removed_action.get()) 661 if (popped_out_action_ == removed_action.get())
651 UndoPopOut(); 662 UndoPopOut();
652 removed_action.reset(); 663 removed_action.reset();
653 664
654 // If the extension is being upgraded we don't want the bar to shrink 665 // If the extension is being upgraded we don't want the bar to shrink
655 // because the icon is just going to get re-added to the same location. 666 // because the icon is just going to get re-added to the same location.
656 // There is an exception if this is an off-the-record profile, and the 667 // There is an exception if this is an off-the-record profile, and the
657 // extension is no longer incognito-enabled. 668 // extension is no longer incognito-enabled.
658 if (!extensions::ExtensionSystem::Get(browser_->profile()) 669 if (!extensions::ExtensionSystem::Get(browser_->profile())
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 observer.OnToolbarActionsBarDidStartResize(); 730 observer.OnToolbarActionsBarDidStartResize();
720 } 731 }
721 732
722 void ToolbarActionsBar::OnToolbarHighlightModeChanged(bool is_highlighting) { 733 void ToolbarActionsBar::OnToolbarHighlightModeChanged(bool is_highlighting) {
723 if (!model_->actions_initialized()) 734 if (!model_->actions_initialized())
724 return; 735 return;
725 736
726 { 737 {
727 base::AutoReset<bool> layout_resetter(&suppress_layout_, true); 738 base::AutoReset<bool> layout_resetter(&suppress_layout_, true);
728 base::AutoReset<bool> animation_resetter(&suppress_animation_, true); 739 base::AutoReset<bool> animation_resetter(&suppress_animation_, true);
729 std::set<std::string> seen; 740 std::set<std::string> model_item_ids;
730 for (const ToolbarActionsModel::ToolbarItem item : 741 for (const auto& model_item : model_->toolbar_items()) {
731 model_->toolbar_items()) { 742 model_item_ids.insert(model_item.id);
732 auto current_pos = 743
733 std::find_if(toolbar_actions_.begin(), toolbar_actions_.end(), 744 bool found = false;
734 [&item](const ToolbarActionViewController* action) { 745 for (size_t i = 0; i < toolbar_actions_.size(); ++i) {
735 return action->GetId() == item.id; 746 if (toolbar_actions_[i]->GetId() == model_item.id) {
736 }); 747 found = true;
737 if (current_pos == toolbar_actions_.end()) { 748 break;
749 }
750 }
751
752 if (!found) {
738 toolbar_actions_.push_back( 753 toolbar_actions_.push_back(
739 model_->CreateActionForItem(browser_, this, item).release()); 754 model_->CreateActionForItem(browser_, this, model_item));
740 delegate_->AddViewForAction(toolbar_actions_.back(), 755 delegate_->AddViewForAction(toolbar_actions_.back().get(),
741 toolbar_actions_.size() - 1); 756 toolbar_actions_.size() - 1);
742 } 757 }
743 seen.insert(item.id);
744 } 758 }
745 759
746 for (ToolbarActions::iterator iter = toolbar_actions_.begin(); 760 for (auto iter = toolbar_actions_.begin();
747 iter != toolbar_actions_.end();) { 761 iter != toolbar_actions_.end();) {
748 if (seen.count((*iter)->GetId()) == 0) { 762 if (model_item_ids.count((*iter)->GetId()) == 0) {
749 delegate_->RemoveViewForAction(*iter); 763 delegate_->RemoveViewForAction(iter->get());
750 iter = toolbar_actions_.erase(iter); 764 iter = toolbar_actions_.erase(iter);
751 } else { 765 } else {
752 ++iter; 766 ++iter;
753 } 767 }
754 } 768 }
755 } 769 }
756 770
757 ReorderActions(); 771 ReorderActions();
758 } 772 }
759 773
(...skipping 20 matching lines...) Expand all
780 794
781 void ToolbarActionsBar::ReorderActions() { 795 void ToolbarActionsBar::ReorderActions() {
782 if (toolbar_actions_.empty()) 796 if (toolbar_actions_.empty())
783 return; 797 return;
784 798
785 // First, reset the order to that of the model. 799 // First, reset the order to that of the model.
786 auto compare = [](ToolbarActionViewController* const& action, 800 auto compare = [](ToolbarActionViewController* const& action,
787 const ToolbarActionsModel::ToolbarItem& item) { 801 const ToolbarActionsModel::ToolbarItem& item) {
788 return action->GetId() == item.id; 802 return action->GetId() == item.id;
789 }; 803 };
790 SortContainer(&toolbar_actions_.get(), model_->toolbar_items(), compare); 804 SortContainer(&toolbar_actions_, model_->toolbar_items(), compare);
791 805
792 // Our visible browser actions may have changed - re-Layout() and check the 806 // Our visible browser actions may have changed - re-Layout() and check the
793 // size (if we aren't suppressing the layout). 807 // size (if we aren't suppressing the layout).
794 if (!suppress_layout_) { 808 if (!suppress_layout_) {
795 ResizeDelegate(gfx::Tween::EASE_OUT, false); 809 ResizeDelegate(gfx::Tween::EASE_OUT, false);
796 delegate_->Redraw(true); 810 delegate_->Redraw(true);
797 } 811 }
798 } 812 }
799 813
800 ToolbarActionViewController* ToolbarActionsBar::GetActionForId( 814 ToolbarActionViewController* ToolbarActionsBar::GetActionForId(
801 const std::string& action_id) { 815 const std::string& action_id) {
802 for (ToolbarActionViewController* action : toolbar_actions_) { 816 for (const auto& action : toolbar_actions_) {
803 if (action->GetId() == action_id) 817 if (action->GetId() == action_id)
804 return action; 818 return action.get();
805 } 819 }
806 return nullptr; 820 return nullptr;
807 } 821 }
808 822
809 content::WebContents* ToolbarActionsBar::GetCurrentWebContents() { 823 content::WebContents* ToolbarActionsBar::GetCurrentWebContents() {
810 return browser_->tab_strip_model()->GetActiveWebContents(); 824 return browser_->tab_strip_model()->GetActiveWebContents();
811 } 825 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698