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 #ifndef CONTENT_RENDERER_MANIFEST_MANIFEST_PARSER_H_ | |
6 #define CONTENT_RENDERER_MANIFEST_MANIFEST_PARSER_H_ | |
7 | |
8 #include "base/strings/nullable_string16.h" | |
9 #include "base/strings/string_piece.h" | |
10 #include "content/common/content_export.h" | |
11 #include "content/public/common/manifest.h" | |
12 | |
13 class GURL; | |
14 | |
15 namespace base { | |
16 class DictionaryValue; | |
17 } | |
18 | |
19 namespace content { | |
20 | |
21 // ManifestParser handles the logic of parsing the Web Manifest from a string. | |
22 // It implements: | |
23 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-manifest | |
24 class CONTENT_EXPORT ManifestParser { | |
25 public: | |
26 ManifestParser(const base::StringPiece& data, | |
27 const GURL& manifest_url, | |
28 const GURL& document_url); | |
29 ~ManifestParser(); | |
30 | |
31 // Parse the Manifest from a string using following: | |
32 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-manifest | |
33 void Parse(); | |
34 | |
35 const Manifest& manifest() const; | |
36 const std::vector<std::string>& errors() const; | |
37 bool failed() const; | |
38 | |
39 private: | |
40 // Used to indicate whether to strip whitespace when parsing a string. | |
41 enum TrimType { | |
42 Trim, | |
43 NoTrim | |
44 }; | |
45 | |
46 // Helper function to parse booleans present on a given |dictionary| in a | |
47 // given field identified by its |key|. | |
48 // Returns the parsed boolean if any, or |default_value| if parsing failed. | |
49 bool ParseBoolean(const base::DictionaryValue& dictionary, | |
50 const std::string& key, | |
51 bool default_value); | |
52 | |
53 // Helper function to parse strings present on a given |dictionary| in a given | |
54 // field identified by its |key|. | |
55 // Returns the parsed string if any, a null string if the parsing failed. | |
56 base::NullableString16 ParseString(const base::DictionaryValue& dictionary, | |
57 const std::string& key, | |
58 TrimType trim); | |
59 | |
60 // Helper function to parse URLs present on a given |dictionary| in a given | |
61 // field identified by its |key|. The URL is first parsed as a string then | |
62 // resolved using |base_url|. | |
63 // Returns a GURL. If the parsing failed, the GURL will not be valid. | |
64 GURL ParseURL(const base::DictionaryValue& dictionary, | |
65 const std::string& key, | |
66 const GURL& base_url); | |
67 | |
68 // Parses the 'name' field of the manifest, as defined in: | |
69 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member | |
70 // Returns the parsed string if any, a null string if the parsing failed. | |
71 base::NullableString16 ParseName(const base::DictionaryValue& dictionary); | |
72 | |
73 // Parses the 'short_name' field of the manifest, as defined in: | |
74 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-memb
er | |
75 // Returns the parsed string if any, a null string if the parsing failed. | |
76 base::NullableString16 ParseShortName( | |
77 const base::DictionaryValue& dictionary); | |
78 | |
79 // Parses the 'start_url' field of the manifest, as defined in: | |
80 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-membe
r | |
81 // Returns the parsed GURL if any, an empty GURL if the parsing failed. | |
82 GURL ParseStartURL(const base::DictionaryValue& dictionary); | |
83 | |
84 // Parses the 'display' field of the manifest, as defined in: | |
85 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-display-member | |
86 // Returns the parsed DisplayMode if any, DISPLAY_MODE_UNSPECIFIED if the | |
87 // parsing failed. | |
88 Manifest::DisplayMode ParseDisplay(const base::DictionaryValue& dictionary); | |
89 | |
90 // Parses the 'orientation' field of the manifest, as defined in: | |
91 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-orientation-mem
ber | |
92 // Returns the parsed WebScreenOrientationLockType if any, | |
93 // WebScreenOrientationLockDefault if the parsing failed. | |
94 blink::WebScreenOrientationLockType ParseOrientation( | |
95 const base::DictionaryValue& dictionary); | |
96 | |
97 // Parses the 'src' field of an icon, as defined in: | |
98 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-src-member-of-a
n-icon | |
99 // Returns the parsed GURL if any, an empty GURL if the parsing failed. | |
100 GURL ParseIconSrc(const base::DictionaryValue& icon); | |
101 | |
102 // Parses the 'type' field of an icon, as defined in: | |
103 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-type-member-of-
an-icon | |
104 // Returns the parsed string if any, a null string if the parsing failed. | |
105 base::NullableString16 ParseIconType(const base::DictionaryValue& icon); | |
106 | |
107 // Parses the 'density' field of an icon, as defined in: | |
108 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-density-member-of
-an-icon | |
109 // Returns the parsed double if any, Manifest::Icon::kDefaultDensity if the | |
110 // parsing failed. | |
111 double ParseIconDensity(const base::DictionaryValue& icon); | |
112 | |
113 // Parses the 'sizes' field of an icon, as defined in: | |
114 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-sizes-member-of-a
n-icon | |
115 // Returns a vector of gfx::Size with the successfully parsed sizes, if any. | |
116 // An empty vector if the field was not present or empty. "Any" is represented | |
117 // by gfx::Size(0, 0). | |
118 std::vector<gfx::Size> ParseIconSizes(const base::DictionaryValue& icon); | |
119 | |
120 // Parses the 'icons' field of a Manifest, as defined in: | |
121 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-icons-member | |
122 // Returns a vector of Manifest::Icon with the successfully parsed icons, if | |
123 // any. An empty vector if the field was not present or empty. | |
124 std::vector<Manifest::Icon> ParseIcons( | |
125 const base::DictionaryValue& dictionary); | |
126 | |
127 // Parses the 'gcm_sender_id' field of the manifest. | |
128 // This is a proprietary extension of the Web Manifest specification. | |
129 // Returns the parsed string if any, a null string if the parsing failed. | |
130 base::NullableString16 ParseGCMSenderID( | |
131 const base::DictionaryValue& dictionary); | |
132 | |
133 // Parses the 'gcm_user_visible_only' field of the manifest. | |
134 // This is a proprietary extension of the Web Manifest specification. | |
135 // Returns true iff the string could be parsed as the boolean true. | |
136 bool ParseGCMUserVisibleOnly(const base::DictionaryValue& dictionary); | |
137 | |
138 const base::StringPiece& data_; | |
139 GURL manifest_url_; | |
140 GURL document_url_; | |
141 | |
142 bool failed_; | |
143 Manifest manifest_; | |
144 std::vector<std::string> errors_; | |
145 | |
146 DISALLOW_COPY_AND_ASSIGN(ManifestParser); | |
147 }; | |
148 | |
149 } // namespace content | |
150 | |
151 #endif // CONTENT_RENDERER_MANIFEST_MANIFEST_PARSER_H_ | |
OLD | NEW |