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 void OnReadManifest(base::WeakPtr<Reader> reader, | |
26 const std::string& name, | |
27 const Reader::ReadManifestCallback& callback, | |
28 scoped_ptr<base::Value> manifest) { | |
29 if (!reader) { | |
30 // The Reader was destroyed, we're likely in shutdown. Run the callback so | |
31 // we don't trigger a DCHECK. | |
32 callback.Run(nullptr); | |
33 return; | |
34 } | |
35 scoped_ptr<Entry> entry; | |
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 } | |
43 | |
44 } // namespace | |
45 | |
46 Reader::Reader(const base::FilePath& package_path, | |
47 base::TaskRunner* file_task_runner) | |
48 : package_path_(package_path), | |
49 file_task_runner_(file_task_runner), | |
50 weak_factory_(this) {} | |
51 Reader::~Reader() {} | |
52 | |
53 void Reader::Read(const std::string& name, | |
54 const ReadManifestCallback& callback) { | |
55 base::FilePath manifest_path = GetManifestPath(name); | |
56 if (manifest_path.empty()) { | |
57 callback.Run(nullptr); | |
58 return; | |
59 } | |
60 | |
61 std::string type = mojo::GetNameType(name); | |
62 CHECK(type == "mojo" || type == "exe"); | |
63 base::PostTaskAndReplyWithResult( | |
64 file_task_runner_, FROM_HERE, base::Bind(&ReadManifest, manifest_path), | |
65 base::Bind(&OnReadManifest, weak_factory_.GetWeakPtr(), name, callback)); | |
66 } | |
67 | |
68 base::FilePath Reader::GetManifestPath(const std::string& name) const { | |
69 // TODO(beng): think more about how this should be done for exe targets. | |
70 std::string type = mojo::GetNameType(name); | |
71 std::string path = mojo::GetNamePath(name); | |
72 if (type == "mojo") | |
73 return package_path_.AppendASCII(path + "/manifest.json"); | |
74 else if (type == "exe") | |
75 return package_path_.AppendASCII(path + "_manifest.json"); | |
76 return base::FilePath(); | |
77 } | |
78 | |
79 } // namespace catalog | |
OLD | NEW |