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)26SubscriberObserver::SubscriberObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo) 27 :OHOS::EventFwk::CommonEventSubscriber(subscribeInfo) 28 {} 29 ~SubscriberObserver()30SubscriberObserver::~SubscriberObserver() 31 {} 32 OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)33void SubscriberObserver::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) 34 { 35 CommonEvent_RcvData *cData = new (std::nothrow) CommonEvent_RcvData(); 36 if (cData == nullptr) { 37 EVENT_LOGE("Failed to create CommonEventRcvData"); 38 return; 39 } 40 if (IsOrderedCommonEvent()) { 41 EVENT_LOGD("SetAsyncResult"); 42 SubscriberManager::GetInstance()->SetAsyncResult(this); 43 } 44 auto want = data.GetWant(); 45 cData->code = data.GetCode(); 46 cData->data = data.GetData(); 47 cData->event = want.GetAction(); 48 cData->bundleName = want.GetBundle(); 49 cData->parameters = new (std::nothrow) CArrParameters(); 50 if (cData->parameters == nullptr) { 51 EVENT_LOGE("Failed to init cData parameters"); 52 delete cData; 53 cData = nullptr; 54 return; 55 } 56 cData->parameters->wantParams = want.GetParams(); 57 if (callback_ != nullptr) { 58 (*callback_)(cData); 59 } 60 delete cData->parameters; 61 cData->parameters = nullptr; 62 delete cData; 63 cData = nullptr; 64 } 65 SetCallback(CommonEvent_ReceiveCallback callback)66void SubscriberObserver::SetCallback(CommonEvent_ReceiveCallback callback) 67 { 68 callback_ = callback; 69 } 70 71 ffrt::mutex SubscriberManager::instanceMutex_; 72 ffrt::mutex SubscriberManager::resultCacheMutex_; 73 std::shared_ptr<SubscriberManager> SubscriberManager::instance_; 74 std::map<std::shared_ptr<SubscriberObserver>, ResultCacheItem> SubscriberManager::resultCache_; 75 GetInstance()76std::shared_ptr<SubscriberManager> SubscriberManager::GetInstance() 77 { 78 if (instance_ == nullptr) { 79 std::lock_guard<ffrt::mutex> lock(instanceMutex_); 80 if (instance_ == nullptr) { 81 instance_ = std::make_shared<SubscriberManager>(); 82 } 83 } 84 return instance_; 85 } 86 CreateSubscriber(const CommonEvent_SubscribeInfo * subscribeInfo,CommonEvent_ReceiveCallback callback)87CommonEvent_Subscriber* SubscriberManager::CreateSubscriber(const CommonEvent_SubscribeInfo* subscribeInfo, 88 CommonEvent_ReceiveCallback callback) 89 { 90 if (subscribeInfo == nullptr) { 91 EVENT_LOGE("SubscribeInfo is null"); 92 return nullptr; 93 } 94 OHOS::EventFwk::MatchingSkills matchingSkills; 95 for (const auto& iter : subscribeInfo->events) { 96 if (!iter.empty()) { 97 matchingSkills.AddEvent(iter); 98 } 99 } 100 OHOS::EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); 101 if (!subscribeInfo->permission.empty()) { 102 commonEventSubscribeInfo.SetPermission(subscribeInfo->permission); 103 } 104 if (!subscribeInfo->bundleName.empty()) { 105 commonEventSubscribeInfo.SetPublisherBundleName(subscribeInfo->bundleName); 106 } 107 108 auto observer = std::make_shared<SubscriberObserver>(commonEventSubscribeInfo); 109 observer->SetCallback(callback); 110 return new (std::nothrow) std::shared_ptr<SubscriberObserver>(observer); 111 } 112 DestroySubscriber(CommonEvent_Subscriber * subscriber)113void SubscriberManager::DestroySubscriber(CommonEvent_Subscriber* subscriber) 114 { 115 if (subscriber != nullptr) { 116 auto subscriberPtr = reinterpret_cast<std::shared_ptr<SubscriberObserver>*>(subscriber); 117 delete subscriberPtr; 118 subscriberPtr = nullptr; 119 } 120 } 121 Subscribe(const CommonEvent_Subscriber * subscriber)122CommonEvent_ErrCode SubscriberManager::Subscribe(const CommonEvent_Subscriber* subscriber) 123 { 124 if (subscriber == nullptr) { 125 EVENT_LOGE("subscriber is null"); 126 return COMMONEVENT_ERR_INVALID_PARAMETER; 127 } 128 auto observer = *(reinterpret_cast<const std::shared_ptr<SubscriberObserver>*>(subscriber)); 129 int32_t ret = OHOS::EventFwk::CommonEventManager::NewSubscribeCommonEvent(observer); 130 if (ret == OHOS::Notification::ERR_NOTIFICATION_CES_COMMON_SYSTEMCAP_NOT_SUPPORT) { 131 return COMMONEVENT_ERR_SUBSCRIBER_NUM_EXCEEDED; 132 } 133 if (ret != OHOS::ERR_OK) { 134 return static_cast<CommonEvent_ErrCode>(ret); 135 } 136 { 137 std::lock_guard<ffrt::mutex> lock(resultCacheMutex_); 138 resultCache_.emplace(observer, ResultCacheItem()); 139 } 140 return static_cast<CommonEvent_ErrCode>(ret); 141 } 142 UnSubscribe(const CommonEvent_Subscriber * subscriber)143CommonEvent_ErrCode SubscriberManager::UnSubscribe(const CommonEvent_Subscriber* subscriber) 144 { 145 if (subscriber == nullptr) { 146 EVENT_LOGE("subscriber is null"); 147 return COMMONEVENT_ERR_INVALID_PARAMETER; 148 } 149 auto observer = *(reinterpret_cast<const std::shared_ptr<SubscriberObserver>*>(subscriber)); 150 int32_t ret = OHOS::EventFwk::CommonEventManager::NewUnSubscribeCommonEvent(observer); 151 { 152 std::lock_guard<ffrt::mutex> lock(resultCacheMutex_); 153 resultCache_.erase(observer); 154 } 155 return static_cast<CommonEvent_ErrCode>(ret); 156 } 157 SetAsyncResult(SubscriberObserver * subscriber)158void SubscriberManager::SetAsyncResult(SubscriberObserver* subscriber) 159 { 160 if (subscriber == nullptr) { 161 EVENT_LOGE("subscriber is null"); 162 return; 163 } 164 { 165 std::lock_guard<ffrt::mutex> lock(resultCacheMutex_); 166 for (auto& iter : resultCache_) { 167 if (iter.first.get() == subscriber) { 168 iter.second.result = subscriber->GoAsyncCommonEvent(); 169 break; 170 } 171 } 172 } 173 } 174 GetAsyncResult(const SubscriberObserver * subscriber)175ResultCacheItem* SubscriberManager::GetAsyncResult(const SubscriberObserver* subscriber) 176 { 177 if (subscriber == nullptr) { 178 EVENT_LOGE("subscriber is null"); 179 return nullptr; 180 } 181 { 182 std::lock_guard<ffrt::mutex> lock(resultCacheMutex_); 183 for (auto& iter : resultCache_) { 184 if (iter.first.get() == subscriber) { 185 return &iter.second; 186 } 187 } 188 } 189 EVENT_LOGI("subscriber not found"); 190 return nullptr; 191 }