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

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