1 // Copyright (c) 2012 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 #include "chrome/common/web_apps.h"
10 #include "base/json/json_reader.h"
11 #include "base/string16.h"
12 #include "base/string_number_conversions.h"
13 #include "base/string_split.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/common/json_schema_validator.h"
17 #include "googleurl/src/gurl.h"
18 #include "grit/common_resources.h"
19 #include "grit/generated_resources.h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeList.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
26 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/size.h"
30 #include "webkit/glue/dom_operations.h"
32 using WebKit::WebDocument
;
33 using WebKit::WebElement
;
34 using WebKit::WebFrame
;
35 using WebKit::WebNode
;
36 using WebKit::WebNodeList
;
37 using WebKit::WebString
;
41 // Sizes a single size (the width or height) from a 'sizes' attribute. A size
42 // matches must match the following regex: [1-9][0-9]*.
43 static int ParseSingleIconSize(const string16
& text
) {
44 // Size must not start with 0, and be between 0 and 9.
45 if (text
.empty() || !(text
[0] >= L
'1' && text
[0] <= L
'9'))
48 // Make sure all chars are from 0-9.
49 for (size_t i
= 1; i
< text
.length(); ++i
) {
50 if (!(text
[i
] >= L
'0' && text
[i
] <= L
'9'))
54 if (!base::StringToInt(text
, &output
))
59 void AddInstallIcon(const WebElement
& link
,
60 std::vector
<WebApplicationInfo::IconInfo
>* icons
) {
61 WebString href
= link
.getAttribute("href");
62 if (href
.isNull() || href
.isEmpty())
66 GURL url
= link
.document().completeURL(href
);
70 if (!link
.hasAttribute("sizes"))
74 std::vector
<gfx::Size
> icon_sizes
;
75 if (!web_apps::ParseIconSizes(link
.getAttribute("sizes"), &icon_sizes
,
78 icon_sizes
.size() != 1) {
81 WebApplicationInfo::IconInfo icon_info
;
82 icon_info
.width
= icon_sizes
[0].width();
83 icon_info
.height
= icon_sizes
[0].height();
85 icons
->push_back(icon_info
);
90 const char WebApplicationInfo::kInvalidDefinitionURL
[] =
91 "Invalid application definition URL. Must be a valid relative URL or "
92 "an absolute URL with the same origin as the document.";
93 const char WebApplicationInfo::kInvalidLaunchURL
[] =
94 "Invalid value for property 'launch_url'. Must be a valid relative URL or "
95 "an absolute URL with the same origin as the application definition.";
96 const char WebApplicationInfo::kInvalidURL
[] =
97 "Invalid value for property 'urls[*]'. Must be a valid relative URL or "
98 "an absolute URL with the same origin as the application definition.";
99 const char WebApplicationInfo::kInvalidIconURL
[] =
100 "Invalid value for property 'icons.*'. Must be a valid relative URL or "
101 "an absolute URL with the same origin as the application definition.";
103 WebApplicationInfo::WebApplicationInfo() {
104 is_bookmark_app
= false;
105 is_offline_enabled
= false;
108 WebApplicationInfo::~WebApplicationInfo() {
114 gfx::Size
ParseIconSize(const string16
& text
) {
115 std::vector
<string16
> sizes
;
116 base::SplitStringDontTrim(text
, L
'x', &sizes
);
117 if (sizes
.size() != 2)
120 return gfx::Size(ParseSingleIconSize(sizes
[0]),
121 ParseSingleIconSize(sizes
[1]));
124 bool ParseIconSizes(const string16
& text
,
125 std::vector
<gfx::Size
>* sizes
,
128 std::vector
<string16
> size_strings
;
129 base::SplitStringAlongWhitespace(text
, &size_strings
);
130 for (size_t i
= 0; i
< size_strings
.size(); ++i
) {
131 if (EqualsASCII(size_strings
[i
], "any")) {
134 gfx::Size size
= ParseIconSize(size_strings
[i
]);
135 if (size
.width() <= 0 || size
.height() <= 0)
136 return false; // Bogus size.
137 sizes
->push_back(size
);
140 if (*is_any
&& !sizes
->empty()) {
141 // If is_any is true, it must occur by itself.
144 return (*is_any
|| !sizes
->empty());
147 bool ParseWebAppFromWebDocument(WebFrame
* frame
,
148 WebApplicationInfo
* app_info
,
150 WebDocument document
= frame
->document();
151 if (document
.isNull())
154 WebElement head
= document
.head();
158 GURL document_url
= document
.url();
159 WebNodeList children
= head
.childNodes();
160 for (unsigned i
= 0; i
< children
.length(); ++i
) {
161 WebNode child
= children
.item(i
);
162 if (!child
.isElementNode())
164 WebElement elem
= child
.to
<WebElement
>();
166 if (elem
.hasTagName("link")) {
167 std::string rel
= elem
.getAttribute("rel").utf8();
168 // "rel" attribute may use either "icon" or "shortcut icon".
170 // <http://en.wikipedia.org/wiki/Favicon>
171 // <http://dev.w3.org/html5/spec/Overview.html#rel-icon>
172 if (LowerCaseEqualsASCII(rel
, "icon") ||
173 LowerCaseEqualsASCII(rel
, "shortcut icon")) {
174 AddInstallIcon(elem
, &app_info
->icons
);
175 } else if (LowerCaseEqualsASCII(rel
, "chrome-application-definition")) {
176 std::string
definition_url_string(elem
.getAttribute("href").utf8());
178 if (!(definition_url
=
179 document_url
.Resolve(definition_url_string
)).is_valid() ||
180 definition_url
.GetOrigin() != document_url
.GetOrigin()) {
181 *error
= UTF8ToUTF16(WebApplicationInfo::kInvalidDefinitionURL
);
185 // If there is a definition file, all attributes come from it.
186 *app_info
= WebApplicationInfo();
187 app_info
->manifest_url
= definition_url
;
190 } else if (elem
.hasTagName("meta") && elem
.hasAttribute("name")) {
191 std::string name
= elem
.getAttribute("name").utf8();
192 WebString content
= elem
.getAttribute("content");
193 if (name
== "application-name") {
194 app_info
->title
= content
;
195 } else if (name
== "description") {
196 app_info
->description
= content
;
197 } else if (name
== "application-url") {
198 std::string url
= content
.utf8();
199 app_info
->app_url
= document_url
.is_valid() ?
200 document_url
.Resolve(url
) : GURL(url
);
201 if (!app_info
->app_url
.is_valid())
202 app_info
->app_url
= GURL();
210 bool ParseWebAppFromDefinitionFile(Value
* definition_value
,
211 WebApplicationInfo
* web_app
,
213 DCHECK(web_app
->manifest_url
.is_valid());
216 std::string error_message
;
217 scoped_ptr
<Value
> schema(
218 base::JSONReader::ReadAndReturnError(
219 ResourceBundle::GetSharedInstance().GetRawDataResource(
221 base::JSON_PARSE_RFC
, // options
225 << "Error parsing JSON schema: " << error_code
<< ": " << error_message
;
226 DCHECK(schema
->IsType(Value::TYPE_DICTIONARY
))
227 << "schema root must be dictionary.";
229 JSONSchemaValidator
validator(static_cast<DictionaryValue
*>(schema
.get()));
231 // We allow extra properties in the schema for easy compat with other systems,
232 // and for forward compat with ourselves.
233 validator
.set_default_allow_additional_properties(true);
235 if (!validator
.Validate(definition_value
)) {
236 *error
= UTF8ToUTF16(
237 validator
.errors()[0].path
+ ": " + validator
.errors()[0].message
);
241 // This must be true because the schema requires the root value to be a
243 DCHECK(definition_value
->IsType(Value::TYPE_DICTIONARY
));
244 DictionaryValue
* definition
= static_cast<DictionaryValue
*>(definition_value
);
246 // Parse launch URL. It must be a valid URL in the same origin as the
248 std::string app_url_string
;
250 CHECK(definition
->GetString("launch_url", &app_url_string
));
251 if (!(app_url
= web_app
->manifest_url
.Resolve(app_url_string
)).is_valid() ||
252 app_url
.GetOrigin() != web_app
->manifest_url
.GetOrigin()) {
253 *error
= UTF8ToUTF16(WebApplicationInfo::kInvalidLaunchURL
);
257 // Parse out the permissions if present.
258 std::vector
<std::string
> permissions
;
259 ListValue
* permissions_value
= NULL
;
260 if (definition
->GetList("permissions", &permissions_value
)) {
261 for (size_t i
= 0; i
< permissions_value
->GetSize(); ++i
) {
262 std::string permission
;
263 CHECK(permissions_value
->GetString(i
, &permission
));
264 permissions
.push_back(permission
);
268 // Parse out the URLs if present.
269 std::vector
<GURL
> urls
;
270 ListValue
* urls_value
= NULL
;
271 if (definition
->GetList("urls", &urls_value
)) {
272 for (size_t i
= 0; i
< urls_value
->GetSize(); ++i
) {
273 std::string url_string
;
275 CHECK(urls_value
->GetString(i
, &url_string
));
276 if (!(url
= web_app
->manifest_url
.Resolve(url_string
)).is_valid() ||
277 url
.GetOrigin() != web_app
->manifest_url
.GetOrigin()) {
278 *error
= UTF8ToUTF16(
279 JSONSchemaValidator::FormatErrorMessage(
280 WebApplicationInfo::kInvalidURL
, base::Uint64ToString(i
)));
287 // Parse out the icons if present.
288 std::vector
<WebApplicationInfo::IconInfo
> icons
;
289 DictionaryValue
* icons_value
= NULL
;
290 if (definition
->GetDictionary("icons", &icons_value
)) {
291 for (DictionaryValue::key_iterator iter
= icons_value
->begin_keys();
292 iter
!= icons_value
->end_keys(); ++iter
) {
293 // Ignore unknown properties. Better for forward compat.
295 if (!base::StringToInt(*iter
, &size
) || size
< 0 || size
> 128)
298 std::string icon_url_string
;
300 if (!icons_value
->GetString(*iter
, &icon_url_string
) ||
301 !(icon_url
= web_app
->manifest_url
.Resolve(
302 icon_url_string
)).is_valid()) {
303 *error
= UTF8ToUTF16(
304 JSONSchemaValidator::FormatErrorMessage(
305 WebApplicationInfo::kInvalidIconURL
, base::IntToString(size
)));
309 WebApplicationInfo::IconInfo icon
;
314 icons
.push_back(icon
);
318 // Parse if offline mode is enabled.
319 definition
->GetBoolean("offline_enabled", &web_app
->is_offline_enabled
);
321 CHECK(definition
->GetString("name", &web_app
->title
));
322 definition
->GetString("description", &web_app
->description
);
323 definition
->GetString("launch_container", &web_app
->launch_container
);
324 web_app
->app_url
= app_url
;
325 web_app
->urls
= urls
;
326 web_app
->permissions
= permissions
;
327 web_app
->icons
= icons
;
333 } // namespace web_apps