Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(805)

Side by Side Diff: mojo/services/catalog/catalog.cc

Issue 1818373003: Move serialize/deserialize logic onto Entry. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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" 12 #include "mojo/services/catalog/entry.h"
14 #include "mojo/services/catalog/store.h" 13 #include "mojo/services/catalog/store.h"
15 #include "mojo/shell/public/cpp/names.h" 14 #include "mojo/shell/public/cpp/names.h"
16 #include "mojo/util/filename_util.h" 15 #include "mojo/util/filename_util.h"
17 #include "url/url_util.h" 16 #include "url/url_util.h"
18 17
19 namespace catalog { 18 namespace catalog {
20 namespace { 19 namespace {
21 20
22 scoped_ptr<base::Value> ReadManifest(const base::FilePath& manifest_path) { 21 scoped_ptr<base::Value> ReadManifest(const base::FilePath& manifest_path) {
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 112
114 void Catalog::GetEntries(mojo::Array<mojo::String> names, 113 void Catalog::GetEntries(mojo::Array<mojo::String> names,
115 const GetEntriesCallback& callback) { 114 const GetEntriesCallback& callback) {
116 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; 115 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries;
117 std::vector<mojo::String> names_vec = names.PassStorage(); 116 std::vector<mojo::String> names_vec = names.PassStorage();
118 for (const std::string& name : names_vec) { 117 for (const std::string& name : names_vec) {
119 if (catalog_.find(name) == catalog_.end()) 118 if (catalog_.find(name) == catalog_.end())
120 continue; 119 continue;
121 const Entry& entry = catalog_[name]; 120 const Entry& entry = catalog_[name];
122 mojom::CatalogEntryPtr entry_ptr(mojom::CatalogEntry::New()); 121 mojom::CatalogEntryPtr entry_ptr(mojom::CatalogEntry::New());
123 entry_ptr->display_name = entry.display_name; 122 entry_ptr->display_name = entry.display_name();
124 entries[entry.name] = std::move(entry_ptr); 123 entries[entry.name()] = std::move(entry_ptr);
125 } 124 }
126 callback.Run(std::move(entries)); 125 callback.Run(std::move(entries));
127 } 126 }
128 127
129 //////////////////////////////////////////////////////////////////////////////// 128 ////////////////////////////////////////////////////////////////////////////////
130 // Catalog, private: 129 // Catalog, private:
131 130
132 void Catalog::CompleteResolveMojoName( 131 void Catalog::CompleteResolveMojoName(
133 const std::string& resolved_name, 132 const std::string& resolved_name,
134 const std::string& qualifier, 133 const std::string& qualifier,
(...skipping 11 matching lines...) Expand all
146 #if defined OS_WIN 145 #if defined OS_WIN
147 std::string extension = ".exe"; 146 std::string extension = ".exe";
148 #else 147 #else
149 std::string extension; 148 std::string extension;
150 #endif 149 #endif
151 file_url = system_package_dir_.Resolve( 150 file_url = system_package_dir_.Resolve(
152 mojo::GetNamePath(resolved_name) + extension); 151 mojo::GetNamePath(resolved_name) + extension);
153 } 152 }
154 153
155 mojo::shell::mojom::CapabilitySpecPtr capabilities_ptr = 154 mojo::shell::mojom::CapabilitySpecPtr capabilities_ptr =
156 mojo::shell::mojom::CapabilitySpec::From(entry_iter->second.capabilities); 155 mojo::shell::mojom::CapabilitySpec::From(
156 entry_iter->second.capabilities());
157 157
158 callback.Run(resolved_name, qualifier, std::move(capabilities_ptr), 158 callback.Run(resolved_name, qualifier, std::move(capabilities_ptr),
159 file_url.spec()); 159 file_url.spec());
160 } 160 }
161 161
162 bool Catalog::IsNameInCatalog(const std::string& name) const { 162 bool Catalog::IsNameInCatalog(const std::string& name) const {
163 return catalog_.find(name) != catalog_.end(); 163 return catalog_.find(name) != catalog_.end();
164 } 164 }
165 165
166 void Catalog::AddNameToCatalog(const std::string& name, 166 void Catalog::AddNameToCatalog(const std::string& name,
(...skipping 19 matching lines...) Expand all
186 void Catalog::DeserializeCatalog() { 186 void Catalog::DeserializeCatalog() {
187 if (!store_) 187 if (!store_)
188 return; 188 return;
189 const base::ListValue* catalog = store_->GetStore(); 189 const base::ListValue* catalog = store_->GetStore();
190 CHECK(catalog); 190 CHECK(catalog);
191 // TODO(sky): make this handle aliases. 191 // TODO(sky): make this handle aliases.
192 for (auto it = catalog->begin(); it != catalog->end(); ++it) { 192 for (auto it = catalog->begin(); it != catalog->end(); ++it) {
193 const base::DictionaryValue* dictionary = nullptr; 193 const base::DictionaryValue* dictionary = nullptr;
194 const base::Value* v = *it; 194 const base::Value* v = *it;
195 CHECK(v->GetAsDictionary(&dictionary)); 195 CHECK(v->GetAsDictionary(&dictionary));
196 const Entry entry = BuildEntry(*dictionary); 196 Entry entry;
197 catalog_[entry.name] = entry; 197 if (Entry::DeserializeResult::SUCCESS ==
198 Entry::Deserialize(*dictionary, &entry)) {
199 catalog_[entry.name()] = entry;
200 }
198 } 201 }
199 } 202 }
200 203
201 void Catalog::SerializeCatalog() { 204 void Catalog::SerializeCatalog() {
202 scoped_ptr<base::ListValue> catalog(new base::ListValue); 205 scoped_ptr<base::ListValue> catalog(new base::ListValue);
203 for (const auto& entry : catalog_) 206 for (const auto& entry : catalog_)
204 catalog->Append(SerializeEntry(entry.second)); 207 catalog->Append(entry.second.Serialize());
205 if (store_) 208 if (store_)
206 store_->UpdateStore(std::move(catalog)); 209 store_->UpdateStore(std::move(catalog));
207 } 210 }
208 211
209 const Entry& Catalog::DeserializeApplication( 212 Entry::DeserializeResult Catalog::DeserializeApplication(
210 const base::DictionaryValue* dictionary) { 213 const base::DictionaryValue* dictionary,
211 Entry entry = BuildEntry(*dictionary); 214 Entry* out_entry) {
212 if (catalog_.find(entry.name) == catalog_.end()) { 215 Entry entry;
213 catalog_[entry.name] = entry; 216 Entry::DeserializeResult result =
217 Entry::Deserialize(*dictionary, &entry);
218 if (result != Entry::DeserializeResult::SUCCESS)
219 return result;
220
221 // TODO(beng): move raw dictionary analysis into Deserialize().
222 if (catalog_.find(entry.name()) == catalog_.end()) {
223 catalog_[entry.name()] = entry;
214 224
215 if (dictionary->HasKey("applications")) { 225 if (dictionary->HasKey("applications")) {
216 const base::ListValue* applications = nullptr; 226 const base::ListValue* applications = nullptr;
217 dictionary->GetList("applications", &applications); 227 dictionary->GetList("applications", &applications);
218 for (size_t i = 0; i < applications->GetSize(); ++i) { 228 for (size_t i = 0; i < applications->GetSize(); ++i) {
219 const base::DictionaryValue* child_value = nullptr; 229 const base::DictionaryValue* child_value = nullptr;
220 applications->GetDictionary(i, &child_value); 230 applications->GetDictionary(i, &child_value);
221 const Entry& child = DeserializeApplication(child_value); 231 Entry child;
222 mojo_name_aliases_[child.name] = 232 if (Entry::DeserializeResult::SUCCESS ==
223 std::make_pair(entry.name, child.qualifier); 233 DeserializeApplication(child_value, &child)) {
234 mojo_name_aliases_[child.name()] =
235 std::make_pair(entry.name(), child.qualifier());
236 }
224 } 237 }
225 } 238 }
226 qualifiers_[entry.name] = entry.qualifier; 239 qualifiers_[entry.name()] = entry.qualifier();
227 } 240 }
228 return catalog_[entry.name]; 241 if (out_entry)
242 *out_entry = catalog_[entry.name()];
243 return result;
229 } 244 }
230 245
231 GURL Catalog::GetManifestURL(const std::string& name) { 246 GURL Catalog::GetManifestURL(const std::string& name) {
232 // TODO(beng): think more about how this should be done for exe targets. 247 // TODO(beng): think more about how this should be done for exe targets.
233 std::string type = mojo::GetNameType(name); 248 std::string type = mojo::GetNameType(name);
234 std::string path = mojo::GetNamePath(name); 249 std::string path = mojo::GetNamePath(name);
235 if (type == "mojo") 250 if (type == "mojo")
236 return system_package_dir_.Resolve(path + "/manifest.json"); 251 return system_package_dir_.Resolve(path + "/manifest.json");
237 else if (type == "exe") 252 else if (type == "exe")
238 return system_package_dir_.Resolve(path + "_manifest.json"); 253 return system_package_dir_.Resolve(path + "_manifest.json");
(...skipping 13 matching lines...) Expand all
252 } 267 }
253 catalog->OnReadManifestImpl(name, callback, std::move(manifest)); 268 catalog->OnReadManifestImpl(name, callback, std::move(manifest));
254 } 269 }
255 270
256 void Catalog::OnReadManifestImpl(const std::string& name, 271 void Catalog::OnReadManifestImpl(const std::string& name,
257 const ResolveMojoNameCallback& callback, 272 const ResolveMojoNameCallback& callback,
258 scoped_ptr<base::Value> manifest) { 273 scoped_ptr<base::Value> manifest) {
259 if (manifest) { 274 if (manifest) {
260 base::DictionaryValue* dictionary = nullptr; 275 base::DictionaryValue* dictionary = nullptr;
261 CHECK(manifest->GetAsDictionary(&dictionary)); 276 CHECK(manifest->GetAsDictionary(&dictionary));
262 DeserializeApplication(dictionary); 277 DeserializeApplication(dictionary, nullptr);
263 } else { 278 } else {
264 Entry entry; 279 Entry entry;
265 entry.name = name; 280 entry.set_name(name);
266 entry.display_name = name; 281 entry.set_display_name(name);
267 catalog_[entry.name] = entry; 282 catalog_[entry.name()] = entry;
268 qualifiers_[entry.name] = mojo::GetNamePath(name); 283 qualifiers_[entry.name()] = mojo::GetNamePath(name);
269 } 284 }
270 SerializeCatalog(); 285 SerializeCatalog();
271 286
272 auto qualifier_iter = qualifiers_.find(name); 287 auto qualifier_iter = qualifiers_.find(name);
273 DCHECK(qualifier_iter != qualifiers_.end()); 288 DCHECK(qualifier_iter != qualifiers_.end());
274 std::string qualifier = qualifier_iter->second; 289 std::string qualifier = qualifier_iter->second;
275 CompleteResolveMojoName(name, qualifier, callback); 290 CompleteResolveMojoName(name, qualifier, callback);
276 } 291 }
277 292
278 } // namespace catalog 293 } // namespace catalog
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698