• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 BASE_ACCOUNT_OHOS_ACCOUNT_KITS_IMPL_H
17 #define BASE_ACCOUNT_OHOS_ACCOUNT_KITS_IMPL_H
18 
19 #include <mutex>
20 #include "distributed_account_event_service.h"
21 #include "iaccount.h"
22 #include "ipc_skeleton.h"
23 #include "ohos_account_kits.h"
24 
25 namespace OHOS {
26 namespace AccountSA {
27 using DomainAccountSubscribeSACallbackFunc = std::function<void(int32_t, const std::string)>;
28 class OhosAccountKitsImpl final : public OhosAccountKits {
29 public:
30     DISALLOW_COPY_AND_MOVE(OhosAccountKitsImpl);
31     static OhosAccountKitsImpl &GetInstance();
32     ErrCode UpdateOhosAccountInfo(const std::string& accountName, const std::string& uid,
33         const std::string& eventStr) final;
34     ErrCode SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo,
35         const std::string &eventStr) final;
36     ErrCode SetOsAccountDistributedInfo(
37         const int32_t localId, const OhosAccountInfo& ohosAccountInfo, const std::string& eventStr) final;
38     std::pair<bool, OhosAccountInfo> QueryOhosAccountInfo() final;
39     ErrCode QueryOhosAccountInfo(OhosAccountInfo &accountInfo);
40     ErrCode QueryDistributedVirtualDeviceId(std::string &dvid);
41     ErrCode QueryDistributedVirtualDeviceId(const std::string &bundleName, int32_t localId, std::string &dvid);
42     ErrCode GetOhosAccountInfo(OhosAccountInfo &accountInfo) final;
43     ErrCode GetOsAccountDistributedInfo(int32_t localId, OhosAccountInfo &accountInfo) final;
44     std::pair<bool, OhosAccountInfo> QueryOsAccountDistributedInfo(std::int32_t localId) final;
45     ErrCode QueryOsAccountDistributedInfo(std::int32_t localId, OhosAccountInfo &accountInfo);
46     ErrCode QueryDeviceAccountId(std::int32_t& accountId) final;
47     std::int32_t GetDeviceAccountIdByUID(std::int32_t& uid) final;
48     ErrCode SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
49         const std::shared_ptr<DistributedAccountSubscribeCallback> &callback) final;
50     ErrCode UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
51         const std::shared_ptr<DistributedAccountSubscribeCallback> &callback) final;
52     void RestoreSubscribe();
53     ErrCode SubscribeSystemAbility(const DomainAccountSubscribeSACallbackFunc& callbackFunc);
54     sptr<IRemoteObject> GetOsAccountService();
55     sptr<IRemoteObject> GetDomainAccountService();
56     sptr<IRemoteObject> GetAppAccountService();
57     sptr<IRemoteObject> GetAccountIAMService();
58 
59 private:
60     OhosAccountKitsImpl() = default;
61     ~OhosAccountKitsImpl() = default;
62     // For death event procession
63     class DeathRecipient final : public IRemoteObject::DeathRecipient {
64     public:
65         DeathRecipient() = default;
66         ~DeathRecipient() final = default;
67         DISALLOW_COPY_AND_MOVE(DeathRecipient);
68 
69         void OnRemoteDied(const wptr<IRemoteObject>& remote) final;
70     };
71 
72     void ResetService(const wptr<IRemoteObject>& remote);
73     sptr<IAccount> GetService();
74     ErrCode CreateDistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
75         const std::shared_ptr<DistributedAccountSubscribeCallback> &callback,
76         sptr<IRemoteObject> &DistributedAccountEventService);
77 
78     bool isSubscribeSA_ = false;
79     std::mutex eventListenersMutex_;
80     std::map<std::shared_ptr<DistributedAccountSubscribeCallback>,
81         sptr<DistributedAccountEventService>> eventListeners_;
82     sptr<IRemoteObject::DeathRecipient> deathRecipient_{};
83     std::mutex accountProxyLock_;
84     sptr<IAccount> accountProxy_{};
85 };
86 } // namespace AccountSA
87 } // namespace OHOS
88 
89 #endif // BASE_ACCOUNT_OHOS_ACCOUNT_KITS_IMPL_H
90