Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnetworkproxy_android.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qapplicationstatic.h>
8#include <QtCore/qcoreapplication_platform.h>
9#include <QtCore/qjnienvironment.h>
10#include <QtCore/qjniobject.h>
11
12#ifndef QT_NO_NETWORKPROXY
13
14QT_BEGIN_NAMESPACE
15
16struct ProxyInfoObject
17{
18public:
19 ProxyInfoObject();
20 ~ProxyInfoObject();
21};
22
23using namespace QNativeInterface;
24using namespace QtJniTypes;
25
26Q_APPLICATION_STATIC(ProxyInfoObject, proxyInfoInstance)
27
28Q_DECLARE_JNI_CLASS(QtNetwork, "org/qtproject/qt/android/network/QtNetwork")
29Q_DECLARE_JNI_CLASS(ProxyInfo, "android/net/ProxyInfo")
30
31ProxyInfoObject::ProxyInfoObject()
32{
33 QtNetwork::callStaticMethod<void>("registerReceiver", QAndroidApplication::context());
34}
35
36ProxyInfoObject::~ProxyInfoObject()
37{
38 QtNetwork::callStaticMethod<void>("unregisterReceiver", QAndroidApplication::context());
39}
40
41QList<QNetworkProxy> QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query)
42{
43 QList<QNetworkProxy> proxyList;
44 if (!proxyInfoInstance)
45 return proxyList;
46
47 QJniObject proxyInfo = QtNetwork::callStaticMethod<ProxyInfo>("getProxyInfo",
48 QAndroidApplication::context());
49 if (proxyInfo.isValid()) {
50 const QJniArray exclusionList = proxyInfo.callMethod<String[]>("getExclusionList");
51 bool exclude = false;
52 if (exclusionList.isValid()) {
53 const QUrl host = QUrl(query.url().host());
54 for (const auto &entry : exclusionList) {
55 if (host.matches(QUrl(entry.toString()), QUrl::RemoveScheme)) {
56 exclude = true;
57 break;
58 }
59 }
60 }
61 if (!exclude) {
62 const QString hostName = proxyInfo.callMethod<QString>("getHost");
63 const int port = proxyInfo.callMethod<jint>("getPort");
64 QNetworkProxy proxy(QNetworkProxy::HttpProxy, hostName, port);
65 proxyList << proxy;
66 }
67 }
68 if (proxyList.isEmpty())
69 proxyList << QNetworkProxy::NoProxy;
70
71 return proxyList;
72}
73
74QT_END_NAMESPACE
75
76#endif