2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include "ResourceHandleManager.h"
39 #include "ResourceHandleInternal.h"
43 static ResourceHandleManager* s_self = 0;
46 ResourceHandleManager* ResourceHandleManager::self()
49 s_self = new ResourceHandleManager();
54 QtJob::QtJob(const QString& path)
60 void QtJob::timerEvent(QTimerEvent* e)
62 killTimer(e->timerId());
66 if (f.open(QIODevice::ReadOnly)) {
71 emit finished(this, data);
77 ResourceHandleManager::ResourceHandleManager()
84 ResourceHandleManager::~ResourceHandleManager()
89 void ResourceHandleManager::deliverJobData(QtJob*, const QByteArray&)
93 void ResourceHandleManager::slotData(KIO::Job* kioJob, const QByteArray& data)
95 ResourceHandle* job = 0;
97 // Check if we know about 'kioJob'...
98 QMap<KIO::Job*, ResourceHandle*>::const_iterator it = m_kioToJobMap.find(kioJob);
99 if (it != m_kioToJobMap.end())
105 ResourceHandleInternal* d = job->getInternal();
106 if (!d || !d->m_client)
109 d->m_client->didReceiveData(job, data.data(), data.size());
112 void ResourceHandleManager::slotMimetype(KIO::Job* kioJob, const QString& type)
114 ResourceHandle* job = 0;
116 // Check if we know about 'kioJob'...
117 QMap<KIO::Job*, ResourceHandle*>::const_iterator it = m_kioToJobMap.find(kioJob);
118 if (it != m_kioToJobMap.end())
124 ResourceHandleInternal* d = job->getInternal();
125 if (!d || !d->m_client)
128 d->m_mimetype = type;
131 void ResourceHandleManager::slotResult(KJob* kjob)
133 KIO::Job* kioJob = qobject_cast<KIO::Job*>(kjob);
137 ResourceHandle* job = 0;
139 // Check if we know about 'kioJob'...
140 QMap<KIO::Job*, ResourceHandle*>::const_iterator it = m_kioToJobMap.find(kioJob);
141 if (it != m_kioToJobMap.end())
147 //FIXME: should report an error
148 //job->setError(kjob->error());
151 ASSERT(m_frameClient);
152 m_frameClient->checkLoaded();
155 void ResourceHandleManager::remove(ResourceHandle* job)
157 ResourceHandleInternal* d = job->getInternal();
158 if (!d || !d->m_client)
161 KIO::Job* kioJob = 0;
163 // Check if we know about 'job'...
164 QMap<ResourceHandle*, KIO::Job*>::const_iterator it = m_jobToKioMap.find(job);
165 if (it != m_jobToKioMap.end())
171 QString headers = kioJob->queryMetaData("HTTP-Headers");
172 if (job->method() == "GET")
173 d->m_charset = job->extractCharsetFromHeaders(headers);
174 else if (job->method() == "POST") {
175 // Will take care of informing our client...
176 // This must be called before didFinishLoading(),
177 // otherwhise assembleResponseHeaders() is called too early...
178 RefPtr<PlatformResponseQt> response(new PlatformResponseQt());
179 response->data = headers;
180 response->url = job->url().url();
182 job->receivedResponse(response);
185 d->m_client->receivedAllData(job, 0);
186 d->m_client->didFinishLoading(job);
188 m_jobToKioMap.remove(job);
189 m_kioToJobMap.remove(kioJob);
192 void ResourceHandleManager::add(ResourceHandle* job, FrameQtClient* frameClient)
194 ResourceHandleInternal* d = job->getInternal();
195 DeprecatedString url = d->m_request.url().url();
197 KIO::Job* kioJob = 0;
199 if (job->method() == "POST") {
200 ASSERT(job->postData());
201 DeprecatedString postData = job->postData()->flattenToString().deprecatedString();
202 QByteArray postDataArray(postData.ascii(), postData.length());
204 kioJob = KIO::http_post(KUrl(url), postDataArray, false);
205 kioJob->addMetaData("PropagateHttpHeader", "true");
206 kioJob->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
208 kioJob = KIO::get(KUrl(url), false, false);
210 Q_ASSERT(kioJob != 0);
212 QObject::connect(kioJob, SIGNAL(data(KIO::Job*, const QByteArray&)), this, SLOT(slotData(KIO::Job*, const QByteArray&)));
213 QObject::connect(kioJob, SIGNAL(mimetype(KIO::Job*, const QString&)), this, SLOT(slotMimetype(KIO::Job*, const QString&)));
214 QObject::connect(kioJob, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
216 m_jobToKioMap.insert(job, kioJob);
217 m_kioToJobMap.insert(kioJob, job);
220 m_frameClient = frameClient;
222 ASSERT(m_frameClient == frameClient);
225 void ResourceHandleManager::cancel(ResourceHandle* job)
228 //FIXME set an error state
232 // Qt Resource Handle Manager
234 ResourceHandleManager::ResourceHandleManager()
239 ResourceHandleManager::~ResourceHandleManager()
244 void ResourceHandleManager::remove(ResourceHandle* job)
246 ResourceHandleInternal* d = job->getInternal();
247 if (!d || !d->m_client)
250 // Check if we know about 'job'...
251 QtJob *qtJob = m_resourceToJob.value(job);
255 d->m_client->receivedAllData(job, 0);
256 d->m_client->didFinishLoading(job);
258 m_resourceToJob.remove(job);
259 m_jobToResource.remove(qtJob);
262 void ResourceHandleManager::add(ResourceHandle* resource, FrameQtClient* frameClient)
264 ResourceHandleInternal* d = resource->getInternal();
266 if (resource->method() == "POST"
267 || !d->m_request.url().isLocalFile()) {
268 // ### not supported for the local filesystem
271 QtJob* qtJob = new QtJob(d->m_request.url().path());
272 connect(qtJob, SIGNAL(finished(QtJob *, const QByteArray &)),
273 this, SLOT(deliverJobData(QtJob *, const QByteArray &)));
275 m_resourceToJob.insert(resource, qtJob);
276 m_jobToResource.insert(qtJob, resource);
279 m_frameClient = frameClient;
281 ASSERT(m_frameClient == frameClient);
284 void ResourceHandleManager::cancel(ResourceHandle* job)
290 void ResourceHandleManager::deliverJobData(QtJob* job, const QByteArray& data)
292 ResourceHandle* handle = m_jobToResource.value(job);
296 ResourceHandleInternal* d = handle->getInternal();
297 if (!d || !d->m_client)
300 d->m_client->didReceiveData(handle, data.data(), data.size());
305 ASSERT(m_frameClient);
306 m_frameClient->checkLoaded();
311 } // namespace WebCore
313 #include "ResourceHandleManager.moc"