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
|
// Copyright (c) 2013 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 "extensions/common/manifest_handler.h"
#include <stddef.h>
#include <map>
#include "base/logging.h"
#include "base/stl_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/permissions/manifest_permission.h"
#include "extensions/common/permissions/manifest_permission_set.h"
namespace extensions {
namespace {
static base::LazyInstance<ManifestHandlerRegistry> g_registry =
LAZY_INSTANCE_INITIALIZER;
static ManifestHandlerRegistry* g_registry_override = NULL;
ManifestHandlerRegistry* GetRegistry() {
if (!g_registry_override)
return g_registry.Pointer();
return g_registry_override;
}
} // namespace
ManifestHandler::ManifestHandler() {
}
ManifestHandler::~ManifestHandler() {
}
bool ManifestHandler::Validate(const Extension* extension,
std::string* error,
std::vector<InstallWarning>* warnings) const {
return true;
}
bool ManifestHandler::AlwaysParseForType(Manifest::Type type) const {
return false;
}
bool ManifestHandler::AlwaysValidateForType(Manifest::Type type) const {
return false;
}
const std::vector<std::string> ManifestHandler::PrerequisiteKeys() const {
return std::vector<std::string>();
}
void ManifestHandler::Register() {
linked_ptr<ManifestHandler> this_linked(this);
const std::vector<std::string> keys = Keys();
for (size_t i = 0; i < keys.size(); ++i)
GetRegistry()->RegisterManifestHandler(keys[i], this_linked);
}
ManifestPermission* ManifestHandler::CreatePermission() {
return NULL;
}
ManifestPermission* ManifestHandler::CreateInitialRequiredPermission(
const Extension* extension) {
return NULL;
}
// static
void ManifestHandler::FinalizeRegistration() {
GetRegistry()->Finalize();
}
// static
bool ManifestHandler::IsRegistrationFinalized() {
return GetRegistry()->is_finalized_;
}
// static
bool ManifestHandler::ParseExtension(Extension* extension,
base::string16* error) {
return GetRegistry()->ParseExtension(extension, error);
}
// static
bool ManifestHandler::ValidateExtension(const Extension* extension,
std::string* error,
std::vector<InstallWarning>* warnings) {
return GetRegistry()->ValidateExtension(extension, error, warnings);
}
// static
ManifestPermission* ManifestHandler::CreatePermission(const std::string& name) {
return GetRegistry()->CreatePermission(name);
}
// static
void ManifestHandler::AddExtensionInitialRequiredPermissions(
const Extension* extension, ManifestPermissionSet* permission_set) {
return GetRegistry()->AddExtensionInitialRequiredPermissions(extension,
permission_set);
}
// static
const std::vector<std::string> ManifestHandler::SingleKey(
const std::string& key) {
return std::vector<std::string>(1, key);
}
ManifestHandlerRegistry::ManifestHandlerRegistry() : is_finalized_(false) {
}
ManifestHandlerRegistry::~ManifestHandlerRegistry() {
}
void ManifestHandlerRegistry::Finalize() {
CHECK(!is_finalized_);
SortManifestHandlers();
is_finalized_ = true;
}
void ManifestHandlerRegistry::RegisterManifestHandler(
const std::string& key, linked_ptr<ManifestHandler> handler) {
CHECK(!is_finalized_);
handlers_[key] = handler;
}
bool ManifestHandlerRegistry::ParseExtension(Extension* extension,
base::string16* error) {
std::map<int, ManifestHandler*> handlers_by_priority;
for (ManifestHandlerMap::iterator iter = handlers_.begin();
iter != handlers_.end(); ++iter) {
ManifestHandler* handler = iter->second.get();
if (extension->manifest()->HasPath(iter->first) ||
handler->AlwaysParseForType(extension->GetType())) {
handlers_by_priority[priority_map_[handler]] = handler;
}
}
for (std::map<int, ManifestHandler*>::iterator iter =
handlers_by_priority.begin();
iter != handlers_by_priority.end(); ++iter) {
if (!(iter->second)->Parse(extension, error))
return false;
}
return true;
}
bool ManifestHandlerRegistry::ValidateExtension(
const Extension* extension,
std::string* error,
std::vector<InstallWarning>* warnings) {
std::set<ManifestHandler*> handlers;
for (ManifestHandlerMap::iterator iter = handlers_.begin();
iter != handlers_.end(); ++iter) {
ManifestHandler* handler = iter->second.get();
if (extension->manifest()->HasPath(iter->first) ||
handler->AlwaysValidateForType(extension->GetType())) {
handlers.insert(handler);
}
}
for (std::set<ManifestHandler*>::iterator iter = handlers.begin();
iter != handlers.end(); ++iter) {
if (!(*iter)->Validate(extension, error, warnings))
return false;
}
return true;
}
ManifestPermission* ManifestHandlerRegistry::CreatePermission(
const std::string& name) {
ManifestHandlerMap::const_iterator it = handlers_.find(name);
if (it == handlers_.end())
return NULL;
return it->second->CreatePermission();
}
void ManifestHandlerRegistry::AddExtensionInitialRequiredPermissions(
const Extension* extension, ManifestPermissionSet* permission_set) {
for (ManifestHandlerMap::const_iterator it = handlers_.begin();
it != handlers_.end(); ++it) {
ManifestPermission* permission =
it->second->CreateInitialRequiredPermission(extension);
if (permission) {
permission_set->insert(permission);
}
}
}
// static
ManifestHandlerRegistry* ManifestHandlerRegistry::SetForTesting(
ManifestHandlerRegistry* new_registry) {
ManifestHandlerRegistry* old_registry = GetRegistry();
if (new_registry != g_registry.Pointer())
g_registry_override = new_registry;
else
g_registry_override = NULL;
return old_registry;
}
void ManifestHandlerRegistry::SortManifestHandlers() {
std::set<ManifestHandler*> unsorted_handlers;
for (ManifestHandlerMap::const_iterator iter = handlers_.begin();
iter != handlers_.end(); ++iter) {
unsorted_handlers.insert(iter->second.get());
}
int priority = 0;
while (true) {
std::set<ManifestHandler*> next_unsorted_handlers;
for (std::set<ManifestHandler*>::const_iterator iter =
unsorted_handlers.begin();
iter != unsorted_handlers.end(); ++iter) {
ManifestHandler* handler = *iter;
const std::vector<std::string>& prerequisites =
handler->PrerequisiteKeys();
int unsatisfied = prerequisites.size();
for (size_t i = 0; i < prerequisites.size(); ++i) {
ManifestHandlerMap::const_iterator prereq_iter =
handlers_.find(prerequisites[i]);
// If the prerequisite does not exist, crash.
CHECK(prereq_iter != handlers_.end())
<< "Extension manifest handler depends on unrecognized key "
<< prerequisites[i];
// Prerequisite is in our map.
if (ContainsKey(priority_map_, prereq_iter->second.get()))
unsatisfied--;
}
if (unsatisfied == 0) {
priority_map_[handler] = priority;
priority++;
} else {
// Put in the list for next time.
next_unsorted_handlers.insert(handler);
}
}
if (next_unsorted_handlers.size() == unsorted_handlers.size())
break;
unsorted_handlers.swap(next_unsorted_handlers);
}
// If there are any leftover unsorted handlers, they must have had
// circular dependencies.
CHECK_EQ(unsorted_handlers.size(), std::set<ManifestHandler*>::size_type(0))
<< "Extension manifest handlers have circular dependencies!";
}
} // namespace extensions
|