| 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/json/json_reader.h" |
| 9 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
| 10 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 11 #include "mojo/common/url_type_converters.h" | 13 #include "mojo/common/url_type_converters.h" |
| 12 #include "mojo/services/catalog/entry.h" | 14 #include "mojo/services/catalog/entry.h" |
| 15 #include "mojo/services/catalog/manifest_provider.h" |
| 13 #include "mojo/services/catalog/store.h" | 16 #include "mojo/services/catalog/store.h" |
| 14 #include "mojo/shell/public/cpp/names.h" | 17 #include "mojo/shell/public/cpp/names.h" |
| 15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 16 #include "url/url_util.h" | 19 #include "url/url_util.h" |
| 17 | 20 |
| 18 namespace catalog { | 21 namespace catalog { |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 base::FilePath GetManifestPath(const base::FilePath& package_dir, | 24 base::FilePath GetManifestPath(const base::FilePath& package_dir, |
| 22 const std::string& name) { | 25 const std::string& name) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 45 #if defined OS_WIN | 48 #if defined OS_WIN |
| 46 std::string extension = ".exe"; | 49 std::string extension = ".exe"; |
| 47 #else | 50 #else |
| 48 std::string extension; | 51 std::string extension; |
| 49 #endif | 52 #endif |
| 50 return package_dir.AppendASCII(mojo::GetNamePath(name) + extension); | 53 return package_dir.AppendASCII(mojo::GetNamePath(name) + extension); |
| 51 } | 54 } |
| 52 return base::FilePath(); | 55 return base::FilePath(); |
| 53 } | 56 } |
| 54 | 57 |
| 58 scoped_ptr<ReadManifestResult> ProcessManifest( |
| 59 const base::FilePath& user_package_dir, |
| 60 const base::FilePath& system_package_dir, |
| 61 const std::string& name, |
| 62 scoped_ptr<base::Value> manifest_root) { |
| 63 scoped_ptr<Entry> entry(new Entry(name)); |
| 64 if (manifest_root) { |
| 65 const base::DictionaryValue* dictionary = nullptr; |
| 66 CHECK(manifest_root->GetAsDictionary(&dictionary)); |
| 67 entry = Entry::Deserialize(*dictionary); |
| 68 } |
| 69 entry->set_path(GetPackagePath(system_package_dir, name)); |
| 70 |
| 71 scoped_ptr<ReadManifestResult> result(new ReadManifestResult); |
| 72 // NOTE: This TypeConverter must run on a thread which allows IO. |
| 73 result->resolve_result = mojo::shell::mojom::ResolveResult::From(*entry); |
| 74 result->catalog_entry = std::move(entry); |
| 75 result->package_dir = system_package_dir; |
| 76 return result; |
| 77 } |
| 78 |
| 55 scoped_ptr<ReadManifestResult> ReadManifest( | 79 scoped_ptr<ReadManifestResult> ReadManifest( |
| 56 const base::FilePath& user_package_dir, | 80 const base::FilePath& user_package_dir, |
| 57 const base::FilePath& system_package_dir, | 81 const base::FilePath& system_package_dir, |
| 58 const std::string& name) { | 82 const std::string& name) { |
| 59 base::FilePath manifest_path = GetManifestPath(system_package_dir, name); | 83 base::FilePath manifest_path = GetManifestPath(system_package_dir, name); |
| 60 JSONFileValueDeserializer deserializer(manifest_path); | 84 JSONFileValueDeserializer deserializer(manifest_path); |
| 61 int error = 0; | 85 int error = 0; |
| 62 std::string message; | 86 std::string message; |
| 87 |
| 63 // TODO(beng): probably want to do more detailed error checking. This should | 88 // TODO(beng): probably want to do more detailed error checking. This should |
| 64 // be done when figuring out if to unblock connection completion. | 89 // be done when figuring out if to unblock connection completion. |
| 65 scoped_ptr<ReadManifestResult> result(new ReadManifestResult); | 90 return ProcessManifest(user_package_dir, system_package_dir, name, |
| 66 result->manifest_root = deserializer.Deserialize(&error, &message); | 91 deserializer.Deserialize(&error, &message)); |
| 67 result->package_dir = system_package_dir; | |
| 68 return result; | |
| 69 } | 92 } |
| 70 | 93 |
| 71 void AddEntryToMap(const Entry& entry, | 94 void AddEntryToMap(const Entry& entry, |
| 72 mojo::Map<mojo::String, mojom::CatalogEntryPtr>* map) { | 95 mojo::Map<mojo::String, mojom::CatalogEntryPtr>* map) { |
| 73 mojom::CatalogEntryPtr entry_ptr(mojom::CatalogEntry::New()); | 96 mojom::CatalogEntryPtr entry_ptr(mojom::CatalogEntry::New()); |
| 74 entry_ptr->display_name = entry.display_name(); | 97 entry_ptr->display_name = entry.display_name(); |
| 75 (*map)[entry.name()] = std::move(entry_ptr); | 98 (*map)[entry.name()] = std::move(entry_ptr); |
| 76 } | 99 } |
| 77 | 100 |
| 78 } // namespace | 101 } // namespace |
| 79 | 102 |
| 80 ReadManifestResult::ReadManifestResult() {} | 103 ReadManifestResult::ReadManifestResult() {} |
| 81 ReadManifestResult::~ReadManifestResult() {} | 104 ReadManifestResult::~ReadManifestResult() {} |
| 82 | 105 |
| 83 //////////////////////////////////////////////////////////////////////////////// | 106 //////////////////////////////////////////////////////////////////////////////// |
| 84 // Catalog, public: | 107 // Catalog, public: |
| 85 | 108 |
| 86 Catalog::Catalog(scoped_ptr<Store> store, | 109 Catalog::Catalog(scoped_ptr<Store> store, |
| 87 base::TaskRunner* file_task_runner, | 110 base::TaskRunner* file_task_runner, |
| 88 EntryCache* system_catalog) | 111 EntryCache* system_catalog, |
| 89 : store_(std::move(store)), | 112 ManifestProvider* manifest_provider) |
| 113 : manifest_provider_(manifest_provider), |
| 114 store_(std::move(store)), |
| 90 file_task_runner_(file_task_runner), | 115 file_task_runner_(file_task_runner), |
| 91 system_catalog_(system_catalog), | 116 system_catalog_(system_catalog), |
| 92 weak_factory_(this) { | 117 weak_factory_(this) { |
| 93 PathService::Get(base::DIR_MODULE, &system_package_dir_); | 118 PathService::Get(base::DIR_MODULE, &system_package_dir_); |
| 94 DeserializeCatalog(); | 119 DeserializeCatalog(); |
| 95 } | 120 } |
| 96 | 121 |
| 97 Catalog::~Catalog() {} | 122 Catalog::~Catalog() {} |
| 98 | 123 |
| 99 void Catalog::BindResolver(mojom::ResolverRequest request) { | 124 void Catalog::BindResolver(mojom::ResolverRequest request) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 auto entry = user_catalog_.find(mojo_name); | 173 auto entry = user_catalog_.find(mojo_name); |
| 149 if (entry != user_catalog_.end()) { | 174 if (entry != user_catalog_.end()) { |
| 150 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry->second)); | 175 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry->second)); |
| 151 return; | 176 return; |
| 152 } | 177 } |
| 153 entry = system_catalog_->find(mojo_name); | 178 entry = system_catalog_->find(mojo_name); |
| 154 if (entry != system_catalog_->end()) { | 179 if (entry != system_catalog_->end()) { |
| 155 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry->second)); | 180 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry->second)); |
| 156 return; | 181 return; |
| 157 } | 182 } |
| 158 base::PostTaskAndReplyWithResult( | 183 |
| 159 file_task_runner_, FROM_HERE, | 184 std::string manifest_contents; |
| 160 base::Bind(&ReadManifest, base::FilePath(), system_package_dir_, | 185 if (manifest_provider_ && |
| 161 mojo_name), | 186 manifest_provider_->GetApplicationManifest(mojo_name.To<std::string>(), |
| 162 base::Bind(&Catalog::OnReadManifest, weak_factory_.GetWeakPtr(), | 187 &manifest_contents)) { |
| 163 mojo_name, callback)); | 188 scoped_ptr<base::Value> manifest_root = |
| 189 base::JSONReader::Read(manifest_contents); |
| 190 base::PostTaskAndReplyWithResult( |
| 191 file_task_runner_, FROM_HERE, |
| 192 base::Bind(&ProcessManifest, user_package_dir_, system_package_dir_, |
| 193 mojo_name, base::Passed(&manifest_root)), |
| 194 base::Bind(&Catalog::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 195 mojo_name, callback)); |
| 196 } else { |
| 197 base::PostTaskAndReplyWithResult( |
| 198 file_task_runner_, FROM_HERE, |
| 199 base::Bind(&ReadManifest, user_package_dir_, system_package_dir_, |
| 200 mojo_name), |
| 201 base::Bind(&Catalog::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 202 mojo_name, callback)); |
| 203 } |
| 164 } | 204 } |
| 165 | 205 |
| 166 //////////////////////////////////////////////////////////////////////////////// | 206 //////////////////////////////////////////////////////////////////////////////// |
| 167 // Catalog, mojom::Catalog: | 207 // Catalog, mojom::Catalog: |
| 168 | 208 |
| 169 void Catalog::GetEntries(mojo::Array<mojo::String> names, | 209 void Catalog::GetEntries(mojo::Array<mojo::String> names, |
| 170 const GetEntriesCallback& callback) { | 210 const GetEntriesCallback& callback) { |
| 171 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; | 211 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; |
| 172 if (names.is_null()) { | 212 if (names.is_null()) { |
| 173 for (const auto& entry : user_catalog_) | 213 for (const auto& entry : user_catalog_) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 catalog->Append(entry.second->Serialize()); | 257 catalog->Append(entry.second->Serialize()); |
| 218 if (store_) | 258 if (store_) |
| 219 store_->UpdateStore(std::move(catalog)); | 259 store_->UpdateStore(std::move(catalog)); |
| 220 } | 260 } |
| 221 | 261 |
| 222 // static | 262 // static |
| 223 void Catalog::OnReadManifest(base::WeakPtr<Catalog> catalog, | 263 void Catalog::OnReadManifest(base::WeakPtr<Catalog> catalog, |
| 224 const std::string& name, | 264 const std::string& name, |
| 225 const ResolveMojoNameCallback& callback, | 265 const ResolveMojoNameCallback& callback, |
| 226 scoped_ptr<ReadManifestResult> result) { | 266 scoped_ptr<ReadManifestResult> result) { |
| 227 scoped_ptr<Entry> entry(new Entry(name)); | 267 callback.Run(std::move(result ->resolve_result)); |
| 228 if (result->manifest_root) { | |
| 229 const base::DictionaryValue* dictionary = nullptr; | |
| 230 CHECK(result->manifest_root->GetAsDictionary(&dictionary)); | |
| 231 entry = Entry::Deserialize(*dictionary); | |
| 232 } | |
| 233 entry->set_path(GetPackagePath(result->package_dir, name)); | |
| 234 | |
| 235 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry)); | |
| 236 if (catalog) { | 268 if (catalog) { |
| 237 catalog->AddEntryToCatalog( | 269 catalog->AddEntryToCatalog( |
| 238 std::move(entry), result->package_dir == catalog->system_package_dir_); | 270 std::move(result->catalog_entry), |
| 271 result->package_dir == catalog->system_package_dir_); |
| 239 } | 272 } |
| 240 } | 273 } |
| 241 | 274 |
| 242 void Catalog::AddEntryToCatalog(scoped_ptr<Entry> entry, | 275 void Catalog::AddEntryToCatalog(scoped_ptr<Entry> entry, |
| 243 bool is_system_catalog) { | 276 bool is_system_catalog) { |
| 244 DCHECK(entry); | 277 DCHECK(entry); |
| 245 EntryCache* catalog = is_system_catalog ? system_catalog_ : &user_catalog_; | 278 EntryCache* catalog = is_system_catalog ? system_catalog_ : &user_catalog_; |
| 246 if (catalog->end() != catalog->find(entry->name())) | 279 if (catalog->end() != catalog->find(entry->name())) |
| 247 return; | 280 return; |
| 248 for (auto child : entry->applications()) | 281 for (auto child : entry->applications()) |
| 249 AddEntryToCatalog(make_scoped_ptr(child), is_system_catalog); | 282 AddEntryToCatalog(make_scoped_ptr(child), is_system_catalog); |
| 250 (*catalog)[entry->name()] = std::move(entry); | 283 (*catalog)[entry->name()] = std::move(entry); |
| 251 SerializeCatalog(); | 284 SerializeCatalog(); |
| 252 } | 285 } |
| 253 | 286 |
| 254 } // namespace catalog | 287 } // namespace catalog |
| OLD | NEW |