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 #ifndef OH_COMMON_EVENT_WRAPPER_C_H 17 #define OH_COMMON_EVENT_WRAPPER_C_H 18 19 #include <map> 20 #include <string> 21 #include <vector> 22 23 #include "common_event_subscriber.h" 24 #include "ffrt.h" 25 #include "oh_commonevent.h" 26 #include "want_params.h" 27 28 struct CArrParameters { 29 OHOS::AAFwk::WantParams wantParams; 30 mutable std::vector<void*> allocatedPointers; 31 }; 32 33 struct CommonEvent_SubscribeInfo { 34 std::vector<std::string> events; 35 std::string permission; 36 std::string bundleName; 37 }; 38 39 struct CommonEvent_PublishInfo { 40 bool ordered = false; 41 std::string bundleName; 42 std::vector<std::string> subscriberPermissions; 43 int32_t code = 0; 44 std::string data; 45 CArrParameters* parameters = nullptr; 46 }; 47 48 struct CommonEvent_RcvData { 49 std::string event; 50 std::string bundleName; 51 int32_t code; 52 std::string data; 53 CArrParameters* parameters = nullptr; 54 }; 55 56 struct ResultCacheItem { 57 std::shared_ptr<OHOS::EventFwk::AsyncCommonEventResult> result = nullptr; 58 std::string data; 59 }; 60 61 class SubscriberObserver : public OHOS::EventFwk::CommonEventSubscriber { 62 public: 63 SubscriberObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo); 64 ~SubscriberObserver(); 65 66 void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override; 67 void SetCallback(CommonEvent_ReceiveCallback callback); 68 private: 69 CommonEvent_ReceiveCallback callback_ = nullptr; 70 }; 71 72 class SubscriberManager { 73 public: 74 SubscriberManager() = default; 75 ~SubscriberManager() = default; 76 CommonEvent_Subscriber* CreateSubscriber(const CommonEvent_SubscribeInfo* subscribeInfo, 77 CommonEvent_ReceiveCallback callback); 78 void DestroySubscriber(CommonEvent_Subscriber* subscriber); 79 CommonEvent_ErrCode Subscribe(const CommonEvent_Subscriber* subscriber); 80 CommonEvent_ErrCode UnSubscribe(const CommonEvent_Subscriber* subscriber); 81 void SetAsyncResult(SubscriberObserver* subscriber); 82 ResultCacheItem* GetAsyncResult(const SubscriberObserver* subscriber); 83 static std::shared_ptr<SubscriberManager> GetInstance(); 84 private: 85 static ffrt::mutex instanceMutex_; 86 static ffrt::mutex resultCacheMutex_; 87 static std::shared_ptr<SubscriberManager> instance_; 88 static std::map<std::shared_ptr<SubscriberObserver>, ResultCacheItem> resultCache_; 89 }; 90 91 #endif