| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/extension_api.h" | 5 #include "extensions/common/extension_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { | 105 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { |
| 106 } | 106 } |
| 107 std::unique_ptr<ExtensionAPI> api; | 107 std::unique_ptr<ExtensionAPI> api; |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; | 110 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; |
| 111 | 111 |
| 112 // May override |g_lazy_instance| for a test. | 112 // May override |g_lazy_instance| for a test. |
| 113 ExtensionAPI* g_shared_instance_for_test = NULL; | 113 ExtensionAPI* g_shared_instance_for_test = NULL; |
| 114 | 114 |
| 115 // If it exists and does not already specify a namespace, then the value stored | |
| 116 // with key |key| in |schema| will be updated to |schema_namespace| + "." + | |
| 117 // |schema[key]|. | |
| 118 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace, | |
| 119 base::DictionaryValue* schema, | |
| 120 const std::string& key) { | |
| 121 if (!schema->HasKey(key)) | |
| 122 return; | |
| 123 | |
| 124 std::string old_id; | |
| 125 CHECK(schema->GetString(key, &old_id)); | |
| 126 if (old_id.find(".") == std::string::npos) | |
| 127 schema->SetString(key, schema_namespace + "." + old_id); | |
| 128 } | |
| 129 | |
| 130 // Modify all "$ref" keys anywhere in |schema| to be prefxied by | |
| 131 // |schema_namespace| if they do not already specify a namespace. | |
| 132 void PrefixRefsWithNamespace(const std::string& schema_namespace, | |
| 133 base::Value* value) { | |
| 134 base::ListValue* list = NULL; | |
| 135 base::DictionaryValue* dict = NULL; | |
| 136 if (value->GetAsList(&list)) { | |
| 137 for (const auto& i : *list) { | |
| 138 PrefixRefsWithNamespace(schema_namespace, i.get()); | |
| 139 } | |
| 140 } else if (value->GetAsDictionary(&dict)) { | |
| 141 MaybePrefixFieldWithNamespace(schema_namespace, dict, "$ref"); | |
| 142 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { | |
| 143 base::Value* value = NULL; | |
| 144 CHECK(dict->GetWithoutPathExpansion(i.key(), &value)); | |
| 145 PrefixRefsWithNamespace(schema_namespace, value); | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 // Modify all objects in the "types" section of the schema to be prefixed by | |
| 151 // |schema_namespace| if they do not already specify a namespace. | |
| 152 void PrefixTypesWithNamespace(const std::string& schema_namespace, | |
| 153 base::DictionaryValue* schema) { | |
| 154 if (!schema->HasKey("types")) | |
| 155 return; | |
| 156 | |
| 157 // Add the namespace to all of the types defined in this schema | |
| 158 base::ListValue *types = NULL; | |
| 159 CHECK(schema->GetList("types", &types)); | |
| 160 for (size_t i = 0; i < types->GetSize(); ++i) { | |
| 161 base::DictionaryValue *type = NULL; | |
| 162 CHECK(types->GetDictionary(i, &type)); | |
| 163 MaybePrefixFieldWithNamespace(schema_namespace, type, "id"); | |
| 164 MaybePrefixFieldWithNamespace(schema_namespace, type, "customBindings"); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 // Modify the schema so that all types are fully qualified. | |
| 169 void PrefixWithNamespace(const std::string& schema_namespace, | |
| 170 base::DictionaryValue* schema) { | |
| 171 PrefixTypesWithNamespace(schema_namespace, schema); | |
| 172 PrefixRefsWithNamespace(schema_namespace, schema); | |
| 173 } | |
| 174 | |
| 175 } // namespace | 115 } // namespace |
| 176 | 116 |
| 177 // static | 117 // static |
| 178 ExtensionAPI* ExtensionAPI::GetSharedInstance() { | 118 ExtensionAPI* ExtensionAPI::GetSharedInstance() { |
| 179 return g_shared_instance_for_test ? g_shared_instance_for_test | 119 return g_shared_instance_for_test ? g_shared_instance_for_test |
| 180 : g_lazy_instance.Get().api.get(); | 120 : g_lazy_instance.Get().api.get(); |
| 181 } | 121 } |
| 182 | 122 |
| 183 // static | 123 // static |
| 184 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() { | 124 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 DCHECK(extensions_client); | 165 DCHECK(extensions_client); |
| 226 while (!schema_list->empty()) { | 166 while (!schema_list->empty()) { |
| 227 std::unique_ptr<base::DictionaryValue> schema; | 167 std::unique_ptr<base::DictionaryValue> schema; |
| 228 { | 168 { |
| 229 std::unique_ptr<base::Value> val; | 169 std::unique_ptr<base::Value> val; |
| 230 schema_list->Erase(schema_list->begin(), &val); | 170 schema_list->Erase(schema_list->begin(), &val); |
| 231 schema = base::DictionaryValue::From(std::move(val)); | 171 schema = base::DictionaryValue::From(std::move(val)); |
| 232 CHECK(schema); | 172 CHECK(schema); |
| 233 } | 173 } |
| 234 CHECK(schema->GetString("namespace", &schema_namespace)); | 174 CHECK(schema->GetString("namespace", &schema_namespace)); |
| 235 PrefixWithNamespace(schema_namespace, schema.get()); | |
| 236 schemas_[schema_namespace] = std::move(schema); | 175 schemas_[schema_namespace] = std::move(schema); |
| 237 } | 176 } |
| 238 } | 177 } |
| 239 | 178 |
| 240 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { | 179 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { |
| 241 } | 180 } |
| 242 | 181 |
| 243 ExtensionAPI::~ExtensionAPI() { | 182 ExtensionAPI::~ExtensionAPI() { |
| 244 } | 183 } |
| 245 | 184 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 return std::string(); | 320 return std::string(); |
| 382 } | 321 } |
| 383 | 322 |
| 384 bool ExtensionAPI::IsKnownAPI(const std::string& name, | 323 bool ExtensionAPI::IsKnownAPI(const std::string& name, |
| 385 ExtensionsClient* client) { | 324 ExtensionsClient* client) { |
| 386 return schemas_.find(name) != schemas_.end() || | 325 return schemas_.find(name) != schemas_.end() || |
| 387 client->IsAPISchemaGenerated(name); | 326 client->IsAPISchemaGenerated(name); |
| 388 } | 327 } |
| 389 | 328 |
| 390 } // namespace extensions | 329 } // namespace extensions |
| OLD | NEW |