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