• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
17 #define OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
18 
19 #include <memory>
20 #include <singleton.h>
21 #include <thread_ex.h>
22 #include <unordered_map>
23 #include "cpp/mutex.h"
24 
25 #include "dataobs_mgr_inner.h"
26 #include "dataobs_mgr_inner_ext.h"
27 #include "dataobs_mgr_inner_pref.h"
28 #include "dataobs_mgr_stub.h"
29 #include "iremote_object.h"
30 #include "system_ability.h"
31 #include "task_handler_wrap.h"
32 #include "uri.h"
33 
34 namespace OHOS {
35 namespace AAFwk {
36 enum class DataObsServiceRunningState { STATE_NOT_START, STATE_RUNNING };
37 constexpr char SHARE_PREFERENCES[] = "sharepreferences";
38 /**
39  * @class DataObsMgrService
40  * DataObsMgrService provides a facility for dataobserver.
41  */
42 class DataObsMgrService : public SystemAbility,
43                           public DataObsManagerStub,
44                           public std::enable_shared_from_this<DataObsMgrService> {
45     DECLARE_DELAYED_SINGLETON(DataObsMgrService)
46     DECLEAR_SYSTEM_ABILITY(DataObsMgrService)
47 public:
48     void OnStart() override;
49     void OnStop() override;
50     DataObsServiceRunningState QueryServiceState() const;
51 
52     virtual int RegisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver) override;
53     virtual int UnregisterObserver(const Uri &uri, sptr<IDataAbilityObserver> dataObserver) override;
54     virtual int NotifyChange(const Uri &uri) override;
55     virtual Status RegisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver,
56         bool isDescendants) override;
57     virtual Status UnregisterObserverExt(const Uri &uri, sptr<IDataAbilityObserver> dataObserver) override;
58     virtual Status UnregisterObserverExt(sptr<IDataAbilityObserver> dataObserver) override;
59     virtual Status NotifyChangeExt(const ChangeInfo &changeInfo) override;
60     virtual Status NotifyProcessObserver(const std::string &key, const sptr<IRemoteObject> &observer) override;
61 
62     /**
63      * @brief DataObs hidumper.
64      * @param fd Indicates the fd.
65      * @param args Indicates the params.
66      * @return Returns the dump result.
67      */
68     int Dump(int fd, const std::vector<std::u16string>& args) override;
69 
70 private:
71     bool Init();
72     void Dump(const std::vector<std::u16string>& args, std::string& result) const;
73     void ShowHelp(std::string& result) const;
74     Status DeepCopyChangeInfo(const ChangeInfo &src, ChangeInfo &dst) const;
75     void GetFocusedAppInfo(int32_t &windowId, sptr<IRemoteObject> &abilityToken) const;
76     sptr<IRemoteObject> GetAbilityManagerService() const;
77 private:
78     static constexpr std::uint32_t TASK_COUNT_MAX = 50;
79     ffrt::mutex taskCountMutex_;
80     std::uint32_t taskCount_ = 0;
81     std::shared_ptr<TaskHandlerWrap> handler_;
82 
83     DataObsServiceRunningState state_;
84 
85     std::shared_ptr<DataObsMgrInner> dataObsMgrInner_;
86     std::shared_ptr<DataObsMgrInnerExt> dataObsMgrInnerExt_;
87     std::shared_ptr<DataObsMgrInnerPref> dataObsMgrInnerPref_;
88 };
89 }  // namespace AAFwk
90 }  // namespace OHOS
91 #endif  // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
92