1 /*
2  * Copyright (c) 2021-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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APP_ACCOUNT_SUBSCRIBE_MANAGER_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APP_ACCOUNT_SUBSCRIBE_MANAGER_H
18 
19 #include <map>
20 #include <set>
21 
22 #include "ios_account_event.h"
23 #include "ios_account_subscribe.h"
24 #include "singleton.h"
25 
26 
27 namespace OHOS {
28 namespace AccountSA {
29 class OsAccountSubscribeManager : public IOsAccountSubscribe {
30 public:
31     static OsAccountSubscribeManager &GetInstance();
32     ErrCode SubscribeOsAccount(const std::shared_ptr<OsAccountSubscribeInfo> &subscribeInfoPtr,
33         const sptr<IRemoteObject> &eventListener) override;
34     ErrCode UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener) override;
35     const std::shared_ptr<OsAccountSubscribeInfo> GetSubscribeRecordInfo(
36         const sptr<IRemoteObject> &eventListener) override;
37     ErrCode Publish(const int id, OS_ACCOUNT_SUBSCRIBE_TYPE subscribeType) override;
38     ErrCode Publish(const int newId, const int oldId, OS_ACCOUNT_SUBSCRIBE_TYPE subscribeType) override;
39     bool OnAccountsChanged(const sptr<IOsAccountEvent> &eventProxy, const int id);
40     bool OnAccountsSwitch(const sptr<IOsAccountEvent> &eventProxy, const int newId, const int oldId);
41 
42 private:
43     OsAccountSubscribeManager();
44     ~OsAccountSubscribeManager() = default;
45     DISALLOW_COPY_AND_MOVE(OsAccountSubscribeManager);
46     ErrCode InsertSubscribeRecord(const OsSubscribeRecordPtr &subscribeRecordPtr);
47     ErrCode RemoveSubscribeRecord(const sptr<IRemoteObject> &eventListener);
48 
49 private:
50     std::mutex mutex_;
51     sptr<IRemoteObject::DeathRecipient> subscribeDeathRecipient_;
52     std::mutex subscribeRecordMutex_;
53     std::vector<OsSubscribeRecordPtr> subscribeRecords_;
54 };
55 }  // namespace AccountSA
56 }  // namespace OHOS
57 
58 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APP_ACCOUNT_SUBSCRIBE_MANAGER_H
59