• 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 
24 #include "dataobs_mgr_inner.h"
25 #include "dataobs_mgr_stub.h"
26 #include "hilog_wrapper.h"
27 #include "iremote_object.h"
28 #include "system_ability.h"
29 #include "uri.h"
30 #include "event_runner.h"
31 #include "event_handler.h"
32 
33 namespace OHOS {
34 namespace AAFwk {
35 using EventRunner = OHOS::AppExecFwk::EventRunner;
36 using EventHandler = OHOS::AppExecFwk::EventHandler;
37 enum class DataObsServiceRunningState { STATE_NOT_START, STATE_RUNNING };
38 
39 /**
40  * @class DataObsMgrService
41  * DataObsMgrService provides a facility for dataobserver.
42  */
43 class DataObsMgrService : public SystemAbility,
44                           public DataObsManagerStub,
45                           public std::enable_shared_from_this<DataObsMgrService> {
46     DECLARE_DELAYED_SINGLETON(DataObsMgrService)
47     DECLEAR_SYSTEM_ABILITY(DataObsMgrService)
48 public:
49     void OnStart() override;
50     void OnStop() override;
51     DataObsServiceRunningState QueryServiceState() const;
52 
53     virtual int RegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver) override;
54     virtual int UnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver) override;
55     virtual int NotifyChange(const Uri &uri) override;
56 
57     /**
58      * @brief DataObs hidumper.
59      * @param fd Indicates the fd.
60      * @param args Indicates the params.
61      * @return Returns the dump result.
62      */
63     int Dump(int fd, const std::vector<std::u16string>& args) override;
64 
65     /**
66      * GetEventHandler, get the dataobs manager service's handler.
67      *
68      * @return Returns EventHandler ptr.
69      */
70     std::shared_ptr<EventHandler> GetEventHandler();
71 
72 private:
73     bool Init();
74     void Dump(const std::vector<std::u16string>& args, std::string& result) const;
75     void ShowHelp(std::string& result) const;
76 
77 private:
78     std::shared_ptr<EventRunner> eventLoop_;
79     std::shared_ptr<EventHandler> handler_;
80     DataObsServiceRunningState state_;
81     std::shared_ptr<DataObsMgrInner> dataObsMgrInner_;
82     const int taskMax_ = 50;
83 };
84 }  // namespace AAFwk
85 }  // namespace OHOS
86 #endif  // OHOS_ABILITY_RUNTIME_DATAOBS_MGR_SERVICE_H
87