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/catalog.h" | 5 #include "mojo/services/catalog/catalog.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_file_value_serializer.h" | 8 #include "base/json/json_file_value_serializer.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
11 #include "mojo/common/url_type_converters.h" | 11 #include "mojo/common/url_type_converters.h" |
| 12 #include "mojo/services/catalog/builder.h" |
| 13 #include "mojo/services/catalog/entry.h" |
| 14 #include "mojo/services/catalog/store.h" |
12 #include "mojo/shell/public/cpp/names.h" | 15 #include "mojo/shell/public/cpp/names.h" |
13 #include "mojo/util/filename_util.h" | 16 #include "mojo/util/filename_util.h" |
14 #include "net/base/filename_util.h" | 17 #include "net/base/filename_util.h" |
15 #include "url/url_util.h" | 18 #include "url/url_util.h" |
16 | 19 |
17 namespace catalog { | 20 namespace catalog { |
18 namespace { | 21 namespace { |
19 | 22 |
20 CapabilityFilter BuildCapabilityFilterFromDictionary( | |
21 const base::DictionaryValue& value) { | |
22 CapabilityFilter filter; | |
23 base::DictionaryValue::Iterator it(value); | |
24 for (; !it.IsAtEnd(); it.Advance()) { | |
25 const base::ListValue* values = nullptr; | |
26 CHECK(it.value().GetAsList(&values)); | |
27 AllowedInterfaces interfaces; | |
28 for (auto i = values->begin(); i != values->end(); ++i) { | |
29 std::string iface_name; | |
30 const base::Value* v = *i; | |
31 CHECK(v->GetAsString(&iface_name)); | |
32 interfaces.insert(iface_name); | |
33 } | |
34 filter[it.key()] = interfaces; | |
35 } | |
36 return filter; | |
37 } | |
38 | |
39 ApplicationInfo BuildApplicationInfoFromDictionary( | |
40 const base::DictionaryValue& value) { | |
41 ApplicationInfo info; | |
42 std::string name_string; | |
43 CHECK(value.GetString(Store::kNameKey, &name_string)); | |
44 CHECK(mojo::IsValidName(name_string)) << "Invalid Name: " << name_string; | |
45 info.name = name_string; | |
46 if (value.HasKey(Store::kQualifierKey)) { | |
47 CHECK(value.GetString(Store::kQualifierKey, &info.qualifier)); | |
48 } else { | |
49 info.qualifier = mojo::GetNamePath(name_string); | |
50 } | |
51 CHECK(value.GetString(Store::kDisplayNameKey, &info.display_name)); | |
52 const base::DictionaryValue* capabilities = nullptr; | |
53 CHECK(value.GetDictionary(Store::kCapabilitiesKey, &capabilities)); | |
54 info.base_filter = BuildCapabilityFilterFromDictionary(*capabilities); | |
55 return info; | |
56 } | |
57 | |
58 void SerializeEntry(const ApplicationInfo& entry, | |
59 base::DictionaryValue** value) { | |
60 *value = new base::DictionaryValue; | |
61 (*value)->SetString(Store::kNameKey, entry.name); | |
62 (*value)->SetString(Store::kDisplayNameKey, entry.display_name); | |
63 base::DictionaryValue* capabilities = new base::DictionaryValue; | |
64 for (const auto& pair : entry.base_filter) { | |
65 scoped_ptr<base::ListValue> interfaces(new base::ListValue); | |
66 for (const auto& iface_name : pair.second) | |
67 interfaces->AppendString(iface_name); | |
68 capabilities->Set(pair.first, std::move(interfaces)); | |
69 } | |
70 (*value)->Set(Store::kCapabilitiesKey, make_scoped_ptr(capabilities)); | |
71 } | |
72 | |
73 scoped_ptr<base::Value> ReadManifest(const base::FilePath& manifest_path) { | 23 scoped_ptr<base::Value> ReadManifest(const base::FilePath& manifest_path) { |
74 JSONFileValueDeserializer deserializer(manifest_path); | 24 JSONFileValueDeserializer deserializer(manifest_path); |
75 int error = 0; | 25 int error = 0; |
76 std::string message; | 26 std::string message; |
77 // TODO(beng): probably want to do more detailed error checking. This should | 27 // TODO(beng): probably want to do more detailed error checking. This should |
78 // be done when figuring out if to unblock connection completion. | 28 // be done when figuring out if to unblock connection completion. |
79 return deserializer.Deserialize(&error, &message); | 29 return deserializer.Deserialize(&error, &message); |
80 } | 30 } |
81 | 31 |
82 } // namespace | 32 } // namespace |
83 | 33 |
84 // static | |
85 const char Store::kNameKey[] = "name"; | |
86 // static | |
87 const char Store::kQualifierKey[] = "process-group"; | |
88 // static | |
89 const char Store::kDisplayNameKey[] = "display_name"; | |
90 // static | |
91 const char Store::kCapabilitiesKey[] = "capabilities"; | |
92 | |
93 ApplicationInfo::ApplicationInfo() {} | |
94 ApplicationInfo::ApplicationInfo(const ApplicationInfo& other) = default; | |
95 ApplicationInfo::~ApplicationInfo() {} | |
96 | |
97 Catalog::Catalog(base::TaskRunner* blocking_pool, | 34 Catalog::Catalog(base::TaskRunner* blocking_pool, |
98 scoped_ptr<Store> catalog) | 35 scoped_ptr<Store> catalog) |
99 : blocking_pool_(blocking_pool), | 36 : blocking_pool_(blocking_pool), |
100 store_(std::move(catalog)), | 37 store_(std::move(catalog)), |
101 weak_factory_(this) { | 38 weak_factory_(this) { |
102 base::FilePath shell_dir; | 39 base::FilePath shell_dir; |
103 PathService::Get(base::DIR_MODULE, &shell_dir); | 40 PathService::Get(base::DIR_MODULE, &shell_dir); |
104 | 41 |
105 system_package_dir_ = | 42 system_package_dir_ = |
106 mojo::util::FilePathToFileURL(shell_dir).Resolve(std::string()); | 43 mojo::util::FilePathToFileURL(shell_dir).Resolve(std::string()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 AddNameToCatalog(resolved_name, callback); | 110 AddNameToCatalog(resolved_name, callback); |
174 } | 111 } |
175 | 112 |
176 void Catalog::GetEntries(mojo::Array<mojo::String> names, | 113 void Catalog::GetEntries(mojo::Array<mojo::String> names, |
177 const GetEntriesCallback& callback) { | 114 const GetEntriesCallback& callback) { |
178 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; | 115 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; |
179 std::vector<mojo::String> names_vec = names.PassStorage(); | 116 std::vector<mojo::String> names_vec = names.PassStorage(); |
180 for (const std::string& name : names_vec) { | 117 for (const std::string& name : names_vec) { |
181 if (catalog_.find(name) == catalog_.end()) | 118 if (catalog_.find(name) == catalog_.end()) |
182 continue; | 119 continue; |
183 const ApplicationInfo& info = catalog_[name]; | 120 const Entry& entry = catalog_[name]; |
184 mojom::CatalogEntryPtr entry(mojom::CatalogEntry::New()); | 121 mojom::CatalogEntryPtr entry_ptr(mojom::CatalogEntry::New()); |
185 entry->display_name = info.display_name; | 122 entry_ptr->display_name = entry.display_name; |
186 entries[info.name] = std::move(entry); | 123 entries[entry.name] = std::move(entry_ptr); |
187 } | 124 } |
188 callback.Run(std::move(entries)); | 125 callback.Run(std::move(entries)); |
189 } | 126 } |
190 | 127 |
191 void Catalog::CompleteResolveMojoName( | 128 void Catalog::CompleteResolveMojoName( |
192 const std::string& resolved_name, | 129 const std::string& resolved_name, |
193 const std::string& qualifier, | 130 const std::string& qualifier, |
194 const ResolveMojoNameCallback& callback) { | 131 const ResolveMojoNameCallback& callback) { |
195 auto info_iter = catalog_.find(resolved_name); | 132 auto entry_iter = catalog_.find(resolved_name); |
196 CHECK(info_iter != catalog_.end()); | 133 CHECK(entry_iter != catalog_.end()); |
197 | 134 |
198 GURL file_url; | 135 GURL file_url; |
199 std::string type = mojo::GetNameType(resolved_name); | 136 std::string type = mojo::GetNameType(resolved_name); |
200 if (type == "mojo") { | 137 if (type == "mojo") { |
201 // It's still a mojo: URL, use the default mapping scheme. | 138 // It's still a mojo: URL, use the default mapping scheme. |
202 const std::string host = mojo::GetNamePath(resolved_name); | 139 const std::string host = mojo::GetNamePath(resolved_name); |
203 file_url = system_package_dir_.Resolve(host + "/" + host + ".mojo"); | 140 file_url = system_package_dir_.Resolve(host + "/" + host + ".mojo"); |
204 } else if (type == "exe") { | 141 } else if (type == "exe") { |
205 #if defined OS_WIN | 142 #if defined OS_WIN |
206 std::string extension = ".exe"; | 143 std::string extension = ".exe"; |
207 #else | 144 #else |
208 std::string extension; | 145 std::string extension; |
209 #endif | 146 #endif |
210 file_url = system_package_dir_.Resolve( | 147 file_url = system_package_dir_.Resolve( |
211 mojo::GetNamePath(resolved_name) + extension); | 148 mojo::GetNamePath(resolved_name) + extension); |
212 } | 149 } |
213 | 150 |
214 mojo::shell::mojom::CapabilityFilterPtr filter( | 151 mojo::shell::mojom::CapabilityFilterPtr filter( |
215 mojo::shell::mojom::CapabilityFilter::New()); | 152 mojo::shell::mojom::CapabilityFilter::New()); |
216 filter->filter = mojo::Map<mojo::String, mojo::Array<mojo::String>>(); | 153 filter->filter = mojo::Map<mojo::String, mojo::Array<mojo::String>>(); |
217 for (const auto& entry : info_iter->second.base_filter) { | 154 for (const auto& entry : entry_iter->second.capabilities) { |
218 mojo::Array<mojo::String> interfaces; | 155 mojo::Array<mojo::String> interfaces; |
219 for (auto interface_name : entry.second) | 156 for (auto interface_name : entry.second) |
220 interfaces.push_back(interface_name); | 157 interfaces.push_back(interface_name); |
221 filter->filter.insert(entry.first, std::move(interfaces)); | 158 filter->filter.insert(entry.first, std::move(interfaces)); |
222 } | 159 } |
223 callback.Run(resolved_name, qualifier, std::move(filter), | 160 callback.Run(resolved_name, qualifier, std::move(filter), |
224 file_url.spec()); | 161 file_url.spec()); |
225 } | 162 } |
226 | 163 |
227 bool Catalog::IsNameInCatalog(const std::string& name) const { | 164 bool Catalog::IsNameInCatalog(const std::string& name) const { |
(...skipping 24 matching lines...) Expand all Loading... |
252 void Catalog::DeserializeCatalog() { | 189 void Catalog::DeserializeCatalog() { |
253 if (!store_) | 190 if (!store_) |
254 return; | 191 return; |
255 const base::ListValue* catalog = store_->GetStore(); | 192 const base::ListValue* catalog = store_->GetStore(); |
256 CHECK(catalog); | 193 CHECK(catalog); |
257 // TODO(sky): make this handle aliases. | 194 // TODO(sky): make this handle aliases. |
258 for (auto it = catalog->begin(); it != catalog->end(); ++it) { | 195 for (auto it = catalog->begin(); it != catalog->end(); ++it) { |
259 const base::DictionaryValue* dictionary = nullptr; | 196 const base::DictionaryValue* dictionary = nullptr; |
260 const base::Value* v = *it; | 197 const base::Value* v = *it; |
261 CHECK(v->GetAsDictionary(&dictionary)); | 198 CHECK(v->GetAsDictionary(&dictionary)); |
262 const ApplicationInfo app_info = | 199 const Entry entry = BuildEntry(*dictionary); |
263 BuildApplicationInfoFromDictionary(*dictionary); | 200 catalog_[entry.name] = entry; |
264 catalog_[app_info.name] = app_info; | |
265 } | 201 } |
266 } | 202 } |
267 | 203 |
268 void Catalog::SerializeCatalog() { | 204 void Catalog::SerializeCatalog() { |
269 scoped_ptr<base::ListValue> catalog(new base::ListValue); | 205 scoped_ptr<base::ListValue> catalog(new base::ListValue); |
270 for (const auto& info : catalog_) { | 206 for (const auto& entry : catalog_) { |
271 base::DictionaryValue* dictionary = nullptr; | 207 base::DictionaryValue* dictionary = nullptr; |
272 SerializeEntry(info.second, &dictionary); | 208 SerializeEntry(entry.second, &dictionary); |
273 catalog->Append(make_scoped_ptr(dictionary)); | 209 catalog->Append(make_scoped_ptr(dictionary)); |
274 } | 210 } |
275 if (store_) | 211 if (store_) |
276 store_->UpdateStore(std::move(catalog)); | 212 store_->UpdateStore(std::move(catalog)); |
277 } | 213 } |
278 | 214 |
279 const ApplicationInfo& Catalog::DeserializeApplication( | 215 const Entry& Catalog::DeserializeApplication( |
280 const base::DictionaryValue* dictionary) { | 216 const base::DictionaryValue* dictionary) { |
281 ApplicationInfo info = BuildApplicationInfoFromDictionary(*dictionary); | 217 Entry entry = BuildEntry(*dictionary); |
282 if (catalog_.find(info.name) == catalog_.end()) { | 218 if (catalog_.find(entry.name) == catalog_.end()) { |
283 catalog_[info.name] = info; | 219 catalog_[entry.name] = entry; |
284 | 220 |
285 if (dictionary->HasKey("applications")) { | 221 if (dictionary->HasKey("applications")) { |
286 const base::ListValue* applications = nullptr; | 222 const base::ListValue* applications = nullptr; |
287 dictionary->GetList("applications", &applications); | 223 dictionary->GetList("applications", &applications); |
288 for (size_t i = 0; i < applications->GetSize(); ++i) { | 224 for (size_t i = 0; i < applications->GetSize(); ++i) { |
289 const base::DictionaryValue* child = nullptr; | 225 const base::DictionaryValue* child_value = nullptr; |
290 applications->GetDictionary(i, &child); | 226 applications->GetDictionary(i, &child_value); |
291 const ApplicationInfo& child_info = DeserializeApplication(child); | 227 const Entry& child = DeserializeApplication(child_value); |
292 mojo_name_aliases_[child_info.name] = | 228 mojo_name_aliases_[child.name] = |
293 std::make_pair(info.name, child_info.qualifier); | 229 std::make_pair(entry.name, child.qualifier); |
294 } | 230 } |
295 } | 231 } |
296 qualifiers_[info.name] = info.qualifier; | 232 qualifiers_[entry.name] = entry.qualifier; |
297 } | 233 } |
298 return catalog_[info.name]; | 234 return catalog_[entry.name]; |
299 } | 235 } |
300 | 236 |
301 GURL Catalog::GetManifestURL(const std::string& name) { | 237 GURL Catalog::GetManifestURL(const std::string& name) { |
302 // TODO(beng): think more about how this should be done for exe targets. | 238 // TODO(beng): think more about how this should be done for exe targets. |
303 std::string type = mojo::GetNameType(name); | 239 std::string type = mojo::GetNameType(name); |
304 std::string path = mojo::GetNamePath(name); | 240 std::string path = mojo::GetNamePath(name); |
305 if (type == "mojo") | 241 if (type == "mojo") |
306 return system_package_dir_.Resolve(path + "/manifest.json"); | 242 return system_package_dir_.Resolve(path + "/manifest.json"); |
307 else if (type == "exe") | 243 else if (type == "exe") |
308 return system_package_dir_.Resolve(path + "_manifest.json"); | 244 return system_package_dir_.Resolve(path + "_manifest.json"); |
(...skipping 15 matching lines...) Expand all Loading... |
324 } | 260 } |
325 | 261 |
326 void Catalog::OnReadManifestImpl(const std::string& name, | 262 void Catalog::OnReadManifestImpl(const std::string& name, |
327 const ResolveMojoNameCallback& callback, | 263 const ResolveMojoNameCallback& callback, |
328 scoped_ptr<base::Value> manifest) { | 264 scoped_ptr<base::Value> manifest) { |
329 if (manifest) { | 265 if (manifest) { |
330 base::DictionaryValue* dictionary = nullptr; | 266 base::DictionaryValue* dictionary = nullptr; |
331 CHECK(manifest->GetAsDictionary(&dictionary)); | 267 CHECK(manifest->GetAsDictionary(&dictionary)); |
332 DeserializeApplication(dictionary); | 268 DeserializeApplication(dictionary); |
333 } else { | 269 } else { |
334 ApplicationInfo info; | 270 Entry entry; |
335 info.name = name; | 271 entry.name = name; |
336 info.display_name = name; | 272 entry.display_name = name; |
337 catalog_[info.name] = info; | 273 catalog_[entry.name] = entry; |
338 qualifiers_[info.name] = mojo::GetNamePath(name); | 274 qualifiers_[entry.name] = mojo::GetNamePath(name); |
339 } | 275 } |
340 SerializeCatalog(); | 276 SerializeCatalog(); |
341 | 277 |
342 auto qualifier_iter = qualifiers_.find(name); | 278 auto qualifier_iter = qualifiers_.find(name); |
343 DCHECK(qualifier_iter != qualifiers_.end()); | 279 DCHECK(qualifier_iter != qualifiers_.end()); |
344 std::string qualifier = qualifier_iter->second; | 280 std::string qualifier = qualifier_iter->second; |
345 CompleteResolveMojoName(name, qualifier, callback); | 281 CompleteResolveMojoName(name, qualifier, callback); |
346 } | 282 } |
347 | 283 |
348 } // namespace catalog | 284 } // namespace catalog |
OLD | NEW |