公共事件服务通知模块增加ffi桥阶层代码

Signed-off-by: PipiSummer <xiatian44@huawei.com>
This commit is contained in:
PipiSummer 2024-05-07 14:41:08 +08:00
parent 68ce04950b
commit 6fc2a9a162
17 changed files with 1855 additions and 1 deletions

View File

@ -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",

55
interfaces/kits/cj/BUILD.gn Executable file
View File

@ -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"
}

View File

@ -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<std::shared_ptr<SubscriberImpl>, SubscriberInstanceInfo> subscriberImpls;
static std::mutex subscriberImplMutex;
void SetPublishResult(OHOS::CommonEventManager::SubscriberImpl *subImpl)
{
LOGI("SetPublishResult start");
std::lock_guard<std::mutex> lock(subscriberImplMutex);
for (auto subscriberImpl : subscriberImpls) {
if (subscriberImpl.first.get() == subImpl) {
LOGI("Get success.");
subscriberImpls[subscriberImpl.first].commonEventResult = subImpl->GoAsyncCommonEvent();
break;
}
}
}
std::shared_ptr<AsyncCommonEventResult> GetAsyncResult(const SubscriberImpl *objectInfo)
{
LOGI("GetAsyncResult start");
if (!objectInfo) {
LOGE("Invalidity objectInfo");
return nullptr;
}
std::lock_guard<std::mutex> 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<SubscriberImpl> subscriber,
const std::function<void(CCommonEventData)> &callback)
{
LOGI("Set subscriberImpls.")
subscriber->SetCallback(callback);
AsyncCallbackInfoSubscribe *asyncCallbackInfo =
new (std::nothrow) AsyncCallbackInfoSubscribe{.callback = callback, .subscriber = subscriber};
std::lock_guard<std::mutex> lock(subscriberImplMutex);
subscriberImpls[asyncCallbackInfo->subscriber].asyncCallbackInfo.emplace_back(asyncCallbackInfo);
}
void DeleteCallBack(std::vector<AsyncCallbackInfoSubscribe *> &asyncCallbackInfos)
{
for (auto asyncCallbackInfo : asyncCallbackInfos) {
delete asyncCallbackInfo;
asyncCallbackInfo = nullptr;
}
}
void SetSubscriber(int64_t id, std::shared_ptr<OHOS::CommonEventManager::SubscriberImpl> newSubscriber)
{
std::lock_guard<std::mutex> 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<SubscriberImpl> subscriber)
{
LOGI("DeleteSubscribe start");
std::lock_guard<std::mutex> 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<SubscriberImpl> subscriber, int64_t &code)
{
std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get());
if (result) {
code = result->GetCode();
} else {
code = 0;
}
}
int32_t SetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber, int32_t code)
{
std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get());
if (result) {
return result->SetCode(code) ? NO_ERROR : ERR_CES_FAILED;
}
return NO_ERROR;
}
void GetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber, char *data)
{
std::shared_ptr<AsyncCommonEventResult> 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<SubscriberImpl> subscriber, const char *data)
{
std::shared_ptr<AsyncCommonEventResult> 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<SubscriberImpl> subscriber, int32_t code, const char *data)
{
std::shared_ptr<AsyncCommonEventResult> 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<SubscriberImpl> subscriber, bool &data)
{
std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get());
if (result) {
data = result->IsStickyCommonEvent();
} else {
data = subscriber->IsStickyCommonEvent();
}
}
void IsCommonEventOrdered(std::shared_ptr<SubscriberImpl> subscriber, bool &data)
{
std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get());
if (result) {
data = result->IsOrderedCommonEvent();
} else {
data = subscriber->IsOrderedCommonEvent();
}
}
}

View File

@ -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<void(CCommonEventData)> callback;
std::shared_ptr<SubscriberImpl> subscriber = nullptr;
int8_t errorCode = NO_ERROR;
};
struct SubscriberInstanceInfo {
std::vector<AsyncCallbackInfoSubscribe *> asyncCallbackInfo;
std::shared_ptr<AsyncCommonEventResult> commonEventResult = nullptr;
};
void SetPublishResult(OHOS::CommonEventManager::SubscriberImpl *subImpl);
std::shared_ptr<AsyncCommonEventResult> GetAsyncResult(const SubscriberImpl *objectInfo);
void SetSubscribeInfo(std::shared_ptr<SubscriberImpl> subscriber,
const std::function<void(CCommonEventData)> &callback);
void SetSubscriber(int64_t id, std::shared_ptr<OHOS::CommonEventManager::SubscriberImpl> newSubscriber);
void DeleteSubscribe(std::shared_ptr<SubscriberImpl> subscriber);
void GetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber, int64_t &code);
int32_t SetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber, int32_t code);
void GetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber, char *data);
int32_t SetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber, const char *data);
int32_t SetSubscriberCodeAndData(std::shared_ptr<SubscriberImpl> subscriber, int32_t code, const char *data);
void IsCommonEventSticky(std::shared_ptr<SubscriberImpl> subscriber, bool &data);
void IsCommonEventOrdered(std::shared_ptr<SubscriberImpl> subscriber, bool &data);
}
#endif

