OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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_SHELF_MODEL_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SHELF_MODEL_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/observer_list.h" | |
13 #include "chrome/browser/extensions/extension_host.h" | |
14 #include "chrome/common/extensions/extension.h" | |
15 #include "chrome/common/notification_observer.h" | |
16 #include "chrome/common/notification_registrar.h" | |
17 | |
18 class Browser; | |
19 class ExtensionPrefs; | |
20 class ExtensionShelfModelObserver; | |
21 | |
22 // The model representing the toolstrips on an ExtensionShelf. The order of | |
23 // the toolstrips is common across all of the models for a given Profile, | |
24 // but there are multiple models. Each model contains the hosts/views which | |
25 // are specific to a Browser. | |
26 class ExtensionShelfModel : public NotificationObserver { | |
27 public: | |
28 explicit ExtensionShelfModel(Browser* browser); | |
29 virtual ~ExtensionShelfModel(); | |
30 | |
31 struct ToolstripItem { | |
32 ExtensionHost* host; | |
33 Extension::ToolstripInfo info; | |
34 void* data; | |
35 int height; | |
36 GURL url; | |
37 }; | |
38 | |
39 typedef std::vector<ToolstripItem> ToolstripList; | |
40 typedef ToolstripList::iterator iterator; | |
41 | |
42 // Add and remove observers to changes within this ExtensionShelfModel. | |
43 void AddObserver(ExtensionShelfModelObserver* observer); | |
44 void RemoveObserver(ExtensionShelfModelObserver* observer); | |
45 | |
46 // The number of toolstrips in the model. | |
47 int count() const { return static_cast<int>(toolstrips_.size()); } | |
48 bool empty() const { return toolstrips_.empty(); } | |
49 | |
50 // Iterators for the toolstrips in the model. | |
51 iterator begin() { return toolstrips_.begin(); } | |
52 ExtensionShelfModel::iterator end() { return toolstrips_.end(); } | |
53 | |
54 // Add |toolstrip| to the end of the shelf. | |
55 void AppendToolstrip(const ToolstripItem& toolstrip); | |
56 | |
57 // Insert |toolstrip| and |data| at |index|. | |
58 void InsertToolstripAt(int index, const ToolstripItem& toolstrip); | |
59 | |
60 // Remove the toolstrip at |index|. | |
61 void RemoveToolstripAt(int index); | |
62 | |
63 // Move the toolstrip at |index| to |to_index|. | |
64 void MoveToolstripAt(int index, int to_index); | |
65 | |
66 // Lookup the index of |host|. Returns -1 if not present. | |
67 int IndexOfHost(ExtensionHost* host); | |
68 | |
69 // Return the toolstrip at |index|. | |
70 const ToolstripItem& ToolstripAt(int index); | |
71 | |
72 // Return the ToolstripItem associated with |host| or NULL if it's not | |
73 // present. | |
74 ToolstripList::iterator ToolstripForHost(ExtensionHost* host); | |
75 | |
76 // Set some arbitrary data associated with a particular toolstrip. | |
77 void SetToolstripDataAt(int index, void* data); | |
78 | |
79 // Update the ToolstripItem for |toolstrip| to set its |url| and |height| | |
80 // and then call ToolstripChanged for all observers. | |
81 // If |url| is empty, no navigation is requested. | |
82 void ExpandToolstrip(iterator toolstrip, const GURL& url, int height); | |
83 | |
84 // Update the ToolstripItem for |toolstrip| to set its |url| and its height | |
85 // to 0, and then call ToolstripChanged for all observers. | |
86 // If |url| is empty, no navigation is requested. | |
87 void CollapseToolstrip(iterator toolstrip, const GURL& url); | |
88 | |
89 // NotificationObserver | |
90 virtual void Observe(NotificationType type, | |
91 const NotificationSource& source, | |
92 const NotificationDetails& details); | |
93 | |
94 private: | |
95 // Add all of the toolstrips from |extension|. | |
96 void AddExtension(Extension* extension); | |
97 | |
98 // Add all of the toolstrips from each extension in |extensions|. | |
99 void AddExtensions(const ExtensionList* extensions); | |
100 | |
101 // Remove all of the toolstrips in |extension| from the shelf. | |
102 void RemoveExtension(Extension* extension); | |
103 | |
104 // Update prefs with the most recent changes. | |
105 void UpdatePrefs(); | |
106 | |
107 // Reloads order from prefs. | |
108 void SortToolstrips(); | |
109 | |
110 // The browser that this model is attached to. | |
111 Browser* browser_; | |
112 | |
113 // The preferences that this model uses. | |
114 ExtensionPrefs* prefs_; | |
115 | |
116 // Manages our notification registrations. | |
117 NotificationRegistrar registrar_; | |
118 | |
119 // The Toolstrips loaded in this model. The model owns these objects. | |
120 ToolstripList toolstrips_; | |
121 | |
122 // Our observers. | |
123 typedef ObserverList<ExtensionShelfModelObserver> | |
124 ExtensionShelfModelObservers; | |
125 ExtensionShelfModelObservers observers_; | |
126 | |
127 // Whether the model has received an EXTENSIONS_READY notification. | |
128 bool ready_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(ExtensionShelfModel); | |
131 }; | |
132 | |
133 // Objects implement this interface when they wish to be notified of changes to | |
134 // the ExtensionShelfModel. | |
135 // | |
136 // Register your ExtensionShelfModelObserver with the ExtensionShelfModel using | |
137 // Add/RemoveObserver methods. | |
138 class ExtensionShelfModelObserver { | |
139 public: | |
140 // A new toolstrip was inserted into ExtensionShelfModel at |index|. | |
141 virtual void ToolstripInsertedAt(ExtensionHost* toolstrip, int index) {} | |
142 | |
143 // The specified toolstrip is being removed and destroyed. | |
144 virtual void ToolstripRemovingAt(ExtensionHost* toolstrip, int index) {} | |
145 | |
146 // |toolstrip| moved from |from_index| to |to_index|. | |
147 virtual void ToolstripMoved(ExtensionHost* toolstrip, | |
148 int from_index, | |
149 int to_index) {} | |
150 | |
151 // The specified toolstrip changed in some way (currently only size changes) | |
152 virtual void ToolstripChanged(ExtensionShelfModel::iterator toolstrip) {} | |
153 | |
154 // There are no more toolstrips in the model. | |
155 virtual void ExtensionShelfEmpty() {} | |
156 | |
157 // The entire model may have changed. | |
158 virtual void ShelfModelReloaded() {} | |
159 | |
160 // The model is being destroyed. | |
161 virtual void ShelfModelDeleting() {} | |
162 | |
163 protected: | |
164 virtual ~ExtensionShelfModelObserver() {} | |
165 }; | |
166 | |
167 | |
168 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SHELF_MODEL_H_ | |
OLD | NEW |