From 85c848a56c58748cd74c37f8052d9e591428183f Mon Sep 17 00:00:00 2001 From: zhaoyuan17 Date: Wed, 15 Sep 2021 15:54:26 +0800 Subject: [PATCH] Federated build fetch LTS Signed-off-by: zhaoyuan17 --- BUILD.gn | 2 +- .../common/log/include/event_log_wrapper.h | 3 +- frameworks/core/include/common_event.h | 2 +- .../core/include/common_event_listener.h | 2 + frameworks/core/src/common_event.cpp | 9 +- frameworks/core/src/common_event_listener.cpp | 15 + interfaces/kits/js/@ohos.commonEvent.d.ts | 950 +++++++++++++ .../kits/js/commonEvent/commonEventData.d.ts | 52 + .../commonEvent/commonEventPublishData.d.ts | 69 + .../commonEvent/commonEventSubscribeInfo.d.ts | 65 + .../js/commonEvent/commonEventSubscriber.d.ts | 230 ++++ .../napi/common_event/include/common_event.h | 150 +-- .../kits/napi/common_event/include/support.h | 4 +- .../napi/common_event/src/common_event.cpp | 1185 +++++++++-------- .../kits/napi/common_event/src/support.cpp | 4 +- sa_profile/3299.xml | 2 +- services/ces/include/history_event_record.h | 4 +- .../ces/src/common_event_control_manager.cpp | 2 +- .../test/mock/include/mock_bundle_manager.h | 33 + services/test/moduletest/BUILD.gn | 16 +- .../BUILD.gn | 16 +- .../BUILD.gn | 16 +- ..._event_publish_manager_event_unit_test.cpp | 2 +- ..._event_publish_ordered_event_unit_test.cpp | 27 +- .../mock/include/mock_bundle_manager.h | 34 +- .../unittest/mock/src/mock_bundle_manager.cpp | 2 +- test/systemtest/BUILD.gn | 16 +- test/systemtest/common/acts/BUILD.gn | 16 +- .../acts/actsCESCESpublishInfoTest/BUILD.gn | 16 +- .../actsCESCESpublishInfoTest.cpp | 167 +-- .../common/acts/actsCESDataTest/BUILD.gn | 16 +- .../acts/actsCESDataTest/actsCESDataTest.cpp | 2 +- .../common/acts/actsCESManagertest/BUILD.gn | 16 +- .../actsCESManagertest/actsCESManagertest.cpp | 843 ++++++------ .../acts/actsCESMatchingSkillsTest/BUILD.gn | 16 +- .../actsCESMatchingSkillsTest.cpp | 120 +- .../actsCESSubscribeInfoTest.cpp | 80 +- .../common/acts/actsfuzztest/BUILD.gn | 17 +- test/systemtest/common/ces/BUILD.gn | 16 +- .../BUILD.gn | 16 +- .../BUILD.gn | 16 +- .../resource/cesSTPermissionAndAB/BUILD.gn | 2 + .../resource/cesSTPermissionDefaultA/BUILD.gn | 2 + .../resource/cesSTPermissionOrAB/BUILD.gn | 2 + .../resource/cesSTPermissionOrAX/BUILD.gn | 2 + .../resource/fuzzTest/fuzzconfig/config.json | 1 - .../fuzzTest/include/fuzzTestManager.h | 6 +- .../resource/fuzzTest/src/fuzzTestManager.cpp | 13 +- .../common/resource/fuzzTest/src/getparam.cpp | 2 +- tools/test/systemtest/cem/BUILD.gn | 7 +- 50 files changed, 2956 insertions(+), 1350 deletions(-) create mode 100644 interfaces/kits/js/@ohos.commonEvent.d.ts create mode 100644 interfaces/kits/js/commonEvent/commonEventData.d.ts create mode 100644 interfaces/kits/js/commonEvent/commonEventPublishData.d.ts create mode 100644 interfaces/kits/js/commonEvent/commonEventSubscribeInfo.d.ts create mode 100644 interfaces/kits/js/commonEvent/commonEventSubscriber.d.ts mode change 100644 => 100755 test/systemtest/common/acts/actsfuzztest/BUILD.gn mode change 100644 => 100755 test/systemtest/common/resource/cesSTPermissionAndAB/BUILD.gn mode change 100644 => 100755 test/systemtest/common/resource/cesSTPermissionDefaultA/BUILD.gn mode change 100644 => 100755 test/systemtest/common/resource/cesSTPermissionOrAB/BUILD.gn mode change 100644 => 100755 test/systemtest/common/resource/cesSTPermissionOrAX/BUILD.gn mode change 100644 => 100755 tools/test/systemtest/cem/BUILD.gn diff --git a/BUILD.gn b/BUILD.gn index 8cee824a..11f42d3f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -9,6 +9,6 @@ # 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. +# limitations under the License. import("//build/ohos.gni") diff --git a/frameworks/common/log/include/event_log_wrapper.h b/frameworks/common/log/include/event_log_wrapper.h index b2d14d82..fe9c7cc2 100644 --- a/frameworks/common/log/include/event_log_wrapper.h +++ b/frameworks/common/log/include/event_log_wrapper.h @@ -55,9 +55,10 @@ private: #define PRINT_LOG(LEVEL, Level, fmt, ...) \ if (EventLogWrapper::JudgeLevel(EventLogLevel::LEVEL)) \ OHOS::HiviewDFX::HiLog::Level(Event_LABEL, \ - "[%{public}s(%{public}s)] " fmt, \ + "[%{public}s:(%{public}s):%{public}d] " fmt, \ EventLogWrapper::GetBriefFileName(__FILE__).c_str(), \ __FUNCTION__, \ + __LINE__, \ ##__VA_ARGS__) #define EVENT_LOGD(fmt, ...) PRINT_LOG(DEBUG, Debug, fmt, ##__VA_ARGS__) diff --git a/frameworks/core/include/common_event.h b/frameworks/core/include/common_event.h index 1e76c27d..3d5cae24 100644 --- a/frameworks/core/include/common_event.h +++ b/frameworks/core/include/common_event.h @@ -142,7 +142,7 @@ private: std::mutex mutex_; std::mutex eventListenersMutex_; sptr commonEventProxy_; - std::map, sptr> eventListeners_; + std::map, sptr> eventListeners_; sptr recipient_; const unsigned int SUBSCRIBER_MAX_SIZE = 200; }; diff --git a/frameworks/core/include/common_event_listener.h b/frameworks/core/include/common_event_listener.h index 45c46adb..a2c74c93 100644 --- a/frameworks/core/include/common_event_listener.h +++ b/frameworks/core/include/common_event_listener.h @@ -33,6 +33,8 @@ public: virtual void NotifyEvent(const CommonEventData &CommonEventData, const bool &ordered, const bool &sticky) override; + void Stop(); + private: ErrCode Init(); diff --git a/frameworks/core/src/common_event.cpp b/frameworks/core/src/common_event.cpp index ea4b6046..b7becd11 100644 --- a/frameworks/core/src/common_event.cpp +++ b/frameworks/core/src/common_event.cpp @@ -118,7 +118,8 @@ bool CommonEvent::UnSubscribeCommonEvent(const std::shared_ptr lock(eventListenersMutex_); auto eventListener = eventListeners_.find(subscriber); if (eventListener != eventListeners_.end()) { - if (commonEventProxy_->UnsubscribeCommonEvent(eventListener->second)) { + if (commonEventProxy_->UnsubscribeCommonEvent(eventListener->second->AsObject())) { + eventListener->second->Stop(); eventListeners_.erase(eventListener); return true; } @@ -262,7 +263,7 @@ int CommonEvent::CreateCommonEventListener( auto eventListener = eventListeners_.find(subscriber); if (eventListener != eventListeners_.end()) { - commonEventListener = eventListener->second; + commonEventListener = eventListener->second->AsObject(); EVENT_LOGW("subscriber has common event listener"); return ALREADY_SUBSCRIBED; } else { @@ -272,13 +273,13 @@ int CommonEvent::CreateCommonEventListener( return SUBSCRIBE_FAILD; } - sptr listener = new CommonEventListener(subscriber); + sptr listener = new CommonEventListener(subscriber); if (!listener) { EVENT_LOGE("the common event listener is null"); return SUBSCRIBE_FAILD; } commonEventListener = listener->AsObject(); - eventListeners_[subscriber] = commonEventListener; + eventListeners_[subscriber] = listener; } return INITIAL_SUBSCRIPTION; diff --git a/frameworks/core/src/common_event_listener.cpp b/frameworks/core/src/common_event_listener.cpp index 3ca18477..4ee7b642 100644 --- a/frameworks/core/src/common_event_listener.cpp +++ b/frameworks/core/src/common_event_listener.cpp @@ -114,5 +114,20 @@ void CommonEventListener::OnReceiveEvent( commonEventSubscriber_->GetAsyncCommonEventResult()->FinishCommonEvent(); } } + +void CommonEventListener::Stop() +{ + if (handler_) { + handler_.reset(); + } + + if (CommonEventSubscribeInfo::HANDLER == commonEventSubscriber_->GetSubscribeInfo().GetThreadMode()) { + return; + } + + if (runner_) { + runner_.reset(); + } +} } // namespace EventFwk } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/@ohos.commonEvent.d.ts b/interfaces/kits/js/@ohos.commonEvent.d.ts new file mode 100644 index 00000000..cff53acc --- /dev/null +++ b/interfaces/kits/js/@ohos.commonEvent.d.ts @@ -0,0 +1,950 @@ +/* + * 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. + */ +import { AsyncCallback } from './basic'; +import { CommonEventData } from './commonEvent/commonEventData'; +import { CommonEventSubscriber } from './commonEvent/commonEventSubscriber' +import { CommonEventSubscribeInfo } from './commonEvent/commonEventSubscribeInfo'; +import { CommonEventPublishData } from './commonEvent/commonEventPublishData'; + +/** + * the defination for commonevent + * @name commonEvent + * @since 7 + * @permission N/A + */ +declare namespace commonEvent { + /** + * Publishes an ordered, sticky, or standard common event. + * + * @since 7 + * @param data Indicate the CommonEventPublishData containing the common event content and attributes. + * @param callback Specified callback method. + * @return - + */ + function publish(event: string, callback: AsyncCallback): void; + + /** + * Publishes an ordered, sticky, or standard common event. + * + * @since 7 + * @param data Indicate the CommonEventPublishData containing the common event content and attributes. + * @param callback Specified callback method. + * @return - + */ + function publish(event: string, options: CommonEventPublishData, callback: AsyncCallback): void; + + + /** + * create the CommonEventSubscriber for the SubscriberInfo. + * + * @since 7 + * @param subscribeInfo Indicate the information of the subscriber. + * @param callback Specified callback method. + * @return - + */ + function createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback): void; + + /** + * create the CommonEventSubscriber for the SubscriberInfo. + * + * @since 7 + * @param subscribeInfo Indicate the information of the subscriber. + * @param callback Specified callback method. + * @return - + */ + function createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise; + + /** + * subscribe an ordered, sticky, or standard common event. + * + * @since 7 + * @param subscriber Indicate the subscriber of the common event. + * @param callback Specified callback method. + * @return - + */ + function subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback): void; + + /** + * unsubscribe an ordered, sticky, or standard common event. + * + * @since 7 + * @param subscriber Indicate the subscriber of the common event. + * @param callback Specified callback method. + * @return - + */ + function unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback): void; + + /** + * the event type that the commonEvent supported + * @name Support + * @since 7 + * @permission N/A + */ + export enum Support { + /** + * this commonEvent means when the device is booted or system upgrade completed, and only be sent by system. + */ + COMMON_EVENT_BOOT_COMPLETED = "usual.event.BOOT_COMPLETED", + + /** + * this commonEvent means when the device finnish booting, but still in the locked state. + */ + COMMON_EVENT_LOCKED_BOOT_COMPLETED = "usual.event.LOCKED_BOOT_COMPLETED", + + /** + * this commonEvent means when the device is shutting down, note: turn off, not sleeping. + */ + COMMON_EVENT_SHUTDOWN = "usual.event.SHUTDOWN", + + /** + * this commonEvent means when the charging state, level and so on about the battery. + */ + COMMON_EVENT_BATTERY_CHANGED = "usual.event.BATTERY_CHANGED", + + /** + * this commonEvent means when the device in low battery state.. + */ + COMMON_EVENT_BATTERY_LOW = "usual.event.BATTERY_LOW", + + /** + * this commonEvent means when the battery level is an ok state. + */ + COMMON_EVENT_BATTERY_OKAY = "usual.event.BATTERY_OKAY", + + /** + * this commonEvent means when the other power is connected to the device. + */ + COMMON_EVENT_POWER_CONNECTED = "usual.event.POWER_CONNECTED", + + /** + * this commonEvent means when the other power is removed from the device. + */ + COMMON_EVENT_POWER_DISCONNECTED = "usual.event.POWER_DISCONNECTED", + + /** + * this commonEvent means when the screen is turned off. + */ + COMMON_EVENT_SCREEN_OFF = "usual.event.SCREEN_OFF", + + /** + * this commonEvent means when the device is waked up and interactive. + */ + COMMON_EVENT_SCREEN_ON = "usual.event.SCREEN_ON", + + /** + * this commonEvent means when the user is present after the device waked up. + */ + COMMON_EVENT_USER_PRESENT = "usual.event.USER_PRESENT", + + /** + * this commonEvent means when the current time is changed. + */ + COMMON_EVENT_TIME_TICK = "usual.event.TIME_TICK", + + /** + * this commonEvent means when the time is set. + */ + COMMON_EVENT_TIME_CHANGED = "usual.event.TIME_CHANGED", + + /** + * this commonEvent means when the current date is changed. + */ + COMMON_EVENT_DATE_CHANGED = "usual.event.DATE_CHANGED", + + /** + * this commonEvent means when the time zone is changed. + */ + COMMON_EVENT_TIMEZONE_CHANGED = "usual.event.TIMEZONE_CHANGED", + + /** + * this commonEvent means when the dialog to dismiss. + */ + COMMON_EVENT_CLOSE_SYSTEM_DIALOGS = "usual.event.CLOSE_SYSTEM_DIALOGS", + + /** + * this commonEvent means when a new application package is installed on the device. + */ + COMMON_EVENT_PACKAGE_ADDED = "usual.event.PACKAGE_ADDED", + + /** + * this commonEvent means when a new version application package is installed on the device and + * replace the old version.the data contains the name of the package. + */ + COMMON_EVENT_PACKAGE_REPLACED = "usual.event.PACKAGE_REPLACED", + + /** + * this commonEvent means when a new version application package is installed on the device and + * replace the old version, it does not contain additional data and only be sent to the replaced application. + */ + COMMON_EVENT_MY_PACKAGE_REPLACED = "usual.event.MY_PACKAGE_REPLACED", + + /** + * this commonEvent means when an existing application package is removed from the device. + */ + COMMON_EVENT_PACKAGE_REMOVED = "usual.event.PACKAGE_REMOVED", + + /** + * this commonEvent means when an existing application package is removed from the device. + */ + COMMON_EVENT_BUNDLE_REMOVED = "usual.event.BUNDLE_REMOVED", + + /** + * this commonEvent means when an existing application package is completely removed from the device. + */ + COMMON_EVENT_PACKAGE_FULLY_REMOVED = "usual.event.PACKAGE_FULLY_REMOVED", + + /** + * this commonEvent means when an existing application package has been changed. + */ + COMMON_EVENT_PACKAGE_CHANGED = "usual.event.PACKAGE_CHANGED", + + /** + * this commonEvent means the user has restarted a package, and all of its processes have been killed. + */ + COMMON_EVENT_PACKAGE_RESTARTED = "usual.event.PACKAGE_RESTARTED", + + /** + * this commonEvent means the user has cleared the package data. + */ + COMMON_EVENT_PACKAGE_DATA_CLEARED = "usual.event.PACKAGE_DATA_CLEARED", + + /** + * this commonEvent means the packages have been suspended. + */ + COMMON_EVENT_PACKAGES_SUSPENDED = "usual.event.PACKAGES_SUSPENDED", + + /** + * this commonEvent means the packages have been un-suspended. + */ + COMMON_EVENT_PACKAGES_UNSUSPENDED = "usual.event.PACKAGES_UNSUSPENDED", + + /** + * this commonEvent Sent to a package that has been suspended by the system. + */ + COMMON_EVENT_MY_PACKAGE_SUSPENDED = "usual.event.MY_PACKAGE_SUSPENDED", + + /** + * Sent to a package that has been un-suspended. + */ + COMMON_EVENT_MY_PACKAGE_UNSUSPENDED = "usual.event.MY_PACKAGE_UNSUSPENDED", + + /** + * a user id has been removed from the system. + */ + COMMON_EVENT_UID_REMOVED = "usual.event.UID_REMOVED", + + /** + * the application is first launched after installed. + */ + COMMON_EVENT_PACKAGE_FIRST_LAUNCH = "usual.event.PACKAGE_FIRST_LAUNCH", + + /** + * sent by system package verifier when a package need to be verified. + */ + COMMON_EVENT_PACKAGE_NEEDS_VERIFICATION = + "usual.event.PACKAGE_NEEDS_VERIFICATION", + + /** + * sent by system package verifier when a package is verified. + */ + COMMON_EVENT_PACKAGE_VERIFIED = "usual.event.PACKAGE_VERIFIED", + + /** + * Resources for a set of packages (which were previously unavailable) are currently + * available since the media on which they exist is available. + */ + COMMON_EVENT_EXTERNAL_APPLICATIONS_AVAILABLE = + "usual.event.EXTERNAL_APPLICATIONS_AVAILABLE", + + /** + * Resources for a set of packages are currently unavailable since the media on which they exist is unavailable. + */ + COMMON_EVENT_EXTERNAL_APPLICATIONS_UNAVAILABLE = + "usual.event.EXTERNAL_APPLICATIONS_UNAVAILABLE", + + /** + * the device configuration such as orientation,locale have been changed. + */ + COMMON_EVENT_CONFIGURATION_CHANGED = "usual.event.CONFIGURATION_CHANGED", + + /** + * The current device's locale has changed. + */ + COMMON_EVENT_LOCALE_CHANGED = "usual.event.LOCALE_CHANGED", + + /** + * Indicates low memory condition notification acknowledged by user and package management should be started. + */ + COMMON_EVENT_MANAGE_PACKAGE_STORAGE = "usual.event.MANAGE_PACKAGE_STORAGE", + + /** + * sent by the smart function when the system in drive mode. + */ + COMMON_EVENT_DRIVE_MODE = "common.event.DRIVE_MODE", + + /** + * sent by the smart function when the system in home mode. + */ + COMMON_EVENT_HOME_MODE = "common.event.HOME_MODE", + + /** + * sent by the smart function when the system in office mode. + */ + COMMON_EVENT_OFFICE_MODE = "common.event.OFFICE_MODE", + + /** + * remind new user of preparing to start. + */ + COMMON_EVENT_USER_STARTED = "usual.event.USER_STARTED", + + /** + * remind previous user of that the service has been the background. + */ + COMMON_EVENT_USER_BACKGROUND = "usual.event.USER_BACKGROUND", + + /** + * remind new user of that the service has been the foreground. + */ + COMMON_EVENT_USER_FOREGROUND = "usual.event.USER_FOREGROUND", + + /** + * remind new user of that the service has been switched to new user. + */ + COMMON_EVENT_USER_SWITCHED = "usual.event.USER_SWITCHED", + + /** + * remind new user of that the service has been starting. + */ + COMMON_EVENT_USER_STARTING = "usual.event.USER_STARTING", + + /** + * remind new user of that the service has been unlocked. + */ + COMMON_EVENT_USER_UNLOCKED = "usual.event.USER_UNLOCKED", + + /** + * remind new user of that the service has been stopping. + */ + COMMON_EVENT_USER_STOPPING = "usual.event.USER_STOPPING", + + /** + * remind new user of that the service has stopped. + */ + COMMON_EVENT_USER_STOPPED = "usual.event.USER_STOPPED", + + /** + * HW id login successfully. + */ + COMMON_EVENT_HWID_LOGIN = "common.event.HWID_LOGIN", + + /** + * HW id logout successfully. + */ + COMMON_EVENT_HWID_LOGOUT = "common.event.HWID_LOGOUT", + + /** + * HW id is invalid. + */ + COMMON_EVENT_HWID_TOKEN_INVALID = "common.event.HWID_TOKEN_INVALID", + + /** + * HW id logs off. + */ + COMMON_EVENT_HWID_LOGOFF = "common.event.HWID_LOGOFF", + + /** + * WIFI state. + */ + COMMON_EVENT_WIFI_POWER_STATE = "usual.event.wifi.POWER_STATE", + + /** + * WIFI scan results. + */ + COMMON_EVENT_WIFI_SCAN_FINISHED = "usual.event.wifi.SCAN_FINISHED", + + /** + * WIFI RSSI change. + */ + COMMON_EVENT_WIFI_RSSI_VALUE = "usual.event.wifi.RSSI_VALUE", + + /** + * WIFI connect state. + */ + COMMON_EVENT_WIFI_CONN_STATE = "usual.event.wifi.CONN_STATE", + + /** + * WIFI hotspot state. + */ + COMMON_EVENT_WIFI_HOTSPOT_STATE = "usual.event.wifi.HOTSPOT_STATE", + + /** + * WIFI ap sta join. + */ + COMMON_EVENT_WIFI_AP_STA_JOIN = "usual.event.wifi.WIFI_HS_STA_JOIN", + + /** + * WIFI ap sta join. + */ + COMMON_EVENT_WIFI_AP_STA_LEAVE = "usual.event.wifi.WIFI_HS_STA_LEAVE", + + /** + * Indicates Wi-Fi MpLink state notification acknowledged by binding or unbinding MpLink. + */ + COMMON_EVENT_WIFI_MPLINK_STATE = "usual.event.wifi.mplink.STATE_CHANGE", + + /** + * Indicates Wi-Fi P2P connection state notification acknowledged by connecting or disconnecting P2P. + */ + COMMON_EVENT_WIFI_P2P_CONN_STATE = "usual.event.wifi.p2p.CONN_STATE_CHANGE", + + /** + * Indicates that the Wi-Fi P2P state change. + */ + COMMON_EVENT_WIFI_P2P_STATE_CHANGED = "usual.event.wifi.p2p.STATE_CHANGE", + + /** + * Indicates that the Wi-Fi P2P peers state change. + */ + COMMON_EVENT_WIFI_P2P_PEERS_STATE_CHANGED = + "usual.event.wifi.p2p.DEVICES_CHANGE", + + /** + * Indicates that the Wi-Fi P2P discovery state change. + */ + COMMON_EVENT_WIFI_P2P_PEERS_DISCOVERY_STATE_CHANGED = + "usual.event.wifi.p2p.PEER_DISCOVERY_STATE_CHANGE", + + /** + * Indicates that the Wi-Fi P2P current device state change. + */ + COMMON_EVENT_WIFI_P2P_CURRENT_DEVICE_STATE_CHANGED = + "usual.event.wifi.p2p.CURRENT_DEVICE_CHANGE", + + /** + * Indicates that the Wi-Fi P2P group info is changed. + */ + COMMON_EVENT_WIFI_P2P_GROUP_STATE_CHANGED = + "usual.event.wifi.p2p.GROUP_STATE_CHANGED", + + /** + * bluetooth.handsfree.ag.connect.state.update. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_CONNECT_STATE_UPDATE = + "usual.event.bluetooth.handsfree.ag.CONNECT_STATE_UPDATE", + + /** + * bluetooth.handsfree.ag.current.device.update. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_CURRENT_DEVICE_UPDATE = + "usual.event.bluetooth.handsfree.ag.CURRENT_DEVICE_UPDATE", + + /** + * bluetooth.handsfree.ag.audio.state.update. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREE_AG_AUDIO_STATE_UPDATE = + "usual.event.bluetooth.handsfree.ag.AUDIO_STATE_UPDATE", + + /** + * bluetooth.a2dpsource.connect.state.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CONNECT_STATE_UPDATE = + "usual.event.bluetooth.a2dpsource.CONNECT_STATE_UPDATE", + + /** + * bluetooth.a2dpsource.current.device.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CURRENT_DEVICE_UPDATE = + "usual.event.bluetooth.a2dpsource.CURRENT_DEVICE_UPDATE", + + /** + * bluetooth.a2dpsource.playing.state.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSOURCE_PLAYING_STATE_UPDATE = + "usual.event.bluetooth.a2dpsource.PLAYING_STATE_UPDATE", + + /** + * bluetooth.a2dpsource.avrcp.connect.state.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSOURCE_AVRCP_CONNECT_STATE_UPDATE = + "usual.event.bluetooth.a2dpsource.AVRCP_CONNECT_STATE_UPDATE", + + /** + * bluetooth.a2dpsource.codec.value.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSOURCE_CODEC_VALUE_UPDATE = + "usual.event.bluetooth.a2dpsource.CODEC_VALUE_UPDATE", + + /** + * bluetooth.remotedevice.discovered. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_DISCOVERED = + "usual.event.bluetooth.remotedevice.DISCOVERED", + + /** + * bluetooth.remotedevice.class.value.update. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_CLASS_VALUE_UPDATE = + "usual.event.bluetooth.remotedevice.CLASS_VALUE_UPDATE", + + /** + * bluetooth.remotedevice.acl.connected. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_ACL_CONNECTED = + "usual.event.bluetooth.remotedevice.ACL_CONNECTED", + + /** + * bluetooth.remotedevice.acl.disconnected. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_ACL_DISCONNECTED = + "usual.event.bluetooth.remotedevice.ACL_DISCONNECTED", + + /** + * bluetooth.remotedevice.name.update. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_NAME_UPDATE = + "usual.event.bluetooth.remotedevice.NAME_UPDATE", + + /** + * bluetooth.remotedevice.pair.state. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIR_STATE = + "usual.event.bluetooth.remotedevice.PAIR_STATE", + + /** + * bluetooth.remotedevice.battery.value.update. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_BATTERY_VALUE_UPDATE = + "usual.event.bluetooth.remotedevice.BATTERY_VALUE_UPDATE", + + /** + * bluetooth.remotedevice.sdp.result. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_SDP_RESULT = + "usual.event.bluetooth.remotedevice.SDP_RESULT", + + /** + * bluetooth.remotedevice.uuid.value. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_UUID_VALUE = + "usual.event.bluetooth.remotedevice.UUID_VALUE", + + /** + * bluetooth.remotedevice.pairing.req. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIRING_REQ = + "usual.event.bluetooth.remotedevice.PAIRING_REQ", + + /** + * bluetooth.remotedevice.pairing.cancel. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_PAIRING_CANCEL = + "usual.event.bluetooth.remotedevice.PAIRING_CANCEL", + + /** + * bluetooth.remotedevice.connect.req. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_CONNECT_REQ = + "usual.event.bluetooth.remotedevice.CONNECT_REQ", + + /** + * bluetooth.remotedevice.connect.reply. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_CONNECT_REPLY = + "usual.event.bluetooth.remotedevice.CONNECT_REPLY", + + /** + * bluetooth.remotedevice.connect.cancel. + */ + COMMON_EVENT_BLUETOOTH_REMOTEDEVICE_CONNECT_CANCEL = + "usual.event.bluetooth.remotedevice.CONNECT_CANCEL", + + /** + * bluetooth.handsfreeunit.connect.state.update. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREEUNIT_CONNECT_STATE_UPDATE = + "usual.event.bluetooth.handsfreeunit.CONNECT_STATE_UPDATE", + + /** + * bluetooth.handsfreeunit.audio.state.update. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREEUNIT_AUDIO_STATE_UPDATE = + "usual.event.bluetooth.handsfreeunit.AUDIO_STATE_UPDATE", + + /** + * bluetooth.handsfreeunit.ag.common.event. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREEUNIT_AG_COMMON_EVENT = + "usual.event.bluetooth.handsfreeunit.AG_COMMON_EVENT", + + /** + * bluetooth.handsfreeunit.ag.call.state.update. + */ + COMMON_EVENT_BLUETOOTH_HANDSFREEUNIT_AG_CALL_STATE_UPDATE = + "usual.event.bluetooth.handsfreeunit.AG_CALL_STATE_UPDATE", + + /** + * bluetooth.host.state.update. + */ + COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE = + "usual.event.bluetooth.host.STATE_UPDATE", + + /** + * bluetooth.host.req.discoverable. + */ + COMMON_EVENT_BLUETOOTH_HOST_REQ_DISCOVERABLE = + "usual.event.bluetooth.host.REQ_DISCOVERABLE", + + /** + * bluetooth.host.req.enable. + */ + COMMON_EVENT_BLUETOOTH_HOST_REQ_ENABLE = "usual.event.bluetooth.host.REQ_ENABLE", + + /** + * bluetooth.host.req.disable. + */ + COMMON_EVENT_BLUETOOTH_HOST_REQ_DISABLE = + "usual.event.bluetooth.host.REQ_DISABLE", + + /** + * bluetooth.host.scan.mode.update. + */ + COMMON_EVENT_BLUETOOTH_HOST_SCAN_MODE_UPDATE = + "usual.event.bluetooth.host.SCAN_MODE_UPDATE", + + /** + * bluetooth.host.discovery.stated. + */ + COMMON_EVENT_BLUETOOTH_HOST_DISCOVERY_STARTED = + "usual.event.bluetooth.host.DISCOVERY_STARTED", + + /** + * bluetooth.host.discovery.finished. + */ + COMMON_EVENT_BLUETOOTH_HOST_DISCOVERY_FINISHED = + "usual.event.bluetooth.host.DISCOVERY_FINISHED", + + /** + * bluetooth.host.name.update. + */ + COMMON_EVENT_BLUETOOTH_HOST_NAME_UPDATE = + "usual.event.bluetooth.host.NAME_UPDATE", + + /** + * bluetooth.a2dp.connect.state.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSINK_CONNECT_STATE_UPDATE = + "usual.event.bluetooth.a2dpsink.CONNECT_STATE_UPDATE", + + /** + * bluetooth.a2dp.playing.state.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSINK_PLAYING_STATE_UPDATE = + "usual.event.bluetooth.a2dpsink.PLAYING_STATE_UPDATE", + + /** + * bluetooth.a2dp.audio.state.update. + */ + COMMON_EVENT_BLUETOOTH_A2DPSINK_AUDIO_STATE_UPDATE = + "usual.event.bluetooth.a2dpsink.AUDIO_STATE_UPDATE", + + /** + * nfc state change. + */ + COMMON_EVENT_NFC_ACTION_ADAPTER_STATE_CHANGED = + "usual.event.nfc.action.ADAPTER_STATE_CHANGED", + + /** + * nfc field on detected. + */ + COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED = + "usual.event.nfc.action.RF_FIELD_ON_DETECTED", + + /** + * nfc field off detected. + */ + COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED = + "usual.event.nfc.action.RF_FIELD_OFF_DETECTED", + + /** + * Sent when stop charging battery. + */ + COMMON_EVENT_DISCHARGING = "usual.event.DISCHARGING", + + /** + * Sent when start charging battery. + */ + COMMON_EVENT_CHARGING = "usual.event.CHARGING", + + /** + * Sent when device's idle mode changed + */ + COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED = "usual.event.DEVICE_IDLE_MODE_CHANGED", + + /** + * Sent when device's power save mode changed + */ + COMMON_EVENT_POWER_SAVE_MODE_CHANGED = "usual.event.POWER_SAVE_MODE_CHANGED", + + /** + * user added. + */ + COMMON_EVENT_USER_ADDED = "usual.event.USER_ADDED", + + /** + * user removed. + */ + COMMON_EVENT_USER_REMOVED = "usual.event.USER_REMOVED", + + /** + * Sent when ability is added. + */ + COMMON_EVENT_ABILITY_ADDED = "common.event.ABILITY_ADDED", + + /** + * Sent when ability is removed. + */ + COMMON_EVENT_ABILITY_REMOVED = "common.event.ABILITY_REMOVED", + + /** + * Sent when ability is updated. + */ + COMMON_EVENT_ABILITY_UPDATED = "common.event.ABILITY_UPDATED", + + /** + * gps mode state changed. + */ + COMMON_EVENT_LOCATION_MODE_STATE_CHANGED = + "usual.event.location.MODE_STATE_CHANGED", + + /** + * The ivi is about to go into sleep state when the ivi is turned off power. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_SLEEP = "common.event.IVI_SLEEP", + + /** + * The ivi is slept and notify the app stop playing. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_PAUSE = "common.event.IVI_PAUSE", + + /** + * The ivi is standby and notify the app stop playing. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_STANDBY = "common.event.IVI_STANDBY", + + /** + * The app stop playing and save state. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_LASTMODE_SAVE = "common.event.IVI_LASTMODE_SAVE", + + /** + * The ivi is voltage abnormal. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_VOLTAGE_ABNORMAL = "common.event.IVI_VOLTAGE_ABNORMAL", + + /** + * The ivi temperature is too high. + * This is a protected common event that can only be sent by system.this common event will be delete later, + * please use COMMON_EVENT_IVI_TEMPERATURE_ABNORMAL. + */ + COMMON_EVENT_IVI_HIGH_TEMPERATURE = "common.event.IVI_HIGH_TEMPERATURE", + + /** + * The ivi temperature is extreme high. + * This is a protected common event that can only be sent by system.this common event will be delete later, + * please use COMMON_EVENT_IVI_TEMPERATURE_ABNORMAL. + */ + COMMON_EVENT_IVI_EXTREME_TEMPERATURE = "common.event.IVI_EXTREME_TEMPERATURE", + + /** + * The ivi temperature is abnormal. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_TEMPERATURE_ABNORMAL = "common.event.IVI_TEMPERATURE_ABNORMAL", + + /** + * The ivi voltage is recovery. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_VOLTAGE_RECOVERY = "common.event.IVI_VOLTAGE_RECOVERY", + + /** + * The ivi temperature is recovery. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_TEMPERATURE_RECOVERY = "common.event.IVI_TEMPERATURE_RECOVERY", + + /** + * The battery service is active. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_IVI_ACTIVE = "common.event.IVI_ACTIVE", + + /** + * The usb state changed. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_USB_STATE = "usual.event.hardware.usb.action.USB_STATE", + + /** + * The usb port changed. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_USB_PORT_CHANGED = + "usual.event.hardware.usb.action.USB_PORT_CHANGED", + + /** + * The usb device attached. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_USB_DEVICE_ATTACHED = + "usual.event.hardware.usb.action.USB_DEVICE_ATTACHED", + + /** + * The usb device detached. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_USB_DEVICE_DETACHED = + "usual.event.hardware.usb.action.USB_DEVICE_DETACHED", + + /** + * The usb accessory attached. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_USB_ACCESSORY_ATTACHED = + "usual.event.hardware.usb.action.USB_ACCESSORY_ATTACHED", + + /** + * The usb accessory detached. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_USB_ACCESSORY_DETACHED = + "usual.event.hardware.usb.action.USB_ACCESSORY_DETACHED", + + /** + * The storage space is low. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DEVICE_STORAGE_LOW = "usual.event.DEVICE_STORAGE_LOW", + + /** + * The storage space is normal. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DEVICE_STORAGE_OK = "usual.event.DEVICE_STORAGE_OK", + + /** + * The storage space is full. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DEVICE_STORAGE_FULL = "usual.event.DEVICE_STORAGE_FULL", + + /** + * The network connection was changed. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_CONNECTIVITY_CHANGE = "usual.event.CONNECTIVITY_CHANGE", + + /** + * The external storage was removed. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DISK_REMOVED = "usual.event.data.DISK_REMOVED", + + /** + * The external storage was unmounted. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DISK_UNMOUNTED = "usual.event.data.DISK_UNMOUNTED", + + /** + * The external storage was mounted. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DISK_MOUNTED = "usual.event.data.DISK_MOUNTED", + + /** + * The external storage was bad removal. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DISK_BAD_REMOVAL = "usual.event.data.DISK_BAD_REMOVAL", + + /** + * The external storage was unmountable. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DISK_UNMOUNTABLE = "usual.event.data.DISK_UNMOUNTABLE", + + /** + * The external storage was eject. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_DISK_EJECT = "usual.event.data.DISK_EJECT", + + /** + * The visible of account was updated. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_VISIBLE_ACCOUNTS_UPDATED = + "usual.event.data.VISIBLE_ACCOUNTS_UPDATED", + + /** + * Account was deleted. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_ACCOUNT_DELETED = "usual.event.data.ACCOUNT_DELETED", + + /** + * Foundation was ready. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_FOUNDATION_READY = "common.event.FOUNDATION_READY", + + /** + * Indicates the action of a common event that the phone SIM card state has changed. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_SIM_CARD_DEFAULT_VOICE_SUBSCRIPTION_CHANGED = + "usual.event.SIM.DEFAULT_VOICE_SUBSCRIPTION_CHANGED", + + /** + * Indicates the action of a common event that the phone SIM card state has changed. + * This is a protected common event that can only be sent by system. + */ + COMMON_EVENT_SIM_STATE_CHANGED = "usual.event.SIM.CARD_STATE_CHANGED", + + /** + * Indicates the common event Action indicating that the airplane mode status of the device changes. + * Users can register this event to listen to the change of the airplane mode status of the device. + */ + COMMON_EVENT_AIRPLANE_MODE_CHANGED = "usual.event.AIRPLANE_MODE", + + /** + * Only for test case. + */ + COMMON_EVENT_TEST_ACTION1 = "usual.event.test1", + + /** + * Only for test case. + */ + COMMON_EVENT_TEST_ACTION2 = "usual.event.test2" + } +} + +export default commonEvent; \ No newline at end of file diff --git a/interfaces/kits/js/commonEvent/commonEventData.d.ts b/interfaces/kits/js/commonEvent/commonEventData.d.ts new file mode 100644 index 00000000..6447604c --- /dev/null +++ b/interfaces/kits/js/commonEvent/commonEventData.d.ts @@ -0,0 +1,52 @@ +/* + * 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. + */ + +/** + * the data of the commonEvent + * @name CommonEventData + * @since 7 + * @permission N/A + */ +export interface CommonEventData { + /** + * event type + * @default - + * @since 7 + */ + event: string + + /** + * bundle name + * @default - + * @since 7 + */ + bundleName?: string; + + /** + * The custom result code of the common event. + * + * @default 0 + * @since 7 + */ + code?: number; + + /** + * The custom result data of the common event. + * + * @default "" + * @since 7 + */ + data?: string; +} diff --git a/interfaces/kits/js/commonEvent/commonEventPublishData.d.ts b/interfaces/kits/js/commonEvent/commonEventPublishData.d.ts new file mode 100644 index 00000000..b9e46b93 --- /dev/null +++ b/interfaces/kits/js/commonEvent/commonEventPublishData.d.ts @@ -0,0 +1,69 @@ +/* + * 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. + */ + +/** + * containing the common event content and attributes + * @name CommonEventPublishData + * @since 7 + * @permission N/A + */ +export interface CommonEventPublishData { + /** + * bundle name + * @default - + * @since 7 + */ + bundleName?: string; + + /** + * The custom result code of the common event. + * + * @default 0 + * @since 7 + */ + code?: number; + + /** + * The custom result data of the common event. + * + * @default "" + * @since 7 + */ + data?: string; + + /** + * The permissions for subscribers. Only subscribers with required permissions can receive published common events. + * + * @default - + * @since 7 + */ + subscriberPermissions?: Array; + + /** + * Whether the type of a common event is ordered or not. + * + * @default false + * @since 7 + */ + isOrdered?: boolean; + + /** + * Whether the type of a common event is sticky or not. + * + * @default false + * @since 7 + */ + isSticky?: boolean; +} \ No newline at end of file diff --git a/interfaces/kits/js/commonEvent/commonEventSubscribeInfo.d.ts b/interfaces/kits/js/commonEvent/commonEventSubscribeInfo.d.ts new file mode 100644 index 00000000..32ff8798 --- /dev/null +++ b/interfaces/kits/js/commonEvent/commonEventSubscribeInfo.d.ts @@ -0,0 +1,65 @@ +/* + * 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. + */ + +/** + * the information of the subscriber + * @name CommonEventSubscribeInfo + * @since 7 + * @permission N/A + */ +export interface CommonEventSubscribeInfo { + /** + * The permission that the publisher must have in order to send a common event to this subscriber. + * This subscriber receives only common events sent by publishers granted with this permission. + * + * @default "" + * @since 7 + */ + events: Array + + /** + * The permission that the publisher must have in order to send a common event to this subscriber. + * This subscriber receives only common events sent by publishers granted with this permission. + * + * @default "" + * @since 7 + */ + publisherPermission?: string; + + /** + * deviceId Indicates the device ID. The value must be an existing device ID on the same ohos network. + * + * @default "" + * @since 7 + */ + publisherDeviceId?: string; + + /** + * Indicates the user ID. This parameter is optional, and the default value is the ID of the + * current user. If this parameter is specified, the value must be an existing user ID in the system. + * + * @default "" + * @since 7 + */ + userId?: number; + + /** + * Indicates the subscriber priority. The value ranges from -100 to 1000. + * + * @default "" + * @since 7 + */ + priority?: number; +} \ No newline at end of file diff --git a/interfaces/kits/js/commonEvent/commonEventSubscriber.d.ts b/interfaces/kits/js/commonEvent/commonEventSubscriber.d.ts new file mode 100644 index 00000000..65e5aba3 --- /dev/null +++ b/interfaces/kits/js/commonEvent/commonEventSubscriber.d.ts @@ -0,0 +1,230 @@ +/* + * 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. + */ +import { AsyncCallback } from './../basic'; +import { CommonEventSubscribeInfo } from './commonEventSubscribeInfo'; + +/** + * the subscriber of common event + * @name CommonEventSubscriber + * @since 7 + * @permission N/A + */ +export interface CommonEventSubscriber { + /** + * Obtains the result code of the current ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getCode(callback: AsyncCallback): void; + + /** + * Obtains the result code of the current ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getCode(): Promise; + + /** + * Sets the result code of the current ordered common event. + * + * @since 7 + * @param code Indicates the custom result code to set. You can set it to any value. + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + setCode(code: number, callback: AsyncCallback): void; + + /** + * Sets the result code of the current ordered common event. + * + * @since 7 + * @param code Indicates the custom result code to set. You can set it to any value. + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + setCode(code: number): Promise; + + /** + * Obtains the result data of the current ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getData(callback: AsyncCallback): void; + + /** + * Obtains the result data of the current ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getData(): Promise; + + /** + * Sets the result data of the current ordered common event. + * + * @since 7 + * @param data Indicates the custom result data to set. You can set it to any character string. + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + setData(data: string, callback: AsyncCallback): void; + + /** + * Sets the result data of the current ordered common event. + * + * @since 7 + * @param data Indicates the custom result data to set. You can set it to any character string. + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + setData(data: string): Promise; + + /** + * Sets the result of the current ordered common event. + * + * @since 7 + * @param code Indicates the custom result code to set. You can set it to any value. + * @param data Indicates the custom result data to set. You can set it to any character string. + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + setCodeAndData(code: number, data: string, callback: AsyncCallback): void; + + /** + * Sets the result of the current ordered common event. + * + * @since 7 + * @param code Indicates the custom result code to set. You can set it to any value. + * @param data Indicates the custom result data to set. You can set it to any character string. + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + setCodeAndData(code: number, data: string): Promise; + + /** + * Checks whether the current common event is an ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + isOrderedCommonEvent(callback: AsyncCallback): void; + + /** + * Checks whether the current common event is an ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + isOrderedCommonEvent(): Promise; + + /** + * Checks whether the current common event is a sticky common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + isStickyCommonEvent(callback: AsyncCallback): void; + + /** + * Checks whether the current common event is a sticky common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + isStickyCommonEvent(): Promise; + + /** + * Aborts the current ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + abortCommonEvent(callback: AsyncCallback): void; + + /** + * Aborts the current ordered common event. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + abortCommonEvent(): Promise; + + /** + * Clears the abort state of the current ordered common event + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + clearAbortCommonEvent(callback: AsyncCallback): void; + + /** + * Clears the abort state of the current ordered common event + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + clearAbortCommonEvent(): Promise; + + /** + * Checks whether the current ordered common event should be aborted. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getAbortCommonEvent(callback: AsyncCallback): void; + + /** + * Checks whether the current ordered common event should be aborted. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getAbortCommonEvent(): Promise; + + /** + * get the CommonEventSubscribeInfo of this CommonEventSubscriber. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getSubscribeInfo(callback: AsyncCallback): void; + + /** + * get the CommonEventSubscribeInfo of this CommonEventSubscriber. + * + * @since 7 + * @param callback Indicate the callback funtion to receive the common event. + * @return - + */ + getSubscribeInfo(): Promise; +} diff --git a/interfaces/kits/napi/common_event/include/common_event.h b/interfaces/kits/napi/common_event/include/common_event.h index c57fea0c..d69a9170 100644 --- a/interfaces/kits/napi/common_event/include/common_event.h +++ b/interfaces/kits/napi/common_event/include/common_event.h @@ -1,6 +1,6 @@ /* - * Copyright (c); 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License");; + * 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 * @@ -50,26 +50,26 @@ static napi_value g_CommonEventSubscriber; static std::map, subscriberInstanceInfo> subscriberInstances; struct AsyncCallbackInfoSubscribe { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - std::shared_ptr subscriber; + napi_ref callback = nullptr; + std::shared_ptr subscriber = nullptr; }; struct AsyncCallbackInfoUnsubscribe { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - size_t argc; - std::shared_ptr subscriber; + napi_ref callback = nullptr; + size_t argc = 0; + std::shared_ptr subscriber = nullptr; }; struct AsyncCallbackInfoSubscribeInfo { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; std::vector events; std::string permission; std::string deviceId; @@ -78,139 +78,131 @@ struct AsyncCallbackInfoSubscribeInfo { }; struct AsyncCallbackInfoOrderedCommonEvent { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; bool isCallback = false; bool isOrdered = false; }; struct AsyncCallbackInfoStickyCommonEvent { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; bool isSticky = false; bool isCallback = false; }; struct AsyncCallbackInfoGetCode { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; int code = 0; bool isCallback = false; }; struct AsyncCallbackInfoSetCode { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; int code = 0; bool isCallback = false; }; struct AsyncCallbackInfoGetData { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; std::string data; bool isCallback = false; }; struct AsyncCallbackInfoSetData { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; std::string data; bool isCallback = false; }; struct AsyncCallbackInfoSetCodeAndData { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; int code = 0; std::string data; bool isCallback = false; }; struct AsyncCallbackInfoAbort { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; bool isCallback = false; }; struct AsyncCallbackInfoClearAbort { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; bool isCallback = false; }; struct AsyncCallbackInfoGetAbort { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; bool abortEvent = false; bool isCallback = false; }; struct AsyncCallbackInfoFinish { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - SubscriberInstance *objectInfo; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + SubscriberInstance *objectInfo = nullptr; bool isCallback = false; }; struct AsyncCallbackInfoCreate { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; - napi_deferred deferred; - napi_ref subscriberInfo = 0; + napi_ref callback = nullptr; + napi_deferred deferred = nullptr; + napi_ref subscriberInfo = nullptr; bool isCallback = false; - napi_value result = 0; + napi_value result = nullptr; }; struct AsyncCallbackInfoPublish { - napi_env env; + napi_env env = nullptr; napi_async_work asyncWork; - napi_ref callback = 0; + napi_ref callback = nullptr; CommonEventData commonEventData; CommonEventPublishInfo commonEventPublishInfo; }; -struct CommonEventSubscribeInfoByjs { - std::vector events; - std::string publisherPermission; - std::string publisherDeviceId; - int userId = 0; - int priority = 0; -}; - struct CommonEventPublishDataByjs { std::string bundleName; std::string data; @@ -221,8 +213,8 @@ struct CommonEventPublishDataByjs { }; struct CommonEventDataWorker { - napi_env env; - napi_ref ref = 0; + napi_env env = nullptr; + napi_ref ref = nullptr; Want want; int code = 0; std::string data; @@ -239,16 +231,16 @@ public: void SetCallbackRef(const napi_ref &ref); private: - napi_env env_; - napi_ref ref_; + napi_env env_ = nullptr; + napi_ref ref_ = nullptr; }; napi_value NapiGetNull(napi_env env); napi_value GetCallbackErrorValue(napi_env env, int errCode); -napi_value ParseParametersByCreateSubscriber(const napi_env &env, const napi_value (&argv)[CREATE_MAX_PARA], - const size_t &argc, CommonEventSubscribeInfoByjs &subscribeInfo, napi_ref &callback); +napi_value ParseParametersByCreateSubscriber( + const napi_env &env, const napi_value (&argv)[CREATE_MAX_PARA], const size_t &argc, napi_ref &callback); void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result); @@ -405,7 +397,7 @@ napi_value ParseParametersByUnsubscribe(const napi_env &env, const size_t &argc, const napi_value (&argv)[UNSUBSCRIBE_MAX_PARA], std::shared_ptr &subscriber, napi_ref &callback); -void NapiDeleteSubscribe(std::shared_ptr &subscriber); +void NapiDeleteSubscribe(const napi_env &env, std::shared_ptr &subscriber); napi_value Unsubscribe(napi_env env, napi_callback_info info); diff --git a/interfaces/kits/napi/common_event/include/support.h b/interfaces/kits/napi/common_event/include/support.h index 3bff811d..2ca1384c 100644 --- a/interfaces/kits/napi/common_event/include/support.h +++ b/interfaces/kits/napi/common_event/include/support.h @@ -1,6 +1,6 @@ /* - * Copyright (c); 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License");; + * 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 * diff --git a/interfaces/kits/napi/common_event/src/common_event.cpp b/interfaces/kits/napi/common_event/src/common_event.cpp index 01af5359..33370936 100644 --- a/interfaces/kits/napi/common_event/src/common_event.cpp +++ b/interfaces/kits/napi/common_event/src/common_event.cpp @@ -60,9 +60,16 @@ void SubscriberInstance::OnReceiveEvent(const CommonEventData &data) napi_get_uv_event_loop(env_, &loop); #endif // NAPI_VERSION >= 2 - uv_work_t *work = new uv_work_t; - - CommonEventDataWorker *commonEventDataWorker = new CommonEventDataWorker(); + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + EVENT_LOGE("work is null"); + return; + } + CommonEventDataWorker *commonEventDataWorker = new (std::nothrow) CommonEventDataWorker(); + if (commonEventDataWorker == nullptr) { + EVENT_LOGE("commonEventDataWorker is null"); + return; + } commonEventDataWorker->want = data.GetWant(); EVENT_LOGI("OnReceiveEvent() action = %{public}s", data.GetWant().GetAction().c_str()); commonEventDataWorker->code = data.GetCode(); @@ -84,20 +91,21 @@ void SubscriberInstance::OnReceiveEvent(const CommonEventData &data) } } - uv_queue_work( - loop, + uv_queue_work(loop, work, [](uv_work_t *work) {}, [](uv_work_t *work, int status) { EVENT_LOGI("OnReceiveEvent uv_work_t start"); + if (work == nullptr) { + return; + } CommonEventDataWorker *commonEventDataWorkerData = (CommonEventDataWorker *)work->data; if (commonEventDataWorkerData == nullptr) { return; } - - napi_value result; + napi_value result = nullptr; napi_create_object(commonEventDataWorkerData->env, &result); - napi_value value; + napi_value value = nullptr; // event napi_create_string_utf8(commonEventDataWorkerData->env, @@ -122,14 +130,14 @@ void SubscriberInstance::OnReceiveEvent(const CommonEventData &data) commonEventDataWorkerData->env, commonEventDataWorkerData->data.c_str(), NAPI_AUTO_LENGTH, &value); napi_set_named_property(commonEventDataWorkerData->env, result, "data", value); - napi_value undefined; + napi_value undefined = nullptr; napi_get_undefined(commonEventDataWorkerData->env, &undefined); - napi_value callback; - napi_value resultout; + napi_value callback = nullptr; + napi_value resultout = nullptr; napi_get_reference_value(commonEventDataWorkerData->env, commonEventDataWorkerData->ref, &callback); - napi_value results[ARGS_TWO] = {0}; + napi_value results[ARGS_TWO] = {nullptr}; results[PARAM0] = GetCallbackErrorValue(commonEventDataWorkerData->env, NO_ERROR); results[PARAM1] = result; NAPI_CALL_RETURN_VOID(commonEventDataWorkerData->env, @@ -157,7 +165,7 @@ void SubscriberInstance::SetCallbackRef(const napi_ref &ref) napi_value NapiGetNull(napi_env env) { - napi_value result = 0; + napi_value result = nullptr; napi_get_null(env, &result); return result; @@ -173,8 +181,8 @@ napi_value GetCallbackErrorValue(napi_env env, int errCode) return result; } -napi_value ParseParametersByCreateSubscriber(const napi_env &env, const napi_value (&argv)[CREATE_MAX_PARA], - const size_t &argc, CommonEventSubscribeInfoByjs &subscribeInfo, napi_ref &callback) +napi_value ParseParametersByCreateSubscriber( + const napi_env &env, const napi_value (&argv)[CREATE_MAX_PARA], const size_t &argc, napi_ref &callback) { napi_valuetype valuetype; @@ -185,7 +193,7 @@ napi_value ParseParametersByCreateSubscriber(const napi_env &env, const napi_val // argv[1]:callback if (argc >= CREATE_MAX_PARA) { NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); - NAPI_ASSERT(env, valuetype = napi_function, "Wrong argument type. Function expected."); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); napi_create_reference(env, argv[1], 1, &callback); } @@ -194,14 +202,14 @@ napi_value ParseParametersByCreateSubscriber(const napi_env &env, const napi_val void SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result) { - napi_value undefined; + napi_value undefined = nullptr; napi_get_undefined(env, &undefined); - napi_value callback; - napi_value resultout; + napi_value callback = nullptr; + napi_value resultout = nullptr; napi_get_reference_value(env, callbackIn, &callback); - napi_value results[ARGS_TWO] = {0}; + napi_value results[ARGS_TWO] = {nullptr}; results[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); results[PARAM1] = result; @@ -226,18 +234,18 @@ void ReturnCallbackPromise(const napi_env &env, const bool &isCallback, const na } void PaddingAsyncCallbackInfoCreateSubscriber(const napi_env &env, const size_t &argc, - AsyncCallbackInfoCreate *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoCreate *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoCreateSubscriber start"); if (argc >= CREATE_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; napi_create_promise(env, &deferred, &promise); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } } @@ -246,60 +254,64 @@ napi_value CreateSubscriber(napi_env env, napi_callback_info info) EVENT_LOGI("CreateSubscriber start"); size_t argc = CREATE_MAX_PARA; - napi_value argv[CREATE_MAX_PARA]; + napi_value argv[CREATE_MAX_PARA] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); - CommonEventSubscribeInfoByjs subscribeInfoByjs; - napi_ref callback = 0; - if (ParseParametersByCreateSubscriber(env, argv, argc, subscribeInfoByjs, callback) == nullptr) { + napi_ref callback = nullptr; + if (ParseParametersByCreateSubscriber(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } - AsyncCallbackInfoCreate *asynccallbackinfo = - new AsyncCallbackInfoCreate{.env = env, .asyncWork = nullptr, .subscriberInfo = nullptr}; - - napi_value promise = 0; + AsyncCallbackInfoCreate *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoCreate{.env = env, .asyncWork = nullptr, .subscriberInfo = nullptr}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; - PaddingAsyncCallbackInfoCreateSubscriber(env, argc, asynccallbackinfo, callback, promise); + PaddingAsyncCallbackInfoCreateSubscriber(env, argc, asyncCallbackInfo, callback, promise); - napi_create_reference(env, argv[0], 1, &asynccallbackinfo->subscriberInfo); + napi_create_reference(env, argv[0], 1, &asyncCallbackInfo->subscriberInfo); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "CreateSubscriber", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("CreateSubscriber napi_create_async_work start"); }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("CreateSubscriber napi_create_async_work end"); - AsyncCallbackInfoCreate *asynccallbackinfo = (AsyncCallbackInfoCreate *)data; - napi_value subscriberInfoRefValue; - napi_get_reference_value(env, asynccallbackinfo->subscriberInfo, &subscriberInfoRefValue); + AsyncCallbackInfoCreate *asyncCallbackInfo = (AsyncCallbackInfoCreate *)data; + napi_value subscriberInfoRefValue = nullptr; + napi_get_reference_value(env, asyncCallbackInfo->subscriberInfo, &subscriberInfoRefValue); - napi_new_instance(env, g_CommonEventSubscriber, 1, &subscriberInfoRefValue, &asynccallbackinfo->result); + napi_new_instance(env, g_CommonEventSubscriber, 1, &subscriberInfoRefValue, &asyncCallbackInfo->result); ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, - asynccallbackinfo->result); - - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, + asyncCallbackInfo->result); + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -328,7 +340,7 @@ void SetEventsResult(const napi_env &env, const std::vector &events napi_value value = nullptr; if (events.size() > 0) { - napi_value nEvents; + napi_value nEvents = nullptr; if (napi_create_array(env, &nEvents) != napi_ok) { return; } @@ -350,7 +362,7 @@ void SetPublisherPermissionResult( { EVENT_LOGI("SetPublisherPermissionResult start"); - napi_value value; + napi_value value = nullptr; napi_create_string_utf8(env, permission.c_str(), NAPI_AUTO_LENGTH, &value); napi_set_named_property(env, commonEventSubscribeInfo, "publisherPermission", value); @@ -360,7 +372,7 @@ void SetPublisherDeviceIdResult(const napi_env &env, const std::string &deviceId { EVENT_LOGI("SetPublisherDeviceIdResult start"); - napi_value value; + napi_value value = nullptr; napi_create_string_utf8(env, deviceId.c_str(), NAPI_AUTO_LENGTH, &value); napi_set_named_property(env, commonEventSubscribeInfo, "publisherDeviceId", value); @@ -370,47 +382,47 @@ void SetPublisherPriorityResult(const napi_env &env, const int32_t &priority, na { EVENT_LOGI("SetPublisherPriorityResult start"); - napi_value value; + napi_value value = nullptr; napi_create_int32(env, priority, &value); napi_set_named_property(env, commonEventSubscribeInfo, "priority", value); } void PaddingAsyncCallbackInfoGetSubscribeInfo(const napi_env &env, const size_t &argc, - AsyncCallbackInfoSubscribeInfo *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoSubscribeInfo *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoGetSubscribeInfo start"); if (argc >= GETSUBSCREBEINFO_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } } void PaddingNapiCreateAsyncWorkCallbackInfo( - AsyncCallbackInfoSubscribeInfo *&asynccallbackinfo, SubscriberInstance *&subscriber) + AsyncCallbackInfoSubscribeInfo *&asyncCallbackInfo, SubscriberInstance *&subscriber) { EVENT_LOGI("PaddingNapiCreateAsyncWorkCallbackInfo start"); - asynccallbackinfo->events = subscriber->GetSubscribeInfo().GetMatchingSkills().GetEvents(); - asynccallbackinfo->permission = subscriber->GetSubscribeInfo().GetPermission(); - asynccallbackinfo->deviceId = subscriber->GetSubscribeInfo().GetDeviceId(); - asynccallbackinfo->priority = subscriber->GetSubscribeInfo().GetPriority(); + asyncCallbackInfo->events = subscriber->GetSubscribeInfo().GetMatchingSkills().GetEvents(); + asyncCallbackInfo->permission = subscriber->GetSubscribeInfo().GetPermission(); + asyncCallbackInfo->deviceId = subscriber->GetSubscribeInfo().GetDeviceId(); + asyncCallbackInfo->priority = subscriber->GetSubscribeInfo().GetPriority(); } -void SetNapiResult(const napi_env &env, const AsyncCallbackInfoSubscribeInfo *asynccallbackinfo, napi_value &result) +void SetNapiResult(const napi_env &env, const AsyncCallbackInfoSubscribeInfo *asyncCallbackInfo, napi_value &result) { EVENT_LOGI("SetNapiResult start"); - SetEventsResult(env, asynccallbackinfo->events, result); - SetPublisherPermissionResult(env, asynccallbackinfo->permission, result); - SetPublisherDeviceIdResult(env, asynccallbackinfo->deviceId, result); - SetPublisherPriorityResult(env, asynccallbackinfo->priority, result); + SetEventsResult(env, asyncCallbackInfo->events, result); + SetPublisherPermissionResult(env, asyncCallbackInfo->permission, result); + SetPublisherDeviceIdResult(env, asyncCallbackInfo->deviceId, result); + SetPublisherPriorityResult(env, asyncCallbackInfo->priority, result); } napi_value GetSubscribeInfo(napi_env env, napi_callback_info info) @@ -418,11 +430,11 @@ napi_value GetSubscribeInfo(napi_env env, napi_callback_info info) EVENT_LOGI("GetSubscribeInfo start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByGetSubscribeInfo(env, argc, argv, callback) == nullptr) { return NapiGetNull(env); } @@ -431,48 +443,54 @@ napi_value GetSubscribeInfo(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("GetSubscribeInfo objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoSubscribeInfo *asynccallbackinfo = - new AsyncCallbackInfoSubscribeInfo{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoGetSubscribeInfo(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoSubscribeInfo *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoSubscribeInfo{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoGetSubscribeInfo(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "getSubscribeInfo", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("GetSubscribeInfo napi_create_async_work start"); - AsyncCallbackInfoSubscribeInfo *asynccallbackinfo = (AsyncCallbackInfoSubscribeInfo *)data; + AsyncCallbackInfoSubscribeInfo *asyncCallbackInfo = (AsyncCallbackInfoSubscribeInfo *)data; - PaddingNapiCreateAsyncWorkCallbackInfo(asynccallbackinfo, asynccallbackinfo->objectInfo); + PaddingNapiCreateAsyncWorkCallbackInfo(asyncCallbackInfo, asyncCallbackInfo->objectInfo); }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("GetSubscribeInfo napi_create_async_work end"); - AsyncCallbackInfoSubscribeInfo *asynccallbackinfo = (AsyncCallbackInfoSubscribeInfo *)data; + AsyncCallbackInfoSubscribeInfo *asyncCallbackInfo = (AsyncCallbackInfoSubscribeInfo *)data; - napi_value result = 0; + napi_value result = nullptr; napi_create_object(env, &result); - SetNapiResult(env, asynccallbackinfo, result); + SetNapiResult(env, asyncCallbackInfo, result); ReturnCallbackPromise( - env, asynccallbackinfo->isCallback, asynccallbackinfo->callback, asynccallbackinfo->deferred, result); + env, asyncCallbackInfo->isCallback, asyncCallbackInfo->callback, asyncCallbackInfo->deferred, result); + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -495,19 +513,35 @@ napi_value ParseParametersByIsOrderedCommonEvent( } void PaddingAsyncCallbackInfoIsOrderedCommonEvent(const napi_env &env, const size_t &argc, - AsyncCallbackInfoOrderedCommonEvent *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoOrderedCommonEvent *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoIsOrderedCommonEvent start"); if (argc >= ISORDEREDCOMMONEVENT_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; + } +} + +std::shared_ptr GetAsyncResult(const SubscriberInstance *objectInfo) +{ + EVENT_LOGI("GetAsyncResult start"); + if (objectInfo) { + for (auto subscriberInstance : subscriberInstances) { + EVENT_LOGI("GetAsyncResult SubscriberInstance = %{public}p", subscriberInstance.first.get()); + if (subscriberInstance.first.get() == objectInfo) { + EVENT_LOGI("GetAsyncResult Result = %{public}p", subscriberInstance.second.commonEventResult.get()); + return subscriberInstance.second.commonEventResult; + } + } } + EVENT_LOGI("GetAsyncResult end"); + return nullptr; } napi_value IsOrderedCommonEvent(napi_env env, napi_callback_info info) @@ -515,10 +549,10 @@ napi_value IsOrderedCommonEvent(napi_env env, napi_callback_info info) EVENT_LOGI("IsOrderedCommonEvent start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByIsOrderedCommonEvent(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -527,46 +561,57 @@ napi_value IsOrderedCommonEvent(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("IsOrderedCommonEvent objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoOrderedCommonEvent *asynccallbackinfo = - new AsyncCallbackInfoOrderedCommonEvent{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - napi_value promise = 0; - PaddingAsyncCallbackInfoIsOrderedCommonEvent(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoOrderedCommonEvent *asyncCallbackInfo = new (std::nothrow) + AsyncCallbackInfoOrderedCommonEvent{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoIsOrderedCommonEvent(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "isOrderedCommonEvent", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("IsOrderedCommonEvent napi_create_async_work start"); - AsyncCallbackInfoOrderedCommonEvent *asynccallbackinfo = (AsyncCallbackInfoOrderedCommonEvent *)data; - - asynccallbackinfo->isOrdered = asynccallbackinfo->objectInfo->IsOrderedCommonEvent(); + AsyncCallbackInfoOrderedCommonEvent *asyncCallbackInfo = (AsyncCallbackInfoOrderedCommonEvent *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); + if (asyncResult) { + asyncCallbackInfo->isOrdered = asyncResult->IsOrderedCommonEvent(); + } else { + asyncCallbackInfo->isOrdered = asyncCallbackInfo->objectInfo->IsOrderedCommonEvent(); + } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("IsOrderedCommonEvent napi_create_async_work end"); - AsyncCallbackInfoOrderedCommonEvent *asynccallbackinfo = (AsyncCallbackInfoOrderedCommonEvent *)data; + AsyncCallbackInfoOrderedCommonEvent *asyncCallbackInfo = (AsyncCallbackInfoOrderedCommonEvent *)data; - napi_value result = 0; - napi_get_boolean(env, asynccallbackinfo->isOrdered, &result); + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackInfo->isOrdered, &result); ReturnCallbackPromise( - env, asynccallbackinfo->isCallback, asynccallbackinfo->callback, asynccallbackinfo->deferred, result); + env, asyncCallbackInfo->isCallback, asyncCallbackInfo->callback, asyncCallbackInfo->deferred, result); + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -589,18 +634,18 @@ napi_value ParseParametersByIsStickyCommonEvent( } void PaddingAsyncCallbackInfoIsStickyCommonEvent(const napi_env &env, const size_t &argc, - AsyncCallbackInfoStickyCommonEvent *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoStickyCommonEvent *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoIsStickyCommonEvent start"); if (argc >= ISSTICKYCOMMONEVENT_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoIsStickyCommonEvent end"); @@ -611,11 +656,11 @@ napi_value IsStickyCommonEvent(napi_env env, napi_callback_info info) EVENT_LOGI("IsStickyCommonEvent start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByIsStickyCommonEvent(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -624,46 +669,57 @@ napi_value IsStickyCommonEvent(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("IsStickyCommonEvent: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoStickyCommonEvent *asynccallbackinfo = - new AsyncCallbackInfoStickyCommonEvent{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoIsStickyCommonEvent(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoStickyCommonEvent *asyncCallbackInfo = new (std::nothrow) + AsyncCallbackInfoStickyCommonEvent{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoIsStickyCommonEvent(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "isStickyCommonEvent", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("isStickyCommonEvent napi_create_async_work start"); - AsyncCallbackInfoStickyCommonEvent *asynccallbackinfo = (AsyncCallbackInfoStickyCommonEvent *)data; + AsyncCallbackInfoStickyCommonEvent *asyncCallbackInfo = (AsyncCallbackInfoStickyCommonEvent *)data; - asynccallbackinfo->isSticky = asynccallbackinfo->objectInfo->IsStickyCommonEvent(); + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); + if (asyncResult) { + asyncCallbackInfo->isSticky = asyncResult->IsStickyCommonEvent(); + } else { + asyncCallbackInfo->isSticky = asyncCallbackInfo->objectInfo->IsStickyCommonEvent(); + } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("isStickyCommonEvent napi_create_async_work end"); - AsyncCallbackInfoStickyCommonEvent *asynccallbackinfo = (AsyncCallbackInfoStickyCommonEvent *)data; + AsyncCallbackInfoStickyCommonEvent *asyncCallbackInfo = (AsyncCallbackInfoStickyCommonEvent *)data; - napi_value result = 0; - napi_get_boolean(env, asynccallbackinfo->isSticky, &result); + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackInfo->isSticky, &result); ReturnCallbackPromise( - env, asynccallbackinfo->isCallback, asynccallbackinfo->callback, asynccallbackinfo->deferred, result); + env, asyncCallbackInfo->isCallback, asyncCallbackInfo->callback, asyncCallbackInfo->deferred, result); + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -684,47 +740,32 @@ napi_value ParseParametersByGetCode(const napi_env &env, const napi_value (&argv } void PaddingAsyncCallbackInfoGetCode(const napi_env &env, const size_t &argc, - AsyncCallbackInfoGetCode *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoGetCode *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoGetCode start"); if (argc >= GET_CODE_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoGetCode end"); } -std::shared_ptr GetAsyncResult(const SubscriberInstance *objectInfo) -{ - EVENT_LOGI("GetAsyncResult start"); - if (objectInfo) { - for (auto subscriberInstance : subscriberInstances) { - EVENT_LOGI("GetAsyncResult SubscriberInstance = %{public}p", subscriberInstance.first.get()); - if (subscriberInstance.first.get() == objectInfo) { - EVENT_LOGI("GetAsyncResult Result = %{public}p", subscriberInstance.second.commonEventResult.get()); - return subscriberInstance.second.commonEventResult; - } - } - } - EVENT_LOGI("GetAsyncResult end"); - return nullptr; -} napi_value GetCode(napi_env env, napi_callback_info info) { EVENT_LOGI("GetCode start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByGetCode(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -733,50 +774,56 @@ napi_value GetCode(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("GetCode: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoGetCode *asynccallbackinfo = - new AsyncCallbackInfoGetCode{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoGetCode(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoGetCode *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoGetCode{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoGetCode(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "getCode", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("GetCode napi_create_async_work start"); - AsyncCallbackInfoGetCode *asynccallbackinfo = (AsyncCallbackInfoGetCode *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoGetCode *asyncCallbackInfo = (AsyncCallbackInfoGetCode *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { - asynccallbackinfo->code = asyncResult->GetCode(); + asyncCallbackInfo->code = asyncResult->GetCode(); } else { - asynccallbackinfo->code = 0; + asyncCallbackInfo->code = 0; } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("GetCode napi_create_async_work end"); - AsyncCallbackInfoGetCode *asynccallbackinfo = (AsyncCallbackInfoGetCode *)data; + AsyncCallbackInfoGetCode *asyncCallbackInfo = (AsyncCallbackInfoGetCode *)data; - napi_value result = 0; - napi_create_int32(env, asynccallbackinfo->code, &result); + napi_value result = nullptr; + napi_create_int32(env, asyncCallbackInfo->code, &result); ReturnCallbackPromise( - env, asynccallbackinfo->isCallback, asynccallbackinfo->callback, asynccallbackinfo->deferred, result); + env, asyncCallbackInfo->isCallback, asyncCallbackInfo->callback, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -803,18 +850,18 @@ napi_value ParseParametersBySetCode( } void PaddingAsyncCallbackInfoSetCode(const napi_env &env, const size_t &argc, - AsyncCallbackInfoSetCode *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoSetCode *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoSetCode start"); if (argc >= SET_CODE_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoSetCode end"); @@ -824,11 +871,11 @@ napi_value SetCode(napi_env env, napi_callback_info info) { EVENT_LOGI("SetCode start"); size_t argc = 2; - napi_value argv[2]; + napi_value argv[2] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; int code = 0; if (ParseParametersBySetCode(env, argv, argc, code, callback) == nullptr) { return NapiGetNull(env); @@ -838,51 +885,57 @@ napi_value SetCode(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("SetCode: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoSetCode *asynccallbackinfo = - new AsyncCallbackInfoSetCode{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .code = code}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoSetCode(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoSetCode *asyncCallbackInfo = new (std::nothrow) + AsyncCallbackInfoSetCode{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .code = code}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoSetCode(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "setCode", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("SetCode napi_create_async_work start"); - AsyncCallbackInfoSetCode *asynccallbackinfo = (AsyncCallbackInfoSetCode *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoSetCode *asyncCallbackInfo = (AsyncCallbackInfoSetCode *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); EVENT_LOGI("SetCode get = %{public}p", asyncResult.get()); if (asyncResult) { EVENT_LOGI("SetCode get2 = %{public}p", asyncResult.get()); - asyncResult->SetCode(asynccallbackinfo->code); + asyncResult->SetCode(asyncCallbackInfo->code); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("SetCode napi_create_async_work end"); - AsyncCallbackInfoSetCode *asynccallbackinfo = (AsyncCallbackInfoSetCode *)data; + AsyncCallbackInfoSetCode *asyncCallbackInfo = (AsyncCallbackInfoSetCode *)data; ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -904,18 +957,18 @@ napi_value ParseParametersByGetData(const napi_env &env, const napi_value (&argv } void PaddingAsyncCallbackInfoGetData(const napi_env &env, const size_t &argc, - AsyncCallbackInfoGetData *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoGetData *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoGetData start"); if (argc >= GET_DATA_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoGetData end"); @@ -925,11 +978,11 @@ napi_value GetData(napi_env env, napi_callback_info info) { EVENT_LOGI("GetData start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByGetData(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -938,50 +991,56 @@ napi_value GetData(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("GetData: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoGetData *asynccallbackinfo = - new AsyncCallbackInfoGetData{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoGetData(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoGetData *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoGetData{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoGetData(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "getData", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("GetData napi_create_async_work start"); - AsyncCallbackInfoGetData *asynccallbackinfo = (AsyncCallbackInfoGetData *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoGetData *asyncCallbackInfo = (AsyncCallbackInfoGetData *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { - asynccallbackinfo->data = asyncResult->GetData(); + asyncCallbackInfo->data = asyncResult->GetData(); } else { - asynccallbackinfo->data = std::string(); + asyncCallbackInfo->data = std::string(); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("GetData napi_create_async_work end"); - AsyncCallbackInfoGetData *asynccallbackinfo = (AsyncCallbackInfoGetData *)data; + AsyncCallbackInfoGetData *asyncCallbackInfo = (AsyncCallbackInfoGetData *)data; - napi_value result = 0; - napi_create_string_utf8(env, asynccallbackinfo->data.c_str(), NAPI_AUTO_LENGTH, &result); + napi_value result = nullptr; + napi_create_string_utf8(env, asyncCallbackInfo->data.c_str(), NAPI_AUTO_LENGTH, &result); ReturnCallbackPromise( - env, asynccallbackinfo->isCallback, asynccallbackinfo->callback, asynccallbackinfo->deferred, result); + env, asyncCallbackInfo->isCallback, asyncCallbackInfo->callback, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1010,18 +1069,18 @@ napi_value ParseParametersBySetData( } void PaddingAsyncCallbackInfoSetData(const napi_env &env, const size_t &argc, - AsyncCallbackInfoSetData *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoSetData *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoSetData start"); if (argc >= SET_DATA_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoSetData end"); @@ -1031,11 +1090,11 @@ napi_value SetData(napi_env env, napi_callback_info info) { EVENT_LOGI("SetData start"); size_t argc = 2; - napi_value argv[2]; + napi_value argv[2] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; std::string data; if (ParseParametersBySetData(env, argv, argc, data, callback) == nullptr) { return NapiGetNull(env); @@ -1045,51 +1104,57 @@ napi_value SetData(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("SetData: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoSetData *asynccallbackinfo = - new AsyncCallbackInfoSetData{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .data = data}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoSetData(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoSetData *asyncCallbackInfo = new (std::nothrow) + AsyncCallbackInfoSetData{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .data = data}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoSetData(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "setData", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("SetData napi_create_async_work start"); - AsyncCallbackInfoSetData *asynccallbackinfo = (AsyncCallbackInfoSetData *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoSetData *asyncCallbackInfo = (AsyncCallbackInfoSetData *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); EVENT_LOGI("SetData get = %{public}p", asyncResult.get()); if (asyncResult) { EVENT_LOGI("SetData get2 = %{public}p", asyncResult.get()); - asyncResult->SetData(asynccallbackinfo->data); + asyncResult->SetData(asyncCallbackInfo->data); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("SetData napi_create_async_work end"); - AsyncCallbackInfoSetData *asynccallbackinfo = (AsyncCallbackInfoSetData *)data; + AsyncCallbackInfoSetData *asyncCallbackInfo = (AsyncCallbackInfoSetData *)data; ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1124,18 +1189,18 @@ napi_value ParseParametersBySetCodeAndData( } void PaddingAsyncCallbackInfoSetCodeAndData(const napi_env &env, const size_t &argc, - AsyncCallbackInfoSetCodeAndData *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoSetCodeAndData *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoSetCodeAndData start"); if (argc >= SET_CODE_AND_DATA_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoSetCodeAndData end"); @@ -1145,11 +1210,11 @@ napi_value SetCodeAndData(napi_env env, napi_callback_info info) { EVENT_LOGI("SetCodeAndData start"); size_t argc = 3; - napi_value argv[3]; + napi_value argv[3] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; int code = 0; std::string data; if (ParseParametersBySetCodeAndData(env, argv, argc, code, data, callback) == nullptr) { @@ -1160,49 +1225,55 @@ napi_value SetCodeAndData(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("SetCodeAndData: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoSetCodeAndData *asynccallbackinfo = new AsyncCallbackInfoSetCodeAndData{ + AsyncCallbackInfoSetCodeAndData *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfoSetCodeAndData{ .env = env, .asyncWork = nullptr, .objectInfo = objectInfo, .code = code, .data = data}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoSetCodeAndData(env, argc, asyncCallbackInfo, callback, promise); - napi_value promise = 0; - PaddingAsyncCallbackInfoSetCodeAndData(env, argc, asynccallbackinfo, callback, promise); - - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "setCodeAndData", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("SetCodeAndData napi_create_async_work start"); - AsyncCallbackInfoSetCodeAndData *asynccallbackinfo = (AsyncCallbackInfoSetCodeAndData *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoSetCodeAndData *asyncCallbackInfo = (AsyncCallbackInfoSetCodeAndData *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { - asyncResult->SetCodeAndData(asynccallbackinfo->code, asynccallbackinfo->data); + asyncResult->SetCodeAndData(asyncCallbackInfo->code, asyncCallbackInfo->data); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("SetCodeAndData napi_create_async_work end"); - AsyncCallbackInfoSetCodeAndData *asynccallbackinfo = (AsyncCallbackInfoSetCodeAndData *)data; + AsyncCallbackInfoSetCodeAndData *asyncCallbackInfo = (AsyncCallbackInfoSetCodeAndData *)data; ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1223,19 +1294,19 @@ napi_value ParseParametersByAbort(const napi_env &env, const napi_value (&argv)[ return NapiGetNull(env); } -void PaddingAsyncCallbackInfoAbort(const napi_env &env, const size_t &argc, AsyncCallbackInfoAbort *&asynccallbackinfo, +void PaddingAsyncCallbackInfoAbort(const napi_env &env, const size_t &argc, AsyncCallbackInfoAbort *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoAbort start"); if (argc >= ABORT_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoAbort end"); @@ -1245,11 +1316,11 @@ napi_value AbortCommonEvent(napi_env env, napi_callback_info info) { EVENT_LOGI("Abort start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByAbort(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -1258,49 +1329,55 @@ napi_value AbortCommonEvent(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("Abort: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoAbort *asynccallbackinfo = - new AsyncCallbackInfoAbort{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoAbort(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoAbort *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoAbort{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoAbort(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "abort", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("Abort napi_create_async_work start"); - AsyncCallbackInfoAbort *asynccallbackinfo = (AsyncCallbackInfoAbort *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoAbort *asyncCallbackInfo = (AsyncCallbackInfoAbort *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { asyncResult->AbortCommonEvent(); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("Abort napi_create_async_work end"); - AsyncCallbackInfoAbort *asynccallbackinfo = (AsyncCallbackInfoAbort *)data; + AsyncCallbackInfoAbort *asyncCallbackInfo = (AsyncCallbackInfoAbort *)data; ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1323,18 +1400,18 @@ napi_value ParseParametersByClearAbort( } void PaddingAsyncCallbackInfoClearAbort(const napi_env &env, const size_t &argc, - AsyncCallbackInfoClearAbort *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoClearAbort *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoClearAbort start"); if (argc >= CLEAR_ABORT_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoClearAbort end"); @@ -1344,11 +1421,11 @@ napi_value ClearAbortCommonEvent(napi_env env, napi_callback_info info) { EVENT_LOGI("ClearAbort start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByClearAbort(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -1357,49 +1434,55 @@ napi_value ClearAbortCommonEvent(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("ClearAbort: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoClearAbort *asynccallbackinfo = - new AsyncCallbackInfoClearAbort{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoClearAbort(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoClearAbort *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoClearAbort{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoClearAbort(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "clearAbort", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("ClearAbort napi_create_async_work start"); - AsyncCallbackInfoClearAbort *asynccallbackinfo = (AsyncCallbackInfoClearAbort *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoClearAbort *asyncCallbackInfo = (AsyncCallbackInfoClearAbort *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { asyncResult->ClearAbortCommonEvent(); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("ClearAbort napi_create_async_work end"); - AsyncCallbackInfoClearAbort *asynccallbackinfo = (AsyncCallbackInfoClearAbort *)data; + AsyncCallbackInfoClearAbort *asyncCallbackInfo = (AsyncCallbackInfoClearAbort *)data; ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1421,18 +1504,18 @@ napi_value ParseParametersByGetAbort(const napi_env &env, const napi_value (&arg } void PaddingAsyncCallbackInfoGetAbort(const napi_env &env, const size_t &argc, - AsyncCallbackInfoGetAbort *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoGetAbort *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoGetAbort start"); if (argc >= GET_ABORT_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoGetAbort end"); @@ -1442,11 +1525,11 @@ napi_value GetAbortCommonEvent(napi_env env, napi_callback_info info) { EVENT_LOGI("GetAbort start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByGetAbort(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -1455,50 +1538,56 @@ napi_value GetAbortCommonEvent(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("GetAbort: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoGetAbort *asynccallbackinfo = - new AsyncCallbackInfoGetAbort{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoGetAbort(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoGetAbort *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoGetAbort{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoGetAbort(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "getAbort", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("GetAbort napi_create_async_work start"); - AsyncCallbackInfoGetAbort *asynccallbackinfo = (AsyncCallbackInfoGetAbort *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoGetAbort *asyncCallbackInfo = (AsyncCallbackInfoGetAbort *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { - asynccallbackinfo->abortEvent = asyncResult->GetAbortCommonEvent(); + asyncCallbackInfo->abortEvent = asyncResult->GetAbortCommonEvent(); } else { - asynccallbackinfo->abortEvent = false; + asyncCallbackInfo->abortEvent = false; } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("GetAbort napi_create_async_work end"); - AsyncCallbackInfoGetAbort *asynccallbackinfo = (AsyncCallbackInfoGetAbort *)data; + AsyncCallbackInfoGetAbort *asyncCallbackInfo = (AsyncCallbackInfoGetAbort *)data; - napi_value result = 0; - napi_get_boolean(env, asynccallbackinfo->abortEvent, &result); + napi_value result = nullptr; + napi_get_boolean(env, asyncCallbackInfo->abortEvent, &result); ReturnCallbackPromise( - env, asynccallbackinfo->isCallback, asynccallbackinfo->callback, asynccallbackinfo->deferred, result); + env, asyncCallbackInfo->isCallback, asyncCallbackInfo->callback, asyncCallbackInfo->deferred, result); + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1520,18 +1609,18 @@ napi_value ParseParametersByFinish(const napi_env &env, const napi_value (&argv) } void PaddingAsyncCallbackInfoFinish(const napi_env &env, const size_t &argc, - AsyncCallbackInfoFinish *&asynccallbackinfo, const napi_ref &callback, napi_value &promise) + AsyncCallbackInfoFinish *&asyncCallbackInfo, const napi_ref &callback, napi_value &promise) { EVENT_LOGI("PaddingAsyncCallbackInfoFinish start"); if (argc >= FINISH_MAX_PARA) { - asynccallbackinfo->callback = callback; - asynccallbackinfo->isCallback = true; + asyncCallbackInfo->callback = callback; + asyncCallbackInfo->isCallback = true; } else { napi_deferred deferred = nullptr; NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise)); - asynccallbackinfo->deferred = deferred; - asynccallbackinfo->isCallback = false; + asyncCallbackInfo->deferred = deferred; + asyncCallbackInfo->isCallback = false; } EVENT_LOGI("PaddingAsyncCallbackInfoFinish end"); @@ -1541,11 +1630,11 @@ napi_value FinishCommonEvent(napi_env env, napi_callback_info info) { EVENT_LOGI("Finish start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByFinish(env, argv, argc, callback) == nullptr) { return NapiGetNull(env); } @@ -1554,49 +1643,55 @@ napi_value FinishCommonEvent(napi_env env, napi_callback_info info) napi_unwrap(env, thisVar, (void **)&objectInfo); EVENT_LOGI("Finish: objectInfo = %{public}p", objectInfo); - AsyncCallbackInfoFinish *asynccallbackinfo = - new AsyncCallbackInfoFinish{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; - - napi_value promise = 0; - PaddingAsyncCallbackInfoFinish(env, argc, asynccallbackinfo, callback, promise); + AsyncCallbackInfoFinish *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoFinish{.env = env, .asyncWork = nullptr, .objectInfo = objectInfo}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + napi_value promise = nullptr; + PaddingAsyncCallbackInfoFinish(env, argc, asyncCallbackInfo, callback, promise); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "finish", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("Finish napi_create_async_work start"); - AsyncCallbackInfoFinish *asynccallbackinfo = (AsyncCallbackInfoFinish *)data; - std::shared_ptr asyncResult = GetAsyncResult(asynccallbackinfo->objectInfo); + AsyncCallbackInfoFinish *asyncCallbackInfo = (AsyncCallbackInfoFinish *)data; + std::shared_ptr asyncResult = GetAsyncResult(asyncCallbackInfo->objectInfo); if (asyncResult) { asyncResult->FinishCommonEvent(); } }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("Finish napi_create_async_work end"); - AsyncCallbackInfoFinish *asynccallbackinfo = (AsyncCallbackInfoFinish *)data; + AsyncCallbackInfoFinish *asyncCallbackInfo = (AsyncCallbackInfoFinish *)data; ReturnCallbackPromise(env, - asynccallbackinfo->isCallback, - asynccallbackinfo->callback, - asynccallbackinfo->deferred, + asyncCallbackInfo->isCallback, + asyncCallbackInfo->callback, + asyncCallbackInfo->deferred, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); - if (asynccallbackinfo->isCallback) { + if (asyncCallbackInfo->isCallback) { return NapiGetNull(env); } else { return promise; @@ -1648,49 +1743,52 @@ napi_value Subscribe(napi_env env, napi_callback_info info) // Argument parsing size_t argc = SUBSCRIBE_MAX_PARA; - napi_value argv[SUBSCRIBE_MAX_PARA]; + napi_value argv[SUBSCRIBE_MAX_PARA] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); NAPI_ASSERT(env, argc >= SUBSCRIBE_MAX_PARA, "Wrong number of arguments"); - napi_ref callback = 0; - std::shared_ptr subscriber; + napi_ref callback = nullptr; + std::shared_ptr subscriber = nullptr; if (ParseParametersBySubscribe(env, argv, subscriber, callback) == nullptr) { return NapiGetNull(env); } - AsyncCallbackInfoSubscribe *asynccallbackinfo = - new AsyncCallbackInfoSubscribe{.env = env, .asyncWork = nullptr, .subscriber = nullptr}; - EVENT_LOGI("Subscribe new asynccallbackinfo = %{public}p", asynccallbackinfo); - asynccallbackinfo->subscriber = subscriber; - asynccallbackinfo->callback = callback; - subscriberInstances[asynccallbackinfo->subscriber].asyncCallbackInfo.push_back(asynccallbackinfo); - EVENT_LOGI("Subscribe AsyncCallbackInfoSubscribe * asynccallbackinfo = %{public}p", asynccallbackinfo); + AsyncCallbackInfoSubscribe *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoSubscribe{.env = env, .asyncWork = nullptr, .subscriber = nullptr}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + EVENT_LOGI("Subscribe new asyncCallbackInfo = %{public}p", asyncCallbackInfo); + asyncCallbackInfo->subscriber = subscriber; + asyncCallbackInfo->callback = callback; + subscriberInstances[asyncCallbackInfo->subscriber].asyncCallbackInfo.push_back(asyncCallbackInfo); + EVENT_LOGI("Subscribe AsyncCallbackInfoSubscribe * asyncCallbackInfo = %{public}p", asyncCallbackInfo); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "Subscribe", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("Subscribe napi_create_async_work start"); - AsyncCallbackInfoSubscribe *asynccallbackinfo = (AsyncCallbackInfoSubscribe *)data; - asynccallbackinfo->subscriber->SetEnv(env); - asynccallbackinfo->subscriber->SetCallbackRef(asynccallbackinfo->callback); - CommonEventManager::SubscribeCommonEvent(asynccallbackinfo->subscriber); + AsyncCallbackInfoSubscribe *asyncCallbackInfo = (AsyncCallbackInfoSubscribe *)data; + asyncCallbackInfo->subscriber->SetEnv(env); + asyncCallbackInfo->subscriber->SetCallbackRef(asyncCallbackInfo->callback); + CommonEventManager::SubscribeCommonEvent(asyncCallbackInfo->subscriber); }, [](napi_env env, napi_status status, void *data) { EVENT_LOGI("Subscribe napi_create_async_work end"); - AsyncCallbackInfoSubscribe *asynccallbackinfo = (AsyncCallbackInfoSubscribe *)data; - napi_delete_async_work(env, asynccallbackinfo->asyncWork); + AsyncCallbackInfoSubscribe *asyncCallbackInfo = (AsyncCallbackInfoSubscribe *)data; + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); return NapiGetNull(env); } @@ -1699,7 +1797,7 @@ napi_value GetBundlenameByPublish(const napi_env &env, const napi_value &value, EVENT_LOGI("GetBundlenameByPublish start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; char str[STR_MAX_SIZE] = {0}; size_t strLen = 0; bool hasProperty = false; @@ -1721,7 +1819,7 @@ napi_value GetDataByPublish(const napi_env &env, const napi_value &value, std::s EVENT_LOGI("GetDataByPublish start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; char str[STR_MAX_SIZE] = {0}; size_t strLen = 0; bool hasProperty = false; @@ -1743,7 +1841,7 @@ napi_value GetCodeByPublish(const napi_env &env, const napi_value &value, int &c EVENT_LOGI("GetCodeByPublish start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; bool hasProperty = false; NAPI_CALL(env, napi_has_named_property(env, value, "code", &hasProperty)); @@ -1763,7 +1861,7 @@ napi_value GetSubscriberPermissionsByPublish( EVENT_LOGI("GetSubscriberPermissionsByPublish start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; bool isArray = false; char str[STR_MAX_SIZE] = {0}; size_t strLen = 0; @@ -1778,7 +1876,7 @@ napi_value GetSubscriberPermissionsByPublish( napi_get_array_length(env, result, &length); if (length > 0) { for (uint32_t i = 0; i < length; ++i) { - napi_value nSubscriberPermission; + napi_value nSubscriberPermission = nullptr; napi_get_element(env, result, i, &nSubscriberPermission); NAPI_CALL(env, napi_typeof(env, nSubscriberPermission, &valuetype)); NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); @@ -1799,7 +1897,7 @@ napi_value GetIsOrderedByPublish(const napi_env &env, const napi_value &value, b EVENT_LOGI("GetIsOrderedByPublish start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; bool hasProperty = false; NAPI_CALL(env, napi_has_named_property(env, value, "isOrdered", &hasProperty)); @@ -1818,7 +1916,7 @@ napi_value GetIsStickyByPublish(const napi_env &env, const napi_value &value, bo EVENT_LOGI("GetIsStickyByPublish start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; bool hasProperty = false; NAPI_CALL(env, napi_has_named_property(env, value, "isSticky", &hasProperty)); @@ -1881,28 +1979,28 @@ napi_value ParseParametersByPublish(const napi_env &env, const napi_value (&argv // argv[2]: callback if (argc == PUBLISH_MAX_PARA_BY_PUBLISHDATA) { NAPI_CALL(env, napi_typeof(env, argv[PUBLISH_MAX_PARA], &valuetype)); - NAPI_ASSERT(env, valuetype = napi_function, "Wrong argument type. Function expected."); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); napi_create_reference(env, argv[PUBLISH_MAX_PARA], 1, &callback); } else { NAPI_CALL(env, napi_typeof(env, argv[1], &valuetype)); - NAPI_ASSERT(env, valuetype = napi_function, "Wrong argument type. Function expected."); + NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); napi_create_reference(env, argv[1], 1, &callback); } return NapiGetNull(env); } -void PaddingCallbackInfoPublish(Want &want, AsyncCallbackInfoPublish *&asynccallbackinfo, +void PaddingCallbackInfoPublish(Want &want, AsyncCallbackInfoPublish *&asyncCallbackInfo, const CommonEventPublishDataByjs &commonEventPublishDatajs) { EVENT_LOGI("PaddingCallbackInfoPublish start"); want.SetBundle(commonEventPublishDatajs.bundleName); - asynccallbackinfo->commonEventData.SetCode(commonEventPublishDatajs.code); - asynccallbackinfo->commonEventData.SetData(commonEventPublishDatajs.data); - asynccallbackinfo->commonEventPublishInfo.SetSubscriberPermissions(commonEventPublishDatajs.subscriberPermissions); - asynccallbackinfo->commonEventPublishInfo.SetOrdered(commonEventPublishDatajs.isOrdered); - asynccallbackinfo->commonEventPublishInfo.SetSticky(commonEventPublishDatajs.isSticky); + asyncCallbackInfo->commonEventData.SetCode(commonEventPublishDatajs.code); + asyncCallbackInfo->commonEventData.SetData(commonEventPublishDatajs.data); + asyncCallbackInfo->commonEventPublishInfo.SetSubscriberPermissions(commonEventPublishDatajs.subscriberPermissions); + asyncCallbackInfo->commonEventPublishInfo.SetOrdered(commonEventPublishDatajs.isOrdered); + asyncCallbackInfo->commonEventPublishInfo.SetSticky(commonEventPublishDatajs.isSticky); } napi_value Publish(napi_env env, napi_callback_info info) @@ -1910,58 +2008,66 @@ napi_value Publish(napi_env env, napi_callback_info info) EVENT_LOGI("Publish start"); size_t argc = PUBLISH_MAX_PARA_BY_PUBLISHDATA; - napi_value argv[PUBLISH_MAX_PARA_BY_PUBLISHDATA]; + napi_value argv[PUBLISH_MAX_PARA_BY_PUBLISHDATA] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); NAPI_ASSERT(env, argc >= PUBLISH_MAX_PARA, "Wrong number of arguments"); std::string event; CommonEventPublishDataByjs commonEventPublishDatajs; - napi_ref callback = 0; + napi_ref callback = nullptr; if (ParseParametersByPublish(env, argv, argc, event, commonEventPublishDatajs, callback) == nullptr) { return NapiGetNull(env); } - AsyncCallbackInfoPublish *asynccallbackinfo = new AsyncCallbackInfoPublish{.env = env, .asyncWork = nullptr}; - - asynccallbackinfo->callback = callback; + AsyncCallbackInfoPublish *asyncCallbackInfo = + new (std::nothrow) AsyncCallbackInfoPublish{.env = env, .asyncWork = nullptr}; + if (asyncCallbackInfo == nullptr) { + EVENT_LOGE("asyncCallbackInfo is null"); + return NapiGetNull(env); + } + asyncCallbackInfo->callback = callback; // CommonEventData::want->action Want want; want.SetAction(event); if (argc == PUBLISH_MAX_PARA_BY_PUBLISHDATA) { - PaddingCallbackInfoPublish(want, asynccallbackinfo, commonEventPublishDatajs); + PaddingCallbackInfoPublish(want, asyncCallbackInfo, commonEventPublishDatajs); } - asynccallbackinfo->commonEventData.SetWant(want); + asyncCallbackInfo->commonEventData.SetWant(want); - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "Publish", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("Publish napi_create_async_work start"); - AsyncCallbackInfoPublish *asynccallbackinfo = (AsyncCallbackInfoPublish *)data; + AsyncCallbackInfoPublish *asyncCallbackInfo = (AsyncCallbackInfoPublish *)data; CommonEventManager::PublishCommonEvent( - asynccallbackinfo->commonEventData, asynccallbackinfo->commonEventPublishInfo); + asyncCallbackInfo->commonEventData, asyncCallbackInfo->commonEventPublishInfo); }, [](napi_env env, napi_status status, void *data) { - AsyncCallbackInfoPublish *asynccallbackinfo = (AsyncCallbackInfoPublish *)data; + AsyncCallbackInfoPublish *asyncCallbackInfo = (AsyncCallbackInfoPublish *)data; - SetCallback(env, asynccallbackinfo->callback, NapiGetNull(env)); - napi_delete_async_work(env, asynccallbackinfo->asyncWork); - if (asynccallbackinfo) { - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + SetCallback(env, asyncCallbackInfo->callback, NapiGetNull(env)); + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + if (asyncCallbackInfo) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, - (void *)asynccallbackinfo, - &asynccallbackinfo->asyncWork); + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); - NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); return NapiGetNull(env); } @@ -1997,7 +2103,7 @@ napi_value ParseParametersByUnsubscribe(const napi_env &env, const size_t &argc, EVENT_LOGI("ParseParametersByUnsubscribe start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; // argv[0]:subscriber NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type for arg0. Subscribe expected."); @@ -2018,7 +2124,7 @@ napi_value ParseParametersByUnsubscribe(const napi_env &env, const size_t &argc, return result; } -void NapiDeleteSubscribe(std::shared_ptr &subscriber) +void NapiDeleteSubscribe(const napi_env &env, std::shared_ptr &subscriber) { EVENT_LOGI("NapiDeleteSubscribe start"); @@ -2027,6 +2133,9 @@ void NapiDeleteSubscribe(std::shared_ptr &subscriber) EVENT_LOGI("NapiDeleteSubscribe size = %{public}zu", subscribe->second.asyncCallbackInfo.size()); for (auto asyncCallbackInfoSubscribe : subscribe->second.asyncCallbackInfo) { EVENT_LOGI("NapiDeleteSubscribe ptr = %{public}p", asyncCallbackInfoSubscribe); + if (asyncCallbackInfoSubscribe->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfoSubscribe->callback); + } delete asyncCallbackInfoSubscribe; asyncCallbackInfoSubscribe = nullptr; } @@ -2040,13 +2149,13 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info) // Argument parsing size_t argc = UNSUBSCRIBE_MAX_PARA; - napi_value argv[UNSUBSCRIBE_MAX_PARA]; + napi_value argv[UNSUBSCRIBE_MAX_PARA] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); - napi_ref callback = 0; - std::shared_ptr subscriber; - napi_value result; + napi_ref callback = nullptr; + std::shared_ptr subscriber = nullptr; + napi_value result = nullptr; result = ParseParametersByUnsubscribe(env, argc, argv, subscriber, callback); if (result == nullptr) { return NapiGetNull(env); @@ -2059,46 +2168,52 @@ napi_value Unsubscribe(napi_env env, napi_callback_info info) } AsyncCallbackInfoUnsubscribe *asynccallback = - new AsyncCallbackInfoUnsubscribe{.env = env, .asyncWork = nullptr, .subscriber = nullptr}; - + new (std::nothrow) AsyncCallbackInfoUnsubscribe{.env = env, .asyncWork = nullptr, .subscriber = nullptr}; + if (asynccallback == nullptr) { + EVENT_LOGE("asynccallback is null"); + return NapiGetNull(env); + } asynccallback->subscriber = subscriber; asynccallback->argc = argc; if (argc >= UNSUBSCRIBE_MAX_PARA) { asynccallback->callback = callback; } - napi_value resourceName; + napi_value resourceName = nullptr; napi_create_string_latin1(env, "Unsubscribe", NAPI_AUTO_LENGTH, &resourceName); // Asynchronous function call - napi_create_async_work( - env, + napi_create_async_work(env, nullptr, resourceName, [](napi_env env, void *data) { EVENT_LOGI("Unsubscribe napi_create_async_work start"); - AsyncCallbackInfoUnsubscribe *asynccallbackinfo = (AsyncCallbackInfoUnsubscribe *)data; + AsyncCallbackInfoUnsubscribe *asyncCallbackInfo = (AsyncCallbackInfoUnsubscribe *)data; - CommonEventManager::UnSubscribeCommonEvent(asynccallbackinfo->subscriber); + CommonEventManager::UnSubscribeCommonEvent(asyncCallbackInfo->subscriber); }, [](napi_env env, napi_status status, void *data) { - EVENT_LOGI("Subscribe napi_create_async_work end"); - AsyncCallbackInfoUnsubscribe *asynccallbackinfo = (AsyncCallbackInfoUnsubscribe *)data; + EVENT_LOGI("Unsubscribe napi_create_async_work end"); + AsyncCallbackInfoUnsubscribe *asyncCallbackInfo = (AsyncCallbackInfoUnsubscribe *)data; - if (asynccallbackinfo->argc >= UNSUBSCRIBE_MAX_PARA) { - napi_value result = 0; + if (asyncCallbackInfo->argc >= UNSUBSCRIBE_MAX_PARA) { + napi_value result = nullptr; napi_get_null(env, &result); - SetCallback(env, asynccallbackinfo->callback, result); + SetCallback(env, asyncCallbackInfo->callback, result); + } + + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); } - napi_delete_async_work(env, asynccallbackinfo->asyncWork); + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - NapiDeleteSubscribe(asynccallbackinfo->subscriber); + NapiDeleteSubscribe(env, asyncCallbackInfo->subscriber); - if (asynccallbackinfo) { - EVENT_LOGI("delete asynccallbackinfo"); - delete asynccallbackinfo; - asynccallbackinfo = nullptr; + if (asyncCallbackInfo) { + EVENT_LOGI("delete asyncCallbackInfo"); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; } }, (void *)asynccallback, @@ -2119,14 +2234,14 @@ napi_value GetEventsByCreateSubscriber(const napi_env &env, const napi_value &ar // events NAPI_CALL(env, napi_has_named_property(env, argv, "events", &hasProperty)); if (hasProperty) { - napi_value eventsNapi; + napi_value eventsNapi = nullptr; napi_get_named_property(env, argv, "events", &eventsNapi); napi_is_array(env, eventsNapi, &isArray); if (isArray) { napi_get_array_length(env, eventsNapi, &length); NAPI_ASSERT(env, length > 0, "The array is empty."); for (size_t i = 0; i < length; i++) { - napi_value event; + napi_value event = nullptr; napi_get_element(env, eventsNapi, i, &event); NAPI_CALL(env, napi_typeof(env, event, &valuetype)); NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); @@ -2148,7 +2263,7 @@ napi_value GetPublisherPermissionByCreateSubscriber( napi_valuetype valuetype; size_t strLen = 0; - napi_value result; + napi_value result = nullptr; char str[STR_MAX_SIZE] = {0}; // publisherPermission @@ -2171,7 +2286,7 @@ napi_value GetPublisherDeviceIdByCreateSubscriber( napi_valuetype valuetype; size_t strLen = 0; - napi_value result; + napi_value result = nullptr; char str[STR_MAX_SIZE] = {0}; // publisherDeviceId @@ -2192,7 +2307,7 @@ napi_value GetPriorityByCreateSubscriber(const napi_env &env, const napi_value & EVENT_LOGI("GetPriorityByCreateSubscriber start"); napi_valuetype valuetype; - napi_value result; + napi_value result = nullptr; int32_t value; // priority @@ -2213,7 +2328,7 @@ napi_value CommonEventSubscriberConstructor(napi_env env, napi_callback_info inf EVENT_LOGI("CommonEventSubscriberConstructor start"); size_t argc = 1; - napi_value argv[1]; + napi_value argv[1] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); @@ -2261,11 +2376,14 @@ napi_value CommonEventSubscriberConstructor(napi_env env, napi_callback_info inf subscriberInfo.SetPriority(priority); } - SubscriberInstance *objectInfo = new SubscriberInstance(subscriberInfo); + SubscriberInstance *objectInfo = new (std::nothrow) SubscriberInstance(subscriberInfo); + if (objectInfo == nullptr) { + EVENT_LOGE("objectInfo is null"); + return nullptr; + } EVENT_LOGI("CommonEventSubscriberConstructor objectInfo = %{public}p", objectInfo); - napi_wrap( - env, + napi_wrap(env, thisVar, objectInfo, [](napi_env env, void *data, void *hint) { @@ -2277,6 +2395,9 @@ napi_value CommonEventSubscriberConstructor(napi_env env, napi_callback_info inf if (subscriberInstance.first.get() == objectInfo) { for (auto asyncCallbackInfo : subscriberInstance.second.asyncCallbackInfo) { EVENT_LOGI("CommonEventSubscriberConstructor ptr = %{public}p", asyncCallbackInfo); + if (asyncCallbackInfo->callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->callback); + } delete asyncCallbackInfo; asyncCallbackInfo = nullptr; } @@ -2346,4 +2467,4 @@ napi_value CommonEventInit(napi_env env, napi_value exports) return exports; } } // namespace EventFwkNapi -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/common_event/src/support.cpp b/interfaces/kits/napi/common_event/src/support.cpp index 04d65284..59976d15 100644 --- a/interfaces/kits/napi/common_event/src/support.cpp +++ b/interfaces/kits/napi/common_event/src/support.cpp @@ -1,6 +1,6 @@ /* - * Copyright (c); 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License");; + * 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 * diff --git a/sa_profile/3299.xml b/sa_profile/3299.xml index 154aa53f..b9788925 100644 --- a/sa_profile/3299.xml +++ b/sa_profile/3299.xml @@ -1,5 +1,5 @@ -