extension_toolbar_model.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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. 4 5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ 6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ 7 8#include "base/compiler_specific.h" 9#include "base/observer_list.h" 10#include "base/prefs/pref_change_registrar.h" 11#include "components/keyed_service/core/keyed_service.h" 12#include "content/public/browser/notification_observer.h" 13#include "content/public/browser/notification_registrar.h" 14#include "extensions/browser/extension_prefs.h" 15#include "extensions/common/extension.h" 16 17class Browser; 18class ExtensionService; 19class PrefService; 20class Profile; 21 22namespace extensions { 23 24// Model for the browser actions toolbar. 25class ExtensionToolbarModel : public content::NotificationObserver, 26 public KeyedService { 27 public: 28 ExtensionToolbarModel(Profile* profile, ExtensionPrefs* extension_prefs); 29 virtual ~ExtensionToolbarModel(); 30 31 // The action that should be taken as a result of clicking a browser action. 32 enum Action { 33 ACTION_NONE, 34 ACTION_SHOW_POPUP, 35 // Unlike LocationBarController there is no ACTION_SHOW_CONTEXT_MENU, 36 // because UI implementations tend to handle this themselves at a higher 37 // level. 38 }; 39 40 // A class which is informed of changes to the model; represents the view of 41 // MVC. Also used for signaling view changes such as showing extension popups. 42 class Observer { 43 public: 44 // An extension with a browser action button has been added, and should go 45 // in the toolbar at |index|. 46 virtual void BrowserActionAdded(const Extension* extension, int index) {} 47 48 // The browser action button for |extension| should no longer show. 49 virtual void BrowserActionRemoved(const Extension* extension) {} 50 51 // The browser action button for |extension| has been moved to |index|. 52 virtual void BrowserActionMoved(const Extension* extension, int index) {} 53 54 // Signal the |extension| to show the popup now in the active window. 55 // Returns true if a popup was slated to be shown. 56 virtual bool BrowserActionShowPopup(const Extension* extension); 57 58 // Signal when the container needs to be redrawn because of a size change, 59 // and when the model has finished loading. 60 virtual void VisibleCountChanged() {} 61 62 // Signal that the model has entered or exited highlighting mode, or that 63 // the extensions being highlighted have (probably*) changed. Highlighting 64 // mode indicates that only a subset of the extensions are actively 65 // displayed, and those extensions should be highlighted for extra emphasis. 66 // * probably, because if we are in highlight mode and receive a call to 67 // highlight a new set of extensions, we do not compare the current set 68 // with the new set (and just assume the new set is different). 69 virtual void HighlightModeChanged(bool is_highlighting) {} 70 71 protected: 72 virtual ~Observer() {} 73 }; 74 75 // Convenience function to get the ExtensionToolbarModel for a Profile. 76 static ExtensionToolbarModel* Get(Profile* profile); 77 78 // Functions called by the view. 79 void AddObserver(Observer* observer); 80 void RemoveObserver(Observer* observer); 81 void MoveBrowserAction(const Extension* extension, int index); 82 // Executes the browser action for an extension and returns the action that 83 // the UI should perform in response. 84 // |popup_url_out| will be set if the extension should show a popup, with 85 // the URL that should be shown, if non-NULL. |should_grant| controls whether 86 // the extension should be granted page tab permissions, which is what happens 87 // when the user clicks the browser action, but not, for example, when the 88 // showPopup API is called. 89 Action ExecuteBrowserAction(const Extension* extension, 90 Browser* browser, 91 GURL* popup_url_out, 92 bool should_grant); 93 // If count == size(), this will set the visible icon count to -1, meaning 94 // "show all actions". 95 void SetVisibleIconCount(int count); 96 // As above, a return value of -1 represents "show all actions". 97 int GetVisibleIconCount() const { return visible_icon_count_; } 98 99 bool extensions_initialized() const { return extensions_initialized_; } 100 101 const ExtensionList& toolbar_items() const { 102 return is_highlighting_ ? highlighted_items_ : toolbar_items_; 103 } 104 105 bool is_highlighting() const { return is_highlighting_; } 106 107 // Utility functions for converting between an index into the list of 108 // incognito-enabled browser actions, and the list of all browser actions. 109 int IncognitoIndexToOriginal(int incognito_index); 110 int OriginalIndexToIncognito(int original_index); 111 112 void OnExtensionToolbarPrefChange(); 113 114 // Tells observers to display a popup without granting tab permissions and 115 // returns whether the popup was slated to be shown. 116 bool ShowBrowserActionPopup(const Extension* extension); 117 118 // Ensures that the extensions in the |extension_ids| list are visible on the 119 // toolbar. This might mean they need to be moved to the front (if they are in 120 // the overflow bucket). 121 void EnsureVisibility(const ExtensionIdList& extension_ids); 122 123 // Highlight the extensions specified by |extension_ids|. This will cause 124 // the ToolbarModel to only display those extensions. 125 // Highlighting mode is only entered if there is at least one extension to 126 // be shown. 127 // Returns true if highlighting mode is entered, false otherwise. 128 bool HighlightExtensions(const ExtensionIdList& extension_ids); 129 130 // Stop highlighting extensions. All extensions can be shown again, and the 131 // number of visible icons will be reset to what it was before highlighting. 132 void StopHighlighting(); 133 134 private: 135 // content::NotificationObserver implementation. 136 virtual void Observe(int type, 137 const content::NotificationSource& source, 138 const content::NotificationDetails& details) OVERRIDE; 139 140 // To be called after the extension service is ready; gets loaded extensions 141 // from the extension service and their saved order from the pref service 142 // and constructs |toolbar_items_| from these data. 143 void InitializeExtensionList(ExtensionService* service); 144 void Populate(const ExtensionIdList& positions, ExtensionService* service); 145 146 // Fills |list| with extensions based on provided |order|. 147 void FillExtensionList(const ExtensionIdList& order, 148 ExtensionService* service); 149 150 // Save the model to prefs. 151 void UpdatePrefs(); 152 153 // Finds the last known visible position of the icon for an |extension|. The 154 // value returned is a zero-based index into the vector of visible items. 155 size_t FindNewPositionFromLastKnownGood(const Extension* extension); 156 157 // Our observers. 158 ObserverList<Observer> observers_; 159 160 void AddExtension(const Extension* extension); 161 void RemoveExtension(const Extension* extension); 162 void UninstalledExtension(const Extension* extension); 163 164 // The Profile this toolbar model is for. 165 Profile* profile_; 166 167 ExtensionPrefs* extension_prefs_; 168 PrefService* prefs_; 169 170 // True if we've handled the initial EXTENSIONS_READY notification. 171 bool extensions_initialized_; 172 173 // Ordered list of browser action buttons. 174 ExtensionList toolbar_items_; 175 176 // List of browser action buttons which should be highlighted. 177 ExtensionList highlighted_items_; 178 179 // Indication whether or not we are currently in highlight mode; typically, 180 // this is equivalent to !highlighted_items_.empty(), but can be different 181 // if we are exiting highlight mode due to no longer having highlighted items. 182 bool is_highlighting_; 183 184 // The number of icons which were visible before highlighting a subset, in 185 // order to restore the count when finished. 186 int old_visible_icon_count_; 187 188 ExtensionIdList last_known_positions_; 189 190 // The number of icons visible (the rest should be hidden in the overflow 191 // chevron). 192 int visible_icon_count_; 193 194 content::NotificationRegistrar registrar_; 195 196 // For observing change of toolbar order preference by external entity (sync). 197 PrefChangeRegistrar pref_change_registrar_; 198 base::Closure pref_change_callback_; 199 200 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_; 201 202 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel); 203}; 204 205} // namespace extensions 206 207#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_ 208