1 /* 2 * Copyright (c) 2021 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 DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_H 17 #define DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_H 18 19 #include <mutex> 20 #include <memory> 21 #include "executor_pool.h" 22 #include "types.h" 23 #include "visibility.h" 24 25 namespace OHOS { 26 namespace DistributedKv { 27 enum class AccountStatus { 28 HARMONY_ACCOUNT_LOGIN = 0, // the openHarmony account is logged in 29 HARMONY_ACCOUNT_LOGOUT, // the openHarmony account is logged out 30 HARMONY_ACCOUNT_DELETE, // the openHarmony account is deleted 31 DEVICE_ACCOUNT_DELETE, // the device account is deleted 32 DEVICE_ACCOUNT_SWITCHED, // the device account is switched 33 DEVICE_ACCOUNT_UNLOCKED, // the device account is unlocked 34 }; 35 36 struct AccountEventInfo { 37 std::string harmonyAccountId; 38 std::string userId; 39 AccountStatus status; 40 }; 41 42 class AccountDelegate { 43 public: 44 class Observer { 45 public: 46 enum class LevelType { 47 HIGH, 48 LOW, 49 }; 50 API_EXPORT virtual ~Observer() = default; 51 API_EXPORT virtual void OnAccountChanged(const AccountEventInfo &eventInfo) = 0; 52 53 // must specify unique name for observer 54 API_EXPORT virtual std::string Name() = 0; 55 API_EXPORT virtual LevelType GetLevel() = 0; 56 }; 57 using HashFunc = std::string(*)(const void *data, size_t size, bool isUpper); 58 API_EXPORT virtual ~AccountDelegate() = default; 59 API_EXPORT virtual Status Subscribe(std::shared_ptr<Observer> observer) = 0; 60 API_EXPORT virtual Status Unsubscribe(std::shared_ptr<Observer> observer) = 0; 61 API_EXPORT virtual std::string GetCurrentAccountId() const = 0; 62 API_EXPORT virtual int32_t GetUserByToken(uint32_t tokenId) const = 0; 63 API_EXPORT virtual void SubscribeAccountEvent() = 0; 64 API_EXPORT virtual void UnsubscribeAccountEvent() = 0; 65 API_EXPORT virtual bool QueryUsers(std::vector<int> &users) = 0; 66 API_EXPORT virtual bool IsVerified(int userId) = 0; 67 API_EXPORT virtual bool RegisterHashFunc(HashFunc hash) = 0; 68 API_EXPORT virtual void BindExecutor(std::shared_ptr<ExecutorPool> executors) = 0; 69 API_EXPORT static AccountDelegate *GetInstance(); 70 71 private: 72 using BaseInstance = AccountDelegate *(*)(); 73 static BaseInstance getInstance_; 74 }; 75 } // namespace DistributedKv 76 } // namespace OHOS 77 #endif // DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_H 78