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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H 18 19 #include <string> 20 21 #include "singleton.h" 22 23 #include "event_handler.h" 24 #include "event_runner.h" 25 #include "ffrt.h" 26 27 #include "distributed_database.h" 28 #include "distributed_database_callback.h" 29 #include "distributed_device_callback.h" 30 #include "notification_request.h" 31 32 namespace OHOS { 33 namespace Notification { 34 class DistributedNotificationManager : public DelayedSingleton<DistributedNotificationManager> { 35 public: 36 /** 37 * @brief Distributed notification callback function for remote device. 38 */ 39 struct IDistributedCallback { 40 std::function<void( 41 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request)> 42 OnPublish; 43 std::function<void( 44 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request)> 45 OnUpdate; 46 std::function<void( 47 const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id)> 48 OnDelete; 49 }; 50 51 /** 52 * @brief Publishes a local notification to remote device. 53 * 54 * @param bundleName Indicates the bundle name of the application whose notifications are to be publish. 55 * @param label Indicates the label of the notifications. 56 * @param id Indicates the bundle uid of the application whose notifications are to be publish. 57 * @param request Indicates the NotificationRequest object for setting the notification content. 58 * @return ErrCode Returns the publish result. 59 */ 60 ErrCode Publish( 61 const std::string &bundleName, const std::string &label, int32_t id, const sptr<NotificationRequest> &request); 62 63 /** 64 * @brief Updates infomation of local notification to remote device. 65 * 66 * @param bundleName Indicates the bundle name of the application whose notifications are to be update. 67 * @param label Indicates the label of the notifications. 68 * @param id Indicates the bundle uid of the application whose notifications are to be update. 69 * @param request Indicates the NotificationRequest object for setting the notification content. 70 * @return ErrCode Returns the update result. 71 */ 72 ErrCode Update( 73 const std::string &bundleName, const std::string &label, int32_t id, const sptr<NotificationRequest> &request); 74 75 /** 76 * @brief Removes a local notification. 77 * 78 * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. 79 * @param label Indicates the label of the notifications. 80 * @param id Indicates the bundle uid of the application whose notifications are to be remove. 81 * @return ErrCode Returns the remove result. 82 */ 83 ErrCode Delete(const std::string &bundleName, const std::string &label, int32_t id); 84 85 /** 86 * @brief Removes a remote notification. 87 * 88 * @param deviceId Indicates the ID of the device. 89 * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. 90 * @param label Indicates the label of the notifications. 91 * @param id Indicates the bundle uid of the application whose notifications are to be remove. 92 * @return ErrCode Returns the remove result. 93 */ 94 ErrCode DeleteRemoteNotification( 95 const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); 96 97 /** 98 * @brief Register callback of distributed notification changed. 99 * 100 * @param callback Indicates the callback structure 101 * @return ErrCode Returns the register result. 102 */ 103 ErrCode RegisterCallback(const IDistributedCallback &callback); 104 105 /** 106 * @brief Unregister Callback of Distributed notification changed. 107 * 108 * @return ErrCode Returns the unregister result. 109 */ 110 ErrCode UngegisterCallback(void); 111 112 /** 113 * @brief Get all distributed notification in database. 114 * 115 * @param requestList Indicates the list of NotificationRequest object for setting the notification content. 116 * @return ErrCode Returns Get all distributed notification result. 117 */ 118 ErrCode GetCurrentDistributedNotification(std::vector<sptr<NotificationRequest>> &requestList); 119 120 /** 121 * @brief Get local device info. 122 * 123 * @param deviceInfo Indicates the infomation of local device. 124 * @return ErrCode Returns get device infomation result. 125 */ 126 ErrCode GetLocalDeviceInfo(DistributedDatabase::DeviceInfo &deviceInfo); 127 128 /** 129 * @brief Obtains the death event of the Distributed KvStore service. 130 */ 131 ErrCode OnDistributedKvStoreDeathRecipient(); 132 133 /** 134 * @brief Reset ffrt queue 135 */ 136 void ResetFfrtQueue(); 137 138 private: 139 void OnDatabaseInsert(const std::string &deviceId, const std::string &key, const std::string &value); 140 void OnDatabaseUpdate(const std::string &deviceId, const std::string &key, const std::string &value); 141 void OnDatabaseDelete(const std::string &deviceId, const std::string &key, const std::string &value); 142 void OnDeviceConnected(const std::string &deviceId); 143 void OnDeviceDisconnected(const std::string &deviceId); 144 145 private: 146 struct ResolveKey { 147 std::string deviceId; 148 std::string bundleName; 149 std::string label; 150 int32_t id = 0; 151 }; 152 153 void GenerateDistributedKey(const std::string &deviceId, const std::string &bundleName, const std::string &label, 154 int32_t id, std::string &key); 155 bool GenerateLocalDistributedKey( 156 const std::string &bundleName, const std::string &label, int32_t id, std::string &key); 157 bool ResolveDistributedKey(const std::string &key, ResolveKey &resolveKey); 158 bool GetDeviceIdFromKey(const std::string &key, std::string &deviceId); 159 bool CheckDeviceId(const std::string &deviceId, const std::string &key); 160 161 bool PublishCallback( 162 const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request); 163 bool UpdateCallback(const std::string &deviceId, const std::string &bundleName, sptr<NotificationRequest> &request); 164 bool DeleteCallback( 165 const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); 166 167 private: 168 std::shared_ptr<OHOS::AppExecFwk::EventRunner> runner_ = nullptr; 169 std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_ = nullptr; 170 std::shared_ptr<DistributedDatabase> database_ = nullptr; 171 172 std::shared_ptr<DistributedDatabaseCallback> databaseCb_; 173 std::shared_ptr<DistributedDeviceCallback> deviceCb_; 174 IDistributedCallback callback_ = {0}; 175 std::shared_ptr<ffrt::queue> distributedQueue_ = nullptr; 176 177 DECLARE_DELAYED_SINGLETON(DistributedNotificationManager); 178 DISALLOW_COPY_AND_MOVE(DistributedNotificationManager); 179 }; 180 } // namespace Notification 181 } // namespace OHOS 182 183 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H