| 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/reader.h" | 5 #include "services/catalog/reader.h" |
| 6 | 6 |
| 7 #include "base/base_paths.h" | 7 #include "base/base_paths.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/task_runner_util.h" | 17 #include "base/task_runner_util.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "services/catalog/constants.h" | 20 #include "services/catalog/constants.h" |
| 21 #include "services/catalog/entry.h" | 21 #include "services/catalog/entry.h" |
| 22 #include "services/catalog/entry_cache.h" |
| 22 #include "services/catalog/manifest_provider.h" | 23 #include "services/catalog/manifest_provider.h" |
| 23 #include "services/catalog/public/interfaces/constants.mojom.h" | 24 #include "services/catalog/public/interfaces/constants.mojom.h" |
| 24 #include "services/service_manager/public/interfaces/constants.mojom.h" | 25 #include "services/service_manager/public/interfaces/constants.mojom.h" |
| 25 | 26 |
| 26 namespace catalog { | 27 namespace catalog { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 30 const char kServiceExecutableExtension[] = ".service.exe"; | 31 const char kServiceExecutableExtension[] = ".service.exe"; |
| 31 #else | 32 #else |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 std::unique_ptr<Entry> entry = CreateEntryForManifestAt(manifest_path, | 146 std::unique_ptr<Entry> entry = CreateEntryForManifestAt(manifest_path, |
| 146 package_dir); | 147 package_dir); |
| 147 if (!entry) { | 148 if (!entry) { |
| 148 entry.reset(new Entry(mojo_name)); | 149 entry.reset(new Entry(mojo_name)); |
| 149 entry->set_path(GetExecutablePath( | 150 entry->set_path(GetExecutablePath( |
| 150 package_dir.AppendASCII(kPackagesDirName), mojo_name)); | 151 package_dir.AppendASCII(kPackagesDirName), mojo_name)); |
| 151 } | 152 } |
| 152 return entry; | 153 return entry; |
| 153 } | 154 } |
| 154 | 155 |
| 155 void AddEntryToCache(EntryCache* cache, std::unique_ptr<Entry> entry) { | 156 void IgnoreResolveResult(service_manager::mojom::ResolveResultPtr, |
| 156 std::vector<std::unique_ptr<Entry>> children = entry->TakeChildren(); | 157 service_manager::mojom::ResolveResultPtr) {} |
| 157 for (auto& child : children) | |
| 158 AddEntryToCache(cache, std::move(child)); | |
| 159 (*cache)[entry->name()] = std::move(entry); | |
| 160 } | |
| 161 | |
| 162 void DoNothing(service_manager::mojom::ResolveResultPtr) {} | |
| 163 | 158 |
| 164 void LoadCatalogManifestIntoCache(const base::Value* root, | 159 void LoadCatalogManifestIntoCache(const base::Value* root, |
| 165 const base::FilePath& package_dir, | 160 const base::FilePath& package_dir, |
| 166 EntryCache* cache) { | 161 EntryCache* cache) { |
| 167 DCHECK(root); | 162 DCHECK(root); |
| 168 const base::DictionaryValue* catalog = nullptr; | 163 const base::DictionaryValue* catalog = nullptr; |
| 169 if (!root->GetAsDictionary(&catalog)) { | 164 if (!root->GetAsDictionary(&catalog)) { |
| 170 LOG(ERROR) << "Catalog manifest is not a dictionary value."; | 165 LOG(ERROR) << "Catalog manifest is not a dictionary value."; |
| 171 return; | 166 return; |
| 172 } | 167 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 LOG(ERROR) << "Catalog entry for \"" << it.key() << "\" has an invalid " | 210 LOG(ERROR) << "Catalog entry for \"" << it.key() << "\" has an invalid " |
| 216 << "\"manifest\" value."; | 211 << "\"manifest\" value."; |
| 217 continue; | 212 continue; |
| 218 } | 213 } |
| 219 | 214 |
| 220 DCHECK(!(is_embedded && !executable_path.empty())); | 215 DCHECK(!(is_embedded && !executable_path.empty())); |
| 221 | 216 |
| 222 auto entry = ProcessManifest( | 217 auto entry = ProcessManifest( |
| 223 manifest, is_embedded ? base::FilePath() : package_dir, | 218 manifest, is_embedded ? base::FilePath() : package_dir, |
| 224 executable_path); | 219 executable_path); |
| 225 if (entry) | 220 if (entry) { |
| 226 AddEntryToCache(cache, std::move(entry)); | 221 bool added = cache->AddRootEntry(std::move(entry)); |
| 227 else | 222 DCHECK(added); |
| 223 } else { |
| 228 LOG(ERROR) << "Failed to read manifest entry for \"" << it.key() << "\"."; | 224 LOG(ERROR) << "Failed to read manifest entry for \"" << it.key() << "\"."; |
| 225 } |
| 229 } | 226 } |
| 230 } | 227 } |
| 231 | 228 |
| 232 } // namespace | 229 } // namespace |
| 233 | 230 |
| 234 Reader::Reader(std::unique_ptr<base::Value> static_manifest, | 231 Reader::Reader(std::unique_ptr<base::Value> static_manifest, |
| 235 EntryCache* cache) | 232 EntryCache* cache) |
| 236 : using_static_catalog_(true), | 233 : using_static_catalog_(true), |
| 237 manifest_provider_(nullptr), | 234 manifest_provider_(nullptr), |
| 238 weak_factory_(this) { | 235 weak_factory_(this) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 261 | 258 |
| 262 Reader::~Reader() {} | 259 Reader::~Reader() {} |
| 263 | 260 |
| 264 void Reader::Read(const base::FilePath& package_dir, | 261 void Reader::Read(const base::FilePath& package_dir, |
| 265 EntryCache* cache, | 262 EntryCache* cache, |
| 266 const base::Closure& read_complete_closure) { | 263 const base::Closure& read_complete_closure) { |
| 267 file_task_runner_->PostTask( | 264 file_task_runner_->PostTask( |
| 268 FROM_HERE, | 265 FROM_HERE, |
| 269 base::Bind(&ScanDir, package_dir, | 266 base::Bind(&ScanDir, package_dir, |
| 270 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), | 267 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 271 cache, base::Bind(&DoNothing)), | 268 cache, base::Bind(&IgnoreResolveResult)), |
| 272 base::ThreadTaskRunnerHandle::Get(), | 269 base::ThreadTaskRunnerHandle::Get(), |
| 273 read_complete_closure)); | 270 read_complete_closure)); |
| 274 } | 271 } |
| 275 | 272 |
| 276 void Reader::CreateEntryForName( | 273 void Reader::CreateEntryForName( |
| 277 const std::string& mojo_name, | 274 const std::string& name, |
| 278 EntryCache* cache, | 275 EntryCache* cache, |
| 279 const CreateEntryForNameCallback& entry_created_callback) { | 276 const CreateEntryForNameCallback& entry_created_callback) { |
| 280 if (manifest_provider_) { | 277 if (manifest_provider_) { |
| 281 std::unique_ptr<base::Value> manifest_root = | 278 std::unique_ptr<base::Value> manifest_root = |
| 282 manifest_provider_->GetManifest(mojo_name); | 279 manifest_provider_->GetManifest(name); |
| 283 if (manifest_root) { | 280 if (manifest_root) { |
| 284 base::PostTaskAndReplyWithResult( | 281 base::PostTaskAndReplyWithResult( |
| 285 file_task_runner_.get(), FROM_HERE, | 282 file_task_runner_.get(), FROM_HERE, |
| 286 base::Bind(&ProcessUniqueManifest, base::Passed(&manifest_root), | 283 base::Bind(&ProcessUniqueManifest, base::Passed(&manifest_root), |
| 287 system_package_dir_), | 284 system_package_dir_), |
| 288 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), cache, | 285 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), cache, |
| 289 entry_created_callback)); | 286 entry_created_callback)); |
| 290 return; | 287 return; |
| 291 } | 288 } |
| 292 } else if (using_static_catalog_) { | 289 } else if (using_static_catalog_) { |
| 293 // A Reader using a static catalog manifest does not support dynamic | 290 // A Reader using a static catalog manifest does not support dynamic |
| 294 // discovery or introduction of new catalog entries. | 291 // discovery or introduction of new catalog entries. |
| 295 entry_created_callback.Run(service_manager::mojom::ResolveResultPtr()); | 292 entry_created_callback.Run(service_manager::mojom::ResolveResultPtr(), |
| 293 service_manager::mojom::ResolveResultPtr()); |
| 296 return; | 294 return; |
| 297 } | 295 } |
| 298 | 296 |
| 299 base::FilePath manifest_path_override; | 297 base::FilePath manifest_path_override; |
| 300 { | 298 { |
| 301 auto override_iter = manifest_path_overrides_.find(mojo_name); | 299 auto override_iter = manifest_path_overrides_.find(name); |
| 302 if (override_iter != manifest_path_overrides_.end()) | 300 if (override_iter != manifest_path_overrides_.end()) |
| 303 manifest_path_override = override_iter->second; | 301 manifest_path_override = override_iter->second; |
| 304 } | 302 } |
| 305 | 303 |
| 306 std::string package_name_override; | 304 std::string package_name_override; |
| 307 { | 305 { |
| 308 auto override_iter = package_name_overrides_.find(mojo_name); | 306 auto override_iter = package_name_overrides_.find(name); |
| 309 if (override_iter != package_name_overrides_.end()) | 307 if (override_iter != package_name_overrides_.end()) |
| 310 package_name_override = override_iter->second; | 308 package_name_override = override_iter->second; |
| 311 } | 309 } |
| 312 base::PostTaskAndReplyWithResult( | 310 base::PostTaskAndReplyWithResult( |
| 313 file_task_runner_.get(), FROM_HERE, | 311 file_task_runner_.get(), FROM_HERE, |
| 314 base::Bind(&ReadManifest, system_package_dir_, mojo_name, | 312 base::Bind(&ReadManifest, system_package_dir_, name, |
| 315 package_name_override, manifest_path_override), | 313 package_name_override, manifest_path_override), |
| 316 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), cache, | 314 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), cache, |
| 317 entry_created_callback)); | 315 entry_created_callback)); |
| 318 } | 316 } |
| 319 | 317 |
| 320 void Reader::OverridePackageName(const std::string& service_name, | 318 void Reader::OverridePackageName(const std::string& service_name, |
| 321 const std::string& package_name) { | 319 const std::string& package_name) { |
| 322 package_name_overrides_.insert(std::make_pair(service_name, package_name)); | 320 package_name_overrides_.insert(std::make_pair(service_name, package_name)); |
| 323 } | 321 } |
| 324 | 322 |
| 325 void Reader::OverrideManifestPath(const std::string& service_name, | 323 void Reader::OverrideManifestPath(const std::string& service_name, |
| 326 const base::FilePath& path) { | 324 const base::FilePath& path) { |
| 327 manifest_path_overrides_.insert(std::make_pair(service_name, path)); | 325 manifest_path_overrides_.insert(std::make_pair(service_name, path)); |
| 328 } | 326 } |
| 329 | 327 |
| 330 Reader::Reader(ManifestProvider* manifest_provider) | 328 Reader::Reader(ManifestProvider* manifest_provider) |
| 331 : using_static_catalog_(false), | 329 : using_static_catalog_(false), |
| 332 manifest_provider_(manifest_provider), | 330 manifest_provider_(manifest_provider), |
| 333 weak_factory_(this) { | 331 weak_factory_(this) { |
| 334 PathService::Get(base::DIR_MODULE, &system_package_dir_); | 332 PathService::Get(base::DIR_MODULE, &system_package_dir_); |
| 335 } | 333 } |
| 336 | 334 |
| 337 void Reader::OnReadManifest( | 335 void Reader::OnReadManifest( |
| 338 EntryCache* cache, | 336 EntryCache* cache, |
| 339 const CreateEntryForNameCallback& entry_created_callback, | 337 const CreateEntryForNameCallback& entry_created_callback, |
| 340 std::unique_ptr<Entry> entry) { | 338 std::unique_ptr<Entry> entry) { |
| 341 if (!entry) | 339 if (!entry) |
| 342 return; | 340 return; |
| 343 service_manager::mojom::ResolveResultPtr result = | 341 |
| 344 service_manager::mojom::ResolveResult::From(*entry); | 342 std::string name = entry->name(); |
| 345 AddEntryToCache(cache, std::move(entry)); | 343 cache->AddRootEntry(std::move(entry)); |
| 346 entry_created_callback.Run(std::move(result)); | 344 |
| 345 // NOTE: It's currently possible to end up with a duplicate entry, in which |
| 346 // case the above call to AddRootEntry() may actually discard |entry|. We |
| 347 // therefore look up the Entry by name here to get resolution metadata. |
| 348 |
| 349 const Entry* resolved_entry = cache->GetEntry(name); |
| 350 DCHECK(resolved_entry); |
| 351 entry_created_callback.Run( |
| 352 service_manager::mojom::ResolveResult::From(resolved_entry), |
| 353 service_manager::mojom::ResolveResult::From(resolved_entry->parent())); |
| 347 } | 354 } |
| 348 | 355 |
| 349 } // namespace catalog | 356 } // namespace catalog |
| OLD | NEW |