• 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 <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 };
34 
35 struct AccountEventInfo {
36     std::string harmonyAccountId;
37     std::string userId;
38     AccountStatus status;
39 };
40 
41 class AccountDelegate {
42 public:
43     class Observer {
44     public:
45         enum class LevelType {
46             HIGH,
47             LOW,
48         };
49         API_EXPORT virtual ~Observer() = default;
50         API_EXPORT virtual void OnAccountChanged(const AccountEventInfo &eventInfo) = 0;
51 
52         // must specify unique name for observer
53         API_EXPORT virtual std::string Name() = 0;
54         API_EXPORT virtual LevelType GetLevel() = 0;
55     };
56     using HashFunc = std::string(*)(const void *data, size_t size, bool isUpper);
57     API_EXPORT virtual ~AccountDelegate() = default;
58     API_EXPORT virtual Status Subscribe(std::shared_ptr<Observer> observer) = 0;
59     API_EXPORT virtual Status Unsubscribe(std::shared_ptr<Observer> observer) = 0;
60     API_EXPORT virtual std::string GetCurrentAccountId() 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 virtual void BindExecutor(std::shared_ptr<ExecutorPool> executors) = 0;
67     API_EXPORT static AccountDelegate *GetInstance();
68 
69 private:
70     using BaseInstance = AccountDelegate *(*)();
71     static BaseInstance getInstance_;
72 };
73 }  // namespace DistributedKv
74 }  // namespace OHOS
75 #endif // DISTRIBUTEDDATAMGR_ACCOUNT_DELEGATE_H
76