2008-11-06  Antti Koivisto  <antti@apple.com>

        Reviewed by Dan Bernstein.

        https://bugs.webkit.org/show_bug.cgi?id=22093
        
        Delaying the text decoding caused regression since the decoding
        also determines the encoding in case of @charset rule. 
        
        Decode immediately in data() and keep the decoded string around
        during the checkNotify().

        * loader/CachedCSSStyleSheet.cpp:
        (WebCore::CachedCSSStyleSheet::sheetText):
        (WebCore::CachedCSSStyleSheet::data):
        * loader/CachedCSSStyleSheet.h:



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@38213 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index f9494a5..c8b29d4 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,20 @@
+2008-11-06  Antti Koivisto  <antti@apple.com>
+
+        Reviewed by Dan Bernstein.
+
+        https://bugs.webkit.org/show_bug.cgi?id=22093
+        
+        Delaying the text decoding caused regression since the decoding
+        also determines the encoding in case of @charset rule. 
+        
+        Decode immediately in data() and keep the decoded string around
+        during the checkNotify().
+
+        * loader/CachedCSSStyleSheet.cpp:
+        (WebCore::CachedCSSStyleSheet::sheetText):
+        (WebCore::CachedCSSStyleSheet::data):
+        * loader/CachedCSSStyleSheet.h:
+
 2008-11-06  Alexey Proskuryakov  <ap@webkit.org>
 
         Reviewed by Darin Adler.
diff --git a/WebCore/loader/CachedCSSStyleSheet.cpp b/WebCore/loader/CachedCSSStyleSheet.cpp
index e100121..3c7f335 100644
--- a/WebCore/loader/CachedCSSStyleSheet.cpp
+++ b/WebCore/loader/CachedCSSStyleSheet.cpp
@@ -71,9 +71,13 @@
     if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType))
         return String();
     
-    String result = m_decoder->decode(m_data->data(), encodedSize());
-    result += m_decoder->flush();
-    return result;
+    if (!m_decodedSheetText)
+        return m_decodedSheetText;
+    
+    // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
+    String sheetText = m_decoder->decode(m_data->data(), m_data->size());
+    sheetText += m_decoder->flush();
+    return sheetText;
 }
 
 void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
@@ -83,8 +87,15 @@
 
     m_data = data;
     setEncodedSize(m_data.get() ? m_data->size() : 0);
+    // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
+    if (m_data) {
+        m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
+        m_decodedSheetText += m_decoder->flush();
+    }
     m_loading = false;
     checkNotify();
+    // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
+    m_decodedSheetText = String();
 }
 
 void CachedCSSStyleSheet::checkNotify()
diff --git a/WebCore/loader/CachedCSSStyleSheet.h b/WebCore/loader/CachedCSSStyleSheet.h
index 19fc041..645b02b 100644
--- a/WebCore/loader/CachedCSSStyleSheet.h
+++ b/WebCore/loader/CachedCSSStyleSheet.h
@@ -60,6 +60,7 @@
 
     protected:
         RefPtr<TextResourceDecoder> m_decoder;
+        String m_decodedSheetText;
     };
 
 }