• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "i_net_monitor_callback.h"
27 #include "net_conn_types.h"
28 #include "net_http_probe.h"
29 #include "net_link_info.h"
30 #include "probe_thread.h"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 class NetMonitor : public virtual RefBase, public std::enable_shared_from_this<NetMonitor> {
35 public:
36     /**
37      * Construct a new NetMonitor to detection a network
38      *
39      * @param netId Detection network's id
40      * @param bearType bearType network type
41      * @param netLinkInfo Network link information
42      * @param callback Network monitor callback weak reference
43      */
44     NetMonitor(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo,
45                const std::weak_ptr<INetMonitorCallback> &callback);
46 
47     /**
48      * Destroy the NetMonitor
49      *
50      */
51     virtual ~NetMonitor() = default;
52 
53     /**
54      * Start detection
55      *
56      */
57     void Start();
58 
59     /**
60      * Stop detecting
61      *
62      */
63     void Stop();
64 
65     /**
66      * Is network monitor detecting
67      *
68      * @return Status value of whether the network is detecting
69      */
70     bool IsDetecting();
71 
72     /**
73      * Network monitor detection
74      *
75      */
76     void Detection();
77 
78     /**
79      * Update global http proxy
80      *
81      */
82     void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
83 
84 private:
85     void LoadGlobalHttpProxy();
86     void ProcessDetection(NetHttpProbeResult& probeResult, NetDetectionStatus& result);
87     NetHttpProbeResult SendProbe();
88     NetHttpProbeResult ProcessThreadDetectResult(NetHttpProbeResult& httpProbeResult,
89         NetHttpProbeResult& httpsProbeResult, NetHttpProbeResult& fallbackHttpProbeResult,
90         NetHttpProbeResult& fallbackHttpsProbeResult);
91     NetHttpProbeResult GetThreadDetectResult(std::shared_ptr<ProbeThread>& probeThread, ProbeType probeType);
92     void GetHttpProbeUrlFromConfig();
93 
94 private:
95     uint32_t netId_ = 0;
96     NetBearType netBearType_;
97     NetLinkInfo netLinkInfo_;
98     std::atomic<bool> isDetecting_ = false;
99     std::mutex detectionMtx_;
100     std::mutex probeMtx_;
101     std::condition_variable detectionCond_;
102     std::mutex primaryDetectMutex_;
103     std::condition_variable primaryDetectionCond_;
104     uint32_t detectionDelay_ = 0;
105     std::weak_ptr<INetMonitorCallback> netMonitorCallback_;
106     bool needDetectionWithoutProxy_ = true;
107     HttpProxy globalHttpProxy_;
108     std::string httpUrl_;
109     std::string httpsUrl_;
110     std::string fallbackHttpUrl_;
111     std::string fallbackHttpsUrl_;
112     std::mutex proxyMtx_;
113 };
114 } // namespace NetManagerStandard
115 } // namespace OHOS
116 #endif // NET_MONITOR_H
117