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 #include "notification_config_parse.h"
24
25 namespace OHOS {
26 namespace Notification {
27
OnStart()28 void PermissionFilter::OnStart()
29 {}
30
OnStop()31 void PermissionFilter::OnStop()
32 {}
33
OnPublish(const std::shared_ptr<NotificationRecord> & record)34 ErrCode PermissionFilter::OnPublish(const std::shared_ptr<NotificationRecord> &record)
35 {
36 bool isForceControl = false;
37 bool enable = false;
38 NotificationConstant::SWITCH_STATE state = NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF;
39 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_1);
40 ErrCode result =
41 NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(record->bundleOption, state);
42 if (result == ERR_OK) {
43 enable = (state == NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON ||
44 state == NotificationConstant::SWITCH_STATE::USER_MODIFIED_ON);
45 }
46 if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
47 result = ERR_OK;
48 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
49 if (bundleManager != nullptr) {
50 enable = bundleManager->CheckApiCompatibility(record->bundleOption);
51 }
52 }
53
54 if (record->request->IsSystemLiveView()) {
55 ANS_LOGE("System live view no need check switch");
56 return ERR_OK;
57 }
58
59 sptr<NotificationSlot> slot;
60 NotificationConstant::SlotType slotType = record->request->GetSlotType();
61 message.SlotType(slotType);
62 result = NotificationPreferences::GetInstance()->GetNotificationSlot(record->bundleOption, slotType, slot);
63 if (result == ERR_OK) {
64 if (slot != nullptr) {
65 isForceControl = slot->GetForceControl();
66 } else {
67 message.ErrorCode(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED).Message("Notification slot not enable.");
68 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
69 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED;
70 ANS_LOGE("Notification slot not enable.");
71 }
72 } else {
73 if (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST) {
74 message.ErrorCode(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST).Message("Slot type not exist.");
75 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
76 ANS_LOGE("Slot type %{public}d not exist.", slotType);
77 }
78 }
79
80 if (result == ERR_OK) {
81 if (!enable && !isForceControl) {
82 message.ErrorCode(ERR_ANS_NOT_ALLOWED).Message("Notifications is off.");
83 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
84 ANS_LOGE("Enable notifications for bundle is OFF");
85 return ERR_ANS_NOT_ALLOWED;
86 }
87
88 if (record->notification->GetBundleName() != record->notification->GetCreateBundle()) {
89 // Publish as bundle
90 }
91 }
92 return result;
93 }
94 } // namespace Notification
95 } // namespace OHOS