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;
47 ResourceHandleManager::ResourceHandleManager()
54 ResourceHandleManager::~ResourceHandleManager()
58 ResourceHandleManager* ResourceHandleManager::self()
61 s_self = new ResourceHandleManager();
66 void ResourceHandleManager::slotData(KIO::Job* kioJob, const QByteArray& data)
68 ResourceHandle* job = 0;
70 // Check if we know about 'kioJob'...
71 QMap<KIO::Job*, ResourceHandle*>::const_iterator it = m_kioToJobMap.find(kioJob);
72 if (it != m_kioToJobMap.end())
78 ResourceHandleInternal* d = job->getInternal();
79 if (!d || !d->m_client)
82 d->m_client->didReceiveData(job, data.data(), data.size());
85 void ResourceHandleManager::slotMimetype(KIO::Job* kioJob, const QString& type)
87 ResourceHandle* job = 0;
89 // Check if we know about 'kioJob'...
90 QMap<KIO::Job*, ResourceHandle*>::const_iterator it = m_kioToJobMap.find(kioJob);
91 if (it != m_kioToJobMap.end())
97 ResourceHandleInternal* d = job->getInternal();
98 if (!d || !d->m_client)
101 d->m_mimetype = type;
104 void ResourceHandleManager::slotResult(KJob* kjob)
106 KIO::Job* kioJob = qobject_cast<KIO::Job*>(kjob);
110 ResourceHandle* job = 0;
112 // Check if we know about 'kioJob'...
113 QMap<KIO::Job*, ResourceHandle*>::const_iterator it = m_kioToJobMap.find(kioJob);
114 if (it != m_kioToJobMap.end())
120 job->setError(kjob->error());
123 ASSERT(m_frameClient);
124 m_frameClient->checkLoaded();
127 void ResourceHandleManager::remove(ResourceHandle* job)
129 ResourceHandleInternal* d = job->getInternal();
130 if (!d || !d->m_client)
133 KIO::Job* kioJob = 0;
135 // Check if we know about 'job'...
136 QMap<ResourceHandle*, KIO::Job*>::const_iterator it = m_jobToKioMap.find(job);
137 if (it != m_jobToKioMap.end())
143 QString headers = kioJob->queryMetaData("HTTP-Headers");
144 if (job->method() == "GET")
145 d->m_charset = job->extractCharsetFromHeaders(headers);
146 else if (job->method() == "POST") {
147 // Will take care of informing our client...
148 // This must be called before didFinishLoading(),
149 // otherwhise assembleResponseHeaders() is called too early...
150 RefPtr<PlatformResponseQt> response(new PlatformResponseQt());
151 response->data = headers;
152 response->url = job->url().url();
154 job->receivedResponse(response);
157 d->m_client->receivedAllData(job, 0);
158 d->m_client->didFinishLoading(job);
160 m_jobToKioMap.remove(job);
161 m_kioToJobMap.remove(kioJob);
164 void ResourceHandleManager::add(ResourceHandle* job, FrameQtClient* frameClient)
166 ResourceHandleInternal* d = job->getInternal();
167 DeprecatedString url = d->m_request.url().url();
169 KIO::Job* kioJob = 0;
171 if (job->method() == "POST") {
172 DeprecatedString postData = job->postData().flattenToString().deprecatedString();
173 QByteArray postDataArray(postData.ascii(), postData.length());
175 kioJob = KIO::http_post(KUrl(url), postDataArray, false);
176 kioJob->addMetaData("PropagateHttpHeader", "true");
177 kioJob->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
179 kioJob = KIO::get(KUrl(url), false, false);
181 Q_ASSERT(kioJob != 0);
183 QObject::connect(kioJob, SIGNAL(data(KIO::Job*, const QByteArray&)), this, SLOT(slotData(KIO::Job*, const QByteArray&)));
184 QObject::connect(kioJob, SIGNAL(mimetype(KIO::Job*, const QString&)), this, SLOT(slotMimetype(KIO::Job*, const QString&)));
185 QObject::connect(kioJob, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
187 m_jobToKioMap.insert(job, kioJob);
188 m_kioToJobMap.insert(kioJob, job);
191 m_frameClient = frameClient;
193 ASSERT(m_frameClient == frameClient);
196 void ResourceHandleManager::cancel(ResourceHandle* job)
203 // Qt Resource Handle Manager
205 QtJob::QtJob(const QString& path)
211 void QtJob::timerEvent(QTimerEvent* e)
213 killTimer(e->timerId());
217 if (f.open(QIODevice::ReadOnly)) {
222 emit finished(this, data);
227 ResourceHandleManager::ResourceHandleManager()
232 ResourceHandleManager::~ResourceHandleManager()
236 ResourceHandleManager* ResourceHandleManager::self()
239 s_self = new 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"