From 6fc2a9a162a8097ad2baac8622b454ef807f4c42 Mon Sep 17 00:00:00 2001 From: PipiSummer Date: Tue, 7 May 2024 14:41:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=85=B1=E4=BA=8B=E4=BB=B6=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E9=80=9A=E7=9F=A5=E6=A8=A1=E5=9D=97=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?ffi=E6=A1=A5=E9=98=B6=E5=B1=82=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: PipiSummer --- bundle.json | 3 +- interfaces/kits/cj/BUILD.gn | 55 +++ interfaces/kits/cj/src/common_event.cpp | 178 +++++++ interfaces/kits/cj/src/common_event.h | 61 +++ .../kits/cj/src/common_event_defination.h | 57 +++ .../kits/cj/src/common_event_manager_ffi.cpp | 380 +++++++++++++++ .../kits/cj/src/common_event_manager_ffi.h | 71 +++ .../kits/cj/src/common_event_manager_impl.cpp | 102 ++++ .../kits/cj/src/common_event_manager_impl.h | 39 ++ .../kits/cj/src/common_event_manager_mock.cpp | 51 ++ interfaces/kits/cj/src/native_log.h | 43 ++ interfaces/kits/cj/src/parameter_parse.cpp | 455 ++++++++++++++++++ interfaces/kits/cj/src/parameter_parse.h | 31 ++ interfaces/kits/cj/src/subscribe_info.cpp | 86 ++++ interfaces/kits/cj/src/subscribe_info.h | 57 +++ interfaces/kits/cj/src/subscriber.cpp | 101 ++++ interfaces/kits/cj/src/subscriber.h | 86 ++++ 17 files changed, 1855 insertions(+), 1 deletion(-) create mode 100755 interfaces/kits/cj/BUILD.gn create mode 100755 interfaces/kits/cj/src/common_event.cpp create mode 100755 interfaces/kits/cj/src/common_event.h create mode 100755 interfaces/kits/cj/src/common_event_defination.h create mode 100755 interfaces/kits/cj/src/common_event_manager_ffi.cpp create mode 100755 interfaces/kits/cj/src/common_event_manager_ffi.h create mode 100755 interfaces/kits/cj/src/common_event_manager_impl.cpp create mode 100755 interfaces/kits/cj/src/common_event_manager_impl.h create mode 100755 interfaces/kits/cj/src/common_event_manager_mock.cpp create mode 100755 interfaces/kits/cj/src/native_log.h create mode 100755 interfaces/kits/cj/src/parameter_parse.cpp create mode 100755 interfaces/kits/cj/src/parameter_parse.h create mode 100755 interfaces/kits/cj/src/subscribe_info.cpp create mode 100755 interfaces/kits/cj/src/subscribe_info.h create mode 100755 interfaces/kits/cj/src/subscriber.cpp create mode 100755 interfaces/kits/cj/src/subscriber.h diff --git a/bundle.json b/bundle.json index ea6558ee..2696a39f 100644 --- a/bundle.json +++ b/bundle.json @@ -86,7 +86,8 @@ "//base/notification/common_event_service/frameworks/core:cesfwk_core", "//base/notification/common_event_service/frameworks/native:cesfwk_innerkits", "//base/notification/common_event_service/frameworks/extension:cesfwk_extension", - "//base/notification/common_event_service/interfaces/kits/napi:napi_packages" + "//base/notification/common_event_service/interfaces/kits/napi:napi_packages", + "//base/notification/common_event_service/interfaces/kits/cj:cj_common_event_manager_ffi" ], "service_group": [ "//base/notification/common_event_service/sa_profile:event_sa_profile", diff --git a/interfaces/kits/cj/BUILD.gn b/interfaces/kits/cj/BUILD.gn new file mode 100755 index 00000000..3b984ee3 --- /dev/null +++ b/interfaces/kits/cj/BUILD.gn @@ -0,0 +1,55 @@ +# 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. + +import("//base/notification/common_event_service/event.gni") +import("//build/ohos.gni") + +ohos_shared_library("cj_common_event_manager_ffi") { + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "../napi/common_event/include", + "../../../interfaces/inner_api", + ] + + if (product_name != "ohos-sdk") { + deps = [ "../../../frameworks/core:cesfwk_core" ] + + external_deps = [ + "ability_runtime:ability_manager", + "ability_runtime:abilitykit_native", + "ability_runtime:napi_base_context", + "hilog:libhilog", + "napi:cj_bind_ffi", + ] + + sources = [ + "src/common_event.cpp", + "src/common_event_manager_ffi.cpp", + "src/common_event_manager_impl.cpp", + "src/parameter_parse.cpp", + "src/subscribe_info.cpp", + "src/subscriber.cpp", + ] + } else { + sources = [ "src/common_event_manager_mock.cpp" ] + } + + relative_install_dir = "module" + subsystem_name = "notification" + part_name = "common_event_service" +} diff --git a/interfaces/kits/cj/src/common_event.cpp b/interfaces/kits/cj/src/common_event.cpp new file mode 100755 index 00000000..724ffe6c --- /dev/null +++ b/interfaces/kits/cj/src/common_event.cpp @@ -0,0 +1,178 @@ +/* + * 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 "common_event.h" +#include "native_log.h" +#include "common_event_manager_impl.h" + +#include "common_event_manager.h" +#include "securec.h" +using namespace OHOS::FFI; +using CommonEventManagerImpl = OHOS::CommonEventManager::CommonEventManagerImpl; + +namespace OHOS::CommonEventManager { + + static std::map, SubscriberInstanceInfo> subscriberImpls; + static std::mutex subscriberImplMutex; + + void SetPublishResult(OHOS::CommonEventManager::SubscriberImpl *subImpl) + { + LOGI("SetPublishResult start"); + std::lock_guard lock(subscriberImplMutex); + for (auto subscriberImpl : subscriberImpls) { + if (subscriberImpl.first.get() == subImpl) { + LOGI("Get success."); + subscriberImpls[subscriberImpl.first].commonEventResult = subImpl->GoAsyncCommonEvent(); + break; + } + } + } + + std::shared_ptr GetAsyncResult(const SubscriberImpl *objectInfo) + { + LOGI("GetAsyncResult start"); + if (!objectInfo) { + LOGE("Invalidity objectInfo"); + return nullptr; + } + std::lock_guard lock(subscriberImplMutex); + for (auto subscriberImpl : subscriberImpls) { + if (subscriberImpl.first.get() == objectInfo) { + return subscriberImpl.second.commonEventResult; + } + } + LOGI("No found objectInfo"); + return nullptr; + } + + void SetSubscribeInfo(std::shared_ptr subscriber, + const std::function &callback) + { + LOGI("Set subscriberImpls.") + subscriber->SetCallback(callback); + AsyncCallbackInfoSubscribe *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoSubscribe{.callback = callback, .subscriber = subscriber}; + std::lock_guard lock(subscriberImplMutex); + subscriberImpls[asyncCallbackInfo->subscriber].asyncCallbackInfo.emplace_back(asyncCallbackInfo); + } + + void DeleteCallBack(std::vector &asyncCallbackInfos) + { + for (auto asyncCallbackInfo : asyncCallbackInfos) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + } + + void SetSubscriber(int64_t id, std::shared_ptr newSubscriber) + { + std::lock_guard lock(subscriberImplMutex); + for (auto subscriberImpl : subscriberImpls) { + if (subscriberImpl.first->GetSubscribeInfoId() == id) { + newSubscriber = subscriberImpl.first; + DeleteCallBack(subscriberImpl.second.asyncCallbackInfo); + newSubscriber->SetCallback(nullptr); + OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(newSubscriber); + subscriberImpls.erase(newSubscriber); + LOGI("Execution complete"); + break; + } + } + } + + void DeleteSubscribe(std::shared_ptr subscriber) + { + LOGI("DeleteSubscribe start"); + std::lock_guard lock(subscriberImplMutex); + auto subscribe = subscriberImpls.find(subscriber); + if (subscribe != subscriberImpls.end()) { + for (auto asyncCallbackInfoSubscribe : subscribe->second.asyncCallbackInfo) { + delete asyncCallbackInfoSubscribe; + asyncCallbackInfoSubscribe = nullptr; + } + subscriber->SetCallback(nullptr); + subscriberImpls.erase(subscribe); + } + } + + void GetSubscriberCode(std::shared_ptr subscriber, int64_t &code) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + code = result->GetCode(); + } else { + code = 0; + } + } + + int32_t SetSubscriberCode(std::shared_ptr subscriber, int32_t code) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + return result->SetCode(code) ? NO_ERROR : ERR_CES_FAILED; + } + return NO_ERROR; + } + + void GetSubscriberData(std::shared_ptr subscriber, char *data) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + std::string str; + if (result) { + str = result->GetData(); + } else { + str = std::string(); + } + data = MallocCString(str); + } + + int32_t SetSubscriberData(std::shared_ptr subscriber, const char *data) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + return result->SetData(std::string(data)) ? NO_ERROR : ERR_CES_FAILED; + } + return NO_ERROR; + } + + int32_t SetSubscriberCodeAndData(std::shared_ptr subscriber, int32_t code, const char *data) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + return result->SetCodeAndData(code, std::string(data)) ? NO_ERROR : ERR_CES_FAILED; + } + return NO_ERROR; + } + + void IsCommonEventSticky(std::shared_ptr subscriber, bool &data) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + data = result->IsStickyCommonEvent(); + } else { + data = subscriber->IsStickyCommonEvent(); + } + } + + void IsCommonEventOrdered(std::shared_ptr subscriber, bool &data) + { + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + data = result->IsOrderedCommonEvent(); + } else { + data = subscriber->IsOrderedCommonEvent(); + } + } +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event.h b/interfaces/kits/cj/src/common_event.h new file mode 100755 index 00000000..160b6280 --- /dev/null +++ b/interfaces/kits/cj/src/common_event.h @@ -0,0 +1,61 @@ +/* + * 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 COMMON_EVENT_H +#define COMMON_EVENT_H + +#include "subscriber.h" + +namespace OHOS::CommonEventManager { + using AsyncCommonEventResult = OHOS::EventFwk::AsyncCommonEventResult; + + struct AsyncCallbackInfoSubscribe { + std::function callback; + std::shared_ptr subscriber = nullptr; + int8_t errorCode = NO_ERROR; + }; + + struct SubscriberInstanceInfo { + std::vector asyncCallbackInfo; + std::shared_ptr commonEventResult = nullptr; + }; + + void SetPublishResult(OHOS::CommonEventManager::SubscriberImpl *subImpl); + + std::shared_ptr GetAsyncResult(const SubscriberImpl *objectInfo); + + void SetSubscribeInfo(std::shared_ptr subscriber, + const std::function &callback); + + void SetSubscriber(int64_t id, std::shared_ptr newSubscriber); + + void DeleteSubscribe(std::shared_ptr subscriber); + + void GetSubscriberCode(std::shared_ptr subscriber, int64_t &code); + + int32_t SetSubscriberCode(std::shared_ptr subscriber, int32_t code); + + void GetSubscriberData(std::shared_ptr subscriber, char *data); + + int32_t SetSubscriberData(std::shared_ptr subscriber, const char *data); + + int32_t SetSubscriberCodeAndData(std::shared_ptr subscriber, int32_t code, const char *data); + + void IsCommonEventSticky(std::shared_ptr subscriber, bool &data); + + void IsCommonEventOrdered(std::shared_ptr subscriber, bool &data); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event_defination.h b/interfaces/kits/cj/src/common_event_defination.h new file mode 100755 index 00000000..c2ded146 --- /dev/null +++ b/interfaces/kits/cj/src/common_event_defination.h @@ -0,0 +1,57 @@ +/* + * 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 COMMON_EVENT_DEFINATION_H +#define COMMON_EVENT_DEFINATION_H + +#include "cj_common_ffi.h" + +struct CParameters { + int8_t valueType; + char *key; + void *value; + int64_t size; +}; + +struct CArrParameters { + CParameters *head; + int64_t size; +}; + +struct CommonEventPublishDataBycj { + char *bundleName; + char *data; + int32_t code; + CArrString permissions; + bool isOrdered; + bool isSticky; + CArrParameters parameters; +}; + +struct CCommonEventData { + char *event; + char *bundleName; + int32_t code; + char *data; + CArrParameters parameters; +}; + +namespace OHOS::CommonEventManager { + constexpr int8_t NO_ERROR = 0; + constexpr int8_t ERR_CES_FAILED = 1; + + char *MallocCString(const std::string &origin); +} +#endif // COMMON_EVENT_CONSTANT_H \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event_manager_ffi.cpp b/interfaces/kits/cj/src/common_event_manager_ffi.cpp new file mode 100755 index 00000000..baf72d3f --- /dev/null +++ b/interfaces/kits/cj/src/common_event_manager_ffi.cpp @@ -0,0 +1,380 @@ +/* + * 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 "common_event_manager_ffi.h" +#include "common_event_constant.h" +#include "common_event_manager_impl.h" +#include "common_event.h" + +#include "native_log.h" +#include "cj_lambda.h" +#include "securec.h" +using namespace OHOS::FFI; +using CommonEventManagerImpl = OHOS::CommonEventManager::CommonEventManagerImpl; + +namespace OHOS::CommonEventManager { + const int32_t ERR_INVALID_INSTANCE_ID = -1; + + extern "C" + { + int32_t CJ_PublishEvent(char *event, int32_t userId) + { + return CommonEventManagerImpl::PublishEvent(event, userId); + } + + int32_t CJ_PublishEventWithData(char *event, int32_t userId, CommonEventPublishDataBycj options) + { + return CommonEventManagerImpl::PublishEventWithData(event, userId, options); + } + + int32_t CJ_SetStaticSubscriberState(bool enable) + { + return CommonEventManagerImpl::SetStaticSubscriberState(enable); + } + + int32_t CJ_RemoveStickyCommonEvent(char *event) + { + return CommonEventManagerImpl::RemoveStickyCommonEvent(event); + } + + int64_t CJ_CreateCommonEventSubscribeInfo(CArrString events) + { + auto infoPtr = CommonEventManagerImpl::CreateCommonEventSubscribeInfo(events.head, events.size); + auto ptr = FFIData::Create(infoPtr); + return ptr->GetID(); + } + + int64_t CJ_CreateSubscriber(int64_t id) + { + std::shared_ptr newSubscriber = nullptr; + SetSubscriber(id, newSubscriber); + if (newSubscriber) { + return newSubscriber->GetSubscriberManagerId(); + } + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id); + return static_cast(ERR_INVALID_INSTANCE_ID); + } + auto info = instance->GetInfoPtr(); + auto ptr = FFIData::Create(info, id); + ptr->GetSubscriber()->SetSubscriberManagerId(ptr->GetID()); + return ptr->GetID(); + } + + int32_t CJ_Subscribe(int64_t id, void (*callbackRef)(const CCommonEventData data)) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + auto subscriber = instance->GetSubscriber(); + auto onChange = [lambda = CJLambda::Create(callbackRef)](const CCommonEventData data) -> void { + lambda(data); + }; + return CommonEventManagerImpl::Subscribe(subscriber, onChange); + } + + int32_t CJ_Unsubscribe(int64_t id) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + return CommonEventManagerImpl::Unsubscribe(instance->GetSubscriber()); + } + + RetDataI64 CJ_GetCode(int64_t id) + { + RetDataI64 ret = {.code = ERR_INVALID_INSTANCE_CODE, .data = 0}; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ret; + } + auto subscriber = instance->GetSubscriber(); + GetSubscriberCode(subscriber, ret.data); + ret.code = NO_ERROR; + return ret; + } + + int32_t CJ_SetCode(int64_t id, int32_t code) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + auto subscriber = instance->GetSubscriber(); + return SetSubscriberCode(subscriber, code); + } + + RetDataCString CJ_GetData(int64_t id) + { + RetDataCString ret = {.code = ERR_INVALID_INSTANCE_CODE, .data = nullptr}; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ret; + } + auto subscriber = instance->GetSubscriber(); + GetSubscriberData(subscriber, ret.data); + ret.code = NO_ERROR; + return ret; + } + + int32_t CJ_SetData(int64_t id, char *data) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + auto subscriber = instance->GetSubscriber(); + return SetSubscriberData(subscriber, data); + } + + int32_t CJ_SetCodeAndData(int64_t id, int32_t code, char *data) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + auto subscriber = instance->GetSubscriber(); + return SetSubscriberCodeAndData(subscriber, code, data); + } + + RetDataBool CJ_IsOrderedCommonEvent(int64_t id) + { + RetDataBool ret = {.code = ERR_INVALID_INSTANCE_CODE, .data = false}; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ret; + } + auto subscriber = instance->GetSubscriber(); + IsCommonEventOrdered(subscriber, ret.data); + ret.code = NO_ERROR; + return ret; + } + + RetDataBool CJ_IsStickyCommonEvent(int64_t id) + { + RetDataBool ret = {.code = ERR_INVALID_INSTANCE_CODE, .data = false}; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ret; + } + auto subscriber = instance->GetSubscriber(); + IsCommonEventSticky(subscriber, ret.data); + ret.code = NO_ERROR; + return ret; + } + + int32_t CJ_AbortCommonEvent(int64_t id) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + if (!instance->GetSubscriber()->AbortCommonEvent()) { + return ERR_CES_FAILED; + } + return SUCCESS_CODE; + } + + int32_t CJ_ClearAbortCommonEvent(int64_t id) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_CODE; + } + if (!instance->GetSubscriber()->ClearAbortCommonEvent()) { + return ERR_CES_FAILED; + } + return SUCCESS_CODE; + } + + RetDataBool CJ_GetAbortCommonEvent(int64_t id) + { + RetDataBool ret = {.code = ERR_INVALID_INSTANCE_CODE, .data = false}; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ret; + } + ret.code = SUCCESS_CODE; + ret.data = instance->GetSubscriber()->GetAbortCommonEvent(); + return ret; + } + + RetDataI64 CJ_GetSubscribeInfo(int64_t id) + { + RetDataI64 ret = {.code = ERR_INVALID_INSTANCE_ID, .data = 0}; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ret; + } + ret.code = SUCCESS_CODE; + ret.data = instance->GetSubscribeInfoId(); + return ret; + } + + int32_t CJ_FinishCommonEvent(int64_t id) + { + int32_t errorCode = ERR_CES_FAILED; + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("SubscriberManager instance not exist %{public}" PRId64, id); + return ERR_INVALID_INSTANCE_ID; + } + auto subscriber = instance->GetSubscriber(); + std::shared_ptr result = GetAsyncResult(subscriber.get()); + if (result) { + errorCode = result->FinishCommonEvent() ? NO_ERROR : ERR_CES_FAILED; + } + return errorCode; + } + + void CJ_SetPermission(int64_t id, char *value) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id); + return; + } + + return instance->SetPermission(std::string(value)); + } + + void CJ_SetDeviceId(int64_t id, const char *value) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id); + return; + } + return instance->SetDeviceId(std::string(value)); + } + + void CJ_SetUserId(int64_t id, int32_t value) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id); + return; + } + return instance->SetUserId(value); + } + + void CJ_SetPriority(int64_t id, int32_t value) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id); + return; + } + return instance->SetPriority(value); + } + + void CJ_SetBundleName(int64_t id, const char *value) + { + auto instance = FFIData::GetData(id); + if (!instance) { + LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id); + return; + } + return instance->SetPublisherBundleName(std::string(value)); + } + + const char *CJ_GetPermission(int64_t id) + { + auto instance = FFIData::GetData(id); + auto str = instance->GetPermission(); + auto ret = MallocCString(str); + return ret; + } + + const char *CJ_GetDeviceId(int64_t id) + { + auto instance = FFIData::GetData(id); + auto str = instance->GetDeviceId(); + auto ret = MallocCString(str); + return ret; + } + + int32_t CJ_GetUserId(int64_t id) + { + auto instance = FFIData::GetData(id); + return instance->GetUserId(); + } + + int32_t CJ_GetPriority(int64_t id) + { + auto instance = FFIData::GetData(id); + return instance->GetPriority(); + } + + const char *CJ_GetBundleName(int64_t id) + { + auto instance = FFIData::GetData(id); + auto str = instance->GetPublisherBundleName(); + auto ret = MallocCString(str); + return ret; + } + + void ClearCharPointer(char** ptr, int count) + { + for (int i = 0; i < count; i++) { + free(ptr[i]); + } + } + + char **VectorToCharPointer(std::vector &vec) + { + char** result = static_cast(malloc(sizeof(char*) * vec.size())); + if (result == nullptr) { + return nullptr; + } + for (size_t i = 0; i < vec.size(); i++) { + result[i] = MallocCString(vec[i]); + if (result[i] == nullptr) { + ClearCharPointer(result, i); + free(result); + return nullptr; + } + } + return result; + } + + CArrString CJ_GetEvents(int64_t id) + { + auto instance = FFIData::GetData(id); + auto vStr = instance->GetEvents(); + CArrString ret = {.head = VectorToCharPointer(vStr), .size = vStr.size()}; + if (ret.head == nullptr) { + LOGE("Failed to malloc."); + } + return ret; + } + } +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event_manager_ffi.h b/interfaces/kits/cj/src/common_event_manager_ffi.h new file mode 100755 index 00000000..9ef13f8e --- /dev/null +++ b/interfaces/kits/cj/src/common_event_manager_ffi.h @@ -0,0 +1,71 @@ +/* + * 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 COMMON_EVENT_MANAGER_FFI_H +#define COMMON_EVENT_MANAGER_FFI_H + +#include "ffi_remote_data.h" +#include "cj_common_ffi.h" +#include "common_event_defination.h" + +#include + +extern "C" +{ + // CommonEventManager ffi apis + FFI_EXPORT int32_t CJ_PublishEvent(char *event, int32_t userId); + FFI_EXPORT int32_t CJ_PublishEventWithData(char *event, int32_t userId, CommonEventPublishDataBycj options); + FFI_EXPORT int32_t CJ_SetStaticSubscriberState(bool enable); + FFI_EXPORT int32_t CJ_RemoveStickyCommonEvent(char *event); + FFI_EXPORT int64_t CJ_CreateCommonEventSubscribeInfo(CArrString events); + FFI_EXPORT int64_t CJ_CreateSubscriber(int64_t id); + FFI_EXPORT int32_t CJ_Subscribe(int64_t id, void (*callbackRef)(const CCommonEventData data)); + FFI_EXPORT int32_t CJ_Unsubscribe(int64_t id); + + FFI_EXPORT RetDataI64 CJ_GetCode(int64_t id); + FFI_EXPORT int32_t CJ_SetCode(int64_t id, int32_t code); + FFI_EXPORT RetDataCString CJ_GetData(int64_t id); + FFI_EXPORT int32_t CJ_SetData(int64_t id, char *data); + FFI_EXPORT int32_t CJ_SetCodeAndData(int64_t id, int32_t code, char *data); + + // subscriber properties + FFI_EXPORT RetDataI64 CJ_GetCode(int64_t id); + FFI_EXPORT int32_t CJ_SetCode(int64_t id, int32_t code); + FFI_EXPORT RetDataCString CJ_GetData(int64_t id); + FFI_EXPORT int32_t CJ_SetData(int64_t id, char *data); + FFI_EXPORT int32_t CJ_SetCodeAndData(int64_t id, int32_t code, char *data); + FFI_EXPORT RetDataBool CJ_IsOrderedCommonEvent(int64_t id); + FFI_EXPORT RetDataBool CJ_IsStickyCommonEvent(int64_t id); + FFI_EXPORT int32_t CJ_AbortCommonEvent(int64_t id); + FFI_EXPORT int32_t CJ_ClearAbortCommonEvent(int64_t id); + FFI_EXPORT RetDataBool CJ_GetAbortCommonEvent(int64_t id); + FFI_EXPORT RetDataI64 CJ_GetSubscribeInfo(int64_t id); + FFI_EXPORT int32_t CJ_FinishCommonEvent(int64_t id); + + // CommonEventSubscribeInfo properties + FFI_EXPORT void CJ_SetPermission(int64_t id, char *value); + FFI_EXPORT void CJ_SetDeviceId(int64_t id, const char *value); + FFI_EXPORT void CJ_SetUserId(int64_t id, int32_t value); + FFI_EXPORT void CJ_SetPriority(int64_t id, int32_t value); + FFI_EXPORT void CJ_SetBundleName(int64_t id, const char *value); + FFI_EXPORT const char *CJ_GetPermission(int64_t id); + FFI_EXPORT const char *CJ_GetDeviceId(int64_t id); + FFI_EXPORT int32_t CJ_GetUserId(int64_t id); + FFI_EXPORT int32_t CJ_GetPriority(int64_t id); + FFI_EXPORT const char *CJ_GetBundleName(int64_t id); + FFI_EXPORT CArrString CJ_GetEvents(int64_t id); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event_manager_impl.cpp b/interfaces/kits/cj/src/common_event_manager_impl.cpp new file mode 100755 index 00000000..9a1efa0a --- /dev/null +++ b/interfaces/kits/cj/src/common_event_manager_impl.cpp @@ -0,0 +1,102 @@ +/* + * 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 "common_event_manager_impl.h" +#include "native_log.h" +#include "common_event.h" +#include "parameter_parse.h" +#include "common_event_manager.h" +#include "securec.h" + +#include "common_event_publish_info.h" + +namespace OHOS::CommonEventManager { + using Want = OHOS::AAFwk::Want; + using CommonEventPublishInfo = OHOS::EventFwk::CommonEventPublishInfo; + + int32_t CommonEventManagerImpl::PublishEvent(char *event, int32_t userId) + { + Want want; + want.SetAction(std::string(event)); + auto data = CommonEventData(want); + return OHOS::EventFwk::CommonEventManager::PublishCommonEventAsUser(data, userId) ? NO_ERROR : ERR_CES_FAILED; + } + + int32_t CommonEventManagerImpl::PublishEventWithData(char *event, int32_t userId, + CommonEventPublishDataBycj options) + { + Want want; + want.SetAction(std::string(event)); + // parameters + if (options.parameters.size != 0) { + WantParams wantP; + SetDataParameters(options.parameters, wantP); + want.SetParams(wantP); + } + auto data = CommonEventData(want, options.code, std::string(options.data)); + CommonEventPublishInfo publishInfo = CommonEventPublishInfo(); + // subPermissions + if (options.permissions.size != 0) { + std::vector subscriberPermissions; + charPtrToVector(options.permissions.head, options.permissions.size, subscriberPermissions); + publishInfo.SetSubscriberPermissions(subscriberPermissions); + } + publishInfo.SetOrdered(options.isOrdered); + publishInfo.SetSticky(options.isSticky); + publishInfo.SetBundleName(std::string(options.bundleName)); + return OHOS::EventFwk::CommonEventManager::NewPublishCommonEventAsUser(data, publishInfo, userId); + } + + int32_t CommonEventManagerImpl::SetStaticSubscriberState(bool enable) + { + return OHOS::EventFwk::CommonEventManager::SetStaticSubscriberState(enable); + } + + int32_t CommonEventManagerImpl::RemoveStickyCommonEvent(char *event) + { + return OHOS::EventFwk::CommonEventManager::RemoveStickyCommonEvent(std::string(event)); + } + + std::shared_ptr CommonEventManagerImpl::CreateCommonEventSubscribeInfo( + char **event, int64_t size) + { + LOGI("CJ_CreateSubscribeInfo start"); + OHOS::EventFwk::MatchingSkills matchingSkills; + for (int64_t i = 0; i < size; i++) { + matchingSkills.AddEvent(std::string(event[i])); + } + CommonEventSubscribeInfo subscriberInfo(matchingSkills); + return std::make_shared(subscriberInfo); + } + + int32_t CommonEventManagerImpl::Subscribe(std::shared_ptr subscriber, + const std::function &callback) + { + LOGI("Start Subscribe!") + auto errorCode = OHOS::EventFwk::CommonEventManager::NewSubscribeCommonEvent(subscriber); + if (errorCode == NO_ERROR) { + SetSubscribeInfo(subscriber, callback); + } + return errorCode; + } + + int32_t CommonEventManagerImpl::Unsubscribe(std::shared_ptr subscriber) + { + auto errorCode = OHOS::EventFwk::CommonEventManager::NewUnSubscribeCommonEvent(subscriber); + DeleteSubscribe(subscriber); + return errorCode; + } + +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event_manager_impl.h b/interfaces/kits/cj/src/common_event_manager_impl.h new file mode 100755 index 00000000..4f42432a --- /dev/null +++ b/interfaces/kits/cj/src/common_event_manager_impl.h @@ -0,0 +1,39 @@ +/* + * 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 COMMON_EVENT_MANAGER_IMPL_H +#define COMMON_EVENT_MANAGER_IMPL_H + +#include "subscribe_info.h" +#include "subscriber.h" + +#include + +namespace OHOS::CommonEventManager { + + class CommonEventManagerImpl { + public: + static int32_t PublishEvent(char *event, int32_t userId); + static int32_t PublishEventWithData(char *event, int32_t userId, CommonEventPublishDataBycj options); + static int32_t SetStaticSubscriberState(bool enable); + static int32_t RemoveStickyCommonEvent(char *event); + static std::shared_ptr CreateCommonEventSubscribeInfo(char **event, int64_t size); + static int32_t Subscribe(std::shared_ptr subscriber, + const std::function &callback); + static int32_t Unsubscribe(std::shared_ptr subscriber); + }; +} + +#endif // COMMON_EVENT_MANAGER_IMPL \ No newline at end of file diff --git a/interfaces/kits/cj/src/common_event_manager_mock.cpp b/interfaces/kits/cj/src/common_event_manager_mock.cpp new file mode 100755 index 00000000..3c495ab2 --- /dev/null +++ b/interfaces/kits/cj/src/common_event_manager_mock.cpp @@ -0,0 +1,51 @@ +/* + * 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 "cj_ffi/cj_common_ffi.h" + +extern "C" +{ + FFI_EXPORT int CJ_PublishEvent = 0; + FFI_EXPORT int CJ_PublishEventWithData; + FFI_EXPORT int CJ_SetStaticSubscriberState = 0; + FFI_EXPORT int CJ_RemoveStickyCommonEvent = 0; + FFI_EXPORT int CJ_CreateCommonEventSubscribeInfo = 0; + FFI_EXPORT int CJ_CreateSubscriber = 0; + FFI_EXPORT int CJ_Subscribe; + FFI_EXPORT int CJ_Unsubscribe; + FFI_EXPORT int CJ_GetCode = 0; + FFI_EXPORT int CJ_SetCode = 0; + FFI_EXPORT int CJ_GetData = 0; + FFI_EXPORT int CJ_SetData = 0; + FFI_EXPORT int CJ_SetCodeAndData = 0; + FFI_EXPORT int CJ_IsOrderedCommonEvent = 0; + FFI_EXPORT int CJ_IsStickyCommonEvent = 0; + FFI_EXPORT int CJ_AbortCommonEvent = 0; + FFI_EXPORT int CJ_ClearAbortCommonEvent = 0; + FFI_EXPORT int CJ_GetAbortCommonEvent = 0; + FFI_EXPORT int CJ_GetSubscribeInfo = 0; + FFI_EXPORT int CJ_FinishCommonEvent = 0; + FFI_EXPORT int CJ_SetPermission = 0; + FFI_EXPORT int CJ_SetDeviceId = 0; + FFI_EXPORT int CJ_SetUserId = 0; + FFI_EXPORT int CJ_SetPriority = 0; + FFI_EXPORT int CJ_SetBundleName = 0; + FFI_EXPORT int CJ_GetPermission = 0; + FFI_EXPORT int CJ_GetDeviceId = 0; + FFI_EXPORT int CJ_GetUserId = 0; + FFI_EXPORT int CJ_GetPriority = 0; + FFI_EXPORT int CJ_GetBundleName = 0; + FFI_EXPORT int CJ_GetEvents = 0; +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/native_log.h b/interfaces/kits/cj/src/native_log.h new file mode 100755 index 00000000..5aa07a30 --- /dev/null +++ b/interfaces/kits/cj/src/native_log.h @@ -0,0 +1,43 @@ +/* + * 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 NATIVE_LOG_H +#define NATIVE_LOG_H + +#include "hilog/log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_DOMAIN 0xD001200 +#define LOG_TAG "Ces" + +#define LOGI(...) \ + if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_INFO)) \ + { \ + HILOG_INFO(LOG_CORE, ##__VA_ARGS__); \ + } + +#define LOGE(...) \ + if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_ERROR)) \ + { \ + HILOG_ERROR(LOG_CORE, __VA_ARGS__); \ + } + +#endif // NATIVE_LOG_H \ No newline at end of file diff --git a/interfaces/kits/cj/src/parameter_parse.cpp b/interfaces/kits/cj/src/parameter_parse.cpp new file mode 100755 index 00000000..715f40b5 --- /dev/null +++ b/interfaces/kits/cj/src/parameter_parse.cpp @@ -0,0 +1,455 @@ +/* + * 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 "parameter_parse.h" + +#include "native_log.h" +#include "int_wrapper.h" +#include "double_wrapper.h" +#include "string_wrapper.h" +#include "bool_wrapper.h" +#include "long_wrapper.h" +#include "array_wrapper.h" +#include "want_params_wrapper.h" +#include "securec.h" + +namespace OHOS::CommonEventManager { + const char *FD = "FD"; + const char *REMOTE_OBJECT = "RemoteObject"; + const char *TYPE_PROPERTY = "type"; + const char *VALUE_PROPERTY = "value"; + const int8_t I32_TYPE = 0; + const int8_t DOUBLE_TYPE = 1; + const int8_t STR_TYPE = 2; + const int8_t BOOL_TYPE = 3; + const int8_t FD_TYPE = 4; + const int8_t CHAR_PTR_TYPE = 5; + const int8_t I32_PTR_TYPE = 6; + const int8_t I64_PTR_TYPE = 7; + const int8_t BOOL_PTR_TYPE = 8; + const int8_t DOUBLE_PTR_TYPE = 9; + const int8_t FD_PTR_TYPE = 10; + const int SUCCESS = 0; + const int NONE_VALUE = 1; + using Want = OHOS::AAFwk::Want; + using WantParams = OHOS::AAFwk::WantParams; + + void charPtrToVector(char **charPtr, int size, std::vector &result) + { + for (int i = 0; i < size; i++) { + result.push_back(std::string(charPtr[i])); + } + } + + void SetFdData(std::string key, int *value, WantParams &wantP) + { + WantParams wp; + wp.SetParam(TYPE_PROPERTY, OHOS::AAFwk::String::Box(FD)); + wp.SetParam(VALUE_PROPERTY, OHOS::AAFwk::Integer::Box(*value)); + sptr pWantParams = OHOS::AAFwk::WantParamWrapper::Box(wp); + wantP.SetParam(key, pWantParams); + } + + bool InnerSetWantParamsArrayString( + const std::string &key, const std::vector &value, AAFwk::WantParams &wantParams) + { + size_t size = value.size(); + sptr ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IString); + if (ao != nullptr) { + for (size_t i = 0; i < size; i++) { + ao->Set(i, AAFwk::String::Box(value[i])); + } + wantParams.SetParam(key, ao); + return true; + } else { + return false; + } + } + + bool InnerSetWantParamsArrayInt(const std::string &key, const std::vector &value, + AAFwk::WantParams &wantParams) + { + size_t size = value.size(); + sptr ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IInteger); + if (ao != nullptr) { + for (size_t i = 0; i < size; i++) { + ao->Set(i, AAFwk::Integer::Box(value[i])); + } + wantParams.SetParam(key, ao); + return true; + } else { + return false; + } + } + + bool InnerSetWantParamsArrayLong(const std::string &key, const std::vector &value, + AAFwk::WantParams &wantParams) + { + size_t size = value.size(); + sptr ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_ILong); + if (ao != nullptr) { + for (size_t i = 0; i < size; i++) { + ao->Set(i, AAFwk::Long::Box(value[i])); + } + wantParams.SetParam(key, ao); + return true; + } else { + return false; + } + } + + bool InnerSetWantParamsArrayBool(const std::string &key, const std::vector &value, + AAFwk::WantParams &wantParams) + { + size_t size = value.size(); + sptr ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IBoolean); + if (ao != nullptr) { + for (size_t i = 0; i < size; i++) { + ao->Set(i, AAFwk::Boolean::Box(value[i])); + } + wantParams.SetParam(key, ao); + return true; + } else { + return false; + } + } + + bool InnerSetWantParamsArrayDouble( + const std::string &key, const std::vector &value, AAFwk::WantParams &wantParams) + { + size_t size = value.size(); + sptr ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IDouble); + if (ao != nullptr) { + for (size_t i = 0; i < size; i++) { + ao->Set(i, AAFwk::Double::Box(value[i])); + } + wantParams.SetParam(key, ao); + return true; + } else { + return false; + } + } + + void InnerSetWantParamsArrayFD(CParameters* head, int64_t size, AAFwk::WantParams &wantParams) + { + sptr ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IWantParams); + if (ao != nullptr) { + for (size_t i = 0; i < static_cast(size); i++) { + WantParams wp; + SetFdData(std::string(head->key), static_cast(head->value) + i, wp); + wp.DumpInfo(0); + ao->Set(i, OHOS::AAFwk::WantParamWrapper::Box(wp)); + } + wantParams.SetParam(std::string(head->key), ao); + } + return; + } + + void SetDataParameters(CArrParameters parameters, WantParams &wantP) + { + for (int i = 0; i < parameters.size; i++) { + auto head = parameters.head + i; + auto key = std::string(head->key); + if (head->valueType == I32_TYPE) { // int32_t + wantP.SetParam(key, OHOS::AAFwk::Integer::Box(*static_cast(head->value))); + } else if (head->valueType == DOUBLE_TYPE) { // double + wantP.SetParam(key, OHOS::AAFwk::Double::Box(*static_cast(head->value))); + } else if (head->valueType == STR_TYPE) { // std::string + wantP.SetParam(key, OHOS::AAFwk::String::Box(std::string(static_cast(head->value)))); + } else if (head->valueType == BOOL_TYPE) { // bool + wantP.SetParam(key, OHOS::AAFwk::Boolean::Box(*static_cast(head->value))); + } else if (head->valueType == FD_TYPE) { // "FD" + SetFdData(key, static_cast(head->value), wantP); + } else if (head->valueType == CHAR_PTR_TYPE) { // char** + char **strPtr = (char **)head->value; + std::vector strVec; + charPtrToVector(strPtr, head->size, strVec); + InnerSetWantParamsArrayString(key, strVec, wantP); + } else if (head->valueType == I32_PTR_TYPE) { // int32_t* + int *intArr = (int *)head->value; + std::vector intVec(intArr, intArr + head->size); + InnerSetWantParamsArrayInt(key, intVec, wantP); + } else if (head->valueType == I64_PTR_TYPE) { // int64_t* + long *longArr = (long *)head->value; + std::vector longVec(longArr, longArr + head->size); + InnerSetWantParamsArrayLong(key, longVec, wantP); + } else if (head->valueType == BOOL_PTR_TYPE) { // bool* + bool *boolArr = (bool *)head->value; + std::vector boolVec(boolArr, boolArr + head->size); + InnerSetWantParamsArrayBool(key, boolVec, wantP); + } else if (head->valueType == DOUBLE_PTR_TYPE) { // double* + double *doubleArr = (double *)head->value; + std::vector doubleVec(doubleArr, doubleArr + head->size); + InnerSetWantParamsArrayDouble(key, doubleVec, wantP); + } else if (head->valueType == FD_PTR_TYPE) { // FD* + InnerSetWantParamsArrayFD(head, head->size, wantP); + } else { + LOGE("Wrong type!"); + } + } + } + + char *MallocCString(const std::string &origin) + { + if (origin.empty()) { + return nullptr; + } + auto len = origin.length() + 1; + char *res = (char *)malloc(sizeof(char) * len); + if (res == nullptr) { + return nullptr; + } + return std::char_traits::copy(res, origin.c_str(), len); + } + + // WantParameters -> CArrParameters + void InnerWrapWantParamsString(WantParams &wantParams, CParameters *p) + { + auto value = wantParams.GetParam(p->key); + AAFwk::IString *ao = AAFwk::IString::Query(value); + if (ao == nullptr) { + LOGE("No value"); + return; + } + std::string natValue = OHOS::AAFwk::String::Unbox(ao); + p->value = MallocCString(natValue); + p->size = static_cast(natValue.length()) + 1; + p->valueType = STR_TYPE; + } + + template + void InnerWrapWantParamsT(WantParams &wantParams, CParameters *p) + { + auto value = wantParams.GetParam(p->key); + TBase *ao = TBase::Query(value); + if (ao == nullptr) { + LOGE("No value"); + return; + } + NativeT natValue = T::Unbox(ao); + NativeT *ptr = static_cast(malloc(sizeof(NativeT))); + if (ptr == nullptr) { + return; + } + *ptr = natValue; + p->value = static_cast(ptr); + p->size = sizeof(NativeT); + } + + void InnerWrapWantParamsArrayString(sptr &ao, CParameters *p) + { + long size = 0; + if (ao->GetLength(size) != ERR_OK) { + LOGE("fail to get length"); + return; + } + char **arrP = static_cast(malloc(sizeof(char *) * size)); + if (arrP == nullptr) { + return; + } + for (long i = 0; i < size; i++) { + sptr iface = nullptr; + if (ao->Get(i, iface) == ERR_OK) { + AAFwk::IString *iValue = AAFwk::IString::Query(iface); + if (iValue != nullptr) { + auto val = AAFwk::String::Unbox(iValue); + arrP[i] = MallocCString(val); + } + } + } + p->size = size; + p->value = static_cast(arrP); + } + + void ClearParametersPtr(CParameters *p, int count) + { + for (int i = 0; i < count; i++) { + free((p + i)->value); + } + free(p); + } + + template + void InnerWrapWantParamsArrayT(sptr &ao, CParameters *p) + { + long size = 0; + if (ao->GetLength(size) != ERR_OK) { + LOGE("fail to get length"); + return; + } + NativeT *arrP = static_cast(malloc(sizeof(NativeT) * size)); + if (arrP == nullptr) { + return; + } + for (long i = 0; i < size; i++) { + sptr iface = nullptr; + if (ao->Get(i, iface) == ERR_OK) { + TBase *iValue = TBase::Query(iface); + if (iValue != nullptr) { + arrP[i] = T::Unbox(iValue); + } + } + } + p->size = size; + p->value = static_cast(arrP); + } + + int GetFDValue(WantParams &wantParams, std::string key, int *ptr) + { + auto value = wantParams.GetParam(key); + AAFwk::IWantParams *o = AAFwk::IWantParams::Query(value); + if (o == nullptr) { + return NONE_VALUE; + } + AAFwk::WantParams wp = AAFwk::WantParamWrapper::Unbox(o); + value = wp.GetParam(VALUE_PROPERTY); + AAFwk::IInteger *ao = AAFwk::IInteger::Query(value); + if (ao == nullptr) { + LOGE("No value"); + return NONE_VALUE; + } + *ptr = OHOS::AAFwk::Integer::Unbox(ao); + return SUCCESS; + } + + void InnerWrapWantParamsFd(WantParams &wantParams, CParameters *p) + { + int *ptr = static_cast(malloc(sizeof(int))); + if (ptr == nullptr) { + return; + } + int error = GetFDValue(wantParams, std::string(p->key), ptr); + if (error != SUCCESS) { + free(ptr); + return; + } + p->value = static_cast(ptr); + p->size = sizeof(int32_t); + p->valueType = FD_TYPE; + } + + int InnerWrapWantParamsArrayFd(sptr &ao, CParameters *p) + { + long size = 0; + if (ao->GetLength(size) != ERR_OK) { + LOGE("fail to get length"); + return NONE_VALUE; + } + int *arrP = static_cast(malloc(sizeof(int) * size)); + if (arrP == nullptr) { + return NONE_VALUE; + } + for (long i = 0; i < size; i++) { + sptr iface = nullptr; + if (ao->Get(i, iface) == ERR_OK) { + AAFwk::IWantParams *iValue = AAFwk::IWantParams::Query(iface); + if (iValue == nullptr) { + free(arrP); + return NONE_VALUE; + } + WantParams wantP = AAFwk::WantParamWrapper::Unbox(iValue); + int ret = GetFDValue(wantP, std::string(p->key), arrP + i); + if (ret != SUCCESS) { + free(arrP); + return ret; + } + } + } + p->size = size; + p->value = arrP; + p->valueType = FD_PTR_TYPE; + return SUCCESS; + } + + void InnerWrapWantParamsArray(WantParams &wantParams, sptr &ao, CParameters *p) + { + LOGI("%{public}s called. key=%{public}s", __func__, p->key); + if (AAFwk::Array::IsStringArray(ao)) { + p->valueType = CHAR_PTR_TYPE; + InnerWrapWantParamsArrayString(ao, p); + return; + } else if (AAFwk::Array::IsBooleanArray(ao)) { + p->valueType = BOOL_PTR_TYPE; + return InnerWrapWantParamsArrayT(ao, p); + } else if (AAFwk::Array::IsIntegerArray(ao)) { + p->valueType = I32_PTR_TYPE; + return InnerWrapWantParamsArrayT(ao, p); + } else if (AAFwk::Array::IsLongArray(ao)) { + p->valueType = I64_PTR_TYPE; + return InnerWrapWantParamsArrayT(ao, p); + } else if (AAFwk::Array::IsDoubleArray(ao)) { + p->valueType = DOUBLE_PTR_TYPE; + return InnerWrapWantParamsArrayT(ao, p); + } else if (AAFwk::Array::IsWantParamsArray(ao)) { + p->valueType = FD_PTR_TYPE; + InnerWrapWantParamsArrayFd(ao, p); + return; + } else { + return; + } + } + + void ParseParameters(Want &want, CCommonEventData &cData) + { + WantParams wantP = want.GetParams(); + + std::map> paramsMap = wantP.GetParams(); + int count = 0; + auto size = static_cast(paramsMap.size()); + LOGI("paramsMap size = %{public}lld", size); + cData.parameters.head = static_cast(malloc(sizeof(CParameters) * size)); + if (cData.parameters.head == nullptr) { + return; + } + cData.parameters.size = size; + for (auto iter = paramsMap.begin(); iter != paramsMap.end(); iter++) { + auto ptr = cData.parameters.head + count; + ptr->key = MallocCString(iter->first); + ptr->value = nullptr; + if (AAFwk::IString::Query(iter->second) != nullptr) { + InnerWrapWantParamsString(wantP, ptr); + } else if (AAFwk::IBoolean::Query(iter->second) != nullptr) { + ptr->valueType = BOOL_TYPE; + InnerWrapWantParamsT(wantP, ptr); + } else if (AAFwk::IInteger::Query(iter->second) != nullptr) { + ptr->valueType = I32_TYPE; + InnerWrapWantParamsT(wantP, ptr); + } else if (AAFwk::IDouble::Query(iter->second) != nullptr) { + ptr->valueType = DOUBLE_TYPE; + InnerWrapWantParamsT(wantP, ptr); + } else if (AAFwk::IWantParams::Query(iter->second) != nullptr) { + InnerWrapWantParamsFd(wantP, ptr); + } else if (AAFwk::IArray::Query(iter->second) != nullptr) { + AAFwk::IArray *ao = AAFwk::IArray::Query(iter->second); + sptr array(ao); + InnerWrapWantParamsArray(wantP, array, ptr); + } + if (ptr == nullptr) { + return ClearParametersPtr(cData.parameters.head, count); + } + count++; + } + } + + void GetCommonEventData(const CommonEventData &data, CCommonEventData &cData) + { + auto want = data.GetWant(); + cData.code = data.GetCode(); + cData.data = MallocCString(data.GetData()); + cData.event = MallocCString(want.GetAction()); + cData.bundleName = MallocCString(want.GetBundle()); + ParseParameters(want, cData); + } +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/parameter_parse.h b/interfaces/kits/cj/src/parameter_parse.h new file mode 100755 index 00000000..522656aa --- /dev/null +++ b/interfaces/kits/cj/src/parameter_parse.h @@ -0,0 +1,31 @@ +/* + * 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 PARAMETER_PARSE_H +#define PARAMETER_PARSE_H + +#include "common_event_defination.h" +#include "common_event_data.h" + +namespace OHOS::CommonEventManager { + using CommonEventData = OHOS::EventFwk::CommonEventData; + using WantParams = OHOS::AAFwk::WantParams; + void GetCommonEventData(const CommonEventData &data, CCommonEventData &cData); + void charPtrToVector(char **charPtr, int size, std::vector &result); + void SetFdData(CParameters *head, WantParams &wantP); + void SetDataParameters(CArrParameters parameters, WantParams &wantP); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/cj/src/subscribe_info.cpp b/interfaces/kits/cj/src/subscribe_info.cpp new file mode 100755 index 00000000..4fb1a3eb --- /dev/null +++ b/interfaces/kits/cj/src/subscribe_info.cpp @@ -0,0 +1,86 @@ +/* + * 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 "subscribe_info.h" +#include "subscriber.h" +#include "common_event_defination.h" + +namespace OHOS::CommonEventManager { + // CommonEventSubscribeInfoImpl + CommonEventSubscribeInfoImpl::CommonEventSubscribeInfoImpl(std::shared_ptr info) + { + info_ = info; + } + + void CommonEventSubscribeInfoImpl::SetPriority(int32_t &priority) + { + return info_->SetPriority(priority); + } + + int32_t CommonEventSubscribeInfoImpl::GetPriority() + { + return info_->GetPriority(); + } + + void CommonEventSubscribeInfoImpl::SetUserId(int32_t &userId) + { + return info_->SetUserId(userId); + } + + int32_t CommonEventSubscribeInfoImpl::GetUserId() + { + return info_->GetUserId(); + } + + void CommonEventSubscribeInfoImpl::SetPermission(const std::string &permission) + { + return info_->SetPermission(permission); + } + + std::string CommonEventSubscribeInfoImpl::GetPermission() + { + return info_->GetPermission(); + } + + void CommonEventSubscribeInfoImpl::SetDeviceId(const std::string &deviceId) + { + return info_->SetDeviceId(deviceId); + } + + std::string CommonEventSubscribeInfoImpl::GetDeviceId() + { + return info_->GetDeviceId(); + } + + void CommonEventSubscribeInfoImpl::SetPublisherBundleName(const std::string &publisherBundleName) + { + return info_->SetPublisherBundleName(publisherBundleName); + } + + std::string CommonEventSubscribeInfoImpl::GetPublisherBundleName() + { + return info_->GetPublisherBundleName(); + } + + std::vector CommonEventSubscribeInfoImpl::GetEvents() + { + return info_->GetMatchingSkills().GetEvents(); + } + + std::shared_ptr CommonEventSubscribeInfoImpl::GetInfoPtr() + { + return info_; + } +} \ No newline at end of file diff --git a/interfaces/kits/cj/src/subscribe_info.h b/interfaces/kits/cj/src/subscribe_info.h new file mode 100755 index 00000000..5e6b28a1 --- /dev/null +++ b/interfaces/kits/cj/src/subscribe_info.h @@ -0,0 +1,57 @@ +/* + * 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 SUBSCRIBE_INFO_H +#define SUBSCRIBE_INFO_H + +#include "ffi_remote_data.h" +#include "common_event_subscribe_info.h" + +namespace OHOS::CommonEventManager { + using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo; + class CommonEventSubscribeInfoImpl : public OHOS::FFI::FFIData { + public: + OHOS::FFI::RuntimeType *GetRuntimeType() override + { + return GetClassType(); + } + explicit CommonEventSubscribeInfoImpl(std::shared_ptr info); + void SetPriority(int32_t &priority); + int32_t GetPriority(); + void SetUserId(int32_t &userId); + int32_t GetUserId(); + void SetPermission(const std::string &permission); + std::string GetPermission(); + void SetDeviceId(const std::string &deviceId); + std::string GetDeviceId(); + void SetPublisherBundleName(const std::string &publisherBundleName); + std::string GetPublisherBundleName(); + std::vector GetEvents(); + std::shared_ptr GetInfoPtr(); + + private: + friend class OHOS::FFI::RuntimeType; + friend class OHOS::FFI::TypeBase; + static OHOS::FFI::RuntimeType *GetClassType() + { + static OHOS::FFI::RuntimeType runtimeType = + OHOS::FFI::RuntimeType::Create("CommonEventSubscribeInfoImpl"); + return &runtimeType; + } + std::shared_ptr info_; + }; +} // namespace OHOS::CommonEventManager + +#endif // SUBSCRIBE_INFO_H diff --git a/interfaces/kits/cj/src/subscriber.cpp b/interfaces/kits/cj/src/subscriber.cpp new file mode 100755 index 00000000..3af03624 --- /dev/null +++ b/interfaces/kits/cj/src/subscriber.cpp @@ -0,0 +1,101 @@ +/* + * 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 "common_event.h" +#include "native_log.h" +#include "parameter_parse.h" + +namespace OHOS::CommonEventManager { + + std::atomic_ullong SubscriberImpl::subscriberID_ = 0; + + SubscriberImpl::SubscriberImpl(std::shared_ptr sp, int64_t infoId) + : CommonEventSubscriber(*sp) + { + id_ = ++subscriberID_; + LOGI("constructor SubscriberImpl"); + valid_ = std::make_shared(false); + infoId_ = infoId; + } + + SubscriberImpl::~SubscriberImpl() + { + LOGI("destructor SubscriberImpl[%{public}llu]", id_.load()); + *valid_ = false; + } + + void SubscriberImpl::OnReceiveEvent(const CommonEventData &data) + { + LOGI("Receive event.") + if (valid_ == nullptr || *(valid_) == false) { + LOGE("OnReceiveEvent commonEventDataWorkerData or ref is invalid which may be freed before"); + return; + } + if (this->IsOrderedCommonEvent()) { + LOGI("IsOrderedCommonEvent is true"); + SetPublishResult(this); + } + LOGI("Subscribe callback start to run.") + CCommonEventData cData; + GetCommonEventData(data, cData); + callback_(cData); + } + + unsigned long long SubscriberImpl::GetID() + { + return id_.load(); + } + + int64_t SubscriberImpl::GetSubscribeInfoId() + { + return infoId_; + } + + void SubscriberImpl::SetSubscriberManagerId(int64_t id) + { + managerId_ = id; + } + + int64_t SubscriberImpl::GetSubscriberManagerId() + { + return managerId_; + } + + void SubscriberImpl::SetCallback(const std::function &callback) + { + callback_ = callback; + *valid_ = true; + } + + SubscriberManager::SubscriberManager(std::shared_ptr info, int64_t infoId) + { + auto objectInfo = new (std::nothrow) SubscriberImpl(info, infoId); + subscriber = std::shared_ptr(objectInfo); + } + + SubscriberManager::~SubscriberManager() + { + } + + std::shared_ptr SubscriberManager::GetSubscriber() + { + return subscriber; + } + + int64_t SubscriberManager::GetSubscribeInfoId() + { + return subscriber->GetSubscribeInfoId(); + } +} // namespace OHOS::CommonEventManager \ No newline at end of file diff --git a/interfaces/kits/cj/src/subscriber.h b/interfaces/kits/cj/src/subscriber.h new file mode 100755 index 00000000..2881c057 --- /dev/null +++ b/interfaces/kits/cj/src/subscriber.h @@ -0,0 +1,86 @@ +/* + * 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 SUBSCRIBER_H +#define SUBSCRIBER_H + +#include "ffi_remote_data.h" +#include "common_event_defination.h" + +#include "common_event_subscribe_info.h" +#include "common_event_subscriber.h" + +namespace OHOS::CommonEventManager { + using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo; + using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber; + using CommonEventData = OHOS::EventFwk::CommonEventData; + + class SubscriberImpl; + struct SubscriberInstanceInfo; + + class SubscriberImpl : public CommonEventSubscriber { + public: + SubscriberImpl(std::shared_ptr sp, int64_t infoId); + ~SubscriberImpl() override; + + void OnReceiveEvent(const CommonEventData &data) override; + + unsigned long long GetID(); + + int64_t GetSubscribeInfoId(); + + void SetSubscriberManagerId(int64_t id); + + int64_t GetSubscriberManagerId(); + + void SetCallback(const std::function &callback); + + private: + std::function callback_; + std::shared_ptr valid_; + std::atomic_ullong id_; + static std::atomic_ullong subscriberID_; + int64_t infoId_; + int64_t managerId_ = -1; + }; + + class SubscriberManager : public OHOS::FFI::FFIData { + public: + OHOS::FFI::RuntimeType *GetRuntimeType() override + { + return GetClassType(); + } + SubscriberManager(std::shared_ptr sp, int64_t infoId); + ~SubscriberManager() override; + + std::shared_ptr GetSubscriber(); + + int64_t GetSubscribeInfoId(); + + private: + friend class OHOS::FFI::RuntimeType; + friend class OHOS::FFI::TypeBase; + static OHOS::FFI::RuntimeType *GetClassType() + { + static OHOS::FFI::RuntimeType runtimeType = + OHOS::FFI::RuntimeType::Create("SubscriberManager"); + return &runtimeType; + } + std::shared_ptr subscriber; + }; + +} // namespace OHOS::CommonEventManager + +#endif // SUBSCRIBER_H