38ac7b909dbb9ed6b39c83a17cff81355fec88d0
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / qt / qquicknetworkreply.cpp
1 /*
2  * Copyright (C) 2011 Zeno Albisser <zeno@webkit.org>
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 "qquicknetworkreply_p.h"
23
24 #include "QtNetworkReplyData.h"
25 #include "QtNetworkRequestData.h"
26 #include "qquickwebview_p.h"
27 #include <QDateTime>
28
29 using namespace WebKit;
30
31 QQuickNetworkReply::QQuickNetworkReply(QObject* parent)
32     : QObject(parent)
33     , m_networkReplyData(adoptRef(new WebKit::QtRefCountedNetworkReplyData))
34 {
35     Q_ASSERT(parent);
36 }
37
38 QString QQuickNetworkReply::contentType() const
39 {
40     return m_networkReplyData->data().m_contentType;
41 }
42
43 void QQuickNetworkReply::setContentType(const QString& contentType)
44 {
45     m_networkReplyData->data().m_contentType = contentType;
46 }
47
48 QVariant QQuickNetworkReply::data() const
49 {
50     return m_data;
51 }
52
53 void QQuickNetworkReply::setData(const QVariant& data)
54 {
55     m_data = data;
56 }
57
58 void QQuickNetworkReply::send()
59 {
60     if (m_data.isNull())
61         return;
62
63     uint64_t smLength = 0;
64     const void* ptrData = 0;
65     QString stringData;
66     QByteArray byteArrayData;
67     if (m_data.type() == QVariant::String) {
68         stringData = m_data.toString();
69         ptrData = reinterpret_cast<const void*>(stringData.constData());
70         smLength = sizeof(QChar) * stringData.length();
71         setContentType(QLatin1String("text/html; charset=utf-16"));
72     } else {
73         if (!m_data.canConvert<QByteArray>())
74             return;
75         byteArrayData = m_data.toByteArray();
76         ptrData = byteArrayData.data();
77         smLength = byteArrayData.size();
78     }
79
80     if (contentType().isEmpty()) {
81         qWarning("QQuickNetworkReply::send - Cannot send raw data without a content type being specified!");
82         return;
83     }
84
85     WTF::RefPtr<WebKit::SharedMemory> sharedMemory = SharedMemory::create(smLength);
86     if (!sharedMemory)
87         return;
88     // The size of the allocated shared memory can be bigger than requested.
89     // Usually the size will be rounded up to the next multiple of a page size.
90     memcpy(sharedMemory->data(), ptrData, smLength);
91
92     if (sharedMemory->createHandle(m_networkReplyData->data().m_dataHandle, SharedMemory::ReadOnly)) {
93         m_networkReplyData->data().m_contentLength = smLength;
94         if (m_webViewExperimental)
95             m_webViewExperimental.data()->sendApplicationSchemeReply(this);
96     }
97
98     // After sending the reply data, we have to reinitialize the m_networkReplyData,
99     // to make sure we have a fresh SharesMemory::Handle.
100     m_networkReplyData = adoptRef(new WebKit::QtRefCountedNetworkReplyData);
101 }
102
103 void QQuickNetworkReply::setWebViewExperimental(QQuickWebViewExperimental* webViewExperimental)
104 {
105     m_webViewExperimental = webViewExperimental;
106 }
107
108 WebKit::QtRefCountedNetworkRequestData* QQuickNetworkReply::networkRequestData() const
109 {
110     return m_networkRequestData.get();
111 }
112
113 void QQuickNetworkReply::setNetworkRequestData(WTF::PassRefPtr<WebKit::QtRefCountedNetworkRequestData> data)
114 {
115     m_networkRequestData = data;
116     m_networkReplyData->data().m_replyUuid = m_networkRequestData->data().m_replyUuid;
117 }
118
119 WebKit::QtRefCountedNetworkReplyData* QQuickNetworkReply::networkReplyData() const
120 {
121     return m_networkReplyData.get();
122 }
123
124 #include "moc_qquicknetworkreply_p.cpp"