Ignore non-active fullscreen windows for shelf state.
[chromium-blink-merge.git] / extensions / common / manifest_handler.cc
blob4546f3283abe2d4e0ee2f4e6fd2362f5b3ea6174
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"
7 #include <map>
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 {
17 namespace {
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;
29 } // namespace
31 ManifestHandler::ManifestHandler() {
34 ManifestHandler::~ManifestHandler() {
37 bool ManifestHandler::Validate(const Extension* extension,
38 std::string* error,
39 std::vector<InstallWarning>* warnings) const {
40 return true;
43 bool ManifestHandler::AlwaysParseForType(Manifest::Type type) const {
44 return false;
47 bool ManifestHandler::AlwaysValidateForType(Manifest::Type type) const {
48 return false;
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() {
63 return NULL;
66 ManifestPermission* ManifestHandler::CreateInitialRequiredPermission(
67 const Extension* extension) {
68 return NULL;
71 // static
72 void ManifestHandler::FinalizeRegistration() {
73 GetRegistry()->Finalize();
76 // static
77 bool ManifestHandler::IsRegistrationFinalized() {
78 return GetRegistry()->is_finalized_;
81 // static
82 bool ManifestHandler::ParseExtension(Extension* extension, string16* error) {
83 return GetRegistry()->ParseExtension(extension, error);
86 // static
87 bool ManifestHandler::ValidateExtension(const Extension* extension,
88 std::string* error,
89 std::vector<InstallWarning>* warnings) {
90 return GetRegistry()->ValidateExtension(extension, error, warnings);
93 // static
94 ManifestPermission* ManifestHandler::CreatePermission(const std::string& name) {
95 return GetRegistry()->CreatePermission(name);
98 // static
99 void ManifestHandler::AddExtensionInitialRequiredPermissions(
100 const Extension* extension, ManifestPermissionSet* permission_set) {
101 return GetRegistry()->AddExtensionInitialRequiredPermissions(extension,
102 permission_set);
105 // static
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,
130 string16* error) {
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))
144 return false;
146 return true;
149 bool ManifestHandlerRegistry::ValidateExtension(
150 const Extension* extension,
151 std::string* error,
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))
165 return false;
167 return true;
170 ManifestPermission* ManifestHandlerRegistry::CreatePermission(
171 const std::string& name) {
172 ManifestHandlerMap::const_iterator it = handlers_.find(name);
173 if (it == handlers_.end())
174 return NULL;
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);
185 if (permission) {
186 permission_set->insert(permission);
191 // static
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;
197 else
198 g_registry_override = NULL;
199 return old_registry;
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());
209 int priority = 0;
210 while (true) {
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 "
225 << prerequisites[i];
226 // Prerequisite is in our map.
227 if (ContainsKey(priority_map_, prereq_iter->second.get()))
228 unsatisfied--;
230 if (unsatisfied == 0) {
231 priority_map_[handler] = priority;
232 priority++;
233 } else {
234 // Put in the list for next time.
235 next_unsorted_handlers.insert(handler);
238 if (next_unsorted_handlers.size() == unsorted_handlers.size())
239 break;
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