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/extension_action.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_registry_observer.h"
18 #include "extensions/common/extension.h"
24 namespace extensions {
25 class ExtensionRegistry;
28 // Model for the browser actions toolbar.
29 class ExtensionToolbarModel : public content::NotificationObserver,
30 public ExtensionRegistryObserver,
33 ExtensionToolbarModel(Profile* profile, ExtensionPrefs* extension_prefs);
34 virtual ~ExtensionToolbarModel();
36 // A class which is informed of changes to the model; represents the view of
37 // MVC. Also used for signaling view changes such as showing extension popups.
40 // An extension with a browser action button has been added, and should go
41 // in the toolbar at |index|.
42 virtual void BrowserActionAdded(const Extension* extension, int index) {}
44 // The browser action button for |extension| should no longer show.
45 virtual void BrowserActionRemoved(const Extension* extension) {}
47 // The browser action button for |extension| has been moved to |index|.
48 virtual void BrowserActionMoved(const Extension* extension, int index) {}
50 // Signal the |extension| to show the popup now in the active window.
51 // Returns true if a popup was slated to be shown.
52 virtual bool BrowserActionShowPopup(const Extension* extension);
54 // Signal when the container needs to be redrawn because of a size change,
55 // and when the model has finished loading.
56 virtual void VisibleCountChanged() {}
58 // Signal that the model has entered or exited highlighting mode, or that
59 // the extensions being highlighted have (probably*) changed. Highlighting
60 // mode indicates that only a subset of the extensions are actively
61 // displayed, and those extensions should be highlighted for extra emphasis.
62 // * probably, because if we are in highlight mode and receive a call to
63 // highlight a new set of extensions, we do not compare the current set
64 // with the new set (and just assume the new set is different).
65 virtual void HighlightModeChanged(bool is_highlighting) {}
68 virtual ~Observer() {}
71 // Convenience function to get the ExtensionToolbarModel for a Profile.
72 static ExtensionToolbarModel* Get(Profile* profile);
74 // Functions called by the view.
75 void AddObserver(Observer* observer);
76 void RemoveObserver(Observer* observer);
77 void MoveBrowserAction(const Extension* extension, int index);
78 // Executes the browser action for an extension and returns the action that
79 // the UI should perform in response.
80 // |popup_url_out| will be set if the extension should show a popup, with
81 // the URL that should be shown, if non-NULL. |should_grant| controls whether
82 // the extension should be granted page tab permissions, which is what happens
83 // when the user clicks the browser action, but not, for example, when the
84 // showPopup API is called.
85 ExtensionAction::ShowAction ExecuteBrowserAction(const Extension* extension,
89 // If count == size(), this will set the visible icon count to -1, meaning
90 // "show all actions".
91 void SetVisibleIconCount(int count);
92 // As above, a return value of -1 represents "show all actions".
93 int GetVisibleIconCount() const { return visible_icon_count_; }
95 bool extensions_initialized() const { return extensions_initialized_; }
97 const ExtensionList& toolbar_items() const {
98 return is_highlighting_ ? highlighted_items_ : toolbar_items_;
101 bool is_highlighting() const { return is_highlighting_; }
103 // Utility functions for converting between an index into the list of
104 // incognito-enabled browser actions, and the list of all browser actions.
105 int IncognitoIndexToOriginal(int incognito_index);
106 int OriginalIndexToIncognito(int original_index);
108 void OnExtensionToolbarPrefChange();
110 // Tells observers to display a popup without granting tab permissions and
111 // returns whether the popup was slated to be shown.
112 bool ShowBrowserActionPopup(const Extension* extension);
114 // Ensures that the extensions in the |extension_ids| list are visible on the
115 // toolbar. This might mean they need to be moved to the front (if they are in
116 // the overflow bucket).
117 void EnsureVisibility(const ExtensionIdList& extension_ids);
119 // Highlight the extensions specified by |extension_ids|. This will cause
120 // the ToolbarModel to only display those extensions.
121 // Highlighting mode is only entered if there is at least one extension to
123 // Returns true if highlighting mode is entered, false otherwise.
124 bool HighlightExtensions(const ExtensionIdList& extension_ids);
126 // Stop highlighting extensions. All extensions can be shown again, and the
127 // number of visible icons will be reset to what it was before highlighting.
128 void StopHighlighting();
131 // content::NotificationObserver implementation.
132 virtual void Observe(int type,
133 const content::NotificationSource& source,
134 const content::NotificationDetails& details) OVERRIDE;
136 // Callback when extensions are ready.
139 // ExtensionRegistryObserver implementation.
140 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
141 const Extension* extension) OVERRIDE;
142 virtual void OnExtensionUnloaded(
143 content::BrowserContext* browser_context,
144 const Extension* extension,
145 UnloadedExtensionInfo::Reason reason) OVERRIDE;
146 virtual void OnExtensionUninstalled(
147 content::BrowserContext* browser_context,
148 const Extension* extension,
149 extensions::UninstallReason reason) OVERRIDE;
151 // To be called after the extension service is ready; gets loaded extensions
152 // from the extension service and their saved order from the pref service
153 // and constructs |toolbar_items_| from these data.
154 void InitializeExtensionList(const ExtensionSet& extensions);
155 void Populate(const ExtensionIdList& positions,
156 const ExtensionSet& extensions);
158 // Save the model to prefs.
161 // Finds the last known visible position of the icon for an |extension|. The
162 // value returned is a zero-based index into the vector of visible items.
163 size_t FindNewPositionFromLastKnownGood(const Extension* extension);
166 ObserverList<Observer> observers_;
168 void AddExtension(const Extension* extension);
169 void RemoveExtension(const Extension* extension);
171 // The Profile this toolbar model is for.
174 ExtensionPrefs* extension_prefs_;
177 // True if we've handled the initial EXTENSIONS_READY notification.
178 bool extensions_initialized_;
180 // Ordered list of browser action buttons.
181 ExtensionList toolbar_items_;
183 // List of browser action buttons which should be highlighted.
184 ExtensionList highlighted_items_;
186 // Indication whether or not we are currently in highlight mode; typically,
187 // this is equivalent to !highlighted_items_.empty(), but can be different
188 // if we are exiting highlight mode due to no longer having highlighted items.
189 bool is_highlighting_;
191 // The number of icons which were visible before highlighting a subset, in
192 // order to restore the count when finished.
193 int old_visible_icon_count_;
195 ExtensionIdList last_known_positions_;
197 // The number of icons visible (the rest should be hidden in the overflow
199 int visible_icon_count_;
201 content::NotificationRegistrar registrar_;
203 // Listen to extension load, unloaded notifications.
204 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
205 extension_registry_observer_;
207 // For observing change of toolbar order preference by external entity (sync).
208 PrefChangeRegistrar pref_change_registrar_;
209 base::Closure pref_change_callback_;
211 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_;
213 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel);
216 } // namespace extensions
218 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_