153c3577eSopenharmony_ci/*
253c3577eSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
353c3577eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
453c3577eSopenharmony_ci * you may not use this file except in compliance with the License.
553c3577eSopenharmony_ci * You may obtain a copy of the License at
653c3577eSopenharmony_ci *
753c3577eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
853c3577eSopenharmony_ci *
953c3577eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1053c3577eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1153c3577eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1253c3577eSopenharmony_ci * See the License for the specific language governing permissions and
1353c3577eSopenharmony_ci * limitations under the License.
1453c3577eSopenharmony_ci */
1553c3577eSopenharmony_ci
1653c3577eSopenharmony_ci#ifndef DISTRIBUTEDDATAMGR_USER_DELEGATE_H
1753c3577eSopenharmony_ci#define DISTRIBUTEDDATAMGR_USER_DELEGATE_H
1853c3577eSopenharmony_ci
1953c3577eSopenharmony_ci#include <memory>
2053c3577eSopenharmony_ci#include <set>
2153c3577eSopenharmony_ci
2253c3577eSopenharmony_ci#include "account/account_delegate.h"
2353c3577eSopenharmony_ci#include "concurrent_map.h"
2453c3577eSopenharmony_ci#include "executor_pool.h"
2553c3577eSopenharmony_ci#include "metadata/user_meta_data.h"
2653c3577eSopenharmony_ci#include "visibility.h"
2753c3577eSopenharmony_ci
2853c3577eSopenharmony_cinamespace OHOS::DistributedData {
2953c3577eSopenharmony_ciusing AccountDelegate = DistributedKv::AccountDelegate;
3053c3577eSopenharmony_ciusing DistributedData::UserStatus;
3153c3577eSopenharmony_ciclass UserDelegate {
3253c3577eSopenharmony_cipublic:
3353c3577eSopenharmony_ci    struct UserEvent {
3453c3577eSopenharmony_ci        int id;
3553c3577eSopenharmony_ci        bool isActive;
3653c3577eSopenharmony_ci    };
3753c3577eSopenharmony_ci    API_EXPORT static UserDelegate &GetInstance();
3853c3577eSopenharmony_ci
3953c3577eSopenharmony_ci    API_EXPORT void Init(const std::shared_ptr<ExecutorPool> &executors);
4053c3577eSopenharmony_ci    API_EXPORT std::vector<UserStatus> GetLocalUserStatus();
4153c3577eSopenharmony_ci    API_EXPORT std::set<std::string> GetLocalUsers();
4253c3577eSopenharmony_ci    API_EXPORT std::vector<UserStatus> GetRemoteUserStatus(const std::string &deviceId);
4353c3577eSopenharmony_ci    API_EXPORT bool InitLocalUserMeta();
4453c3577eSopenharmony_ci
4553c3577eSopenharmony_ci    API_EXPORT static constexpr const int SYSTEM_USER = 0;
4653c3577eSopenharmony_ci
4753c3577eSopenharmony_ciprivate:
4853c3577eSopenharmony_ci    class LocalUserObserver : public AccountDelegate::Observer {
4953c3577eSopenharmony_ci    public:
5053c3577eSopenharmony_ci        explicit LocalUserObserver(UserDelegate &userDelegate);
5153c3577eSopenharmony_ci        void OnAccountChanged(const DistributedKv::AccountEventInfo &eventInfo) override;
5253c3577eSopenharmony_ci        std::string Name() override;
5353c3577eSopenharmony_ci        LevelType GetLevel() override
5453c3577eSopenharmony_ci        {
5553c3577eSopenharmony_ci            return LevelType::HIGH;
5653c3577eSopenharmony_ci        }
5753c3577eSopenharmony_ci
5853c3577eSopenharmony_ci    private:
5953c3577eSopenharmony_ci        UserDelegate &userDelegate_;
6053c3577eSopenharmony_ci    };
6153c3577eSopenharmony_ci    std::vector<UserStatus> GetUsers(const std::string &deviceId);
6253c3577eSopenharmony_ci    void UpdateUsers(const std::string &deviceId, const std::vector<UserStatus> &userStatus);
6353c3577eSopenharmony_ci    void DeleteUsers(const std::string &deviceId);
6453c3577eSopenharmony_ci    bool NotifyUserEvent(const UserEvent &userEvent);
6553c3577eSopenharmony_ci    ExecutorPool::Task GeTask();
6653c3577eSopenharmony_ci
6753c3577eSopenharmony_ci    static constexpr int RETRY_INTERVAL = 500; // millisecond
6853c3577eSopenharmony_ci    std::shared_ptr<ExecutorPool> executors_;
6953c3577eSopenharmony_ci    // device : { user : isActive }
7053c3577eSopenharmony_ci    ConcurrentMap<std::string, std::map<int, bool>> deviceUser_;
7153c3577eSopenharmony_ci};
7253c3577eSopenharmony_ci} // namespace OHOS::DistributedData
7353c3577eSopenharmony_ci
7453c3577eSopenharmony_ci#endif // DISTRIBUTEDDATAMGR_USER_DELEGATE_H
75