Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / fetch / CSSStyleSheetResource.cpp
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 "core/fetch/CSSStyleSheetResource.h"
29
30 #include "core/css/StyleSheetContents.h"
31 #include "core/fetch/ResourceClientWalker.h"
32 #include "core/fetch/StyleSheetResourceClient.h"
33 #include "core/html/parser/TextResourceDecoder.h"
34 #include "platform/SharedBuffer.h"
35 #include "platform/network/HTTPParsers.h"
36 #include "wtf/CurrentTime.h"
37 #include "wtf/Vector.h"
38
39 namespace WebCore {
40
41 CSSStyleSheetResource::CSSStyleSheetResource(const ResourceRequest& resourceRequest, const String& charset)
42     : StyleSheetResource(resourceRequest, CSSStyleSheet)
43     , m_decoder(TextResourceDecoder::create("text/css", charset))
44 {
45     DEFINE_STATIC_LOCAL(const AtomicString, acceptCSS, ("text/css,*/*;q=0.1", AtomicString::ConstructFromLiteral));
46
47     // Prefer text/css but accept any type (dell.com serves a stylesheet
48     // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
49     setAccept(acceptCSS);
50 }
51
52 CSSStyleSheetResource::~CSSStyleSheetResource()
53 {
54     if (m_parsedStyleSheetCache)
55         m_parsedStyleSheetCache->removedFromMemoryCache();
56 }
57
58 void CSSStyleSheetResource::didAddClient(ResourceClient* c)
59 {
60     ASSERT(c->resourceClientType() == StyleSheetResourceClient::expectedType());
61     // Resource::didAddClient() must be before setCSSStyleSheet(),
62     // because setCSSStyleSheet() may cause scripts to be executed, which could destroy 'c' if it is an instance of HTMLLinkElement.
63     // see the comment of HTMLLinkElement::setCSSStyleSheet.
64     Resource::didAddClient(c);
65
66     if (!isLoading())
67         static_cast<StyleSheetResourceClient*>(c)->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this);
68 }
69
70 void CSSStyleSheetResource::setEncoding(const String& chs)
71 {
72     m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
73 }
74
75 String CSSStyleSheetResource::encoding() const
76 {
77     return m_decoder->encoding().name();
78 }
79
80 const String CSSStyleSheetResource::sheetText(bool enforceMIMEType, bool* hasValidMIMEType) const
81 {
82     ASSERT(!isPurgeable());
83
84     if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType, hasValidMIMEType))
85         return String();
86
87     if (!m_decodedSheetText.isNull())
88         return m_decodedSheetText;
89
90     // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
91     String sheetText = m_decoder->decode(m_data->data(), m_data->size());
92     sheetText = sheetText + m_decoder->flush();
93     return sheetText;
94 }
95
96 void CSSStyleSheetResource::checkNotify()
97 {
98     // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
99     if (m_data) {
100         m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
101         m_decodedSheetText = m_decodedSheetText + m_decoder->flush();
102     }
103
104     ResourceClientWalker<StyleSheetResourceClient> w(m_clients);
105     while (StyleSheetResourceClient* c = w.next())
106         c->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this);
107     // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
108     m_decodedSheetText = String();
109 }
110
111 bool CSSStyleSheetResource::isSafeToUnlock() const
112 {
113     return m_data->hasOneRef();
114 }
115
116 void CSSStyleSheetResource::destroyDecodedDataIfPossible()
117 {
118     if (!m_parsedStyleSheetCache)
119         return;
120
121     m_parsedStyleSheetCache->removedFromMemoryCache();
122     m_parsedStyleSheetCache.clear();
123
124     setDecodedSize(0);
125 }
126
127 bool CSSStyleSheetResource::canUseSheet(bool enforceMIMEType, bool* hasValidMIMEType) const
128 {
129     if (errorOccurred())
130         return false;
131
132     if (!enforceMIMEType && !hasValidMIMEType)
133         return true;
134
135     // This check exactly matches Firefox. Note that we grab the Content-Type
136     // header directly because we want to see what the value is BEFORE content
137     // sniffing. Firefox does this by setting a "type hint" on the channel.
138     // This implementation should be observationally equivalent.
139     //
140     // This code defaults to allowing the stylesheet for non-HTTP protocols so
141     // folks can use standards mode for local HTML documents.
142     const AtomicString& mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type"));
143     bool typeOK = mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
144     if (hasValidMIMEType)
145         *hasValidMIMEType = typeOK;
146     if (!enforceMIMEType)
147         return true;
148     return typeOK;
149 }
150
151 PassRefPtrWillBeRawPtr<StyleSheetContents> CSSStyleSheetResource::restoreParsedStyleSheet(const CSSParserContext& context)
152 {
153     if (!m_parsedStyleSheetCache)
154         return nullptr;
155     if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) {
156         m_parsedStyleSheetCache->removedFromMemoryCache();
157         m_parsedStyleSheetCache.clear();
158         return nullptr;
159     }
160
161     ASSERT(m_parsedStyleSheetCache->isCacheable());
162     ASSERT(m_parsedStyleSheetCache->isInMemoryCache());
163
164     // Contexts must be identical so we know we would get the same exact result if we parsed again.
165     if (m_parsedStyleSheetCache->parserContext() != context)
166         return nullptr;
167
168     didAccessDecodedData();
169
170     return m_parsedStyleSheetCache;
171 }
172
173 void CSSStyleSheetResource::saveParsedStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents> sheet)
174 {
175     ASSERT(sheet && sheet->isCacheable());
176
177     if (m_parsedStyleSheetCache)
178         m_parsedStyleSheetCache->removedFromMemoryCache();
179     m_parsedStyleSheetCache = sheet;
180     m_parsedStyleSheetCache->addedToMemoryCache();
181
182     setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes());
183 }
184
185 }