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