From 68782c9be5e87a2ebf7ca92eca1b91cc2dc9e5cc Mon Sep 17 00:00:00 2001 From: YangWeimin Date: Thu, 5 Jun 2025 23:22:42 +0800 Subject: [PATCH 1/2] code check for UT Signed-off-by: YangWeimin --- .../utils/netstack_chr_client/NetStackChrClientTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/utils/netstack_chr_client/NetStackChrClientTest.cpp b/test/unittest/utils/netstack_chr_client/NetStackChrClientTest.cpp index 73f7a1118..c2dbf5ae2 100644 --- a/test/unittest/utils/netstack_chr_client/NetStackChrClientTest.cpp +++ b/test/unittest/utils/netstack_chr_client/NetStackChrClientTest.cpp @@ -153,7 +153,7 @@ HWTEST_F(NetStackChrClientTest, NetStackChrClientTestPort, TestSize.Level2) { ChrClient::DataTransTcpInfo tcpInfo; int sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd >0) { + if (sockfd > 0) { ChrClient::NetStackChrClient::GetInstance().GetTcpInfoFromSock(sockfd, tcpInfo); EXPECT_EQ(tcpInfo.unacked, 0); EXPECT_EQ(tcpInfo.srcPort, 0); -- Gitee From efed4f551bcf8d150338e3f08fbf0d010746f3fc Mon Sep 17 00:00:00 2001 From: YangWeimin Date: Tue, 10 Jun 2025 19:40:34 +0800 Subject: [PATCH 2/2] add background app check Signed-off-by: YangWeimin --- frameworks/js/napi/http/BUILD.gn | 5 + interfaces/innerkits/http_client/BUILD.gn | 6 + .../include/app_state_aware.h | 56 ++++++++ .../include/netstack_chr_client.h | 1 + .../src/app_state_aware.cpp | 135 ++++++++++++++++++ .../src/netstack_chr_client.cpp | 26 +++- 6 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 utils/netstack_chr_client/include/app_state_aware.h create mode 100644 utils/netstack_chr_client/src/app_state_aware.cpp diff --git a/frameworks/js/napi/http/BUILD.gn b/frameworks/js/napi/http/BUILD.gn index dc9622fdd..05e69ed2d 100644 --- a/frameworks/js/napi/http/BUILD.gn +++ b/frameworks/js/napi/http/BUILD.gn @@ -162,6 +162,10 @@ ohos_shared_library("http") { global_parts_info.communication_netmanager_base) { external_deps += [ "netmanager_base:net_conn_manager_if" ] external_deps += [ "netmanager_base:netsys_client" ] + external_deps += [ + "ability_runtime:app_manager", + "ipc:ipc_core", + ] defines = [ "HAS_NETMANAGER_BASE=1", "HAS_NETSTACK_CHR=1", @@ -169,6 +173,7 @@ ohos_shared_library("http") { sources += [ "$NETSTACK_DIR/utils/netstack_chr_client/src/netstack_chr_client.cpp", "$NETSTACK_DIR/utils/netstack_chr_client/src/netstack_chr_report.cpp", + "$NETSTACK_DIR/utils/netstack_chr_client/src/app_state_aware.cpp", "$NETSTACK_DIR/utils/http_over_curl/src/epoll_multi_driver.cpp", "$NETSTACK_DIR/utils/http_over_curl/src/epoll_request_handler.cpp", ] diff --git a/interfaces/innerkits/http_client/BUILD.gn b/interfaces/innerkits/http_client/BUILD.gn index d2fd824cd..0a815b1b8 100644 --- a/interfaces/innerkits/http_client/BUILD.gn +++ b/interfaces/innerkits/http_client/BUILD.gn @@ -67,6 +67,7 @@ ohos_shared_library("http_client") { "$NETSTACK_DIR/utils/http_over_curl/src/epoll_request_handler.cpp", "$NETSTACK_DIR/utils/netstack_chr_client/src/netstack_chr_client.cpp", "$NETSTACK_DIR/utils/netstack_chr_client/src/netstack_chr_report.cpp", + "$NETSTACK_DIR/utils/netstack_chr_client/src/app_state_aware.cpp", "$NETSTACK_DIR/utils/profiler_utils/src/http_client_network_message.cpp", "$NETSTACK_DIR/utils/profiler_utils/src/i_network_message.cpp", "$NETSTACK_DIR/utils/profiler_utils/src/netstack_network_profiler.cpp", @@ -126,6 +127,11 @@ ohos_shared_library("http_client") { "netmanager_base:net_conn_manager_if", "time_service:time_client", ] + external_deps += [ + "ability_runtime:app_manager", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] if (product_name != "ohos-sdk") { external_deps += [ "init:libbegetutil" ] } diff --git a/utils/netstack_chr_client/include/app_state_aware.h b/utils/netstack_chr_client/include/app_state_aware.h new file mode 100644 index 000000000..92c313876 --- /dev/null +++ b/utils/netstack_chr_client/include/app_state_aware.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef COMMUNICATIONNETSTACK_APP_STATE_AWARE_H +#define COMMUNICATIONNETSTACK_APP_STATE_AWARE_H + +#include "appmgr/app_mgr_interface.h" +#include "appmgr/app_state_data.h" +#include "iremote_object.h" +#include "appmgr/application_state_observer_stub.h" + +namespace OHOS::NetStack::ChrClient { + +constexpr uint32_t MAX_RETRY_COUNT = 5; + +struct AppStateAwareCallback { + std::function OnForegroundAppChanged; +}; + +class AppStateObserver : public AppExecFwk::ApplicationStateObserverStub { +public: + void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) override; +}; + +class AppStateAwareManager { +public: + explicit AppStateAwareManager(); + ~AppStateAwareManager(); + static AppStateAwareManager &GetInstance(); + sptr GetAppMgr(); + bool SubscribeAppState(); + void UnSubscribeAppState(); + void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData); + bool IsForegroundApp(const uint32_t uid); +private: + std::atomic foregroundAppUid_; + std::mutex mutex_ {}; + sptr appStateObserver_ = nullptr; + AppStateAwareCallback appStateAwareCallback_; + uint32_t retryCount_ = 0; + + static std::mutex instanceMutex_; +}; +} // namespace OHOS::NetStack::ChrClient +#endif // COMMUNICATIONNETSTACK_APP_STATE_AWARE_H \ No newline at end of file diff --git a/utils/netstack_chr_client/include/netstack_chr_client.h b/utils/netstack_chr_client/include/netstack_chr_client.h index fadb94450..bf6a4d7bd 100644 --- a/utils/netstack_chr_client/include/netstack_chr_client.h +++ b/utils/netstack_chr_client/include/netstack_chr_client.h @@ -42,6 +42,7 @@ private: static DataType GetNumericAttributeFromCurl(CURL *handle, CURLINFO info); static std::string GetStringAttributeFromCurl(CURL *handle, CURLINFO info); static long GetRequestStartTime(curl_off_t totalTime); + static bool IsInbackgroundWhiteList(cont int uid); static int ShouldReportHttpAbnormalEvent(const DataTransHttpInfo &httpInfo); NetStackChrReport netstackChrReport_; }; diff --git a/utils/netstack_chr_client/src/app_state_aware.cpp b/utils/netstack_chr_client/src/app_state_aware.cpp new file mode 100644 index 000000000..796293a21 --- /dev/null +++ b/utils/netstack_chr_client/src/app_state_aware.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2025-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "app_state_aware.h" + +#include "app_mgr_constants.h" +#include "iservice_registry.h" +#include +#include "system_ability_definition.h" +#include "netstack_log.h" + +namespace OHOS::NetStack::ChrClient { +std::mutex AppStateAwareManager::instanceMutex_; + +AppStateAwareManager::AppStateAwareManager() +{ + SubscribeAppState(); +} + +AppStateAwareManager::~AppStateAwareManager() +{ + UnSubscribeAppState(); +} + +AppStateAwareManager &AppStateAwareManager::GetInstance() +{ + std::lock_guard lock(instanceMutex_); + static AppStateAwareManager gAppStateAwareManager; + if (!gAppStateAwareManager.appStateObserver_) { + if (gAppStateAwareManager.retryCount_ < MAX_RETRY_COUNT) { + gAppStateAwareManager.SubscribeAppState(); + } + } + return gAppStateAwareManager; +} + +bool AppStateAwareManager::SubscribeAppState() +{ + std::lock_guard lock(mutex_); + retryCount_++; + if (appStateObserver_) { + NETSTACK_LOGI("SubscribeAppState: appStateObserver_ has register"); + return false; + } + sptr appMgrProxy = GetAppMgr(); + if (!appMgrProxy) { + return false; + } + appStateObserver_ = new (std::nothrow)AppStateObserver(); + auto err = appMgrProxy->RegisterApplicationStateObserver(appStateObserver_); + if (err != 0) { + NETSTACK_LOGE("SubscribeAppState error, code = %{public}d", err); + appStateObserver_ = nullptr; + return false; + } + return true; +} + +void AppStateAwareManager::UnSubscribeAppState() +{ + NETSTACK_LOGI("UnSubscribeAppState start"); + std::lock_guard lock(mutex_); + if (!appStateObserver_) { + NETSTACK_LOGE("UnSubscribeAppState: appStateObserver_ is nullptr"); + return; + } + sptr appMgrProxy = GetAppMgr(); + if (appMgrProxy) { + appMgrProxy->UnregisterApplicationStateObserver(appStateObserver_); + appStateObserver_ = nullptr; + retryCount_ = 0; + } + NETSTACK_LOGI("UnSubscribeAppState end"); +} + +sptr AppStateAwareManager::GetAppMgr() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + NETSTACK_LOGE("get SystemAbilityManager failed"); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + NETSTACK_LOGE("get App Manager Service failed"); + return nullptr; + } + return iface_cast(remoteObject); +} + +bool AppStateAwareManager::IsForegroundApp(const uint32_t uid) +{ + if (!appStateObserver_) { + return false; + } + uint32_t foregroundAppUid = static_cast(foregroundAppUid_.load()); + return foregroundAppUid == uid; +} + +void AppStateAwareManager::OnForegroundApplicationChanged( + const AppExecFwk::AppStateData &appStateData) +{ + int32_t state = appStateData.state; + auto appState = static_cast(state); + + if (appState == AppExecFwk::ApplicationState::APP_STATE_FOREGROUND + && foregroundAppUid_ != appStateData.uid) { + foregroundAppUid_ = appStateData.uid; + if (appStateAwareCallback_.OnForegroundAppChanged != nullptr) { + appStateAwareCallback_.OnForegroundAppChanged(foregroundAppUid_); + } + } +} + +void AppStateObserver::OnForegroundApplicationChanged( + const AppExecFwk::AppStateData &appStateData) +{ + NETSTACK_LOGI("%{public}s bundleName: %{public}s, uid: %{public}d, state: %{public}d, isFocused: %{public}d", + __func__, appStateData.bundleName.c_str(), appStateData.uid, appStateData.state, appStateData.isFocused); + AppStateAwareManager::GetInstance().OnForegroundApplicationChanged(appStateData); +} +} // namespace OHOS::NetStack::ChrClient \ No newline at end of file diff --git a/utils/netstack_chr_client/src/netstack_chr_client.cpp b/utils/netstack_chr_client/src/netstack_chr_client.cpp index 0ad525422..fca75c3f6 100644 --- a/utils/netstack_chr_client/src/netstack_chr_client.cpp +++ b/utils/netstack_chr_client/src/netstack_chr_client.cpp @@ -20,12 +20,14 @@ #include "netstack_common_utils.h" #include "netstack_log.h" #include "i_netstack_chr_client.h" +#include "app_state_aware.h" namespace OHOS::NetStack::ChrClient { -static constexpr const long HTTP_REQUEST_SUCCESS = 200; -static constexpr const int HTTP_FILE_TRANSFER_SIZE_THRESHOLD = 100000; -static constexpr const int HTTP_FILE_TRANSFER_TIME_THRESHOLD = 500000; +static constexpr const long HTTP_REQUEST_SUCCESS_MIN = 200; +static constexpr const long HTTP_REQUEST_SUCCESS_MAX = 299; +static constexpr const int UID_PUSH = 7023; +static constexpr const int UID_ACCOUNT = 7008; NetStackChrClient &NetStackChrClient::GetInstance() { @@ -164,14 +166,24 @@ void NetStackChrClient::GetHttpInfoFromCurl(CURL *handle, DataTransHttpInfo &htt httpInfo.contentType = GetStringAttributeFromCurl(handle, CURLINFO_CONTENT_TYPE); } +bool NetStackChrClient::IsInbackgroundWhiteList(const int uid) +{ + if (uid == UID_PUSH || uid == UID_ACCOUNT) { + return true; + } + return false; +} + int NetStackChrClient::ShouldReportHttpAbnormalEvent(const DataTransHttpInfo &httpInfo) { - if (httpInfo.curlCode != 0 || httpInfo.responseCode != HTTP_REQUEST_SUCCESS || - httpInfo.osError != 0 || httpInfo.proxyError != 0) { + if (AppStateAwareManager::GetInstance().IsForegroundApp(httpInfo.uid) == false && + IsInbackgroundWhiteList(httpInfo.uid) == false) { + return -1; + } + if (httpInfo.responseCode < HTTP_REQUEST_SUCCESS_MIN || httpInfo.responseCode > HTTP_REQUEST_SUCCESS_MAX) { return 0; } - if ((httpInfo.sizeUpload + httpInfo.sizeDownload <= HTTP_FILE_TRANSFER_SIZE_THRESHOLD) && - httpInfo.totalTime > HTTP_FILE_TRANSFER_TIME_THRESHOLD) { + if (httpInfo.curlCode != 0 || httpInfo.osError != 0 || httpInfo.proxyError != 0) { return 0; } -- Gitee