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