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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
// 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.
#include "chrome/browser/extensions/extension_shelf_model.h"
#include "base/stl_util-inl.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_process_manager.h"
#include "chrome/browser/extensions/extension_toolstrip_api.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
ExtensionShelfModel::ExtensionShelfModel(Browser* browser)
: browser_(browser), ready_(false) {
// Watch extensions loaded and unloaded notifications.
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
NotificationService::AllSources());
registrar_.Add(this, NotificationType::EXTENSIONS_READY,
NotificationService::AllSources());
// Add any already-loaded extensions now, since we missed the notification for
// those.
ExtensionsService* service = browser_->profile()->GetExtensionsService();
if (service) { // This can be null in unit tests.
prefs_ = browser_->profile()->GetExtensionsService()->extension_prefs();
registrar_.Add(this, NotificationType::EXTENSION_SHELF_MODEL_CHANGED,
Source<ExtensionPrefs>(prefs_));
ready_ = service->is_ready();
if (ready_) {
AddExtensions(service->extensions());
SortToolstrips();
}
}
}
ExtensionShelfModel::~ExtensionShelfModel() {
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ShelfModelDeleting());
observers_.Clear();
for (iterator t = toolstrips_.begin(); t != toolstrips_.end(); ++t)
delete t->host;
toolstrips_.clear();
}
void ExtensionShelfModel::AddObserver(ExtensionShelfModelObserver* observer) {
observers_.AddObserver(observer);
}
void ExtensionShelfModel::RemoveObserver(
ExtensionShelfModelObserver* observer) {
observers_.RemoveObserver(observer);
}
void ExtensionShelfModel::AppendToolstrip(const ToolstripItem& toolstrip) {
InsertToolstripAt(count(), toolstrip);
}
void ExtensionShelfModel::InsertToolstripAt(int index,
const ToolstripItem& toolstrip) {
toolstrips_.insert(toolstrips_.begin() + index, toolstrip);
if (ready_) {
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripInsertedAt(toolstrip.host, index));
}
}
void ExtensionShelfModel::RemoveToolstripAt(int index) {
ExtensionHost* host = ToolstripAt(index).host;
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripRemovingAt(host, index));
toolstrips_.erase(toolstrips_.begin() + index);
delete host;
}
void ExtensionShelfModel::MoveToolstripAt(int index, int to_index) {
DCHECK(index >= 0);
DCHECK(to_index >= 0);
if (index == to_index)
return;
ToolstripItem toolstrip = toolstrips_[index];
toolstrips_.erase(toolstrips_.begin() + index);
toolstrips_.insert(toolstrips_.begin() + to_index, toolstrip);
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripMoved(toolstrip.host, index, to_index));
UpdatePrefs();
}
int ExtensionShelfModel::IndexOfHost(ExtensionHost* host) {
for (iterator i = toolstrips_.begin(); i != toolstrips_.end(); ++i) {
if (i->host == host)
return i - toolstrips_.begin();
}
return -1;
}
ExtensionShelfModel::iterator ExtensionShelfModel::ToolstripForHost(
ExtensionHost* host) {
for (iterator i = toolstrips_.begin(); i != toolstrips_.end(); ++i) {
if (i->host == host)
return i;
}
return toolstrips_.end();
}
const ExtensionShelfModel::ToolstripItem& ExtensionShelfModel::ToolstripAt(
int index) {
DCHECK(index >= 0);
return toolstrips_[index];
}
void ExtensionShelfModel::SetToolstripDataAt(int index, void* data) {
DCHECK(index >= 0);
toolstrips_[index].data = data;
}
void ExtensionShelfModel::ExpandToolstrip(iterator toolstrip,
const GURL& url, int height) {
if (toolstrip == end())
return;
toolstrip->height = height;
toolstrip->url = url;
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripChanged(toolstrip));
int routing_id = toolstrip->host->render_view_host()->routing_id();
ToolstripEventRouter::OnToolstripExpanded(browser_->profile(),
routing_id,
url, height);
toolstrip->host->SetRenderViewType(ViewType::EXTENSION_MOLE);
}
void ExtensionShelfModel::CollapseToolstrip(iterator toolstrip,
const GURL& url) {
if (toolstrip == end())
return;
toolstrip->height = 0;
toolstrip->url = url;
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ToolstripChanged(toolstrip));
int routing_id = toolstrip->host->render_view_host()->routing_id();
ToolstripEventRouter::OnToolstripCollapsed(browser_->profile(),
routing_id,
url);
toolstrip->host->SetRenderViewType(ViewType::EXTENSION_TOOLSTRIP);
}
void ExtensionShelfModel::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSION_LOADED:
if (ready_)
AddExtension(Details<Extension>(details).ptr());
break;
case NotificationType::EXTENSION_UNLOADED:
RemoveExtension(Details<Extension>(details).ptr());
break;
case NotificationType::EXTENSIONS_READY:
if (browser_->profile()->GetExtensionsService()) {
AddExtensions(
browser_->profile()->GetExtensionsService()->extensions());
SortToolstrips();
}
ready_ = true;
break;
case NotificationType::EXTENSION_SHELF_MODEL_CHANGED:
// Ignore changes that this model originated.
if (Details<ExtensionShelfModel>(details).ptr() != this)
SortToolstrips();
break;
default:
DCHECK(false) << "Unhandled notification of type: " << type.value;
break;
}
}
void ExtensionShelfModel::AddExtension(Extension* extension) {
ExtensionProcessManager* manager =
browser_->profile()->GetExtensionProcessManager();
DCHECK(manager);
if (!manager)
return;
for (std::vector<Extension::ToolstripInfo>::const_iterator toolstrip =
extension->toolstrips().begin();
toolstrip != extension->toolstrips().end(); ++toolstrip) {
GURL url = toolstrip->toolstrip;
ToolstripItem item;
item.host = manager->CreateToolstrip(extension, url, browser_);
item.info = *toolstrip;
item.data = NULL;
item.height = 0;
AppendToolstrip(item);
}
}
void ExtensionShelfModel::AddExtensions(const ExtensionList* extensions) {
if (extensions->size()) {
ExtensionList::const_iterator extension = extensions->begin();
for (; extension != extensions->end(); ++extension)
AddExtension(*extension);
}
}
void ExtensionShelfModel::RemoveExtension(Extension* extension) {
bool changed = false;
for (int i = count() - 1; i >= 0; --i) {
ExtensionHost* t = ToolstripAt(i).host;
if (t->extension()->id() == extension->id()) {
changed = true;
RemoveToolstripAt(i);
// There can be more than one toolstrip per extension, so we have to keep
// looping even after finding a match.
}
}
if (changed)
UpdatePrefs();
}
void ExtensionShelfModel::UpdatePrefs() {
if (!prefs_)
return;
// It's easiest to just rebuild the list each time.
ExtensionPrefs::URLList urls;
for (int i = 0; i < count(); ++i)
urls.push_back(ToolstripAt(i).host->GetURL());
prefs_->SetShelfToolstripOrder(urls);
NotificationService::current()->Notify(
NotificationType::EXTENSION_SHELF_MODEL_CHANGED,
Source<ExtensionPrefs>(prefs_),
Details<ExtensionShelfModel>(this));
}
void ExtensionShelfModel::SortToolstrips() {
ExtensionPrefs::URLList urls = prefs_->GetShelfToolstripOrder();
ToolstripList copy =
ToolstripList(toolstrips_.begin(), toolstrips_.end());
toolstrips_.clear();
// Go through the urls and find the matching toolstrip, re-adding it to the
// new list in the proper order.
for (size_t i = 0; i < urls.size(); ++i) {
GURL& url = urls[i];
for (iterator toolstrip = copy.begin();
toolstrip != copy.end(); ++toolstrip) {
if (url == toolstrip->host->GetURL()) {
// Note that it's technically possible for the same URL to appear in
// multiple toolstrips, so we don't do any testing for uniqueness.
toolstrips_.push_back(*toolstrip);
// Remove the toolstrip from the list so we don't have to iterate over
// it next time.
copy.erase(toolstrip);
break;
}
}
}
// Any toolstrips remaining in |copy| were somehow missing from the prefs,
// so just append them to the end.
for (iterator toolstrip = copy.begin();
toolstrip != copy.end(); ++toolstrip) {
toolstrips_.push_back(*toolstrip);
}
FOR_EACH_OBSERVER(ExtensionShelfModelObserver, observers_,
ShelfModelReloaded());
}
|