void QQuickNetworkReply::send()
{
    if (m_data.isNull())
        return;

    uint64_t smLength = 0;
    const void* ptrData = 0;
    QString stringData;
    QByteArray byteArrayData;
    if (m_data.type() == QVariant::String) {
        stringData = m_data.toString();
        ptrData = reinterpret_cast<const void*>(stringData.constData());
        smLength = sizeof(QChar) * stringData.length();
        setContentType(QLatin1String("text/html; charset=utf-16"));
    } else {
        if (!m_data.canConvert<QByteArray>())
            return;
        byteArrayData = m_data.toByteArray();
        ptrData = byteArrayData.data();
        smLength = byteArrayData.size();
    }

    if (contentType().isEmpty()) {
        qWarning("QQuickNetworkReply::send - Cannot send raw data without a content type being specified!");
        return;
    }

    WTF::RefPtr<WebKit::SharedMemory> sharedMemory = SharedMemory::create(smLength);
    if (!sharedMemory)
        return;
    // The size of the allocated shared memory can be bigger than requested.
    // Usually the size will be rounded up to the next multiple of a page size.
    memcpy(sharedMemory->data(), ptrData, smLength);

    if (sharedMemory->createHandle(m_networkReplyData->data().m_dataHandle, SharedMemory::ReadOnly)) {
        m_networkReplyData->data().m_contentLength = smLength;
        if (m_webViewExperimental)
            m_webViewExperimental.data()->sendApplicationSchemeReply(this);
    }

    // After sending the reply data, we have to reinitialize the m_networkReplyData,
    // to make sure we have a fresh SharesMemory::Handle.
    m_networkReplyData = adoptRef(new WebKit::QtRefCountedNetworkReplyData);
}
Ejemplo n.º 2
0
void QQuickNetworkReply::send()
{
    if (m_data.isNull())
        return;

    uint64_t smLength = 0;
    const void* ptrData = 0;
    QString stringData;
    QByteArray byteArrayData;
    if (m_data.type() == QVariant::String) {
        stringData = m_data.toString();
        ptrData = reinterpret_cast<const void*>(stringData.constData());
        smLength = sizeof(QChar) * stringData.length();
    } else {
        if (!m_data.canConvert<QByteArray>())
            return;
        byteArrayData = m_data.toByteArray();
        ptrData = byteArrayData.data();
        smLength = byteArrayData.size();
    }

    WTF::RefPtr<WebKit::SharedMemory> sharedMemory = SharedMemory::create(smLength);
    if (!sharedMemory)
        return;
    // The size of the allocated shared memory can be bigger than requested.
    // Usually the size will be rounded up to the next multiple of a page size.
    memcpy(sharedMemory->data(), ptrData, smLength);

    if (sharedMemory->createHandle(m_networkReplyData->data().m_dataHandle, SharedMemory::ReadOnly)) {
        m_networkReplyData->data().m_contentLength = smLength;

        QObject* schemeParent = parent()->parent();
        if (schemeParent) {
            QQuickWebViewExperimental* webViewExperimental = qobject_cast<QQuickWebViewExperimental*>(schemeParent->parent());
            if (webViewExperimental)
                webViewExperimental->sendApplicationSchemeReply(this);
        }
    }

    // After sending the reply data, we have to reinitialize the m_networkReplyData,
    // to make sure we have a fresh SharesMemory::Handle.
    m_networkReplyData = adoptRef(new WebKit::QtRefCountedNetworkReplyData);
}