OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_SERVICES_CATALOG_READER_H_ |
| 6 #define MOJO_SERVICES_CATALOG_READER_H_ |
| 7 |
| 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/thread_task_runner_handle.h" |
| 11 |
| 12 namespace catalog { |
| 13 namespace { |
| 14 |
| 15 base::FilePath GetManifestPath(const base::FilePath& package_dir, |
| 16 const std::string& name) { |
| 17 // TODO(beng): think more about how this should be done for exe targets. |
| 18 std::string type = mojo::GetNameType(name); |
| 19 std::string path = mojo::GetNamePath(name); |
| 20 if (type == mojo::kNameType_Mojo) |
| 21 return package_dir.AppendASCII(path + "/manifest.json"); |
| 22 if (type == mojo::kNameType_Exe) |
| 23 return package_dir.AppendASCII(path + "_manifest.json"); |
| 24 return base::FilePath(); |
| 25 } |
| 26 |
| 27 base::FilePath GetPackagePath(const base::FilePath& package_dir, |
| 28 const std::string& name) { |
| 29 std::string type = mojo::GetNameType(name); |
| 30 if (type == mojo::kNameType_Mojo) { |
| 31 // It's still a mojo: URL, use the default mapping scheme. |
| 32 const std::string host = mojo::GetNamePath(name); |
| 33 return package_dir.AppendASCII(host + "/" + host + ".mojo"); |
| 34 } |
| 35 if (type == mojo::kNameType_Exe) { |
| 36 #if defined OS_WIN |
| 37 std::string extension = ".exe"; |
| 38 #else |
| 39 std::string extension; |
| 40 #endif |
| 41 return package_dir.AppendASCII(mojo::GetNamePath(name) + extension); |
| 42 } |
| 43 return base::FilePath(); |
| 44 } |
| 45 |
| 46 } |
| 47 |
| 48 Reader::Reader(EntryCache* entry_cache, |
| 49 base::TaskRunner* file_task_runner, |
| 50 const base::FilePath& package_dir, |
| 51 const base::Closure& read_complete_closure) |
| 52 : entry_cache_(entry_cache), |
| 53 original_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 54 file_task_runner_(file_task_runner), |
| 55 package_dir_(package_dir), |
| 56 read_complete_closure_(read_complete_closure) {} |
| 57 |
| 58 Reader::~Reader() {} |
| 59 |
| 60 void Reader::ReadAllManifests(const base::Closure& read_complete_closure) { |
| 61 base::PostTaskAndReplyWithResult( |
| 62 file_task_runer_, |
| 63 FROM_HERE, |
| 64 base::Bind(&Reader::ScanPackageDir, base::Unretained(this), |
| 65 package_dir_), |
| 66 read_complete_closure); |
| 67 } |
| 68 |
| 69 void Reader::ReadManifestForName(const std::string& mojo_name, |
| 70 const base::Closure& read_complete_closure) { |
| 71 base::PostTaskAndReplyWithResult( |
| 72 file_task_runer_, |
| 73 FROM_HERE, |
| 74 base::Bind(&Reader::ReadManifestForNameOnFileThread, base::Unretained(this
), |
| 75 mojo_name), |
| 76 read_complete_closure); |
| 77 } |
| 78 |
| 79 void Reader::ScanPackageDir(const base::FilePath& package_dir) { |
| 80 base::FileEnumerator enumerator(package_dir, false, base::DIRECTORIES); |
| 81 do { |
| 82 base::FilePath app_directory = enumerator.Next(); |
| 83 if (app_directory.empty()) |
| 84 break; |
| 85 |
| 86 ReadManifestAtPath(app_directory.Append("manifest.json")); |
| 87 } while (true); |
| 88 } |
| 89 |
| 90 void Reader::ReadManifestForNameOnFileThread(const std::string& mojo_name) { |
| 91 ReadManifestAtPath(GetManifestPath(mojo_name)) |
| 92 } |
| 93 |
| 94 void Reader::ReadManifestAtPath() { |
| 95 JSONFileValueDeserializer deserializer(manifest_path); |
| 96 int error = 0; |
| 97 std::string message; |
| 98 scoped_ptr<base::Value> value = deserializer.Deserialize(&error, &message); |
| 99 original_task_runner_->PostTask( |
| 100 FROM_HERE, |
| 101 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 102 std::move(value))); |
| 103 } |
| 104 |
| 105 void Reader::OnReadManifest(scoped_ptr<base::Value> manifest) { |
| 106 scoped_ptr<Entry> entry(new Entry(name)); |
| 107 if (manifest) { |
| 108 const base::DictionaryValue* dictionary = nullptr; |
| 109 CHECK(manifest->GetAsDictionary(&dictionary)); |
| 110 entry = Entry::Deserialize(*dictionary); |
| 111 } |
| 112 entry->set_path(GetPackagePath(package_dir_, name)); |
| 113 AddEntryToCatalog(std::move(entry)); |
| 114 } |
| 115 |
| 116 void Reader::AddEntryToCatalog(scoped_ptr<Entry> entry) { |
| 117 DCHECK(entry); |
| 118 if (entry_cache_->end() != entry_cache_->find(entry->name())) |
| 119 return; |
| 120 for (auto child : entry->applications()) |
| 121 AddEntryToCatalog(make_scoped_ptr(child)); |
| 122 (*entry_cache_)[entry->name()] = std::move(entry); |
| 123 } |
| 124 |
| 125 } // namespace catalog |
| 126 |
| 127 #endif // MOJO_SERVICES_CATALOG_READER_H_ |
OLD | NEW |