| 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 19 matching lines...) Expand all Loading... |
| 42 #if defined OS_WIN | 45 #if defined OS_WIN |
| 43 std::string extension = ".exe"; | 46 std::string extension = ".exe"; |
| 44 #else | 47 #else |
| 45 std::string extension; | 48 std::string extension; |
| 46 #endif | 49 #endif |
| 47 return package_dir.AppendASCII(mojo::GetNamePath(name) + extension); | 50 return package_dir.AppendASCII(mojo::GetNamePath(name) + extension); |
| 48 } | 51 } |
| 49 return base::FilePath(); | 52 return base::FilePath(); |
| 50 } | 53 } |
| 51 | 54 |
| 55 scoped_ptr<ReadManifestResult> ProcessManifest( |
| 56 const base::FilePath& package_dir, |
| 57 const std::string& name, |
| 58 scoped_ptr<base::Value> manifest_root) { |
| 59 scoped_ptr<Entry> entry(new Entry(name)); |
| 60 if (manifest_root) { |
| 61 const base::DictionaryValue* dictionary = nullptr; |
| 62 CHECK(manifest_root->GetAsDictionary(&dictionary)); |
| 63 entry = Entry::Deserialize(*dictionary); |
| 64 } |
| 65 entry->set_path(GetPackagePath(package_dir, name)); |
| 66 |
| 67 scoped_ptr<ReadManifestResult> result(new ReadManifestResult); |
| 68 // NOTE: This TypeConverter must run on a thread which allows IO. |
| 69 result->resolve_result = mojo::shell::mojom::ResolveResult::From(*entry); |
| 70 result->catalog_entry = std::move(entry); |
| 71 return result; |
| 72 } |
| 73 |
| 52 scoped_ptr<ReadManifestResult> ReadManifest(const base::FilePath& package_dir, | 74 scoped_ptr<ReadManifestResult> ReadManifest(const base::FilePath& package_dir, |
| 53 const std::string& name) { | 75 const std::string& name) { |
| 54 base::FilePath manifest_path = GetManifestPath(package_dir, name); | 76 base::FilePath manifest_path = GetManifestPath(package_dir, name); |
| 55 JSONFileValueDeserializer deserializer(manifest_path); | 77 JSONFileValueDeserializer deserializer(manifest_path); |
| 56 int error = 0; | 78 int error = 0; |
| 57 std::string message; | 79 std::string message; |
| 80 |
| 58 // TODO(beng): probably want to do more detailed error checking. This should | 81 // TODO(beng): probably want to do more detailed error checking. This should |
| 59 // be done when figuring out if to unblock connection completion. | 82 // be done when figuring out if to unblock connection completion. |
| 60 scoped_ptr<ReadManifestResult> result(new ReadManifestResult); | 83 return ProcessManifest(package_dir, name, |
| 61 result->manifest_root = deserializer.Deserialize(&error, &message); | 84 deserializer.Deserialize(&error, &message)); |
| 62 result->package_dir = package_dir; | |
| 63 return result; | |
| 64 } | 85 } |
| 65 | 86 |
| 66 } // namespace | 87 } // namespace |
| 67 | 88 |
| 68 ReadManifestResult::ReadManifestResult() {} | 89 ReadManifestResult::ReadManifestResult() {} |
| 69 ReadManifestResult::~ReadManifestResult() {} | 90 ReadManifestResult::~ReadManifestResult() {} |
| 70 | 91 |
| 71 //////////////////////////////////////////////////////////////////////////////// | 92 //////////////////////////////////////////////////////////////////////////////// |
| 72 // Catalog, public: | 93 // Catalog, public: |
| 73 | 94 |
| 74 Catalog::Catalog(scoped_ptr<Store> store, base::TaskRunner* file_task_runner) | 95 Catalog::Catalog(scoped_ptr<Store> store, |
| 75 : store_(std::move(store)), | 96 base::TaskRunner* file_task_runner, |
| 97 ManifestProvider* manifest_provider) |
| 98 : manifest_provider_(manifest_provider), |
| 99 store_(std::move(store)), |
| 76 file_task_runner_(file_task_runner), | 100 file_task_runner_(file_task_runner), |
| 77 weak_factory_(this) { | 101 weak_factory_(this) { |
| 78 PathService::Get(base::DIR_MODULE, &system_package_dir_); | 102 PathService::Get(base::DIR_MODULE, &system_package_dir_); |
| 79 DeserializeCatalog(); | 103 DeserializeCatalog(); |
| 80 } | 104 } |
| 81 | 105 |
| 82 Catalog::~Catalog() {} | 106 Catalog::~Catalog() {} |
| 83 | 107 |
| 84 void Catalog::BindResolver(mojom::ResolverRequest request) { | 108 void Catalog::BindResolver(mojom::ResolverRequest request) { |
| 85 resolver_bindings_.AddBinding(this, std::move(request)); | 109 resolver_bindings_.AddBinding(this, std::move(request)); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 if (type != "mojo" && type != "exe") { | 151 if (type != "mojo" && type != "exe") { |
| 128 scoped_ptr<Entry> entry(new Entry(mojo_name)); | 152 scoped_ptr<Entry> entry(new Entry(mojo_name)); |
| 129 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry)); | 153 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry)); |
| 130 return; | 154 return; |
| 131 } | 155 } |
| 132 | 156 |
| 133 auto entry = catalog_.find(mojo_name); | 157 auto entry = catalog_.find(mojo_name); |
| 134 if (entry != catalog_.end()) { | 158 if (entry != catalog_.end()) { |
| 135 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry->second)); | 159 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry->second)); |
| 136 } else { | 160 } else { |
| 137 base::PostTaskAndReplyWithResult( | 161 std::string manifest_contents; |
| 138 file_task_runner_, FROM_HERE, | 162 if (manifest_provider_ && |
| 139 base::Bind(&ReadManifest, system_package_dir_, mojo_name), | 163 manifest_provider_->GetApplicationManifest(mojo_name.To<std::string>(), |
| 140 base::Bind(&Catalog::OnReadManifest, weak_factory_.GetWeakPtr(), | 164 &manifest_contents)) { |
| 141 mojo_name, callback)); | 165 scoped_ptr<base::Value> manifest_root = |
| 166 base::JSONReader::Read(manifest_contents); |
| 167 base::PostTaskAndReplyWithResult( |
| 168 file_task_runner_, FROM_HERE, |
| 169 base::Bind(&ProcessManifest, system_package_dir_, mojo_name, |
| 170 base::Passed(&manifest_root)), |
| 171 base::Bind(&Catalog::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 172 mojo_name, callback)); |
| 173 } else { |
| 174 base::PostTaskAndReplyWithResult( |
| 175 file_task_runner_, FROM_HERE, |
| 176 base::Bind(&ReadManifest, system_package_dir_, mojo_name), |
| 177 base::Bind(&Catalog::OnReadManifest, weak_factory_.GetWeakPtr(), |
| 178 mojo_name, callback)); |
| 179 } |
| 142 } | 180 } |
| 143 } | 181 } |
| 144 | 182 |
| 145 //////////////////////////////////////////////////////////////////////////////// | 183 //////////////////////////////////////////////////////////////////////////////// |
| 146 // Catalog, mojom::Catalog: | 184 // Catalog, mojom::Catalog: |
| 147 | 185 |
| 148 void Catalog::GetEntries(mojo::Array<mojo::String> names, | 186 void Catalog::GetEntries(mojo::Array<mojo::String> names, |
| 149 const GetEntriesCallback& callback) { | 187 const GetEntriesCallback& callback) { |
| 150 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; | 188 mojo::Map<mojo::String, mojom::CatalogEntryPtr> entries; |
| 151 std::vector<mojo::String> names_vec = names.PassStorage(); | 189 std::vector<mojo::String> names_vec = names.PassStorage(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 catalog->Append(entry.second->Serialize()); | 223 catalog->Append(entry.second->Serialize()); |
| 186 if (store_) | 224 if (store_) |
| 187 store_->UpdateStore(std::move(catalog)); | 225 store_->UpdateStore(std::move(catalog)); |
| 188 } | 226 } |
| 189 | 227 |
| 190 // static | 228 // static |
| 191 void Catalog::OnReadManifest(base::WeakPtr<Catalog> catalog, | 229 void Catalog::OnReadManifest(base::WeakPtr<Catalog> catalog, |
| 192 const std::string& name, | 230 const std::string& name, |
| 193 const ResolveMojoNameCallback& callback, | 231 const ResolveMojoNameCallback& callback, |
| 194 scoped_ptr<ReadManifestResult> result) { | 232 scoped_ptr<ReadManifestResult> result) { |
| 195 scoped_ptr<Entry> entry(new Entry(name)); | 233 callback.Run(std::move(result ->resolve_result)); |
| 196 if (result->manifest_root) { | |
| 197 const base::DictionaryValue* dictionary = nullptr; | |
| 198 CHECK(result->manifest_root->GetAsDictionary(&dictionary)); | |
| 199 entry = Entry::Deserialize(*dictionary); | |
| 200 } | |
| 201 entry->set_path(GetPackagePath(result->package_dir, name)); | |
| 202 | |
| 203 callback.Run(mojo::shell::mojom::ResolveResult::From(*entry)); | |
| 204 if (catalog) | 234 if (catalog) |
| 205 catalog->AddEntryToCatalog(std::move(entry)); | 235 catalog->AddEntryToCatalog(std::move(result->catalog_entry)); |
| 206 } | 236 } |
| 207 | 237 |
| 208 void Catalog::AddEntryToCatalog(scoped_ptr<Entry> entry) { | 238 void Catalog::AddEntryToCatalog(scoped_ptr<Entry> entry) { |
| 209 DCHECK(entry); | 239 DCHECK(entry); |
| 210 if (catalog_.end() != catalog_.find(entry->name())) | 240 if (catalog_.end() != catalog_.find(entry->name())) |
| 211 return; | 241 return; |
| 212 for (auto child : entry->applications()) | 242 for (auto child : entry->applications()) |
| 213 AddEntryToCatalog(make_scoped_ptr(child)); | 243 AddEntryToCatalog(make_scoped_ptr(child)); |
| 214 catalog_[entry->name()] = std::move(entry); | 244 catalog_[entry->name()] = std::move(entry); |
| 215 SerializeCatalog(); | 245 SerializeCatalog(); |
| 216 } | 246 } |
| 217 | 247 |
| 218 } // namespace catalog | 248 } // namespace catalog |
| OLD | NEW |