CachedCSSStyleSheet.cpp revision d8543bb6618c17b12da906afa77d216f58cf4058
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 "loader.h"
35#include <wtf/Vector.h>
36
37namespace WebCore {
38
39CachedCSSStyleSheet::CachedCSSStyleSheet(DocLoader* dl, const String& url, const String& charset, bool skipCanLoadCheck, bool sendResourceLoadCallbacks)
40    : CachedResource(url, CSSStyleSheet, true, sendResourceLoadCallbacks)
41    , m_decoder(new TextResourceDecoder("text/css", charset))
42{
43    // Prefer text/css but accept any type (dell.com serves a stylesheet
44    // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
45    setAccept("text/css,*/*;q=0.1");
46    cache()->loader()->load(dl, this, false, skipCanLoadCheck, sendResourceLoadCallbacks);
47    m_loading = true;
48}
49
50CachedCSSStyleSheet::~CachedCSSStyleSheet()
51{
52}
53
54void CachedCSSStyleSheet::ref(CachedResourceClient *c)
55{
56    CachedResource::ref(c);
57
58    if (!m_loading)
59        c->setCSSStyleSheet(m_url, m_decoder->encoding().name(), this);
60}
61
62void CachedCSSStyleSheet::setEncoding(const String& chs)
63{
64    m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
65}
66
67String CachedCSSStyleSheet::encoding() const
68{
69    return m_decoder->encoding().name();
70}
71
72void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
73{
74    if (!allDataReceived)
75        return;
76
77    m_data = data;
78    setEncodedSize(m_data.get() ? m_data->size() : 0);
79    if (m_data.get()) {
80        m_sheet = m_decoder->decode(m_data->data(), encodedSize());
81        m_sheet += m_decoder->flush();
82    }
83    m_loading = false;
84    checkNotify();
85}
86
87void CachedCSSStyleSheet::checkNotify()
88{
89    if (m_loading)
90        return;
91
92    CachedResourceClientWalker w(m_clients);
93    while (CachedResourceClient *c = w.next())
94        c->setCSSStyleSheet(m_response.url().string(), m_decoder->encoding().name(), this);
95
96#if USE(LOW_BANDWIDTH_DISPLAY)
97    // if checkNotify() is called from error(), client's setCSSStyleSheet(...)
98    // can't find "this" from url, so they can't do clean up if needed.
99    // call notifyFinished() to make sure they have a chance.
100    CachedResourceClientWalker n(m_clients);
101    while (CachedResourceClient* s = n.next())
102        s->notifyFinished(this);
103#endif
104}
105
106void CachedCSSStyleSheet::error()
107{
108    m_loading = false;
109    m_errorOccurred = true;
110    checkNotify();
111}
112
113bool CachedCSSStyleSheet::canUseSheet(bool strict) const
114{
115    if (errorOccurred())
116        return false;
117
118    if (!strict)
119        return true;
120
121    // This check exactly matches Firefox.
122    String mimeType = response().mimeType();
123    return mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
124}
125
126}
127