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" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 // TODO(mlamouri): provide a custom message to the developer console about | 31 // TODO(mlamouri): provide a custom message to the developer console about |
32 // the property being incorrectly set. | 32 // the property being incorrectly set. |
33 return base::NullableString16(); | 33 return base::NullableString16(); |
34 } | 34 } |
35 | 35 |
36 if (trim == Trim) | 36 if (trim == Trim) |
37 base::TrimWhitespace(value, base::TRIM_ALL, &value); | 37 base::TrimWhitespace(value, base::TRIM_ALL, &value); |
38 return base::NullableString16(value, false); | 38 return base::NullableString16(value, false); |
39 } | 39 } |
40 | 40 |
41 // Helper function to parse URLs present on a given |dictionary| in a given | |
42 // field identified by its |key|. The URL is first parsed as a string then | |
43 // resolved using |base_url|. | |
44 // Returns a GURL. If the parsing failed, the GURL will not be valid. | |
45 GURL ParseURL(const base::DictionaryValue& dictionary, | |
46 const std::string& key, | |
47 const GURL& base_url) { | |
48 base::NullableString16 url_str = ParseString(dictionary, key, NoTrim); | |
49 if (url_str.is_null()) | |
50 return GURL(); | |
51 | |
52 return base_url.Resolve(url_str.string()); | |
53 } | |
54 | |
41 // Parses the 'name' field of the manifest, as defined in: | 55 // Parses the 'name' field of the manifest, as defined in: |
42 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member | 56 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member |
43 // Returns the parsed string if any, a null string if the parsing failed. | 57 // Returns the parsed string if any, a null string if the parsing failed. |
44 base::NullableString16 ParseName(const base::DictionaryValue& dictionary) { | 58 base::NullableString16 ParseName(const base::DictionaryValue& dictionary) { |
45 return ParseString(dictionary, "name", Trim); | 59 return ParseString(dictionary, "name", Trim); |
46 } | 60 } |
47 | 61 |
48 // Parses the 'short_name' field of the manifest, as defined in: | 62 // Parses the 'short_name' field of the manifest, as defined in: |
49 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member | 63 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member |
50 // Returns the parsed string if any, a null string if the parsing failed. | 64 // Returns the parsed string if any, a null string if the parsing failed. |
51 base::NullableString16 ParseShortName( | 65 base::NullableString16 ParseShortName( |
52 const base::DictionaryValue& dictionary) { | 66 const base::DictionaryValue& dictionary) { |
53 return ParseString(dictionary, "short_name", Trim); | 67 return ParseString(dictionary, "short_name", Trim); |
54 } | 68 } |
55 | 69 |
56 // Parses the 'start_url' field of the manifest, as defined in: | 70 // Parses the 'start_url' field of the manifest, as defined in: |
57 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member | 71 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member |
58 // Returns the parsed GURL if any, an empty GURL if the parsing failed. | 72 // Returns the parsed GURL if any, an empty GURL if the parsing failed. |
59 GURL ParseStartURL(const base::DictionaryValue& dictionary, | 73 GURL ParseStartURL(const base::DictionaryValue& dictionary, |
60 const GURL& manifest_url, | 74 const GURL& manifest_url, |
61 const GURL& document_url) { | 75 const GURL& document_url) { |
62 base::NullableString16 start_url_str = | 76 GURL start_url = ParseURL(dictionary, "start_url", manifest_url); |
63 ParseString(dictionary, "start_url", NoTrim); | |
64 | |
65 if (start_url_str.is_null()) | |
66 return GURL(); | |
67 | |
68 GURL start_url = manifest_url.Resolve(start_url_str.string()); | |
69 if (!start_url.is_valid()) | 77 if (!start_url.is_valid()) |
70 return GURL(); | 78 return GURL(); |
71 | 79 |
72 if (start_url.GetOrigin() != document_url.GetOrigin()) { | 80 if (start_url.GetOrigin() != document_url.GetOrigin()) { |
73 // TODO(mlamouri): provide a custom message to the developer console. | 81 // TODO(mlamouri): provide a custom message to the developer console. |
74 return GURL(); | 82 return GURL(); |
75 } | 83 } |
76 | 84 |
77 return start_url; | 85 return start_url; |
78 } | 86 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 else if (LowerCaseEqualsASCII(orientation.string(), "portrait")) | 132 else if (LowerCaseEqualsASCII(orientation.string(), "portrait")) |
125 return blink::WebScreenOrientationLockPortrait; | 133 return blink::WebScreenOrientationLockPortrait; |
126 else if (LowerCaseEqualsASCII(orientation.string(), "portrait-primary")) | 134 else if (LowerCaseEqualsASCII(orientation.string(), "portrait-primary")) |
127 return blink::WebScreenOrientationLockPortraitPrimary; | 135 return blink::WebScreenOrientationLockPortraitPrimary; |
128 else if (LowerCaseEqualsASCII(orientation.string(), "portrait-secondary")) | 136 else if (LowerCaseEqualsASCII(orientation.string(), "portrait-secondary")) |
129 return blink::WebScreenOrientationLockPortraitSecondary; | 137 return blink::WebScreenOrientationLockPortraitSecondary; |
130 else | 138 else |
131 return blink::WebScreenOrientationLockDefault; | 139 return blink::WebScreenOrientationLockDefault; |
132 } | 140 } |
133 | 141 |
142 // Parses the 'src' field of an icon, as defined in: | |
143 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-src-member-of-an- icon | |
144 // Returns the parsed GURL if any, an empty GURL if the parsing failed. | |
145 GURL ParseIconSrc(const base::DictionaryValue& icon, | |
146 const GURL& manifest_url) { | |
147 return ParseURL(icon, "src", manifest_url); | |
148 } | |
149 | |
150 // Parses the 'type' field of an icon, as defined in: | |
151 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-type-member-of-an -icon | |
152 // Returns the parsed string if any, a null string if the parsing failed. | |
153 base::NullableString16 ParseIconType(const base::DictionaryValue& icon) { | |
154 return ParseString(icon, "type", Trim); | |
155 } | |
156 | |
157 // Parses the 'density' field of an icon, as defined in: | |
158 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-density-member-of-a n-icon | |
159 // Returns the parsed double if any, Manifest::Icon::kDefaultDensity if the | |
160 // parsing failed. | |
161 double ParseIconDensity(const base::DictionaryValue& icon) { | |
162 double density; | |
163 if (!icon.GetDouble("density", &density) || density <= 0) | |
164 return Manifest::Icon::kDefaultDensity; | |
165 return density; | |
166 } | |
167 | |
168 std::vector<Manifest::Icon> ParseIcons(const base::DictionaryValue& dictionary, | |
169 const GURL& manifest_url) { | |
palmer
2014/09/22 20:35:43
Nit: indentation
mlamouri (slow - plz ping)
2014/09/22 21:09:31
Done.
| |
170 std::vector<Manifest::Icon> icons; | |
171 if (!dictionary.HasKey("icons")) | |
172 return icons; | |
173 | |
174 const base::ListValue* icons_list = 0; | |
175 if (!dictionary.GetList("icons", &icons_list)) { | |
176 // TODO(mlamouri): provide a custom message to the developer console about | |
177 // the property being incorrectly set. | |
178 return icons; | |
179 } | |
180 | |
181 for (size_t i = 0; i < icons_list->GetSize(); ++i) { | |
182 const base::DictionaryValue* icon_dictionary = 0; | |
183 if (!icons_list->GetDictionary(i, &icon_dictionary)) | |
184 continue; | |
185 | |
186 Manifest::Icon icon; | |
187 icon.src = ParseIconSrc(*icon_dictionary, manifest_url); | |
188 // An icon MUST have a valid src. If it does not, it MUST be ignored. | |
189 if (!icon.src.is_valid()) | |
190 continue; | |
191 icon.type = ParseIconType(*icon_dictionary); | |
192 icon.density = ParseIconDensity(*icon_dictionary); | |
193 // TODO(mlamouri): icon.sizes | |
194 | |
195 icons.push_back(icon); | |
196 } | |
197 | |
198 return icons; | |
199 } | |
200 | |
134 } // anonymous namespace | 201 } // anonymous namespace |
135 | 202 |
136 Manifest ManifestParser::Parse(const base::StringPiece& json, | 203 Manifest ManifestParser::Parse(const base::StringPiece& json, |
137 const GURL& manifest_url, | 204 const GURL& manifest_url, |
138 const GURL& document_url) { | 205 const GURL& document_url) { |
139 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); | 206 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); |
140 if (!value) { | 207 if (!value) { |
141 // TODO(mlamouri): get the JSON parsing error and report it to the developer | 208 // TODO(mlamouri): get the JSON parsing error and report it to the developer |
142 // console. | 209 // console. |
143 return Manifest(); | 210 return Manifest(); |
(...skipping 11 matching lines...) Expand all Loading... | |
155 return Manifest(); | 222 return Manifest(); |
156 } | 223 } |
157 | 224 |
158 Manifest manifest; | 225 Manifest manifest; |
159 | 226 |
160 manifest.name = ParseName(*dictionary); | 227 manifest.name = ParseName(*dictionary); |
161 manifest.short_name = ParseShortName(*dictionary); | 228 manifest.short_name = ParseShortName(*dictionary); |
162 manifest.start_url = ParseStartURL(*dictionary, manifest_url, document_url); | 229 manifest.start_url = ParseStartURL(*dictionary, manifest_url, document_url); |
163 manifest.display = ParseDisplay(*dictionary); | 230 manifest.display = ParseDisplay(*dictionary); |
164 manifest.orientation = ParseOrientation(*dictionary); | 231 manifest.orientation = ParseOrientation(*dictionary); |
232 manifest.icons = ParseIcons(*dictionary, manifest_url); | |
165 | 233 |
166 return manifest; | 234 return manifest; |
167 } | 235 } |
168 | 236 |
169 } // namespace content | 237 } // namespace content |
OLD | NEW |