Add ICU message format support
[chromium-blink-merge.git] / content / renderer / manifest / manifest_parser.h
blobc8342474a868b9cc5f4ce96cbebb4568c5100c2b
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.
5 #ifndef CONTENT_RENDERER_MANIFEST_MANIFEST_PARSER_H_
6 #define CONTENT_RENDERER_MANIFEST_MANIFEST_PARSER_H_
8 #include <stdint.h>
10 #include "base/strings/nullable_string16.h"
11 #include "base/strings/string_piece.h"
12 #include "content/common/content_export.h"
13 #include "content/public/common/manifest.h"
15 class GURL;
17 namespace base {
18 class DictionaryValue;
21 namespace content {
23 // ManifestParser handles the logic of parsing the Web Manifest from a string.
24 // It implements:
25 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-manifest
26 class CONTENT_EXPORT ManifestParser {
27 public:
28 ManifestParser(const base::StringPiece& data,
29 const GURL& manifest_url,
30 const GURL& document_url);
31 ~ManifestParser();
33 // Parse the Manifest from a string using following:
34 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-manifest
35 void Parse();
37 const Manifest& manifest() const;
38 const std::vector<std::string>& errors() const;
39 bool failed() const;
41 private:
42 // Used to indicate whether to strip whitespace when parsing a string.
43 enum TrimType {
44 Trim,
45 NoTrim
48 // Helper function to parse booleans present on a given |dictionary| in a
49 // given field identified by its |key|.
50 // Returns the parsed boolean if any, or |default_value| if parsing failed.
51 bool ParseBoolean(const base::DictionaryValue& dictionary,
52 const std::string& key,
53 bool default_value);
55 // Helper function to parse strings present on a given |dictionary| in a given
56 // field identified by its |key|.
57 // Returns the parsed string if any, a null string if the parsing failed.
58 base::NullableString16 ParseString(const base::DictionaryValue& dictionary,
59 const std::string& key,
60 TrimType trim);
62 // Helper function to parse URLs present on a given |dictionary| in a given
63 // field identified by its |key|. The URL is first parsed as a string then
64 // resolved using |base_url|.
65 // Returns a GURL. If the parsing failed, the GURL will not be valid.
66 GURL ParseURL(const base::DictionaryValue& dictionary,
67 const std::string& key,
68 const GURL& base_url);
70 // Parses the 'name' field of the manifest, as defined in:
71 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-name-member
72 // Returns the parsed string if any, a null string if the parsing failed.
73 base::NullableString16 ParseName(const base::DictionaryValue& dictionary);
75 // Parses the 'short_name' field of the manifest, as defined in:
76 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-short-name-member
77 // Returns the parsed string if any, a null string if the parsing failed.
78 base::NullableString16 ParseShortName(
79 const base::DictionaryValue& dictionary);
81 // Parses the 'start_url' field of the manifest, as defined in:
82 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-start_url-member
83 // Returns the parsed GURL if any, an empty GURL if the parsing failed.
84 GURL ParseStartURL(const base::DictionaryValue& dictionary);
86 // Parses the 'display' field of the manifest, as defined in:
87 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-display-member
88 // Returns the parsed DisplayMode if any, DISPLAY_MODE_UNSPECIFIED if the
89 // parsing failed.
90 Manifest::DisplayMode ParseDisplay(const base::DictionaryValue& dictionary);
92 // Parses the 'orientation' field of the manifest, as defined in:
93 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-orientation-member
94 // Returns the parsed WebScreenOrientationLockType if any,
95 // WebScreenOrientationLockDefault if the parsing failed.
96 blink::WebScreenOrientationLockType ParseOrientation(
97 const base::DictionaryValue& dictionary);
99 // Parses the 'src' field of an icon, as defined in:
100 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-src-member-of-an-icon
101 // Returns the parsed GURL if any, an empty GURL if the parsing failed.
102 GURL ParseIconSrc(const base::DictionaryValue& icon);
104 // Parses the 'type' field of an icon, as defined in:
105 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-type-member-of-an-icon
106 // Returns the parsed string if any, a null string if the parsing failed.
107 base::NullableString16 ParseIconType(const base::DictionaryValue& icon);
109 // Parses the 'density' field of an icon, as defined in:
110 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-density-member-of-an-icon
111 // Returns the parsed double if any, Manifest::Icon::kDefaultDensity if the
112 // parsing failed.
113 double ParseIconDensity(const base::DictionaryValue& icon);
115 // Parses the 'sizes' field of an icon, as defined in:
116 // http://w3c.github.io/manifest/#dfn-steps-for-processing-a-sizes-member-of-an-icon
117 // Returns a vector of gfx::Size with the successfully parsed sizes, if any.
118 // An empty vector if the field was not present or empty. "Any" is represented
119 // by gfx::Size(0, 0).
120 std::vector<gfx::Size> ParseIconSizes(const base::DictionaryValue& icon);
122 // Parses the 'icons' field of a Manifest, as defined in:
123 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-icons-member
124 // Returns a vector of Manifest::Icon with the successfully parsed icons, if
125 // any. An empty vector if the field was not present or empty.
126 std::vector<Manifest::Icon> ParseIcons(
127 const base::DictionaryValue& dictionary);
129 // Parses the 'platform' field of a related application, as defined in:
130 // https://w3c.github.io/manifest/#dfn-steps-for-processing-the-platform-member-of-an-application
131 // Returns the parsed string if any, a null string if the parsing failed.
132 base::NullableString16 ParseRelatedApplicationPlatform(
133 const base::DictionaryValue& application);
135 // Parses the 'url' field of a related application, as defined in:
136 // https://w3c.github.io/manifest/#dfn-steps-for-processing-the-url-member-of-an-application
137 // Returns the paresed GURL if any, an empty GURL if the parsing failed.
138 GURL ParseRelatedApplicationURL(const base::DictionaryValue& application);
140 // Parses the 'id' field of a related application, as defined in:
141 // https://w3c.github.io/manifest/#dfn-steps-for-processing-the-id-member-of-an-application
142 // Returns the parsed string if any, a null string if the parsing failed.
143 base::NullableString16 ParseRelatedApplicationId(
144 const base::DictionaryValue& application);
146 // Parses the 'related_applications' field of the manifest, as defined in:
147 // https://w3c.github.io/manifest/#dfn-steps-for-processing-the-related_applications-member
148 // Returns a vector of Manifest::RelatedApplication with the successfully
149 // parsed applications, if any. An empty vector if the field was not present
150 // or empty.
151 std::vector<Manifest::RelatedApplication> ParseRelatedApplications(
152 const base::DictionaryValue& dictionary);
154 // Parses the 'prefer_related_applications' field on the manifest, as defined
155 // in:
156 // https://w3c.github.io/manifest/#dfn-steps-for-processing-the-prefer_related_applications-member
157 // returns true iff the field could be parsed as the boolean true.
158 bool ParsePreferRelatedApplications(const base::DictionaryValue& dictionary);
160 // Parses the 'theme_color' field of the manifest, as defined in:
161 // http://w3c.github.io/manifest/#dfn-steps-for-processing-the-theme_color-member
162 // Returns the parsed theme color if any,
163 // Manifest::kInvalidOrMissingThemeColor if the parsing failed.
164 int64_t ParseThemeColor(const base::DictionaryValue& dictionary);
166 // Parses the 'gcm_sender_id' field of the manifest.
167 // This is a proprietary extension of the Web Manifest specification.
168 // Returns the parsed string if any, a null string if the parsing failed.
169 base::NullableString16 ParseGCMSenderID(
170 const base::DictionaryValue& dictionary);
172 const base::StringPiece& data_;
173 GURL manifest_url_;
174 GURL document_url_;
176 bool failed_;
177 Manifest manifest_;
178 std::vector<std::string> errors_;
180 DISALLOW_COPY_AND_ASSIGN(ManifestParser);
183 } // namespace content
185 #endif // CONTENT_RENDERER_MANIFEST_MANIFEST_PARSER_H_