• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SYMBOL_EXPORT
29 #define SYMBOL_EXPORT __attribute__ ((visibility("default")))
30 #endif
31 
32 namespace OHOS::Notification {
33 const std::string EXTENTION_WRAPPER_PATH = "libans_ext.z.so";
34 const int32_t ACTIVE_DELETE = 0;
35 const int32_t PASSITIVE_DELETE = 1;
36 static constexpr const char *SETTINGS_DATA_UNIFIED_GROUP_ENABLE_URI =
37     "datashare:///com.ohos.settingsdata/entry/settingsdata/"
38     "USER_SETTINGSDATA_SECURE_100?Proxy=true&key=unified_group_enable";
ExtensionWrapper()39 ExtensionWrapper::ExtensionWrapper()
40 {
41     InitExtentionWrapper();
42 }
43 ExtensionWrapper::~ExtensionWrapper() = default;
44 
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
UpdateUnifiedGroupInfo(const std::string & key,std::shared_ptr<NotificationUnifiedGroupInfo> & groupInfo)50 void UpdateUnifiedGroupInfo(const std::string &key, std::shared_ptr<NotificationUnifiedGroupInfo> &groupInfo)
51 {
52     AdvancedNotificationService::GetInstance()->UpdateUnifiedGroupInfo(key, groupInfo);
53 }
54 
55 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
GetAdditionalConfig(const std::string & key,std::string & value)56 SYMBOL_EXPORT void GetAdditionalConfig(const std::string &key, std::string &value)
57 {
58     value = NotificationPreferences::GetInstance()->GetAdditionalConfig(key);
59     ANS_LOGD("GetAdditionalConfig SYMBOL_EXPORT valiue %{public}s", value.c_str());
60 }
61 
SetKvToDb(const std::string & key,const std::string & value,const int32_t & userId)62 SYMBOL_EXPORT int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId)
63 {
64     return NotificationPreferences::GetInstance()->SetKvToDb(key, value, userId);
65 }
66 
GetKvFromDb(const std::string & key,std::string & value,const int32_t & userId,int32_t & retCode)67 SYMBOL_EXPORT int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId, int32_t& retCode)
68 {
69     return NotificationPreferences::GetInstance()->GetKvFromDb(key, value, userId, retCode);
70 }
71 
GetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t & slotFlags)72 SYMBOL_EXPORT ErrCode GetNotificationSlotFlagsForBundle(const sptr<NotificationBundleOption> &bundleOption,
73     uint32_t &slotFlags)
74 {
75     return NotificationPreferences::GetInstance()->GetNotificationSlotFlagsForBundle(bundleOption, slotFlags);
76 }
77 #endif
78 
79 #ifdef __cplusplus
80 }
81 #endif
82 
InitExtentionWrapper()83 void ExtensionWrapper::InitExtentionWrapper()
84 {
85     extensionWrapperHandle_ = dlopen(EXTENTION_WRAPPER_PATH.c_str(), RTLD_NOW);
86     if (extensionWrapperHandle_ == nullptr) {
87         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
88         return;
89     }
90 
91     syncAdditionConfig_ = (SYNC_ADDITION_CONFIG)dlsym(extensionWrapperHandle_, "SyncAdditionConfig");
92     if (syncAdditionConfig_ == nullptr) {
93         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
94         return;
95     }
96 #ifdef ENABLE_ANS_ADDITIONAL_CONTROL
97     localControl_ = (LOCAL_CONTROL)dlsym(extensionWrapperHandle_, "LocalControl");
98     reminderControl_ = (REMINDER_CONTROL)dlsym(extensionWrapperHandle_, "ReminderControl");
99     bannerControl_ = (BANNER_CONTROL)dlsym(extensionWrapperHandle_, "BannerControl");
100     if (bannerControl_ == nullptr || localControl_ == nullptr || reminderControl_ == nullptr) {
101         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
102         return;
103     }
104 
105     std::string ctrlConfig = NotificationPreferences::GetInstance()->GetAdditionalConfig("NOTIFICATION_CTL_LIST_PKG");
106     if (!ctrlConfig.empty()) {
107         syncAdditionConfig_("NOTIFICATION_CTL_LIST_PKG", ctrlConfig);
108     }
109 #endif
110 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
111     isPrivilegeMessage_ = (IS_PRIVILEGE_MESSAGE)dlsym(extensionWrapperHandle_, "IsPrivilegeMessage");
112     if (isPrivilegeMessage_ == nullptr) {
113         ANS_LOGE("extension wrapper isPrivilegeMessage_ symbol failed, error: %{public}s", dlerror());
114         return;
115     }
116     handlePrivilegeMessage_ = (HANDLE_PRIVILEGE_MESSAGE)dlsym(extensionWrapperHandle_, "HandlePrivilegeMessage");
117     if (handlePrivilegeMessage_ == nullptr) {
118         ANS_LOGE("extension wrapper handlePrivilegeMessage_ symbol failed, error: %{public}s", dlerror());
119         return;
120     }
121     getPrivilegeDialogPopped_ = (GET_PRIVILEGE_DIALOG_POPPED)dlsym(extensionWrapperHandle_, "GetPrivilegeDialogPopped");
122     if (getPrivilegeDialogPopped_ == nullptr) {
123         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
124         return;
125     }
126     setDialogOpenSuccessTimeStamp_ =
127         (SET_DIALOG_OPENSUCCESS_TIMESTAMP)dlsym(extensionWrapperHandle_, "SetDialogOpenSuccessTimeStamp");
128     if (setDialogOpenSuccessTimeStamp_ == nullptr) {
129         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
130         return;
131     }
132 
133     setDialogOpenSuccessTimeInterval_ =
134         (SET_DIALOG_OPENSUCCESS_TIMEINTERVAL)dlsym(extensionWrapperHandle_, "SetDialogOpenSuccessTimeInterval");
135     if (setDialogOpenSuccessTimeInterval_ == nullptr) {
136         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
137         return;
138     }
139 #endif
140 #ifdef ENABLE_ANS_AGGREGATION
141     std::string aggregateConfig = NotificationPreferences::GetInstance()->GetAdditionalConfig("AGGREGATE_CONFIG");
142     if (!aggregateConfig.empty()) {
143         syncAdditionConfig_("AGGREGATE_CONFIG", aggregateConfig);
144     }
145     if (initSummary_ != nullptr) {
146         initSummary_(UpdateUnifiedGroupInfo);
147     }
148 #endif
149     notificationDialogControl_ = (NOTIFICATIONDIALOGCONTROL)dlsym(extensionWrapperHandle_, "NotificationDialogControl");
150     if (notificationDialogControl_ == nullptr) {
151         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
152         return;
153     }
154     verifyCloudCapability_ = (VERIFY_CLOUD_CAPABILITY)dlsym(extensionWrapperHandle_, "VerifyCloudCapability");
155     if (verifyCloudCapability_ == nullptr) {
156         ANS_LOGE("extension wrapper symbol failed, error: %{public}s", dlerror());
157         return;
158     }
159     ANS_LOGI("extension wrapper init success");
160 }
161 
CheckIfSetlocalSwitch()162 void ExtensionWrapper::CheckIfSetlocalSwitch()
163 {
164     ANS_LOGD("CheckIfSetlocalSwitch enter");
165     if (extensionWrapperHandle_ == nullptr) {
166         return;
167     }
168     if (!isRegisterDataSettingObserver) {
169         RegisterDataSettingObserver();
170         isRegisterDataSettingObserver = true;
171     }
172     std::string enable = "";
173     AdvancedNotificationService::GetInstance()->GetUnifiedGroupInfoFromDb(enable);
174     SetlocalSwitch(enable);
175 }
176 
SetlocalSwitch(std::string & enable)177 void ExtensionWrapper::SetlocalSwitch(std::string &enable)
178 {
179     if (setLocalSwitch_ == nullptr) {
180         return;
181     }
182     bool status = (enable == "false" ? false : true);
183     setLocalSwitch_(status);
184 }
185 
RegisterDataSettingObserver()186 void ExtensionWrapper::RegisterDataSettingObserver()
187 {
188     ANS_LOGD("ExtensionWrapper::RegisterDataSettingObserver enter");
189     sptr<AdvancedAggregationDataRoamingObserver> aggregationRoamingObserver;
190     if (aggregationRoamingObserver == nullptr) {
191         aggregationRoamingObserver = new (std::nothrow) AdvancedAggregationDataRoamingObserver();
192     }
193 
194     if (aggregationRoamingObserver == nullptr) {
195         return;
196     }
197 
198     Uri dataEnableUri(SETTINGS_DATA_UNIFIED_GROUP_ENABLE_URI);
199     AdvancedDatashareObserver::GetInstance().RegisterSettingsObserver(dataEnableUri, aggregationRoamingObserver);
200 }
201 
SyncAdditionConfig(const std::string & key,const std::string & value)202 ErrCode ExtensionWrapper::SyncAdditionConfig(const std::string& key, const std::string& value)
203 {
204     if (syncAdditionConfig_ == nullptr) {
205         ANS_LOGE("syncAdditionConfig wrapper symbol failed");
206         return 0;
207     }
208     return syncAdditionConfig_(key, value);
209 }
210 
UpdateByCancel(const std::vector<sptr<Notification>> & notifications,int deleteReason)211 void ExtensionWrapper::UpdateByCancel(const std::vector<sptr<Notification>>& notifications, int deleteReason)
212 {
213     if (updateByCancel_ == nullptr) {
214         return;
215     }
216     int32_t deleteType = convertToDelType(deleteReason);
217     updateByCancel_(notifications, deleteType);
218 }
219 
GetUnifiedGroupInfo(const sptr<NotificationRequest> & request)220 ErrCode ExtensionWrapper::GetUnifiedGroupInfo(const sptr<NotificationRequest> &request)
221 {
222     if (getUnifiedGroupInfo_ == nullptr) {
223         return 0;
224     }
225     return getUnifiedGroupInfo_(request);
226 }
227 
ReminderControl(const std::string & bundleName)228 int32_t ExtensionWrapper::ReminderControl(const std::string &bundleName)
229 {
230     if (reminderControl_ == nullptr) {
231         ANS_LOGE("ReminderControl wrapper symbol failed");
232         return 0;
233     }
234     return reminderControl_(bundleName);
235 }
236 
BannerControl(const std::string & bundleName)237 int32_t ExtensionWrapper::BannerControl(const std::string &bundleName)
238 {
239     if (bannerControl_ == nullptr) {
240         ANS_LOGE("ReminderControl wrapper symbol failed");
241         return -1;
242     }
243     return bannerControl_(bundleName);
244 }
245 
VerifyCloudCapability(const int32_t & uid,const std::string & capability)246 int32_t ExtensionWrapper::VerifyCloudCapability(const int32_t &uid, const std::string &capability)
247 {
248     if (verifyCloudCapability_ == nullptr) {
249         ANS_LOGE("VerifyCloudCapability wrapper symbol failed");
250         return -1;
251     }
252     return verifyCloudCapability_(uid, capability);
253 }
254 
255 
256 #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER
IsPrivilegeMessage(const sptr<NotificationRequest> & request)257 bool ExtensionWrapper::IsPrivilegeMessage(const sptr<NotificationRequest> &request)
258 {
259     if (isPrivilegeMessage_ == nullptr) {
260         ANS_LOGE("IsPrivilegeMessage wrapper symbol failed");
261         return false;
262     }
263     return isPrivilegeMessage_(request);
264 }
265 
HandlePrivilegeMessage(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationRequest> & request,bool isAgentController)266 void ExtensionWrapper::HandlePrivilegeMessage(const sptr<NotificationBundleOption>& bundleOption,
267     const sptr<NotificationRequest> &request, bool isAgentController)
268 {
269     if (handlePrivilegeMessage_ == nullptr) {
270         ANS_LOGE("HandlePrivilegeMessage wrapper symbol failed");
271         return;
272     }
273     return handlePrivilegeMessage_(bundleOption, request, isAgentController);
274 }
275 
GetPrivilegeDialogPopped(const sptr<NotificationBundleOption> & bundleOption,const int32_t & userId)276 bool ExtensionWrapper::GetPrivilegeDialogPopped(const sptr<NotificationBundleOption>& bundleOption,
277     const int32_t &userId)
278 {
279     if (getPrivilegeDialogPopped_ == nullptr) {
280         ANS_LOGE("GetPrivilegeDialogPopped wrapper symbol failed.");
281         return -1;
282     }
283     return getPrivilegeDialogPopped_(bundleOption, userId);
284 }
285 
SetDialogOpenSuccessTimeStamp(const sptr<NotificationBundleOption> & bundleOption,const int32_t & userId)286 bool ExtensionWrapper::SetDialogOpenSuccessTimeStamp(const sptr<NotificationBundleOption>& bundleOption,
287     const int32_t &userId)
288 {
289     if (setDialogOpenSuccessTimeStamp_ == nullptr) {
290         ANS_LOGE("SetDialogOpenSuccessTimeStamp wrapper symbol failed.");
291         return -1;
292     }
293     return setDialogOpenSuccessTimeStamp_(bundleOption, userId);
294 }
295 
SetDialogOpenSuccessTimeInterval(const sptr<NotificationBundleOption> & bundleOption,const int32_t & userId)296 bool ExtensionWrapper::SetDialogOpenSuccessTimeInterval(const sptr<NotificationBundleOption>& bundleOption,
297     const int32_t &userId)
298 {
299     if (setDialogOpenSuccessTimeInterval_ == nullptr) {
300         ANS_LOGE("SetDialogOpenSuccessTimeInterval wrapper symbol failed.");
301         return -1;
302     }
303     return setDialogOpenSuccessTimeInterval_(bundleOption, userId);
304 }
305 #endif
306 
LocalControl(const sptr<NotificationRequest> & request)307 __attribute__((no_sanitize("cfi"))) int32_t ExtensionWrapper::LocalControl(const sptr<NotificationRequest> &request)
308 {
309     if (localControl_ == nullptr) {
310         ANS_LOGE("LocalControl wrapper symbol failed");
311         return 0;
312     }
313     return localControl_(request);
314 }
315 
UpdateByBundle(const std::string bundleName,int deleteReason)316 void ExtensionWrapper::UpdateByBundle(const std::string bundleName, int deleteReason)
317 {
318     if (updateByBundle_ == nullptr) {
319         return;
320     }
321     int32_t deleteType = convertToDelType(deleteReason);
322     updateByBundle_(bundleName, deleteType);
323 }
324 
NotificationDialogControl()325 bool ExtensionWrapper::NotificationDialogControl()
326 {
327     if (notificationDialogControl_ == nullptr) {
328         ANS_LOGE("isSampleDevice_ is null");
329         return true;
330     }
331     bool result = notificationDialogControl_();
332     ANS_LOGI("result = %{public}d", result);
333     return result;
334 }
335 
convertToDelType(int32_t deleteReason)336 int32_t ExtensionWrapper::convertToDelType(int32_t deleteReason)
337 {
338     int32_t delType = ACTIVE_DELETE;
339     switch (deleteReason) {
340         case NotificationConstant::PACKAGE_CHANGED_REASON_DELETE:
341         case NotificationConstant::USER_REMOVED_REASON_DELETE:
342         case NotificationConstant::DISABLE_SLOT_REASON_DELETE:
343         case NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE:
344             delType = PASSITIVE_DELETE;
345             break;
346         default:
347             delType = ACTIVE_DELETE;
348     }
349 
350     ANS_LOGD("convertToDelType from delete reason %d to delete type %d", deleteReason, delType);
351     return delType;
352 }
353 } // namespace OHOS::Notification
354