1 /*
2 * Copyright (c) 2024-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 "base_publish_process.h"
17
18 #include "access_token_helper.h"
19 #include "ans_const_define.h"
20 #include "ans_log_wrapper.h"
21 #include "ans_inner_errors.h"
22 #include "ans_permission_def.h"
23 #include "os_account_manager_helper.h"
24 #include "ipc_skeleton.h"
25 #include "parameters.h"
26 #include "notification_analytics_util.h"
27
28 namespace OHOS {
29 namespace Notification {
30
PublishPreWork(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed)31 ErrCode BasePublishProcess::PublishPreWork(const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed)
32 {
33 if (!request->IsRemoveAllowed()) {
34 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) {
35 request->SetRemoveAllowed(true);
36 }
37 }
38 return ERR_OK;
39 }
40
CommonPublishCheck(const sptr<NotificationRequest> & request)41 ErrCode BasePublishProcess::CommonPublishCheck(const sptr<NotificationRequest> &request)
42 {
43 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_1);
44 if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT) {
45 if (!AccessTokenHelper::IsSystemApp()) {
46 message.ErrorCode(ERR_ANS_NON_SYSTEM_APP);
47 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
48 return ERR_ANS_NON_SYSTEM_APP;
49 }
50 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
51 message.ErrorCode(ERR_ANS_NON_SYSTEM_APP);
52 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
53 return ERR_ANS_PERMISSION_DENIED;
54 }
55 }
56 return ERR_OK;
57 }
58
CommonPublishProcess(const sptr<NotificationRequest> & request)59 ErrCode BasePublishProcess::CommonPublishProcess(const sptr<NotificationRequest> &request)
60 {
61 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
62 if (AccessTokenHelper::IsDlpHap(callerToken)) {
63 ANS_LOGE("DLP hap not allowed to send notifications");
64 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_5)
65 .ErrorCode(ERR_ANS_DLP_HAP).Message("CommonPublishProcess failed");
66 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
67 return ERR_ANS_DLP_HAP;
68 }
69 return ERR_OK;
70 }
71 } // namespace Notification
72 } // namespace OHOS
73