Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / PendingScript.cpp
blobb70c07ac40a6352e968e5599aad04689f575288c
1 /*
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
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.
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.
26 #include "config.h"
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"
34 namespace blink {
36 PendingScript::PendingScript()
37 : m_watchingForLoad(false)
38 , m_startingPosition(TextPosition::belowRangePosition())
42 PendingScript::PendingScript(Element* element, ScriptResource* resource)
43 : m_watchingForLoad(false)
44 , m_element(element)
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)
65 if (this == &other)
66 return *this;
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);
73 return *this;
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;
85 if (m_streamer) {
86 m_streamer->addClient(client);
87 } else {
88 resource()->addClient(client);
92 void PendingScript::stopWatchingForLoad(ScriptResourceClient* client)
94 if (!m_watchingForLoad)
95 return;
96 ASSERT(resource());
97 if (m_streamer) {
98 m_streamer->removeClient(client);
99 } else {
100 resource()->removeClient(client);
102 m_watchingForLoad = false;
105 void PendingScript::setElement(Element* element)
107 m_element = element;
110 PassRefPtrWillBeRawPtr<Element> PendingScript::releaseElementAndClear()
112 setScriptResource(0);
113 m_watchingForLoad = false;
114 m_startingPosition = TextPosition::belowRangePosition();
115 if (m_streamer)
116 m_streamer->cancel();
117 m_streamer.release();
118 return m_element.release();
121 void PendingScript::setScriptResource(ScriptResource* resource)
123 setResource(resource);
126 void PendingScript::notifyFinished(Resource* resource)
128 if (m_streamer)
129 m_streamer->notifyFinished(resource);
132 void PendingScript::notifyAppendData(ScriptResource* resource)
134 if (m_streamer)
135 m_streamer->notifyAppendData(resource);
138 DEFINE_TRACE(PendingScript)
140 visitor->trace(m_element);
141 visitor->trace(m_streamer);
144 ScriptSourceCode PendingScript::getSource(const KURL& documentURL, bool& errorOccurred) const
146 if (resource()) {
147 errorOccurred = resource()->errorOccurred();
148 ASSERT(resource()->isLoaded());
149 if (m_streamer && !m_streamer->streamingSuppressed())
150 return ScriptSourceCode(m_streamer, resource());
151 return ScriptSourceCode(resource());
153 errorOccurred = false;
154 return ScriptSourceCode(m_element->textContent(), documentURL, startingPosition());
157 void PendingScript::setStreamer(PassRefPtrWillBeRawPtr<ScriptStreamer> streamer)
159 ASSERT(!m_streamer);
160 ASSERT(!m_watchingForLoad);
161 m_streamer = streamer;
164 bool PendingScript::isReady() const
166 if (resource() && !resource()->isLoaded())
167 return false;
168 if (m_streamer && !m_streamer->isFinished())
169 return false;
170 return true;