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 if (record->request->IsSystemLiveView()) {
48 ANS_LOGI("System live view no need check switch.");
49 return ERR_OK;
50 }
51
52 sptr<NotificationSlot> slot;
53 NotificationConstant::SlotType slotType = record->request->GetSlotType();
54 message.SlotType(slotType);
55 result = NotificationPreferences::GetInstance()->GetNotificationSlot(record->bundleOption, slotType, slot);
56 if (result == ERR_OK) {
57 if (slot != nullptr) {
58 isForceControl = slot->GetForceControl();
59 } else {
60 message.ErrorCode(ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED).Message("Slot type not exist.");
61 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
62 result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED;
63 ANS_LOGE("Type[%{public}d] slot does not exist", slotType);
64 }
65 }
66
67 if (result == ERR_OK) {
68 if (!enable && !isForceControl) {
69 message.ErrorCode(ERR_ANS_NOT_ALLOWED).Message("Notifications is off.");
70 NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
71 ANS_LOGE("Enable notifications for bundle is OFF");
72 return ERR_ANS_NOT_ALLOWED;
73 }
74
75 if (record->notification->GetBundleName() != record->notification->GetCreateBundle()) {
76 // Publish as bundle
77 }
78 }
79 return result;
80 }
81 } // namespace Notification
82 } // namespace OHOS
83