1 /*
2  * Copyright (C) 2024-2024 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_MANAGER_BASE_PROBE_THREAD_H
17 #define NET_MANAGER_BASE_PROBE_THREAD_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <memory>
22 #include <mutex>
23 #include <thread>
24 #include <pthread.h>
25 
26 #include "refbase.h"
27 
28 #include "i_net_monitor_callback.h"
29 #include "net_conn_types.h"
30 #include "net_http_probe.h"
31 #include "net_link_info.h"
32 #include "tiny_count_down_latch.h"
33 
34 namespace OHOS {
35 namespace NetManagerStandard {
36 class ProbeThread : public virtual RefBase, public std::enable_shared_from_this<ProbeThread> {
37 public:
38 
39     ProbeThread(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo,
40         std::shared_ptr<TinyCountDownLatch> latch, std::shared_ptr<TinyCountDownLatch> latchAll,
41         ProbeType probeType, std::string httpUrl, std::string httpsUrl);
42 
43     /**
44      * Destroy the ProbeThread
45      *
46      */
47     ~ProbeThread();
48 
49     /**
50      * Start detection
51      *
52      */
53     void Start();
54 
55     /**
56      * Update global http proxy
57      *
58      */
59     void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
60 
61     /*
62      * send http probe
63     */
64     void SendHttpProbe(ProbeType probeType);
65 
66     /*
67      * Get Http detection Result
68     */
69     NetHttpProbeResult GetHttpProbeResult();
70 
71     /*
72      * Get Https detection result
73     */
74     NetHttpProbeResult GetHttpsProbeResult();
75 
76     /*
77      * set detection without proxy
78     */
79     void ProbeWithoutGlobalHttpProxy();
80 
81     /*
82      * check http result whether portal or success
83     */
84     bool IsConclusiveResult();
85 
86     /*
87      * check whether thread is detecting
88     */
89     bool IsDetecting();
90 
91     /*
92      * get current probe type
93     */
94     ProbeType GetProbeType();
95 
96 private:
97     uint32_t netId_ = 0;
98     std::unique_ptr<NetHttpProbe> httpProbe_;
99     std::thread thread_;
100     ProbeType probeType_;
101     std::shared_ptr<TinyCountDownLatch> latch_;
102     std::shared_ptr<TinyCountDownLatch> latchAll_;
103     std::atomic<bool> isDetecting_ = false;
104     std::string httpProbeUrl_;
105     std::string httpsProbeUrl_;
106 };
107 } // namespace NetManagerStandard
108 } // namespace OHOS
109 #endif // NET_MANAGER_BASE_PROBE_THREAD_H
110