• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 OHOS_DM_DATASAHRE_COMMON_EVENT_H
17 #define OHOS_DM_DATASAHRE_COMMON_EVENT_H
18 
19 #include "common_event_manager.h"
20 #include "system_ability_status_change_stub.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
24 using OHOS::EventFwk::CommonEventData;
25 using OHOS::EventFwk::CommonEventSubscriber;
26 using OHOS::EventFwk::CommonEventSubscribeInfo;
27 using DataShareEventCallback = std::function<void(std::string)>;
28 
29 class DmDataShareEventSubscriber : public CommonEventSubscriber {
30 public:
DmDataShareEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo,const DataShareEventCallback & callback,const std::vector<std::string> & eventNameVec)31     DmDataShareEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const DataShareEventCallback &callback,
32         const std::vector<std::string> &eventNameVec) : CommonEventSubscriber(subscribeInfo),
33         eventNameVec_(eventNameVec), callback_(callback) {}
34     ~DmDataShareEventSubscriber() override = default;
35     std::vector<std::string> GetSubscriberEventNameVec() const;
36     void OnReceiveEvent(const CommonEventData &data) override;
37 
38 private:
39     std::vector<std::string> eventNameVec_;
40     DataShareEventCallback callback_;
41 };
42 
43 class DmDataShareCommonEventManager {
44 public:
45     DmDataShareCommonEventManager() = default;
46     ~DmDataShareCommonEventManager();
47     bool SubscribeDataShareCommonEvent(const std::vector<std::string> &eventNameVec,
48         const DataShareEventCallback &callback);
49     bool UnsubscribeDataShareCommonEvent();
50 
51 private:
52     std::vector<std::string> eventNameVec_;
53     bool eventValidFlag_ = false;
54     std::mutex evenSubscriberMutex_;
55     std::shared_ptr<DmDataShareEventSubscriber> subscriber_ = nullptr;
56     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
57     int32_t counter_ = 0;
58 
59 private:
60     class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
61     public:
SystemAbilityStatusChangeListener(std::shared_ptr<DmDataShareEventSubscriber> DataShareSubscriber)62         explicit SystemAbilityStatusChangeListener(std::shared_ptr<DmDataShareEventSubscriber> DataShareSubscriber)
63             : changeSubscriber_(DataShareSubscriber) {}
64         ~SystemAbilityStatusChangeListener() = default;
65         void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
66         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
67 
68     private:
69         std::shared_ptr<DmDataShareEventSubscriber> changeSubscriber_;
70     };
71 };
72 } // namespace DistributedHardware
73 } // namespace OHOS
74 #endif // OHOS_DM_DATASAHRE_COMMON_EVENT_H
75