View File

@ -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

View File

@ -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<CommonEventSubscribeInfoImpl>(infoPtr);
return ptr->GetID();
}
int64_t CJ_CreateSubscriber(int64_t id)
{
std::shared_ptr<OHOS::CommonEventManager::SubscriberImpl> newSubscriber = nullptr;
SetSubscriber(id, newSubscriber);
if (newSubscriber) {
return newSubscriber->GetSubscriberManagerId();
}
auto instance = FFIData::GetData<CommonEventSubscribeInfoImpl>(id);
if (!instance) {
LOGE("CommonEventSubscribeInfoImpl instance not exist %{public}" PRId64, id);
return static_cast<int64_t>(ERR_INVALID_INSTANCE_ID);
}
auto info = instance->GetInfoPtr();
auto ptr = FFIData::Create<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(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<SubscriberManager>(id);
if (!instance) {
LOGE("SubscriberManager instance not exist %{public}" PRId64, id);
return ERR_INVALID_INSTANCE_ID;
}
auto subscriber = instance->GetSubscriber();
std::shared_ptr<AsyncCommonEventResult> 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<CommonEventSubscribeInfoImpl>(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<CommonEventSubscribeInfoImpl>(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<CommonEventSubscribeInfoImpl>(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<CommonEventSubscribeInfoImpl>(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<CommonEventSubscribeInfoImpl>(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<CommonEventSubscribeInfoImpl>(id);
auto str = instance->GetPermission();
auto ret = MallocCString(str);
return ret;
}
const char *CJ_GetDeviceId(int64_t id)
{
auto instance = FFIData::GetData<CommonEventSubscribeInfoImpl>(id);
auto str = instance->GetDeviceId();
auto ret = MallocCString(str);
return ret;
}
int32_t CJ_GetUserId(int64_t id)
{
auto instance = FFIData::GetData<CommonEventSubscribeInfoImpl>(id);
return instance->GetUserId();
}
int32_t CJ_GetPriority(int64_t id)
{
auto instance = FFIData::GetData<CommonEventSubscribeInfoImpl>(id);
return instance->GetPriority();
}
const char *CJ_GetBundleName(int64_t id)
{
auto instance = FFIData::GetData<CommonEventSubscribeInfoImpl>(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<std::string> &vec)
{
char** result = static_cast<char**>(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<CommonEventSubscribeInfoImpl>(id);
auto vStr = instance->GetEvents();
CArrString ret = {.head = VectorToCharPointer(vStr), .size = vStr.size()};
if (ret.head == nullptr) {
LOGE("Failed to malloc.");
}
return ret;
}
}
}

View File

@ -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 <cstdint>
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

View File

@ -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<std::string> 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<CommonEventSubscribeInfo> 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<CommonEventSubscribeInfo>(subscriberInfo);
}
int32_t CommonEventManagerImpl::Subscribe(std::shared_ptr<SubscriberImpl> subscriber,
const std::function<void(CCommonEventData)> &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<SubscriberImpl> subscriber)
{
auto errorCode = OHOS::EventFwk::CommonEventManager::NewUnSubscribeCommonEvent(subscriber);
DeleteSubscribe(subscriber);
return errorCode;
}
}

View File

@ -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 <cinttypes>
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<CommonEventSubscribeInfo> CreateCommonEventSubscribeInfo(char **event, int64_t size);
static int32_t Subscribe(std::shared_ptr<SubscriberImpl> subscriber,
const std::function<void(CCommonEventData)> &callback);
static int32_t Unsubscribe(std::shared_ptr<SubscriberImpl> subscriber);
};
}
#endif // COMMON_EVENT_MANAGER_IMPL

View File

@ -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;
}

View File

@ -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

View File

@ -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<std::string> &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<OHOS::AAFwk::IWantParams> pWantParams = OHOS::AAFwk::WantParamWrapper::Box(wp);
wantP.SetParam(key, pWantParams);
}
bool InnerSetWantParamsArrayString(
const std::string &key, const std::vector<std::string> &value, AAFwk::WantParams &wantParams)
{
size_t size = value.size();
sptr<AAFwk::IArray> 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<int> &value,
AAFwk::WantParams &wantParams)
{
size_t size = value.size();
sptr<AAFwk::IArray> 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<long> &value,
AAFwk::WantParams &wantParams)
{
size_t size = value.size();
sptr<AAFwk::IArray> 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<bool> &value,
AAFwk::WantParams &wantParams)
{
size_t size = value.size();
sptr<AAFwk::IArray> 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<double> &value, AAFwk::WantParams &wantParams)
{
size_t size = value.size();
sptr<AAFwk::IArray> 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<AAFwk::IArray> ao = new (std::nothrow) AAFwk::Array(size, AAFwk::g_IID_IWantParams);
if (ao != nullptr) {
for (size_t i = 0; i < static_cast<size_t>(size); i++) {
WantParams wp;
SetFdData(std::string(head->key), static_cast<int *>(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<int32_t *>(head->value)));
} else if (head->valueType == DOUBLE_TYPE) { // double
wantP.SetParam(key, OHOS::AAFwk::Double::Box(*static_cast<double *>(head->value)));
} else if (head->valueType == STR_TYPE) { // std::string
wantP.SetParam(key, OHOS::AAFwk::String::Box(std::string(static_cast<char *>(head->value))));
} else if (head->valueType == BOOL_TYPE) { // bool
wantP.SetParam(key, OHOS::AAFwk::Boolean::Box(*static_cast<bool *>(head->value)));
} else if (head->valueType == FD_TYPE) { // "FD"
SetFdData(key, static_cast<int *>(head->value), wantP);
} else if (head->valueType == CHAR_PTR_TYPE) { // char**
char **strPtr = (char **)head->value;
std::vector<std::string> 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<int> 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<long> longVec(longArr, longArr + head->size);
InnerSetWantParamsArrayLong(key, longVec, wantP);
} else if (head->valueType == BOOL_PTR_TYPE) { // bool*
bool *boolArr = (bool *)head->value;
std::vector<bool> boolVec(boolArr, boolArr + head->size);
InnerSetWantParamsArrayBool(key, boolVec, wantP);
} else if (head->valueType == DOUBLE_PTR_TYPE) { // double*
double *doubleArr = (double *)head->value;
std::vector<double> 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<char>::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<int64_t>(natValue.length()) + 1;
p->valueType = STR_TYPE;
}
template <class TBase, class T, class NativeT>
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<NativeT *>(malloc(sizeof(NativeT)));
if (ptr == nullptr) {
return;
}
*ptr = natValue;
p->value = static_cast<void*>(ptr);
p->size = sizeof(NativeT);
}
void InnerWrapWantParamsArrayString(sptr<AAFwk::IArray> &ao, CParameters *p)
{
long size = 0;
if (ao->GetLength(size) != ERR_OK) {
LOGE("fail to get length");
return;
}
char **arrP = static_cast<char **>(malloc(sizeof(char *) * size));
if (arrP == nullptr) {
return;
}
for (long i = 0; i < size; i++) {
sptr<AAFwk::IInterface> 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<void *>(arrP);
}
void ClearParametersPtr(CParameters *p, int count)
{
for (int i = 0; i < count; i++) {
free((p + i)->value);
}
free(p);
}
template <class TBase, class T, class NativeT>
void InnerWrapWantParamsArrayT(sptr<AAFwk::IArray> &ao, CParameters *p)
{
long size = 0;
if (ao->GetLength(size) != ERR_OK) {
LOGE("fail to get length");
return;
}
NativeT *arrP = static_cast<NativeT *>(malloc(sizeof(NativeT) * size));
if (arrP == nullptr) {
return;
}
for (long i = 0; i < size; i++) {
sptr<AAFwk::IInterface> 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<void *>(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<int *>(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<void*>(ptr);
p->size = sizeof(int32_t);
p->valueType = FD_TYPE;
}
int InnerWrapWantParamsArrayFd(sptr<AAFwk::IArray> &ao, CParameters *p)
{
long size = 0;
if (ao->GetLength(size) != ERR_OK) {
LOGE("fail to get length");
return NONE_VALUE;
}
int *arrP = static_cast<int *>(malloc(sizeof(int) * size));
if (arrP == nullptr) {
return NONE_VALUE;
}
for (long i = 0; i < size; i++) {
sptr<AAFwk::IInterface> 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<AAFwk::IArray> &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<AAFwk::IBoolean, AAFwk::Boolean, bool>(ao, p);
} else if (AAFwk::Array::IsIntegerArray(ao)) {
p->valueType = I32_PTR_TYPE;
return InnerWrapWantParamsArrayT<AAFwk::IInteger, AAFwk::Integer, int>(ao, p);
} else if (AAFwk::Array::IsLongArray(ao)) {
p->valueType = I64_PTR_TYPE;
return InnerWrapWantParamsArrayT<AAFwk::ILong, AAFwk::Long, int64_t>(ao, p);
} else if (AAFwk::Array::IsDoubleArray(ao)) {
p->valueType = DOUBLE_PTR_TYPE;
return InnerWrapWantParamsArrayT<AAFwk::IDouble, AAFwk::Double, double>(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<std::string, sptr<OHOS::AAFwk::IInterface>> paramsMap = wantP.GetParams();
int count = 0;
auto size = static_cast<int64_t>(paramsMap.size());
LOGI("paramsMap size = %{public}lld", size);
cData.parameters.head = static_cast<CParameters *>(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<AAFwk::IBoolean, AAFwk::Boolean, bool>(wantP, ptr);
} else if (AAFwk::IInteger::Query(iter->second) != nullptr) {
ptr->valueType = I32_TYPE;
InnerWrapWantParamsT<AAFwk::IInteger, AAFwk::Integer, int>(wantP, ptr);
} else if (AAFwk::IDouble::Query(iter->second) != nullptr) {
ptr->valueType = DOUBLE_TYPE;
InnerWrapWantParamsT<AAFwk::IDouble, AAFwk::Double, double>(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<AAFwk::IArray> 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);
}
}

View File

@ -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<std::string> &result);
void SetFdData(CParameters *head, WantParams &wantP);
void SetDataParameters(CArrParameters parameters, WantParams &wantP);
}
#endif

View File

@ -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<CommonEventSubscribeInfo> 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<std::string> CommonEventSubscribeInfoImpl::GetEvents()
{
return info_->GetMatchingSkills().GetEvents();
}
std::shared_ptr<CommonEventSubscribeInfo> CommonEventSubscribeInfoImpl::GetInfoPtr()
{
return info_;
}
}

View File

@ -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<CommonEventSubscribeInfo> 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<std::string> GetEvents();
std::shared_ptr<CommonEventSubscribeInfo> 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<OHOS::FFI::FFIData>("CommonEventSubscribeInfoImpl");
return &runtimeType;
}
std::shared_ptr<CommonEventSubscribeInfo> info_;
};
} // namespace OHOS::CommonEventManager
#endif // SUBSCRIBE_INFO_H

View File

@ -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<CommonEventSubscribeInfo> sp, int64_t infoId)
: CommonEventSubscriber(*sp)
{
id_ = ++subscriberID_;
LOGI("constructor SubscriberImpl");
valid_ = std::make_shared<bool>(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<void(CCommonEventData)> &callback)
{
callback_ = callback;
*valid_ = true;
}
SubscriberManager::SubscriberManager(std::shared_ptr<CommonEventSubscribeInfo> info, int64_t infoId)
{
auto objectInfo = new (std::nothrow) SubscriberImpl(info, infoId);
subscriber = std::shared_ptr<SubscriberImpl>(objectInfo);
}
SubscriberManager::~SubscriberManager()
{
}
std::shared_ptr<SubscriberImpl> SubscriberManager::GetSubscriber()
{
return subscriber;
}
int64_t SubscriberManager::GetSubscribeInfoId()
{
return subscriber->GetSubscribeInfoId();
}
} // namespace OHOS::CommonEventManager

View File

@ -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<CommonEventSubscribeInfo> 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<void(CCommonEventData)> &callback);
private:
std::function<void(CCommonEventData)> callback_;
std::shared_ptr<bool> 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<CommonEventSubscribeInfo> sp, int64_t infoId);
~SubscriberManager() override;
std::shared_ptr<SubscriberImpl> 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<OHOS::FFI::FFIData>("SubscriberManager");
return &runtimeType;
}
std::shared_ptr<SubscriberImpl> subscriber;
};
} // namespace OHOS::CommonEventManager
#endif // SUBSCRIBER_H