Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / manifest / manifest_parser.cc
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_number_conversions.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "content/public/common/manifest.h"
15 #include "content/renderer/manifest/manifest_uma_util.h"
16 #include "ui/gfx/geometry/size.h"
17
18 namespace content {
19
20 namespace {
21
22 enum TrimType {
23   Trim,
24   NoTrim
25 };
26
27 base::NullableString16 ParseString(const base::DictionaryValue& dictionary,
28                                    const std::string& key,
29                                    TrimType trim) {
30   if (!dictionary.HasKey(key))
31     return base::NullableString16();
32
33   base::string16 value;
34   if (!dictionary.GetString(key, &value)) {
35     // TODO(mlamouri): provide a custom message to the developer console about
36     // the property being incorrectly set.
37     return base::NullableString16();
38   }
39
40   if (trim == Trim)
41     base::TrimWhitespace(value, base::TRIM_ALL, &value);
42   return base::NullableString16(value, false);
43 }
44
45 // Helper function to parse URLs present on a given |dictionary| in a given
46 // field identified by its |key|. The URL is first parsed as a string then
47 // resolved using |base_url|.
48 // Returns a GURL. If the parsing failed, the GURL will not be valid.
49 GURL ParseURL(const base::DictionaryValue& dictionary,
50               const std::string& key,
51               const GURL& base_url) {
52   base::NullableString16 url_str = ParseString(dictionary, key, NoTrim);
53   if (url_str.is_null())
54     return GURL();
55
56   return base_url.Resolve(url_str.string());
57 }
58
59 // Parses the 'name' field of the manifest, as defined in:
60 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member
61 // Returns the parsed string if any, a null string if the parsing failed.
62 base::NullableString16 ParseName(const base::DictionaryValue& dictionary)  {
63   return ParseString(dictionary, "name", Trim);
64 }
65
66 // Parses the 'short_name' field of the manifest, as defined in:
67 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member
68 // Returns the parsed string if any, a null string if the parsing failed.
69 base::NullableString16 ParseShortName(
70     const base::DictionaryValue& dictionary)  {
71   return ParseString(dictionary, "short_name", Trim);
72 }
73
74 // Parses the 'start_url' field of the manifest, as defined in:
75 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member
76 // Returns the parsed GURL if any, an empty GURL if the parsing failed.
77 GURL ParseStartURL(const base::DictionaryValue& dictionary,
78                    const GURL& manifest_url,
79                    const GURL& document_url) {
80   GURL start_url = ParseURL(dictionary, "start_url", manifest_url);
81   if (!start_url.is_valid())
82     return GURL();
83
84   if (start_url.GetOrigin() != document_url.GetOrigin()) {
85     // TODO(mlamouri): provide a custom message to the developer console.
86     return GURL();
87   }
88
89   return start_url;
90 }
91
92 // Parses the 'display' field of the manifest, as defined in:
93 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-display-member
94 // Returns the parsed DisplayMode if any, DISPLAY_MODE_UNSPECIFIED if the
95 // parsing failed.
96 Manifest::DisplayMode ParseDisplay(const base::DictionaryValue& dictionary) {
97   base::NullableString16 display = ParseString(dictionary, "display", Trim);
98   if (display.is_null())
99     return Manifest::DISPLAY_MODE_UNSPECIFIED;
100
101   if (LowerCaseEqualsASCII(display.string(), "fullscreen"))
102     return Manifest::DISPLAY_MODE_FULLSCREEN;
103   else if (LowerCaseEqualsASCII(display.string(), "standalone"))
104     return Manifest::DISPLAY_MODE_STANDALONE;
105   else if (LowerCaseEqualsASCII(display.string(), "minimal-ui"))
106     return Manifest::DISPLAY_MODE_MINIMAL_UI;
107   else if (LowerCaseEqualsASCII(display.string(), "browser"))
108     return Manifest::DISPLAY_MODE_BROWSER;
109   else
110     return Manifest::DISPLAY_MODE_UNSPECIFIED;
111 }
112
113 // Parses the 'orientation' field of the manifest, as defined in:
114 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-orientation-member
115 // Returns the parsed WebScreenOrientationLockType if any,
116 // WebScreenOrientationLockDefault if the parsing failed.
117 blink::WebScreenOrientationLockType ParseOrientation(
118     const base::DictionaryValue& dictionary) {
119   base::NullableString16 orientation =
120       ParseString(dictionary, "orientation", Trim);
121
122   if (orientation.is_null())
123     return blink::WebScreenOrientationLockDefault;
124
125   if (LowerCaseEqualsASCII(orientation.string(), "any"))
126     return blink::WebScreenOrientationLockAny;
127   else if (LowerCaseEqualsASCII(orientation.string(), "natural"))
128     return blink::WebScreenOrientationLockNatural;
129   else if (LowerCaseEqualsASCII(orientation.string(), "landscape"))
130     return blink::WebScreenOrientationLockLandscape;
131   else if (LowerCaseEqualsASCII(orientation.string(), "landscape-primary"))
132     return blink::WebScreenOrientationLockLandscapePrimary;
133   else if (LowerCaseEqualsASCII(orientation.string(), "landscape-secondary"))
134     return blink::WebScreenOrientationLockLandscapeSecondary;
135   else if (LowerCaseEqualsASCII(orientation.string(), "portrait"))
136     return blink::WebScreenOrientationLockPortrait;
137   else if (LowerCaseEqualsASCII(orientation.string(), "portrait-primary"))
138     return blink::WebScreenOrientationLockPortraitPrimary;
139   else if (LowerCaseEqualsASCII(orientation.string(), "portrait-secondary"))
140     return blink::WebScreenOrientationLockPortraitSecondary;
141   else
142     return blink::WebScreenOrientationLockDefault;
143 }
144
145 // Parses the 'src' field of an icon, as defined in:
146 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-src-member-of-an-icon
147 // Returns the parsed GURL if any, an empty GURL if the parsing failed.
148 GURL ParseIconSrc(const base::DictionaryValue& icon,
149                   const GURL& manifest_url) {
150   return ParseURL(icon, "src", manifest_url);
151 }
152
153 // Parses the 'type' field of an icon, as defined in:
154 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-type-member-of-an-icon
155 // Returns the parsed string if any, a null string if the parsing failed.
156 base::NullableString16 ParseIconType(const base::DictionaryValue& icon) {
157     return ParseString(icon, "type", Trim);
158 }
159
160 // Parses the 'density' field of an icon, as defined in:
161 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-density-member-of-an-icon
162 // Returns the parsed double if any, Manifest::Icon::kDefaultDensity if the
163 // parsing failed.
164 double ParseIconDensity(const base::DictionaryValue& icon) {
165   double density;
166   if (!icon.GetDouble("density", &density) || density <= 0)
167     return Manifest::Icon::kDefaultDensity;
168   return density;
169 }
170
171 // Helper function that returns whether the given |str| is a valid width or
172 // height value for an icon sizes per:
173 // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-sizes
174 bool IsValidIconWidthOrHeight(const std::string& str) {
175   if (str.empty() || str[0] == '0')
176     return false;
177   for (size_t i = 0; i < str.size(); ++i)
178     if (!IsAsciiDigit(str[i]))
179       return false;
180   return true;
181 }
182
183 // Parses the 'sizes' attribute of an icon as described in the HTML spec:
184 // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-sizes
185 // Return a vector of gfx::Size that contains the valid sizes found. "Any" is
186 // represented by gfx::Size(0, 0).
187 // TODO(mlamouri): this is implemented as a separate function because it should
188 // be refactored with the other icon sizes parsing implementations, see
189 // http://crbug.com/416477
190 std::vector<gfx::Size> ParseIconSizesHTML(const base::string16& sizes_str16) {
191   if (!base::IsStringASCII(sizes_str16))
192     return std::vector<gfx::Size>();
193
194   std::vector<gfx::Size> sizes;
195   std::string sizes_str =
196       base::StringToLowerASCII(base::UTF16ToUTF8(sizes_str16));
197   std::vector<std::string> sizes_str_list;
198   base::SplitStringAlongWhitespace(sizes_str, &sizes_str_list);
199
200   for (size_t i = 0; i < sizes_str_list.size(); ++i) {
201     std::string& size_str = sizes_str_list[i];
202     if (size_str == "any") {
203       sizes.push_back(gfx::Size(0, 0));
204       continue;
205     }
206
207     // It is expected that [0] => width and [1] => height after the split.
208     std::vector<std::string> size_list;
209     base::SplitStringDontTrim(size_str, L'x', &size_list);
210     if (size_list.size() != 2)
211       continue;
212     if (!IsValidIconWidthOrHeight(size_list[0]) ||
213         !IsValidIconWidthOrHeight(size_list[1])) {
214       continue;
215     }
216
217     int width, height;
218     if (!base::StringToInt(size_list[0], &width) ||
219         !base::StringToInt(size_list[1], &height)) {
220       continue;
221     }
222
223     sizes.push_back(gfx::Size(width, height));
224   }
225
226   return sizes;
227 }
228
229 // Parses the 'sizes' field of an icon, as defined in:
230 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-sizes-member-of-an-icon
231 // Returns a vector of gfx::Size with the successfully parsed sizes, if any. An
232 // empty vector if the field was not present or empty. "Any" is represented by
233 // gfx::Size(0, 0).
234 std::vector<gfx::Size> ParseIconSizes(const base::DictionaryValue& icon) {
235   base::NullableString16 sizes_str = ParseString(icon, "sizes", NoTrim);
236
237   return sizes_str.is_null() ? std::vector<gfx::Size>()
238                              : ParseIconSizesHTML(sizes_str.string());
239 }
240
241 // Parses the 'icons' field of a Manifest, as defined in:
242 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-icons-member
243 // Returns a vector of Manifest::Icon with the successfully parsed icons, if
244 // any. An empty vector if the field was not present or empty.
245 std::vector<Manifest::Icon> ParseIcons(const base::DictionaryValue& dictionary,
246                                        const GURL& manifest_url) {
247   std::vector<Manifest::Icon> icons;
248   if (!dictionary.HasKey("icons"))
249     return icons;
250
251   const base::ListValue* icons_list = 0;
252   if (!dictionary.GetList("icons", &icons_list)) {
253     // TODO(mlamouri): provide a custom message to the developer console about
254     // the property being incorrectly set.
255     return icons;
256   }
257
258   for (size_t i = 0; i < icons_list->GetSize(); ++i) {
259     const base::DictionaryValue* icon_dictionary = 0;
260     if (!icons_list->GetDictionary(i, &icon_dictionary))
261       continue;
262
263     Manifest::Icon icon;
264     icon.src = ParseIconSrc(*icon_dictionary, manifest_url);
265     // An icon MUST have a valid src. If it does not, it MUST be ignored.
266     if (!icon.src.is_valid())
267       continue;
268     icon.type = ParseIconType(*icon_dictionary);
269     icon.density = ParseIconDensity(*icon_dictionary);
270     icon.sizes = ParseIconSizes(*icon_dictionary);
271
272     icons.push_back(icon);
273   }
274
275   return icons;
276 }
277
278 // Parses the 'gcm_sender_id' field of the manifest.
279 // This is a proprietary extension of the Web Manifest specification.
280 // Returns the parsed string if any, a null string if the parsing failed.
281 base::NullableString16 ParseGCMSenderID(
282     const base::DictionaryValue& dictionary)  {
283   return ParseString(dictionary, "gcm_sender_id", Trim);
284 }
285
286 } // anonymous namespace
287
288 Manifest ManifestParser::Parse(const base::StringPiece& json,
289                                const GURL& manifest_url,
290                                const GURL& document_url) {
291   scoped_ptr<base::Value> value(base::JSONReader::Read(json));
292   if (!value) {
293     // TODO(mlamouri): get the JSON parsing error and report it to the developer
294     // console.
295     ManifestUmaUtil::ParseFailed();
296     return Manifest();
297   }
298
299   if (value->GetType() != base::Value::TYPE_DICTIONARY) {
300     // TODO(mlamouri): provide a custom message to the developer console.
301     ManifestUmaUtil::ParseFailed();
302     return Manifest();
303   }
304
305   base::DictionaryValue* dictionary = 0;
306   value->GetAsDictionary(&dictionary);
307   if (!dictionary) {
308     // TODO(mlamouri): provide a custom message to the developer console.
309     ManifestUmaUtil::ParseFailed();
310     return Manifest();
311   }
312
313   Manifest manifest;
314
315   manifest.name = ParseName(*dictionary);
316   manifest.short_name = ParseShortName(*dictionary);
317   manifest.start_url = ParseStartURL(*dictionary, manifest_url, document_url);
318   manifest.display = ParseDisplay(*dictionary);
319   manifest.orientation = ParseOrientation(*dictionary);
320   manifest.icons = ParseIcons(*dictionary, manifest_url);
321   manifest.gcm_sender_id = ParseGCMSenderID(*dictionary);
322
323   ManifestUmaUtil::ParseSucceeded(manifest);
324
325   return manifest;
326 }
327
328 } // namespace content