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 bool UpdateOhosAccountInfo(const std::string& accountName, const std::string& uid, 33 const std::string& eventStr) final; 34 std::int32_t SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, 35 const std::string &eventStr) final; 36 ErrCode SetOhosAccountInfoByUserId( 37 const int32_t userId, 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 GetOhosAccountInfo(OhosAccountInfo &accountInfo) final; 42 ErrCode GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &accountInfo) final; 43 std::pair<bool, OhosAccountInfo> QueryOhosAccountInfoByUserId(std::int32_t userId) final; 44 ErrCode QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo); 45 ErrCode QueryDeviceAccountId(std::int32_t& accountId) final; 46 std::int32_t GetDeviceAccountIdByUID(std::int32_t& uid) final; 47 ErrCode SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, 48 const std::shared_ptr<DistributedAccountSubscribeCallback> &callback) final; 49 ErrCode UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, 50 const std::shared_ptr<DistributedAccountSubscribeCallback> &callback) final; 51 void RestoreSubscribe(); 52 ErrCode SubscribeSystemAbility(const DomainAccountSubscribeSACallbackFunc& callbackFunc); 53 sptr<IRemoteObject> GetOsAccountService(); 54 sptr<IRemoteObject> GetDomainAccountService(); 55 sptr<IRemoteObject> GetAppAccountService(); 56 sptr<IRemoteObject> GetAccountIAMService(); 57 58 private: 59 OhosAccountKitsImpl() = default; 60 ~OhosAccountKitsImpl() = default; 61 // For death event procession 62 class DeathRecipient final : public IRemoteObject::DeathRecipient { 63 public: 64 DeathRecipient() = default; 65 ~DeathRecipient() final = default; 66 DISALLOW_COPY_AND_MOVE(DeathRecipient); 67 68 void OnRemoteDied(const wptr<IRemoteObject>& remote) final; 69 }; 70 71 void ResetService(const wptr<IRemoteObject>& remote); 72 sptr<IAccount> GetService(); 73 ErrCode CreateDistributedAccountEventService(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type, 74 const std::shared_ptr<DistributedAccountSubscribeCallback> &callback, 75 sptr<IRemoteObject> &DistributedAccountEventService); 76 77 bool isSubscribeSA_ = false; 78 std::mutex eventListenersMutex_; 79 std::map<std::shared_ptr<DistributedAccountSubscribeCallback>, 80 sptr<DistributedAccountEventService>> eventListeners_; 81 sptr<IRemoteObject::DeathRecipient> deathRecipient_{}; 82 std::mutex accountProxyLock_; 83 sptr<IAccount> accountProxy_{}; 84 }; 85 } // namespace AccountSA 86 } // namespace OHOS 87 88 #endif // BASE_ACCOUNT_OHOS_ACCOUNT_KITS_IMPL_H 89