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

Side by Side Diff: chrome/browser/extensions/extension_toolbar_model.cc

Issue 661493004: Add infrastructure for Chrome Actions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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/extensions/extension_toolbar_model.h" 5 #include "chrome/browser/extensions/extension_toolbar_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 } 73 }
74 74
75 void ExtensionToolbarModel::AddObserver(Observer* observer) { 75 void ExtensionToolbarModel::AddObserver(Observer* observer) {
76 observers_.AddObserver(observer); 76 observers_.AddObserver(observer);
77 } 77 }
78 78
79 void ExtensionToolbarModel::RemoveObserver(Observer* observer) { 79 void ExtensionToolbarModel::RemoveObserver(Observer* observer) {
80 observers_.RemoveObserver(observer); 80 observers_.RemoveObserver(observer);
81 } 81 }
82 82
83 void ExtensionToolbarModel::MoveExtensionIcon(const Extension* extension, 83 void ExtensionToolbarModel::MoveExtensionIcon(const std::string& id,
84 size_t index) { 84 size_t index) {
85 ExtensionList::iterator pos = std::find(toolbar_items_.begin(), 85 ExtensionList::iterator pos = toolbar_items_.begin();
86 toolbar_items_.end(), extension); 86 while (pos != toolbar_items_.end() && (*pos)->id() != id)
87 ++pos;
87 if (pos == toolbar_items_.end()) { 88 if (pos == toolbar_items_.end()) {
88 NOTREACHED(); 89 NOTREACHED();
89 return; 90 return;
90 } 91 }
92 scoped_refptr<const Extension> extension = *pos;
91 toolbar_items_.erase(pos); 93 toolbar_items_.erase(pos);
92 94
93 ExtensionIdList::iterator pos_id = std::find(last_known_positions_.begin(), 95 ExtensionIdList::iterator pos_id = std::find(last_known_positions_.begin(),
94 last_known_positions_.end(), 96 last_known_positions_.end(),
95 extension->id()); 97 id);
96 if (pos_id != last_known_positions_.end()) 98 if (pos_id != last_known_positions_.end())
97 last_known_positions_.erase(pos_id); 99 last_known_positions_.erase(pos_id);
98 100
99 if (index < toolbar_items_.size()) { 101 if (index < toolbar_items_.size()) {
100 // If the index is not at the end, find the item currently at |index|, and 102 // If the index is not at the end, find the item currently at |index|, and
101 // insert |extension| before it in both |toolbar_items_| and 103 // insert |extension| before it in both |toolbar_items_| and
102 // |last_known_positions_|. 104 // |last_known_positions_|.
103 ExtensionList::iterator iter = toolbar_items_.begin() + index; 105 ExtensionList::iterator iter = toolbar_items_.begin() + index;
104 last_known_positions_.insert(std::find(last_known_positions_.begin(), 106 last_known_positions_.insert(std::find(last_known_positions_.begin(),
105 last_known_positions_.end(), 107 last_known_positions_.end(),
106 (*iter)->id()), 108 (*iter)->id()),
107 extension->id()); 109 id);
108 toolbar_items_.insert(iter, extension); 110 toolbar_items_.insert(iter, extension);
109 } else { 111 } else {
110 // Otherwise, put |extension| at the end. 112 // Otherwise, put |extension| at the end.
111 DCHECK_EQ(toolbar_items_.size(), index); 113 DCHECK_EQ(toolbar_items_.size(), index);
112 index = toolbar_items_.size(); 114 index = toolbar_items_.size();
113 toolbar_items_.push_back(make_scoped_refptr(extension)); 115 toolbar_items_.push_back(extension);
114 last_known_positions_.push_back(extension->id()); 116 last_known_positions_.push_back(id);
115 } 117 }
116 118
117 FOR_EACH_OBSERVER( 119 FOR_EACH_OBSERVER(
118 Observer, observers_, ToolbarExtensionMoved(extension, index)); 120 Observer, observers_, ToolbarExtensionMoved(extension.get(), index));
119 MaybeUpdateVisibilityPref(extension, index); 121 MaybeUpdateVisibilityPref(extension.get(), index);
120 UpdatePrefs(); 122 UpdatePrefs();
121 } 123 }
122 124
123 void ExtensionToolbarModel::SetVisibleIconCount(int count) { 125 void ExtensionToolbarModel::SetVisibleIconCount(int count) {
124 visible_icon_count_ = 126 visible_icon_count_ =
125 count == static_cast<int>(toolbar_items_.size()) ? -1 : count; 127 count == static_cast<int>(toolbar_items_.size()) ? -1 : count;
126 128
127 // Only set the prefs if we're not in highlight mode and the profile is not 129 // Only set the prefs if we're not in highlight mode and the profile is not
128 // incognito. Highlight mode is designed to be a transitory state, and should 130 // incognito. Highlight mode is designed to be a transitory state, and should
129 // not persist across browser restarts (though it may be re-entered), and we 131 // not persist across browser restarts (though it may be re-entered), and we
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 } else { 223 } else {
222 // If we're hiding one, we must be showing at least one. 224 // If we're hiding one, we must be showing at least one.
223 DCHECK_NE(visible_icon_count_, 0); 225 DCHECK_NE(visible_icon_count_, 0);
224 // Shrink the bar by one and move the extension to the beginning of the 226 // Shrink the bar by one and move the extension to the beginning of the
225 // overflow menu. 227 // overflow menu.
226 new_size = visible_icon_count_ == -1 ? 228 new_size = visible_icon_count_ == -1 ?
227 toolbar_items_.size() - 1 : visible_icon_count_ - 1; 229 toolbar_items_.size() - 1 : visible_icon_count_ - 1;
228 new_index = new_size; 230 new_index = new_size;
229 } 231 }
230 SetVisibleIconCount(new_size); 232 SetVisibleIconCount(new_size);
231 MoveExtensionIcon(extension, new_index); 233 MoveExtensionIcon(extension->id(), new_index);
232 } else { // Don't include all extensions. 234 } else { // Don't include all extensions.
233 if (visible) 235 if (visible)
234 AddExtension(extension); 236 AddExtension(extension);
235 else 237 else
236 RemoveExtension(extension); 238 RemoveExtension(extension);
237 } 239 }
238 } 240 }
239 241
240 void ExtensionToolbarModel::OnReady() { 242 void ExtensionToolbarModel::OnReady() {
241 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); 243 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 if (visible_icon_count_ == -1) 597 if (visible_icon_count_ == -1)
596 return; // May have been set to max by SetVisibleIconCount. 598 return; // May have been set to max by SetVisibleIconCount.
597 599
598 // Guillotine's Delight: Move an orange noble to the front of the line. 600 // Guillotine's Delight: Move an orange noble to the front of the line.
599 for (ExtensionIdList::const_iterator it = extension_ids.begin(); 601 for (ExtensionIdList::const_iterator it = extension_ids.begin();
600 it != extension_ids.end(); ++it) { 602 it != extension_ids.end(); ++it) {
601 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); 603 for (ExtensionList::const_iterator extension = toolbar_items_.begin();
602 extension != toolbar_items_.end(); ++extension) { 604 extension != toolbar_items_.end(); ++extension) {
603 if ((*extension)->id() == (*it)) { 605 if ((*extension)->id() == (*it)) {
604 if (extension - toolbar_items_.begin() >= visible_icon_count_) 606 if (extension - toolbar_items_.begin() >= visible_icon_count_)
605 MoveExtensionIcon(extension->get(), 0); 607 MoveExtensionIcon((*extension)->id(), 0);
606 break; 608 break;
607 } 609 }
608 } 610 }
609 } 611 }
610 } 612 }
611 613
612 bool ExtensionToolbarModel::HighlightExtensions( 614 bool ExtensionToolbarModel::HighlightExtensions(
613 const ExtensionIdList& extension_ids) { 615 const ExtensionIdList& extension_ids) {
614 highlighted_items_.clear(); 616 highlighted_items_.clear();
615 617
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 if (is_highlighting_) { 651 if (is_highlighting_) {
650 highlighted_items_.clear(); 652 highlighted_items_.clear();
651 is_highlighting_ = false; 653 is_highlighting_ = false;
652 if (old_visible_icon_count_ != visible_icon_count_) 654 if (old_visible_icon_count_ != visible_icon_count_)
653 SetVisibleIconCount(old_visible_icon_count_); 655 SetVisibleIconCount(old_visible_icon_count_);
654 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); 656 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false));
655 } 657 }
656 } 658 }
657 659
658 } // namespace extensions 660 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_toolbar_model.h ('k') | chrome/browser/extensions/extension_toolbar_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698