From 0849df0212e01b3d1ac24133aece90d061b5c8dd Mon Sep 17 00:00:00 2001 From: hupeixi Date: Tue, 26 Dec 2023 14:39:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E8=BF=9B=E7=A8=8B=E9=9C=80=E6=B1=82=EF=BC=8C?= =?UTF-8?q?=E6=96=B0=E5=A2=9ESA=E6=A1=86=E6=9E=B6.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hupeixi --- bundle.json | 2 + etc/init/BUILD.gn | 27 + etc/init/ans_distributed.cfg | 19 + frameworks/ans/BUILD.gn | 2 + frameworks/ans/src/notification_helper.cpp | 18 + .../core/common/include/ans_const_define.h | 2 - .../core/include/ans_distributed_interface.h | 114 ++++ .../core/include/ans_distributed_proxy.h | 104 ++++ .../core/include/ans_distributed_stub.h | 119 ++++ .../core/include/ans_manager_interface.h | 33 ++ frameworks/core/include/ans_manager_proxy.h | 33 ++ frameworks/core/include/ans_manager_stub.h | 36 ++ frameworks/core/include/ans_notification.h | 33 ++ ..._notification_service_ipc_interface_code.h | 5 +- frameworks/core/src/ans_distributed_proxy.cpp | 292 ++++++++++ frameworks/core/src/ans_distributed_stub.cpp | 301 ++++++++++ frameworks/core/src/ans_manager_proxy.cpp | 136 +++++ frameworks/core/src/ans_manager_stub.cpp | 135 +++++ frameworks/core/src/ans_notification.cpp | 30 + frameworks/core/test/unittest/BUILD.gn | 2 + .../ans_distributed_proxy_test/BUILD.gn | 55 ++ .../ans_distributed_proxy_unit_test.cpp | 436 ++++++++++++++ .../ans_distributed_stub_test/BUILD.gn | 55 ++ .../ans_distributed_stub_unit_test.cpp | 541 ++++++++++++++++++ .../ans_notification_branch_test.cpp | 10 + interfaces/inner_api/notification.h | 1 + interfaces/inner_api/notification_helper.h | 33 ++ interfaces/inner_api/notification_sorting.h | 1 + sa_profile/65978.json | 29 + sa_profile/BUILD.gn | 2 +- services/BUILD.gn | 4 + .../include/advanced_notification_service.h | 79 ++- services/ans/include/bundle_manager_helper.h | 2 - services/ans/include/interface_system_event.h | 2 - .../ans/include/notification_preferences.h | 62 ++ .../notification_preferences_database.h | 34 ++ .../include/notification_preferences_info.h | 68 +++ services/ans/include/notification_record.h | 2 - .../ans/src/advanced_notification_service.cpp | 394 +++++-------- services/ans/src/bundle_manager_helper.cpp | 2 - services/ans/src/notification_preferences.cpp | 115 ++++ .../src/notification_preferences_database.cpp | 97 ++++ .../ans/src/notification_preferences_info.cpp | 53 ++ services/ans/src/system_event_observer.cpp | 4 - .../advanced_notification_service_test.cpp | 282 ++++----- .../notification_preferences_test.cpp | 86 +++ services/distributed/BUILD.gn | 16 +- .../distributed_notification_manager.h | 36 +- .../distributed_notification_service.h | 120 ++++ ...distributed_notification_service_ability.h | 54 ++ .../src/distributed_notification_manager.cpp | 121 ++-- .../src/distributed_notification_service.cpp | 131 +++++ ...stributed_notification_service_ability.cpp | 48 ++ services/distributed/test/unittest/BUILD.gn | 70 ++- ...buted_notification_manager_branch_test.cpp | 71 +-- .../mock_distributed_database.cpp | 39 ++ .../distributed_notification_manager_test.cpp | 117 ++-- .../distributed_notification_service_test.cpp | 164 ++++++ .../distributed_preferences_branch_test.cpp | 12 - .../mock_distributed_preferences_database.cpp | 25 +- .../mock_distributed_flow_control.cpp | 10 +- .../mock_distributed_flow_control.cpp | 10 +- tools/dump/src/notification_shell_command.cpp | 10 - 63 files changed, 4268 insertions(+), 678 deletions(-) create mode 100644 etc/init/BUILD.gn create mode 100644 etc/init/ans_distributed.cfg create mode 100644 frameworks/core/include/ans_distributed_interface.h create mode 100644 frameworks/core/include/ans_distributed_proxy.h create mode 100644 frameworks/core/include/ans_distributed_stub.h create mode 100644 frameworks/core/src/ans_distributed_proxy.cpp create mode 100644 frameworks/core/src/ans_distributed_stub.cpp create mode 100644 frameworks/core/test/unittest/ans_distributed_proxy_test/BUILD.gn create mode 100644 frameworks/core/test/unittest/ans_distributed_proxy_test/ans_distributed_proxy_unit_test.cpp create mode 100644 frameworks/core/test/unittest/ans_distributed_stub_test/BUILD.gn create mode 100644 frameworks/core/test/unittest/ans_distributed_stub_test/ans_distributed_stub_unit_test.cpp create mode 100644 sa_profile/65978.json create mode 100644 services/distributed/include/distributed_notification_service.h create mode 100644 services/distributed/include/distributed_notification_service_ability.h create mode 100644 services/distributed/src/distributed_notification_service.cpp create mode 100644 services/distributed/src/distributed_notification_service_ability.cpp create mode 100644 services/distributed/test/unittest/distributed_notification_service_test.cpp diff --git a/bundle.json b/bundle.json index 67e9c3057..29e81c446 100644 --- a/bundle.json +++ b/bundle.json @@ -100,8 +100,10 @@ "//base/notification/distributed_notification_service/frameworks/js/napi:napi_reminder" ], "service_group": [ + "//base/notification/distributed_notification_service/etc/init:etc", "//base/notification/distributed_notification_service/sa_profile:ans_sa_profile", "//base/notification/distributed_notification_service/services:ans_service", + "//base/notification/distributed_notification_service/services:dns_service", "//base/notification/distributed_notification_service/tools:tools_shell" ] }, diff --git a/etc/init/BUILD.gn b/etc/init/BUILD.gn new file mode 100644 index 000000000..907dabe7e --- /dev/null +++ b/etc/init/BUILD.gn @@ -0,0 +1,27 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +################################################################################# + +group("etc") { + deps = [ ":dns_etc" ] +} + +ohos_prebuilt_etc("dns_etc") { + source = "ans_distributed.cfg" + relative_install_dir = "init" + subsystem_name = "notification" + part_name = "distributed_notification_service" +} diff --git a/etc/init/ans_distributed.cfg b/etc/init/ans_distributed.cfg new file mode 100644 index 000000000..73a3ee58b --- /dev/null +++ b/etc/init/ans_distributed.cfg @@ -0,0 +1,19 @@ +{ + "jobs":[{ + "name" : "ans_distributed", + "cmds" : [ + "mkdir /data/ans_distributed 0711 root root" + ] + } + ], + "services" : [{ + "name" : "ans_distributed", + "path" : ["/system/bin/sa_main", "/system/profile/ans_distributed.json"], + "uid" : "ans_distributed", + "gid" : "ans_distributed", + "apl" : "system_basic", + "ondemand" : true, + "secon" : "u:r:ans_distributed:s0" + } + ] +} diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 2a0d2768e..62f6bd21a 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -62,6 +62,8 @@ ohos_shared_library("ans_innerkits") { "${core_path}/src/ans_subscriber_local_live_view_stub.cpp", "${core_path}/src/ans_subscriber_proxy.cpp", "${core_path}/src/ans_subscriber_stub.cpp", + "${core_path}/src/ans_distributed_stub.cpp", + "${core_path}/src/ans_distributed_proxy.cpp", "${frameworks_module_ans_path}/src/ans_dialog_host_client.cpp", "${frameworks_module_ans_path}/src/badge_number_callback_data.cpp", "${frameworks_module_ans_path}/src/enabled_notification_callback_data.cpp", diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index c325c0cb8..563d699d1 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -455,5 +455,23 @@ ErrCode NotificationHelper::UnregisterPushCallback() { return DelayedSingleton::GetInstance()->UnregisterPushCallback(); } + +void NotificationHelper::OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + return DelayedSingleton::GetInstance()->OnDistributedPublish(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); +} + +void NotificationHelper::OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + return DelayedSingleton::GetInstance()->OnDistributedUpdate(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); +} + +void NotificationHelper::OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) +{ + return DelayedSingleton::GetInstance()->OnDistributedDelete(deviceId, bundleName, label, id, localDeviceId); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index 435f1d1d2..b37349b0d 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -56,11 +56,9 @@ const static std::vector DEFAULT_NOTIFICATION_VIBRATION = {200}; // Default path for template const static std::string DEFAULT_TEMPLATE_PATH("/system/etc/notification_template/external.json"); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED constexpr NotificationConstant::DistributedReminderPolicy DEFAULT_DISTRIBUTED_REMINDER_POLICY = NotificationConstant::DistributedReminderPolicy::DEFAULT; constexpr bool DEFAULT_DISTRIBUTED_ENABLE_IN_APPLICATION_INFO = false; -#endif } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/include/ans_distributed_interface.h b/frameworks/core/include/ans_distributed_interface.h new file mode 100644 index 000000000..b6120b3bf --- /dev/null +++ b/frameworks/core/include/ans_distributed_interface.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_DISTRIBUTED_INTERFACE_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_DISTRIBUTED_INTERFACE_H + +#include +#include + +#include "iremote_broker.h" + +#include "notification_bundle_option.h" +#include "notification_request.h" +#include "nocopyable.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { + +// ans_advance_notification_interface +enum class AnsDistributedInterfaceCode{ + DISTRIBUTEDKVSTOREDEATHRECIPIENT, + DISTRIBUTEDDELETE, + DELETEREMOTENOTIFICATION, + DISTRIBUTEDPUBLISH, + DISTRIBUTEDCHECKREMOTEDEVICESISUSING, + DISTRIBUTEDSETLOCALSCREENSTATUS, + DISTRIBUTEDRESETFFRTQUEUE +}; + +class AnsDistributedInterface : public IRemoteBroker { +public: + AnsDistributedInterface() = default; + virtual ~AnsDistributedInterface() = default; + DISALLOW_COPY_AND_MOVE(AnsDistributedInterface); + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Notification.AnsDistributeInterface"); + + /** + * @brief Obtains the death event of the Distributed KvStore service. + */ + virtual ErrCode OnDistributedKvStoreDeathRecipient() = 0; + + /** + * @brief Removes a local notification. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + virtual ErrCode Delete(const std::string &bundleName, const std::string &label, int32_t id) = 0; + + /** + * @brief Removes a remote notification. + * + * @param deviceId Indicates the ID of the device. + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + virtual ErrCode DeleteRemoteNotification( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) = 0; + + /** + * @brief Publishes a local notification to remote device. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be publish. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be publish. + * @param request Indicates the NotificationRequest object for setting the notification content. + * @return ErrCode Returns the publish result. + */ + virtual ErrCode Publish( + const std::string &bundleName, const std::string &label, int32_t id, const sptr &request) = 0; + + /** + * @brief Check if any other device screen is on. + * + * @param isUsing True for any other device screen is on, otherwise false. + * @return Returns the error code. + */ + virtual ErrCode CheckRemoteDevicesIsUsing(bool &isUsing) = 0; + + /** + * @brief Set screen status of local device. + * + * @param screenOn Indicates the local device screen status. + * @return Returns the error code. + */ + virtual ErrCode SetLocalScreenStatus(bool screenOn) = 0; + + /** + * @brief Reset ffrt queue + */ + virtual ErrCode ResetFfrtQueue() = 0; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_MANAGER_INTERFACE_H diff --git a/frameworks/core/include/ans_distributed_proxy.h b/frameworks/core/include/ans_distributed_proxy.h new file mode 100644 index 000000000..cf093e0bf --- /dev/null +++ b/frameworks/core/include/ans_distributed_proxy.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_DISTRIBUTED_PROXY_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_DISTRIBUTED_PROXY_H + + +#include +#include + + +#include "ans_distributed_interface.h" +#include "distributed_notification_service_ipc_interface_code.h" +#include "notification_bundle_option.h" +#include "iremote_proxy.h" + +namespace OHOS::Notification { +class AnsDistributedProxy : public IRemoteProxy { +public: + explicit AnsDistributedProxy(const sptr &impl) + : IRemoteProxy(impl) {} + ~AnsDistributedProxy() override = default; + DISALLOW_COPY_AND_MOVE(AnsDistributedProxy); + + /** + * @brief Obtains the death event of the Distributed KvStore service. + */ + ErrCode OnDistributedKvStoreDeathRecipient() override; + + /** + * @brief Removes a local notification. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + ErrCode Delete(const std::string &bundleName, const std::string &label, int32_t id) override; + + /** + * @brief Removes a remote notification. + * + * @param deviceId Indicates the ID of the device. + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + ErrCode DeleteRemoteNotification( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) override; + + /** + * @brief Publishes a local notification to remote device. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be publish. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be publish. + * @param request Indicates the NotificationRequest object for setting the notification content. + * @return ErrCode Returns the publish result. + */ + ErrCode Publish( + const std::string &bundleName, const std::string &label, int32_t id, const sptr &request) override; + + /** + * @brief Check if any other device screen is on. + * + * @param isUsing True for any other device screen is on, otherwise false. + * @return Returns the error code. + */ + ErrCode CheckRemoteDevicesIsUsing(bool &isUsing) override; + + /** + * @brief Set screen status of local device. + * + * @param screenOn Indicates the local device screen status. + * @return Returns the error code. + */ + ErrCode SetLocalScreenStatus(bool screenOn) override; + + /** + * @brief Reset ffrt queue + */ + ErrCode ResetFfrtQueue() override; +private: + ErrCode InnerTransact(AnsDistributedInterfaceCode code, + MessageOption &flags, MessageParcel &data, MessageParcel &reply); + + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::Notification + +#endif // BASE_NOTIFICATION_ANS_DIALOG_CALLBACK_PROXY_H diff --git a/frameworks/core/include/ans_distributed_stub.h b/frameworks/core/include/ans_distributed_stub.h new file mode 100644 index 000000000..40b09fd5e --- /dev/null +++ b/frameworks/core/include/ans_distributed_stub.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_DISTRIBUTED_STUB_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_DISTRIBUTED_STUB_H + +#include +#include + +#include "ans_distributed_interface.h" +#include "distributed_notification_service_ipc_interface_code.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace Notification { +class AnsDistributedStub : public IRemoteStub { +public: + AnsDistributedStub(); + ~AnsDistributedStub() override; + DISALLOW_COPY_AND_MOVE(AnsDistributedStub); + + /** + * @brief Handle remote request. + * + * @param data Indicates the input parcel. + * @param reply Indicates the output parcel. + * @param option Indicates the message option. + * @return Returns ERR_OK on success, others on failure. + */ + virtual int OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + + /** + * @brief Obtains the death event of the Distributed KvStore service. + */ + ErrCode OnDistributedKvStoreDeathRecipient() override; + + /** + * @brief Removes a local notification. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + ErrCode Delete(const std::string &bundleName, const std::string &label, int32_t id) override; + + /** + * @brief Removes a remote notification. + * + * @param deviceId Indicates the ID of the device. + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + ErrCode DeleteRemoteNotification( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) override; + + /** + * @brief Publishes a local notification to remote device. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be publish. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be publish. + * @param request Indicates the NotificationRequest object for setting the notification content. + * @return ErrCode Returns the publish result. + */ + ErrCode Publish( + const std::string &bundleName, const std::string &label, int32_t id, const sptr &request) override; + + /** + * @brief Check if any other device screen is on. + * + * @param isUsing True for any other device screen is on, otherwise false. + * @return Returns the error code. + */ + ErrCode CheckRemoteDevicesIsUsing(bool &isUsing) override; + + /** + * @brief Set screen status of local device. + * + * @param screenOn Indicates the local device screen status. + * @return Returns the error code. + */ + ErrCode SetLocalScreenStatus(bool screenOn) override; + + /** + * @brief Reset ffrt queue + */ + ErrCode ResetFfrtQueue() override; +private: + static const std::map> + interfaces_; + + ErrCode HandleOnDistributedKvStoreDeathRecipient(MessageParcel &data, MessageParcel &reply); + ErrCode HandleDelete(MessageParcel &data, MessageParcel &reply); + ErrCode HandleDeleteRemoteNotification(MessageParcel &data, MessageParcel &reply); + ErrCode HandlePublish(MessageParcel &data, MessageParcel &reply); + ErrCode HandleCheckRemoteDevicesIsUsing(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSetLocalScreenStatus(MessageParcel &data, MessageParcel &reply); + ErrCode HandleResetFfrtQueue(MessageParcel &data, MessageParcel &reply); +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_DNS_MANAGER_STUB_H diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index d0fca17b1..f5976b46b 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -742,6 +742,39 @@ public: * @return Returns unregister push Callback result. */ virtual ErrCode UnregisterPushCallback() = 0; + + /** + * @brief publish a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + virtual void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) = 0; + + /** + * @brief update a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + virtual void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) = 0; + + /** + * @brief delete a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param label Indicates the label of the notification to delete. + * @param id Indicates the unique notification ID in the application. + * @param localDeviceId Indicates the ID of the local device. + */ + virtual void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) = 0; }; } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 7dbdf6b6e..c47175218 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -734,6 +734,39 @@ public: */ ErrCode UnregisterPushCallback() override; + /** + * @brief publish a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override; + + /** + * @brief update a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override; + + /** + * @brief delete a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param label Indicates the label of the notification to delete. + * @param id Indicates the unique notification ID in the application. + * @param localDeviceId Indicates the ID of the local device. + */ + void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) override; + private: ErrCode InnerTransact(NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index ba7674c5c..0fe514678 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -734,6 +734,39 @@ public: */ ErrCode UnregisterPushCallback() override; + /** + * @brief publish a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override; + + /** + * @brief update a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override; + + /** + * @brief delete a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param label Indicates the label of the notification to delete. + * @param id Indicates the unique notification ID in the application. + * @param localDeviceId Indicates the ID of the local device. + */ + void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) override; + private: static const std::map> interfaces_; @@ -818,6 +851,9 @@ private: ErrCode HandleSubscribeSelf(MessageParcel &data, MessageParcel &reply); ErrCode HandleGetSlotFlagsAsBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetSlotFlagsAsBundle(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnDistributedPublish(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnDistributedUpdate(MessageParcel &data, MessageParcel &reply); + ErrCode HandleOnDistributedDelete(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result) { diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index df54eefbf..85a469698 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -889,6 +889,39 @@ public: */ ErrCode UnregisterPushCallback(); + /** + * @brief publish a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing); + + /** + * @brief update a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing); + + /** + * @brief delete a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param label Indicates the label of the notification to delete. + * @param id Indicates the unique notification ID in the application. + * @param localDeviceId Indicates the ID of the local device. + */ + void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId); + private: /** * @brief Gets Ans Manager proxy. diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 448b02c9f..0ebfe81d5 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -126,8 +126,11 @@ namespace Notification { SUBSCRIBE_NOTIFICATION_SELF, SET_SLOTFLAGS_BY_BUNDLE, GET_SLOTFLAGS_BY_BUNDLE, + ON_DISTRIBUTED_PUBLISH, + ON_DISTRIBUTED_UPDATE, + ON_DISTRIBUTED_DELETE, }; } } -#endif \ No newline at end of file +#endif diff --git a/frameworks/core/src/ans_distributed_proxy.cpp b/frameworks/core/src/ans_distributed_proxy.cpp new file mode 100644 index 000000000..6159fcc76 --- /dev/null +++ b/frameworks/core/src/ans_distributed_proxy.cpp @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ans_distributed_proxy.h" +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" +#include "parcel.h" + + +namespace OHOS::Notification { + +ErrCode AnsDistributedProxy::OnDistributedKvStoreDeathRecipient() +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[OnDistributedKvStoreDeathRecipient] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DISTRIBUTEDKVSTOREDEATHRECIPIENT, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnDistributedKvStoreDeathRecipient] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[OnDistributedKvStoreDeathRecipient] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::Delete(const std::string &bundleName, const std::string &label, int32_t id) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[Delete] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(bundleName)) { + ANS_LOGE("[Delete] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(label)) { + ANS_LOGE("[Delete] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(id)) { + ANS_LOGE("[Delete] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DISTRIBUTEDDELETE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[Delete] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[Delete] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::DeleteRemoteNotification(const std::string &deviceId, + const std::string &bundleName, const std::string &label, int32_t id) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[DeleteRemoteNotification] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(deviceId)) { + ANS_LOGE("[DeleteRemoteNotification] fail: write deviceId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(bundleName)) { + ANS_LOGE("[DeleteRemoteNotification] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(label)) { + ANS_LOGE("[DeleteRemoteNotification] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(id)) { + ANS_LOGE("[DeleteRemoteNotification] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DELETEREMOTENOTIFICATION, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[DeleteRemoteNotification] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[DeleteRemoteNotification] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::Publish(const std::string &bundleName, + const std::string &label, int32_t id, const sptr &request) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[Publish] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(bundleName)) { + ANS_LOGE("[Publish] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(label)) { + ANS_LOGE("[Publish] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(id)) { + ANS_LOGE("[Publish] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteParcelable(request)) { + ANS_LOGE("[Publish] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DISTRIBUTEDPUBLISH, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[DeleteRemoteNotification] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[DeleteRemoteNotification] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::CheckRemoteDevicesIsUsing(bool &isUsing) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[CheckRemoteDevicesIsUsing] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteBool(isUsing)) { + ANS_LOGE("[CheckRemoteDevicesIsUsing] fail: write deviceId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DISTRIBUTEDCHECKREMOTEDEVICESISUSING, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[CheckRemoteDevicesIsUsing] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[CheckRemoteDevicesIsUsing] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::SetLocalScreenStatus(bool screenOn) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[SetLocalScreenStatus] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool isUsing = false; + if (!data.WriteBool(isUsing)) { + ANS_LOGE("[SetLocalScreenStatus] fail: write deviceId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DISTRIBUTEDSETLOCALSCREENSTATUS, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[SetLocalScreenStatus] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[SetLocalScreenStatus] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::ResetFfrtQueue() +{ + ANS_LOGD("%{public}s", __FUNCTION__); + MessageParcel data; + if (!data.WriteInterfaceToken(AnsDistributedProxy::GetDescriptor())) { + ANS_LOGE("[ResetFfrtQueue] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(AnsDistributedInterfaceCode::DISTRIBUTEDRESETFFRTQUEUE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[ResetFfrtQueue] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[ResetFfrtQueue] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedProxy::InnerTransact(AnsDistributedInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply) +{ + auto remote = Remote(); + if (remote == nullptr) { + ANS_LOGE("[InnerTransact] defeat: get Remote defeat code %{public}u", code); + return ERR_DEAD_OBJECT; + } + int32_t err = remote->SendRequest(static_cast(code), data, reply, flags); + switch (err) { + case NO_ERROR: { + return ERR_OK; + } + case DEAD_OBJECT: { + ANS_LOGE("[InnerTransact] defeat: ipcErr=%{public}d code %{public}d", err, code); + return ERR_DEAD_OBJECT; + } + default: { + ANS_LOGE("[InnerTransact] defeat: ipcErr=%{public}d code %{public}d", err, code); + return ERR_ANS_TRANSACT_FAILED; + } + } +} +} // namespace OHOS::Notification diff --git a/frameworks/core/src/ans_distributed_stub.cpp b/frameworks/core/src/ans_distributed_stub.cpp new file mode 100644 index 000000000..d055fa3a2 --- /dev/null +++ b/frameworks/core/src/ans_distributed_stub.cpp @@ -0,0 +1,301 @@ +/* + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ans_distributed_stub.h" + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +const std::map> + AnsDistributedStub::interfaces_ = { + {AnsDistributedInterfaceCode::DISTRIBUTEDKVSTOREDEATHRECIPIENT, + std::bind(&AnsDistributedStub::HandleOnDistributedKvStoreDeathRecipient, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + {AnsDistributedInterfaceCode::DISTRIBUTEDDELETE, + std::bind(&AnsDistributedStub::HandleDelete, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + {AnsDistributedInterfaceCode::DELETEREMOTENOTIFICATION, + std::bind(&AnsDistributedStub::HandleDeleteRemoteNotification, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + {AnsDistributedInterfaceCode::DISTRIBUTEDPUBLISH, + std::bind(&AnsDistributedStub::HandlePublish, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + {AnsDistributedInterfaceCode::DISTRIBUTEDCHECKREMOTEDEVICESISUSING, + std::bind(&AnsDistributedStub::HandleCheckRemoteDevicesIsUsing, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + {AnsDistributedInterfaceCode::DISTRIBUTEDSETLOCALSCREENSTATUS, + std::bind(&AnsDistributedStub::HandleSetLocalScreenStatus, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + {AnsDistributedInterfaceCode::DISTRIBUTEDRESETFFRTQUEUE, + std::bind(&AnsDistributedStub::HandleResetFfrtQueue, std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3)}, + }; + +AnsDistributedStub::AnsDistributedStub() +{} + +AnsDistributedStub::~AnsDistributedStub() +{} + +int32_t AnsDistributedStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags) +{ + std::u16string descriptor = AnsDistributedStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + ANS_LOGE("[OnRemoteRequest] fail: invalid interface token!"); + return OBJECT_NULL; + } + + auto it = interfaces_.find(static_cast(code)); + if (it == interfaces_.end()) { + ANS_LOGE("[OnRemoteRequest] fail: unknown code!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); + } + + auto fun = it->second; + if (fun == nullptr) { + ANS_LOGE("[OnRemoteRequest] fail: not find function!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, flags); + } + + ErrCode result = fun(this, data, reply); + if (SUCCEEDED(result)) { + return NO_ERROR; + } + + ANS_LOGE("[OnRemoteRequest] fail: Failed to call interface %{public}u, err:%{public}d", code, result); + return result; +} + +ErrCode AnsDistributedStub::HandleOnDistributedKvStoreDeathRecipient(MessageParcel &data, MessageParcel &reply) +{ + ANS_LOGE("HandleOnDistributedKvStoreDeathRecipient called!"); + + ErrCode result = OnDistributedKvStoreDeathRecipient(); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleOnDistributedKvStoreDeathRecipient] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return ERR_OK; +} + +ErrCode AnsDistributedStub::HandleDelete(MessageParcel &data, MessageParcel &reply) +{ + ANS_LOGE("HandleDelete called!"); + + std::string bundleName; + if (!data.ReadString(bundleName)) { + ANS_LOGE("[HandleDelete] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string label; + if (!data.ReadString(label)) { + ANS_LOGE("[HandleDelete] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + int id = 0; + if (!data.ReadInt32(id)) { + ANS_LOGE("[HandleDelete] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = Delete(bundleName, label, id); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleDelete] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return ERR_OK; +} + +ErrCode AnsDistributedStub::HandleDeleteRemoteNotification(MessageParcel &data, MessageParcel &reply) +{ + ANS_LOGE("HandleDeleteRemoteNotification called!"); + + std::string deviceId; + if (!data.ReadString(deviceId)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write deviceId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string bundleName; + if (!data.ReadString(bundleName)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string label; + if (!data.ReadString(label)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + int id = 0; + if (!data.ReadInt32(id)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = DeleteRemoteNotification(deviceId, bundleName, label, id); + + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return ERR_OK; +} + +ErrCode AnsDistributedStub::HandlePublish(MessageParcel &data, MessageParcel &reply) +{ + ANS_LOGE("HandlePublish called!"); + + std::string bundleName; + if (!data.ReadString(bundleName)) { + ANS_LOGE("[Publish] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string label; + if (!data.ReadString(label)) { + ANS_LOGE("[Publish] fail: write label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + int id = 0; + if (!data.ReadInt32(id)) { + ANS_LOGE("[Publish] fail: write id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr request = data.ReadParcelable(); + if (!request) { + ANS_LOGE("[Publish] fail: request ReadParcelable failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = Publish(bundleName, label, id, request); + + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return ERR_OK; +} + +ErrCode AnsDistributedStub::HandleCheckRemoteDevicesIsUsing(MessageParcel &data, MessageParcel &reply) +{ + bool isUsing = false; + if (!data.ReadBool(isUsing)) { + ANS_LOGE("[Publish] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = CheckRemoteDevicesIsUsing(isUsing); + + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedStub::HandleSetLocalScreenStatus(MessageParcel &data, MessageParcel &reply) +{ + bool screenOn = false; + if (!data.ReadBool(screenOn)) { + ANS_LOGE("[Publish] fail: write bundleName failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = SetLocalScreenStatus(screenOn); + + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedStub::HandleResetFfrtQueue(MessageParcel &data, MessageParcel &reply) +{ + ErrCode result = ResetFfrtQueue(); + + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleDeleteRemoteNotification] fail: write result failed, ErrCode=%{public}d", result); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsDistributedStub::OnDistributedKvStoreDeathRecipient() +{ + ANS_LOGE("OnDistributedKvStoreDeathRecipient called!"); + return ERR_OK; +} + +ErrCode AnsDistributedStub::Delete(const std::string &bundleName, const std::string &label, int32_t id) +{ + ANS_LOGE("Delete called!"); + return ERR_OK; +} + +ErrCode AnsDistributedStub::DeleteRemoteNotification(const std::string &deviceId, + const std::string &bundleName, const std::string &label, int32_t id) +{ + ANS_LOGE("DeleteRemoteNotification called!"); + return ERR_OK; +} + +ErrCode AnsDistributedStub::Publish(const std::string &bundleName, + const std::string &label, int32_t id, const sptr &request) +{ + ANS_LOGE("Publish called!"); + return ERR_OK; +} + +ErrCode AnsDistributedStub::CheckRemoteDevicesIsUsing(bool &isUsing) +{ + ANS_LOGE("CheckRemoteDevicesIsUsing called!"); + return ERR_OK; +} + +ErrCode AnsDistributedStub::SetLocalScreenStatus(bool screenOn) +{ + ANS_LOGE("SetLocalScreenStatus called!"); + return ERR_OK; +} + +ErrCode AnsDistributedStub::ResetFfrtQueue() +{ + ANS_LOGE("ResetFfrtQueue called!"); + return ERR_OK; +} + +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index cf498ed97..62a8552fc 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -2991,5 +2991,141 @@ ErrCode AnsManagerProxy::UnregisterPushCallback() return result; } + +void AnsManagerProxy::OnDistributedPublish(const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + if (request == nullptr) { + ANS_LOGE("[OnDistributedPublish] fail: request is null ptr."); + return; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[OnDistributedPublish] fail: write interface token failed."); + return; + } + + if (!data.WriteString(deviceId)) { + ANS_LOGE("[OnDistributedPublish] fail: write device id failed."); + return; + } + + if (!data.WriteString(bundleName)) { + ANS_LOGE("[OnDistributedPublish] fail: write bundle name failed."); + return; + } + + if (!data.WriteParcelable(request)) { + ANS_LOGE("[OnDistributedPublish] fail: write request parcelable failed"); + return; + } + + if (!data.WriteBool(canPublishWithoutApp)) { + ANS_LOGE("[OnDistributedPublish] fail: write canPublishWithoutApp failed"); + return; + } + + if (!data.WriteBool(remoteUsing)) { + ANS_LOGE("[OnDistributedPublish] fail: write remoteUsing failed"); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISTRIBUTED_PUBLISH, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnDistributedPublish] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + +void AnsManagerProxy::OnDistributedUpdate(const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + if (request == nullptr) { + ANS_LOGE("[OnDistributedUpdate] fail: request is null ptr."); + return; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[OnDistributedUpdate] fail: write interface token failed."); + return; + } + + if (!data.WriteString(deviceId)) { + ANS_LOGE("[OnDistributedUpdate] fail: write device id failed."); + return; + } + + if (!data.WriteString(bundleName)) { + ANS_LOGE("[OnDistributedUpdate] fail: write bundle name failed."); + return; + } + + if (!data.WriteParcelable(request)) { + ANS_LOGE("[OnDistributedUpdate] fail: write request parcelable failed"); + return; + } + + if (!data.WriteBool(canPublishWithoutApp)) { + ANS_LOGE("[OnDistributedUpdate] fail: write canPublishWithoutApp failed"); + return; + } + + if (!data.WriteBool(remoteUsing)) { + ANS_LOGE("[OnDistributedUpdate] fail: write remoteUsing failed"); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISTRIBUTED_UPDATE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnDistributedUpdate] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} + +void AnsManagerProxy::OnDistributedDelete(const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[OnDistributedDelete] fail: write interface token failed."); + return; + } + + if (!data.WriteString(deviceId)) { + ANS_LOGE("[OnDistributedDelete] fail: write device id failed."); + return; + } + + if (!data.WriteString(bundleName)) { + ANS_LOGE("[OnDistributedDelete] fail: write bundle name failed."); + return; + } + + if (!data.WriteString(label)) { + ANS_LOGE("[OnDistributedDelete] fail: write label failed"); + return; + } + + if (!data.WriteInt32(id)) { + ANS_LOGE("[OnDistributedDelete] fail: write id failed"); + return; + } + + if (!data.WriteString(localDeviceId)) { + ANS_LOGE("[OnDistributedDelete] fail: write local device id failed"); + return; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_ASYNC}; + ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISTRIBUTED_DELETE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[OnDistributedDelete] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED"); + return; + } +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 9cfff9732..353d7beef 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -274,6 +274,15 @@ const std::map notification = data.ReadParcelable(); + if (!notification) { + ANS_LOGE("[HandleOnDistributedPublish] fail: notification ReadParcelable failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool canPublishWithoutApp = false; + if (!data.ReadBool(canPublishWithoutApp)) { + ANS_LOGE("[HandleOnDistributedPublish] fail: read canPublishWithoutApp failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool remoteUsing = false; + if (!data.ReadBool(remoteUsing)) { + ANS_LOGE("[HandleOnDistributedPublish] fail: read remoteUsing failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + OnDistributedPublish(deviceId, bundleName, notification, canPublishWithoutApp, remoteUsing); + return ERR_OK; +} + +ErrCode AnsManagerStub::HandleOnDistributedUpdate(MessageParcel &data, MessageParcel &reply) +{ + std::string deviceId; + if (!data.ReadString(deviceId)) { + ANS_LOGE("[HandleOnDistributedUpdate] fail: read device id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string bundleName; + if (!data.ReadString(bundleName)) { + ANS_LOGE("[HandleOnDistributedUpdate] fail: read bundle name failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr notification = data.ReadParcelable(); + if (!notification) { + ANS_LOGE("[HandleOnDistributedUpdate] fail: notification ReadParcelable failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool canPublishWithoutApp = false; + if (!data.ReadBool(canPublishWithoutApp)) { + ANS_LOGE("[HandleOnDistributedUpdate] fail: read canPublishWithoutApp failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + bool remoteUsing = false; + if (!data.ReadBool(remoteUsing)) { + ANS_LOGE("[HandleOnDistributedUpdate] fail: read remoteUsing failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + OnDistributedUpdate(deviceId, bundleName, notification, canPublishWithoutApp, remoteUsing); + return ERR_OK; +} + +ErrCode AnsManagerStub::HandleOnDistributedDelete(MessageParcel &data, MessageParcel &reply) +{ + std::string deviceId; + if (!data.ReadString(deviceId)) { + ANS_LOGE("[HandleOnDistributedDelete] fail: read device id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string bundleName; + if (!data.ReadString(bundleName)) { + ANS_LOGE("[HandleOnDistributedDelete] fail: read bundle name failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string label; + if (!data.ReadString(label)) { + ANS_LOGE("[HandleOnDistributedDelete] fail: read label failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + int32_t id = 0; + if (!data.ReadInt32(id)) { + ANS_LOGE("[HandleOnDistributedDelete] fail: read id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + std::string localDeviceId; + if (!data.ReadString(localDeviceId)) { + ANS_LOGE("[HandleOnDistributedDelete] fail: read local device id failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + OnDistributedDelete(deviceId, bundleName, label, id, localDeviceId); + return ERR_OK; +} + ErrCode AnsManagerStub::Publish(const std::string &label, const sptr ¬ification) { ANS_LOGE("AnsManagerStub::Publish called!"); @@ -2475,5 +2592,23 @@ ErrCode AnsManagerStub::UnregisterPushCallback() ANS_LOGE("UnregisterPushCallback called!"); return ERR_INVALID_OPERATION; } + +void AnsManagerStub::OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + ANS_LOGE("OnDistributedPublish called!"); +} + +void AnsManagerStub::OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + ANS_LOGE("OnDistributedUpdate called!"); +} + +void AnsManagerStub::OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) +{ + ANS_LOGE("OnDistributedDelete called!"); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 30d7c1f36..feb153e80 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -1445,5 +1445,35 @@ ErrCode AnsNotification::UnregisterPushCallback() return ansManagerProxy_->UnregisterPushCallback(); } + +void AnsNotification::OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("OnDistributedPublish fail."); + } + + ansManagerProxy_->OnDistributedPublish(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); +} + +void AnsNotification::OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("OnDistributedUpdate fail."); + } + + ansManagerProxy_->OnDistributedUpdate(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); +} + +void AnsNotification::OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("OnDistributedDelete fail."); + } + + ansManagerProxy_->OnDistributedDelete(deviceId, bundleName, label, id, localDeviceId); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/test/unittest/BUILD.gn b/frameworks/core/test/unittest/BUILD.gn index a9cd55895..246c1965b 100644 --- a/frameworks/core/test/unittest/BUILD.gn +++ b/frameworks/core/test/unittest/BUILD.gn @@ -26,5 +26,7 @@ group("unittest") { "ans_subscriber_proxy_branch_test:unittest", "ans_subscriber_proxy_test:unittest", "ans_subscriber_stub_test:unittest", + "ans_distributed_proxy_test:unittest", + "ans_distributed_stub_test:unittest", ] } diff --git a/frameworks/core/test/unittest/ans_distributed_proxy_test/BUILD.gn b/frameworks/core/test/unittest/ans_distributed_proxy_test/BUILD.gn new file mode 100644 index 000000000..dfcc980f8 --- /dev/null +++ b/frameworks/core/test/unittest/ans_distributed_proxy_test/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") +import("//build/test.gni") + +module_output_path = "${component_name}/unittest" + +ohos_unittest("ans_distributed_proxy_test") { + module_out_path = module_output_path + include_dirs = [ + "${core_path}/include", + "../mock/", + ] + + sources = [ "ans_distributed_proxy_unit_test.cpp" ] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hilog:libhilog", + "image_framework:image_native", + "ipc:ipc_single", + "relational_store:native_rdb", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +group("unittest") { + testonly = true + deps = [] + + deps += [ ":ans_distributed_proxy_test" ] +} diff --git a/frameworks/core/test/unittest/ans_distributed_proxy_test/ans_distributed_proxy_unit_test.cpp b/frameworks/core/test/unittest/ans_distributed_proxy_test/ans_distributed_proxy_unit_test.cpp new file mode 100644 index 000000000..184e9bb89 --- /dev/null +++ b/frameworks/core/test/unittest/ans_distributed_proxy_test/ans_distributed_proxy_unit_test.cpp @@ -0,0 +1,436 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification_request.h" +#include + +#define private public +#define protected public +#include "ans_distributed_proxy.h" +#undef private +#undef protected +#include "ans_inner_errors.h" +#include "ipc_types.h" +#include "mock_i_remote_object.h" +#include "notification.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::Notification; + +class AnsDistributedProxyUnitTest : public testing::Test { +public: + AnsDistributedProxyUnitTest() {} + + virtual ~AnsDistributedProxyUnitTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp(); + + void TearDown(); +}; + +void AnsDistributedProxyUnitTest::SetUpTestCase() {} + +void AnsDistributedProxyUnitTest::TearDownTestCase() {} + +void AnsDistributedProxyUnitTest::SetUp() {} + +void AnsDistributedProxyUnitTest::TearDown() {} + +/* + * @tc.name: InnerTransactTest_0100 + * @tc.desc: test if AnsDistributedProxy's InnerTransact function executed as expected in normal case. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, InnerTransactTest_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, InnerTransactTest_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).WillOnce(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + uint32_t code = 0; + MessageOption flags; + MessageParcel data; + MessageParcel reply; + ErrCode res = proxy->InnerTransact(static_cast(code), flags, data, reply); + EXPECT_EQ(ERR_OK, res); +} + +/* + * @tc.name: InnerTransactTest_0200 + * @tc.desc: test AnsDistributedProxy's InnerTransact function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, InnerTransactTest_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, InnerTransactTest_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).WillOnce(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + uint32_t code = 0; + MessageOption flags; + MessageParcel data; + MessageParcel reply; + ErrCode res = proxy->InnerTransact(static_cast(code), flags, data, reply); + EXPECT_EQ(ERR_DEAD_OBJECT, res); +} + +/* + * @tc.name: InnerTransactTest_0300 + * @tc.desc: test AnsDistributedProxy's InnerTransact function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, InnerTransactTest_0300, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, InnerTransactTest_0300, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).WillOnce(DoAll(Return(-1))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + uint32_t code = 0; + MessageOption flags; + MessageParcel data; + MessageParcel reply; + ErrCode res = proxy->InnerTransact(static_cast(code), flags, data, reply); + EXPECT_EQ(ERR_ANS_TRANSACT_FAILED, res); +} + +/* + * @tc.name: InnerTransactTest_0400 + * @tc.desc: test AnsDistributedProxy's InnerTransact function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, InnerTransactTest_0400, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, InnerTransactTest_0400, TestSize.Level1"; + std::shared_ptr proxy = std::make_shared(nullptr); + ASSERT_NE(nullptr, proxy); + uint32_t code = 0; + MessageOption flags; + MessageParcel data; + MessageParcel reply; + ErrCode res = proxy->InnerTransact(static_cast(code), flags, data, reply); + EXPECT_EQ(ERR_DEAD_OBJECT, res); +} + +/* + * @tc.name: OnDistributedKvStoreDeathRecipient_0100 + * @tc.desc: test AnsDistributedProxy's OnDistributedKvStoreDeathRecipient function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, OnDistributedKvStoreDeathRecipient_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, OnDistributedKvStoreDeathRecipient_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + ErrCode res = proxy->OnDistributedKvStoreDeathRecipient(); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: OnDistributedKvStoreDeathRecipient_0200 + * @tc.desc: test AnsDistributedProxy's OnDistributedKvStoreDeathRecipient function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, OnDistributedKvStoreDeathRecipient_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, OnDistributedKvStoreDeathRecipient_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + ErrCode res = proxy->OnDistributedKvStoreDeathRecipient(); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} + +/* + * @tc.name: Delete_0100 + * @tc.desc: test AnsDistributedProxy's Delete function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, Delete_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, Delete_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + int32_t id = 0; + std::string label = "label"; + std::string bundleName = ""; + ErrCode res = proxy->Delete(bundleName, label, label); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} + +/* + * @tc.name: Delete_0200 + * @tc.desc: test AnsDistributedProxy's Delete function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, Delete_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, Delete_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + int32_t id = 0; + std::string label = "label"; + std::string bundleName = ""; + ErrCode res = proxy->Delete(bundleName, label, label); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: DeleteRemoteNotification_0100 + * @tc.desc: test AnsDistributedProxy's DeleteRemoteNotification function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, DeleteRemoteNotification_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, DeleteRemoteNotification_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + int32_t id = 0; + std::string label = "label"; + std::string deviceId = "" + std::string bundleName = ""; + ErrCode res = proxy->DeleteRemoteNotification(deviceId, bundleName, label, id); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: DeleteRemoteNotification_0200 + * @tc.desc: test AnsDistributedProxy's DeleteRemoteNotification function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, DeleteRemoteNotification_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, DeleteRemoteNotification_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + int32_t id = 0; + std::string label = "label"; + std::string deviceId = "" + std::string bundleName = ""; + ErrCode res = proxy->DeleteRemoteNotification(deviceId, bundleName, label, id); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} + +/* + * @tc.name: Publish_0100 + * @tc.desc: test AnsDistributedProxy's Publish function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, Publish_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, Publish_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + int32_t id = 0; + std::string label = "label"; + std::string bundleName = ""; + sptr request = + new (std::nothrow) OHOS::Notification::NotificationRequest(); + ASSERT_NE(nullptr, request); + ErrCode res = proxy->Publish(bundleName, label, id, request); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} + +/* + * @tc.name: Publish_0200 + * @tc.desc: test AnsDistributedProxy's Publish function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, Publish_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, Publish_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + int32_t id = 0; + std::string label = "label"; + std::string bundleName = ""; + sptr request = + new (std::nothrow) OHOS::Notification::NotificationRequest(); + ASSERT_NE(nullptr, request); + ErrCode res = proxy->Publish(bundleName, label, id, request); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: CheckRemoteDevicesIsUsing_0100 + * @tc.desc: test AnsDistributedProxy's CheckRemoteDevicesIsUsing function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, CheckRemoteDevicesIsUsing_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, CheckRemoteDevicesIsUsing_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + bool isUsing = false; + ErrCode res = proxy->CheckRemoteDevicesIsUsing(isUsing); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: CheckRemoteDevicesIsUsing_0200 + * @tc.desc: test AnsDistributedProxy's CheckRemoteDevicesIsUsing function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, CheckRemoteDevicesIsUsing_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, CheckRemoteDevicesIsUsing_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + bool isUsing = false; + ErrCode res = proxy->CheckRemoteDevicesIsUsing(isUsing); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} + +/* + * @tc.name: SetLocalScreenStatus_0100 + * @tc.desc: test AnsDistributedProxy's SetLocalScreenStatus function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, SetLocalScreenStatus_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, SetLocalScreenStatus_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + bool isUsing = false; + ErrCode res = proxy->SetLocalScreenStatus(isUsing); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: SetLocalScreenStatus_0200 + * @tc.desc: test AnsDistributedProxy's SetLocalScreenStatus function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, SetLocalScreenStatus_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, SetLocalScreenStatus_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + bool isUsing = false; + ErrCode res = proxy->SetLocalScreenStatus(isUsing); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} + +/* + * @tc.name: ResetFfrtQueue_0100 + * @tc.desc: test AnsDistributedProxy's ResetFfrtQueue function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, ResetFfrtQueue_0100, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, ResetFfrtQueue_0100, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(NO_ERROR))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + ErrCode res = proxy->ResetFfrtQueue(); + EXPECT_EQ(res, ERR_OK); +} + +/* + * @tc.name: ResetFfrtQueue_0200 + * @tc.desc: test AnsDistributedProxy's ResetFfrtQueue function + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedProxyUnitTest, ResetFfrtQueue_0200, Function | MediumTest | Level1) +{ + GTEST_LOG_(INFO) + << "AnsDistributedProxyUnitTest, ResetFfrtQueue_0200, TestSize.Level1"; + sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); + ASSERT_NE(nullptr, iremoteObject); + EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1).WillRepeatedly(DoAll(Return(DEAD_OBJECT))); + std::shared_ptr proxy = std::make_shared(iremoteObject); + ASSERT_NE(nullptr, proxy); + + ErrCode res = proxy->ResetFfrtQueue(); + EXPECT_EQ(res, ERR_DEAD_OBJECT); +} \ No newline at end of file diff --git a/frameworks/core/test/unittest/ans_distributed_stub_test/BUILD.gn b/frameworks/core/test/unittest/ans_distributed_stub_test/BUILD.gn new file mode 100644 index 000000000..22dc52580 --- /dev/null +++ b/frameworks/core/test/unittest/ans_distributed_stub_test/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") +import("//build/test.gni") + +module_output_path = "${component_name}/unittest" + +ohos_unittest("ans_distributed_stub_test") { + module_out_path = module_output_path + include_dirs = [ + "${core_path}/include", + "../mock/", + ] + + sources = [ "ans_distributed_stub_unit_test.cpp" ] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:wantagent_innerkits", + "c_utils:utils", + "hilog:libhilog", + "image_framework:image_native", + "ipc:ipc_single", + "relational_store:native_rdb", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +group("unittest") { + testonly = true + deps = [] + + deps += [ ":ans_distributed_stub_test" ] +} diff --git a/frameworks/core/test/unittest/ans_distributed_stub_test/ans_distributed_stub_unit_test.cpp b/frameworks/core/test/unittest/ans_distributed_stub_test/ans_distributed_stub_unit_test.cpp new file mode 100644 index 000000000..6140aa0bf --- /dev/null +++ b/frameworks/core/test/unittest/ans_distributed_stub_test/ans_distributed_stub_unit_test.cpp @@ -0,0 +1,541 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#define private public +#define protected public +#include "ans_distributed_stub.h" +#include "ans_inner_errors.h" +#include "ans_notification.h" +#undef private +#undef protected +#include "ans_distributed_interface.h" +#include "ans_const_define.h" +#include "message_option.h" +#include "message_parcel.h" +#include "notification.h" +#include "parcel.h" +#include "mock_i_remote_object.h" + + + +using namespace testing; +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class AnsDistributedStubUnitTest : public testing::Test { +public: + AnsDistributedStubUnitTest() {} + + virtual ~AnsDistributedStubUnitTest() {} + + static void SetUpTestCase(); + + static void TearDownTestCase(); + + void SetUp() override; + + void TearDown() override; + + sptr stub_; +}; + +void AnsDistributedStubUnitTest::SetUpTestCase() +{ +} + +void AnsDistributedStubUnitTest::TearDownTestCase() +{ +} + +void AnsDistributedStubUnitTest::SetUp() +{ + stub_ = new AnsDistributedStub(); +} + +void AnsDistributedStubUnitTest::TearDown() +{ +} + +/** + * @tc.name: OnRemoteRequest01 + * @tc.desc: Test if get the wrong descriptor. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, OnRemoteRequest0001, Function | SmallTest | Level1) +{ + uint32_t code = static_cast(NotificationInterfaceCode::PUBLISH_NOTIFICATION); + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + data.WriteInterfaceToken(u"error.GetDescriptor"); + + ErrCode ret = stub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, (int)OBJECT_NULL); +} + +/** + * @tc.name: OnRemoteRequest02 + * @tc.desc: Test if get the wrong code. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, OnRemoteRequest0002, Function | SmallTest | Level1) +{ + uint32_t code = 267; + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + data.WriteInterfaceToken(AnsDistributedStub::GetDescriptor()); + + ErrCode ret = stub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(ret, (int)IPC_STUB_UNKNOW_TRANS_ERR); +} + +/** +* @tc.name: HandleOnDistributedKvStoreDeathRecipient +* @tc.desc: test HandleOnDistributedKvStoreDeathRecipient success +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleOnDistributedKvStoreDeathRecipient, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandleOnDistributedKvStoreDeathRecipient(data, reply); + EXPECT_EQ(res, ERR_OK); +} + +/** +* @tc.name: HandleDelete01 +* @tc.desc: test HandleDelete return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDelete01, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandleDelete(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDelete02 +* @tc.desc: test HandleDelete return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDelete02, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + std::string bundleName = "test_bundlename"; + data.WriteString(bundleName); + ErrCode res = stub_->HandleDelete(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDelete03 +* @tc.desc: test HandleDelete return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDelete03, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + std::string bundleName = "test_bundlename"; + std::string label = "this is test label"; + data.WriteString(bundleName); + data.WriteString(label); + ErrCode res = stub_->HandleDelete(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDelete04 +* @tc.desc: test HandleDelete return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDelete04, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + + int32_t id = 0; + std::string bundleName = "test_bundlename"; + std::string label = "this is test label"; + data.WriteString(bundleName); + data.WriteString(label); + data.WriteInt32(id); + ErrCode res = stub_->HandleDelete(data, reply); + EXPECT_EQ(res, ERR_OK); +} + +/** +* @tc.name: HandleDeleteRemoteNotification01 +* @tc.desc: test HandleDeleteRemoteNotification return +* @tc.type: Fun +*/ + +HWTEST_F(AnsDistributedStubUnitTest, HandleDeleteRemoteNotification01, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandleDeleteRemoteNotification(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDeleteRemoteNotification02 +* @tc.desc: test HandleDeleteRemoteNotification return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDeleteRemoteNotification02, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + std::string deviceId = "this is test id"; + date.WriteString(deviceId); + ErrCode res = stub_->HandleDeleteRemoteNotification(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDeleteRemoteNotification03 +* @tc.desc: test HandleDeleteRemoteNotification return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDeleteRemoteNotification03, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + std::string deviceId = "this is test id"; + std::string bundleName = "this is test name"; + date.WriteString(deviceId); + date.WriteString(bundleName); + ErrCode res = stub_->HandleDeleteRemoteNotification(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDeleteRemoteNotification04 +* @tc.desc: test HandleDeleteRemoteNotification return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDeleteRemoteNotification04, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + std::string deviceId = "this is test id"; + std::string bundleName = "this is test name"; + std::string label = "this is test label"; + date.WriteString(deviceId); + date.WriteString(bundleName); + date.WriteString(label); + ErrCode res = stub_->HandleDeleteRemoteNotification(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleDeleteRemoteNotification05 +* @tc.desc: test HandleDeleteRemoteNotification return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleDeleteRemoteNotification05, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + int32_t id = 0; + std::string deviceId = "this is test id"; + std::string bundleName = "this is test name"; + std::string label = "this is test label"; + date.WriteString(deviceId); + date.WriteString(bundleName); + date.WriteString(label); + date.WriteInt32(id); + ErrCode res = stub_->HandleDeleteRemoteNotification(data, reply); + EXPECT_EQ(res, ERR_OK); +} + +/** +* @tc.name: HandlePublish01 +* @tc.desc: test HandlePublish return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandlePublish01, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandlePublish(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + + +/** +* @tc.name: HandlePublish02 +* @tc.desc: test HandlePublish return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandlePublish02, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + std::string bundleName = "this is test name"; + date.WriteString(bundleName); + ErrCode res = stub_->HandlePublish(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandlePublish03 +* @tc.desc: test HandlePublish return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandlePublish03, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + std::string label = "this is test label"; + std::string bundleName = "this is test name"; + date.WriteString(bundleName); + date.WriteString(label); + ErrCode res = stub_->HandlePublish(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandlePublish04 +* @tc.desc: test HandlePublish return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandlePublish04, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + int32_t id = 0; + std::string label = "this is test label"; + std::string bundleName = "this is test name"; + date.WriteString(bundleName); + date.WriteString(label); + date.WriteInt32(id); + ErrCode res = stub_->HandlePublish(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandlePublish05 +* @tc.desc: test HandlePublish return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandlePublish05, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + sptr request = new NotificationRequest(); + int32_t id = 0; + std::string label = "this is test label"; + std::string bundleName = "this is test name"; + date.WriteString(bundleName); + date.WriteString(label); + date.WriteInt32(id); + data.WriteParcelable(request); + ErrCode res = stub_->HandlePublish(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleCheckRemoteDevicesIsUsing01 +* @tc.desc: test HandleCheckRemoteDevicesIsUsing return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleCheckRemoteDevicesIsUsing01, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandleCheckRemoteDevicesIsUsing(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleCheckRemoteDevicesIsUsing02 +* @tc.desc: test HandleCheckRemoteDevicesIsUsing return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleCheckRemoteDevicesIsUsing02, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + bool isUsing = false; + data.WriteBool(isUsing); + ErrCode res = stub_->HandleCheckRemoteDevicesIsUsing(data, reply); + EXPECT_EQ(res, ERR_OK); +} + +/** +* @tc.name: HandleSetLocalScreenStatus01 +* @tc.desc: test HandleSetLocalScreenStatus return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleSetLocalScreenStatus01, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandleSetLocalScreenStatus(data, reply); + EXPECT_EQ(res, ERR_ANS_PARCELABLE_FAILED); +} + +/** +* @tc.name: HandleSetLocalScreenStatus02 +* @tc.desc: test HandleSetLocalScreenStatus return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleSetLocalScreenStatus02, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + bool screenOn = false; + data.WriteBool(screenOn); + ErrCode res = stub_->HandleSetLocalScreenStatus(data, reply); + EXPECT_EQ(res, ERR_OK); +} + +/** +* @tc.name: HandleResetFfrtQueue +* @tc.desc: test HandleResetFfrtQueue return +* @tc.type: Fun +*/ +HWTEST_F(AnsDistributedStubUnitTest, HandleResetFfrtQueue, Function | SmallTest | Level2) +{ + MessageParcel data; + MessageParcel reply; + + ErrCode res = stub_->HandleResetFfrtQueue(data, reply); + EXPECT_EQ(res, ERR_OK); +} + +/** + * @tc.name: OnDistributedKvStoreDeathRecipient + * @tc.desc: Test OnDistributedKvStoreDeathRecipient return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, OnDistributedKvStoreDeathRecipient, Function | SmallTest | Level1) +{ + ErrCode result = stub_->OnDistributedKvStoreDeathRecipient(); + EXPECT_EQ(result, (int)ERR_OK); +} + +/** + * @tc.name: Delete + * @tc.desc: Test Delete return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, Delete, Function | SmallTest | Level1) +{ + int32_t id = 0; + std::string bundleName = "test"; + std::string label = "test_label"; + ErrCode result = stub_->Delete(bundleName, label, id); + EXPECT_EQ(result, (int)ERR_OK); +} + +/** + * @tc.name: DeleteRemoteNotification + * @tc.desc: Test DeleteRemoteNotification return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, DeleteRemoteNotification, Function | SmallTest | Level1) +{ + int32_t id = 0; + std::string label = "test_lable"; + std::string deviceId = "test_Id"; + std::string bundleName = "test_bundle"; + ErrCode result = stub_->DeleteRemoteNotification(deviceId, bundleName, label, id); + EXPECT_EQ(result, (int)ERR_OK); +} + +/** + * @tc.name: Publish + * @tc.desc: Test Publish return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, Publish, Function | SmallTest | Level1) +{ + int32_t id = 0; + std::string label = "test_lable"; + std::string deviceId = "test_Id"; + std::string bundleName = "test_bundle"; + sptr request = nullptr; + ErrCode result = stub_->Publish(bundleName, label, id, request); + EXPECT_EQ(result, (int)ERR_OK); +} + +/** + * @tc.name: CheckRemoteDevicesIsUsing + * @tc.desc: Test CheckRemoteDevicesIsUsing return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, CheckRemoteDevicesIsUsing, Function | SmallTest | Level1) +{ + bool isUsing = false; + ErrCode result = stub_->CheckRemoteDevicesIsUsing(isUsing); + EXPECT_EQ(result, (int)ERR_OK); +} + +/** + * @tc.name: SetLocalScreenStatus + * @tc.desc: Test SetLocalScreenStatus return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, SetLocalScreenStatus, Function | SmallTest | Level1) +{ + bool isUsing = false; + ErrCode result = stub_->SetLocalScreenStatus(isUsing); + EXPECT_EQ(result, (int)ERR_OK); +} + +/** + * @tc.name: ResetFfrtQueue + * @tc.desc: Test ResetFfrtQueue return. + * @tc.type: FUNC + */ +HWTEST_F(AnsDistributedStubUnitTest, ResetFfrtQueue, Function | SmallTest | Level1) +{ + ErrCode result = stub_->ResetFfrtQueue(); + EXPECT_EQ(result, (int)ERR_OK); +} + +} +} diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index e378b0f42..c8501222b 100755 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -469,6 +469,16 @@ public: { return ERR_ANS_INVALID_PARAM; } + + void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override + {} + void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override + {} + void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) override + {} }; class AnsNotificationBranchTest : public testing::Test { diff --git a/interfaces/inner_api/notification.h b/interfaces/inner_api/notification.h index edfe7eda5..667e987c1 100644 --- a/interfaces/inner_api/notification.h +++ b/interfaces/inner_api/notification.h @@ -335,6 +335,7 @@ private: friend class AdvancedNotificationService; friend class NotificationSlotFilter; friend class DisturbFilter; + friend class DistributedNotificationService; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index a44d38816..c18f8fe39 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -841,6 +841,39 @@ public: * @return Returns unregister push Callback result. */ static ErrCode UnregisterPushCallback(); + + /** + * @brief publish a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + static void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing); + + /** + * @brief update a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + static void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing); + + /** + * @brief delete a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param label Indicates the label of the notification to delete. + * @param id Indicates the unique notification ID in the application. + * @param localDeviceId Indicates the ID of the local device. + */ + static void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId); }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/inner_api/notification_sorting.h b/interfaces/inner_api/notification_sorting.h index 324101613..fb5a738e7 100644 --- a/interfaces/inner_api/notification_sorting.h +++ b/interfaces/inner_api/notification_sorting.h @@ -166,6 +166,7 @@ private: sptr slot_ = new (std::nothrow) NotificationSlot(NotificationConstant::SlotType::OTHER); friend class AdvancedNotificationService; + friend class DistributedNotificationService; }; } // namespace Notification } // namespace OHOS diff --git a/sa_profile/65978.json b/sa_profile/65978.json new file mode 100644 index 000000000..c2cf1bf23 --- /dev/null +++ b/sa_profile/65978.json @@ -0,0 +1,29 @@ +{ + "process": "ans_distributed", + "systemability": [ + { + "name": 65978, + "libpath": "libans_distributed.z.so", + "run-on-create": false, + "auto-restart": true, + "distributed": true, + "dump-level": 1, + "start-on-demand": { + "deviceonline": [ + { + "name": "deviceonline", + "value": "on" + } + ] + }, + "stop-on-demand": { + "deviceonline": [ + { + "name": "deviceonline", + "value": "off" + } + ] + } + } + ] +} diff --git a/sa_profile/BUILD.gn b/sa_profile/BUILD.gn index 3e6fa1419..9890586ff 100644 --- a/sa_profile/BUILD.gn +++ b/sa_profile/BUILD.gn @@ -13,7 +13,7 @@ import("//build/ohos/sa_profile/sa_profile.gni") ohos_sa_profile("ans_sa_profile") { - sources = [ "3203.json" ] + sources = [ "3203.json", "65978.json" ] part_name = "distributed_notification_service" } diff --git a/services/BUILD.gn b/services/BUILD.gn index 8a6dde621..0cf77a457 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -20,3 +20,7 @@ group("ans_service") { deps += [ "distributed:libans_distributed" ] } } + +group("dns_service") { + deps = [ "distributed:libans_distributed" ] +} diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index c37b5c6e5..a6e5350fe 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -40,6 +40,7 @@ #include "push_callback_interface.h" #include "system_event_observer.h" #include "notification_subscriber_manager.h" +#include "ans_distributed_interface.h" namespace OHOS { namespace Notification { @@ -737,7 +738,6 @@ public: virtual ErrCode SetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t slotFlags) override; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED /** * @brief Obtains the event of turn on screen. */ @@ -747,7 +747,7 @@ public: * @brief Obtains the event of turn off screen. */ void OnScreenOff(); -#endif + void OnResourceRemove(int32_t userId); void OnBundleDataCleared(const sptr &bundleOption); @@ -840,6 +840,41 @@ public: */ void SetRequestBySlotType(const sptr &request); + void ResetAnsDistributeProxy(); + + /** + * @brief publish a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedPublish( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override; + + /** + * @brief update a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param request Indicates active NotificationRequest objects of the current application. + * @param canPublishWithoutApp Indicates whether the device can publish notifications without application. + * @param remoteUsing Indicates whether the remote device is using. + */ + void OnDistributedUpdate( + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) override; + + /** + * @brief delete a notification on remote device. + * @param deviceId Indicates the ID of the device running the application. + * @param bundleName Indicates the bundle name of the application. + * @param label Indicates the label of the notification to delete. + * @param id Indicates the unique notification ID in the application. + * @param localDeviceId Indicates the ID of the local device. + */ + void OnDistributedDelete( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) override; + private: struct RecentInfo; AdvancedNotificationService(); @@ -873,10 +908,8 @@ private: int64_t GetNowSysTime(); ErrCode ActiveNotificationDump(const std::string& bundle, int32_t userId, std::vector &dumpInfo); ErrCode RecentNotificationDump(const std::string& bundle, int32_t userId, std::vector &dumpInfo); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED ErrCode DistributedNotificationDump(const std::string& bundle, int32_t userId, std::vector &dumpInfo); -#endif ErrCode SetRecentNotificationCount(const std::string arg); void UpdateRecentNotification(sptr ¬ification, bool isDelete, int32_t reason); @@ -889,27 +922,17 @@ private: bool CheckApiCompatibility(const sptr &bundleOption); ErrCode IsAllowedNotifySelf(const sptr &bundleOption, bool &allowed); - ErrCode SetNotificationRemindType(sptr notification, bool isLocal); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + ErrCode SetNotificationRemindType(sptr notification, const bool &remoteUsing); std::vector GetLocalNotificationKeys(const sptr &bundleOption); - NotificationConstant::RemindType GetRemindType(); + NotificationConstant::RemindType GetRemindType(const bool &remoteUsing); ErrCode DoDistributedPublish( const sptr bundleOption, const std::shared_ptr record); ErrCode DoDistributedDelete( const std::string deviceId, const std::string bundleName, const sptr notification); void GetDistributedInfo(const std::string &key, std::string &deviceId, std::string &bundleName); - bool CheckDistributedNotificationType(const sptr &request); - void OnDistributedPublish( - const std::string &deviceId, const std::string &bundleName, sptr &request); - void OnDistributedUpdate( - const std::string &deviceId, const std::string &bundleName, sptr &request); - void OnDistributedDelete( - const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); static ErrCode GetDistributedEnableInApplicationInfo( const sptr bundleOption, bool &enable); - bool CheckPublishWithoutApp(const int32_t userId, const sptr &request); - void InitDistributeCallBack(); -#endif + ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr &date); ErrCode GetDoNotDisturbDateByUser(const int32_t &userId, sptr &date); @@ -991,6 +1014,8 @@ private: ErrCode IsAllowedGetNotificationByFilter(const std::shared_ptr &record); ErrCode FillRequestByKeys(const sptr &oldRequest, const std::vector extraInfoKeys, sptr &newRequest); + bool GetAnsNotificationServiceProxy(); + private: static sptr instance_; static std::mutex instanceMutex_; @@ -1009,15 +1034,31 @@ private: sptr pushRecipient_ = nullptr; std::shared_ptr notificationSvrQueue_ = nullptr; static std::string supportCheckSaPermission_; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED NotificationConstant::DistributedReminderPolicy distributedReminderPolicy_ = DEFAULT_DISTRIBUTED_REMINDER_POLICY; bool localScreenOn_ = true; -#endif + std::shared_ptr permissonFilter_ = nullptr; std::shared_ptr notificationSlotFilter_ = nullptr; std::shared_ptr dialogManager_ = nullptr; std::set localLiveViewSubscribedList_; std::mutex liveViewMutext_; + + + std::mutex mutex_; + sptr ansDistributeProxy_; + sptr recipient_; + +}; + +/** + * @class AnsDistributeInterfaceRecipient + * AnsDistributeInterfaceRecipient notices IRemoteBroker died. + */ +class AnsDistributeInterfaceRecipient : public IRemoteObject::DeathRecipient { +public: + AnsDistributeInterfaceRecipient(); + virtual ~AnsDistributeInterfaceRecipient(); + void OnRemoteDied(const wptr &remote); }; /** diff --git a/services/ans/include/bundle_manager_helper.h b/services/ans/include/bundle_manager_helper.h index ea71a8b83..be1f92ced 100644 --- a/services/ans/include/bundle_manager_helper.h +++ b/services/ans/include/bundle_manager_helper.h @@ -75,7 +75,6 @@ public: */ bool GetBundleInfoByBundleName(const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED /** * @brief Check whether the specified bundle has the distributed notification supported. * @@ -84,7 +83,6 @@ public: * @return Returns the check result. */ bool GetDistributedNotificationEnabled(const std::string &bundleName, const int32_t userId); -#endif /** * @brief Obtains bundle info by bundle name. diff --git a/services/ans/include/interface_system_event.h b/services/ans/include/interface_system_event.h index 71cf783da..551d435b0 100644 --- a/services/ans/include/interface_system_event.h +++ b/services/ans/include/interface_system_event.h @@ -25,10 +25,8 @@ namespace OHOS { namespace Notification { struct ISystemEvent { std::function &)> onBundleRemoved; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::function onScreenOn; std::function onScreenOff; -#endif std::function onResourceRemove; std::function &)> onBundleDataCleared; std::function &)> onBundleAdd; diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 474fc272a..15206994d 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -16,12 +16,15 @@ #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_H #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_H + #include "refbase.h" #include "singleton.h" #include "notification_do_not_disturb_date.h" #include "notification_preferences_database.h" + + namespace OHOS { namespace Notification { class NotificationPreferences final { @@ -255,6 +258,65 @@ public: */ ErrCode ClearNotificationInRestoreFactorySettings(); + /** + * @brief Set whether the device supports distributed notifications. + * + * @param enable Specifies whether to enable the device to support distributed notification. + * @return Returns enable distributed result. + */ + ErrCode SetDistributedEnable(bool enable); + + /** + * @brief Check if the device supports distributed notification. + * + * @param enable True if the device supports distributed notification; false otherwise. + * @return Returns is distributed enabled result. + */ + ErrCode GetDistributedEnable(bool &enable); + + /** + * @brief Set whether an application supports distributed notifications. + * + * @param bundleOption Indicates the bundle name and uid of an application. + * @param isEnable Specifies whether to enable an application to support distributed notification. + * @return Returns enable distributed by bundle result. + */ + ErrCode SetDistributedBundleEnable(const sptr &bundleOption, bool enable); + + /** + * @brief Check whether an application supports distributed notifications. + * + * @param bundleOption Indicates the bundle name and uid of an application. + * @param isEnable True if the application supports distributed notification; false otherwise. + * @return Returns is distributed enabled by bundle result. + */ + ErrCode GetDistributedBundleEnable(const sptr &bundleOption, bool &enable); + + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + ErrCode SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled); + + /** + * @brief Get whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled); + + /** + * @brief Remove the setting of whether the application supports distributed notification. + * + * @param bundleOption Indicates the bundle name and uid of an application. + * @return Returns remove the setting result. + */ + ErrCode DeleteDistributedBundleInfo(const sptr &bundleOption); /** * @brief Death recipient. */ diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 51d2bc09a..d730de0fa 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -154,6 +154,40 @@ public: */ bool StoreDeathRecipient(); + /** + * @brief Put distributed enable into disturbe DB. + * + * @param enable Indicates distribued notification enalbed or not. + * @return Return true on success, false on failure. + */ + bool PutDistributedEnable(bool enable); + + /** + * @brief Put bundle distributed enable into disturbe DB. + * + * @param bundleInfo Indicates bundle info. + * @param enable Specifies whether to enable an application to support distributed notification. + * @return Return true on success, false on failure. + */ + bool PutDistributedBundleEnable(const std::string bundleName, const int32_t userId, bool enable); + + /** + * @brief Put sync enabled without app into disturbe DB. + * + * @param userId user ID. + * @param enable Allow or disallow sync notifications + * @return Return true on success, false on failure. + */ + bool PutSyncEnabledWithoutApp(const int32_t userId, bool enable); + + /** + * @brief Delete a key-value of its key from database. + * + * @param key Indicates the key. + * @return Whether to delete key-value success. + */ + bool DeleteToDistributedDB(const std::string &key); + bool RemoveNotificationEnable(const int32_t userId); bool RemoveDoNotDisturbDate(const int32_t userId); bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid); diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index a6fb9c353..3bd48fb69 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -213,6 +213,13 @@ public: */ void RemoveAllSlots(); + /** + * @brief Check if the device supports distributed notification. + * + * @return True if the device supports distributed notification; false otherwise. + */ + bool GetDistributedEnable(); + void SetBundleUid(const int32_t &uid); int32_t GetBundleUid() const; void SetSlotEnabled(NotificationConstant::SlotType slotType, bool enabled); @@ -229,6 +236,7 @@ public: bool hasPoppedDialog_ = BUNDLE_POPPED_DIALOG; std::map> slots_; std::map slotFlagsMap_; + bool distributedEnable_ = false; }; /* @@ -300,6 +308,54 @@ public: */ void SetEnabledAllNotification(const int32_t &userId, const bool &enable); + /** + * @brief Check if the device supports distributed notification. + * + * @return True if the device supports distributed notification; false otherwise. + */ + bool GetDistributedEnable(); + + /** + * @brief Set whether the device supports distributed notifications. + * + * @param enable Specifies whether to enable the device to support distributed notification. + */ + void SetDistributedEnable(bool enable); + + /** + * @brief Set whether an application supports distributed notifications. + * + * @param bundleName Indicates the bundle name of an application. + * @param uid Indicates the uid of an application. + * @param enable Specifies whether to enable an application to support distributed notification. + */ + void SetDistributedBundleEnable(const std::string &bundleName, int32_t uid, bool enable); + + /** + * @brief Get whether an application supports distributed notifications. + * + * @param bundleName Indicates the bundle name of an application. + * @param uid Indicates the uid of an application. + * @param enable Specifies whether to enable an application to support distributed notification. + */ + bool GetDistributedBundleEnable(const std::string &bundleName, int32_t uid); + + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + */ + void SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled); + + /** + * @brief Get whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + */ + ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled); + /** * get enable all notification from preferences info. * @param userId Indicates userId. @@ -307,13 +363,25 @@ public: * @return Whether to enable all notification success. */ bool GetEnabledAllNotification(const int32_t &userId, bool &enable) const; + + /** + * @brief Remove the setting of whether the application supports distributed notification. + * + * @param bundleName Indicates the bundle name of an application. + * @param uid Indicates the uid of an application. + */ + void DeleteDistributedBundleInfo(const std::string &bundleName, int32_t uid); void RemoveNotificationEnable(const int32_t userId); void RemoveDoNotDisturbDate(const int32_t userId); void SetBundleInfoFromDb(const BundleInfo &info, std::string bundleKey); + private: std::map isEnabledAllNotification_; std::map> doNotDisturbDate_; std::map infos_; + bool distributedEnable_ = false; + std::map bundleDistributeEnable_; + std::map enabledWithoutApp_; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_record.h b/services/ans/include/notification_record.h index f80c6c703..a87f09bf3 100644 --- a/services/ans/include/notification_record.h +++ b/services/ans/include/notification_record.h @@ -32,10 +32,8 @@ struct NotificationRecord { sptr request; sptr notification; sptr slot; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; -#endif // DISTRIBUTED_NOTIFICATION_SUPPORTED }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index dcbe95840..57880a2ee 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -61,12 +61,6 @@ #include "notification_config_parse.h" #include "want_params_wrapper.h" -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED -#include "distributed_notification_manager.h" -#include "distributed_preferences.h" -#include "distributed_screen_status_manager.h" -#endif - #define CHECK_BUNDLE_OPTION_IS_INVALID(option) \ if (option == nullptr || option->GetBundleName().empty()) { \ ANS_LOGE("Bundle option sptr is null or bundle name is empty!"); \ @@ -85,9 +79,7 @@ namespace { constexpr char ACTIVE_NOTIFICATION_OPTION[] = "active"; constexpr char RECENT_NOTIFICATION_OPTION[] = "recent"; constexpr char HELP_NOTIFICATION_OPTION[] = "help"; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED constexpr char DISTRIBUTED_NOTIFICATION_OPTION[] = "distributed"; -#endif constexpr char SET_RECENT_COUNT_OPTION[] = "setRecentCount"; constexpr char FOUNDATION_BUNDLE_NAME[] = "ohos.global.systemres"; @@ -347,31 +339,6 @@ std::map& AdvancedNotificationService::GetDefaultSlotConf return slotFlagsDefaultMap_; } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED -void AdvancedNotificationService::InitDistributeCallBack() -{ - DistributedNotificationManager::IDistributedCallback distributedCallback = { - .OnPublish = std::bind(&AdvancedNotificationService::OnDistributedPublish, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnUpdate = std::bind(&AdvancedNotificationService::OnDistributedUpdate, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnDelete = std::bind(&AdvancedNotificationService::OnDistributedDelete, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4), - }; - DistributedNotificationManager::GetInstance()->RegisterCallback(distributedCallback); -} -#endif - AdvancedNotificationService::AdvancedNotificationService() { ANS_LOGI("constructor"); @@ -396,10 +363,8 @@ AdvancedNotificationService::AdvancedNotificationService() ISystemEvent iSystemEvent = { std::bind(&AdvancedNotificationService::OnBundleRemoved, this, std::placeholders::_1), -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::bind(&AdvancedNotificationService::OnScreenOn, this), std::bind(&AdvancedNotificationService::OnScreenOff, this), -#endif std::bind(&AdvancedNotificationService::OnResourceRemove, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataCleared, this, std::placeholders::_1), std::bind(&AdvancedNotificationService::OnBundleDataAdd, this, std::placeholders::_1), @@ -409,9 +374,8 @@ AdvancedNotificationService::AdvancedNotificationService() systemEventObserver_ = std::make_shared(iSystemEvent); dataManager_.RegisterKvStoreServiceDeathRecipient(distributedKvStoreDeathRecipient_); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - InitDistributeCallBack(); -#endif + permissonFilter_ = std::make_shared(); + notificationSlotFilter_ = std::make_shared(); supportCheckSaPermission_ = OHOS::system::GetParameter(NOTIFICATION_ANS_CHECK_SA_PERMISSION, "false"); } @@ -422,9 +386,6 @@ AdvancedNotificationService::~AdvancedNotificationService() NotificationSubscriberManager::GetInstance()->UnRegisterOnSubscriberAddCallback(); StopFilters(); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DistributedNotificationManager::GetInstance()->UngegisterCallback(); -#endif SelfClean(); slotFlagsDefaultMap_.clear(); } @@ -436,7 +397,11 @@ void AdvancedNotificationService::SelfClean() } NotificationSubscriberManager::GetInstance()->ResetFfrtQueue(); - DistributedNotificationManager::GetInstance()->ResetFfrtQueue(); + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ; + } + ansDistributeProxy_->ResetFfrtQueue(); NotificationLocalLiveViewSubscriberManager::GetInstance()->ResetFfrtQueue(); } @@ -559,9 +524,7 @@ ErrCode AdvancedNotificationService::CancelPreparedNotification( int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete("", "", notification); -#endif } })); notificationSvrQueue_->wait(handler); @@ -772,11 +735,9 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( sptr sortingMap = GenerateSortingMap(); ReportInfoToResourceSchedule(request->GetCreatorUserId(), bundleOption->GetBundleName()); NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED if (!request->IsAgentNotification()) { DoDistributedPublish(bundleOption, record); } -#endif NotificationRequestDb requestDb = { .request = record->request, .bundleOption = bundleOption}; UpdateNotificationTimerInfo(record); result = SetNotificationRequestToDb(requestDb); @@ -1030,11 +991,9 @@ ErrCode AdvancedNotificationService::CancelAll() std::vector keys = GetNotificationKeys(bundleOption); std::vector> notifications; for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif result = RemoveFromNotificationList(key, notification, true, NotificationConstant::APP_CANCEL_ALL_REASON_DELETE); if (result != ERR_OK) { @@ -1045,9 +1004,7 @@ ErrCode AdvancedNotificationService::CancelAll() int32_t reason = NotificationConstant::APP_CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); notifications.emplace_back(notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { std::vector> currNotificationList = notifications; @@ -1324,11 +1281,9 @@ ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t remo ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); sptr notification = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif result = RemoveFromNotificationList(key, notification, false, removeReason); if (result != ERR_OK) { return; @@ -1337,9 +1292,7 @@ ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t remo if (notification != nullptr) { UpdateRecentNotification(notification, true, removeReason); NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } })); notificationSvrQueue_->wait(handler); @@ -1376,11 +1329,9 @@ ErrCode AdvancedNotificationService::DeleteByBundle(const sptr keys = GetNotificationKeys(bundle); for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif sptr notification = nullptr; result = RemoveFromNotificationList(key, notification, false, NotificationConstant::CANCEL_REASON_DELETE); @@ -1392,9 +1343,7 @@ ErrCode AdvancedNotificationService::DeleteByBundle(const sptrNotifyCanceled(notification, nullptr, reason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } } @@ -1433,11 +1382,9 @@ ErrCode AdvancedNotificationService::DeleteAll() std::vector keys = GetNotificationKeys(nullptr); std::vector> notifications; for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif sptr notification = nullptr; result = RemoveFromNotificationListForDeleteAll(key, activeUserId, notification); @@ -1449,9 +1396,7 @@ ErrCode AdvancedNotificationService::DeleteAll() int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); notifications.emplace_back(notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { ANS_LOGD("Notifications size greater than or equal to MAX_CANCELED_PARCELABLE_VECTOR_NUM."); @@ -1672,9 +1617,7 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationList(const sptrbundleOption->GetUid() == bundleOption->GetUid()) && (record->notification->GetLabel() == label) && (record->notification->GetId() == notificationId) -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED && record->deviceId.empty() -#endif ) { if (!isCancel && !record->notification->IsRemoveAllowed()) { return ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; @@ -2704,11 +2647,9 @@ ErrCode AdvancedNotificationService::ActiveNotificationDump(const std::string& b if (!bundle.empty() && bundle != record->notification->GetBundleName()) { continue; } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED if (!record->deviceId.empty()) { continue; } -#endif stream.clear(); stream.str(""); stream << "\tUserId: " << record->notification->GetUserId() << "\n"; @@ -2771,7 +2712,6 @@ ErrCode AdvancedNotificationService::RecentNotificationDump(const std::string& b return ERR_OK; } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED ErrCode AdvancedNotificationService::DistributedNotificationDump(const std::string& bundle, int32_t userId, std::vector &dumpInfo) { @@ -2810,7 +2750,6 @@ ErrCode AdvancedNotificationService::DistributedNotificationDump(const std::stri return ERR_OK; } -#endif ErrCode AdvancedNotificationService::SetRecentNotificationCount(const std::string arg) { @@ -2959,7 +2898,7 @@ void AdvancedNotificationService::OnBundleRemoved(const sptrDeleteDistributedBundleInfo(bundleOption); + NotificationPreferences::GetInstance().DeleteDistributedBundleInfo(bundleOption); std::vector keys = GetLocalNotificationKeys(bundleOption); #else std::vector keys = GetNotificationKeys(bundleOption); @@ -2983,9 +2922,7 @@ void AdvancedNotificationService::OnBundleRemoved(const sptrSetLocalScreenStatus(true); + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ; + } + ansDistributeProxy_->SetLocalScreenStatus(true); } void AdvancedNotificationService::OnScreenOff() { ANS_LOGI("%{public}s", __FUNCTION__); localScreenOn_ = false; - DistributedScreenStatusManager::GetInstance()->SetLocalScreenStatus(false); + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ; + } + ansDistributeProxy_->SetLocalScreenStatus(false); } -#endif void AdvancedNotificationService::OnDistributedKvStoreDeathRecipient() { @@ -3095,9 +3038,11 @@ void AdvancedNotificationService::OnDistributedKvStoreDeathRecipient() ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); NotificationPreferences::GetInstance().OnDistributedKvStoreDeathRecipient(); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DistributedNotificationManager::GetInstance()->OnDistributedKvStoreDeathRecipient(); -#endif + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ; + } + ansDistributeProxy_->OnDistributedKvStoreDeathRecipient(); })); } @@ -3188,11 +3133,9 @@ ErrCode AdvancedNotificationService::RemoveNotificationFromRecordList( for (auto& record : recordList) { std::string key = record->notification->GetKey(); sptr notification = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif result = RemoveFromNotificationList(key, notification, true, NotificationConstant::USER_STOPPED_REASON_DELETE); if (result != ERR_OK) { @@ -3202,9 +3145,7 @@ ErrCode AdvancedNotificationService::RemoveNotificationFromRecordList( int32_t reason = NotificationConstant::USER_STOPPED_REASON_DELETE; UpdateRecentNotification(notification, true, reason); notifications.emplace_back(notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - DoDistributedDelete(deviceId, bundleName, notification); -#endif + DoDistributedDelete(deviceId, bundleName, notification); } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { std::vector> currNotificationList = notifications; @@ -3316,32 +3257,25 @@ ErrCode AdvancedNotificationService::RemoveNotification(const sptr notification = nullptr; sptr notificationRequest = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; -#endif for (auto record : notificationList_) { if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && (record->bundleOption->GetUid() == bundle->GetUid()) && -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED (record->deviceId.empty()) && -#endif (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) { if (!record->notification->IsRemoveAllowed()) { result = ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED; break; } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + deviceId = record->deviceId; bundleName = record->bundleName; -#endif notification = record->notification; notificationRequest = record->request; - if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { ProcForDeleteLiveView(record); } - notificationList_.remove(record); result = ERR_OK; break; @@ -3351,9 +3285,7 @@ ErrCode AdvancedNotificationService::RemoveNotification(const sptrNotifyCanceled(notification, nullptr, removeReason); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } if (removeReason != NotificationConstant::CLICK_REASON_DELETE) { TriggerRemoveWantAgent(notificationRequest); @@ -3408,9 +3340,7 @@ ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptrbundleOption->GetBundleName() == bundle->GetBundleName()) && (record->bundleOption->GetUid() == bundle->GetUid()) -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED && record->deviceId.empty() -#endif ) { ProcForDeleteLiveView(record); removeList.push_back(record); @@ -3424,9 +3354,7 @@ ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptrnotification is not nullptr."); UpdateRecentNotification(record->notification, true, reason); notifications.emplace_back(record->notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(record->deviceId, record->bundleName, record->notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { SendNotificationsOnCanceled(notifications, nullptr, reason); @@ -3467,11 +3395,9 @@ ErrCode AdvancedNotificationService::RemoveNotifications( std::vector> notifications; for (auto key : keys) { sptr notification = nullptr; -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif ErrCode result = RemoveFromNotificationList(key, notification, false, removeReason); if (result != ERR_OK) { continue; @@ -3479,9 +3405,7 @@ ErrCode AdvancedNotificationService::RemoveNotifications( if (notification != nullptr) { UpdateRecentNotification(notification, true, removeReason); notifications.emplace_back(notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { std::vector> currNotificationList = notifications; @@ -3614,9 +3538,7 @@ ErrCode AdvancedNotificationService::CancelGroup(const std::string &groupName) for (auto record : notificationList_) { if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) && (record->bundleOption->GetUid() == bundleOption->GetUid()) && -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED record->deviceId.empty() && -#endif (record->request->GetGroupName() == groupName)) { removeList.push_back(record); } @@ -3630,9 +3552,7 @@ ErrCode AdvancedNotificationService::CancelGroup(const std::string &groupName) int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE; UpdateRecentNotification(record->notification, true, reason); notifications.emplace_back(record->notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(record->deviceId, record->bundleName, record->notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { std::vector> currNotificationList = notifications; @@ -3689,9 +3609,7 @@ ErrCode AdvancedNotificationService::RemoveGroupByBundle( } if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) && (record->bundleOption->GetUid() == bundle->GetUid()) && !record->request->IsUnremovable() && -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED record->deviceId.empty() && -#endif (record->request->GetGroupName() == groupName)) { ANS_LOGD("RemoveList push enter."); removeList.push_back(record); @@ -3706,9 +3624,7 @@ ErrCode AdvancedNotificationService::RemoveGroupByBundle( if (record->notification != nullptr) { UpdateRecentNotification(record->notification, true, reason); notifications.emplace_back(record->notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(record->deviceId, record->bundleName, record->notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { SendNotificationsOnCanceled(notifications, nullptr, reason); @@ -3861,7 +3777,7 @@ bool AdvancedNotificationService::CheckPermission(const std::string &permission) ErrCode AdvancedNotificationService::IsDistributedEnabled(bool &enabled) { ANS_LOGD("%{public}s", __FUNCTION__); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + if (notificationSvrQueue_ == nullptr) { ANS_LOGE("Serial queue is invalid."); return ERR_ANS_INVALID_PARAM; @@ -3869,7 +3785,7 @@ ErrCode AdvancedNotificationService::IsDistributedEnabled(bool &enabled) ErrCode result = ERR_OK; ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); - result = DistributedPreferences::GetInstance()->GetDistributedEnable(enabled); + result = NotificationPreferences::GetInstance().GetDistributedEnable(enabled); if (result != ERR_OK) { result = ERR_OK; enabled = false; @@ -3877,16 +3793,12 @@ ErrCode AdvancedNotificationService::IsDistributedEnabled(bool &enabled) })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif } ErrCode AdvancedNotificationService::EnableDistributed(bool enabled) { ANS_LOGD("%{public}s", __FUNCTION__); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { ANS_LOGD("VerifyNativeToken and IsSystemApp is false."); @@ -3904,14 +3816,11 @@ ErrCode AdvancedNotificationService::EnableDistributed(bool enabled) ErrCode result = ERR_OK; ffrt::task_handle handler = notificationSvrQueue_->submit_h( std::bind([&]() { - result = DistributedPreferences::GetInstance()->SetDistributedEnable(enabled); + result = NotificationPreferences::GetInstance().SetDistributedEnable(enabled); ANS_LOGE("ffrt enter!"); })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif } ErrCode AdvancedNotificationService::EnableDistributedByBundle( @@ -3919,7 +3828,6 @@ ErrCode AdvancedNotificationService::EnableDistributedByBundle( { ANS_LOGD("%{public}s", __FUNCTION__); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { return ERR_ANS_NON_SYSTEM_APP; @@ -3935,14 +3843,12 @@ ErrCode AdvancedNotificationService::EnableDistributedByBundle( ANS_LOGD("Create bundle failed."); return ERR_ANS_INVALID_BUNDLE; } - bool appInfoEnable = true; GetDistributedEnableInApplicationInfo(bundle, appInfoEnable); if (!appInfoEnable) { ANS_LOGD("Get from bms is %{public}d", appInfoEnable); return ERR_ANS_PERMISSION_DENIED; } - if (notificationSvrQueue_ == nullptr) { ANS_LOGE("Serial queue is invalid."); return ERR_ANS_INVALID_PARAM; @@ -3950,7 +3856,7 @@ ErrCode AdvancedNotificationService::EnableDistributedByBundle( ErrCode result = ERR_OK; ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); - result = DistributedPreferences::GetInstance()->SetDistributedBundleEnable(bundle, enabled); + result = NotificationPreferences::GetInstance().SetDistributedBundleEnable(bundle, enabled); if (result != ERR_OK) { result = ERR_OK; enabled = false; @@ -3958,27 +3864,23 @@ ErrCode AdvancedNotificationService::EnableDistributedByBundle( })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif + } ErrCode AdvancedNotificationService::EnableDistributedSelf(const bool enabled) { ANS_LOGD("%{public}s", __FUNCTION__); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } - bool appInfoEnable = true; GetDistributedEnableInApplicationInfo(bundleOption, appInfoEnable); if (!appInfoEnable) { ANS_LOGD("Get from bms is %{public}d", appInfoEnable); return ERR_ANS_PERMISSION_DENIED; } - if (notificationSvrQueue_ == nullptr) { ANS_LOGE("notificationSvrQueue_ is nullptr."); return ERR_ANS_INVALID_PARAM; @@ -3987,13 +3889,10 @@ ErrCode AdvancedNotificationService::EnableDistributedSelf(const bool enabled) ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind( [&]() { ANS_LOGD("ffrt enter!"); - result = DistributedPreferences::GetInstance()->SetDistributedBundleEnable(bundleOption, enabled); + result = NotificationPreferences::GetInstance().SetDistributedBundleEnable(bundleOption, enabled); })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif } ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( @@ -4001,7 +3900,6 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( { ANS_LOGD("%{public}s", __FUNCTION__); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { return ERR_ANS_NON_SYSTEM_APP; @@ -4016,7 +3914,6 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( ANS_LOGD("Failed to create bundle."); return ERR_ANS_INVALID_BUNDLE; } - bool appInfoEnable = true; GetDistributedEnableInApplicationInfo(bundle, appInfoEnable); if (!appInfoEnable) { @@ -4024,7 +3921,6 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( enabled = appInfoEnable; return ERR_OK; } - if (notificationSvrQueue_ == nullptr) { ANS_LOGE("Serial queue is invalid."); return ERR_ANS_INVALID_PARAM; @@ -4032,7 +3928,7 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( ErrCode result = ERR_OK; ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { ANS_LOGD("ffrt enter!"); - result = DistributedPreferences::GetInstance()->GetDistributedBundleEnable(bundle, enabled); + result = NotificationPreferences::GetInstance().GetDistributedBundleEnable(bundle, enabled); if (result != ERR_OK) { result = ERR_OK; enabled = false; @@ -4040,9 +3936,6 @@ ErrCode AdvancedNotificationService::IsDistributedEnableByBundle( })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif } ErrCode AdvancedNotificationService::GetDeviceRemindType(NotificationConstant::RemindType &remindType) @@ -4063,7 +3956,15 @@ ErrCode AdvancedNotificationService::GetDeviceRemindType(NotificationConstant::R ANS_LOGE("Serial queue is invalid."); return ERR_ANS_INVALID_PARAM; } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { remindType = GetRemindType(); })); + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + bool remoteUsing = false; + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ; + } + ansDistributeProxy_->CheckRemoteDevicesIsUsing(remoteUsing); + remindType = GetRemindType(remoteUsing); + })); notificationSvrQueue_->wait(handler); return ERR_OK; #else @@ -4071,17 +3972,16 @@ ErrCode AdvancedNotificationService::GetDeviceRemindType(NotificationConstant::R #endif } -ErrCode AdvancedNotificationService::SetNotificationRemindType(sptr notification, bool isLocal) +ErrCode AdvancedNotificationService::SetNotificationRemindType(sptr notification, const bool &remoteUsing) { #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED - notification->SetRemindType(GetRemindType()); + notification->SetRemindType(GetRemindType(remoteUsing)); #else notification->SetRemindType(NotificationConstant::RemindType::NONE); #endif return ERR_OK; } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::vector AdvancedNotificationService::GetLocalNotificationKeys( const sptr &bundleOption) { @@ -4098,15 +3998,10 @@ std::vector AdvancedNotificationService::GetLocalNotificationKeys( return keys; } -NotificationConstant::RemindType AdvancedNotificationService::GetRemindType() +NotificationConstant::RemindType AdvancedNotificationService::GetRemindType(const bool &remoteUsing) { bool remind = localScreenOn_; if (distributedReminderPolicy_ == NotificationConstant::DistributedReminderPolicy::DEFAULT) { - bool remoteUsing = false; - ErrCode result = DistributedScreenStatusManager::GetInstance()->CheckRemoteDevicesIsUsing(remoteUsing); - if (result != ERR_OK) { - remind = true; - } if (!localScreenOn_ && !remoteUsing) { remind = true; } @@ -4158,18 +4053,23 @@ ErrCode AdvancedNotificationService::DoDistributedPublish( ErrCode result; bool distributedEnable = false; - result = DistributedPreferences::GetInstance()->GetDistributedEnable(distributedEnable); + result = NotificationPreferences::GetInstance().GetDistributedEnable(distributedEnable); if (result != ERR_OK || !distributedEnable) { return result; } bool bundleDistributedEnable = false; - result = DistributedPreferences::GetInstance()->GetDistributedBundleEnable(bundleOption, bundleDistributedEnable); + result = NotificationPreferences::GetInstance().GetDistributedBundleEnable(bundleOption, bundleDistributedEnable); if (result != ERR_OK || !bundleDistributedEnable) { return result; } - return DistributedNotificationManager::GetInstance()->Publish(record->notification->GetBundleName(), + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansDistributeProxy_->Publish(record->notification->GetBundleName(), record->notification->GetLabel(), record->notification->GetId(), record->request); @@ -4182,36 +4082,41 @@ ErrCode AdvancedNotificationService::DoDistributedDelete( if (!notification->GetNotificationRequest().GetNotificationDistributedOptions().IsDistributed()) { return ERR_OK; } + + if(!GetAnsNotificationServiceProxy()) { + ANS_LOGE("GetAnsNotificationServiceProxy is invalid."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + if (deviceId.empty()) { - return DistributedNotificationManager::GetInstance()->Delete( - notification->GetBundleName(), notification->GetLabel(), notification->GetId()); + return ansDistributeProxy_->Delete(notification->GetBundleName(), + notification->GetLabel(), notification->GetId()); } else { - return DistributedNotificationManager::GetInstance()->DeleteRemoteNotification( - deviceId, bundleName, notification->GetLabel(), notification->GetId()); + return ansDistributeProxy_->DeleteRemoteNotification(deviceId, + bundleName, notification->GetLabel(), notification->GetId()); } return ERR_OK; } -bool AdvancedNotificationService::CheckDistributedNotificationType(const sptr &request) +ErrCode AdvancedNotificationService::GetDistributedEnableInApplicationInfo( + const sptr bundleOption, bool &enable) { - auto deviceTypeList = request->GetNotificationDistributedOptions().GetDevicesSupportDisplay(); - if (deviceTypeList.empty()) { - return true; - } + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); - DistributedDatabase::DeviceInfo localDeviceInfo; - DistributedNotificationManager::GetInstance()->GetLocalDeviceInfo(localDeviceInfo); - for (auto device : deviceTypeList) { - if (atoi(device.c_str()) == localDeviceInfo.deviceTypeId) { - return true; - } + if (userId >= SUBSCRIBE_USER_SYSTEM_BEGIN && userId <= SUBSCRIBE_USER_SYSTEM_END) { + enable = true; + } else { + enable = BundleManagerHelper::GetInstance()->GetDistributedNotificationEnabled( + bundleOption->GetBundleName(), userId); } - return false; + + return ERR_OK; } void AdvancedNotificationService::OnDistributedPublish( - const std::string &deviceId, const std::string &bundleName, sptr &request) + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) { ANS_LOGD("%{public}s", __FUNCTION__); int32_t activeUserId = -1; @@ -4224,16 +4129,12 @@ void AdvancedNotificationService::OnDistributedPublish( ANS_LOGE("notificationSvrQueue_ is nullptr."); return; } - notificationSvrQueue_->submit(std::bind([this, deviceId, bundleName, request, activeUserId]() { + notificationSvrQueue_->submit(std::bind([this, deviceId, bundleName, request, canPublishWithoutApp, remoteUsing, activeUserId]() { ANS_LOGD("ffrt enter!"); - if (!CheckDistributedNotificationType(request)) { - ANS_LOGD("CheckDistributedNotificationType is false."); - return; - } int32_t uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId); if (uid <= 0) { - if (CheckPublishWithoutApp(activeUserId, request)) { + if (canPublishWithoutApp) { request->SetOwnerBundleName(FOUNDATION_BUNDLE_NAME); request->SetCreatorBundleName(FOUNDATION_BUNDLE_NAME); } else { @@ -4260,7 +4161,7 @@ void AdvancedNotificationService::OnDistributedPublish( record->bundleOption = bundleOption; record->deviceId = deviceId; record->bundleName = bundleName; - SetNotificationRemindType(record->notification, false); + SetNotificationRemindType(record->notification, remoteUsing); ErrCode result = AssignValidNotificationSlot(record); if (result != ERR_OK) { @@ -4286,7 +4187,7 @@ void AdvancedNotificationService::OnDistributedPublish( } void AdvancedNotificationService::OnDistributedUpdate( - const std::string &deviceId, const std::string &bundleName, sptr &request) + const std::string &deviceId, const std::string &bundleName, sptr &request, const bool &canPublishWithoutApp, const bool &remoteUsing) { ANS_LOGD("%{public}s", __FUNCTION__); int32_t activeUserId = -1; @@ -4299,16 +4200,12 @@ void AdvancedNotificationService::OnDistributedUpdate( ANS_LOGE("Serial queue is invalid."); return; } - notificationSvrQueue_->submit(std::bind([this, deviceId, bundleName, request, activeUserId]() { + notificationSvrQueue_->submit(std::bind([this, deviceId, bundleName, request, canPublishWithoutApp, remoteUsing, activeUserId]() { ANS_LOGD("ffrt enter!"); - if (!CheckDistributedNotificationType(request)) { - ANS_LOGD("device type not support display."); - return; - } int32_t uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId); if (uid <= 0) { - if (CheckPublishWithoutApp(activeUserId, request)) { + if (canPublishWithoutApp) { request->SetOwnerBundleName(FOUNDATION_BUNDLE_NAME); request->SetCreatorBundleName(FOUNDATION_BUNDLE_NAME); } else { @@ -4334,7 +4231,7 @@ void AdvancedNotificationService::OnDistributedUpdate( record->bundleOption = bundleOption; record->deviceId = deviceId; record->bundleName = bundleName; - SetNotificationRemindType(record->notification, false); + SetNotificationRemindType(record->notification, remoteUsing); ErrCode result = AssignValidNotificationSlot(record); if (result != ERR_OK) { @@ -4364,14 +4261,14 @@ void AdvancedNotificationService::OnDistributedUpdate( } void AdvancedNotificationService::OnDistributedDelete( - const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id, const std::string &localDeviceId) { ANS_LOGD("%{public}s", __FUNCTION__); if (notificationSvrQueue_ == nullptr) { ANS_LOGE("Serial queue is invalid."); return; } - notificationSvrQueue_->submit(std::bind([this, deviceId, bundleName, label, id]() { + notificationSvrQueue_->submit(std::bind([this, deviceId, bundleName, label, id, localDeviceId]() { ANS_LOGD("ffrt enter!"); int32_t activeUserId = -1; if (!GetActiveUserId(activeUserId)) { @@ -4384,9 +4281,7 @@ void AdvancedNotificationService::OnDistributedDelete( GenerateValidBundleOption(new NotificationBundleOption(bundle, 0)); std::string recordDeviceId; - DistributedDatabase::DeviceInfo localDeviceInfo; - if (DistributedNotificationManager::GetInstance()->GetLocalDeviceInfo(localDeviceInfo) == ERR_OK && - strcmp(deviceId.c_str(), localDeviceInfo.deviceId) == 0) { + if (strcmp(deviceId.c_str(), localDeviceId.c_str()) == 0) { recordDeviceId = ""; } else { recordDeviceId = deviceId; @@ -4413,47 +4308,6 @@ void AdvancedNotificationService::OnDistributedDelete( })); } -ErrCode AdvancedNotificationService::GetDistributedEnableInApplicationInfo( - const sptr bundleOption, bool &enable) -{ - int32_t userId = SUBSCRIBE_USER_INIT; - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(bundleOption->GetUid(), userId); - - if (userId >= SUBSCRIBE_USER_SYSTEM_BEGIN && userId <= SUBSCRIBE_USER_SYSTEM_END) { - enable = true; - } else { - enable = BundleManagerHelper::GetInstance()->GetDistributedNotificationEnabled( - bundleOption->GetBundleName(), userId); - } - - return ERR_OK; -} - -bool AdvancedNotificationService::CheckPublishWithoutApp(const int32_t userId, const sptr &request) -{ - bool enabled = false; - DistributedPreferences::GetInstance()->GetSyncEnabledWithoutApp(userId, enabled); - if (!enabled) { - ANS_LOGE("enable is false, userId[%{public}d]", userId); - return false; - } - - std::shared_ptr wantAgent = request->GetWantAgent(); - if (!wantAgent) { - ANS_LOGE("Failed to get wantAgent!"); - return false; - } - - std::shared_ptr want = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(wantAgent); - if (!want || want->GetDeviceId().empty()) { - ANS_LOGE("Failed to get want!"); - return false; - } - - return true; -} -#endif - ErrCode AdvancedNotificationService::PrepareContinuousTaskNotificationRequest( const sptr &request, const int32_t &uid) { @@ -4588,11 +4442,9 @@ ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) std::vector keys = GetNotificationKeys(nullptr); std::vector> notifications; for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif sptr notification = nullptr; result = RemoveFromNotificationListForDeleteAll(key, userId, notification); @@ -4604,9 +4456,7 @@ ErrCode AdvancedNotificationService::DeleteAllByUser(const int32_t &userId) int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE; UpdateRecentNotification(notification, true, reason); notifications.emplace_back(notification); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED DoDistributedDelete(deviceId, bundleName, notification); -#endif } if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { SendNotificationsOnCanceled(notifications, nullptr, NotificationConstant::CANCEL_ALL_REASON_DELETE); @@ -4824,11 +4674,9 @@ void AdvancedNotificationService::OnBundleDataCleared(const sptr keys = GetNotificationKeys(bundleOption); std::vector> notifications; for (auto key : keys) { -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED std::string deviceId; std::string bundleName; GetDistributedInfo(key, deviceId, bundleName); -#endif sptr notification = nullptr; ErrCode result = RemoveFromNotificationList(key, notification, true, @@ -4841,9 +4689,7 @@ void AdvancedNotificationService::OnBundleDataCleared(const sptr= MAX_CANCELED_PARCELABLE_VECTOR_NUM) { std::vector> currNotificationList = notifications; @@ -5058,10 +4904,8 @@ ErrCode AdvancedNotificationService::ShellDump(const std::string &cmd, const std result = ActiveNotificationDump(bundle, userId, dumpInfo); } else if (cmd == RECENT_NOTIFICATION_OPTION) { result = RecentNotificationDump(bundle, userId, dumpInfo); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED } else if (cmd == DISTRIBUTED_NOTIFICATION_OPTION) { result = DistributedNotificationDump(bundle, userId, dumpInfo); -#endif } else if (cmd.substr(0, cmd.find_first_of(" ", 0)) == SET_RECENT_COUNT_OPTION) { result = SetRecentNotificationCount(cmd.substr(cmd.find_first_of(" ", 0) + 1)); } else { @@ -5263,7 +5107,6 @@ ErrCode AdvancedNotificationService::SetSyncNotificationEnabledWithoutApp(const { ANS_LOGD("userId: %{public}d, enabled: %{public}d", userId, enabled); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { return ERR_ANS_NON_SYSTEM_APP; @@ -5282,20 +5125,15 @@ ErrCode AdvancedNotificationService::SetSyncNotificationEnabledWithoutApp(const ffrt::task_handle handler = notificationSvrQueue_->submit_h( std::bind([&]() { ANS_LOGD("ffrt enter!"); - result = DistributedPreferences::GetInstance()->SetSyncEnabledWithoutApp(userId, enabled); + result = NotificationPreferences::GetInstance().SetSyncEnabledWithoutApp(userId, enabled); })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif } ErrCode AdvancedNotificationService::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) { ANS_LOGD("userId: %{public}d", userId); - -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { return ERR_ANS_NON_SYSTEM_APP; @@ -5313,13 +5151,10 @@ ErrCode AdvancedNotificationService::GetSyncNotificationEnabledWithoutApp(const ffrt::task_handle handler = notificationSvrQueue_->submit_h( std::bind([&]() { ANS_LOGD("ffrt enter!"); - result = DistributedPreferences::GetInstance()->GetSyncEnabledWithoutApp(userId, enabled); + result = NotificationPreferences::GetInstance().GetSyncEnabledWithoutApp(userId, enabled); })); notificationSvrQueue_->wait(handler); return result; -#else - return ERR_INVALID_OPERATION; -#endif } ErrCode AdvancedNotificationService::PrePublishNotificationBySa(const sptr &request, @@ -5935,6 +5770,61 @@ void AdvancedNotificationService::FillActionButtons(const sptr lock(mutex_); + if (!ansDistributeProxy_) { + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + ANS_LOGE("Failed to get system ability mgr."); + return false; + } + + sptr remoteObject = + systemAbilityManager->GetSystemAbility(DISTRIBUTED_NOTIFICATION_SA_ID); + if (!remoteObject) { + ANS_LOGE("Failed to get notification Manager."); + return false; + } + + ansDistributeProxy_ = iface_cast(remoteObject); + if ((!ansDistributeProxy_) || (!ansDistributeProxy_->AsObject())) { + ANS_LOGE("Failed to get notification Manager's proxy"); + return false; + } + + recipient_ = new (std::nothrow) AnsDistributeInterfaceRecipient(); + if (!recipient_) { + ANS_LOGE("Failed to create death recipient"); + return false; + } + ansDistributeProxy_->AsObject()->AddDeathRecipient(recipient_); + } + } + + return true; +} + +void AdvancedNotificationService::ResetAnsDistributeProxy() +{ + ANS_LOGI("enter"); + std::lock_guard lock(mutex_); + if ((ansDistributeProxy_ != nullptr) && (ansDistributeProxy_->AsObject() != nullptr)) { + ansDistributeProxy_->AsObject()->RemoveDeathRecipient(recipient_); + } + ansDistributeProxy_ = nullptr; +} + +AnsDistributeInterfaceRecipient::AnsDistributeInterfaceRecipient() {}; +AnsDistributeInterfaceRecipient::~AnsDistributeInterfaceRecipient() {}; +void AnsDistributeInterfaceRecipient::OnRemoteDied(const wptr &remote) +{ + ANS_LOGI("AnsDistributeInterfaceRecipient Callback died, remove the proxy object"); + AdvancedNotificationService::GetInstance()->ResetAnsDistributeProxy(); +} + void PushCallbackRecipient::OnRemoteDied(const wptr &remote) { ANS_LOGI("Push Callback died, remove the proxy object"); diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index 71ff872a2..3b24c413a 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -163,7 +163,6 @@ int32_t BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle return uid; } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &bundleName, const int32_t userId) { std::lock_guard lock(connectionMutex_); @@ -182,7 +181,6 @@ bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &b ANS_LOGD("APPLICATION_INFO distributed enabled is default"); return DEFAULT_DISTRIBUTED_ENABLE_IN_APPLICATION_INFO; } -#endif bool BundleManagerHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, int32_t userId, AppExecFwk::BundleInfo &bundleInfo) diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 49b01b4af..4fda6cd02 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -771,5 +771,120 @@ int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key) } return preferncesDB_->DeleteKvFromDb(key); } + +ErrCode NotificationPreferences::SetDistributedEnable(bool enable) +{ + ANS_LOGI("start"); + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + preferencesInfo.SetDistributedEnable(enable); + ErrCode result = ERR_OK; + if (!preferncesDB_->PutDistributedEnable(enable)) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + + if (result == ERR_OK) { + preferencesInfo_ = preferencesInfo; + } + return result; +} + +ErrCode NotificationPreferences::GetDistributedEnable(bool &enable) +{ + ANS_LOGI("start"); + + enable = preferencesInfo_.GetDistributedEnable(); + + return ERR_OK; +} + +ErrCode NotificationPreferences::SetDistributedBundleEnable( + const sptr &bundleOption, bool enable) +{ + ANS_LOGI("start"); + + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERR_ANS_INVALID_PARAM; + } + + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + preferencesInfo.SetDistributedBundleEnable(bundleOption->GetBundleName(), bundleOption->GetUid(), enable); + ErrCode result = ERR_OK; + if (!preferncesDB_->PutDistributedBundleEnable(bundleOption->GetBundleName(), bundleOption->GetUid(), enable)) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + + if (result == ERR_OK) { + preferencesInfo_ = preferencesInfo; + } + return result; +} + +ErrCode NotificationPreferences::GetDistributedBundleEnable( + const sptr &bundleOption, bool &enable) +{ + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERR_ANS_INVALID_PARAM; + } + + enable = preferencesInfo_.GetDistributedBundleEnable(bundleOption->GetBundleName(), bundleOption->GetUid()); + + return ERR_OK; +} + +ErrCode NotificationPreferences::SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + ANS_LOGI("start"); + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + preferencesInfo.SetSyncEnabledWithoutApp(userId, enabled); + ErrCode result = ERR_OK; + if (!preferncesDB_->PutSyncEnabledWithoutApp(userId, enabled)) { + result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } + + if (result == ERR_OK) { + preferencesInfo_ = preferencesInfo; + } + return result; +} + +ErrCode NotificationPreferences::GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + return preferencesInfo_.GetSyncEnabledWithoutApp(userId, enabled); +} + +ErrCode NotificationPreferences::DeleteDistributedBundleInfo(const sptr &bundleOption) +{ + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERR_ANS_INVALID_PARAM; + } + + if (preferncesDB_ == nullptr) { + ANS_LOGE("database is nullptr"); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + ErrCode result = ERR_OK; + std::string typeKey = + std::string().append(bundleOption->GetBundleName()).append(std::to_string(bundleOption->GetUid())); + if (!preferncesDB_->DeleteToDistributedDB(typeKey)) { + ANS_LOGE("delete to distributed DB failed. key:%{public}s", typeKey.c_str()); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + preferencesInfo.DeleteDistributedBundleInfo(bundleOption->GetBundleName(), bundleOption->GetUid()); + if (result == ERR_OK) { + preferencesInfo_ = preferencesInfo; + } + return ERR_OK; +} + } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 9b0306cda..8eae1aa29 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -178,6 +178,16 @@ const static std::string KEY_BUNDLE_SLOTFLGS_TYPE = "bundleReminderFlagsType"; */ const static std::string KEY_SLOT_SLOTFLGS_TYPE = "reminderFlagsType"; +/** + * Indicates the distributed notification begin key. + */ +const static std::string KEY_DISTRIBUTED = "distributed"; + +/** + * Indicates the distributed notification is enabled. + */ +const static std::string KEY_DISTRIBUTED_ENABLED = "enabled"; + const std::map &, std::string &)>> NotificationPreferencesDatabase::slotMap_ = { @@ -1513,5 +1523,92 @@ int32_t NotificationPreferencesDatabase::DeleteKvFromDb(const std::string &key) return NativeRdb::E_OK; } + +bool NotificationPreferencesDatabase::PutDistributedEnable(bool enable) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::string typeKey = + std::string().append(KEY_DISTRIBUTED).append(KEY_DISTRIBUTED_ENABLED); + std::string enableValue = std::to_string(enable); + int32_t result = rdbDataManager_->InsertData(typeKey, enableValue); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Store enable distributed notification failed. %{public}d", result); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::PutDistributedBundleEnable( + const std::string bundleName, const int32_t userId, bool enable) +{ + if (bundleName.empty()) { + ANS_LOGE("Bundle name is nullptr."); + return false; + } + + if (!CheckBundle(bundleName, userId)) { + return false; + } + + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::string typeKey = + std::string().append(KEY_DISTRIBUTED).append(bundleName).append(std::to_string(userId)); + std::string enableValue = std::to_string(enable); + int32_t result = rdbDataManager_->InsertData(typeKey, enableValue); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Store enable bundle distributed notification failed. %{public}d", result); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::PutSyncEnabledWithoutApp(const int32_t userId, bool enable) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + std::string typeKey = + std::string().append(KEY_DISTRIBUTED).append(KEY_UNDER_LINE).append(std::to_string(userId)); + std::string enableValue = std::to_string(enable); + int32_t result = rdbDataManager_->InsertData(typeKey, enableValue); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Store enable bundle distributed notification failed. %{public}d", result); + return false; + } + return true; +} + +bool NotificationPreferencesDatabase::DeleteToDistributedDB(const std::string &key) +{ + if (rdbDataManager_ == nullptr) { + ANS_LOGE("rdbDataManager_ is nullptr."); + return false; + } + + if (!CheckRdbStore()) { + ANS_LOGE("CheckRdbStore is defeat."); + return false; + } + + std::string typeKey = std::string().append(KEY_DISTRIBUTED) + key; + int32_t result = rdbDataManager_->DeleteData(typeKey); + if (result != NativeRdb::E_OK) { + ANS_LOGE("kvStore Delete() failed ret = 0x%{public}x", result); + return false; + } + + return true; +} + } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 884b1644c..321fd175a 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -304,5 +304,58 @@ void NotificationPreferencesInfo::SetBundleInfoFromDb(const BundleInfo &info, st { infos_.insert_or_assign(bundleKey, info); } + +void NotificationPreferencesInfo::SetDistributedEnable(bool enable) +{ + distributedEnable_ = enable; +} + +bool NotificationPreferencesInfo::GetDistributedEnable() +{ + return distributedEnable_; +} + +void NotificationPreferencesInfo::SetDistributedBundleEnable(const std::string &bundleName, int32_t uid, bool enable) +{ + std::string bundleStr = bundleName + std::to_string(uid); + bundleDistributeEnable_[bundleStr] = enable; +} + +bool NotificationPreferencesInfo::GetDistributedBundleEnable(const std::string &bundleName, int32_t uid) +{ + const std::string bundleStr = bundleName + std::to_string(uid);; + auto iter = bundleDistributeEnable_.find(bundleStr); + if (iter == bundleDistributeEnable_.end()) { + ANS_LOGW("bundle %{public}s(%{public}d) not found.", bundleName.c_str(), uid); + return true; + } + + return iter->second; +} + +void NotificationPreferencesInfo::SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + enabledWithoutApp_[userId] = enabled; +} + +ErrCode NotificationPreferencesInfo::GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + auto iter = enabledWithoutApp_.find(userId); + if (iter == enabledWithoutApp_.end()) { + enabled = false; + ANS_LOGW("userId(%{public}d) not found. enabled default false", userId); + } else { + enabled = iter->second; + ANS_LOGI("userId(%{public}d) enabled = %{public}d", userId, enabled); + } + return ERR_OK; +} + +void NotificationPreferencesInfo::DeleteDistributedBundleInfo(const std::string &bundleName, int32_t uid) +{ + std::string bundleStr = bundleName + std::to_string(uid); + bundleDistributeEnable_.erase(bundleStr); +} + } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index 2cc27feed..1f4eda0ed 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -28,10 +28,8 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba { EventFwk::MatchingSkills matchingSkills; matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); -#endif matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED); @@ -78,7 +76,6 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) callbacks_.onBundleRemoved(bundleOption); } } -#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON) { if (callbacks_.onScreenOn != nullptr) { callbacks_.onScreenOn(); @@ -87,7 +84,6 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) if (callbacks_.onScreenOff != nullptr) { callbacks_.onScreenOff(); } -#endif } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { NotificationPreferences::GetInstance().InitSettingFromDisturbDB(); } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED) { diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 03ac6a5a4..6db1c7d59 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -51,8 +51,6 @@ using namespace OHOS::Media; namespace OHOS { namespace Notification { extern void MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum mockRet); -extern void MockIsSystemApp(bool isSystemApp); -extern void MockIsNonBundleName(bool isNonBundleName); class AdvancedNotificationServiceTest : public testing::Test { public: @@ -145,13 +143,11 @@ void AdvancedNotificationServiceTest::TestAddSlot(NotificationConstant::SlotType HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00100, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::OTHER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -172,13 +168,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00100, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00200, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::OTHER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -200,13 +194,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00200, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00300, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(false); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CUSTOM); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -227,14 +219,12 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00300, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00400, Function | SmallTest | Level1) { - MockIsNonBundleName(true); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingUid(NON_BUNDLE_NAME_UID); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -244,7 +234,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00400, EXPECT_NE(content, nullptr); req->SetContent(content); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_INVALID_BUNDLE); - MockIsNonBundleName(false); SleepForFC(); } @@ -257,13 +246,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00400, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00500, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::SERVICE_REMINDER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -285,13 +272,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00500, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00600, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -312,13 +297,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00600, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00700, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -339,13 +322,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00700, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00800, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -366,13 +347,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00800, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00900, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::SERVICE_REMINDER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::SERVICE_REMINDER); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -393,13 +372,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_00900, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01000, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CONTENT_INFORMATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -420,13 +397,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01000, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01100, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::OTHER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -447,13 +422,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01100, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01200, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::CUSTOM); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::CUSTOM); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -462,8 +435,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01200, std::shared_ptr content = std::make_shared(normalContent); EXPECT_NE(content, nullptr); req->SetContent(content); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(false); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NON_SYSTEM_APP); SleepForFC(); } @@ -482,7 +453,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01300, EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); EXPECT_NE(normalContent, nullptr); @@ -495,8 +465,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_01300, std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false), (int)ERR_OK); IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NOT_ALLOWED); SleepForFC(); } @@ -672,11 +640,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04000, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04100, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); req->SetLabel("req's label"); - req->SetCreatorUid(1); req->SetAlertOneTime(true); std::string label = "publish's label"; std::shared_ptr normalContent = std::make_shared(); @@ -688,6 +656,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04100, req->SetContent(content); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); std::vector> allNotifications; EXPECT_EQ(advancedNotificationService_->GetAllActiveNotifications(allNotifications), (int)ERR_OK); EXPECT_EQ(allNotifications.size(), (size_t)1); @@ -710,14 +679,12 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04100, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04600, Function | SmallTest | Level1) { sptr req = new NotificationRequest(1); - req->SetCreatorUid(1); req->SetSlotType(NotificationConstant::OTHER); TestAddSlot(NotificationConstant::SlotType::OTHER); EXPECT_EQ(advancedNotificationService_->SetNotificationsEnabledForSpecialBundle( std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false), (int)ERR_OK); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->Publish(std::string(), req), (int)ERR_ANS_NOT_ALLOWED); } @@ -729,21 +696,18 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04600, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04700, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::OTHER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); std::string label = "testLabel"; { sptr req = new NotificationRequest(1); req->SetSlotType(NotificationConstant::OTHER); req->SetLabel(label); - req->SetCreatorUid(1); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK); } { sptr req = new NotificationRequest(2); req->SetSlotType(NotificationConstant::OTHER); req->SetLabel(label); - req->SetCreatorUid(1); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK); } EXPECT_EQ(advancedNotificationService_->Cancel(1, label), (int)ERR_OK); @@ -782,15 +746,13 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_04900, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_05000, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::OTHER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); int32_t notificationId = 2; std::string label = "testLabel"; sptr req = new NotificationRequest(notificationId); req->SetSlotType(NotificationConstant::OTHER); req->SetLabel(label); req->SetUnremovable(true); - req->SetCreatorUid(1); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK); EXPECT_EQ(advancedNotificationService_->Cancel(notificationId, label), (int)ERR_OK); } @@ -1019,7 +981,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_08600, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_08700, Function | SmallTest | Level1) { TestAddSlot(NotificationConstant::SlotType::OTHER); - sptr slot = new NotificationSlot(NotificationConstant::SlotType::OTHER); + sptr slot; EXPECT_EQ((int)advancedNotificationService_->GetSlotByType(NotificationConstant::OTHER, slot), (int)ERR_OK); EXPECT_EQ(slot->GetType(), NotificationConstant::SlotType::OTHER); } @@ -1166,8 +1128,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10000, const int BIG_PICTURE_HEIGHT = 300; const int ICON_SIZE = 36; - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1200,8 +1161,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10100, const int BIG_PICTURE_HEIGHT = 1024; const int ICON_SIZE = 36; - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1234,8 +1194,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10200, const int BIG_PICTURE_HEIGHT = 300; const int ICON_SIZE = 256; - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1264,8 +1223,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10200, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10300, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); ASSERT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1291,8 +1249,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10300, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10400, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); ASSERT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1308,6 +1265,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10400, req->SetGroupName(groupName); EXPECT_EQ(advancedNotificationService_->Publish("label", req), (int)ERR_OK); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); sptr bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID); EXPECT_EQ(advancedNotificationService_->RemoveGroupByBundle(bundleOption, groupName), (int)ERR_OK); SleepForFC(); @@ -1463,6 +1421,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_10900, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11000, Function | SmallTest | Level1) { + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1478,8 +1438,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11000, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11100, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1495,6 +1454,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11100, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11200, Function | SmallTest | Level1) { + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); std::string label = "testLabel"; { sptr req = new NotificationRequest(1); @@ -1512,6 +1473,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11200, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11300, Function | SmallTest | Level1) { + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); + IPCSkeleton::SetCallingUid(SYSTEM_SERVICE_UID); std::string label = "testLabel"; { sptr req = new NotificationRequest(1); @@ -1519,8 +1482,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_11300, req->SetLabel(label); EXPECT_EQ(advancedNotificationService_->PublishContinuousTaskNotification(req), (int)ERR_OK); } - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ( advancedNotificationService_->CancelContinuousTaskNotification(label, 1), (int)ERR_ANS_NOT_SYSTEM_SERVICE); } @@ -1555,8 +1517,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12000, std::string(), new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), false), (int)ERR_OK); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_NOT_ALLOWED); SleepForFC(); } @@ -1588,8 +1549,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12100, false, false); EXPECT_EQ(result, (int)ERR_OK); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED); SleepForFC(); } @@ -1608,10 +1568,10 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12200, sptr req = new NotificationRequest(notificationId); req->SetSlotType(NotificationConstant::OTHER); req->SetLabel(label); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->Publish(label, req), (int)ERR_OK); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); auto result = advancedNotificationService_->RemoveNotification( new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID), notificationId, label, NotificationConstant::CANCEL_REASON_DELETE); @@ -1626,6 +1586,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12200, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12300, Function | SmallTest | Level1) { + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); TestAddSlot(NotificationConstant::SlotType::CONTENT_INFORMATION); sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); @@ -1654,8 +1615,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12300, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12400, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(DLP_NATIVE_TOKEN); sptr req = new (std::nothrow) NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -1671,6 +1631,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12400, EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_ANS_DLP_HAP); SleepForFC(); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->Publish(label, req), ERR_OK); } @@ -1682,8 +1643,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12400, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_12500, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new (std::nothrow) NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -2025,11 +1985,13 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15200, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15300, Function | SmallTest | Level1) { + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); std::string cmd = "CMD"; std::string bundle = "Bundle"; int32_t userId = 4; std::vector dumpInfo; EXPECT_EQ(advancedNotificationService_->ShellDump(cmd, bundle, userId, dumpInfo), (int)ERR_ANS_INVALID_PARAM); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); } /** @@ -2053,8 +2015,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15400, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15500, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new (std::nothrow) NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -2088,8 +2049,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15500, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15600, Function | SmallTest | Level1) { - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new (std::nothrow) NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -2124,6 +2084,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15600, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15700, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "PrepareNotificationRequest_0100 test start"; + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); + sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); @@ -2153,9 +2115,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15700, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_15800, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "GenerateBundleOption_0100 test start"; - MockIsNonBundleName(true); + IPCSkeleton::SetCallingUid(NON_BUNDLE_NAME_UID); EXPECT_EQ(advancedNotificationService_->GenerateBundleOption(), nullptr); - MockIsNonBundleName(false); GTEST_LOG_(INFO) << "GenerateBundleOption_0100 test end"; } @@ -2220,6 +2181,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16200, GTEST_LOG_(INFO) << "ANS_CancelAsBundle_0200 test start"; TestAddSlot(NotificationConstant::SlotType::OTHER); + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); int32_t notificationId = 1; std::string representativeBundle = "RepresentativeBundle"; int32_t userId = 1; @@ -2258,6 +2220,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16300, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16400, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "ANS_AddSlots_0100 test start"; + + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); std::vector> slots; sptr slot = new NotificationSlot(NotificationConstant::SlotType::OTHER); slots.push_back(slot); @@ -2275,13 +2239,13 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16400, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16600, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "ANS_AddSlots_0300 test start"; - MockIsNonBundleName(true); + IPCSkeleton::SetCallingUid(NON_BUNDLE_NAME_UID); std::vector> slots; sptr slot = new NotificationSlot(NotificationConstant::SlotType::OTHER); slots.push_back(slot); EXPECT_EQ(advancedNotificationService_->AddSlots(slots), ERR_ANS_INVALID_BUNDLE); - MockIsNonBundleName(false); + GTEST_LOG_(INFO) << "ANS_AddSlots_0300 test end"; } @@ -2310,13 +2274,13 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16700, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16800, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "ANS_GetSlots_0100 test start"; - MockIsNonBundleName(true); + IPCSkeleton::SetCallingUid(NON_BUNDLE_NAME_UID); std::vector> slots; sptr slot = new NotificationSlot(NotificationConstant::SlotType::OTHER); slots.push_back(slot); EXPECT_EQ(advancedNotificationService_->GetSlots(slots), ERR_ANS_INVALID_BUNDLE); - MockIsNonBundleName(false); + GTEST_LOG_(INFO) << "ANS_GetSlots_0100 test end"; } @@ -2330,7 +2294,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16900, { GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test start"; - MockIsNonBundleName(true); + IPCSkeleton::SetCallingUid(NON_BUNDLE_NAME_UID); std::vector> notifications; EXPECT_EQ(advancedNotificationService_->GetActiveNotifications(notifications), ERR_ANS_INVALID_BUNDLE); uint64_t num = 1; @@ -2371,7 +2335,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16900, bool enabled = true; EXPECT_EQ(advancedNotificationService_->EnableDistributedSelf(enabled), ERR_ANS_INVALID_BUNDLE); - MockIsNonBundleName(false); + GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test end"; } @@ -2402,6 +2366,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17100, { GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test start"; + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); std::string key = "key"; int32_t removeReason = 0; sptr bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID); @@ -2493,8 +2458,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17200, GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test start"; TestAddSlot(NotificationConstant::SlotType::OTHER); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr req = new NotificationRequest(1); EXPECT_NE(req, nullptr); req->SetSlotType(NotificationConstant::SlotType::OTHER); @@ -2512,6 +2476,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17200, req->SetCreatorUserId(SUBSCRIBE_USER_INIT); std::shared_ptr notification = std::make_shared(req); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->DeleteAll(), ERR_OK); GTEST_LOG_(INFO) << "ANS_GetActiveNotifications_0100 test end"; @@ -2526,6 +2491,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17200, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17300, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "ANS_GetSlotsByBundle_0100 test start"; + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); + std::vector> slots; EXPECT_EQ(advancedNotificationService_->GetSlotsByBundle( new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID), slots), @@ -2548,6 +2515,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17400, { GTEST_LOG_(INFO) << "Subscribe_1000 test start"; + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); + auto subscriber = new TestAnsSubscriber(); sptr info = new NotificationSubscribeInfo(); sptr advancedNotificationServices = new (std::nothrow) AdvancedNotificationService(); @@ -2608,6 +2577,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17600, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17700, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test start"; + + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); sptr bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, NON_SYSTEM_APP_UID); sptr targetBundle = nullptr; bundleOption->SetBundleName("test"); @@ -2644,6 +2615,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_17900, { GTEST_LOG_(INFO) << "GetAppTargetBundle_1000 test start"; + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); int32_t reminderId = 1; sptr reminder = new ReminderRequest(reminderId); reminder->InitNotificationRequest(); @@ -2662,12 +2635,13 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_18000, { GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test start"; - MockIsNonBundleName(true); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); + IPCSkeleton::SetCallingUid(NON_BUNDLE_NAME_UID); int32_t reminderId = 1; sptr reminder = new ReminderRequest(reminderId); reminder->InitNotificationRequest(); EXPECT_EQ(advancedNotificationService_->PublishReminder(reminder), ERR_ANS_INVALID_BUNDLE); - MockIsNonBundleName(false); + GTEST_LOG_(INFO) << "GetAppTargetBundle_2000 test end"; } @@ -2944,43 +2918,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_19600, GTEST_LOG_(INFO) << "GetLocalNotificationKeys_0100 test end"; } -/** - * @tc.number : AdvancedNotificationServiceTest_19700 - * @tc.name : CheckDistributedNotificationType_0100 - * @tc.desc : Test CheckDistributedNotificationType function - * @tc.require : #I61RF2 - */ -HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_19700, Function | SmallTest | Level1) -{ - GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0100 test start"; - - sptr req = new NotificationRequest(); - EXPECT_EQ(advancedNotificationService_->CheckDistributedNotificationType(req), true); - - GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0100 test end"; -} - -/** - * @tc.number : AdvancedNotificationServiceTest_19800 - * @tc.name : CheckDistributedNotificationType_0200 - * @tc.desc : Test CheckDistributedNotificationType function - * @tc.require : #I61RF2 - */ -HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_19800, Function | SmallTest | Level1) -{ - GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0200 test start"; - - sptr req = new NotificationRequest(); - std::vector devices; - devices.push_back("a"); - devices.push_back("b"); - devices.push_back("c"); - req->GetNotificationDistributedOptions().SetDevicesSupportDisplay(devices); - EXPECT_EQ(advancedNotificationService_->CheckDistributedNotificationType(req), true); - - GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0200 test end"; -} - /** * @tc.number : AdvancedNotificationServiceTest_19900 * @tc.name : OnDistributedPublish_0100 @@ -2989,16 +2926,18 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_19800, */ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_19900, Function | SmallTest | Level1) { - GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0100 test start"; + GTEST_LOG_(INFO) << "OnDistributedPublish_0100 test start"; std::string deviceId = "DeviceId"; std::string bundleName = "BundleName"; sptr request = new NotificationRequest(); + bool canPublishWithoutApp = true; + bool remoteUsing = false; ASSERT_NE(nullptr, advancedNotificationService_); - advancedNotificationService_->OnDistributedPublish(deviceId, bundleName, request); + advancedNotificationService_->OnDistributedPublish(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); - GTEST_LOG_(INFO) << "CheckDistributedNotificationType_0100 test end"; + GTEST_LOG_(INFO) << "OnDistributedPublish_0100 test end"; } /** @@ -3014,9 +2953,11 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20000, std::string deviceId = "DeviceId"; std::string bundleName = "BundleName"; sptr request = new NotificationRequest(); + bool canPublishWithoutApp = true; + bool remoteUsing = false; ASSERT_NE(nullptr, advancedNotificationService_); - advancedNotificationService_->OnDistributedUpdate(deviceId, bundleName, request); + advancedNotificationService_->OnDistributedUpdate(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); GTEST_LOG_(INFO) << "OnDistributedUpdate_0100 test end"; } @@ -3035,47 +2976,14 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20100, std::string bundleName = "BundleName"; std::string label = "testLabel"; int32_t id = 1; + std::string localDeviceId = "LocalDeviceId"; ASSERT_NE(nullptr, advancedNotificationService_); - advancedNotificationService_->OnDistributedDelete(deviceId, bundleName, label, id); + advancedNotificationService_->OnDistributedDelete(deviceId, bundleName, label, id, localDeviceId); GTEST_LOG_(INFO) << "OnDistributedDelete_0100 test end"; } -/** - * @tc.number : AdvancedNotificationServiceTest_20200 - * @tc.name : CheckPublishWithoutApp_0100 - * @tc.desc : Test CheckPublishWithoutApp function - * @tc.require : #I61RF2 - */ -HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20200, Function | SmallTest | Level1) -{ - GTEST_LOG_(INFO) << "CheckPublishWithoutApp_0100 test start"; - - int32_t userId = 1; - sptr request = new NotificationRequest(); - EXPECT_EQ(advancedNotificationService_->CheckPublishWithoutApp(userId, request), false); - - GTEST_LOG_(INFO) << "CheckPublishWithoutApp_0100 test end"; -} - -/** - * @tc.number : AdvancedNotificationServiceTest_20300 - * @tc.name : CheckPublishWithoutApp_0200 - * @tc.desc : Test CheckPublishWithoutApp function - * @tc.require : #I61RF2 - */ -HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_20300, Function | SmallTest | Level1) -{ - GTEST_LOG_(INFO) << "CheckPublishWithoutApp_0200 test start"; - - int32_t userId = SYSTEM_APP_UID; - sptr request = new NotificationRequest(); - EXPECT_EQ(advancedNotificationService_->CheckPublishWithoutApp(userId, request), false); - - GTEST_LOG_(INFO) << "CheckPublishWithoutApp_0200 test end"; -} - /** * @tc.number : AdvancedNotificationServiceTest_20400 * @tc.name : TriggerRemoveWantAgent_0100 @@ -3235,6 +3143,9 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21200, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21300, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "PrepareNotificationRequest_0200 test start"; + IPCSkeleton::SetCallingUid(NON_SYSTEM_APP_UID); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); + sptr req = new NotificationRequest(); int32_t myNotificationId = 10; bool isAgentTrue = true; @@ -3300,8 +3211,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21600, EXPECT_NE(pushCallbackProxy, nullptr); sptr pushCallback = pushCallbackProxy->AsObject(); sptr checkRequest = new (std::nothrow) NotificationCheckRequest(); - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); + EXPECT_EQ(advancedNotificationService_->RegisterPushCallback(pushCallback, checkRequest), ERR_OK); advancedNotificationService_->UnregisterPushCallback(); @@ -3319,6 +3229,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21700, { GTEST_LOG_(INFO) << "RegisterPushCallback_0200 test start"; + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub(); EXPECT_NE(pushCallbackProxy, nullptr); sptr pushCallback = pushCallbackProxy->AsObject(); @@ -3342,8 +3253,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21800, { GTEST_LOG_(INFO) << "RegisterPushCallback_0300 test start"; - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub(); EXPECT_NE(pushCallbackProxy, nullptr); sptr pushCallback = pushCallbackProxy->AsObject(); @@ -3369,8 +3278,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21900, { GTEST_LOG_(INFO) << "UnregisterPushCallback_0100 test start"; - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub(); EXPECT_NE(pushCallbackProxy, nullptr); sptr pushCallback = pushCallbackProxy->AsObject(); @@ -3393,6 +3300,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_21900, HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_22000, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "UnregisterPushCallback_0200 test start"; + + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); EXPECT_EQ(advancedNotificationService_->UnregisterPushCallback(), (int)ERR_ANS_NON_SYSTEM_APP); GTEST_LOG_(INFO) << "UnregisterPushCallback_0200 test end"; @@ -3408,8 +3317,6 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_22100, { GTEST_LOG_(INFO) << "UnregisterPushCallback_0300 test start"; - MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); - MockIsSystemApp(true); EXPECT_EQ(advancedNotificationService_->UnregisterPushCallback(), (int)ERR_INVALID_OPERATION); GTEST_LOG_(INFO) << "UnregisterPushCallback_0300 test end"; @@ -3982,6 +3889,7 @@ HWTEST_F(AdvancedNotificationServiceTest, RecoverLiveViewFromDb_0001, Function | { GTEST_LOG_(INFO) << "RecoverLiveViewFromDb_0001 test start"; advancedNotificationService_->notificationList_.clear(); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr request = new NotificationRequest(1); std::shared_ptr liveViewContent = std::make_shared(); std::shared_ptr content = std::make_shared(liveViewContent); @@ -4023,6 +3931,7 @@ HWTEST_F(AdvancedNotificationServiceTest, RecoverLiveViewFromDb_0002, Function | { GTEST_LOG_(INFO) << "RecoverLiveViewFromDb_0002 test start"; advancedNotificationService_->notificationList_.clear(); + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr request = new NotificationRequest(1); std::shared_ptr liveViewContent = std::make_shared(); std::shared_ptr content = std::make_shared(liveViewContent); @@ -4058,6 +3967,8 @@ HWTEST_F(AdvancedNotificationServiceTest, RecoverLiveViewFromDb_0002, Function | HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0001, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "IsNeedPushCheckTest_0001 test start"; + + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr request = new NotificationRequest(); std::shared_ptr liveViewContent = std::make_shared(); auto status = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE; @@ -4080,6 +3991,8 @@ HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0002, Function | S { GTEST_LOG_(INFO) << "IsNeedPushCheckTest_0002 test start"; + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); + auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub(); EXPECT_NE(pushCallbackProxy, nullptr); sptr pushCallback = pushCallbackProxy->AsObject(); @@ -4120,6 +4033,8 @@ HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0002, Function | S HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0003, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "IsNeedPushCheckTest_0003 test start"; + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); + sptr request = new NotificationRequest(); request->SetSlotType(NotificationConstant::SlotType::CUSTOM); EXPECT_EQ(advancedNotificationService_->IsNeedPushCheck(request), false); @@ -4153,6 +4068,8 @@ HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0004, Function | S HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0005, Function | SmallTest | Level1) { GTEST_LOG_(INFO) << "IsNeedPushCheckTest_0005 test start"; + + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); sptr request = new NotificationRequest(); std::shared_ptr liveViewContent = std::make_shared(); auto status = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE; @@ -4175,6 +4092,8 @@ HWTEST_F(AdvancedNotificationServiceTest, IsNeedPushCheckTest_0006, Function | S { GTEST_LOG_(INFO) << "IsNeedPushCheckTest_0006 test start"; + IPCSkeleton::SetCallingTokenID(NON_NATIVE_TOKEN); + auto pushCallbackProxy = new (std::nothrow)MockPushCallBackStub(); EXPECT_NE(pushCallbackProxy, nullptr); sptr pushCallback = pushCallbackProxy->AsObject(); @@ -4246,6 +4165,7 @@ HWTEST_F(AdvancedNotificationServiceTest, GetActiveNotificationByFilter_0001, Fu GTEST_LOG_(INFO) << "GetActiveNotificationByFilter_0001 test start"; TestAddSlot(NotificationConstant::SlotType::LIVE_VIEW); + IPCSkeleton::SetCallingTokenID(NATIVE_TOKEN); sptr req; sptr bundleOption = new NotificationBundleOption(TEST_DEFUALT_BUNDLE, SYSTEM_APP_UID); diff --git a/services/ans/test/unittest/notification_preferences_test.cpp b/services/ans/test/unittest/notification_preferences_test.cpp index 06d1d93f7..f1f8f9b6b 100644 --- a/services/ans/test/unittest/notification_preferences_test.cpp +++ b/services/ans/test/unittest/notification_preferences_test.cpp @@ -1276,5 +1276,91 @@ HWTEST_F(NotificationPreferencesTest, GetTemplateSupported_00100, Function | Sma EXPECT_EQ((int)NotificationPreferences::GetInstance().GetTemplateSupported(templateName, support), (int)ERR_ANS_INVALID_PARAM); } + +/** + * @tc.name : SetDistributedEnable_00100 + * @tc.number : SetDistributedEnable_00100 + * @tc.desc : Set distributed notification enable. + */ +HWTEST_F(NotificationPreferencesTest, SetDistributedEnable_00100, Function | SmallTest | Level1) +{ + bool enable = true; + + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDistributedEnable(enable), (int)ERR_OK); +} + +/** + * @tc.name : GetDistributedEnable_00100 + * @tc.number : GetDistributedEnable_00100 + * @tc.desc : Get distributed notification enable. + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedEnable_00100, Function | SmallTest | Level1) +{ + bool enable; + + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDistributedEnable(enable), (int)ERR_OK); +} + +/** + * @tc.name : SetDistributedBundleEnable_00100 + * @tc.number : SetDistributedBundleEnable_00100 + * @tc.desc : Set distributed notification enable of a bundle. + */ +HWTEST_F(NotificationPreferencesTest, SetDistributedBundleEnable_00100, Function | SmallTest | Level1) +{ + bool enable = true; + sptr bundleOption = new NotificationBundleOption("", 783); + + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDistributedBundleEnable(bundleOption, enable), (int)ERR_OK); +} + +/** + * @tc.name : GetDistributedBundleEnable_00100 + * @tc.number : GetDistributedBundleEnable_00100 + * @tc.desc : Get distributed notification enable of a bundle. + */ +HWTEST_F(NotificationPreferencesTest, GetDistributedBundleEnable_00100, Function | SmallTest | Level1) +{ + sptr bundleOption = new NotificationBundleOption("", 783); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDistributedBundleEnable(bundleOption, true), (int)ERR_OK); + bool enable = false; + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDistributedBundleEnable(bundleOption, enable), (int)ERR_OK); + EXPECT_TRUE(enable); +} + +/** + * @tc.name : SetSyncEnabledWithoutApp_00100 + * @tc.number : SetSyncEnabledWithoutApp_00100 + * @tc.desc : Set enable without app into disturbe DB, return is ERR_OK. + */ +HWTEST_F(NotificationPreferencesTest, SetSyncEnabledWithoutApp_00100, Function | SmallTest | Level1) +{ + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetSyncEnabledWithoutApp(100, true), (int)ERR_OK); +} + +/** + * @tc.name : GetSyncEnabledWithoutApp_00100 + * @tc.number : GetSyncEnabledWithoutApp_00100 + * @tc.desc : Get enable without app from disturbe DB, return is ERR_OK. + */ +HWTEST_F(NotificationPreferencesTest, GetSyncEnabledWithoutApp_00100, Function | SmallTest | Level1) +{ + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetSyncEnabledWithoutApp(100, true), (int)ERR_OK); + bool enable = false; + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetSyncEnabledWithoutApp(100, enable), (int)ERR_OK); + EXPECT_TRUE(enable); +} + +/** + * @tc.name : DeleteDistributedBundleInfo_00100 + * @tc.number : DeleteDistributedBundleInfo_00100 + * @tc.desc : delete bundle info from disturbe DB , return is ERR_ANS_DISTRIBUTED_OPERATION_FAILED. + */ +HWTEST_F(NotificationPreferencesTest, DeleteDistributedBundleInfo_00100, Function | SmallTest | Level1) +{ + sptr bundleOption = new NotificationBundleOption("", 783); + EXPECT_EQ((int)NotificationPreferences::GetInstance().DeleteDistributedBundleInfo(bundleOption), + (int)ERR_ANS_DISTRIBUTED_OPERATION_FAILED); +} } // namespace Notification } // namespace OHOS diff --git a/services/distributed/BUILD.gn b/services/distributed/BUILD.gn index 07554f3a3..fb9ca50d4 100644 --- a/services/distributed/BUILD.gn +++ b/services/distributed/BUILD.gn @@ -22,6 +22,7 @@ config("ans_distributed_config") { } ohos_shared_library("libans_distributed") { + shlib_type = "sa" sanitize = { integer_overflow = true ubsan = true @@ -38,6 +39,8 @@ ohos_shared_library("libans_distributed") { "src/distributed_device_callback.cpp", "src/distributed_flow_control.cpp", "src/distributed_notification_manager.cpp", + "src/distributed_notification_service.cpp", + "src/distributed_notification_service_ability.cpp", "src/distributed_preferences.cpp", "src/distributed_preferences_database.cpp", "src/distributed_preferences_info.cpp", @@ -58,19 +61,30 @@ ohos_shared_library("libans_distributed") { external_deps = [ "ability_base:want", "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", "c_utils:utils", "device_manager:devicemanagersdk", "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", "kv_store:distributeddata_inner", - ] + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "ability_runtime:wantagent_innerkits", +] + + external_deps += component_external_deps if (ans_hitrace_usage) { external_deps += [ "hitrace:hitrace_meter" ] defines += [ "HITRACE_METER_ENABLE" ] } + if (distributed_notification_supported) { + defines += [ "DISTRIBUTED_NOTIFICATION_SUPPORTED" ] + } + subsystem_name = "${subsystem_name}" part_name = "${component_name}" } diff --git a/services/distributed/include/distributed_notification_manager.h b/services/distributed/include/distributed_notification_manager.h index d25604ca8..e1edadbf8 100644 --- a/services/distributed/include/distributed_notification_manager.h +++ b/services/distributed/include/distributed_notification_manager.h @@ -33,21 +33,6 @@ namespace OHOS { namespace Notification { class DistributedNotificationManager : public DelayedSingleton { public: - /** - * @brief Distributed notification callback function for remote device. - */ - struct IDistributedCallback { - std::function &request)> - OnPublish; - std::function &request)> - OnUpdate; - std::function - OnDelete; - }; - /** * @brief Publishes a local notification to remote device. * @@ -94,21 +79,6 @@ public: ErrCode DeleteRemoteNotification( const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); - /** - * @brief Register callback of distributed notification changed. - * - * @param callback Indicates the callback structure - * @return ErrCode Returns the register result. - */ - ErrCode RegisterCallback(const IDistributedCallback &callback); - - /** - * @brief Unregister Callback of Distributed notification changed. - * - * @return ErrCode Returns the unregister result. - */ - ErrCode UngegisterCallback(); - /** * @brief Get all distributed notification in database. * @@ -157,6 +127,9 @@ private: bool ResolveDistributedKey(const std::string &key, ResolveKey &resolveKey); bool GetDeviceIdFromKey(const std::string &key, std::string &deviceId); bool CheckDeviceId(const std::string &deviceId, const std::string &key); + bool CheckDistributedNotificationType(const sptr &request); + bool CheckPublishWithoutApp(const int32_t userId, const sptr &request); + static bool GetActiveUserId(int& userId); bool PublishCallback( const std::string &deviceId, const std::string &bundleName, sptr &request); @@ -171,7 +144,6 @@ private: std::shared_ptr databaseCb_; std::shared_ptr deviceCb_; - IDistributedCallback callback_ = {0}; std::shared_ptr distributedQueue_ = nullptr; DECLARE_DELAYED_SINGLETON(DistributedNotificationManager); @@ -180,4 +152,4 @@ private: } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H \ No newline at end of file +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_MANAGER_H diff --git a/services/distributed/include/distributed_notification_service.h b/services/distributed/include/distributed_notification_service.h new file mode 100644 index 000000000..2cdd3a37b --- /dev/null +++ b/services/distributed/include/distributed_notification_service.h @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_SERVICE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_SERVICE_H + + +#include +#include + +#include "refbase.h" + +#include "ans_distributed_stub.h" +#include "notification_request.h" +#include "notification_bundle_option.h" +#include "ffrt.h" + + +namespace OHOS { +namespace Notification { +class DistributedNotificationService final : public AnsDistributedStub { +public: + + ~DistributedNotificationService() override; + + DISALLOW_COPY_AND_MOVE(DistributedNotificationService); + + /** + * @brief Get the instance of service. + * + * @return Returns the instance. + */ + static sptr GetInstance(); + +private: + DistributedNotificationService(); + +private: + static sptr instance_; + static std::mutex instanceMutex_; + +public: + + /** + * @brief Obtains the death event of the Distributed KvStore service. + */ + ErrCode OnDistributedKvStoreDeathRecipient() override; + + /** + * @brief Removes a local notification. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + ErrCode Delete(const std::string &bundleName, const std::string &label, int32_t id) override; + + /** + * @brief Removes a remote notification. + * + * @param deviceId Indicates the ID of the device. + * @param bundleName Indicates the bundle name of the application whose notifications are to be remove. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be remove. + * @return ErrCode Returns the remove result. + */ + ErrCode DeleteRemoteNotification( + const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) override; + + /** + * @brief Publishes a local notification to remote device. + * + * @param bundleName Indicates the bundle name of the application whose notifications are to be publish. + * @param label Indicates the label of the notifications. + * @param id Indicates the bundle uid of the application whose notifications are to be publish. + * @param request Indicates the NotificationRequest object for setting the notification content. + * @return ErrCode Returns the publish result. + */ + ErrCode Publish( + const std::string &bundleName, const std::string &label, int32_t id, const sptr &request) override; + + /** + * @brief Check if any other device screen is on. + * + * @param isUsing True for any other device screen is on, otherwise false. + * @return Returns the error code. + */ + ErrCode CheckRemoteDevicesIsUsing(bool &isUsing) override; + + /** + * @brief Set screen status of local device. + * + * @param screenOn Indicates the local device screen status. + * @return Returns the error code. + */ + ErrCode SetLocalScreenStatus(bool screenOn) override; + + /** + * @brief Reset ffrt queue + */ + ErrCode ResetFfrtQueue() override; + +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_SERVICE_H \ No newline at end of file diff --git a/services/distributed/include/distributed_notification_service_ability.h b/services/distributed/include/distributed_notification_service_ability.h new file mode 100644 index 000000000..079605b4d --- /dev/null +++ b/services/distributed/include/distributed_notification_service_ability.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_SERVICE_ABILITY_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_SERVICE_ABILITY_H + +#include "system_ability.h" + +#include "distributed_notification_service.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace Notification { +class DistributedNotificationServiceAbility final : public SystemAbility { +public: + /** + * @brief The constructor of service ability. + * + * @param systemAbilityId Indicates the system ability id. + * @param runOnCreate Run the system ability on created. + */ + DistributedNotificationServiceAbility(const int32_t systemAbilityId, bool runOnCreate); + + /** + * @brief The deconstructor. + */ + ~DistributedNotificationServiceAbility() final; + + DISALLOW_COPY_AND_MOVE(DistributedNotificationServiceAbility); + DECLARE_SYSTEM_ABILITY(DistributedNotificationServiceAbility); + +private: + void OnStart() final; + void OnStop() final; + +private: + sptr service_; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_DISTRIBUTED_INCLUDE_NOTIFICATION_SERVICE_ABILITY_H \ No newline at end of file diff --git a/services/distributed/src/distributed_notification_manager.cpp b/services/distributed/src/distributed_notification_manager.cpp index 7e179d565..9ef337cb1 100644 --- a/services/distributed/src/distributed_notification_manager.cpp +++ b/services/distributed/src/distributed_notification_manager.cpp @@ -21,6 +21,11 @@ #include "ans_log_wrapper.h" #include "ans_watchdog.h" #include "hitrace_meter_adapter.h" +#include "os_account_manager.h" +#include "want_agent_helper.h" +#include "distributed_preferences.h" +#include "distributed_screen_status_manager.h" +#include "notification_helper.h" namespace OHOS { namespace Notification { @@ -63,10 +68,6 @@ DistributedNotificationManager::DistributedNotificationManager() DistributedNotificationManager::~DistributedNotificationManager() { ANS_LOGI("deconstructor"); - if (distributedQueue_ != nullptr) { - ffrt::task_handle handler = distributedQueue_->submit_h(std::bind([&]() { callback_ = {}; })); - distributedQueue_->wait(handler); - } } void DistributedNotificationManager::ResetFfrtQueue() @@ -136,6 +137,59 @@ bool DistributedNotificationManager::CheckDeviceId(const std::string &deviceId, return deviceId == resolveKey.deviceId; } +bool DistributedNotificationManager::CheckDistributedNotificationType(const sptr &request) +{ + auto deviceTypeList = request->GetNotificationDistributedOptions().GetDevicesSupportDisplay(); + if (deviceTypeList.empty()) { + return true; + } + + DistributedDatabase::DeviceInfo localDeviceInfo; + GetLocalDeviceInfo(localDeviceInfo); + for (auto device : deviceTypeList) { + if (atoi(device.c_str()) == localDeviceInfo.deviceTypeId) { + return true; + } + } + return false; +} + +bool DistributedNotificationManager::CheckPublishWithoutApp(const int32_t userId, const sptr &request) +{ + bool enabled = false; + DistributedPreferences::GetInstance()->GetSyncEnabledWithoutApp(userId, enabled); + if (!enabled) { + ANS_LOGE("enable is false, userId[%{public}d]", userId); + return false; + } + + std::shared_ptr wantAgent = request->GetWantAgent(); + if (!wantAgent) { + ANS_LOGE("Failed to get wantAgent!"); + return false; + } + + std::shared_ptr want = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(wantAgent); + if (!want || want->GetDeviceId().empty()) { + ANS_LOGE("Failed to get want!"); + return false; + } + + return true; +} + +bool DistributedNotificationManager::GetActiveUserId(int& userId) +{ + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); + if (activeUserId.size() > 0) { + userId = activeUserId[0]; + ANS_LOGD("Return active userId=%{public}d", userId); + return true; + } + return false; +} + void DistributedNotificationManager::OnDatabaseInsert( const std::string &deviceId, const std::string &key, const std::string &value) { @@ -279,9 +333,19 @@ bool DistributedNotificationManager::PublishCallback( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGI("start"); - if (callback_.OnPublish) { - callback_.OnPublish(deviceId, bundleName, request); + int32_t activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return false; } + if (!CheckDistributedNotificationType(request)) { + ANS_LOGD("CheckDistributedNotificationType is false."); + return true; + } + bool canPublishWithoutApp = CheckPublishWithoutApp(activeUserId, request); + bool remoteUsing = false; + DistributedScreenStatusManager::GetInstance()->CheckRemoteDevicesIsUsing(remoteUsing); + NotificationHelper::OnDistributedPublish(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); ANS_LOGI("end"); return true; @@ -291,9 +355,19 @@ bool DistributedNotificationManager::UpdateCallback( const std::string &deviceId, const std::string &bundleName, sptr &request) { ANS_LOGI("start"); - if (callback_.OnUpdate) { - callback_.OnUpdate(deviceId, bundleName, request); + int32_t activeUserId = -1; + if (!GetActiveUserId(activeUserId)) { + ANS_LOGE("Failed to get active user id!"); + return false; + } + if (!CheckDistributedNotificationType(request)) { + ANS_LOGD("device type not support display."); + return true; } + bool canPublishWithoutApp = CheckPublishWithoutApp(activeUserId, request); + bool remoteUsing = false; + DistributedScreenStatusManager::GetInstance()->CheckRemoteDevicesIsUsing(remoteUsing); + NotificationHelper::OnDistributedUpdate(deviceId, bundleName, request, canPublishWithoutApp, remoteUsing); ANS_LOGI("end"); return true; @@ -303,9 +377,12 @@ bool DistributedNotificationManager::DeleteCallback( const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) { ANS_LOGI("start"); - if (callback_.OnDelete) { - callback_.OnDelete(deviceId, bundleName, label, id); + std::string localDeviceId = ""; + DistributedDatabase::DeviceInfo localDeviceInfo; + if (GetLocalDeviceInfo(localDeviceInfo) == ERR_OK) { + localDeviceId = localDeviceInfo.deviceId; } + NotificationHelper::OnDistributedDelete(deviceId, bundleName, label, id, localDeviceId); ANS_LOGI("end"); return true; @@ -408,30 +485,6 @@ ErrCode DistributedNotificationManager::DeleteRemoteNotification( return ERR_OK; } -ErrCode DistributedNotificationManager::RegisterCallback(const IDistributedCallback &callback) -{ - ANS_LOGD("start"); - if (distributedQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = distributedQueue_->submit_h(std::bind([&]() { callback_ = callback; })); - distributedQueue_->wait(handler); - return ERR_OK; -} - -ErrCode DistributedNotificationManager::UngegisterCallback() -{ - ANS_LOGD("start"); - if (distributedQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ffrt::task_handle handler = distributedQueue_->submit_h(std::bind([&]() { callback_ = {}; })); - distributedQueue_->wait(handler); - return ERR_OK; -} - ErrCode DistributedNotificationManager::GetCurrentDistributedNotification( std::vector> &requestList) { diff --git a/services/distributed/src/distributed_notification_service.cpp b/services/distributed/src/distributed_notification_service.cpp new file mode 100644 index 000000000..ccfebe122 --- /dev/null +++ b/services/distributed/src/distributed_notification_service.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_notification_service.h" + +#include "ans_log_wrapper.h" + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "os_account_manager.h" +#include "distributed_preferences.h" +#include "distributed_notification_manager.h" +#include "distributed_screen_status_manager.h" +#include "ipc_skeleton.h" +#include "ans_permission_def.h" +#include "errors.h" + + +namespace OHOS { +namespace Notification { + +sptr DistributedNotificationService::instance_; +std::mutex DistributedNotificationService::instanceMutex_; + +sptr DistributedNotificationService::GetInstance() +{ + std::lock_guard lock(instanceMutex_); + + if (instance_ == nullptr) { + instance_ = new (std::nothrow) DistributedNotificationService(); + if (instance_ == nullptr) { + ANS_LOGE("Failed to create DistributedNotificationService instance"); + return nullptr; + } + } + return instance_; +} + +DistributedNotificationService::DistributedNotificationService() +{ + ANS_LOGI("constructor"); +} + +DistributedNotificationService::~DistributedNotificationService() +{ + ANS_LOGI("deconstructor"); +} + +ErrCode DistributedNotificationService::OnDistributedKvStoreDeathRecipient() +{ + ErrCode result = ERR_OK; + auto distributedInstance = DistributedNotificationManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + distributedInstance->OnDistributedKvStoreDeathRecipient(); + + return result; +} + +ErrCode DistributedNotificationService::Delete(const std::string &bundleName, const std::string &label, int32_t id) +{ + auto distributedInstance = DistributedNotificationManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + return distributedInstance->Delete(bundleName, label, id); +} + +ErrCode DistributedNotificationService::DeleteRemoteNotification(const std::string &deviceId, + const std::string &bundleName, const std::string &label, int32_t id) +{ + auto distributedInstance = DistributedNotificationManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + return distributedInstance->DeleteRemoteNotification(deviceId ,bundleName, label, id); +} + +ErrCode DistributedNotificationService::Publish(const std::string &bundleName, + const std::string &label, int32_t id, const sptr &request) +{ + auto distributedInstance = DistributedNotificationManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + return distributedInstance->Publish(bundleName, label, id, request); +} + +ErrCode DistributedNotificationService::CheckRemoteDevicesIsUsing(bool &isUsing) +{ + auto distributedInstance = DistributedScreenStatusManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + return distributedInstance->CheckRemoteDevicesIsUsing(isUsing); +} + +ErrCode DistributedNotificationService::SetLocalScreenStatus(bool screenOn) +{ + auto distributedInstance = DistributedScreenStatusManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + return distributedInstance->SetLocalScreenStatus(screenOn); +} + +ErrCode DistributedNotificationService::ResetFfrtQueue() +{ + auto distributedInstance = DistributedNotificationManager::GetInstance(); + if( distributedInstance == nullptr ) { + return ERR_INVALID_STATE; + } + distributedInstance->ResetFfrtQueue(); + return ERR_OK; +} + +} // namespace Notification +} // namespace OHOS diff --git a/services/distributed/src/distributed_notification_service_ability.cpp b/services/distributed/src/distributed_notification_service_ability.cpp new file mode 100644 index 000000000..2dde281a5 --- /dev/null +++ b/services/distributed/src/distributed_notification_service_ability.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "distributed_notification_service_ability.h" + +namespace OHOS { +namespace Notification { +namespace { +REGISTER_SYSTEM_ABILITY_BY_ID(DistributedNotificationServiceAbility, DISTRIBUTED_NOTIFICATION_SA_ID, true); +} + +DistributedNotificationServiceAbility::DistributedNotificationServiceAbility(const int32_t systemAbilityId, bool runOnCreate) + : SystemAbility(systemAbilityId, runOnCreate), service_(nullptr) +{} + +DistributedNotificationServiceAbility::~DistributedNotificationServiceAbility() +{} + +void DistributedNotificationServiceAbility::OnStart() +{ + if (service_ != nullptr) { + return; + } + + service_ = DistributedNotificationService::GetInstance(); + if (!Publish(service_)) { + return; + } +} + +void DistributedNotificationServiceAbility::OnStop() +{ + service_ = nullptr; +} +} // namespace Notification +} // namespace OHOS diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index 0dd4677ed..8bc6abb34 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -29,6 +29,7 @@ group("ans_unit_test") { ":distributed_preferences_branch_test", ":distributed_preferences_database_test", ":distributed_screen_status_manager_branch_test", + ":distributed_notification_service_test", ] } } @@ -59,6 +60,7 @@ ohos_unittest("ans_distributed_unit_test") { "${services_path}/distributed/test/unittest/mock/mock_device_manager_impl.cpp", "${services_path}/distributed/test/unittest/mock/mock_event_handler.cpp", "${services_path}/distributed/test/unittest/mock/mock_single_kv_store.cpp", + "${services_path}/distributed/src/distributed_notification_service.cpp", ] configs = [ @@ -86,6 +88,10 @@ ohos_unittest("ans_distributed_unit_test") { "image_framework:image_native", "ipc:ipc_core", "kv_store:distributeddata_inner", + "relational_store:native_rdb", + "os_account:os_account_innerkits", + "ability_runtime:wantagent_innerkits", + "samgr:samgr_proxy", ] subsystem_name = "${subsystem_name}" @@ -104,6 +110,7 @@ ohos_unittest("distributed_preferences_database_test") { "distributed_preferences_database_test/mock_distributed_flow_control.cpp", "distributed_preferences_database_test/mock_distributed_kv_data_manager.cpp", "distributed_preferences_database_test/mock_single_kv_store.cpp", + "distributed_notification_manager_branch_test/mock_distributed_database.cpp", ] configs = [ @@ -196,6 +203,7 @@ ohos_unittest("distributed_database_branch_test") { "distributed_database_branch_test/mock_distributed_flow_control.cpp", "distributed_database_branch_test/mock_distributed_kv_data_manager.cpp", "distributed_database_branch_test/mock_single_kv_store.cpp", + "distributed_notification_manager_branch_test/mock_distributed_database.cpp", ] configs = [ @@ -273,7 +281,7 @@ ohos_unittest("distributed_device_callback_test") { module_out_path = "${component_name}/unittest" include_dirs = [ "/${services_path}/distributed/include" ] - sources = [ "distributed_device_callback_test.cpp" ] + sources = [ "distributed_device_callback_test.cpp"] configs = [ "${services_path}/distributed/:ans_distributed_config", @@ -346,7 +354,10 @@ ohos_unittest("distributed_flow_control_test") { module_out_path = "${component_name}/unittest" include_dirs = [ "/${services_path}/distributed/include" ] - sources = [ "distributed_flow_control_test.cpp" ] + sources = [ + "${services_path}/distributed/src/distributed_flow_control.cpp", + "distributed_flow_control_test.cpp", + ] configs = [ "${services_path}/distributed/:ans_distributed_config", @@ -416,8 +427,63 @@ ohos_unittest("distributed_notification_manager_branch_test") { "image_framework:image_native", "ipc:ipc_core", "kv_store:distributeddata_inner", + "relational_store:native_rdb", ] subsystem_name = "${subsystem_name}" part_name = "${component_name}" } + +ohos_unittest("distributed_notification_service_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "${ffrt_path}/interfaces/kits", + ] + + sources = [ + "distributed_notification_service_test.cpp", + "distributed_notification_manager_branch_test/mock_distributed_database.cpp", + "${services_path}/distributed/src/distributed_preferences.cpp", + "${services_path}/distributed/src/distributed_preferences_info.cpp", + "${services_path}/distributed/src/distributed_notification_service.cpp", + "${services_path}/distributed/src/distributed_screen_status_manager.cpp", + "${services_path}/distributed/src/distributed_notification_manager.cpp", + "${services_path}/distributed/src/distributed_preferences_database.cpp", + "${services_path}/distributed/src/distributed_flow_control.cpp", + "${services_path}/distributed/src/distributed_database_callback.cpp", + "${services_path}/distributed/src/distributed_device_callback.cpp", + ] + + configs = [ + "${services_path}/distributed/:ans_distributed_config", + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + deps = [ + "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/distributed:libans_distributed", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "c_utils:utils", + "device_manager:devicemanagersdk", + "eventhandler:libeventhandler", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "os_account:os_account_innerkits", + "relational_store:native_rdb", + "ability_runtime:wantagent_innerkits", + "samgr:samgr_proxy", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_notification_manager_branch_test/distributed_notification_manager_branch_test.cpp b/services/distributed/test/unittest/distributed_notification_manager_branch_test/distributed_notification_manager_branch_test.cpp index 5e3b74ebf..7a0ecb58b 100755 --- a/services/distributed/test/unittest/distributed_notification_manager_branch_test/distributed_notification_manager_branch_test.cpp +++ b/services/distributed/test/unittest/distributed_notification_manager_branch_test/distributed_notification_manager_branch_test.cpp @@ -33,14 +33,6 @@ public: void SetUp() override; void TearDown() override; -public: - virtual void OnPublish( - const std::string &deviceId, const std::string &bundleName, sptr &request) {}; - virtual void OnUpdate( - const std::string &deviceId, const std::string &bundleName, sptr &request) {}; - virtual void OnDelete( - const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) {}; - protected: std::shared_ptr distributedManager_; }; @@ -107,29 +99,10 @@ HWTEST_F(DistributedNotificationManagerBranchTest, DistributedNotificationManage /** * @tc.name : DistributedNotificationManager_00700 * @tc.number : DistributedNotificationManager_00700 - * @tc.desc : test PublishCallback function and callback_.OnPublish is not nullptr. + * @tc.desc : test PublishCallback function. */ HWTEST_F(DistributedNotificationManagerBranchTest, DistributedNotificationManager_00700, Function | SmallTest | Level1) { - DistributedNotificationManager::IDistributedCallback callback = { - .OnPublish = std::bind(&DistributedNotificationManagerBranchTest::OnPublish, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnUpdate = std::bind(&DistributedNotificationManagerBranchTest::OnUpdate, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnDelete = std::bind(&DistributedNotificationManagerBranchTest::OnDelete, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4), - }; - distributedManager_->callback_ = callback; std::string deviceId = "test1"; std::string bundleName = "test2"; sptr request = nullptr; @@ -139,29 +112,10 @@ HWTEST_F(DistributedNotificationManagerBranchTest, DistributedNotificationManage /** * @tc.name : DistributedNotificationManager_00800 * @tc.number : DistributedNotificationManager_00800 - * @tc.desc : test UpdateCallback function and callback_.OnUpdate is not nullptr. + * @tc.desc : test UpdateCallback function. */ HWTEST_F(DistributedNotificationManagerBranchTest, DistributedNotificationManager_00800, Function | SmallTest | Level1) { - DistributedNotificationManager::IDistributedCallback callback = { - .OnPublish = std::bind(&DistributedNotificationManagerBranchTest::OnPublish, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnUpdate = std::bind(&DistributedNotificationManagerBranchTest::OnUpdate, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnDelete = std::bind(&DistributedNotificationManagerBranchTest::OnDelete, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4), - }; - distributedManager_->callback_ = callback; std::string deviceId = "test1"; std::string bundleName = "test2"; sptr request = nullptr; @@ -171,29 +125,10 @@ HWTEST_F(DistributedNotificationManagerBranchTest, DistributedNotificationManage /** * @tc.name : DistributedNotificationManager_00900 * @tc.number : DistributedNotificationManager_00900 - * @tc.desc : test DeleteCallback function and callback_.OnDelete is not nullptr. + * @tc.desc : test DeleteCallback function. */ HWTEST_F(DistributedNotificationManagerBranchTest, DistributedNotificationManager_00900, Function | SmallTest | Level1) { - DistributedNotificationManager::IDistributedCallback callback = { - .OnPublish = std::bind(&DistributedNotificationManagerBranchTest::OnPublish, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnUpdate = std::bind(&DistributedNotificationManagerBranchTest::OnUpdate, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3), - .OnDelete = std::bind(&DistributedNotificationManagerBranchTest::OnDelete, - this, - std::placeholders::_1, - std::placeholders::_2, - std::placeholders::_3, - std::placeholders::_4), - }; - distributedManager_->callback_ = callback; std::string deviceId = "test1"; std::string bundleName = "test2"; std::string label = "test3"; diff --git a/services/distributed/test/unittest/distributed_notification_manager_branch_test/mock_distributed_database.cpp b/services/distributed/test/unittest/distributed_notification_manager_branch_test/mock_distributed_database.cpp index 77fc20f6a..6cb290d24 100755 --- a/services/distributed/test/unittest/distributed_notification_manager_branch_test/mock_distributed_database.cpp +++ b/services/distributed/test/unittest/distributed_notification_manager_branch_test/mock_distributed_database.cpp @@ -83,5 +83,44 @@ bool DistributedDatabase::RecreateDistributedDB() { return false; } + +void DistributedDatabase::GetKvStore() +{} + +DistributedDatabase::DistributedDatabase( + std::shared_ptr databaseCb, std::shared_ptr deviceCb){} + +DistributedDatabase::~DistributedDatabase() +{} + + +bool DistributedDatabase::GetFromDistributedDB(const std::string &key, std::string &value) +{ + return true; +} + +bool DistributedDatabase::DeleteToDistributedDB(const std::string &key) +{ + return true; +} + +bool DistributedDatabase::ClearDataByDevice(const std::string &deviceId) +{ + return true; +} + +void DistributedDatabase::GetKvDataManager() +{} + +bool DistributedDatabase::CheckKvDataManager() +{ + return false; +} + +bool DistributedDatabase::CheckKvStore() +{ + return false; +} + } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_notification_manager_test.cpp b/services/distributed/test/unittest/distributed_notification_manager_test.cpp index f3a9ddca0..4237c34aa 100644 --- a/services/distributed/test/unittest/distributed_notification_manager_test.cpp +++ b/services/distributed/test/unittest/distributed_notification_manager_test.cpp @@ -19,6 +19,7 @@ #define private public #include "ans_inner_errors.h" #include "distributed_notification_manager.h" +#undef private using namespace testing::ext; @@ -29,14 +30,6 @@ public: void SetUp() override; void TearDown() override; -public: - virtual void OnPublish( - const std::string &deviceId, const std::string &bundleName, sptr &request) {}; - virtual void OnUpdate( - const std::string &deviceId, const std::string &bundleName, sptr &request) {}; - virtual void OnDelete( - const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) {}; - protected: std::shared_ptr distributedManager_; }; @@ -64,6 +57,7 @@ HWTEST_F(DistributedNotificationManagerTest, Distributed_Publish_00100, Function request->SetLabel("