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 size_t visible_icon_count() const { |
104 int GetVisibleIconCount() const { return visible_icon_count_; } | 111 return visible_icon_count_ == -1 ? |
| 112 toolbar_items().size() : static_cast<size_t>(visible_icon_count_); |
| 113 } |
| 114 |
| 115 bool all_icons_visible() const { return visible_icon_count_ == -1; } |
105 | 116 |
106 bool extensions_initialized() const { return extensions_initialized_; } | 117 bool extensions_initialized() const { return extensions_initialized_; } |
107 | 118 |
108 const ExtensionList& toolbar_items() const { | 119 const ExtensionList& toolbar_items() const { |
109 return is_highlighting_ ? highlighted_items_ : toolbar_items_; | 120 return is_highlighting_ ? highlighted_items_ : toolbar_items_; |
110 } | 121 } |
111 | 122 |
112 bool is_highlighting() const { return is_highlighting_; } | 123 bool is_highlighting() const { return is_highlighting_; } |
113 | 124 |
114 void OnExtensionToolbarPrefChange(); | 125 void OnExtensionToolbarPrefChange(); |
115 | 126 |
| 127 // Returns the item order for a given tab. This can be different from the |
| 128 // base item order if the action wants to run on the given page, and needs to |
| 129 // be popped out of overflow. |
| 130 ExtensionList GetItemOrderForTab(content::WebContents* web_contents) const; |
| 131 |
| 132 // Returns the visible icon count for a given tab. This can be different from |
| 133 // the base item order if the action wants to run on the given page and needs |
| 134 // to be popped out of overflow. |
| 135 size_t GetVisibleIconCountForTab(content::WebContents* web_contents) const; |
| 136 |
116 // Finds the Observer associated with |browser| and tells it to display a | 137 // 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 | 138 // 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 | 139 // grants active tab permissions to the |extension|; only do this because of |
119 // a direct user action. | 140 // a direct user action. |
120 bool ShowExtensionActionPopup(const Extension* extension, | 141 bool ShowExtensionActionPopup(const Extension* extension, |
121 Browser* browser, | 142 Browser* browser, |
122 bool grant_active_tab); | 143 bool grant_active_tab); |
123 | 144 |
124 // Ensures that the extensions in the |extension_ids| list are visible on the | 145 // 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 | 146 // toolbar. This might mean they need to be moved to the front (if they are in |
126 // the overflow bucket). | 147 // the overflow bucket). |
127 void EnsureVisibility(const ExtensionIdList& extension_ids); | 148 void EnsureVisibility(const ExtensionIdList& extension_ids); |
128 | 149 |
129 // Highlight the extensions specified by |extension_ids|. This will cause | 150 // Highlights the extensions specified by |extension_ids|. This will cause |
130 // the ToolbarModel to only display those extensions. | 151 // the ToolbarModel to only display those extensions. |
131 // Highlighting mode is only entered if there is at least one extension to | 152 // Highlighting mode is only entered if there is at least one extension to |
132 // be shown. | 153 // be shown. |
133 // Returns true if highlighting mode is entered, false otherwise. | 154 // Returns true if highlighting mode is entered, false otherwise. |
134 bool HighlightExtensions(const ExtensionIdList& extension_ids); | 155 bool HighlightExtensions(const ExtensionIdList& extension_ids); |
135 | 156 |
136 // Stop highlighting extensions. All extensions can be shown again, and the | 157 // 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. | 158 // number of visible icons will be reset to what it was before highlighting. |
138 void StopHighlighting(); | 159 void StopHighlighting(); |
139 | 160 |
(...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. | 243 // if we are exiting highlight mode due to no longer having highlighted items. |
223 bool is_highlighting_; | 244 bool is_highlighting_; |
224 | 245 |
225 // The number of icons which were visible before highlighting a subset, in | 246 // The number of icons which were visible before highlighting a subset, in |
226 // order to restore the count when finished. | 247 // order to restore the count when finished. |
227 int old_visible_icon_count_; | 248 int old_visible_icon_count_; |
228 | 249 |
229 ExtensionIdList last_known_positions_; | 250 ExtensionIdList last_known_positions_; |
230 | 251 |
231 // The number of icons visible (the rest should be hidden in the overflow | 252 // The number of icons visible (the rest should be hidden in the overflow |
232 // chevron). | 253 // chevron). A value of -1 indicates that all icons should be visible. |
| 254 // TODO(devlin): Make a new variable to indicate that all icons should be |
| 255 // visible, instead of overloading this one. |
233 int visible_icon_count_; | 256 int visible_icon_count_; |
234 | 257 |
235 content::NotificationRegistrar registrar_; | 258 content::NotificationRegistrar registrar_; |
236 | 259 |
237 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> | 260 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> |
238 extension_action_observer_; | 261 extension_action_observer_; |
239 | 262 |
240 // Listen to extension load, unloaded notifications. | 263 // Listen to extension load, unloaded notifications. |
241 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 264 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
242 extension_registry_observer_; | 265 extension_registry_observer_; |
243 | 266 |
244 // For observing change of toolbar order preference by external entity (sync). | 267 // For observing change of toolbar order preference by external entity (sync). |
245 PrefChangeRegistrar pref_change_registrar_; | 268 PrefChangeRegistrar pref_change_registrar_; |
246 base::Closure pref_change_callback_; | 269 base::Closure pref_change_callback_; |
247 | 270 |
248 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; | 271 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; |
249 | 272 |
250 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); | 273 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); |
251 }; | 274 }; |
252 | 275 |
253 } // namespace extensions | 276 } // namespace extensions |
254 | 277 |
255 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ | 278 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ |
OLD | NEW |