- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_toolbar_model.h
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/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"
15
16 class Browser;
17 class ExtensionService;
18 class PrefService;
19
20 // Model for the browser actions toolbar.
21 class ExtensionToolbarModel : public content::NotificationObserver {
22  public:
23   explicit ExtensionToolbarModel(ExtensionService* service);
24   virtual ~ExtensionToolbarModel();
25
26   // The action that should be taken as a result of clicking a browser action.
27   enum Action {
28     ACTION_NONE,
29     ACTION_SHOW_POPUP,
30     // Unlike LocationBarController there is no ACTION_SHOW_CONTEXT_MENU,
31     // because UI implementations tend to handle this themselves at a higher
32     // level.
33   };
34
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.
37   class Observer {
38    public:
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,
42                                     int index) {}
43
44     // The browser action button for |extension| should no longer show.
45     virtual void BrowserActionRemoved(const extensions::Extension* extension) {}
46
47     // The browser action button for |extension| has been moved to |index|.
48     virtual void BrowserActionMoved(const extensions::Extension* extension,
49                                     int index) {}
50
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);
54
55     // Called when the model has finished loading.
56     virtual void ModelLoaded() {}
57
58    protected:
59     virtual ~Observer() {}
60   };
61
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,
74                               Browser* browser,
75                               GURL* popup_url_out,
76                               bool should_grant);
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_; }
82
83   bool extensions_initialized() const { return extensions_initialized_; }
84
85   const extensions::ExtensionList& toolbar_items() const {
86     return toolbar_items_;
87   }
88
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);
93
94   void OnExtensionToolbarPrefChange();
95
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);
99
100  private:
101   // content::NotificationObserver implementation.
102   virtual void Observe(int type,
103                        const content::NotificationSource& source,
104                        const content::NotificationDetails& details) OVERRIDE;
105
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);
111
112   // Fills |list| with extensions based on provided |order|.
113   void FillExtensionList(const extensions::ExtensionIdList& order);
114
115   // Save the model to prefs.
116   void UpdatePrefs();
117
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);
122
123   // Our observers.
124   ObserverList<Observer> observers_;
125
126   void AddExtension(const extensions::Extension* extension);
127   void RemoveExtension(const extensions::Extension* extension);
128   void UninstalledExtension(const extensions::Extension* extension);
129
130   // Our ExtensionService, guaranteed to outlive us.
131   ExtensionService* service_;
132
133   PrefService* prefs_;
134
135   // True if we've handled the initial EXTENSIONS_READY notification.
136   bool extensions_initialized_;
137
138   // Ordered list of browser action buttons.
139   extensions::ExtensionList toolbar_items_;
140
141   extensions::ExtensionIdList last_known_positions_;
142
143   // The number of icons visible (the rest should be hidden in the overflow
144   // chevron).
145   int visible_icon_count_;
146
147   content::NotificationRegistrar registrar_;
148
149   // For observing change of toolbar order preference by external entity (sync).
150   PrefChangeRegistrar pref_change_registrar_;
151   base::Closure pref_change_callback_;
152
153   base::WeakPtrFactory<ExtensionToolbarModel> weak_ptr_factory_;
154
155   DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModel);
156 };
157
158 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_