| 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 | |
| 4 | #ifndef QNETWORKINFORMATION_P_H |
| 5 | #define QNETWORKINFORMATION_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of the Network Information API. This header file may change from |
| 13 | // version to version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtNetwork/private/qtnetworkglobal_p.h> |
| 19 | |
| 20 | #include <QtNetwork/qnetworkinformation.h> |
| 21 | |
| 22 | #include <QtCore/qloggingcategory.h> |
| 23 | #include <QtCore/qreadwritelock.h> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class Q_NETWORK_EXPORT QNetworkInformationBackend : public QObject |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | |
| 31 | using Reachability = QNetworkInformation::Reachability; |
| 32 | using TransportMedium = QNetworkInformation::TransportMedium; |
| 33 | |
| 34 | public: |
| 35 | static inline const char16_t PluginNames[4][22] = { |
| 36 | { u"networklistmanager"}, |
| 37 | { u"scnetworkreachability"}, |
| 38 | { u"android"}, |
| 39 | { u"networkmanager"}, |
| 40 | }; |
| 41 | static constexpr int PluginNamesWindowsIndex = 0; |
| 42 | static constexpr int PluginNamesAppleIndex = 1; |
| 43 | static constexpr int PluginNamesAndroidIndex = 2; |
| 44 | static constexpr int PluginNamesLinuxIndex = 3; |
| 45 | |
| 46 | QNetworkInformationBackend() = default; |
| 47 | ~QNetworkInformationBackend() override; |
| 48 | |
| 49 | virtual QString name() const = 0; |
| 50 | virtual QNetworkInformation::Features featuresSupported() const = 0; |
| 51 | |
| 52 | Reachability reachability() const |
| 53 | { |
| 54 | QReadLocker locker(&m_lock); |
| 55 | return m_reachability; |
| 56 | } |
| 57 | |
| 58 | bool behindCaptivePortal() const |
| 59 | { |
| 60 | QReadLocker locker(&m_lock); |
| 61 | return m_behindCaptivePortal; |
| 62 | } |
| 63 | |
| 64 | TransportMedium transportMedium() const |
| 65 | { |
| 66 | QReadLocker locker(&m_lock); |
| 67 | return m_transportMedium; |
| 68 | } |
| 69 | |
| 70 | bool isMetered() const |
| 71 | { |
| 72 | QReadLocker locker(&m_lock); |
| 73 | return m_metered; |
| 74 | } |
| 75 | |
| 76 | Q_SIGNALS: |
| 77 | void reachabilityChanged(QNetworkInformation::Reachability reachability); |
| 78 | void behindCaptivePortalChanged(bool behindPortal); |
| 79 | void transportMediumChanged(QNetworkInformation::TransportMedium medium); |
| 80 | void isMeteredChanged(bool isMetered); |
| 81 | |
| 82 | protected: |
| 83 | void setReachability(QNetworkInformation::Reachability reachability) |
| 84 | { |
| 85 | QWriteLocker locker(&m_lock); |
| 86 | if (m_reachability != reachability) { |
| 87 | m_reachability = reachability; |
| 88 | locker.unlock(); |
| 89 | emit reachabilityChanged(reachability); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void setBehindCaptivePortal(bool behindPortal) |
| 94 | { |
| 95 | QWriteLocker locker(&m_lock); |
| 96 | if (m_behindCaptivePortal != behindPortal) { |
| 97 | m_behindCaptivePortal = behindPortal; |
| 98 | locker.unlock(); |
| 99 | emit behindCaptivePortalChanged(behindPortal); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void setTransportMedium(TransportMedium medium) |
| 104 | { |
| 105 | QWriteLocker locker(&m_lock); |
| 106 | if (m_transportMedium != medium) { |
| 107 | m_transportMedium = medium; |
| 108 | locker.unlock(); |
| 109 | emit transportMediumChanged(medium); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void setMetered(bool isMetered) |
| 114 | { |
| 115 | QWriteLocker locker(&m_lock); |
| 116 | if (m_metered != isMetered) { |
| 117 | m_metered = isMetered; |
| 118 | locker.unlock(); |
| 119 | emit isMeteredChanged(isMetered); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | mutable QReadWriteLock m_lock; |
| 125 | Reachability m_reachability = Reachability::Unknown; |
| 126 | TransportMedium m_transportMedium = TransportMedium::Unknown; |
| 127 | bool m_behindCaptivePortal = false; |
| 128 | bool m_metered = false; |
| 129 | |
| 130 | Q_DISABLE_COPY_MOVE(QNetworkInformationBackend) |
| 131 | friend class QNetworkInformation; |
| 132 | friend class QNetworkInformationPrivate; |
| 133 | }; |
| 134 | |
| 135 | class Q_NETWORK_EXPORT QNetworkInformationBackendFactory : public QObject |
| 136 | { |
| 137 | Q_OBJECT |
| 138 | |
| 139 | using Features = QNetworkInformation::Features; |
| 140 | |
| 141 | public: |
| 142 | QNetworkInformationBackendFactory(); |
| 143 | virtual ~QNetworkInformationBackendFactory(); |
| 144 | virtual QString name() const = 0; |
| 145 | virtual QNetworkInformationBackend *create(Features requiredFeatures) const = 0; |
| 146 | virtual Features featuresSupported() const = 0; |
| 147 | |
| 148 | private: |
| 149 | Q_DISABLE_COPY_MOVE(QNetworkInformationBackendFactory) |
| 150 | }; |
| 151 | #define QNetworkInformationBackendFactory_iid "org.qt-project.Qt.NetworkInformationBackendFactory" |
| 152 | Q_DECLARE_INTERFACE(QNetworkInformationBackendFactory, QNetworkInformationBackendFactory_iid); |
| 153 | |
| 154 | QT_END_NAMESPACE |
| 155 | |
| 156 | #endif |
| 157 |
