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