[Qt][WK2] Move non-api classes to WebKit namespace at WebKit2/UiProcess/qt
[WebKit-https.git] / Source / WebKit2 / UIProcess / qt / QtDownloadManager.cpp
1 /*
2  * Copyright (C) 2010, 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  */
20
21 #include "config.h"
22 #include "QtDownloadManager.h"
23
24 #include "DownloadProxy.h"
25 #include "QtWebError.h"
26 #include "WKStringQt.h"
27 #include "WKURLQt.h"
28 #include "WebContext.h"
29 #include "qwebdownloaditem_p.h"
30 #include "qwebdownloaditem_p_p.h"
31
32 namespace WebKit {
33
34 QtDownloadManager::QtDownloadManager(WebContext* context)
35 {
36     WKContextDownloadClient downloadClient;
37     memset(&downloadClient, 0, sizeof(WKContextDownloadClient));
38     downloadClient.version = kWKContextDownloadClientCurrentVersion;
39     downloadClient.clientInfo = this;
40     downloadClient.didReceiveResponse = didReceiveResponse;
41     downloadClient.didReceiveData = didReceiveDataForDownload;
42     downloadClient.didCreateDestination = didCreateDestination;
43     downloadClient.didFinish = didFinishDownload;
44     downloadClient.didFail = didFailDownload;
45     WKContextSetDownloadClient(toAPI(context), &downloadClient);
46 }
47
48 QtDownloadManager::~QtDownloadManager()
49 {
50 }
51
52 void QtDownloadManager::addDownload(DownloadProxy* download, QWebDownloadItem* downloadItem)
53 {
54     m_downloads[download->downloadID()] = downloadItem;
55 }
56
57 void QtDownloadManager::downloadReceivedResponse(DownloadProxy* download, const WebCore::ResourceResponse& response)
58 {
59     // Will be called when the headers are read by WebProcess.
60     QWebDownloadItem* downloadItem = m_downloads.value(download->downloadID());
61     ASSERT(downloadItem);
62
63     downloadItem->d->sourceUrl = response.url();
64     downloadItem->d->mimeType = response.mimeType();
65     downloadItem->d->expectedContentLength = response.expectedContentLength();
66     downloadItem->d->suggestedFilename = response.suggestedFilename();
67
68     downloadItem->d->didReceiveResponse(downloadItem);
69 }
70
71 void QtDownloadManager::downloadCreatedDestination(DownloadProxy* download, const QString& path)
72 {
73     QWebDownloadItem* downloadItem = m_downloads.value(download->downloadID());
74     ASSERT(downloadItem);
75     downloadItem->d->destinationPath = path;
76     emit downloadItem->destinationFileCreated(downloadItem->d->destinationPath);
77 }
78
79 void QtDownloadManager::downloadFinished(DownloadProxy* download)
80 {
81     // Will be called when download finishes with success.
82     QWebDownloadItem* downloadItem = m_downloads.take(download->downloadID());
83     ASSERT(downloadItem);
84     emit downloadItem->succeeded();
85 }
86
87 void QtDownloadManager::downloadFailed(DownloadProxy* download, const QtWebError& error)
88 {
89     // Will be called when download fails or is aborted.
90     QWebDownloadItem* downloadItem = m_downloads.take(download->downloadID());
91     ASSERT(downloadItem);
92
93     // If the parent is null at this point, the download failed before it
94     // received a response and downloadRequested was emitted.
95     // Due to this the item will never be parented and we have to delete it
96     // manually at this point.
97     if (!downloadItem->parent()) {
98         delete downloadItem;
99         return;
100     }
101
102     emit downloadItem->failed(error.errorCodeAsDownloadError(), error.url(), error.description());
103 }
104
105 void QtDownloadManager::downloadDataReceived(DownloadProxy* download, uint64_t length)
106 {
107     // Will be called everytime bytes were written to destination file by WebProcess.
108     QWebDownloadItem* downloadItem = m_downloads.value(download->downloadID());
109     ASSERT(downloadItem);
110     downloadItem->d->totalBytesReceived += length;
111     emit downloadItem->totalBytesReceivedChanged(length);
112 }
113
114 static inline QtDownloadManager* toQtDownloadManager(const void* clientInfo)
115 {
116     ASSERT(clientInfo);
117     return reinterpret_cast<QtDownloadManager*>(const_cast<void*>(clientInfo));
118 }
119
120 void QtDownloadManager::didReceiveResponse(WKContextRef, WKDownloadRef download, WKURLResponseRef response, const void *clientInfo)
121 {
122     toQtDownloadManager(clientInfo)->downloadReceivedResponse(toImpl(download), toImpl(response)->resourceResponse());
123 }
124
125 void QtDownloadManager::didCreateDestination(WKContextRef, WKDownloadRef download, WKStringRef path, const void *clientInfo)
126 {
127     toQtDownloadManager(clientInfo)->downloadCreatedDestination(toImpl(download), WKStringCopyQString(path));
128 }
129
130 void QtDownloadManager::didFinishDownload(WKContextRef, WKDownloadRef download, const void *clientInfo)
131 {
132     toQtDownloadManager(clientInfo)->downloadFinished(toImpl(download));
133 }
134
135 void QtDownloadManager::didFailDownload(WKContextRef, WKDownloadRef download, WKErrorRef error, const void *clientInfo)
136 {
137     toQtDownloadManager(clientInfo)->downloadFailed(toImpl(download), QtWebError(error));
138 }
139
140 void QtDownloadManager::didReceiveDataForDownload(WKContextRef, WKDownloadRef download, uint64_t length, const void *clientInfo)
141 {
142     toQtDownloadManager(clientInfo)->downloadDataReceived(toImpl(download), length);
143 }
144
145 } // namespace WebKit