1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NET_MONITOR_H 17 #define NET_MONITOR_H 18 19 #include <atomic> 20 #include <condition_variable> 21 #include <memory> 22 #include <mutex> 23 24 #include "refbase.h" 25 26 #include "system_ability.h" 27 #include "system_ability_definition.h" 28 #include "i_net_monitor_callback.h" 29 #include "net_conn_types.h" 30 #include "net_datashare_utils.h" 31 #include "net_http_probe.h" 32 #include "net_link_info.h" 33 #include "probe_thread.h" 34 35 namespace OHOS { 36 namespace NetManagerStandard { 37 typedef struct { 38 bool isScreenOn; 39 uint64_t lastDetectTime; 40 } NetMonitorInfo; 41 42 class NetMonitor : public virtual RefBase, public std::enable_shared_from_this<NetMonitor> { 43 public: 44 /** 45 * Construct a new NetMonitor to detection a network 46 * 47 * @param netId Detection network's id 48 * @param bearType bearType network type 49 * @param netLinkInfo Network link information 50 * @param callback Network monitor callback weak reference 51 */ 52 NetMonitor(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo, 53 const std::weak_ptr<INetMonitorCallback> &callback, NetMonitorInfo &netMonitorInfo); 54 55 /** 56 * Destroy the NetMonitor 57 * 58 */ 59 virtual ~NetMonitor() = default; 60 61 /** 62 * Start detection 63 * 64 */ 65 void Start(); 66 67 /** 68 * Stop detecting 69 * 70 */ 71 void Stop(); 72 73 /** 74 * Is network monitor detecting 75 * 76 * @return Status value of whether the network is detecting 77 */ 78 bool IsDetecting(); 79 80 /** 81 * Network monitor detection 82 * 83 */ 84 void Detection(); 85 86 /** 87 * Update global http proxy 88 * 89 */ 90 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 91 92 /** 93 * Set screen state 94 * 95 */ 96 void SetScreenState(bool isScreenOn); 97 98 void DetectionDelayWhenScreenOff(); 99 uint64_t GetLastDetectTime(); 100 101 private: 102 void LoadGlobalHttpProxy(); 103 void ProcessDetection(NetHttpProbeResult& probeResult, NetDetectionStatus& result); 104 NetHttpProbeResult SendProbe(); 105 NetHttpProbeResult ProcessThreadDetectResult(std::shared_ptr<ProbeThread>& httpProbeThread, 106 std::shared_ptr<ProbeThread>& httpsProbeThread, std::shared_ptr<ProbeThread>& backHttpThread, 107 std::shared_ptr<ProbeThread>& backHttpsThread); 108 void StartProbe(std::shared_ptr<ProbeThread>& httpProbeThread, std::shared_ptr<ProbeThread>& httpsProbeThread, 109 std::shared_ptr<ProbeThread>& backHttpThread, std::shared_ptr<ProbeThread>& backHttpsThread, bool needProxy); 110 NetHttpProbeResult GetThreadDetectResult(std::shared_ptr<ProbeThread>& probeThread, ProbeType probeType); 111 void GetHttpProbeUrlFromConfig(); 112 void GetDetectUrlConfig(); 113 bool CheckIfSettingsDataReady(); 114 void CreateProbeThread(std::shared_ptr<ProbeThread>& httpThread, std::shared_ptr<ProbeThread>& httpsThread, 115 std::shared_ptr<TinyCountDownLatch>& latch, std::shared_ptr<TinyCountDownLatch>& latchAll, bool isPrimProbe); 116 117 private: 118 uint32_t netId_ = 0; 119 NetBearType netBearType_; 120 NetLinkInfo netLinkInfo_; 121 std::atomic<bool> isDetecting_ = false; 122 std::mutex detectionMtx_; 123 std::mutex probeMtx_; 124 std::condition_variable detectionCond_; 125 std::mutex primaryDetectMutex_; 126 std::condition_variable primaryDetectionCond_; 127 uint32_t detectionDelay_ = 0; 128 std::weak_ptr<INetMonitorCallback> netMonitorCallback_; 129 bool needDetectionWithoutProxy_ = true; 130 HttpProxy globalHttpProxy_; 131 std::string httpUrl_; 132 std::string httpsUrl_; 133 std::string fallbackHttpUrl_; 134 std::string fallbackHttpsUrl_; 135 std::mutex proxyMtx_; 136 bool isNeedSuffix_ = false; 137 bool isDataShareReady_ = false; 138 bool isScreenOn_ = true; 139 uint64_t lastDetectTimestamp_ = 0; 140 }; 141 } // namespace NetManagerStandard 142 } // namespace OHOS 143 #endif // NET_MONITOR_H 144