Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / extensions / extension_toolbar_model.h
blobc9d975df632a0fcec5ea65d212e1b2c6d9294e15
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 virtual ~ExtensionToolbarModel();
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 Extension* extension, 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 virtual 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 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
151 const Extension* extension) override;
152 virtual void OnExtensionUnloaded(
153 content::BrowserContext* browser_context,
154 const Extension* extension,
155 UnloadedExtensionInfo::Reason reason) override;
156 virtual void OnExtensionUninstalled(
157 content::BrowserContext* browser_context,
158 const Extension* extension,
159 extensions::UninstallReason reason) override;
161 // ExtensionActionAPI::Observer:
162 virtual void OnExtensionActionUpdated(
163 ExtensionAction* extension_action,
164 content::WebContents* web_contents,
165 content::BrowserContext* browser_context) override;
167 // To be called after the extension service is ready; gets loaded extensions
168 // from the ExtensionRegistry and their saved order from the pref service
169 // and constructs |toolbar_items_| from these data. IncognitoPopulate()
170 // takes the shortcut - looking at the regular model's content and modifying
171 // it.
172 void InitializeExtensionList();
173 void Populate(const ExtensionIdList& positions);
174 void IncognitoPopulate();
176 // Save the model to prefs.
177 void UpdatePrefs();
179 // Updates |extension|'s browser action visibility pref if the browser action
180 // is in the overflow menu and should be considered hidden.
181 void MaybeUpdateVisibilityPref(const Extension* extension, int index);
183 // Calls MaybeUpdateVisibilityPref() for each extension in |toolbar_items|.
184 void MaybeUpdateVisibilityPrefs();
186 // Finds the last known visible position of the icon for an |extension|. The
187 // value returned is a zero-based index into the vector of visible items.
188 size_t FindNewPositionFromLastKnownGood(const Extension* extension);
190 // Returns true if the given |extension| should be added to the toolbar.
191 bool ShouldAddExtension(const Extension* extension);
193 // Adds or removes the given |extension| from the toolbar model.
194 void AddExtension(const Extension* extension);
195 void RemoveExtension(const Extension* extension);
197 // Removes all current items (because we're going to [re]Populate()).
198 void ClearItems();
200 // Our observers.
201 ObserverList<Observer> observers_;
203 // The Profile this toolbar model is for.
204 Profile* profile_;
206 ExtensionPrefs* extension_prefs_;
207 PrefService* prefs_;
209 // True if we've handled the initial EXTENSIONS_READY notification.
210 bool extensions_initialized_;
212 // If true, we include all extensions in the toolbar model. If false, we only
213 // include browser actions.
214 bool include_all_extensions_;
216 // Ordered list of browser action buttons.
217 ExtensionList toolbar_items_;
219 // List of browser action buttons which should be highlighted.
220 ExtensionList highlighted_items_;
222 // Indication whether or not we are currently in highlight mode; typically,
223 // this is equivalent to !highlighted_items_.empty(), but can be different
224 // if we are exiting highlight mode due to no longer having highlighted items.
225 bool is_highlighting_;
227 // The number of icons which were visible before highlighting a subset, in
228 // order to restore the count when finished.
229 int old_visible_icon_count_;
231 ExtensionIdList last_known_positions_;
233 // The number of icons visible (the rest should be hidden in the overflow
234 // chevron).
235 int visible_icon_count_;
237 content::NotificationRegistrar registrar_;
239 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer>
240 extension_action_observer_;
242 // Listen to extension load, unloaded notifications.
243 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
244 extension_registry_observer_;
246 // For observing change of toolbar order preference by external entity (sync).
247 PrefChangeRegistrar pref_change_registrar_;
248 base::Closure pref_change_callback_;
250 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_;
252 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel);
255 } // namespace extensions
257 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_