Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Unified Diff: mojo/services/catalog/reader.cc

Issue 1821383002: Moves manifest parsing to a new class, Reader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@60catref
Patch Set: . Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/services/catalog/reader.h ('k') | mojo/services/catalog/store.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/services/catalog/reader.cc
diff --git a/mojo/services/catalog/reader.cc b/mojo/services/catalog/reader.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e26b778ed3414623b38ca28b7a4e359af66cd8e6
--- /dev/null
+++ b/mojo/services/catalog/reader.cc
@@ -0,0 +1,79 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/services/catalog/reader.h"
+
+#include "base/json/json_file_value_serializer.h"
+#include "base/location.h"
+#include "base/task_runner_util.h"
+#include "mojo/services/catalog/entry.h"
+#include "mojo/shell/public/cpp/names.h"
+
+namespace catalog {
+namespace {
+
+scoped_ptr<base::Value> ReadManifest(const base::FilePath& manifest_path) {
+ JSONFileValueDeserializer deserializer(manifest_path);
+ int error = 0;
+ std::string message;
+ // TODO(beng): probably want to do more detailed error checking. This should
+ // be done when figuring out if to unblock connection completion.
+ return deserializer.Deserialize(&error, &message);
+}
+
+void OnReadManifest(base::WeakPtr<Reader> reader,
+ const std::string& name,
+ const Reader::ReadManifestCallback& callback,
+ scoped_ptr<base::Value> manifest) {
+ if (!reader) {
+ // The Reader was destroyed, we're likely in shutdown. Run the callback so
+ // we don't trigger a DCHECK.
+ callback.Run(nullptr);
+ return;
+ }
+ scoped_ptr<Entry> entry;
+ if (manifest) {
+ const base::DictionaryValue* dictionary = nullptr;
+ CHECK(manifest->GetAsDictionary(&dictionary));
+ entry = Entry::Deserialize(*dictionary);
+ }
+ callback.Run(std::move(entry));
+}
+
+} // namespace
+
+Reader::Reader(const base::FilePath& package_path,
+ base::TaskRunner* file_task_runner)
+ : package_path_(package_path),
+ file_task_runner_(file_task_runner),
+ weak_factory_(this) {}
+Reader::~Reader() {}
+
+void Reader::Read(const std::string& name,
+ const ReadManifestCallback& callback) {
+ base::FilePath manifest_path = GetManifestPath(name);
+ if (manifest_path.empty()) {
+ callback.Run(nullptr);
+ return;
+ }
+
+ std::string type = mojo::GetNameType(name);
+ CHECK(type == "mojo" || type == "exe");
+ base::PostTaskAndReplyWithResult(
+ file_task_runner_, FROM_HERE, base::Bind(&ReadManifest, manifest_path),
+ base::Bind(&OnReadManifest, weak_factory_.GetWeakPtr(), name, callback));
+}
+
+base::FilePath Reader::GetManifestPath(const std::string& name) const {
+ // TODO(beng): think more about how this should be done for exe targets.
+ std::string type = mojo::GetNameType(name);
+ std::string path = mojo::GetNamePath(name);
+ if (type == "mojo")
+ return package_path_.AppendASCII(path + "/manifest.json");
+ else if (type == "exe")
+ return package_path_.AppendASCII(path + "_manifest.json");
+ return base::FilePath();
+}
+
+} // namespace catalog
« no previous file with comments | « mojo/services/catalog/reader.h ('k') | mojo/services/catalog/store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698