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