From 352405e2ece929cdd463738628ec0c1d7f7565bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Thu, 27 Feb 2025 21:48:41 +0800 Subject: [PATCH 01/21] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=86=B7=E5=86=BB?= =?UTF-8?q?=E4=B8=8D=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- bundle.json | 3 +- .../netconnclient/src/net_conn_client.cpp | 21 +++ .../src/proxy/net_conn_service_proxy.cpp | 45 +++++ .../netconnclient/include/net_conn_client.h | 3 + .../include/proxy/conn_ipc_interface_code.h | 2 + .../include/proxy/i_net_conn_service.h | 2 + .../include/proxy/net_conn_service_proxy.h | 3 +- .../netconnclient/libnetconn_kits.map | 2 + netmanager_base_config.gni | 1 + services/common/BUILD.gn | 3 + services/common/include/app_state_aware.h | 59 +++++++ services/common/src/app_state_aware.cpp | 139 +++++++++++++++ services/netconnmanager/BUILD.gn | 6 + .../netconnmanager/include/net_activate.h | 12 +- .../include/net_conn_callback_proxy_wrapper.h | 50 ++++++ .../netconnmanager/include/net_conn_service.h | 9 + .../include/stub/net_conn_service_stub.h | 2 + services/netconnmanager/src/net_activate.cpp | 61 +++++++ .../src/net_conn_callback_proxy_wrapper.cpp | 104 +++++++++++ .../netconnmanager/src/net_conn_service.cpp | 164 +++++++++++++++--- .../src/stub/net_conn_service_stub.cpp | 41 +++++ .../net_conn_service_test.cpp | 19 ++ .../net_conn_multi_test/net_activate_test.cpp | 28 +++ .../net_conn_client_test.cpp | 18 ++ .../net_conn_service_stub_test.cpp | 21 +++ .../net_conn_service_stub_test.h | 10 ++ .../net_conn_service_stub_test.h | 10 ++ 27 files changed, 809 insertions(+), 29 deletions(-) create mode 100644 services/common/include/app_state_aware.h create mode 100644 services/common/src/app_state_aware.cpp create mode 100644 services/netconnmanager/include/net_conn_callback_proxy_wrapper.h create mode 100644 services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp diff --git a/bundle.json b/bundle.json index c59db4a89..97c7ce034 100644 --- a/bundle.json +++ b/bundle.json @@ -33,7 +33,8 @@ "netmanager_base_support_ebpf_memory_miniaturization", "netmanager_base_enable_traffic_statistic", "netmanager_base_extended_features", - "netmanager_base_share_traffic_limit_enable" + "netmanager_base_share_traffic_limit_enable", + "netmanager_base_enable_set_app_frozened" ], "adapted_system_type": [ "standard" diff --git a/frameworks/native/netconnclient/src/net_conn_client.cpp b/frameworks/native/netconnclient/src/net_conn_client.cpp index f365b5299..56b49147b 100755 --- a/frameworks/native/netconnclient/src/net_conn_client.cpp +++ b/frameworks/native/netconnclient/src/net_conn_client.cpp @@ -1169,5 +1169,26 @@ int32_t NetConnClient::GetSpecificNet(NetBearType bearerType, std::list } return proxy->GetSpecificNet(bearerType, netIdList); } + +int32_t NetConnClient::SetAppIsFrozened(uint32_t uid, bool isFrozened) +{ + sptr proxy= GetProxy(); + if (proxy == nullptr) { + NETMGR_LOG_E("proxy is nullptr"); + return NETMANAGER_ERR_GET_PROXY_FAIL; + } + return proxy->SetAppIsFrozened(uid, isFrozened); +} + +int32_t NetConnClient::EnableAppFrozenedCallbackLimitation(bool flag) +{ + sptr proxy= GetProxy(); + if (proxy == nullptr) { + NETMGR_LOG_E("proxy is nullptr"); + return NETMANAGER_ERR_GET_PROXY_FAIL; + } + return proxy->EnableAppFrozenedCallbackLimitation(flag); +} + } // namespace NetManagerStandard } // namespace OHOS diff --git a/frameworks/native/netconnclient/src/proxy/net_conn_service_proxy.cpp b/frameworks/native/netconnclient/src/proxy/net_conn_service_proxy.cpp index 02f3c8eda..142804ed4 100644 --- a/frameworks/native/netconnclient/src/proxy/net_conn_service_proxy.cpp +++ b/frameworks/native/netconnclient/src/proxy/net_conn_service_proxy.cpp @@ -1960,5 +1960,50 @@ int32_t NetConnServiceProxy::CloseSocketsUid(int32_t netId, uint32_t uid) } return reply.ReadInt32(); } + +int32_t NetConnServiceProxy::SetAppIsFrozened(uint32_t uid, bool isFrozened) +{ + MessageParcel data; + MessageParcel reply; + if (!WriteInterfaceToken(data)) { + NETMGR_LOG_E("WriteInterfaceToken failed"); + return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + if (!data.WriteUint32(uid)) { + NETMGR_LOG_E("WriteInt32 failed"); + return NETMANAGER_ERR_WRITE_DATA_FAIL; + } + if (!data.WriteBool(isFrozened)) { + NETMGR_LOG_E("WriteBool failed"); + return NETMANAGER_ERR_WRITE_DATA_FAIL; + } + int32_t retCode = RemoteSendRequest(static_cast(ConnInterfaceCode::CMD_NM_SET_APP_IS_FROZENED), + data, reply); + if (retCode != NETMANAGER_SUCCESS) { + return retCode; + } + return reply.ReadInt32(); +} + +int32_t NetConnServiceProxy::EnableAppFrozenedCallbackLimitation(bool flag) +{ + MessageParcel data; + MessageParcel reply; + if (!WriteInterfaceToken(data)) { + NETMGR_LOG_E("WriteInterfaceToken failed"); + return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL; + } + if (!data.WriteBool(flag)) { + NETMGR_LOG_E("WriteBool failed"); + return NETMANAGER_ERR_WRITE_DATA_FAIL; + } + int32_t retCode = RemoteSendRequest(static_cast( + ConnInterfaceCode::CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION), data, reply); + if (retCode != NETMANAGER_SUCCESS) { + return retCode; + } + return reply.ReadInt32(); +} + } // namespace NetManagerStandard } // namespace OHOS diff --git a/interfaces/innerkits/netconnclient/include/net_conn_client.h b/interfaces/innerkits/netconnclient/include/net_conn_client.h index 9eab8ce0a..161ff0858 100644 --- a/interfaces/innerkits/netconnclient/include/net_conn_client.h +++ b/interfaces/innerkits/netconnclient/include/net_conn_client.h @@ -511,6 +511,9 @@ public: int32_t GetPacUrl(std::string &pacUrl); int32_t GetSpecificNet(NetBearType bearerType, std::list &netIdList); + + int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened); + int32_t EnableAppFrozenedCallbackLimitation(bool flag); private: class NetConnDeathRecipient : public IRemoteObject::DeathRecipient { public: diff --git a/interfaces/innerkits/netconnclient/include/proxy/conn_ipc_interface_code.h b/interfaces/innerkits/netconnclient/include/proxy/conn_ipc_interface_code.h index 47e78b4af..124bb4936 100644 --- a/interfaces/innerkits/netconnclient/include/proxy/conn_ipc_interface_code.h +++ b/interfaces/innerkits/netconnclient/include/proxy/conn_ipc_interface_code.h @@ -86,6 +86,8 @@ enum class ConnInterfaceCode { CMD_NM_SET_INTERFACE_DOWN, CMD_NM_SET_PAC_URL, CMD_NM_GET_PAC_URL, + CMD_NM_SET_APP_IS_FROZENED, + CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION, CMD_NM_END, }; diff --git a/interfaces/innerkits/netconnclient/include/proxy/i_net_conn_service.h b/interfaces/innerkits/netconnclient/include/proxy/i_net_conn_service.h index 00f1cd29f..be28d5d9f 100644 --- a/interfaces/innerkits/netconnclient/include/proxy/i_net_conn_service.h +++ b/interfaces/innerkits/netconnclient/include/proxy/i_net_conn_service.h @@ -118,6 +118,8 @@ public: virtual int32_t CloseSocketsUid(int32_t netId, uint32_t uid) = 0; virtual int32_t SetPacUrl(const std::string &pacUrl) = 0; virtual int32_t GetPacUrl(std::string &pacUrl) = 0; + virtual int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened) = 0; + virtual int32_t EnableAppFrozenedCallbackLimitation(bool flag) = 0; }; } // namespace NetManagerStandard } // namespace OHOS diff --git a/interfaces/innerkits/netconnclient/include/proxy/net_conn_service_proxy.h b/interfaces/innerkits/netconnclient/include/proxy/net_conn_service_proxy.h index 95da049e1..294f652c2 100644 --- a/interfaces/innerkits/netconnclient/include/proxy/net_conn_service_proxy.h +++ b/interfaces/innerkits/netconnclient/include/proxy/net_conn_service_proxy.h @@ -99,7 +99,8 @@ public: int32_t CloseSocketsUid(int32_t netId, uint32_t uid) override; int32_t SetPacUrl(const std::string &pacUrl) override; int32_t GetPacUrl(std::string &pacUrl) override; - + virtual int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened) override; + virtual int32_t EnableAppFrozenedCallbackLimitation(bool flag) override; private: bool WriteInterfaceToken(MessageParcel &data); int32_t GetNetCapData(MessageParcel &reply, NetAllCapabilities &netAllCap); diff --git a/interfaces/innerkits/netconnclient/libnetconn_kits.map b/interfaces/innerkits/netconnclient/libnetconn_kits.map index af7572398..3610908a4 100644 --- a/interfaces/innerkits/netconnclient/libnetconn_kits.map +++ b/interfaces/innerkits/netconnclient/libnetconn_kits.map @@ -250,6 +250,8 @@ "OHOS::NetManagerStandard::NetConnClient::CloseSocketsUid(int, unsigned int)"; "OHOS::NetManagerStandard::NetConnClient::DlCloseRemoveDeathRecipient()"; "OHOS::NetManagerStandard::NetConnClient::GetSpecificNet(OHOS::NetManagerStandard::NetBearType, std::__h::list>&)"; + "OHOS::NetManagerStandard::NetConnClient::SetAppIsFrozened(unsigned int, bool)"; + "OHOS::NetManagerStandard::NetConnClient::EnableAppFrozenedCallbackLimitation(bool)"; }; local: *; diff --git a/netmanager_base_config.gni b/netmanager_base_config.gni index 7a1680a30..e70903091 100644 --- a/netmanager_base_config.gni +++ b/netmanager_base_config.gni @@ -70,5 +70,6 @@ declare_args() { netmanager_base_enable_traffic_statistic = false netmanager_base_extended_features = true netmanager_base_share_traffic_limit_enable = false + netmanager_base_enable_set_app_frozened = false } fuzz_test_path = "netmanager_base/netmanager_base" diff --git a/services/common/BUILD.gn b/services/common/BUILD.gn index 7f1a825ea..38e2dd519 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -104,6 +104,7 @@ ohos_shared_library("net_service_common") { "src/net_manager_center.cpp", "src/net_settings.cpp", "src/route_utils.cpp", + "src/app_state_aware.cpp", ] include_dirs = [ @@ -142,6 +143,8 @@ ohos_shared_library("net_service_common") { "common_event_service:cesfwk_innerkits", "ffrt:libffrt", "ipc:ipc_core", + "ability_runtime:app_manager", + "samgr:samgr_proxy", ] defines = [ diff --git a/services/common/include/app_state_aware.h b/services/common/include/app_state_aware.h new file mode 100644 index 000000000..205672b9d --- /dev/null +++ b/services/common/include/app_state_aware.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021-2022 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 APP_STATE_AWARE_H +#define 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 { +namespace NetManagerStandard { + +constexpr uint32_t MAX_RETRY_COUNT = 3; + +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(); + void RegisterAppStateAwareCallback(const AppStateAwareCallback &appStateAwareCallback); + void RegisterAppStateObserver(); + 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; +}; +} // namespace NetManagerStandard +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp new file mode 100644 index 000000000..32d181884 --- /dev/null +++ b/services/common/src/app_state_aware.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2023 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 "ability_manager_client.h" +#include "system_ability_definition.h" +#include "net_mgr_log_wrapper.h" + +namespace OHOS { +namespace NetManagerStandard { + +AppStateAwareManager::AppStateAwareManager() +{ + SubscribeAppState(); +} + +AppStateAwareManager::~AppStateAwareManager() +{ + UnSubscribeAppState(); +} + +AppStateAwareManager &AppStateAwareManager::GetInstance() +{ + 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_) { + NETMGR_LOG_I("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) { + NETMGR_LOG_I("SubscribeAppState error, code = %{public}d", err); + appStateObserver_ = nullptr; + return false; + } + return true; +} + +void AppStateAwareManager::UnSubscribeAppState() +{ + NETMGR_LOG_I("UnSubscribeAppState start"); + std::lock_guard lock(mutex_); + if (!appStateObserver_) { + NETMGR_LOG_I("UnSubscribeAppState: appStateObserver_ is nullptr"); + return; + } + sptr appMgrProxy = GetAppMgr(); + if (appMgrProxy) { + appMgrProxy->UnregisterApplicationStateObserver(appStateObserver_); + appStateObserver_ = nullptr; + retryCount_ = 0; + } + NETMGR_LOG_I("UnSubscribeAppState end"); +} + +void AppStateAwareManager::RegisterAppStateAwareCallback(const AppStateAwareCallback &appStateAwareCallback) +{ + appStateAwareCallback_ = appStateAwareCallback; +} + +sptr AppStateAwareManager::GetAppMgr() +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + NETMGR_LOG_I("get SystemAbilityManager failed"); + return nullptr; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(APP_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + NETMGR_LOG_I("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 = foregroundAppUid_.load(); + return foregroundAppUid == uid; +} + +void AppStateAwareManager::OnForegroundApplicationChanged( + const AppExecFwk::AppStateData &appStateData) +{ + if (foregroundAppUid_ != appStateData.uid) { + foregroundAppUid_ = appStateData.uid; + if (appStateAwareCallback_.OnForegroundAppChanged != nullptr) { + appStateAwareCallback_.OnForegroundAppChanged(foregroundAppUid_); + } + } +} + +void AppStateObserver::OnForegroundApplicationChanged( + const AppExecFwk::AppStateData &appStateData) +{ + NETMGR_LOG_I("%{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 NetManagerStandard +} // namespace OHOS \ No newline at end of file diff --git a/services/netconnmanager/BUILD.gn b/services/netconnmanager/BUILD.gn index 5ef2f38b2..a88e721a9 100644 --- a/services/netconnmanager/BUILD.gn +++ b/services/netconnmanager/BUILD.gn @@ -52,6 +52,7 @@ ohos_shared_library("net_conn_manager") { "src/net_conn_event_handler.cpp", "src/net_conn_service.cpp", "src/net_conn_service_iface.cpp", + "src/net_conn_callback_proxy_wrapper.cpp", "src/net_datashare_utils.cpp", "src/net_factoryreset_callback.cpp", "src/net_http_probe.cpp", @@ -149,6 +150,10 @@ ohos_shared_library("net_conn_manager") { defines += [ "FEATURE_SUPPORT_POWERMANAGER" ] } + if (netmanager_base_enable_set_app_frozened) { + defines += [ "ENABLE_SET_APP_FROZENED" ] + } + external_deps += [ "hilog:libhilog" ] part_name = "netmanager_base" @@ -174,6 +179,7 @@ ohos_static_library("net_conn_manager_static") { "src/net_conn_event_handler.cpp", "src/net_conn_service.cpp", "src/net_conn_service_iface.cpp", + "src/net_conn_callback_proxy_wrapper.cpp", "src/net_datashare_utils.cpp", "src/net_factoryreset_callback.cpp", "src/net_http_probe.cpp", diff --git a/services/netconnmanager/include/net_activate.h b/services/netconnmanager/include/net_activate.h index 30105e28a..790eac802 100644 --- a/services/netconnmanager/include/net_activate.h +++ b/services/netconnmanager/include/net_activate.h @@ -60,7 +60,13 @@ public: std::set GetBearType() const; void StartTimeOutNetAvailable(); uint32_t GetUid() const; - + bool IsAppFrozened() const; + void SetIsAppFrozened(bool isFrozened); + CallbackType GetLastCallbackType() const; + void SetLastCallbackType(CallbackType callbackType); + bool IsAllowCallback(CallbackType callbackType); + sptr GetLastServiceSupply(); + void SetLastServiceSupply(sptr lastNetServiceSupplied); private: bool CompareByNetworkIdent(const std::string &ident, NetBearType bearerType, bool skipCheckIdent); bool CompareByNetworkCapabilities(const NetCaps &netCaps); @@ -81,6 +87,10 @@ private: std::string activateName_ = ""; uint32_t uid_ = 0; int32_t registerType_ = REGISTER; + std::atomic isAppFrozened_ = false; + std::atomic lastCallbackType_ = 0; + sptr lastNetServiceSupplied_ = nullptr; + std::mutex lastNetServiceSuppliedMutex_; }; } // namespace NetManagerStandard } // namespace OHOS diff --git a/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h b/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h new file mode 100644 index 000000000..4fab4446b --- /dev/null +++ b/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021-2022 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 NET_CONN_CALLBACK_PROXY_WRAPPER_H +#define NET_CONN_CALLBACK_PROXY_WRAPPER_H + +#include "iremote_proxy.h" +#include "i_net_conn_callback.h" +#include "net_activate.h" + +namespace OHOS { +namespace NetManagerStandard { +class NetConnCallbackProxyWrapper : public INetConnCallback { +public: + NetConnCallbackProxyWrapper(const sptr &callback); + ~NetConnCallbackProxyWrapper(); + +public: + int32_t NetAvailable(sptr &netHandle) override; + int32_t NetCapabilitiesChange(sptr &netHandle, const sptr &netAllCap) override; + int32_t NetConnectionPropertiesChange(sptr &netHandle, const sptr &info) override; + int32_t NetLost(sptr &netHandle) override; + int32_t NetUnavailable() override; + int32_t NetBlockStatusChange(sptr &netHandle, bool blocked) override; + sptr AsObject() override; + void SetNetActivate(std::shared_ptr netActivate); + +private: + bool IsAllowCallback(CallbackType callbackType); + +private: + sptr netConnCallback_ = nullptr; + std::weak_ptr netActivate_; +}; + +} // namespace NetManagerStandard +} // namespace OHOS +#endif // NET_CONN_CALLBACK_PROXY_H diff --git a/services/netconnmanager/include/net_conn_service.h b/services/netconnmanager/include/net_conn_service.h index c09c456ab..da2179216 100644 --- a/services/netconnmanager/include/net_conn_service.h +++ b/services/netconnmanager/include/net_conn_service.h @@ -47,6 +47,7 @@ #include "common_event_subscriber.h" #include "common_event_support.h" #include "os_account_manager.h" +#include "app_state_aware.h" namespace OHOS { namespace NetManagerStandard { @@ -383,6 +384,8 @@ public: int32_t CloseSocketsUid(int32_t netId, uint32_t uid) override; int32_t SetPacUrl(const std::string &pacUrl) override; int32_t GetPacUrl(std::string &pacUrl) override; + int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened) override; + int32_t EnableAppFrozenedCallbackLimitation(bool flag) override; private: class NetInterfaceStateCallback : public NetsysControllerCallback { @@ -541,6 +544,10 @@ private: const std::string &dstAddr); int32_t DisableDistributedNetAsync(bool isServer); int32_t CloseSocketsUidAsync(int32_t netId, uint32_t uid); + int32_t SetAppIsFrozenedAsync(uint32_t uid, bool isFrozened); + int32_t EnableAppFrozenedCallbackLimitationAsync(bool flag); + void HandleCallback(sptr &supplier, sptr &netHandle, + sptr callback, CallbackType type); // for NET_CAPABILITY_INTERNAL_DEFAULT bool IsInRequestNetUids(int32_t uid); @@ -600,6 +607,8 @@ private: static constexpr int32_t ROOT_USER_ID = 0; int32_t currentUserId_ = INVALID_USER_ID; bool isFallbackProbeWithProxy_ = false; + AppStateAwareCallback appStateAwareCallback_; + std::atomic enableAppFrozenedCallbackLimitation_ = false; private: class ConnCallbackDeathRecipient : public IRemoteObject::DeathRecipient { diff --git a/services/netconnmanager/include/stub/net_conn_service_stub.h b/services/netconnmanager/include/stub/net_conn_service_stub.h index c5a3395a1..b1cdbbd8f 100644 --- a/services/netconnmanager/include/stub/net_conn_service_stub.h +++ b/services/netconnmanager/include/stub/net_conn_service_stub.h @@ -110,6 +110,8 @@ private: int32_t OnCloseSocketsUid(MessageParcel &data, MessageParcel &reply); int32_t OnGetPacUrl(MessageParcel &data, MessageParcel &reply); int32_t OnSetPacUrl(MessageParcel &data, MessageParcel &reply); + int32_t OnSetAppIsFrozened(MessageParcel &data, MessageParcel &reply); + int32_t OnEnableAppFrozenedCallbackLimitation(MessageParcel &data, MessageParcel &reply); private: std::map memberFuncMap_; }; diff --git a/services/netconnmanager/src/net_activate.cpp b/services/netconnmanager/src/net_activate.cpp index d35bd69d4..583a9e9f4 100644 --- a/services/netconnmanager/src/net_activate.cpp +++ b/services/netconnmanager/src/net_activate.cpp @@ -19,6 +19,7 @@ #include "net_activate.h" #include "net_caps.h" #include "net_mgr_log_wrapper.h" +#include "app_state_aware.h" namespace OHOS { namespace NetManagerStandard { @@ -237,5 +238,65 @@ uint32_t NetActivate::GetUid() const { return uid_; } + +bool NetActivate::IsAppFrozened() const +{ + bool isAppFrozened = isAppFrozened_.load(); + return isAppFrozened; +} + +void NetActivate::SetIsAppFrozened(bool isFrozened) +{ + isAppFrozened_ = isFrozened; +} + +CallbackType NetActivate::GetLastCallbackType() const +{ + int32_t lastCallbackType = lastCallbackType_; + return static_cast(lastCallbackType); +} + +void NetActivate::SetLastCallbackType(CallbackType callbackType) +{ + if (callbackType == CALL_TYPE_UPDATE_CAP || callbackType == CALL_TYPE_UPDATE_LINK) { + return; + } + lastCallbackType_ = callbackType; +} + + +sptr NetActivate::GetLastServiceSupply() +{ + std::lock_guard lock(lastNetServiceSuppliedMutex_); + return lastNetServiceSupplied_; +} + +void NetActivate::SetLastServiceSupply(sptr lastNetServiceSupplied) +{ + std::lock_guard lock(lastNetServiceSuppliedMutex_); + lastNetServiceSupplied_ = lastNetServiceSupplied; +} + +bool NetActivate::IsAllowCallback(CallbackType callbackType) +{ + bool isAppFrozened = isAppFrozened_.load(); + bool isForegroundApp = AppStateAwareManager::GetInstance().IsForegroundApp(uid_); + if (isAppFrozened && !isForegroundApp) { + if (lastCallbackType_ == CALL_TYPE_LOST && callbackType != CALL_TYPE_LOST) { + SetLastServiceSupply(nullptr); + } + if (lastCallbackType_ != CALL_TYPE_LOST && callbackType == CALL_TYPE_LOST) { + SetLastServiceSupply(netServiceSupplied_); + } + SetLastCallbackType(callbackType); + + NETMGR_LOG_I("UID[%{public}d] is AppFrozened, not Allow send callbackType[%{public}d]", + uid_, callbackType); + return false; + } + NETMGR_LOG_I("UID[%{public}d] Allow send callbackType[%{public}d]", uid_, callbackType); + return true; +} + } // namespace NetManagerStandard } // namespace OHOS diff --git a/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp b/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp new file mode 100644 index 000000000..e2ab5719a --- /dev/null +++ b/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2021-2023 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 "net_conn_callback_proxy_wrapper.h" + +namespace OHOS { +namespace NetManagerStandard { +NetConnCallbackProxyWrapper::NetConnCallbackProxyWrapper(const sptr &callback) + : netConnCallback_(callback) {} + +NetConnCallbackProxyWrapper::~NetConnCallbackProxyWrapper() {} + +int32_t NetConnCallbackProxyWrapper::NetAvailable(sptr &netHandle) +{ + if (IsAllowCallback(CALL_TYPE_AVAILABLE)) { + return netConnCallback_->NetAvailable(netHandle); + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnCallbackProxyWrapper::NetCapabilitiesChange( + sptr &netHandle, const sptr &netAllCap) +{ + if (IsAllowCallback(CALL_TYPE_UPDATE_CAP)) { + return netConnCallback_->NetCapabilitiesChange(netHandle, netAllCap); + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnCallbackProxyWrapper::NetConnectionPropertiesChange + (sptr &netHandle, const sptr &info) +{ + if (IsAllowCallback(CALL_TYPE_UPDATE_LINK)) { + return netConnCallback_->NetConnectionPropertiesChange(netHandle, info); + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnCallbackProxyWrapper::NetLost(sptr &netHandle) +{ + if (IsAllowCallback(CALL_TYPE_LOST)) { + return netConnCallback_->NetLost(netHandle); + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnCallbackProxyWrapper::NetUnavailable() +{ + if (IsAllowCallback(CALL_TYPE_UNAVAILABLE)) { + return netConnCallback_->NetUnavailable(); + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnCallbackProxyWrapper::NetBlockStatusChange(sptr &netHandle, bool blocked) +{ + if (IsAllowCallback(CALL_TYPE_BLOCK_STATUS)) { + return netConnCallback_->NetBlockStatusChange(netHandle, blocked); + } + return NETMANAGER_SUCCESS; +} + +sptr NetConnCallbackProxyWrapper::AsObject() +{ + if (netConnCallback_ == nullptr) { + return nullptr; + } + return netConnCallback_->AsObject(); +} + +void NetConnCallbackProxyWrapper::SetNetActivate(std::shared_ptr netActivate) +{ + netActivate_ = netActivate; +} + +bool NetConnCallbackProxyWrapper::IsAllowCallback(CallbackType callback) +{ + if (netConnCallback_ == nullptr) { + return false; + } + auto netActivate = netActivate_.lock(); + if (netActivate) { + bool isAllow = netActivate->IsAllowCallback(callback); + if (!isAllow) { + return false; + } + } + return true; +} + +} // namespace NetManagerStandard +} // namespace OHOS diff --git a/services/netconnmanager/src/net_conn_service.cpp b/services/netconnmanager/src/net_conn_service.cpp index c5df40aab..7a9e8a1e4 100644 --- a/services/netconnmanager/src/net_conn_service.cpp +++ b/services/netconnmanager/src/net_conn_service.cpp @@ -34,6 +34,7 @@ #include "event_report.h" #include "net_activate.h" #include "net_conn_service.h" +#include "net_conn_callback_proxy_wrapper.h" #include "net_conn_types.h" #include "net_policy_client.h" #include "net_datashare_utils.h" @@ -183,6 +184,22 @@ bool NetConnService::Init() AddSystemAbilityListener(ACCESS_TOKEN_MANAGER_SERVICE_ID); AddSystemAbilityListener(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID); AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); + + #ifdef ENABLE_SET_APP_FROZENED + if (netConnEventHandler_) { + int64_t delayTime = 3000; + appStateAwareCallback_.OnForegroundAppChanged = [] (const uint32_t uid) { + std::shared_ptr netConnService = NetConnService::GetInstance(); + if (netConnService) { + netConnService ->SetAppIsFrozened(uid, false); + } + }; + netConnEventHandler_->PostAsyncTask([this]() { + AppStateAwareManager::GetInstance().RegisterAppStateAwareCallback(appStateAwareCallback_); + }, + delayTime); + } + #endif NETMGR_LOG_I("Init end"); return true; } @@ -1132,8 +1149,12 @@ int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, return NETMANAGER_ERR_PARAMETER_ERROR; } std::weak_ptr timeoutCb = shared_from_this(); + + sptr callbakWrapper = new (std::nothrow) NetConnCallbackProxyWrapper(callback); std::shared_ptr request = std::make_shared( - netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + netSpecifier, callbakWrapper, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + callbakWrapper->SetNetActivate(request); + request->StartTimeOutNetAvailable(); uint32_t reqId = request->GetRequestId(); NETMGR_LOG_I("New request [id:%{public}u]", reqId); @@ -1527,33 +1548,45 @@ void NetConnService::CallbackForSupplier(sptr &supplier, CallbackTy } sptr callback = reqIt->second->GetNetCallback(); sptr netHandle = supplier->GetNetHandle(); - switch (type) { - case CALL_TYPE_LOST: - callback->NetLost(netHandle); - break; - case CALL_TYPE_UPDATE_CAP: { - sptr pNetAllCap = std::make_unique().release(); - std::lock_guard locker(netManagerMutex_); - *pNetAllCap = supplier->GetNetCapabilities(); - callback->NetCapabilitiesChange(netHandle, pNetAllCap); - break; - } - case CALL_TYPE_UPDATE_LINK: { - sptr pInfo = std::make_unique().release(); - auto network = supplier->GetNetwork(); - if (network != nullptr && pInfo != nullptr) { - *pInfo = network->GetNetLinkInfo(); - } - callback->NetConnectionPropertiesChange(netHandle, pInfo); - break; + HandleCallback(supplier, netHandle, callback, type); + } +} + +void NetConnService::HandleCallback(sptr &supplier, sptr &netHandle, + sptr callback, CallbackType type) +{ + switch (type) { + case CALL_TYPE_LOST: { + callback->NetLost(netHandle); + break; + } + case CALL_TYPE_UPDATE_CAP: { + sptr pNetAllCap = std::make_unique().release(); + std::lock_guard locker(netManagerMutex_); + *pNetAllCap = supplier->GetNetCapabilities(); + callback->NetCapabilitiesChange(netHandle, pNetAllCap); + break; + } + case CALL_TYPE_UPDATE_LINK: { + sptr pInfo = std::make_unique().release(); + auto network = supplier->GetNetwork(); + if (network != nullptr && pInfo != nullptr) { + *pInfo = network->GetNetLinkInfo(); } - case CALL_TYPE_BLOCK_STATUS: - callback->NetBlockStatusChange(netHandle, NetManagerCenter::GetInstance().IsUidNetAccess( - supplier->GetSupplierUid(), supplier->HasNetCap(NET_CAPABILITY_NOT_METERED))); - break; - default: - break; + callback->NetConnectionPropertiesChange(netHandle, pInfo); + break; + } + case CALL_TYPE_BLOCK_STATUS: { + callback->NetBlockStatusChange(netHandle, NetManagerCenter::GetInstance().IsUidNetAccess( + supplier->GetSupplierUid(), supplier->HasNetCap(NET_CAPABILITY_NOT_METERED))); + break; + } + case CALL_TYPE_UNAVAILABLE: { + callback->NetUnavailable(); + break; } + default: + break; } } @@ -3195,6 +3228,7 @@ void NetConnService::RemoveALLClientDeathRecipient() item->AsObject()->RemoveDeathRecipient(deathRecipient_); } remoteCallback_.clear(); + appStateAwareCallback_.OnForegroundAppChanged = nullptr; deathRecipient_ = nullptr; } @@ -3493,5 +3527,83 @@ int32_t NetConnService::CloseSocketsUidAsync(int32_t netId, uint32_t uid) iterNetwork->second->CloseSocketsUid(uid); return NETMANAGER_SUCCESS; } + +int32_t NetConnService::SetAppIsFrozened(uint32_t uid, bool isFrozened) +{ + int32_t result = NETMANAGER_SUCCESS; + // #ifdef ENABLE_SET_APP_FROZENED + if (netConnEventHandler_ && enableAppFrozenedCallbackLimitation_.load()) { + netConnEventHandler_->PostSyncTask( + [this, uid, isFrozened, &result]() { result = this->SetAppIsFrozenedAsync(uid, isFrozened); }); + } + // #endif + return result; +} + +int32_t NetConnService::SetAppIsFrozenedAsync(uint32_t uid, bool isFrozened) +{ + std::vector> activates; + { + std::lock_guard guard(uidActivateMutex_); + auto it = netUidActivates_.find(uid); + if ((it == netUidActivates_.end())) { + return NETMANAGER_SUCCESS; + } + activates = it->second; + } + NETMGR_LOG_I("SetAppIsFrozenedAsync uid[%{public}d], isFrozened=[%{public}d].", uid, isFrozened); + for (auto iter = activates.begin(); iter != activates.end();++iter) { + auto curNetAct = (*iter); + if (curNetAct -> IsAppFrozened() == isFrozened) { + continue; + } + curNetAct -> SetIsAppFrozened(isFrozened); + if (isFrozened) { + continue; + } + sptr netSupplier = curNetAct -> GetServiceSupply(); + CallbackType callbackType = curNetAct -> GetLastCallbackType(); + if (callbackType == CALL_TYPE_UNKNOWN) { + continue; + } + if (netSupplier == nullptr) { + if (callbackType != CALL_TYPE_LOST) { + continue; + } + netSupplier = curNetAct -> GetLastServiceSupply(); + sptr callback = curNetAct -> GetNetCallback(); + if (netSupplier && callback) { + sptr netHandle = netSupplier ->GetNetHandle(); + callback->NetLost(netHandle); + } + curNetAct -> SetLastServiceSupply(nullptr); + } else if (callbackType == CALL_TYPE_AVAILABLE) { + CallbackForAvailable(netSupplier, curNetAct->GetNetCallback()); + } else { + CallbackForSupplier(netSupplier, callbackType); + } + curNetAct -> SetLastCallbackType(CALL_TYPE_UNKNOWN); + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnService::EnableAppFrozenedCallbackLimitation(bool flag) +{ + int32_t result = NETMANAGER_SUCCESS; + if (netConnEventHandler_) { + netConnEventHandler_->PostSyncTask([this, flag, &result]() { + result = this->EnableAppFrozenedCallbackLimitationAsync(flag); + }); + } + return result; +} + +int32_t NetConnService::EnableAppFrozenedCallbackLimitationAsync(bool flag) +{ + enableAppFrozenedCallbackLimitation_ = flag; + NETMGR_LOG_I("enableAppFrozenedCallbackLimitation_ = %{public}d", enableAppFrozenedCallbackLimitation_.load()); + return NETMANAGER_SUCCESS; +} + } // namespace NetManagerStandard } // namespace OHOS diff --git a/services/netconnmanager/src/stub/net_conn_service_stub.cpp b/services/netconnmanager/src/stub/net_conn_service_stub.cpp index cffd144d3..48e29016b 100644 --- a/services/netconnmanager/src/stub/net_conn_service_stub.cpp +++ b/services/netconnmanager/src/stub/net_conn_service_stub.cpp @@ -116,6 +116,10 @@ void NetConnServiceStub::InitInterfaceFuncToInterfaceMap() &NetConnServiceStub::OnUpdateSupplierScore, {Permission::CONNECTIVITY_INTERNAL}}; memberFuncMap_[static_cast(ConnInterfaceCode::CMD_NM_CLOSE_SOCKETS_UID)] = { &NetConnServiceStub::OnCloseSocketsUid, {Permission::CONNECTIVITY_INTERNAL}}; + memberFuncMap_[static_cast(ConnInterfaceCode::CMD_NM_SET_APP_IS_FROZENED)] = { + &NetConnServiceStub::OnSetAppIsFrozened, {Permission::CONNECTIVITY_INTERNAL}}; + memberFuncMap_[static_cast(ConnInterfaceCode::CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION)] = { + &NetConnServiceStub::OnEnableAppFrozenedCallbackLimitation, {Permission::CONNECTIVITY_INTERNAL}}; } void NetConnServiceStub::InitResetNetFuncToInterfaceMap() @@ -1814,5 +1818,42 @@ int32_t NetConnServiceStub::OnCloseSocketsUid(MessageParcel &data, MessageParcel } return NETMANAGER_SUCCESS; } + +int32_t NetConnServiceStub::OnSetAppIsFrozened(MessageParcel &data, MessageParcel &reply) +{ + uint32_t uid = 0; + if (!data.ReadUint32(uid)) { + NETMGR_LOG_E("ReadUint32 error."); + return NETMANAGER_ERR_READ_DATA_FAIL; + } + bool isFrozened = false; + if (!data.ReadBool(isFrozened)) { + NETMGR_LOG_E("ReadBool error."); + return NETMANAGER_ERR_READ_DATA_FAIL; + } + + int32_t ret = SetAppIsFrozened(uid, isFrozened); + if (!reply.WriteInt32(ret)) { + NETMGR_LOG_E("reply.WriteInt32 error"); + return NETMANAGER_ERR_WRITE_REPLY_FAIL; + } + return NETMANAGER_SUCCESS; +} + +int32_t NetConnServiceStub::OnEnableAppFrozenedCallbackLimitation(MessageParcel &data, MessageParcel &reply) +{ + bool flag = false; + if (!data.ReadBool(flag)) { + NETMGR_LOG_E("ReadBool error."); + return NETMANAGER_ERR_READ_DATA_FAIL; + } + + int32_t ret = EnableAppFrozenedCallbackLimitation(flag); + if (!reply.WriteInt32(ret)) { + NETMGR_LOG_E("reply.WriteInt32 error"); + return NETMANAGER_ERR_WRITE_REPLY_FAIL; + } + return NETMANAGER_SUCCESS; +} } // namespace NetManagerStandard } // namespace OHOS diff --git a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp index 6caa85318..a4cffb1ed 100644 --- a/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp +++ b/test/netconnmanager/unittest/net_conn_manager_test/net_conn_service_test.cpp @@ -1816,5 +1816,24 @@ HWTEST_F(NetConnServiceTest, GetPacUrlTest001, TestSize.Level1) ASSERT_EQ(ret, NETMANAGER_SUCCESS); } + +HWTEST_F(NetConnServiceTest, SetAppIsFrozenedTest001, TestSize.Level1) +{ + auto ret = NetConnService::GetInstance()->SetAppIsFrozened(20020177, true); + ASSERT_EQ(ret, NETMANAGER_SUCCESS); +} + +HWTEST_F(NetConnServiceTest, SetAppIsFrozenedTest002, TestSize.Level1) +{ + auto ret = NetConnService::GetInstance()->SetAppIsFrozened(20020177, false); + ASSERT_EQ(ret, NETMANAGER_SUCCESS); +} + +HWTEST_F(NetConnServiceTest, EnableAppFrozenedCallbackLimitationTest001, TestSize.Level1) +{ + auto ret = NetConnService::GetInstance()->EnableAppFrozenedCallbackLimitation(true); + ASSERT_EQ(ret, NETMANAGER_SUCCESS); +} + } // namespace NetManagerStandard } // namespace OHOS diff --git a/test/netconnmanager/unittest/net_conn_multi_test/net_activate_test.cpp b/test/netconnmanager/unittest/net_conn_multi_test/net_activate_test.cpp index cbc6e2dc5..f651c2439 100644 --- a/test/netconnmanager/unittest/net_conn_multi_test/net_activate_test.cpp +++ b/test/netconnmanager/unittest/net_conn_multi_test/net_activate_test.cpp @@ -280,5 +280,33 @@ HWTEST_F(NetActivateTest, NetActivateBranchTest001, TestSize.Level1) instance_->TimeOutNetAvailable(); EXPECT_TRUE(instance_->GetNetCallback() == nullptr); } + +HWTEST_F(NetActivateTest, IsAppFrozenedTest001, TestSize.Level1) +{ + instance_->SetIsAppFrozened(true); + auto ret = instance_->IsAppFrozened(); + EXPECT_TRUE(ret); +} + +HWTEST_F(NetActivateTest, LastCallbackTypeTest001, TestSize.Level1) +{ + instance_->SetLastCallbackType(CALL_TYPE_AVAILABLE); + auto ret = instance_->GetLastCallbackType(); + EXPECT_TRUE(ret == CALL_TYPE_AVAILABLE); +} + +HWTEST_F(NetActivateTest, GetLastServiceSupplyTest001, TestSize.Level1) +{ + instance_->SetLastServiceSupply(nullptr); + auto ret = instance_->GetLastServiceSupply(); + EXPECT_TRUE(ret == nullptr); +} + +HWTEST_F(NetActivateTest, IsAllowCallbackTest001, TestSize.Level1) +{ + auto ret = instance_->IsAllowCallback(CALL_TYPE_AVAILABLE); + EXPECT_FALSE(ret); +} + } // namespace NetManagerStandard } // namespace OHOS \ No newline at end of file diff --git a/test/netconnmanager/unittest/net_conn_multi_test/net_conn_client_test.cpp b/test/netconnmanager/unittest/net_conn_multi_test/net_conn_client_test.cpp index 37f308b39..f08086180 100644 --- a/test/netconnmanager/unittest/net_conn_multi_test/net_conn_client_test.cpp +++ b/test/netconnmanager/unittest/net_conn_multi_test/net_conn_client_test.cpp @@ -1616,5 +1616,23 @@ HWTEST_F(NetConnClientTest, IsCleartextPermitted002, TestSize.Level1) EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED); } +HWTEST_F(NetConnClientTest, SetAppIsFrozenedTest001, TestSize.Level1) +{ + auto ret = NetConnClient::GetInstance().SetAppIsFrozened(20020177, true); + ASSERT_TRUE(ret == NETMANAGER_SUCCESS || ret == NETMANAGER_ERR_PERMISSION_DENIED); +} + +HWTEST_F(NetConnClientTest, SetAppIsFrozenedTest002, TestSize.Level1) +{ + auto ret = NetConnClient::GetInstance().SetAppIsFrozened(20020177, false); + ASSERT_TRUE(ret == NETMANAGER_SUCCESS || ret == NETMANAGER_ERR_PERMISSION_DENIED); +} + +HWTEST_F(NetConnClientTest, EnableAppFrozenedCallbackLimitationTest001, TestSize.Level1) +{ + auto ret = NetConnClient::GetInstance().EnableAppFrozenedCallbackLimitation(false); + ASSERT_TRUE(ret == NETMANAGER_SUCCESS || ret == NETMANAGER_ERR_PERMISSION_DENIED); +} + } // namespace NetManagerStandard } // namespace OHOS diff --git a/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.cpp b/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.cpp index 866e17e26..190cd96c7 100644 --- a/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.cpp +++ b/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.cpp @@ -1251,5 +1251,26 @@ HWTEST_F(NetConnServiceStubTest, OnGetPacUrlTest001, TestSize.Level1) int32_t ret = SendRemoteRequest(data, ConnInterfaceCode::CMD_NM_GET_PAC_URL); EXPECT_EQ(ret, NETMANAGER_SUCCESS); } + +HWTEST_F(NetConnServiceStubTest, OnSetAppIsFrozenedTest001, TestSize.Level1) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(NetConnServiceStub::GetDescriptor())) { + return; + } + int32_t ret = SendRemoteRequest(data, ConnInterfaceCode::CMD_NM_SET_APP_IS_FROZENED); + EXPECT_NE(ret, NETMANAGER_ERR_PARAMETER_ERROR); +} + +HWTEST_F(NetConnServiceStubTest, OnEnableAppFrozenedCallbackLimitationTest001, TestSize.Level1) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(NetConnServiceStub::GetDescriptor())) { + return; + } + int32_t ret = SendRemoteRequest(data, ConnInterfaceCode::CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION); + EXPECT_NE(ret, NETMANAGER_ERR_PARAMETER_ERROR); +} + } // namespace NetManagerStandard } // namespace OHOS \ No newline at end of file diff --git a/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.h b/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.h index 14e1126fa..baee25745 100644 --- a/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.h +++ b/test/netconnmanager/unittest/net_conn_service_test/net_conn_service_stub_test.h @@ -359,6 +359,16 @@ public: { return 0; } + + int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened) override + { + return 0; + } + + int32_t EnableAppFrozenedCallbackLimitation(bool flag) override + { + return 0; + } }; } // namespace NetManagerStandard } // namespace OHOS diff --git a/test/netconnmanager/unittest/netmgr_distributed_test/net_conn_service_stub_test.h b/test/netconnmanager/unittest/netmgr_distributed_test/net_conn_service_stub_test.h index d022f665e..7db60a206 100644 --- a/test/netconnmanager/unittest/netmgr_distributed_test/net_conn_service_stub_test.h +++ b/test/netconnmanager/unittest/netmgr_distributed_test/net_conn_service_stub_test.h @@ -359,6 +359,16 @@ public: { return 0; } + + int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened) override + { + return 0; + } + + int32_t EnableAppFrozenedCallbackLimitation(bool flag) override + { + return 0; + } }; } // namespace NetManagerStandard } // namespace OHOS -- Gitee From 9ac7d38c71bc4e014f7149293a2af99b3497c52b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 10:31:23 +0800 Subject: [PATCH 02/21] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=86=B7=E5=86=BB?= =?UTF-8?q?=E4=B8=8D=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/BUILD.gn | 1 + .../netconnmanager/src/net_conn_service.cpp | 4 +-- .../net_conn_client_fuzzer.cpp | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/services/common/BUILD.gn b/services/common/BUILD.gn index 38e2dd519..d6fc1eff4 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -145,6 +145,7 @@ ohos_shared_library("net_service_common") { "ipc:ipc_core", "ability_runtime:app_manager", "samgr:samgr_proxy", + "cJSON:cjson", ] defines = [ diff --git a/services/netconnmanager/src/net_conn_service.cpp b/services/netconnmanager/src/net_conn_service.cpp index 7a9e8a1e4..06a43a8e3 100644 --- a/services/netconnmanager/src/net_conn_service.cpp +++ b/services/netconnmanager/src/net_conn_service.cpp @@ -3531,12 +3531,12 @@ int32_t NetConnService::CloseSocketsUidAsync(int32_t netId, uint32_t uid) int32_t NetConnService::SetAppIsFrozened(uint32_t uid, bool isFrozened) { int32_t result = NETMANAGER_SUCCESS; - // #ifdef ENABLE_SET_APP_FROZENED + #ifdef ENABLE_SET_APP_FROZENED if (netConnEventHandler_ && enableAppFrozenedCallbackLimitation_.load()) { netConnEventHandler_->PostSyncTask( [this, uid, isFrozened, &result]() { result = this->SetAppIsFrozenedAsync(uid, isFrozened); }); } - // #endif + #endif return result; } diff --git a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp index 8563665cd..38e8a529d 100644 --- a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp +++ b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp @@ -1504,6 +1504,36 @@ void CloseSocketsUidTest(const uint8_t *data, size_t size) OnRemoteRequest(static_cast(ConnInterfaceCode::CMD_NM_CLOSE_SOCKETS_UID), dataParcelNoIfName); } +void SetAppIsFrozenedTest(const uint8_t *data, size_t size) +{ + uint32_t uid = NetConnGetData(); + bool isFrozened = NetConnGetData(); + + MessageParcel dataParcel; + if (!IsConnClientDataAndSizeValid(data, size, dataParcel)) { + NETMGR_LOG_D("SetAppIsFrozenedTest write token failed or invalid parameter."); + return; + } + + dataParcel.WriteUint32(uid); + dataParcel.WriteBool(isFrozened); + OnRemoteRequest(static_cast(ConnInterfaceCode::CMD_NM_SET_APP_IS_FROZENED), dataParcel); +} + +void EnableAppFrozenedCallbackLimitationTest(const uint8_t *data, size_t size) +{ + bool flag = NetConnGetData(); + + MessageParcel dataParcel; + if (!IsConnClientDataAndSizeValid(data, size, dataParcel)) { + NETMGR_LOG_D("EnableAppFrozenedCallbackLimitationTest write token failed or invalid parameter."); + return; + } + + dataParcel.WriteBool(flag); + OnRemoteRequest(static_cast(ConnInterfaceCode::CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION), dataParcel); +} + void SetInterfaceUpFuzzTest(const uint8_t *data, size_t size) { NetManagerBaseAccessToken token; @@ -1609,5 +1639,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::NetManagerStandard::DisableDistributedNetFuzzTest(data, size); OHOS::NetManagerStandard::CloseSocketsUidTest(data, size); OHOS::NetManagerStandard::LLVMFuzzerTestOneInputNew(data, size); + OHOS::NetManagerStandard::SetAppIsFrozened(data, size); + OHOS::NetManagerStandard::EnableAppFrozenedCallbackLimitation(data, size); return 0; } -- Gitee From 182396cf497d087920e31df30ef4bc19a53e3d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 03:38:51 +0000 Subject: [PATCH 03/21] update services/common/src/app_state_aware.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/src/app_state_aware.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp index 32d181884..2a1339b6f 100644 --- a/services/common/src/app_state_aware.cpp +++ b/services/common/src/app_state_aware.cpp @@ -17,7 +17,7 @@ #include "app_mgr_constants.h" #include "iservice_registry.h" -#include "ability_manager_client.h" +#include #include "system_ability_definition.h" #include "net_mgr_log_wrapper.h" @@ -136,4 +136,4 @@ void AppStateObserver::OnForegroundApplicationChanged( } // namespace NetManagerStandard -} // namespace OHOS \ No newline at end of file +} // namespace OHOS -- Gitee From 35bd493c7ef3f01fad6249e1d991dfb8b34d1852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 03:50:00 +0000 Subject: [PATCH 04/21] update services/common/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/common/BUILD.gn b/services/common/BUILD.gn index d6fc1eff4..abed735be 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -145,7 +145,7 @@ ohos_shared_library("net_service_common") { "ipc:ipc_core", "ability_runtime:app_manager", "samgr:samgr_proxy", - "cJSON:cjson", + "jsoncpp:jsoncpp", ] defines = [ -- Gitee From 60b8bba3a9551c02ed0c46c6fc86fd7d55a92405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 04:41:28 +0000 Subject: [PATCH 05/21] update bundle.json. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- bundle.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bundle.json b/bundle.json index 97c7ce034..6163ddd99 100644 --- a/bundle.json +++ b/bundle.json @@ -82,7 +82,8 @@ "core_service", "distributed_notification_service", "i18n", - "netmanager_ext" + "netmanager_ext", + "jsoncpp" ] }, "build": { -- Gitee From c04b11f3d902924bec9014f228c8276a0acab2c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 06:10:09 +0000 Subject: [PATCH 06/21] update services/common/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/common/BUILD.gn b/services/common/BUILD.gn index abed735be..acfe9d661 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -138,14 +138,14 @@ ohos_shared_library("net_service_common") { external_deps = [ "ability_base:want", + "ability_runtime:app_manager", "bounds_checking_function:libsec_shared", "c_utils:utils", "common_event_service:cesfwk_innerkits", "ffrt:libffrt", "ipc:ipc_core", - "ability_runtime:app_manager", - "samgr:samgr_proxy", "jsoncpp:jsoncpp", + "samgr:samgr_proxy", ] defines = [ -- Gitee From 34d38fd7ccf9c7250f6e507b15086d9a96ce83b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 06:14:22 +0000 Subject: [PATCH 07/21] update test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp index 38e8a529d..32160538d 100644 --- a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp +++ b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp @@ -1639,7 +1639,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::NetManagerStandard::DisableDistributedNetFuzzTest(data, size); OHOS::NetManagerStandard::CloseSocketsUidTest(data, size); OHOS::NetManagerStandard::LLVMFuzzerTestOneInputNew(data, size); - OHOS::NetManagerStandard::SetAppIsFrozened(data, size); - OHOS::NetManagerStandard::EnableAppFrozenedCallbackLimitation(data, size); + OHOS::NetManagerStandard::SetAppIsFrozenedTest(data, size); + OHOS::NetManagerStandard::EnableAppFrozenedCallbackLimitationTest(data, size); return 0; } -- Gitee From 6ca5ea796fa7d21f05882ceb5e6d66e07d168f58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 06:58:02 +0000 Subject: [PATCH 08/21] update services/common/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/common/BUILD.gn b/services/common/BUILD.gn index acfe9d661..e37b71f5f 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -100,11 +100,11 @@ ohos_shared_library("net_service_common") { branch_protector_ret = "pac_ret" sources = [ + "src/app_state_aware.cpp", "src/broadcast_manager.cpp", "src/net_manager_center.cpp", "src/net_settings.cpp", "src/route_utils.cpp", - "src/app_state_aware.cpp", ] include_dirs = [ -- Gitee From e9e79bae090adc2ac3c4cb0e7ce9b7ea0402691f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 07:08:14 +0000 Subject: [PATCH 09/21] update test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- .../netconnclient_fuzzer/net_conn_client_fuzzer.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp index 32160538d..b3f997d76 100644 --- a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp +++ b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp @@ -1531,7 +1531,8 @@ void EnableAppFrozenedCallbackLimitationTest(const uint8_t *data, size_t size) } dataParcel.WriteBool(flag); - OnRemoteRequest(static_cast(ConnInterfaceCode::CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION), dataParcel); + OnRemoteRequest(static_cast(ConnInterfaceCode::CMD_NM_ENABLE_APP_FROZENED_CALLBACK_LIMITATION), + dataParcel); } void SetInterfaceUpFuzzTest(const uint8_t *data, size_t size) @@ -1582,6 +1583,8 @@ void LLVMFuzzerTestOneInputNew(const uint8_t *data, size_t size) OHOS::NetManagerStandard::SetInterfaceDownFuzzTest(data, size); OHOS::NetManagerStandard::SetNetInterfaceIpAddressFuzzTest(data, size); OHOS::NetManagerStandard::UnregisterNetInterfaceCallbackFuzzTest(data, size); + OHOS::NetManagerStandard::SetAppIsFrozenedTest(data, size); + OHOS::NetManagerStandard::EnableAppFrozenedCallbackLimitationTest(data, size); } } // namespace NetManagerStandard } // namespace OHOS @@ -1639,7 +1642,5 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::NetManagerStandard::DisableDistributedNetFuzzTest(data, size); OHOS::NetManagerStandard::CloseSocketsUidTest(data, size); OHOS::NetManagerStandard::LLVMFuzzerTestOneInputNew(data, size); - OHOS::NetManagerStandard::SetAppIsFrozenedTest(data, size); - OHOS::NetManagerStandard::EnableAppFrozenedCallbackLimitationTest(data, size); return 0; } -- Gitee From 541e6600daaa2b1172d3c6cce6628cc5489d012a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Fri, 28 Feb 2025 09:48:07 +0000 Subject: [PATCH 10/21] update services/netconnmanager/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/netconnmanager/BUILD.gn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/netconnmanager/BUILD.gn b/services/netconnmanager/BUILD.gn index a88e721a9..533282eab 100644 --- a/services/netconnmanager/BUILD.gn +++ b/services/netconnmanager/BUILD.gn @@ -49,10 +49,10 @@ ohos_shared_library("net_conn_manager") { "src/nat464_service.cpp", "src/net_activate.cpp", "src/net_caps.cpp", + "src/net_conn_callback_proxy_wrapper.cpp", "src/net_conn_event_handler.cpp", "src/net_conn_service.cpp", "src/net_conn_service_iface.cpp", - "src/net_conn_callback_proxy_wrapper.cpp", "src/net_datashare_utils.cpp", "src/net_factoryreset_callback.cpp", "src/net_http_probe.cpp", @@ -176,10 +176,10 @@ ohos_static_library("net_conn_manager_static") { "src/nat464_service.cpp", "src/net_activate.cpp", "src/net_caps.cpp", + "src/net_conn_callback_proxy_wrapper.cpp", "src/net_conn_event_handler.cpp", "src/net_conn_service.cpp", "src/net_conn_service_iface.cpp", - "src/net_conn_callback_proxy_wrapper.cpp", "src/net_datashare_utils.cpp", "src/net_factoryreset_callback.cpp", "src/net_http_probe.cpp", -- Gitee From df4d92dcc339b186028e3f85d80d187069c76f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Sun, 2 Mar 2025 15:32:55 +0800 Subject: [PATCH 11/21] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/netconnmanager/src/net_activate.cpp | 3 +- .../netconnmanager/src/net_conn_service.cpp | 60 +++++++++++-------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/services/netconnmanager/src/net_activate.cpp b/services/netconnmanager/src/net_activate.cpp index 583a9e9f4..56df33879 100644 --- a/services/netconnmanager/src/net_activate.cpp +++ b/services/netconnmanager/src/net_activate.cpp @@ -258,7 +258,8 @@ CallbackType NetActivate::GetLastCallbackType() const void NetActivate::SetLastCallbackType(CallbackType callbackType) { - if (callbackType == CALL_TYPE_UPDATE_CAP || callbackType == CALL_TYPE_UPDATE_LINK) { + if (callbackType == CALL_TYPE_UPDATE_CAP || callbackType == CALL_TYPE_UPDATE_LINK + && lastCallbackType_.load() == CALL_TYPE_AVAILABLE) { return; } lastCallbackType_ = callbackType; diff --git a/services/netconnmanager/src/net_conn_service.cpp b/services/netconnmanager/src/net_conn_service.cpp index 06a43a8e3..ff592f41a 100644 --- a/services/netconnmanager/src/net_conn_service.cpp +++ b/services/netconnmanager/src/net_conn_service.cpp @@ -185,21 +185,21 @@ bool NetConnService::Init() AddSystemAbilityListener(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID); AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID); - #ifdef ENABLE_SET_APP_FROZENED - if (netConnEventHandler_) { - int64_t delayTime = 3000; - appStateAwareCallback_.OnForegroundAppChanged = [] (const uint32_t uid) { - std::shared_ptr netConnService = NetConnService::GetInstance(); - if (netConnService) { - netConnService ->SetAppIsFrozened(uid, false); - } - }; - netConnEventHandler_->PostAsyncTask([this]() { - AppStateAwareManager::GetInstance().RegisterAppStateAwareCallback(appStateAwareCallback_); - }, - delayTime); - } - #endif +#ifdef ENABLE_SET_APP_FROZENED + if (netConnEventHandler_) { + int64_t delayTime = 3000; + appStateAwareCallback_.OnForegroundAppChanged = [] (const uint32_t uid) { + std::shared_ptr netConnService = NetConnService::GetInstance(); + if (netConnService) { + netConnService ->SetAppIsFrozened(uid, false); + } + }; + netConnEventHandler_->PostAsyncTask([this]() { + AppStateAwareManager::GetInstance().RegisterAppStateAwareCallback(appStateAwareCallback_); + }, + delayTime); + } +#endif NETMGR_LOG_I("Init end"); return true; } @@ -1150,10 +1150,22 @@ int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, } std::weak_ptr timeoutCb = shared_from_this(); + std::shared_ptr request = nullptr; +#ifdef ENABLE_SET_APP_FROZENED sptr callbakWrapper = new (std::nothrow) NetConnCallbackProxyWrapper(callback); - std::shared_ptr request = std::make_shared( - netSpecifier, callbakWrapper, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); - callbakWrapper->SetNetActivate(request); + if (callbakWrapper == nullptr) { + NETMGR_LOG_E("NetConnCallbackProxyWrapper ptr is null"); + request = std::make_shared( + netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + } else { + request = std::make_shared( + netSpecifier, callbakWrapper, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + callbakWrapper->SetNetActivate(request); + } +#else + request = std::make_shared( + netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); +#endif request->StartTimeOutNetAvailable(); uint32_t reqId = request->GetRequestId(); @@ -3531,12 +3543,12 @@ int32_t NetConnService::CloseSocketsUidAsync(int32_t netId, uint32_t uid) int32_t NetConnService::SetAppIsFrozened(uint32_t uid, bool isFrozened) { int32_t result = NETMANAGER_SUCCESS; - #ifdef ENABLE_SET_APP_FROZENED - if (netConnEventHandler_ && enableAppFrozenedCallbackLimitation_.load()) { - netConnEventHandler_->PostSyncTask( - [this, uid, isFrozened, &result]() { result = this->SetAppIsFrozenedAsync(uid, isFrozened); }); - } - #endif +#ifdef ENABLE_SET_APP_FROZENED + if (netConnEventHandler_ && enableAppFrozenedCallbackLimitation_.load()) { + netConnEventHandler_->PostSyncTask( + [this, uid, isFrozened, &result]() { result = this->SetAppIsFrozenedAsync(uid, isFrozened); }); + } +#endif return result; } -- Gitee From 63796975817d8ab063db27596567045324d01916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Sun, 2 Mar 2025 16:09:02 +0800 Subject: [PATCH 12/21] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- .../netconnmanager/include/net_conn_service.h | 4 ++ services/netconnmanager/src/net_activate.cpp | 4 +- .../netconnmanager/src/net_conn_service.cpp | 44 ++++++++++++------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/services/netconnmanager/include/net_conn_service.h b/services/netconnmanager/include/net_conn_service.h index da2179216..276711c9f 100644 --- a/services/netconnmanager/include/net_conn_service.h +++ b/services/netconnmanager/include/net_conn_service.h @@ -548,6 +548,10 @@ private: int32_t EnableAppFrozenedCallbackLimitationAsync(bool flag); void HandleCallback(sptr &supplier, sptr &netHandle, sptr callback, CallbackType type); + std::shared_ptr CreateNetActivateRequest(const sptr &netSpecifier, + const sptr &callback, + const uint32_t &timeoutMS, const int32_t registerType, + const uint32_t callingUid); // for NET_CAPABILITY_INTERNAL_DEFAULT bool IsInRequestNetUids(int32_t uid); diff --git a/services/netconnmanager/src/net_activate.cpp b/services/netconnmanager/src/net_activate.cpp index 56df33879..87cb7f687 100644 --- a/services/netconnmanager/src/net_activate.cpp +++ b/services/netconnmanager/src/net_activate.cpp @@ -258,8 +258,8 @@ CallbackType NetActivate::GetLastCallbackType() const void NetActivate::SetLastCallbackType(CallbackType callbackType) { - if (callbackType == CALL_TYPE_UPDATE_CAP || callbackType == CALL_TYPE_UPDATE_LINK - && lastCallbackType_.load() == CALL_TYPE_AVAILABLE) { + if ((callbackType == CALL_TYPE_UPDATE_CAP || callbackType == CALL_TYPE_UPDATE_LINK) + && (lastCallbackType_.load() == CALL_TYPE_AVAILABLE)) { return; } lastCallbackType_ = callbackType; diff --git a/services/netconnmanager/src/net_conn_service.cpp b/services/netconnmanager/src/net_conn_service.cpp index ff592f41a..efae85fc1 100644 --- a/services/netconnmanager/src/net_conn_service.cpp +++ b/services/netconnmanager/src/net_conn_service.cpp @@ -1150,22 +1150,8 @@ int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, } std::weak_ptr timeoutCb = shared_from_this(); - std::shared_ptr request = nullptr; -#ifdef ENABLE_SET_APP_FROZENED - sptr callbakWrapper = new (std::nothrow) NetConnCallbackProxyWrapper(callback); - if (callbakWrapper == nullptr) { - NETMGR_LOG_E("NetConnCallbackProxyWrapper ptr is null"); - request = std::make_shared( - netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); - } else { - request = std::make_shared( - netSpecifier, callbakWrapper, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); - callbakWrapper->SetNetActivate(request); - } -#else - request = std::make_shared( - netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); -#endif + std::shared_ptr request = CreateNetActivateRequest(netSpecifier, callback, + timeoutMS, REQUEST, callingUid); request->StartTimeOutNetAvailable(); uint32_t reqId = request->GetRequestId(); @@ -1208,6 +1194,32 @@ int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, return NETMANAGER_SUCCESS; } +std::shared_ptr NetConnService::CreateNetActivateRequest(const sptr &netSpecifier, + const sptr &callback, + const uint32_t &timeoutMS, + const int32_t registerType, + const uint32_t callingUid) +{ + std::weak_ptr timeoutCb = shared_from_this(); + std::shared_ptr request = nullptr; +#ifndef ENABLE_SET_APP_FROZENED + sptr callbakWrapper = new (std::nothrow) NetConnCallbackProxyWrapper(callback); + if (callbakWrapper == nullptr) { + NETMGR_LOG_E("NetConnCallbackProxyWrapper ptr is null"); + request = std::make_shared( + netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + } else { + request = std::make_shared( + netSpecifier, callbakWrapper, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + callbakWrapper->SetNetActivate(request); + } +#else + request = std::make_shared( + netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); +#endif + return request; +} + void NetConnService::OnNetActivateTimeOut(uint32_t reqId) { if (netConnEventHandler_) { -- Gitee From a730a55106b9235033ced70306515426af357e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Mon, 3 Mar 2025 14:49:29 +0800 Subject: [PATCH 13/21] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- .../netconnmanager/include/net_conn_service.h | 1 + services/netconnmanager/src/net_activate.cpp | 8 ++-- .../netconnmanager/src/net_conn_service.cpp | 40 ++++++++++--------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/services/netconnmanager/include/net_conn_service.h b/services/netconnmanager/include/net_conn_service.h index 276711c9f..3c5c4cc11 100644 --- a/services/netconnmanager/include/net_conn_service.h +++ b/services/netconnmanager/include/net_conn_service.h @@ -386,6 +386,7 @@ public: int32_t GetPacUrl(std::string &pacUrl) override; int32_t SetAppIsFrozened(uint32_t uid, bool isFrozened) override; int32_t EnableAppFrozenedCallbackLimitation(bool flag) override; + bool IsAppFrozenedCallbackLimitation(); private: class NetInterfaceStateCallback : public NetsysControllerCallback { diff --git a/services/netconnmanager/src/net_activate.cpp b/services/netconnmanager/src/net_activate.cpp index 87cb7f687..e750732e3 100644 --- a/services/netconnmanager/src/net_activate.cpp +++ b/services/netconnmanager/src/net_activate.cpp @@ -18,6 +18,7 @@ #include "net_activate.h" #include "net_caps.h" +#include "net_conn_service.h" #include "net_mgr_log_wrapper.h" #include "app_state_aware.h" @@ -280,12 +281,12 @@ void NetActivate::SetLastServiceSupply(sptr lastNetServiceSupplied) bool NetActivate::IsAllowCallback(CallbackType callbackType) { + if (!NetConnService::GetInstance()->IsAppFrozenedCallbackLimitation()) { + return true; + } bool isAppFrozened = isAppFrozened_.load(); bool isForegroundApp = AppStateAwareManager::GetInstance().IsForegroundApp(uid_); if (isAppFrozened && !isForegroundApp) { - if (lastCallbackType_ == CALL_TYPE_LOST && callbackType != CALL_TYPE_LOST) { - SetLastServiceSupply(nullptr); - } if (lastCallbackType_ != CALL_TYPE_LOST && callbackType == CALL_TYPE_LOST) { SetLastServiceSupply(netServiceSupplied_); } @@ -295,7 +296,6 @@ bool NetActivate::IsAllowCallback(CallbackType callbackType) uid_, callbackType); return false; } - NETMGR_LOG_I("UID[%{public}d] Allow send callbackType[%{public}d]", uid_, callbackType); return true; } diff --git a/services/netconnmanager/src/net_conn_service.cpp b/services/netconnmanager/src/net_conn_service.cpp index efae85fc1..b300e3661 100644 --- a/services/netconnmanager/src/net_conn_service.cpp +++ b/services/netconnmanager/src/net_conn_service.cpp @@ -3566,27 +3566,25 @@ int32_t NetConnService::SetAppIsFrozened(uint32_t uid, bool isFrozened) int32_t NetConnService::SetAppIsFrozenedAsync(uint32_t uid, bool isFrozened) { - std::vector> activates; - { - std::lock_guard guard(uidActivateMutex_); - auto it = netUidActivates_.find(uid); - if ((it == netUidActivates_.end())) { - return NETMANAGER_SUCCESS; - } - activates = it->second; + std::lock_guard guard(uidActivateMutex_); + auto it = netUidActivates_.find(uid); + if ((it == netUidActivates_.end())) { + return NETMANAGER_SUCCESS; } + std::vector> activates = it->second; NETMGR_LOG_I("SetAppIsFrozenedAsync uid[%{public}d], isFrozened=[%{public}d].", uid, isFrozened); for (auto iter = activates.begin(); iter != activates.end();++iter) { auto curNetAct = (*iter); - if (curNetAct -> IsAppFrozened() == isFrozened) { + if (curNetAct->IsAppFrozened() == isFrozened) { continue; } - curNetAct -> SetIsAppFrozened(isFrozened); + curNetAct->SetIsAppFrozened(isFrozened); if (isFrozened) { continue; } - sptr netSupplier = curNetAct -> GetServiceSupply(); - CallbackType callbackType = curNetAct -> GetLastCallbackType(); + sptr netSupplier = curNetAct->GetServiceSupply(); + sptr callback = curNetAct->GetNetCallback(); + CallbackType callbackType = curNetAct->GetLastCallbackType(); if (callbackType == CALL_TYPE_UNKNOWN) { continue; } @@ -3594,19 +3592,19 @@ int32_t NetConnService::SetAppIsFrozenedAsync(uint32_t uid, bool isFrozened) if (callbackType != CALL_TYPE_LOST) { continue; } - netSupplier = curNetAct -> GetLastServiceSupply(); - sptr callback = curNetAct -> GetNetCallback(); + netSupplier = curNetAct->GetLastServiceSupply(); if (netSupplier && callback) { - sptr netHandle = netSupplier ->GetNetHandle(); + sptr netHandle = netSupplier->GetNetHandle(); callback->NetLost(netHandle); } - curNetAct -> SetLastServiceSupply(nullptr); } else if (callbackType == CALL_TYPE_AVAILABLE) { CallbackForAvailable(netSupplier, curNetAct->GetNetCallback()); } else { - CallbackForSupplier(netSupplier, callbackType); + sptr netHandle = netSupplier->GetNetHandle(); + HandleCallback(netSupplier, netHandle, callback, callbackType); } - curNetAct -> SetLastCallbackType(CALL_TYPE_UNKNOWN); + curNetAct->SetLastServiceSupply(nullptr); + curNetAct->SetLastCallbackType(CALL_TYPE_UNKNOWN); } return NETMANAGER_SUCCESS; } @@ -3629,5 +3627,11 @@ int32_t NetConnService::EnableAppFrozenedCallbackLimitationAsync(bool flag) return NETMANAGER_SUCCESS; } +bool NetConnService::IsAppFrozenedCallbackLimitation() +{ + bool ret = enableAppFrozenedCallbackLimitation_.load(); + return ret; +} + } // namespace NetManagerStandard } // namespace OHOS -- Gitee From d93ff6f448316afca0b998cdaea27a6669eead46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Tue, 4 Mar 2025 10:33:52 +0000 Subject: [PATCH 14/21] update services/netconnmanager/src/net_activate.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/netconnmanager/src/net_activate.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/netconnmanager/src/net_activate.cpp b/services/netconnmanager/src/net_activate.cpp index e750732e3..8eec1510b 100644 --- a/services/netconnmanager/src/net_activate.cpp +++ b/services/netconnmanager/src/net_activate.cpp @@ -287,7 +287,8 @@ bool NetActivate::IsAllowCallback(CallbackType callbackType) bool isAppFrozened = isAppFrozened_.load(); bool isForegroundApp = AppStateAwareManager::GetInstance().IsForegroundApp(uid_); if (isAppFrozened && !isForegroundApp) { - if (lastCallbackType_ != CALL_TYPE_LOST && callbackType == CALL_TYPE_LOST) { + if (lastCallbackType_ != CALL_TYPE_LOST && callbackType == CALL_TYPE_LOST + && GetLastServiceSupply() == nullptr) { SetLastServiceSupply(netServiceSupplied_); } SetLastCallbackType(callbackType); -- Gitee From 3d27a20ac1971a76ebc5709704bc34ba26fcb9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 14:19:40 +0800 Subject: [PATCH 15/21] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/include/app_state_aware.h | 118 +++++++++--------- services/common/src/app_state_aware.cpp | 2 +- .../include/net_conn_callback_proxy_wrapper.h | 2 +- .../src/net_conn_callback_proxy_wrapper.cpp | 2 +- 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/services/common/include/app_state_aware.h b/services/common/include/app_state_aware.h index 205672b9d..74cdd0ed5 100644 --- a/services/common/include/app_state_aware.h +++ b/services/common/include/app_state_aware.h @@ -1,59 +1,59 @@ -/* - * Copyright (C) 2021-2022 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 APP_STATE_AWARE_H -#define 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 { -namespace NetManagerStandard { - -constexpr uint32_t MAX_RETRY_COUNT = 3; - -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(); - void RegisterAppStateAwareCallback(const AppStateAwareCallback &appStateAwareCallback); - void RegisterAppStateObserver(); - 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; -}; -} // namespace NetManagerStandard -} // namespace OHOS -#endif \ No newline at end of file +/* + * Copyright (C) 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 APP_STATE_AWARE_H +#define 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 { +namespace NetManagerStandard { + +constexpr uint32_t MAX_RETRY_COUNT = 3; + +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(); + void RegisterAppStateAwareCallback(const AppStateAwareCallback &appStateAwareCallback); + void RegisterAppStateObserver(); + 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; +}; +} // namespace NetManagerStandard +} // namespace OHOS +#endif diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp index 2a1339b6f..67f956e0b 100644 --- a/services/common/src/app_state_aware.cpp +++ b/services/common/src/app_state_aware.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 Huawei Device Co., Ltd. + * Copyright (C) 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 diff --git a/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h b/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h index 4fab4446b..4aa8ce8e6 100644 --- a/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h +++ b/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 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 diff --git a/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp b/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp index e2ab5719a..13adb9f47 100644 --- a/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp +++ b/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 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 -- Gitee From 21fa7e2a0ffe4d1e53be02d3dfa6ed5906b8c035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 15:19:03 +0800 Subject: [PATCH 16/21] update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/include/app_state_aware.h | 1 + services/common/src/app_state_aware.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/services/common/include/app_state_aware.h b/services/common/include/app_state_aware.h index 74cdd0ed5..e3b5fc112 100644 --- a/services/common/include/app_state_aware.h +++ b/services/common/include/app_state_aware.h @@ -49,6 +49,7 @@ public: bool IsForegroundApp(const uint32_t uid); private: std::atomic foregroundAppUid_; + std::mutex instanceMutex_ {}; std::mutex mutex_ {}; sptr appStateObserver_ = nullptr; AppStateAwareCallback appStateAwareCallback_; diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp index 67f956e0b..4442b22f3 100644 --- a/services/common/src/app_state_aware.cpp +++ b/services/common/src/app_state_aware.cpp @@ -36,6 +36,7 @@ AppStateAwareManager::~AppStateAwareManager() AppStateAwareManager &AppStateAwareManager::GetInstance() { + std::lock_guard lock(instanceMutex_); static AppStateAwareManager gAppStateAwareManager; if (!gAppStateAwareManager.appStateObserver_) { if (gAppStateAwareManager.retryCount_ < MAX_RETRY_COUNT) { -- Gitee From 119af1f595f5fd355f69ed25ec7713c16909d4fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 09:28:24 +0000 Subject: [PATCH 17/21] update services/common/src/app_state_aware.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/src/app_state_aware.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp index 4442b22f3..a7b0c7c04 100644 --- a/services/common/src/app_state_aware.cpp +++ b/services/common/src/app_state_aware.cpp @@ -34,6 +34,8 @@ AppStateAwareManager::~AppStateAwareManager() UnSubscribeAppState(); } +std::mutex AppStateAwareManager::instanceMutex_ {}; + AppStateAwareManager &AppStateAwareManager::GetInstance() { std::lock_guard lock(instanceMutex_); -- Gitee From 21fadf6a33c7885c283b2139f55046da3aee3205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 09:28:47 +0000 Subject: [PATCH 18/21] update services/common/include/app_state_aware.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/include/app_state_aware.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/common/include/app_state_aware.h b/services/common/include/app_state_aware.h index e3b5fc112..3acad8050 100644 --- a/services/common/include/app_state_aware.h +++ b/services/common/include/app_state_aware.h @@ -54,6 +54,8 @@ private: sptr appStateObserver_ = nullptr; AppStateAwareCallback appStateAwareCallback_; uint32_t retryCount_ = 0; + + static std::mutex instanceMutex_; }; } // namespace NetManagerStandard } // namespace OHOS -- Gitee From 1f997d259af54b354aa70413be2652f5b268cc18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 09:55:53 +0000 Subject: [PATCH 19/21] update services/common/src/app_state_aware.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/src/app_state_aware.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp index a7b0c7c04..72938172c 100644 --- a/services/common/src/app_state_aware.cpp +++ b/services/common/src/app_state_aware.cpp @@ -23,7 +23,7 @@ namespace OHOS { namespace NetManagerStandard { - +std::mutex AppStateAwareManager::instanceMutex_; AppStateAwareManager::AppStateAwareManager() { SubscribeAppState(); @@ -34,8 +34,6 @@ AppStateAwareManager::~AppStateAwareManager() UnSubscribeAppState(); } -std::mutex AppStateAwareManager::instanceMutex_ {}; - AppStateAwareManager &AppStateAwareManager::GetInstance() { std::lock_guard lock(instanceMutex_); -- Gitee From c404369d96a44cb58001dcea815c1f16eb4c0c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 11:42:51 +0000 Subject: [PATCH 20/21] update services/common/include/app_state_aware.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/include/app_state_aware.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/common/include/app_state_aware.h b/services/common/include/app_state_aware.h index 3acad8050..5e2a4fc81 100644 --- a/services/common/include/app_state_aware.h +++ b/services/common/include/app_state_aware.h @@ -24,7 +24,7 @@ namespace OHOS { namespace NetManagerStandard { -constexpr uint32_t MAX_RETRY_COUNT = 3; +constexpr uint32_t MAX_RETRY_COUNT = 5; struct AppStateAwareCallback { std::function OnForegroundAppChanged; -- Gitee From c750ad71936e70a18fcaaa18eae2e98eb6082eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=87=E6=9D=B0?= Date: Wed, 5 Mar 2025 12:50:07 +0000 Subject: [PATCH 21/21] update services/common/include/app_state_aware.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张文杰 --- services/common/include/app_state_aware.h | 1 - 1 file changed, 1 deletion(-) diff --git a/services/common/include/app_state_aware.h b/services/common/include/app_state_aware.h index 5e2a4fc81..5dfebb7ab 100644 --- a/services/common/include/app_state_aware.h +++ b/services/common/include/app_state_aware.h @@ -49,7 +49,6 @@ public: bool IsForegroundApp(const uint32_t uid); private: std::atomic foregroundAppUid_; - std::mutex instanceMutex_ {}; std::mutex mutex_ {}; sptr appStateObserver_ = nullptr; AppStateAwareCallback appStateAwareCallback_; -- Gitee