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 WATCHER_MANAGER_KITS_H
17#define WATCHER_MANAGER_KITS_H
18#include <functional>
19#include <map>
20#include <mutex>
21#include <thread>
22
23#include "iwatcher.h"
24#include "iwatcher_manager.h"
25#include "singleton.h"
26#include "init_param.h"
27#include "watcher.h"
28#include "watcher_utils.h"
29#include "beget_ext.h"
30
31namespace OHOS {
32namespace init_param {
33class INIT_LOCAL_API WatcherManagerKits final : public DelayedRefSingleton<WatcherManagerKits> {
34    DECLARE_DELAYED_REF_SINGLETON(WatcherManagerKits);
35public:
36    DISALLOW_COPY_AND_MOVE(WatcherManagerKits);
37
38    static WatcherManagerKits &GetInstance(void);
39    int32_t AddWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context);
40    int32_t DelWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context);
41    void ReAddWatcher(void);
42private:
43    class ParameterChangeListener {
44    public:
45        ParameterChangeListener(ParameterChangePtr callback, void *context)
46            : callback_(callback), context_(context) {}
47        ~ParameterChangeListener(void) = default;
48
49        bool IsEqual(ParameterChangePtr callback, const void *context) const
50        {
51            return (callback == callback_ && context == context_);
52        }
53        void OnParameterChange(const std::string &name, const std::string &value);
54        bool CheckValueChange(const std::string &name, const std::string &value)
55        {
56            bool ret = (value_ == value && name_ == name);
57            value_ = value;
58            name_ = name;
59            return ret;
60        }
61    private:
62        std::string value_ {};
63        std::string name_ {};
64        ParameterChangePtr callback_ { nullptr };
65        void *context_ { nullptr };
66    };
67
68    class ParamWatcher {
69    public:
70        explicit ParamWatcher(const std::string &key) : keyPrefix_(key) {}
71        ~ParamWatcher(void)
72        {
73            parameterChangeListeners.clear();
74        };
75        void OnParameterChange(const std::string &name, const std::string &value);
76        int AddParameterListener(ParameterChangePtr callback, void *context);
77        int DelParameterListener(ParameterChangePtr callback, void *context);
78    private:
79        ParameterChangeListener *GetParameterListener(uint32_t *idx);
80        void RemoveParameterListener(uint32_t idx);
81        std::string keyPrefix_ {};
82        std::mutex mutex_;
83        uint32_t listenerId_ { 0 };
84        std::map<uint32_t, std::shared_ptr<ParameterChangeListener>> parameterChangeListeners;
85    };
86
87    class RemoteWatcher final : public Watcher {
88    public:
89        explicit RemoteWatcher(WatcherManagerKits *watcherManager) : watcherManager_(watcherManager) {}
90        ~RemoteWatcher(void) override {}
91
92        void OnParameterChange(const std::string &prefix, const std::string &name, const std::string &value) final;
93    private:
94        WatcherManagerKits *watcherManager_ = { nullptr };
95    };
96
97    using ParamWatcherKitPtr = std::shared_ptr<WatcherManagerKits::ParamWatcher>;
98    // For death event procession
99    class DeathRecipient final : public IRemoteObject::DeathRecipient {
100    public:
101        DeathRecipient(void) = default;
102        ~DeathRecipient(void) final = default;
103        DISALLOW_COPY_AND_MOVE(DeathRecipient);
104        void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
105    };
106    sptr<IRemoteObject::DeathRecipient> GetDeathRecipient(void)
107    {
108        return deathRecipient_;
109    }
110
111    uint32_t GetRemoteWatcher(void);
112    ParamWatcher *GetParamWatcher(const std::string &keyPrefix);
113    void ResetService(const wptr<IRemoteObject> &remote);
114    sptr<IWatcherManager> GetService(void);
115    std::mutex lock_;
116    sptr<IWatcherManager> watcherManager_ {};
117    sptr<IRemoteObject::DeathRecipient> deathRecipient_ {};
118
119    std::mutex mutex_;
120    uint32_t remoteWatcherId_ = { 0 };
121    sptr<Watcher>  remoteWatcher_ = { nullptr };
122    std::map<std::string, ParamWatcherKitPtr> watchers_;
123    std::atomic<bool> stop_ { false };
124    std::thread *threadForReWatch_ { nullptr };
125};
126} // namespace init_param
127} // namespace OHOS
128#endif // WATCHER_MANAGER_KITS_H
129