• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "metadata/user_meta_data.h"
24 #include "visibility.h"
25 
26 namespace OHOS::DistributedData {
27 using AccountDelegate = DistributedKv::AccountDelegate;
28 using DistributedData::UserStatus;
29 class UserDelegate {
30 public:
31     struct UserEvent {
32         int id;
33         bool isActive;
34     };
35     API_EXPORT static UserDelegate &GetInstance();
36 
37     API_EXPORT void Init();
38     API_EXPORT std::vector<UserStatus> GetLocalUserStatus();
39     API_EXPORT std::set<std::string> GetLocalUsers();
40     API_EXPORT std::vector<UserStatus> GetRemoteUserStatus(const std::string &deviceId);
41     API_EXPORT bool InitLocalUserMeta();
42 
43     API_EXPORT static constexpr const int SYSTEM_USER = 0;
44 private:
45     class LocalUserObserver : public AccountDelegate::Observer {
46     public:
47         explicit LocalUserObserver(UserDelegate &userDelegate);
48         void OnAccountChanged(const DistributedKv::AccountEventInfo &eventInfo) override;
49         std::string Name() override;
GetLevel()50         LevelType GetLevel() override
51         {
52             return LevelType::HIGH;
53         }
54 
55     private:
56         UserDelegate &userDelegate_;
57     };
58     std::vector<UserStatus> GetUsers(const std::string &deviceId);
59     void LoadFromMeta(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 
64     // device : { user : isActive }
65     ConcurrentMap<std::string, std::map<int, bool>> deviceUserMap_;
66 };
67 } // namespace OHOS::DistributedData
68 
69 #endif // DISTRIBUTEDDATAMGR_USER_DELEGATE_H
70