Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / PendingScript.cpp
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     : m_watchingForLoad(false)
38     , m_startingPosition(TextPosition::belowRangePosition())
39 {
40 }
41
42 PendingScript::PendingScript(Element* element, ScriptResource* resource)
43     : m_watchingForLoad(false)
44     , m_element(element)
45 {
46     setScriptResource(resource);
47 }
48
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)
55 {
56     setScriptResource(other.resource());
57 }
58
59 PendingScript::~PendingScript()
60 {
61 }
62
63 PendingScript& PendingScript::operator=(const PendingScript& other)
64 {
65     if (this == &other)
66         return *this;
67
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;
74 }
75
76 void PendingScript::watchForLoad(ScriptResourceClient* client)
77 {
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);
89     }
90 }
91
92 void PendingScript::stopWatchingForLoad(ScriptResourceClient* client)
93 {
94     if (!m_watchingForLoad)
95         return;
96     ASSERT(resource());
97     if (m_streamer) {
98         m_streamer->removeClient(client);
99     } else {
100         resource()->removeClient(client);
101     }
102     m_watchingForLoad = false;
103 }
104
105 PassRefPtrWillBeRawPtr<Element> PendingScript::releaseElementAndClear()
106 {
107     setScriptResource(0);
108     m_watchingForLoad = false;
109     m_startingPosition = TextPosition::belowRangePosition();
110     if (m_streamer)
111         m_streamer->cancel();
112     m_streamer.release();
113     return m_element.release();
114 }
115
116 void PendingScript::setScriptResource(ScriptResource* resource)
117 {
118     setResource(resource);
119 }
120
121 void PendingScript::notifyFinished(Resource* resource)
122 {
123     if (m_streamer)
124         m_streamer->notifyFinished(resource);
125 }
126
127 void PendingScript::notifyAppendData(ScriptResource* resource)
128 {
129     if (m_streamer)
130         m_streamer->notifyAppendData(resource);
131 }
132
133 void PendingScript::trace(Visitor* visitor)
134 {
135     visitor->trace(m_element);
136 }
137
138 ScriptSourceCode PendingScript::getSource(const KURL& documentURL, bool& errorOccurred) const
139 {
140     if (resource()) {
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());
146     }
147     errorOccurred = false;
148     return ScriptSourceCode(m_element->textContent(), documentURL, startingPosition());
149 }
150
151 void PendingScript::setStreamer(PassRefPtr<ScriptStreamer> streamer)
152 {
153     ASSERT(!m_streamer);
154     ASSERT(!m_watchingForLoad);
155     m_streamer = streamer;
156 }
157
158 bool PendingScript::isReady() const
159 {
160     if (resource() && !resource()->isLoaded())
161         return false;
162     if (m_streamer && !m_streamer->isFinished())
163         return false;
164     return true;
165 }
166
167 }