1 /*
2 * Copyright (c) 2021 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 #include <dlfcn.h>
16 #include <string>
17
18 #include "advanced_notification_service.h"
19 #include "notification_extension_wrapper.h"
20 #include "notification_preferences.h"
21 #include "advanced_datashare_observer.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24
25 #include "common_event_subscriber.h"
26 #include "system_event_observer.h"
27
28 namespace OHOS::Notification {
29 const std::string EXTENTION_WRAPPER_PATH = "libans_ext.z.so";
30 const int32_t ACTIVE_DELETE = 0;
31 const int32_t PASSITIVE_DELETE = 1;
32 static constexpr const char *SETTINGS_DATA_UNIFIED_GROUP_ENABLE_URI =
33 "datashare:///com.ohos.settingsdata/entry/settingsdata/"
34 "USER_SETTINGSDATA_SECURE_100?Proxy=true&key=unified_group_enable";
35 ExtensionWrapper::ExtensionWrapper() = default;
36 ExtensionWrapper::~ExtensionWrapper() = default;
37
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
UpdateUnifiedGroupInfo(const std::string & key,std::shared_ptr<NotificationUnifiedGroupInfo> & groupInfo)43 void UpdateUnifiedGroupInfo(const std::string &key, std::shared_ptr<NotificationUnifiedGroupInfo> &groupInfo)
44 {
45 AdvancedNotificationService::GetInstance()->UpdateUnifiedGroupInfo(key, groupInfo);
46 }
47
48 #ifdef __cplusplus
49 }
50 #endif
51
InitExtentionWrapper()52 void ExtensionWrapper::InitExtentionWrapper()
53 {
54 extensionWrapperHandle_ = dlopen(EXTENTION_WRAPPER_PATH.c_str(), RTLD_NOW);
55 if (extensionWrapperHandle_ == nullptr) {
56 ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
57 return;
58 }
59
60 syncAdditionConfig_ = (SYNC_ADDITION_CONFIG)dlsym(extensionWrapperHandle_, "SyncAdditionConfig");
61 if (syncAdditionConfig_ == nullptr) {
62 ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
63 return;
64 }
65 #ifdef ENABLE_ANS_ADDITIONAL_CONTROL
66 localControl_ = (LOCAL_CONTROL)dlsym(extensionWrapperHandle_, "LocalControl");
67 reminderControl_ = (REMINDER_CONTROL)dlsym(extensionWrapperHandle_, "ReminderControl");
68 bannerControl_ = (BANNER_CONTROL)dlsym(extensionWrapperHandle_, "BannerControl");
69 if (bannerControl_ == nullptr || localControl_ == nullptr || reminderControl_ == nullptr) {
70 ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
71 return;
72 }
73
74 std::string ctrlConfig = NotificationPreferences::GetInstance()->GetAdditionalConfig("NOTIFICATION_CTL_LIST_PKG");
75 if (!ctrlConfig.empty()) {
76 syncAdditionConfig_("NOTIFICATION_CTL_LIST_PKG", ctrlConfig);
77 }
78 #endif
79 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
80 modifyReminderFlags_ = (MODIFY_REMINDER_FLAGS)dlsym(extensionWrapperHandle_, "ModifyReminderFlags");
81 if (modifyReminderFlags_ == nullptr) {
82 ANS_LOGE("extension wrapper modifyReminderFlags symbol failed, error: %{public}s", dlerror());
83 return;
84 }
85 #endif
86 #ifdef ENABLE_ANS_AGGREGATION
87 std::string aggregateConfig = NotificationPreferences::GetInstance()->GetAdditionalConfig("AGGREGATE_CONFIG");
88 if (!aggregateConfig.empty()) {
89 syncAdditionConfig_("AGGREGATE_CONFIG", aggregateConfig);
90 }
91 if (initSummary_ != nullptr) {
92 initSummary_(UpdateUnifiedGroupInfo);
93 }
94 #endif
95 ANS_LOGD("extension wrapper init success");
96 }
97
CheckIfSetlocalSwitch()98 void ExtensionWrapper::CheckIfSetlocalSwitch()
99 {
100 ANS_LOGD("CheckIfSetlocalSwitch enter");
101 if (extensionWrapperHandle_ == nullptr) {
102 return;
103 }
104 if (!isRegisterDataSettingObserver) {
105 RegisterDataSettingObserver();
106 isRegisterDataSettingObserver = true;
107 }
108 std::string enable = "";
109 AdvancedNotificationService::GetInstance()->GetUnifiedGroupInfoFromDb(enable);
110 SetlocalSwitch(enable);
111 }
112
SetlocalSwitch(std::string & enable)113 void ExtensionWrapper::SetlocalSwitch(std::string &enable)
114 {
115 if (setLocalSwitch_ == nullptr) {
116 return;
117 }
118 bool status = (enable == "false" ? false : true);
119 setLocalSwitch_(status);
120 }
121
RegisterDataSettingObserver()122 void ExtensionWrapper::RegisterDataSettingObserver()
123 {
124 ANS_LOGD("ExtensionWrapper::RegisterDataSettingObserver enter");
125 sptr<AdvancedAggregationDataRoamingObserver> aggregationRoamingObserver;
126 if (aggregationRoamingObserver == nullptr) {
127 aggregationRoamingObserver = new (std::nothrow) AdvancedAggregationDataRoamingObserver();
128 }
129
130 if (aggregationRoamingObserver == nullptr) {
131 return;
132 }
133
134 Uri dataEnableUri(SETTINGS_DATA_UNIFIED_GROUP_ENABLE_URI);
135 AdvancedDatashareObserver::GetInstance().RegisterSettingsObserver(dataEnableUri, aggregationRoamingObserver);
136 }
137
SyncAdditionConfig(const std::string & key,const std::string & value)138 ErrCode ExtensionWrapper::SyncAdditionConfig(const std::string& key, const std::string& value)
139 {
140 if (syncAdditionConfig_ == nullptr) {
141 ANS_LOGE("syncAdditionConfig wrapper symbol failed");
142 return 0;
143 }
144 return syncAdditionConfig_(key, value);
145 }
146
UpdateByCancel(const std::vector<sptr<Notification>> & notifications,int deleteReason)147 void ExtensionWrapper::UpdateByCancel(const std::vector<sptr<Notification>>& notifications, int deleteReason)
148 {
149 if (updateByCancel_ == nullptr) {
150 return;
151 }
152 int32_t deleteType = convertToDelType(deleteReason);
153 updateByCancel_(notifications, deleteType);
154 }
155
GetUnifiedGroupInfo(const sptr<NotificationRequest> & request)156 ErrCode ExtensionWrapper::GetUnifiedGroupInfo(const sptr<NotificationRequest> &request)
157 {
158 if (getUnifiedGroupInfo_ == nullptr) {
159 return 0;
160 }
161 return getUnifiedGroupInfo_(request);
162 }
163
ReminderControl(const std::string & bundleName)164 int32_t ExtensionWrapper::ReminderControl(const std::string &bundleName)
165 {
166 if (reminderControl_ == nullptr) {
167 ANS_LOGE("ReminderControl wrapper symbol failed");
168 return 0;
169 }
170 return reminderControl_(bundleName);
171 }
172
BannerControl(const std::string & bundleName)173 int32_t ExtensionWrapper::BannerControl(const std::string &bundleName)
174 {
175 if (bannerControl_ == nullptr) {
176 ANS_LOGE("ReminderControl wrapper symbol failed");
177 return -1;
178 }
179 return bannerControl_(bundleName);
180 }
181
182 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
ModifyReminderFlags(const sptr<NotificationRequest> & request)183 bool ExtensionWrapper::ModifyReminderFlags(const sptr<NotificationRequest> &request)
184 {
185 if (modifyReminderFlags_ == nullptr) {
186 ANS_LOGE("ModifyReminderFlags wrapper symbol failed");
187 return false;
188 }
189 return modifyReminderFlags_(request);
190 }
191 #endif
192
LocalControl(const sptr<NotificationRequest> & request)193 __attribute__((no_sanitize("cfi"))) int32_t ExtensionWrapper::LocalControl(const sptr<NotificationRequest> &request)
194 {
195 if (localControl_ == nullptr) {
196 ANS_LOGE("LocalControl wrapper symbol failed");
197 return 0;
198 }
199 return localControl_(request);
200 }
201
UpdateByBundle(const std::string bundleName,int deleteReason)202 void ExtensionWrapper::UpdateByBundle(const std::string bundleName, int deleteReason)
203 {
204 if (updateByBundle_ == nullptr) {
205 return;
206 }
207 int32_t deleteType = convertToDelType(deleteReason);
208 updateByBundle_(bundleName, deleteType);
209 }
210
convertToDelType(int32_t deleteReason)211 int32_t ExtensionWrapper::convertToDelType(int32_t deleteReason)
212 {
213 int32_t delType = ACTIVE_DELETE;
214 switch (deleteReason) {
215 case NotificationConstant::PACKAGE_CHANGED_REASON_DELETE:
216 case NotificationConstant::USER_REMOVED_REASON_DELETE:
217 case NotificationConstant::DISABLE_SLOT_REASON_DELETE:
218 case NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE:
219 delType = PASSITIVE_DELETE;
220 break;
221 default:
222 delType = ACTIVE_DELETE;
223 }
224
225 ANS_LOGD("convertToDelType from delete reason %d to delete type %d", deleteReason, delType);
226 return delType;
227 }
228 } // namespace OHOS::Notification
229