summaryrefslogtreecommitdiffstats
path: root/webkit/glue
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-17 05:34:36 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-17 05:34:36 +0000
commitd978840b3799a139f284c413ffe79b03944afa5e (patch)
tree852096116256af73dcb5fb79c5c8406d3694d6ac /webkit/glue
parent61bb694d38cc0d944c5cdd07ed44f6c72868e05e (diff)
downloadchromium_src-d978840b3799a139f284c413ffe79b03944afa5e.zip
chromium_src-d978840b3799a139f284c413ffe79b03944afa5e.tar.gz
chromium_src-d978840b3799a139f284c413ffe79b03944afa5e.tar.bz2
Revert "Implement web app definition parsing."
This reverts commit 24e3962db61243eea4976bead441c6206cd4f21e. TBR=aa@chromium.org git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66394 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue')
-rw-r--r--webkit/glue/dom_operations.cc136
-rw-r--r--webkit/glue/dom_operations.h43
-rw-r--r--webkit/glue/dom_operations_unittest.cc54
3 files changed, 232 insertions, 1 deletions
diff --git a/webkit/glue/dom_operations.cc b/webkit/glue/dom_operations.cc
index 07c43b2..bc546e2 100644
--- a/webkit/glue/dom_operations.cc
+++ b/webkit/glue/dom_operations.cc
@@ -7,7 +7,8 @@
#include <set>
#include "base/compiler_specific.h"
-#include "base/logging.h"
+#include "base/string_number_conversions.h"
+#include "base/string_split.h"
#include "base/string_util.h"
#include "third_party/WebKit/WebKit/chromium/public/WebAnimationController.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
@@ -238,6 +239,139 @@ bool GetAllSavableResourceLinksForCurrentPage(WebView* view,
return true;
}
+// Sizes a single size (the width or height) from a 'sizes' attribute. A size
+// matches must match the following regex: [1-9][0-9]*.
+static int ParseSingleIconSize(const string16& text) {
+ // Size must not start with 0, and be between 0 and 9.
+ if (text.empty() || !(text[0] >= L'1' && text[0] <= L'9'))
+ return 0;
+ // Make sure all chars are from 0-9.
+ for (size_t i = 1; i < text.length(); ++i) {
+ if (!(text[i] >= L'0' && text[i] <= L'9'))
+ return 0;
+ }
+ int output;
+ if (!base::StringToInt(text, &output))
+ return 0;
+ return output;
+}
+
+// Parses an icon size. An icon size must match the following regex:
+// [1-9][0-9]*x[1-9][0-9]*.
+// If the input couldn't be parsed, a size with a width/height < 0 is returned.
+static gfx::Size ParseIconSize(const string16& text) {
+ std::vector<string16> sizes;
+ base::SplitStringDontTrim(text, L'x', &sizes);
+ if (sizes.size() != 2)
+ return gfx::Size();
+
+ return gfx::Size(ParseSingleIconSize(sizes[0]),
+ ParseSingleIconSize(sizes[1]));
+}
+
+WebApplicationInfo::WebApplicationInfo() {}
+
+WebApplicationInfo::~WebApplicationInfo() {}
+
+bool ParseIconSizes(const string16& text,
+ std::vector<gfx::Size>* sizes,
+ bool* is_any) {
+ *is_any = false;
+ std::vector<string16> size_strings;
+ base::SplitStringAlongWhitespace(text, &size_strings);
+ for (size_t i = 0; i < size_strings.size(); ++i) {
+ if (EqualsASCII(size_strings[i], "any")) {
+ *is_any = true;
+ } else {
+ gfx::Size size = ParseIconSize(size_strings[i]);
+ if (size.width() <= 0 || size.height() <= 0)
+ return false; // Bogus size.
+ sizes->push_back(size);
+ }
+ }
+ if (*is_any && !sizes->empty()) {
+ // If is_any is true, it must occur by itself.
+ return false;
+ }
+ return (*is_any || !sizes->empty());
+}
+
+static void AddInstallIcon(const WebElement& link,
+ std::vector<WebApplicationInfo::IconInfo>* icons) {
+ WebString href = link.getAttribute("href");
+ if (href.isNull() || href.isEmpty())
+ return;
+
+ // Get complete url.
+ GURL url = link.document().completeURL(href);
+ if (!url.is_valid())
+ return;
+
+ if (!link.hasAttribute("sizes"))
+ return;
+
+ bool is_any = false;
+ std::vector<gfx::Size> icon_sizes;
+ if (!ParseIconSizes(link.getAttribute("sizes"), &icon_sizes, &is_any) ||
+ is_any ||
+ icon_sizes.size() != 1) {
+ return;
+ }
+ WebApplicationInfo::IconInfo icon_info;
+ icon_info.width = icon_sizes[0].width();
+ icon_info.height = icon_sizes[0].height();
+ icon_info.url = url;
+ icons->push_back(icon_info);
+}
+
+void GetApplicationInfo(WebView* view, WebApplicationInfo* app_info) {
+ WebFrame* main_frame = view->mainFrame();
+ if (!main_frame)
+ return;
+
+ WebDocument doc = main_frame->document();
+ if (doc.isNull())
+ return;
+
+ WebElement head = main_frame->document().head();
+ if (head.isNull())
+ return;
+
+ WebNodeList children = head.childNodes();
+ for (unsigned i = 0; i < children.length(); ++i) {
+ WebNode child = children.item(i);
+ if (!child.isElementNode())
+ continue;
+ WebElement elem = child.to<WebElement>();
+
+ if (elem.hasTagName("link")) {
+ std::string rel = elem.getAttribute("rel").utf8();
+ // "rel" attribute may use either "icon" or "shortcut icon".
+ // see also
+ // <http://en.wikipedia.org/wiki/Favicon>
+ // <http://dev.w3.org/html5/spec/Overview.html#rel-icon>
+ if (LowerCaseEqualsASCII(rel, "icon") ||
+ LowerCaseEqualsASCII(rel, "shortcut icon"))
+ AddInstallIcon(elem, &app_info->icons);
+ } else if (elem.hasTagName("meta") && elem.hasAttribute("name")) {
+ std::string name = elem.getAttribute("name").utf8();
+ WebString content = elem.getAttribute("content");
+ if (name == "application-name") {
+ app_info->title = content;
+ } else if (name == "description") {
+ app_info->description = content;
+ } else if (name == "application-url") {
+ std::string url = content.utf8();
+ GURL main_url = main_frame->url();
+ app_info->app_url = main_url.is_valid() ?
+ main_url.Resolve(url) : GURL(url);
+ if (!app_info->app_url.is_valid())
+ app_info->app_url = GURL();
+ }
+ }
+ }
+}
+
bool PauseAnimationAtTimeOnElementWithId(WebView* view,
const std::string& animation_name,
double time,
diff --git a/webkit/glue/dom_operations.h b/webkit/glue/dom_operations.h
index 924878f..951eb0f 100644
--- a/webkit/glue/dom_operations.h
+++ b/webkit/glue/dom_operations.h
@@ -8,6 +8,7 @@
#include <string>
#include <vector>
+#include "gfx/size.h"
#include "googleurl/src/gurl.h"
namespace WebKit {
@@ -53,6 +54,48 @@ bool GetAllSavableResourceLinksForCurrentPage(WebKit::WebView* view,
const GURL& page_url, SavableResourcesResult* savable_resources_result,
const char** savable_schemes);
+// Structure used when installing a web page as an app. Populated via
+// GetApplicationInfo.
+struct WebApplicationInfo {
+ WebApplicationInfo();
+ ~WebApplicationInfo();
+
+ struct IconInfo {
+ GURL url;
+ int width;
+ int height;
+ };
+
+ // Title of the application. This is set from the meta tag whose name is
+ // 'application-name'.
+ string16 title;
+
+ // Description of the application. This is set from the meta tag whose name
+ // is 'description'.
+ string16 description;
+
+ // URL for the app. This is set from the meta tag whose name is
+ // 'application-url'.
+ GURL app_url;
+
+ // Set of available icons. This is set for all link tags whose rel=icon. Only
+ // icons that have a non-zero (width and/or height) are added.
+ std::vector<IconInfo> icons;
+};
+
+// Parses the icon's size attribute as defined in the HTML 5 spec. Returns true
+// on success, false on errors. On success either all the sizes specified in
+// the attribute are added to sizes, or is_any is set to true.
+//
+// You shouldn't have a need to invoke this directly, it's public for testing.
+bool ParseIconSizes(const string16& text,
+ std::vector<gfx::Size>* sizes,
+ bool* is_any);
+
+// Gets the application info for the specified page. See the description of
+// WebApplicationInfo for details as to where each field comes from.
+void GetApplicationInfo(WebKit::WebView* view, WebApplicationInfo* app_info);
+
// Invokes pauseAnimationAtTime on the AnimationController associated with the
// |view|s main frame.
// This is used by test shell.
diff --git a/webkit/glue/dom_operations_unittest.cc b/webkit/glue/dom_operations_unittest.cc
index fac49c6..c57e943 100644
--- a/webkit/glue/dom_operations_unittest.cc
+++ b/webkit/glue/dom_operations_unittest.cc
@@ -128,4 +128,58 @@ TEST_F(DomOperationsTests, GetSavableResourceLinksWithPageHasInvalidLinks) {
GetSavableResourceLinksForPage(page_file_path, expected_resources_set);
}
+// Tests ParseIconSizes with various input.
+TEST_F(DomOperationsTests, ParseIconSizes) {
+ struct TestData {
+ const char* input;
+ const bool expected_result;
+ const bool is_any;
+ const size_t expected_size_count;
+ const int width1;
+ const int height1;
+ const int width2;
+ const int height2;
+ } data[] = {
+ // Bogus input cases.
+ { "10", false, false, 0, 0, 0, 0, 0 },
+ { "10 10", false, false, 0, 0, 0, 0, 0 },
+ { "010", false, false, 0, 0, 0, 0, 0 },
+ { " 010 ", false, false, 0, 0, 0, 0, 0 },
+ { " 10x ", false, false, 0, 0, 0, 0, 0 },
+ { " x10 ", false, false, 0, 0, 0, 0, 0 },
+ { "any 10x10", false, false, 0, 0, 0, 0, 0 },
+ { "", false, false, 0, 0, 0, 0, 0 },
+ { "10ax11", false, false, 0, 0, 0, 0, 0 },
+
+ // Any.
+ { "any", true, true, 0, 0, 0, 0, 0 },
+ { " any", true, true, 0, 0, 0, 0, 0 },
+ { " any ", true, true, 0, 0, 0, 0, 0 },
+
+ // Sizes.
+ { "10x11", true, false, 1, 10, 11, 0, 0 },
+ { " 10x11 ", true, false, 1, 10, 11, 0, 0 },
+ { " 10x11 1x2", true, false, 2, 10, 11, 1, 2 },
+ };
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
+ bool is_any;
+ std::vector<gfx::Size> sizes;
+ bool result = webkit_glue::ParseIconSizes(
+ ASCIIToUTF16(data[i].input), &sizes, &is_any);
+ ASSERT_EQ(result, data[i].expected_result);
+ if (result) {
+ ASSERT_EQ(data[i].is_any, is_any);
+ ASSERT_EQ(data[i].expected_size_count, sizes.size());
+ if (sizes.size() > 0) {
+ ASSERT_EQ(data[i].width1, sizes[0].width());
+ ASSERT_EQ(data[i].height1, sizes[0].height());
+ }
+ if (sizes.size() > 1) {
+ ASSERT_EQ(data[i].width2, sizes[1].width());
+ ASSERT_EQ(data[i].height2, sizes[1].height());
+ }
+ }
+ }
+}
+
} // namespace