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