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 "permission_filter.h"
17
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "bundle_manager_helper.h"
21 #include "notification_preferences.h"
22 #include "notification_analytics_util.h"
23
24 namespace OHOS {
25 namespace Notification {
OnStart()26 void PermissionFilter::OnStart()
27 {}
28
OnStop()29 void PermissionFilter::OnStop()
30 {}
31
OnPublish(const std::shared_ptr<NotificationRecord> & record)32 ErrCode PermissionFilter::OnPublish(const std::shared_ptr<NotificationRecord> &record)
33 {
34 bool isForceControl = false;
35 bool enable = false;
36 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_1);
37 ErrCode result =
38 NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(record->bundleOption, enable);
39 if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
40 result = ERR_OK;
41 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
42 if (bundleManager != nullptr) {
43 enable = bundleManager->CheckApiCompatibility(record->bundleOption);
44 }
45 }
46
47 sptr<NotificationSlot> slot;
48 NotificationConstant::SlotType slotType = record->request->GetSlotType();
49 message.SlotType(slotType);
50 result = NotificationPreferences::GetInstance()->GetNotificationSlot(record->bundleOption, slotType, slot);
51 if (result == ERR_OK) {
52 if (slot != nullptr) {
53 isForceControl = slot->GetForceControl();
54 } else {
55 message.ErrorCode(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED).Message("Slot type not exist.");
56 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
57 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED;
58 ANS_LOGE("Type[%{public}d] slot does not exist", slotType);
59 }
60 }
61
62 if (result == ERR_OK) {
63 if (!enable && !isForceControl) {
64 message.ErrorCode(ERR_ANS_NOT_ALLOWED).Message("Notifications is off.");
65 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
66 ANS_LOGE("Enable notifications for bundle is OFF");
67 return ERR_ANS_NOT_ALLOWED;
68 }
69
70 if (record->notification->GetBundleName() != record->notification->GetCreateBundle()) {
71 // Publish as bundle
72 }
73 }
74 return result;
75 }
76 } // namespace Notification
77 } // namespace OHOS
78