0
|
1 |
/*
|
|
2 |
* Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
|
|
3 |
* Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
|
|
4 |
* Copyright (C) 2008 Holger Hans Peter Freyther
|
|
5 |
*
|
|
6 |
* All rights reserved.
|
|
7 |
*
|
|
8 |
* Redistribution and use in source and binary forms, with or without
|
|
9 |
* modification, are permitted provided that the following conditions
|
|
10 |
* are met:
|
|
11 |
* 1. Redistributions of source code must retain the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer.
|
|
13 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer in the
|
|
15 |
* documentation and/or other materials provided with the distribution.
|
|
16 |
*
|
|
17 |
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
|
|
18 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
19 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
|
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
24 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
25 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
28 |
*/
|
|
29 |
|
|
30 |
#include "config.h"
|
|
31 |
|
|
32 |
#include "Frame.h"
|
|
33 |
#include "DocLoader.h"
|
|
34 |
#include "ResourceHandle.h"
|
|
35 |
#include "ResourceHandleClient.h"
|
|
36 |
#include "ResourceHandleInternal.h"
|
|
37 |
#include "qwebpage_p.h"
|
|
38 |
#include "qwebframe_p.h"
|
|
39 |
#include "ChromeClientQt.h"
|
|
40 |
#include "FrameLoaderClientQt.h"
|
|
41 |
#include "Page.h"
|
|
42 |
#include "QNetworkReplyHandler.h"
|
|
43 |
|
|
44 |
#include "NotImplemented.h"
|
|
45 |
|
|
46 |
#if QT_VERSION >= 0x040500
|
|
47 |
#include <QAbstractNetworkCache>
|
|
48 |
#endif
|
|
49 |
#include <QCoreApplication>
|
|
50 |
#include <QUrl>
|
|
51 |
#if QT_VERSION >= 0x040400
|
|
52 |
#include <QNetworkAccessManager>
|
|
53 |
#include <QNetworkRequest>
|
|
54 |
#include <QNetworkReply>
|
|
55 |
#else
|
|
56 |
#include "qwebnetworkinterface_p.h"
|
|
57 |
#endif
|
|
58 |
|
|
59 |
namespace WebCore {
|
|
60 |
|
|
61 |
class WebCoreSynchronousLoader : public ResourceHandleClient {
|
|
62 |
public:
|
|
63 |
WebCoreSynchronousLoader();
|
|
64 |
|
|
65 |
void waitForCompletion();
|
|
66 |
|
|
67 |
virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
|
|
68 |
virtual void didReceiveData(ResourceHandle*, const char*, int, int lengthReceived);
|
|
69 |
virtual void didFinishLoading(ResourceHandle*);
|
|
70 |
virtual void didFail(ResourceHandle*, const ResourceError&);
|
|
71 |
|
|
72 |
ResourceResponse resourceResponse() const { return m_response; }
|
|
73 |
ResourceError resourceError() const { return m_error; }
|
|
74 |
Vector<char> data() const { return m_data; }
|
|
75 |
|
|
76 |
private:
|
|
77 |
ResourceResponse m_response;
|
|
78 |
ResourceError m_error;
|
|
79 |
Vector<char> m_data;
|
|
80 |
QEventLoop m_eventLoop;
|
|
81 |
};
|
|
82 |
|
|
83 |
WebCoreSynchronousLoader::WebCoreSynchronousLoader()
|
|
84 |
{
|
|
85 |
}
|
|
86 |
|
|
87 |
void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
|
|
88 |
{
|
|
89 |
m_response = response;
|
|
90 |
}
|
|
91 |
|
|
92 |
void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
|
|
93 |
{
|
|
94 |
m_data.append(data, length);
|
|
95 |
}
|
|
96 |
|
|
97 |
void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*)
|
|
98 |
{
|
|
99 |
m_eventLoop.exit();
|
|
100 |
}
|
|
101 |
|
|
102 |
void WebCoreSynchronousLoader::didFail(ResourceHandle*, const ResourceError& error)
|
|
103 |
{
|
|
104 |
m_error = error;
|
|
105 |
m_eventLoop.exit();
|
|
106 |
}
|
|
107 |
|
|
108 |
void WebCoreSynchronousLoader::waitForCompletion()
|
|
109 |
{
|
|
110 |
m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
|
|
111 |
}
|
|
112 |
|
|
113 |
ResourceHandleInternal::~ResourceHandleInternal()
|
|
114 |
{
|
|
115 |
}
|
|
116 |
|
|
117 |
ResourceHandle::~ResourceHandle()
|
|
118 |
{
|
|
119 |
if (d->m_job)
|
|
120 |
cancel();
|
|
121 |
}
|
|
122 |
|
|
123 |
bool ResourceHandle::start(Frame* frame)
|
|
124 |
{
|
|
125 |
if (!frame)
|
|
126 |
return false;
|
|
127 |
|
|
128 |
Page *page = frame->page();
|
|
129 |
// If we are no longer attached to a Page, this must be an attempted load from an
|
|
130 |
// onUnload handler, so let's just block it.
|
|
131 |
if (!page)
|
|
132 |
return false;
|
|
133 |
|
|
134 |
getInternal()->m_frame = static_cast<FrameLoaderClientQt*>(frame->loader()->client())->webFrame();
|
|
135 |
#if QT_VERSION < 0x040400
|
|
136 |
return QWebNetworkManager::self()->add(this, getInternal()->m_frame->page()->d->networkInterface);
|
|
137 |
#else
|
|
138 |
ResourceHandleInternal *d = getInternal();
|
|
139 |
d->m_job = new QNetworkReplyHandler(this, QNetworkReplyHandler::LoadMode(d->m_defersLoading));
|
|
140 |
return true;
|
|
141 |
#endif
|
|
142 |
}
|
|
143 |
|
|
144 |
void ResourceHandle::cancel()
|
|
145 |
{
|
|
146 |
#if QT_VERSION < 0x040400
|
|
147 |
QWebNetworkManager::self()->cancel(this);
|
|
148 |
#else
|
|
149 |
if (d->m_job)
|
|
150 |
d->m_job->abort();
|
|
151 |
#endif
|
|
152 |
}
|
|
153 |
|
|
154 |
bool ResourceHandle::loadsBlocked()
|
|
155 |
{
|
|
156 |
return false;
|
|
157 |
}
|
|
158 |
|
|
159 |
bool ResourceHandle::willLoadFromCache(ResourceRequest& request, Frame* frame)
|
|
160 |
{
|
|
161 |
if (!frame)
|
|
162 |
return false;
|
|
163 |
|
|
164 |
#if QT_VERSION >= 0x040500
|
|
165 |
QNetworkAccessManager* manager = QWebFramePrivate::kit(frame)->page()->networkAccessManager();
|
|
166 |
QAbstractNetworkCache* cache = manager->cache();
|
|
167 |
|
|
168 |
if (!cache)
|
|
169 |
return false;
|
|
170 |
|
|
171 |
QNetworkCacheMetaData data = cache->metaData(request.url());
|
|
172 |
if (data.isValid()) {
|
|
173 |
request.setCachePolicy(ReturnCacheDataDontLoad);
|
|
174 |
return true;
|
|
175 |
}
|
|
176 |
|
|
177 |
return false;
|
|
178 |
#else
|
|
179 |
return false;
|
|
180 |
#endif
|
|
181 |
}
|
|
182 |
|
|
183 |
bool ResourceHandle::supportsBufferedData()
|
|
184 |
{
|
|
185 |
return false;
|
|
186 |
}
|
|
187 |
|
|
188 |
PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
|
|
189 |
{
|
|
190 |
ASSERT_NOT_REACHED();
|
|
191 |
return 0;
|
|
192 |
}
|
|
193 |
|
|
194 |
void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data, Frame* frame)
|
|
195 |
{
|
|
196 |
WebCoreSynchronousLoader syncLoader;
|
|
197 |
ResourceHandle handle(request, &syncLoader, true, false, true);
|
|
198 |
|
|
199 |
#if QT_VERSION < 0x040400
|
|
200 |
if (!QWebNetworkManager::self()->add(&handle, QWebNetworkInterface::defaultInterface(), QWebNetworkManager::SynchronousJob)) {
|
|
201 |
// FIXME Create a sane ResourceError
|
|
202 |
error = ResourceError(String(), -1, String(), String());
|
|
203 |
return;
|
|
204 |
}
|
|
205 |
#else
|
|
206 |
ResourceHandleInternal *d = handle.getInternal();
|
|
207 |
d->m_frame = static_cast<FrameLoaderClientQt*>(frame->loader()->client())->webFrame();
|
|
208 |
d->m_job = new QNetworkReplyHandler(&handle, QNetworkReplyHandler::LoadNormal);
|
|
209 |
#endif
|
|
210 |
|
|
211 |
syncLoader.waitForCompletion();
|
|
212 |
error = syncLoader.resourceError();
|
|
213 |
data = syncLoader.data();
|
|
214 |
response = syncLoader.resourceResponse();
|
|
215 |
}
|
|
216 |
|
|
217 |
|
|
218 |
void ResourceHandle::setDefersLoading(bool defers)
|
|
219 |
{
|
|
220 |
d->m_defersLoading = defers;
|
|
221 |
|
|
222 |
#if QT_VERSION >= 0x040400
|
|
223 |
if (d->m_job)
|
|
224 |
d->m_job->setLoadMode(QNetworkReplyHandler::LoadMode(defers));
|
|
225 |
#endif
|
|
226 |
}
|
|
227 |
|
|
228 |
} // namespace WebCore
|