• 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 FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H
17 #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H
18 
19 #include <singleton.h>
20 #include "common_event_stub.h"
21 #include "event_handler.h"
22 #include "inner_common_event_manager.h"
23 #include "nocopyable.h"
24 
25 namespace OHOS {
26 namespace EventFwk {
27 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
28 
29 class CommonEventManagerService : public CommonEventStub {
30 public:
31     CommonEventManagerService();
32     virtual ~CommonEventManagerService();
33     /**
34      * Publishes a common event.
35      *
36      * @param event Indicates the common event data.
37      * @param publishInfo Indicates the publish info.
38      * @param commonEventListener Indicates the common event subscriber object.
39      * @param userId Indicates the user ID.
40      * @return Returns true if successful; false otherwise.
41      */
42     int32_t PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
43         const sptr<IRemoteObject> &commonEventListener, const int32_t &userId) override;
44 
45     /**
46      * Publishes a common event.
47      *
48      * @param event Indicates the common event data.
49      * @param publishInfo Indicates the publish info.
50      * @param commonEventListener Indicates the common event subscriber.
51      * @param uid Indicates the uid of application.
52      * @param callerToken Indicates the caller token
53      * @param userId Indicates the user ID.
54      * @return Returns true if successful; false otherwise.
55      */
56     bool PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
57         const sptr<IRemoteObject> &commonEventListener, const uid_t &uid, const int32_t &callerToken,
58         const int32_t &userId) override;
59 
60     /**
61      * Subscribes to common events.
62      *
63      * @param subscribeInfo Indicates the subscribe info.
64      * @param commonEventListener Indicates the common event subscriber.
65      * @return Returns true if successful; false otherwise.
66      */
67     int32_t SubscribeCommonEvent(
68         const CommonEventSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &commonEventListener) override;
69 
70     /**
71      * Unsubscribes from common events.
72      *
73      * @param commonEventListener Indicates the common event subscriber.
74      * @return Returns true if successful; false otherwise.
75      */
76     int32_t UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener) override;
77 
78     /**
79      * Gets the current sticky common event
80      *
81      * @param event Indicates the common event.
82      * @param eventData Indicates the common event data.
83      * @return Returns true if successful; false otherwise.
84      */
85     bool GetStickyCommonEvent(const std::string &event, CommonEventData &eventData) override;
86 
87     /**
88      * Dumps state of common event service.
89      *
90      * @param dumpType Indicates the dump type.
91      * @param event Specifies the information for the common event. Set null string ("") if you want to dump all.
92      * @param userId Indicates the user ID.
93      * @param state Indicates the state of common event service.
94      * @return Returns true if successful; false otherwise.
95      */
96     bool DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
97         std::vector<std::string> &state) override;
98 
99     /**
100      * Finishes Receiver.
101      *
102      * @param proxy Indicates the receiver proxy.
103      * @param code Indicates the code of a common event.
104      * @param data Indicates the data of a common event.
105      * @param abortEvent Indicates Whether to cancel the current common event.
106      * @return Returns true if successful; false otherwise.
107      */
108     bool FinishReceiver(const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData,
109         const bool &abortEvent) override;
110 
111     /**
112      * Freezes application.
113      *
114      * @param uid Indicates the uid of application.
115      * @return Returns true if successful; false otherwise.
116      */
117     bool Freeze(const uid_t &uid) override;
118 
119     /**
120      * Unfreezes application.
121      *
122      * @param uid Indicates the Uid of application.
123      * @return Returns true if successful; false otherwise.
124      */
125     bool Unfreeze(const uid_t &uid) override;
126 
127     /**
128      * Unfreezes all frozen applications.
129      *
130      * @return Returns true if successful; false otherwise.
131      */
132     bool UnfreezeAll() override;
133 
134     int Dump(int fd, const std::vector<std::u16string> &args) override;
135 
136     ErrCode Init();
137 
138 private:
139     bool IsReady() const;
140 
141     int32_t PublishCommonEventDetailed(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
142         const sptr<IRemoteObject> &commonEventListener, const pid_t &pid, const uid_t &uid,
143         const int32_t &clientToken, const int32_t &userId);
144 
145     void GetHidumpInfo(const std::vector<std::u16string> &args, std::string &result);
146 private:
147     std::shared_ptr<InnerCommonEventManager> innerCommonEventManager_;
148     ServiceRunningState serviceRunningState_ = ServiceRunningState::STATE_NOT_START;
149     std::shared_ptr<EventRunner> runner_;
150     std::shared_ptr<EventHandler> handler_;
151 
152     DISALLOW_COPY_AND_MOVE(CommonEventManagerService);
153 };
154 }  // namespace EventFwk
155 }  // namespace OHOS
156 
157 #endif  // FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_MANAGER_SERVICE_H