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

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

Issue 1715683002: chrome: Use base's ContainsValue helper function instead of std::find (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated as per latest code Created 4 years, 8 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 (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/toolbar/toolbar_actions_model.h" 5 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/metrics/histogram_base.h" 12 #include "base/metrics/histogram_base.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h"
14 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
15 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/component_migration_helper.h" 17 #include "chrome/browser/extensions/component_migration_helper.h"
17 #include "chrome/browser/extensions/extension_action_manager.h" 18 #include "chrome/browser/extensions/extension_action_manager.h"
18 #include "chrome/browser/extensions/extension_tab_util.h" 19 #include "chrome/browser/extensions/extension_tab_util.h"
19 #include "chrome/browser/extensions/extension_util.h" 20 #include "chrome/browser/extensions/extension_util.h"
20 #include "chrome/browser/extensions/tab_helper.h" 21 #include "chrome/browser/extensions/tab_helper.h"
21 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/extensions/extension_action_view_controller.h" 24 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 AddItem(ToolbarItem(extension->id(), EXTENSION_ACTION), 315 AddItem(ToolbarItem(extension->id(), EXTENSION_ACTION),
315 extensions::Manifest::IsComponentLocation(extension->location())); 316 extensions::Manifest::IsComponentLocation(extension->location()));
316 } 317 }
317 318
318 void ToolbarActionsModel::AddItem(const ToolbarItem& item, bool is_component) { 319 void ToolbarActionsModel::AddItem(const ToolbarItem& item, bool is_component) {
319 // We only use AddItem() once the system is initialized. 320 // We only use AddItem() once the system is initialized.
320 DCHECK(actions_initialized_); 321 DCHECK(actions_initialized_);
321 322
322 // See if we have a last known good position for this extension. 323 // See if we have a last known good position for this extension.
323 bool is_new_extension = 324 bool is_new_extension =
324 std::find(last_known_positions_.begin(), last_known_positions_.end(), 325 !ContainsValue(last_known_positions_, item.id);
325 item.id) == last_known_positions_.end();
326 326
327 // New extensions go at the right (end) of the visible extensions. Other 327 // New extensions go at the right (end) of the visible extensions. Other
328 // extensions go at their previous position. 328 // extensions go at their previous position.
329 size_t new_index = 0; 329 size_t new_index = 0;
330 if (is_new_extension) { 330 if (is_new_extension) {
331 new_index = is_component ? 0 : visible_icon_count(); 331 new_index = is_component ? 0 : visible_icon_count();
332 // For the last-known position, we use the index of the extension that is 332 // For the last-known position, we use the index of the extension that is
333 // just before this extension, plus one. (Note that this isn't the same 333 // just before this extension, plus one. (Note that this isn't the same
334 // as new_index + 1, because last_known_positions_ can include disabled 334 // as new_index + 1, because last_known_positions_ can include disabled
335 // extensions.) 335 // extensions.)
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 // BrowserActionsVisible is when the redesign has been enabled. 558 // BrowserActionsVisible is when the redesign has been enabled.
559 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.ToolbarActionsVisible", 559 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.ToolbarActionsVisible",
560 visible_icon_count_ == -1 560 visible_icon_count_ == -1
561 ? base::HistogramBase::kSampleType_MAX 561 ? base::HistogramBase::kSampleType_MAX
562 : visible_icon_count_); 562 : visible_icon_count_);
563 } 563 }
564 } 564 }
565 } 565 }
566 566
567 bool ToolbarActionsModel::HasItem(const ToolbarItem& item) const { 567 bool ToolbarActionsModel::HasItem(const ToolbarItem& item) const {
568 return std::find(toolbar_items_.begin(), toolbar_items_.end(), item) != 568 return ContainsValue(toolbar_items_, item);
569 toolbar_items_.end();
570 } 569 }
571 570
572 bool ToolbarActionsModel::HasComponentAction( 571 bool ToolbarActionsModel::HasComponentAction(
573 const std::string& action_id) const { 572 const std::string& action_id) const {
574 DCHECK(use_redesign_); 573 DCHECK(use_redesign_);
575 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); 574 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION));
576 } 575 }
577 576
578 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { 577 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) {
579 DCHECK(use_redesign_); 578 DCHECK(use_redesign_);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
682 void ToolbarActionsModel::OnActionToolbarPrefChange() { 681 void ToolbarActionsModel::OnActionToolbarPrefChange() {
683 // If extensions are not ready, defer to later Populate() call. 682 // If extensions are not ready, defer to later Populate() call.
684 if (!actions_initialized_) 683 if (!actions_initialized_)
685 return; 684 return;
686 685
687 // Recalculate |last_known_positions_| to be |pref_positions| followed by 686 // Recalculate |last_known_positions_| to be |pref_positions| followed by
688 // ones that are only in |last_known_positions_|. 687 // ones that are only in |last_known_positions_|.
689 std::vector<std::string> pref_positions = extension_prefs_->GetToolbarOrder(); 688 std::vector<std::string> pref_positions = extension_prefs_->GetToolbarOrder();
690 size_t pref_position_size = pref_positions.size(); 689 size_t pref_position_size = pref_positions.size();
691 for (size_t i = 0; i < last_known_positions_.size(); ++i) { 690 for (size_t i = 0; i < last_known_positions_.size(); ++i) {
692 if (std::find(pref_positions.begin(), pref_positions.end(), 691 if (!ContainsValue(pref_positions, last_known_positions_[i])) {
693 last_known_positions_[i]) == pref_positions.end()) {
694 pref_positions.push_back(last_known_positions_[i]); 692 pref_positions.push_back(last_known_positions_[i]);
695 } 693 }
696 } 694 }
697 last_known_positions_.swap(pref_positions); 695 last_known_positions_.swap(pref_positions);
698 696
699 // Loop over the updated list of last known positions, moving any extensions 697 // Loop over the updated list of last known positions, moving any extensions
700 // that are in the wrong place. 698 // that are in the wrong place.
701 auto desired_pos = toolbar_items_.begin(); 699 auto desired_pos = toolbar_items_.begin();
702 for (const std::string& id : last_known_positions_) { 700 for (const std::string& id : last_known_positions_) {
703 auto current_pos = std::find_if( 701 auto current_pos = std::find_if(
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
799 return true; 797 return true;
800 } 798 }
801 } 799 }
802 return false; 800 return false;
803 } 801 }
804 802
805 const extensions::Extension* ToolbarActionsModel::GetExtensionById( 803 const extensions::Extension* ToolbarActionsModel::GetExtensionById(
806 const std::string& id) const { 804 const std::string& id) const {
807 return extension_registry_->enabled_extensions().GetByID(id); 805 return extension_registry_->enabled_extensions().GetByID(id);
808 } 806 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698