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