1 /* 2 * Copyright (c) 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 #ifndef DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_NORMAL_IMPL_H 16 #define DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_NORMAL_IMPL_H 17 18 #include "account_delegate_impl.h" 19 #include "executor_pool.h" 20 #include "log_print.h" 21 #include "os_account_subscriber.h" 22 23 namespace OHOS { 24 namespace DistributedData { 25 using namespace AccountSA; 26 using EventCallback = std::function<void(AccountEventInfo &account, int32_t timeout)>; 27 class AccountSubscriber final : public OsAccountSubscriber { 28 public: ~AccountSubscriber()29 ~AccountSubscriber() {} 30 explicit AccountSubscriber(const OsAccountSubscribeInfo &subscribeInfo, std::shared_ptr<ExecutorPool> executors); 31 void SetEventCallback(EventCallback callback); 32 void OnStateChanged(const OsAccountStateData &data) override; 33 private: 34 EventCallback eventCallback_{}; 35 std::shared_ptr<ExecutorPool> executors_ = nullptr; 36 }; 37 class AccountDelegateNormalImpl final : public AccountDelegateImpl { 38 public: 39 AccountDelegateNormalImpl(); 40 std::string GetCurrentAccountId() const override; 41 int32_t GetUserByToken(uint32_t tokenId) const override; 42 bool QueryUsers(std::vector<int> &users) override; 43 bool QueryForegroundUsers(std::vector<int> &users) override; 44 bool IsLoginAccount() override; 45 bool IsVerified(int userId) override; 46 bool IsDeactivating(int userId) override; 47 void SubscribeAccountEvent() override; 48 void UnsubscribeAccountEvent() override; 49 void BindExecutor(std::shared_ptr<ExecutorPool> executors) override; 50 std::string GetUnencryptedAccountId(int32_t userId = 0) const override; 51 bool QueryForegroundUserId(int &foregroundUserId) override; 52 bool IsUserForeground(int32_t userId) override; 53 static bool Init(); 54 55 private: 56 ~AccountDelegateNormalImpl(); 57 std::string Sha256AccountId(const std::string &plainText) const; 58 ExecutorPool::Task GetTask(uint32_t retry); 59 void UpdateUserStatus(const AccountEventInfo &account); 60 static constexpr int MAX_RETRY_TIME = 300; 61 static constexpr int RETRY_WAIT_TIME_S = 1; 62 std::shared_ptr<AccountSubscriber> accountSubscriber_{}; 63 std::shared_ptr<ExecutorPool> executors_; 64 ConcurrentMap<int32_t, bool> userStatus_{}; 65 ConcurrentMap<int32_t, bool> userDeactivating_{}; 66 }; 67 } // namespace DistributedData 68 } // namespace OHOS 69 #endif // DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_NORMAL_IMPL_H