Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/manifest/manifest_parser.h" | 5 #include "content/renderer/manifest/manifest_parser.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/strings/nullable_string16.h" | 8 #include "base/strings/nullable_string16.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "content/public/common/manifest.h" | 12 #include "content/public/common/manifest.h" |
| 13 | 13 |
| 14 namespace content { | |
| 15 | |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 18 enum TrimType { | |
| 19 Trim, | |
| 20 NoTrim | |
| 21 }; | |
| 22 | |
| 23 base::NullableString16 ParseString(const base::DictionaryValue& dictionary, | |
| 24 const std::string& key, | |
| 25 TrimType trim) { | |
| 26 if (!dictionary.HasKey(key)) | |
| 27 return base::NullableString16(); | |
| 28 | |
| 29 base::string16 value; | |
| 30 if (!dictionary.GetString(key, &value)) { | |
| 31 // TODO(mlamouri): provide a custom message to the developer console about | |
| 32 // the property being incorrectly set. | |
| 33 return base::NullableString16(); | |
| 34 } | |
| 35 | |
| 36 if (trim == Trim) { | |
| 37 base::TrimWhitespace(value, base::TRIM_ALL, &value); | |
| 38 } | |
|
Avi (use Gerrit)
2014/09/18 02:00:02
No {} here; you don't use it above for a one-line
| |
| 39 return base::NullableString16(value, false); | |
| 40 } | |
| 41 | |
| 16 // Parses the 'name' field of the manifest, as defined in: | 42 // Parses the 'name' field of the manifest, as defined in: |
| 17 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member | 43 // 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. | 44 // Returns the parsed string if any, a null string if the parsing failed. |
| 19 base::NullableString16 ParseName(const base::DictionaryValue& dictionary) { | 45 base::NullableString16 ParseName(const base::DictionaryValue& dictionary) { |
| 20 if (!dictionary.HasKey("name")) | 46 return ParseString(dictionary, "name", Trim); |
| 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 } | 47 } |
| 32 | 48 |
| 33 // Parses the 'short_name' field of the manifest, as defined in: | 49 // 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 | 50 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member |
| 35 // Returns the parsed string if any, a null string if the parsing failed. | 51 // Returns the parsed string if any, a null string if the parsing failed. |
| 36 base::NullableString16 ParseShortName( | 52 base::NullableString16 ParseShortName( |
| 37 const base::DictionaryValue& dictionary) { | 53 const base::DictionaryValue& dictionary) { |
| 38 if (!dictionary.HasKey("short_name")) | 54 return ParseString(dictionary, "short_name", Trim); |
| 39 return base::NullableString16(); | |
| 40 | |
| 41 base::string16 short_name; | |
| 42 if (!dictionary.GetString("short_name", &short_name)) { | |
| 43 // TODO(mlamouri): provide a custom message to the developer console. | |
| 44 return base::NullableString16(); | |
| 45 } | |
| 46 | |
| 47 base::TrimWhitespace(short_name, base::TRIM_ALL, &short_name); | |
| 48 return base::NullableString16(short_name, false); | |
| 49 } | 55 } |
| 50 | 56 |
| 51 // Parses the 'start_url' field of the manifest, as defined in: | 57 // Parses the 'start_url' field of the manifest, as defined in: |
| 52 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member | 58 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member |
| 53 // Returns the parsed GURL if any, an empty GURL if the parsing failed. | 59 // Returns the parsed GURL if any, an empty GURL if the parsing failed. |
| 54 GURL ParseStartURL(const base::DictionaryValue& dictionary, | 60 GURL ParseStartURL(const base::DictionaryValue& dictionary, |
| 55 const GURL& manifest_url, | 61 const GURL& manifest_url, |
| 56 const GURL& document_url) { | 62 const GURL& document_url) { |
| 57 if (!dictionary.HasKey("start_url")) | 63 base::NullableString16 start_url_str = |
| 64 ParseString(dictionary, "start_url", NoTrim); | |
| 65 | |
| 66 if (start_url_str.is_null()) | |
| 58 return GURL(); | 67 return GURL(); |
| 59 | 68 |
| 60 base::string16 start_url_str; | 69 GURL start_url = manifest_url.Resolve(start_url_str.string()); |
| 61 if (!dictionary.GetString("start_url", &start_url_str)) { | |
| 62 // TODO(mlamouri): provide a custom message to the developer console. | |
| 63 return GURL(); | |
| 64 } | |
| 65 | |
| 66 GURL start_url = manifest_url.Resolve(start_url_str); | |
| 67 if (!start_url.is_valid()) | 70 if (!start_url.is_valid()) |
| 68 return GURL(); | 71 return GURL(); |
| 69 | 72 |
| 70 if (start_url.GetOrigin() != document_url.GetOrigin()) { | 73 if (start_url.GetOrigin() != document_url.GetOrigin()) { |
| 71 // TODO(mlamouri): provide a custom message to the developer console. | 74 // TODO(mlamouri): provide a custom message to the developer console. |
| 72 return GURL(); | 75 return GURL(); |
| 73 } | 76 } |
| 74 | 77 |
| 75 return start_url; | 78 return start_url; |
| 76 } | 79 } |
| 77 | 80 |
| 81 // Parses the 'display' field of the manifest, as defined in: | |
| 82 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-display-member | |
| 83 // Returns the parsed DisplayMode if any, DISPLAY_MODE_UNSPECIFIED if the | |
| 84 // parsing failed. | |
| 85 Manifest::DisplayMode ParseDisplay(const base::DictionaryValue& dictionary) { | |
| 86 base::NullableString16 display = ParseString(dictionary, "display", Trim); | |
| 87 | |
| 88 if (display.is_null()) | |
| 89 return Manifest::DISPLAY_MODE_UNSPECIFIED; | |
| 90 | |
| 91 if (LowerCaseEqualsASCII(display.string(), "fullscreen")) | |
| 92 return Manifest::DISPLAY_MODE_FULLSCREEN; | |
| 93 else if (LowerCaseEqualsASCII(display.string(), "standalone")) | |
| 94 return Manifest::DISPLAY_MODE_STANDALONE; | |
| 95 else if (LowerCaseEqualsASCII(display.string(), "minimal-ui")) | |
| 96 return Manifest::DISPLAY_MODE_MINIMAL_UI; | |
| 97 else if (LowerCaseEqualsASCII(display.string(), "browser")) | |
| 98 return Manifest::DISPLAY_MODE_BROWSER; | |
| 99 else | |
| 100 return Manifest::DISPLAY_MODE_UNSPECIFIED; | |
| 101 } | |
| 102 | |
| 78 } // anonymous namespace | 103 } // anonymous namespace |
| 79 | 104 |
| 80 namespace content { | |
| 81 | |
| 82 Manifest ManifestParser::Parse(const base::StringPiece& json, | 105 Manifest ManifestParser::Parse(const base::StringPiece& json, |
| 83 const GURL& manifest_url, | 106 const GURL& manifest_url, |
| 84 const GURL& document_url) { | 107 const GURL& document_url) { |
| 85 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); | 108 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); |
| 86 if (!value) { | 109 if (!value) { |
| 87 // TODO(mlamouri): get the JSON parsing error and report it to the developer | 110 // TODO(mlamouri): get the JSON parsing error and report it to the developer |
| 88 // console. | 111 // console. |
| 89 return Manifest(); | 112 return Manifest(); |
| 90 } | 113 } |
| 91 | 114 |
| 92 if (value->GetType() != base::Value::TYPE_DICTIONARY) { | 115 if (value->GetType() != base::Value::TYPE_DICTIONARY) { |
| 93 // TODO(mlamouri): provide a custom message to the developer console. | 116 // TODO(mlamouri): provide a custom message to the developer console. |
| 94 return Manifest(); | 117 return Manifest(); |
| 95 } | 118 } |
| 96 | 119 |
| 97 base::DictionaryValue* dictionary = 0; | 120 base::DictionaryValue* dictionary = 0; |
| 98 value->GetAsDictionary(&dictionary); | 121 value->GetAsDictionary(&dictionary); |
| 99 if (!dictionary) { | 122 if (!dictionary) { |
| 100 // TODO(mlamouri): provide a custom message to the developer console. | 123 // TODO(mlamouri): provide a custom message to the developer console. |
| 101 return Manifest(); | 124 return Manifest(); |
| 102 } | 125 } |
| 103 | 126 |
| 104 Manifest manifest; | 127 Manifest manifest; |
| 105 | 128 |
| 106 manifest.name = ParseName(*dictionary); | 129 manifest.name = ParseName(*dictionary); |
| 107 manifest.short_name = ParseShortName(*dictionary); | 130 manifest.short_name = ParseShortName(*dictionary); |
| 108 manifest.start_url = ParseStartURL(*dictionary, manifest_url, document_url); | 131 manifest.start_url = ParseStartURL(*dictionary, manifest_url, document_url); |
| 132 manifest.display = ParseDisplay(*dictionary); | |
| 109 | 133 |
| 110 return manifest; | 134 return manifest; |
| 111 } | 135 } |
| 112 | 136 |
| 113 } // namespace content | 137 } // namespace content |
| OLD | NEW |