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

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
« no previous file with comments | « mojo/services/catalog/catalog.h ('k') | mojo/services/catalog/entry.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 scoped_ptr<Entry> entry = Entry::Deserialize(*dictionary);
197 catalog_[entry.name] = entry; 197 if (entry.get())
198 catalog_[entry->name()] = *entry;
198 } 199 }
199 } 200 }
200 201
201 void Catalog::SerializeCatalog() { 202 void Catalog::SerializeCatalog() {
202 scoped_ptr<base::ListValue> catalog(new base::ListValue); 203 scoped_ptr<base::ListValue> catalog(new base::ListValue);
203 for (const auto& entry : catalog_) 204 for (const auto& entry : catalog_)
204 catalog->Append(SerializeEntry(entry.second)); 205 catalog->Append(entry.second.Serialize());
205 if (store_) 206 if (store_)
206 store_->UpdateStore(std::move(catalog)); 207 store_->UpdateStore(std::move(catalog));
207 } 208 }
208 209
209 const Entry& Catalog::DeserializeApplication( 210 scoped_ptr<Entry> Catalog::DeserializeApplication(
210 const base::DictionaryValue* dictionary) { 211 const base::DictionaryValue* dictionary) {
211 Entry entry = BuildEntry(*dictionary); 212 scoped_ptr<Entry> entry = Entry::Deserialize(*dictionary);
212 if (catalog_.find(entry.name) == catalog_.end()) { 213 if (!entry)
213 catalog_[entry.name] = entry; 214 return entry;
215
216 // TODO(beng): move raw dictionary analysis into Deserialize().
217 if (catalog_.find(entry->name()) == catalog_.end()) {
218 catalog_[entry->name()] = *entry;
214 219
215 if (dictionary->HasKey("applications")) { 220 if (dictionary->HasKey("applications")) {
216 const base::ListValue* applications = nullptr; 221 const base::ListValue* applications = nullptr;
217 dictionary->GetList("applications", &applications); 222 dictionary->GetList("applications", &applications);
218 for (size_t i = 0; i < applications->GetSize(); ++i) { 223 for (size_t i = 0; i < applications->GetSize(); ++i) {
219 const base::DictionaryValue* child_value = nullptr; 224 const base::DictionaryValue* child_value = nullptr;
220 applications->GetDictionary(i, &child_value); 225 applications->GetDictionary(i, &child_value);
221 const Entry& child = DeserializeApplication(child_value); 226 scoped_ptr<Entry> child = DeserializeApplication(child_value);
222 mojo_name_aliases_[child.name] = 227 if (child) {
223 std::make_pair(entry.name, child.qualifier); 228 mojo_name_aliases_[child->name()] =
229 std::make_pair(entry->name(), child->qualifier());
230 }
224 } 231 }
225 } 232 }
226 qualifiers_[entry.name] = entry.qualifier; 233 qualifiers_[entry->name()] = entry->qualifier();
227 } 234 }
228 return catalog_[entry.name]; 235 return entry;
229 } 236 }
230 237
231 GURL Catalog::GetManifestURL(const std::string& name) { 238 GURL Catalog::GetManifestURL(const std::string& name) {
232 // TODO(beng): think more about how this should be done for exe targets. 239 // TODO(beng): think more about how this should be done for exe targets.
233 std::string type = mojo::GetNameType(name); 240 std::string type = mojo::GetNameType(name);
234 std::string path = mojo::GetNamePath(name); 241 std::string path = mojo::GetNamePath(name);
235 if (type == "mojo") 242 if (type == "mojo")
236 return system_package_dir_.Resolve(path + "/manifest.json"); 243 return system_package_dir_.Resolve(path + "/manifest.json");
237 else if (type == "exe") 244 else if (type == "exe")
238 return system_package_dir_.Resolve(path + "_manifest.json"); 245 return system_package_dir_.Resolve(path + "_manifest.json");
(...skipping 16 matching lines...) Expand all
255 262
256 void Catalog::OnReadManifestImpl(const std::string& name, 263 void Catalog::OnReadManifestImpl(const std::string& name,
257 const ResolveMojoNameCallback& callback, 264 const ResolveMojoNameCallback& callback,
258 scoped_ptr<base::Value> manifest) { 265 scoped_ptr<base::Value> manifest) {
259 if (manifest) { 266 if (manifest) {
260 base::DictionaryValue* dictionary = nullptr; 267 base::DictionaryValue* dictionary = nullptr;
261 CHECK(manifest->GetAsDictionary(&dictionary)); 268 CHECK(manifest->GetAsDictionary(&dictionary));
262 DeserializeApplication(dictionary); 269 DeserializeApplication(dictionary);
263 } else { 270 } else {
264 Entry entry; 271 Entry entry;
265 entry.name = name; 272 entry.set_name(name);
266 entry.display_name = name; 273 entry.set_display_name(name);
267 catalog_[entry.name] = entry; 274 catalog_[entry.name()] = entry;
268 qualifiers_[entry.name] = mojo::GetNamePath(name); 275 qualifiers_[entry.name()] = mojo::GetNamePath(name);
269 } 276 }
270 SerializeCatalog(); 277 SerializeCatalog();
271 278
272 auto qualifier_iter = qualifiers_.find(name); 279 auto qualifier_iter = qualifiers_.find(name);
273 DCHECK(qualifier_iter != qualifiers_.end()); 280 DCHECK(qualifier_iter != qualifiers_.end());
274 std::string qualifier = qualifier_iter->second; 281 std::string qualifier = qualifier_iter->second;
275 CompleteResolveMojoName(name, qualifier, callback); 282 CompleteResolveMojoName(name, qualifier, callback);
276 } 283 }
277 284
278 } // namespace catalog 285 } // namespace catalog
OLDNEW
« no previous file with comments | « mojo/services/catalog/catalog.h ('k') | mojo/services/catalog/entry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698