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