1 // Copyright (c) 2013 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.
5 #include "extensions/common/manifest_handler.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/permissions/manifest_permission.h"
13 #include "extensions/common/permissions/manifest_permission_set.h"
15 namespace extensions
{
19 static base::LazyInstance
<ManifestHandlerRegistry
> g_registry
=
20 LAZY_INSTANCE_INITIALIZER
;
21 static ManifestHandlerRegistry
* g_registry_override
= NULL
;
23 ManifestHandlerRegistry
* GetRegistry() {
24 if (!g_registry_override
)
25 return g_registry
.Pointer();
26 return g_registry_override
;
31 ManifestHandler::ManifestHandler() {
34 ManifestHandler::~ManifestHandler() {
37 bool ManifestHandler::Validate(const Extension
* extension
,
39 std::vector
<InstallWarning
>* warnings
) const {
43 bool ManifestHandler::AlwaysParseForType(Manifest::Type type
) const {
47 bool ManifestHandler::AlwaysValidateForType(Manifest::Type type
) const {
51 const std::vector
<std::string
> ManifestHandler::PrerequisiteKeys() const {
52 return std::vector
<std::string
>();
55 void ManifestHandler::Register() {
56 linked_ptr
<ManifestHandler
> this_linked(this);
57 const std::vector
<std::string
> keys
= Keys();
58 for (size_t i
= 0; i
< keys
.size(); ++i
)
59 GetRegistry()->RegisterManifestHandler(keys
[i
], this_linked
);
62 ManifestPermission
* ManifestHandler::CreatePermission() {
66 ManifestPermission
* ManifestHandler::CreateInitialRequiredPermission(
67 const Extension
* extension
) {
72 void ManifestHandler::FinalizeRegistration() {
73 GetRegistry()->Finalize();
77 bool ManifestHandler::IsRegistrationFinalized() {
78 return GetRegistry()->is_finalized_
;
82 bool ManifestHandler::ParseExtension(Extension
* extension
, string16
* error
) {
83 return GetRegistry()->ParseExtension(extension
, error
);
87 bool ManifestHandler::ValidateExtension(const Extension
* extension
,
89 std::vector
<InstallWarning
>* warnings
) {
90 return GetRegistry()->ValidateExtension(extension
, error
, warnings
);
94 ManifestPermission
* ManifestHandler::CreatePermission(const std::string
& name
) {
95 return GetRegistry()->CreatePermission(name
);
99 void ManifestHandler::AddExtensionInitialRequiredPermissions(
100 const Extension
* extension
, ManifestPermissionSet
* permission_set
) {
101 return GetRegistry()->AddExtensionInitialRequiredPermissions(extension
,
106 const std::vector
<std::string
> ManifestHandler::SingleKey(
107 const std::string
& key
) {
108 return std::vector
<std::string
>(1, key
);
111 ManifestHandlerRegistry::ManifestHandlerRegistry() : is_finalized_(false) {
114 ManifestHandlerRegistry::~ManifestHandlerRegistry() {
117 void ManifestHandlerRegistry::Finalize() {
118 CHECK(!is_finalized_
);
119 SortManifestHandlers();
120 is_finalized_
= true;
123 void ManifestHandlerRegistry::RegisterManifestHandler(
124 const std::string
& key
, linked_ptr
<ManifestHandler
> handler
) {
125 CHECK(!is_finalized_
);
126 handlers_
[key
] = handler
;
129 bool ManifestHandlerRegistry::ParseExtension(Extension
* extension
,
131 std::map
<int, ManifestHandler
*> handlers_by_priority
;
132 for (ManifestHandlerMap::iterator iter
= handlers_
.begin();
133 iter
!= handlers_
.end(); ++iter
) {
134 ManifestHandler
* handler
= iter
->second
.get();
135 if (extension
->manifest()->HasPath(iter
->first
) ||
136 handler
->AlwaysParseForType(extension
->GetType())) {
137 handlers_by_priority
[priority_map_
[handler
]] = handler
;
140 for (std::map
<int, ManifestHandler
*>::iterator iter
=
141 handlers_by_priority
.begin();
142 iter
!= handlers_by_priority
.end(); ++iter
) {
143 if (!(iter
->second
)->Parse(extension
, error
))
149 bool ManifestHandlerRegistry::ValidateExtension(
150 const Extension
* extension
,
152 std::vector
<InstallWarning
>* warnings
) {
153 std::set
<ManifestHandler
*> handlers
;
154 for (ManifestHandlerMap::iterator iter
= handlers_
.begin();
155 iter
!= handlers_
.end(); ++iter
) {
156 ManifestHandler
* handler
= iter
->second
.get();
157 if (extension
->manifest()->HasPath(iter
->first
) ||
158 handler
->AlwaysValidateForType(extension
->GetType())) {
159 handlers
.insert(handler
);
162 for (std::set
<ManifestHandler
*>::iterator iter
= handlers
.begin();
163 iter
!= handlers
.end(); ++iter
) {
164 if (!(*iter
)->Validate(extension
, error
, warnings
))
170 ManifestPermission
* ManifestHandlerRegistry::CreatePermission(
171 const std::string
& name
) {
172 ManifestHandlerMap::const_iterator it
= handlers_
.find(name
);
173 if (it
== handlers_
.end())
176 return it
->second
->CreatePermission();
179 void ManifestHandlerRegistry::AddExtensionInitialRequiredPermissions(
180 const Extension
* extension
, ManifestPermissionSet
* permission_set
) {
181 for (ManifestHandlerMap::const_iterator it
= handlers_
.begin();
182 it
!= handlers_
.end(); ++it
) {
183 ManifestPermission
* permission
=
184 it
->second
->CreateInitialRequiredPermission(extension
);
186 permission_set
->insert(permission
);
192 ManifestHandlerRegistry
* ManifestHandlerRegistry::SetForTesting(
193 ManifestHandlerRegistry
* new_registry
) {
194 ManifestHandlerRegistry
* old_registry
= GetRegistry();
195 if (new_registry
!= g_registry
.Pointer())
196 g_registry_override
= new_registry
;
198 g_registry_override
= NULL
;
202 void ManifestHandlerRegistry::SortManifestHandlers() {
203 std::set
<ManifestHandler
*> unsorted_handlers
;
204 for (ManifestHandlerMap::const_iterator iter
= handlers_
.begin();
205 iter
!= handlers_
.end(); ++iter
) {
206 unsorted_handlers
.insert(iter
->second
.get());
211 std::set
<ManifestHandler
*> next_unsorted_handlers
;
212 for (std::set
<ManifestHandler
*>::const_iterator iter
=
213 unsorted_handlers
.begin();
214 iter
!= unsorted_handlers
.end(); ++iter
) {
215 ManifestHandler
* handler
= *iter
;
216 const std::vector
<std::string
>& prerequisites
=
217 handler
->PrerequisiteKeys();
218 int unsatisfied
= prerequisites
.size();
219 for (size_t i
= 0; i
< prerequisites
.size(); ++i
) {
220 ManifestHandlerMap::const_iterator prereq_iter
=
221 handlers_
.find(prerequisites
[i
]);
222 // If the prerequisite does not exist, crash.
223 CHECK(prereq_iter
!= handlers_
.end())
224 << "Extension manifest handler depends on unrecognized key "
226 // Prerequisite is in our map.
227 if (ContainsKey(priority_map_
, prereq_iter
->second
.get()))
230 if (unsatisfied
== 0) {
231 priority_map_
[handler
] = priority
;
234 // Put in the list for next time.
235 next_unsorted_handlers
.insert(handler
);
238 if (next_unsorted_handlers
.size() == unsorted_handlers
.size())
240 unsorted_handlers
.swap(next_unsorted_handlers
);
243 // If there are any leftover unsorted handlers, they must have had
244 // circular dependencies.
245 CHECK(unsorted_handlers
.size() == 0) << "Extension manifest handlers have "
246 << "circular dependencies!";
249 } // namespace extensions