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

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

Issue 1241063003: Support Component Actions in the toolbar. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Switch GetComponentActionId to unix_hacker_style. Created 5 years, 4 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/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/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/thread_task_runner_handle.h" 15 #include "base/thread_task_runner_handle.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
18 #include "chrome/browser/extensions/extension_action_manager.h" 17 #include "chrome/browser/extensions/extension_action_manager.h"
19 #include "chrome/browser/extensions/extension_tab_util.h" 18 #include "chrome/browser/extensions/extension_tab_util.h"
20 #include "chrome/browser/extensions/extension_toolbar_model_factory.h"
21 #include "chrome/browser/extensions/extension_util.h" 19 #include "chrome/browser/extensions/extension_util.h"
22 #include "chrome/browser/extensions/tab_helper.h" 20 #include "chrome/browser/extensions/tab_helper.h"
23 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/extensions/extension_action_view_controller.h"
25 #include "chrome/browser/ui/extensions/extension_toolbar_icon_surfacing_bubble_d elegate.h" 24 #include "chrome/browser/ui/extensions/extension_toolbar_icon_surfacing_bubble_d elegate.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h" 25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/browser/ui/toolbar/component_toolbar_actions_factory.h"
27 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
28 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
29 #include "chrome/browser/ui/toolbar/toolbar_actions_model_factory.h"
27 #include "content/public/browser/notification_details.h" 30 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_source.h" 31 #include "content/public/browser/notification_source.h"
29 #include "content/public/browser/web_contents.h" 32 #include "content/public/browser/web_contents.h"
30 #include "extensions/browser/extension_prefs.h"
31 #include "extensions/browser/extension_registry.h" 33 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/extension_system.h" 34 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/pref_names.h" 35 #include "extensions/browser/pref_names.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/common/extension_set.h" 36 #include "extensions/common/extension_set.h"
36 #include "extensions/common/feature_switch.h" 37 #include "extensions/common/feature_switch.h"
37 #include "extensions/common/manifest_constants.h" 38 #include "extensions/common/manifest_constants.h"
38 #include "extensions/common/one_shot_event.h" 39 #include "extensions/common/one_shot_event.h"
39 40
40 namespace extensions { 41 ToolbarActionsModel::ToolbarActionsModel(
41 42 Profile* profile,
42 ExtensionToolbarModel::ExtensionToolbarModel(Profile* profile, 43 extensions::ExtensionPrefs* extension_prefs)
43 ExtensionPrefs* extension_prefs)
44 : profile_(profile), 44 : profile_(profile),
45 extension_prefs_(extension_prefs), 45 extension_prefs_(extension_prefs),
46 prefs_(profile_->GetPrefs()), 46 prefs_(profile_->GetPrefs()),
47 extension_action_api_(ExtensionActionAPI::Get(profile_)), 47 extension_action_api_(extensions::ExtensionActionAPI::Get(profile_)),
48 extensions_initialized_(false), 48 extension_registry_(extensions::ExtensionRegistry::Get(profile_)),
49 include_all_extensions_(FeatureSwitch::extension_action_redesign() 49 actions_initialized_(false),
50 ->IsEnabled()), 50 use_redesign_(extensions::FeatureSwitch::extension_action_redesign()
51 ->IsEnabled()),
51 highlight_type_(HIGHLIGHT_NONE), 52 highlight_type_(HIGHLIGHT_NONE),
52 extension_action_observer_(this), 53 extension_action_observer_(this),
53 extension_registry_observer_(this), 54 extension_registry_observer_(this),
54 weak_ptr_factory_(this) { 55 weak_ptr_factory_(this) {
55 ExtensionSystem::Get(profile_)->ready().Post( 56 extensions::ExtensionSystem::Get(profile_)->ready().Post(
56 FROM_HERE, 57 FROM_HERE, base::Bind(&ToolbarActionsModel::OnReady,
57 base::Bind(&ExtensionToolbarModel::OnReady, 58 weak_ptr_factory_.GetWeakPtr()));
58 weak_ptr_factory_.GetWeakPtr())); 59 visible_icon_count_ =
59 visible_icon_count_ = prefs_->GetInteger(pref_names::kToolbarSize); 60 prefs_->GetInteger(extensions::pref_names::kToolbarSize);
60 61
61 // We only care about watching the prefs if not in incognito mode. 62 // We only care about watching the prefs if not in incognito mode.
62 if (!profile_->IsOffTheRecord()) { 63 if (!profile_->IsOffTheRecord()) {
63 pref_change_registrar_.Init(prefs_); 64 pref_change_registrar_.Init(prefs_);
64 pref_change_callback_ = 65 pref_change_callback_ =
65 base::Bind(&ExtensionToolbarModel::OnExtensionToolbarPrefChange, 66 base::Bind(&ToolbarActionsModel::OnActionToolbarPrefChange,
66 base::Unretained(this)); 67 base::Unretained(this));
67 pref_change_registrar_.Add(pref_names::kToolbar, pref_change_callback_); 68 pref_change_registrar_.Add(extensions::pref_names::kToolbar,
69 pref_change_callback_);
68 } 70 }
69 } 71 }
70 72
71 ExtensionToolbarModel::~ExtensionToolbarModel() { 73 ToolbarActionsModel::~ToolbarActionsModel() {}
74
75 // static
76 ToolbarActionsModel* ToolbarActionsModel::Get(Profile* profile) {
77 return ToolbarActionsModelFactory::GetForProfile(profile);
72 } 78 }
73 79
74 // static 80 void ToolbarActionsModel::AddObserver(Observer* observer) {
75 ExtensionToolbarModel* ExtensionToolbarModel::Get(Profile* profile) {
76 return ExtensionToolbarModelFactory::GetForProfile(profile);
77 }
78
79 void ExtensionToolbarModel::AddObserver(Observer* observer) {
80 observers_.AddObserver(observer); 81 observers_.AddObserver(observer);
81 } 82 }
82 83
83 void ExtensionToolbarModel::RemoveObserver(Observer* observer) { 84 void ToolbarActionsModel::RemoveObserver(Observer* observer) {
84 observers_.RemoveObserver(observer); 85 observers_.RemoveObserver(observer);
85 } 86 }
86 87
87 void ExtensionToolbarModel::MoveExtensionIcon(const std::string& id, 88 void ToolbarActionsModel::MoveActionIcon(const std::string& id, size_t index) {
88 size_t index) { 89 std::vector<ToolbarItem>::iterator pos = toolbar_items_.begin();
89 ExtensionList::iterator pos = toolbar_items_.begin(); 90 while (pos != toolbar_items_.end() && (*pos).id != id)
90 while (pos != toolbar_items_.end() && (*pos)->id() != id)
91 ++pos; 91 ++pos;
92 if (pos == toolbar_items_.end()) { 92 if (pos == toolbar_items_.end()) {
93 NOTREACHED(); 93 NOTREACHED();
94 return; 94 return;
95 } 95 }
96 scoped_refptr<const Extension> extension = *pos; 96
97 ToolbarItem action = *pos;
97 toolbar_items_.erase(pos); 98 toolbar_items_.erase(pos);
98 99
99 ExtensionIdList::iterator pos_id = std::find(last_known_positions_.begin(), 100 std::vector<std::string>::iterator pos_id =
100 last_known_positions_.end(), 101 std::find(last_known_positions_.begin(), last_known_positions_.end(), id);
101 id);
102 if (pos_id != last_known_positions_.end()) 102 if (pos_id != last_known_positions_.end())
103 last_known_positions_.erase(pos_id); 103 last_known_positions_.erase(pos_id);
104 104
105 if (index < toolbar_items_.size()) { 105 if (index < toolbar_items_.size()) {
106 // If the index is not at the end, find the item currently at |index|, and 106 // If the index is not at the end, find the item currently at |index|, and
107 // insert |extension| before it in both |toolbar_items_| and 107 // insert |action| before it in |toolbar_items_| and |action|'s id in
108 // |last_known_positions_|. 108 // |last_known_positions_|.
109 ExtensionList::iterator iter = toolbar_items_.begin() + index; 109 std::vector<ToolbarItem>::iterator iter = toolbar_items_.begin() + index;
110 last_known_positions_.insert(std::find(last_known_positions_.begin(), 110 last_known_positions_.insert(
111 last_known_positions_.end(), 111 std::find(last_known_positions_.begin(), last_known_positions_.end(),
112 (*iter)->id()), 112 iter->id),
113 id); 113 id);
114 toolbar_items_.insert(iter, extension); 114 toolbar_items_.insert(iter, action);
115 } else { 115 } else {
116 // Otherwise, put |extension| at the end. 116 // Otherwise, put |action| and |id| at the end.
117 DCHECK_EQ(toolbar_items_.size(), index); 117 DCHECK_EQ(toolbar_items_.size(), index);
118 index = toolbar_items_.size(); 118 toolbar_items_.push_back(action);
119 toolbar_items_.push_back(extension);
120 last_known_positions_.push_back(id); 119 last_known_positions_.push_back(id);
121 } 120 }
122 121
123 FOR_EACH_OBSERVER(Observer, observers_, 122 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarActionMoved(id, index));
124 OnToolbarExtensionMoved(extension.get(), index)); 123 MaybeUpdateVisibilityPref(action, index);
125 MaybeUpdateVisibilityPref(extension.get(), index);
126 UpdatePrefs(); 124 UpdatePrefs();
127 } 125 }
128 126
129 void ExtensionToolbarModel::SetVisibleIconCount(size_t count) { 127 void ToolbarActionsModel::SetVisibleIconCount(size_t count) {
130 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count; 128 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count;
131 129
132 // Only set the prefs if we're not in highlight mode and the profile is not 130 // Only set the prefs if we're not in highlight mode and the profile is not
133 // incognito. Highlight mode is designed to be a transitory state, and should 131 // incognito. Highlight mode is designed to be a transitory state, and should
134 // not persist across browser restarts (though it may be re-entered), and we 132 // not persist across browser restarts (though it may be re-entered), and we
135 // don't store anything in incognito. 133 // don't store anything in incognito.
136 if (!is_highlighting() && !profile_->IsOffTheRecord()) { 134 if (!is_highlighting() && !profile_->IsOffTheRecord()) {
137 // Additionally, if we are using the new toolbar, any icons which are in the 135 // Additionally, if we are using the new toolbar, any icons which are in the
138 // overflow menu are considered "hidden". But it so happens that the times 136 // overflow menu are considered "hidden". But it so happens that the times
139 // we are likely to call SetVisibleIconCount() are also those when we are 137 // we are likely to call SetVisibleIconCount() are also those when we are
140 // in flux. So wait for things to cool down before setting the prefs. 138 // in flux. So wait for things to cool down before setting the prefs.
141 base::ThreadTaskRunnerHandle::Get()->PostTask( 139 base::ThreadTaskRunnerHandle::Get()->PostTask(
142 FROM_HERE, 140 FROM_HERE, base::Bind(&ToolbarActionsModel::MaybeUpdateVisibilityPrefs,
143 base::Bind(&ExtensionToolbarModel::MaybeUpdateVisibilityPrefs, 141 weak_ptr_factory_.GetWeakPtr()));
144 weak_ptr_factory_.GetWeakPtr())); 142 prefs_->SetInteger(extensions::pref_names::kToolbarSize,
145 prefs_->SetInteger(pref_names::kToolbarSize, visible_icon_count_); 143 visible_icon_count_);
146 } 144 }
147 145
148 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarVisibleCountChanged()); 146 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarVisibleCountChanged());
149 } 147 }
150 148
151 void ExtensionToolbarModel::OnExtensionActionUpdated( 149 void ToolbarActionsModel::OnExtensionActionUpdated(
152 ExtensionAction* extension_action, 150 ExtensionAction* extension_action,
153 content::WebContents* web_contents, 151 content::WebContents* web_contents,
154 content::BrowserContext* browser_context) { 152 content::BrowserContext* browser_context) {
155 const Extension* extension =
156 ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID(
157 extension_action->extension_id());
158 // Notify observers if the extension exists and is in the model. 153 // Notify observers if the extension exists and is in the model.
159 if (std::find(toolbar_items_.begin(), toolbar_items_.end(), extension) != 154 if (std::find(toolbar_items_.begin(), toolbar_items_.end(),
160 toolbar_items_.end()) { 155 ToolbarItem(extension_action->extension_id(),
156 EXTENSION_ACTION)) != toolbar_items_.end()) {
161 FOR_EACH_OBSERVER(Observer, observers_, 157 FOR_EACH_OBSERVER(Observer, observers_,
162 OnToolbarExtensionUpdated(extension)); 158 OnToolbarActionUpdated(extension_action->extension_id()));
163 } 159 }
164 } 160 }
165 161
166 void ExtensionToolbarModel::OnExtensionActionVisibilityChanged( 162 ScopedVector<ToolbarActionViewController> ToolbarActionsModel::CreateActions(
163 Browser* browser,
164 ToolbarActionsBar* bar) {
165 DCHECK(browser);
166 DCHECK(bar);
167 ScopedVector<ToolbarActionViewController> action_list;
168
169 // Get the component action list.
170 ScopedVector<ToolbarActionViewController> component_actions =
171 ComponentToolbarActionsFactory::GetInstance()->GetComponentToolbarActions(
172 browser);
173
174 extensions::ExtensionActionManager* action_manager =
175 extensions::ExtensionActionManager::Get(profile_);
176
177 // toolbar_items() might not equate to toolbar_items_ in the case where a
178 // subset are highlighted.
179 std::vector<ToolbarItem> items = toolbar_items();
180 for (const ToolbarItem& action : items) {
181 if (action.type == EXTENSION_ACTION) {
182 // Get the extension.
183 const extensions::Extension* extension = GetExtensionById(action.id);
184 DCHECK(extension);
185
186 // Create and add an ExtensionActionViewController for the extension.
187 action_list.push_back(new ExtensionActionViewController(
188 extension, browser, action_manager->GetExtensionAction(*extension),
189 bar));
190 } else if (action.type == COMPONENT_ACTION) {
191 DCHECK(use_redesign_);
192 // Find the corresponding action to |action|.
193 for (auto component_action : component_actions) {
194 if (component_action->GetId() == action.id) {
195 action_list.push_back(component_action);
196 break;
197 }
198 }
199 }
200 }
201
202 // We've moved ownership of the subset of the component actions that we
203 // kept track of via toolbar_items() from |component_actions| to
204 // |action_list|, so we don't need to keep track of these.
205 component_actions.weak_clear();
206
207 return action_list.Pass();
208 }
209
210 void ToolbarActionsModel::OnExtensionActionVisibilityChanged(
167 const std::string& extension_id, 211 const std::string& extension_id,
168 bool is_now_visible) { 212 bool is_now_visible) {
169 const Extension* extension =
170 ExtensionRegistry::Get(profile_)->GetExtensionById(
171 extension_id, ExtensionRegistry::EVERYTHING);
172
173 // Hiding works differently with the new and old toolbars. 213 // Hiding works differently with the new and old toolbars.
174 if (include_all_extensions_) { 214 if (use_redesign_) {
175 // It's possible that we haven't added this extension yet, if its 215 // It's possible that we haven't added this action yet, if its
176 // visibility was adjusted in the course of its initialization. 216 // visibility was adjusted in the course of its initialization.
177 if (std::find(toolbar_items_.begin(), toolbar_items_.end(), extension) == 217 if (std::find(toolbar_items_.begin(), toolbar_items_.end(),
178 toolbar_items_.end()) 218 ToolbarItem(extension_id, EXTENSION_ACTION)) ==
219 toolbar_items_.end())
179 return; 220 return;
180 221
181 int new_size = 0; 222 int new_size = 0;
182 int new_index = 0; 223 int new_index = 0;
183 if (is_now_visible) { 224 if (is_now_visible) {
184 // If this action used to be hidden, we can't possibly be showing all. 225 // If this action used to be hidden, we can't possibly be showing all.
185 DCHECK_LT(visible_icon_count(), toolbar_items_.size()); 226 DCHECK_LT(visible_icon_count(), toolbar_items_.size());
186 // Grow the bar by one and move the extension to the end of the visibles. 227 // Grow the bar by one and move the action to the end of the visibles.
187 new_size = visible_icon_count() + 1; 228 new_size = visible_icon_count() + 1;
188 new_index = new_size - 1; 229 new_index = new_size - 1;
189 } else { 230 } else {
190 // If we're hiding one, we must be showing at least one. 231 // If we're hiding one, we must be showing at least one.
191 DCHECK_GE(visible_icon_count(), 0u); 232 DCHECK_GE(visible_icon_count(), 0u);
192 // Shrink the bar by one and move the extension to the beginning of the 233 // Shrink the bar by one and move the action to the beginning of the
193 // overflow menu. 234 // overflow menu.
194 new_size = visible_icon_count() - 1; 235 new_size = visible_icon_count() - 1;
195 new_index = new_size; 236 new_index = new_size;
196 } 237 }
197 SetVisibleIconCount(new_size); 238 SetVisibleIconCount(new_size);
198 MoveExtensionIcon(extension->id(), new_index); 239 MoveActionIcon(extension_id, new_index);
199 } else { // Don't include all extensions. 240 } else { // Don't include all extensions.
241 const extensions::Extension* extension = GetExtensionById(extension_id);
200 if (is_now_visible) 242 if (is_now_visible)
201 AddExtension(extension); 243 AddExtension(extension);
202 else 244 else
203 RemoveExtension(extension); 245 RemoveExtension(extension);
204 } 246 }
205 } 247 }
206 248
207 void ExtensionToolbarModel::OnExtensionLoaded( 249 void ToolbarActionsModel::OnExtensionLoaded(
208 content::BrowserContext* browser_context, 250 content::BrowserContext* browser_context,
209 const Extension* extension) { 251 const extensions::Extension* extension) {
210 // We don't want to add the same extension twice. It may have already been 252 // We don't want to add the same extension twice. It may have already been
211 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user 253 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user
212 // hides the browser action and then disables and enables the extension. 254 // hides the browser action and then disables and enables the extension.
213 for (size_t i = 0; i < toolbar_items_.size(); i++) { 255 if (std::find(toolbar_items_.begin(), toolbar_items_.end(),
214 if (toolbar_items_[i].get() == extension) 256 ToolbarItem(extension->id(), EXTENSION_ACTION)) !=
215 return; 257 toolbar_items_.end())
216 } 258 return;
217 259
218 AddExtension(extension); 260 AddExtension(extension);
219 } 261 }
220 262
221 void ExtensionToolbarModel::OnExtensionUnloaded( 263 void ToolbarActionsModel::OnExtensionUnloaded(
222 content::BrowserContext* browser_context, 264 content::BrowserContext* browser_context,
223 const Extension* extension, 265 const extensions::Extension* extension,
224 UnloadedExtensionInfo::Reason reason) { 266 extensions::UnloadedExtensionInfo::Reason reason) {
225 RemoveExtension(extension); 267 RemoveExtension(extension);
226 } 268 }
227 269
228 void ExtensionToolbarModel::OnExtensionUninstalled( 270 void ToolbarActionsModel::OnExtensionUninstalled(
229 content::BrowserContext* browser_context, 271 content::BrowserContext* browser_context,
230 const Extension* extension, 272 const extensions::Extension* extension,
231 extensions::UninstallReason reason) { 273 extensions::UninstallReason reason) {
232 // Remove the extension id from the ordered list, if it exists (the extension 274 // Remove the extension id from the ordered list, if it exists (the extension
233 // might not be represented in the list because it might not have an icon). 275 // might not be represented in the list because it might not have an icon).
234 ExtensionIdList::iterator pos = 276 std::vector<std::string>::iterator pos =
235 std::find(last_known_positions_.begin(), 277 std::find(last_known_positions_.begin(), last_known_positions_.end(),
236 last_known_positions_.end(), extension->id()); 278 extension->id());
237 279
238 if (pos != last_known_positions_.end()) { 280 if (pos != last_known_positions_.end()) {
239 last_known_positions_.erase(pos); 281 last_known_positions_.erase(pos);
240 UpdatePrefs(); 282 UpdatePrefs();
241 } 283 }
242 } 284 }
243 285
244 void ExtensionToolbarModel::OnReady() { 286 void ToolbarActionsModel::OnReady() {
245 ExtensionRegistry* registry = ExtensionRegistry::Get(profile_); 287 InitializeActionList();
246 InitializeExtensionList();
247 // Wait until the extension system is ready before observing any further 288 // Wait until the extension system is ready before observing any further
248 // changes so that the toolbar buttons can be shown in their stable ordering 289 // changes so that the toolbar buttons can be shown in their stable ordering
249 // taken from prefs. 290 // taken from prefs.
250 extension_registry_observer_.Add(registry); 291 extension_registry_observer_.Add(extension_registry_);
251 extension_action_observer_.Add(extension_action_api_); 292 extension_action_observer_.Add(extension_action_api_);
252 293
253 if (ExtensionToolbarIconSurfacingBubbleDelegate::ShouldShowForProfile( 294 if (ExtensionToolbarIconSurfacingBubbleDelegate::ShouldShowForProfile(
254 profile_)) { 295 profile_)) {
255 ExtensionIdList ids; 296 std::vector<std::string> ids;
256 for (const auto& extension : toolbar_items_) 297 for (const ToolbarItem& action : toolbar_items_)
257 ids.push_back(extension->id()); 298 ids.push_back(action.id);
258 HighlightExtensions(ids, HIGHLIGHT_INFO); 299 HighlightActions(ids, HIGHLIGHT_INFO);
259 } 300 }
260 301
261 extensions_initialized_ = true; 302 actions_initialized_ = true;
262 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); 303 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized());
263 } 304 }
264 305
265 size_t ExtensionToolbarModel::FindNewPositionFromLastKnownGood( 306 size_t ToolbarActionsModel::FindNewPositionFromLastKnownGood(
266 const Extension* extension) { 307 const ToolbarItem& action) {
267 // See if we have last known good position for this extension. 308 // See if we have last known good position for this action.
268 size_t new_index = 0; 309 size_t new_index = 0;
269 // Loop through the ID list of known positions, to count the number of visible 310 // Loop through the ID list of known positions, to count the number of
270 // extension icons preceding |extension|. 311 // visible action icons preceding |action|'s id.
271 for (ExtensionIdList::const_iterator iter_id = last_known_positions_.begin(); 312 for (const std::string& last_pos_id : last_known_positions_) {
272 iter_id < last_known_positions_.end(); ++iter_id) { 313 if (last_pos_id == action.id)
273 if ((*iter_id) == extension->id())
274 return new_index; // We've found the right position. 314 return new_index; // We've found the right position.
275 // Found an id, need to see if it is visible. 315 // Found an action, need to see if it is visible.
276 for (ExtensionList::const_iterator iter_ext = toolbar_items_.begin(); 316 for (const ToolbarItem& item : toolbar_items_) {
277 iter_ext < toolbar_items_.end(); ++iter_ext) { 317 if (item.id == last_pos_id) {
278 if ((*iter_ext)->id() == (*iter_id)) {
279 // This extension is visible, update the index value. 318 // This extension is visible, update the index value.
280 ++new_index; 319 ++new_index;
281 break; 320 break;
282 } 321 }
283 } 322 }
284 } 323 }
285 324
286 // Position not found. 325 // Position not found.
287 return toolbar_items_.size(); 326 return toolbar_items_.size();
288 } 327 }
289 328
290 bool ExtensionToolbarModel::ShouldAddExtension(const Extension* extension) { 329 bool ToolbarActionsModel::ShouldAddExtension(
330 const extensions::Extension* extension) {
291 // In incognito mode, don't add any extensions that aren't incognito-enabled. 331 // In incognito mode, don't add any extensions that aren't incognito-enabled.
292 if (profile_->IsOffTheRecord() && 332 if (profile_->IsOffTheRecord() &&
293 !util::IsIncognitoEnabled(extension->id(), profile_)) 333 !extensions::util::IsIncognitoEnabled(extension->id(), profile_))
294 return false; 334 return false;
295 335
296 ExtensionActionManager* action_manager = 336 extensions::ExtensionActionManager* action_manager =
297 ExtensionActionManager::Get(profile_); 337 extensions::ExtensionActionManager::Get(profile_);
298 if (include_all_extensions_) { 338 if (use_redesign_) {
299 // In this case, we don't care about the browser action visibility, because 339 // In this case, we don't care about the browser action visibility, because
300 // we want to show each extension regardless. 340 // we want to show each extension regardless.
301 // TODO(devlin): Extension actions which are not visible should be moved to
302 // the overflow menu by default.
303 return action_manager->GetExtensionAction(*extension) != NULL; 341 return action_manager->GetExtensionAction(*extension) != NULL;
304 } 342 }
305 343
306 return action_manager->GetBrowserAction(*extension) && 344 return action_manager->GetBrowserAction(*extension) &&
307 extension_action_api_->GetBrowserActionVisibility(extension->id()); 345 extension_action_api_->GetBrowserActionVisibility(extension->id());
308 } 346 }
309 347
310 void ExtensionToolbarModel::AddExtension(const Extension* extension) { 348 void ToolbarActionsModel::AddExtension(const extensions::Extension* extension) {
311 // We only use AddExtension() once the system is initialized. 349 // We only use AddExtension() once the system is initialized.
312 DCHECK(extensions_initialized_); 350 DCHECK(actions_initialized_);
313 if (!ShouldAddExtension(extension)) 351 if (!ShouldAddExtension(extension))
314 return; 352 return;
315 353
316 // See if we have a last known good position for this extension. 354 // See if we have a last known good position for this extension.
317 bool is_new_extension = 355 bool is_new_extension =
318 std::find(last_known_positions_.begin(), 356 std::find(last_known_positions_.begin(), last_known_positions_.end(),
319 last_known_positions_.end(),
320 extension->id()) == last_known_positions_.end(); 357 extension->id()) == last_known_positions_.end();
321 358
322 // New extensions go at the right (end) of the visible extensions. Other 359 // New extensions go at the right (end) of the visible extensions. Other
323 // extensions go at their previous position. 360 // extensions go at their previous position.
324 size_t new_index = 0; 361 size_t new_index = 0;
325 if (is_new_extension) { 362 if (is_new_extension) {
326 new_index = Manifest::IsComponentLocation(extension->location()) ? 363 new_index = extensions::Manifest::IsComponentLocation(extension->location())
327 0 : visible_icon_count(); 364 ? 0
365 : visible_icon_count();
328 // For the last-known position, we use the index of the extension that is 366 // For the last-known position, we use the index of the extension that is
329 // just before this extension, plus one. (Note that this isn't the same 367 // just before this extension, plus one. (Note that this isn't the same
330 // as new_index + 1, because last_known_positions_ can include disabled 368 // as new_index + 1, because last_known_positions_ can include disabled
331 // extensions.) 369 // extensions.)
332 int new_last_known_index = 370 int new_last_known_index =
333 new_index == 0 ? 0 : 371 new_index == 0 ? 0 : std::find(last_known_positions_.begin(),
334 std::find(last_known_positions_.begin(), 372 last_known_positions_.end(),
335 last_known_positions_.end(), 373 toolbar_items_[new_index - 1].id) -
336 toolbar_items_[new_index - 1]->id()) - 374 last_known_positions_.begin() + 1;
337 last_known_positions_.begin() + 1;
338 // In theory, the extension before this one should always 375 // In theory, the extension before this one should always
339 // be in last known positions, but if something funny happened with prefs, 376 // be in last known positions, but if something funny happened with prefs,
340 // make sure we handle it. 377 // make sure we handle it.
341 // TODO(devlin): Track down these cases so we can CHECK this. 378 // TODO(devlin): Track down these cases so we can CHECK this.
342 new_last_known_index = 379 new_last_known_index =
343 std::min<int>(new_last_known_index, last_known_positions_.size()); 380 std::min<int>(new_last_known_index, last_known_positions_.size());
344 last_known_positions_.insert( 381 last_known_positions_.insert(
345 last_known_positions_.begin() + new_last_known_index, extension->id()); 382 last_known_positions_.begin() + new_last_known_index, extension->id());
346 UpdatePrefs(); 383 UpdatePrefs();
347 } else { 384 } else {
348 new_index = FindNewPositionFromLastKnownGood(extension); 385 new_index = FindNewPositionFromLastKnownGood(
386 ToolbarItem(extension->id(), EXTENSION_ACTION));
349 } 387 }
350 388
351 toolbar_items_.insert(toolbar_items_.begin() + new_index, extension); 389 toolbar_items_.insert(toolbar_items_.begin() + new_index,
390 ToolbarItem(extension->id(), EXTENSION_ACTION));
352 391
353 // If we're currently highlighting, then even though we add a browser action 392 // If we're currently highlighting, then even though we add a browser action
354 // to the full list (|toolbar_items_|, there won't be another *visible* 393 // to the full list (|toolbar_items_|, there won't be another *visible*
355 // browser action, which was what the observers care about. 394 // browser action, which was what the observers care about.
356 if (!is_highlighting()) { 395 if (!is_highlighting()) {
357 FOR_EACH_OBSERVER(Observer, observers_, 396 FOR_EACH_OBSERVER(Observer, observers_,
358 OnToolbarExtensionAdded(extension, new_index)); 397 OnToolbarActionAdded(extension->id(), new_index));
359 398
360 int visible_count_delta = 0; 399 int visible_count_delta = 0;
361 if (is_new_extension && !all_icons_visible()) { 400 if (is_new_extension && !all_icons_visible()) {
362 // If this is a new extension (and not all extensions are visible), we 401 // If this is a new extension (and not all extensions are visible), we
363 // expand the toolbar out so that the new one can be seen. 402 // expand the toolbar out so that the new one can be seen.
364 visible_count_delta = 1; 403 visible_count_delta = 1;
365 } else if (profile_->IsOffTheRecord()) { 404 } else if (profile_->IsOffTheRecord()) {
366 // If this is an incognito profile, we also have to check to make sure the 405 // If this is an incognito profile, we also have to check to make sure the
367 // overflow matches the main bar's status. 406 // overflow matches the main bar's status.
368 ExtensionToolbarModel* main_model = 407 ToolbarActionsModel* main_model =
369 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); 408 ToolbarActionsModel::Get(profile_->GetOriginalProfile());
370 // Find what the index will be in the main bar. Because Observer calls are 409 // Find what the index will be in the main bar. Because Observer calls are
371 // nondeterministic, we can't just assume the main bar will have the 410 // nondeterministic, we can't just assume the main bar will have the
372 // extension and look it up. 411 // extension and look it up.
373 size_t main_index = 412 size_t main_index = main_model->FindNewPositionFromLastKnownGood(
374 main_model->FindNewPositionFromLastKnownGood(extension); 413 ToolbarItem(extension->id(), EXTENSION_ACTION));
375 bool visible = main_index < main_model->visible_icon_count(); 414 bool visible = main_index < main_model->visible_icon_count();
376 // We may need to adjust the visible count if the incognito bar isn't 415 // We may need to adjust the visible count if the incognito bar isn't
377 // showing all icons and this one is visible, or if it is showing all 416 // showing all icons and this one is visible, or if it is showing all
378 // icons and this is hidden. 417 // icons and this is hidden.
379 if (visible && !all_icons_visible()) 418 if (visible && !all_icons_visible())
380 visible_count_delta = 1; 419 visible_count_delta = 1;
381 else if (!visible && all_icons_visible()) 420 else if (!visible && all_icons_visible())
382 visible_count_delta = -1; 421 visible_count_delta = -1;
383 } 422 }
384 423
385 if (visible_count_delta) 424 if (visible_count_delta)
386 SetVisibleIconCount(visible_icon_count() + visible_count_delta); 425 SetVisibleIconCount(visible_icon_count() + visible_count_delta);
387 } 426 }
388 427
389 MaybeUpdateVisibilityPref(extension, new_index); 428 MaybeUpdateVisibilityPref(ToolbarItem(extension->id(), EXTENSION_ACTION),
429 new_index);
390 } 430 }
391 431
392 void ExtensionToolbarModel::RemoveExtension(const Extension* extension) { 432 void ToolbarActionsModel::RemoveExtension(
393 ExtensionList::iterator pos = 433 const extensions::Extension* extension) {
394 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension); 434 std::vector<ToolbarItem>::iterator pos =
435 std::find(toolbar_items_.begin(), toolbar_items_.end(),
436 ToolbarItem(extension->id(), EXTENSION_ACTION));
437
395 if (pos == toolbar_items_.end()) 438 if (pos == toolbar_items_.end())
396 return; 439 return;
397 440
398 size_t index = pos - toolbar_items_.begin(); 441 size_t index = pos - toolbar_items_.begin();
399 // If the removed extension was on the toolbar, a new one will take its place 442 // If the removed extension was on the toolbar, a new one will take its place
400 // if there are any in overflow. 443 // if there are any in overflow.
401 bool new_extension_shown = 444 bool new_extension_shown =
402 !all_icons_visible() && index < visible_icon_count(); 445 !all_icons_visible() && index < visible_icon_count();
403 446
404 // If our visible count is set to the current size, we need to decrement it. 447 // If our visible count is set to the current size, we need to decrement it.
405 if (visible_icon_count_ == static_cast<int>(toolbar_items_.size())) 448 if (visible_icon_count_ == static_cast<int>(toolbar_items_.size()))
406 SetVisibleIconCount(toolbar_items_.size() - 1); 449 SetVisibleIconCount(toolbar_items_.size() - 1);
407 450
408 toolbar_items_.erase(pos); 451 toolbar_items_.erase(pos);
409 452
410 // If we're in highlight mode, we also have to remove the extension from 453 // If we're in highlight mode, we also have to remove the extension from
411 // the highlighted list. 454 // the highlighted list.
412 if (is_highlighting()) { 455 if (is_highlighting()) {
413 pos = std::find(highlighted_items_.begin(), 456 pos = std::find(highlighted_items_.begin(), highlighted_items_.end(),
414 highlighted_items_.end(), 457 ToolbarItem(extension->id(), EXTENSION_ACTION));
415 extension);
416 if (pos != highlighted_items_.end()) { 458 if (pos != highlighted_items_.end()) {
417 highlighted_items_.erase(pos); 459 highlighted_items_.erase(pos);
418 FOR_EACH_OBSERVER(Observer, observers_, 460 FOR_EACH_OBSERVER(Observer, observers_,
419 OnToolbarExtensionRemoved(extension)); 461 OnToolbarActionRemoved(extension->id()));
420 // If the highlighted list is now empty, we stop highlighting. 462 // If the highlighted list is now empty, we stop highlighting.
421 if (highlighted_items_.empty()) 463 if (highlighted_items_.empty())
422 StopHighlighting(); 464 StopHighlighting();
423 } 465 }
424 } else { 466 } else {
425 FOR_EACH_OBSERVER(Observer, observers_, 467 FOR_EACH_OBSERVER(Observer, observers_,
426 OnToolbarExtensionRemoved(extension)); 468 OnToolbarActionRemoved(extension->id()));
427 } 469 }
428 470
429 UpdatePrefs(); 471 UpdatePrefs();
430 if (new_extension_shown) { 472 if (new_extension_shown) {
431 size_t newly_visible_index = visible_icon_count() - 1; 473 size_t newly_visible_index = visible_icon_count() - 1;
432 MaybeUpdateVisibilityPref(toolbar_items_[newly_visible_index].get(), 474 MaybeUpdateVisibilityPref(toolbar_items_[newly_visible_index],
433 newly_visible_index); 475 newly_visible_index);
434 } 476 }
435 } 477 }
436 478
437 // Combine the currently enabled extensions that have browser actions (which 479 // Combine the currently enabled extensions that have browser actions (which
438 // we get from the ExtensionRegistry) with the ordering we get from the 480 // we get from the ExtensionRegistry) and component actions (which we get from
439 // pref service. For robustness we use a somewhat inefficient process: 481 // ComponentToolbarActionsFactory) with the ordering we get from the pref
440 // 1. Create a vector of extensions sorted by their pref values. This vector may 482 // service. For robustness we use a somewhat inefficient process:
483 // 1. Create a vector of actions sorted by their pref values. This vector may
441 // have holes. 484 // have holes.
442 // 2. Create a vector of extensions that did not have a pref value. 485 // 2. Create a vector of actions that did not have a pref value.
443 // 3. Remove holes from the sorted vector and append the unsorted vector. 486 // 3. Remove holes from the sorted vector and append the unsorted vector.
444 void ExtensionToolbarModel::InitializeExtensionList() { 487 void ToolbarActionsModel::InitializeActionList() {
445 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet. 488 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet.
446 489
447 last_known_positions_ = extension_prefs_->GetToolbarOrder(); 490 last_known_positions_ = extension_prefs_->GetToolbarOrder();
448 if (profile_->IsOffTheRecord()) 491 if (profile_->IsOffTheRecord())
449 IncognitoPopulate(); 492 IncognitoPopulate();
450 else 493 else
451 Populate(&last_known_positions_); 494 Populate(&last_known_positions_);
452 495
453 MaybeUpdateVisibilityPrefs(); 496 MaybeUpdateVisibilityPrefs();
454 } 497 }
455 498
456 void ExtensionToolbarModel::Populate(ExtensionIdList* positions) { 499 void ToolbarActionsModel::Populate(std::vector<std::string>* positions) {
457 DCHECK(!profile_->IsOffTheRecord()); 500 DCHECK(!profile_->IsOffTheRecord());
458 const ExtensionSet& extensions =
459 ExtensionRegistry::Get(profile_)->enabled_extensions();
460 // Items that have explicit positions.
461 ExtensionList sorted(positions->size(), NULL);
462 // The items that don't have explicit positions.
463 ExtensionList unsorted;
464 501
465 // Create the lists. 502 std::vector<ToolbarItem> all_actions;
503 // Ids of actions that have explicit positions.
504 std::vector<ToolbarItem> sorted(positions->size(), ToolbarItem());
505 // Ids of actions that don't have explicit positions.
506 std::vector<ToolbarItem> unsorted;
507
508 // Populate the lists.
466 int hidden = 0; 509 int hidden = 0;
467 for (const scoped_refptr<const Extension>& extension : extensions) { 510 int browser_actions_count = 0;
511 int component_actions_count = 0;
512
513 // First, add the extension action ids to all_actions.
514 const extensions::ExtensionSet& extensions =
515 extension_registry_->enabled_extensions();
516 for (const scoped_refptr<const extensions::Extension>& extension :
517 extensions) {
468 if (!ShouldAddExtension(extension.get())) { 518 if (!ShouldAddExtension(extension.get())) {
469 if (!extension_action_api_->GetBrowserActionVisibility(extension->id())) 519 if (!extension_action_api_->GetBrowserActionVisibility(extension->id()))
470 ++hidden; 520 ++hidden;
471 continue; 521 continue;
472 } 522 }
473 523
474 ExtensionIdList::const_iterator pos = 524 all_actions.push_back(ToolbarItem(extension->id(), EXTENSION_ACTION));
475 std::find(positions->begin(), positions->end(), extension->id()); 525 }
526
527 // Next, add the component action ids.
528 std::vector<std::string> component_ids =
529 ComponentToolbarActionsFactory::GetComponentIds();
530 for (const std::string& id : component_ids)
531 all_actions.push_back(ToolbarItem(id, COMPONENT_ACTION));
532
533 // Add each action id to the appropriate list.
534 for (const ToolbarItem& action : all_actions) {
535 std::vector<std::string>::const_iterator pos =
536 std::find(positions->begin(), positions->end(), action.id);
476 if (pos != positions->end()) { 537 if (pos != positions->end()) {
477 sorted[pos - positions->begin()] = extension; 538 sorted[pos - positions->begin()] = action;
478 } else { 539 } else {
479 // Unknown extension - push it to the back of unsorted, and add it to the 540 // Unknown action - push it to the back of unsorted, and add it to the
480 // list of ids at the end. 541 // list of ids at the end.
481 unsorted.push_back(extension); 542 unsorted.push_back(action);
482 positions->push_back(extension->id()); 543 positions->push_back(action.id);
483 } 544 }
484 } 545 }
485 546
486 // Merge the lists. 547 // Merge the lists.
487 sorted.insert(sorted.end(), unsorted.begin(), unsorted.end()); 548 sorted.insert(sorted.end(), unsorted.begin(), unsorted.end());
488 toolbar_items_.reserve(sorted.size()); 549 toolbar_items_.reserve(sorted.size());
489 550
490 for (const scoped_refptr<const Extension>& extension : sorted) { 551 // We don't notify observers of the added extension yet. Rather, observers
491 // It's possible for the extension order to contain items that aren't 552 // should wait for the "OnToolbarModelInitialized" notification, and then
492 // actually loaded on this machine. For example, when extension sync is on, 553 // bulk-update. (This saves a lot of bouncing-back-and-forth here, and allows
493 // we sync the extension order as-is but double-check with the user before 554 // observers to ensure that the extension system is always initialized before
494 // syncing NPAPI-containing extensions, so if one of those is not actually 555 // using the extensions).
495 // synced, we'll get a NULL in the list. This sort of case can also happen 556 for (const ToolbarItem& action : sorted) {
496 // if some error prevents an extension from loading. 557 if (action.type == EXTENSION_ACTION) {
497 if (extension.get()) { 558 // It's possible for the extension order to contain items that aren't
498 // We don't notify observers of the added extension yet. Rather, observers 559 // actually loaded on this machine. For example, when extension sync is
499 // should wait for the "OnToolbarModelInitialized" notification, and then 560 // on, we sync the extension order as-is but double-check with the user
500 // bulk-update. (This saves a lot of bouncing-back-and-forth here, and 561 // before syncing NPAPI-containing extensions, so if one of those is not
501 // allows observers to ensure that the extension system is always 562 // actually synced, we'll get a NULL in the list. This sort of case can
502 // initialized before using the extensions). 563 // also happen if some error prevents an extension from loading.
503 toolbar_items_.push_back(extension); 564 if (GetExtensionById(action.id)) {
565 toolbar_items_.push_back(ToolbarItem(action.id, EXTENSION_ACTION));
566 ++browser_actions_count;
567 }
568 } else if (action.type == COMPONENT_ACTION) {
569 toolbar_items_.push_back(ToolbarItem(action.id, COMPONENT_ACTION));
570 ++component_actions_count;
504 } 571 }
505 } 572 }
506 573
574 // Histogram names are prefixed with "ExtensionToolbarModel" rather than
575 // "ToolbarActionsModel" for historical reasons.
507 UMA_HISTOGRAM_COUNTS_100( 576 UMA_HISTOGRAM_COUNTS_100(
508 "ExtensionToolbarModel.BrowserActionsPermanentlyHidden", hidden); 577 "ExtensionToolbarModel.BrowserActionsPermanentlyHidden", hidden);
509 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsCount", 578 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsCount",
579 browser_actions_count);
580 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.ComponentActionsCount",
581 component_actions_count);
582 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.OverallActionsCount",
510 toolbar_items_.size()); 583 toolbar_items_.size());
511 584
512 if (!toolbar_items_.empty()) { 585 if (!toolbar_items_.empty()) {
513 // Visible count can be -1, meaning: 'show all'. Since UMA converts negative 586 // Visible count can be -1, meaning: 'show all'. Since UMA converts negative
514 // values to 0, this would be counted as 'show none' unless we convert it to 587 // values to 0, this would be counted as 'show none' unless we convert it to
515 // max. 588 // max.
516 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsVisible", 589 UMA_HISTOGRAM_COUNTS_100(
517 visible_icon_count_ == -1 ? 590 "ExtensionToolbarModel.BrowserActionsVisible",
518 base::HistogramBase::kSampleType_MAX : 591 visible_icon_count_ == -1
519 visible_icon_count_); 592 ? visible_icon_count_
593 : visible_icon_count_ - component_actions_count);
594
595 if (use_redesign_) {
596 // The only time this will useful and possibly vary from
597 // BrowserActionsVisible is when the redesign has been enabled.
598 UMA_HISTOGRAM_COUNTS_100("Toolbar.ActionsModel.ToolbarActionsVisible",
599 visible_icon_count_ == -1
600 ? base::HistogramBase::kSampleType_MAX
601 : visible_icon_count_);
602 }
520 } 603 }
521 } 604 }
522 605
523 void ExtensionToolbarModel::IncognitoPopulate() { 606 void ToolbarActionsModel::IncognitoPopulate() {
524 DCHECK(profile_->IsOffTheRecord()); 607 DCHECK(profile_->IsOffTheRecord());
525 const ExtensionToolbarModel* original_model = 608 const ToolbarActionsModel* original_model =
526 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); 609 ToolbarActionsModel::Get(profile_->GetOriginalProfile());
527 610
528 // Find the absolute value of the original model's count. 611 // Find the absolute value of the original model's count.
529 int original_visible = original_model->visible_icon_count(); 612 int original_visible = original_model->visible_icon_count();
530 613
531 // In incognito mode, we show only those extensions that are 614 // In incognito mode, we show only those actions that are incognito-enabled
532 // incognito-enabled. Further, any actions that were overflowed in regular 615 // Further, any actions that were overflowed in regular mode are still
533 // mode are still overflowed. Order is the same as in regular mode. 616 // overflowed. Order is the same as in regular mode.
534 visible_icon_count_ = 0; 617 visible_icon_count_ = 0;
535 for (ExtensionList::const_iterator iter = 618
619 for (std::vector<ToolbarItem>::const_iterator iter =
536 original_model->toolbar_items_.begin(); 620 original_model->toolbar_items_.begin();
537 iter != original_model->toolbar_items_.end(); ++iter) { 621 iter != original_model->toolbar_items_.end(); ++iter) {
538 if (ShouldAddExtension(iter->get())) { 622 // The extension might not be shown in incognito mode. For now, all
539 toolbar_items_.push_back(*iter); 623 // component actions are present.
540 if (iter - original_model->toolbar_items_.begin() < original_visible) 624 if (iter->type == EXTENSION_ACTION &&
541 ++visible_icon_count_; 625 !ShouldAddExtension(GetExtensionById(iter->id)))
542 } 626 continue;
627 toolbar_items_.push_back(*iter);
628 if (iter - original_model->toolbar_items_.begin() < original_visible)
629 ++visible_icon_count_;
543 } 630 }
544 } 631 }
545 632
546 void ExtensionToolbarModel::UpdatePrefs() { 633 void ToolbarActionsModel::UpdatePrefs() {
547 if (!extension_prefs_ || profile_->IsOffTheRecord()) 634 if (!extension_prefs_ || profile_->IsOffTheRecord())
548 return; 635 return;
549 636
550 // Don't observe change caused by self. 637 // Don't observe change caused by self.
551 pref_change_registrar_.Remove(pref_names::kToolbar); 638 pref_change_registrar_.Remove(extensions::pref_names::kToolbar);
552 extension_prefs_->SetToolbarOrder(last_known_positions_); 639 extension_prefs_->SetToolbarOrder(last_known_positions_);
553 pref_change_registrar_.Add(pref_names::kToolbar, pref_change_callback_); 640 pref_change_registrar_.Add(extensions::pref_names::kToolbar,
641 pref_change_callback_);
554 } 642 }
555 643
556 void ExtensionToolbarModel::MaybeUpdateVisibilityPref( 644 void ToolbarActionsModel::MaybeUpdateVisibilityPref(const ToolbarItem& action,
557 const Extension* extension, size_t index) { 645 size_t index) {
646 // Component actions don't have prefs to update.
647 if (action.type == COMPONENT_ACTION)
648 return;
649
558 // We only update the visibility pref for hidden/not hidden based on the 650 // We only update the visibility pref for hidden/not hidden based on the
559 // overflow menu with the new toolbar design. 651 // overflow menu with the new toolbar design.
560 if (include_all_extensions_ && !profile_->IsOffTheRecord()) { 652 if (use_redesign_ && !profile_->IsOffTheRecord()) {
561 bool visible = index < visible_icon_count(); 653 bool visible = index < visible_icon_count();
562 if (visible != extension_action_api_->GetBrowserActionVisibility( 654 if (visible !=
563 extension->id())) { 655 extension_action_api_->GetBrowserActionVisibility(action.id)) {
564 // Don't observe changes caused by ourselves. 656 // Don't observe changes caused by ourselves.
565 bool was_registered = false; 657 bool was_registered = false;
566 if (extension_action_observer_.IsObserving(extension_action_api_)) { 658 if (extension_action_observer_.IsObserving(extension_action_api_)) {
567 was_registered = true; 659 was_registered = true;
568 extension_action_observer_.RemoveAll(); 660 extension_action_observer_.RemoveAll();
569 } 661 }
570 extension_action_api_->SetBrowserActionVisibility(extension->id(), 662 extension_action_api_->SetBrowserActionVisibility(action.id, visible);
571 visible);
572 if (was_registered) 663 if (was_registered)
573 extension_action_observer_.Add(extension_action_api_); 664 extension_action_observer_.Add(extension_action_api_);
574 } 665 }
575 } 666 }
576 } 667 }
577 668
578 void ExtensionToolbarModel::MaybeUpdateVisibilityPrefs() { 669 void ToolbarActionsModel::MaybeUpdateVisibilityPrefs() {
579 for (size_t i = 0u; i < toolbar_items_.size(); ++i) 670 for (size_t i = 0u; i < toolbar_items_.size(); ++i)
580 MaybeUpdateVisibilityPref(toolbar_items_[i].get(), i); 671 MaybeUpdateVisibilityPref(toolbar_items_[i], i);
581 } 672 }
582 673
583 void ExtensionToolbarModel::OnExtensionToolbarPrefChange() { 674 void ToolbarActionsModel::OnActionToolbarPrefChange() {
584 // If extensions are not ready, defer to later Populate() call. 675 // If extensions are not ready, defer to later Populate() call.
585 if (!extensions_initialized_) 676 if (!actions_initialized_)
586 return; 677 return;
587 678
588 // Recalculate |last_known_positions_| to be |pref_positions| followed by 679 // Recalculate |last_known_positions_| to be |pref_positions| followed by
589 // ones that are only in |last_known_positions_|. 680 // ones that are only in |last_known_positions_|.
590 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); 681 std::vector<std::string> pref_positions = extension_prefs_->GetToolbarOrder();
591 size_t pref_position_size = pref_positions.size(); 682 size_t pref_position_size = pref_positions.size();
592 for (size_t i = 0; i < last_known_positions_.size(); ++i) { 683 for (size_t i = 0; i < last_known_positions_.size(); ++i) {
593 if (std::find(pref_positions.begin(), pref_positions.end(), 684 if (std::find(pref_positions.begin(), pref_positions.end(),
594 last_known_positions_[i]) == pref_positions.end()) { 685 last_known_positions_[i]) == pref_positions.end()) {
595 pref_positions.push_back(last_known_positions_[i]); 686 pref_positions.push_back(last_known_positions_[i]);
596 } 687 }
597 } 688 }
598 last_known_positions_.swap(pref_positions); 689 last_known_positions_.swap(pref_positions);
599 690
600 int desired_index = 0; 691 int desired_index = 0;
601 // Loop over the updated list of last known positions, moving any extensions 692 // Loop over the updated list of last known positions, moving any extensions
602 // that are in the wrong place. 693 // that are in the wrong place.
603 for (const std::string& id : last_known_positions_) { 694 for (const std::string& id : last_known_positions_) {
604 int current_index = GetIndexForId(id); 695 int current_index = GetIndexForId(id);
605 if (current_index == -1) 696 if (current_index == -1)
606 continue; 697 continue;
607 if (current_index != desired_index) { 698 if (current_index != desired_index) {
608 scoped_refptr<const Extension> extension = toolbar_items_[current_index]; 699 ToolbarItem action = toolbar_items_[current_index];
609 toolbar_items_.erase(toolbar_items_.begin() + current_index); 700 toolbar_items_.erase(toolbar_items_.begin() + current_index);
610 toolbar_items_.insert(toolbar_items_.begin() + desired_index, extension); 701 toolbar_items_.insert(toolbar_items_.begin() + desired_index, action);
611 // Notify the observers to keep them up-to-date. 702 // Notify the observers to keep them up-to-date.
612 FOR_EACH_OBSERVER( 703 FOR_EACH_OBSERVER(Observer, observers_,
613 Observer, observers_, 704 OnToolbarActionMoved(action.id, desired_index));
614 OnToolbarExtensionMoved(extension.get(), desired_index));
615 } 705 }
616 ++desired_index; 706 ++desired_index;
617 } 707 }
618 708
619 if (last_known_positions_.size() > pref_position_size) { 709 if (last_known_positions_.size() > pref_position_size) {
620 // Need to update pref because we have extra icons. But can't call 710 // Need to update pref because we have extra icons. But can't call
621 // UpdatePrefs() directly within observation closure. 711 // UpdatePrefs() directly within observation closure.
622 base::ThreadTaskRunnerHandle::Get()->PostTask( 712 base::ThreadTaskRunnerHandle::Get()->PostTask(
623 FROM_HERE, base::Bind(&ExtensionToolbarModel::UpdatePrefs, 713 FROM_HERE, base::Bind(&ToolbarActionsModel::UpdatePrefs,
624 weak_ptr_factory_.GetWeakPtr())); 714 weak_ptr_factory_.GetWeakPtr()));
625 } 715 }
626 } 716 }
627 717
628 int ExtensionToolbarModel::GetIndexForId(const std::string& id) const { 718 int ToolbarActionsModel::GetIndexForId(const std::string& id) const {
629 for (size_t i = 0; i < toolbar_items().size(); ++i) { 719 for (size_t i = 0; i < toolbar_items().size(); ++i) {
630 if (toolbar_items()[i]->id() == id) 720 if (toolbar_items()[i].id == id)
631 return i; 721 return i;
632 } 722 }
633 return -1; 723 return -1;
634 } 724 }
635 725
636 bool ExtensionToolbarModel::ShowExtensionActionPopup( 726 bool ToolbarActionsModel::ShowToolbarActionPopup(const std::string& id,
637 const Extension* extension, 727 Browser* browser,
638 Browser* browser, 728 bool grant_active_tab) {
639 bool grant_active_tab) {
640 base::ObserverListBase<Observer>::Iterator it(&observers_); 729 base::ObserverListBase<Observer>::Iterator it(&observers_);
641 Observer* obs = NULL; 730 Observer* obs = NULL;
642 // Look for the Observer associated with the browser. 731 // Look for the Observer associated with the browser.
643 // This would be cleaner if we had an abstract class for the Toolbar UI 732 // This would be cleaner if we had an abstract class for the Toolbar UI
644 // (like we do for LocationBar), but sadly, we don't. 733 // (like we do for LocationBar), but sadly, we don't.
645 while ((obs = it.GetNext()) != NULL) { 734 while ((obs = it.GetNext()) != NULL) {
646 if (obs->GetBrowser() == browser) 735 if (obs->GetBrowser() == browser)
647 return obs->ShowExtensionActionPopup(extension, grant_active_tab); 736 return obs->ShowToolbarActionPopup(id, grant_active_tab);
648 } 737 }
649 return false; 738 return false;
650 } 739 }
651 740
652 void ExtensionToolbarModel::EnsureVisibility( 741 void ToolbarActionsModel::EnsureVisibility(
653 const ExtensionIdList& extension_ids) { 742 const std::vector<std::string>& ids) {
654 if (all_icons_visible()) 743 if (all_icons_visible())
655 return; // Already showing all. 744 return; // Already showing all.
656 745
657 // Otherwise, make sure we have enough room to show all the extensions 746 // Otherwise, make sure we have enough room to show all the extensions
658 // requested. 747 // requested.
659 if (visible_icon_count() < extension_ids.size()) 748 if (visible_icon_count() < ids.size())
660 SetVisibleIconCount(extension_ids.size()); 749 SetVisibleIconCount(ids.size());
661 750
662 if (all_icons_visible()) 751 if (all_icons_visible())
663 return; // May have been set to max by SetVisibleIconCount. 752 return; // May have been set to max by SetVisibleIconCount.
664 753
665 // Guillotine's Delight: Move an orange noble to the front of the line. 754 // Guillotine's Delight: Move an orange noble to the front of the line.
666 for (ExtensionIdList::const_iterator it = extension_ids.begin(); 755 for (std::vector<std::string>::const_iterator it = ids.begin();
667 it != extension_ids.end(); ++it) { 756 it != ids.end(); ++it) {
668 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); 757 for (std::vector<ToolbarItem>::const_iterator item = toolbar_items_.begin();
669 extension != toolbar_items_.end(); ++extension) { 758 item != toolbar_items_.end(); ++item) {
670 if ((*extension)->id() == (*it)) { 759 if (item->id == *it) {
671 if (extension - toolbar_items_.begin() >= 760 if (item - toolbar_items_.begin() >=
672 static_cast<int>(visible_icon_count())) 761 static_cast<int>(visible_icon_count()))
673 MoveExtensionIcon((*extension)->id(), 0); 762 MoveActionIcon(*it, 0);
674 break; 763 break;
675 } 764 }
676 } 765 }
677 } 766 }
678 } 767 }
679 768
680 bool ExtensionToolbarModel::HighlightExtensions( 769 bool ToolbarActionsModel::HighlightActions(const std::vector<std::string>& ids,
681 const ExtensionIdList& extension_ids, 770 HighlightType highlight_type) {
682 HighlightType highlight_type) {
683 highlighted_items_.clear(); 771 highlighted_items_.clear();
684 772
685 for (ExtensionIdList::const_iterator id = extension_ids.begin(); 773 for (const std::string& action_id : ids) {
686 id != extension_ids.end(); 774 for (const ToolbarItem& item : toolbar_items_) {
687 ++id) { 775 if (action_id == item.id)
688 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); 776 highlighted_items_.push_back(item);
689 extension != toolbar_items_.end();
690 ++extension) {
691 if (*id == (*extension)->id())
692 highlighted_items_.push_back(*extension);
693 } 777 }
694 } 778 }
695 779
696 // If we have any items in |highlighted_items_|, then we entered highlighting 780 // If we have any items in |highlighted_items_|, then we entered highlighting
697 // mode. 781 // mode.
698 if (highlighted_items_.size()) { 782 if (highlighted_items_.size()) {
699 // It's important that is_highlighting_ is changed immediately before the 783 // It's important that is_highlighting_ is changed immediately before the
700 // observers are notified since it changes the result of toolbar_items(). 784 // observers are notified since it changes the result of toolbar_items().
701 highlight_type_ = highlight_type; 785 highlight_type_ = highlight_type;
702 FOR_EACH_OBSERVER(Observer, observers_, 786 FOR_EACH_OBSERVER(Observer, observers_,
703 OnToolbarHighlightModeChanged(true)); 787 OnToolbarHighlightModeChanged(true));
704 788
705 // We set the visible icon count after the highlight mode change because 789 // We set the visible icon count after the highlight mode change because
706 // the UI actions are created/destroyed during highlight, and doing that 790 // the UI actions are created/destroyed during highlight, and doing that
707 // prior to changing the size allows us to still have smooth animations. 791 // prior to changing the size allows us to still have smooth animations.
708 if (visible_icon_count() < extension_ids.size()) 792 if (visible_icon_count() < ids.size())
709 SetVisibleIconCount(extension_ids.size()); 793 SetVisibleIconCount(ids.size());
710 794
711 return true; 795 return true;
712 } 796 }
713 797
714 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if 798 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if
715 // we were otherwise in it). 799 // we were otherwise in it).
716 if (is_highlighting()) 800 if (is_highlighting())
717 StopHighlighting(); 801 StopHighlighting();
718 return false; 802 return false;
719 } 803 }
720 804
721 void ExtensionToolbarModel::StopHighlighting() { 805 void ToolbarActionsModel::StopHighlighting() {
722 if (is_highlighting()) { 806 if (is_highlighting()) {
723 // It's important that is_highlighting_ is changed immediately before the 807 // It's important that is_highlighting_ is changed immediately before the
724 // observers are notified since it changes the result of toolbar_items(). 808 // observers are notified since it changes the result of toolbar_items().
725 highlight_type_ = HIGHLIGHT_NONE; 809 highlight_type_ = HIGHLIGHT_NONE;
726 FOR_EACH_OBSERVER(Observer, observers_, 810 FOR_EACH_OBSERVER(Observer, observers_,
727 OnToolbarHighlightModeChanged(false)); 811 OnToolbarHighlightModeChanged(false));
728 812
729 // For the same reason, we don't clear highlighted_items_ until after the 813 // For the same reason, we don't clear highlighted_items_ until after the
730 // mode changed. 814 // mode changed.
731 highlighted_items_.clear(); 815 highlighted_items_.clear();
732 816
733 // We set the visible icon count after the highlight mode change because 817 // We set the visible icon count after the highlight mode change because
734 // the UI actions are created/destroyed during highlight, and doing that 818 // the UI actions are created/destroyed during highlight, and doing that
735 // prior to changing the size allows us to still have smooth animations. 819 // prior to changing the size allows us to still have smooth animations.
736 int saved_icon_count = prefs_->GetInteger(pref_names::kToolbarSize); 820 int saved_icon_count =
821 prefs_->GetInteger(extensions::pref_names::kToolbarSize);
737 if (saved_icon_count != visible_icon_count_) 822 if (saved_icon_count != visible_icon_count_)
738 SetVisibleIconCount(saved_icon_count); 823 SetVisibleIconCount(saved_icon_count);
739 } 824 }
740 } 825 }
741 826
742 bool ExtensionToolbarModel::RedesignIsShowingNewIcons() const { 827 bool ToolbarActionsModel::RedesignIsShowingNewIcons() const {
743 for (const scoped_refptr<const Extension>& extension : toolbar_items_) { 828 for (const ToolbarItem& action : toolbar_items_) {
744 // Without the redesign, we only show extensions with browser actions. 829 if (action.type == EXTENSION_ACTION) {
745 // Any extension without a browser action is an indication that we're 830 // Without the redesign, we only show extensions with browser actions.
746 // showing something new. 831 // Any extension without a browser action is an indication that we're
747 if (!extension->manifest()->HasKey(manifest_keys::kBrowserAction)) 832 // showing something new.
748 return true; 833 if (!GetExtensionById(action.id)->manifest()->HasKey(
834 extensions::manifest_keys::kBrowserAction))
835 return true;
836 }
749 } 837 }
750 return false; 838 return false;
751 } 839 }
752 840
753 } // namespace extensions 841 const extensions::Extension* ToolbarActionsModel::GetExtensionById(
842 const std::string& id) const {
843 return extension_registry_->enabled_extensions().GetByID(id);
844 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/toolbar/toolbar_actions_model.h ('k') | chrome/browser/ui/toolbar/toolbar_actions_model_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698