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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_
8 #include "base/compiler_specific.h"
9 #include "base/observer_list.h"
10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/scoped_observer.h"
12 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
13 #include "chrome/browser/extensions/extension_action.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/extension_registry_observer.h"
17 #include "extensions/common/extension.h"
23 namespace extensions
{
24 class ExtensionRegistry
;
27 // Model for the browser actions toolbar.
28 class ExtensionToolbarModel
: public ExtensionActionAPI::Observer
,
29 public ExtensionRegistryObserver
,
32 ExtensionToolbarModel(Profile
* profile
, ExtensionPrefs
* extension_prefs
);
33 ~ExtensionToolbarModel() override
;
35 // A class which is informed of changes to the model; represents the view of
36 // MVC. Also used for signaling view changes such as showing extension popups.
37 // TODO(devlin): Should this really be an observer? It acts more like a
41 // Signals that an |extension| has been added to the toolbar at |index|.
42 // This will *only* be called after the toolbar model has been initialized.
43 virtual void OnToolbarExtensionAdded(const Extension
* extension
,
46 // Signals that the given |extension| has been removed from the toolbar.
47 virtual void OnToolbarExtensionRemoved(const Extension
* extension
) = 0;
49 // Signals that the given |extension| has been moved to |index|. |index| is
50 // the desired *final* index of the extension (that is, in the adjusted
51 // order, extension should be at |index|).
52 virtual void OnToolbarExtensionMoved(const Extension
* extension
,
55 // Signals that the browser action for the given |extension| has been
57 virtual void OnToolbarExtensionUpdated(const Extension
* extension
) = 0;
59 // Signals the |extension| to show the popup now in the active window.
60 // If |grant_active_tab| is true, then active tab permissions should be
61 // given to the extension (only do this if this is through a user action).
62 // Returns true if a popup was slated to be shown.
63 virtual bool ShowExtensionActionPopup(const Extension
* extension
,
64 bool grant_active_tab
) = 0;
66 // Signals when the container needs to be redrawn because of a size change,
67 // and when the model has finished loading.
68 virtual void OnToolbarVisibleCountChanged() = 0;
70 // Signals that the model has entered or exited highlighting mode, or that
71 // the extensions being highlighted have (probably*) changed. Highlighting
72 // mode indicates that only a subset of the extensions are actively
73 // displayed, and those extensions should be highlighted for extra emphasis.
74 // * probably, because if we are in highlight mode and receive a call to
75 // highlight a new set of extensions, we do not compare the current set
76 // with the new set (and just assume the new set is different).
77 virtual void OnToolbarHighlightModeChanged(bool is_highlighting
) = 0;
79 // Signals that the toolbar model has been initialized, so that if any
80 // observers were postponing animation during the initialization stage, they
82 virtual void OnToolbarModelInitialized() = 0;
84 // Returns the browser associated with the Observer.
85 virtual Browser
* GetBrowser() = 0;
88 virtual ~Observer() {}
91 // Convenience function to get the ExtensionToolbarModel for a Profile.
92 static ExtensionToolbarModel
* Get(Profile
* profile
);
94 // Adds or removes an observer.
95 void AddObserver(Observer
* observer
);
96 void RemoveObserver(Observer
* observer
);
98 // Moves the given |extension|'s icon to the given |index|.
99 void MoveExtensionIcon(const std::string
& id
, size_t index
);
101 // Sets the number of extension icons that should be visible.
102 // If count == size(), this will set the visible icon count to -1, meaning
103 // "show all actions".
104 void SetVisibleIconCount(size_t count
);
106 size_t visible_icon_count() const {
107 // We have guards around this because |visible_icon_count_| can be set by
108 // prefs/sync, and we want to ensure that the icon count returned is within
110 return visible_icon_count_
== -1 ?
111 toolbar_items().size() :
112 std::min(static_cast<size_t>(visible_icon_count_
),
113 toolbar_items().size());
116 bool all_icons_visible() const { return visible_icon_count_
== -1; }
118 bool extensions_initialized() const { return extensions_initialized_
; }
120 const ExtensionList
& toolbar_items() const {
121 return is_highlighting_
? highlighted_items_
: toolbar_items_
;
124 bool is_highlighting() const { return is_highlighting_
; }
126 void OnExtensionToolbarPrefChange();
128 // Returns the index of the given |id|, or -1 if the id wasn't found.
129 int GetIndexForId(const std::string
& id
) const;
131 // Finds the Observer associated with |browser| and tells it to display a
132 // popup for the given |extension|. If |grant_active_tab| is true, this
133 // grants active tab permissions to the |extension|; only do this because of
134 // a direct user action.
135 bool ShowExtensionActionPopup(const Extension
* extension
,
137 bool grant_active_tab
);
139 // Ensures that the extensions in the |extension_ids| list are visible on the
140 // toolbar. This might mean they need to be moved to the front (if they are in
141 // the overflow bucket).
142 void EnsureVisibility(const ExtensionIdList
& extension_ids
);
144 // Highlights the extensions specified by |extension_ids|. This will cause
145 // the ToolbarModel to only display those extensions.
146 // Highlighting mode is only entered if there is at least one extension to
148 // Returns true if highlighting mode is entered, false otherwise.
149 bool HighlightExtensions(const ExtensionIdList
& extension_ids
);
151 // Stop highlighting extensions. All extensions can be shown again, and the
152 // number of visible icons will be reset to what it was before highlighting.
153 void StopHighlighting();
155 // Returns true if the toolbar model is running with the redesign and is
156 // showing new icons as a result.
157 bool RedesignIsShowingNewIcons() const;
160 // Callback when extensions are ready.
163 // ExtensionRegistryObserver:
164 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
165 const Extension
* extension
) override
;
166 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
167 const Extension
* extension
,
168 UnloadedExtensionInfo::Reason reason
) override
;
169 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
170 const Extension
* extension
,
171 extensions::UninstallReason reason
) override
;
173 // ExtensionActionAPI::Observer:
174 void OnExtensionActionUpdated(
175 ExtensionAction
* extension_action
,
176 content::WebContents
* web_contents
,
177 content::BrowserContext
* browser_context
) override
;
178 void OnExtensionActionVisibilityChanged(const std::string
& extension_id
,
179 bool is_now_visible
) override
;
181 // To be called after the extension service is ready; gets loaded extensions
182 // from the ExtensionRegistry and their saved order from the pref service
183 // and constructs |toolbar_items_| from these data. IncognitoPopulate()
184 // takes the shortcut - looking at the regular model's content and modifying
186 void InitializeExtensionList();
187 void Populate(ExtensionIdList
* positions
);
188 void IncognitoPopulate();
190 // Save the model to prefs.
193 // Updates |extension|'s browser action visibility pref if the browser action
194 // is in the overflow menu and should be considered hidden.
195 void MaybeUpdateVisibilityPref(const Extension
* extension
, size_t index
);
197 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|.
198 void MaybeUpdateVisibilityPrefs();
200 // Finds the last known visible position of the icon for an |extension|. The
201 // value returned is a zero-based index into the vector of visible items.
202 size_t FindNewPositionFromLastKnownGood(const Extension
* extension
);
204 // Returns true if the given |extension| should be added to the toolbar.
205 bool ShouldAddExtension(const Extension
* extension
);
207 // Adds or removes the given |extension| from the toolbar model.
208 void AddExtension(const Extension
* extension
);
209 void RemoveExtension(const Extension
* extension
);
212 base::ObserverList
<Observer
> observers_
;
214 // The Profile this toolbar model is for.
217 ExtensionPrefs
* extension_prefs_
;
220 // The ExtensionActionAPI object, cached for convenience.
221 ExtensionActionAPI
* extension_action_api_
;
223 // True if we've handled the initial EXTENSIONS_READY notification.
224 bool extensions_initialized_
;
226 // If true, we include all extensions in the toolbar model. If false, we only
227 // include browser actions.
228 bool include_all_extensions_
;
230 // Ordered list of browser action buttons.
231 ExtensionList toolbar_items_
;
233 // List of browser action buttons which should be highlighted.
234 ExtensionList highlighted_items_
;
236 // Indication whether or not we are currently in highlight mode; typically,
237 // this is equivalent to !highlighted_items_.empty(), but can be different
238 // if we are exiting highlight mode due to no longer having highlighted items.
239 bool is_highlighting_
;
241 // The number of icons which were visible before highlighting a subset, in
242 // order to restore the count when finished.
243 int old_visible_icon_count_
;
245 ExtensionIdList last_known_positions_
;
247 // The number of icons visible (the rest should be hidden in the overflow
248 // chevron). A value of -1 indicates that all icons should be visible.
249 // Instead of using this variable directly, use visible_icon_count() if
251 // TODO(devlin): Make a new variable to indicate that all icons should be
252 // visible, instead of overloading this one.
253 int visible_icon_count_
;
255 ScopedObserver
<ExtensionActionAPI
, ExtensionActionAPI::Observer
>
256 extension_action_observer_
;
258 // Listen to extension load, unloaded notifications.
259 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
260 extension_registry_observer_
;
262 // For observing change of toolbar order preference by external entity (sync).
263 PrefChangeRegistrar pref_change_registrar_
;
264 base::Closure pref_change_callback_
;
266 base::WeakPtrFactory
<ExtensionToolbarModel
> weak_ptr_factory_
;
268 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel
);
271 } // namespace extensions
273 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_