12dc7c57fSopenharmony_ci/*
22dc7c57fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
32dc7c57fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42dc7c57fSopenharmony_ci * you may not use this file except in compliance with the License.
52dc7c57fSopenharmony_ci * You may obtain a copy of the License at
62dc7c57fSopenharmony_ci *
72dc7c57fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82dc7c57fSopenharmony_ci *
92dc7c57fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102dc7c57fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112dc7c57fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122dc7c57fSopenharmony_ci * See the License for the specific language governing permissions and
132dc7c57fSopenharmony_ci * limitations under the License.
142dc7c57fSopenharmony_ci */
152dc7c57fSopenharmony_ci
162dc7c57fSopenharmony_ci#ifndef SUBSCRIBER_H
172dc7c57fSopenharmony_ci#define SUBSCRIBER_H
182dc7c57fSopenharmony_ci
192dc7c57fSopenharmony_ci#include "ffi_remote_data.h"
202dc7c57fSopenharmony_ci#include "common_event_defination.h"
212dc7c57fSopenharmony_ci
222dc7c57fSopenharmony_ci#include "common_event_subscribe_info.h"
232dc7c57fSopenharmony_ci#include "common_event_subscriber.h"
242dc7c57fSopenharmony_ci
252dc7c57fSopenharmony_cinamespace OHOS::CommonEventManager {
262dc7c57fSopenharmony_ci    using CommonEventSubscribeInfo = OHOS::EventFwk::CommonEventSubscribeInfo;
272dc7c57fSopenharmony_ci    using CommonEventSubscriber = OHOS::EventFwk::CommonEventSubscriber;
282dc7c57fSopenharmony_ci    using CommonEventData = OHOS::EventFwk::CommonEventData;
292dc7c57fSopenharmony_ci
302dc7c57fSopenharmony_ci    class SubscriberImpl : public CommonEventSubscriber {
312dc7c57fSopenharmony_ci    public:
322dc7c57fSopenharmony_ci        SubscriberImpl(std::shared_ptr<CommonEventSubscribeInfo> sp, int64_t infoId);
332dc7c57fSopenharmony_ci        ~SubscriberImpl() override;
342dc7c57fSopenharmony_ci
352dc7c57fSopenharmony_ci        void OnReceiveEvent(const CommonEventData &data) override;
362dc7c57fSopenharmony_ci
372dc7c57fSopenharmony_ci        unsigned long long GetID();
382dc7c57fSopenharmony_ci
392dc7c57fSopenharmony_ci        int64_t GetSubscribeInfoId();
402dc7c57fSopenharmony_ci
412dc7c57fSopenharmony_ci        void SetSubscriberManagerId(int64_t id);
422dc7c57fSopenharmony_ci
432dc7c57fSopenharmony_ci        int64_t GetSubscriberManagerId();
442dc7c57fSopenharmony_ci
452dc7c57fSopenharmony_ci        void SetCallback(const std::function<void(CCommonEventData)> &callback);
462dc7c57fSopenharmony_ci
472dc7c57fSopenharmony_ci    private:
482dc7c57fSopenharmony_ci        std::function<void(CCommonEventData)> callback_;
492dc7c57fSopenharmony_ci        std::shared_ptr<bool> valid_;
502dc7c57fSopenharmony_ci        std::atomic_ullong id_;
512dc7c57fSopenharmony_ci        static std::atomic_ullong subscriberID_;
522dc7c57fSopenharmony_ci        int64_t infoId_;
532dc7c57fSopenharmony_ci        int64_t managerId_ = -1;
542dc7c57fSopenharmony_ci    };
552dc7c57fSopenharmony_ci
562dc7c57fSopenharmony_ci    class SubscriberManager : public OHOS::FFI::FFIData {
572dc7c57fSopenharmony_ci    public:
582dc7c57fSopenharmony_ci        OHOS::FFI::RuntimeType *GetRuntimeType() override
592dc7c57fSopenharmony_ci        {
602dc7c57fSopenharmony_ci            return GetClassType();
612dc7c57fSopenharmony_ci        }
622dc7c57fSopenharmony_ci        SubscriberManager(std::shared_ptr<CommonEventSubscribeInfo> info, int64_t infoId);
632dc7c57fSopenharmony_ci        ~SubscriberManager() override;
642dc7c57fSopenharmony_ci
652dc7c57fSopenharmony_ci        std::shared_ptr<SubscriberImpl> GetSubscriber();
662dc7c57fSopenharmony_ci
672dc7c57fSopenharmony_ci        int64_t GetSubscribeInfoId();
682dc7c57fSopenharmony_ci
692dc7c57fSopenharmony_ci    private:
702dc7c57fSopenharmony_ci        friend class OHOS::FFI::RuntimeType;
712dc7c57fSopenharmony_ci        friend class OHOS::FFI::TypeBase;
722dc7c57fSopenharmony_ci        static OHOS::FFI::RuntimeType *GetClassType()
732dc7c57fSopenharmony_ci        {
742dc7c57fSopenharmony_ci            static OHOS::FFI::RuntimeType runtimeType =
752dc7c57fSopenharmony_ci                OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("SubscriberManager");
762dc7c57fSopenharmony_ci            return &runtimeType;
772dc7c57fSopenharmony_ci        }
782dc7c57fSopenharmony_ci        std::shared_ptr<SubscriberImpl> subscriber;
792dc7c57fSopenharmony_ci    };
802dc7c57fSopenharmony_ci
812dc7c57fSopenharmony_ci} // namespace OHOS::CommonEventManager
822dc7c57fSopenharmony_ci
832dc7c57fSopenharmony_ci#endif // SUBSCRIBER_H
84