1 /*
2 * Copyright (c) 2021-2022 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 #include "common_event_manager.h"
17
18 #include "common_event.h"
19 #include "common_event_constant.h"
20 #include "event_log_wrapper.h"
21 #include "singleton.h"
22
23 namespace OHOS {
24 namespace EventFwk {
PublishCommonEvent(const CommonEventData & data)25 bool CommonEventManager::PublishCommonEvent(const CommonEventData &data)
26 {
27 CommonEventPublishInfo publishInfo;
28 return PublishCommonEventAsUser(data, publishInfo, nullptr, UNDEFINED_USER);
29 }
30
PublishCommonEventAsUser(const CommonEventData & data,const int32_t & userId)31 bool CommonEventManager::PublishCommonEventAsUser(const CommonEventData &data, const int32_t &userId)
32 {
33 CommonEventPublishInfo publishInfo;
34 return PublishCommonEventAsUser(data, publishInfo, nullptr, userId);
35 }
36
PublishCommonEvent(const CommonEventData & data,const CommonEventPublishInfo & publishInfo)37 bool CommonEventManager::PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo)
38 {
39 return NewPublishCommonEvent(data, publishInfo) == ERR_OK ? true : false;
40 }
41
NewPublishCommonEvent(const CommonEventData & data,const CommonEventPublishInfo & publishInfo)42 int32_t CommonEventManager::NewPublishCommonEvent(const CommonEventData &data,
43 const CommonEventPublishInfo &publishInfo)
44 {
45 return NewPublishCommonEventAsUser(data, publishInfo, nullptr, UNDEFINED_USER);
46 }
47
PublishCommonEventAsUser(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const int32_t & userId)48 bool CommonEventManager::PublishCommonEventAsUser(const CommonEventData &data,
49 const CommonEventPublishInfo &publishInfo, const int32_t &userId)
50 {
51 return NewPublishCommonEventAsUser(data, publishInfo, userId) == ERR_OK ? true : false;
52 }
53
NewPublishCommonEventAsUser(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const int32_t & userId)54 int32_t CommonEventManager::NewPublishCommonEventAsUser(const CommonEventData &data,
55 const CommonEventPublishInfo &publishInfo, const int32_t &userId)
56 {
57 return NewPublishCommonEventAsUser(data, publishInfo, nullptr, userId);
58 }
59
PublishCommonEvent(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const std::shared_ptr<CommonEventSubscriber> & subscriber)60 bool CommonEventManager::PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
61 const std::shared_ptr<CommonEventSubscriber> &subscriber)
62 {
63 return PublishCommonEventAsUser(data, publishInfo, subscriber, UNDEFINED_USER);
64 }
65
PublishCommonEventAsUser(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const std::shared_ptr<CommonEventSubscriber> & subscriber,const int32_t & userId)66 bool CommonEventManager::PublishCommonEventAsUser(const CommonEventData &data,
67 const CommonEventPublishInfo &publishInfo, const std::shared_ptr<CommonEventSubscriber> &subscriber,
68 const int32_t &userId)
69 {
70 return NewPublishCommonEventAsUser(data, publishInfo, subscriber, userId) == ERR_OK ? true : false;
71 }
72
NewPublishCommonEventAsUser(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const std::shared_ptr<CommonEventSubscriber> & subscriber,const int32_t & userId)73 int32_t CommonEventManager::NewPublishCommonEventAsUser(const CommonEventData &data,
74 const CommonEventPublishInfo &publishInfo, const std::shared_ptr<CommonEventSubscriber> &subscriber,
75 const int32_t &userId)
76 {
77 return DelayedSingleton<CommonEvent>::GetInstance()->PublishCommonEventAsUser(data, publishInfo, subscriber,
78 userId);
79 }
80
PublishCommonEvent(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const std::shared_ptr<CommonEventSubscriber> & subscriber,const uid_t & uid,const int32_t & callerToken)81 bool CommonEventManager::PublishCommonEvent(const CommonEventData &data, const CommonEventPublishInfo &publishInfo,
82 const std::shared_ptr<CommonEventSubscriber> &subscriber, const uid_t &uid, const int32_t &callerToken)
83 {
84 return PublishCommonEventAsUser(data, publishInfo, subscriber, uid, callerToken, UNDEFINED_USER);
85 }
86
PublishCommonEventAsUser(const CommonEventData & data,const CommonEventPublishInfo & publishInfo,const std::shared_ptr<CommonEventSubscriber> & subscriber,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)87 bool CommonEventManager::PublishCommonEventAsUser(const CommonEventData &data,
88 const CommonEventPublishInfo &publishInfo, const std::shared_ptr<CommonEventSubscriber> &subscriber,
89 const uid_t &uid, const int32_t &callerToken, const int32_t &userId)
90 {
91 return DelayedSingleton<CommonEvent>::GetInstance()->PublishCommonEventAsUser(data, publishInfo, subscriber, uid,
92 callerToken, userId);
93 }
94
SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)95 bool CommonEventManager::SubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
96 {
97 return NewSubscribeCommonEvent(subscriber) == ERR_OK ? true : false;
98 }
99
NewSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)100 int32_t CommonEventManager::NewSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
101 {
102 return DelayedSingleton<CommonEvent>::GetInstance()->SubscribeCommonEvent(subscriber);
103 }
104
UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)105 bool CommonEventManager::UnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
106 {
107 return NewUnSubscribeCommonEvent(subscriber) == ERR_OK ? true : false;
108 }
109
NewUnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> & subscriber)110 int32_t CommonEventManager::NewUnSubscribeCommonEvent(const std::shared_ptr<CommonEventSubscriber> &subscriber)
111 {
112 return DelayedSingleton<CommonEvent>::GetInstance()->UnSubscribeCommonEvent(subscriber);
113 }
114
GetStickyCommonEvent(const std::string & event,CommonEventData & commonEventData)115 bool CommonEventManager::GetStickyCommonEvent(const std::string &event, CommonEventData &commonEventData)
116 {
117 return DelayedSingleton<CommonEvent>::GetInstance()->GetStickyCommonEvent(event, commonEventData);
118 }
119
Freeze(const uid_t & uid)120 bool CommonEventManager::Freeze(const uid_t &uid)
121 {
122 return DelayedSingleton<CommonEvent>::GetInstance()->Freeze(uid);
123 }
124
Unfreeze(const uid_t & uid)125 bool CommonEventManager::Unfreeze(const uid_t &uid)
126 {
127 return DelayedSingleton<CommonEvent>::GetInstance()->Unfreeze(uid);
128 }
129
UnfreezeAll()130 bool CommonEventManager::UnfreezeAll()
131 {
132 return DelayedSingleton<CommonEvent>::GetInstance()->UnfreezeAll();
133 }
134
RemoveStickyCommonEvent(const std::string & event)135 int32_t CommonEventManager::RemoveStickyCommonEvent(const std::string &event)
136 {
137 return DelayedSingleton<CommonEvent>::GetInstance()->RemoveStickyCommonEvent(event);
138 }
139
SetStaticSubscriberState(bool enable)140 int32_t CommonEventManager::SetStaticSubscriberState(bool enable)
141 {
142 return DelayedSingleton<CommonEvent>::GetInstance()->SetStaticSubscriberState(enable);
143 }
144 } // namespace EventFwk
145 } // namespace OHOS