summaryrefslogtreecommitdiffstats
path: root/src/network/kernel/qnetworkproxy_android.cpp
blob: d5b56bba865b938636f4957c7e85887790dc344f (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
// Copyright (C) 2021 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 "qnetworkproxy.h"

#include <QtCore/qcoreapplication_platform.h>
#include <QtCore/qjnienvironment.h>
#include <QtCore/qjniobject.h>

#ifndef QT_NO_NETWORKPROXY

QT_BEGIN_NAMESPACE

struct ProxyInfoObject
{
public:
    ProxyInfoObject();
    ~ProxyInfoObject();
};

using namespace QNativeInterface;
using namespace QtJniTypes;

Q_GLOBAL_STATIC(ProxyInfoObject, proxyInfoInstance)

Q_DECLARE_JNI_CLASS(QtNetwork, "org/qtproject/qt/android/network/QtNetwork")
Q_DECLARE_JNI_CLASS(ProxyInfo, "android/net/ProxyInfo")

ProxyInfoObject::ProxyInfoObject()
{
    QtNetwork::callStaticMethod<void>("registerReceiver", QAndroidApplication::context());
}

ProxyInfoObject::~ProxyInfoObject()
{
    QtNetwork::callStaticMethod<void>("unregisterReceiver", QAndroidApplication::context());
}

QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query)
{
    QList<QNetworkProxy> proxyList;
    if (!proxyInfoInstance)
        return proxyList;

    QJniObject proxyInfo = QtNetwork::callStaticMethod<ProxyInfo>("getProxyInfo",
                                                                  QAndroidApplication::context());
    if (proxyInfo.isValid()) {
        const QJniArray exclusionList = proxyInfo.callMethod<String[]>("getExclusionList");
        bool exclude = false;
        if (exclusionList.isValid()) {
            const QUrl host = QUrl(query.url().host());
            for (const auto &entry : exclusionList) {
                if (host.matches(QUrl(entry.toString()), QUrl::RemoveScheme)) {
                    exclude = true;
                    break;
                }
            }
        }
        if (!exclude) {
            const QString hostName = proxyInfo.callMethod<QString>("getHost");
            const int port = proxyInfo.callMethod<jint>("getPort");
            QNetworkProxy proxy(QNetworkProxy::HttpProxy, hostName, port);
            proxyList << proxy;
        }
    }
    if (proxyList.isEmpty())
        proxyList << QNetworkProxy::NoProxy;

    return proxyList;
}

QT_END_NAMESPACE

#endif