1 /*
2  * Copyright (c) 2022 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_USER_DELEGATE_H
17 #define DISTRIBUTEDDATAMGR_USER_DELEGATE_H
18 
19 #include <memory>
20 #include <set>
21 #include "account/account_delegate.h"
22 #include "concurrent_map.h"
23 #include "executor_pool.h"
24 #include "metadata/user_meta_data.h"
25 #include "visibility.h"
26 
27 namespace OHOS::DistributedData {
28 using AccountDelegate = DistributedKv::AccountDelegate;
29 using DistributedData::UserStatus;
30 class UserDelegate {
31 public:
32     struct UserEvent {
33         int id;
34         bool isActive;
35     };
36     API_EXPORT static UserDelegate &GetInstance();
37 
38     API_EXPORT void Init(const std::shared_ptr<ExecutorPool>& executors);
39     API_EXPORT std::vector<UserStatus> GetLocalUserStatus();
40     API_EXPORT std::set<std::string> GetLocalUsers();
41     API_EXPORT std::vector<UserStatus> GetRemoteUserStatus(const std::string &deviceId);
42     API_EXPORT bool InitLocalUserMeta();
43 
44     API_EXPORT static constexpr const int SYSTEM_USER = 0;
45 private:
46     class LocalUserObserver : public AccountDelegate::Observer {
47     public:
48         explicit LocalUserObserver(UserDelegate &userDelegate);
49         void OnAccountChanged(const DistributedKv::AccountEventInfo &eventInfo) override;
50         std::string Name() override;
GetLevel()51         LevelType GetLevel() override
52         {
53             return LevelType::HIGH;
54         }
55 
56     private:
57         UserDelegate &userDelegate_;
58     };
59     std::vector<UserStatus> GetUsers(const std::string &deviceId);
60     void UpdateUsers(const std::string &deviceId, const std::vector<UserStatus> &userStatus);
61     void DeleteUsers(const std::string &deviceId);
62     bool NotifyUserEvent(const UserEvent &userEvent);
63     ExecutorPool::Task GeTask();
64 
65     static constexpr int RETRY_INTERVAL = 500; // millisecond
66     std::shared_ptr<ExecutorPool> executors_;
67     // device : { user : isActive }
68     ConcurrentMap<std::string, std::map<int, bool>> deviceUser_;
69 };
70 } // namespace OHOS::DistributedData
71 
72 #endif // DISTRIBUTEDDATAMGR_USER_DELEGATE_H
73