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