From be6863cf7c6774555791aaf33097620d1aeaea42 Mon Sep 17 00:00:00 2001 From: xiaoshuai Date: Thu, 21 Mar 2024 17:24:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E6=81=AF=E8=81=9A=E5=90=88-=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoshuai Change-Id: I134a6ccf6db8085b9e1bc0eabf8139ae06105517 --- frameworks/js/napi/BUILD.gn | 1 + frameworks/js/napi/include/sync_config.h | 28 ++++ frameworks/js/napi/src/init.cpp | 4 +- frameworks/js/napi/src/sync_config.cpp | 154 ++++++++++++++++++ .../advanced_notification_slot_service.cpp | 9 + 5 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 frameworks/js/napi/include/sync_config.h create mode 100644 frameworks/js/napi/src/sync_config.cpp diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index cd7d44450..33d2b4343 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -85,6 +85,7 @@ ohos_shared_library("notification") { "src/slot.cpp", "src/subscribe.cpp", "src/unsubscribe.cpp", + "src/sync_config.cpp" ] deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] diff --git a/frameworks/js/napi/include/sync_config.h b/frameworks/js/napi/include/sync_config.h new file mode 100644 index 000000000..c1380d845 --- /dev/null +++ b/frameworks/js/napi/include/sync_config.h @@ -0,0 +1,28 @@ +/* + * 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_INCLUDE_SYNC_CONFIG_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_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_INCLUDE_SYNC_CONFIG_H diff --git a/frameworks/js/napi/src/init.cpp b/frameworks/js/napi/src/init.cpp index ae115b2bb..77222e8fc 100644 --- a/frameworks/js/napi/src/init.cpp +++ b/frameworks/js/napi/src/init.cpp @@ -28,6 +28,7 @@ #include "slot.h" #include "subscribe.h" #include "unsubscribe.h" +#include "sync_config.h" namespace OHOS { namespace NotificationNapi { @@ -83,6 +84,7 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("isNotificationSlotEnabled", IsEnableNotificationSlot), DECLARE_NAPI_FUNCTION("setSyncNotificationEnabledWithoutApp", SetSyncNotificationEnabledWithoutApp), DECLARE_NAPI_FUNCTION("getSyncNotificationEnabledWithoutApp", GetSyncNotificationEnabledWithoutApp), + DECLARE_NAPI_FUNCTION("setAdditionConfig", NapiSetAdditionConfig), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); @@ -116,4 +118,4 @@ __attribute__((constructor)) void RegisterModule(void) } EXTERN_C_END } // namespace NotificationNapi -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/frameworks/js/napi/src/sync_config.cpp b/frameworks/js/napi/src/sync_config.cpp new file mode 100644 index 000000000..b33bc1ec1 --- /dev/null +++ b/frameworks/js/napi/src/sync_config.cpp @@ -0,0 +1,154 @@ +/* + * 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 "sync_config.h" +#include "common.h" +#include "napi_common_util.h" +#include "ans_inner_errors.h" + +namespace OHOS { +namespace NotificationNapi { +namespace { +constexpr int8_t SETADDITION_CONFIG_MAX = 3; +constexpr int8_t SETADDITION_CONFIG_MIN = 2; +constexpr char KET_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("Wrong argument type. 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, KET_NAME) != 0)) { + ANS_LOGW("Wrong argument type. String expected."); + return nullptr; + } + + // argv[1]: key: string + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_string) { + ANS_LOGW("Wrong argument type. String expected."); + return nullptr; + } + char valueStr[STR_MAX_SIZE] = {0}; + size_t valstrLen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM1], valueStr, 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 params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoConfig *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoConfig {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.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("SetAdditionConfig work excute."); + AsyncCallbackInfoConfig *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + ANS_LOGI("asynccallbackinfo->params.key = %{public}s", asynccallbackinfo->params.key.c_str()); + asynccallbackinfo->info.errorCode = + NotificationHelper::SetAdditionConfig(asynccallbackinfo->params.key, asynccallbackinfo->params.value); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetAdditionConfig work complete."); + AsyncCallbackInfoConfig *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Set AdditionConfig callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGD("SetAdditionConfig 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("SetAdditionConfig callback is nullptr."); + return Common::NapiGetNull(env); + } else { + return promise; + } +} + +} // namespace NotificationNapi +} // namespace OHOS diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index fbdd44734..3767f78f1 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -28,6 +28,7 @@ #include "common_event_support.h" #include "hitrace_meter_adapter.h" #include "ipc_skeleton.h" +#include "notification_extension_wrapper.h" #include "advanced_notification_inline.cpp" @@ -719,6 +720,14 @@ ErrCode AdvancedNotificationService::SetAdditionConfig(const std::string &key, c return ERR_ANS_INVALID_PARAM; } + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + +#ifdef ENABLE_ANS_EXT_WRAPPER + NOTIFICATIONEXT_WRAPPER.SyncAdditionConfig(key, value); +#endif + ErrCode result = ERR_OK; ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); -- Gitee