diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index 8ace25244ae11ef5f487f328c9a6e58901752ff3..c135a7dc0e3f4effee0f0ad3c6338b3690b1010b 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -762,8 +762,8 @@ std::string NotificationRequest::Dump() ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") + ", notificationBundleOption = " + (notificationBundleOption_ != nullptr ? "not null" : "null") + ", creatorUserId = " + std::to_string(creatorUserId_) + ", ownerUserId = " + std::to_string(ownerUserId_) + - ", receiverUserId = " + std::to_string(receiverUserId_) + ", updateDeadLine = " + - std::to_string(updateDeadLine_) + ", finishDeadLine = " + std::to_string(finishDeadLine_) + " }"; + ", receiverUserId = " + std::to_string(receiverUserId_) + ", updateDeadLine = " +std::to_string(updateDeadLine_) + + ",finishDeadLine = " + std::to_string(finishDeadLine_) + ", unifiedGroupInfo_ = " + (this->unifiedGroupInfo_ == nullptr ? "nullptr " : this->unifiedGroupInfo_->Dump())+ "}"; } bool NotificationRequest::ToJson(nlohmann::json &jsonObject) const diff --git a/frameworks/js/napi/include/common.h b/frameworks/js/napi/include/common.h index a98f56aa1b10164d085eee94b06c6bb98dc8f654..b16a93ffe3b0e674689af6d930733fcc42432d34 100644 --- a/frameworks/js/napi/include/common.h +++ b/frameworks/js/napi/include/common.h @@ -1622,6 +1622,17 @@ public: static napi_value SetNotificationFlags( const napi_env &env, const std::shared_ptr &flags, napi_value &result); + /** + * @brief Sets a js object by specified NotificationUnifiedGroupInfo object + * + * @param env Indicates the environment that the API is invoked under + * @param flags Indicates a NotificationUnifiedGroupInfo object to be converted + * @param result Indicates a js object to be set + * @return Returns the null object if success, returns the null value otherwise + */ + static napi_value SetNotificationUnifiedGroupInfo( + const napi_env &env, const std::shared_ptr &info, napi_value &result); + /** * @brief Gets the number of badge of NotificationRequest object from specified js object * diff --git a/frameworks/js/napi/include/manager/napi_sync_config.h b/frameworks/js/napi/include/manager/napi_sync_config.h new file mode 100644 index 0000000000000000000000000000000000000000..8c65f254289253b281d9b4bac300403eed5adceb --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_sync_config.h @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2024 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SYNC_CONFIG_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SYNC_CONFIG_H + +#include "common.h" + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +napi_value NapiSetAdditionConfig(napi_env env, napi_callback_info info); +} // namespace NotificationNapi +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SYNC_CONFIG_H diff --git a/frameworks/js/napi/src/common.cpp b/frameworks/js/napi/src/common.cpp index 814cbc6c333187f095aa6753a5108b78e21a99b0..ff58324f205ce76f6b3b6c00872a545e09efa0db 100644 --- a/frameworks/js/napi/src/common.cpp +++ b/frameworks/js/napi/src/common.cpp @@ -1320,5 +1320,44 @@ napi_value Common::SetNotificationFlags( return NapiGetBoolean(env, true); } + +napi_value Common::SetNotificationUnifiedGroupInfo( + const napi_env &env, const std::shared_ptr &info, napi_value &result) +{ + ANS_LOGD("enter"); + + if (info == nullptr) { + ANS_LOGE("info is null"); + return NapiGetBoolean(env, false); + } + + napi_value value = nullptr; + + // title?: string + napi_create_string_utf8(env, info->GetTitle().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "title", value); + + // key?: string + napi_create_string_utf8(env, info->GetKey().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "key", value); + + // content?: string + napi_create_string_utf8(env, info->GetContent().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "content", value); + + // scene?: string + napi_create_string_utf8(env, info->GetSceneName().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "scene", value); + + // extraInfo?: {[key:string] : any} + std::shared_ptr extraInfoData = info->GetExtraInfo(); + if (extraInfoData) { + napi_value extraInfo = nullptr; + extraInfo = OHOS::AppExecFwk::WrapWantParams(env, *extraInfoData); + napi_set_named_property(env, result, "extraInfo", extraInfo); + } + + return NapiGetBoolean(env, true); +} } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/src/common_convert_request.cpp b/frameworks/js/napi/src/common_convert_request.cpp index d1a75461d83add7f66c37b36faf9b0c154e2e909..c6a7c1901bea6d87264be624fdbb02ffe5dd4f7e 100644 --- a/frameworks/js/napi/src/common_convert_request.cpp +++ b/frameworks/js/napi/src/common_convert_request.cpp @@ -27,6 +27,7 @@ #include "notification_progress.h" #include "notification_time.h" #include "pixel_map_napi.h" +#include namespace OHOS { namespace NotificationNapi { @@ -346,6 +347,17 @@ napi_value Common::SetNotificationRequestByCustom( napi_set_named_property(env, result, "notificationFlags", flagsResult); } + // unifiedGroupInfo?: UnifiedGroupInfo; + std::shared_ptr unifiedInfo = request->GetUnifiedGroupInfo(); + if (unifiedInfo) { + napi_value unifiedInfoResult = nullptr; + napi_create_object(env, &unifiedInfoResult); + if (!SetNotificationUnifiedGroupInfo(env, unifiedInfo, unifiedInfoResult)) { + ANS_LOGE("SetNotificationUnifiedGroupInfo call failed"); + return NapiGetBoolean(env, false); + } + napi_set_named_property(env, result, "unifiedGroupInfo", unifiedInfoResult); + } return NapiGetBoolean(env, true); } @@ -1764,7 +1776,7 @@ napi_value Common::GetNotificationUnifiedGroupInfo( } // sceneName?: string - jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "sceneName", napi_string); + jsValue = AppExecFwk::GetPropertyValueByPropertyName(env, info, "scene", napi_string); if (jsValue != nullptr) { std::string sceneName = AppExecFwk::UnwrapStringFromJS(env, jsValue); unifiedGroupInfo->SetSceneName(sceneName); diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn index ec1403b4bbd698a7d3c234302560a88a61a90410..32edfa3a919899be41b53381b107a90bc92f944e 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -80,6 +80,7 @@ ohos_shared_library("notificationmanager") { "napi_push_callback.cpp", "napi_remove_group.cpp", "napi_slot.cpp", + "napi_sync_config.cpp", "napi_template.cpp", ] diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index fb3d48756d2d35fbfea4f69605eda92bad60d3f8..7392df147367f827e3669b5ead4668714cb57645 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -33,6 +33,7 @@ #include "napi_push.h" #include "napi_local_live_view.h" #include "napi_distributed_enable.h" +#include "napi_sync_config.h" namespace OHOS { namespace NotificationNapi { @@ -115,6 +116,7 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("setSmartReminderEnabled", NapiSetSmartReminderEnabled), DECLARE_NAPI_FUNCTION("isSmartReminderEnabled", NapiIsSmartReminderEnabled), DECLARE_NAPI_FUNCTION("getSlotByBundle", NapiGetSlotByBundle), + DECLARE_NAPI_FUNCTION("setAdditionConfig", NapiSetAdditionConfig), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/frameworks/js/napi/src/manager/napi_sync_config.cpp b/frameworks/js/napi/src/manager/napi_sync_config.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19f87994436e28fdb0bb876be4bc0d405be057b6 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_sync_config.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2024 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 "napi_sync_config.h" +#include "common.h" +#include "napi_common_util.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace NotificationNapi { +namespace { + constexpr int8_t SETADDITION_CONFIG_MAX = 3; + constexpr int8_t SETADDITION_CONFIG_MIN = 2; + constexpr char KEY_NAME[] = "AGGREGATE_CONFIG"; +} + +struct ConfigParams { + std::string key = ""; + std::string value = ""; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoConfig { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + ConfigParams params; + CallbackPromiseInfo info; +}; + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ConfigParams ¶ms) +{ + ANS_LOGD("enter"); + + size_t argc = SETADDITION_CONFIG_MAX; + napi_value argv[SETADDITION_CONFIG_MAX] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < SETADDITION_CONFIG_MIN) { + ANS_LOGW("Wrong number of arguments."); + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + // argv[0]: key: string + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_string) { + ANS_LOGW("Argument type error. String expected."); + return nullptr; + } + char keyStr[STR_MAX_SIZE] = {0}; + size_t keyStrLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM0], keyStr, STR_MAX_SIZE - 1, &keyStrLen)); + params.key = keyStr; + if (std::strlen(keyStr) == 0 || (strcmp(keyStr, KEY_NAME) != 0)) { + ANS_LOGW("Argument type error. String expected."); + return nullptr; + } + + // argv[1]: value: string + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_string) { + ANS_LOGW("Argument type error. String expected."); + return nullptr; + } + + char valueStr[LONG_LONG_STR_MAX_SIZE] = {0}; + size_t valStrLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], valueStr, LONG_LONG_STR_MAX_SIZE - 1, &valStrLen)); + params.value = valueStr; + + // argv[2]:callback + if (argc >= SETADDITION_CONFIG_MAX) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGW("Callback is not function excute promise."); + return Common::NapiGetNull(env); + } + napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback); + } + return Common::NapiGetNull(env); +} + +napi_value NapiSetAdditionConfig(napi_env env, napi_callback_info info) +{ + ANS_LOGD("enter"); + ConfigParams paras {}; + if (ParseParameters(env, info, paras) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoConfig *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoConfig { + .env = env, + .asyncWork = nullptr, + .params = paras + }; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setAdditionConfig", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("NapiSetAdditionConfig work excute."); + AsyncCallbackInfoConfig *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = + NotificationHelper::SetAdditionConfig(asynccallbackinfo->params.key, asynccallbackinfo->params.value); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("NapiSetAdditionConfig work complete."); + AsyncCallbackInfoConfig *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + napi_create_int32(env, asynccallbackinfo->info.errorCode, &result); + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete NapiSetAdditionConfig callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGD("NapiSetAdditionConfig work complete end."); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + if (asynccallbackinfo->info.isCallback) { + ANS_LOGD("NapiSetAdditionConfig callback is nullptr."); + return Common::NapiGetNull(env); + } else { + return promise; + } +} +} // namespace NotificationNapi +} // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index fcbdb0ddc787794af0f44740665fb49162f7ebcc..96a02c9d7a52a4010de6b48834a02e58a8545639 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -60,6 +60,7 @@ #include "time_service_client.h" #include "notification_config_parse.h" #include "want_params_wrapper.h" +#include "notification_extension_wrapper.h" #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED #include "distributed_notification_manager.h" @@ -192,7 +193,12 @@ ErrCode AdvancedNotificationService::PrepareNotificationRequest(const sptrGetUnifiedGroupInfo PrepareNotificationRequest : %{public}s",request->Dump().c_str()); + EXTENTION_WRAPPER->GetUnifiedGroupInfo(request); +#else + ANS_LOGD("EXTENTION_WRAPPER->not GetUnifiedGroupInfo"); +#endif return result; } diff --git a/services/ans/src/advanced_notification_service_ability.cpp b/services/ans/src/advanced_notification_service_ability.cpp index c9969120d1d7356427f33394d71734e21ca564d9..7023cb040a51ef5101619c1c6578c2fe0560f137 100644 --- a/services/ans/src/advanced_notification_service_ability.cpp +++ b/services/ans/src/advanced_notification_service_ability.cpp @@ -37,6 +37,8 @@ void AdvancedNotificationServiceAbility::OnStart() #ifdef ENABLE_ANS_EXT_WRAPPER EXTENTION_WRAPPER->InitExtentionWrapper(); +#else + ANS_LOGD("EXTENTION_WRAPPER --> ENABLE_ANS_EXT_WRAPPER"); #endif service_ = AdvancedNotificationService::GetInstance(); diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index fbdd447348358cbde8d3ad4c37c07f6a1d129f54..8fc395d4a4e1c0be598e3de59dad3d9aa7d717f6 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -28,11 +28,15 @@ #include "common_event_support.h" #include "hitrace_meter_adapter.h" #include "ipc_skeleton.h" +#include "notification_extension_wrapper.h" #include "advanced_notification_inline.cpp" namespace OHOS { namespace Notification { +namespace { + constexpr char KEY_NAME[] = "AGGREGATE_CONFIG"; +} ErrCode AdvancedNotificationService::AddSlots(const std::vector> &slots) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -710,7 +714,8 @@ ErrCode AdvancedNotificationService::SetAdditionConfig(const std::string &key, c { ANS_LOGD("Called."); bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem) { + bool isPushSa = strcmp(key.c_str(), KEY_NAME) == 0; + if (!isPushSa && !isSubsystem) { return ERR_ANS_NOT_SYSTEM_SERVICE; } @@ -719,6 +724,16 @@ ErrCode AdvancedNotificationService::SetAdditionConfig(const std::string &key, c return ERR_ANS_INVALID_PARAM; } + bool isSyncConfig = strcmp(key.c_str(), KEY_NAME) == 0; + if (isSyncConfig) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } +#ifdef ENABLE_ANS_EXT_WRAPPER + EXTENTION_WRAPPER->SyncAdditionConfig(key, value); +#endif + } + ErrCode result = ERR_OK; ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!");