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 "chrome/common/extensions/extension.h"
12 #include "chrome/browser/extensions/extension_prefs.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
17 class ExtensionService;
20 // Model for the browser actions toolbar.
21 class ExtensionToolbarModel : public content::NotificationObserver {
23 explicit ExtensionToolbarModel(ExtensionService* service);
24 virtual ~ExtensionToolbarModel();
26 // The action that should be taken as a result of clicking a browser action.
30 // Unlike LocationBarController there is no ACTION_SHOW_CONTEXT_MENU,
31 // because UI implementations tend to handle this themselves at a higher
35 // A class which is informed of changes to the model; represents the view of
36 // MVC. Also used for signaling view changes such as showing extension popups.
39 // An extension with a browser action button has been added, and should go
40 // in the toolbar at |index|.
41 virtual void BrowserActionAdded(const extensions::Extension* extension,
44 // The browser action button for |extension| should no longer show.
45 virtual void BrowserActionRemoved(const extensions::Extension* extension) {}
47 // The browser action button for |extension| has been moved to |index|.
48 virtual void BrowserActionMoved(const extensions::Extension* extension,
51 // Signal the |extension| to show the popup now in the active window.
52 // Returns true if a popup was slated to be shown.
53 virtual bool BrowserActionShowPopup(const extensions::Extension* extension);
55 // Called when the model has finished loading.
56 virtual void ModelLoaded() {}
59 virtual ~Observer() {}
62 // Functions called by the view.
63 void AddObserver(Observer* observer);
64 void RemoveObserver(Observer* observer);
65 void MoveBrowserAction(const extensions::Extension* extension, int index);
66 // Executes the browser action for an extension and returns the action that
67 // the UI should perform in response.
68 // |popup_url_out| will be set if the extension should show a popup, with
69 // the URL that should be shown, if non-NULL. |should_grant| controls whether
70 // the extension should be granted page tab permissions, which is what happens
71 // when the user clicks the browser action, but not, for example, when the
72 // showPopup API is called.
73 Action ExecuteBrowserAction(const extensions::Extension* extension,
77 // If count == size(), this will set the visible icon count to -1, meaning
78 // "show all actions".
79 void SetVisibleIconCount(int count);
80 // As above, a return value of -1 represents "show all actions".
81 int GetVisibleIconCount() const { return visible_icon_count_; }
83 bool extensions_initialized() const { return extensions_initialized_; }
85 const extensions::ExtensionList& toolbar_items() const {
86 return toolbar_items_;
89 // Utility functions for converting between an index into the list of
90 // incognito-enabled browser actions, and the list of all browser actions.
91 int IncognitoIndexToOriginal(int incognito_index);
92 int OriginalIndexToIncognito(int original_index);
94 void OnExtensionToolbarPrefChange();
96 // Tells observers to display a popup without granting tab permissions and
97 // returns whether the popup was slated to be shown.
98 bool ShowBrowserActionPopup(const extensions::Extension* extension);
101 // content::NotificationObserver implementation.
102 virtual void Observe(int type,
103 const content::NotificationSource& source,
104 const content::NotificationDetails& details) OVERRIDE;
106 // To be called after the extension service is ready; gets loaded extensions
107 // from the extension service and their saved order from the pref service
108 // and constructs |toolbar_items_| from these data.
109 void InitializeExtensionList();
110 void Populate(const extensions::ExtensionIdList& positions);
112 // Fills |list| with extensions based on provided |order|.
113 void FillExtensionList(const extensions::ExtensionIdList& order);
115 // Save the model to prefs.
118 // Finds the last known visible position of the icon for an |extension|. The
119 // value returned is a zero-based index into the vector of visible items.
120 size_t FindNewPositionFromLastKnownGood(
121 const extensions::Extension* extension);
124 ObserverList<Observer> observers_;
126 void AddExtension(const extensions::Extension* extension);
127 void RemoveExtension(const extensions::Extension* extension);
128 void UninstalledExtension(const extensions::Extension* extension);
130 // Our ExtensionService, guaranteed to outlive us.
131 ExtensionService* service_;
135 // True if we've handled the initial EXTENSIONS_READY notification.
136 bool extensions_initialized_;
138 // Ordered list of browser action buttons.
139 extensions::ExtensionList toolbar_items_;
141 extensions::ExtensionIdList last_known_positions_;
143 // The number of icons visible (the rest should be hidden in the overflow
145 int visible_icon_count_;
147 content::NotificationRegistrar registrar_;
149 // For observing change of toolbar order preference by external entity (sync).
150 PrefChangeRegistrar pref_change_registrar_;
151 base::Closure pref_change_callback_;
153 base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_;
155 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel);
158 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_