• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "distributed_notification_manager.h"
17 
18 #include <vector>
19 
20 #include "ans_inner_errors.h"
21 #include "ans_log_wrapper.h"
22 #include "ans_watchdog.h"
23 #include "hitrace_meter.h"
24 
25 namespace OHOS {
26 namespace Notification {
27 namespace {
28 const std::string DELIMITER = "|";
29 }  // namespace
30 
DistributedNotificationManager()31 DistributedNotificationManager::DistributedNotificationManager()
32 {
33     runner_ = OHOS::AppExecFwk::EventRunner::Create("NotificationDistributedMgr");
34     handler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(runner_);
35     AnsWatchdog::AddHandlerThread(handler_, runner_);
36 
37     DistributedDatabaseCallback::IDatabaseChange databaseCallback = {
38         .OnInsert = std::bind(&DistributedNotificationManager::OnDatabaseInsert,
39             this,
40             std::placeholders::_1,
41             std::placeholders::_2,
42             std::placeholders::_3),
43         .OnUpdate = std::bind(&DistributedNotificationManager::OnDatabaseUpdate,
44             this,
45             std::placeholders::_1,
46             std::placeholders::_2,
47             std::placeholders::_3),
48         .OnDelete = std::bind(&DistributedNotificationManager::OnDatabaseDelete,
49             this,
50             std::placeholders::_1,
51             std::placeholders::_2,
52             std::placeholders::_3),
53     };
54     databaseCb_ = std::make_shared<DistributedDatabaseCallback>(databaseCallback);
55 
56     DistributedDeviceCallback::IDeviceChange deviceCallback = {
57         .OnConnected = std::bind(&DistributedNotificationManager::OnDeviceConnected, this, std::placeholders::_1),
58         .OnDisconnected = std::bind(&DistributedNotificationManager::OnDeviceDisconnected, this, std::placeholders::_1),
59     };
60     deviceCb_ = std::make_shared<DistributedDeviceCallback>(deviceCallback);
61 
62     database_ = std::make_shared<DistributedDatabase>(databaseCb_, deviceCb_);
63     if (database_ == nullptr) {
64         ANS_LOGE("database_ is nullptr.");
65         return;
66     }
67 }
68 
~DistributedNotificationManager()69 DistributedNotificationManager::~DistributedNotificationManager()
70 {
71     handler_->PostSyncTask(std::bind([&]() { callback_ = {}; }), AppExecFwk::EventHandler::Priority::HIGH);
72 }
73 
GenerateDistributedKey(const std::string & deviceId,const std::string & bundleName,const std::string & label,int32_t id,std::string & key)74 void DistributedNotificationManager::GenerateDistributedKey(
75     const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, std::string &key)
76 {
77     key = deviceId + DELIMITER + bundleName + DELIMITER + label + DELIMITER + ToString(id);
78 }
79 
GenerateLocalDistributedKey(const std::string & bundleName,const std::string & label,int32_t id,std::string & key)80 bool DistributedNotificationManager::GenerateLocalDistributedKey(
81     const std::string &bundleName, const std::string &label, int32_t id, std::string &key)
82 {
83     std::string deviceId;
84     if (!database_->GetLocalDeviceId(deviceId)) {
85         return false;
86     }
87 
88     GenerateDistributedKey(deviceId, bundleName, label, id, key);
89     return true;
90 }
91 
ResolveDistributedKey(const std::string & key,ResolveKey & resolveKey)92 bool DistributedNotificationManager::ResolveDistributedKey(const std::string &key, ResolveKey &resolveKey)
93 {
94     std::size_t deviceIdPosition = 0;
95     std::size_t deviceIdEndPosition = key.find(DELIMITER, deviceIdPosition);
96     if (deviceIdEndPosition == std::string::npos) {
97         return false;
98     }
99     std::size_t bundleNamePosition = deviceIdEndPosition + DELIMITER.size();
100     std::size_t bundleNameEndPosition = key.find(DELIMITER, bundleNamePosition);
101     if (bundleNameEndPosition == std::string::npos) {
102         return false;
103     }
104     std::size_t labelPosition = bundleNameEndPosition + DELIMITER.size();
105     std::size_t labelEndPosition = key.find_last_of(DELIMITER) - DELIMITER.size() + 1;
106     if (labelEndPosition < labelPosition) {
107         return false;
108     }
109     std::size_t idPosition = key.find_last_of(DELIMITER) + DELIMITER.size();
110 
111     resolveKey.deviceId = key.substr(deviceIdPosition, deviceIdEndPosition - deviceIdPosition);
112     resolveKey.bundleName = key.substr(bundleNamePosition, bundleNameEndPosition - bundleNamePosition);
113     resolveKey.label = key.substr(labelPosition, labelEndPosition - labelPosition);
114     resolveKey.id = atoi(&key[idPosition]);
115 
116     return true;
117 }
118 
CheckDeviceId(const std::string & deviceId,const std::string & key)119 bool DistributedNotificationManager::CheckDeviceId(const std::string &deviceId, const std::string &key)
120 {
121     ResolveKey resolveKey;
122     if (!ResolveDistributedKey(key, resolveKey)) {
123         ANS_LOGE("key <%{public}s> is invalid.", key.c_str());
124         return false;
125     }
126 
127     return deviceId == resolveKey.deviceId;
128 }
129 
OnDatabaseInsert(const std::string & deviceId,const std::string & key,const std::string & value)130 void DistributedNotificationManager::OnDatabaseInsert(
131     const std::string &deviceId, const std::string &key, const std::string &value)
132 {
133     ANS_LOGD("%{public}s", __FUNCTION__);
134     handler_->PostTask(std::bind([=]() {
135         if (!CheckDeviceId(deviceId, key)) {
136             ANS_LOGD("device id are not the same. deviceId:%{public}s key:%{public}s", deviceId.c_str(), key.c_str());
137         }
138 
139         ResolveKey resolveKey;
140         if (!ResolveDistributedKey(key, resolveKey)) {
141             ANS_LOGE("key <%{public}s> is invalid.", key.c_str());
142             return;
143         }
144 
145         sptr<NotificationRequest> request =
146             NotificationJsonConverter::ConvertFromJsonString<NotificationRequest>(value);
147         if (request == nullptr) {
148             ANS_LOGE("convert json to request failed. key:%{public}s", key.c_str());
149             return;
150         }
151 
152         PublishCallback(resolveKey.deviceId, resolveKey.bundleName, request);
153     }));
154 }
155 
OnDatabaseUpdate(const std::string & deviceId,const std::string & key,const std::string & value)156 void DistributedNotificationManager::OnDatabaseUpdate(
157     const std::string &deviceId, const std::string &key, const std::string &value)
158 {
159     ANS_LOGD("%{public}s", __FUNCTION__);
160     handler_->PostTask(std::bind([=]() {
161         if (!CheckDeviceId(deviceId, key)) {
162             ANS_LOGD("device id are not the same. deviceId:%{public}s key:%{public}s", deviceId.c_str(), key.c_str());
163         }
164 
165         ResolveKey resolveKey;
166         if (!ResolveDistributedKey(key, resolveKey)) {
167             ANS_LOGE("key <%{public}s> is invalid.", key.c_str());
168             return;
169         }
170 
171         sptr<NotificationRequest> request =
172             NotificationJsonConverter::ConvertFromJsonString<NotificationRequest>(value);
173         if (request == nullptr) {
174             ANS_LOGE("convert json to request failed. key:%{public}s", key.c_str());
175             return;
176         }
177 
178         UpdateCallback(resolveKey.deviceId, resolveKey.bundleName, request);
179     }));
180 }
181 
OnDatabaseDelete(const std::string & deviceId,const std::string & key,const std::string & value)182 void DistributedNotificationManager::OnDatabaseDelete(
183     const std::string &deviceId, const std::string &key, const std::string &value)
184 {
185     ANS_LOGD("%{public}s", __FUNCTION__);
186     handler_->PostTask(std::bind([=]() {
187         if (!CheckDeviceId(deviceId, key)) {
188             ANS_LOGD("device id are not the same. deviceId:%{public}s key:%{public}s", deviceId.c_str(), key.c_str());
189         }
190 
191         ResolveKey resolveKey;
192         if (!ResolveDistributedKey(key, resolveKey)) {
193             ANS_LOGE("key <%{public}s> is invalid.", key.c_str());
194             return;
195         }
196 
197         DeleteCallback(resolveKey.deviceId, resolveKey.bundleName, resolveKey.label, resolveKey.id);
198     }));
199 }
200 
OnDeviceConnected(const std::string & deviceId)201 void DistributedNotificationManager::OnDeviceConnected(const std::string &deviceId)
202 {
203     ANS_LOGD("%{public}s", __FUNCTION__);
204     handler_->PostTask(std::bind([=]() {
205         if (database_ == nullptr) {
206             ANS_LOGE("OnDeviceConnected failed: database is null");
207             return;
208         }
209         if (!database_->OnDeviceConnected()) {
210             ANS_LOGE("OnDeviceConnected failed.");
211         }
212     }));
213 }
214 
OnDeviceDisconnected(const std::string & deviceId)215 void DistributedNotificationManager::OnDeviceDisconnected(const std::string &deviceId)
216 {
217     ANS_LOGD("%{public}s", __FUNCTION__);
218 
219     handler_->PostTask(std::bind([=]() {
220         std::string prefixKey = deviceId + DELIMITER;
221         std::vector<DistributedDatabase::Entry> entries;
222         if (!database_->GetEntriesFromDistributedDB(prefixKey, entries)) {
223             ANS_LOGE("GetEntriesFromDistributedDB failed.");
224             return;
225         }
226 
227         for (auto index : entries) {
228             ResolveKey resolveKey;
229             if (!ResolveDistributedKey(index.key.ToString(), resolveKey)) {
230                 ANS_LOGE("key <%{public}s> is invalid.", index.key.ToString().c_str());
231                 continue;
232             }
233 
234             DeleteCallback(resolveKey.deviceId, resolveKey.bundleName, resolveKey.label, resolveKey.id);
235         }
236 
237         database_->ClearDataByDevice(deviceId);
238 
239         std::vector<DistributedDatabase::DeviceInfo> deviceList;
240         if (database_->GetDeviceInfoList(deviceList) == ERR_OK && deviceList.empty()) {
241             database_->RecreateDistributedDB();
242         }
243     }));
244     return;
245 }
246 
PublishCallback(const std::string & deviceId,const std::string & bundleName,sptr<NotificationRequest> & request)247 bool DistributedNotificationManager::PublishCallback(
248     const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request)
249 {
250     ANS_LOGI("callback_.OnPublish start.");
251     if (callback_.OnPublish) {
252         callback_.OnPublish(deviceId, bundleName, request);
253     }
254     ANS_LOGI("callback_.OnPublish end.");
255 
256     return true;
257 }
258 
UpdateCallback(const std::string & deviceId,const std::string & bundleName,sptr<NotificationRequest> & request)259 bool DistributedNotificationManager::UpdateCallback(
260     const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request)
261 {
262     ANS_LOGI("callback_.OnUpdate start.");
263     if (callback_.OnUpdate) {
264         callback_.OnUpdate(deviceId, bundleName, request);
265     }
266     ANS_LOGI("callback_.OnUpdate end.");
267 
268     return true;
269 }
270 
DeleteCallback(const std::string & deviceId,const std::string & bundleName,const std::string & label,int32_t id)271 bool DistributedNotificationManager::DeleteCallback(
272     const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id)
273 {
274     ANS_LOGI("callback_.OnDelete start.");
275     if (callback_.OnDelete) {
276         callback_.OnDelete(deviceId, bundleName, label, id);
277     }
278     ANS_LOGI("callback_.OnDelete end.");
279 
280     return true;
281 }
282 
Publish(const std::string & bundleName,const std::string & label,int32_t id,const sptr<NotificationRequest> & request)283 ErrCode DistributedNotificationManager::Publish(
284     const std::string &bundleName, const std::string &label, int32_t id, const sptr<NotificationRequest> &request)
285 {
286     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
287     ANS_LOGI("%{public}s start", __FUNCTION__);
288     std::string key;
289     if (!GenerateLocalDistributedKey(bundleName, label, id, key)) {
290         ANS_LOGE("Generate distributed key failed.");
291         return ERR_ANS_DISTRIBUTED_GET_INFO_FAILED;
292     }
293 
294     std::string value;
295     if (!NotificationJsonConverter::ConvertToJsonString(request, value)) {
296         ANS_LOGE("convert request to json failed. key:%{public}s", key.c_str());
297         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
298     }
299 
300     if (!database_->PutToDistributedDB(key, value)) {
301         ANS_LOGE("put to distributed DB failed. key:%{public}s", key.c_str());
302         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
303     }
304 
305     return ERR_OK;
306 }
307 
Update(const std::string & bundleName,const std::string & label,int32_t id,const sptr<NotificationRequest> & request)308 ErrCode DistributedNotificationManager::Update(
309     const std::string &bundleName, const std::string &label, int32_t id, const sptr<NotificationRequest> &request)
310 {
311     ANS_LOGI("%{public}s start", __FUNCTION__);
312     std::string key;
313     if (!GenerateLocalDistributedKey(bundleName, label, id, key)) {
314         ANS_LOGE("Generate distributed key failed.");
315         return ERR_ANS_DISTRIBUTED_GET_INFO_FAILED;
316     }
317 
318     std::string value;
319     if (!NotificationJsonConverter::ConvertToJsonString(request, value)) {
320         ANS_LOGE("convert request to json failed. key:%{public}s", key.c_str());
321         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
322     }
323 
324     if (!database_->PutToDistributedDB(key, value)) {
325         ANS_LOGE("put to distributed DB failed. key:%{public}s", key.c_str());
326         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
327     }
328     return ERR_OK;
329 }
330 
Delete(const std::string & bundleName,const std::string & label,int32_t id)331 ErrCode DistributedNotificationManager::Delete(const std::string &bundleName, const std::string &label, int32_t id)
332 {
333     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
334     ANS_LOGI("%{public}s start", __FUNCTION__);
335     std::string key;
336     if (!GenerateLocalDistributedKey(bundleName, label, id, key)) {
337         ANS_LOGE("Generate distributed key failed.");
338         return ERR_ANS_DISTRIBUTED_GET_INFO_FAILED;
339     }
340 
341     if (!database_->DeleteToDistributedDB(key)) {
342         ANS_LOGE("delete to distributed DB failed. key:%{public}s", key.c_str());
343         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
344     }
345     return ERR_OK;
346 }
347 
DeleteRemoteNotification(const std::string & deviceId,const std::string & bundleName,const std::string & label,int32_t id)348 ErrCode DistributedNotificationManager::DeleteRemoteNotification(
349     const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id)
350 {
351     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
352     ANS_LOGI("%{public}s start", __FUNCTION__);
353 
354     std::string key;
355     GenerateDistributedKey(deviceId, bundleName, label, id, key);
356 
357     if (!database_->DeleteToDistributedDB(key)) {
358         ANS_LOGE("delete to distributed DB failed. key:%{public}s", key.c_str());
359         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
360     }
361     return ERR_OK;
362 }
363 
RegisterCallback(const IDistributedCallback & callback)364 ErrCode DistributedNotificationManager::RegisterCallback(const IDistributedCallback &callback)
365 {
366     ANS_LOGI("%{public}s start", __FUNCTION__);
367     handler_->PostSyncTask(std::bind([&]() { callback_ = callback; }), AppExecFwk::EventHandler::Priority::HIGH);
368     return ERR_OK;
369 }
370 
UngegisterCallback(void)371 ErrCode DistributedNotificationManager::UngegisterCallback(void)
372 {
373     ANS_LOGI("%{public}s start", __FUNCTION__);
374     handler_->PostSyncTask(std::bind([&]() { callback_ = {}; }), AppExecFwk::EventHandler::Priority::HIGH);
375     return ERR_OK;
376 }
377 
GetCurrentDistributedNotification(std::vector<sptr<NotificationRequest>> & requestList)378 ErrCode DistributedNotificationManager::GetCurrentDistributedNotification(
379     std::vector<sptr<NotificationRequest>> &requestList)
380 {
381     ANS_LOGI("%{public}s start", __FUNCTION__);
382     std::string prefixKey = "";
383     std::vector<DistributedDatabase::Entry> entries;
384     if (!database_->GetEntriesFromDistributedDB(prefixKey, entries)) {
385         ANS_LOGE("GetEntriesFromDistributedDB failed.");
386         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
387     }
388 
389     for (auto index : entries) {
390         ResolveKey resolveKey;
391         if (!ResolveDistributedKey(index.key.ToString(), resolveKey)) {
392             ANS_LOGE("key <%{public}s> is invalid.", index.key.ToString().c_str());
393             continue;
394         }
395 
396         sptr<NotificationRequest> request =
397             NotificationJsonConverter::ConvertFromJsonString<NotificationRequest>(index.value.ToString());
398         if (request == nullptr) {
399             ANS_LOGE("convert json to request failed. key:%{public}s", index.key.ToString().c_str());
400             continue;
401         }
402 
403         PublishCallback(resolveKey.deviceId, resolveKey.bundleName, request);
404     }
405 
406     return ERR_OK;
407 }
408 
GetLocalDeviceInfo(DistributedDatabase::DeviceInfo & deviceInfo)409 ErrCode DistributedNotificationManager::GetLocalDeviceInfo(DistributedDatabase::DeviceInfo &deviceInfo)
410 {
411     ANS_LOGI("%{public}s start", __FUNCTION__);
412 
413     if (!database_->GetLocalDeviceInfo(deviceInfo)) {
414         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
415     }
416 
417     return ERR_OK;
418 }
419 
OnDistributedKvStoreDeathRecipient()420 ErrCode DistributedNotificationManager::OnDistributedKvStoreDeathRecipient()
421 {
422     ANS_LOGI("%{public}s start", __FUNCTION__);
423 
424     database_ = std::make_shared<DistributedDatabase>(databaseCb_, deviceCb_);
425     if (database_ == nullptr) {
426         ANS_LOGE("database_ is nullptr.");
427         return ERR_ANS_NO_MEMORY;
428     }
429     if (!database_->RecreateDistributedDB()) {
430         ANS_LOGE("RecreateDistributedDB failed.");
431         return ERR_ANS_DISTRIBUTED_OPERATION_FAILED;
432     }
433     return ERR_OK;
434 }
435 }  // namespace Notification
436 }  // namespace OHOS