rdevlin.cronin@chromium.org | 7eeab9e | 2013-01-15 04:08:33 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
yoz@chromium.org | 582d12b | 2013-11-06 18:42:33 | [diff] [blame] | 5 | #include "extensions/common/manifest_handler.h" |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 6 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 9 | #include <map> |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 10 | #include <vector> |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 11 | |
Hans Wennborg | 0997959 | 2020-04-27 12:34:30 | [diff] [blame] | 12 | #include "base/check.h" |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 13 | #include "base/stl_util.h" |
yoz@chromium.org | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 14 | #include "extensions/common/extension.h" |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 15 | #include "extensions/common/permissions/manifest_permission.h" |
| 16 | #include "extensions/common/permissions/manifest_permission_set.h" |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 17 | |
| 18 | namespace extensions { |
| 19 | |
| 20 | namespace { |
| 21 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 22 | static base::LazyInstance<ManifestHandlerRegistry>::DestructorAtExit |
| 23 | g_registry = LAZY_INSTANCE_INITIALIZER; |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 24 | static ManifestHandlerRegistry* g_registry_override = NULL; |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 25 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 26 | } // namespace |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 27 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 28 | ManifestHandler::ManifestHandler() { |
| 29 | } |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 30 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 31 | ManifestHandler::~ManifestHandler() { |
| 32 | } |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 33 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 34 | bool ManifestHandler::Validate(const Extension* extension, |
| 35 | std::string* error, |
| 36 | std::vector<InstallWarning>* warnings) const { |
| 37 | return true; |
| 38 | } |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 39 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 40 | bool ManifestHandler::AlwaysParseForType(Manifest::Type type) const { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | bool ManifestHandler::AlwaysValidateForType(Manifest::Type type) const { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | const std::vector<std::string> ManifestHandler::PrerequisiteKeys() const { |
| 49 | return std::vector<std::string>(); |
| 50 | } |
| 51 | |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 52 | ManifestPermission* ManifestHandler::CreatePermission() { |
| 53 | return NULL; |
| 54 | } |
| 55 | |
| 56 | ManifestPermission* ManifestHandler::CreateInitialRequiredPermission( |
| 57 | const Extension* extension) { |
| 58 | return NULL; |
| 59 | } |
| 60 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 61 | // static |
yoz@chromium.org | a31c398 | 2013-07-13 04:33:31 | [diff] [blame] | 62 | void ManifestHandler::FinalizeRegistration() { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 63 | ManifestHandlerRegistry::Get()->Finalize(); |
yoz@chromium.org | a31c398 | 2013-07-13 04:33:31 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // static |
| 67 | bool ManifestHandler::IsRegistrationFinalized() { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 68 | return ManifestHandlerRegistry::Get()->is_finalized_; |
yoz@chromium.org | a31c398 | 2013-07-13 04:33:31 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
dbeam@chromium.org | 0d163dc | 2013-12-20 23:48:36 | [diff] [blame] | 72 | bool ManifestHandler::ParseExtension(Extension* extension, |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 73 | std::u16string* error) { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 74 | return ManifestHandlerRegistry::Get()->ParseExtension(extension, error); |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // static |
| 78 | bool ManifestHandler::ValidateExtension(const Extension* extension, |
| 79 | std::string* error, |
| 80 | std::vector<InstallWarning>* warnings) { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 81 | return ManifestHandlerRegistry::Get()->ValidateExtension(extension, error, |
| 82 | warnings); |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // static |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 86 | ManifestPermission* ManifestHandler::CreatePermission(const std::string& name) { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 87 | return ManifestHandlerRegistry::Get()->CreatePermission(name); |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // static |
| 91 | void ManifestHandler::AddExtensionInitialRequiredPermissions( |
| 92 | const Extension* extension, ManifestPermissionSet* permission_set) { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 93 | return ManifestHandlerRegistry::Get()->AddExtensionInitialRequiredPermissions( |
| 94 | extension, permission_set); |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // static |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 98 | const std::vector<std::string> ManifestHandler::SingleKey( |
| 99 | const std::string& key) { |
| 100 | return std::vector<std::string>(1, key); |
| 101 | } |
| 102 | |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 103 | // static |
| 104 | const size_t ManifestHandlerRegistry::kHandlerMax; |
| 105 | |
yoz@chromium.org | a31c398 | 2013-07-13 04:33:31 | [diff] [blame] | 106 | ManifestHandlerRegistry::ManifestHandlerRegistry() : is_finalized_(false) { |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | ManifestHandlerRegistry::~ManifestHandlerRegistry() { |
| 110 | } |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 111 | |
yoz@chromium.org | a31c398 | 2013-07-13 04:33:31 | [diff] [blame] | 112 | void ManifestHandlerRegistry::Finalize() { |
| 113 | CHECK(!is_finalized_); |
| 114 | SortManifestHandlers(); |
| 115 | is_finalized_ = true; |
| 116 | } |
| 117 | |
Devlin Cronin | 2033d60 | 2018-12-14 23:54:49 | [diff] [blame] | 118 | void ManifestHandlerRegistry::RegisterHandler( |
| 119 | std::unique_ptr<ManifestHandler> handler) { |
yoz@chromium.org | a31c398 | 2013-07-13 04:33:31 | [diff] [blame] | 120 | CHECK(!is_finalized_); |
Devlin Cronin | cd24bd8 | 2018-12-14 03:36:08 | [diff] [blame] | 121 | |
Devlin Cronin | 2033d60 | 2018-12-14 23:54:49 | [diff] [blame] | 122 | ManifestHandler* raw_handler = handler.get(); |
| 123 | owned_manifest_handlers_.push_back(std::move(handler)); |
| 124 | |
| 125 | for (const char* key : raw_handler->Keys()) { |
| 126 | auto insertion = handlers_.emplace(key, raw_handler); |
| 127 | DCHECK(insertion.second) |
| 128 | << "A ManifestHandler was already registered for key: " << key; |
| 129 | } |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 130 | } |
| 131 | |
yoz@chromium.org | ce613ba | 2013-01-30 00:01:18 | [diff] [blame] | 132 | bool ManifestHandlerRegistry::ParseExtension(Extension* extension, |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 133 | std::u16string* error) { |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 134 | std::map<int, ManifestHandler*> handlers_by_priority; |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 135 | for (ManifestHandlerMap::iterator iter = handlers_.begin(); |
| 136 | iter != handlers_.end(); ++iter) { |
Devlin Cronin | 2033d60 | 2018-12-14 23:54:49 | [diff] [blame] | 137 | ManifestHandler* handler = iter->second; |
yoz@chromium.org | ce613ba | 2013-01-30 00:01:18 | [diff] [blame] | 138 | if (extension->manifest()->HasPath(iter->first) || |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 139 | handler->AlwaysParseForType(extension->GetType())) { |
| 140 | handlers_by_priority[priority_map_[handler]] = handler; |
| 141 | } |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 142 | } |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 143 | for (auto iter = handlers_by_priority.begin(); |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 144 | iter != handlers_by_priority.end(); ++iter) { |
| 145 | if (!(iter->second)->Parse(extension, error)) |
yoz@chromium.org | ce613ba | 2013-01-30 00:01:18 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | return true; |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 149 | } |
| 150 | |
yoz@chromium.org | 989ae86 | 2013-03-13 04:22:58 | [diff] [blame] | 151 | bool ManifestHandlerRegistry::ValidateExtension( |
| 152 | const Extension* extension, |
| 153 | std::string* error, |
| 154 | std::vector<InstallWarning>* warnings) { |
| 155 | std::set<ManifestHandler*> handlers; |
| 156 | for (ManifestHandlerMap::iterator iter = handlers_.begin(); |
| 157 | iter != handlers_.end(); ++iter) { |
Devlin Cronin | 2033d60 | 2018-12-14 23:54:49 | [diff] [blame] | 158 | ManifestHandler* handler = iter->second; |
yoz@chromium.org | 989ae86 | 2013-03-13 04:22:58 | [diff] [blame] | 159 | if (extension->manifest()->HasPath(iter->first) || |
| 160 | handler->AlwaysValidateForType(extension->GetType())) { |
| 161 | handlers.insert(handler); |
| 162 | } |
| 163 | } |
jdoerrie | a1e1598b | 2018-10-10 09:10:37 | [diff] [blame] | 164 | for (auto iter = handlers.begin(); iter != handlers.end(); ++iter) { |
yoz@chromium.org | 989ae86 | 2013-03-13 04:22:58 | [diff] [blame] | 165 | if (!(*iter)->Validate(extension, error, warnings)) |
| 166 | return false; |
| 167 | } |
| 168 | return true; |
| 169 | } |
| 170 | |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 171 | ManifestPermission* ManifestHandlerRegistry::CreatePermission( |
| 172 | const std::string& name) { |
| 173 | ManifestHandlerMap::const_iterator it = handlers_.find(name); |
| 174 | if (it == handlers_.end()) |
| 175 | return NULL; |
| 176 | |
| 177 | return it->second->CreatePermission(); |
| 178 | } |
| 179 | |
| 180 | void ManifestHandlerRegistry::AddExtensionInitialRequiredPermissions( |
| 181 | const Extension* extension, ManifestPermissionSet* permission_set) { |
| 182 | for (ManifestHandlerMap::const_iterator it = handlers_.begin(); |
| 183 | it != handlers_.end(); ++it) { |
Devlin Cronin | 033021e6 | 2018-11-27 18:21:45 | [diff] [blame] | 184 | std::unique_ptr<ManifestPermission> permission( |
| 185 | it->second->CreateInitialRequiredPermission(extension)); |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 186 | if (permission) { |
Devlin Cronin | 033021e6 | 2018-11-27 18:21:45 | [diff] [blame] | 187 | permission_set->insert(std::move(permission)); |
rpaquay@chromium.org | e737c44 | 2013-11-15 15:55:24 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 192 | // static |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 193 | ManifestHandlerRegistry* ManifestHandlerRegistry::Get() { |
| 194 | if (!g_registry_override) |
| 195 | return g_registry.Pointer(); |
| 196 | return g_registry_override; |
| 197 | } |
| 198 | |
| 199 | // static |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 200 | ManifestHandlerRegistry* ManifestHandlerRegistry::SetForTesting( |
| 201 | ManifestHandlerRegistry* new_registry) { |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 202 | ManifestHandlerRegistry* old_registry = ManifestHandlerRegistry::Get(); |
yoz@chromium.org | d197284 | 2013-05-01 17:07:32 | [diff] [blame] | 203 | if (new_registry != g_registry.Pointer()) |
| 204 | g_registry_override = new_registry; |
| 205 | else |
| 206 | g_registry_override = NULL; |
| 207 | return old_registry; |
| 208 | } |
| 209 | |
David Bertoni | c041689 | 2018-08-15 00:07:20 | [diff] [blame] | 210 | // static |
| 211 | void ManifestHandlerRegistry::ResetForTesting() { |
| 212 | ManifestHandlerRegistry* registry = Get(); |
| 213 | registry->priority_map_.clear(); |
| 214 | registry->handlers_.clear(); |
| 215 | registry->is_finalized_ = false; |
| 216 | } |
| 217 | |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 218 | void ManifestHandlerRegistry::SortManifestHandlers() { |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 219 | std::vector<ManifestHandler*> unsorted_handlers; |
| 220 | unsorted_handlers.reserve(handlers_.size()); |
| 221 | for (const auto& key_value : handlers_) { |
Devlin Cronin | 2033d60 | 2018-12-14 23:54:49 | [diff] [blame] | 222 | unsorted_handlers.push_back(key_value.second); |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int priority = 0; |
| 226 | while (true) { |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 227 | std::vector<ManifestHandler*> next_unsorted_handlers; |
| 228 | next_unsorted_handlers.reserve(unsorted_handlers.size()); |
| 229 | for (ManifestHandler* handler : unsorted_handlers) { |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 230 | const std::vector<std::string>& prerequisites = |
| 231 | handler->PrerequisiteKeys(); |
| 232 | int unsatisfied = prerequisites.size(); |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 233 | for (const std::string& key : prerequisites) { |
| 234 | ManifestHandlerMap::const_iterator prereq_iter = handlers_.find(key); |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 235 | // If the prerequisite does not exist, crash. |
| 236 | CHECK(prereq_iter != handlers_.end()) |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 237 | << "Extension manifest handler depends on unrecognized key " << key; |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 238 | // Prerequisite is in our map. |
Jan Wilken Dörrie | 0fd53a2 | 2019-06-07 09:55:46 | [diff] [blame] | 239 | if (base::Contains(priority_map_, prereq_iter->second)) |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 240 | unsatisfied--; |
| 241 | } |
| 242 | if (unsatisfied == 0) { |
| 243 | priority_map_[handler] = priority; |
| 244 | priority++; |
| 245 | } else { |
| 246 | // Put in the list for next time. |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 247 | next_unsorted_handlers.push_back(handler); |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | if (next_unsorted_handlers.size() == unsorted_handlers.size()) |
| 251 | break; |
| 252 | unsorted_handlers.swap(next_unsorted_handlers); |
| 253 | } |
| 254 | |
| 255 | // If there are any leftover unsorted handlers, they must have had |
| 256 | // circular dependencies. |
David Bertoni | 88a71c18 | 2018-09-05 01:35:55 | [diff] [blame] | 257 | CHECK(unsorted_handlers.empty()) |
lauren.yeun.kim@gmail.com | 1b9280945 | 2014-05-29 20:20:27 | [diff] [blame] | 258 | << "Extension manifest handlers have circular dependencies!"; |
yoz@chromium.org | 03e0753 | 2013-02-05 00:26:24 | [diff] [blame] | 259 | } |
| 260 | |
yoz@chromium.org | d356c98 | 2012-12-12 19:32:55 | [diff] [blame] | 261 | } // namespace extensions |