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