Chromium Code Reviews| 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 #include "mojo/services/catalog/reader.h" | |
| 6 | |
| 7 #include "base/json/json_file_value_serializer.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/task_runner_util.h" | |
| 10 #include "mojo/services/catalog/entry.h" | |
| 11 #include "mojo/shell/public/cpp/names.h" | |
| 12 | |
| 13 namespace catalog { | |
| 14 namespace { | |
| 15 | |
| 16 scoped_ptr<base::Value> ReadManifest(const base::FilePath& manifest_path) { | |
| 17 JSONFileValueDeserializer deserializer(manifest_path); | |
| 18 int error = 0; | |
| 19 std::string message; | |
| 20 // TODO(beng): probably want to do more detailed error checking. This should | |
| 21 // be done when figuring out if to unblock connection completion. | |
| 22 return deserializer.Deserialize(&error, &message); | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 Reader::Reader(const base::FilePath& package_path, | |
| 28 base::TaskRunner* file_task_runner) | |
| 29 : package_path_(package_path), | |
| 30 file_task_runner_(file_task_runner), | |
| 31 weak_factory_(this) {} | |
| 32 Reader::~Reader() {} | |
| 33 | |
| 34 void Reader::Read(const std::string& name, | |
| 35 const ReadManifestCallback& callback) { | |
| 36 base::FilePath manifest_path = GetManifestPath(name); | |
| 37 if (manifest_path.empty()) { | |
| 38 callback.Run(nullptr); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 std::string type = mojo::GetNameType(name); | |
| 43 CHECK(type == "mojo" || type == "exe"); | |
| 44 base::PostTaskAndReplyWithResult( | |
| 45 file_task_runner_, FROM_HERE, base::Bind(&ReadManifest, manifest_path), | |
| 46 base::Bind(&Reader::OnReadManifest, weak_factory_.GetWeakPtr(), | |
| 47 name, callback)); | |
| 48 } | |
| 49 | |
| 50 base::FilePath Reader::GetManifestPath(const std::string& name) const { | |
| 51 // TODO(beng): think more about how this should be done for exe targets. | |
| 52 std::string type = mojo::GetNameType(name); | |
| 53 std::string path = mojo::GetNamePath(name); | |
| 54 if (type == "mojo") | |
| 55 return package_path_.AppendASCII(path + "/manifest.json"); | |
| 56 else if (type == "exe") | |
| 57 return package_path_.AppendASCII(path + "_manifest.json"); | |
| 58 return base::FilePath(); | |
| 59 } | |
| 60 | |
| 61 // static | |
| 62 void Reader::OnReadManifest(base::WeakPtr<Reader> reader, | |
|
sky
2016/03/22 23:47:46
Is there a reason you need this static? It doesn't
| |
| 63 const std::string& name, | |
| 64 const ReadManifestCallback& callback, | |
| 65 scoped_ptr<base::Value> manifest) { | |
| 66 if (!reader) { | |
| 67 // The Reader was destroyed, we're likely in shutdown. Run the callback so | |
| 68 // we don't trigger a DCHECK. | |
| 69 callback.Run(nullptr); | |
| 70 return; | |
| 71 } | |
| 72 reader->OnReadManifestImpl(callback, std::move(manifest)); | |
| 73 } | |
| 74 | |
| 75 void Reader::OnReadManifestImpl(const ReadManifestCallback& callback, | |
| 76 scoped_ptr<base::Value> manifest) { | |
| 77 scoped_ptr<Entry> entry; | |
| 78 if (manifest) { | |
| 79 const base::DictionaryValue* dictionary = nullptr; | |
| 80 CHECK(manifest->GetAsDictionary(&dictionary)); | |
| 81 entry = Entry::Deserialize(*dictionary); | |
| 82 } | |
| 83 callback.Run(std::move(entry)); | |
| 84 } | |
| 85 | |
| 86 } // namespace catalog | |
| OLD | NEW |