OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_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" |
(...skipping 25 matching lines...) Expand all Loading... |
36 ~ExtensionToolbarModel() override; | 36 ~ExtensionToolbarModel() override; |
37 | 37 |
38 // A class which is informed of changes to the model; represents the view of | 38 // A class which is informed of changes to the model; represents the view of |
39 // MVC. Also used for signaling view changes such as showing extension popups. | 39 // MVC. Also used for signaling view changes such as showing extension popups. |
40 // TODO(devlin): Should this really be an observer? It acts more like a | 40 // TODO(devlin): Should this really be an observer? It acts more like a |
41 // delegate. | 41 // delegate. |
42 class Observer { | 42 class Observer { |
43 public: | 43 public: |
44 // TODO(devlin): Rename these methods to be OnFoo. | 44 // TODO(devlin): Rename these methods to be OnFoo. |
45 // Signals that an |extension| has been added to the toolbar at |index|. | 45 // Signals that an |extension| has been added to the toolbar at |index|. |
| 46 // This will *only* be called after the toolbar model has been initialized. |
46 virtual void ToolbarExtensionAdded(const Extension* extension, | 47 virtual void ToolbarExtensionAdded(const Extension* extension, |
47 int index) = 0; | 48 int index) = 0; |
48 | 49 |
49 // Signals that the given |extension| has been removed from the toolbar. | 50 // Signals that the given |extension| has been removed from the toolbar. |
50 virtual void ToolbarExtensionRemoved(const Extension* extension) = 0; | 51 virtual void ToolbarExtensionRemoved(const Extension* extension) = 0; |
51 | 52 |
52 // Signals that the given |extension| has been moved to |index|. |index| is | 53 // Signals that the given |extension| has been moved to |index|. |index| is |
53 // the desired *final* index of the extension (that is, in the adjusted | 54 // the desired *final* index of the extension (that is, in the adjusted |
54 // order, extension should be at |index|). | 55 // order, extension should be at |index|). |
55 virtual void ToolbarExtensionMoved(const Extension* extension, | 56 virtual void ToolbarExtensionMoved(const Extension* extension, |
(...skipping 16 matching lines...) Expand all Loading... |
72 | 73 |
73 // Signals that the model has entered or exited highlighting mode, or that | 74 // Signals that the model has entered or exited highlighting mode, or that |
74 // the extensions being highlighted have (probably*) changed. Highlighting | 75 // the extensions being highlighted have (probably*) changed. Highlighting |
75 // mode indicates that only a subset of the extensions are actively | 76 // mode indicates that only a subset of the extensions are actively |
76 // displayed, and those extensions should be highlighted for extra emphasis. | 77 // displayed, and those extensions should be highlighted for extra emphasis. |
77 // * probably, because if we are in highlight mode and receive a call to | 78 // * probably, because if we are in highlight mode and receive a call to |
78 // highlight a new set of extensions, we do not compare the current set | 79 // highlight a new set of extensions, we do not compare the current set |
79 // with the new set (and just assume the new set is different). | 80 // with the new set (and just assume the new set is different). |
80 virtual void ToolbarHighlightModeChanged(bool is_highlighting) = 0; | 81 virtual void ToolbarHighlightModeChanged(bool is_highlighting) = 0; |
81 | 82 |
| 83 // Signals that the toolbar model has been initialized, so that if any |
| 84 // observers were postponing animation during the initialization stage, they |
| 85 // can catch up. |
| 86 virtual void OnToolbarModelInitialized() = 0; |
| 87 |
82 // Signals that the toolbar needs to be reordered for the given | 88 // Signals that the toolbar needs to be reordered for the given |
83 // |web_contents|. This is caused by an overflowed action wanting to run, | 89 // |web_contents|. This is caused by an overflowed action wanting to run, |
84 // and needing to "pop itself out". | 90 // and needing to "pop itself out". |
85 virtual void OnToolbarReorderNecessary( | 91 virtual void OnToolbarReorderNecessary( |
86 content::WebContents* web_contents) = 0; | 92 content::WebContents* web_contents) = 0; |
87 | 93 |
88 // Signals that the toolbar model has been initialized, so that if any | |
89 // observers were postponing animation during the initialization stage, they | |
90 // can catch up. | |
91 virtual void OnToolbarModelInitialized() = 0; | |
92 | |
93 // Returns the browser associated with the Observer. | 94 // Returns the browser associated with the Observer. |
94 virtual Browser* GetBrowser() = 0; | 95 virtual Browser* GetBrowser() = 0; |
95 | 96 |
96 protected: | 97 protected: |
97 virtual ~Observer() {} | 98 virtual ~Observer() {} |
98 }; | 99 }; |
99 | 100 |
100 // Convenience function to get the ExtensionToolbarModel for a Profile. | 101 // Convenience function to get the ExtensionToolbarModel for a Profile. |
101 static ExtensionToolbarModel* Get(Profile* profile); | 102 static ExtensionToolbarModel* Get(Profile* profile); |
102 | 103 |
103 // Adds or removes an observer. | 104 // Adds or removes an observer. |
104 void AddObserver(Observer* observer); | 105 void AddObserver(Observer* observer); |
105 void RemoveObserver(Observer* observer); | 106 void RemoveObserver(Observer* observer); |
106 | 107 |
107 // Moves the given |extension|'s icon to the given |index|. | 108 // Moves the given |extension|'s icon to the given |index|. |
108 void MoveExtensionIcon(const std::string& id, size_t index); | 109 void MoveExtensionIcon(const std::string& id, size_t index); |
109 | 110 |
110 // Sets the number of extension icons that should be visible. | 111 // Sets the number of extension icons that should be visible. |
111 // If count == size(), this will set the visible icon count to -1, meaning | 112 // If count == size(), this will set the visible icon count to -1, meaning |
112 // "show all actions". | 113 // "show all actions". |
113 void SetVisibleIconCount(size_t count); | 114 void SetVisibleIconCount(size_t count); |
114 | 115 |
115 size_t visible_icon_count() const { | 116 size_t visible_icon_count() const { |
| 117 // We have guards around this because |visible_icon_count_| can be set by |
| 118 // prefs/sync, and we want to ensure that the icon count returned is within |
| 119 // bounds. |
116 return visible_icon_count_ == -1 ? | 120 return visible_icon_count_ == -1 ? |
117 toolbar_items().size() : static_cast<size_t>(visible_icon_count_); | 121 toolbar_items().size() : |
| 122 std::min(static_cast<size_t>(visible_icon_count_), |
| 123 toolbar_items().size()); |
118 } | 124 } |
119 | 125 |
120 bool all_icons_visible() const { return visible_icon_count_ == -1; } | 126 bool all_icons_visible() const { return visible_icon_count_ == -1; } |
121 | 127 |
122 bool extensions_initialized() const { return extensions_initialized_; } | 128 bool extensions_initialized() const { return extensions_initialized_; } |
123 | 129 |
124 const ExtensionList& toolbar_items() const { | 130 const ExtensionList& toolbar_items() const { |
125 return is_highlighting_ ? highlighted_items_ : toolbar_items_; | 131 return is_highlighting_ ? highlighted_items_ : toolbar_items_; |
126 } | 132 } |
127 | 133 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 // it. | 201 // it. |
196 void InitializeExtensionList(); | 202 void InitializeExtensionList(); |
197 void Populate(const ExtensionIdList& positions); | 203 void Populate(const ExtensionIdList& positions); |
198 void IncognitoPopulate(); | 204 void IncognitoPopulate(); |
199 | 205 |
200 // Save the model to prefs. | 206 // Save the model to prefs. |
201 void UpdatePrefs(); | 207 void UpdatePrefs(); |
202 | 208 |
203 // Updates |extension|'s browser action visibility pref if the browser action | 209 // Updates |extension|'s browser action visibility pref if the browser action |
204 // is in the overflow menu and should be considered hidden. | 210 // is in the overflow menu and should be considered hidden. |
205 void MaybeUpdateVisibilityPref(const Extension* extension, int index); | 211 void MaybeUpdateVisibilityPref(const Extension* extension, size_t index); |
206 | 212 |
207 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|. | 213 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|. |
208 void MaybeUpdateVisibilityPrefs(); | 214 void MaybeUpdateVisibilityPrefs(); |
209 | 215 |
210 // Finds the last known visible position of the icon for an |extension|. The | 216 // Finds the last known visible position of the icon for an |extension|. The |
211 // value returned is a zero-based index into the vector of visible items. | 217 // value returned is a zero-based index into the vector of visible items. |
212 size_t FindNewPositionFromLastKnownGood(const Extension* extension); | 218 size_t FindNewPositionFromLastKnownGood(const Extension* extension); |
213 | 219 |
214 // Returns true if the given |extension| should be added to the toolbar. | 220 // Returns true if the given |extension| should be added to the toolbar. |
215 bool ShouldAddExtension(const Extension* extension); | 221 bool ShouldAddExtension(const Extension* extension); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 bool is_highlighting_; | 255 bool is_highlighting_; |
250 | 256 |
251 // The number of icons which were visible before highlighting a subset, in | 257 // The number of icons which were visible before highlighting a subset, in |
252 // order to restore the count when finished. | 258 // order to restore the count when finished. |
253 int old_visible_icon_count_; | 259 int old_visible_icon_count_; |
254 | 260 |
255 ExtensionIdList last_known_positions_; | 261 ExtensionIdList last_known_positions_; |
256 | 262 |
257 // The number of icons visible (the rest should be hidden in the overflow | 263 // The number of icons visible (the rest should be hidden in the overflow |
258 // chevron). A value of -1 indicates that all icons should be visible. | 264 // chevron). A value of -1 indicates that all icons should be visible. |
| 265 // Instead of using this variable directly, use visible_icon_count() if |
| 266 // possible. |
259 // TODO(devlin): Make a new variable to indicate that all icons should be | 267 // TODO(devlin): Make a new variable to indicate that all icons should be |
260 // visible, instead of overloading this one. | 268 // visible, instead of overloading this one. |
261 int visible_icon_count_; | 269 int visible_icon_count_; |
262 | 270 |
263 content::NotificationRegistrar registrar_; | 271 content::NotificationRegistrar registrar_; |
264 | 272 |
265 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> | 273 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> |
266 extension_action_observer_; | 274 extension_action_observer_; |
267 | 275 |
268 // Listen to extension load, unloaded notifications. | 276 // Listen to extension load, unloaded notifications. |
269 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 277 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
270 extension_registry_observer_; | 278 extension_registry_observer_; |
271 | 279 |
272 // For observing change of toolbar order preference by external entity (sync). | 280 // For observing change of toolbar order preference by external entity (sync). |
273 PrefChangeRegistrar pref_change_registrar_; | 281 PrefChangeRegistrar pref_change_registrar_; |
274 base::Closure pref_change_callback_; | 282 base::Closure pref_change_callback_; |
275 | 283 |
276 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; | 284 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; |
277 | 285 |
278 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); | 286 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); |
279 }; | 287 }; |
280 | 288 |
281 } // namespace extensions | 289 } // namespace extensions |
282 | 290 |
283 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ | 291 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ |
OLD | NEW |