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

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

Issue 1241063003: Support Component Actions in the toolbar. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ 5 #ifndef CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_MODEL_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ 6 #define CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_MODEL_H_
7 7
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/observer_list.h" 9 #include "base/observer_list.h"
10 #include "base/prefs/pref_change_registrar.h" 10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/scoped_observer.h" 11 #include "base/scoped_observer.h"
12 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" 12 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
13 #include "chrome/browser/extensions/extension_action.h" 13 #include "chrome/browser/extensions/extension_action.h"
14 #include "components/keyed_service/core/keyed_service.h" 14 #include "components/keyed_service/core/keyed_service.h"
15 #include "extensions/browser/extension_prefs.h" 15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_registry_observer.h" 16 #include "extensions/browser/extension_registry_observer.h"
17 #include "extensions/common/extension.h" 17 #include "extensions/common/extension.h"
18 18
19 class Browser; 19 class Browser;
20 class PrefService; 20 class PrefService;
21 class Profile; 21 class Profile;
22 class ToolbarActionsBar;
23 class ToolbarActionViewController;
22 24
23 namespace extensions { 25 namespace extensions {
24 class ExtensionRegistry; 26 class ExtensionRegistry;
25 class ExtensionSet; 27 class ExtensionSet;
28 }
29
30 using ActionIds = std::vector<std::string>;
Peter Kasting 2015/08/11 19:10:49 Nit: Consider naming this ActionIDs instead (up to
apacible 2015/08/12 05:26:44 Same as other comment -- it looks like there's a m
31 using Actions = ScopedVector<ToolbarActionViewController>;
26 32
27 // Model for the browser actions toolbar. 33 // Model for the browser actions toolbar.
28 class ExtensionToolbarModel : public ExtensionActionAPI::Observer, 34 class ToolbarActionsModel : public extensions::ExtensionActionAPI::Observer,
29 public ExtensionRegistryObserver, 35 public extensions::ExtensionRegistryObserver,
30 public KeyedService { 36 public KeyedService {
31 public: 37 public:
32 // The different options for highlighting. 38 // The different options for highlighting.
33 enum HighlightType { 39 enum HighlightType {
34 HIGHLIGHT_NONE, 40 HIGHLIGHT_NONE,
35 HIGHLIGHT_INFO, 41 HIGHLIGHT_INFO,
36 HIGHLIGHT_WARNING, 42 HIGHLIGHT_WARNING,
37 }; 43 };
38 44
39 ExtensionToolbarModel(Profile* profile, ExtensionPrefs* extension_prefs); 45 // The different types of actions.
40 ~ExtensionToolbarModel() override; 46 enum ActionType {
47 COMPONENT_ACTION,
48 EXTENSION_ACTION,
49 };
50
51 ToolbarActionsModel(Profile* profile,
52 extensions::ExtensionPrefs* extension_prefs);
53 ~ToolbarActionsModel() override;
41 54
42 // A class which is informed of changes to the model; represents the view of 55 // A class which is informed of changes to the model; represents the view of
43 // MVC. Also used for signaling view changes such as showing extension popups. 56 // MVC. Also used for signaling view changes such as showing extension popups.
44 // TODO(devlin): Should this really be an observer? It acts more like a 57 // TODO(devlin): Should this really be an observer? It acts more like a
45 // delegate. 58 // delegate.
46 class Observer { 59 class Observer {
47 public: 60 public:
48 // Signals that an |extension| has been added to the toolbar at |index|. 61 // Signals that an action with |id| has been added to the toolbar at
49 // This will *only* be called after the toolbar model has been initialized. 62 // |index|. This will *only* be called after the toolbar model has been
50 virtual void OnToolbarExtensionAdded(const Extension* extension, 63 // initialized.
51 int index) = 0; 64 virtual void OnToolbarActionAdded(const std::string& id, int index) = 0;
52 65
53 // Signals that the given |extension| has been removed from the toolbar. 66 // Signals that the given action with |id| has been removed from the
54 virtual void OnToolbarExtensionRemoved(const Extension* extension) = 0; 67 // toolbar.
68 virtual void OnToolbarActionRemoved(const std::string& id) = 0;
55 69
56 // Signals that the given |extension| has been moved to |index|. |index| is 70 // Signals that the given action with |id| has been moved to |index|.
57 // the desired *final* index of the extension (that is, in the adjusted 71 // |index| is the desired *final* index of the action (that is, in the
58 // order, extension should be at |index|). 72 // adjusted order, action should be at |index|).
59 virtual void OnToolbarExtensionMoved(const Extension* extension, 73 virtual void OnToolbarActionMoved(const std::string& id, int index) = 0;
60 int index) = 0;
61 74
62 // Signals that the browser action for the given |extension| has been 75 // Signals that the browser action with |id| has been updated.
63 // updated. 76 virtual void OnToolbarActionUpdated(const std::string& id) = 0;
64 virtual void OnToolbarExtensionUpdated(const Extension* extension) = 0;
65 77
66 // Signals the |extension| to show the popup now in the active window. 78 // Signals the action with |id| to show the popup now in the active
67 // If |grant_active_tab| is true, then active tab permissions should be 79 // window. If |grant_active_tab| is true, then active tab permissions
68 // given to the extension (only do this if this is through a user action). 80 // should be given to the action (only do this if this is through a user
69 // Returns true if a popup was slated to be shown. 81 // action). Returns true if a popup was slated to be shown.
70 virtual bool ShowExtensionActionPopup(const Extension* extension, 82 virtual bool ShowToolbarActionPopup(const std::string& id,
71 bool grant_active_tab) = 0; 83 bool grant_active_tab) = 0;
72 84
73 // Signals when the container needs to be redrawn because of a size change, 85 // Signals when the container needs to be redrawn because of a size change,
74 // and when the model has finished loading. 86 // and when the model has finished loading.
75 virtual void OnToolbarVisibleCountChanged() = 0; 87 virtual void OnToolbarVisibleCountChanged() = 0;
76 88
77 // Signals that the model has entered or exited highlighting mode, or that 89 // Signals that the model has entered or exited highlighting mode, or that
78 // the extensions being highlighted have (probably*) changed. Highlighting 90 // the actions being highlighted have (probably*) changed. Highlighting
79 // mode indicates that only a subset of the extensions are actively 91 // mode indicates that only a subset of the toolbar actions are actively
80 // displayed, and those extensions should be highlighted for extra emphasis. 92 // displayed, and those actions should be highlighted for extra emphasis.
81 // * probably, because if we are in highlight mode and receive a call to 93 // * probably, because if we are in highlight mode and receive a call to
82 // highlight a new set of extensions, we do not compare the current set 94 // highlight a new set of actions, we do not compare the current set with
83 // with the new set (and just assume the new set is different). 95 // the new set (and just assume the new set is different).
84 virtual void OnToolbarHighlightModeChanged(bool is_highlighting) = 0; 96 virtual void OnToolbarHighlightModeChanged(bool is_highlighting) = 0;
85 97
86 // Signals that the toolbar model has been initialized, so that if any 98 // Signals that the toolbar model has been initialized, so that if any
87 // observers were postponing animation during the initialization stage, they 99 // observers were postponing animation during the initialization stage, they
88 // can catch up. 100 // can catch up.
89 virtual void OnToolbarModelInitialized() = 0; 101 virtual void OnToolbarModelInitialized() = 0;
90 102
91 // Returns the browser associated with the Observer. 103 // Returns the browser associated with the Observer.
92 virtual Browser* GetBrowser() = 0; 104 virtual Browser* GetBrowser() = 0;
93 105
94 protected: 106 protected:
95 virtual ~Observer() {} 107 virtual ~Observer() {}
96 }; 108 };
97 109
98 // Convenience function to get the ExtensionToolbarModel for a Profile. 110 // Convenience function to get the ToolbarActionsModel for a Profile.
99 static ExtensionToolbarModel* Get(Profile* profile); 111 static ToolbarActionsModel* Get(Profile* profile);
100 112
101 // Adds or removes an observer. 113 // Adds or removes an observer.
102 void AddObserver(Observer* observer); 114 void AddObserver(Observer* observer);
103 void RemoveObserver(Observer* observer); 115 void RemoveObserver(Observer* observer);
104 116
105 // Moves the given |extension|'s icon to the given |index|. 117 // Moves the given action with |id|'s icon to the given |index|.
106 void MoveExtensionIcon(const std::string& id, size_t index); 118 void MoveActionIcon(const std::string& id, size_t index);
107 119
108 // Sets the number of extension icons that should be visible. 120 // Sets the number of action icons that should be visible.
109 // If count == size(), this will set the visible icon count to -1, meaning 121 // If count == size(), this will set the visible icon count to -1, meaning
110 // "show all actions". 122 // "show all actions".
111 void SetVisibleIconCount(size_t count); 123 void SetVisibleIconCount(size_t count);
112 124
113 size_t visible_icon_count() const { 125 size_t visible_icon_count() const {
114 // We have guards around this because |visible_icon_count_| can be set by 126 // We have guards around this because |visible_icon_count_| can be set by
115 // prefs/sync, and we want to ensure that the icon count returned is within 127 // prefs/sync, and we want to ensure that the icon count returned is within
116 // bounds. 128 // bounds.
117 return visible_icon_count_ == -1 ? 129 return visible_icon_count_ == -1 ?
118 toolbar_items().size() : 130 toolbar_items().size() :
119 std::min(static_cast<size_t>(visible_icon_count_), 131 std::min(static_cast<size_t>(visible_icon_count_),
120 toolbar_items().size()); 132 toolbar_items().size());
121 } 133 }
122 134
123 bool all_icons_visible() const { return visible_icon_count_ == -1; } 135 bool all_icons_visible() const { return visible_icon_count_ == -1; }
124 136
125 bool extensions_initialized() const { return extensions_initialized_; } 137 bool actions_initialized() const { return actions_initialized_; }
126 138
127 const ExtensionList& toolbar_items() const { 139 Actions CreateActions(Browser* browser, ToolbarActionsBar* bar);
140
141 const ActionIds& toolbar_items() const {
128 return is_highlighting() ? highlighted_items_ : toolbar_items_; 142 return is_highlighting() ? highlighted_items_ : toolbar_items_;
129 } 143 }
130 144
145 const std::map<std::string, ActionType> action_id_to_type() const {
146 return action_id_to_type_;
147 }
148
131 bool is_highlighting() const { return highlight_type_ != HIGHLIGHT_NONE; } 149 bool is_highlighting() const { return highlight_type_ != HIGHLIGHT_NONE; }
132 HighlightType highlight_type() const { return highlight_type_; } 150 HighlightType highlight_type() const { return highlight_type_; }
133 151
134 void OnExtensionToolbarPrefChange(); 152 void OnActionToolbarPrefChange();
135 153
136 // Returns the index of the given |id|, or -1 if the id wasn't found. 154 // Returns the index of the given action with |id|, or -1 if the id
155 // wasn't found.
137 int GetIndexForId(const std::string& id) const; 156 int GetIndexForId(const std::string& id) const;
138 157
139 // Finds the Observer associated with |browser| and tells it to display a 158 // Finds the Observer associated with |browser| and tells it to display a
140 // popup for the given |extension|. If |grant_active_tab| is true, this 159 // popup for the given action with |id|. If |grant_active_tab| is true,
141 // grants active tab permissions to the |extension|; only do this because of 160 // this grants active tab permissions to the action; only do this because of
142 // a direct user action. 161 // a direct user action.
143 bool ShowExtensionActionPopup(const Extension* extension, 162 bool ShowToolbarActionPopup(const std::string& id,
144 Browser* browser, 163 Browser* browser,
145 bool grant_active_tab); 164 bool grant_active_tab);
146 165
147 // Ensures that the extensions in the |extension_ids| list are visible on the 166 // Ensures that the actions in the |action_ids| list are visible on the
148 // toolbar. This might mean they need to be moved to the front (if they are in 167 // toolbar. This might mean they need to be moved to the front (if they are in
149 // the overflow bucket). 168 // the overflow bucket).
150 void EnsureVisibility(const ExtensionIdList& extension_ids); 169 void EnsureVisibility(const ActionIds& action_ids);
151 170
152 // Highlights the extensions specified by |extension_ids|. This will cause 171 // Highlights the actions specified by |action_ids|. This will cause
153 // the ToolbarModel to only display those extensions. 172 // the ToolbarModel to only display those actions.
154 // Highlighting mode is only entered if there is at least one extension to 173 // Highlighting mode is only entered if there is at least one action to be
155 // be shown. 174 // shown.
156 // Returns true if highlighting mode is entered, false otherwise. 175 // Returns true if highlighting mode is entered, false otherwise.
157 bool HighlightExtensions(const ExtensionIdList& extension_ids, 176 bool HighlightActions(const ActionIds& action_ids,
158 HighlightType type); 177 HighlightType type);
159 178
160 // Stop highlighting extensions. All extensions can be shown again, and the 179 // Stop highlighting actions. All actions can be shown again, and the
161 // number of visible icons will be reset to what it was before highlighting. 180 // number of visible icons will be reset to what it was before highlighting.
162 void StopHighlighting(); 181 void StopHighlighting();
163 182
164 // Returns true if the toolbar model is running with the redesign and is 183 // Returns true if the toolbar model is running with the redesign and is
165 // showing new icons as a result. 184 // showing new icons as a result.
166 bool RedesignIsShowingNewIcons() const; 185 bool RedesignIsShowingNewIcons() const;
167 186
187 // Returns true if the action with |id| is of |type|.
188 bool IsKnownActionWithType(const std::string& id, ActionType type) const;
189
168 private: 190 private:
169 // Callback when extensions are ready. 191 // Callback when actions are ready.
170 void OnReady(); 192 void OnReady();
171 193
172 // ExtensionRegistryObserver: 194 // ExtensionRegistryObserver:
173 void OnExtensionLoaded(content::BrowserContext* browser_context, 195 void OnExtensionLoaded(content::BrowserContext* browser_context,
174 const Extension* extension) override; 196 const extensions::Extension* extension) override;
175 void OnExtensionUnloaded(content::BrowserContext* browser_context, 197 void OnExtensionUnloaded(content::BrowserContext* browser_context,
176 const Extension* extension, 198 const extensions::Extension* extension,
177 UnloadedExtensionInfo::Reason reason) override; 199 extensions::UnloadedExtensionInfo::Reason reason)
200 override;
178 void OnExtensionUninstalled(content::BrowserContext* browser_context, 201 void OnExtensionUninstalled(content::BrowserContext* browser_context,
179 const Extension* extension, 202 const extensions::Extension* extension,
180 extensions::UninstallReason reason) override; 203 extensions::UninstallReason reason) override;
181 204
182 // ExtensionActionAPI::Observer: 205 // ExtensionActionAPI::Observer:
183 void OnExtensionActionUpdated( 206 void OnExtensionActionUpdated(
184 ExtensionAction* extension_action, 207 ExtensionAction* extension_action,
185 content::WebContents* web_contents, 208 content::WebContents* web_contents,
186 content::BrowserContext* browser_context) override; 209 content::BrowserContext* browser_context) override;
187 void OnExtensionActionVisibilityChanged(const std::string& extension_id, 210 void OnExtensionActionVisibilityChanged(const std::string& extension_id,
188 bool is_now_visible) override; 211 bool is_now_visible) override;
189 212
190 // To be called after the extension service is ready; gets loaded extensions 213 // To be called after the extension service is ready; gets loaded extensions
191 // from the ExtensionRegistry and their saved order from the pref service 214 // from the ExtensionRegistry and their saved order from the pref service
192 // and constructs |toolbar_items_| from these data. IncognitoPopulate() 215 // and constructs |toolbar_items_| from these data. IncognitoPopulate()
193 // takes the shortcut - looking at the regular model's content and modifying 216 // takes the shortcut - looking at the regular model's content and modifying
194 // it. 217 // it.
195 void InitializeExtensionList(); 218 void InitializeActionList();
196 void Populate(ExtensionIdList* positions); 219 void Populate(ActionIds* positions);
197 void IncognitoPopulate(); 220 void IncognitoPopulate();
198 221
199 // Save the model to prefs. 222 // Save the model to prefs.
200 void UpdatePrefs(); 223 void UpdatePrefs();
201 224
202 // Updates |extension|'s browser action visibility pref if the browser action 225 // Updates action with |id|'s browser action visibility pref if the
203 // is in the overflow menu and should be considered hidden. 226 // browser action is in the overflow menu and should be considered hidden.
204 void MaybeUpdateVisibilityPref(const Extension* extension, size_t index); 227 void MaybeUpdateVisibilityPref(const std::string& id, size_t index);
205 228
206 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|. 229 // Calls MaybeUpdateVisibilityPref() for each action in |toolbar_items|.
207 void MaybeUpdateVisibilityPrefs(); 230 void MaybeUpdateVisibilityPrefs();
208 231
209 // Finds the last known visible position of the icon for an |extension|. The 232 // Finds the last known visible position of the icon for an action with |id|.
210 // value returned is a zero-based index into the vector of visible items. 233 // The value returned is a zero-based index into the vector of visible items.
211 size_t FindNewPositionFromLastKnownGood(const Extension* extension); 234 size_t FindNewPositionFromLastKnownGood(const std::string& id);
212 235
213 // Returns true if the given |extension| should be added to the toolbar. 236 // Returns true if the given |extension| should be added to the toolbar.
214 bool ShouldAddExtension(const Extension* extension); 237 bool ShouldAddExtension(const extensions::Extension* extension);
215 238
216 // Adds or removes the given |extension| from the toolbar model. 239 // Adds or removes the given |extension| from the toolbar model.
217 void AddExtension(const Extension* extension); 240 void AddExtension(const extensions::Extension* extension);
218 void RemoveExtension(const Extension* extension); 241 void RemoveExtension(const extensions::Extension* extension);
242
243 const extensions::Extension* GetExtensionById(const std::string& id) const;
219 244
220 // Our observers. 245 // Our observers.
221 base::ObserverList<Observer> observers_; 246 base::ObserverList<Observer> observers_;
222 247
223 // The Profile this toolbar model is for. 248 // The Profile this toolbar model is for.
224 Profile* profile_; 249 Profile* profile_;
225 250
226 ExtensionPrefs* extension_prefs_; 251 extensions::ExtensionPrefs* prefs_service_;
227 PrefService* prefs_; 252 PrefService* prefs_;
228 253
229 // The ExtensionActionAPI object, cached for convenience. 254 // The ExtensionActionAPI object, cached for convenience.
230 ExtensionActionAPI* extension_action_api_; 255 extensions::ExtensionActionAPI* extension_action_api_;
256
257 // The ExtensionRegistry object, cached for convenience.
258 extensions::ExtensionRegistry* extension_registry_;
231 259
232 // True if we've handled the initial EXTENSIONS_READY notification. 260 // True if we've handled the initial EXTENSIONS_READY notification.
233 bool extensions_initialized_; 261 bool actions_initialized_;
234 262
235 // If true, we include all extensions in the toolbar model. If false, we only 263 // If true, we include all actions in the toolbar model.
236 // include browser actions. 264 bool use_redesign_;
237 bool include_all_extensions_;
238 265
239 // Ordered list of browser action buttons. 266 // Ordered list of browser action ids.
240 ExtensionList toolbar_items_; 267 ActionIds toolbar_items_;
241 268
242 // List of browser action buttons which should be highlighted. 269 // List of browser action ids which should be highlighted.
243 ExtensionList highlighted_items_; 270 ActionIds highlighted_items_;
271
272 // Maps each action id to their corresponding ActionType.
273 std::map<std::string, ActionType> action_id_to_type_;
244 274
245 // The current type of highlight (with HIGHLIGHT_NONE indicating no current 275 // The current type of highlight (with HIGHLIGHT_NONE indicating no current
246 // highlight). 276 // highlight).
247 HighlightType highlight_type_; 277 HighlightType highlight_type_;
248 278
249 ExtensionIdList last_known_positions_; 279 // A list of action ids ordered to correspond with their last known
280 // positions.
281 ActionIds last_known_positions_;
250 282
251 // The number of icons visible (the rest should be hidden in the overflow 283 // The number of icons visible (the rest should be hidden in the overflow
252 // chevron). A value of -1 indicates that all icons should be visible. 284 // chevron). A value of -1 indicates that all icons should be visible.
253 // Instead of using this variable directly, use visible_icon_count() if 285 // Instead of using this variable directly, use visible_icon_count() if
254 // possible. 286 // possible.
255 // TODO(devlin): Make a new variable to indicate that all icons should be 287 // TODO(devlin): Make a new variable to indicate that all icons should be
256 // visible, instead of overloading this one. 288 // visible, instead of overloading this one.
257 int visible_icon_count_; 289 int visible_icon_count_;
258 290
259 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> 291 ScopedObserver<extensions::ExtensionActionAPI,
260 extension_action_observer_; 292 extensions::ExtensionActionAPI::Observer> extension_action_observer_;
261 293
262 // Listen to extension load, unloaded notifications. 294 // Listen to extension load, unloaded notifications.
263 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> 295 ScopedObserver<extensions::ExtensionRegistry, ExtensionRegistryObserver>
264 extension_registry_observer_; 296 extension_registry_observer_;
265 297
266 // For observing change of toolbar order preference by external entity (sync). 298 // For observing change of toolbar order preference by external entity (sync).
267 PrefChangeRegistrar pref_change_registrar_; 299 PrefChangeRegistrar pref_change_registrar_;
268 base::Closure pref_change_callback_; 300 base::Closure pref_change_callback_;
269 301
270 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; 302 base::WeakPtrFactory<ToolbarActionsModel> weak_ptr_factory_;
271 303
272 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); 304 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsModel);
273 }; 305 };
274 306
275 } // namespace extensions 307 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_MODEL_H_
276
277 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698