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 "common_event_subscriber.h"
20 #include "oh_commonevent.h"
21 
22 struct CParameters {
23     int8_t valueType;
24     char *key;
25     void *value;
26     int64_t size;
27 };
28 
29 struct CArrParameters {
30     CParameters *head;
31     int64_t size;
32 };
33 
34 struct CommonEvent_SubscribeInfo {
35     uint32_t eventLength = 0;
36     char **events = nullptr;
37     char *permission = nullptr;
38     char *bundleName = nullptr;
39 };
40 
41 struct CommonEvent_RcvData {
42     char *event;
43     char *bundleName;
44     int32_t code;
45     char *data;
46     CArrParameters* parameters;
47 };
48 
49 class SubscriberObserver : public OHOS::EventFwk::CommonEventSubscriber {
50 public:
51     SubscriberObserver(const OHOS::EventFwk::CommonEventSubscribeInfo &subscribeInfo);
52     ~SubscriberObserver();
53 
54     void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) override;
55     void SetCallback(CommonEvent_ReceiveCallback callback);
56 private:
57     CommonEvent_ReceiveCallback callback_ = nullptr;
58 };
59 
60 class SubscriberManager {
61 public:
62     SubscriberManager() = default;
63     ~SubscriberManager() = default;
64     CommonEvent_Subscriber* CreateSubscriber(const CommonEvent_SubscribeInfo* subscribeInfo,
65         CommonEvent_ReceiveCallback callback);
66     void DestroySubscriber(CommonEvent_Subscriber* subscriber);
67     CommonEvent_ErrCode Subscribe(const CommonEvent_Subscriber* subscriber);
68     CommonEvent_ErrCode UnSubscribe(const CommonEvent_Subscriber* subscriber);
69     static std::shared_ptr<SubscriberManager> GetInstance();
70 private:
71     static std::mutex instanceMutex_;
72     static std::shared_ptr<SubscriberManager> instance_;
73 };
74 
75 #endif