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 "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/browser/extension_prefs.h"
18 #include "extensions/browser/extension_registry_observer.h"
19 #include "extensions/common/extension.h"
25 namespace extensions
{
26 class ExtensionRegistry
;
29 // Model for the browser actions toolbar.
30 class ExtensionToolbarModel
: public content::NotificationObserver
,
31 public ExtensionActionAPI::Observer
,
32 public ExtensionRegistryObserver
,
35 ExtensionToolbarModel(Profile
* profile
, ExtensionPrefs
* extension_prefs
);
36 ~ExtensionToolbarModel() override
;
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.
40 // TODO(devlin): Should this really be an observer? It acts more like a
44 // Signals that an |extension| has been added to the toolbar at |index|.
45 // This will *only* be called after the toolbar model has been initialized.
46 virtual void OnToolbarExtensionAdded(const Extension
* extension
,
49 // Signals that the given |extension| has been removed from the toolbar.
50 virtual void OnToolbarExtensionRemoved(const Extension
* extension
) = 0;
52 // 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 // order, extension should be at |index|).
55 virtual void OnToolbarExtensionMoved(const Extension
* extension
,
58 // Signals that the browser action for the given |extension| has been
60 virtual void OnToolbarExtensionUpdated(const Extension
* extension
) = 0;
62 // Signals the |extension| to show the popup now in the active window.
63 // If |grant_active_tab| is true, then active tab permissions should be
64 // given to the extension (only do this if this is through a user action).
65 // Returns true if a popup was slated to be shown.
66 virtual bool ShowExtensionActionPopup(const Extension
* extension
,
67 bool grant_active_tab
) = 0;
69 // Signals when the container needs to be redrawn because of a size change,
70 // and when the model has finished loading.
71 virtual void OnToolbarVisibleCountChanged() = 0;
73 // Signals that the model has entered or exited highlighting mode, or that
74 // the extensions being highlighted have (probably*) changed. Highlighting
75 // mode indicates that only a subset of the extensions are actively
76 // 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 // 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 virtual void OnToolbarHighlightModeChanged(bool is_highlighting
) = 0;
82 // Signals that the toolbar model has been initialized, so that if any
83 // observers were postponing animation during the initialization stage, they
85 virtual void OnToolbarModelInitialized() = 0;
87 // Returns the browser associated with the Observer.
88 virtual Browser
* GetBrowser() = 0;
91 virtual ~Observer() {}
94 // Convenience function to get the ExtensionToolbarModel for a Profile.
95 static ExtensionToolbarModel
* Get(Profile
* profile
);
97 // Adds or removes an observer.
98 void AddObserver(Observer
* observer
);
99 void RemoveObserver(Observer
* observer
);
101 // Moves the given |extension|'s icon to the given |index|.
102 void MoveExtensionIcon(const std::string
& id
, size_t index
);
104 // Sets the number of extension icons that should be visible.
105 // If count == size(), this will set the visible icon count to -1, meaning
106 // "show all actions".
107 void SetVisibleIconCount(size_t count
);
109 size_t visible_icon_count() const {
110 // We have guards around this because |visible_icon_count_| can be set by
111 // prefs/sync, and we want to ensure that the icon count returned is within
113 return visible_icon_count_
== -1 ?
114 toolbar_items().size() :
115 std::min(static_cast<size_t>(visible_icon_count_
),
116 toolbar_items().size());
119 bool all_icons_visible() const { return visible_icon_count_
== -1; }
121 bool extensions_initialized() const { return extensions_initialized_
; }
123 const ExtensionList
& toolbar_items() const {
124 return is_highlighting_
? highlighted_items_
: toolbar_items_
;
127 bool is_highlighting() const { return is_highlighting_
; }
129 void OnExtensionToolbarPrefChange();
131 // Returns the index of the given |id|, or -1 if the id wasn't found.
132 int GetIndexForId(const std::string
& id
) const;
134 // Finds the Observer associated with |browser| and tells it to display a
135 // popup for the given |extension|. If |grant_active_tab| is true, this
136 // grants active tab permissions to the |extension|; only do this because of
137 // a direct user action.
138 bool ShowExtensionActionPopup(const Extension
* extension
,
140 bool grant_active_tab
);
142 // Ensures that the extensions in the |extension_ids| list are visible on the
143 // toolbar. This might mean they need to be moved to the front (if they are in
144 // the overflow bucket).
145 void EnsureVisibility(const ExtensionIdList
& extension_ids
);
147 // Highlights the extensions specified by |extension_ids|. This will cause
148 // the ToolbarModel to only display those extensions.
149 // Highlighting mode is only entered if there is at least one extension to
151 // Returns true if highlighting mode is entered, false otherwise.
152 bool HighlightExtensions(const ExtensionIdList
& extension_ids
);
154 // Stop highlighting extensions. All extensions can be shown again, and the
155 // number of visible icons will be reset to what it was before highlighting.
156 void StopHighlighting();
158 // Returns true if the toolbar model is running with the redesign and is
159 // showing new icons as a result.
160 bool RedesignIsShowingNewIcons() const;
163 // content::NotificationObserver:
164 void Observe(int type
,
165 const content::NotificationSource
& source
,
166 const content::NotificationDetails
& details
) override
;
168 // Callback when extensions are ready.
171 // ExtensionRegistryObserver:
172 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
173 const Extension
* extension
) override
;
174 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
175 const Extension
* extension
,
176 UnloadedExtensionInfo::Reason reason
) override
;
177 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
178 const Extension
* extension
,
179 extensions::UninstallReason reason
) override
;
181 // ExtensionActionAPI::Observer:
182 void OnExtensionActionUpdated(
183 ExtensionAction
* extension_action
,
184 content::WebContents
* web_contents
,
185 content::BrowserContext
* browser_context
) override
;
187 // To be called after the extension service is ready; gets loaded extensions
188 // from the ExtensionRegistry and their saved order from the pref service
189 // and constructs |toolbar_items_| from these data. IncognitoPopulate()
190 // takes the shortcut - looking at the regular model's content and modifying
192 void InitializeExtensionList();
193 void Populate(ExtensionIdList
* positions
);
194 void IncognitoPopulate();
196 // Save the model to prefs.
199 // Updates |extension|'s browser action visibility pref if the browser action
200 // is in the overflow menu and should be considered hidden.
201 void MaybeUpdateVisibilityPref(const Extension
* extension
, size_t index
);
203 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|.
204 void MaybeUpdateVisibilityPrefs();
206 // Finds the last known visible position of the icon for an |extension|. The
207 // value returned is a zero-based index into the vector of visible items.
208 size_t FindNewPositionFromLastKnownGood(const Extension
* extension
);
210 // Returns true if the given |extension| should be added to the toolbar.
211 bool ShouldAddExtension(const Extension
* extension
);
213 // Adds or removes the given |extension| from the toolbar model.
214 void AddExtension(const Extension
* extension
);
215 void RemoveExtension(const Extension
* extension
);
218 ObserverList
<Observer
> observers_
;
220 // The Profile this toolbar model is for.
223 ExtensionPrefs
* extension_prefs_
;
226 // True if we've handled the initial EXTENSIONS_READY notification.
227 bool extensions_initialized_
;
229 // If true, we include all extensions in the toolbar model. If false, we only
230 // include browser actions.
231 bool include_all_extensions_
;
233 // Ordered list of browser action buttons.
234 ExtensionList toolbar_items_
;
236 // List of browser action buttons which should be highlighted.
237 ExtensionList highlighted_items_
;
239 // Indication whether or not we are currently in highlight mode; typically,
240 // this is equivalent to !highlighted_items_.empty(), but can be different
241 // if we are exiting highlight mode due to no longer having highlighted items.
242 bool is_highlighting_
;
244 // The number of icons which were visible before highlighting a subset, in
245 // order to restore the count when finished.
246 int old_visible_icon_count_
;
248 ExtensionIdList last_known_positions_
;
250 // The number of icons visible (the rest should be hidden in the overflow
251 // chevron). A value of -1 indicates that all icons should be visible.
252 // Instead of using this variable directly, use visible_icon_count() if
254 // TODO(devlin): Make a new variable to indicate that all icons should be
255 // visible, instead of overloading this one.
256 int visible_icon_count_
;
258 content::NotificationRegistrar registrar_
;
260 ScopedObserver
<ExtensionActionAPI
, ExtensionActionAPI::Observer
>
261 extension_action_observer_
;
263 // Listen to extension load, unloaded notifications.
264 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
265 extension_registry_observer_
;
267 // For observing change of toolbar order preference by external entity (sync).
268 PrefChangeRegistrar pref_change_registrar_
;
269 base::Closure pref_change_callback_
;
271 base::WeakPtrFactory
<ExtensionToolbarModel
> weak_ptr_factory_
;
273 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel
);
276 } // namespace extensions
278 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_