• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 #include <arpa/inet.h>
17 #include <cstring>
18 #include <fcntl.h>
19 #include <fstream>
20 #include <future>
21 #include <list>
22 #include <memory>
23 #include <netdb.h>
24 #include <regex>
25 #include <securec.h>
26 #include <sys/socket.h>
27 #include <thread>
28 #include <pthread.h>
29 #include <unistd.h>
30 #include <string>
31 
32 #include "net_monitor.h"
33 #include "dns_config_client.h"
34 #include "event_report.h"
35 #include "fwmark_client.h"
36 #include "netmanager_base_common_utils.h"
37 #include "netsys_controller.h"
38 #include "net_http_proxy_tracker.h"
39 #include "net_mgr_log_wrapper.h"
40 #include "net_manager_constants.h"
41 #include "tiny_count_down_latch.h"
42 
43 namespace OHOS {
44 namespace NetManagerStandard {
45 namespace {
46 constexpr int32_t INIT_DETECTION_DELAY_MS = 1 * 1000;
47 constexpr int32_t MAX_FAILED_DETECTION_DELAY_MS = 10 * 60 * 1000;
48 constexpr int32_t PRIMARY_DETECTION_RESULT_WAIT_MS = 3 * 1000;
49 constexpr int32_t ALL_DETECTION_RESULT_WAIT_MS = 10 * 1000;
50 constexpr int32_t CAPTIVE_PORTAL_DETECTION_DELAY_MS = 15 * 1000;
51 constexpr int32_t SCREENOFF_PORTAL_DETECTION_DELAY_MS = 5 * 60 * 1000;
52 constexpr int32_t SCREENOFF_DETECTION_INTERVAL_MS = 5 * 60 * 1000;
53 constexpr int32_t DOUBLE = 2;
54 constexpr int32_t SIM_PORTAL_CODE = 302;
55 constexpr int32_t ONE_URL_DETECT_NUM = 4;
56 constexpr int32_t ALL_DETECT_THREAD_NUM = 8;
57 constexpr const char NEW_LINE_STR = '\n';
58 constexpr const char* URL_CFG_FILE = "/system/etc/netdetectionurl.conf";
59 constexpr const char* DETECT_CFG_FILE = "/system/etc/detectionconfig.conf";
60 constexpr const char *SETTINGS_DATASHARE_URI =
61         "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true";
62 constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
63 const std::string HTTP_URL_HEADER = "HttpProbeUrl:";
64 const std::string HTTPS_URL_HEADER = "HttpsProbeUrl:";
65 const std::string FALLBACK_HTTP_URL_HEADER = "FallbackHttpProbeUrl:";
66 const std::string FALLBACK_HTTPS_URL_HEADER = "FallbackHttpsProbeUrl:";
67 const std::string ADD_RANDOM_CFG_PREFIX = "AddSuffix:";
68 const std::string ADD_RANDOM_CFG_VALUE = "true";
69 } // namespace
NetDetectThread(const std::shared_ptr<NetMonitor> & netMonitor)70 static void NetDetectThread(const std::shared_ptr<NetMonitor> &netMonitor)
71 {
72     if (netMonitor == nullptr) {
73         NETMGR_LOG_E("netMonitor is nullptr");
74         return;
75     }
76     netMonitor->DetectionDelayWhenScreenOff();
77     while (netMonitor->IsDetecting()) {
78         netMonitor->Detection();
79     }
80 }
81 
NetMonitor(uint32_t netId,NetBearType bearType,const NetLinkInfo & netLinkInfo,const std::weak_ptr<INetMonitorCallback> & callback,NetMonitorInfo & netMonitorInfo)82 NetMonitor::NetMonitor(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo,
83     const std::weak_ptr<INetMonitorCallback> &callback, NetMonitorInfo &netMonitorInfo)
84     : netId_(netId), netLinkInfo_(netLinkInfo), netMonitorCallback_(callback), isScreenOn_(netMonitorInfo.isScreenOn)
85 {
86     netBearType_ = bearType;
87     lastDetectTimestamp_ = netMonitorInfo.lastDetectTime;
88     LoadGlobalHttpProxy();
89     GetDetectUrlConfig();
90     GetHttpProbeUrlFromConfig();
91 }
92 
Start()93 void NetMonitor::Start()
94 {
95     NETMGR_LOG_D("Start net[%{public}d] monitor in", netId_);
96     if (isDetecting_) {
97         if (isScreenOn_) {
98             NETMGR_LOG_W("Net[%{public}d] monitor is detecting, notify", netId_);
99             detectionDelay_ = 0;
100             detectionCond_.notify_all();
101         }
102         return;
103     }
104     isDetecting_ = true;
105     std::thread t([sp = shared_from_this()]() { NetDetectThread(sp); });
106     std::string threadName = "netDetect";
107     pthread_setname_np(t.native_handle(), threadName.c_str());
108     t.detach();
109 }
110 
Stop()111 void NetMonitor::Stop()
112 {
113     NETMGR_LOG_I("Stop net[%{public}d] monitor in", netId_);
114     isDetecting_ = false;
115     detectionCond_.notify_all();
116     NETMGR_LOG_D("Stop net[%{public}d] monitor out", netId_);
117 }
118 
IsDetecting()119 bool NetMonitor::IsDetecting()
120 {
121     return isDetecting_.load();
122 }
123 
ProcessDetection(NetHttpProbeResult & probeResult,NetDetectionStatus & result)124 void NetMonitor::ProcessDetection(NetHttpProbeResult& probeResult, NetDetectionStatus& result)
125 {
126     if (probeResult.IsSuccessful()) {
127         NETMGR_LOG_I("Net[%{public}d] probe success", netId_);
128         isDetecting_ = false;
129         needDetectionWithoutProxy_ = true;
130         result = VERIFICATION_STATE;
131         detectionDelay_ = 0;
132     } else if (probeResult.GetCode() == SIM_PORTAL_CODE && netBearType_ == BEARER_CELLULAR) {
133         NETMGR_LOG_E("Net[%{public}d] probe failed with 302 response on Cell", netId_);
134         detectionDelay_ = MAX_FAILED_DETECTION_DELAY_MS;
135         result = CAPTIVE_PORTAL_STATE;
136     } else if (probeResult.IsNeedPortal()) {
137         NETMGR_LOG_W("Net[%{public}d] need portal", netId_);
138         if (!isScreenOn_ && netBearType_ == BEARER_WIFI) {
139             detectionDelay_ = SCREENOFF_PORTAL_DETECTION_DELAY_MS;
140         } else {
141             detectionDelay_ = CAPTIVE_PORTAL_DETECTION_DELAY_MS;
142         }
143         result = CAPTIVE_PORTAL_STATE;
144     } else {
145         NETMGR_LOG_E("Net[%{public}d] probe failed", netId_);
146         detectionDelay_ *= DOUBLE;
147         if (detectionDelay_ == 0) {
148             detectionDelay_ = INIT_DETECTION_DELAY_MS;
149         } else if (detectionDelay_ >= MAX_FAILED_DETECTION_DELAY_MS) {
150             detectionDelay_ = MAX_FAILED_DETECTION_DELAY_MS;
151         }
152         NETMGR_LOG_I("Net probe failed detectionDelay time [%{public}d]", detectionDelay_);
153         result = INVALID_DETECTION_STATE;
154     }
155     auto monitorCallback = netMonitorCallback_.lock();
156     if (monitorCallback == nullptr) {
157         Stop();
158         return;
159     }
160     monitorCallback->OnHandleNetMonitorResult(result, probeResult.GetRedirectUrl());
161 
162     struct EventInfo eventInfo = {.monitorStatus = static_cast<int32_t>(result)};
163     EventReport::SendMonitorBehaviorEvent(eventInfo);
164     if (isDetecting_) {
165         std::unique_lock<std::mutex> locker(detectionMtx_);
166         detectionCond_.wait_for(locker, std::chrono::milliseconds(detectionDelay_));
167     }
168 }
169 
Detection()170 void NetMonitor::Detection()
171 {
172     lastDetectTimestamp_ = CommonUtils::GetCurrentMilliSecond();
173     NetHttpProbeResult probeResult = SendProbe();
174     bool isTmpDetecting = IsDetecting();
175     NETMGR_LOG_I("Detection isTmpDetecting[%{public}d]", isTmpDetecting);
176     if (IsDetecting()) {
177         NetDetectionStatus result = UNKNOWN_STATE;
178         ProcessDetection(probeResult, result);
179     }
180 }
181 
SendProbe()182 NetHttpProbeResult NetMonitor::SendProbe()
183 {
184     NETMGR_LOG_D("start net detection");
185     std::lock_guard<std::mutex> monitorLocker(probeMtx_);
186     std::shared_ptr<TinyCountDownLatch> latch = std::make_shared<TinyCountDownLatch>(ONE_URL_DETECT_NUM);
187     std::shared_ptr<TinyCountDownLatch> latchAll = std::make_shared<TinyCountDownLatch>(ALL_DETECT_THREAD_NUM);
188     std::shared_ptr<ProbeThread> httpProxyThread = nullptr;
189     std::shared_ptr<ProbeThread> httpsProxyThread = nullptr;
190     std::shared_ptr<ProbeThread> backHttpProxyThread = nullptr;
191     std::shared_ptr<ProbeThread> backHttpsProxyThread = nullptr;
192     CreateProbeThread(httpProxyThread, httpsProxyThread, latch, latchAll, true);
193     CreateProbeThread(backHttpProxyThread, backHttpsProxyThread, latch, latchAll, false);
194     StartProbe(httpProxyThread, httpsProxyThread, backHttpProxyThread, backHttpsProxyThread, true);
195     latch->Await(std::chrono::milliseconds(PRIMARY_DETECTION_RESULT_WAIT_MS));
196     NetHttpProbeResult proxyResult = ProcessThreadDetectResult(httpProxyThread, httpsProxyThread, backHttpProxyThread,
197         backHttpsProxyThread);
198     if (proxyResult.IsNeedPortal() || proxyResult.IsSuccessful()) {
199         return proxyResult;
200     }
201     NETMGR_LOG_I("backup url detection");
202     std::shared_ptr<ProbeThread> httpNoProxyThread = nullptr;
203     std::shared_ptr<ProbeThread> httpsNoProxyThread = nullptr;
204     std::shared_ptr<ProbeThread> backHttpNoProxyThread = nullptr;
205     std::shared_ptr<ProbeThread> backHttpsNoProxyThread = nullptr;
206     CreateProbeThread(httpNoProxyThread, httpsNoProxyThread, latch, latchAll, true);
207     CreateProbeThread(backHttpNoProxyThread, backHttpsNoProxyThread, latch, latchAll, false);
208     StartProbe(httpNoProxyThread, httpsNoProxyThread, backHttpNoProxyThread, backHttpsNoProxyThread, false);
209     latchAll->Await(std::chrono::milliseconds(ALL_DETECTION_RESULT_WAIT_MS));
210     proxyResult = ProcessThreadDetectResult(httpProxyThread, httpsProxyThread, backHttpProxyThread,
211         backHttpsProxyThread);
212     NetHttpProbeResult noProxyResult = ProcessThreadDetectResult(httpNoProxyThread, httpsNoProxyThread,
213         backHttpNoProxyThread, backHttpsNoProxyThread);
214     if (proxyResult.IsNeedPortal()) {
215         return proxyResult;
216     } else if (noProxyResult.IsNeedPortal()) {
217         return noProxyResult;
218     } else if (proxyResult.IsSuccessful()) {
219         return proxyResult;
220     } else if (noProxyResult.IsSuccessful()) {
221         return noProxyResult;
222     } else {
223         return proxyResult;
224     }
225 }
226 
CreateProbeThread(std::shared_ptr<ProbeThread> & httpThread,std::shared_ptr<ProbeThread> & httpsThread,std::shared_ptr<TinyCountDownLatch> & latch,std::shared_ptr<TinyCountDownLatch> & latchAll,bool isPrimProbe)227 void NetMonitor::CreateProbeThread(std::shared_ptr<ProbeThread>& httpThread, std::shared_ptr<ProbeThread>& httpsThread,
228     std::shared_ptr<TinyCountDownLatch>& latch, std::shared_ptr<TinyCountDownLatch>& latchAll, bool isPrimProbe)
229 {
230     if (isPrimProbe) {
231         if (netBearType_ == BEARER_CELLULAR) {
232             NETMGR_LOG_I("create primary probeThread for cellular");
233             httpsThread = std::make_shared<ProbeThread>(
234                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTPS, httpUrl_, httpsUrl_);
235             httpThread = std::make_shared<ProbeThread>(
236                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTP, httpUrl_, httpsUrl_);
237         } else {
238             NETMGR_LOG_D("create primary probeThread for others");
239             httpThread = std::make_shared<ProbeThread>(
240                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTP, httpUrl_, httpsUrl_);
241             httpsThread = std::make_shared<ProbeThread>(
242                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTPS, httpUrl_, httpsUrl_);
243         }
244     } else {
245         if (netBearType_ == BEARER_CELLULAR) {
246             NETMGR_LOG_I("create fallback probeThread for cellular");
247             httpsThread = std::make_shared<ProbeThread>(
248                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTPS_FALLBACK, fallbackHttpUrl_,
249                 fallbackHttpsUrl_);
250             httpThread = std::make_shared<ProbeThread>(
251                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTP_FALLBACK, fallbackHttpUrl_,
252                 fallbackHttpsUrl_);
253         } else {
254             NETMGR_LOG_D("create fallback probeThread for others");
255             httpThread = std::make_shared<ProbeThread>(
256                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTP_FALLBACK, fallbackHttpUrl_,
257                 fallbackHttpsUrl_);
258             httpsThread = std::make_shared<ProbeThread>(
259                 netId_, netBearType_, netLinkInfo_, latch, latchAll, ProbeType::PROBE_HTTPS_FALLBACK, fallbackHttpUrl_,
260                 fallbackHttpsUrl_);
261         }
262     }
263 }
264 
StartProbe(std::shared_ptr<ProbeThread> & httpProbeThread,std::shared_ptr<ProbeThread> & httpsProbeThread,std::shared_ptr<ProbeThread> & backHttpThread,std::shared_ptr<ProbeThread> & backHttpsThread,bool needProxy)265 void NetMonitor::StartProbe(std::shared_ptr<ProbeThread>& httpProbeThread,
266     std::shared_ptr<ProbeThread>& httpsProbeThread, std::shared_ptr<ProbeThread>& backHttpThread,
267     std::shared_ptr<ProbeThread>& backHttpsThread, bool needProxy)
268 {
269     if (needProxy) {
270         std::lock_guard<std::mutex> proxyLocker(proxyMtx_);
271         httpProbeThread->UpdateGlobalHttpProxy(globalHttpProxy_);
272         httpsProbeThread->UpdateGlobalHttpProxy(globalHttpProxy_);
273         backHttpThread->UpdateGlobalHttpProxy(globalHttpProxy_);
274         backHttpsThread->UpdateGlobalHttpProxy(globalHttpProxy_);
275     } else {
276         httpProbeThread->ProbeWithoutGlobalHttpProxy();
277         httpsProbeThread->ProbeWithoutGlobalHttpProxy();
278         backHttpThread->ProbeWithoutGlobalHttpProxy();
279         backHttpsThread->ProbeWithoutGlobalHttpProxy();
280     }
281     if (netBearType_ == BEARER_CELLULAR) {
282         httpsProbeThread->Start();
283         httpProbeThread->Start();
284         backHttpsThread->Start();
285         backHttpThread->Start();
286     } else {
287         httpProbeThread->Start();
288         httpsProbeThread->Start();
289         backHttpThread->Start();
290         backHttpsThread->Start();
291     }
292 }
293 
GetThreadDetectResult(std::shared_ptr<ProbeThread> & probeThread,ProbeType probeType)294 NetHttpProbeResult NetMonitor::GetThreadDetectResult(std::shared_ptr<ProbeThread>& probeThread, ProbeType probeType)
295 {
296     NetHttpProbeResult result;
297     if (!probeThread->IsDetecting()) {
298         if (probeType == ProbeType::PROBE_HTTP || probeType == ProbeType::PROBE_HTTP_FALLBACK) {
299             return probeThread->GetHttpProbeResult();
300         } else {
301             return probeThread->GetHttpsProbeResult();
302         }
303     }
304     return result;
305 }
306 
ProcessThreadDetectResult(std::shared_ptr<ProbeThread> & httpProbeThread,std::shared_ptr<ProbeThread> & httpsProbeThread,std::shared_ptr<ProbeThread> & backHttpThread,std::shared_ptr<ProbeThread> & backHttpsThread)307 NetHttpProbeResult NetMonitor::ProcessThreadDetectResult(std::shared_ptr<ProbeThread>& httpProbeThread,
308     std::shared_ptr<ProbeThread>& httpsProbeThread, std::shared_ptr<ProbeThread>& backHttpThread,
309     std::shared_ptr<ProbeThread>& backHttpsThread)
310 {
311     NetHttpProbeResult httpResult = GetThreadDetectResult(httpProbeThread, ProbeType::PROBE_HTTP);
312     NetHttpProbeResult httpsResult = GetThreadDetectResult(httpsProbeThread, ProbeType::PROBE_HTTPS);
313     NetHttpProbeResult backHttpResult = GetThreadDetectResult(backHttpThread, ProbeType::PROBE_HTTP_FALLBACK);
314     NetHttpProbeResult backHttpsResult = GetThreadDetectResult(backHttpsThread, ProbeType::PROBE_HTTPS_FALLBACK);
315     if (httpResult.IsNeedPortal()) {
316         NETMGR_LOG_I("primary http detect result: portal");
317         return httpResult;
318     }
319     if (backHttpResult.IsNeedPortal()) {
320         NETMGR_LOG_I("fallback http detect result: portal");
321         return backHttpResult;
322     }
323     if (httpsResult.IsSuccessful()) {
324         NETMGR_LOG_D("primary https detect result: success");
325         return httpsResult;
326     }
327     if (backHttpsResult.IsSuccessful()) {
328         NETMGR_LOG_I("fallback https detect result: success");
329         return backHttpsResult;
330     }
331     if (httpResult.IsSuccessful() && backHttpResult.IsSuccessful()) {
332         NETMGR_LOG_I("both primary http and fallback http detect result success");
333         return httpResult;
334     }
335     return httpsResult;
336 }
337 
LoadGlobalHttpProxy()338 void NetMonitor::LoadGlobalHttpProxy()
339 {
340     if (!CheckIfSettingsDataReady()) {
341         NETMGR_LOG_E("data_share is not ready");
342         return;
343     }
344     NetHttpProxyTracker httpProxyTracker;
345     httpProxyTracker.ReadFromSettingsData(globalHttpProxy_);
346 }
347 
UpdateGlobalHttpProxy(const HttpProxy & httpProxy)348 void NetMonitor::UpdateGlobalHttpProxy(const HttpProxy &httpProxy)
349 {
350     std::unique_lock<std::mutex> proxyLocker(proxyMtx_);
351     globalHttpProxy_ = httpProxy;
352 }
353 
GetHttpProbeUrlFromConfig()354 void NetMonitor::GetHttpProbeUrlFromConfig()
355 {
356     if (!std::filesystem::exists(URL_CFG_FILE)) {
357         NETMGR_LOG_E("File not exist (%{public}s)", URL_CFG_FILE);
358         return;
359     }
360 
361     std::ifstream file(URL_CFG_FILE);
362     if (!file.is_open()) {
363         NETMGR_LOG_E("Open file failed (%{public}s)", strerror(errno));
364         return;
365     }
366 
367     std::ostringstream oss;
368     oss << file.rdbuf();
369     std::string content = oss.str();
370     auto pos = content.find(HTTP_URL_HEADER);
371     if (pos != std::string::npos) {
372         pos += HTTP_URL_HEADER.length();
373         httpUrl_ = content.substr(pos, content.find(NEW_LINE_STR, pos) - pos);
374         if (isNeedSuffix_) {
375             uint64_t ranNum = CommonUtils::GenRandomNumber();
376             httpUrl_ = httpUrl_ + std::string("_") + std::to_string(ranNum);
377         }
378     }
379 
380     pos = content.find(HTTPS_URL_HEADER);
381     if (pos != std::string::npos) {
382         pos += HTTPS_URL_HEADER.length();
383         httpsUrl_ = content.substr(pos, content.find(NEW_LINE_STR, pos) - pos);
384     }
385 
386     pos = content.find(FALLBACK_HTTP_URL_HEADER);
387     if (pos != std::string::npos) {
388         pos += FALLBACK_HTTP_URL_HEADER.length();
389         fallbackHttpUrl_ = content.substr(pos, content.find(NEW_LINE_STR, pos) - pos);
390     }
391 
392     pos = content.find(FALLBACK_HTTPS_URL_HEADER);
393     if (pos != std::string::npos) {
394         pos += FALLBACK_HTTPS_URL_HEADER.length();
395         fallbackHttpsUrl_ = content.substr(pos, content.find(NEW_LINE_STR, pos) - pos);
396     }
397     NETMGR_LOG_D("Get net detection http url:[%{public}s], https url:[%{public}s], fallback http url:[%{public}s],"
398         " fallback https url:[%{public}s]", httpUrl_.c_str(), httpsUrl_.c_str(), fallbackHttpUrl_.c_str(),
399         fallbackHttpsUrl_.c_str());
400 }
401 
GetDetectUrlConfig()402 void NetMonitor::GetDetectUrlConfig()
403 {
404     if (!std::filesystem::exists(DETECT_CFG_FILE)) {
405         NETMGR_LOG_E("File not exist (%{public}s)", DETECT_CFG_FILE);
406         return;
407     }
408 
409     std::ifstream file(DETECT_CFG_FILE);
410     if (!file.is_open()) {
411         NETMGR_LOG_E("Open file failed (%{public}s)", strerror(errno));
412         return;
413     }
414     std::ostringstream oss;
415     oss << file.rdbuf();
416     std::string content = oss.str();
417     auto pos = content.find(ADD_RANDOM_CFG_PREFIX);
418     if (pos != std::string::npos) {
419         pos += ADD_RANDOM_CFG_PREFIX.length();
420         std::string value = content.substr(pos, content.find(NEW_LINE_STR, pos) - pos);
421         value = CommonUtils::Trim(value);
422         isNeedSuffix_ = value.compare(ADD_RANDOM_CFG_VALUE) == 0;
423     }
424     NETMGR_LOG_I("is need add suffix (%{public}d)", isNeedSuffix_);
425 }
426 
CheckIfSettingsDataReady()427 bool NetMonitor::CheckIfSettingsDataReady()
428 {
429     if (isDataShareReady_) {
430         return true;
431     }
432     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
433     if (saManager == nullptr) {
434         NETMGR_LOG_E("GetSystemAbilityManager failed.");
435         return false;
436     }
437     sptr<IRemoteObject> dataShareSa = saManager->GetSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID);
438     if (dataShareSa == nullptr) {
439         NETMGR_LOG_E("Get dataShare SA Failed.");
440         return false;
441     }
442     sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
443     if (remoteObj == nullptr) {
444         NETMGR_LOG_E("NetDataShareHelperUtils GetSystemAbility Service Failed.");
445         return false;
446     }
447     std::pair<int, std::shared_ptr<DataShare::DataShareHelper>> ret =
448             DataShare::DataShareHelper::Create(remoteObj, SETTINGS_DATASHARE_URI, SETTINGS_DATA_EXT_URI);
449     NETMGR_LOG_I("create data_share helper, ret=%{public}d", ret.first);
450     if (ret.first == DataShare::E_OK) {
451         NETMGR_LOG_I("create data_share helper success");
452         auto helper = ret.second;
453         if (helper != nullptr) {
454             bool releaseRet = helper->Release();
455             NETMGR_LOG_I("release data_share helper, releaseRet=%{public}d", releaseRet);
456         }
457         isDataShareReady_ = true;
458         return true;
459     } else if (ret.first == DataShare::E_DATA_SHARE_NOT_READY) {
460         NETMGR_LOG_E("create data_share helper failed");
461         isDataShareReady_ = false;
462         return false;
463     }
464     NETMGR_LOG_E("data_share unknown.");
465     return true;
466 }
467 
SetScreenState(bool isScreenOn)468 void NetMonitor::SetScreenState(bool isScreenOn)
469 {
470     isScreenOn_ = isScreenOn;
471 }
472 
DetectionDelayWhenScreenOff()473 void NetMonitor::DetectionDelayWhenScreenOff()
474 {
475     uint64_t nowTime = CommonUtils::GetCurrentMilliSecond();
476     if (!isScreenOn_ && (nowTime - lastDetectTimestamp_) < SCREENOFF_DETECTION_INTERVAL_MS) {
477         uint64_t delayTime = SCREENOFF_DETECTION_INTERVAL_MS - (nowTime - lastDetectTimestamp_);
478         std::unique_lock<std::mutex> locker(detectionMtx_);
479         detectionCond_.wait_for(locker, std::chrono::milliseconds(delayTime));
480         locker.unlock();
481     }
482 }
483 
GetLastDetectTime()484 uint64_t NetMonitor::GetLastDetectTime()
485 {
486     return lastDetectTimestamp_;
487 }
488 } // namespace NetManagerStandard
489 } // namespace OHOS
490