• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "advanced_notification_service.h"
17 
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "accesstoken_kit.h"
23 #include "ans_const_define.h"
24 #include "ans_inner_errors.h"
25 #include "ans_log_wrapper.h"
26 #include "errors.h"
27 
28 #include "ipc_skeleton.h"
29 #include "notification_constant.h"
30 #include "os_account_manager_helper.h"
31 #include "hitrace_meter_adapter.h"
32 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
33 #include "distributed_notification_manager.h"
34 #include "distributed_preferences.h"
35 #include "distributed_screen_status_manager.h"
36 #endif
37 
38 #include "advanced_notification_inline.cpp"
39 #include "notification_analytics_util.h"
40 
41 namespace OHOS {
42 namespace Notification {
43 
Subscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)44 ErrCode AdvancedNotificationService::Subscribe(
45     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &info)
46 {
47     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
48     ANS_LOGD("%{public}s", __FUNCTION__);
49     ErrCode errCode = ERR_OK;
50     do {
51         if (subscriber == nullptr) {
52             errCode = ERR_ANS_INVALID_PARAM;
53             break;
54         }
55 
56         bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
57         if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
58             ANS_LOGE("Client is not a system app or subsystem");
59             errCode = ERR_ANS_NON_SYSTEM_APP;
60             break;
61         }
62 
63         if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
64             errCode = ERR_ANS_PERMISSION_DENIED;
65             break;
66         }
67 
68         if (info != nullptr && info->GetAppUserId() != SUBSCRIBE_USER_ALL) {
69             errCode = CheckUserIdParams(info->GetAppUserId());
70             if (errCode != ERR_OK) {
71                 break;
72             }
73         }
74 
75         errCode = NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, info);
76         if (errCode != ERR_OK) {
77             break;
78         }
79     } while (0);
80     SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info, errCode);
81     return errCode;
82 }
83 
SubscribeSelf(const sptr<AnsSubscriberInterface> & subscriber)84 ErrCode AdvancedNotificationService::SubscribeSelf(const sptr<AnsSubscriberInterface> &subscriber)
85 {
86     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
87     ANS_LOGD("%{public}s", __FUNCTION__);
88     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo();
89     if (sptrInfo == nullptr) {
90         ANS_LOGE("Failed to create sptrInfo");
91         return ERR_ANS_NO_MEMORY;
92     }
93     ErrCode errCode = ERR_OK;
94     do {
95         if (subscriber == nullptr) {
96             errCode = ERR_ANS_INVALID_PARAM;
97             break;
98         }
99 
100         bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
101         if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
102             ANS_LOGE("Client is not a system app or subsystem");
103             errCode = ERR_ANS_NON_SYSTEM_APP;
104             break;
105         }
106 
107         int32_t uid = IPCSkeleton().GetCallingUid();
108         // subscribeSelf doesn't need OHOS_PERMISSION_NOTIFICATION_CONTROLLER permission
109         std::string bundle;
110         std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
111         if (bundleManager != nullptr) {
112             bundle = bundleManager->GetBundleNameByUid(uid);
113         }
114 
115         sptrInfo->AddAppName(bundle);
116         sptrInfo->SetSubscriberUid(uid);
117 
118         errCode = NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, sptrInfo);
119         if (errCode != ERR_OK) {
120             break;
121         }
122     } while (0);
123 
124     if (errCode == ERR_OK) {
125         int32_t callingUid = IPCSkeleton::GetCallingUid();
126         ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
127             LivePublishProcess::GetInstance()->AddLiveViewSubscriber(callingUid);
128         }));
129         notificationSvrQueue_->wait(handler);
130     }
131     SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), sptrInfo, errCode);
132     return errCode;
133 }
134 
Unsubscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)135 ErrCode AdvancedNotificationService::Unsubscribe(
136     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &info)
137 {
138     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
139     ANS_LOGD("%{public}s", __FUNCTION__);
140 
141     SendUnSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info);
142     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_3);
143     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
144     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
145         ANS_LOGE("Client is not a system app or subsystem");
146         message.Message("Unsubscribe notification: " + std::to_string(ERR_ANS_NON_SYSTEM_APP));
147         NotificationAnalyticsUtil::ReportModifyEvent(message);
148         return ERR_ANS_NON_SYSTEM_APP;
149     }
150 
151     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
152         message.Message("Unsubscribe notification: " + std::to_string(ERR_ANS_PERMISSION_DENIED));
153         NotificationAnalyticsUtil::ReportModifyEvent(message);
154         return ERR_ANS_PERMISSION_DENIED;
155     }
156 
157     if (subscriber == nullptr) {
158         message.Message("Unsubscribe notification: " + std::to_string(ERR_ANS_INVALID_PARAM));
159         NotificationAnalyticsUtil::ReportModifyEvent(message);
160         return ERR_ANS_INVALID_PARAM;
161     }
162 
163     ErrCode errCode = NotificationSubscriberManager::GetInstance()->RemoveSubscriber(subscriber, info);
164     if (errCode != ERR_OK) {
165         return errCode;
166     }
167 
168     return ERR_OK;
169 }
170 }  // namespace Notification
171 }  // namespace OHOS
172