| 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/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 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 AddItem(ToolbarItem(extension->id(), EXTENSION_ACTION), | 326 AddItem(ToolbarItem(extension->id(), EXTENSION_ACTION), |
| 326 extensions::Manifest::IsComponentLocation(extension->location())); | 327 extensions::Manifest::IsComponentLocation(extension->location())); |
| 327 } | 328 } |
| 328 | 329 |
| 329 void ToolbarActionsModel::AddItem(const ToolbarItem& item, bool is_component) { | 330 void ToolbarActionsModel::AddItem(const ToolbarItem& item, bool is_component) { |
| 330 // We only use AddItem() once the system is initialized. | 331 // We only use AddItem() once the system is initialized. |
| 331 DCHECK(actions_initialized_); | 332 DCHECK(actions_initialized_); |
| 332 | 333 |
| 333 // See if we have a last known good position for this extension. | 334 // See if we have a last known good position for this extension. |
| 334 bool is_new_extension = | 335 bool is_new_extension = |
| 335 std::find(last_known_positions_.begin(), last_known_positions_.end(), | 336 !ContainsValue(last_known_positions_, item.id); |
| 336 item.id) == last_known_positions_.end(); | |
| 337 | 337 |
| 338 // New extensions go at the right (end) of the visible extensions. Other | 338 // New extensions go at the right (end) of the visible extensions. Other |
| 339 // extensions go at their previous position. | 339 // extensions go at their previous position. |
| 340 size_t new_index = 0; | 340 size_t new_index = 0; |
| 341 if (is_new_extension) { | 341 if (is_new_extension) { |
| 342 new_index = is_component ? 0 : visible_icon_count(); | 342 new_index = is_component ? 0 : visible_icon_count(); |
| 343 // For the last-known position, we use the index of the extension that is | 343 // For the last-known position, we use the index of the extension that is |
| 344 // just before this extension, plus one. (Note that this isn't the same | 344 // just before this extension, plus one. (Note that this isn't the same |
| 345 // as new_index + 1, because last_known_positions_ can include disabled | 345 // as new_index + 1, because last_known_positions_ can include disabled |
| 346 // extensions.) | 346 // extensions.) |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 // BrowserActionsVisible is when the redesign has been enabled. | 569 // BrowserActionsVisible is when the redesign has been enabled. |
| 570 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.ToolbarActionsVisible", | 570 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.ToolbarActionsVisible", |
| 571 visible_icon_count_ == -1 | 571 visible_icon_count_ == -1 |
| 572 ? base::HistogramBase::kSampleType_MAX | 572 ? base::HistogramBase::kSampleType_MAX |
| 573 : visible_icon_count_); | 573 : visible_icon_count_); |
| 574 } | 574 } |
| 575 } | 575 } |
| 576 } | 576 } |
| 577 | 577 |
| 578 bool ToolbarActionsModel::HasItem(const ToolbarItem& item) const { | 578 bool ToolbarActionsModel::HasItem(const ToolbarItem& item) const { |
| 579 return std::find(toolbar_items_.begin(), toolbar_items_.end(), item) != | 579 return ContainsValue(toolbar_items_, item); |
| 580 toolbar_items_.end(); | |
| 581 } | 580 } |
| 582 | 581 |
| 583 bool ToolbarActionsModel::HasComponentAction( | 582 bool ToolbarActionsModel::HasComponentAction( |
| 584 const std::string& action_id) const { | 583 const std::string& action_id) const { |
| 585 DCHECK(use_redesign_); | 584 DCHECK(use_redesign_); |
| 586 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); | 585 return HasItem(ToolbarItem(action_id, COMPONENT_ACTION)); |
| 587 } | 586 } |
| 588 | 587 |
| 589 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { | 588 void ToolbarActionsModel::AddComponentAction(const std::string& action_id) { |
| 590 DCHECK(use_redesign_); | 589 DCHECK(use_redesign_); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 void ToolbarActionsModel::OnActionToolbarPrefChange() { | 692 void ToolbarActionsModel::OnActionToolbarPrefChange() { |
| 694 // If extensions are not ready, defer to later Populate() call. | 693 // If extensions are not ready, defer to later Populate() call. |
| 695 if (!actions_initialized_) | 694 if (!actions_initialized_) |
| 696 return; | 695 return; |
| 697 | 696 |
| 698 // Recalculate |last_known_positions_| to be |pref_positions| followed by | 697 // Recalculate |last_known_positions_| to be |pref_positions| followed by |
| 699 // ones that are only in |last_known_positions_|. | 698 // ones that are only in |last_known_positions_|. |
| 700 std::vector<std::string> pref_positions = extension_prefs_->GetToolbarOrder(); | 699 std::vector<std::string> pref_positions = extension_prefs_->GetToolbarOrder(); |
| 701 size_t pref_position_size = pref_positions.size(); | 700 size_t pref_position_size = pref_positions.size(); |
| 702 for (size_t i = 0; i < last_known_positions_.size(); ++i) { | 701 for (size_t i = 0; i < last_known_positions_.size(); ++i) { |
| 703 if (std::find(pref_positions.begin(), pref_positions.end(), | 702 if (!ContainsValue(pref_positions, last_known_positions_[i])) { |
| 704 last_known_positions_[i]) == pref_positions.end()) { | |
| 705 pref_positions.push_back(last_known_positions_[i]); | 703 pref_positions.push_back(last_known_positions_[i]); |
| 706 } | 704 } |
| 707 } | 705 } |
| 708 last_known_positions_.swap(pref_positions); | 706 last_known_positions_.swap(pref_positions); |
| 709 | 707 |
| 710 // Loop over the updated list of last known positions, moving any extensions | 708 // Loop over the updated list of last known positions, moving any extensions |
| 711 // that are in the wrong place. | 709 // that are in the wrong place. |
| 712 auto desired_pos = toolbar_items_.begin(); | 710 auto desired_pos = toolbar_items_.begin(); |
| 713 for (const std::string& id : last_known_positions_) { | 711 for (const std::string& id : last_known_positions_) { |
| 714 auto current_pos = std::find_if( | 712 auto current_pos = std::find_if( |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 return true; | 809 return true; |
| 812 } | 810 } |
| 813 } | 811 } |
| 814 return false; | 812 return false; |
| 815 } | 813 } |
| 816 | 814 |
| 817 const extensions::Extension* ToolbarActionsModel::GetExtensionById( | 815 const extensions::Extension* ToolbarActionsModel::GetExtensionById( |
| 818 const std::string& id) const { | 816 const std::string& id) const { |
| 819 return extension_registry_->enabled_extensions().GetByID(id); | 817 return extension_registry_->enabled_extensions().GetByID(id); |
| 820 } | 818 } |
| OLD | NEW |