WebFonts are re-fetched from the server upon Document::styleSelectorChanged call.
authorbashi@chromium.org <bashi@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 10 Jan 2012 10:13:02 +0000 (10:13 +0000)
committerbashi@chromium.org <bashi@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 10 Jan 2012 10:13:02 +0000 (10:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=73419

Source/WebCore:

Holds the CachedFont handle in CSSFontFaceSrcValue so that avoiding re-validation during style recalculation.

Reviewed by Dan Bernstein.

Test: http/tests/css/font-face-src-loading.html

* css/CSSFontFaceSrcValue.cpp:
(WebCore::CSSFontFaceSrcValue::cachedFont): Added.
* css/CSSFontFaceSrcValue.h:
* css/CSSFontSelector.cpp:
(WebCore::CSSFontSelector::addFontFaceRule): Gets the CachedFont from CSSFontFaceSrcValue object. The object will request the resource if it doesn't exist.

LayoutTests:

Reviewed by Dan Bernstein.

Adds a test that ensures re-fetching WebFonts doesn't occur after style recalculation.

* http/tests/css/font-face-src-loading-expected.txt: Added.
* http/tests/css/font-face-src-loading.html: Added.
* http/tests/css/resources/webfont-request.php: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@104542 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/css/font-face-src-loading-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/css/font-face-src-loading.html [new file with mode: 0644]
LayoutTests/http/tests/css/resources/webfont-request.php [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/css/CSSFontFaceSrcValue.cpp
Source/WebCore/css/CSSFontFaceSrcValue.h
Source/WebCore/css/CSSFontSelector.cpp

index 6f2d596..a9cb39b 100644 (file)
@@ -1,3 +1,16 @@
+2012-01-10  Kenichi Ishibashi  <bashi@chromium.org>
+
+        WebFonts are re-fetched from the server upon Document::styleSelectorChanged call.
+        https://bugs.webkit.org/show_bug.cgi?id=73419
+
+        Reviewed by Dan Bernstein.
+
+        Adds a test that ensures re-fetching WebFonts doesn't occur after style recalculation.
+
+        * http/tests/css/font-face-src-loading-expected.txt: Added.
+        * http/tests/css/font-face-src-loading.html: Added.
+        * http/tests/css/resources/webfont-request.php: Added.
+
 2012-01-09  Dmitry Titov  <dimich@chromium.org>
 
         [Chromium] Unreviewed rebaseline, fixing incorrect rebaseline from r104489.
diff --git a/LayoutTests/http/tests/css/font-face-src-loading-expected.txt b/LayoutTests/http/tests/css/font-face-src-loading-expected.txt
new file mode 100644 (file)
index 0000000..b627280
--- /dev/null
@@ -0,0 +1,5 @@
+This test ensures that webfonts are not re-fetched after style recalculation.
+
+Text
+Number of requests after recalculation: 0
+SUCCESS
diff --git a/LayoutTests/http/tests/css/font-face-src-loading.html b/LayoutTests/http/tests/css/font-face-src-loading.html
new file mode 100644 (file)
index 0000000..f8ed290
--- /dev/null
@@ -0,0 +1,68 @@
+<head>
+<style>
+@font-face {
+    font-family: 'webfont';
+    src: url(http://localhost:8000/css/resources/webfont-request.php?mode=getFont&filename=webfontRequestCount.txt) format('truetype');
+}
+</style>
+<script>
+if (window.layoutTestController) {
+    layoutTestController.dumpAsText();
+    layoutTestController.waitUntilDone();
+}
+
+var filename = 'filename=webfontRequestCount.txt';
+var xhr;
+
+function startTest() {
+    // Resets request count before starting the test.
+    xhr = new XMLHttpRequest;
+    xhr.onerror = function() {
+        document.getElementById('result').innerText = 'FAIL: Network error.';
+        if (window.layoutTestController)
+            layoutTestController.notifyDone();
+    };
+    xhr.onreadystatechange = function() {
+        if (xhr.readyState == xhr.DONE)
+            forceStyleSelectorRecreation();
+    };
+    xhr.open('GET', '/resources/reset-temp-file.php?' + filename, true);
+    xhr.send();
+}
+
+function forceStyleSelectorRecreation() {
+    document.head.appendChild(document.createElement("style"));
+    // Pauses for a moment to see whether font re-fetching occurs.
+    setTimeout(getRequestCount, 100);
+}
+
+function getRequestCount() {
+    xhr.onreadystatechange = function() {
+        if (xhr.readyState == xhr.DONE)
+            checkRequestCount();
+    };
+    xhr.open('GET', 'http://localhost:8000/css/resources/webfont-request.php?mode=getRequestCount&' + filename, true);
+    xhr.send();
+}
+
+function checkRequestCount() {
+    var numRequest = xhr.responseText;
+    var result = document.getElementById('result');
+    result.innerHTML = 'Number of requests after recalculation: ' + numRequest + '<br />';
+    if (numRequest == '0')
+        result.innerHTML += 'SUCCESS';
+    else
+        result.innerHTML += 'FAIL';
+    if (window.layoutTestController)
+        layoutTestController.notifyDone();
+};
+</script>
+</head>
+<body onload="startTest()">
+<p>This test ensures that webfonts are not re-fetched after style recalculation.</p>
+<div style="font-family: webfont;">Text</div>
+<div id="result"></div>
+<script>
+document.body.offsetTop;
+</script>
+</body>
diff --git a/LayoutTests/http/tests/css/resources/webfont-request.php b/LayoutTests/http/tests/css/resources/webfont-request.php
new file mode 100644 (file)
index 0000000..ff6b59d
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+require_once "../../resources/portabilityLayer.php";
+
+function getRequestCount($file)
+{
+    if (!file_exists($file)) {
+        return 0;
+    }
+    return (int)file_get_contents($file);
+}
+
+function setRequestCount($file, $count)
+{
+    file_put_contents($file, $count);
+}
+
+$tmpFile = sys_get_temp_dir() . "/" . $_GET["filename"];
+
+$currentCount = getRequestCount($tmpFile);
+$mode = $_GET["mode"];
+
+if ($mode == "getFont") {
+    setRequestCount($tmpFile, $currentCount + 1);
+    header("Access-control-max-age: 0");
+    header("Access-control-allow-origin: *");
+    header("Access-control-allow-methods: *");
+    header("Cache-Control: max-age=0");
+    header("Content-Type: application/octet-stream");
+    echo "";
+} else if ($mode == "getRequestCount") {
+    header("Access-control-max-age: 0");
+    header("Access-control-allow-origin: *");
+    header("Access-control-allow-methods: *");
+    echo $currentCount;
+}
+?>
index 6e7d007..1ffbf04 100644 (file)
@@ -1,3 +1,20 @@
+2012-01-10  Kenichi Ishibashi  <bashi@chromium.org>
+
+        WebFonts are re-fetched from the server upon Document::styleSelectorChanged call.
+        https://bugs.webkit.org/show_bug.cgi?id=73419
+
+        Holds the CachedFont handle in CSSFontFaceSrcValue so that avoiding re-validation during style recalculation.
+
+        Reviewed by Dan Bernstein.
+
+        Test: http/tests/css/font-face-src-loading.html
+
+        * css/CSSFontFaceSrcValue.cpp:
+        (WebCore::CSSFontFaceSrcValue::cachedFont): Added.
+        * css/CSSFontFaceSrcValue.h:
+        * css/CSSFontSelector.cpp:
+        (WebCore::CSSFontSelector::addFontFaceRule): Gets the CachedFont from CSSFontFaceSrcValue object. The object will request the resource if it doesn't exist.
+
 2012-01-10  pfeldman@chomium.org  <pavel.feldman@gmail.com>
 
         Web Inspector: introduce experimental setting that makes source files always editable
index 57300ef..3cdece8 100644 (file)
@@ -26,6 +26,9 @@
 #include "config.h"
 #include "CSSFontFaceSrcValue.h"
 #include "CSSStyleSheet.h"
+#include "CachedFont.h"
+#include "CachedResourceLoader.h"
+#include "Document.h"
 #include "FontCustomPlatformData.h"
 #include "Node.h"
 
@@ -76,5 +79,14 @@ void CSSFontFaceSrcValue::addSubresourceStyleURLs(ListHashSet<KURL>& urls, const
         addSubresourceURL(urls, styleSheet->completeURL(m_resource));
 }
 
+CachedFont* CSSFontFaceSrcValue::cachedFont(Document* document)
+{
+    if (!m_cachedFont) {
+        ResourceRequest request(document->completeURL(m_resource));
+        m_cachedFont = document->cachedResourceLoader()->requestFont(request);
+    }
+    return m_cachedFont.get();
+}
+
 }
 
index 121f4d4..a5f54de 100644 (file)
 #define CSSFontFaceSrcValue_h
 
 #include "CSSValue.h"
+#include "CachedResourceHandle.h"
 #include "PlatformString.h"
 #include <wtf/PassRefPtr.h>
 
 namespace WebCore {
 
+class CachedFont;
+class Document;
 class SVGFontFaceElement;
 
 class CSSFontFaceSrcValue : public CSSValue {
@@ -64,6 +67,8 @@ public:
 
     void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
 
+    CachedFont* cachedFont(Document*);
+
 private:
     CSSFontFaceSrcValue(const String& resource, bool local)
         : CSSValue(FontFaceSrcClass)
@@ -79,6 +84,8 @@ private:
     String m_format;
     bool m_isLocal;
 
+    CachedResourceHandle<CachedFont> m_cachedFont;
+
 #if ENABLE(SVG_FONTS)
     SVGFontFaceElement* m_svgFontFaceElement;
 #endif
index ed71503..4c90ae3 100644 (file)
@@ -243,8 +243,7 @@ void CSSFontSelector::addFontFaceRule(const CSSFontFaceRule* fontFaceRule)
             Settings* settings = m_document ? m_document->frame() ? m_document->frame()->settings() : 0 : 0;
             bool allowDownloading = foundSVGFont || (settings && settings->downloadableBinaryFontsEnabled());
             if (allowDownloading && item->isSupportedFormat() && m_document) {
-                ResourceRequest request(m_document->completeURL(item->resource()));
-                CachedFont* cachedFont = m_document->cachedResourceLoader()->requestFont(request);
+                CachedFont* cachedFont = item->cachedFont(m_document);
                 if (cachedFont) {
                     source = adoptPtr(new CSSFontFaceSource(item->resource(), cachedFont));
 #if ENABLE(SVG_FONTS)