| 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { | 102 : api(ExtensionAPI::CreateWithDefaultConfiguration()) { |
| 103 } | 103 } |
| 104 std::unique_ptr<ExtensionAPI> api; | 104 std::unique_ptr<ExtensionAPI> api; |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; | 107 base::LazyInstance<Static> g_lazy_instance = LAZY_INSTANCE_INITIALIZER; |
| 108 | 108 |
| 109 // May override |g_lazy_instance| for a test. | 109 // May override |g_lazy_instance| for a test. |
| 110 ExtensionAPI* g_shared_instance_for_test = NULL; | 110 ExtensionAPI* g_shared_instance_for_test = NULL; |
| 111 | 111 |
| 112 // If it exists and does not already specify a namespace, then the value stored | |
| 113 // with key |key| in |schema| will be updated to |schema_namespace| + "." + | |
| 114 // |schema[key]|. | |
| 115 void MaybePrefixFieldWithNamespace(const std::string& schema_namespace, | |
| 116 base::DictionaryValue* schema, | |
| 117 const std::string& key) { | |
| 118 if (!schema->HasKey(key)) | |
| 119 return; | |
| 120 | |
| 121 std::string old_id; | |
| 122 CHECK(schema->GetString(key, &old_id)); | |
| 123 if (old_id.find(".") == std::string::npos) | |
| 124 schema->SetString(key, schema_namespace + "." + old_id); | |
| 125 } | |
| 126 | |
| 127 // Modify all "$ref" keys anywhere in |schema| to be prefxied by | |
| 128 // |schema_namespace| if they do not already specify a namespace. | |
| 129 void PrefixRefsWithNamespace(const std::string& schema_namespace, | |
| 130 base::Value* value) { | |
| 131 base::ListValue* list = NULL; | |
| 132 base::DictionaryValue* dict = NULL; | |
| 133 if (value->GetAsList(&list)) { | |
| 134 for (const auto& i : *list) { | |
| 135 PrefixRefsWithNamespace(schema_namespace, i.get()); | |
| 136 } | |
| 137 } else if (value->GetAsDictionary(&dict)) { | |
| 138 MaybePrefixFieldWithNamespace(schema_namespace, dict, "$ref"); | |
| 139 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) { | |
| 140 base::Value* value = NULL; | |
| 141 CHECK(dict->GetWithoutPathExpansion(i.key(), &value)); | |
| 142 PrefixRefsWithNamespace(schema_namespace, value); | |
| 143 } | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 // Modify all objects in the "types" section of the schema to be prefixed by | |
| 148 // |schema_namespace| if they do not already specify a namespace. | |
| 149 void PrefixTypesWithNamespace(const std::string& schema_namespace, | |
| 150 base::DictionaryValue* schema) { | |
| 151 if (!schema->HasKey("types")) | |
| 152 return; | |
| 153 | |
| 154 // Add the namespace to all of the types defined in this schema | |
| 155 base::ListValue *types = NULL; | |
| 156 CHECK(schema->GetList("types", &types)); | |
| 157 for (size_t i = 0; i < types->GetSize(); ++i) { | |
| 158 base::DictionaryValue *type = NULL; | |
| 159 CHECK(types->GetDictionary(i, &type)); | |
| 160 MaybePrefixFieldWithNamespace(schema_namespace, type, "id"); | |
| 161 MaybePrefixFieldWithNamespace(schema_namespace, type, "customBindings"); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 // Modify the schema so that all types are fully qualified. | |
| 166 void PrefixWithNamespace(const std::string& schema_namespace, | |
| 167 base::DictionaryValue* schema) { | |
| 168 PrefixTypesWithNamespace(schema_namespace, schema); | |
| 169 PrefixRefsWithNamespace(schema_namespace, schema); | |
| 170 } | |
| 171 | |
| 172 } // namespace | 112 } // namespace |
| 173 | 113 |
| 174 // static | 114 // static |
| 175 ExtensionAPI* ExtensionAPI::GetSharedInstance() { | 115 ExtensionAPI* ExtensionAPI::GetSharedInstance() { |
| 176 return g_shared_instance_for_test ? g_shared_instance_for_test | 116 return g_shared_instance_for_test ? g_shared_instance_for_test |
| 177 : g_lazy_instance.Get().api.get(); | 117 : g_lazy_instance.Get().api.get(); |
| 178 } | 118 } |
| 179 | 119 |
| 180 // static | 120 // static |
| 181 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() { | 121 ExtensionAPI* ExtensionAPI::CreateWithDefaultConfiguration() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 DCHECK(extensions_client); | 159 DCHECK(extensions_client); |
| 220 while (!schema_list->empty()) { | 160 while (!schema_list->empty()) { |
| 221 std::unique_ptr<base::DictionaryValue> schema; | 161 std::unique_ptr<base::DictionaryValue> schema; |
| 222 { | 162 { |
| 223 std::unique_ptr<base::Value> val; | 163 std::unique_ptr<base::Value> val; |
| 224 schema_list->Erase(schema_list->begin(), &val); | 164 schema_list->Erase(schema_list->begin(), &val); |
| 225 schema = base::DictionaryValue::From(std::move(val)); | 165 schema = base::DictionaryValue::From(std::move(val)); |
| 226 CHECK(schema); | 166 CHECK(schema); |
| 227 } | 167 } |
| 228 CHECK(schema->GetString("namespace", &schema_namespace)); | 168 CHECK(schema->GetString("namespace", &schema_namespace)); |
| 229 PrefixWithNamespace(schema_namespace, schema.get()); | |
| 230 schemas_[schema_namespace] = std::move(schema); | 169 schemas_[schema_namespace] = std::move(schema); |
| 231 if (!extensions_client->IsAPISchemaGenerated(schema_namespace)) | 170 if (!extensions_client->IsAPISchemaGenerated(schema_namespace)) |
| 232 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace)); | 171 CHECK_EQ(1u, unloaded_schemas_.erase(schema_namespace)); |
| 233 } | 172 } |
| 234 } | 173 } |
| 235 | 174 |
| 236 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { | 175 ExtensionAPI::ExtensionAPI() : default_configuration_initialized_(false) { |
| 237 } | 176 } |
| 238 | 177 |
| 239 ExtensionAPI::~ExtensionAPI() { | 178 ExtensionAPI::~ExtensionAPI() { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 381 break; | 320 break; |
| 382 | 321 |
| 383 api_name_candidate = api_name_candidate.substr(0, last_dot_index); | 322 api_name_candidate = api_name_candidate.substr(0, last_dot_index); |
| 384 } | 323 } |
| 385 | 324 |
| 386 *child_name = ""; | 325 *child_name = ""; |
| 387 return std::string(); | 326 return std::string(); |
| 388 } | 327 } |
| 389 | 328 |
| 390 } // namespace extensions | 329 } // namespace extensions |
| OLD | NEW |