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 23 matching lines...) Expand all Loading... | |
34 public: | 34 public: |
35 ExtensionToolbarModel(Profile* profile, ExtensionPrefs* extension_prefs); | 35 ExtensionToolbarModel(Profile* profile, ExtensionPrefs* extension_prefs); |
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 // An extension has been added to the toolbar and should go at |index|. | 44 // TODO(devlin): Rename these methods to be OnFoo. |
45 // Signals that an |extension| has been added to the toolbar at |index|. | |
45 virtual void ToolbarExtensionAdded(const Extension* extension, | 46 virtual void ToolbarExtensionAdded(const Extension* extension, |
46 int index) = 0; | 47 int index) = 0; |
47 | 48 |
48 // The given |extension| should be removed from the toolbar. | 49 // Signals that the given |extension| has been removed from the toolbar. |
49 virtual void ToolbarExtensionRemoved(const Extension* extension) = 0; | 50 virtual void ToolbarExtensionRemoved(const Extension* extension) = 0; |
50 | 51 |
51 // The given |extension| has been moved to |index|. |index| is the desired | 52 // Signals that the given |extension| has been moved to |index|. |index| is |
52 // *final* index of the extension (that is, in the adjusted order, extension | 53 // the desired *final* index of the extension (that is, in the adjusted |
53 // should be at |index|). | 54 // order, extension should be at |index|). |
54 virtual void ToolbarExtensionMoved(const Extension* extension, | 55 virtual void ToolbarExtensionMoved(const Extension* extension, |
55 int index) = 0; | 56 int index) = 0; |
56 | 57 |
57 // Signals that the browser action for the given |extension| has been | 58 // Signals that the browser action for the given |extension| has been |
58 // updated. | 59 // updated. |
59 virtual void ToolbarExtensionUpdated(const Extension* extension) = 0; | 60 virtual void ToolbarExtensionUpdated(const Extension* extension) = 0; |
60 | 61 |
61 // Signal the |extension| to show the popup now in the active window. | 62 // Signals the |extension| to show the popup now in the active window. |
62 // If |grant_active_tab| is true, then active tab permissions should be | 63 // If |grant_active_tab| is true, then active tab permissions should be |
63 // given to the extension (only do this if this is through a user action). | 64 // given to the extension (only do this if this is through a user action). |
64 // Returns true if a popup was slated to be shown. | 65 // Returns true if a popup was slated to be shown. |
65 virtual bool ShowExtensionActionPopup(const Extension* extension, | 66 virtual bool ShowExtensionActionPopup(const Extension* extension, |
66 bool grant_active_tab) = 0; | 67 bool grant_active_tab) = 0; |
67 | 68 |
68 // Signal when the container needs to be redrawn because of a size change, | 69 // Signals when the container needs to be redrawn because of a size change, |
69 // and when the model has finished loading. | 70 // and when the model has finished loading. |
70 virtual void ToolbarVisibleCountChanged() = 0; | 71 virtual void ToolbarVisibleCountChanged() = 0; |
71 | 72 |
72 // Signal that the model has entered or exited highlighting mode, or that | 73 // Signals that the model has entered or exited highlighting mode, or that |
73 // the extensions being highlighted have (probably*) changed. Highlighting | 74 // the extensions being highlighted have (probably*) changed. Highlighting |
74 // mode indicates that only a subset of the extensions are actively | 75 // mode indicates that only a subset of the extensions are actively |
75 // displayed, and those extensions should be highlighted for extra emphasis. | 76 // displayed, and those extensions should be highlighted for extra emphasis. |
76 // * probably, because if we are in highlight mode and receive a call to | 77 // * probably, because if we are in highlight mode and receive a call to |
77 // highlight a new set of extensions, we do not compare the current set | 78 // highlight a new set of extensions, we do not compare the current set |
78 // with the new set (and just assume the new set is different). | 79 // with the new set (and just assume the new set is different). |
79 virtual void ToolbarHighlightModeChanged(bool is_highlighting) = 0; | 80 virtual void ToolbarHighlightModeChanged(bool is_highlighting) = 0; |
80 | 81 |
82 // Signals that the toolbar needs to be reordered for the given | |
83 // |web_contents|. This is caused by an overflowed action wanting to run, | |
84 // and needing to "pop itself out". | |
85 virtual void OnToolbarReorderNecessary( | |
86 content::WebContents* web_contents) = 0; | |
87 | |
81 // Returns the browser associated with the Observer. | 88 // Returns the browser associated with the Observer. |
82 virtual Browser* GetBrowser() = 0; | 89 virtual Browser* GetBrowser() = 0; |
83 | 90 |
84 protected: | 91 protected: |
85 virtual ~Observer() {} | 92 virtual ~Observer() {} |
86 }; | 93 }; |
87 | 94 |
88 // Convenience function to get the ExtensionToolbarModel for a Profile. | 95 // Convenience function to get the ExtensionToolbarModel for a Profile. |
89 static ExtensionToolbarModel* Get(Profile* profile); | 96 static ExtensionToolbarModel* Get(Profile* profile); |
90 | 97 |
91 // Add or remove an observer. | 98 // Adds or removes an observer. |
92 void AddObserver(Observer* observer); | 99 void AddObserver(Observer* observer); |
93 void RemoveObserver(Observer* observer); | 100 void RemoveObserver(Observer* observer); |
94 | 101 |
95 // Moves the given |extension|'s icon to the given |index|. | 102 // Moves the given |extension|'s icon to the given |index|. |
96 void MoveExtensionIcon(const std::string& id, size_t index); | 103 void MoveExtensionIcon(const std::string& id, size_t index); |
97 | 104 |
98 // Sets the number of extension icons that should be visible. | 105 // Sets the number of extension icons that should be visible. |
99 // If count == size(), this will set the visible icon count to -1, meaning | 106 // If count == size(), this will set the visible icon count to -1, meaning |
100 // "show all actions". | 107 // "show all actions". |
101 void SetVisibleIconCount(int count); | 108 void SetVisibleIconCount(size_t count); |
102 | 109 |
103 // As above, a return value of -1 represents "show all actions". | 110 // Returns the number of visible icons. |
104 int GetVisibleIconCount() const { return visible_icon_count_; } | 111 size_t visible_icon_count() const { |
112 return visible_icon_count_ == -1 ? | |
113 toolbar_items().size() : static_cast<size_t>(visible_icon_count_); | |
Peter Kasting
2014/10/31 19:01:08
Nit: So... now that we have both these accessors,
Devlin
2014/10/31 20:49:30
Yeah, I was thinking the same thing, but I'll post
| |
114 } | |
115 | |
116 bool all_icons_visible() const { return visible_icon_count_ == -1; } | |
105 | 117 |
106 bool extensions_initialized() const { return extensions_initialized_; } | 118 bool extensions_initialized() const { return extensions_initialized_; } |
107 | 119 |
108 const ExtensionList& toolbar_items() const { | 120 const ExtensionList& toolbar_items() const { |
109 return is_highlighting_ ? highlighted_items_ : toolbar_items_; | 121 return is_highlighting_ ? highlighted_items_ : toolbar_items_; |
110 } | 122 } |
111 | 123 |
112 bool is_highlighting() const { return is_highlighting_; } | 124 bool is_highlighting() const { return is_highlighting_; } |
113 | 125 |
114 void OnExtensionToolbarPrefChange(); | 126 void OnExtensionToolbarPrefChange(); |
115 | 127 |
128 // Returns the item order for a given tab. This can be different from the | |
129 // base item order if the action wants to run on the given page, and needs to | |
130 // be popped out of overflow. | |
131 ExtensionList GetItemOrderForTab(content::WebContents* web_contents) const; | |
132 | |
133 // Returns the visible icon count for a given tab. This can be different from | |
134 // the base item order if the action wants to run on the given page and needs | |
135 // to be popped out of overflow. | |
136 size_t GetVisibleIconCountForTab(content::WebContents* web_contents) const; | |
137 | |
116 // Finds the Observer associated with |browser| and tells it to display a | 138 // Finds the Observer associated with |browser| and tells it to display a |
117 // popup for the given |extension|. If |grant_active_tab| is true, this | 139 // popup for the given |extension|. If |grant_active_tab| is true, this |
118 // grants active tab permissions to the |extension|; only do this because of | 140 // grants active tab permissions to the |extension|; only do this because of |
119 // a direct user action. | 141 // a direct user action. |
120 bool ShowExtensionActionPopup(const Extension* extension, | 142 bool ShowExtensionActionPopup(const Extension* extension, |
121 Browser* browser, | 143 Browser* browser, |
122 bool grant_active_tab); | 144 bool grant_active_tab); |
123 | 145 |
124 // Ensures that the extensions in the |extension_ids| list are visible on the | 146 // Ensures that the extensions in the |extension_ids| list are visible on the |
125 // toolbar. This might mean they need to be moved to the front (if they are in | 147 // toolbar. This might mean they need to be moved to the front (if they are in |
126 // the overflow bucket). | 148 // the overflow bucket). |
127 void EnsureVisibility(const ExtensionIdList& extension_ids); | 149 void EnsureVisibility(const ExtensionIdList& extension_ids); |
128 | 150 |
129 // Highlight the extensions specified by |extension_ids|. This will cause | 151 // Highlights the extensions specified by |extension_ids|. This will cause |
130 // the ToolbarModel to only display those extensions. | 152 // the ToolbarModel to only display those extensions. |
131 // Highlighting mode is only entered if there is at least one extension to | 153 // Highlighting mode is only entered if there is at least one extension to |
132 // be shown. | 154 // be shown. |
133 // Returns true if highlighting mode is entered, false otherwise. | 155 // Returns true if highlighting mode is entered, false otherwise. |
134 bool HighlightExtensions(const ExtensionIdList& extension_ids); | 156 bool HighlightExtensions(const ExtensionIdList& extension_ids); |
135 | 157 |
136 // Stop highlighting extensions. All extensions can be shown again, and the | 158 // Stop highlighting extensions. All extensions can be shown again, and the |
137 // number of visible icons will be reset to what it was before highlighting. | 159 // number of visible icons will be reset to what it was before highlighting. |
138 void StopHighlighting(); | 160 void StopHighlighting(); |
139 | 161 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 // if we are exiting highlight mode due to no longer having highlighted items. | 244 // if we are exiting highlight mode due to no longer having highlighted items. |
223 bool is_highlighting_; | 245 bool is_highlighting_; |
224 | 246 |
225 // The number of icons which were visible before highlighting a subset, in | 247 // The number of icons which were visible before highlighting a subset, in |
226 // order to restore the count when finished. | 248 // order to restore the count when finished. |
227 int old_visible_icon_count_; | 249 int old_visible_icon_count_; |
228 | 250 |
229 ExtensionIdList last_known_positions_; | 251 ExtensionIdList last_known_positions_; |
230 | 252 |
231 // The number of icons visible (the rest should be hidden in the overflow | 253 // The number of icons visible (the rest should be hidden in the overflow |
232 // chevron). | 254 // chevron). A value of -1 indicates that all icons should be visible. |
233 int visible_icon_count_; | 255 int visible_icon_count_; |
234 | 256 |
235 content::NotificationRegistrar registrar_; | 257 content::NotificationRegistrar registrar_; |
236 | 258 |
237 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> | 259 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> |
238 extension_action_observer_; | 260 extension_action_observer_; |
239 | 261 |
240 // Listen to extension load, unloaded notifications. | 262 // Listen to extension load, unloaded notifications. |
241 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 263 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
242 extension_registry_observer_; | 264 extension_registry_observer_; |
243 | 265 |
244 // For observing change of toolbar order preference by external entity (sync). | 266 // For observing change of toolbar order preference by external entity (sync). |
245 PrefChangeRegistrar pref_change_registrar_; | 267 PrefChangeRegistrar pref_change_registrar_; |
246 base::Closure pref_change_callback_; | 268 base::Closure pref_change_callback_; |
247 | 269 |
248 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; | 270 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; |
249 | 271 |
250 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); | 272 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); |
251 }; | 273 }; |
252 | 274 |
253 } // namespace extensions | 275 } // namespace extensions |
254 | 276 |
255 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ | 277 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ |
OLD | NEW |