author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 22 Jan 2010 10:32:13 +0200 | |
changeset 1 | ae9c8dab0e3e |
parent 0 | 1918ee327afb |
child 2 | 56cd8111b7f7 |
permissions | -rw-r--r-- |
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; |
|
1
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
|
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
if (!(d->m_user.isEmpty() || d->m_pass.isEmpty())) { |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
// If credentials were specified for this request, add them to the url, |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
// so that they will be passed to QNetworkRequest. |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
KURL urlWithCredentials(d->m_request.url()); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
urlWithCredentials.setUser(d->m_user); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
urlWithCredentials.setPass(d->m_pass); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
140 |
d->m_request.setURL(urlWithCredentials); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
} |
0 | 142 |
|
143 |
getInternal()->m_frame = static_cast<FrameLoaderClientQt*>(frame->loader()->client())->webFrame(); |
|
144 |
#if QT_VERSION < 0x040400 |
|
145 |
return QWebNetworkManager::self()->add(this, getInternal()->m_frame->page()->d->networkInterface); |
|
146 |
#else |
|
147 |
ResourceHandleInternal *d = getInternal(); |
|
148 |
d->m_job = new QNetworkReplyHandler(this, QNetworkReplyHandler::LoadMode(d->m_defersLoading)); |
|
149 |
return true; |
|
150 |
#endif |
|
151 |
} |
|
152 |
||
153 |
void ResourceHandle::cancel() |
|
154 |
{ |
|
155 |
#if QT_VERSION < 0x040400 |
|
156 |
QWebNetworkManager::self()->cancel(this); |
|
157 |
#else |
|
158 |
if (d->m_job) |
|
159 |
d->m_job->abort(); |
|
160 |
#endif |
|
161 |
} |
|
162 |
||
163 |
bool ResourceHandle::loadsBlocked() |
|
164 |
{ |
|
165 |
return false; |
|
166 |
} |
|
167 |
||
168 |
bool ResourceHandle::willLoadFromCache(ResourceRequest& request, Frame* frame) |
|
169 |
{ |
|
170 |
if (!frame) |
|
171 |
return false; |
|
172 |
||
173 |
#if QT_VERSION >= 0x040500 |
|
174 |
QNetworkAccessManager* manager = QWebFramePrivate::kit(frame)->page()->networkAccessManager(); |
|
175 |
QAbstractNetworkCache* cache = manager->cache(); |
|
176 |
||
177 |
if (!cache) |
|
178 |
return false; |
|
179 |
||
180 |
QNetworkCacheMetaData data = cache->metaData(request.url()); |
|
181 |
if (data.isValid()) { |
|
182 |
request.setCachePolicy(ReturnCacheDataDontLoad); |
|
183 |
return true; |
|
184 |
} |
|
185 |
||
186 |
return false; |
|
187 |
#else |
|
188 |
return false; |
|
189 |
#endif |
|
190 |
} |
|
191 |
||
192 |
bool ResourceHandle::supportsBufferedData() |
|
193 |
{ |
|
194 |
return false; |
|
195 |
} |
|
196 |
||
197 |
PassRefPtr<SharedBuffer> ResourceHandle::bufferedData() |
|
198 |
{ |
|
199 |
ASSERT_NOT_REACHED(); |
|
200 |
return 0; |
|
201 |
} |
|
202 |
||
203 |
void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data, Frame* frame) |
|
204 |
{ |
|
205 |
WebCoreSynchronousLoader syncLoader; |
|
206 |
ResourceHandle handle(request, &syncLoader, true, false, true); |
|
207 |
||
208 |
#if QT_VERSION < 0x040400 |
|
209 |
if (!QWebNetworkManager::self()->add(&handle, QWebNetworkInterface::defaultInterface(), QWebNetworkManager::SynchronousJob)) { |
|
210 |
// FIXME Create a sane ResourceError |
|
211 |
error = ResourceError(String(), -1, String(), String()); |
|
212 |
return; |
|
213 |
} |
|
214 |
#else |
|
215 |
ResourceHandleInternal *d = handle.getInternal(); |
|
1
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
if (!(d->m_user.isEmpty() || d->m_pass.isEmpty())) { |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
// If credentials were specified for this request, add them to the url, |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
// so that they will be passed to QNetworkRequest. |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
KURL urlWithCredentials(d->m_request.url()); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
urlWithCredentials.setUser(d->m_user); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
urlWithCredentials.setPass(d->m_pass); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
d->m_request.setURL(urlWithCredentials); |
ae9c8dab0e3e
Revision: 201001
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
} |
0 | 224 |
d->m_frame = static_cast<FrameLoaderClientQt*>(frame->loader()->client())->webFrame(); |
225 |
d->m_job = new QNetworkReplyHandler(&handle, QNetworkReplyHandler::LoadNormal); |
|
226 |
#endif |
|
227 |
||
228 |
syncLoader.waitForCompletion(); |
|
229 |
error = syncLoader.resourceError(); |
|
230 |
data = syncLoader.data(); |
|
231 |
response = syncLoader.resourceResponse(); |
|
232 |
} |
|
233 |
||
234 |
||
235 |
void ResourceHandle::setDefersLoading(bool defers) |
|
236 |
{ |
|
237 |
d->m_defersLoading = defers; |
|
238 |
||
239 |
#if QT_VERSION >= 0x040400 |
|
240 |
if (d->m_job) |
|
241 |
d->m_job->setLoadMode(QNetworkReplyHandler::LoadMode(defers)); |
|
242 |
#endif |
|
243 |
} |
|
244 |
||
245 |
} // namespace WebCore |