CachedScript.cpp revision cad810f21b803229eb11403f9209855525a25d57
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, 2007, 2008 Apple Inc. All rights reserved.
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 "CachedScript.h"
29
30#include "MemoryCache.h"
31#include "CachedResourceClient.h"
32#include "CachedResourceClientWalker.h"
33#include "SharedBuffer.h"
34#include "TextResourceDecoder.h"
35#include <wtf/Vector.h>
36
37namespace WebCore {
38
39CachedScript::CachedScript(const String& url, const String& charset)
40    : CachedResource(url, Script)
41    , m_decoder(TextResourceDecoder::create("application/javascript", charset))
42    , m_decodedDataDeletionTimer(this, &CachedScript::decodedDataDeletionTimerFired)
43{
44    // It's javascript we want.
45    // But some websites think their scripts are <some wrong mimetype here>
46    // and refuse to serve them if we only accept application/x-javascript.
47    setAccept("*/*");
48}
49
50CachedScript::~CachedScript()
51{
52}
53
54void CachedScript::allClientsRemoved()
55{
56    m_decodedDataDeletionTimer.startOneShot(0);
57}
58
59void CachedScript::setEncoding(const String& chs)
60{
61    m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
62}
63
64String CachedScript::encoding() const
65{
66    return m_decoder->encoding().name();
67}
68
69const String& CachedScript::script()
70{
71    ASSERT(!isPurgeable());
72
73    if (!m_script && m_data) {
74        m_script = m_decoder->decode(m_data->data(), encodedSize());
75        m_script += m_decoder->flush();
76        setDecodedSize(m_script.length() * sizeof(UChar));
77    }
78    m_decodedDataDeletionTimer.startOneShot(0);
79    return m_script;
80}
81
82void CachedScript::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
83{
84    if (!allDataReceived)
85        return;
86
87    m_data = data;
88    setEncodedSize(m_data.get() ? m_data->size() : 0);
89    setLoading(false);
90    checkNotify();
91}
92
93void CachedScript::checkNotify()
94{
95    if (isLoading())
96        return;
97
98    CachedResourceClientWalker w(m_clients);
99    while (CachedResourceClient* c = w.next())
100        c->notifyFinished(this);
101}
102
103void CachedScript::error(CachedResource::Status status)
104{
105    setStatus(status);
106    ASSERT(errorOccurred());
107    setLoading(false);
108    checkNotify();
109}
110
111void CachedScript::destroyDecodedData()
112{
113    m_script = String();
114    setDecodedSize(0);
115    if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable())
116        makePurgeable(true);
117}
118
119void CachedScript::decodedDataDeletionTimerFired(Timer<CachedScript>*)
120{
121    destroyDecodedData();
122}
123
124} // namespace WebCore
125