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
|
// Copyright (c) 2010 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_toolbar_model.h"
#include "chrome/browser/extensions/extension_prefs.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
ExtensionToolbarModel::ExtensionToolbarModel(ExtensionsService* service)
: service_(service) {
DCHECK(service_);
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
Source<Profile>(service_->profile()));
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
Source<Profile>(service_->profile()));
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
Source<Profile>(service_->profile()));
registrar_.Add(this, NotificationType::EXTENSIONS_READY,
Source<Profile>(service_->profile()));
}
ExtensionToolbarModel::~ExtensionToolbarModel() {
}
void ExtensionToolbarModel::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void ExtensionToolbarModel::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void ExtensionToolbarModel::MoveBrowserAction(Extension* extension,
int index) {
ExtensionList::iterator pos = std::find(begin(), end(), extension);
if (pos == end()) {
NOTREACHED();
return;
}
toolitems_.erase(pos);
int i = 0;
bool inserted = false;
for (ExtensionList::iterator iter = begin(); iter != end(); ++iter, ++i) {
if (i == index) {
toolitems_.insert(iter, extension);
inserted = true;
break;
}
}
if (!inserted) {
DCHECK_EQ(index, static_cast<int>(toolitems_.size()));
index = toolitems_.size();
toolitems_.push_back(extension);
}
FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index));
UpdatePrefs();
}
void ExtensionToolbarModel::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == NotificationType::EXTENSIONS_READY) {
InitializeExtensionList();
return;
}
if (!service_->is_ready())
return;
Extension* extension = Details<Extension>(details).ptr();
if (type == NotificationType::EXTENSION_LOADED) {
AddExtension(extension);
} else if (type == NotificationType::EXTENSION_UNLOADED ||
type == NotificationType::EXTENSION_UNLOADED_DISABLED) {
RemoveExtension(extension);
} else {
NOTREACHED() << "Received unexpected notification";
}
}
void ExtensionToolbarModel::AddExtension(Extension* extension) {
// We only care about extensions with browser actions.
if (!extension->browser_action())
return;
if (extension->id() == last_extension_removed_ &&
last_extension_removed_index_ < toolitems_.size()) {
toolitems_.insert(begin() + last_extension_removed_index_, extension);
FOR_EACH_OBSERVER(Observer, observers_,
BrowserActionAdded(extension, last_extension_removed_index_));
} else {
toolitems_.push_back(extension);
FOR_EACH_OBSERVER(Observer, observers_,
BrowserActionAdded(extension, toolitems_.size() - 1));
}
last_extension_removed_ = "";
last_extension_removed_index_ = -1;
UpdatePrefs();
}
void ExtensionToolbarModel::RemoveExtension(Extension* extension) {
ExtensionList::iterator pos = std::find(begin(), end(), extension);
if (pos == end()) {
return;
}
last_extension_removed_ = extension->id();
last_extension_removed_index_ = pos - begin();
toolitems_.erase(pos);
FOR_EACH_OBSERVER(Observer, observers_,
BrowserActionRemoved(extension));
UpdatePrefs();
}
// Combine the currently enabled extensions that have browser actions (which
// we get from the ExtensionsService) with the ordering we get from the
// pref service. For robustness we use a somewhat inefficient process:
// 1. Create a vector of extensions sorted by their pref values. This vector may
// have holes.
// 2. Create a vector of extensions that did not have a pref value.
// 3. Remove holes from the sorted vector and append the unsorted vector.
void ExtensionToolbarModel::InitializeExtensionList() {
DCHECK(service_->is_ready());
std::vector<std::string> pref_order = service_->extension_prefs()->
GetToolbarOrder();
// Items that have a pref for their position.
ExtensionList sorted;
sorted.resize(pref_order.size(), NULL);
// The items that don't have a pref for their position.
ExtensionList unsorted;
// Create the lists.
for (size_t i = 0; i < service_->extensions()->size(); ++i) {
Extension* extension = service_->extensions()->at(i);
if (!extension->browser_action())
continue;
std::vector<std::string>::iterator pos =
std::find(pref_order.begin(), pref_order.end(), extension->id());
if (pos != pref_order.end()) {
int index = std::distance(pref_order.begin(), pos);
sorted[index] = extension;
} else {
unsorted.push_back(extension);
}
}
// Merge the lists.
toolitems_.reserve(sorted.size() + unsorted.size());
for (ExtensionList::iterator iter = sorted.begin();
iter != sorted.end(); ++iter) {
if (*iter != NULL)
toolitems_.push_back(*iter);
}
toolitems_.insert(toolitems_.end(), unsorted.begin(), unsorted.end());
// Inform observers.
for (size_t i = 0; i < toolitems_.size(); i++) {
FOR_EACH_OBSERVER(Observer, observers_,
BrowserActionAdded(toolitems_[i], i));
}
UpdatePrefs();
}
void ExtensionToolbarModel::UpdatePrefs() {
if (!service_->extension_prefs())
return;
std::vector<std::string> ids;
ids.reserve(toolitems_.size());
for (ExtensionList::iterator iter = begin(); iter != end(); ++iter)
ids.push_back((*iter)->id());
service_->extension_prefs()->SetToolbarOrder(ids);
}
|