• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <mutex>
20 #include <thread>
21 
22 #include "http_request.h"
23 #include "net_conn_types.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
27 const std::string DEFAULT_PORTAL_HTTPS_URL = "http://connectivitycheck.platform.hicloud.com/generate_204";
28 constexpr int32_t HTTP_DETECTION_WAIT_TIME_MS = 10000;
29 const std::string PORTAL_URL_REDIRECT_FIRST_CASE = "Location: ";
30 const std::string PORTAL_URL_REDIRECT_SECOND_CASE = "http";
31 const std::string CONTENT_STR = "Content-Length:";
32 const std::string PORTAL_END_STR = "?";
33 constexpr int32_t PORTAL_CONTENT_LENGTH_MIN = 4;
34 constexpr int32_t NET_CONTENT_LENGTH = 6;
35 
36 class NetMonitor {
37 public:
38     NetMonitor(NetDetectionStateHandler handle);
39     virtual ~NetMonitor();
40     /**
41      * @brief : Start NetMonitor thread
42      * @Return success : NET_OPT_SUCCESS  failed : NET_OPT_FAILED
43      */
44     ResultCode InitNetMonitorThread();
45 
46     /**
47      * @brief : Trigger thread to perform network detection
48      * @param ifaceName
49      */
50     void SignalNetMonitorThread(const std::string &ifaceName);
51 
52     /**
53      * @brief : stop the NetMonitor processing thread.
54      *
55      */
56     void StopNetMonitorThread();
57 
58 private:
59     /**
60      * @brief : Detect Internet ability
61      * @return true http detection success, false http detection failed
62      */
63     bool HttpDetection();
64 
65     /**
66      * @brief : NetMonitor thread function
67      */
68     void RunNetMonitorThreadFunc();
69 
70     /**
71      * @brief : Exit the NetMonitor thread.
72      *
73      */
74     void ExitNetMonitorThread();
75 
76     /**
77      * @brief Get the Status Code From Response object
78      *
79      * @param strResponse
80      * @return int32_t Returns -1, strResponse is invalid; otherwise returns statusCode
81      */
82     int32_t GetStatusCodeFromResponse(const std::string &strResponse);
83 
84     /**
85      * @brief Get the Url Redirect From Response object
86      *
87      * @param strResponse  Response data obtained from the server
88      * @param urlRedirect    The redirected url obtained from the response data
89      * @return int32_t Returns 0, get urlRedirect; returns -1, urlRedirect is empty
90      */
91     int32_t GetUrlRedirectFromResponse(const std::string &strResponse, std::string &urlRedirect);
92 
93 private:
94     std::mutex mutex_;
95     std::condition_variable condition_;
96     std::condition_variable conditionTimeout_;
97     bool isStopNetMonitor_;
98     bool isExitNetMonitorThread_;
99     std::unique_ptr<std::thread> netMonitorThread_;
100     NetDetectionStateHandler netDetectionStatus_;
101     NetDetectionStatus lastDetectionState_;
102     std::string ifaceName_;
103 };
104 }  // namespace NetManagerStandard
105 }  // namespace OHOS
106 #endif // NET_MONITOR_H