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 "services/catalog/instance.h" | 5 #include "services/catalog/instance.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "services/catalog/entry.h" | 8 #include "services/catalog/entry.h" |
9 #include "services/catalog/manifest_provider.h" | 9 #include "services/catalog/manifest_provider.h" |
10 #include "services/catalog/reader.h" | 10 #include "services/catalog/reader.h" |
11 #include "services/catalog/store.h" | |
12 | 11 |
13 namespace catalog { | 12 namespace catalog { |
14 namespace { | 13 namespace { |
15 | 14 |
16 void AddEntry(const Entry& entry, std::vector<mojom::EntryPtr>* ary) { | 15 void AddEntry(const Entry& entry, std::vector<mojom::EntryPtr>* ary) { |
17 mojom::EntryPtr entry_ptr(mojom::Entry::New()); | 16 mojom::EntryPtr entry_ptr(mojom::Entry::New()); |
18 entry_ptr->name = entry.name(); | 17 entry_ptr->name = entry.name(); |
19 entry_ptr->display_name = entry.display_name(); | 18 entry_ptr->display_name = entry.display_name(); |
20 ary->push_back(std::move(entry_ptr)); | 19 ary->push_back(std::move(entry_ptr)); |
21 } | 20 } |
22 | 21 |
23 } // namespace | 22 } // namespace |
24 | 23 |
25 //////////////////////////////////////////////////////////////////////////////// | 24 Instance::Instance(Reader* system_reader) : system_reader_(system_reader) {} |
26 // Instance, public: | |
27 | 25 |
28 Instance::Instance(std::unique_ptr<Store> store, Reader* system_reader) | |
29 : store_(std::move(store)), | |
30 system_reader_(system_reader), | |
31 weak_factory_(this) {} | |
32 Instance::~Instance() {} | 26 Instance::~Instance() {} |
33 | 27 |
34 void Instance::BindResolver(service_manager::mojom::ResolverRequest request) { | 28 void Instance::BindResolver(service_manager::mojom::ResolverRequest request) { |
35 if (system_cache_) | 29 if (system_cache_) |
36 resolver_bindings_.AddBinding(this, std::move(request)); | 30 resolver_bindings_.AddBinding(this, std::move(request)); |
37 else | 31 else |
38 pending_resolver_requests_.push_back(std::move(request)); | 32 pending_resolver_requests_.push_back(std::move(request)); |
39 } | 33 } |
40 | 34 |
41 void Instance::BindCatalog(mojom::CatalogRequest request) { | 35 void Instance::BindCatalog(mojom::CatalogRequest request) { |
42 if (system_cache_) | 36 if (system_cache_) |
43 catalog_bindings_.AddBinding(this, std::move(request)); | 37 catalog_bindings_.AddBinding(this, std::move(request)); |
44 else | 38 else |
45 pending_catalog_requests_.push_back(std::move(request)); | 39 pending_catalog_requests_.push_back(std::move(request)); |
46 } | 40 } |
47 | 41 |
48 void Instance::CacheReady(EntryCache* cache) { | 42 void Instance::CacheReady(EntryCache* cache) { |
49 system_cache_ = cache; | 43 system_cache_ = cache; |
50 DeserializeCatalog(); | |
51 for (auto& request : pending_resolver_requests_) | 44 for (auto& request : pending_resolver_requests_) |
52 BindResolver(std::move(request)); | 45 BindResolver(std::move(request)); |
53 for (auto& request : pending_catalog_requests_) | 46 for (auto& request : pending_catalog_requests_) |
54 BindCatalog(std::move(request)); | 47 BindCatalog(std::move(request)); |
55 } | 48 } |
56 | 49 |
57 //////////////////////////////////////////////////////////////////////////////// | 50 void Instance::ResolveServiceName(const std::string& service_name, |
58 // Instance, service_manager::mojom::Resolver: | 51 const ResolveServiceNameCallback& callback) { |
59 | |
60 void Instance::ResolveMojoName(const std::string& service_name, | |
61 const ResolveMojoNameCallback& callback) { | |
62 DCHECK(system_cache_); | 52 DCHECK(system_cache_); |
63 | 53 |
64 // TODO(beng): per-user catalogs. | 54 // TODO(beng): per-user catalogs. |
65 auto entry = system_cache_->find(service_name); | 55 auto entry = system_cache_->find(service_name); |
66 if (entry != system_cache_->end()) { | 56 if (entry != system_cache_->end()) { |
67 callback.Run(service_manager::mojom::ResolveResult::From(*entry->second)); | 57 callback.Run(service_manager::mojom::ResolveResult::From(*entry->second)); |
68 return; | 58 return; |
69 } | 59 } |
70 | 60 |
71 // Manifests for mojo: names should always be in the catalog by this point. | 61 system_reader_->CreateEntryForName(service_name, system_cache_, callback); |
72 // DCHECK(type == service_manager::kNameType_Exe); | |
73 system_reader_->CreateEntryForName( | |
74 service_name, system_cache_, | |
75 base::Bind(&Instance::OnReadManifest, weak_factory_.GetWeakPtr(), | |
76 service_name, callback)); | |
77 } | 62 } |
78 | 63 |
79 //////////////////////////////////////////////////////////////////////////////// | |
80 // Instance, mojom::Catalog: | |
81 | |
82 void Instance::GetEntries(const base::Optional<std::vector<std::string>>& names, | 64 void Instance::GetEntries(const base::Optional<std::vector<std::string>>& names, |
83 const GetEntriesCallback& callback) { | 65 const GetEntriesCallback& callback) { |
84 DCHECK(system_cache_); | 66 DCHECK(system_cache_); |
85 | 67 |
86 std::vector<mojom::EntryPtr> entries; | 68 std::vector<mojom::EntryPtr> entries; |
87 if (!names.has_value()) { | 69 if (!names.has_value()) { |
88 // TODO(beng): user catalog. | 70 // TODO(beng): user catalog. |
89 for (const auto& entry : *system_cache_) | 71 for (const auto& entry : *system_cache_) |
90 AddEntry(*entry.second, &entries); | 72 AddEntry(*entry.second, &entries); |
91 } else { | 73 } else { |
(...skipping 25 matching lines...) Expand all Loading... |
117 const GetEntriesConsumingMIMETypeCallback& callback) { | 99 const GetEntriesConsumingMIMETypeCallback& callback) { |
118 // TODO(beng): implement. | 100 // TODO(beng): implement. |
119 } | 101 } |
120 | 102 |
121 void Instance::GetEntriesSupportingScheme( | 103 void Instance::GetEntriesSupportingScheme( |
122 const std::string& scheme, | 104 const std::string& scheme, |
123 const GetEntriesSupportingSchemeCallback& callback) { | 105 const GetEntriesSupportingSchemeCallback& callback) { |
124 // TODO(beng): implement. | 106 // TODO(beng): implement. |
125 } | 107 } |
126 | 108 |
127 //////////////////////////////////////////////////////////////////////////////// | |
128 // Instance, private: | |
129 | |
130 void Instance::DeserializeCatalog() { | |
131 DCHECK(system_cache_); | |
132 if (!store_) | |
133 return; | |
134 const base::ListValue* catalog = store_->GetStore(); | |
135 CHECK(catalog); | |
136 // TODO(sky): make this handle aliases. | |
137 // TODO(beng): implement this properly! | |
138 for (const auto& v : *catalog) { | |
139 const base::DictionaryValue* dictionary = nullptr; | |
140 CHECK(v->GetAsDictionary(&dictionary)); | |
141 std::unique_ptr<Entry> entry = Entry::Deserialize(*dictionary); | |
142 // TODO(beng): user catalog. | |
143 if (entry) | |
144 (*system_cache_)[entry->name()] = std::move(entry); | |
145 } | |
146 } | |
147 | |
148 void Instance::SerializeCatalog() { | |
149 DCHECK(system_cache_); | |
150 std::unique_ptr<base::ListValue> catalog(new base::ListValue); | |
151 // TODO(beng): user catalog. | |
152 for (const auto& entry : *system_cache_) | |
153 catalog->Append(entry.second->Serialize()); | |
154 if (store_) | |
155 store_->UpdateStore(std::move(catalog)); | |
156 } | |
157 | |
158 // static | |
159 void Instance::OnReadManifest(base::WeakPtr<Instance> instance, | |
160 const std::string& service_name, | |
161 const ResolveMojoNameCallback& callback, | |
162 service_manager::mojom::ResolveResultPtr result) { | |
163 callback.Run(std::move(result)); | |
164 if (instance) | |
165 instance->SerializeCatalog(); | |
166 } | |
167 | |
168 } // namespace catalog | 109 } // namespace catalog |
OLD | NEW |