diff --git a/bundle.json b/bundle.json index c59db4a8970a3111dd761ce53f3ac07e85919b3a..6163ddd995acda07eef249cf2a73d1e0d4954294 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" @@ -81,7 +82,8 @@ "core_service", "distributed_notification_service", "i18n", - "netmanager_ext" + "netmanager_ext", + "jsoncpp" ] }, "build": { diff --git a/frameworks/native/netconnclient/src/net_conn_client.cpp b/frameworks/native/netconnclient/src/net_conn_client.cpp index 057802d71e1c21e67b964cbface1976e533bbafe..6a39e2bd9ac8550c908760cd351fca70198462f4 100755 --- a/frameworks/native/netconnclient/src/net_conn_client.cpp +++ b/frameworks/native/netconnclient/src/net_conn_client.cpp @@ -1177,5 +1177,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 02f3c8edad6b27af2edb396d880a922ec8be8042..142804ed4fa5caa3af303f5fdaffc78355bf5667 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 9eab8ce0a5db21411b4da5f2071a7fcab617d992..161ff08584c0e59171c952aa3ee0cb3e7d78f641 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 47e78b4af12c6274734dd35a91e8e201e1007281..124bb4936b5bd17e7ac5f43cc5de786536925a61 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 00f1cd29f6b5ccfdcbf416096f5a1284c23f95de..be28d5d9f7a3ca74116b50898bc99f4262791e04 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 95da049e15038bbdc5cf22b97049b24054764885..294f652c2ea7d8bfe8968f31b6d2747bcc8e8139 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 af757239891f7f2ad81953776cd882d5e28a577d..3610908a4eb678fd3fe16b418743d8c1e5e135af 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 7a1680a30eb5d82614769ba876b070e2d9ee4885..e7090309184e604f6c76f75047d778353f98d3cc 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 7f1a825eaff9276c9b132306d5083c353ff69845..e37b71f5feccae076dd90adafb82f0d7a71bcc01 100644 --- a/services/common/BUILD.gn +++ b/services/common/BUILD.gn @@ -100,6 +100,7 @@ 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", @@ -137,11 +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", + "jsoncpp:jsoncpp", + "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 0000000000000000000000000000000000000000..5dfebb7abf46edce76cb06a9cf63c91040c64b66 --- /dev/null +++ b/services/common/include/app_state_aware.h @@ -0,0 +1,61 @@ +/* + * 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 = 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(); + 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; + + static std::mutex instanceMutex_; +}; +} // namespace NetManagerStandard +} // namespace OHOS +#endif diff --git a/services/common/src/app_state_aware.cpp b/services/common/src/app_state_aware.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72938172c4eba1aa5e941382710db97485f624ed --- /dev/null +++ b/services/common/src/app_state_aware.cpp @@ -0,0 +1,140 @@ +/* + * 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. + */ + +#include "app_state_aware.h" + +#include "app_mgr_constants.h" +#include "iservice_registry.h" +#include +#include "system_ability_definition.h" +#include "net_mgr_log_wrapper.h" + +namespace OHOS { +namespace NetManagerStandard { +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_) { + 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 diff --git a/services/netconnmanager/BUILD.gn b/services/netconnmanager/BUILD.gn index 5ef2f38b2ea63d66c4d14a635d29f5d936945200..533282eab21900ab9d09cca93c8b68f637ca18e0 100644 --- a/services/netconnmanager/BUILD.gn +++ b/services/netconnmanager/BUILD.gn @@ -49,6 +49,7 @@ 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", @@ -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" @@ -171,6 +176,7 @@ 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", diff --git a/services/netconnmanager/include/net_activate.h b/services/netconnmanager/include/net_activate.h index 30105e28a9e026d5821e3109b2fe9272e7ef6a38..790eac80254f88c5d29159399502adff9a27823a 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 0000000000000000000000000000000000000000..4aa8ce8e6b1907a1872a64df2f0ab6ee2cac54cb --- /dev/null +++ b/services/netconnmanager/include/net_conn_callback_proxy_wrapper.h @@ -0,0 +1,50 @@ +/* + * 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 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 b0d8076334a13420c2022e9737531d67116a6c7e..3c5c4cc114cb35bbe7844c154c86a776b69b8ebd 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,9 @@ 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; + bool IsAppFrozenedCallbackLimitation(); private: class NetInterfaceStateCallback : public NetsysControllerCallback { @@ -541,6 +545,14 @@ 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); + 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); @@ -599,6 +611,9 @@ private: static constexpr int32_t INVALID_USER_ID = -1; 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 c5a3395a1f9d0b6102c474ed81025dd0af0c0495..b1cdbbd8f9e2231ec8d8d86164fd426efcc3925a 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 d35bd69d46ae2b7661aff4423c47a81e8dcf6782..8eec1510b99eec015bdf95660f3bbc4ee4d8e05e 100644 --- a/services/netconnmanager/src/net_activate.cpp +++ b/services/netconnmanager/src/net_activate.cpp @@ -18,7 +18,9 @@ #include "net_activate.h" #include "net_caps.h" +#include "net_conn_service.h" #include "net_mgr_log_wrapper.h" +#include "app_state_aware.h" namespace OHOS { namespace NetManagerStandard { @@ -237,5 +239,66 @@ 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) + && (lastCallbackType_.load() == CALL_TYPE_AVAILABLE)) { + 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) +{ + 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 + && GetLastServiceSupply() == nullptr) { + SetLastServiceSupply(netServiceSupplied_); + } + SetLastCallbackType(callbackType); + + NETMGR_LOG_I("UID[%{public}d] is AppFrozened, not Allow send callbackType[%{public}d]", + uid_, callbackType); + return false; + } + 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 0000000000000000000000000000000000000000..13adb9f47476af9751c7ea76a47918403514a96a --- /dev/null +++ b/services/netconnmanager/src/net_conn_callback_proxy_wrapper.cpp @@ -0,0 +1,104 @@ +/* + * 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. + */ + +#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 fbb986e8c2a65737e5447b544af5ee53264160c8..16eab52991942f8c67cbec961b9ef72ecf98b962 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; } @@ -1133,8 +1150,10 @@ int32_t NetConnService::ActivateNetwork(const sptr &netSpecifier, return NETMANAGER_ERR_PARAMETER_ERROR; } std::weak_ptr timeoutCb = shared_from_this(); - std::shared_ptr request = std::make_shared( - netSpecifier, callback, timeoutCb, timeoutMS, netConnEventHandler_, callingUid, registerType); + + std::shared_ptr request = CreateNetActivateRequest(netSpecifier, callback, + timeoutMS, REQUEST, callingUid); + request->StartTimeOutNetAvailable(); uint32_t reqId = request->GetRequestId(); NETMGR_LOG_I("New request [id:%{public}u]", reqId); @@ -1175,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_) { @@ -1527,33 +1572,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 +3252,7 @@ void NetConnService::RemoveALLClientDeathRecipient() item->AsObject()->RemoveDeathRecipient(deathRecipient_); } remoteCallback_.clear(); + appStateAwareCallback_.OnForegroundAppChanged = nullptr; deathRecipient_ = nullptr; } @@ -3493,5 +3551,87 @@ 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::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) { + continue; + } + curNetAct->SetIsAppFrozened(isFrozened); + if (isFrozened) { + continue; + } + sptr netSupplier = curNetAct->GetServiceSupply(); + sptr callback = curNetAct->GetNetCallback(); + CallbackType callbackType = curNetAct->GetLastCallbackType(); + if (callbackType == CALL_TYPE_UNKNOWN) { + continue; + } + if (netSupplier == nullptr) { + if (callbackType != CALL_TYPE_LOST) { + continue; + } + netSupplier = curNetAct->GetLastServiceSupply(); + if (netSupplier && callback) { + sptr netHandle = netSupplier->GetNetHandle(); + callback->NetLost(netHandle); + } + } else if (callbackType == CALL_TYPE_AVAILABLE) { + CallbackForAvailable(netSupplier, curNetAct->GetNetCallback()); + } else { + sptr netHandle = netSupplier->GetNetHandle(); + HandleCallback(netSupplier, netHandle, callback, callbackType); + } + curNetAct->SetLastServiceSupply(nullptr); + 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; +} + +bool NetConnService::IsAppFrozenedCallbackLimitation() +{ + bool ret = enableAppFrozenedCallbackLimitation_.load(); + return ret; +} + } // 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 cffd144d3092bc527628d1ced46c67eae43a32f2..48e29016b3acf077d04739ff844d7b6bcb393673 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/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp index 8563665cda8289c90c67320bc861b5619d805bf5..b3f997d763d5065e419c38d58d2b0b4aa996cdf1 100644 --- a/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp +++ b/test/fuzztest/netconnclient_fuzzer/net_conn_client_fuzzer.cpp @@ -1504,6 +1504,37 @@ 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; @@ -1552,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 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 6caa85318fb97e67258d088563bde4c3da230a9b..a4cffb1ede138bf23b09b8450b983736eec2447f 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 cbc6e2dc54e604c05f990409fd538f83b46a3dcc..f651c243956d7bc6aa21b624d66cdf53e68413cf 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 37f308b39cd950483edbfdab7fb97a844af23e28..f08086180e43a1ec57786f92c1e7c3e282be7d56 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 866e17e265ca48efd5a3073cd0b2caf2d93ca2a8..190cd96c72845bb7fd429867ff64ce935754964a 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 14e1126fa777c2fd38be1a3977578b1e0f98cffe..baee257454156bc511593b8b9d5d660fc81a3c85 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 d022f665e8b956c2d8bcd279053f65b2c060ce63..7db60a206ef262716cd97cc44702425df9f29a7b 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