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