summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_shelf_model.h
blob: 934ec3b1e84bb6edf5b936ee7df705dde853a1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SHELF_MODEL_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_SHELF_MODEL_H_

#include <vector>

#include "base/basictypes.h"
#include "base/observer_list.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"

class Browser;
class ExtensionPrefs;
class ExtensionShelfModelObserver;

// The model representing the toolstrips on an ExtensionShelf.  The order of
// the toolstrips is common across all of the models for a given Profile,
// but there are multiple models.  Each model contains the hosts/views which
// are specific to a Browser.
class ExtensionShelfModel : public NotificationObserver {
 public:
  explicit ExtensionShelfModel(Browser* browser);
  virtual ~ExtensionShelfModel();

  struct ToolstripItem {
    ExtensionHost* host;
    Extension::ToolstripInfo info;
    void* data;
    int height;
    GURL url;
  };

  typedef std::vector<ToolstripItem> ToolstripList;
  typedef ToolstripList::iterator iterator;

  // Add and remove observers to changes within this ExtensionShelfModel.
  void AddObserver(ExtensionShelfModelObserver* observer);
  void RemoveObserver(ExtensionShelfModelObserver* observer);

  // The number of toolstrips in the model.
  int count() const { return static_cast<int>(toolstrips_.size()); }
  bool empty() const { return toolstrips_.empty(); }

  // Iterators for the toolstrips in the model.
  iterator begin() { return toolstrips_.begin(); }
  ExtensionShelfModel::iterator end() { return toolstrips_.end(); }

  // Add |toolstrip| to the end of the shelf.
  void AppendToolstrip(const ToolstripItem& toolstrip);

  // Insert |toolstrip| and |data| at |index|.
  void InsertToolstripAt(int index, const ToolstripItem& toolstrip);

  // Remove the toolstrip at |index|.
  void RemoveToolstripAt(int index);

  // Move the toolstrip at |index| to |to_index|.
  void MoveToolstripAt(int index, int to_index);

  // Lookup the index of |host|.  Returns -1 if not present.
  int IndexOfHost(ExtensionHost* host);

  // Return the toolstrip at |index|.
  const ToolstripItem& ToolstripAt(int index);

  // Return the ToolstripItem associated with |host| or NULL if it's not
  // present.
  ToolstripList::iterator ToolstripForHost(ExtensionHost* host);

  // Set some arbitrary data associated with a particular toolstrip.
  void SetToolstripDataAt(int index, void* data);

  // Update the ToolstripItem for |toolstrip| to set its |url| and |height|
  // and then call ToolstripChanged for all observers.
  // If |url| is empty, no navigation is requested.
  void ExpandToolstrip(iterator toolstrip, const GURL& url, int height);

  // Update the ToolstripItem for |toolstrip| to set its |url| and its height
  // to 0, and then call ToolstripChanged for all observers.
  // If |url| is empty, no navigation is requested.
  void CollapseToolstrip(iterator toolstrip, const GURL& url);

  // NotificationObserver
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

 private:
  // Add all of the toolstrips from |extension|.
  void AddExtension(Extension* extension);

  // Add all of the toolstrips from each extension in |extensions|.
  void AddExtensions(const ExtensionList* extensions);

  // Remove all of the toolstrips in |extension| from the shelf.
  void RemoveExtension(Extension* extension);

  // Update prefs with the most recent changes.
  void UpdatePrefs();

  // Reloads order from prefs.
  void SortToolstrips();

  // The browser that this model is attached to.
  Browser* browser_;

  // The preferences that this model uses.
  ExtensionPrefs* prefs_;

  // Manages our notification registrations.
  NotificationRegistrar registrar_;

  // The Toolstrips loaded in this model. The model owns these objects.
  ToolstripList toolstrips_;

  // Our observers.
  typedef ObserverList<ExtensionShelfModelObserver>
      ExtensionShelfModelObservers;
  ExtensionShelfModelObservers observers_;

  // Whether the model has received an EXTENSIONS_READY notification.
  bool ready_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionShelfModel);
};

// Objects implement this interface when they wish to be notified of changes to
// the ExtensionShelfModel.
//
// Register your ExtensionShelfModelObserver with the ExtensionShelfModel using
// Add/RemoveObserver methods.
class ExtensionShelfModelObserver {
 public:
  // A new toolstrip was inserted into ExtensionShelfModel at |index|.
  virtual void ToolstripInsertedAt(ExtensionHost* toolstrip, int index) {}

  // The specified toolstrip is being removed and destroyed.
  virtual void ToolstripRemovingAt(ExtensionHost* toolstrip, int index) {}

  // |toolstrip| moved from |from_index| to |to_index|.
  virtual void ToolstripMoved(ExtensionHost* toolstrip,
                              int from_index,
                              int to_index) {}

  // The specified toolstrip changed in some way (currently only size changes)
  virtual void ToolstripChanged(ExtensionShelfModel::iterator toolstrip) {}

  // There are no more toolstrips in the model.
  virtual void ExtensionShelfEmpty() {}

  // The entire model may have changed.
  virtual void ShelfModelReloaded() {}

  // The model is being destroyed.
  virtual void ShelfModelDeleting() {}

 protected:
  virtual ~ExtensionShelfModelObserver() {}
};


#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_SHELF_MODEL_H_