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 class NetMonitor : public virtual RefBase, public std::enable_shared_from_this<NetMonitor> { 38 public: 39 /** 40 * Construct a new NetMonitor to detection a network 41 * 42 * @param netId Detection network's id 43 * @param bearType bearType network type 44 * @param netLinkInfo Network link information 45 * @param callback Network monitor callback weak reference 46 */ 47 NetMonitor(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo, 48 const std::weak_ptr<INetMonitorCallback> &callback, bool isScreenOn); 49 50 /** 51 * Destroy the NetMonitor 52 * 53 */ 54 virtual ~NetMonitor() = default; 55 56 /** 57 * Start detection 58 * 59 */ 60 void Start(); 61 62 /** 63 * Stop detecting 64 * 65 */ 66 void Stop(); 67 68 /** 69 * Is network monitor detecting 70 * 71 * @return Status value of whether the network is detecting 72 */ 73 bool IsDetecting(); 74 75 /** 76 * Network monitor detection 77 * 78 */ 79 void Detection(); 80 81 /** 82 * Update global http proxy 83 * 84 */ 85 void UpdateGlobalHttpProxy(const HttpProxy &httpProxy); 86 87 /** 88 * Set screen state 89 * 90 */ 91 void SetScreenState(bool isScreenOn); 92 93 private: 94 void LoadGlobalHttpProxy(); 95 void ProcessDetection(NetHttpProbeResult& probeResult, NetDetectionStatus& result); 96 NetHttpProbeResult SendProbe(); 97 NetHttpProbeResult ProcessThreadDetectResult(std::shared_ptr<ProbeThread>& httpProbeThread, 98 std::shared_ptr<ProbeThread>& httpsProbeThread, std::shared_ptr<ProbeThread>& backHttpThread, 99 std::shared_ptr<ProbeThread>& backHttpsThread); 100 void StartProbe(std::shared_ptr<ProbeThread>& httpProbeThread, std::shared_ptr<ProbeThread>& httpsProbeThread, 101 std::shared_ptr<ProbeThread>& backHttpThread, std::shared_ptr<ProbeThread>& backHttpsThread, bool needProxy); 102 NetHttpProbeResult GetThreadDetectResult(std::shared_ptr<ProbeThread>& probeThread, ProbeType probeType); 103 void GetHttpProbeUrlFromConfig(); 104 void GetDetectUrlConfig(); 105 bool CheckIfSettingsDataReady(); 106 107 private: 108 uint32_t netId_ = 0; 109 NetBearType netBearType_; 110 NetLinkInfo netLinkInfo_; 111 std::atomic<bool> isDetecting_ = false; 112 std::mutex detectionMtx_; 113 std::mutex probeMtx_; 114 std::condition_variable detectionCond_; 115 std::mutex primaryDetectMutex_; 116 std::condition_variable primaryDetectionCond_; 117 uint32_t detectionDelay_ = 0; 118 std::weak_ptr<INetMonitorCallback> netMonitorCallback_; 119 bool needDetectionWithoutProxy_ = true; 120 HttpProxy globalHttpProxy_; 121 std::string httpUrl_; 122 std::string httpsUrl_; 123 std::string fallbackHttpUrl_; 124 std::string fallbackHttpsUrl_; 125 std::mutex proxyMtx_; 126 bool isNeedSuffix_ = false; 127 bool isDataShareReady_ = false; 128 bool isScreenOn_ = true; 129 }; 130 } // namespace NetManagerStandard 131 } // namespace OHOS 132 #endif // NET_MONITOR_H 133