diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index 71440fee38b73f6f5c346c68dc9ca591c71dbcdc..3f975d5a9e47425768385b7e5970d9e0e5521b1c 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -922,7 +922,10 @@ napi_value GetNotificationSubscriber( napi_value nOnConsumed = nullptr; napi_get_named_property(env, value, "onConsume", &nOnConsumed); NAPI_CALL(env, napi_typeof(env, nOnConsumed, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnConsumed, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, CONSUME, result); } @@ -932,7 +935,10 @@ napi_value GetNotificationSubscriber( napi_value nOnCanceled = nullptr; napi_get_named_property(env, value, "onCancel", &nOnCanceled); NAPI_CALL(env, napi_typeof(env, nOnCanceled, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnCanceled, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, CANCEL, result); } @@ -942,7 +948,10 @@ napi_value GetNotificationSubscriber( napi_value nOnUpdate = nullptr; napi_get_named_property(env, value, "onUpdate", &nOnUpdate); NAPI_CALL(env, napi_typeof(env, nOnUpdate, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnUpdate, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, UPDATE, result); } @@ -952,7 +961,10 @@ napi_value GetNotificationSubscriber( napi_value nOnConnected = nullptr; napi_get_named_property(env, value, "onConnect", &nOnConnected); NAPI_CALL(env, napi_typeof(env, nOnConnected, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnConnected, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, CONNECTED, result); } @@ -962,7 +974,10 @@ napi_value GetNotificationSubscriber( napi_value nOnDisConnect = nullptr; napi_get_named_property(env, value, "onDisconnect", &nOnDisConnect); NAPI_CALL(env, napi_typeof(env, nOnDisConnect, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnDisConnect, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, DIS_CONNECTED, result); } @@ -972,7 +987,10 @@ napi_value GetNotificationSubscriber( napi_value nOnDied = nullptr; napi_get_named_property(env, value, "onDestroy", &nOnDied); NAPI_CALL(env, napi_typeof(env, nOnDied, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnDied, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, DIE, result); } @@ -982,7 +1000,10 @@ napi_value GetNotificationSubscriber( napi_value nOnDisturbModeChanged = nullptr; napi_get_named_property(env, value, "onDisturbModeChange", &nOnDisturbModeChanged); NAPI_CALL(env, napi_typeof(env, nOnDisturbModeChanged, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnDisturbModeChanged, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, DISTURB_MODE_CHANGE, result); } @@ -993,12 +1014,27 @@ napi_value GetNotificationSubscriber( napi_value nOnDisturbDateChanged = nullptr; napi_get_named_property(env, value, "onDoNotDisturbDateChange", &nOnDisturbDateChanged); NAPI_CALL(env, napi_typeof(env, nOnDisturbDateChanged, &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, nOnDisturbDateChanged, 1, &result); subscriberInfo.subscriber->SetCallbackInfo(env, DISTURB_DATE_CHANGE, result); } // onEnabledNotificationChanged?:(data: notification.EnabledNotificationCallbackData) => void + NAPI_CALL(env, napi_has_named_property(env, value, "onEnabledNotificationChanged", &hasProperty)); + if (hasProperty) { + napi_value nOnEnabledNotificationChanged = nullptr; + napi_get_named_property(env, value, "onEnabledNotificationChanged", &nOnEnabledNotificationChanged); + NAPI_CALL(env, napi_typeof(env, nOnEnabledNotificationChanged, &valuetype)); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } + napi_create_reference(env, nOnEnabledNotificationChanged, 1, &result); + subscriberInfo.subscriber->SetCallbackInfo(env, ENABLE_NOTIFICATION_CHANGED, result); + } return Common::NapiGetNull(env); } @@ -1051,13 +1087,19 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[SUBSRIBE_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); + if (argc < 1) { + ANS_LOGE("Wrong number of arguments"); + return nullptr; + } napi_valuetype valuetype = napi_undefined; // argv[0]:subscriber NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type for arg0. NotificationSubscriber object expected."); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type for arg0. NotificationSubscriber object expected."); + return nullptr; + } SubscriberInstancesInfo subscriberInstancesInfo; if (!HasNotificationSubscriber(env, argv[PARAM0], subscriberInstancesInfo)) { @@ -1099,7 +1141,10 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, // argv[2]:callback if (argc >= SUBSRIBE_MAX_PARA) { NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, argv[PARAM2], 1, &callback); } diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index fb606b2bd83b31798c530507c67d6fa194b91b76..539c4360e81c62617249bc333430ee46ed485a07 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -187,6 +187,7 @@ private: ErrCode PrepereContinuousTaskNotificationRequest(const sptr &request, const int &uid); bool GetActiveUserId(int& userId); void TriggerRemoveWantAgent(const sptr &request); + bool CheckApiCompatibility(const sptr &bundleOption); ErrCode SetNotificationRemindType(sptr notification, bool isLocal); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index e08a2d415dc897484ec68c4982332eeff30e805d..9a060facae6ec7dcb7d1bdab33e06a77af3423df 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -34,7 +34,7 @@ public: std::string GetBundleNameByUid(int uid); bool IsSystemApp(int uid); int GetDefaultUidByBundleName(const std::string &bundle); - bool GetBundleInfoByBundleName(const std::string bundle, AppExecFwk::BundleInfo &bundleInfo); + bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool GetDistributedNotificationEnabled(const std::string &bundleName, const int userId); #endif diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 9f2405361e9a312bbeee826b23b5aaa6362d933d..df87d68d529dae64013da4503f1cfa70777d9c93 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -118,21 +118,6 @@ inline bool IsSystemApp() return isSystemApp; } -inline bool CheckApiCompatibility(const std::string bundleName) -{ - AppExecFwk::BundleInfo bundleInfo; - std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - if (bundleManager != nullptr) { - bundleManager->GetBundleInfoByBundleName(bundleName, bundleInfo); - } - for (auto abilityInfo : bundleInfo.abilityInfos) { - if (abilityInfo.isStageBasedModel) { - return false; - } - } - return true; -} - inline int64_t ResetSeconds(int64_t date) { auto milliseconds = std::chrono::milliseconds(date); @@ -1375,14 +1360,14 @@ ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string bool allowedNotify = false; result = IsSpecialBundleAllowedNotify(bundleOption, allowedNotify); - if (allowedNotify) { + if (result != ERR_OK || allowedNotify) { ANS_LOGD("Already granted permission"); return result; } bool hasPopped = false; result = GetHasPoppedDialog(bundleOption, hasPopped); - if (hasPopped) { + if (result != ERR_OK || hasPopped) { ANS_LOGD("Already shown dialog"); return result; } @@ -1528,12 +1513,11 @@ ErrCode AdvancedNotificationService::IsAllowedNotifySelf(bool &allowed) return ERR_ANS_INVALID_BUNDLE; } - allowed = CheckApiCompatibility(bundleOption->GetBundleName()); - handler_->PostSyncTask(std::bind([&]() { result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(bundleOption, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; + allowed = CheckApiCompatibility(bundleOption); SetNotificationsEnabledForSpecialBundle("", bundleOption, allowed); } })); @@ -1586,7 +1570,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { result = ERR_OK; - allowed = false; + allowed = CheckApiCompatibility(bundleOption); } } })); @@ -3180,5 +3164,25 @@ ErrCode AdvancedNotificationService::GetHasPoppedDialog( })); return result; } + +bool AdvancedNotificationService::CheckApiCompatibility(const sptr &bundleOption) +{ + AppExecFwk::BundleInfo bundleInfo; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + int32_t callingUserId; + AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), callingUserId); + if (bundleManager != nullptr) { + if (!bundleManager->GetBundleInfoByBundleName(bundleOption->GetBundleName(), callingUserId, bundleInfo)) { + return false; + } + } + + for (auto abilityInfo : bundleInfo.abilityInfos) { + if (abilityInfo.isStageBasedModel) { + return false; + } + } + return true; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index dd9cfa14fe105de809800070ccf9c771ef68bb9b..7afa5a38f68c2b2184681f66ba420df836aaa17a 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -71,15 +71,15 @@ bool BundleManagerHelper::IsSystemApp(int uid) return isSystemApp; } -bool BundleManagerHelper::GetBundleInfoByBundleName(const std::string bundle, AppExecFwk::BundleInfo &bundleInfo) +bool BundleManagerHelper::GetBundleInfoByBundleName( + const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) { if (bundleMgr_ == nullptr) { return false; } - return bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo); + return bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, userId); } - void BundleManagerHelper::Connect() { if (bundleMgr_ != nullptr) { diff --git a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp index f49cc6066a569a9b94c7606018a89f38683b18dc..7528a5c370598c4f3198c81625b6103ae9dd515e 100644 --- a/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp +++ b/services/ans/test/unittest/mock/mock_bundle_manager_helper.cpp @@ -46,7 +46,8 @@ bool BundleManagerHelper::IsSystemApp(int uid) return (uid == SYSTEM_APP_UID); } -bool BundleManagerHelper::GetBundleInfoByBundleName(const std::string bundle, AppExecFwk::BundleInfo &bundleInfo) +bool BundleManagerHelper::GetBundleInfoByBundleName( + const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) { return true; }