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 "chrome/browser/extensions/extension_prefs.h"
12#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
13#include "content/public/browser/notification_observer.h"
14#include "content/public/browser/notification_registrar.h"
15#include "extensions/common/extension.h"
16
17class Browser;
18class ExtensionService;
19class PrefService;
20class Profile;
21
22// Model for the browser actions toolbar.
23class ExtensionToolbarModel : public content::NotificationObserver,
24                              public BrowserContextKeyedService   {
25 public:
26  ExtensionToolbarModel(Profile* profile,
27                        extensions::ExtensionPrefs* extension_prefs);
28  virtual ~ExtensionToolbarModel();
29
30  // The action that should be taken as a result of clicking a browser action.
31  enum Action {
32    ACTION_NONE,
33    ACTION_SHOW_POPUP,
34    // Unlike LocationBarController there is no ACTION_SHOW_CONTEXT_MENU,
35    // because UI implementations tend to handle this themselves at a higher
36    // level.
37  };
38
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.
41  class Observer {
42   public:
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,
46                                    int index) {}
47
48    // The browser action button for |extension| should no longer show.
49    virtual void BrowserActionRemoved(const extensions::Extension* extension) {}
50
51    // The browser action button for |extension| has been moved to |index|.
52    virtual void BrowserActionMoved(const extensions::Extension* extension,
53                                    int index) {}
54
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);
58
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() {}
62
63   protected:
64    virtual ~Observer() {}
65  };
66
67  // Convenience function to get the ExtensionToolbarModel for a Profile.
68  static ExtensionToolbarModel* Get(Profile* profile);
69
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,
82                              Browser* browser,
83                              GURL* popup_url_out,
84                              bool should_grant);
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_; }
90
91  bool extensions_initialized() const { return extensions_initialized_; }
92
93  const extensions::ExtensionList& toolbar_items() const {
94    return toolbar_items_;
95  }
96
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);
101
102  void OnExtensionToolbarPrefChange();
103
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);
107
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);
112
113 private:
114  // content::NotificationObserver implementation.
115  virtual void Observe(int type,
116                       const content::NotificationSource& source,
117                       const content::NotificationDetails& details) OVERRIDE;
118
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);
125
126  // Fills |list| with extensions based on provided |order|.
127  void FillExtensionList(const extensions::ExtensionIdList& order,
128                         ExtensionService* service);
129
130  // Save the model to prefs.
131  void UpdatePrefs();
132
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);
137
138  // Our observers.
139  ObserverList<Observer> observers_;
140
141  void AddExtension(const extensions::Extension* extension);
142  void RemoveExtension(const extensions::Extension* extension);
143  void UninstalledExtension(const extensions::Extension* extension);
144
145  // The Profile this toolbar model is for.
146  Profile* profile_;
147
148  extensions::ExtensionPrefs* extension_prefs_;
149  PrefService* prefs_;
150
151  // True if we've handled the initial EXTENSIONS_READY notification.
152  bool extensions_initialized_;
153
154  // Ordered list of browser action buttons.
155  extensions::ExtensionList toolbar_items_;
156
157  extensions::ExtensionIdList last_known_positions_;
158
159  // The number of icons visible (the rest should be hidden in the overflow
160  // chevron).
161  int visible_icon_count_;
162
163  content::NotificationRegistrar registrar_;
164
165  // For observing change of toolbar order preference by external entity (sync).
166  PrefChangeRegistrar pref_change_registrar_;
167  base::Closure pref_change_callback_;
168
169  base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_;
170
171  DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel);
172};
173
174#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_
175