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/nullable_string16.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" |
| 12 #include "content/public/common/manifest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Parses the 'name' field of the manifest, as defined in: |
| 17 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member |
| 18 // Returns the parsed string if any, a null string if the parsing failed. |
| 19 base::NullableString16 ParseName(const base::DictionaryValue& dictionary) { |
| 20 if (!dictionary.HasKey("name")) |
| 21 return base::NullableString16(); |
| 22 |
| 23 base::string16 name; |
| 24 if (!dictionary.GetString("name", &name)) { |
| 25 // TODO(mlamouri): provide a custom message to the developer console. |
| 26 return base::NullableString16(); |
| 27 } |
| 28 |
| 29 base::TrimWhitespace(name, base::TRIM_ALL, &name); |
| 30 return base::NullableString16(name, false); |
| 31 } |
| 32 |
| 33 // Parses the 'short_name' field of the manifest, as defined in: |
| 34 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member |
| 35 // |short_name| is an out parameter that must not be null. |
| 36 // Returns the parsed string if any, a null string if the parsing failed. |
| 37 base::NullableString16 ParseShortName( |
| 38 const base::DictionaryValue& dictionary) { |
| 39 if (!dictionary.HasKey("short_name")) |
| 40 return base::NullableString16(); |
| 41 |
| 42 base::string16 short_name; |
| 43 if (!dictionary.GetString("short_name", &short_name)) { |
| 44 // TODO(mlamouri): provide a custom message to the developer console. |
| 45 return base::NullableString16(); |
| 46 } |
| 47 |
| 48 base::TrimWhitespace(short_name, base::TRIM_ALL, &short_name); |
| 49 return base::NullableString16(short_name, false); |
| 50 } |
| 51 |
| 52 } // anonymous namespace |
| 53 |
| 54 namespace content { |
| 55 |
| 56 Manifest ManifestParser::Parse(const base::StringPiece& json) { |
| 57 base::Value* value = base::JSONReader::Read(json); |
| 58 if (!value) { |
| 59 // TODO(mlamouri): get the JSON parsing error and report it to the developer |
| 60 // console. |
| 61 return Manifest(); |
| 62 } |
| 63 |
| 64 if (value->GetType() != base::Value::TYPE_DICTIONARY) { |
| 65 // TODO(mlamouri): provide a custom message to the developer console. |
| 66 return Manifest(); |
| 67 } |
| 68 |
| 69 base::DictionaryValue* dictionary = 0; |
| 70 value->GetAsDictionary(&dictionary); |
| 71 if (!dictionary) { |
| 72 // TODO(mlamouri): provide a custom message to the developer console. |
| 73 return Manifest(); |
| 74 } |
| 75 |
| 76 Manifest manifest; |
| 77 |
| 78 manifest.name = ParseName(*dictionary); |
| 79 manifest.short_name = ParseShortName(*dictionary); |
| 80 |
| 81 return manifest; |
| 82 } |
| 83 |
| 84 } // namespace content |
OLD | NEW |