webengine/osswebengine/WebCore/loader/CachedCSSStyleSheet.cpp
changeset 0 dd21522fd290
child 13 10e98eab6f85
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2     Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
       
     3     Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
       
     4     Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
       
     5     Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
       
     6     Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
       
     7 
       
     8     This library is free software; you can redistribute it and/or
       
     9     modify it under the terms of the GNU Library General Public
       
    10     License as published by the Free Software Foundation; either
       
    11     version 2 of the License, or (at your option) any later version.
       
    12 
       
    13     This library is distributed in the hope that it will be useful,
       
    14     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16     Library General Public License for more details.
       
    17 
       
    18     You should have received a copy of the GNU Library General Public License
       
    19     along with this library; see the file COPYING.LIB.  If not, write to
       
    20     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    21     Boston, MA 02110-1301, USA.
       
    22 
       
    23     This class provides all functionality needed for loading images, style sheets and html
       
    24     pages from the web. It has a memory cache for these objects.
       
    25 */
       
    26 
       
    27 #include "config.h"
       
    28 #include "CachedCSSStyleSheet.h"
       
    29 
       
    30 #include "Cache.h"
       
    31 #include "CachedResourceClient.h"
       
    32 #include "CachedResourceClientWalker.h"
       
    33 #include "TextResourceDecoder.h"
       
    34 #include "DeprecatedString.h"
       
    35 #include "loader.h"
       
    36 #include <wtf/Vector.h>
       
    37 
       
    38 namespace WebCore {
       
    39 
       
    40 CachedCSSStyleSheet::CachedCSSStyleSheet(DocLoader* dl, const String& url, const String& charset, bool skipCanLoadCheck, bool sendResourceLoadCallbacks)
       
    41     : CachedResource(url, CSSStyleSheet, true, sendResourceLoadCallbacks)
       
    42     , m_decoder(new TextResourceDecoder("text/css", charset))
       
    43 {
       
    44     // Prefer text/css but accept any type (dell.com serves a stylesheet
       
    45     // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
       
    46     setAccept("text/css,*/*;q=0.1");
       
    47     cache()->loader()->load(dl, this, false, skipCanLoadCheck, sendResourceLoadCallbacks);
       
    48     m_loading = true;
       
    49 }
       
    50 
       
    51 CachedCSSStyleSheet::~CachedCSSStyleSheet()
       
    52 {
       
    53 }
       
    54 
       
    55 void CachedCSSStyleSheet::ref(CachedResourceClient *c)
       
    56 {
       
    57     CachedResource::ref(c);
       
    58 
       
    59     if (!m_loading)
       
    60         c->setCSSStyleSheet(m_url, m_decoder->encoding().name(), errorOccurred() ? "" : m_sheet);
       
    61 }
       
    62 
       
    63 void CachedCSSStyleSheet::setEncoding(const String& chs)
       
    64 {
       
    65     m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
       
    66 }
       
    67 
       
    68 void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
       
    69 {
       
    70     if (!allDataReceived)
       
    71         return;
       
    72 
       
    73     m_data = data;
       
    74     setEncodedSize(m_data.get() ? m_data->size() : 0);
       
    75     if (m_data.get()) {
       
    76         m_sheet = m_decoder->decode(m_data->data(), encodedSize());
       
    77         m_sheet += m_decoder->flush();
       
    78     }
       
    79     m_loading = false;
       
    80     checkNotify();
       
    81 }
       
    82 
       
    83 void CachedCSSStyleSheet::checkNotify()
       
    84 {
       
    85     if (m_loading)
       
    86         return;
       
    87 
       
    88     CachedResourceClientWalker w(m_clients);
       
    89     while (CachedResourceClient *c = w.next())
       
    90         c->setCSSStyleSheet(m_response.url().url(), m_decoder->encoding().name(), m_sheet);
       
    91 
       
    92 #if USE(LOW_BANDWIDTH_DISPLAY)        
       
    93     // if checkNotify() is called from error(), client's setCSSStyleSheet(...)
       
    94     // can't find "this" from url, so they can't do clean up if needed.
       
    95     // call notifyFinished() to make sure they have a chance.
       
    96     CachedResourceClientWalker n(m_clients);
       
    97     while (CachedResourceClient* s = n.next())
       
    98         s->notifyFinished(this);
       
    99 #endif        
       
   100 }
       
   101 
       
   102 void CachedCSSStyleSheet::error()
       
   103 {
       
   104     m_loading = false;
       
   105     m_errorOccurred = true;
       
   106     checkNotify();
       
   107 }
       
   108 
       
   109 }