Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 "content/renderer/manifest/manifest_parser.h" | |
| 6 | |
| 7 #include "base/json/json_reader.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "content/public/common/manifest.h" | |
| 12 | |
| 13 namespace content { | |
| 14 | |
| 15 Manifest ManifestParser::Parse( | |
| 16 const base::StringPiece& json) { | |
| 17 base::Value* value = base::JSONReader::Read(json); | |
| 18 if (!value) { | |
| 19 // TODO(mlamouri): get the JSON parsing error and report it to the developer | |
| 20 // console. | |
| 21 return Manifest(); | |
| 22 } | |
| 23 | |
| 24 if (value->GetType() != base::Value::TYPE_DICTIONARY) { | |
| 25 // TODO(mlamouri): provide a custom message to the developer console. | |
| 26 return Manifest(); | |
| 27 } | |
| 28 | |
| 29 | |
| 30 base::DictionaryValue* dictionary = 0; | |
| 31 value->GetAsDictionary(&dictionary); | |
| 32 if (!dictionary) { | |
| 33 // TODO(mlamouri): provide a custom message to the developer console. | |
| 34 return Manifest(); | |
| 35 } | |
| 36 | |
| 37 Manifest manifest; | |
| 38 | |
| 39 manifest.name = ParseName(*dictionary); | |
| 40 manifest.short_name = ParseShortName(*dictionary); | |
| 41 | |
| 42 return manifest; | |
| 43 } | |
| 44 | |
| 45 base::NullableString16 ManifestParser::ParseName( | |
| 46 const base::DictionaryValue& dictionary) { | |
| 47 if (!dictionary.HasKey("name")) | |
| 48 return base::NullableString16(); | |
| 49 | |
| 50 base::string16 name; | |
| 51 if (!dictionary.GetString("name", &name)) { | |
| 52 // TODO(mlamouri): provide a custom message to the developer console. | |
| 53 return base::NullableString16(); | |
| 54 } | |
| 55 | |
| 56 base::TrimWhitespace(name, base::TRIM_ALL, &name); | |
| 57 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
| |
| 58 } | |
| 59 | |
| 60 base::NullableString16 ManifestParser::ParseShortName( | |
| 61 const base::DictionaryValue& dictionary) { | |
| 62 if (!dictionary.HasKey("short_name")) | |
| 63 return base::NullableString16(); | |
| 64 | |
| 65 base::string16 short_name; | |
| 66 if (!dictionary.GetString("short_name", &short_name)) { | |
| 67 // TODO(mlamouri): provide a custom message to the developer console. | |
| 68 return base::NullableString16(); | |
| 69 } | |
| 70 | |
| 71 base::TrimWhitespace(short_name, base::TRIM_ALL, &short_name); | |
| 72 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.
| |
| 73 } | |
| 74 | |
| 75 } // namespace content | |
| OLD | NEW |