| 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" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 iter->id), | 122 iter->id), |
| 123 id); | 123 id); |
| 124 toolbar_items_.insert(iter, action); | 124 toolbar_items_.insert(iter, action); |
| 125 } else { | 125 } else { |
| 126 // Otherwise, put |action| and |id| at the end. | 126 // Otherwise, put |action| and |id| at the end. |
| 127 DCHECK_EQ(toolbar_items_.size(), index); | 127 DCHECK_EQ(toolbar_items_.size(), index); |
| 128 toolbar_items_.push_back(action); | 128 toolbar_items_.push_back(action); |
| 129 last_known_positions_.push_back(id); | 129 last_known_positions_.push_back(id); |
| 130 } | 130 } |
| 131 | 131 |
| 132 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarActionMoved(id, index)); | 132 for (Observer& observer : observers_) |
| 133 observer.OnToolbarActionMoved(id, index); |
| 133 UpdatePrefs(); | 134 UpdatePrefs(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 void ToolbarActionsModel::SetVisibleIconCount(size_t count) { | 137 void ToolbarActionsModel::SetVisibleIconCount(size_t count) { |
| 137 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count; | 138 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count; |
| 138 | 139 |
| 139 // Only set the prefs if we're not in highlight mode and the profile is not | 140 // Only set the prefs if we're not in highlight mode and the profile is not |
| 140 // incognito. Highlight mode is designed to be a transitory state, and should | 141 // incognito. Highlight mode is designed to be a transitory state, and should |
| 141 // not persist across browser restarts (though it may be re-entered), and we | 142 // not persist across browser restarts (though it may be re-entered), and we |
| 142 // don't store anything in incognito. | 143 // don't store anything in incognito. |
| 143 if (!is_highlighting() && !profile_->IsOffTheRecord()) { | 144 if (!is_highlighting() && !profile_->IsOffTheRecord()) { |
| 144 prefs_->SetInteger(extensions::pref_names::kToolbarSize, | 145 prefs_->SetInteger(extensions::pref_names::kToolbarSize, |
| 145 visible_icon_count_); | 146 visible_icon_count_); |
| 146 } | 147 } |
| 147 | 148 |
| 148 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarVisibleCountChanged()); | 149 for (Observer& observer : observers_) |
| 150 observer.OnToolbarVisibleCountChanged(); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void ToolbarActionsModel::OnExtensionActionUpdated( | 153 void ToolbarActionsModel::OnExtensionActionUpdated( |
| 152 ExtensionAction* extension_action, | 154 ExtensionAction* extension_action, |
| 153 content::WebContents* web_contents, | 155 content::WebContents* web_contents, |
| 154 content::BrowserContext* browser_context) { | 156 content::BrowserContext* browser_context) { |
| 155 // Notify observers if the extension exists and is in the model. | 157 // Notify observers if the extension exists and is in the model. |
| 156 if (HasItem( | 158 if (HasItem( |
| 157 ToolbarItem(extension_action->extension_id(), EXTENSION_ACTION))) { | 159 ToolbarItem(extension_action->extension_id(), EXTENSION_ACTION))) { |
| 158 FOR_EACH_OBSERVER(Observer, observers_, | 160 for (Observer& observer : observers_) |
| 159 OnToolbarActionUpdated(extension_action->extension_id())); | 161 observer.OnToolbarActionUpdated(extension_action->extension_id()); |
| 160 } | 162 } |
| 161 } | 163 } |
| 162 | 164 |
| 163 ScopedVector<ToolbarActionViewController> ToolbarActionsModel::CreateActions( | 165 ScopedVector<ToolbarActionViewController> ToolbarActionsModel::CreateActions( |
| 164 Browser* browser, | 166 Browser* browser, |
| 165 ToolbarActionsBar* bar) { | 167 ToolbarActionsBar* bar) { |
| 166 DCHECK(browser); | 168 DCHECK(browser); |
| 167 DCHECK(bar); | 169 DCHECK(bar); |
| 168 ScopedVector<ToolbarActionViewController> action_list; | 170 ScopedVector<ToolbarActionViewController> action_list; |
| 169 | 171 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 269 |
| 268 void ToolbarActionsModel::OnReady() { | 270 void ToolbarActionsModel::OnReady() { |
| 269 InitializeActionList(); | 271 InitializeActionList(); |
| 270 // Wait until the extension system is ready before observing any further | 272 // Wait until the extension system is ready before observing any further |
| 271 // changes so that the toolbar buttons can be shown in their stable ordering | 273 // changes so that the toolbar buttons can be shown in their stable ordering |
| 272 // taken from prefs. | 274 // taken from prefs. |
| 273 extension_registry_observer_.Add(extension_registry_); | 275 extension_registry_observer_.Add(extension_registry_); |
| 274 extension_action_observer_.Add(extension_action_api_); | 276 extension_action_observer_.Add(extension_action_api_); |
| 275 | 277 |
| 276 actions_initialized_ = true; | 278 actions_initialized_ = true; |
| 277 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); | 279 for (Observer& observer : observers_) |
| 280 observer.OnToolbarModelInitialized(); |
| 278 | 281 |
| 279 if (use_redesign_) { | 282 if (use_redesign_) { |
| 280 // Handle component action migrations. We must make sure that observers are | 283 // Handle component action migrations. We must make sure that observers are |
| 281 // notified of initialization first, so that the associated widgets are | 284 // notified of initialization first, so that the associated widgets are |
| 282 // created. | 285 // created. |
| 283 ComponentToolbarActionsFactory::GetInstance()->HandleComponentMigrations( | 286 ComponentToolbarActionsFactory::GetInstance()->HandleComponentMigrations( |
| 284 component_migration_helper_.get(), profile_); | 287 component_migration_helper_.get(), profile_); |
| 285 } | 288 } |
| 286 } | 289 } |
| 287 | 290 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } else { | 368 } else { |
| 366 new_index = FindNewPositionFromLastKnownGood(item); | 369 new_index = FindNewPositionFromLastKnownGood(item); |
| 367 } | 370 } |
| 368 | 371 |
| 369 toolbar_items_.insert(toolbar_items_.begin() + new_index, item); | 372 toolbar_items_.insert(toolbar_items_.begin() + new_index, item); |
| 370 | 373 |
| 371 // If we're currently highlighting, then even though we add a browser action | 374 // If we're currently highlighting, then even though we add a browser action |
| 372 // to the full list (|toolbar_items_|, there won't be another *visible* | 375 // to the full list (|toolbar_items_|, there won't be another *visible* |
| 373 // browser action, which was what the observers care about. | 376 // browser action, which was what the observers care about. |
| 374 if (!is_highlighting()) { | 377 if (!is_highlighting()) { |
| 375 FOR_EACH_OBSERVER(Observer, observers_, | 378 for (Observer& observer : observers_) |
| 376 OnToolbarActionAdded(item, new_index)); | 379 observer.OnToolbarActionAdded(item, new_index); |
| 377 | 380 |
| 378 int visible_count_delta = 0; | 381 int visible_count_delta = 0; |
| 379 if (is_new_extension && !all_icons_visible()) { | 382 if (is_new_extension && !all_icons_visible()) { |
| 380 // If this is a new extension (and not all extensions are visible), we | 383 // If this is a new extension (and not all extensions are visible), we |
| 381 // expand the toolbar out so that the new one can be seen. | 384 // expand the toolbar out so that the new one can be seen. |
| 382 visible_count_delta = 1; | 385 visible_count_delta = 1; |
| 383 } else if (profile_->IsOffTheRecord()) { | 386 } else if (profile_->IsOffTheRecord()) { |
| 384 // If this is an incognito profile, we also have to check to make sure the | 387 // If this is an incognito profile, we also have to check to make sure the |
| 385 // overflow matches the main bar's status. | 388 // overflow matches the main bar's status. |
| 386 ToolbarActionsModel* main_model = | 389 ToolbarActionsModel* main_model = |
| (...skipping 29 matching lines...) Expand all Loading... |
| 416 SetVisibleIconCount(toolbar_items_.size() - 1); | 419 SetVisibleIconCount(toolbar_items_.size() - 1); |
| 417 | 420 |
| 418 toolbar_items_.erase(pos); | 421 toolbar_items_.erase(pos); |
| 419 | 422 |
| 420 // If we're in highlight mode, we also have to remove the action from | 423 // If we're in highlight mode, we also have to remove the action from |
| 421 // the highlighted list. | 424 // the highlighted list. |
| 422 if (is_highlighting()) { | 425 if (is_highlighting()) { |
| 423 pos = std::find(highlighted_items_.begin(), highlighted_items_.end(), item); | 426 pos = std::find(highlighted_items_.begin(), highlighted_items_.end(), item); |
| 424 if (pos != highlighted_items_.end()) { | 427 if (pos != highlighted_items_.end()) { |
| 425 highlighted_items_.erase(pos); | 428 highlighted_items_.erase(pos); |
| 426 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarActionRemoved(item.id)); | 429 for (Observer& observer : observers_) |
| 430 observer.OnToolbarActionRemoved(item.id); |
| 427 // If the highlighted list is now empty, we stop highlighting. | 431 // If the highlighted list is now empty, we stop highlighting. |
| 428 if (highlighted_items_.empty()) | 432 if (highlighted_items_.empty()) |
| 429 StopHighlighting(); | 433 StopHighlighting(); |
| 430 } | 434 } |
| 431 } else { | 435 } else { |
| 432 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarActionRemoved(item.id)); | 436 for (Observer& observer : observers_) |
| 437 observer.OnToolbarActionRemoved(item.id); |
| 433 } | 438 } |
| 434 | 439 |
| 435 UpdatePrefs(); | 440 UpdatePrefs(); |
| 436 } | 441 } |
| 437 | 442 |
| 438 std::unique_ptr<extensions::ExtensionMessageBubbleController> | 443 std::unique_ptr<extensions::ExtensionMessageBubbleController> |
| 439 ToolbarActionsModel::GetExtensionMessageBubbleController(Browser* browser) { | 444 ToolbarActionsModel::GetExtensionMessageBubbleController(Browser* browser) { |
| 440 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller; | 445 std::unique_ptr<extensions::ExtensionMessageBubbleController> controller; |
| 441 if (has_active_bubble()) | 446 if (has_active_bubble()) |
| 442 return controller; | 447 return controller; |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 if (current_pos == toolbar_items_.end()) | 764 if (current_pos == toolbar_items_.end()) |
| 760 continue; | 765 continue; |
| 761 | 766 |
| 762 if (current_pos != desired_pos) { | 767 if (current_pos != desired_pos) { |
| 763 if (current_pos < desired_pos) | 768 if (current_pos < desired_pos) |
| 764 std::rotate(current_pos, current_pos + 1, desired_pos + 1); | 769 std::rotate(current_pos, current_pos + 1, desired_pos + 1); |
| 765 else | 770 else |
| 766 std::rotate(desired_pos, current_pos, current_pos + 1); | 771 std::rotate(desired_pos, current_pos, current_pos + 1); |
| 767 // Notify the observers to keep them up to date, unless we're highlighting | 772 // Notify the observers to keep them up to date, unless we're highlighting |
| 768 // (in which case we're deliberately only showing a subset of actions). | 773 // (in which case we're deliberately only showing a subset of actions). |
| 769 if (!is_highlighting()) | 774 if (!is_highlighting()) { |
| 770 FOR_EACH_OBSERVER( | 775 for (Observer& observer : observers_) { |
| 771 Observer, observers_, | 776 observer.OnToolbarActionMoved(id, |
| 772 OnToolbarActionMoved(id, desired_pos - toolbar_items_.begin())); | 777 desired_pos - toolbar_items_.begin()); |
| 778 } |
| 779 } |
| 773 } | 780 } |
| 774 ++desired_pos; | 781 ++desired_pos; |
| 775 } | 782 } |
| 776 | 783 |
| 777 if (last_known_positions_.size() > pref_position_size) { | 784 if (last_known_positions_.size() > pref_position_size) { |
| 778 // Need to update pref because we have extra icons. But can't call | 785 // Need to update pref because we have extra icons. But can't call |
| 779 // UpdatePrefs() directly within observation closure. | 786 // UpdatePrefs() directly within observation closure. |
| 780 base::ThreadTaskRunnerHandle::Get()->PostTask( | 787 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 781 FROM_HERE, base::Bind(&ToolbarActionsModel::UpdatePrefs, | 788 FROM_HERE, base::Bind(&ToolbarActionsModel::UpdatePrefs, |
| 782 weak_ptr_factory_.GetWeakPtr())); | 789 weak_ptr_factory_.GetWeakPtr())); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 793 highlighted_items_.push_back(item); | 800 highlighted_items_.push_back(item); |
| 794 } | 801 } |
| 795 } | 802 } |
| 796 | 803 |
| 797 // If we have any items in |highlighted_items_|, then we entered highlighting | 804 // If we have any items in |highlighted_items_|, then we entered highlighting |
| 798 // mode. | 805 // mode. |
| 799 if (highlighted_items_.size()) { | 806 if (highlighted_items_.size()) { |
| 800 // It's important that is_highlighting_ is changed immediately before the | 807 // It's important that is_highlighting_ is changed immediately before the |
| 801 // observers are notified since it changes the result of toolbar_items(). | 808 // observers are notified since it changes the result of toolbar_items(). |
| 802 highlight_type_ = highlight_type; | 809 highlight_type_ = highlight_type; |
| 803 FOR_EACH_OBSERVER(Observer, observers_, | 810 for (Observer& observer : observers_) |
| 804 OnToolbarHighlightModeChanged(true)); | 811 observer.OnToolbarHighlightModeChanged(true); |
| 805 | 812 |
| 806 // We set the visible icon count after the highlight mode change because | 813 // We set the visible icon count after the highlight mode change because |
| 807 // the UI actions are created/destroyed during highlight, and doing that | 814 // the UI actions are created/destroyed during highlight, and doing that |
| 808 // prior to changing the size allows us to still have smooth animations. | 815 // prior to changing the size allows us to still have smooth animations. |
| 809 if (visible_icon_count() < ids.size()) | 816 if (visible_icon_count() < ids.size()) |
| 810 SetVisibleIconCount(ids.size()); | 817 SetVisibleIconCount(ids.size()); |
| 811 | 818 |
| 812 return true; | 819 return true; |
| 813 } | 820 } |
| 814 | 821 |
| 815 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if | 822 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if |
| 816 // we were otherwise in it). | 823 // we were otherwise in it). |
| 817 if (is_highlighting()) | 824 if (is_highlighting()) |
| 818 StopHighlighting(); | 825 StopHighlighting(); |
| 819 return false; | 826 return false; |
| 820 } | 827 } |
| 821 | 828 |
| 822 void ToolbarActionsModel::StopHighlighting() { | 829 void ToolbarActionsModel::StopHighlighting() { |
| 823 if (is_highlighting()) { | 830 if (is_highlighting()) { |
| 824 // It's important that is_highlighting_ is changed immediately before the | 831 // It's important that is_highlighting_ is changed immediately before the |
| 825 // observers are notified since it changes the result of toolbar_items(). | 832 // observers are notified since it changes the result of toolbar_items(). |
| 826 highlight_type_ = HIGHLIGHT_NONE; | 833 highlight_type_ = HIGHLIGHT_NONE; |
| 827 FOR_EACH_OBSERVER(Observer, observers_, | 834 for (Observer& observer : observers_) |
| 828 OnToolbarHighlightModeChanged(false)); | 835 observer.OnToolbarHighlightModeChanged(false); |
| 829 | 836 |
| 830 // For the same reason, we don't clear highlighted_items_ until after the | 837 // For the same reason, we don't clear highlighted_items_ until after the |
| 831 // mode changed. | 838 // mode changed. |
| 832 highlighted_items_.clear(); | 839 highlighted_items_.clear(); |
| 833 | 840 |
| 834 // We set the visible icon count after the highlight mode change because | 841 // We set the visible icon count after the highlight mode change because |
| 835 // the UI actions are created/destroyed during highlight, and doing that | 842 // the UI actions are created/destroyed during highlight, and doing that |
| 836 // prior to changing the size allows us to still have smooth animations. | 843 // prior to changing the size allows us to still have smooth animations. |
| 837 int saved_icon_count = | 844 int saved_icon_count = |
| 838 prefs_->GetInteger(extensions::pref_names::kToolbarSize); | 845 prefs_->GetInteger(extensions::pref_names::kToolbarSize); |
| 839 if (saved_icon_count != visible_icon_count_) | 846 if (saved_icon_count != visible_icon_count_) |
| 840 SetVisibleIconCount(saved_icon_count); | 847 SetVisibleIconCount(saved_icon_count); |
| 841 } | 848 } |
| 842 } | 849 } |
| 843 | 850 |
| 844 const extensions::Extension* ToolbarActionsModel::GetExtensionById( | 851 const extensions::Extension* ToolbarActionsModel::GetExtensionById( |
| 845 const std::string& id) const { | 852 const std::string& id) const { |
| 846 return extension_registry_->enabled_extensions().GetByID(id); | 853 return extension_registry_->enabled_extensions().GetByID(id); |
| 847 } | 854 } |
| OLD | NEW |