| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include "mojo/services/catalog/builder.h" | |
| 6 | |
| 7 #include "base/values.h" | |
| 8 #include "mojo/services/catalog/store.h" | |
| 9 #include "mojo/shell/public/cpp/capabilities.h" | |
| 10 #include "mojo/shell/public/cpp/names.h" | |
| 11 | |
| 12 // TODO(beng): this code should do better error handling instead of CHECKing so | |
| 13 // much. | |
| 14 | |
| 15 namespace catalog { | |
| 16 | |
| 17 mojo::CapabilitySpec BuildCapabilitiesV0( | |
| 18 const base::DictionaryValue& value) { | |
| 19 mojo::CapabilitySpec capabilities; | |
| 20 base::DictionaryValue::Iterator it(value); | |
| 21 for (; !it.IsAtEnd(); it.Advance()) { | |
| 22 const base::ListValue* values = nullptr; | |
| 23 CHECK(it.value().GetAsList(&values)); | |
| 24 mojo::CapabilityRequest spec; | |
| 25 for (auto i = values->begin(); i != values->end(); ++i) { | |
| 26 mojo::Interface interface_name; | |
| 27 const base::Value* v = *i; | |
| 28 CHECK(v->GetAsString(&interface_name)); | |
| 29 spec.interfaces.insert(interface_name); | |
| 30 } | |
| 31 capabilities.required[it.key()] = spec; | |
| 32 } | |
| 33 return capabilities; | |
| 34 } | |
| 35 | |
| 36 void ReadStringSet(const base::ListValue& list_value, | |
| 37 std::set<std::string>* string_set) { | |
| 38 DCHECK(string_set); | |
| 39 for (auto i = list_value.begin(); i != list_value.end(); ++i) { | |
| 40 std::string value; | |
| 41 const base::Value* value_value = *i; | |
| 42 CHECK(value_value->GetAsString(&value)); | |
| 43 string_set->insert(value); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 void ReadStringSetFromValue(const base::Value& value, | |
| 48 std::set<std::string>* string_set) { | |
| 49 const base::ListValue* list_value = nullptr; | |
| 50 CHECK(value.GetAsList(&list_value)); | |
| 51 ReadStringSet(*list_value, string_set); | |
| 52 } | |
| 53 | |
| 54 void ReadStringSetFromDictionary(const base::DictionaryValue& dictionary, | |
| 55 const std::string& key, | |
| 56 std::set<std::string>* string_set) { | |
| 57 const base::ListValue* list_value = nullptr; | |
| 58 if (dictionary.HasKey(key)) | |
| 59 CHECK(dictionary.GetList(key, &list_value)); | |
| 60 if (list_value) | |
| 61 ReadStringSet(*list_value, string_set); | |
| 62 } | |
| 63 | |
| 64 mojo::CapabilitySpec BuildCapabilitiesV1( | |
| 65 const base::DictionaryValue& value) { | |
| 66 mojo::CapabilitySpec capabilities; | |
| 67 | |
| 68 const base::DictionaryValue* provided_value = nullptr; | |
| 69 if (value.HasKey(Store::kCapabilities_ProvidedKey)) { | |
| 70 CHECK(value.GetDictionary(Store::kCapabilities_ProvidedKey, | |
| 71 &provided_value)); | |
| 72 } | |
| 73 if (provided_value) { | |
| 74 mojo::CapabilityRequest provided; | |
| 75 base::DictionaryValue::Iterator it(*provided_value); | |
| 76 for(; !it.IsAtEnd(); it.Advance()) { | |
| 77 mojo::Interfaces interfaces; | |
| 78 ReadStringSetFromValue(it.value(), &interfaces); | |
| 79 capabilities.provided[it.key()] = interfaces; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 const base::DictionaryValue* required_value = nullptr; | |
| 84 if (value.HasKey(Store::kCapabilities_RequiredKey)) { | |
| 85 CHECK(value.GetDictionary(Store::kCapabilities_RequiredKey, | |
| 86 &required_value)); | |
| 87 } | |
| 88 if (required_value) { | |
| 89 base::DictionaryValue::Iterator it(*required_value); | |
| 90 for (; !it.IsAtEnd(); it.Advance()) { | |
| 91 mojo::CapabilityRequest spec; | |
| 92 const base::DictionaryValue* entry_value = nullptr; | |
| 93 CHECK(it.value().GetAsDictionary(&entry_value)); | |
| 94 ReadStringSetFromDictionary( | |
| 95 *entry_value, Store::kCapabilities_ClassesKey, &spec.classes); | |
| 96 ReadStringSetFromDictionary( | |
| 97 *entry_value, Store::kCapabilities_InterfacesKey, &spec.interfaces); | |
| 98 capabilities.required[it.key()] = spec; | |
| 99 } | |
| 100 } | |
| 101 return capabilities; | |
| 102 } | |
| 103 | |
| 104 Entry BuildEntry(const base::DictionaryValue& value) { | |
| 105 Entry entry; | |
| 106 int manifest_version = 0; | |
| 107 if (value.HasKey(Store::kManifestVersionKey)) | |
| 108 CHECK(value.GetInteger(Store::kManifestVersionKey, &manifest_version)); | |
| 109 std::string name_string; | |
| 110 CHECK(value.GetString(Store::kNameKey, &name_string)); | |
| 111 CHECK(mojo::IsValidName(name_string)) << "Invalid Name: " << name_string; | |
| 112 entry.name = name_string; | |
| 113 if (value.HasKey(Store::kQualifierKey)) { | |
| 114 CHECK(value.GetString(Store::kQualifierKey, &entry.qualifier)); | |
| 115 } else { | |
| 116 entry.qualifier = mojo::GetNamePath(name_string); | |
| 117 } | |
| 118 CHECK(value.GetString(Store::kDisplayNameKey, &entry.display_name)); | |
| 119 const base::DictionaryValue* capabilities = nullptr; | |
| 120 CHECK(value.GetDictionary(Store::kCapabilitiesKey, &capabilities)); | |
| 121 if (manifest_version == 0) | |
| 122 entry.capabilities = BuildCapabilitiesV0(*capabilities); | |
| 123 else | |
| 124 entry.capabilities = BuildCapabilitiesV1(*capabilities); | |
| 125 return entry; | |
| 126 } | |
| 127 | |
| 128 scoped_ptr<base::DictionaryValue> SerializeEntry(const Entry& entry) { | |
| 129 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); | |
| 130 value->SetInteger(Store::kManifestVersionKey, 1); | |
| 131 value->SetString(Store::kNameKey, entry.name); | |
| 132 value->SetString(Store::kDisplayNameKey, entry.display_name); | |
| 133 value->SetString(Store::kQualifierKey, entry.qualifier); | |
| 134 scoped_ptr<base::DictionaryValue> spec(new base::DictionaryValue); | |
| 135 | |
| 136 scoped_ptr<base::DictionaryValue> provided(new base::DictionaryValue); | |
| 137 for (const auto& i : entry.capabilities.provided) { | |
| 138 scoped_ptr<base::ListValue> interfaces(new base::ListValue); | |
| 139 for (const auto& interface_name : i.second) | |
| 140 interfaces->AppendString(interface_name); | |
| 141 provided->Set(i.first, std::move(interfaces)); | |
| 142 } | |
| 143 spec->Set(Store::kCapabilities_ProvidedKey, std::move(provided)); | |
| 144 | |
| 145 scoped_ptr<base::DictionaryValue> required(new base::DictionaryValue); | |
| 146 for (const auto& i : entry.capabilities.required) { | |
| 147 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue); | |
| 148 scoped_ptr<base::ListValue> classes(new base::ListValue); | |
| 149 for (const auto& class_name : i.second.classes) | |
| 150 classes->AppendString(class_name); | |
| 151 request->Set(Store::kCapabilities_ClassesKey, std::move(classes)); | |
| 152 scoped_ptr<base::ListValue> interfaces(new base::ListValue); | |
| 153 for (const auto& interface_name : i.second.interfaces) | |
| 154 interfaces->AppendString(interface_name); | |
| 155 request->Set(Store::kCapabilities_InterfacesKey, std::move(interfaces)); | |
| 156 required->Set(i.first, std::move(request)); | |
| 157 } | |
| 158 spec->Set(Store::kCapabilities_RequiredKey, std::move(required)); | |
| 159 | |
| 160 value->Set(Store::kCapabilitiesKey, std::move(spec)); | |
| 161 return value; | |
| 162 } | |
| 163 | |
| 164 } // namespace catalog | |
| OLD | NEW |