MacViews: Get c/b/ui/views/tabs to build on Mac
[chromium-blink-merge.git] / chrome / browser / extensions / extension_toolbar_model.h
blob950366e7768daa6bb3cbb5b9f9d845ef54b30d6e
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"
21 class Browser;
22 class PrefService;
23 class Profile;
25 namespace extensions {
26 class ExtensionRegistry;
27 class ExtensionSet;
29 // Model for the browser actions toolbar.
30 class ExtensionToolbarModel : public content::NotificationObserver,
31 public ExtensionActionAPI::Observer,
32 public ExtensionRegistryObserver,
33 public KeyedService {
34 public:
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
41 // delegate.
42 class Observer {
43 public:
44 // An extension has been added to the toolbar and should go at |index|.
45 virtual void ToolbarExtensionAdded(const Extension* extension,
46 int index) = 0;
48 // The given |extension| should be removed from the toolbar.
49 virtual void ToolbarExtensionRemoved(const Extension* extension) = 0;
51 // The given |extension| has been moved to |index|. |index| is the desired
52 // *final* index of the extension (that is, in the adjusted order, extension
53 // should be at |index|).
54 virtual void ToolbarExtensionMoved(const Extension* extension,
55 int index) = 0;
57 // Signals that the browser action for the given |extension| has been
58 // updated.
59 virtual void ToolbarExtensionUpdated(const Extension* extension) = 0;
61 // Signal 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 // 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 virtual bool ShowExtensionActionPopup(const Extension* extension,
66 bool grant_active_tab) = 0;
68 // Signal when the container needs to be redrawn because of a size change,
69 // and when the model has finished loading.
70 virtual void ToolbarVisibleCountChanged() = 0;
72 // Signal that the model has entered or exited highlighting mode, or that
73 // the extensions being highlighted have (probably*) changed. Highlighting
74 // mode indicates that only a subset of the extensions are actively
75 // 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 // 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 virtual void ToolbarHighlightModeChanged(bool is_highlighting) = 0;
81 // Returns the browser associated with the Observer.
82 virtual Browser* GetBrowser() = 0;
84 protected:
85 virtual ~Observer() {}
88 // Convenience function to get the ExtensionToolbarModel for a Profile.
89 static ExtensionToolbarModel* Get(Profile* profile);
91 // Add or remove an observer.
92 void AddObserver(Observer* observer);
93 void RemoveObserver(Observer* observer);
95 // Moves the given |extension|'s icon to the given |index|.
96 void MoveExtensionIcon(const std::string& id, size_t index);
98 // Sets the number of extension icons that should be visible.
99 // If count == size(), this will set the visible icon count to -1, meaning
100 // "show all actions".
101 void SetVisibleIconCount(int count);
103 // As above, a return value of -1 represents "show all actions".
104 int GetVisibleIconCount() const { return visible_icon_count_; }
106 bool extensions_initialized() const { return extensions_initialized_; }
108 const ExtensionList& toolbar_items() const {
109 return is_highlighting_ ? highlighted_items_ : toolbar_items_;
112 bool is_highlighting() const { return is_highlighting_; }
114 void OnExtensionToolbarPrefChange();
116 // 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
118 // grants active tab permissions to the |extension|; only do this because of
119 // a direct user action.
120 bool ShowExtensionActionPopup(const Extension* extension,
121 Browser* browser,
122 bool grant_active_tab);
124 // 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
126 // the overflow bucket).
127 void EnsureVisibility(const ExtensionIdList& extension_ids);
129 // Highlight the extensions specified by |extension_ids|. This will cause
130 // the ToolbarModel to only display those extensions.
131 // Highlighting mode is only entered if there is at least one extension to
132 // be shown.
133 // Returns true if highlighting mode is entered, false otherwise.
134 bool HighlightExtensions(const ExtensionIdList& extension_ids);
136 // 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.
138 void StopHighlighting();
140 private:
141 // content::NotificationObserver:
142 void Observe(int type,
143 const content::NotificationSource& source,
144 const content::NotificationDetails& details) override;
146 // Callback when extensions are ready.
147 void OnReady();
149 // ExtensionRegistryObserver:
150 void OnExtensionLoaded(content::BrowserContext* browser_context,
151 const Extension* extension) override;
152 void OnExtensionUnloaded(content::BrowserContext* browser_context,
153 const Extension* extension,
154 UnloadedExtensionInfo::Reason reason) override;
155 void OnExtensionUninstalled(content::BrowserContext* browser_context,
156 const Extension* extension,
157 extensions::UninstallReason reason) override;
159 // ExtensionActionAPI::Observer:
160 void OnExtensionActionUpdated(
161 ExtensionAction* extension_action,
162 content::WebContents* web_contents,
163 content::BrowserContext* browser_context) override;
165 // To be called after the extension service is ready; gets loaded extensions
166 // from the ExtensionRegistry and their saved order from the pref service
167 // and constructs |toolbar_items_| from these data. IncognitoPopulate()
168 // takes the shortcut - looking at the regular model's content and modifying
169 // it.
170 void InitializeExtensionList();
171 void Populate(const ExtensionIdList& positions);
172 void IncognitoPopulate();
174 // Save the model to prefs.
175 void UpdatePrefs();
177 // Updates |extension|'s browser action visibility pref if the browser action
178 // is in the overflow menu and should be considered hidden.
179 void MaybeUpdateVisibilityPref(const Extension* extension, int index);
181 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|.
182 void MaybeUpdateVisibilityPrefs();
184 // Finds the last known visible position of the icon for an |extension|. The
185 // value returned is a zero-based index into the vector of visible items.
186 size_t FindNewPositionFromLastKnownGood(const Extension* extension);
188 // Returns true if the given |extension| should be added to the toolbar.
189 bool ShouldAddExtension(const Extension* extension);
191 // Adds or removes the given |extension| from the toolbar model.
192 void AddExtension(const Extension* extension);
193 void RemoveExtension(const Extension* extension);
195 // Removes all current items (because we're going to [re]Populate()).
196 void ClearItems();
198 // Our observers.
199 ObserverList<Observer> observers_;
201 // The Profile this toolbar model is for.
202 Profile* profile_;
204 ExtensionPrefs* extension_prefs_;
205 PrefService* prefs_;
207 // True if we've handled the initial EXTENSIONS_READY notification.
208 bool extensions_initialized_;
210 // If true, we include all extensions in the toolbar model. If false, we only
211 // include browser actions.
212 bool include_all_extensions_;
214 // Ordered list of browser action buttons.
215 ExtensionList toolbar_items_;
217 // List of browser action buttons which should be highlighted.
218 ExtensionList highlighted_items_;
220 // Indication whether or not we are currently in highlight mode; typically,
221 // this is equivalent to !highlighted_items_.empty(), but can be different
222 // if we are exiting highlight mode due to no longer having highlighted items.
223 bool is_highlighting_;
225 // The number of icons which were visible before highlighting a subset, in
226 // order to restore the count when finished.
227 int old_visible_icon_count_;
229 ExtensionIdList last_known_positions_;
231 // The number of icons visible (the rest should be hidden in the overflow
232 // chevron).
233 int visible_icon_count_;
235 content::NotificationRegistrar registrar_;
237 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer>
238 extension_action_observer_;
240 // Listen to extension load, unloaded notifications.
241 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
242 extension_registry_observer_;
244 // For observing change of toolbar order preference by external entity (sync).
245 PrefChangeRegistrar pref_change_registrar_;
246 base::Closure pref_change_callback_;
248 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_;
250 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel);
253 } // namespace extensions
255 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_