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); 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 private: 88 void LoadGlobalHttpProxy(); 89 void ProcessDetection(NetHttpProbeResult& probeResult, NetDetectionStatus& result); 90 NetHttpProbeResult SendProbe(); 91 NetHttpProbeResult ProcessThreadDetectResult(NetHttpProbeResult& httpProbeResult, 92 NetHttpProbeResult& httpsProbeResult, NetHttpProbeResult& fallbackHttpProbeResult, 93 NetHttpProbeResult& fallbackHttpsProbeResult); 94 NetHttpProbeResult GetThreadDetectResult(std::shared_ptr<ProbeThread>& probeThread, ProbeType probeType); 95 void GetHttpProbeUrlFromConfig(); 96 bool CheckIfSettingsDataReady(); 97 void GetDetectUrlConfig(); 98 99 private: 100 uint32_t netId_ = 0; 101 NetBearType netBearType_; 102 NetLinkInfo netLinkInfo_; 103 std::atomic<bool> isDetecting_ = false; 104 std::mutex detectionMtx_; 105 std::mutex probeMtx_; 106 std::condition_variable detectionCond_; 107 std::mutex primaryDetectMutex_; 108 std::condition_variable primaryDetectionCond_; 109 uint32_t detectionDelay_ = 0; 110 std::weak_ptr<INetMonitorCallback> netMonitorCallback_; 111 bool needDetectionWithoutProxy_ = true; 112 HttpProxy globalHttpProxy_; 113 std::string httpUrl_; 114 std::string httpsUrl_; 115 std::string fallbackHttpUrl_; 116 std::string fallbackHttpsUrl_; 117 std::mutex proxyMtx_; 118 bool isDataShareReady_ = false; 119 bool isNeedSuffix_ = false; 120 }; 121 } // namespace NetManagerStandard 122 } // namespace OHOS 123 #endif // NET_MONITOR_H 124