summaryrefslogtreecommitdiffstats
path: root/src/core/net/url_request_custom_job_proxy.cpp
blob: 0f41a3670823e1e689bd8911dc2d77ff9d7e4138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (C) 2017 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "url_request_custom_job_proxy.h"
#include "url_request_custom_job_delegate.h"

#include "content/public/browser/browser_thread.h"
#include "net/base/net_errors.h"
#include "services/network/public/cpp/resource_request_body.h"

#include "api/qwebengineurlrequestjob.h"
#include "profile_adapter.h"
#include "type_conversion.h"
#include "web_engine_context.h"

namespace QtWebEngineCore {

URLRequestCustomJobProxy::URLRequestCustomJobProxy(URLRequestCustomJobProxy::Client *client,
                                                   const std::string &scheme,
                                                   QPointer<ProfileAdapter> profileAdapter)
    : m_client(client)
    , m_started(false)
    , m_scheme(scheme)
    , m_delegate(nullptr)
    , m_profileAdapter(profileAdapter)
    , m_ioTaskRunner(m_client->taskRunner())
{
    DCHECK(m_ioTaskRunner && m_ioTaskRunner->RunsTasksInCurrentSequence());
}

URLRequestCustomJobProxy::~URLRequestCustomJobProxy()
{
}

void URLRequestCustomJobProxy::release()
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    if (m_delegate) {
        m_delegate->deleteLater();
        m_delegate = nullptr;
    }
}

void URLRequestCustomJobProxy::reply(std::string contentType, QIODevice *device,
                                     QMultiMap<QByteArray, QByteArray> additionalResponseHeaders)
{
    if (!m_client)
        return;
    DCHECK (!m_ioTaskRunner || m_ioTaskRunner->RunsTasksInCurrentSequence());
    QByteArray qcontentType = QByteArray::fromStdString(contentType).toLower();
    const int sidx = qcontentType.indexOf(';');
    if (sidx > 0) {
        const int cidx = qcontentType.indexOf("charset=", sidx);
        if (cidx > 0) {
            m_client->m_charset = qcontentType.mid(cidx + 8).trimmed().toStdString();
            qcontentType = qcontentType.first(sidx);
        } else {
            qWarning() << "QWebEngineUrlRequestJob::reply(): Unrecognized content-type format with ';'" << qcontentType;
        }
    }
    m_client->m_mimeType = qcontentType.trimmed().toStdString();
    m_client->m_device = device;
    m_client->m_additionalResponseHeaders = std::move(additionalResponseHeaders);
    if (m_client->m_device && !m_client->m_device->isReadable())
        m_client->m_device->open(QIODevice::ReadOnly);

    if (m_client->m_device && m_client->m_firstBytePosition > 0)
        m_client->m_device->seek(m_client->m_firstBytePosition);

    qint64 deviceSize = m_client->m_device ? m_client->m_device->size() : -1;
    if (deviceSize > 0)
        m_client->notifyExpectedContentSize(deviceSize);

    if (m_client->m_device && m_client->m_device->isReadable()) {
        m_started = true;
        m_client->notifyHeadersComplete();
    } else {
        fail(net::ERR_INVALID_URL);
    }
}

void URLRequestCustomJobProxy::redirect(GURL url)
{
    if (!m_client)
        return;
    DCHECK (!m_ioTaskRunner || m_ioTaskRunner->RunsTasksInCurrentSequence());
    if (m_client->m_device || m_client->m_error)
        return;
    m_client->m_redirect = url;
    m_started = true;
    m_client->notifyHeadersComplete();
}

void URLRequestCustomJobProxy::abort()
{
    if (!m_client)
        return;
    DCHECK (!m_ioTaskRunner || m_ioTaskRunner->RunsTasksInCurrentSequence());
    if (m_client->m_device && m_client->m_device->isOpen())
        m_client->m_device->close();
    m_client->m_device = nullptr;
    if (m_started)
        m_client->notifyCanceled();
    else
        m_client->notifyAborted();
}

void URLRequestCustomJobProxy::fail(int error)
{
    if (!m_client)
        return;
    DCHECK (m_ioTaskRunner->RunsTasksInCurrentSequence());
    m_client->m_error = error;
    if (m_client->m_device)
        m_client->m_device->close();
    if (!m_started)
        m_client->notifyStartFailure(error);
    // else we fail on the next read, or the read that might already be in progress
}

void URLRequestCustomJobProxy::readyRead()
{
    DCHECK (m_ioTaskRunner->RunsTasksInCurrentSequence());
    if (m_client)
        m_client->notifyReadyRead();
}

void URLRequestCustomJobProxy::initialize(GURL url, std::string method,
                                          absl::optional<url::Origin> initiator,
                                          std::map<std::string, std::string> headers,
                                          scoped_refptr<network::ResourceRequestBody> requestBody)
{
    DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
    Q_ASSERT(!m_delegate);

    QUrl initiatorOrigin;
    if (initiator.has_value())
        initiatorOrigin = QUrl::fromEncoded(QByteArray::fromStdString(initiator.value().Serialize()));

    QWebEngineUrlSchemeHandler *schemeHandler = nullptr;

    if (m_profileAdapter)
        schemeHandler = m_profileAdapter->urlSchemeHandler(toQByteArray(m_scheme));
    QMap<QByteArray, QByteArray> qHeaders;
    for (auto it = headers.cbegin(); it != headers.cend(); ++it)
        qHeaders.insert(toQByteArray(it->first), toQByteArray(it->second));

    if (schemeHandler) {
        m_delegate =
                new URLRequestCustomJobDelegate(this, toQt(url), QByteArray::fromStdString(method),
                                                initiatorOrigin, qHeaders, requestBody.get());
        QWebEngineUrlRequestJob *requestJob = new QWebEngineUrlRequestJob(m_delegate);
        schemeHandler->requestStarted(requestJob);
    }
}

} // namespace