1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <memory>
20eace7efcSopenharmony_ci#include <singleton.h>
21eace7efcSopenharmony_ci#include <thread_ex.h>
22eace7efcSopenharmony_ci#include <unordered_map>
23eace7efcSopenharmony_ci#include "cpp/mutex.h"
24eace7efcSopenharmony_ci
25eace7efcSopenharmony_ci#include "dataobs_mgr_inner.h"
26eace7efcSopenharmony_ci#include "dataobs_mgr_inner_ext.h"
27eace7efcSopenharmony_ci#include "dataobs_mgr_inner_pref.h"
28eace7efcSopenharmony_ci#include "dataobs_mgr_stub.h"
29eace7efcSopenharmony_ci#include "iremote_object.h"
30eace7efcSopenharmony_ci#include "system_ability.h"
31eace7efcSopenharmony_ci#include "task_handler_wrap.h"
32eace7efcSopenharmony_ci#include "uri.h"
33eace7efcSopenharmony_ci
34eace7efcSopenharmony_cinamespace OHOS {
35eace7efcSopenharmony_cinamespace AAFwk {
36eace7efcSopenharmony_cienum class DataObsServiceRunningState { STATE_NOT_START, STATE_RUNNING };
37eace7efcSopenharmony_ciconstexpr char SHARE_PREFERENCES[] = "sharepreferences";
38eace7efcSopenharmony_ci/**
39eace7efcSopenharmony_ci * @class DataObsMgrService
40eace7efcSopenharmony_ci * DataObsMgrService provides a facility for dataobserver.
41eace7efcSopenharmony_ci */
42eace7efcSopenharmony_ciclass DataObsMgrService : public SystemAbility,
43eace7efcSopenharmony_ci                          public DataObsManagerStub,
44eace7efcSopenharmony_ci                          public std::enable_shared_from_this<DataObsMgrService> {
45eace7efcSopenharmony_ci    DECLARE_DELAYED_SINGLETON(DataObsMgrService)
46eace7efcSopenharmony_ci    DECLEAR_SYSTEM_ABILITY(DataObsMgrService)
47eace7efcSopenharmony_cipublic:
48eace7efcSopenharmony_ci    void OnStart() override;
49eace7efcSopenharmony_ci    void OnStop() override;
50eace7efcSopenharmony_ci    DataObsServiceRunningState QueryServiceState() const;
51eace7efcSopenharmony_ci
52eace7efcSopenharmony_ci    virtual int RegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver) override;
53eace7efcSopenharmony_ci    virtual int UnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver) override;
54eace7efcSopenharmony_ci    virtual int NotifyChange(const Uri &uri) override;
55eace7efcSopenharmony_ci    virtual Status RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
56eace7efcSopenharmony_ci        bool isDescendants) override;
57eace7efcSopenharmony_ci    virtual Status UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver) override;
58eace7efcSopenharmony_ci    virtual Status UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver) override;
59eace7efcSopenharmony_ci    virtual Status NotifyChangeExt(const ChangeInfo &changeInfo) override;
60eace7efcSopenharmony_ci
61eace7efcSopenharmony_ci    /**
62eace7efcSopenharmony_ci     * @brief DataObs hidumper.
63eace7efcSopenharmony_ci     * @param fd Indicates the fd.
64eace7efcSopenharmony_ci     * @param args Indicates the params.
65eace7efcSopenharmony_ci     * @return Returns the dump result.
66eace7efcSopenharmony_ci     */
67eace7efcSopenharmony_ci    int Dump(int fd, const std::vector<std::u16string>& args) override;
68eace7efcSopenharmony_ci
69eace7efcSopenharmony_ciprivate:
70eace7efcSopenharmony_ci    bool Init();
71eace7efcSopenharmony_ci    void Dump(const std::vector<std::u16string>& args, std::string& result) const;
72eace7efcSopenharmony_ci    void ShowHelp(std::string& result) const;
73eace7efcSopenharmony_ci    Status DeepCopyChangeInfo(const ChangeInfo &src, ChangeInfo &dst) const;
74eace7efcSopenharmony_ciprivate:
75eace7efcSopenharmony_ci    static constexpr std::uint32_t TASK_COUNT_MAX = 50;
76eace7efcSopenharmony_ci    ffrt::mutex taskCountMutex_;
77eace7efcSopenharmony_ci    std::uint32_t taskCount_ = 0;
78eace7efcSopenharmony_ci    std::shared_ptr<TaskHandlerWrap> handler_;
79eace7efcSopenharmony_ci
80eace7efcSopenharmony_ci    DataObsServiceRunningState state_;
81eace7efcSopenharmony_ci
82eace7efcSopenharmony_ci    std::shared_ptr<DataObsMgrInner> dataObsMgrInner_;
83eace7efcSopenharmony_ci    std::shared_ptr<DataObsMgrInnerExt> dataObsMgrInnerExt_;
84eace7efcSopenharmony_ci    std::shared_ptr<DataObsMgrInnerPref> dataObsMgrInnerPref_;
85eace7efcSopenharmony_ci};
86eace7efcSopenharmony_ci}  // namespace AAFwk
87eace7efcSopenharmony_ci}  // namespace OHOS
88eace7efcSopenharmony_ci#endif  // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
89