• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DistributedData {
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     DEVICE_ACCOUNT_STOPPING,   // the device account is stopping
36     DEVICE_ACCOUNT_STOPPED,    // the device account is stopped
37 };
38 
39 struct AccountEventInfo {
40     std::string harmonyAccountId;
41     std::string userId;
42     AccountStatus status;
43 };
44 
45 class AccountDelegate {
46 public:
47     class Observer {
48     public:
49         enum class LevelType {
50             HIGH,
51             LOW,
52         };
53         API_EXPORT virtual ~Observer() = default;
54         API_EXPORT virtual void OnAccountChanged(const AccountEventInfo &eventInfo, int32_t timeout = 0) = 0;
55 
56         // must specify unique name for observer
57         API_EXPORT virtual std::string Name() = 0;
58         API_EXPORT virtual LevelType GetLevel() = 0;
59     };
60     using HashFunc = std::string (*)(const void *data, size_t size, bool isUpper);
61     API_EXPORT virtual ~AccountDelegate() = default;
62     API_EXPORT virtual int32_t Subscribe(std::shared_ptr<Observer> observer) = 0;
63     API_EXPORT virtual int32_t Unsubscribe(std::shared_ptr<Observer> observer) = 0;
64     API_EXPORT virtual std::string GetCurrentAccountId() const = 0;
65     API_EXPORT virtual int32_t GetUserByToken(uint32_t tokenId) const = 0;
66     API_EXPORT virtual void SubscribeAccountEvent() = 0;
67     API_EXPORT virtual void UnsubscribeAccountEvent() = 0;
68     API_EXPORT virtual bool QueryUsers(std::vector<int> &users) = 0;
69     API_EXPORT virtual bool QueryForegroundUsers(std::vector<int> &users) = 0;
70     API_EXPORT virtual bool QueryForegroundUserId(int &foregroundUserId) = 0;
71     API_EXPORT virtual bool IsLoginAccount() = 0;
72     API_EXPORT virtual bool IsVerified(int userId) = 0;
73     API_EXPORT virtual bool RegisterHashFunc(HashFunc hash) = 0;
74     API_EXPORT virtual bool IsDeactivating(int userId) = 0;
75     API_EXPORT virtual void BindExecutor(std::shared_ptr<ExecutorPool> executors) = 0;
76     API_EXPORT virtual std::string GetUnencryptedAccountId(int32_t userId = 0) const = 0;
77     API_EXPORT static AccountDelegate *GetInstance();
78     API_EXPORT static bool RegisterAccountInstance(AccountDelegate *instance);
79     API_EXPORT virtual bool IsUserForeground(int32_t userId) = 0;
80 
81 private:
82     static AccountDelegate *instance_;
83 };
84 } // namespace DistributedKv
85 } // namespace OHOS
86 #endif // DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_H