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 "common_event.h" 17 #include "native_log.h" 18 #include "common_event_manager_impl.h" 19 20 #include "common_event_manager.h" 21 #include "securec.h" 22 using namespace OHOS::FFI; 23 using CommonEventManagerImpl = OHOS::CommonEventManager::CommonEventManagerImpl; 24 25 namespace OHOS::CommonEventManager { 26 27 static std::map<std::shared_ptr<SubscriberImpl>, SubscriberInstanceInfo> subscriberImpls; 28 static std::mutex subscriberImplMutex; 29 SetPublishResult(OHOS::CommonEventManager::SubscriberImpl * subImpl)30 void SetPublishResult(OHOS::CommonEventManager::SubscriberImpl *subImpl) 31 { 32 LOGI("SetPublishResult start"); 33 std::lock_guard<std::mutex> lock(subscriberImplMutex); 34 for (auto subscriberImpl : subscriberImpls) { 35 if (subscriberImpl.first.get() == subImpl) { 36 LOGI("Get success."); 37 subscriberImpls[subscriberImpl.first].commonEventResult = subImpl->GoAsyncCommonEvent(); 38 break; 39 } 40 } 41 } 42 GetAsyncResult(const SubscriberImpl * objectInfo)43 std::shared_ptr<AsyncCommonEventResult> GetAsyncResult(const SubscriberImpl *objectInfo) 44 { 45 LOGI("GetAsyncResult start"); 46 if (!objectInfo) { 47 LOGE("Invalidity objectInfo"); 48 return nullptr; 49 } 50 std::lock_guard<std::mutex> lock(subscriberImplMutex); 51 for (auto subscriberImpl : subscriberImpls) { 52 if (subscriberImpl.first.get() == objectInfo) { 53 return subscriberImpl.second.commonEventResult; 54 } 55 } 56 LOGI("No found objectInfo"); 57 return nullptr; 58 } 59 SetSubscribeInfo(std::shared_ptr<SubscriberImpl> subscriber,const std::function<void (CCommonEventData)> & callback)60 void SetSubscribeInfo(std::shared_ptr<SubscriberImpl> subscriber, 61 const std::function<void(CCommonEventData)> &callback) 62 { 63 LOGI("Set subscriberImpls.") 64 subscriber->SetCallback(callback); 65 AsyncCallbackInfoSubscribe *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfoSubscribe{ 66 .callback = callback, .subscriber = subscriber}; 67 if (asyncCallbackInfo == nullptr) { 68 LOGE("SetSubscribeInfo failed: out of memory."); 69 return; 70 } 71 std::lock_guard<std::mutex> lock(subscriberImplMutex); 72 subscriberImpls[asyncCallbackInfo->subscriber].asyncCallbackInfo.emplace_back(asyncCallbackInfo); 73 } 74 DeleteCallBack(const std::vector<AsyncCallbackInfoSubscribe * > & asyncCallbackInfos)75 void DeleteCallBack(const std::vector<AsyncCallbackInfoSubscribe *> &asyncCallbackInfos) 76 { 77 for (auto asyncCallbackInfo : asyncCallbackInfos) { 78 delete asyncCallbackInfo; 79 asyncCallbackInfo = nullptr; 80 } 81 } 82 GetManagerId(int64_t id,bool & haveId)83 int64_t GetManagerId(int64_t id, bool &haveId) 84 { 85 std::lock_guard<std::mutex> lock(subscriberImplMutex); 86 for (auto subscriberImpl : subscriberImpls) { 87 if (subscriberImpl.first->GetSubscribeInfoId() == id) { 88 std::shared_ptr<OHOS::CommonEventManager::SubscriberImpl> newSubscriber = subscriberImpl.first; 89 DeleteCallBack(subscriberImpl.second.asyncCallbackInfo); 90 newSubscriber->SetCallback(nullptr); 91 OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(newSubscriber); 92 subscriberImpls.erase(newSubscriber); 93 haveId = true; 94 return newSubscriber->GetSubscriberManagerId(); 95 } 96 } 97 return 0; 98 } 99 DeleteSubscribe(std::shared_ptr<SubscriberImpl> subscriber)100 void DeleteSubscribe(std::shared_ptr<SubscriberImpl> subscriber) 101 { 102 LOGI("DeleteSubscribe start"); 103 std::lock_guard<std::mutex> lock(subscriberImplMutex); 104 auto subscribe = subscriberImpls.find(subscriber); 105 if (subscribe != subscriberImpls.end()) { 106 for (auto asyncCallbackInfoSubscribe : subscribe->second.asyncCallbackInfo) { 107 delete asyncCallbackInfoSubscribe; 108 asyncCallbackInfoSubscribe = nullptr; 109 } 110 subscriber->SetCallback(nullptr); 111 subscriberImpls.erase(subscribe); 112 } 113 } 114 GetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber,int64_t & code)115 void GetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber, int64_t &code) 116 { 117 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 118 if (result) { 119 code = result->GetCode(); 120 } else { 121 code = 0; 122 } 123 } 124 SetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber,int32_t code)125 int32_t SetSubscriberCode(std::shared_ptr<SubscriberImpl> subscriber, int32_t code) 126 { 127 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 128 if (result) { 129 return result->SetCode(code) ? NO_ERROR : ERR_CES_FAILED; 130 } 131 return NO_ERROR; 132 } 133 GetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber)134 std::string GetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber) 135 { 136 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 137 if (result) { 138 return result->GetData(); 139 } else { 140 return std::string(); 141 } 142 } 143 SetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber,const char * data)144 int32_t SetSubscriberData(std::shared_ptr<SubscriberImpl> subscriber, const char *data) 145 { 146 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 147 if (result) { 148 return result->SetData(std::string(data)) ? NO_ERROR : ERR_CES_FAILED; 149 } 150 return NO_ERROR; 151 } 152 SetSubscriberCodeAndData(std::shared_ptr<SubscriberImpl> subscriber,int32_t code,const char * data)153 int32_t SetSubscriberCodeAndData(std::shared_ptr<SubscriberImpl> subscriber, int32_t code, const char *data) 154 { 155 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 156 if (result) { 157 return result->SetCodeAndData(code, std::string(data)) ? NO_ERROR : ERR_CES_FAILED; 158 } 159 return NO_ERROR; 160 } 161 IsCommonEventSticky(std::shared_ptr<SubscriberImpl> subscriber,bool & data)162 void IsCommonEventSticky(std::shared_ptr<SubscriberImpl> subscriber, bool &data) 163 { 164 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 165 if (result) { 166 data = result->IsStickyCommonEvent(); 167 } else { 168 data = subscriber->IsStickyCommonEvent(); 169 } 170 } 171 IsCommonEventOrdered(std::shared_ptr<SubscriberImpl> subscriber,bool & data)172 void IsCommonEventOrdered(std::shared_ptr<SubscriberImpl> subscriber, bool &data) 173 { 174 std::shared_ptr<AsyncCommonEventResult> result = GetAsyncResult(subscriber.get()); 175 if (result) { 176 data = result->IsOrderedCommonEvent(); 177 } else { 178 data = subscriber->IsOrderedCommonEvent(); 179 } 180 } 181 }