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.
4
5 #include "chrome/renderer/web_apps.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/json/json_reader.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/common/web_application_info.h"
17 #include "grit/common_resources.h"
18 #include "grit/generated_resources.h"
19 #include "third_party/WebKit/public/platform/WebString.h"
20 #include "third_party/WebKit/public/platform/WebURL.h"
21 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebElement.h"
23 #include "third_party/WebKit/public/web/WebFrame.h"
24 #include "third_party/WebKit/public/web/WebNode.h"
25 #include "third_party/WebKit/public/web/WebNodeList.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/size.h"
29 #include "url/gurl.h"
30
31 using blink::WebDocument;
32 using blink::WebElement;
33 using blink::WebFrame;
34 using blink::WebNode;
35 using blink::WebNodeList;
36 using blink::WebString;
37
38 namespace web_apps {
39 namespace {
40
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]*.
ParseSingleIconSize(const base::string16 & text)43 int ParseSingleIconSize(const base::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'))
46 return 0;
47
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'))
51 return 0;
52 }
53 int output;
54 if (!base::StringToInt(text, &output))
55 return 0;
56 return output;
57 }
58
59 // Parses an icon size. An icon size must match the following regex:
60 // [1-9][0-9]*x[1-9][0-9]*.
61 // If the input couldn't be parsed, a size with a width/height == 0 is returned.
ParseIconSize(const base::string16 & text)62 gfx::Size ParseIconSize(const base::string16& text) {
63 std::vector<base::string16> sizes;
64 base::SplitStringDontTrim(text, L'x', &sizes);
65 if (sizes.size() != 2)
66 return gfx::Size();
67
68 return gfx::Size(ParseSingleIconSize(sizes[0]),
69 ParseSingleIconSize(sizes[1]));
70 }
71
AddInstallIcon(const WebElement & link,std::vector<WebApplicationInfo::IconInfo> * icons)72 void AddInstallIcon(const WebElement& link,
73 std::vector<WebApplicationInfo::IconInfo>* icons) {
74 WebString href = link.getAttribute("href");
75 if (href.isNull() || href.isEmpty())
76 return;
77
78 // Get complete url.
79 GURL url = link.document().completeURL(href);
80 if (!url.is_valid())
81 return;
82
83 WebApplicationInfo::IconInfo icon_info;
84 bool is_any = false;
85 std::vector<gfx::Size> icon_sizes;
86 if (link.hasAttribute("sizes") &&
87 ParseIconSizes(link.getAttribute("sizes"), &icon_sizes, &is_any) &&
88 !is_any &&
89 icon_sizes.size() == 1) {
90 icon_info.width = icon_sizes[0].width();
91 icon_info.height = icon_sizes[0].height();
92 }
93 icon_info.url = url;
94 icons->push_back(icon_info);
95 }
96
97 } // namespace
98
ParseIconSizes(const base::string16 & text,std::vector<gfx::Size> * sizes,bool * is_any)99 bool ParseIconSizes(const base::string16& text,
100 std::vector<gfx::Size>* sizes,
101 bool* is_any) {
102 *is_any = false;
103 std::vector<base::string16> size_strings;
104 base::SplitStringAlongWhitespace(text, &size_strings);
105 for (size_t i = 0; i < size_strings.size(); ++i) {
106 if (EqualsASCII(size_strings[i], "any")) {
107 *is_any = true;
108 } else {
109 gfx::Size size = ParseIconSize(size_strings[i]);
110 if (size.width() <= 0 || size.height() <= 0)
111 return false; // Bogus size.
112 sizes->push_back(size);
113 }
114 }
115 if (*is_any && !sizes->empty()) {
116 // If is_any is true, it must occur by itself.
117 return false;
118 }
119 return (*is_any || !sizes->empty());
120 }
121
ParseWebAppFromWebDocument(WebFrame * frame,WebApplicationInfo * app_info,base::string16 * error)122 bool ParseWebAppFromWebDocument(WebFrame* frame,
123 WebApplicationInfo* app_info,
124 base::string16* error) {
125 WebDocument document = frame->document();
126 if (document.isNull())
127 return true;
128
129 WebElement head = document.head();
130 if (head.isNull())
131 return true;
132
133 GURL document_url = document.url();
134 WebNodeList children = head.childNodes();
135 for (unsigned i = 0; i < children.length(); ++i) {
136 WebNode child = children.item(i);
137 if (!child.isElementNode())
138 continue;
139 WebElement elem = child.to<WebElement>();
140
141 if (elem.hasTagName("link")) {
142 std::string rel = elem.getAttribute("rel").utf8();
143 // "rel" attribute may use either "icon" or "shortcut icon".
144 // see also
145 // <http://en.wikipedia.org/wiki/Favicon>
146 // <http://dev.w3.org/html5/spec/Overview.html#rel-icon>
147 if (LowerCaseEqualsASCII(rel, "icon") ||
148 LowerCaseEqualsASCII(rel, "shortcut icon")) {
149 AddInstallIcon(elem, &app_info->icons);
150 }
151 } else if (elem.hasTagName("meta") && elem.hasAttribute("name")) {
152 std::string name = elem.getAttribute("name").utf8();
153 WebString content = elem.getAttribute("content");
154 if (name == "application-name") {
155 app_info->title = content;
156 } else if (name == "description") {
157 app_info->description = content;
158 } else if (name == "application-url") {
159 std::string url = content.utf8();
160 app_info->app_url = document_url.is_valid() ?
161 document_url.Resolve(url) : GURL(url);
162 if (!app_info->app_url.is_valid())
163 app_info->app_url = GURL();
164 }
165 }
166 }
167
168 return true;
169 }
170
171 } // namespace web_apps
172