| 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/entry.h" | 5 #include "mojo/services/catalog/entry.h" |
| 6 | 6 |
| 7 #include "base/values.h" |
| 8 #include "mojo/services/catalog/store.h" |
| 9 #include "mojo/shell/public/cpp/names.h" |
| 10 |
| 7 namespace catalog { | 11 namespace catalog { |
| 12 namespace { |
| 13 |
| 14 mojo::CapabilitySpec BuildCapabilitiesV0( |
| 15 const base::DictionaryValue& value) { |
| 16 mojo::CapabilitySpec capabilities; |
| 17 base::DictionaryValue::Iterator it(value); |
| 18 for (; !it.IsAtEnd(); it.Advance()) { |
| 19 const base::ListValue* values = nullptr; |
| 20 CHECK(it.value().GetAsList(&values)); |
| 21 mojo::CapabilityRequest spec; |
| 22 for (auto i = values->begin(); i != values->end(); ++i) { |
| 23 mojo::Interface interface_name; |
| 24 const base::Value* v = *i; |
| 25 CHECK(v->GetAsString(&interface_name)); |
| 26 spec.interfaces.insert(interface_name); |
| 27 } |
| 28 capabilities.required[it.key()] = spec; |
| 29 } |
| 30 return capabilities; |
| 31 } |
| 32 |
| 33 void ReadStringSet(const base::ListValue& list_value, |
| 34 std::set<std::string>* string_set) { |
| 35 DCHECK(string_set); |
| 36 for (auto i = list_value.begin(); i != list_value.end(); ++i) { |
| 37 std::string value; |
| 38 const base::Value* value_value = *i; |
| 39 CHECK(value_value->GetAsString(&value)); |
| 40 string_set->insert(value); |
| 41 } |
| 42 } |
| 43 |
| 44 void ReadStringSetFromValue(const base::Value& value, |
| 45 std::set<std::string>* string_set) { |
| 46 const base::ListValue* list_value = nullptr; |
| 47 CHECK(value.GetAsList(&list_value)); |
| 48 ReadStringSet(*list_value, string_set); |
| 49 } |
| 50 |
| 51 void ReadStringSetFromDictionary(const base::DictionaryValue& dictionary, |
| 52 const std::string& key, |
| 53 std::set<std::string>* string_set) { |
| 54 const base::ListValue* list_value = nullptr; |
| 55 if (dictionary.HasKey(key)) |
| 56 CHECK(dictionary.GetList(key, &list_value)); |
| 57 if (list_value) |
| 58 ReadStringSet(*list_value, string_set); |
| 59 } |
| 60 |
| 61 mojo::CapabilitySpec BuildCapabilitiesV1( |
| 62 const base::DictionaryValue& value) { |
| 63 mojo::CapabilitySpec capabilities; |
| 64 |
| 65 const base::DictionaryValue* provided_value = nullptr; |
| 66 if (value.HasKey(Store::kCapabilities_ProvidedKey)) { |
| 67 CHECK(value.GetDictionary(Store::kCapabilities_ProvidedKey, |
| 68 &provided_value)); |
| 69 } |
| 70 if (provided_value) { |
| 71 mojo::CapabilityRequest provided; |
| 72 base::DictionaryValue::Iterator it(*provided_value); |
| 73 for(; !it.IsAtEnd(); it.Advance()) { |
| 74 mojo::Interfaces interfaces; |
| 75 ReadStringSetFromValue(it.value(), &interfaces); |
| 76 capabilities.provided[it.key()] = interfaces; |
| 77 } |
| 78 } |
| 79 |
| 80 const base::DictionaryValue* required_value = nullptr; |
| 81 if (value.HasKey(Store::kCapabilities_RequiredKey)) { |
| 82 CHECK(value.GetDictionary(Store::kCapabilities_RequiredKey, |
| 83 &required_value)); |
| 84 } |
| 85 if (required_value) { |
| 86 base::DictionaryValue::Iterator it(*required_value); |
| 87 for (; !it.IsAtEnd(); it.Advance()) { |
| 88 mojo::CapabilityRequest spec; |
| 89 const base::DictionaryValue* entry_value = nullptr; |
| 90 CHECK(it.value().GetAsDictionary(&entry_value)); |
| 91 ReadStringSetFromDictionary( |
| 92 *entry_value, Store::kCapabilities_ClassesKey, &spec.classes); |
| 93 ReadStringSetFromDictionary( |
| 94 *entry_value, Store::kCapabilities_InterfacesKey, &spec.interfaces); |
| 95 capabilities.required[it.key()] = spec; |
| 96 } |
| 97 } |
| 98 return capabilities; |
| 99 } |
| 100 |
| 101 } // namespace |
| 8 | 102 |
| 9 Entry::Entry() {} | 103 Entry::Entry() {} |
| 10 Entry::Entry(const Entry& other) = default; | 104 Entry::Entry(const Entry& other) = default; |
| 11 Entry::~Entry() {} | 105 Entry::~Entry() {} |
| 12 | 106 |
| 107 scoped_ptr<base::DictionaryValue> Entry::Serialize() const { |
| 108 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); |
| 109 value->SetInteger(Store::kManifestVersionKey, 1); |
| 110 value->SetString(Store::kNameKey, name_); |
| 111 value->SetString(Store::kDisplayNameKey, display_name_); |
| 112 value->SetString(Store::kQualifierKey, qualifier_); |
| 113 scoped_ptr<base::DictionaryValue> spec(new base::DictionaryValue); |
| 114 |
| 115 scoped_ptr<base::DictionaryValue> provided(new base::DictionaryValue); |
| 116 for (const auto& i : capabilities_.provided) { |
| 117 scoped_ptr<base::ListValue> interfaces(new base::ListValue); |
| 118 for (const auto& interface_name : i.second) |
| 119 interfaces->AppendString(interface_name); |
| 120 provided->Set(i.first, std::move(interfaces)); |
| 121 } |
| 122 spec->Set(Store::kCapabilities_ProvidedKey, std::move(provided)); |
| 123 |
| 124 scoped_ptr<base::DictionaryValue> required(new base::DictionaryValue); |
| 125 for (const auto& i : capabilities_.required) { |
| 126 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue); |
| 127 scoped_ptr<base::ListValue> classes(new base::ListValue); |
| 128 for (const auto& class_name : i.second.classes) |
| 129 classes->AppendString(class_name); |
| 130 request->Set(Store::kCapabilities_ClassesKey, std::move(classes)); |
| 131 scoped_ptr<base::ListValue> interfaces(new base::ListValue); |
| 132 for (const auto& interface_name : i.second.interfaces) |
| 133 interfaces->AppendString(interface_name); |
| 134 request->Set(Store::kCapabilities_InterfacesKey, std::move(interfaces)); |
| 135 required->Set(i.first, std::move(request)); |
| 136 } |
| 137 spec->Set(Store::kCapabilities_RequiredKey, std::move(required)); |
| 138 |
| 139 value->Set(Store::kCapabilitiesKey, std::move(spec)); |
| 140 return value; |
| 141 } |
| 142 |
| 143 // static |
| 144 Entry::DeserializeResult Entry::Deserialize( |
| 145 const base::DictionaryValue& value, |
| 146 Entry* out_entry) { |
| 147 DCHECK(out_entry); |
| 148 Entry entry; |
| 149 int manifest_version = 0; |
| 150 if (value.HasKey(Store::kManifestVersionKey)) |
| 151 CHECK(value.GetInteger(Store::kManifestVersionKey, &manifest_version)); |
| 152 std::string name_string; |
| 153 if (!value.GetString(Store::kNameKey, &name_string)) { |
| 154 LOG(ERROR) << "Entry::Deserialize: dictionary has no name key"; |
| 155 return DeserializeResult::INVALID_DICTIONARY; |
| 156 } |
| 157 if (!mojo::IsValidName(name_string)) { |
| 158 LOG(WARNING) << "Entry::Deserialize: " << name_string << " is not a valid " |
| 159 << "Mojo name"; |
| 160 return DeserializeResult::INVALID_NAME; |
| 161 } |
| 162 entry.set_name(name_string); |
| 163 if (value.HasKey(Store::kQualifierKey)) { |
| 164 std::string qualifier; |
| 165 CHECK(value.GetString(Store::kQualifierKey, &qualifier)); |
| 166 entry.set_qualifier(qualifier); |
| 167 } else { |
| 168 entry.set_qualifier(mojo::GetNamePath(name_string)); |
| 169 } |
| 170 std::string display_name; |
| 171 if (!value.GetString(Store::kDisplayNameKey, &display_name)) { |
| 172 LOG(WARNING) << "Entry::Deserialize: dictionary has no display_name key"; |
| 173 return DeserializeResult::INVALID_DICTIONARY; |
| 174 } |
| 175 entry.set_display_name(display_name); |
| 176 const base::DictionaryValue* capabilities = nullptr; |
| 177 if (!value.GetDictionary(Store::kCapabilitiesKey, &capabilities)) { |
| 178 LOG(WARNING) << "Entry::Description: dictionary has no capabilities key"; |
| 179 return DeserializeResult::INVALID_DICTIONARY; |
| 180 } |
| 181 if (manifest_version == 0) |
| 182 entry.set_capabilities(BuildCapabilitiesV0(*capabilities)); |
| 183 else |
| 184 entry.set_capabilities(BuildCapabilitiesV1(*capabilities)); |
| 185 *out_entry = entry; |
| 186 return DeserializeResult::SUCCESS; |
| 187 } |
| 188 |
| 13 bool Entry::operator==(const Entry& other) const { | 189 bool Entry::operator==(const Entry& other) const { |
| 14 return other.name == name && other.qualifier == qualifier && | 190 return other.name_ == name_ && other.qualifier_ == qualifier_ && |
| 15 other.display_name == display_name && | 191 other.display_name_ == display_name_ && |
| 16 other.capabilities == capabilities; | 192 other.capabilities_ == capabilities_; |
| 17 } | 193 } |
| 18 | 194 |
| 19 } // catalog | 195 } // catalog |
| OLD | NEW |