Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/services/catalog/builder.h" | 5 #include "mojo/services/catalog/builder.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "mojo/services/catalog/store.h" | 8 #include "mojo/services/catalog/store.h" |
| 9 #include "mojo/shell/public/cpp/capabilities.h" | |
| 9 #include "mojo/shell/public/cpp/names.h" | 10 #include "mojo/shell/public/cpp/names.h" |
| 10 | 11 |
| 12 // TODO(beng): this code should do better error handling instead of CHECKing so | |
| 13 // much. | |
| 14 | |
| 11 namespace catalog { | 15 namespace catalog { |
| 12 | 16 |
| 13 CapabilityFilter BuildCapabilityFilter(const base::DictionaryValue& value) { | 17 mojo::caps::CapabilitySpec BuildCapabilitiesV0( |
| 14 CapabilityFilter filter; | 18 const base::DictionaryValue& value) { |
| 19 mojo::caps::CapabilitySpec capabilities; | |
| 15 base::DictionaryValue::Iterator it(value); | 20 base::DictionaryValue::Iterator it(value); |
| 16 for (; !it.IsAtEnd(); it.Advance()) { | 21 for (; !it.IsAtEnd(); it.Advance()) { |
| 17 const base::ListValue* values = nullptr; | 22 const base::ListValue* values = nullptr; |
| 18 CHECK(it.value().GetAsList(&values)); | 23 CHECK(it.value().GetAsList(&values)); |
| 19 AllowedInterfaces interfaces; | 24 mojo::caps::CapabilityRequest spec; |
| 20 for (auto i = values->begin(); i != values->end(); ++i) { | 25 for (auto i = values->begin(); i != values->end(); ++i) { |
| 21 std::string iface_name; | 26 mojo::caps::Interface interface_name; |
| 22 const base::Value* v = *i; | 27 const base::Value* v = *i; |
| 23 CHECK(v->GetAsString(&iface_name)); | 28 CHECK(v->GetAsString(&interface_name)); |
| 24 interfaces.insert(iface_name); | 29 spec.interfaces.insert(interface_name); |
| 25 } | 30 } |
| 26 filter[it.key()] = interfaces; | 31 capabilities.required[it.key()] = spec; |
| 27 } | 32 } |
| 28 return filter; | 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::caps::CapabilitySpec BuildCapabilitiesV1( | |
| 65 const base::DictionaryValue& value) { | |
| 66 mojo::caps::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::caps::CapabilityRequest provided; | |
| 75 base::DictionaryValue::Iterator it(*provided_value); | |
| 76 for(; !it.IsAtEnd(); it.Advance()) { | |
| 77 mojo::caps::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::caps::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; | |
| 29 } | 102 } |
| 30 | 103 |
| 31 Entry BuildEntry(const base::DictionaryValue& value) { | 104 Entry BuildEntry(const base::DictionaryValue& value) { |
| 32 Entry entry; | 105 Entry entry; |
| 106 int manifest_version = 0; | |
| 107 if (value.HasKey(Store::kManifestVersionKey)) | |
| 108 CHECK(value.GetInteger(Store::kManifestVersionKey, &manifest_version)); | |
| 33 std::string name_string; | 109 std::string name_string; |
| 34 CHECK(value.GetString(Store::kNameKey, &name_string)); | 110 CHECK(value.GetString(Store::kNameKey, &name_string)); |
| 35 CHECK(mojo::IsValidName(name_string)) << "Invalid Name: " << name_string; | 111 CHECK(mojo::IsValidName(name_string)) << "Invalid Name: " << name_string; |
| 36 entry.name = name_string; | 112 entry.name = name_string; |
| 37 if (value.HasKey(Store::kQualifierKey)) { | 113 if (value.HasKey(Store::kQualifierKey)) { |
| 38 CHECK(value.GetString(Store::kQualifierKey, &entry.qualifier)); | 114 CHECK(value.GetString(Store::kQualifierKey, &entry.qualifier)); |
| 39 } else { | 115 } else { |
| 40 entry.qualifier = mojo::GetNamePath(name_string); | 116 entry.qualifier = mojo::GetNamePath(name_string); |
| 41 } | 117 } |
| 42 CHECK(value.GetString(Store::kDisplayNameKey, &entry.display_name)); | 118 CHECK(value.GetString(Store::kDisplayNameKey, &entry.display_name)); |
| 43 const base::DictionaryValue* capabilities = nullptr; | 119 const base::DictionaryValue* capabilities = nullptr; |
| 44 CHECK(value.GetDictionary(Store::kCapabilitiesKey, &capabilities)); | 120 CHECK(value.GetDictionary(Store::kCapabilitiesKey, &capabilities)); |
| 45 entry.capabilities = BuildCapabilityFilter(*capabilities); | 121 if (manifest_version == 0) |
| 46 | 122 entry.capabilities = BuildCapabilitiesV0(*capabilities); |
| 123 else | |
| 124 entry.capabilities = BuildCapabilitiesV1(*capabilities); | |
| 47 return entry; | 125 return entry; |
| 48 } | 126 } |
| 49 | 127 |
| 50 void SerializeEntry(const Entry& entry, base::DictionaryValue** value) { | 128 void SerializeEntry(const Entry& entry, base::DictionaryValue** value) { |
| 51 *value = new base::DictionaryValue; | 129 *value = new base::DictionaryValue; |
| 130 (*value)->SetInteger(Store::kManifestVersionKey, 1); | |
| 52 (*value)->SetString(Store::kNameKey, entry.name); | 131 (*value)->SetString(Store::kNameKey, entry.name); |
| 53 (*value)->SetString(Store::kDisplayNameKey, entry.display_name); | 132 (*value)->SetString(Store::kDisplayNameKey, entry.display_name); |
| 54 base::DictionaryValue* capabilities = new base::DictionaryValue; | 133 (*value)->SetString(Store::kQualifierKey, entry.qualifier); |
| 55 for (const auto& pair : entry.capabilities) { | 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) { | |
| 56 scoped_ptr<base::ListValue> interfaces(new base::ListValue); | 138 scoped_ptr<base::ListValue> interfaces(new base::ListValue); |
| 57 for (const auto& iface_name : pair.second) | 139 for (const auto& interface_name : i.second) |
| 58 interfaces->AppendString(iface_name); | 140 interfaces->AppendString(interface_name); |
| 59 capabilities->Set(pair.first, std::move(interfaces)); | 141 provided->Set(i.first, std::move(interfaces)); |
| 60 } | 142 } |
| 61 (*value)->Set(Store::kCapabilitiesKey, make_scoped_ptr(capabilities)); | 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 base::DictionaryValue* request = new base::DictionaryValue; | |
|
sky
2016/03/09 21:33:15
scoped_ptr<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)); | |
| 62 } | 161 } |
| 63 | 162 |
| 64 } // namespace catalog | 163 } // namespace catalog |
| OLD | NEW |