// 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 #include #include #ifndef QT_NO_NETWORKPROXY QT_BEGIN_NAMESPACE struct ProxyInfoObject { public: ProxyInfoObject(); ~ProxyInfoObject(); }; using namespace QNativeInterface; Q_GLOBAL_STATIC(ProxyInfoObject, proxyInfoInstance) static const char networkClass[] = "org/qtproject/qt/android/network/QtNetwork"; Q_DECLARE_JNI_CLASS(ProxyInfo, "android/net/ProxyInfo") Q_DECLARE_JNI_TYPE(JStringArray, "[Ljava/lang/String;") ProxyInfoObject::ProxyInfoObject() { QJniObject::callStaticMethod(networkClass, "registerReceiver", QAndroidApplication::context()); } ProxyInfoObject::~ProxyInfoObject() { QJniObject::callStaticMethod(networkClass, "unregisterReceiver", QAndroidApplication::context()); } QList QNetworkProxyFactory::systemProxyForQuery(const QNetworkProxyQuery &query) { QList proxyList; if (!proxyInfoInstance) return proxyList; QJniObject proxyInfo = QJniObject::callStaticObjectMethod( networkClass, "getProxyInfo", QAndroidApplication::context()); if (proxyInfo.isValid()) { QJniObject exclusionList = proxyInfo.callObjectMethod("getExclusionList"); bool exclude = false; if (exclusionList.isValid()) { jobjectArray listObject = exclusionList.object(); QJniEnvironment env; QJniObject entry; const int size = env->GetArrayLength(listObject); QUrl host = QUrl(query.url().host()); for (int i = 0; i < size; ++i) { entry = env->GetObjectArrayElement(listObject, i); if (host.matches(QUrl(entry.toString()), QUrl::RemoveScheme)) { exclude = true; break; } } } if (!exclude) { QJniObject hostName = proxyInfo.callObjectMethod("getHost"); const int port = proxyInfo.callMethod("getPort"); QNetworkProxy proxy(QNetworkProxy::HttpProxy, hostName.toString(), port); proxyList << proxy; } } if (proxyList.isEmpty()) proxyList << QNetworkProxy::NoProxy; return proxyList; } QT_END_NAMESPACE #endif