Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2010 Google, Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "core/dom/PendingScript.h"
     28 
     29 #include "bindings/core/v8/ScriptSourceCode.h"
     30 #include "bindings/core/v8/ScriptStreamer.h"
     31 #include "core/dom/Element.h"
     32 #include "core/fetch/ScriptResource.h"
     33 
     34 namespace blink {
     35 
     36 PendingScript::~PendingScript()
     37 {
     38 }
     39 
     40 void PendingScript::watchForLoad(ScriptResourceClient* client)
     41 {
     42     ASSERT(!m_watchingForLoad);
     43     ASSERT(!isReady());
     44     if (m_streamer) {
     45         m_streamer->addClient(client);
     46     } else {
     47         // addClient() will call notifyFinished() if the load is
     48         // complete. Callers do not expect to be re-entered from this call, so
     49         // they should not become a client of an already-loaded Resource.
     50         resource()->addClient(client);
     51     }
     52     m_watchingForLoad = true;
     53 }
     54 
     55 void PendingScript::stopWatchingForLoad(ScriptResourceClient* client)
     56 {
     57     if (!m_watchingForLoad)
     58         return;
     59     ASSERT(resource());
     60     if (m_streamer) {
     61         m_streamer->cancel();
     62         m_streamer->removeClient(client);
     63         m_streamer.clear();
     64     } else {
     65         resource()->removeClient(client);
     66     }
     67     m_watchingForLoad = false;
     68 }
     69 
     70 PassRefPtrWillBeRawPtr<Element> PendingScript::releaseElementAndClear()
     71 {
     72     setScriptResource(0);
     73     m_watchingForLoad = false;
     74     m_startingPosition = TextPosition::belowRangePosition();
     75     m_streamer.release();
     76     return m_element.release();
     77 }
     78 
     79 void PendingScript::setScriptResource(ScriptResource* resource)
     80 {
     81     setResource(resource);
     82 }
     83 
     84 void PendingScript::notifyFinished(Resource* resource)
     85 {
     86     if (m_streamer)
     87         m_streamer->notifyFinished(resource);
     88 }
     89 
     90 void PendingScript::notifyAppendData(ScriptResource* resource)
     91 {
     92     if (m_streamer)
     93         m_streamer->notifyAppendData(resource);
     94 }
     95 
     96 void PendingScript::trace(Visitor* visitor)
     97 {
     98     visitor->trace(m_element);
     99 }
    100 
    101 ScriptSourceCode PendingScript::getSource(const KURL& documentURL, bool& errorOccurred) const
    102 {
    103     if (resource()) {
    104         errorOccurred = resource()->errorOccurred();
    105         ASSERT(resource()->isLoaded());
    106         if (m_streamer && !m_streamer->streamingSuppressed())
    107             return ScriptSourceCode(m_streamer, resource());
    108         return ScriptSourceCode(resource());
    109     }
    110     errorOccurred = false;
    111     return ScriptSourceCode(m_element->textContent(), documentURL, startingPosition());
    112 }
    113 
    114 bool PendingScript::isReady() const
    115 {
    116     if (resource() && !resource()->isLoaded())
    117         return false;
    118     if (m_streamer && !m_streamer->isFinished())
    119         return false;
    120     return true;
    121 }
    122 
    123 }
    124