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

Unified Diff: content/renderer/manifest/manifest_parser.cc

Issue 550173002: Implement ManifestParser in content/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@manifest_fetcher
Patch Set: add ctor/dtor Created 6 years, 3 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
Index: content/renderer/manifest/manifest_parser.cc
diff --git a/content/renderer/manifest/manifest_parser.cc b/content/renderer/manifest/manifest_parser.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b1d206d8c0996fc5e5afe313ee6db3e97125651e
--- /dev/null
+++ b/content/renderer/manifest/manifest_parser.cc
@@ -0,0 +1,75 @@
+// Copyright 2014 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 "content/renderer/manifest/manifest_parser.h"
+
+#include "base/json/json_reader.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "content/public/common/manifest.h"
+
+namespace content {
+
+Manifest ManifestParser::Parse(
+ const base::StringPiece& json) {
+ base::Value* value = base::JSONReader::Read(json);
+ if (!value) {
+ // TODO(mlamouri): get the JSON parsing error and report it to the developer
+ // console.
+ return Manifest();
+ }
+
+ if (value->GetType() != base::Value::TYPE_DICTIONARY) {
+ // TODO(mlamouri): provide a custom message to the developer console.
+ return Manifest();
+ }
+
+
+ base::DictionaryValue* dictionary = 0;
+ value->GetAsDictionary(&dictionary);
+ if (!dictionary) {
+ // TODO(mlamouri): provide a custom message to the developer console.
+ return Manifest();
+ }
+
+ Manifest manifest;
+
+ manifest.name = ParseName(*dictionary);
+ manifest.short_name = ParseShortName(*dictionary);
+
+ return manifest;
+}
+
+base::NullableString16 ManifestParser::ParseName(
+ const base::DictionaryValue& dictionary) {
+ if (!dictionary.HasKey("name"))
+ return base::NullableString16();
+
+ base::string16 name;
+ if (!dictionary.GetString("name", &name)) {
+ // TODO(mlamouri): provide a custom message to the developer console.
+ return base::NullableString16();
+ }
+
+ base::TrimWhitespace(name, base::TRIM_ALL, &name);
+ return base::NullableString16(name, false);
jochen (gone - plz use gerrit) 2014/09/12 11:55:18 enforce a max length here?
mlamouri (slow - plz ping) 2014/09/12 12:05:19 I would prefer to avoid that. Maybe we could trunc
jochen (gone - plz use gerrit) 2014/09/12 12:09:40 well, it's not really a parser, but also a builder
mlamouri (slow - plz ping) 2014/09/12 12:16:47 True. However, the string truncation would be a se
jochen (gone - plz use gerrit) 2014/09/12 12:23:09 is there a consumer?
mlamouri (slow - plz ping) 2014/09/12 12:32:32 I have implemented a GetManifest() method in the r
+}
+
+base::NullableString16 ManifestParser::ParseShortName(
+ const base::DictionaryValue& dictionary) {
+ if (!dictionary.HasKey("short_name"))
+ return base::NullableString16();
+
+ base::string16 short_name;
+ if (!dictionary.GetString("short_name", &short_name)) {
+ // TODO(mlamouri): provide a custom message to the developer console.
+ return base::NullableString16();
+ }
+
+ base::TrimWhitespace(short_name, base::TRIM_ALL, &short_name);
+ return base::NullableString16(short_name, false);
jochen (gone - plz use gerrit) 2014/09/12 11:55:18 and here?
mlamouri (slow - plz ping) 2014/09/12 12:05:19 ditto.
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698