1// Copyright (c) 2011 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#pragma once
8
9#include "base/observer_list.h"
10#include "chrome/common/extensions/extension.h"
11#include "content/common/notification_observer.h"
12#include "content/common/notification_registrar.h"
13
14class ExtensionService;
15class PrefService;
16
17// Model for the browser actions toolbar.
18class ExtensionToolbarModel : public NotificationObserver {
19 public:
20  explicit ExtensionToolbarModel(ExtensionService* service);
21  ~ExtensionToolbarModel();
22
23  // Notifies the toolbar model that the Profile that suplied its
24  // prefs is being destroyed.
25  void DestroyingProfile();
26
27  // A class which is informed of changes to the model; represents the view of
28  // MVC.
29  class Observer {
30   public:
31    // An extension with a browser action button has been added, and should go
32    // in the toolbar at |index|.
33    virtual void BrowserActionAdded(const Extension* extension, int index) {}
34
35    // The browser action button for |extension| should no longer show.
36    virtual void BrowserActionRemoved(const Extension* extension) {}
37
38    // The browser action button for |extension| has been moved to |index|.
39    virtual void BrowserActionMoved(const Extension* extension, int index) {}
40
41    // Called when the model has finished loading.
42    virtual void ModelLoaded() {}
43
44   protected:
45    virtual ~Observer() {}
46  };
47
48  // Functions called by the view.
49  void AddObserver(Observer* observer);
50  void RemoveObserver(Observer* observer);
51  void MoveBrowserAction(const Extension* extension, int index);
52  // If count == size(), this will set the visible icon count to -1, meaning
53  // "show all actions".
54  void SetVisibleIconCount(int count);
55  // As above, a return value of -1 represents "show all actions".
56  int GetVisibleIconCount() { return visible_icon_count_; }
57
58  bool extensions_initialized() const { return extensions_initialized_; }
59
60  size_t size() const {
61    return toolitems_.size();
62  }
63
64  ExtensionList::iterator begin() {
65    return toolitems_.begin();
66  }
67
68  ExtensionList::iterator end() {
69    return toolitems_.end();
70  }
71
72  const Extension* GetExtensionByIndex(int index) const;
73
74  // Utility functions for converting between an index into the list of
75  // incognito-enabled browser actions, and the list of all browser actions.
76  int IncognitoIndexToOriginal(int incognito_index);
77  int OriginalIndexToIncognito(int original_index);
78
79 private:
80  // NotificationObserver implementation.
81  virtual void Observe(NotificationType type,
82                       const NotificationSource& source,
83                       const NotificationDetails& details);
84
85  // To be called after the extension service is ready; gets loaded extensions
86  // from the extension service and their saved order from the pref service
87  // and constructs |toolitems_| from these data.
88  void InitializeExtensionList();
89
90  // Save the model to prefs.
91  void UpdatePrefs();
92
93  // Our observers.
94  ObserverList<Observer> observers_;
95
96  void AddExtension(const Extension* extension);
97  void RemoveExtension(const Extension* extension);
98
99  // Our ExtensionService, guaranteed to outlive us.
100  ExtensionService* service_;
101
102  PrefService* prefs_;
103
104  // True if we've handled the initial EXTENSIONS_READY notification.
105  bool extensions_initialized_;
106
107  // Ordered list of browser action buttons.
108  ExtensionList toolitems_;
109
110  // Keeps track of what the last extension to get disabled was.
111  std::string last_extension_removed_;
112
113  // Keeps track of where the last extension to get disabled was in the list.
114  size_t last_extension_removed_index_;
115
116  // The number of icons visible (the rest should be hidden in the overflow
117  // chevron).
118  int visible_icon_count_;
119
120  NotificationRegistrar registrar_;
121};
122
123#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TOOLBAR_MODEL_H_
124