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.
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.
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.
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.
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.
28 #include "CachedScript.h"
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>
38 #include <parser/SourceProvider.h>
43 CachedScript::CachedScript(const ResourceRequest& resourceRequest, const String& charset)
44 : CachedResource(resourceRequest, Script)
45 , m_decoder(TextResourceDecoder::create("application/javascript", charset))
46 , m_decodedDataDeletionTimer(this, &CachedScript::decodedDataDeletionTimerFired)
48 // It's javascript we want.
49 // But some websites think their scripts are <some wrong mimetype here>
50 // and refuse to serve them if we only accept application/x-javascript.
54 CachedScript::~CachedScript()
58 void CachedScript::didAddClient(CachedResourceClient* c)
60 if (m_decodedDataDeletionTimer.isActive())
61 m_decodedDataDeletionTimer.stop();
63 CachedResource::didAddClient(c);
66 void CachedScript::allClientsRemoved()
68 if (double interval = memoryCache()->deadDecodedDataDeletionInterval())
69 m_decodedDataDeletionTimer.startOneShot(interval);
72 void CachedScript::setEncoding(const String& chs)
74 m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
77 String CachedScript::encoding() const
79 return m_decoder->encoding().name();
82 const String& CachedScript::script()
84 ASSERT(!isPurgeable());
86 if (!m_script && m_data) {
87 m_script = m_decoder->decode(m_data->data(), encodedSize());
88 m_script += m_decoder->flush();
89 setDecodedSize(m_script.length() * sizeof(UChar));
91 m_decodedDataDeletionTimer.startOneShot(0);
96 void CachedScript::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
102 setEncodedSize(m_data.get() ? m_data->size() : 0);
107 void CachedScript::error(CachedResource::Status status)
110 ASSERT(errorOccurred());
115 void CachedScript::destroyDecodedData()
118 unsigned extraSize = 0;
120 if (m_sourceProviderCache && m_clients.isEmpty())
121 m_sourceProviderCache->clear();
123 extraSize = m_sourceProviderCache ? m_sourceProviderCache->byteSize() : 0;
125 setDecodedSize(extraSize);
126 if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable())
130 void CachedScript::decodedDataDeletionTimerFired(Timer<CachedScript>*)
132 destroyDecodedData();
136 JSC::SourceProviderCache* CachedScript::sourceProviderCache() const
138 if (!m_sourceProviderCache)
139 m_sourceProviderCache = adoptPtr(new JSC::SourceProviderCache);
140 return m_sourceProviderCache.get();
143 void CachedScript::sourceProviderCacheSizeChanged(int delta)
145 setDecodedSize(decodedSize() + delta);
149 } // namespace WebCore