1 /*
2 * Copyright (c) 2021-2024 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_service.h"
17
18 #include "ability_manager_helper.h"
19 #include "access_token_helper.h"
20 #include "accesstoken_kit.h"
21 #include "bundle_manager_helper.h"
22 #include "common_event_constant.h"
23 #include "datetime_ex.h"
24 #include "event_log_wrapper.h"
25 #include "hitrace_meter_adapter.h"
26 #include "ipc_skeleton.h"
27 #include "parameters.h"
28 #include "publish_manager.h"
29 #include "refbase.h"
30 #include "system_ability_definition.h"
31 #include "os_account_manager_helper.h"
32 #include "xcollie/watchdog.h"
33 #include "ces_inner_error_code.h"
34 #include "system_time.h"
35 #include <mutex>
36 #include <new>
37
38 namespace OHOS {
39 namespace EventFwk {
40 namespace {
41 const std::string NOTIFICATION_CES_CHECK_SA_PERMISSION = "notification.ces.check.sa.permission";
42 } // namespace
43
44 using namespace OHOS::Notification;
45
46 sptr<CommonEventManagerService> CommonEventManagerService::instance_;
47 std::mutex CommonEventManagerService::instanceMutex_;
48
GetInstance()49 sptr<CommonEventManagerService> CommonEventManagerService::GetInstance()
50 {
51 if (instance_ == nullptr) {
52 std::lock_guard<std::mutex> lock(instanceMutex_);
53 if (instance_ == nullptr) {
54 instance_ = new (std::nothrow) CommonEventManagerService();
55 }
56 }
57 return instance_;
58 }
59
CommonEventManagerService()60 CommonEventManagerService::CommonEventManagerService()
61 : serviceRunningState_(ServiceRunningState::STATE_NOT_START),
62 runner_(nullptr),
63 handler_(nullptr)
64 {
65 supportCheckSaPermission_ = OHOS::system::GetParameter(NOTIFICATION_CES_CHECK_SA_PERMISSION, "false");
66 EVENT_LOGD("instance created");
67 }
68
~CommonEventManagerService()69 CommonEventManagerService::~CommonEventManagerService()
70 {
71 EVENT_LOGD("instance destroyed");
72 }
73
Init()74 ErrCode __attribute__((weak)) CommonEventManagerService::Init()
75 {
76 EVENT_LOGD("ready to init");
77 innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
78 if (!innerCommonEventManager_) {
79 EVENT_LOGE("Failed to init without inner service");
80 return ERR_INVALID_OPERATION;
81 }
82
83 commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
84 serviceRunningState_ = ServiceRunningState::STATE_RUNNING;
85
86 return ERR_OK;
87 }
88
IsReady() const89 bool CommonEventManagerService::IsReady() const
90 {
91 if (!innerCommonEventManager_) {
92 EVENT_LOGE("innerCommonEventManager is null");
93 return false;
94 }
95
96 if (!commonEventSrvQueue_) {
97 EVENT_LOGE("queue object is null");
98 return false;
99 }
100
101 return true;
102 }
103
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)104 int32_t CommonEventManagerService::PublishCommonEvent(const CommonEventData &event,
105 const CommonEventPublishInfo &publishinfo, const sptr<IRemoteObject> &commonEventListener,
106 const int32_t &userId)
107 {
108 EVENT_LOGD("enter");
109
110 if (!IsReady()) {
111 EVENT_LOGE("CommonEventManagerService not ready");
112 return ERR_NOTIFICATION_CESM_ERROR;
113 }
114
115 if (userId != ALL_USER && userId != CURRENT_USER && userId != UNDEFINED_USER) {
116 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
117 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
118 EVENT_LOGE("publish to special user must be system application.");
119 return ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
120 }
121 }
122
123 return PublishCommonEventDetailed(event,
124 publishinfo,
125 commonEventListener,
126 IPCSkeleton::GetCallingPid(),
127 IPCSkeleton::GetCallingUid(),
128 IPCSkeleton::GetCallingTokenID(),
129 userId);
130 }
131
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const uid_t & uid,const int32_t & callerToken,const int32_t & userId)132 bool CommonEventManagerService::PublishCommonEvent(const CommonEventData &event,
133 const CommonEventPublishInfo &publishinfo, const sptr<IRemoteObject> &commonEventListener, const uid_t &uid,
134 const int32_t &callerToken, const int32_t &userId)
135 {
136 EVENT_LOGD("enter");
137
138 if (!IsReady()) {
139 EVENT_LOGE("CommonEventManagerService not ready");
140 return false;
141 }
142
143 auto callingUid = IPCSkeleton::GetCallingUid();
144 if (callingUid != FOUNDATION_UID) {
145 EVENT_LOGE("Only foundation can publish common event as proxy uid = %{public}d.", callingUid);
146 return false;
147 }
148
149 return PublishCommonEventDetailed(
150 event, publishinfo, commonEventListener, UNDEFINED_PID, uid, callerToken, userId) == ERR_OK ? true : false;
151 }
152
PublishCommonEventDetailed(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const pid_t & pid,const uid_t & uid,const int32_t & clientToken,const int32_t & userId)153 int32_t CommonEventManagerService::PublishCommonEventDetailed(const CommonEventData &event,
154 const CommonEventPublishInfo &publishinfo, const sptr<IRemoteObject> &commonEventListener, const pid_t &pid,
155 const uid_t &uid, const int32_t &clientToken, const int32_t &userId)
156 {
157 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
158
159 if (AccessTokenHelper::IsDlpHap(clientToken)) {
160 EVENT_LOGE("DLP hap not allowed to send common event");
161 return ERR_NOTIFICATION_CES_NOT_SA_SYSTEM_APP;
162 }
163 struct tm recordTime = {0};
164 if (!GetSystemCurrentTime(&recordTime)) {
165 EVENT_LOGE("Failed to GetSystemCurrentTime");
166 return ERR_NOTIFICATION_SYS_ERROR;
167 }
168 int32_t errCode = CheckUserIdParams(userId);
169 if (errCode != ERR_OK) {
170 return errCode;
171 }
172
173 if (DelayedSingleton<PublishManager>::GetInstance()->CheckIsFloodAttack(uid)) {
174 EVENT_LOGE("Too many common events have been sent in a short period from (pid = %{public}d, uid = "
175 "%{public}d, userId = %{public}d)", pid, uid, userId);
176 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
177 }
178
179 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
180 wptr<CommonEventManagerService> weakThis = this;
181 std::function<void()> publishCommonEventFunc = [wp,
182 event,
183 publishinfo,
184 commonEventListener,
185 recordTime,
186 pid,
187 uid,
188 clientToken,
189 userId,
190 weakThis] () {
191 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
192 if (innerCommonEventManager == nullptr) {
193 EVENT_LOGE("innerCommonEventManager not exist");
194 return;
195 }
196 std::string bundleName = "";
197 if (!AccessTokenHelper::VerifyNativeToken(clientToken)) {
198 bundleName = DelayedSingleton<BundleManagerHelper>::GetInstance()->GetBundleName(uid);
199 }
200 sptr<CommonEventManagerService> commonEventManagerService = weakThis.promote();
201 if (commonEventManagerService == nullptr) {
202 EVENT_LOGE("CommonEventManager not exist");
203 return;
204 }
205 bool ret = innerCommonEventManager->PublishCommonEvent(event,
206 publishinfo,
207 commonEventListener,
208 recordTime,
209 pid,
210 uid,
211 clientToken,
212 userId,
213 bundleName,
214 commonEventManagerService);
215 if (!ret) {
216 EVENT_LOGE("failed to publish event %{public}s", event.GetWant().GetAction().c_str());
217 }
218 };
219 EVENT_LOGD("Start to submit publish commonEvent <%{public}d>", uid);
220 commonEventSrvQueue_->submit(publishCommonEventFunc);
221 return ERR_OK;
222 }
223
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener,const int32_t instanceKey)224 int32_t CommonEventManagerService::SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
225 const sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey)
226 {
227 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
228 EVENT_LOGD("enter");
229
230 if (!IsReady()) {
231 EVENT_LOGE("CommonEventManagerService not ready");
232 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
233 }
234
235 struct tm recordTime = {0};
236 if (!GetSystemCurrentTime(&recordTime)) {
237 EVENT_LOGE("Failed to GetSystemCurrentTime");
238 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
239 }
240
241 int32_t errCode = CheckUserIdParams(subscribeInfo.GetUserId());
242 if (errCode != ERR_OK) {
243 return errCode;
244 }
245
246 auto callingUid = IPCSkeleton::GetCallingUid();
247 auto callingPid = IPCSkeleton::GetCallingPid();
248 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
249 std::string bundleName = "";
250 if (!AccessTokenHelper::VerifyNativeToken(callerToken)) {
251 bundleName = DelayedSingleton<BundleManagerHelper>::GetInstance()->GetBundleName(callingUid);
252 }
253 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
254 int64_t startTime = SystemTime::GetNowSysTime();
255 std::function<void()> subscribeCommonEventFunc = [wp,
256 subscribeInfo,
257 commonEventListener,
258 recordTime,
259 callingPid,
260 callingUid,
261 callerToken,
262 bundleName,
263 instanceKey,
264 startTime] () {
265 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
266 if (innerCommonEventManager == nullptr) {
267 EVENT_LOGE("innerCommonEventManager not exist");
268 return;
269 }
270 bool ret = innerCommonEventManager->SubscribeCommonEvent(subscribeInfo,
271 commonEventListener,
272 recordTime,
273 callingPid,
274 callingUid,
275 callerToken,
276 bundleName,
277 instanceKey,
278 startTime);
279 if (!ret) {
280 EVENT_LOGE("failed to subscribe event");
281 }
282 };
283
284 EVENT_LOGD("Start to submit subscribe commonEvent <%{public}d>", callingUid);
285 commonEventSrvQueue_->submit(subscribeCommonEventFunc);
286 return ERR_OK;
287 }
288
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener)289 int32_t CommonEventManagerService::UnsubscribeCommonEvent(const sptr<IRemoteObject> &commonEventListener)
290 {
291 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
292 EVENT_LOGD("enter");
293
294 if (!IsReady()) {
295 EVENT_LOGE("CommonEventManagerService not ready");
296 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
297 }
298
299 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
300 std::function<void()> unsubscribeCommonEventFunc = [wp, commonEventListener] () {
301 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
302 if (innerCommonEventManager == nullptr) {
303 EVENT_LOGE("innerCommonEventManager not exist");
304 return;
305 }
306 bool ret = innerCommonEventManager->UnsubscribeCommonEvent(commonEventListener);
307 if (!ret) {
308 EVENT_LOGE("failed to unsubscribe event");
309 }
310 };
311
312 commonEventSrvQueue_->submit(unsubscribeCommonEventFunc);
313 return ERR_OK;
314 }
315
UnsubscribeCommonEventSync(const sptr<IRemoteObject> & commonEventListener)316 int32_t CommonEventManagerService::UnsubscribeCommonEventSync(const sptr<IRemoteObject> &commonEventListener)
317 {
318 HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
319 EVENT_LOGD("enter");
320
321 if (!IsReady()) {
322 EVENT_LOGE("CommonEventManagerService not ready");
323 return ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
324 }
325
326 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
327 std::function<void()> unsubscribeCommonEventFunc = [wp, commonEventListener] () {
328 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
329 if (innerCommonEventManager == nullptr) {
330 EVENT_LOGE("innerCommonEventManager not exist");
331 return;
332 }
333 bool ret = innerCommonEventManager->UnsubscribeCommonEvent(commonEventListener);
334 if (!ret) {
335 EVENT_LOGE("failed to unsubscribe event");
336 }
337 };
338
339 ffrt::task_handle handler = commonEventSrvQueue_->submit_h(unsubscribeCommonEventFunc);
340 commonEventSrvQueue_->wait(handler);
341 return ERR_OK;
342 }
343
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData)344 bool CommonEventManagerService::GetStickyCommonEvent(const std::string &event, CommonEventData &eventData)
345 {
346 EVENT_LOGD("enter");
347
348 if (!IsReady()) {
349 EVENT_LOGE("CommonEventManagerService not ready");
350 return false;
351 }
352
353 if (event.empty()) {
354 EVENT_LOGE("event is empty");
355 return false;
356 }
357 auto callerToken = IPCSkeleton::GetCallingTokenID();
358 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(callerToken);
359 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
360 EVENT_LOGE("Not system application or subsystem request.");
361 return ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
362 }
363 auto callingUid = IPCSkeleton::GetCallingUid();
364 std::string bundleName = DelayedSingleton<BundleManagerHelper>::GetInstance()->GetBundleName(callingUid);
365 const std::string permission = "ohos.permission.COMMONEVENT_STICKY";
366 bool ret = AccessTokenHelper::VerifyAccessToken(callerToken, permission);
367 if (!ret) {
368 EVENT_LOGE("No permission to get a sticky common event from %{public}s (uid = %{public}d)",
369 bundleName.c_str(),
370 callingUid);
371 return false;
372 }
373
374 return innerCommonEventManager_->GetStickyCommonEvent(event, eventData);
375 }
376
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)377 bool CommonEventManagerService::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
378 std::vector<std::string> &state)
379 {
380 EVENT_LOGD("enter");
381
382 auto callerToken = IPCSkeleton::GetCallingTokenID();
383 if (!AccessTokenHelper::VerifyShellToken(callerToken) && !AccessTokenHelper::VerifyNativeToken(callerToken)) {
384 EVENT_LOGE("Not subsystem or shell request");
385 return false;
386 }
387 if (!IsReady()) {
388 EVENT_LOGE("CommonEventManagerService not ready");
389 return false;
390 }
391
392 innerCommonEventManager_->DumpState(dumpType, event, userId, state);
393
394 return true;
395 }
396
FinishReceiver(const sptr<IRemoteObject> & proxy,const int32_t & code,const std::string & receiverData,const bool & abortEvent)397 bool CommonEventManagerService::FinishReceiver(
398 const sptr<IRemoteObject> &proxy, const int32_t &code, const std::string &receiverData, const bool &abortEvent)
399 {
400 EVENT_LOGD("enter");
401
402 if (!IsReady()) {
403 EVENT_LOGE("CommonEventManagerService not ready");
404 return false;
405 }
406 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
407 std::function<void()> finishReceiverFunc = [wp, proxy, code, receiverData, abortEvent] () {
408 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
409 if (innerCommonEventManager == nullptr) {
410 EVENT_LOGE("innerCommonEventManager not exist");
411 return;
412 }
413 innerCommonEventManager->FinishReceiver(proxy, code, receiverData, abortEvent);
414 };
415
416 commonEventSrvQueue_->submit(finishReceiverFunc);
417 return true;
418 }
419
Freeze(const uid_t & uid)420 bool CommonEventManagerService::Freeze(const uid_t &uid)
421 {
422 EVENT_LOGD("enter");
423
424 auto tokenId = IPCSkeleton::GetCallingTokenID();
425 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
426 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
427 EVENT_LOGE("Not subsystem request");
428 return false;
429 }
430
431 if (!IsReady()) {
432 EVENT_LOGE("CommonEventManagerService not ready");
433 return false;
434 }
435 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
436 std::function<void()> freezeFunc = [wp, uid] () {
437 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
438 if (innerCommonEventManager == nullptr) {
439 EVENT_LOGE("innerCommonEventManager not exist");
440 return;
441 }
442 innerCommonEventManager->Freeze(uid);
443 };
444
445 commonEventSrvQueue_->submit(freezeFunc);
446 return true;
447 }
448
Unfreeze(const uid_t & uid)449 bool CommonEventManagerService::Unfreeze(const uid_t &uid)
450 {
451 EVENT_LOGD("enter");
452
453 auto tokenId = IPCSkeleton::GetCallingTokenID();
454 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
455 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
456 EVENT_LOGE("Not subsystem request");
457 return false;
458 }
459
460 if (!IsReady()) {
461 EVENT_LOGE("CommonEventManagerService not ready");
462 return false;
463 }
464
465 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
466 std::function<void()> unfreezeFunc = [wp, uid] () {
467 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
468 if (innerCommonEventManager == nullptr) {
469 EVENT_LOGE("innerCommonEventManager not exist");
470 return;
471 }
472 innerCommonEventManager->Unfreeze(uid);
473 };
474
475 commonEventSrvQueue_->submit(unfreezeFunc);
476 return true;
477 }
478
UnfreezeAll()479 bool CommonEventManagerService::UnfreezeAll()
480 {
481 EVENT_LOGD("enter");
482
483 auto tokenId = IPCSkeleton::GetCallingTokenID();
484 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
485 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
486 EVENT_LOGE("Not subsystem request");
487 return false;
488 }
489
490 if (!IsReady()) {
491 EVENT_LOGE("CommonEventManagerService not ready");
492 return false;
493 }
494
495 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
496 std::function<void()> unfreezeAllFunc = [wp] () {
497 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
498 if (innerCommonEventManager == nullptr) {
499 EVENT_LOGE("innerCommonEventManager not exist");
500 return;
501 }
502 innerCommonEventManager->UnfreezeAll();
503 };
504
505 commonEventSrvQueue_->submit(unfreezeAllFunc);
506 return true;
507 }
508
Dump(int fd,const std::vector<std::u16string> & args)509 int CommonEventManagerService::Dump(int fd, const std::vector<std::u16string> &args)
510 {
511 EVENT_LOGD("enter");
512
513 auto callerToken = IPCSkeleton::GetCallingTokenID();
514 if (!AccessTokenHelper::VerifyShellToken(callerToken) && !AccessTokenHelper::VerifyNativeToken(callerToken)) {
515 EVENT_LOGE("Not subsystem or shell request");
516 return ERR_NOTIFICATION_CES_COMMON_PERMISSION_DENIED;
517 }
518 if (!IsReady()) {
519 EVENT_LOGE("CommonEventManagerService not ready");
520 return ERR_INVALID_VALUE;
521 }
522 std::string result;
523 innerCommonEventManager_->HiDump(args, result);
524 int ret = dprintf(fd, "%s\n", result.c_str());
525 if (ret < 0) {
526 EVENT_LOGE("dprintf error");
527 return ERR_INVALID_VALUE;
528 }
529 return ERR_OK;
530 }
531
RemoveStickyCommonEvent(const std::string & event)532 int32_t CommonEventManagerService::RemoveStickyCommonEvent(const std::string &event)
533 {
534 EVENT_LOGI("enter");
535
536 if (!IsReady()) {
537 EVENT_LOGE("CommonEventManagerService not ready");
538 return ERR_NOTIFICATION_CESM_ERROR;
539 }
540
541 auto tokenId = IPCSkeleton::GetCallingTokenID();
542 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(tokenId);
543 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
544 EVENT_LOGE("Not system application or subsystem request.");
545 return ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
546 }
547
548 const std::string permission = "ohos.permission.COMMONEVENT_STICKY";
549 bool ret = AccessTokenHelper::VerifyAccessToken(tokenId, permission);
550 if (!ret && (!isSubsystem || supportCheckSaPermission_.compare("true") == 0)) {
551 EVENT_LOGE("No permission.");
552 return ERR_NOTIFICATION_CES_COMMON_PERMISSION_DENIED;
553 }
554
555 return innerCommonEventManager_->RemoveStickyCommonEvent(event, IPCSkeleton::GetCallingUid());
556 }
557
SetStaticSubscriberState(bool enable)558 int32_t CommonEventManagerService::SetStaticSubscriberState(bool enable)
559 {
560 if (!AccessTokenHelper::IsSystemApp()) {
561 EVENT_LOGE("Not system application");
562 return ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
563 }
564
565 return innerCommonEventManager_->SetStaticSubscriberState(enable);
566 }
567
SetStaticSubscriberState(const std::vector<std::string> & events,bool enable)568 int32_t CommonEventManagerService::SetStaticSubscriberState(const std::vector<std::string> &events, bool enable)
569 {
570 if (!AccessTokenHelper::IsSystemApp()) {
571 EVENT_LOGE("Not system application.");
572 return ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
573 }
574 return innerCommonEventManager_->SetStaticSubscriberState(events, enable);
575 }
576
SetFreezeStatus(std::set<int> pidList,bool isFreeze)577 bool CommonEventManagerService::SetFreezeStatus(std::set<int> pidList, bool isFreeze)
578 {
579 EVENT_LOGD("enter");
580 auto tokenId = IPCSkeleton::GetCallingTokenID();
581 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
582 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
583 EVENT_LOGE("Not subsystem request");
584 return false;
585 }
586
587 if (!IsReady()) {
588 EVENT_LOGE("CommonEventManagerService not ready");
589 return false;
590 }
591
592 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
593 std::function<void()> setFreezeStatusFunc = [wp, pidList, isFreeze] () {
594 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
595 if (innerCommonEventManager == nullptr) {
596 EVENT_LOGE("innerCommonEventManager not exist");
597 return;
598 }
599 innerCommonEventManager->SetFreezeStatus(pidList, isFreeze);
600 };
601
602 commonEventSrvQueue_->submit(setFreezeStatusFunc);
603 return true;
604 }
605
CheckUserIdParams(const int32_t & userId)606 int32_t CommonEventManagerService::CheckUserIdParams(const int32_t &userId)
607 {
608 if (userId != ALL_USER && userId != CURRENT_USER && userId != UNDEFINED_USER
609 && !OsAccountManagerHelper::GetInstance()->CheckUserExists(userId)) {
610 EVENT_LOGE("UserId %{public}d is not exists in OsAccount", userId);
611 return ERR_NOTIFICATION_CES_USERID_INVALID;
612 }
613 return ERR_OK;
614 }
615 } // namespace EventFwk
616 } // namespace OHOS
617