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     ErrCode errCode = ERR_OK;
90     do {
91         if (subscriber == nullptr) {
92             errCode = ERR_ANS_INVALID_PARAM;
93             break;
94         }
95 
96         bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
97         if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
98             ANS_LOGE("Client is not a system app or subsystem");
99             errCode = ERR_ANS_NON_SYSTEM_APP;
100             break;
101         }
102 
103         int32_t uid = IPCSkeleton().GetCallingUid();
104         // subscribeSelf doesn't need OHOS_PERMISSION_NOTIFICATION_CONTROLLER permission
105         std::string bundle;
106         std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
107         if (bundleManager != nullptr) {
108             bundle = bundleManager->GetBundleNameByUid(uid);
109         }
110 
111         sptrInfo->AddAppName(bundle);
112         sptrInfo->SetSubscriberUid(uid);
113 
114         errCode = NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, sptrInfo);
115         if (errCode != ERR_OK) {
116             break;
117         }
118     } while (0);
119 
120     if (errCode == ERR_OK) {
121         int32_t callingUid = IPCSkeleton::GetCallingUid();
122         ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
123             LivePublishProcess::GetInstance()->AddLiveViewSubscriber(callingUid);
124         }));
125         notificationSvrQueue_->wait(handler);
126     }
127     SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), sptrInfo, errCode);
128     return errCode;
129 }
130 
Unsubscribe(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)131 ErrCode AdvancedNotificationService::Unsubscribe(
132     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &info)
133 {
134     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
135     ANS_LOGD("%{public}s", __FUNCTION__);
136 
137     SendUnSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info);
138     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_3);
139     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
140     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
141         ANS_LOGE("Client is not a system app or subsystem");
142         message.Message("Unsubscribe notification: " + std::to_string(ERR_ANS_NON_SYSTEM_APP));
143         NotificationAnalyticsUtil::ReportModifyEvent(message);
144         return ERR_ANS_NON_SYSTEM_APP;
145     }
146 
147     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
148         message.Message("Unsubscribe notification: " + std::to_string(ERR_ANS_PERMISSION_DENIED));
149         NotificationAnalyticsUtil::ReportModifyEvent(message);
150         return ERR_ANS_PERMISSION_DENIED;
151     }
152 
153     if (subscriber == nullptr) {
154         message.Message("Unsubscribe notification: " + std::to_string(ERR_ANS_INVALID_PARAM));
155         NotificationAnalyticsUtil::ReportModifyEvent(message);
156         return ERR_ANS_INVALID_PARAM;
157     }
158 
159     ErrCode errCode = NotificationSubscriberManager::GetInstance()->RemoveSubscriber(subscriber, info);
160     if (errCode != ERR_OK) {
161         return errCode;
162     }
163 
164     return ERR_OK;
165 }
166 }  // namespace Notification
167 }  // namespace OHOS
168