2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/dom/PendingScript.h"
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"
36 PendingScript::PendingScript()
37 : m_watchingForLoad(false)
38 , m_startingPosition(TextPosition::belowRangePosition())
42 PendingScript::PendingScript(Element* element, ScriptResource* resource)
43 : m_watchingForLoad(false)
46 setScriptResource(resource);
49 PendingScript::PendingScript(const PendingScript& other)
50 : ResourceOwner(other)
51 , m_watchingForLoad(other.m_watchingForLoad)
52 , m_element(other.m_element)
53 , m_startingPosition(other.m_startingPosition)
54 , m_streamer(other.m_streamer)
56 setScriptResource(other.resource());
59 PendingScript::~PendingScript()
63 PendingScript& PendingScript::operator=(const PendingScript& other)
68 m_watchingForLoad = other.m_watchingForLoad;
69 m_element = other.m_element;
70 m_startingPosition = other.m_startingPosition;
71 m_streamer = other.m_streamer;
72 this->ResourceOwner<ScriptResource, ScriptResourceClient>::operator=(other);
76 void PendingScript::watchForLoad(ScriptResourceClient* client)
78 ASSERT(!m_watchingForLoad);
79 // addClient() will call notifyFinished() if the load is complete. Callers
80 // who do not expect to be re-entered from this call should not call
81 // watchForLoad for a PendingScript which isReady. We also need to set
82 // m_watchingForLoad early, since addClient() can result in calling
83 // notifyFinished and further stopWatchingForLoad().
84 m_watchingForLoad = true;
86 m_streamer->addClient(client);
88 resource()->addClient(client);
92 void PendingScript::stopWatchingForLoad(ScriptResourceClient* client)
94 if (!m_watchingForLoad)
98 m_streamer->removeClient(client);
100 resource()->removeClient(client);
102 m_watchingForLoad = false;
105 PassRefPtrWillBeRawPtr<Element> PendingScript::releaseElementAndClear()
107 setScriptResource(0);
108 m_watchingForLoad = false;
109 m_startingPosition = TextPosition::belowRangePosition();
111 m_streamer->cancel();
112 m_streamer.release();
113 return m_element.release();
116 void PendingScript::setScriptResource(ScriptResource* resource)
118 setResource(resource);
121 void PendingScript::notifyFinished(Resource* resource)
124 m_streamer->notifyFinished(resource);
127 void PendingScript::notifyAppendData(ScriptResource* resource)
130 m_streamer->notifyAppendData(resource);
133 void PendingScript::trace(Visitor* visitor)
135 visitor->trace(m_element);
138 ScriptSourceCode PendingScript::getSource(const KURL& documentURL, bool& errorOccurred) const
141 errorOccurred = resource()->errorOccurred();
142 ASSERT(resource()->isLoaded());
143 if (m_streamer && !m_streamer->streamingSuppressed())
144 return ScriptSourceCode(m_streamer, resource());
145 return ScriptSourceCode(resource());
147 errorOccurred = false;
148 return ScriptSourceCode(m_element->textContent(), documentURL, startingPosition());
151 void PendingScript::setStreamer(PassRefPtr<ScriptStreamer> streamer)
154 ASSERT(!m_watchingForLoad);
155 m_streamer = streamer;
158 bool PendingScript::isReady() const
160 if (resource() && !resource()->isLoaded())
162 if (m_streamer && !m_streamer->isFinished())