1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "event_log_wrapper.h"
17 #include "ces_inner_error_code.h"
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "oh_commonevent_parameters_parse.h"
21 #include "oh_commonevent_wrapper.h"
22 #include <cstdlib>
23 #include <memory>
24 #include <new>
25 
SubscriberObserver(const OHOS::EventFwk::CommonEventSubscribeInfo & subscribeInfo)26 SubscriberObserver::SubscriberObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo)
27     :OHOS::EventFwk::CommonEventSubscriber(subscribeInfo)
28 {}
29 
~SubscriberObserver()30 SubscriberObserver::~SubscriberObserver()
31 {}
32 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)33 void SubscriberObserver::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
34 {
35     EVENT_LOGD("Receive CommonEvent action = %{public}s", data.GetWant().GetAction().c_str());
36     CommonEvent_RcvData *cData = new (std::nothrow) CommonEvent_RcvData();
37     if (cData == nullptr) {
38         EVENT_LOGE("Failed to create CommonEventRcvData");
39         return;
40     }
41     int32_t code = OHOS::EventFwk::GetCommonEventData(data, cData);
42     if (code != COMMONEVENT_ERR_OK) {
43         EVENT_LOGE("Failed to init GetCommonEventData");\
44         delete cData;
45         cData = nullptr;
46         return;
47     }
48     if (callback_ != nullptr) {
49         EVENT_LOGD("Subscribe callback start to run.");
50         (*callback_)(cData);
51     }
52     OHOS::EventFwk::FreeCCommonEventData(cData);
53     delete cData;
54     cData = nullptr;
55 }
56 
SetCallback(CommonEvent_ReceiveCallback callback)57 void SubscriberObserver::SetCallback(CommonEvent_ReceiveCallback callback)
58 {
59     callback_ = callback;
60 }
61 
62 std::mutex SubscriberManager::instanceMutex_;
63 std::shared_ptr<SubscriberManager> SubscriberManager::instance_;
64 
GetInstance()65 std::shared_ptr<SubscriberManager> SubscriberManager::GetInstance()
66 {
67     if (instance_ == nullptr) {
68         std::lock_guard<std::mutex> lock(instanceMutex_);
69         if (instance_ == nullptr) {
70             instance_ = std::make_shared<SubscriberManager>();
71         }
72     }
73     return instance_;
74 }
75 
CreateSubscriber(const CommonEvent_SubscribeInfo * subscribeInfo,CommonEvent_ReceiveCallback callback)76 CommonEvent_Subscriber* SubscriberManager::CreateSubscriber(const CommonEvent_SubscribeInfo* subscribeInfo,
77     CommonEvent_ReceiveCallback callback)
78 {
79     if (subscribeInfo == nullptr) {
80         EVENT_LOGE("SubscribeInfo is null");
81         return nullptr;
82     }
83     OHOS::EventFwk::MatchingSkills matchingSkills;
84     for (uint32_t i = 0; i < subscribeInfo->eventLength; i++) {
85         if (subscribeInfo->events[i] != nullptr) {
86             matchingSkills.AddEvent(subscribeInfo->events[i]);
87         }
88     }
89     OHOS::EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
90     if (subscribeInfo->permission != nullptr) {
91         commonEventSubscribeInfo.SetPermission(subscribeInfo->permission);
92     }
93     if (subscribeInfo->bundleName != nullptr) {
94         commonEventSubscribeInfo.SetPublisherBundleName(subscribeInfo->bundleName);
95     }
96 
97     auto observer = std::make_shared<SubscriberObserver>(commonEventSubscribeInfo);
98     observer->SetCallback(callback);
99     return new (std::nothrow) std::shared_ptr<SubscriberObserver>(observer);
100 }
101 
DestroySubscriber(CommonEvent_Subscriber * subscriber)102 void SubscriberManager::DestroySubscriber(CommonEvent_Subscriber* subscriber)
103 {
104     if (subscriber != nullptr) {
105         auto subscriberPtr = reinterpret_cast<std::shared_ptr<SubscriberObserver>*>(subscriber);
106         delete subscriberPtr;
107         subscriberPtr = nullptr;
108     }
109 }
110 
Subscribe(const CommonEvent_Subscriber * subscriber)111 CommonEvent_ErrCode SubscriberManager::Subscribe(const CommonEvent_Subscriber* subscriber)
112 {
113     if (subscriber == nullptr) {
114         EVENT_LOGE("subscriber is null");
115         return COMMONEVENT_ERR_INVALID_PARAMETER;
116     }
117     auto observer = *(reinterpret_cast<const std::shared_ptr<SubscriberObserver>*>(subscriber));
118     int32_t ret = OHOS::EventFwk::CommonEventManager::NewSubscribeCommonEvent(observer);
119     if (ret == OHOS::Notification::ERR_NOTIFICATION_CES_COMMON_SYSTEMCAP_NOT_SUPPORT) {
120         return COMMONEVENT_ERR_SUBSCRIBER_NUM_EXCEEDED;
121     }
122     return static_cast<CommonEvent_ErrCode>(ret);
123 }
124 
UnSubscribe(const CommonEvent_Subscriber * subscriber)125 CommonEvent_ErrCode SubscriberManager::UnSubscribe(const CommonEvent_Subscriber* subscriber)
126 {
127     if (subscriber == nullptr) {
128         EVENT_LOGE("subscriber is null");
129         return COMMONEVENT_ERR_INVALID_PARAMETER;
130     }
131     auto observer = *(reinterpret_cast<const std::shared_ptr<SubscriberObserver>*>(subscriber));
132     int32_t ret = OHOS::EventFwk::CommonEventManager::NewUnSubscribeCommonEvent(observer);
133     return static_cast<CommonEvent_ErrCode>(ret);
134 }