• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ACCOUNT_COMMON_EVENT_H
17 #define OHOS_ACCOUNT_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 /**
28  * @brief account event callback define, fun(event_type, current userid, before userid)
29  * first param, accont event
30  * second param, the current userid
31  * third param, the userid before.
32  *          first param         |       second param        |       third param
33  * ------------------------------------------
34  * COMMON_EVENT_USER_SWITCHED   |   switch target user id   |   the user id before switch
35  * COMMON_EVENT_USER_REMOVED    |           -1              |   the user id removed
36  * COMMON_EVENT_HWID_LOGOUT     |  logout in witch user id  |   logout in witch user id
37  * COMMON_EVENT_HWID_LOGIN      |  login in witch user id   |   login in witch user id
38  */
39 using AccountEventCallback = std::function<void(std::string, int32_t, int32_t)>;
40 
41 class DmAccountEventSubscriber : public CommonEventSubscriber {
42 public:
DmAccountEventSubscriber(const CommonEventSubscribeInfo & subscribeInfo,const AccountEventCallback & callback,const std::vector<std::string> & eventNameVec)43     DmAccountEventSubscriber(const CommonEventSubscribeInfo &subscribeInfo, const AccountEventCallback &callback,
44         const std::vector<std::string> &eventNameVec) : CommonEventSubscriber(subscribeInfo),
45         eventNameVec_(eventNameVec), callback_(callback) {}
46     ~DmAccountEventSubscriber() override = default;
47     std::vector<std::string> GetSubscriberEventNameVec() const;
48     void OnReceiveEvent(const CommonEventData &data) override;
49 
50 private:
51     std::vector<std::string> eventNameVec_;
52     AccountEventCallback callback_;
53 };
54 
55 class DmAccountCommonEventManager {
56 public:
57     DmAccountCommonEventManager() = default;
58     ~DmAccountCommonEventManager();
59     bool SubscribeAccountCommonEvent(const std::vector<std::string> &eventNameVec,
60         const AccountEventCallback &callback);
61     bool UnsubscribeAccountCommonEvent();
62 
63 private:
64     std::vector<std::string> eventNameVec_;
65     bool eventValidFlag_ = false;
66     std::mutex evenSubscriberMutex_;
67     std::shared_ptr<DmAccountEventSubscriber> subscriber_ = nullptr;
68     sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr;
69 
70 private:
71     class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
72     public:
SystemAbilityStatusChangeListener(std::shared_ptr<DmAccountEventSubscriber> AccountSubscriber)73         explicit SystemAbilityStatusChangeListener(std::shared_ptr<DmAccountEventSubscriber> AccountSubscriber)
74             : changeSubscriber_(AccountSubscriber) {}
75         ~SystemAbilityStatusChangeListener() = default;
76         void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
77         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
78 
79     private:
80         std::shared_ptr<DmAccountEventSubscriber> changeSubscriber_;
81     };
82 };
83 } // namespace DistributedHardware
84 } // namespace OHOS
85 #endif // OHOS_ACCOUNT_COMMON_EVENT_H
86