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 "components/browser_context_keyed_service/browser_context_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"
18 class ExtensionService
;
22 // Model for the browser actions toolbar.
23 class ExtensionToolbarModel
: public content::NotificationObserver
,
24 public BrowserContextKeyedService
{
26 ExtensionToolbarModel(Profile
* profile
,
27 extensions::ExtensionPrefs
* extension_prefs
);
28 virtual ~ExtensionToolbarModel();
30 // The action that should be taken as a result of clicking a browser action.
34 // Unlike LocationBarController there is no ACTION_SHOW_CONTEXT_MENU,
35 // because UI implementations tend to handle this themselves at a higher
39 // A class which is informed of changes to the model; represents the view of
40 // MVC. Also used for signaling view changes such as showing extension popups.
43 // An extension with a browser action button has been added, and should go
44 // in the toolbar at |index|.
45 virtual void BrowserActionAdded(const extensions::Extension
* extension
,
48 // The browser action button for |extension| should no longer show.
49 virtual void BrowserActionRemoved(const extensions::Extension
* extension
) {}
51 // The browser action button for |extension| has been moved to |index|.
52 virtual void BrowserActionMoved(const extensions::Extension
* extension
,
55 // Signal the |extension| to show the popup now in the active window.
56 // Returns true if a popup was slated to be shown.
57 virtual bool BrowserActionShowPopup(const extensions::Extension
* extension
);
59 // Signal when the container needs to be redrawn because of a size change,
60 // and when the model has finished loading.
61 virtual void VisibleCountChanged() {}
64 virtual ~Observer() {}
67 // Convenience function to get the ExtensionToolbarModel for a Profile.
68 static ExtensionToolbarModel
* Get(Profile
* profile
);
70 // Functions called by the view.
71 void AddObserver(Observer
* observer
);
72 void RemoveObserver(Observer
* observer
);
73 void MoveBrowserAction(const extensions::Extension
* extension
, int index
);
74 // Executes the browser action for an extension and returns the action that
75 // the UI should perform in response.
76 // |popup_url_out| will be set if the extension should show a popup, with
77 // the URL that should be shown, if non-NULL. |should_grant| controls whether
78 // the extension should be granted page tab permissions, which is what happens
79 // when the user clicks the browser action, but not, for example, when the
80 // showPopup API is called.
81 Action
ExecuteBrowserAction(const extensions::Extension
* extension
,
85 // If count == size(), this will set the visible icon count to -1, meaning
86 // "show all actions".
87 void SetVisibleIconCount(int count
);
88 // As above, a return value of -1 represents "show all actions".
89 int GetVisibleIconCount() const { return visible_icon_count_
; }
91 bool extensions_initialized() const { return extensions_initialized_
; }
93 const extensions::ExtensionList
& toolbar_items() const {
94 return toolbar_items_
;
97 // Utility functions for converting between an index into the list of
98 // incognito-enabled browser actions, and the list of all browser actions.
99 int IncognitoIndexToOriginal(int incognito_index
);
100 int OriginalIndexToIncognito(int original_index
);
102 void OnExtensionToolbarPrefChange();
104 // Tells observers to display a popup without granting tab permissions and
105 // returns whether the popup was slated to be shown.
106 bool ShowBrowserActionPopup(const extensions::Extension
* extension
);
108 // Ensures that the extensions in the |extension_ids| list are visible on the
109 // toolbar. This might mean they need to be moved to the front (if they are in
110 // the overflow bucket).
111 void EnsureVisibility(const extensions::ExtensionIdList
& extension_ids
);
114 // content::NotificationObserver implementation.
115 virtual void Observe(int type
,
116 const content::NotificationSource
& source
,
117 const content::NotificationDetails
& details
) OVERRIDE
;
119 // To be called after the extension service is ready; gets loaded extensions
120 // from the extension service and their saved order from the pref service
121 // and constructs |toolbar_items_| from these data.
122 void InitializeExtensionList(ExtensionService
* service
);
123 void Populate(const extensions::ExtensionIdList
& positions
,
124 ExtensionService
* service
);
126 // Fills |list| with extensions based on provided |order|.
127 void FillExtensionList(const extensions::ExtensionIdList
& order
,
128 ExtensionService
* service
);
130 // Save the model to prefs.
133 // Finds the last known visible position of the icon for an |extension|. The
134 // value returned is a zero-based index into the vector of visible items.
135 size_t FindNewPositionFromLastKnownGood(
136 const extensions::Extension
* extension
);
139 ObserverList
<Observer
> observers_
;
141 void AddExtension(const extensions::Extension
* extension
);
142 void RemoveExtension(const extensions::Extension
* extension
);
143 void UninstalledExtension(const extensions::Extension
* extension
);
145 // The Profile this toolbar model is for.
148 extensions::ExtensionPrefs
* extension_prefs_
;
151 // True if we've handled the initial EXTENSIONS_READY notification.
152 bool extensions_initialized_
;
154 // Ordered list of browser action buttons.
155 extensions::ExtensionList toolbar_items_
;
157 extensions::ExtensionIdList last_known_positions_
;
159 // The number of icons visible (the rest should be hidden in the overflow
161 int visible_icon_count_
;
163 content::NotificationRegistrar registrar_
;
165 // For observing change of toolbar order preference by external entity (sync).
166 PrefChangeRegistrar pref_change_registrar_
;
167 base::Closure pref_change_callback_
;
169 base::WeakPtrFactory
<ExtensionToolbarModel
> weak_ptr_factory_
;
171 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel
);
174 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_