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