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