From 7e54a285a833962649746cf8da3dba24400337b8 Mon Sep 17 00:00:00 2001 From: chensi10 Date: Fri, 13 Aug 2021 17:11:50 +0800 Subject: [PATCH] Update openharmony code Signed-off-by: chensi10 --- frameworks/kits/ability/native/BUILD.gn | 3 +- .../ability/native/include/ability_impl.h | 14 + .../ability/native/include/ability_thread.h | 11 + .../ability/native/src/ability_context.cpp | 7 +- .../kits/ability/native/src/ability_impl.cpp | 45 +- .../ability/native/src/ability_thread.cpp | 56 +- .../ability/native/src/page_ability_impl.cpp | 1 + frameworks/kits/ability/native/test/BUILD.gn | 24 + .../include/mock_ability_manager_client.h | 4 + .../include/mock_ability_manager_service.h | 5 + .../mock_resourceManager_interface1.cpp | 15 +- frameworks/kits/test/BUILD.gn | 3 + .../mock/AMS/mock_ability_manager_service.h | 5 + .../AMS/mock_serviceability_manager_service.h | 4 + interfaces/innerkits/ability_manager/BUILD.gn | 1 + .../ability_manager_headers.gni | 1 + .../include/ability_manager_client.h | 2 + .../include/ability_manager_interface.h | 12 + .../include/ability_scheduler_interface.h | 10 +- .../include/global_configuration.h | 71 + .../kits/js/@ohos.ability.featureability.d.ts | 31 +- .../kits/js/@ohos.ability.wantconstant.d.ts | 12 +- .../kits/js/@ohos.app.abilitymanager.d.ts | 70 +- interfaces/kits/js/@ohos.bundle.d.ts | 667 ++- interfaces/kits/js/ability/abilityresult.d.ts | 12 +- .../js/ability/startabilityparameter.d.ts | 12 +- interfaces/kits/js/ability/want.d.ts | 44 +- .../kits/js/app/abilitymissioninfo.d.ts | 32 +- interfaces/kits/js/app/context.d.ts | 85 + interfaces/kits/js/app/processinfo.d.ts | 12 +- .../kits/js/app/runningprocessinfo.d.ts | 32 +- interfaces/kits/js/bundle/abilityinfo.d.ts | 112 +- .../kits/js/bundle/applicationinfo.d.ts | 82 +- interfaces/kits/js/bundle/bundleinfo.d.ts | 152 +- .../kits/js/bundle/bundleinstaller.d.ts | 170 +- interfaces/kits/js/bundle/elementname.d.ts | 142 +- interfaces/kits/js/bundle/hapmoduleinfo.d.ts | 80 +- interfaces/kits/js/bundle/moduleinfo.d.ts | 86 +- interfaces/kits/napi/BUILD.gn | 1 + .../aafwk/featureAbility/feature_ability.cpp | 3102 +----------- .../aafwk/featureAbility/feature_ability.h | 665 +-- .../aafwk/featureAbility/napi_context.cpp | 120 +- .../napi/aafwk/inner/napi_common/BUILD.gn | 2 + .../napi_common}/feature_ability_common.h | 25 +- .../inner/napi_common/napi_common_ability.cpp | 4260 +++++++++++++++++ .../inner/napi_common/napi_common_ability.h | 209 + .../inner/napi_common/napi_common_data.h | 3 + .../inner/napi_common/napi_common_error.h | 11 +- .../inner/napi_common/napi_common_util.cpp | 40 +- .../inner/napi_common/napi_common_util.h | 2 + .../kits/napi/aafwk/particleAbility/BUILD.gn | 57 + .../aafwk/particleAbility/native_module.cpp | 55 + .../particleAbility/particle_ability.cpp | 264 + .../aafwk/particleAbility/particle_ability.h | 36 + services/abilitymgr/abilitymgr.gni | 1 + .../include/ability_manager_proxy.h | 8 + .../include/ability_manager_service.h | 8 + .../abilitymgr/include/ability_manager_stub.h | 1 + services/abilitymgr/include/ability_record.h | 21 + .../include/ability_scheduler_proxy.h | 2 + .../include/ability_scheduler_stub.h | 1 + .../include/ability_stack_manager.h | 9 + services/abilitymgr/include/lifecycle_deal.h | 3 + .../abilitymgr/include/pending_want_manager.h | 7 +- .../abilitymgr/src/ability_manager_client.cpp | 272 +- .../abilitymgr/src/ability_manager_proxy.cpp | 22 + .../src/ability_manager_service.cpp | 28 +- .../abilitymgr/src/ability_manager_stub.cpp | 17 + services/abilitymgr/src/ability_record.cpp | 36 + .../src/ability_scheduler_proxy.cpp | 28 + .../abilitymgr/src/ability_scheduler_stub.cpp | 27 +- .../abilitymgr/src/ability_stack_manager.cpp | 59 +- .../abilitymgr/src/global_configuration.cpp | 51 + services/abilitymgr/src/lifecycle_deal.cpp | 21 + .../abilitymgr/src/pending_want_manager.cpp | 15 +- .../libs/aakit/include/ability_scheduler.h | 2 + .../mock/libs/aakit/src/ability_scheduler.cpp | 3 + .../ability_scheduler_mock.h | 1 + .../ability_manager_stub_mock.h | 5 + .../ability_manager_stub_impl_mock.h | 5 + .../ability_manager_stub_mock.h | 1 + .../ability_schedule_stub_mock.h | 2 + .../ability_stack_manager_test.cpp | 121 +- .../mock_ability_scheduler.cpp | 3 + .../pending_want_manager_test.cpp | 10 +- .../mock/include/mock_ability_mgr_service.h | 1 + .../mock/include/mock_ability_scheduler.h | 1 + .../include/mock_ability_scheduler_stub.h | 1 + .../ability_mgr_service_test/BUILD.gn | 1 + .../moduletest/ability_record_test/BUILD.gn | 1 + .../moduletest/ability_stack_test/BUILD.gn | 1 + .../ability_stack_module_test.cpp | 140 +- tools/test/mock/mock_ability_manager_stub.h | 1 + 93 files changed, 7047 insertions(+), 4869 deletions(-) create mode 100644 interfaces/innerkits/ability_manager/include/global_configuration.h create mode 100644 interfaces/kits/js/app/context.d.ts mode change 100644 => 100755 interfaces/kits/napi/aafwk/featureAbility/feature_ability.h rename interfaces/kits/napi/aafwk/{featureAbility => inner/napi_common}/feature_ability_common.h (89%) create mode 100755 interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp create mode 100755 interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h create mode 100755 interfaces/kits/napi/aafwk/particleAbility/BUILD.gn create mode 100755 interfaces/kits/napi/aafwk/particleAbility/native_module.cpp create mode 100755 interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp create mode 100755 interfaces/kits/napi/aafwk/particleAbility/particle_ability.h create mode 100644 services/abilitymgr/src/global_configuration.cpp diff --git a/frameworks/kits/ability/native/BUILD.gn b/frameworks/kits/ability/native/BUILD.gn index d02d2c1d4eb..172d6c9ecae 100755 --- a/frameworks/kits/ability/native/BUILD.gn +++ b/frameworks/kits/ability/native/BUILD.gn @@ -36,7 +36,6 @@ config("ability_config") { "//third_party/node/src", "//foundation/multimodalinput/input/interfaces/native/innerkits/event/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", - "//foundation/appexecfwk/standard/interfaces/innerkits/task_dispatcher/include/dispatcher", "//foundation/appexecfwk/standard/interfaces/innerkits/task_dispatcher/include/task", "//foundation/appexecfwk/standard/interfaces/innerkits/task_dispatcher/include/threading", @@ -96,6 +95,7 @@ ohos_shared_library("abilitykit_native") { "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/featureAbility/napi_data_ability_helper.cpp", + "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_want.cpp", "//foundation/appexecfwk/standard/kits/appkit/native/app/src/ability_start_setting.cpp", @@ -114,6 +114,7 @@ ohos_shared_library("abilitykit_native") { "//foundation/ace/napi:ace_napi", "//foundation/appexecfwk/standard/common:libappexecfwk_common", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", "//foundation/appexecfwk/standard/interfaces/innerkits/task_dispatcher:appkit_dispatcher_td", "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", diff --git a/frameworks/kits/ability/native/include/ability_impl.h b/frameworks/kits/ability/native/include/ability_impl.h index 23347bd9702..bb30765e92c 100755 --- a/frameworks/kits/ability/native/include/ability_impl.h +++ b/frameworks/kits/ability/native/include/ability_impl.h @@ -284,6 +284,11 @@ public: */ void SetCallingContext(const std::string &deviceId, const std::string &bundleName, const std::string &abilityName); + /* + * @brief ScheduleUpdateConfiguration, scheduling update configuration. + */ + void ScheduleUpdateConfiguration(const AAFwk::GlobalConfiguration &config); + protected: /** * @brief Toggles the lifecycle status of Ability to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application @@ -341,6 +346,13 @@ protected: */ void SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info); + /** + * @brief Check if it needs to restore the data to the ability. + * + * @return Return true if success, otherwise return false. + */ + bool CheckAndRestore(); + int lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL; sptr token_; std::shared_ptr ability_; @@ -358,6 +370,8 @@ private: std::shared_ptr abilityLifecycleCallbacks_; std::shared_ptr applactionImpl_; std::shared_ptr contextDeal_; + bool hasSaveData_ = false; + PacMap restoreData_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/kits/ability/native/include/ability_thread.h b/frameworks/kits/ability/native/include/ability_thread.h index 8499c9442d4..ff4603188a3 100644 --- a/frameworks/kits/ability/native/include/ability_thread.h +++ b/frameworks/kits/ability/native/include/ability_thread.h @@ -35,6 +35,7 @@ namespace OHOS { namespace AppExecFwk { using AbilitySchedulerStub = OHOS::AAFwk::AbilitySchedulerStub; using LifeCycleStateInfo = OHOS::AAFwk::LifeCycleStateInfo; +using GlobalConfiguration = OHOS::AAFwk::GlobalConfiguration; class AbilityImpl; class Ability; class AbilityHandler; @@ -129,6 +130,11 @@ public: */ void ScheduleRestoreAbilityState(const PacMap &state); + /* + * @brief ScheduleUpdateConfiguration, scheduling update configuration. + */ + void ScheduleUpdateConfiguration(const GlobalConfiguration &config); + /** * @brief Send the result code and data to be returned by this Page ability to the caller. * When a Page ability is destroyed, the caller overrides the AbilitySlice#onAbilityResult(int, int, Want) method to @@ -323,6 +329,11 @@ private: */ void HandleRestoreAbilityState(const PacMap &state); + /* + * @brief Handle the scheduling update configuration. + */ + void HandleUpdateConfiguration(const GlobalConfiguration &config); + std::shared_ptr abilityImpl_ = nullptr; sptr token_; std::shared_ptr currentAbility_ = nullptr; diff --git a/frameworks/kits/ability/native/src/ability_context.cpp b/frameworks/kits/ability/native/src/ability_context.cpp index 0378df9e1e5..56d25f2585e 100755 --- a/frameworks/kits/ability/native/src/ability_context.cpp +++ b/frameworks/kits/ability/native/src/ability_context.cpp @@ -545,6 +545,7 @@ int AbilityContext::VerifySelfPermission(const std::string &permission) APP_LOGE("VerifySelfPermission failed to get bundle manager service"); return AppExecFwk::Constants::PERMISSION_NOT_GRANTED; } + return ptr->CheckPermission(bundle_name, permission); } @@ -622,11 +623,7 @@ bool AbilityContext::CanRequestPermission(const std::string &permission) */ int AbilityContext::VerifyCallingOrSelfPermission(const std::string &permission) { - if (VerifyCallingPermission(permission) != AppExecFwk::Constants::PERMISSION_GRANTED) { - return VerifySelfPermission(permission); - } else { - return AppExecFwk::Constants::PERMISSION_GRANTED; - } + return VerifySelfPermission(permission); } /** diff --git a/frameworks/kits/ability/native/src/ability_impl.cpp b/frameworks/kits/ability/native/src/ability_impl.cpp index 18989695c5a..2e5885f949a 100755 --- a/frameworks/kits/ability/native/src/ability_impl.cpp +++ b/frameworks/kits/ability/native/src/ability_impl.cpp @@ -201,8 +201,9 @@ void AbilityImpl::DispatchRestoreAbilityState(const PacMap &inState) return; } - APP_LOGD("AbilityImpl:: DispatchRestoreAbilityState"); - ability_->OnRestoreAbilityState(inState); + hasSaveData_ = true; + restoreData_ = inState; + APP_LOGI("AbilityImpl::DispatchRestoreAbilityState save the data"); } void AbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState) @@ -553,6 +554,31 @@ void AbilityImpl::SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info) contextDeal_->SetLifeCycleStateInfo(info); } +/** + * @brief Check if it needs to restore the data to the ability. + * + * @return Return true if need and success, otherwise return false. + */ +bool AbilityImpl::CheckAndRestore() +{ + APP_LOGI("AbilityImpl::CheckAndRestore called start"); + if (!hasSaveData_) { + APP_LOGE("AbilityImpl::CheckAndRestore hasSaveData_ is false"); + return false; + } + + if (ability_ == nullptr) { + APP_LOGE("AbilityImpl::CheckAndRestore ability_ is nullptr"); + return false; + } + + APP_LOGI("AbilityImpl::CheckAndRestore ready to restore"); + ability_->OnRestoreAbilityState(restoreData_); + + APP_LOGI("AbilityImpl::CheckAndRestore called end"); + return true; +} + /** * @brief Set deviceId/bundleName/abilityName of the calling ability * @@ -569,5 +595,20 @@ void AbilityImpl::SetCallingContext( ability_->SetCallingContext(deviceId, bundleName, abilityName); } } + +/* + * @brief ScheduleUpdateConfiguration, scheduling update configuration. + */ +void AbilityImpl::ScheduleUpdateConfiguration(const AAFwk::GlobalConfiguration &config) +{ + if (ability_ == nullptr) { + APP_LOGE("AbilityImpl::ScheduleUpdateConfiguration ability_ is nullptr"); + } + + Configuration configtest; + ability_->OnConfigurationUpdated(configtest); + // ability_->OnConfigurationUpdated(config); +} + } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/frameworks/kits/ability/native/src/ability_thread.cpp b/frameworks/kits/ability/native/src/ability_thread.cpp index 62491181f17..707f3d3532b 100755 --- a/frameworks/kits/ability/native/src/ability_thread.cpp +++ b/frameworks/kits/ability/native/src/ability_thread.cpp @@ -349,24 +349,14 @@ void AbilityThread::HandleRestoreAbilityState(const PacMap &state) */ void AbilityThread::ScheduleSaveAbilityState(PacMap &state) { - APP_LOGI("AbilityThread::ScheduleSaveAbilityState called"); - + APP_LOGI("AbilityThread::ScheduleSaveAbilityState called start"); if (abilityImpl_ == nullptr) { APP_LOGE("AbilityThread::ScheduleSaveAbilityState abilityImpl_ == nullptr"); return; } - auto task = [abilityThread = this, &state]() { abilityThread->HandleSaveAbilityState(state); }; - - if (abilityHandler_ == nullptr) { - APP_LOGE("AbilityThread::ScheduleSaveAbilityState abilityHandler_ == nullptr"); - return; - } - - bool ret = abilityHandler_->PostTask(task); - if (!ret) { - APP_LOGE("AbilityThread::ScheduleSaveAbilityState PostTask error"); - } + abilityImpl_->DispatchSaveAbilityState(state); + APP_LOGI("AbilityThread::ScheduleSaveAbilityState called end"); } /** @@ -375,24 +365,54 @@ void AbilityThread::ScheduleSaveAbilityState(PacMap &state) */ void AbilityThread::ScheduleRestoreAbilityState(const PacMap &state) { - APP_LOGI("AbilityThread::ScheduleRestoreAbilityState called"); + APP_LOGI("AbilityThread::ScheduleRestoreAbilityState called start"); + if (abilityImpl_ == nullptr) { + APP_LOGE("AbilityThread::ScheduleRestoreAbilityState abilityImpl_ == nullptr"); + return; + } + + abilityImpl_->DispatchRestoreAbilityState(state); + APP_LOGI("AbilityThread::ScheduleRestoreAbilityState called end"); +} + +/* + * @brief ScheduleUpdateConfiguration, scheduling update configuration. + */ +void AbilityThread::ScheduleUpdateConfiguration(const GlobalConfiguration &config) +{ + APP_LOGI("AbilityThread::ScheduleUpdateConfiguration called"); if (abilityImpl_ == nullptr) { - APP_LOGE("ScheduleRestoreAbilityState::failed"); + APP_LOGE("AbilityThread::ScheduleUpdateConfiguration abilityImpl_ is nullptr"); return; } - auto task = [abilityThread = this, state]() { abilityThread->HandleRestoreAbilityState(state); }; + + auto task = [abilitThread = this, config]() { abilitThread->HandleUpdateConfiguration(config); }; if (abilityHandler_ == nullptr) { - APP_LOGE("AbilityThread::ScheduleRestoreAbilityState abilityHandler_ == nullptr"); + APP_LOGE("AbilityThread::ScheduleUpdateConfiguration abilityHandler_ is nullptr"); return; } bool ret = abilityHandler_->PostTask(task); if (!ret) { - APP_LOGE("AbilityThread::ScheduleRestoreAbilityState PostTask error"); + APP_LOGE("AbilityThread::ScheduleUpdateConfiguration PostTask error"); } } +/* + * @brief Handle the scheduling update configuration. + */ +void AbilityThread::HandleUpdateConfiguration(const GlobalConfiguration &config) +{ + APP_LOGI("AbilityThread::HandleUpdateConfiguration called"); + if (abilityImpl_ == nullptr) { + APP_LOGE("AbilityThread::HandleUpdateConfiguration abilityImpl_ is nullptr"); + return; + } + + abilityImpl_->ScheduleUpdateConfiguration(config); +} + /** * @description: Provide operating system AbilityTransaction information to the observer * @param want Indicates the structure containing Transaction information about the ability. diff --git a/frameworks/kits/ability/native/src/page_ability_impl.cpp b/frameworks/kits/ability/native/src/page_ability_impl.cpp index 6e5b840dadc..c79a755b5ae 100755 --- a/frameworks/kits/ability/native/src/page_ability_impl.cpp +++ b/frameworks/kits/ability/native/src/page_ability_impl.cpp @@ -41,6 +41,7 @@ void PageAbilityImpl::HandleAbilityTransaction(const Want &want, const AAFwk::Li if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) { Start(want); + CheckAndRestore(); } if (lifecycleState_ == AAFwk::ABILITY_STATE_ACTIVE) { diff --git a/frameworks/kits/ability/native/test/BUILD.gn b/frameworks/kits/ability/native/test/BUILD.gn index 229430987b5..485526c588f 100755 --- a/frameworks/kits/ability/native/test/BUILD.gn +++ b/frameworks/kits/ability/native/test/BUILD.gn @@ -818,6 +818,30 @@ ohos_unittest("task_handler_client_test") { external_deps = [ "hiviewdfx_hilog_native:libhilog" ] } +ohos_unittest("ability_thread_for_ability_on_configuration_update") { + module_out_path = module_output_path + + sources = [ "//foundation/aafwk/standard/frameworks/kits/ability/native/test/unittest/ability_thread_for_ability_on_configuration_update.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", + "//foundation/appexecfwk/standard/kits:appkit_native", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + ohos_unittest("data_ability_result_test") { module_out_path = module_output_path sources = [ "//foundation/aafwk/standard/frameworks/kits/ability/native/test/unittest/data_ability_result_test.cpp" ] diff --git a/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client.h b/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client.h index 610fd1598d9..375cb8ad978 100644 --- a/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client.h +++ b/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_client.h @@ -37,6 +37,7 @@ namespace OHOS { namespace AppExecFwk { +using GlobalConfiguration = AAFwk::GlobalConfiguration; class MockAbility : public AbilityContext { public: MockAbility() = default; @@ -63,6 +64,9 @@ public: void ScheduleRestoreAbilityState(const PacMap &inState) {} + void ScheduleUpdateConfiguration(const GlobalConfiguration &config) + {} + void ScheduleDisconnectAbility(const Want &want) {} diff --git a/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_service.h b/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_service.h index 78da7367065..4fbf485efd9 100755 --- a/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_service.h +++ b/frameworks/kits/ability/native/test/mock/include/mock_ability_manager_service.h @@ -81,6 +81,11 @@ public: return 0; } + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) + { + return 0; + } + MOCK_METHOD1(GetAllStackInfo, int(StackInfo &stackInfo)); MOCK_METHOD2( GetWantSender, sptr(const WantSenderInfo &wantSenderInfo, const sptr &callerToken)); diff --git a/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp b/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp index 9f1cbeb88ff..f06f2782128 100644 --- a/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp +++ b/frameworks/kits/ability/native/test/mock/include/mock_resourceManager_interface1.cpp @@ -1,4 +1,17 @@ - +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ #include #include #include "gmock/gmock.h" diff --git a/frameworks/kits/test/BUILD.gn b/frameworks/kits/test/BUILD.gn index d8aa70497a0..1ae4f72f6bd 100755 --- a/frameworks/kits/test/BUILD.gn +++ b/frameworks/kits/test/BUILD.gn @@ -58,6 +58,7 @@ ohos_moduletest("ability_moduletest") { "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_proxy.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_stub.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/caller_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/global_configuration.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/image_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_state_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/mission_description_info.cpp", @@ -115,6 +116,7 @@ ohos_moduletest("ability_conetxt_test") { "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_proxy.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_stub.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/caller_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/global_configuration.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_state_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/mission_record_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/mission_stack_info.cpp", @@ -171,6 +173,7 @@ ohos_moduletest("data_ability_operation_moduletest") { "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_proxy.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/ability_scheduler_stub.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/caller_info.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/global_configuration.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/image_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_state_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/mission_description_info.cpp", diff --git a/frameworks/kits/test/mock/AMS/mock_ability_manager_service.h b/frameworks/kits/test/mock/AMS/mock_ability_manager_service.h index 286c4445cc2..f5f380f4ddc 100644 --- a/frameworks/kits/test/mock/AMS/mock_ability_manager_service.h +++ b/frameworks/kits/test/mock/AMS/mock_ability_manager_service.h @@ -111,6 +111,11 @@ public: return 0; } + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) + { + return 0; + } + AbilityLifeCycleState curstate_ = AbilityLifeCycleState::ABILITY_STATE_INITIAL; sptr abilityScheduler_; // kit interface used to schedule ability life Want want_; diff --git a/frameworks/kits/test/mock/AMS/mock_serviceability_manager_service.h b/frameworks/kits/test/mock/AMS/mock_serviceability_manager_service.h index 9db181de247..e88a16d952d 100644 --- a/frameworks/kits/test/mock/AMS/mock_serviceability_manager_service.h +++ b/frameworks/kits/test/mock/AMS/mock_serviceability_manager_service.h @@ -101,6 +101,10 @@ public: { return 0; } + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) + { + return 0; + } sptr AcquireDataAbility( const Uri &uri, bool tryBind, const sptr &callerToken) override diff --git a/interfaces/innerkits/ability_manager/BUILD.gn b/interfaces/innerkits/ability_manager/BUILD.gn index dfeddb31bbe..b3f6dbf4a23 100644 --- a/interfaces/innerkits/ability_manager/BUILD.gn +++ b/interfaces/innerkits/ability_manager/BUILD.gn @@ -45,6 +45,7 @@ ohos_shared_library("ability_manager") { "${services_path}/abilitymgr/src/ability_scheduler_proxy.cpp", "${services_path}/abilitymgr/src/ability_scheduler_stub.cpp", "${services_path}/abilitymgr/src/caller_info.cpp", + "${services_path}/abilitymgr/src/global_configuration.cpp", "${services_path}/abilitymgr/src/image_info.cpp", "${services_path}/abilitymgr/src/lifecycle_state_info.cpp", "${services_path}/abilitymgr/src/mission_description_info.cpp", diff --git a/interfaces/innerkits/ability_manager/ability_manager_headers.gni b/interfaces/innerkits/ability_manager/ability_manager_headers.gni index d6a305e895d..bec8f44a74d 100644 --- a/interfaces/innerkits/ability_manager/ability_manager_headers.gni +++ b/interfaces/innerkits/ability_manager/ability_manager_headers.gni @@ -29,6 +29,7 @@ ability_manager_headers = { "want_sender_info.h", "want_sender_interface.h", "want_receiver_interface.h", + "global_configuration.h", ] header_base = "interfaces/innerkits/ability_manager/include" } diff --git a/interfaces/innerkits/ability_manager/include/ability_manager_client.h b/interfaces/innerkits/ability_manager/include/ability_manager_client.h index f5955a07581..40fc2f0f303 100644 --- a/interfaces/innerkits/ability_manager/include/ability_manager_client.h +++ b/interfaces/innerkits/ability_manager/include/ability_manager_client.h @@ -324,6 +324,8 @@ public: */ int GetMissionLockModeState(); + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType); + sptr GetWantSender(const WantSenderInfo &wantSenderInfo, const sptr &callerToken); ErrCode SendWantSender(const sptr &target, const SenderInfo &senderInfo); diff --git a/interfaces/innerkits/ability_manager/include/ability_manager_interface.h b/interfaces/innerkits/ability_manager/include/ability_manager_interface.h index 17930e108b3..9dd2229e1c1 100644 --- a/interfaces/innerkits/ability_manager/include/ability_manager_interface.h +++ b/interfaces/innerkits/ability_manager/include/ability_manager_interface.h @@ -32,6 +32,7 @@ #include "sender_info.h" #include "want_sender_interface.h" #include "want_receiver_interface.h" +#include "global_configuration.h" namespace OHOS { namespace AAFwk { @@ -347,6 +348,14 @@ public: */ virtual int GetMissionLockModeState() = 0; + /** + * Updates the configuration by modifying the configuration. + * + * @param config Indicates the new configuration + * @return Returns ERR_OK on success, others on failure. + */ + virtual int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) = 0; + virtual sptr GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken) = 0; @@ -456,6 +465,9 @@ public: // ipc id for get mission lock mode state (36) GET_MISSION_LOCK_MODE_STATE, + // ipc id for update configuration (37) + UPDATE_CONFIGURATION, + // ipc id 1001-2000 for DMS // ipc id for starting ability (1001) START_ABILITY = 1001, diff --git a/interfaces/innerkits/ability_manager/include/ability_scheduler_interface.h b/interfaces/innerkits/ability_manager/include/ability_scheduler_interface.h index 989987c7021..cf3a34640af 100644 --- a/interfaces/innerkits/ability_manager/include/ability_scheduler_interface.h +++ b/interfaces/innerkits/ability_manager/include/ability_scheduler_interface.h @@ -21,6 +21,7 @@ #include "dummy_values_bucket.h" #include "dummy_data_ability_predicates.h" #include "dummy_result_set.h" +#include "global_configuration.h" #include "lifecycle_state_info.h" #include "pac_map.h" #include "want.h" @@ -84,6 +85,11 @@ public: */ virtual void ScheduleRestoreAbilityState(const PacMap &inState) = 0; + /* + * ScheduleUpdateConfiguration, scheduling update configuration. + */ + virtual void ScheduleUpdateConfiguration(const GlobalConfiguration &config) = 0; + /** * @brief Obtains the MIME types of files supported. * @@ -248,8 +254,8 @@ public: // ipc id for scheduling BatchInsert​ SCHEDULE_BATCHINSERT, - // ipc id for display unlock message - DISPLAY_UNLOCK_MISSION_MESSAGE + // ipc id for scheduling update configuration + SCHEDULE_UPDATE_CONFIGURATION, }; }; } // namespace AAFwk diff --git a/interfaces/innerkits/ability_manager/include/global_configuration.h b/interfaces/innerkits/ability_manager/include/global_configuration.h new file mode 100644 index 00000000000..d1ca6449efd --- /dev/null +++ b/interfaces/innerkits/ability_manager/include/global_configuration.h @@ -0,0 +1,71 @@ +/* + * 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. + */ + +#ifndef OHOS_AAFWK_INTERFACES_INNERKITS_CONFIGURATION_H +#define OHOS_AAFWK_INTERFACES_INNERKITS_CONFIGURATION_H + +#include + +#include "nocopyable.h" +#include "parcel.h" + +namespace OHOS { +namespace AAFwk { + +class GlobalConfiguration : public Parcelable { +public: + GlobalConfiguration() = default; + explicit GlobalConfiguration(const std::string &name); + virtual ~GlobalConfiguration() = default; + + /** + * @brief Obtains the name of the Configuration. + * + * @return Returns the Configuration name. + */ + inline const std::string &GetName() const + { + return testInfostr_; + } + + /** + * @brief read this Sequenceable object from a Parcel. + * + * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled. + * @return Returns true if read successed; returns false otherwise. + */ + bool ReadFromParcel(Parcel &parcel); + + /** + * @brief Marshals this Sequenceable object into a Parcel. + * + * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled. + */ + virtual bool Marshalling(Parcel &parcel) const override; + + /** + * @brief Unmarshals this Sequenceable object from a Parcel. + * + * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled. + */ + static GlobalConfiguration *Unmarshalling(Parcel &parcel); + +private: + std::string testInfostr_; +}; + +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_AAFWK_INTERFACES_INNERKITS_CONFIGURATION_H \ No newline at end of file diff --git a/interfaces/kits/js/@ohos.ability.featureability.d.ts b/interfaces/kits/js/@ohos.ability.featureability.d.ts index 1672c635aa0..8cb481abddf 100755 --- a/interfaces/kits/js/@ohos.ability.featureability.d.ts +++ b/interfaces/kits/js/@ohos.ability.featureability.d.ts @@ -12,8 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { AsyncCallback } from './.basic'; -import { Context } from './app/context'; +import { AsyncCallback } from './basic'; import { Want } from './ability/want'; import { StartAbilityParameter, StartAbilityForResultParameter } from './ability/startabilityparameter'; import { AbilityResult, StartAbilityResult } from './ability/abilityresult'; @@ -21,16 +20,16 @@ import { AbilityResult, StartAbilityResult } from './ability/abilityresult'; /** * A Feature Ability represents an ability with a UI and is designed to interact with users. * @name featureAbility - * @since 3 + * @since 6 * @sysCap AAFwk - * @devices phone, tablet + * @devices phone * @permission N/A */ declare namespace featureAbility { /** * Checks whether the main window of this ability has window focus. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @param - * @return Returns true if this ability currently has window focus; returns false otherwise. @@ -40,8 +39,8 @@ declare namespace featureAbility { /** * Destroys the current ability. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @param - * @return - @@ -51,8 +50,8 @@ declare namespace featureAbility { /** * Obtain the want sended from the source ability. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @param parameter Indicates the ability to start. * @return - @@ -62,8 +61,8 @@ declare namespace featureAbility { /** * Starts a new ability. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @param parameter Indicates the ability to start. * @return - @@ -73,8 +72,8 @@ declare namespace featureAbility { /** * Starts a new ability, onAbilityResult will be called after this new ability is terminated. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @param parameter Indicates the ability to start. * @return - @@ -86,8 +85,8 @@ declare namespace featureAbility { /** * Sets the result code and data to be returned by this Feature ability to the caller. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @param AbilityResult Indicates the result code returned after the ability is destroyed. * You can define the result code to identify an error. diff --git a/interfaces/kits/js/@ohos.ability.wantconstant.d.ts b/interfaces/kits/js/@ohos.ability.wantconstant.d.ts index 467cad638fe..1255d54626b 100644 --- a/interfaces/kits/js/@ohos.ability.wantconstant.d.ts +++ b/interfaces/kits/js/@ohos.ability.wantconstant.d.ts @@ -16,18 +16,18 @@ /** * the constant for action and entity in the want * @name wantConstant - * @since 3 + * @since 6 * @sysCap aafwk - * @devices phone, tablet + * @devices phone * @permission N/A */ declare namespace wantConstant { /** * the constant for action of the want * @name Action - * @since 3 + * @since 6 * @sysCap aafwk - * @devices phone, tablet + * @devices phone * @permission N/A */ export enum Action { @@ -238,9 +238,9 @@ declare namespace wantConstant { /** * the constant for Entity of the want * @name Action - * @since 3 + * @since 6 * @sysCap aafwk - * @devices phone, tablet + * @devices phone * @permission N/A */ export enum Entity { diff --git a/interfaces/kits/js/@ohos.app.abilitymanager.d.ts b/interfaces/kits/js/@ohos.app.abilitymanager.d.ts index 62d3db42382..11cd65e965a 100644 --- a/interfaces/kits/js/@ohos.app.abilitymanager.d.ts +++ b/interfaces/kits/js/@ohos.app.abilitymanager.d.ts @@ -15,14 +15,14 @@ import { AbilityMissionInfo } from './app/abilitymissioninfo'; import { RunningProcessInfo } from './app/runningprocessinfo'; -import { AsyncCallback } from './.basic'; +import { AsyncCallback } from './basic'; /** * Obtains running process and memory information about an application. * @name abilityManager - * @since 3 + * @since 6 * @sysCap appexecfwk - * @devices phone, tablet + * @devices phone * @permission N/A */ declare namespace abilityManager { @@ -43,8 +43,8 @@ declare namespace abilityManager { * Obtains information about application processes that are running on the device. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @returns Returns a list of running processes. */ @@ -54,8 +54,8 @@ declare namespace abilityManager { * Obtains information about the application process running on the device through callback. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param callback Specified callback method. */ @@ -65,8 +65,8 @@ declare namespace abilityManager { * Queries information about the running Ability Mission. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @returns Returns the AbilityMissionInfos. */ @@ -76,8 +76,8 @@ declare namespace abilityManager { * Queries information about the running Ability Mission through callback. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param callback Specified callback method. */ @@ -87,8 +87,8 @@ declare namespace abilityManager { * Queries information about the recent Ability Mission. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @returns Returns the AbilityMissionInfos. */ @@ -98,8 +98,8 @@ declare namespace abilityManager { * Queries information about the recent Ability Mission. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param callback Specified callback method. */ @@ -109,8 +109,8 @@ declare namespace abilityManager { * Remove the mission associated with the given mission ID. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param missionId Indicates the mission ID * @returns Returns 0 for success, return non-0 for failure. @@ -121,8 +121,8 @@ declare namespace abilityManager { * Remove the mission associated with the given mission ID. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param missionId Indicates the mission ID * @param callback Specified callback method @@ -133,8 +133,8 @@ declare namespace abilityManager { * Remove the missions associated with the given array of the mission ID. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param missionIds Indicates the array of the mission ID * @returns Returns 0 for success, return non-0 for failure. @@ -145,8 +145,8 @@ declare namespace abilityManager { * Remove the missions associated with the given array of the mission ID. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param missionIds Indicates the array of the mission ID * @param callback Specified callback method @@ -157,8 +157,8 @@ declare namespace abilityManager { * Removes all the recent missions * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @returns Returns 0 for success, return non-0 for failure. */ @@ -168,8 +168,8 @@ declare namespace abilityManager { * Removes all the recent missions * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param callback Specified callback method. */ @@ -180,8 +180,8 @@ declare namespace abilityManager { * front of the stack. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param missionId Indicates the mission ID * @returns Returns 0 for success, return non-0 for failure. @@ -193,8 +193,8 @@ declare namespace abilityManager { * front of the stack. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param missionId Indicates the mission ID * @param callback Specified callback method. @@ -205,8 +205,8 @@ declare namespace abilityManager { * Kills all background processes associated with a specified bundle. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param bundleName Indicates the bundle name * @returns Returns 0 for success, return non-0 for failure. @@ -217,8 +217,8 @@ declare namespace abilityManager { * Kills all background processes associated with a specified bundle through callback. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk * @param bundleName Indicates the bundle name * @param callback Specified callback method diff --git a/interfaces/kits/js/@ohos.bundle.d.ts b/interfaces/kits/js/@ohos.bundle.d.ts index 537a609d62b..02a16d3a241 100644 --- a/interfaces/kits/js/@ohos.bundle.d.ts +++ b/interfaces/kits/js/@ohos.bundle.d.ts @@ -1,345 +1,324 @@ -/* - * 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 { ApplicationInfo } from './bundle/applicationinfo'; -import { BundleInfo } from './bundle/bundleinfo'; -import { AbilityInfo } from './bundle/abilityinfo'; -import { Want } from './ability/want'; -import { BundleInstaller } from './bundle/bundleinstaller'; -import permission from './@ohos.security.permission'; - -/** - * @name BundleFlag - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export declare interface BundleFlag { - getBundleDefault: boolean; - getBundleWithAbilities: boolean; - getAbilityInfoWithPermission: boolean; - getAbilityInfoWithApplication: boolean; - getApplicationInfoWithPermission: boolean; - getBundleWithRequestedPermission: boolean; - getAllApplicationInfo: boolean; -} - -export declare interface QueryParameter { - flags?: number; - userId?: string; -} - -/** - * bundle. - * @name bundle - * @since 3 - * @sysCap appexecfwk - * @devices phone, tablet - * @permission N/A - */ -declare namespace bundle { - export enum ModuleUpdateFlag { - FLAG_MODULE_UPGRADE_CHECK = 0, - FLAG_MODULE_UPGRADE_INSTALL = 1, - FLAG_MODULE_UPGRADE_INSTALL_WITH_CONFIG_WINDOWS = 2, - } - - export enum FormType { - JAVA = 0, - JS = 1, - } - - export enum ColorMode { - AUTO_MODE = -1, - DARK_MODE = 0, - LIGHT_MODE = 1, - } - - export enum GrantStatus { - PERMISSION_DENIED = -1, - PERMISSION_GRANTED = 0, - } - - export enum ModuleRemoveFlag { - FLAG_MODULE_NOT_USED_BY_FORM = 0, - FLAG_MODULE_USED_BY_FORM = 1, - FLAG_MODULE_NOT_USED_BY_SHORTCUT = 2, - FLAG_MODULE_USED_BY_SHORTCUT = 3, - } - - export enum SignatureCompareResult { - SIGNATURE_MATCHED = 0, - SIGNATURE_NOT_MATCHED = 1, - SIGNATURE_UNKNOWN_BUNDLE = 2, - } - - export enum ShortcutExistence { - SHORTCUT_EXISTENCE_EXISTS = 0, - SHORTCUT_EXISTENCE_NOT_EXISTS = 1, - SHORTCUT_EXISTENCE_UNKNOW = 2, - } - - export enum QueryShortCutFlag { - QUERY_SHORTCUT_HOME = 0, - } - - /** - * @name AbilityType - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ - export enum AbilityType { - UNKNOWN, - PAGE, - SERVICE, - DATA, - } - - /** - * @name AbilitySubType - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ - export enum AbilitySubType { - UNSPECIFIED = 0, - CA = 1, - } - - /** - * @name DisplayOrientation - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ - export enum DisplayOrientation { - UNSPECIFIED, - LANDSCAPE, - PORTRAIT, - FOLLOWRECENT, - } - - /** - * @name LaunchMode - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ - export enum LaunchMode { - SINGLETON = 0, - STANDARD = 1, - } - - export enum InstallErrorCode{ - SUCCESS = 0, - STATUS_INSTALL_FAILURE = 1, - STATUS_INSTALL_FAILURE_ABORTED = 2, - STATUS_INSTALL_FAILURE_INVALID = 3, - STATUS_INSTALL_FAILURE_CONFLICT = 4, - STATUS_INSTALL_FAILURE_STORAGE = 5, - STATUS_INSTALL_FAILURE_INCOMPATIBLE = 6, - STATUS_UNINSTALL_FAILURE = 7, - STATUS_UNINSTALL_FAILURE_BLOCKED = 8, - STATUS_UNINSTALL_FAILURE_ABORTED = 9, - STATUS_UNINSTALL_FAILURE_CONFLICT = 10, - STATUS_INSTALL_FAILURE_DOWNLOAD_TIMEOUT = 0x0B, - STATUS_INSTALL_FAILURE_DOWNLOAD_FAILED = 0x0C, - STATUS_ABILITY_NOT_FOUND = 0x40, - STATUS_BMS_SERVICE_ERROR = 0x41 - } - - /** - * Obtains BundleInfo based on a given bundle name. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param bundleName Indicates the bundle name. - * @param callback Specified callback method. - */ - function getBundleInfo(bundleName: string, flags: number, callback: AsyncCallback) : void; - - /** - * Obtains BundleInfo based on a given bundle name. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param bundleName Indicates the bundle name. - * @return BundleInfo. - */ - function getBundleInfo(bundleName: string, flags: number) : Promise; - - /** - * Obtains Bundle installer to install or uninstall hap. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @return BundleInstaller. - */ - function getBundleInstaller(callback: AsyncCallback): void; - - /** - * Obtains Bundle installer to install or uninstall hap. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @return BundleInstaller. - */ - function getBundleInstaller(): Promise; - - /** - * Obtains the ApplicationInfo based on a given application name. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param bundleName Indicates the application name. - * @param callback Specified callback method. - */ - function getApplicationInfo(bundleName: string, flags: number, userId: number, callback: AsyncCallback) : void; - - /** - * Obtains the ApplicationInfo based on a given application name. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param bundleName Indicates the application name. - * @return ApplicationInfo. - */ - function getApplicationInfo(bundleName: string, flags: number, userId: number) : Promise; - - /** - * Query the AbilityInfo by the given Want. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param want Indicates the Want for the ability to be queried. - * @param callback Specified callback method. - */ - function queryAbilityByWant(want: Want, params: QueryParameter, callback: AsyncCallback>): void; - - /** - * Query the AbilityInfo by the given Want. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param want Indicates the Want for the ability to be queried. - * @return AbilityInfo. - */ - function queryAbilityByWant(want: Want, params: QueryParameter): Promise; - - /** - * Obtains BundleInfo of all bundles available in the system. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param callback Specified callback method. - */ - function getBundleInfos(flags: BundleFlag, callback: AsyncCallback>) : void; - - /** - * Obtains BundleInfo of all bundles available in the system. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @return Array of BundleInfo. - */ - function getBundleInfos(flags: BundleFlag) : Promise>; - - /** - * Obtains information about all installed applications. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param callback Specified callback method. - */ - function getApplicationInfos(flags: number, userId: number, callback: AsyncCallback>) : void; - - /** - * Obtains information about all installed applications. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param NA - * @return Array of ApplicationInfo. - */ - function getApplicationInfos(flags: number, userId: number) : Promise>; - - /** - * Obtains information about a bundle contained in a HAP. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param hapFilePath Indicates the path of the HAP. - * @param callback Specified callback method. - */ - function getBundleArchiveInfo(hapFilePath: string, flags: number, callback: AsyncCallback) : void; - - /** - * Obtains information about a bundle contained in a HAP. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param hapFilePath Indicates the path of the HAP. - * @return BundleInfo. - */ - function getBundleArchiveInfo(hapFilePath: string, flags: number) : Promise; - - /** - * Obtains detailed information about a specified permission. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param permissionName Indicates the name of the permission. - * @param callback Specified callback method. - */ - function getPermissionDef(permissionName: string, callback: AsyncCallback) : void; - /** - * Obtains detailed information about a specified permission. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param permissionName Indicates the name of the permission. - * @return PermissionDef. - */ - function getPermissionDef(permissionName: string) : Promise; -} - +/* + * 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 { ApplicationInfo } from './bundle/applicationinfo'; +import { BundleInfo } from './bundle/bundleinfo'; +import { AbilityInfo } from './bundle/abilityinfo'; +import { Want } from './ability/want'; +import { BundleInstaller } from './bundle/bundleinstaller'; + +/** + * @name BundleFlag + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export declare interface BundleFlag { + getBundleDefault: boolean; + getBundleWithAbilities: boolean; + getAbilityInfoWithPermission: boolean; + getAbilityInfoWithApplication: boolean; + getApplicationInfoWithPermission: boolean; + getBundleWithRequestedPermission: boolean; + getAllApplicationInfo: boolean; +} + +export declare interface QueryParameter { + flags?: number; + userId?: string; +} + +/** + * bundle. + * @name bundle + * @since 6 + * @sysCap appexecfwk + * @devices phone + * @permission N/A + */ +declare namespace bundle { + export enum ModuleUpdateFlag { + FLAG_MODULE_UPGRADE_CHECK = 0, + FLAG_MODULE_UPGRADE_INSTALL = 1, + FLAG_MODULE_UPGRADE_INSTALL_WITH_CONFIG_WINDOWS = 2, + } + + export enum FormType { + JAVA = 0, + JS = 1, + } + + export enum ColorMode { + AUTO_MODE = -1, + DARK_MODE = 0, + LIGHT_MODE = 1, + } + + export enum GrantStatus { + PERMISSION_DENIED = -1, + PERMISSION_GRANTED = 0, + } + + export enum ModuleRemoveFlag { + FLAG_MODULE_NOT_USED_BY_FORM = 0, + FLAG_MODULE_USED_BY_FORM = 1, + FLAG_MODULE_NOT_USED_BY_SHORTCUT = 2, + FLAG_MODULE_USED_BY_SHORTCUT = 3, + } + + export enum SignatureCompareResult { + SIGNATURE_MATCHED = 0, + SIGNATURE_NOT_MATCHED = 1, + SIGNATURE_UNKNOWN_BUNDLE = 2, + } + + export enum ShortcutExistence { + SHORTCUT_EXISTENCE_EXISTS = 0, + SHORTCUT_EXISTENCE_NOT_EXISTS = 1, + SHORTCUT_EXISTENCE_UNKNOW = 2, + } + + export enum QueryShortCutFlag { + QUERY_SHORTCUT_HOME = 0, + } + + /** + * @name AbilityType + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ + export enum AbilityType { + UNKNOWN, + PAGE, + SERVICE, + DATA, + } + + /** + * @name AbilitySubType + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ + export enum AbilitySubType { + UNSPECIFIED = 0, + CA = 1, + } + + /** + * @name DisplayOrientation + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ + export enum DisplayOrientation { + UNSPECIFIED, + LANDSCAPE, + PORTRAIT, + FOLLOWRECENT, + } + + /** + * @name LaunchMode + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ + export enum LaunchMode { + SINGLETON = 0, + STANDARD = 1, + } + + export enum InstallErrorCode{ + SUCCESS = 0, + STATUS_INSTALL_FAILURE = 1, + STATUS_INSTALL_FAILURE_ABORTED = 2, + STATUS_INSTALL_FAILURE_INVALID = 3, + STATUS_INSTALL_FAILURE_CONFLICT = 4, + STATUS_INSTALL_FAILURE_STORAGE = 5, + STATUS_INSTALL_FAILURE_INCOMPATIBLE = 6, + STATUS_UNINSTALL_FAILURE = 7, + STATUS_UNINSTALL_FAILURE_BLOCKED = 8, + STATUS_UNINSTALL_FAILURE_ABORTED = 9, + STATUS_UNINSTALL_FAILURE_CONFLICT = 10, + STATUS_INSTALL_FAILURE_DOWNLOAD_TIMEOUT = 0x0B, + STATUS_INSTALL_FAILURE_DOWNLOAD_FAILED = 0x0C, + STATUS_ABILITY_NOT_FOUND = 0x40, + STATUS_BMS_SERVICE_ERROR = 0x41 + } + + /** + * Obtains BundleInfo based on a given bundle name. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param bundleName Indicates the bundle name. + * @param callback Specified callback method. + */ + function getBundleInfo(bundleName: string, flags: number, callback: AsyncCallback) : void; + + /** + * Obtains BundleInfo based on a given bundle name. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param bundleName Indicates the bundle name. + * @return BundleInfo. + */ + function getBundleInfo(bundleName: string, flags: number) : Promise; + + /** + * Obtains Bundle installer to install or uninstall hap. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @return BundleInstaller. + */ + function getBundleInstaller(callback: AsyncCallback): void; + + /** + * Obtains Bundle installer to install or uninstall hap. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @return BundleInstaller. + */ + function getBundleInstaller(): Promise; + + /** + * Obtains the ApplicationInfo based on a given application name. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param bundleName Indicates the application name. + * @param callback Specified callback method. + */ + function getApplicationInfo(bundleName: string, flags: number, userId: number, callback: AsyncCallback) : void; + + /** + * Obtains the ApplicationInfo based on a given application name. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param bundleName Indicates the application name. + * @return ApplicationInfo. + */ + function getApplicationInfo(bundleName: string, flags: number, userId: number) : Promise; + + /** + * Query the AbilityInfo by the given Want. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param want Indicates the Want for the ability to be queried. + * @param callback Specified callback method. + */ + function queryAbilityByWant(want: Want, params: QueryParameter, callback: AsyncCallback>): void; + + /** + * Query the AbilityInfo by the given Want. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param want Indicates the Want for the ability to be queried. + * @return AbilityInfo. + */ + function queryAbilityByWant(want: Want, params: QueryParameter): Promise; + + /** + * Obtains BundleInfo of all bundles available in the system. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param callback Specified callback method. + */ + function getBundleInfos(flags: BundleFlag, callback: AsyncCallback>) : void; + + /** + * Obtains BundleInfo of all bundles available in the system. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @return Array of BundleInfo. + */ + function getBundleInfos(flags: BundleFlag) : Promise>; + + /** + * Obtains information about all installed applications. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param callback Specified callback method. + */ + function getApplicationInfos(flags: number, userId: number, callback: AsyncCallback>) : void; + + /** + * Obtains information about all installed applications. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param N/A + * @return Array of ApplicationInfo. + */ + function getApplicationInfos(flags: number, userId: number) : Promise>; + + /** + * Obtains information about a bundle contained in a HAP. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param hapFilePath Indicates the path of the HAP. + * @param callback Specified callback method. + */ + function getBundleArchiveInfo(hapFilePath: string, flags: number, callback: AsyncCallback) : void; + + /** + * Obtains information about a bundle contained in a HAP. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param hapFilePath Indicates the path of the HAP. + * @return BundleInfo. + */ + function getBundleArchiveInfo(hapFilePath: string, flags: number) : Promise; + +} + export default bundle; \ No newline at end of file diff --git a/interfaces/kits/js/ability/abilityresult.d.ts b/interfaces/kits/js/ability/abilityresult.d.ts index f1c5907cfb6..ffbb2e26d44 100644 --- a/interfaces/kits/js/ability/abilityresult.d.ts +++ b/interfaces/kits/js/ability/abilityresult.d.ts @@ -19,8 +19,8 @@ export interface AbilityResult { * Indicates result code for startAbilityResult. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap aafwk */ resultCode: number; @@ -29,8 +29,8 @@ export interface AbilityResult { * Indicates the data returned after the ability is started. You can define the data returned. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap aafwk */ want?: Want; @@ -41,8 +41,8 @@ export interface StartAbilityResult extends AbilityResult { * Indicates the Want containing information about the target ability to start. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap aafwk */ requestCode: number; diff --git a/interfaces/kits/js/ability/startabilityparameter.d.ts b/interfaces/kits/js/ability/startabilityparameter.d.ts index 0fef0182221..e6bd5d6aaa2 100644 --- a/interfaces/kits/js/ability/startabilityparameter.d.ts +++ b/interfaces/kits/js/ability/startabilityparameter.d.ts @@ -19,8 +19,8 @@ export interface StartAbilityParameter { * Indicates the Want containing information about the target ability to start. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap aafwk */ want: Want; @@ -29,8 +29,8 @@ export interface StartAbilityParameter { * Indicates the special start setting used in starting ability. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap aafwk */ abilityStartSetting?: {[key: string]: any}; @@ -41,8 +41,8 @@ export interface StartAbilityForResultParameter extends StartAbilityParameter { * Indicates the Want containing information about the target ability to start. * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap aafwk */ requestCode: number; diff --git a/interfaces/kits/js/ability/want.d.ts b/interfaces/kits/js/ability/want.d.ts index fbbd63b92b6..2e5a130aa35 100644 --- a/interfaces/kits/js/ability/want.d.ts +++ b/interfaces/kits/js/ability/want.d.ts @@ -15,11 +15,11 @@ /** * @name WantOptions - * @since 3 + * @since 6 * @SysCap AAFwk * @import * @permission N/A - * @devices phone, tablet + * @devices phone */ export declare interface WantOptions { // indicates the grant to perform read operations on the URI @@ -66,17 +66,17 @@ export declare interface WantOptions { /** * Want is the basic communication component of the system. * @name Want - * @since 3 + * @since 6 * @sysCap AAFwk - * @devices phone, tablet + * @devices phone * @permission N/A */ export declare interface Want { /** * device id * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk */ deviceId?: string; @@ -84,8 +84,8 @@ export declare interface Want { /** * bundle name * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk */ bundleName?: string; @@ -93,16 +93,16 @@ export declare interface Want { /** * ability name * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk */ abilityName?: string; /** * The description of a URI in a Want. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @default - */ @@ -110,8 +110,8 @@ export declare interface Want { /** * The description of the type in this Want. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @default - */ @@ -119,8 +119,8 @@ export declare interface Want { /** * The options of the flags in this Want. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @default - */ @@ -128,8 +128,8 @@ export declare interface Want { /** * The description of an action in an want. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @default - */ @@ -137,8 +137,8 @@ export declare interface Want { /** * The description of the WantParams object in an Want - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @default - */ @@ -146,8 +146,8 @@ export declare interface Want { /** * The description of a entities in a Want. - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @sysCap AAFwk * @default - */ diff --git a/interfaces/kits/js/app/abilitymissioninfo.d.ts b/interfaces/kits/js/app/abilitymissioninfo.d.ts index 75a376e6865..3b64be448f2 100644 --- a/interfaces/kits/js/app/abilitymissioninfo.d.ts +++ b/interfaces/kits/js/app/abilitymissioninfo.d.ts @@ -16,19 +16,19 @@ import { ElementName } from '../bundle/elementname'; /** * @name Task stack information corresponding to ability - * @since 3 + * @since 6 * @SysCap appexecfwk * @import import AbilityMissionInfo from '@ohos.AbilityMissionInfo' * @permission N/A - * @devices phone, tablet + * @devices phone */ export interface AbilityMissionInfo { /** * Unique identification of task stack information corresponding to ability * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ id: number; @@ -38,8 +38,8 @@ export interface AbilityMissionInfo { * This can be considered the "application" of this task stack * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ baseAbility: ElementName; @@ -49,8 +49,8 @@ export interface AbilityMissionInfo { * This is what the user is currently doing * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ topAbility: ElementName; @@ -59,8 +59,8 @@ export interface AbilityMissionInfo { * The corresponding ability description information in the task stack * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ missionDescription: MissionDescriptionInfo; @@ -68,19 +68,19 @@ export interface AbilityMissionInfo { /** * @name Task stack description information - * @since 3 + * @since 6 * @SysCap appexecfwk * @import import app from '@system.app' * @permission N/A - * @devices phone, tablet + * @devices phone */ export interface MissionDescriptionInfo { /** * The label of the specified ability in the task stack * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ label: string; @@ -89,8 +89,8 @@ export interface MissionDescriptionInfo { * The path to the icon that specifies the ability in the task stack * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ iconPath: string; diff --git a/interfaces/kits/js/app/context.d.ts b/interfaces/kits/js/app/context.d.ts new file mode 100644 index 00000000000..a48c333654d --- /dev/null +++ b/interfaces/kits/js/app/context.d.ts @@ -0,0 +1,85 @@ +/* +* 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'; + +/** + * Provides the context of an object in the application and obtains application environment information. + * @name Context + * @since 6 + * @sysCap AAFwk + * @devices phone + * @permission N/A + */ +export interface Context { + /** + * Checks whether the calling or current process has the given permission. + * @devices phone + * @since 6 + * @sysCap AAFwk + * @param - + * @return Returns 0 if the calling or current process has the permission; returns -1 otherwise. + */ + verifyCallingOrSelfPermission(permission: string, callback: AsyncCallback): void; + verifyCallingOrSelfPermission(permission: string): Promise; + + /** + * Checks whether the calling process for inter-process communication has the given permission. + * @devices phone + * @since 6 + * @sysCap AAFwk + * @param - + * @return Returns 0 if the calling process has the permission; returns -1 otherwise. + */ + verifyCallingPermission(permission: string, callback: AsyncCallback): void; + verifyCallingPermission(permission: string): Promise; + + /** + * Checks whether a process ID (PID) and user ID (UID) have the given permission. + * @devices phone + * @since 6 + * @sysCap AAFwk + * @param - + * @return Returns 0 if the PID and UID have the permission; returns -1 otherwise. + */ + verifyPermission(permission: string, pid: number, uid: number, callback: AsyncCallback): void; + verifyPermission(permission: string, pid: number, uid: number): Promise; + + /** + * Checks whether the current process has the given permission. + * @devices phone + * @since 6 + * @sysCap AAFwk + * @param - + * @return Returns 0 if the current process has the permission; returns -1 otherwise. + */ + verifySelfPermission(permission: string, callback: AsyncCallback): void; + verifySelfPermission(permission: string): Promise; + + /** + * Confirms with the permission management module to check whether a request prompt is + * required for granting a certain permission. + * @devices phone + * @since 6 + * @sysCap AAFwk + * @param - + * @return Returns true if the current application does not have the permission and the user does not + * turn off further requests; returns false if the current application already has the permission, + * the permission is rejected by the system, or the permission is denied by the user and + * the user has turned off further requests. + */ + canRequestPermission(permission: string, callback: AsyncCallback): void; + canRequestPermission(permission: string): Promise; +} \ No newline at end of file diff --git a/interfaces/kits/js/app/processinfo.d.ts b/interfaces/kits/js/app/processinfo.d.ts index f249df8e46d..cf1c3989bf3 100644 --- a/interfaces/kits/js/app/processinfo.d.ts +++ b/interfaces/kits/js/app/processinfo.d.ts @@ -15,19 +15,19 @@ /** * @name This class saves process information about an application - * @since 3 + * @since 6 * @SysCap appexecfwk * @import import app from '@system.app' * @permission N/A - * @devices phone, tablet + * @devices phone */ export interface ProcessInfo { /** * The id of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ pid: number; @@ -36,8 +36,8 @@ export interface ProcessInfo { * The name of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ processName: string; diff --git a/interfaces/kits/js/app/runningprocessinfo.d.ts b/interfaces/kits/js/app/runningprocessinfo.d.ts index aae749e1b6b..71d35e2864c 100755 --- a/interfaces/kits/js/app/runningprocessinfo.d.ts +++ b/interfaces/kits/js/app/runningprocessinfo.d.ts @@ -16,19 +16,19 @@ import abilityManager from './../@ohos.app.abilitymanager'; /** * @name This class saves process information about an application - * @since 3 + * @since 6 * @SysCap appexecfwk * @import import app from '@system.app' * @permission N/A - * @devices phone, tablet + * @devices phone */ export interface RunningProcessInfo { /** * The id of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ pid: number; @@ -37,8 +37,8 @@ import abilityManager from './../@ohos.app.abilitymanager'; * The name of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ processName: string; @@ -47,8 +47,8 @@ import abilityManager from './../@ohos.app.abilitymanager'; * The list of packaget in the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ pkgList: Array; @@ -57,8 +57,8 @@ import abilityManager from './../@ohos.app.abilitymanager'; * The uid of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ uid: number; @@ -67,8 +67,8 @@ import abilityManager from './../@ohos.app.abilitymanager'; * the memory level of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ lastMemoryLevel: number; @@ -77,8 +77,8 @@ import abilityManager from './../@ohos.app.abilitymanager'; * the weight of the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ weight: number; @@ -87,8 +87,8 @@ import abilityManager from './../@ohos.app.abilitymanager'; * the ReasonCode of weight for the current process * * @default - - * @devices phone, tablet - * @since 3 + * @devices phone + * @since 6 * @SysCap appexecfwk */ weightReasonCode: abilityManager.WeightReasonCode; diff --git a/interfaces/kits/js/bundle/abilityinfo.d.ts b/interfaces/kits/js/bundle/abilityinfo.d.ts index 106ad326f82..2117aa3daa7 100644 --- a/interfaces/kits/js/bundle/abilityinfo.d.ts +++ b/interfaces/kits/js/bundle/abilityinfo.d.ts @@ -1,57 +1,57 @@ -/* - * 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 { ApplicationInfo } from './applicationinfo'; -import bundle from './../@ohos.bundle'; - -/** - * @name AbilityInfo - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface AbilityInfo { - readonly bundleName: string; - readonly name: string; - readonly label: string; - readonly description: string; - readonly icon: string; - readonly labelId: number; - readonly descriptionId: number; - readonly iconId: number; - readonly moduleName: string; - readonly process: string; - readonly targetAbility: string; - readonly backgroundModes: number; - readonly isVisible: boolean; - readonly formEnabled: boolean; - readonly type: bundle.AbilityType; - readonly subType: bundle.AbilitySubType; - readonly orientation: bundle.DisplayOrientation; - readonly launchMode: bundle.LaunchMode; - readonly permissions: Array; - readonly deviceTypes: Array; - readonly deviceCapabilities: Array; - readonly readPermission: string; - readonly writePermission: string; - readonly applicationInfo: ApplicationInfo; - readonly formEntity: number; - readonly minFormHeight: number; - readonly defaultFormHeight: number; - readonly minFormWidth: number; - readonly defaultFormWidth: number; - readonly uri: string; +/* + * 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 { ApplicationInfo } from './applicationinfo'; +import bundle from './../@ohos.bundle'; + +/** + * @name AbilityInfo + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface AbilityInfo { + readonly bundleName: string; + readonly name: string; + readonly label: string; + readonly description: string; + readonly icon: string; + readonly labelId: number; + readonly descriptionId: number; + readonly iconId: number; + readonly moduleName: string; + readonly process: string; + readonly targetAbility: string; + readonly backgroundModes: number; + readonly isVisible: boolean; + readonly formEnabled: boolean; + readonly type: bundle.AbilityType; + readonly subType: bundle.AbilitySubType; + readonly orientation: bundle.DisplayOrientation; + readonly launchMode: bundle.LaunchMode; + readonly permissions: Array; + readonly deviceTypes: Array; + readonly deviceCapabilities: Array; + readonly readPermission: string; + readonly writePermission: string; + readonly applicationInfo: ApplicationInfo; + readonly formEntity: number; + readonly minFormHeight: number; + readonly defaultFormHeight: number; + readonly minFormWidth: number; + readonly defaultFormWidth: number; + readonly uri: string; } \ No newline at end of file diff --git a/interfaces/kits/js/bundle/applicationinfo.d.ts b/interfaces/kits/js/bundle/applicationinfo.d.ts index 50ccc0058d5..af3e1e6531f 100644 --- a/interfaces/kits/js/bundle/applicationinfo.d.ts +++ b/interfaces/kits/js/bundle/applicationinfo.d.ts @@ -1,42 +1,42 @@ -/* - * 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 { ModuleInfo } from './moduleinfo'; - -/** - * @name ApplicationInfo - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface ApplicationInfo { - readonly name: string; - readonly description: string; - readonly descriptionId: number; - readonly systemApp: boolean; - readonly enabled: boolean; - readonly label: string; - readonly labelId: string; - readonly icon: string; - readonly iconId: string; - readonly process: string; - readonly supportedModes: number; - readonly moduleSourceDirs: Array; - readonly permissions: Array; - readonly moduleInfos: Array; - flags: number; - readonly entryDir: string; +/* + * 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 { ModuleInfo } from './moduleinfo'; + +/** + * @name ApplicationInfo + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface ApplicationInfo { + readonly name: string; + readonly description: string; + readonly descriptionId: number; + readonly systemApp: boolean; + readonly enabled: boolean; + readonly label: string; + readonly labelId: string; + readonly icon: string; + readonly iconId: string; + readonly process: string; + readonly supportedModes: number; + readonly moduleSourceDirs: Array; + readonly permissions: Array; + readonly moduleInfos: Array; + flags: number; + readonly entryDir: string; } \ No newline at end of file diff --git a/interfaces/kits/js/bundle/bundleinfo.d.ts b/interfaces/kits/js/bundle/bundleinfo.d.ts index 2ec1b7df6af..2dbb3e40650 100644 --- a/interfaces/kits/js/bundle/bundleinfo.d.ts +++ b/interfaces/kits/js/bundle/bundleinfo.d.ts @@ -1,77 +1,77 @@ -/* - * 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 { AbilityInfo } from './abilityinfo'; -import { ApplicationInfo } from './applicationinfo'; -import { HapModuleInfo } from './hapmoduleinfo'; - -/** - * @name UsedScene - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface UsedScene { - abilities: Array; - when: string; -} - -/** - * @name ReqPermissionDetail - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface ReqPermissionDetail { - name: string; - reason: string; - usedScene: UsedScene; -} - -/** - * @name BundleInfo - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface BundleInfo { - readonly name: string; - readonly type: string; - readonly appId: string; - readonly uid: number; - readonly installTime: number; - readonly updateTime: number; - readonly appInfo: ApplicationInfo; - readonly abilityInfos: Array; - readonly reqPermissions: Array; - readonly reqPermissionDetails: Array; - readonly vendor: string; - readonly versionCode: number; - readonly versionName: string; - readonly compatibleVersion: number; - readonly targetVersion: number; - readonly isCompressNativeLibs: boolean; - readonly hapModuleInfos: Array; - readonly entryModuleName: string; - readonly cpuAbi: string; - readonly isSilentInstallation: string; - readonly minCompatibleVersionCode: number; - readonly entryInstallationFree: boolean; +/* + * 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 { AbilityInfo } from './abilityinfo'; +import { ApplicationInfo } from './applicationinfo'; +import { HapModuleInfo } from './hapmoduleinfo'; + +/** + * @name UsedScene + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface UsedScene { + abilities: Array; + when: string; +} + +/** + * @name ReqPermissionDetail + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface ReqPermissionDetail { + name: string; + reason: string; + usedScene: UsedScene; +} + +/** + * @name BundleInfo + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface BundleInfo { + readonly name: string; + readonly type: string; + readonly appId: string; + readonly uid: number; + readonly installTime: number; + readonly updateTime: number; + readonly appInfo: ApplicationInfo; + readonly abilityInfos: Array; + readonly reqPermissions: Array; + readonly reqPermissionDetails: Array; + readonly vendor: string; + readonly versionCode: number; + readonly versionName: string; + readonly compatibleVersion: number; + readonly targetVersion: number; + readonly isCompressNativeLibs: boolean; + readonly hapModuleInfos: Array; + readonly entryModuleName: string; + readonly cpuAbi: string; + readonly isSilentInstallation: string; + readonly minCompatibleVersionCode: number; + readonly entryInstallationFree: boolean; } \ No newline at end of file diff --git a/interfaces/kits/js/bundle/bundleinstaller.d.ts b/interfaces/kits/js/bundle/bundleinstaller.d.ts index fe2f5200df7..36b2ef289b9 100644 --- a/interfaces/kits/js/bundle/bundleinstaller.d.ts +++ b/interfaces/kits/js/bundle/bundleinstaller.d.ts @@ -1,86 +1,86 @@ -/* - * 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 bundle from './../@ohos.bundle'; - -/** - * @name InstallParam - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface InstallParam { - userId: number; - installFlag: number; - isKeepData: boolean; -} - -/** - * @name InstallStatus - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface InstallStatus { - status: bundle.InstallErrorCode; - /** - * The install result string message. - * - * @default - - * @devices phone, tablet - * @since 3 - * @SysCap BMS - */ - statusMessage: string; -} - -/** - * @name BundleInstaller - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface BundleInstaller { - /** - * Install an application in a HAP. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param bundleFilePaths Indicates the paths of the HAP. - * @param InstallParam Indicates the userId and whether keep data. - * @return InstallStatus - */ - install(bundleFilePaths: Array, param: InstallParam, callback: AsyncCallback): void; - - /** - * Uninstall an application. - * - * @devices phone, tablet - * @since 3 - * @SysCap BMS - * @param bundleName Indicates the bundle name. - * @param InstallParam Indicates the userId and whether keep data. - * @return InstallStatus - */ - uninstall(bundleName: string, param: InstallParam, callback: AsyncCallback): void; +/* + * 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 bundle from './../@ohos.bundle'; + +/** + * @name InstallParam + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface InstallParam { + userId: number; + installFlag: number; + isKeepData: boolean; +} + +/** + * @name InstallStatus + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface InstallStatus { + status: bundle.InstallErrorCode; + /** + * The install result string message. + * + * @default - + * @devices phone + * @since 6 + * @SysCap BMS + */ + statusMessage: string; +} + +/** + * @name BundleInstaller + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface BundleInstaller { + /** + * Install an application in a HAP. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param bundleFilePaths Indicates the paths of the HAP. + * @param InstallParam Indicates the userId and whether keep data. + * @return InstallStatus + */ + install(bundleFilePaths: Array, param: InstallParam, callback: AsyncCallback): void; + + /** + * Uninstall an application. + * + * @devices phone + * @since 6 + * @SysCap BMS + * @param bundleName Indicates the bundle name. + * @param InstallParam Indicates the userId and whether keep data. + * @return InstallStatus + */ + uninstall(bundleName: string, param: InstallParam, callback: AsyncCallback): void; } \ No newline at end of file diff --git a/interfaces/kits/js/bundle/elementname.d.ts b/interfaces/kits/js/bundle/elementname.d.ts index fa0552f7049..5e4072de1a0 100644 --- a/interfaces/kits/js/bundle/elementname.d.ts +++ b/interfaces/kits/js/bundle/elementname.d.ts @@ -1,71 +1,71 @@ -/* - * 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. - */ - -/** - * Contains basic Ability information, which uniquely identifies an ability. - * You can use this class to obtain values of the fields set in an element, - * such as the device ID, bundle name, and ability name. - * @name ElementName - * @since 3 - * @sysCap AAFwk - * @devices phone, tablet - * @permission N/A - */ -export interface ElementName { - /** - * device id - * @default - - * @devices phone, tablet - * @since 3 - * @sysCap AAFwk - */ - deviceId?: string; - - /** - * bundle name - * @default - - * @devices phone, tablet - * @since 3 - * @sysCap AAFwk - */ - bundleName: string; - - /** - * ability name - * @default - - * @devices phone, tablet - * @since 3 - * @sysCap AAFwk - */ - abilityName: string; - - /** - * shortName name - * @default - - * @devices phone, tablet - * @since 3 - * @sysCap AAFwk - */ - uri?: string; - - /** - * shortName name - * @default - - * @devices phone, tablet - * @since 3 - * @sysCap AAFwk - */ - shortName?: string; -} +/* + * 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. + */ + +/** + * Contains basic Ability information, which uniquely identifies an ability. + * You can use this class to obtain values of the fields set in an element, + * such as the device ID, bundle name, and ability name. + * @name ElementName + * @since 6 + * @sysCap AAFwk + * @devices phone + * @permission N/A + */ +export interface ElementName { + /** + * device id + * @default - + * @devices phone + * @since 6 + * @sysCap AAFwk + */ + deviceId?: string; + + /** + * bundle name + * @default - + * @devices phone + * @since 6 + * @sysCap AAFwk + */ + bundleName: string; + + /** + * ability name + * @default - + * @devices phone + * @since 6 + * @sysCap AAFwk + */ + abilityName: string; + + /** + * shortName name + * @default - + * @devices phone + * @since 6 + * @sysCap AAFwk + */ + uri?: string; + + /** + * shortName name + * @default - + * @devices phone + * @since 6 + * @sysCap AAFwk + */ + shortName?: string; +} diff --git a/interfaces/kits/js/bundle/hapmoduleinfo.d.ts b/interfaces/kits/js/bundle/hapmoduleinfo.d.ts index 161fbef3997..39422cdc781 100644 --- a/interfaces/kits/js/bundle/hapmoduleinfo.d.ts +++ b/interfaces/kits/js/bundle/hapmoduleinfo.d.ts @@ -1,41 +1,41 @@ -/* - * 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 {AbilityInfo} from "./abilityinfo"; - -/** - * @name HapModuleInfo - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface HapModuleInfo { - readonly name: string; - readonly description: string; - readonly descriptionId: number; - readonly icon: string; - readonly label: string; - readonly labelId: number; - readonly iconId: number; - readonly backgroundImg: string; - readonly supportedModes: number; - readonly reqCapabilities: Array; - readonly deviceTypes: Array; - readonly abilityInfos: Array; - readonly moduleName: string; - readonly mainAbilityName: string; - readonly installationFree: boolean; +/* + * 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 {AbilityInfo} from "./abilityinfo"; + +/** + * @name HapModuleInfo + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface HapModuleInfo { + readonly name: string; + readonly description: string; + readonly descriptionId: number; + readonly icon: string; + readonly label: string; + readonly labelId: number; + readonly iconId: number; + readonly backgroundImg: string; + readonly supportedModes: number; + readonly reqCapabilities: Array; + readonly deviceTypes: Array; + readonly abilityInfos: Array; + readonly moduleName: string; + readonly mainAbilityName: string; + readonly installationFree: boolean; } \ No newline at end of file diff --git a/interfaces/kits/js/bundle/moduleinfo.d.ts b/interfaces/kits/js/bundle/moduleinfo.d.ts index b856a0042b9..1f8ea57a0d0 100644 --- a/interfaces/kits/js/bundle/moduleinfo.d.ts +++ b/interfaces/kits/js/bundle/moduleinfo.d.ts @@ -1,44 +1,44 @@ -/* - * 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. - */ - -/** - * @name ModuleInfo - * @since 3 - * @SysCap BMS - * @import NA - * @permission NA - * @devices phone, tablet - */ -export interface ModuleInfo { - /** - * The module name. - * - * @default - - * @devices phone, tablet - * @since 3 - * @SysCap BMS - */ - readonly moduleName: string; - - /** - * The module source path. - * - * @default - - * @devices phone, tablet - * @since 3 - * @SysCap BMS - */ - readonly moduleSourceDir: string; +/* + * 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. + */ + +/** + * @name ModuleInfo + * @since 6 + * @SysCap BMS + * @import N/A + * @permission N/A + * @devices phone + */ +export interface ModuleInfo { + /** + * The module name. + * + * @default - + * @devices phone + * @since 6 + * @SysCap BMS + */ + readonly moduleName: string; + + /** + * The module source path. + * + * @default - + * @devices phone + * @since 6 + * @SysCap BMS + */ + readonly moduleSourceDir: string; } \ No newline at end of file diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index ca3fe779c28..d0d9b56c36f 100755 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -19,6 +19,7 @@ group("napi_packages") { "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/dataUriUtils:datauriutils", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/featureAbility:featureability", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common:napi_common", + "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/particleAbility:particleability", "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/wantConstant:wantconstant", ] } diff --git a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp index dad2a33f8cc..9fd57ab321f 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.cpp @@ -22,6 +22,7 @@ #include "napi_context.h" #include "element_name.h" #include "napi_data_ability_helper.h" +#include "../inner/napi_common/napi_common_ability.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; @@ -62,6 +63,8 @@ napi_value FeatureAbilityInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getAbilityInfo", NAPI_GetAbilityInfo), DECLARE_NAPI_FUNCTION("getHapModuleInfo", NAPI_GetHapModuleInfo), DECLARE_NAPI_FUNCTION("getDataAbilityHelper", NAPI_GetDataAbilityHelper), + DECLARE_NAPI_FUNCTION("connectAbility", NAPI_FAConnectAbility), + DECLARE_NAPI_FUNCTION("disconnectAbility", NAPI_FADisConnectAbility), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties)); @@ -78,158 +81,8 @@ napi_value FeatureAbilityInit(napi_env env, napi_value exports) */ napi_value NAPI_StartAbility(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s,called", __func__); - AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value ret = StartAbilityWrap(env, info, asyncCallbackInfo); - if (ret == nullptr) { - if (asyncCallbackInfo != nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - } - } - return ret; -} - -/** - * @brief StartAbility processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value StartAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) -{ - - HILOG_INFO("%{public}s,called", __func__); - size_t argcAsync = 2; - const size_t argcPromise = 1; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = 0; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - CallAbilityParam param; - if (UnwrapParam(param, env, args[0]) == nullptr) { - HILOG_ERROR("%{public}s, call UnwrapParam failed.", __func__); - return nullptr; - } - asyncCallbackInfo->param = param; - asyncCallbackInfo->aceCallback = &g_aceCallbackInfo; - - if (argcAsync > argcPromise) { - ret = StartAbilityAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); - } else { - ret = StartAbilityPromise(env, asyncCallbackInfo); - } - - return ret; -} - -napi_value StartAbilityAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - - napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); - if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); - } - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("NAPI_StartAbility, worker pool thread execute."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - AbilityProcess::GetInstance()->StartAbility( - asyncCallbackInfo->ability, asyncCallbackInfo->param, *asyncCallbackInfo->aceCallback); - } - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_StartAbility, main event thread complete."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value callback = 0; - napi_value undefined = 0; - napi_value result[ARGS_TWO] = {0}; - napi_value callResult = 0; - napi_get_undefined(env, &undefined); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - napi_create_int32(env, 0, &result[PARAM1]); - napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); - napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); - - if (asyncCallbackInfo->cbInfo.callback != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - napi_value result = 0; - napi_get_null(env, &result); - return result; -} - -napi_value StartAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise = 0; - napi_create_promise(env, &deferred, &promise); - asyncCallbackInfo->deferred = deferred; - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("NAPI_StartAbility, worker pool thread execute."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - if (asyncCallbackInfo != nullptr) { - AbilityProcess::GetInstance()->StartAbility( - asyncCallbackInfo->ability, asyncCallbackInfo->param, *asyncCallbackInfo->aceCallback); - } - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_StartAbility, main event thread complete."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value result = 0; - napi_create_int32(env, 0, &result); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - return promise; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_StartAbilityCommon(env, info, AbilityType::PAGE); } /** @@ -767,31 +620,19 @@ napi_value NAPI_HasWindowFocus(napi_env env, napi_callback_info info) */ napi_value NAPI_GetContext(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s,called", __func__); - AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value ret = GetContextWrap(env, info, asyncCallbackInfo); - if (ret == nullptr) { - if (asyncCallbackInfo != nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - } - } - return ret; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetContextCommon(env, info, AbilityType::PAGE); } /** - * @brief GetContext processing function. + * @brief HasWindowFocus processing function. * * @param env The environment that the Node-API call is invoked under. * @param asyncCallbackInfo Process data asynchronously. * * @return Return JS data successfully, otherwise return nullptr. */ -napi_value GetContextWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +napi_value HasWindowFocusWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); if (asyncCallbackInfo == nullptr) { @@ -812,15 +653,15 @@ napi_value GetContextWrap(napi_env env, napi_callback_info info, AsyncCallbackIn } if (argcAsync > argcPromise) { - ret = GetContextAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + ret = HasWindowFocusAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); } else { - ret = GetContextPromise(env, asyncCallbackInfo); + ret = HasWindowFocusPromise(env, asyncCallbackInfo); } return ret; } -napi_value GetContextAsync( +napi_value HasWindowFocusAsync( napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, asyncCallback.", __func__); @@ -840,9 +681,15 @@ napi_value GetContextAsync( env, nullptr, resourceName, - [](napi_env env, void *data) { HILOG_INFO("GetContextAsync, worker pool thread execute."); }, + [](napi_env env, void *data) { + HILOG_INFO("NAPI_HasWindowFocus, worker pool thread execute."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + if (asyncCallbackInfo->ability != nullptr) { + asyncCallbackInfo->native_result = asyncCallbackInfo->ability->HasWindowFocus(); + } + }, [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetContextAsync, main event thread complete."); + HILOG_INFO("NAPI_HasWindowFocus, main event thread complete."); AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; napi_value callback = 0; napi_value undefined = 0; @@ -850,7 +697,7 @@ napi_value GetContextAsync( napi_value callResult = 0; napi_get_undefined(env, &undefined); result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - napi_new_instance(env, g_classContext, 0, nullptr, &result[PARAM1]); + napi_get_boolean(env, asyncCallbackInfo->native_result, &result[PARAM1]); napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); @@ -869,7 +716,7 @@ napi_value GetContextAsync( return result; } -napi_value GetContextPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) +napi_value HasWindowFocusPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) { HILOG_INFO("%{public}s, promise.", __func__); if (asyncCallbackInfo == nullptr) { @@ -887,12 +734,18 @@ napi_value GetContextPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) env, nullptr, resourceName, - [](napi_env env, void *data) { HILOG_INFO("GetContextPromise, worker pool thread execute."); }, + [](napi_env env, void *data) { + HILOG_INFO("NAPI_HasWindowFocus, worker pool thread execute."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + if (asyncCallbackInfo->ability != nullptr) { + asyncCallbackInfo->native_result = asyncCallbackInfo->ability->HasWindowFocus(); + } + }, [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetContextPromise, main event thread complete."); + HILOG_INFO("NAPI_HasWindowFocus, main event thread complete."); AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; napi_value result = 0; - napi_new_instance(env, g_classContext, 0, nullptr, &result); + napi_get_boolean(env, asyncCallbackInfo->native_result, &result); napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; @@ -906,178 +759,44 @@ napi_value GetContextPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) } /** - * @brief HasWindowFocus processing function. + * @brief The interface of onAbilityResult provided for ACE to call back to JS. * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. + * @param requestCode Indicates the request code returned after the ability is started. + * @param resultCode Indicates the result code returned after the ability is started. + * @param resultData Indicates the data returned after the ability is started. + * @param cb The environment and call back info that the Node-API call is invoked under. * - * @return Return JS data successfully, otherwise return nullptr. + * @return The return value from NAPI C++ to JS for the module. */ -napi_value HasWindowFocusWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +void CallOnAbilityResult(int requestCode, int resultCode, const Want &resultData, CallbackInfo cb) { - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, asyncCallbackInfo == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = 0; + HILOG_INFO("%{public}s,called env=%{public}p", __func__, cb.env); - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } + uv_loop_s *loop = nullptr; - if (argcAsync > argcPromise) { - ret = HasWindowFocusAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); - } else { - ret = HasWindowFocusPromise(env, asyncCallbackInfo); - } +#if NAPI_VERSION >= 2 + napi_get_uv_event_loop(cb.env, &loop); +#endif // NAPI_VERSION >= 2 - return ret; -} + uv_work_t *work = new uv_work_t; + OnAbilityCallback *onAbilityCB = new (std::nothrow) OnAbilityCallback{ + .requestCode = requestCode, + .resultCode = resultCode, + .resultData = resultData, + .cb = cb, + }; + work->data = (void *)onAbilityCB; -napi_value HasWindowFocusAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - - napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); - if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); - } - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("NAPI_HasWindowFocus, worker pool thread execute."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - if (asyncCallbackInfo->ability != nullptr) { - asyncCallbackInfo->native_result = asyncCallbackInfo->ability->HasWindowFocus(); - } - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_HasWindowFocus, main event thread complete."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value callback = 0; - napi_value undefined = 0; - napi_value result[ARGS_TWO] = {0}; - napi_value callResult = 0; - napi_get_undefined(env, &undefined); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - napi_get_boolean(env, asyncCallbackInfo->native_result, &result[PARAM1]); - napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); - napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); - - if (asyncCallbackInfo->cbInfo.callback != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - napi_value result = 0; - napi_get_null(env, &result); - return result; -} - -napi_value HasWindowFocusPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise = 0; - napi_create_promise(env, &deferred, &promise); - asyncCallbackInfo->deferred = deferred; - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("NAPI_HasWindowFocus, worker pool thread execute."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - if (asyncCallbackInfo->ability != nullptr) { - asyncCallbackInfo->native_result = asyncCallbackInfo->ability->HasWindowFocus(); - } - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("NAPI_HasWindowFocus, main event thread complete."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value result = 0; - napi_get_boolean(env, asyncCallbackInfo->native_result, &result); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - - return promise; -} - -/** - * @brief The interface of onAbilityResult provided for ACE to call back to JS. - * - * @param requestCode Indicates the request code returned after the ability is started. - * @param resultCode Indicates the result code returned after the ability is started. - * @param resultData Indicates the data returned after the ability is started. - * @param cb The environment and call back info that the Node-API call is invoked under. - * - * @return The return value from NAPI C++ to JS for the module. - */ -void CallOnAbilityResult(int requestCode, int resultCode, const Want &resultData, CallbackInfo cb) -{ - HILOG_INFO("%{public}s,called env=%{public}p", __func__, cb.env); - - uv_loop_s *loop = nullptr; - -#if NAPI_VERSION >= 2 - napi_get_uv_event_loop(cb.env, &loop); -#endif // NAPI_VERSION >= 2 - - uv_work_t *work = new uv_work_t; - OnAbilityCallback *onAbilityCB = new (std::nothrow) OnAbilityCallback{ - .requestCode = requestCode, - .resultCode = resultCode, - .resultData = resultData, - .cb = cb, - }; - work->data = (void *)onAbilityCB; - - uv_queue_work( - loop, - work, - [](uv_work_t *work) {}, - [](uv_work_t *work, int status) { - HILOG_INFO("CallOnAbilityResult, uv_queue_work"); - // JS Thread - OnAbilityCallback *event = (OnAbilityCallback *)work->data; - napi_value result[ARGS_TWO] = {0}; - result[PARAM0] = GetCallbackErrorValue(event->cb.env, NO_ERROR); + uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + HILOG_INFO("CallOnAbilityResult, uv_queue_work"); + // JS Thread + OnAbilityCallback *event = (OnAbilityCallback *)work->data; + napi_value result[ARGS_TWO] = {0}; + result[PARAM0] = GetCallbackErrorValue(event->cb.env, NO_ERROR); napi_create_object(event->cb.env, &result[PARAM1]); // create requestCode @@ -1204,175 +923,6 @@ napi_value UnwrapAbilityResult(CallAbilityParam ¶m, napi_env env, napi_value return result; } -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AsyncCallbackInfo on success, nullptr on failure - */ -AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env) -{ - napi_value global = 0; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = 0; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AsyncCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo{ - .cbInfo.env = env, - .asyncWork = nullptr, - .deferred = nullptr, - .ability = ability, - .native_result = false, - }; - return asyncCallbackInfo; -} - -napi_value GetWantAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - - napi_valuetype valuetype = napi_undefined; - napi_typeof(env, args[argcPromise], &valuetype); - if (valuetype == napi_function) { - napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); - } - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetWantAsync, worker pool thread execute."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - if (asyncCallbackInfo != nullptr && asyncCallbackInfo->ability != nullptr) { - std::shared_ptr ptrWant = asyncCallbackInfo->ability->GetWant(); - if (ptrWant != nullptr) { - asyncCallbackInfo->param.want = *ptrWant; - } - } - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetWantAsync, main event thread complete."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value callback = 0; - napi_value undefined = 0; - napi_value result[ARGS_TWO] = {0}; - napi_value callResult = 0; - napi_get_undefined(env, &undefined); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapWant(env, asyncCallbackInfo->param.want); - napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); - napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); - - if (asyncCallbackInfo->cbInfo.callback != nullptr) { - napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); - } - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - napi_value result = 0; - napi_get_null(env, &result); - return result; -} - -napi_value GetWantPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = 0; - napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); - napi_deferred deferred; - napi_value promise = 0; - napi_create_promise(env, &deferred, &promise); - asyncCallbackInfo->deferred = deferred; - - napi_create_async_work( - env, - nullptr, - resourceName, - [](napi_env env, void *data) { - HILOG_INFO("GetWantPromise, worker pool thread execute."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - - if (asyncCallbackInfo != nullptr && asyncCallbackInfo->ability != nullptr) { - std::shared_ptr ptrWant = asyncCallbackInfo->ability->GetWant(); - - if (ptrWant != nullptr) { - asyncCallbackInfo->param.want = *ptrWant; - } - } - }, - [](napi_env env, napi_status status, void *data) { - HILOG_INFO("GetWantPromise, main event thread complete."); - AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; - napi_value result = WrapWant(env, asyncCallbackInfo->param.want); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); - napi_delete_async_work(env, asyncCallbackInfo->asyncWork); - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - }, - (void *)asyncCallbackInfo, - &asyncCallbackInfo->asyncWork); - napi_queue_async_work(env, asyncCallbackInfo->asyncWork); - - return promise; -} - -/** - * @brief GetWantWrap processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetWantWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (asyncCallbackInfo == nullptr) { - HILOG_ERROR("%{public}s, asyncCallbackInfo == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = 0; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetWantAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); - } else { - ret = GetWantPromise(env, asyncCallbackInfo); - } - - return ret; -} - /** * @brief Get want. * @@ -1383,2499 +933,134 @@ napi_value GetWantWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo */ napi_value NAPI_GetWant(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s,called", __func__); - AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - return nullptr; - } - - napi_value ret = GetWantWrap(env, info, asyncCallbackInfo); - if (ret == nullptr) { - if (asyncCallbackInfo != nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - } - } - return ret; -} - -/** - * @brief Obtains information about the current application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetApplicationInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - AppInfoCB *appInfoCB = CreateAppInfoCBInfo(env); - if (appInfoCB == nullptr) { - return nullptr; - } - - napi_value ret = GetApplicationInfoWrap(env, info, appInfoCB); - if (ret == nullptr) { - if (appInfoCB != nullptr) { - delete appInfoCB; - appInfoCB = nullptr; - } - } - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AppInfoCB on success, nullptr on failure. - */ -AppInfoCB *CreateAppInfoCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AppInfoCB *appInfoCB = new (std::nothrow) AppInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return appInfoCB; -} - -/** - * @brief GetApplicationInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param appInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetApplicationInfoWrap(napi_env env, napi_callback_info info, AppInfoCB *appInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (appInfoCB == nullptr) { - HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetApplicationInfoAsync(env, args, argcAsync, argcPromise, appInfoCB); - } else { - ret = GetApplicationInfoPromise(env, appInfoCB); - } - - return ret; -} - -/** - * @brief GetApplicationInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param appInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetApplicationInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppInfoCB *appInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || appInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &appInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAppInfoExecuteCB, - GetAppInfoAsyncCompleteCB, - (void *)appInfoCB, - &appInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetApplicationInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param appInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetApplicationInfoPromise(napi_env env, AppInfoCB *appInfoCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (appInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - appInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAppInfoExecuteCB, - GetAppInfoPromiseCompleteCB, - (void *)appInfoCB, - &appInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetApplicationInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - AppInfoCB *appInfoCB = (AppInfoCB *)data; - if (appInfoCB->cbBase.ability != nullptr) { - std::shared_ptr appInfoPtr = appInfoCB->cbBase.ability->GetApplicationInfo(); - if (appInfoPtr != nullptr) { - SaveAppInfo(appInfoCB->appInfo, *appInfoPtr); - } - } -} - -void SaveAppInfo(AppInfo_ &appInfo, const ApplicationInfo &appInfoOrg) -{ - HILOG_INFO("%{public}s.", __func__); - appInfo.name = appInfoOrg.name; - appInfo.description = appInfoOrg.description; - appInfo.descriptionId = appInfoOrg.descriptionId; - appInfo.systemApp = appInfoOrg.isSystemApp; - appInfo.enabled = appInfoOrg.enabled; - appInfo.label = appInfoOrg.label; - appInfo.labelId = std::to_string(appInfoOrg.labelId); - appInfo.icon = appInfoOrg.iconPath; - appInfo.iconId = std::to_string(appInfoOrg.iconId); - appInfo.process = appInfoOrg.process; - appInfo.supportedModes = appInfoOrg.supportedModes; - for (size_t i = 0; i < appInfoOrg.moduleInfos.size(); i++) { - appInfo.moduleSourceDirs.emplace_back(appInfoOrg.moduleInfos.at(i).moduleSourceDir); - } - for (size_t i = 0; i < appInfoOrg.permissions.size(); i++) { - appInfo.permissions.emplace_back(appInfoOrg.permissions.at(i)); - } - for (size_t i = 0; i < appInfoOrg.moduleInfos.size(); i++) { - appInfo.moduleInfos.emplace_back(appInfoOrg.moduleInfos.at(i)); - } - appInfo.flags = appInfoOrg.flags; - appInfo.entryDir = appInfoOrg.entryDir; -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AppInfoCB *appInfoCB = (AppInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapAppInfo(env, appInfoCB->appInfo); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (appInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appInfoCB->cbBase.asyncWork)); - delete appInfoCB; - appInfoCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AppInfoCB *appInfoCB = (AppInfoCB *)data; - napi_value result = WrapAppInfo(env, appInfoCB->appInfo); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, appInfoCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appInfoCB->cbBase.asyncWork)); - delete appInfoCB; - appInfoCB = nullptr; -} - -napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo) -{ - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); - - NAPI_CALL(env, napi_create_int32(env, appInfo.descriptionId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, appInfo.systemApp, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "systemApp", proValue)); - NAPI_CALL(env, napi_get_boolean(env, appInfo.enabled, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "enabled", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.labelId.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.iconId.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.process.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "process", proValue)); - NAPI_CALL(env, napi_create_int32(env, appInfo.supportedModes, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "supportedModes", proValue)); - napi_value jsArrayModDirs = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayModDirs)); - for (size_t i = 0; i < appInfo.moduleSourceDirs.size(); i++) { - proValue = nullptr; - NAPI_CALL( - env, napi_create_string_utf8(env, appInfo.moduleSourceDirs.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayModDirs, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "moduleSourceDirs", jsArrayModDirs)); - napi_value jsArrayPermissions = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayPermissions)); - for (size_t i = 0; i < appInfo.permissions.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.permissions.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayPermissions, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "permissions", jsArrayPermissions)); - napi_value jsArrayModuleInfo = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayModuleInfo)); - for (size_t i = 0; i < appInfo.moduleInfos.size(); i++) { - napi_value jsModuleInfoObject = nullptr; - NAPI_CALL(env, napi_create_object(env, &jsModuleInfoObject)); - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8(env, appInfo.moduleInfos.at(i).moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, jsModuleInfoObject, "moduleName", proValue)); - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8( - env, appInfo.moduleInfos.at(i).moduleSourceDir.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, jsModuleInfoObject, "moduleSourceDir", proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayModuleInfo, i, jsModuleInfoObject)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "moduleInfos", jsArrayModuleInfo)); - NAPI_CALL(env, napi_create_int32(env, appInfo.flags, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "flags", proValue)); - NAPI_CALL(env, napi_create_string_utf8(env, appInfo.entryDir.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "entryDir", proValue)); - return result; -} - -/** - * @brief Obtains the type of this application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetAppType(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - AppTypeCB *appTypeCB = CreateAppTypeCBInfo(env); - if (appTypeCB == nullptr) { - return nullptr; - } - - napi_value ret = GetAppTypeWrap(env, info, appTypeCB); - if (ret == nullptr) { - if (appTypeCB != nullptr) { - delete appTypeCB; - appTypeCB = nullptr; - } - } - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AppTypeCB on success, nullptr on failure. - */ -AppTypeCB *CreateAppTypeCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AppTypeCB *appTypeCB = new (std::nothrow) AppTypeCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return appTypeCB; -} - -/** - * @brief GetAppType processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypeWrap(napi_env env, napi_callback_info info, AppTypeCB *appTypeCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, appTypeCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetAppTypeAsync(env, args, argcAsync, argcPromise, appTypeCB); - } else { - ret = GetAppTypePromise(env, appTypeCB); - } - - return ret; -} - -/** - * @brief GetAppType Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypeAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppTypeCB *appTypeCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &appTypeCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAppTypeExecuteCB, - GetAppTypeAsyncCompleteCB, - (void *)appTypeCB, - &appTypeCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetAppType Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypePromise(napi_env env, AppTypeCB *appTypeCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (appTypeCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - appTypeCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAppTypeExecuteCB, - GetAppTypePromiseCompleteCB, - (void *)appTypeCB, - &appTypeCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetAppType asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypeExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - AppTypeCB *appTypeCB = (AppTypeCB *)data; - if (appTypeCB->cbBase.ability != nullptr) { - appTypeCB->name = appTypeCB->cbBase.ability->GetAppType(); - } -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypeAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AppTypeCB *appTypeCB = (AppTypeCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - - NAPI_CALL_RETURN_VOID(env, - napi_create_string_utf8( - env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result[PARAM1])); - - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appTypeCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (appTypeCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appTypeCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appTypeCB->cbBase.asyncWork)); - delete appTypeCB; - appTypeCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("GetAppTypePromiseCompleteCB, main event thread complete."); - AppTypeCB *appTypeCB = (AppTypeCB *)data; - napi_value result = nullptr; - NAPI_CALL_RETURN_VOID( - env, napi_create_string_utf8(env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result)); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, appTypeCB->cbBase.deferred, result)); - - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appTypeCB->cbBase.asyncWork)); - delete appTypeCB; - appTypeCB = nullptr; -} - -/** - * @brief Obtains the elementName object of the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetElementName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - ElementNameCB *elementNameCB = CreateElementNameCBInfo(env); - if (elementNameCB == nullptr) { - return nullptr; - } - - napi_value ret = GetElementNameWrap(env, info, elementNameCB); - if (ret == nullptr) { - if (elementNameCB != nullptr) { - delete elementNameCB; - elementNameCB = nullptr; - } - } - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ElementNameCB on success, nullptr on failure. - */ -ElementNameCB *CreateElementNameCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - ElementNameCB *elementNameCB = new (std::nothrow) ElementNameCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return elementNameCB; -} - -/** - * @brief GetElementName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNameWrap(napi_env env, napi_callback_info info, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetElementNameAsync(env, args, argcAsync, argcPromise, elementNameCB); - } else { - ret = GetElementNamePromise(env, elementNameCB); - } - - return ret; -} - -/** - * @brief GetElementName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &elementNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetElementNameExecuteCB, - GetElementNameAsyncCompleteCB, - (void *)elementNameCB, - &elementNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetElementName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNamePromise(napi_env env, ElementNameCB *elementNameCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (elementNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - elementNameCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetElementNameExecuteCB, - GetElementNamePromiseCompleteCB, - (void *)elementNameCB, - &elementNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetElementName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNameExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - ElementNameCB *elementNameCB = (ElementNameCB *)data; - if (elementNameCB->cbBase.ability != nullptr) { - std::shared_ptr elementName = elementNameCB->cbBase.ability->GetElementName(); - if (elementNameCB != nullptr) { - elementNameCB->deviceId = elementName->GetDeviceID(); - elementNameCB->bundleName = elementName->GetBundleName(); - elementNameCB->abilityName = elementName->GetAbilityName(); - elementNameCB->uri = elementNameCB->cbBase.ability->GetWant()->GetUriString(); - elementNameCB->shortName = ""; - } - } -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNameAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - ElementNameCB *elementNameCB = (ElementNameCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapElementName(env, elementNameCB); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, elementNameCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (elementNameCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, elementNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, elementNameCB->cbBase.asyncWork)); - delete elementNameCB; - elementNameCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNamePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - ElementNameCB *elementNameCB = (ElementNameCB *)data; - napi_value result = WrapElementName(env, elementNameCB); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, elementNameCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, elementNameCB->cbBase.asyncWork)); - delete elementNameCB; - elementNameCB = nullptr; -} - -napi_value WrapElementName(napi_env env, ElementNameCB *elementNameCB) -{ - if (elementNameCB == nullptr) { - HILOG_ERROR("Invalid param(appInfoCB = nullptr)"); - return nullptr; - } - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->abilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "abilityName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->deviceId.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "deviceId", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->shortName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "shortName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); - - return result; -} - -/** - * @brief Obtains the class name in this ability name, without the prefixed bundle name. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetAbilityName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - AbilityNameCB *ablityNameCB = CreateAbilityNameCBInfo(env); - if (ablityNameCB == nullptr) { - return nullptr; - } - - napi_value ret = GetAbilityNameWrap(env, info, ablityNameCB); - if (ret == nullptr) { - if (ablityNameCB != nullptr) { - delete ablityNameCB; - ablityNameCB = nullptr; - } - } - return ret; -} - -/** - * @brief Obtains the process Info this application. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - ProcessInfoCB *processInfoCB = CreateProcessInfoCBInfo(env); - if (processInfoCB == nullptr) { - return nullptr; - } - - napi_value ret = GetProcessInfoWrap(env, info, processInfoCB); - if (ret == nullptr) { - if (processInfoCB != nullptr) { - delete processInfoCB; - processInfoCB = nullptr; - } - } - return ret; -} - -/** - * @brief Obtains the name of the current process. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetProcessName(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - - ProcessNameCB *processNameCB = CreateProcessNameCBInfo(env); - if (processNameCB == nullptr) { - return nullptr; - } - - napi_value ret = GetProcessNameWrap(env, info, processNameCB); - if (ret == nullptr) { - if (processNameCB != nullptr) { - delete processNameCB; - processNameCB = nullptr; - } - } - return ret; -} - -/** - * @brief Obtains the bundle name of the ability that called the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetCallingBundle(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - CallingBundleCB *calloingBundleCB = CreateCallingBundleCBInfo(env); - if (calloingBundleCB == nullptr) { - return nullptr; - } - - napi_value ret = GetCallingBundleWrap(env, info, calloingBundleCB); - if (ret == nullptr) { - if (calloingBundleCB != nullptr) { - delete calloingBundleCB; - calloingBundleCB = nullptr; - } - } - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AbilityNameCB on success, nullptr on failure. - */ -AbilityNameCB *CreateAbilityNameCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AbilityNameCB *abilityNameCB = new (std::nothrow) AbilityNameCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - HILOG_INFO("%{public}s, asyncCallback.", __func__); - return abilityNameCB; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ProcessInfoCB on success, nullptr on failure. - */ -ProcessInfoCB *CreateProcessInfoCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - ProcessInfoCB *processInfoCB = new (std::nothrow) ProcessInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return processInfoCB; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ProcessNameCB on success, nullptr on failure. - */ -ProcessNameCB *CreateProcessNameCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - ProcessNameCB *processNameCB = new (std::nothrow) ProcessNameCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return processNameCB; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to CallingBundleCB on success, nullptr on failure. - */ -CallingBundleCB *CreateCallingBundleCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - CallingBundleCB *callingBundleCB = new (std::nothrow) CallingBundleCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return callingBundleCB; -} - -/** - * @brief GetAbilityName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityNameWrap(napi_env env, napi_callback_info info, AbilityNameCB *abilityNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (abilityNameCB == nullptr) { - HILOG_ERROR("%{public}s, abilityNameCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetAbilityNameAsync(env, args, argcAsync, argcPromise, abilityNameCB); - } else { - ret = GetAbilityNamePromise(env, abilityNameCB); - } - - return ret; -} - -/** - * @brief GetProcessInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoWrap(napi_env env, napi_callback_info info, ProcessInfoCB *processInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, processInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetProcessInfoAsync(env, args, argcAsync, argcPromise, processInfoCB); - } else { - ret = GetProcessInfoPromise(env, processInfoCB); - } - - return ret; -} - -/** - * @brief GetProcessName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessNameWrap(napi_env env, napi_callback_info info, ProcessNameCB *processNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (processNameCB == nullptr) { - HILOG_ERROR("%{public}s, processNameCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetProcessNameAsync(env, args, argcAsync, argcPromise, processNameCB); - } else { - ret = GetProcessNamePromise(env, processNameCB); - } - - return ret; -} - -/** - * @brief GetCallingBundle processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundleWrap(napi_env env, napi_callback_info info, CallingBundleCB *callingBundleCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (callingBundleCB == nullptr) { - HILOG_ERROR("%{public}s, callingBundleCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetCallingBundleAsync(env, args, argcAsync, argcPromise, callingBundleCB); - } else { - ret = GetCallingBundlePromise(env, callingBundleCB); - } - - return ret; -} - -/** - * @brief GetAbilityName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param abilityNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityNameCB *abilityNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || abilityNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &abilityNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAbilityNameExecuteCB, - GetAbilityNameAsyncCompleteCB, - (void *)abilityNameCB, - &abilityNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, abilityNameCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetProcessInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessInfoCB *processInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &processInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetProcessInfoExecuteCB, - GetProcessInfoAsyncCompleteCB, - (void *)processInfoCB, - &processInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetProcessName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param ProcessNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessNameCB *processNameCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || processNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &processNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetProcessNameExecuteCB, - GetProcessNameAsyncCompleteCB, - (void *)processNameCB, - &processNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetCallingBundle Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundleAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, CallingBundleCB *callingBundleCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || callingBundleCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &callingBundleCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetCallingBundleExecuteCB, - GetCallingBundleAsyncCompleteCB, - (void *)callingBundleCB, - &callingBundleCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; -} - -/** - * @brief GetAbilityName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityNamePromise(napi_env env, AbilityNameCB *abilityNameCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (abilityNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - abilityNameCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAbilityNameExecuteCB, - GetAbilityNamePromiseCompleteCB, - (void *)abilityNameCB, - &abilityNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, abilityNameCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetProcessInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoPromise(napi_env env, ProcessInfoCB *processInfoCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (processInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - processInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetProcessInfoExecuteCB, - GetProcessInfoPromiseCompleteCB, - (void *)processInfoCB, - &processInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetProcessName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessNamePromise(napi_env env, ProcessNameCB *processNameCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (processNameCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - processNameCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetProcessNameExecuteCB, - GetProcessNamePromiseCompleteCB, - (void *)processNameCB, - &processNameCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetCallingBundle Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundlePromise(napi_env env, CallingBundleCB *callingBundleCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (callingBundleCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - callingBundleCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetCallingBundleExecuteCB, - GetCallingBundlePromiseCompleteCB, - (void *)callingBundleCB, - &callingBundleCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); - - return promise; -} - -/** - * @brief GetAbilityName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityNameExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetAbilityName, worker pool thread execute."); - AbilityNameCB *abilityNameCB = (AbilityNameCB *)data; - if (abilityNameCB->cbBase.ability != nullptr) { - abilityNameCB->name = abilityNameCB->cbBase.ability->GetAbilityName(); - } -} - -/** - * @brief GetProcessInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetProcessInfo, worker pool thread execute."); - ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; - if (processInfoCB->cbBase.ability != nullptr) { - std::shared_ptr processInfoPtr = processInfoCB->cbBase.ability->GetProcessInfo(); - if (processInfoPtr != nullptr) { - processInfoCB->processName = processInfoPtr->GetProcessName(); - processInfoCB->pid = processInfoPtr->GetPid(); - } - } -} - -/** - * @brief GetProcessName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessNameExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetProcessName, worker pool thread execute."); - ProcessNameCB *processNameCB = (ProcessNameCB *)data; - if (processNameCB->cbBase.ability != nullptr) { - processNameCB->processName = processNameCB->cbBase.ability->GetProcessName(); - } -} - -/** - * @brief GetCallingBundle asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundleExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetCallingBundle, worker pool thread execute."); - CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; - if (callingBundleCB->cbBase.ability != nullptr) { - callingBundleCB->callingBundleName = callingBundleCB->cbBase.ability->GetCallingBundle(); - } -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityNameAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetAbilityName, main event thread complete."); - AbilityNameCB *abilityNameCB = (AbilityNameCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapAbilityName(env, abilityNameCB); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, abilityNameCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - if (abilityNameCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, abilityNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityNameCB->cbBase.asyncWork)); - delete abilityNameCB; - abilityNameCB = nullptr; -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); - ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapProcessInfo(env, processInfoCB); - - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (processInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processInfoCB->cbBase.asyncWork)); - delete processInfoCB; - processInfoCB = nullptr; -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessNameAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetProcessName, main event thread complete."); - ProcessNameCB *processNameCB = (ProcessNameCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapProcessName(env, processNameCB); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processNameCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (processNameCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processNameCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processNameCB->cbBase.asyncWork)); - delete processNameCB; - processNameCB = nullptr; -} - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundleAsyncCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); - CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapCallingBundle(env, callingBundleCB); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, callingBundleCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (callingBundleCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, callingBundleCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork)); - delete callingBundleCB; - callingBundleCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityNamePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetAbilityName, main event thread complete."); - AbilityNameCB *abilityNameCB = (AbilityNameCB *)data; - napi_value result = WrapAbilityName(env, abilityNameCB); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, abilityNameCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityNameCB->cbBase.asyncWork)); - delete abilityNameCB; - abilityNameCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); - ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; - napi_value result = WrapProcessInfo(env, processInfoCB); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, processInfoCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processInfoCB->cbBase.asyncWork)); - delete processInfoCB; - processInfoCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessNamePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetProcessName, main event thread complete."); - ProcessNameCB *processNameCB = (ProcessNameCB *)data; - napi_value result = WrapProcessName(env, processNameCB); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, processNameCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processNameCB->cbBase.asyncWork)); - delete processNameCB; - processNameCB = nullptr; -} - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundlePromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); - CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; - napi_value result = WrapCallingBundle(env, callingBundleCB); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, callingBundleCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork)); - delete callingBundleCB; - callingBundleCB = nullptr; -} - -napi_value WrapAbilityName(napi_env env, AbilityNameCB *abilityNameCB) -{ - if (abilityNameCB == nullptr) { - HILOG_ERROR("Invalid param(abilityNameCB == nullptr)"); - return nullptr; - } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, abilityNameCB->name.c_str(), NAPI_AUTO_LENGTH, &result)); - - return result; -} - -napi_value WrapProcessInfo(napi_env env, ProcessInfoCB *processInfoCB) -{ - if (processInfoCB == nullptr) { - HILOG_ERROR("Invalid param(processInfoCB == nullptr)"); - return nullptr; - } - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_int32(env, processInfoCB->pid, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "pid", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, processInfoCB->processName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "processName", proValue)); - - return result; -} - -napi_value WrapProcessName(napi_env env, ProcessNameCB *processNameCB) -{ - if (processNameCB == nullptr) { - HILOG_ERROR("Invalid param(processNameCB == nullptr)"); - return nullptr; - } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, processNameCB->processName.c_str(), NAPI_AUTO_LENGTH, &result)); - - return result; -} - -napi_value WrapCallingBundle(napi_env env, CallingBundleCB *callingBundleCB) -{ - if (callingBundleCB == nullptr) { - HILOG_ERROR("Invalid param(callingBundleCB == nullptr)"); - return nullptr; - } - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, callingBundleCB->callingBundleName.c_str(), NAPI_AUTO_LENGTH, &result)); - - return result; -} - -/** - * @brief Obtains information about the current ability. - * - * @param env The environment that the Node-API call is invoked under. - * @param info The callback info passed into the callback function. - * - * @return The return value from NAPI C++ to JS for the module. - */ -napi_value NAPI_GetAbilityInfo(napi_env env, napi_callback_info info) -{ - HILOG_INFO("%{public}s,called", __func__); - AbilityInfoCB *abilityInfoCB = CreateAbilityInfoCBInfo(env); - if (abilityInfoCB == nullptr) { - return nullptr; - } - - napi_value ret = GetAbilityInfoWrap(env, info, abilityInfoCB); - if (ret == nullptr) { - if (abilityInfoCB != nullptr) { - delete abilityInfoCB; - abilityInfoCB = nullptr; - } - } - return ret; -} - -/** - * @brief GetAbilityInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoWrap(napi_env env, napi_callback_info info, AbilityInfoCB *abilityInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, abilityInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetAbilityInfoAsync(env, args, argcAsync, argcPromise, abilityInfoCB); - } else { - ret = GetAbilityInfoPromise(env, abilityInfoCB); - } - - return ret; -} - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AbilityInfoCB on success, nullptr on failure. - */ -AbilityInfoCB *CreateAbilityInfoCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - AbilityInfoCB *abilityInfoCB = new (std::nothrow) AbilityInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return abilityInfoCB; -} - -/** - * @brief GetAbilityInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityInfoCB *abilityInfoCB) -{ - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &abilityInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAbilityInfoExecuteCB, - GetAbilityInfoAsyncCompleteCB, - (void *)abilityInfoCB, - &abilityInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetWantCommon(env, info, AbilityType::PAGE); } /** - * @brief GetAbilityInfo Promise. + * @brief Obtains information about the current application. * * @param env The environment that the Node-API call is invoked under. - * @param abilityInfoCB Process data asynchronously. + * @param info The callback info passed into the callback function. * - * @return Return JS data successfully, otherwise return nullptr. + * @return The return value from NAPI C++ to JS for the module. */ -napi_value GetAbilityInfoPromise(napi_env env, AbilityInfoCB *abilityInfoCB) +napi_value NAPI_GetApplicationInfo(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s, promise.", __func__); - if (abilityInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - abilityInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetAbilityInfoExecuteCB, - GetAbilityInfoPromiseCompleteCB, - (void *)abilityInfoCB, - &abilityInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); - - return promise; + HILOG_INFO("%{public}s,called", __func__); + return NAPI_GetApplicationInfoCommon(env, info, AbilityType::PAGE); } /** - * @brief GetAbilityInfo asynchronous processing function. + * @brief Obtains the type of this application. * * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. */ -void GetAbilityInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); - AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; - if (abilityInfoCB->cbBase.ability != nullptr) { - std::shared_ptr abilityInfoPtr = abilityInfoCB->cbBase.ability->GetAbilityInfo(); - if (abilityInfoPtr != nullptr) { - SaveAbilityInfo(abilityInfoCB->abilityInfo, *abilityInfoPtr); - } - } -} - -void SaveAbilityInfo(AbilityInfo_ &abilityInfo, const AbilityInfo &abilityInfoOrg) +napi_value NAPI_GetAppType(napi_env env, napi_callback_info info) { - abilityInfo.bundleName = abilityInfoOrg.bundleName; - abilityInfo.name = abilityInfoOrg.name; - abilityInfo.label = abilityInfoOrg.label; - abilityInfo.description = abilityInfoOrg.description; - abilityInfo.icon = abilityInfoOrg.iconPath; - abilityInfo.labelId = abilityInfoOrg.applicationInfo.labelId; - abilityInfo.descriptionId = abilityInfoOrg.applicationInfo.descriptionId; - abilityInfo.iconId = abilityInfoOrg.applicationInfo.iconId; - abilityInfo.moduleName = abilityInfoOrg.moduleName; - abilityInfo.process = abilityInfoOrg.process; - abilityInfo.isVisible = abilityInfoOrg.visible; - - abilityInfo.type = static_cast(abilityInfoOrg.type); - abilityInfo.orientation = static_cast(abilityInfoOrg.orientation); - abilityInfo.launchMode = static_cast(abilityInfoOrg.launchMode); - - abilityInfo.uri = abilityInfoOrg.uri; - abilityInfo.targetAbility = abilityInfoOrg.targetAbility; - - for (size_t i = 0; i < abilityInfoOrg.permissions.size(); i++) { - abilityInfo.permissions.emplace_back(abilityInfoOrg.permissions.at(i)); - } - for (size_t i = 0; i < abilityInfoOrg.deviceTypes.size(); i++) { - abilityInfo.deviceTypes.emplace_back(abilityInfoOrg.deviceTypes.at(i)); - } - for (size_t i = 0; i < abilityInfoOrg.deviceCapabilities.size(); i++) { - abilityInfo.deviceCapabilities.emplace_back(abilityInfoOrg.deviceCapabilities.at(i)); - } - - SaveAppInfo(abilityInfo.appInfo, abilityInfoOrg.applicationInfo); - - abilityInfo.readPermission = abilityInfoOrg.readPermission; - abilityInfo.writePermission = abilityInfoOrg.writePermission; - abilityInfo.formEntity = 0; // no data - abilityInfo.minFormHeight = 0; // no data - abilityInfo.defaultFormHeight = 0; // no data - abilityInfo.minFormWidth = 0; // no data - abilityInfo.defaultFormWidth = 0; // no data - abilityInfo.backgroundModes = 0; // no data - abilityInfo.subType = 0; // no data - abilityInfo.formEnabled = false; // no data + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetAppTypeCommon(env, info, AbilityType::PAGE); } /** - * @brief The callback at the end of the asynchronous callback. + * @brief Obtains the elementName object of the current ability. * * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. */ -void GetAbilityInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +napi_value NAPI_GetElementName(napi_env env, napi_callback_info info) { - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, abilityInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (abilityInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, abilityInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork)); - delete abilityInfoCB; - abilityInfoCB = nullptr; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetElementNameCommon(env, info, AbilityType::PAGE); } /** - * @brief The callback at the end of the Promise callback. + * @brief Obtains the class name in this ability name, without the prefixed bundle name. * * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. */ -void GetAbilityInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) -{ - HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); - AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; - napi_value result = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, abilityInfoCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork)); - delete abilityInfoCB; - abilityInfoCB = nullptr; -} - -napi_value WrapAbilityInfo(napi_env env, const AbilityInfo_ &abilityInfo) -{ - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.process.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "process", proValue)); - - NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.type), &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "type", proValue)); - - NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.orientation), &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "orientation", proValue)); - - NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.launchMode), &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "launchMode", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.readPermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "readPermission", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.writePermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "writePermission", proValue)); - - NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.targetAbility.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "targetAbility", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.labelId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.descriptionId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.iconId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.formEntity, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "formEntity", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormHeight, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "minFormHeight", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormHeight, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormHeight", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormWidth, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "minFormWidth", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormWidth, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormWidth", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.backgroundModes, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "backgroundModes", proValue)); - - NAPI_CALL(env, napi_create_int32(env, abilityInfo.subType, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "subType", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, abilityInfo.isVisible, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "isVisible", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, abilityInfo.formEnabled, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "formEnabled", proValue)); - - napi_value jsArrayPermissions = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayPermissions)); - for (size_t i = 0; i < abilityInfo.permissions.size(); i++) { - proValue = nullptr; - NAPI_CALL( - env, napi_create_string_utf8(env, abilityInfo.permissions.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayPermissions, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "permissions", jsArrayPermissions)); - - napi_value jsArrayDeviceCapabilities = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceCapabilities)); - for (size_t i = 0; i < abilityInfo.deviceCapabilities.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8(env, abilityInfo.deviceCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayDeviceCapabilities, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "deviceCapabilities", jsArrayDeviceCapabilities)); - - napi_value jsArrayDeviceTypes = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceTypes)); - for (size_t i = 0; i < abilityInfo.deviceTypes.size(); i++) { - proValue = nullptr; - NAPI_CALL( - env, napi_create_string_utf8(env, abilityInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayDeviceTypes, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArrayDeviceTypes)); - - napi_value appInfo = nullptr; - appInfo = WrapAppInfo(env, abilityInfo.appInfo); - NAPI_CALL(env, napi_set_named_property(env, result, "applicationInfo", appInfo)); - - return result; -} - -napi_value NAPI_GetHapModuleInfo(napi_env env, napi_callback_info info) +napi_value NAPI_GetAbilityName(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s,called", __func__); - HapModuleInfoCB *hapModuleInfoCB = CreateHapModuleInfoCBInfo(env); - if (hapModuleInfoCB == nullptr) { - return nullptr; - } - - napi_value ret = GetHapModuleInfoWrap(env, info, hapModuleInfoCB); - if (ret == nullptr) { - if (hapModuleInfoCB != nullptr) { - delete hapModuleInfoCB; - hapModuleInfoCB = nullptr; - } - } - return ret; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetAbilityNameCommon(env, info, AbilityType::PAGE); } /** - * @brief Create asynchronous data. + * @brief Obtains the process Info this application. * * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. * - * @return Return a pointer to HapModuleInfoCB on success, nullptr on failure. + * @return The return value from NAPI C++ to JS for the module. */ -HapModuleInfoCB *CreateHapModuleInfoCBInfo(napi_env env) -{ - napi_value global = nullptr; - NAPI_CALL(env, napi_get_global(env, &global)); - - napi_value abilityObj = nullptr; - NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); - - Ability *ability = nullptr; - NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); - - HapModuleInfoCB *hapModuleInfoCB = new (std::nothrow) HapModuleInfoCB{ - .cbBase.cbInfo.env = env, - .cbBase.asyncWork = nullptr, - .cbBase.deferred = nullptr, - .cbBase.ability = ability, - }; - return hapModuleInfoCB; -} - -napi_value GetHapModuleInfoWrap(napi_env env, napi_callback_info info, HapModuleInfoCB *hapModuleInfoCB) +napi_value NAPI_GetProcessInfo(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, hapModuleInfoCB == nullptr.", __func__); - return nullptr; - } - - size_t argcAsync = 1; - const size_t argcPromise = 0; - const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value ret = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); - if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { - HILOG_ERROR("%{public}s, Wrong argument count.", __func__); - return nullptr; - } - - if (argcAsync > argcPromise) { - ret = GetHapModuleInfoAsync(env, args, argcAsync, argcPromise, hapModuleInfoCB); - } else { - ret = GetHapModuleInfoPromise(env, hapModuleInfoCB); - } - - return ret; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetProcessInfoCommon(env, info, AbilityType::PAGE); } /** - * @brief GetHapModuleInfo Async. + * @brief Obtains the name of the current process. * * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param hapModuleInfoCB Process data asynchronously. + * @param info The callback info passed into the callback function. * - * @return Return JS data successfully, otherwise return nullptr. + * @return The return value from NAPI C++ to JS for the module. */ -napi_value GetHapModuleInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, HapModuleInfoCB *hapModuleInfoCB) +napi_value NAPI_GetProcessName(napi_env env, napi_callback_info info) { - HILOG_INFO("%{public}s, asyncCallback.", __func__); - if (args == nullptr || hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - - napi_valuetype valuetype = napi_undefined; - NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); - if (valuetype == napi_function) { - NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &hapModuleInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetHapModuleInfoExecuteCB, - GetHapModuleInfoAsyncCompleteCB, - (void *)hapModuleInfoCB, - &hapModuleInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - napi_value result = nullptr; - NAPI_CALL(env, napi_get_null(env, &result)); - return result; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetProcessNameCommon(env, info, AbilityType::PAGE); } /** - * @brief GetHapModuleInfo Promise. + * @brief Obtains the bundle name of the ability that called the current ability. * * @param env The environment that the Node-API call is invoked under. - * @param hapModuleInfoCB Process data asynchronously. + * @param info The callback info passed into the callback function. * - * @return Return JS data successfully, otherwise return nullptr. + * @return The return value from NAPI C++ to JS for the module. */ -napi_value GetHapModuleInfoPromise(napi_env env, HapModuleInfoCB *hapModuleInfoCB) -{ - HILOG_INFO("%{public}s, promise.", __func__); - if (hapModuleInfoCB == nullptr) { - HILOG_ERROR("%{public}s, param == nullptr.", __func__); - return nullptr; - } - napi_value resourceName = nullptr; - NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); - napi_deferred deferred; - napi_value promise = nullptr; - NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); - hapModuleInfoCB->cbBase.deferred = deferred; - - NAPI_CALL(env, - napi_create_async_work(env, - nullptr, - resourceName, - GetHapModuleInfoExecuteCB, - GetHapModuleInfoPromiseCompleteCB, - (void *)hapModuleInfoCB, - &hapModuleInfoCB->cbBase.asyncWork)); - NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - - return promise; -} - -void GetHapModuleInfoExecuteCB(napi_env env, void *data) -{ - HILOG_INFO("NAPI_GetHapModuleInfo, worker pool thread execute."); - HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; - if (hapModuleInfoCB->cbBase.ability != nullptr) { - std::shared_ptr hapModuleInfoPtr = hapModuleInfoCB->cbBase.ability->GetHapModuleInfo(); - if (hapModuleInfoPtr != nullptr) { - SaveHapModuleInfo(hapModuleInfoCB->hapModuleInfo, *hapModuleInfoPtr); - } - } -} - -void SaveHapModuleInfo(HapModuleInfo_ &hapModuleInfo, const HapModuleInfo &hapModuleInfoOrg) -{ - - hapModuleInfo.name = hapModuleInfoOrg.name; - hapModuleInfo.description = hapModuleInfoOrg.description; - hapModuleInfo.icon = hapModuleInfoOrg.iconPath; - hapModuleInfo.label = hapModuleInfoOrg.label; - hapModuleInfo.backgroundImg = hapModuleInfoOrg.backgroundImg; - hapModuleInfo.moduleName = hapModuleInfoOrg.moduleName; - hapModuleInfo.supportedModes = hapModuleInfoOrg.supportedModes; - hapModuleInfo.descriptionId = 0; // no data - hapModuleInfo.labelId = 0; // no data - hapModuleInfo.iconId = 0; // no data - hapModuleInfo.mainAbilityName = ""; // no data - hapModuleInfo.installationFree = false; // no data - - for (size_t i = 0; i < hapModuleInfoOrg.reqCapabilities.size(); i++) { - hapModuleInfo.reqCapabilities.emplace_back(hapModuleInfoOrg.reqCapabilities.at(i)); - } - - for (size_t i = 0; i < hapModuleInfoOrg.deviceTypes.size(); i++) { - hapModuleInfo.deviceTypes.emplace_back(hapModuleInfoOrg.deviceTypes.at(i)); - } - - for (size_t i = 0; i < hapModuleInfoOrg.abilityInfos.size(); i++) { - AbilityInfo_ abilityInfo; - SaveAbilityInfo(abilityInfo, hapModuleInfoOrg.abilityInfos.at(i)); - hapModuleInfo.abilityInfos.emplace_back(abilityInfo); - } -} - -void GetHapModuleInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +napi_value NAPI_GetCallingBundle(napi_env env, napi_callback_info info) { - HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); - HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; - napi_value callback = nullptr; - napi_value undefined = nullptr; - napi_value result[ARGS_TWO] = {nullptr}; - napi_value callResult = nullptr; - NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); - result[PARAM0] = GetCallbackErrorValue(env, NO_ERROR); - result[PARAM1] = WrapHapModuleInfo(env, *hapModuleInfoCB); - NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, hapModuleInfoCB->cbBase.cbInfo.callback, &callback)); - NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); - - if (hapModuleInfoCB->cbBase.cbInfo.callback != nullptr) { - NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, hapModuleInfoCB->cbBase.cbInfo.callback)); - } - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - delete hapModuleInfoCB; - hapModuleInfoCB = nullptr; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetCallingBundleCommon(env, info, AbilityType::PAGE); } -void GetHapModuleInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +/** + * @brief Obtains information about the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAbilityInfo(napi_env env, napi_callback_info info) { - HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); - HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; - napi_value result = WrapHapModuleInfo(env, *hapModuleInfoCB); - NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, hapModuleInfoCB->cbBase.deferred, result)); - NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); - delete hapModuleInfoCB; - hapModuleInfoCB = nullptr; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetAbilityInfoCommon(env, info, AbilityType::PAGE); } -napi_value WrapHapModuleInfo(napi_env env, const HapModuleInfoCB &hapModuleInfoCB) +/** + * @brief Obtains the HapModuleInfo object of the application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetHapModuleInfo(napi_env env, napi_callback_info info) { - napi_value result = nullptr; - napi_value proValue = nullptr; - NAPI_CALL(env, napi_create_object(env, &result)); - NAPI_CALL( - env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); - - NAPI_CALL( - env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); - - NAPI_CALL( - env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.backgroundImg.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "backgroundImg", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); - - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.supportedModes, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "supportedModes", proValue)); - - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.descriptionId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.labelId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); - - NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.iconId, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); - - NAPI_CALL(env, - napi_create_string_utf8( - env, hapModuleInfoCB.hapModuleInfo.mainAbilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "mainAbilityName", proValue)); - - NAPI_CALL(env, napi_get_boolean(env, hapModuleInfoCB.hapModuleInfo.installationFree, &proValue)); - NAPI_CALL(env, napi_set_named_property(env, result, "installationFree", proValue)); - - napi_value jsArrayreqCapabilities = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArrayreqCapabilities)); - for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.reqCapabilities.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8( - env, hapModuleInfoCB.hapModuleInfo.reqCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArrayreqCapabilities, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "reqCapabilities", jsArrayreqCapabilities)); - - napi_value jsArraydeviceTypes = nullptr; - NAPI_CALL(env, napi_create_array(env, &jsArraydeviceTypes)); - for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.deviceTypes.size(); i++) { - proValue = nullptr; - NAPI_CALL(env, - napi_create_string_utf8( - env, hapModuleInfoCB.hapModuleInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); - NAPI_CALL(env, napi_set_element(env, jsArraydeviceTypes, i, proValue)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArraydeviceTypes)); - - napi_value abilityInfos = nullptr; - NAPI_CALL(env, napi_create_array(env, &abilityInfos)); - for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.abilityInfos.size(); i++) { - napi_value abilityInfo = nullptr; - abilityInfo = WrapAbilityInfo(env, hapModuleInfoCB.hapModuleInfo.abilityInfos.at(i)); - NAPI_CALL(env, napi_set_element(env, abilityInfos, i, abilityInfo)); - } - NAPI_CALL(env, napi_set_named_property(env, result, "abilityInfos", abilityInfos)); - - return result; + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetHapModuleInfoCommon(env, info, AbilityType::PAGE); } /** @@ -4047,5 +1232,32 @@ void GetDataAbilityHelperPromiseCompleteCB(napi_env env, napi_status status, voi dataAbilityHelperCB = nullptr; } +/** + * @brief FeatureAbility NAPI method : connectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_FAConnectAbility(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_ConnectAbilityCommon(env, info, AbilityType::PAGE); +} + +/** + * @brief FeatureAbility NAPI method : disConnectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_FADisConnectAbility(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_DisConnectAbilityCommon(env, info, AbilityType::PAGE); +} } // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h old mode 100644 new mode 100755 index 2061040bc01..24630718fc6 --- a/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h +++ b/interfaces/kits/napi/aafwk/featureAbility/feature_ability.h @@ -15,7 +15,7 @@ #ifndef OHOS_APPEXECFWK_FEATURE_ABILITY_H #define OHOS_APPEXECFWK_FEATURE_ABILITY_H -#include "feature_ability_common.h" +#include "../inner/napi_common/feature_ability_common.h" using Want = OHOS::AAFwk::Want; namespace OHOS { @@ -224,15 +224,6 @@ napi_value UnwrapParam(CallAbilityParam ¶m, napi_env env, napi_value args); */ napi_value UnwrapAbilityResult(CallAbilityParam ¶m, napi_env env, napi_value args); -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AsyncCallbackInfo on success, nullptr on failure. - */ -AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env); - /** * @brief Create asynchronous data. * @@ -242,51 +233,6 @@ AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env); */ AppInfoCB *CreateAppInfoCBInfo(napi_env env); -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AbilityNameCB on success, nullptr on failure. - */ -AbilityNameCB *CreateAbilityNameCBInfo(napi_env env); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ProcessInfoCB on success, nullptr on failure. - */ -ProcessInfoCB *CreateProcessInfoCBInfo(napi_env env); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ProcessNameCB on success, nullptr on failure. - */ -ProcessNameCB *CreateProcessNameCBInfo(napi_env env); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to CallingBundleCB on success, nullptr on failure. - */ -CallingBundleCB *CreateCallingBundleCBInfo(napi_env env); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AbilityInfoCB on success, nullptr on failure. - */ -AbilityInfoCB *CreateAbilityInfoCBInfo(napi_env env); - /** * @brief HasWindowFocus processing function. * @@ -300,24 +246,6 @@ napi_value HasWindowFocusAsync( napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); napi_value HasWindowFocusPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); -napi_value GetContextWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); -napi_value GetContextAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); -napi_value GetContextPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); - -/** - * @brief GetWantWrap processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetWantWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); -napi_value GetWantAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); -napi_value GetWantPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); - /** * @brief TerminateAbility processing function. * @@ -344,19 +272,6 @@ napi_value SetResultAsync( napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); napi_value SetResultPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); -/** - * @brief StartAbility processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value StartAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo); -napi_value StartAbilityAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); -napi_value StartAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); - /** * @brief StartAbilityForResult processing function. * @@ -370,496 +285,6 @@ napi_value StartAbilityForResultAsync( napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo); napi_value StartAbilityForResultPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo); -/** - * @brief GetApplicationInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param appInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetApplicationInfoWrap(napi_env env, napi_callback_info info, AppInfoCB *appInfoCB); - -/** - * @brief GetApplicationInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param appInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetApplicationInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppInfoCB *appInfoCB); - -/** - * @brief GetApplicationInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param appInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetApplicationInfoPromise(napi_env env, AppInfoCB *appInfoCB); - -/** - * @brief GetApplicationInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppInfoPromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo); -void SaveAppInfo(AppInfo_ &appInfo, const ApplicationInfo &appInfoOrg); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to AppTypeCB on success, nullptr on failure. - */ -AppTypeCB *CreateAppTypeCBInfo(napi_env env); - -/** - * @brief GetAppType processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypeWrap(napi_env env, napi_callback_info info, AppTypeCB *appTypeCB); - -/** - * @brief GetAppType Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypeAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppTypeCB *appTypeCB); - -/** - * @brief GetAppType Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param appTypeCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAppTypePromise(napi_env env, AppTypeCB *appTypeCB); - -/** - * @brief GetAppType asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypeExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypeAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAppTypePromiseCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to ElementNameCB on success, nullptr on failure. - */ -ElementNameCB *CreateElementNameCBInfo(napi_env env); - -/** - * @brief GetElementName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNameWrap(napi_env env, napi_callback_info info, ElementNameCB *elementNameCB); - -/** - * @brief GetElementName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ElementNameCB *elementNameCB); - -/** - * @brief GetElementName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param elementNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetElementNamePromise(napi_env env, ElementNameCB *elementNameCB); - -/** - * @brief GetElementName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNameExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNameAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetElementNamePromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapElementName(napi_env env, ElementNameCB *elementNameCB); - -/** - * @brief GetAbilityName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityNameWrap(napi_env env, napi_callback_info info, AbilityNameCB *abilityNameCB); - -/** - * @brief GetAbilityName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param abilityNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityNameCB *abilityNameCB); - -/** - * @brief GetAbilityName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityNamePromise(napi_env env, AbilityNameCB *abilityNameCB); - -/** - * @brief GetAbilityName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityNameExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityNameAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityNamePromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapAbilityName(napi_env env, AbilityNameCB *abilityNameCB); - -/** - * @brief GetProcessInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoWrap(napi_env env, napi_callback_info info, ProcessInfoCB *ProcessInfoCB); - -/** - * @brief GetProcessInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessInfoCB *ProcessInfoCB); - -/** - * @brief GetProcessInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessInfoPromise(napi_env env, ProcessInfoCB *ProcessInfoCB); - -/** - * @brief GetProcessInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessInfoPromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapProcessInfo(napi_env env, ProcessInfoCB *ProcessInfoCB); - -/** - * @brief GetProcessName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessNameWrap(napi_env env, napi_callback_info info, ProcessNameCB *ProcessNameCB); - -/** - * @brief GetProcessName Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param ProcessNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessNameAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessNameCB *ProcessNameCB); - -/** - * @brief GetProcessName Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param ProcessNameCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetProcessNamePromise(napi_env env, ProcessNameCB *ProcessNameCB); - -/** - * @brief GetProcessName asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessNameExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessNameAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetProcessNamePromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapProcessName(napi_env env, ProcessNameCB *ProcessNameCB); - -/** - * @brief GetCallingBundle processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundleWrap(napi_env env, napi_callback_info info, CallingBundleCB *CallingBundleCB); - -/** - * @brief GetCallingBundle Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundleAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, CallingBundleCB *CallingBundleCB); - -/** - * @brief GetCallingBundle Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param CallingBundleCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetCallingBundlePromise(napi_env env, CallingBundleCB *CallingBundleCB); - -/** - * @brief GetCallingBundle asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundleExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundleAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetCallingBundlePromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapCallingBundle(napi_env env, CallingBundleCB *CallingBundleCB); - -/** - * @brief GetAbilityInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoWrap(napi_env env, napi_callback_info info, AbilityInfoCB *abilityInfoCB); - -/** - * @brief GetAbilityInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityInfoCB *abilityInfoCB); - -/** - * @brief GetAbilityInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param abilityInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetAbilityInfoPromise(napi_env env, AbilityInfoCB *abilityInfoCB); - -/** - * @brief GetAbilityInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityInfoExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityInfoAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetAbilityInfoPromiseCompleteCB(napi_env env, napi_status status, void *data); - -napi_value WrapAbilityInfo(napi_env env, const AbilityInfo_ &abilityInfo); -void SaveAbilityInfo(AbilityInfo_ &abilityInfo, const AbilityInfo &abilityInfoOrg); - /** * @brief Obtains the HapModuleInfo object of the application. * @@ -870,74 +295,6 @@ void SaveAbilityInfo(AbilityInfo_ &abilityInfo, const AbilityInfo &abilityInfoOr */ napi_value NAPI_GetHapModuleInfo(napi_env env, napi_callback_info info); -/** - * @brief Create asynchronous data. - * - * @param env The environment that the Node-API call is invoked under. - * - * @return Return a pointer to HapModuleInfoCB on success, nullptr on failure. - */ -HapModuleInfoCB *CreateHapModuleInfoCBInfo(napi_env env); - -/** - * @brief GetHapModuleInfo processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param hapModuleInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetHapModuleInfoWrap(napi_env env, napi_callback_info info, HapModuleInfoCB *hapModuleInfoCB); - -/** - * @brief GetHapModuleInfo Async. - * - * @param env The environment that the Node-API call is invoked under. - * @param args Indicates the arguments passed into the callback. - * @param argcPromise Asynchronous data processing. - * @param hapModuleInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetHapModuleInfoAsync( - napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, HapModuleInfoCB *hapModuleInfoCB); - -/** - * @brief GetHapModuleInfo Promise. - * - * @param env The environment that the Node-API call is invoked under. - * @param hapModuleInfoCB Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value GetHapModuleInfoPromise(napi_env env, HapModuleInfoCB *hapModuleInfoCB); - -/** - * @brief GetHapModuleInfo asynchronous processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetHapModuleInfoExecuteCB(napi_env env, void *data); - -/** - * @brief The callback at the end of the asynchronous callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetHapModuleInfoAsyncCompleteCB(napi_env env, napi_status status, void *data); - -/** - * @brief The callback at the end of the Promise callback. - * - * @param env The environment that the Node-API call is invoked under. - * @param data Point to asynchronous processing of data. - */ -void GetHapModuleInfoPromiseCompleteCB(napi_env env, napi_status status, void *data); -napi_value WrapHapModuleInfo(napi_env env, const HapModuleInfoCB &hapModuleInfoCB); -void SaveHapModuleInfo(HapModuleInfo_ &hapModuleInfo, const HapModuleInfo &hapModuleInfoOrg); - /** * @brief getDataAbilityHelper processing function. * @@ -986,6 +343,26 @@ void GetDataAbilityHelperAsyncCompleteCB(napi_env env, napi_status status, void * @param data Point to asynchronous processing of data. */ void GetDataAbilityHelperPromiseCompleteCB(napi_env env, napi_status status, void *data); + +/** + * @brief FeatureAbility NAPI method : connectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_FAConnectAbility(napi_env env, napi_callback_info info); + +/** + * @brief FeatureAbility NAPI method : disconnectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_FADisConnectAbility(napi_env env, napi_callback_info info); } // namespace AppExecFwk } // namespace OHOS #endif /* OHOS_APPEXECFWK_FEATURE_ABILITY_H */ diff --git a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp index 07096633dbe..d778f74a498 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp +++ b/interfaces/kits/napi/aafwk/featureAbility/napi_context.cpp @@ -19,13 +19,13 @@ #include "hilog_wrapper.h" #include "ability_process.h" #include "feature_ability_common.h" +#include "../inner/napi_common/napi_common_ability.h" using namespace OHOS::AAFwk; using namespace OHOS::AppExecFwk; namespace OHOS { namespace AppExecFwk { -napi_value g_classContext; CallbackInfo aceCallbackInfoPermission; @@ -82,8 +82,9 @@ void VerifySelfPermissionExecuteCallbackWork(napi_env env, void *data) return; } - std::string permission(asyncCallbackInfo->param.paramArgs.GetStringValue("permission")); - asyncCallbackInfo->native_data.int32_value = asyncCallbackInfo->ability->VerifySelfPermission(permission); + asyncCallbackInfo->native_data.data_type = NVT_INT32; + asyncCallbackInfo->native_data.int32_value = asyncCallbackInfo->ability->VerifySelfPermission( + asyncCallbackInfo->param.paramArgs.GetStringValue("permission")); } /** @@ -149,8 +150,7 @@ napi_value NAPI_VerifySelfPermission(napi_env env, napi_callback_info info) napi_value rev = NAPI_VerifySelfPermissionWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -312,8 +312,7 @@ napi_value NAPI_RequestPermissionsFromUser(napi_env env, napi_callback_info info napi_value rev = NAPI_RequestPermissionsFromUserWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -396,8 +395,7 @@ napi_value NAPI_OnRequestPermissionsFromUserResult(napi_env env, napi_callback_i napi_value rev = NAPI_OnRequestPermissionsFromUserResultWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -489,81 +487,6 @@ void CallOnRequestPermissionsFromUserResult(int requestCode, const std::vectorerror_code = NAPI_ERR_NO_ERROR; - asyncCallbackInfo->native_data.data_type = NVT_NONE; - if (asyncCallbackInfo->ability == nullptr) { - asyncCallbackInfo->error_code = NAPI_ERR_ACE_ABILITY; - return; - } - - asyncCallbackInfo->native_data.data_type = NVT_STRING; - asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetBundleName(); - HILOG_INFO("%{public}s called. bundleName=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); -} - -/** - * @brief GetBundleName processing function. - * - * @param env The environment that the Node-API call is invoked under. - * @param asyncCallbackInfo Process data asynchronously. - * - * @return Return JS data successfully, otherwise return nullptr. - */ -napi_value NAPI_GetBundleNameWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) -{ - HILOG_INFO("%{public}s called", __func__); - size_t argc = ARGS_MAX_COUNT; - napi_value args[ARGS_MAX_COUNT] = {nullptr}; - napi_value jsthis = 0; - void *data = nullptr; - - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); - - if (argc > ARGS_ONE) { - HILOG_INFO("%{public}s called, parameters is invalid.", __func__); - return nullptr; - } - - if (argc == ARGS_ONE) { - if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { - HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__); - return nullptr; - } - } - - AsyncParamEx asyncParamEx; - if (asyncCallbackInfo->cbInfo.callback != nullptr) { - HILOG_INFO("%{public}s called. asyncCallback.", __func__); - asyncParamEx.resource = "NAPI_GetBundleNameCallback"; - asyncParamEx.execute = GetBundleNameExecuteCallback; - asyncParamEx.complete = CompleteAsyncCallbackWork; - - return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); - } else { - HILOG_INFO("%{public}s called. promise.", __func__); - asyncParamEx.resource = "NAPI_GetBundleNamePromise"; - asyncParamEx.execute = GetBundleNameExecuteCallback; - asyncParamEx.complete = CompletePromiseCallbackWork; - - return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); - } -} - /** * @brief Get bundle name. * @@ -575,20 +498,7 @@ napi_value NAPI_GetBundleNameWrap(napi_env env, napi_callback_info info, AsyncJS napi_value NAPI_GetBundleName(napi_env env, napi_callback_info info) { HILOG_INFO("%{public}s called", __func__); - - AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); - if (asyncCallbackInfo == nullptr) { - HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); - return WrapVoidToJS(env); - } - - napi_value rev = NAPI_GetBundleNameWrap(env, info, asyncCallbackInfo); - if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; - rev = WrapVoidToJS(env); - } - return rev; + return NAPI_GetBundleNameCommon(env, info, AbilityType::PAGE); } void CanRequestPermissionExecuteCallback(napi_env env, void *data) @@ -659,8 +569,7 @@ napi_value NAPI_CanRequestPermission(napi_env env, napi_callback_info info) napi_value rev = NAPI_CanRequestPermissionWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -772,8 +681,7 @@ napi_value NAPI_VerifyPermission(napi_env env, napi_callback_info info) napi_value rev = NAPI_VerifyPermissionWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -848,8 +756,7 @@ napi_value NAPI_VerifyCallingPermission(napi_env env, napi_callback_info info) napi_value rev = NAPI_VerifyCallingPermissionWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -924,8 +831,7 @@ napi_value NAPI_VerifyCallingOrSelfPermission(napi_env env, napi_callback_info i napi_value rev = NAPI_VerifyCallingOrSelfPermissionWrap(env, info, asyncCallbackInfo); if (rev == nullptr) { - delete asyncCallbackInfo; - asyncCallbackInfo = nullptr; + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); rev = WrapVoidToJS(env); } return rev; @@ -962,7 +868,7 @@ napi_value ContextPermissionInit(napi_env env, napi_value exports) nullptr, sizeof(properties) / sizeof(*properties), properties, - &g_classContext)); + GetGlobalClassContext())); return exports; } diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn b/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn index 018cbb2fbdd..17d6faa6a52 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn +++ b/interfaces/kits/napi/aafwk/inner/napi_common/BUILD.gn @@ -22,6 +22,7 @@ ohos_shared_library("napi_common") { ] sources = [ + "napi_common_ability.cpp", "napi_common_util.cpp", "napi_common_want.cpp", ] @@ -45,6 +46,7 @@ ohos_shared_library("napi_common") { "ipc:ipc_core", ] + relative_install_dir = "module" subsystem_name = "aafwk" part_name = "aafwk_standard" } diff --git a/interfaces/kits/napi/aafwk/featureAbility/feature_ability_common.h b/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h similarity index 89% rename from interfaces/kits/napi/aafwk/featureAbility/feature_ability_common.h rename to interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h index 51d3eeab10c..9749f19ecde 100755 --- a/interfaces/kits/napi/aafwk/featureAbility/feature_ability_common.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/feature_ability_common.h @@ -19,7 +19,7 @@ #include "napi/native_node_api.h" #include "ability.h" #include "want.h" -#include "../inner/napi_common/napi_common.h" +#include "napi_common.h" #include "dummy_values_bucket.h" #include "dummy_data_ability_predicates.h" @@ -40,7 +40,6 @@ struct CallAbilityParam { std::shared_ptr setting = nullptr; }; - struct OnAbilityCallback { int requestCode = 0; int resultCode = 0; @@ -55,6 +54,8 @@ struct AsyncCallbackInfo { CallAbilityParam param; CallbackInfo *aceCallback; bool native_result; + AbilityType abilityType = AbilityType::UNKNOWN; + int errCode = 0; }; struct CBBase { @@ -62,6 +63,8 @@ struct CBBase { napi_async_work asyncWork; napi_deferred deferred; Ability *ability = nullptr; + AbilityType abilityType = AbilityType::UNKNOWN; + int errCode = 0; }; struct AppInfo_ { @@ -197,10 +200,26 @@ struct DAHelperInsertCB { int result = 0; }; +class NAPIAbilityConnection; +struct AbilityConnectionCB { + int resultCode = 0; + ElementName elementName; + sptr connection; + CallbackInfo cbInfo; +}; +struct ConnectAbilityCB { + CBBase cbBase; + Want want; + sptr abilityConnection; + AbilityConnectionCB abilityConnectionCB; + bool result; + int errCode = 0; +}; + static inline std::string NapiValueToStringUtf8(napi_env env, napi_value value) { std::string result = ""; - return UnwrapStringFromJS(env, value, result); + return UnwrapStringFromJS(env, value, result); } } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp new file mode 100755 index 00000000000..a908f749ecf --- /dev/null +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.cpp @@ -0,0 +1,4260 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "napi_common_ability.h" +#include +#include "napi_common_util.h" +#include "securec.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { + +napi_value g_classContext; + +napi_value *GetGlobalClassContext(void) +{ + return &g_classContext; +} + +bool CheckAbilityType(AbilityType typeInAbility, AbilityType typeWant) +{ + switch (typeWant) { + case AbilityType::PAGE: + return typeInAbility == AbilityType::PAGE; + default: + return typeInAbility != AbilityType::PAGE; + } + return false; +} + +bool CheckAbilityType(const CBBase *cbBase) +{ + if (cbBase == nullptr) { + return false; + } + + const std::shared_ptr info = cbBase->ability->GetAbilityInfo(); + if (info == nullptr) { + return false; + } + + return CheckAbilityType((AbilityType)info->type, cbBase->abilityType); +} + +bool CheckAbilityType(const AsyncJSCallbackInfo *asyncCallbackInfo) +{ + if (asyncCallbackInfo == nullptr) { + return false; + } + + const std::shared_ptr info = asyncCallbackInfo->ability->GetAbilityInfo(); + if (info == nullptr) { + return false; + } + + return CheckAbilityType((AbilityType)info->type, asyncCallbackInfo->abilityType); +} + +bool CheckAbilityType(const AsyncCallbackInfo *asyncCallbackInfo) +{ + if (asyncCallbackInfo == nullptr) { + return false; + } + + const std::shared_ptr info = asyncCallbackInfo->ability->GetAbilityInfo(); + if (info == nullptr) { + return false; + } + + return CheckAbilityType((AbilityType)info->type, asyncCallbackInfo->abilityType); +} + +void SaveAppInfo(AppInfo_ &appInfo, const ApplicationInfo &appInfoOrg) +{ + HILOG_INFO("%{public}s.", __func__); + appInfo.name = appInfoOrg.name; + appInfo.description = appInfoOrg.description; + appInfo.descriptionId = appInfoOrg.descriptionId; + appInfo.systemApp = appInfoOrg.isSystemApp; + appInfo.enabled = appInfoOrg.enabled; + appInfo.label = appInfoOrg.label; + appInfo.labelId = std::to_string(appInfoOrg.labelId); + appInfo.icon = appInfoOrg.iconPath; + appInfo.iconId = std::to_string(appInfoOrg.iconId); + appInfo.process = appInfoOrg.process; + appInfo.supportedModes = appInfoOrg.supportedModes; + for (size_t i = 0; i < appInfoOrg.moduleInfos.size(); i++) { + appInfo.moduleSourceDirs.emplace_back(appInfoOrg.moduleInfos.at(i).moduleSourceDir); + } + for (size_t i = 0; i < appInfoOrg.permissions.size(); i++) { + appInfo.permissions.emplace_back(appInfoOrg.permissions.at(i)); + } + for (size_t i = 0; i < appInfoOrg.moduleInfos.size(); i++) { + appInfo.moduleInfos.emplace_back(appInfoOrg.moduleInfos.at(i)); + } + appInfo.flags = appInfoOrg.flags; + appInfo.entryDir = appInfoOrg.entryDir; +} + +/** + * @brief GetApplicationInfo asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + AppInfoCB *appInfoCB = (AppInfoCB *)data; + appInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + + if (appInfoCB->cbBase.ability == nullptr) { + appInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&appInfoCB->cbBase)) { + appInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr appInfoPtr = appInfoCB->cbBase.ability->GetApplicationInfo(); + if (appInfoPtr != nullptr) { + SaveAppInfo(appInfoCB->appInfo, *appInfoPtr); + } else { + appInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } +} + +napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo) +{ + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); + + NAPI_CALL(env, napi_create_int32(env, appInfo.descriptionId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); + + NAPI_CALL(env, napi_get_boolean(env, appInfo.systemApp, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "systemApp", proValue)); + NAPI_CALL(env, napi_get_boolean(env, appInfo.enabled, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "enabled", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.labelId.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.iconId.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.process.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "process", proValue)); + NAPI_CALL(env, napi_create_int32(env, appInfo.supportedModes, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "supportedModes", proValue)); + napi_value jsArrayModDirs = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayModDirs)); + for (size_t i = 0; i < appInfo.moduleSourceDirs.size(); i++) { + proValue = nullptr; + NAPI_CALL( + env, napi_create_string_utf8(env, appInfo.moduleSourceDirs.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayModDirs, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "moduleSourceDirs", jsArrayModDirs)); + napi_value jsArrayPermissions = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayPermissions)); + for (size_t i = 0; i < appInfo.permissions.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.permissions.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayPermissions, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "permissions", jsArrayPermissions)); + napi_value jsArrayModuleInfo = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayModuleInfo)); + for (size_t i = 0; i < appInfo.moduleInfos.size(); i++) { + napi_value jsModuleInfoObject = nullptr; + NAPI_CALL(env, napi_create_object(env, &jsModuleInfoObject)); + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8(env, appInfo.moduleInfos.at(i).moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, jsModuleInfoObject, "moduleName", proValue)); + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8( + env, appInfo.moduleInfos.at(i).moduleSourceDir.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, jsModuleInfoObject, "moduleSourceDir", proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayModuleInfo, i, jsModuleInfoObject)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "moduleInfos", jsArrayModuleInfo)); + NAPI_CALL(env, napi_create_int32(env, appInfo.flags, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "flags", proValue)); + NAPI_CALL(env, napi_create_string_utf8(env, appInfo.entryDir.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "entryDir", proValue)); + return result; +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AppInfoCB *appInfoCB = (AppInfoCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, appInfoCB->cbBase.errCode); + if (appInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapAppInfo(env, appInfoCB->appInfo); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (appInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appInfoCB->cbBase.asyncWork)); + delete appInfoCB; + appInfoCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AppInfoCB *appInfoCB = (AppInfoCB *)data; + if (appInfoCB == nullptr) { + return; + } + + napi_value result = nullptr; + if (appInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapAppInfo(env, appInfoCB->appInfo); + napi_resolve_deferred(env, appInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, appInfoCB->cbBase.errCode); + napi_reject_deferred(env, appInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, appInfoCB->cbBase.asyncWork); + delete appInfoCB; + appInfoCB = nullptr; +} + +/** + * @brief GetApplicationInfo Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param appInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetApplicationInfoAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppInfoCB *appInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &appInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAppInfoExecuteCB, + GetAppInfoAsyncCompleteCB, + (void *)appInfoCB, + &appInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetApplicationInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param appInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetApplicationInfoPromise(napi_env env, AppInfoCB *appInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + appInfoCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAppInfoExecuteCB, + GetAppInfoPromiseCompleteCB, + (void *)appInfoCB, + &appInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appInfoCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetApplicationInfo processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param appInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetApplicationInfoWrap(napi_env env, napi_callback_info info, AppInfoCB *appInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (appInfoCB == nullptr) { + HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetApplicationInfoAsync(env, args, argcAsync, argcPromise, appInfoCB); + } else { + ret = GetApplicationInfoPromise(env, appInfoCB); + } + + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AppInfoCB on success, nullptr on failure. + */ +AppInfoCB *CreateAppInfoCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AppInfoCB *appInfoCB = new (std::nothrow) AppInfoCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + .cbBase.abilityType = AbilityType::UNKNOWN, + .cbBase.errCode = NAPI_ERR_NO_ERROR, + }; + return appInfoCB; +} + +/** + * @brief Obtains information about the current application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetApplicationInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s,called", __func__); + AppInfoCB *appInfoCB = CreateAppInfoCBInfo(env); + if (appInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + appInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetApplicationInfoWrap(env, info, appInfoCB); + if (ret == nullptr) { + if (appInfoCB != nullptr) { + delete appInfoCB; + appInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief GetBundleName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetBundleNameExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called", __func__); + AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called. asyncCallbackInfo is null", __func__); + return; + } + + asyncCallbackInfo->error_code = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->native_data.data_type = NVT_NONE; + if (asyncCallbackInfo->ability == nullptr) { + asyncCallbackInfo->error_code = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(asyncCallbackInfo)) { + asyncCallbackInfo->error_code = NAPI_ERR_ABILITY_TYPE_INVALID; + asyncCallbackInfo->native_data.data_type = NVT_UNDEFINED; + return; + } + + asyncCallbackInfo->native_data.data_type = NVT_STRING; + asyncCallbackInfo->native_data.str_value = asyncCallbackInfo->ability->GetBundleName(); + HILOG_INFO("%{public}s called. bundleName=%{public}s", __func__, asyncCallbackInfo->native_data.str_value.c_str()); +} + +/** + * @brief GetBundleName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value NAPI_GetBundleNameWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called", __func__); + size_t argc = ARGS_MAX_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value jsthis = 0; + void *data = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); + + if (argc > ARGS_ONE) { + HILOG_INFO("%{public}s called, parameters is invalid.", __func__); + return nullptr; + } + + if (argc == ARGS_ONE) { + if (!CreateAsyncCallback(env, args[PARAM0], asyncCallbackInfo)) { + HILOG_INFO("%{public}s called, the first parameter is invalid.", __func__); + return nullptr; + } + } + + AsyncParamEx asyncParamEx; + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + HILOG_INFO("%{public}s called. asyncCallback.", __func__); + asyncParamEx.resource = "NAPI_GetBundleNameCallback"; + asyncParamEx.execute = GetBundleNameExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_GetBundleNamePromise"; + asyncParamEx.execute = GetBundleNameExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +/** + * @brief Get bundle name. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetBundleNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called", __func__); + + AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + return WrapVoidToJS(env); + } + + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = NAPI_GetBundleNameWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); + ret = WrapVoidToJS(env); + } + return ret; +} + +napi_value WrapProcessInfo(napi_env env, ProcessInfoCB *processInfoCB) +{ + if (processInfoCB == nullptr) { + HILOG_ERROR("Invalid param(processInfoCB == nullptr)"); + return nullptr; + } + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_int32(env, processInfoCB->pid, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "pid", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, processInfoCB->processName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "processName", proValue)); + + return result; +} + +/** + * @brief GetProcessInfo asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetProcessInfo, worker pool thread execute."); + ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; + if (processInfoCB == nullptr) { + return; + } + + processInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (processInfoCB->cbBase.ability == nullptr) { + processInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&processInfoCB->cbBase)) { + processInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr processInfoPtr = processInfoCB->cbBase.ability->GetProcessInfo(); + if (processInfoPtr != nullptr) { + processInfoCB->processName = processInfoPtr->GetProcessName(); + processInfoCB->pid = processInfoPtr->GetPid(); + } else { + processInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); + ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, processInfoCB->cbBase.errCode); + if (processInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapProcessInfo(env, processInfoCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (processInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processInfoCB->cbBase.asyncWork)); + delete processInfoCB; + processInfoCB = nullptr; +} + +/** + * @brief GetProcessInfo Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param ProcessInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessInfoAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &processInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessInfoExecuteCB, + GetProcessInfoAsyncCompleteCB, + (void *)processInfoCB, + &processInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessInfo, main event thread complete."); + ProcessInfoCB *processInfoCB = (ProcessInfoCB *)data; + napi_value result = nullptr; + if (processInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapProcessInfo(env, processInfoCB); + napi_resolve_deferred(env, processInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, processInfoCB->cbBase.errCode); + napi_reject_deferred(env, processInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, processInfoCB->cbBase.asyncWork); + delete processInfoCB; + processInfoCB = nullptr; +} + +/** + * @brief GetProcessInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessInfoPromise(napi_env env, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + processInfoCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessInfoExecuteCB, + GetProcessInfoPromiseCompleteCB, + (void *)processInfoCB, + &processInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processInfoCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetProcessInfo processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessInfoWrap(napi_env env, napi_callback_info info, ProcessInfoCB *processInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (processInfoCB == nullptr) { + HILOG_ERROR("%{public}s, processInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetProcessInfoAsync(env, args, argcAsync, argcPromise, processInfoCB); + } else { + ret = GetProcessInfoPromise(env, processInfoCB); + } + + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to ProcessInfoCB on success, nullptr on failure. + */ +ProcessInfoCB *CreateProcessInfoCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ProcessInfoCB *processInfoCB = new (std::nothrow) ProcessInfoCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return processInfoCB; +} + +/** + * @brief Obtains the process Info this application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetProcessInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + ProcessInfoCB *processInfoCB = CreateProcessInfoCBInfo(env); + if (processInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + processInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + processInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetProcessInfoWrap(env, info, processInfoCB); + if (ret == nullptr) { + if (processInfoCB != nullptr) { + delete processInfoCB; + processInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AppTypeCB on success, nullptr on failure. + */ +AppTypeCB *CreateAppTypeCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AppTypeCB *appTypeCB = new (std::nothrow) AppTypeCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return appTypeCB; +} + +/** + * @brief GetAppType asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppTypeExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + AppTypeCB *appTypeCB = (AppTypeCB *)data; + if (appTypeCB == nullptr) { + return; + } + + appTypeCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (appTypeCB->cbBase.ability == nullptr) { + appTypeCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&appTypeCB->cbBase)) { + appTypeCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + appTypeCB->name = appTypeCB->cbBase.ability->GetAppType(); +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppTypeAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AppTypeCB *appTypeCB = (AppTypeCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + + result[PARAM0] = GetCallbackErrorValue(env, appTypeCB->cbBase.errCode); + if (appTypeCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, + napi_create_string_utf8( + env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result[PARAM1])); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, appTypeCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (appTypeCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, appTypeCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, appTypeCB->cbBase.asyncWork)); + delete appTypeCB; + appTypeCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAppTypePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("GetAppTypePromiseCompleteCB, main event thread complete."); + AppTypeCB *appTypeCB = (AppTypeCB *)data; + napi_value result = nullptr; + if (appTypeCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + napi_create_string_utf8(env, appTypeCB->cbBase.ability->GetAppType().c_str(), NAPI_AUTO_LENGTH, &result); + napi_resolve_deferred(env, appTypeCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, appTypeCB->cbBase.errCode); + napi_reject_deferred(env, appTypeCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, appTypeCB->cbBase.asyncWork); + delete appTypeCB; + appTypeCB = nullptr; +} + +/** + * @brief GetAppType Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param appTypeCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAppTypeAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AppTypeCB *appTypeCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || appTypeCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &appTypeCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAppTypeExecuteCB, + GetAppTypeAsyncCompleteCB, + (void *)appTypeCB, + &appTypeCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetAppType Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param appTypeCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAppTypePromise(napi_env env, AppTypeCB *appTypeCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (appTypeCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + appTypeCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAppTypeExecuteCB, + GetAppTypePromiseCompleteCB, + (void *)appTypeCB, + &appTypeCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, appTypeCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetAppType processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param appTypeCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAppTypeWrap(napi_env env, napi_callback_info info, AppTypeCB *appTypeCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (appTypeCB == nullptr) { + HILOG_ERROR("%{public}s, appTypeCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetAppTypeAsync(env, args, argcAsync, argcPromise, appTypeCB); + } else { + ret = GetAppTypePromise(env, appTypeCB); + } + + return ret; +} + +/** + * @brief Obtains the type of this application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AppTypeCB *appTypeCB = CreateAppTypeCBInfo(env); + if (appTypeCB == nullptr) { + return WrapVoidToJS(env); + } + + appTypeCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + appTypeCB->cbBase.abilityType = abilityType; + napi_value ret = GetAppTypeWrap(env, info, appTypeCB); + if (ret == nullptr) { + if (appTypeCB != nullptr) { + delete appTypeCB; + appTypeCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to ElementNameCB on success, nullptr on failure. + */ +ElementNameCB *CreateElementNameCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ElementNameCB *elementNameCB = new (std::nothrow) ElementNameCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return elementNameCB; +} + +napi_value WrapElementName(napi_env env, ElementNameCB *elementNameCB) +{ + if (elementNameCB == nullptr) { + HILOG_ERROR("Invalid param(appInfoCB = nullptr)"); + return nullptr; + } + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->abilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "abilityName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->deviceId.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "deviceId", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->shortName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "shortName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, elementNameCB->uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); + + return result; +} + +/** + * @brief GetElementName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetElementNameExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + ElementNameCB *elementNameCB = (ElementNameCB *)data; + if (elementNameCB == nullptr) { + return; + } + + elementNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (elementNameCB->cbBase.ability == nullptr) { + elementNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&elementNameCB->cbBase)) { + elementNameCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr elementName = elementNameCB->cbBase.ability->GetElementName(); + if (elementNameCB != nullptr) { + elementNameCB->deviceId = elementName->GetDeviceID(); + elementNameCB->bundleName = elementName->GetBundleName(); + elementNameCB->abilityName = elementName->GetAbilityName(); + elementNameCB->uri = elementNameCB->cbBase.ability->GetWant()->GetUriString(); + elementNameCB->shortName = ""; + } else { + elementNameCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetElementNameAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + ElementNameCB *elementNameCB = (ElementNameCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, elementNameCB->cbBase.errCode); + if (elementNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapElementName(env, elementNameCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, elementNameCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (elementNameCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, elementNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, elementNameCB->cbBase.asyncWork)); + delete elementNameCB; + elementNameCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetElementNamePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + ElementNameCB *elementNameCB = (ElementNameCB *)data; + napi_value result = nullptr; + if (elementNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapElementName(env, elementNameCB); + napi_resolve_deferred(env, elementNameCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, elementNameCB->cbBase.errCode); + napi_reject_deferred(env, elementNameCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, elementNameCB->cbBase.asyncWork); + delete elementNameCB; + elementNameCB = nullptr; +} + +/** + * @brief GetElementName Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param elementNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetElementNamePromise(napi_env env, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + elementNameCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetElementNameExecuteCB, + GetElementNamePromiseCompleteCB, + (void *)elementNameCB, + &elementNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetElementName Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param elementNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetElementNameAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &elementNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetElementNameExecuteCB, + GetElementNameAsyncCompleteCB, + (void *)elementNameCB, + &elementNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, elementNameCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetElementName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param elementNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetElementNameWrap(napi_env env, napi_callback_info info, ElementNameCB *elementNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (elementNameCB == nullptr) { + HILOG_ERROR("%{public}s, appInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetElementNameAsync(env, args, argcAsync, argcPromise, elementNameCB); + } else { + ret = GetElementNamePromise(env, elementNameCB); + } + + return ret; +} + +/** + * @brief Obtains the elementName object of the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetElementNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + ElementNameCB *elementNameCB = CreateElementNameCBInfo(env); + if (elementNameCB == nullptr) { + return WrapVoidToJS(env); + } + + elementNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + elementNameCB->cbBase.abilityType = abilityType; + napi_value ret = GetElementNameWrap(env, info, elementNameCB); + if (ret == nullptr) { + if (elementNameCB != nullptr) { + delete elementNameCB; + elementNameCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AbilityInfoCB on success, nullptr on failure. + */ +AbilityInfoCB *CreateAbilityInfoCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AbilityInfoCB *abilityInfoCB = new (std::nothrow) AbilityInfoCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return abilityInfoCB; +} + +void SaveAbilityInfo(AbilityInfo_ &abilityInfo, const AbilityInfo &abilityInfoOrg) +{ + abilityInfo.bundleName = abilityInfoOrg.bundleName; + abilityInfo.name = abilityInfoOrg.name; + abilityInfo.label = abilityInfoOrg.label; + abilityInfo.description = abilityInfoOrg.description; + abilityInfo.icon = abilityInfoOrg.iconPath; + abilityInfo.labelId = abilityInfoOrg.applicationInfo.labelId; + abilityInfo.descriptionId = abilityInfoOrg.applicationInfo.descriptionId; + abilityInfo.iconId = abilityInfoOrg.applicationInfo.iconId; + abilityInfo.moduleName = abilityInfoOrg.moduleName; + abilityInfo.process = abilityInfoOrg.process; + abilityInfo.isVisible = abilityInfoOrg.visible; + + abilityInfo.type = static_cast(abilityInfoOrg.type); + abilityInfo.orientation = static_cast(abilityInfoOrg.orientation); + abilityInfo.launchMode = static_cast(abilityInfoOrg.launchMode); + + abilityInfo.uri = abilityInfoOrg.uri; + abilityInfo.targetAbility = abilityInfoOrg.targetAbility; + + for (size_t i = 0; i < abilityInfoOrg.permissions.size(); i++) { + abilityInfo.permissions.emplace_back(abilityInfoOrg.permissions.at(i)); + } + for (size_t i = 0; i < abilityInfoOrg.deviceTypes.size(); i++) { + abilityInfo.deviceTypes.emplace_back(abilityInfoOrg.deviceTypes.at(i)); + } + for (size_t i = 0; i < abilityInfoOrg.deviceCapabilities.size(); i++) { + abilityInfo.deviceCapabilities.emplace_back(abilityInfoOrg.deviceCapabilities.at(i)); + } + + SaveAppInfo(abilityInfo.appInfo, abilityInfoOrg.applicationInfo); + + abilityInfo.readPermission = abilityInfoOrg.readPermission; + abilityInfo.writePermission = abilityInfoOrg.writePermission; + abilityInfo.formEntity = 0; // no data + abilityInfo.minFormHeight = 0; // no data + abilityInfo.defaultFormHeight = 0; // no data + abilityInfo.minFormWidth = 0; // no data + abilityInfo.defaultFormWidth = 0; // no data + abilityInfo.backgroundModes = 0; // no data + abilityInfo.subType = 0; // no data + abilityInfo.formEnabled = false; // no data +} + +napi_value WrapAbilityInfo(napi_env env, const AbilityInfo_ &abilityInfo) +{ + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.bundleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "bundleName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.process.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "process", proValue)); + + NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.type), &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "type", proValue)); + + NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.orientation), &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "orientation", proValue)); + + NAPI_CALL(env, napi_create_int32(env, static_cast(abilityInfo.launchMode), &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "launchMode", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.uri.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "uri", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.readPermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "readPermission", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.writePermission.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "writePermission", proValue)); + + NAPI_CALL(env, napi_create_string_utf8(env, abilityInfo.targetAbility.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "targetAbility", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.labelId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.descriptionId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.iconId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.formEntity, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "formEntity", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormHeight, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "minFormHeight", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormHeight, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormHeight", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.minFormWidth, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "minFormWidth", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.defaultFormWidth, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "defaultFormWidth", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.backgroundModes, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "backgroundModes", proValue)); + + NAPI_CALL(env, napi_create_int32(env, abilityInfo.subType, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "subType", proValue)); + + NAPI_CALL(env, napi_get_boolean(env, abilityInfo.isVisible, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "isVisible", proValue)); + + NAPI_CALL(env, napi_get_boolean(env, abilityInfo.formEnabled, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "formEnabled", proValue)); + + napi_value jsArrayPermissions = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayPermissions)); + for (size_t i = 0; i < abilityInfo.permissions.size(); i++) { + proValue = nullptr; + NAPI_CALL( + env, napi_create_string_utf8(env, abilityInfo.permissions.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayPermissions, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "permissions", jsArrayPermissions)); + + napi_value jsArrayDeviceCapabilities = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceCapabilities)); + for (size_t i = 0; i < abilityInfo.deviceCapabilities.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8(env, abilityInfo.deviceCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayDeviceCapabilities, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "deviceCapabilities", jsArrayDeviceCapabilities)); + + napi_value jsArrayDeviceTypes = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayDeviceTypes)); + for (size_t i = 0; i < abilityInfo.deviceTypes.size(); i++) { + proValue = nullptr; + NAPI_CALL( + env, napi_create_string_utf8(env, abilityInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayDeviceTypes, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArrayDeviceTypes)); + + napi_value appInfo = nullptr; + appInfo = WrapAppInfo(env, abilityInfo.appInfo); + NAPI_CALL(env, napi_set_named_property(env, result, "applicationInfo", appInfo)); + + return result; +} + +/** + * @brief GetAbilityInfo asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAbilityInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, worker pool thread execute."); + AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; + if (abilityInfoCB == nullptr) { + return; + } + + abilityInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (abilityInfoCB->cbBase.ability == nullptr) { + abilityInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&abilityInfoCB->cbBase)) { + abilityInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr abilityInfoPtr = abilityInfoCB->cbBase.ability->GetAbilityInfo(); + if (abilityInfoPtr != nullptr) { + SaveAbilityInfo(abilityInfoCB->abilityInfo, *abilityInfoPtr); + } else { + abilityInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAbilityInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, abilityInfoCB->cbBase.errCode); + if (abilityInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, abilityInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (abilityInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, abilityInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork)); + delete abilityInfoCB; + abilityInfoCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAbilityInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetApplicationInfo, main event thread complete."); + AbilityInfoCB *abilityInfoCB = (AbilityInfoCB *)data; + napi_value result = nullptr; + if (abilityInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapAbilityInfo(env, abilityInfoCB->abilityInfo); + napi_resolve_deferred(env, abilityInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, abilityInfoCB->cbBase.errCode); + napi_reject_deferred(env, abilityInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, abilityInfoCB->cbBase.asyncWork); + delete abilityInfoCB; + abilityInfoCB = nullptr; +} + +/** + * @brief GetAbilityInfo Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param abilityInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAbilityInfoAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityInfoCB *abilityInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || abilityInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &abilityInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAbilityInfoExecuteCB, + GetAbilityInfoAsyncCompleteCB, + (void *)abilityInfoCB, + &abilityInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetAbilityInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param abilityInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAbilityInfoPromise(napi_env env, AbilityInfoCB *abilityInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (abilityInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + abilityInfoCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAbilityInfoExecuteCB, + GetAbilityInfoPromiseCompleteCB, + (void *)abilityInfoCB, + &abilityInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, abilityInfoCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetAbilityInfo processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param abilityInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAbilityInfoWrap(napi_env env, napi_callback_info info, AbilityInfoCB *abilityInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (abilityInfoCB == nullptr) { + HILOG_ERROR("%{public}s, abilityInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetAbilityInfoAsync(env, args, argcAsync, argcPromise, abilityInfoCB); + } else { + ret = GetAbilityInfoPromise(env, abilityInfoCB); + } + + return ret; +} + +/** + * @brief Obtains information about the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AbilityInfoCB *abilityInfoCB = CreateAbilityInfoCBInfo(env); + if (abilityInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + abilityInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + abilityInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetAbilityInfoWrap(env, info, abilityInfoCB); + if (ret == nullptr) { + if (abilityInfoCB != nullptr) { + delete abilityInfoCB; + abilityInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to HapModuleInfoCB on success, nullptr on failure. + */ +HapModuleInfoCB *CreateHapModuleInfoCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + HapModuleInfoCB *hapModuleInfoCB = new (std::nothrow) HapModuleInfoCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return hapModuleInfoCB; +} + +void SaveHapModuleInfo(HapModuleInfo_ &hapModuleInfo, const HapModuleInfo &hapModuleInfoOrg) +{ + hapModuleInfo.name = hapModuleInfoOrg.name; + hapModuleInfo.description = hapModuleInfoOrg.description; + hapModuleInfo.icon = hapModuleInfoOrg.iconPath; + hapModuleInfo.label = hapModuleInfoOrg.label; + hapModuleInfo.backgroundImg = hapModuleInfoOrg.backgroundImg; + hapModuleInfo.moduleName = hapModuleInfoOrg.moduleName; + hapModuleInfo.supportedModes = hapModuleInfoOrg.supportedModes; + hapModuleInfo.descriptionId = 0; // no data + hapModuleInfo.labelId = 0; // no data + hapModuleInfo.iconId = 0; // no data + hapModuleInfo.mainAbilityName = ""; // no data + hapModuleInfo.installationFree = false; // no data + + for (size_t i = 0; i < hapModuleInfoOrg.reqCapabilities.size(); i++) { + hapModuleInfo.reqCapabilities.emplace_back(hapModuleInfoOrg.reqCapabilities.at(i)); + } + + for (size_t i = 0; i < hapModuleInfoOrg.deviceTypes.size(); i++) { + hapModuleInfo.deviceTypes.emplace_back(hapModuleInfoOrg.deviceTypes.at(i)); + } + + for (size_t i = 0; i < hapModuleInfoOrg.abilityInfos.size(); i++) { + AbilityInfo_ abilityInfo; + SaveAbilityInfo(abilityInfo, hapModuleInfoOrg.abilityInfos.at(i)); + hapModuleInfo.abilityInfos.emplace_back(abilityInfo); + } +} + +napi_value WrapHapModuleInfo(napi_env env, const HapModuleInfoCB &hapModuleInfoCB) +{ + napi_value result = nullptr; + napi_value proValue = nullptr; + NAPI_CALL(env, napi_create_object(env, &result)); + NAPI_CALL( + env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.name.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "name", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.description.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "description", proValue)); + + NAPI_CALL( + env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.icon.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "icon", proValue)); + + NAPI_CALL( + env, napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.label.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "label", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.backgroundImg.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "backgroundImg", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8(env, hapModuleInfoCB.hapModuleInfo.moduleName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "moduleName", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.supportedModes, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "supportedModes", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.descriptionId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "descriptionId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.labelId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "labelId", proValue)); + + NAPI_CALL(env, napi_create_int32(env, hapModuleInfoCB.hapModuleInfo.iconId, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "iconId", proValue)); + + NAPI_CALL(env, + napi_create_string_utf8( + env, hapModuleInfoCB.hapModuleInfo.mainAbilityName.c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "mainAbilityName", proValue)); + + NAPI_CALL(env, napi_get_boolean(env, hapModuleInfoCB.hapModuleInfo.installationFree, &proValue)); + NAPI_CALL(env, napi_set_named_property(env, result, "installationFree", proValue)); + + napi_value jsArrayreqCapabilities = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArrayreqCapabilities)); + for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.reqCapabilities.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8( + env, hapModuleInfoCB.hapModuleInfo.reqCapabilities.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArrayreqCapabilities, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "reqCapabilities", jsArrayreqCapabilities)); + + napi_value jsArraydeviceTypes = nullptr; + NAPI_CALL(env, napi_create_array(env, &jsArraydeviceTypes)); + for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.deviceTypes.size(); i++) { + proValue = nullptr; + NAPI_CALL(env, + napi_create_string_utf8( + env, hapModuleInfoCB.hapModuleInfo.deviceTypes.at(i).c_str(), NAPI_AUTO_LENGTH, &proValue)); + NAPI_CALL(env, napi_set_element(env, jsArraydeviceTypes, i, proValue)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "deviceTypes", jsArraydeviceTypes)); + + napi_value abilityInfos = nullptr; + NAPI_CALL(env, napi_create_array(env, &abilityInfos)); + for (size_t i = 0; i < hapModuleInfoCB.hapModuleInfo.abilityInfos.size(); i++) { + napi_value abilityInfo = nullptr; + abilityInfo = WrapAbilityInfo(env, hapModuleInfoCB.hapModuleInfo.abilityInfos.at(i)); + NAPI_CALL(env, napi_set_element(env, abilityInfos, i, abilityInfo)); + } + NAPI_CALL(env, napi_set_named_property(env, result, "abilityInfos", abilityInfos)); + + return result; +} + +void GetHapModuleInfoExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetHapModuleInfo, worker pool thread execute."); + HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; + if (hapModuleInfoCB == nullptr) { + return; + } + + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (hapModuleInfoCB->cbBase.ability == nullptr) { + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&hapModuleInfoCB->cbBase)) { + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr hapModuleInfoPtr = hapModuleInfoCB->cbBase.ability->GetHapModuleInfo(); + if (hapModuleInfoPtr != nullptr) { + SaveHapModuleInfo(hapModuleInfoCB->hapModuleInfo, *hapModuleInfoPtr); + } else { + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } +} + +void GetHapModuleInfoAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); + HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, hapModuleInfoCB->cbBase.errCode); + if (hapModuleInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapHapModuleInfo(env, *hapModuleInfoCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, hapModuleInfoCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (hapModuleInfoCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, hapModuleInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); + delete hapModuleInfoCB; + hapModuleInfoCB = nullptr; +} + +void GetHapModuleInfoPromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetHapModuleInfo, main event thread complete."); + HapModuleInfoCB *hapModuleInfoCB = (HapModuleInfoCB *)data; + napi_value result = nullptr; + if (hapModuleInfoCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapHapModuleInfo(env, *hapModuleInfoCB); + napi_resolve_deferred(env, hapModuleInfoCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, hapModuleInfoCB->cbBase.errCode); + napi_reject_deferred(env, hapModuleInfoCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, hapModuleInfoCB->cbBase.asyncWork); + delete hapModuleInfoCB; + hapModuleInfoCB = nullptr; +} + +/** + * @brief GetHapModuleInfo Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param hapModuleInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetHapModuleInfoAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, HapModuleInfoCB *hapModuleInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || hapModuleInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &hapModuleInfoCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetHapModuleInfoExecuteCB, + GetHapModuleInfoAsyncCompleteCB, + (void *)hapModuleInfoCB, + &hapModuleInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetHapModuleInfo Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param hapModuleInfoCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetHapModuleInfoPromise(napi_env env, HapModuleInfoCB *hapModuleInfoCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (hapModuleInfoCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + hapModuleInfoCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetHapModuleInfoExecuteCB, + GetHapModuleInfoPromiseCompleteCB, + (void *)hapModuleInfoCB, + &hapModuleInfoCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, hapModuleInfoCB->cbBase.asyncWork)); + + return promise; +} + +napi_value GetHapModuleInfoWrap(napi_env env, napi_callback_info info, HapModuleInfoCB *hapModuleInfoCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (hapModuleInfoCB == nullptr) { + HILOG_ERROR("%{public}s, hapModuleInfoCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetHapModuleInfoAsync(env, args, argcAsync, argcPromise, hapModuleInfoCB); + } else { + ret = GetHapModuleInfoPromise(env, hapModuleInfoCB); + } + + return ret; +} + +/** + * @brief Obtains the HapModuleInfo object of the application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + HapModuleInfoCB *hapModuleInfoCB = CreateHapModuleInfoCBInfo(env); + if (hapModuleInfoCB == nullptr) { + return WrapVoidToJS(env); + } + + hapModuleInfoCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + hapModuleInfoCB->cbBase.abilityType = abilityType; + napi_value ret = GetHapModuleInfoWrap(env, info, hapModuleInfoCB); + if (ret == nullptr) { + if (hapModuleInfoCB != nullptr) { + delete hapModuleInfoCB; + hapModuleInfoCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to ProcessNameCB on success, nullptr on failure. + */ +ProcessNameCB *CreateProcessNameCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ProcessNameCB *processNameCB = new (std::nothrow) ProcessNameCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return processNameCB; +} + +/** + * @brief GetProcessName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessNameExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetProcessName, worker pool thread execute."); + ProcessNameCB *processNameCB = (ProcessNameCB *)data; + if (processNameCB == nullptr) { + return; + } + + processNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (processNameCB->cbBase.ability == nullptr) { + processNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&processNameCB->cbBase)) { + processNameCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + processNameCB->processName = processNameCB->cbBase.ability->GetProcessName(); +} + +napi_value WrapProcessName(napi_env env, ProcessNameCB *processNameCB) +{ + if (processNameCB == nullptr) { + HILOG_ERROR("Invalid param(processNameCB == nullptr)"); + return nullptr; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, processNameCB->processName.c_str(), NAPI_AUTO_LENGTH, &result)); + + return result; +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessNameAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessName, main event thread complete."); + ProcessNameCB *processNameCB = (ProcessNameCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, processNameCB->cbBase.errCode); + if (processNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapProcessName(env, processNameCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, processNameCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (processNameCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, processNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, processNameCB->cbBase.asyncWork)); + delete processNameCB; + processNameCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetProcessNamePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetProcessName, main event thread complete."); + ProcessNameCB *processNameCB = (ProcessNameCB *)data; + napi_value result = nullptr; + if (processNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapProcessName(env, processNameCB); + napi_resolve_deferred(env, processNameCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, processNameCB->cbBase.errCode); + napi_reject_deferred(env, processNameCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, processNameCB->cbBase.asyncWork); + delete processNameCB; + processNameCB = nullptr; +} + +/** + * @brief GetProcessName Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param ProcessNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessNameAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || processNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &processNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessNameExecuteCB, + GetProcessNameAsyncCompleteCB, + (void *)processNameCB, + &processNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetProcessName Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessNamePromise(napi_env env, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (processNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + processNameCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetProcessNameExecuteCB, + GetProcessNamePromiseCompleteCB, + (void *)processNameCB, + &processNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, processNameCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetProcessName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param ProcessNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetProcessNameWrap(napi_env env, napi_callback_info info, ProcessNameCB *processNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (processNameCB == nullptr) { + HILOG_ERROR("%{public}s, processNameCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetProcessNameAsync(env, args, argcAsync, argcPromise, processNameCB); + } else { + ret = GetProcessNamePromise(env, processNameCB); + } + + return ret; +} + +/** + * @brief Obtains the name of the current process. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetProcessNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + + ProcessNameCB *processNameCB = CreateProcessNameCBInfo(env); + if (processNameCB == nullptr) { + return WrapVoidToJS(env); + } + + processNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + processNameCB->cbBase.abilityType = abilityType; + napi_value ret = GetProcessNameWrap(env, info, processNameCB); + if (ret == nullptr) { + if (processNameCB != nullptr) { + delete processNameCB; + processNameCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to CallingBundleCB on success, nullptr on failure. + */ +CallingBundleCB *CreateCallingBundleCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + CallingBundleCB *callingBundleCB = new (std::nothrow) CallingBundleCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return callingBundleCB; +} + +/** + * @brief GetCallingBundle asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCallingBundleExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("NAPI_GetCallingBundle, worker pool thread execute."); + CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + if (callingBundleCB == nullptr) { + return; + } + + callingBundleCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (callingBundleCB->cbBase.ability == nullptr) { + callingBundleCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&callingBundleCB->cbBase)) { + callingBundleCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + callingBundleCB->callingBundleName = callingBundleCB->cbBase.ability->GetCallingBundle(); +} + +napi_value WrapCallingBundle(napi_env env, CallingBundleCB *callingBundleCB) +{ + if (callingBundleCB == nullptr) { + HILOG_ERROR("Invalid param(callingBundleCB == nullptr)"); + return nullptr; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, callingBundleCB->callingBundleName.c_str(), NAPI_AUTO_LENGTH, &result)); + + return result; +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCallingBundleAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); + CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, callingBundleCB->cbBase.errCode); + if (callingBundleCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapCallingBundle(env, callingBundleCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, callingBundleCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (callingBundleCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, callingBundleCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork)); + delete callingBundleCB; + callingBundleCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetCallingBundlePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetCallingBundle, main event thread complete."); + CallingBundleCB *callingBundleCB = (CallingBundleCB *)data; + napi_value result = nullptr; + if (callingBundleCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapCallingBundle(env, callingBundleCB); + napi_resolve_deferred(env, callingBundleCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, callingBundleCB->cbBase.errCode); + napi_reject_deferred(env, callingBundleCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, callingBundleCB->cbBase.asyncWork); + delete callingBundleCB; + callingBundleCB = nullptr; +} + +/** + * @brief GetCallingBundle Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetCallingBundleAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &callingBundleCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetCallingBundleExecuteCB, + GetCallingBundleAsyncCompleteCB, + (void *)callingBundleCB, + &callingBundleCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetCallingBundle Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetCallingBundlePromise(napi_env env, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + callingBundleCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetCallingBundleExecuteCB, + GetCallingBundlePromiseCompleteCB, + (void *)callingBundleCB, + &callingBundleCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, callingBundleCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetCallingBundle processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param CallingBundleCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetCallingBundleWrap(napi_env env, napi_callback_info info, CallingBundleCB *callingBundleCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (callingBundleCB == nullptr) { + HILOG_ERROR("%{public}s, callingBundleCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetCallingBundleAsync(env, args, argcAsync, argcPromise, callingBundleCB); + } else { + ret = GetCallingBundlePromise(env, callingBundleCB); + } + + return ret; +} + +/** + * @brief Obtains the bundle name of the ability that called the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetCallingBundleCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + CallingBundleCB *calloingBundleCB = CreateCallingBundleCBInfo(env); + if (calloingBundleCB == nullptr) { + return WrapVoidToJS(env); + } + + calloingBundleCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + calloingBundleCB->cbBase.abilityType = abilityType; + napi_value ret = GetCallingBundleWrap(env, info, calloingBundleCB); + if (ret == nullptr) { + if (calloingBundleCB != nullptr) { + delete calloingBundleCB; + calloingBundleCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AsyncCallbackInfo on success, nullptr on failure + */ +AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env) +{ + napi_value global = 0; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = 0; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AsyncCallbackInfo *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo{ + .cbInfo.env = env, + .asyncWork = nullptr, + .deferred = nullptr, + .ability = ability, + .native_result = false, + .errCode = NAPI_ERR_NO_ERROR, + .abilityType = AbilityType::UNKNOWN, + }; + return asyncCallbackInfo; +} + +void GetContextAsyncExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("GetContextAsync, worker pool thread execute."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + return; + } + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + if (asyncCallbackInfo->ability == nullptr) { + asyncCallbackInfo->errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(asyncCallbackInfo)) { + asyncCallbackInfo->errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } +} + +napi_value GetContextAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, args[argcPromise], &valuetype); + if (valuetype == napi_function) { + napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + } + napi_create_async_work( + env, + nullptr, + resourceName, + GetContextAsyncExecuteCB, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("GetContextAsync, main event thread complete."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + napi_value callback = 0; + napi_value undefined = 0; + napi_value result[ARGS_TWO] = {0}; + napi_value callResult = 0; + napi_get_undefined(env, &undefined); + result[PARAM0] = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); + if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { + napi_new_instance(env, g_classContext, 0, nullptr, &result[PARAM1]); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); + napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + napi_value result = 0; + napi_get_null(env, &result); + return result; +} + +napi_value GetContextPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_deferred deferred; + napi_value promise = 0; + napi_create_promise(env, &deferred, &promise); + asyncCallbackInfo->deferred = deferred; + + napi_create_async_work( + env, + nullptr, + resourceName, + GetContextAsyncExecuteCB, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("GetContextPromise, main event thread complete."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + napi_value result = nullptr; + if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { + napi_new_instance(env, g_classContext, 0, nullptr, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + result = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); + napi_reject_deferred(env, asyncCallbackInfo->deferred, result); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + + return promise; +} + +/** + * @brief GetContext processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetContextWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, asyncCallbackInfo == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetContextAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + } else { + ret = GetContextPromise(env, asyncCallbackInfo); + } + + return ret; +} + +/** + * @brief Get context. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetContextCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + return WrapVoidToJS(env); + } + + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = GetContextWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + if (asyncCallbackInfo != nullptr) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +void GetWantExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + return; + } + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + if (asyncCallbackInfo->ability == nullptr) { + asyncCallbackInfo->errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(asyncCallbackInfo)) { + asyncCallbackInfo->errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + std::shared_ptr ptrWant = asyncCallbackInfo->ability->GetWant(); + if (ptrWant != nullptr) { + asyncCallbackInfo->param.want = *ptrWant; + } else { + asyncCallbackInfo->errCode = NAPI_ERR_ABILITY_CALL_INVALID; + } +} + +napi_value GetWantAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, args[argcPromise], &valuetype); + if (valuetype == napi_function) { + napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback); + } + napi_create_async_work( + env, + nullptr, + resourceName, + GetWantExecuteCB, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("GetWantAsync, main event thread complete."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + napi_value callback = 0; + napi_value undefined = 0; + napi_value result[ARGS_TWO] = {0}; + napi_value callResult = 0; + napi_get_undefined(env, &undefined); + result[PARAM0] = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); + if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapWant(env, asyncCallbackInfo->param.want); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); + napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + napi_value result = 0; + napi_get_null(env, &result); + return result; +} + +napi_value GetWantPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName); + napi_deferred deferred; + napi_value promise = 0; + napi_create_promise(env, &deferred, &promise); + asyncCallbackInfo->deferred = deferred; + + napi_create_async_work( + env, + nullptr, + resourceName, + GetWantExecuteCB, + [](napi_env env, napi_status status, void *data) { + HILOG_INFO("GetWantPromise, main event thread complete."); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + napi_value result = nullptr; + if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { + result = WrapWant(env, asyncCallbackInfo->param.want); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + result = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); + napi_reject_deferred(env, asyncCallbackInfo->deferred, result); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + }, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork); + napi_queue_async_work(env, asyncCallbackInfo->asyncWork); + + return promise; +} + +/** + * @brief GetWantWrap processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetWantWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, asyncCallbackInfo == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetWantAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + } else { + ret = GetWantPromise(env, asyncCallbackInfo); + } + + return ret; +} + +/** + * @brief Get want. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetWantCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + return WrapVoidToJS(env); + } + + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = GetWantWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + if (asyncCallbackInfo != nullptr) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AbilityNameCB on success, nullptr on failure. + */ +AbilityNameCB *CreateAbilityNameCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + AbilityNameCB *abilityNameCB = new (std::nothrow) AbilityNameCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + return abilityNameCB; +} + +napi_value WrapAbilityName(napi_env env, AbilityNameCB *abilityNameCB) +{ + if (abilityNameCB == nullptr) { + HILOG_ERROR("Invalid param(abilityNameCB == nullptr)"); + return nullptr; + } + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, abilityNameCB->name.c_str(), NAPI_AUTO_LENGTH, &result)); + + return result; +} + +/** + * @brief GetAbilityName asynchronous processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAbilityNameExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AbilityNameCB *abilityNameCB = (AbilityNameCB *)data; + if (abilityNameCB == nullptr) { + return; + } + abilityNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + if (abilityNameCB->cbBase.ability == nullptr) { + abilityNameCB->cbBase.errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&abilityNameCB->cbBase)) { + abilityNameCB->cbBase.errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + abilityNameCB->name = abilityNameCB->cbBase.ability->GetAbilityName(); +} + +/** + * @brief The callback at the end of the asynchronous callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAbilityNameAsyncCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AbilityNameCB *abilityNameCB = (AbilityNameCB *)data; + napi_value callback = nullptr; + napi_value undefined = nullptr; + napi_value result[ARGS_TWO] = {nullptr}; + napi_value callResult = nullptr; + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); + result[PARAM0] = GetCallbackErrorValue(env, abilityNameCB->cbBase.errCode); + if (abilityNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapAbilityName(env, abilityNameCB); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, abilityNameCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + if (abilityNameCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, abilityNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, abilityNameCB->cbBase.asyncWork)); + delete abilityNameCB; + abilityNameCB = nullptr; +} + +/** + * @brief The callback at the end of the Promise callback. + * + * @param env The environment that the Node-API call is invoked under. + * @param data Point to asynchronous processing of data. + */ +void GetAbilityNamePromiseCompleteCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("NAPI_GetAbilityName, main event thread complete."); + AbilityNameCB *abilityNameCB = (AbilityNameCB *)data; + napi_value result = nullptr; + if (abilityNameCB->cbBase.errCode == NAPI_ERR_NO_ERROR) { + result = WrapAbilityName(env, abilityNameCB); + napi_resolve_deferred(env, abilityNameCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, abilityNameCB->cbBase.errCode); + napi_reject_deferred(env, abilityNameCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, abilityNameCB->cbBase.asyncWork); + delete abilityNameCB; + abilityNameCB = nullptr; +} + +/** + * @brief GetAbilityName Async. + * + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * @param argcPromise Asynchronous data processing. + * @param abilityNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAbilityNameAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AbilityNameCB *abilityNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (args == nullptr || abilityNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &abilityNameCB->cbBase.cbInfo.callback)); + } + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAbilityNameExecuteCB, + GetAbilityNameAsyncCompleteCB, + (void *)abilityNameCB, + &abilityNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, abilityNameCB->cbBase.asyncWork)); + napi_value result = nullptr; + NAPI_CALL(env, napi_get_null(env, &result)); + return result; +} + +/** + * @brief GetAbilityName Promise. + * + * @param env The environment that the Node-API call is invoked under. + * @param abilityNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAbilityNamePromise(napi_env env, AbilityNameCB *abilityNameCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (abilityNameCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = nullptr; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = nullptr; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + abilityNameCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + GetAbilityNameExecuteCB, + GetAbilityNamePromiseCompleteCB, + (void *)abilityNameCB, + &abilityNameCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, abilityNameCB->cbBase.asyncWork)); + + return promise; +} + +/** + * @brief GetAbilityName processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param abilityNameCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value GetAbilityNameWrap(napi_env env, napi_callback_info info, AbilityNameCB *abilityNameCB) +{ + HILOG_INFO("%{public}s, asyncCallback.", __func__); + if (abilityNameCB == nullptr) { + HILOG_ERROR("%{public}s, abilityNameCB == nullptr.", __func__); + return nullptr; + } + + size_t argcAsync = 1; + const size_t argcPromise = 0; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > argcPromise) { + ret = GetAbilityNameAsync(env, args, argcAsync, argcPromise, abilityNameCB); + } else { + ret = GetAbilityNamePromise(env, abilityNameCB); + } + + return ret; +} + +/** + * @brief Obtains the class name in this ability name, without the prefixed bundle name. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAbilityNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AbilityNameCB *ablityNameCB = CreateAbilityNameCBInfo(env); + if (ablityNameCB == nullptr) { + return WrapVoidToJS(env); + } + + ablityNameCB->cbBase.errCode = NAPI_ERR_NO_ERROR; + ablityNameCB->cbBase.abilityType = abilityType; + napi_value ret = GetAbilityNameWrap(env, info, ablityNameCB); + if (ret == nullptr) { + if (ablityNameCB != nullptr) { + delete ablityNameCB; + ablityNameCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Parse the parameters. + * + * @param param Indicates the parameters saved the parse result. + * @param env The environment that the Node-API call is invoked under. + * @param args Indicates the arguments passed into the callback. + * + * @return The return value from NAPI C++ to JS for the module. + */ +bool UnwrapParamForWant(napi_env env, napi_value args, AbilityType abilityType, CallAbilityParam ¶m) +{ + HILOG_INFO("%{public}s called.", __func__); + + bool ret = false; + napi_valuetype valueType = napi_undefined; + param.setting = nullptr; + NAPI_CALL_BASE(env, napi_typeof(env, args, &valueType), false); + if (valueType != napi_object) { + return false; + } + + if (abilityType == AbilityType::PAGE) { + napi_value jsWant = GetPropertyValueByPropertyName(env, args, "want", napi_object); + if (jsWant == nullptr) { + return false; + } + + ret = UnwrapWant(env, jsWant, param.want); + + napi_value jsSettingObj = GetPropertyValueByPropertyName(env, args, "abilityStartSetting", napi_object); + if (jsSettingObj != nullptr) { + param.setting = AbilityStartSetting::GetEmptySetting(); + HILOG_INFO("%{public}s abilityStartSetting = %{public}p.", __func__, param.setting.get()); + } + } else { + ret = UnwrapWant(env, args, param.want); + } + return ret; +} + +void StartAbilityExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + return; + } + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + if (asyncCallbackInfo->ability == nullptr) { + asyncCallbackInfo->errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(asyncCallbackInfo)) { + asyncCallbackInfo->errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + if (asyncCallbackInfo->param.setting == nullptr) { + asyncCallbackInfo->ability->StartAbility(asyncCallbackInfo->param.want); + } else { + asyncCallbackInfo->ability->StartAbility( + asyncCallbackInfo->param.want, *(asyncCallbackInfo->param.setting.get())); + } +} + +void StartAbilityCallbackCompletedCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + napi_value callback = 0; + napi_value undefined = 0; + napi_value result[ARGS_TWO] = {0}; + napi_value callResult = 0; + napi_get_undefined(env, &undefined); + result[PARAM0] = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); + if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { + if (asyncCallbackInfo->abilityType == AbilityType::PAGE) { + napi_create_int32(env, 0, &result[PARAM1]); + } else { + result[PARAM1] = WrapVoidToJS(env); + } + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + + napi_get_reference_value(env, asyncCallbackInfo->cbInfo.callback, &callback); + napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + napi_delete_reference(env, asyncCallbackInfo->cbInfo.callback); + } + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; +} + +void StartAbilityPromiseCompletedCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = (AsyncCallbackInfo *)data; + napi_value result = 0; + if (asyncCallbackInfo->errCode == NAPI_ERR_NO_ERROR) { + if (asyncCallbackInfo->abilityType == AbilityType::PAGE) { + napi_create_int32(env, 0, &result); + } else { + result = WrapVoidToJS(env); + } + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + result = GetCallbackErrorValue(env, asyncCallbackInfo->errCode); + napi_reject_deferred(env, asyncCallbackInfo->deferred, result); + } + + napi_delete_async_work(env, asyncCallbackInfo->asyncWork); + delete asyncCallbackInfo; +} + +napi_value StartAbilityAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s asyncCallback.", __func__); + if (args == nullptr || asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &asyncCallbackInfo->cbInfo.callback)); + } + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + StartAbilityExecuteCB, + StartAbilityCallbackCompletedCB, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + + return WrapVoidToJS(env); +} + +napi_value StartAbilityPromise(napi_env env, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (asyncCallbackInfo == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + asyncCallbackInfo->deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + StartAbilityExecuteCB, + StartAbilityPromiseCompletedCB, + (void *)asyncCallbackInfo, + &asyncCallbackInfo->asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork)); + return promise; +} + +/** + * @brief StartAbility processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param asyncCallbackInfo Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value StartAbilityWrap(napi_env env, napi_callback_info info, AsyncCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called.", __func__); + size_t argcAsync = 2; + const size_t argcPromise = 1; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + CallAbilityParam param; + if (UnwrapParamForWant(env, args[PARAM0], asyncCallbackInfo->abilityType, param) == false) { + HILOG_ERROR("%{public}s, call UnwrapParam failed.", __func__); + return nullptr; + } + + asyncCallbackInfo->param = param; + if (argcAsync > argcPromise) { + ret = StartAbilityAsync(env, args, argcAsync, argcPromise, asyncCallbackInfo); + } else { + ret = StartAbilityPromise(env, asyncCallbackInfo); + } + + return ret; +} + +/** + * @brief startAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_StartAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + AsyncCallbackInfo *asyncCallbackInfo = CreateAsyncCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + return WrapVoidToJS(env); + } + + asyncCallbackInfo->errCode = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = StartAbilityWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + if (asyncCallbackInfo != nullptr) { + delete asyncCallbackInfo; + asyncCallbackInfo = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +bool UnwrapParamStopAbilityWrap(napi_env env, size_t argc, napi_value *argv, AsyncJSCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called, argc=%{public}d", __func__, argc); + + const size_t argcMax = 2; + if (argc > argcMax || argc < argcMax - 1) { + HILOG_INFO("%{public}s called, Params is invalid.", __func__); + return false; + } + + if (argc == argcMax) { + if (!CreateAsyncCallback(env, argv[PARAM1], asyncCallbackInfo)) { + HILOG_INFO("%{public}s called, the second parameter is invalid.", __func__); + return false; + } + } + + return UnwrapWant(env, argv[PARAM0], asyncCallbackInfo->param.want); +} + +void StopAbilityExecuteCallback(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + + AsyncJSCallbackInfo *asyncCallbackInfo = (AsyncJSCallbackInfo *)data; + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called. asyncCallbackInfo is null", __func__); + return; + } + + asyncCallbackInfo->error_code = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->native_data.data_type = NVT_NONE; + + if (asyncCallbackInfo->ability == nullptr) { + asyncCallbackInfo->error_code = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(asyncCallbackInfo)) { + asyncCallbackInfo->error_code = NAPI_ERR_ABILITY_TYPE_INVALID; + asyncCallbackInfo->native_data.data_type = NVT_UNDEFINED; + return; + } + + asyncCallbackInfo->native_data.data_type = NVT_BOOL; + asyncCallbackInfo->native_data.bool_value = asyncCallbackInfo->ability->StopAbility(asyncCallbackInfo->param.want); +} + +napi_value StopAbilityWrap(napi_env env, napi_callback_info info, AsyncJSCallbackInfo *asyncCallbackInfo) +{ + HILOG_INFO("%{public}s called.", __func__); + size_t argc = ARGS_MAX_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value jsthis = 0; + void *data = nullptr; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &jsthis, &data)); + + if (!UnwrapParamStopAbilityWrap(env, argc, args, asyncCallbackInfo)) { + HILOG_INFO("%{public}s called. Invoke UnwrapParamStopAbility fail", __func__); + return nullptr; + } + + AsyncParamEx asyncParamEx; + if (asyncCallbackInfo->cbInfo.callback != nullptr) { + HILOG_INFO("%{public}s called. asyncCallback.", __func__); + asyncParamEx.resource = "NAPI_StopAbilityWrapCallback"; + asyncParamEx.execute = StopAbilityExecuteCallback; + asyncParamEx.complete = CompleteAsyncCallbackWork; + + return ExecuteAsyncCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } else { + HILOG_INFO("%{public}s called. promise.", __func__); + asyncParamEx.resource = "NAPI_StopAbilityWrapPromise"; + asyncParamEx.execute = StopAbilityExecuteCallback; + asyncParamEx.complete = CompletePromiseCallbackWork; + + return ExecutePromiseCallbackWork(env, asyncCallbackInfo, &asyncParamEx); + } +} + +/** + * @brief stopAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_StopAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + + AsyncJSCallbackInfo *asyncCallbackInfo = CreateAsyncJSCallbackInfo(env); + if (asyncCallbackInfo == nullptr) { + HILOG_INFO("%{public}s called. Invoke CreateAsyncJSCallbackInfo failed.", __func__); + return WrapVoidToJS(env); + } + + asyncCallbackInfo->error_code = NAPI_ERR_NO_ERROR; + asyncCallbackInfo->abilityType = abilityType; + napi_value ret = StopAbilityWrap(env, info, asyncCallbackInfo); + if (ret == nullptr) { + FreeAsyncJSCallbackInfo(&asyncCallbackInfo); + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AbilityNameCB on success, nullptr on failure. + */ +ConnectAbilityCB *CreateConnectAbilityCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + + sptr connect(new (std::nothrow) NAPIAbilityConnection()); + connectAbilityCB->abilityConnection = connect; + return connectAbilityCB; +} + +void ConnectAbilityExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)data; + if (connectAbilityCB == nullptr) { + return; + } + connectAbilityCB->errCode = NAPI_ERR_NO_ERROR; + if (connectAbilityCB->cbBase.ability == nullptr) { + connectAbilityCB->errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&connectAbilityCB->cbBase)) { + connectAbilityCB->errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + connectAbilityCB->abilityConnection->SetEnv(env); + connectAbilityCB->abilityConnection->SetCallbackRef(connectAbilityCB->abilityConnectionCB.cbInfo.callback); + connectAbilityCB->result = + connectAbilityCB->cbBase.ability->ConnectAbility(connectAbilityCB->want, connectAbilityCB->abilityConnection); + + HILOG_INFO("%{public}s called. bundlename:%{public}s", __func__, connectAbilityCB->want.GetBundle().c_str()); + HILOG_INFO("%{public}s called. abilityname:%{public}s", + __func__, + connectAbilityCB->want.GetElement().GetAbilityName().c_str()); + HILOG_INFO("%{public}s called. result:%{public}d", __func__, connectAbilityCB->result); +} + +void ConnectAbilityCallbackCompletedCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)data; + napi_value callback = 0; + napi_value undefined = 0; + napi_value result[ARGS_TWO] = {0}; + napi_value callResult = 0; + napi_get_undefined(env, &undefined); + result[PARAM0] = GetCallbackErrorValue(env, connectAbilityCB->errCode); + HILOG_INFO("%{public}s errCode=%{public}d.", __func__, connectAbilityCB->errCode); + HILOG_INFO("%{public}s result=%{public}d.", __func__, connectAbilityCB->result); + if (connectAbilityCB->errCode == NAPI_ERR_NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, connectAbilityCB->result, &result[PARAM1])); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, connectAbilityCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (connectAbilityCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, connectAbilityCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, connectAbilityCB->cbBase.asyncWork)); + delete connectAbilityCB; + connectAbilityCB = nullptr; +} + +void ConnectAbilityPromiseCompletedCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)data; + napi_value result = 0; + HILOG_INFO("%{public}s errCode=%{public}d.", __func__, connectAbilityCB->errCode); + HILOG_INFO("%{public}s result=%{public}d.", __func__, connectAbilityCB->result); + if (connectAbilityCB->errCode == NAPI_ERR_NO_ERROR) { + napi_get_boolean(env, connectAbilityCB->result, &result); + napi_resolve_deferred(env, connectAbilityCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, connectAbilityCB->errCode); + napi_reject_deferred(env, connectAbilityCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, connectAbilityCB->cbBase.asyncWork); + delete connectAbilityCB; +} + +napi_value ConnectAbilityAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ConnectAbilityCB *connectAbilityCB) +{ + HILOG_INFO("%{public}s asyncCallback.", __func__); + if (args == nullptr || connectAbilityCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &connectAbilityCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + ConnectAbilityExecuteCB, + ConnectAbilityCallbackCompletedCB, + (void *)connectAbilityCB, + &connectAbilityCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, connectAbilityCB->cbBase.asyncWork)); + + return WrapVoidToJS(env); +} + +napi_value ConnectAbilityPromise(napi_env env, ConnectAbilityCB *connectAbilityCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (connectAbilityCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + connectAbilityCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + ConnectAbilityExecuteCB, + ConnectAbilityPromiseCompletedCB, + (void *)connectAbilityCB, + &connectAbilityCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, connectAbilityCB->cbBase.asyncWork)); + return promise; +} + +/** + * @brief ConnectAbility processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param connectAbilityCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value ConnectAbilityWrap(napi_env env, napi_callback_info info, ConnectAbilityCB *connectAbilityCB) +{ + HILOG_INFO("%{public}s called.", __func__); + size_t argcAsync = ARGS_THREE; + const size_t argcPromise = ARGS_TWO; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (!UnwrapWant(env, args[PARAM0], connectAbilityCB->want)) { + HILOG_INFO("%{public}s called. Invoke UnwrapWant fail", __func__); + return nullptr; + } + + HILOG_INFO("%{public}s called. bundlename:%{public}s", __func__, connectAbilityCB->want.GetBundle().c_str()); + HILOG_INFO("%{public}s called. abilityname:%{public}s", + __func__, + connectAbilityCB->want.GetElement().GetAbilityName().c_str()); + + if (argcAsync > PARAM1) { + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, args[PARAM1], &valuetype); + if (valuetype == napi_function) { + NAPI_CALL(env, + napi_create_reference(env, args[PARAM1], 1, &connectAbilityCB->abilityConnectionCB.cbInfo.callback)); + } + } + + if (argcAsync > argcPromise) { + ret = ConnectAbilityAsync(env, args, argcAsync, argcPromise, connectAbilityCB); + } else { + ret = ConnectAbilityPromise(env, connectAbilityCB); + } + + return ret; +} + +/** + * @brief ConnectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_ConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = CreateConnectAbilityCBInfo(env); + if (connectAbilityCB == nullptr) { + return WrapVoidToJS(env); + } + + connectAbilityCB->errCode = NAPI_ERR_NO_ERROR; + connectAbilityCB->cbBase.abilityType = abilityType; + napi_value ret = ConnectAbilityWrap(env, info, connectAbilityCB); + if (ret == nullptr) { + if (connectAbilityCB != nullptr) { + delete connectAbilityCB; + connectAbilityCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AbilityNameCB on success, nullptr on failure. + */ +ConnectAbilityCB *CreateDisConnectAbilityCBInfo(napi_env env) +{ + napi_value global = nullptr; + NAPI_CALL(env, napi_get_global(env, &global)); + + napi_value abilityObj = nullptr; + NAPI_CALL(env, napi_get_named_property(env, global, "ability", &abilityObj)); + + Ability *ability = nullptr; + NAPI_CALL(env, napi_get_value_external(env, abilityObj, (void **)&ability)); + + ConnectAbilityCB *connectAbilityCB = new (std::nothrow) ConnectAbilityCB{ + .cbBase.cbInfo.env = env, + .cbBase.asyncWork = nullptr, + .cbBase.deferred = nullptr, + .cbBase.ability = ability, + }; + + sptr connect(new (std::nothrow) NAPIAbilityConnection()); + connectAbilityCB->abilityConnection = connect; + return connectAbilityCB; +} + +void DisConnectAbilityExecuteCB(napi_env env, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)data; + if (connectAbilityCB == nullptr) { + return; + } + connectAbilityCB->errCode = NAPI_ERR_NO_ERROR; + if (connectAbilityCB->cbBase.ability == nullptr) { + connectAbilityCB->errCode = NAPI_ERR_ACE_ABILITY; + return; + } + + if (!CheckAbilityType(&connectAbilityCB->cbBase)) { + connectAbilityCB->errCode = NAPI_ERR_ABILITY_TYPE_INVALID; + return; + } + + connectAbilityCB->abilityConnection->SetEnv(env); + connectAbilityCB->abilityConnection->SetCallbackRef(connectAbilityCB->abilityConnectionCB.cbInfo.callback); + connectAbilityCB->cbBase.ability->DisconnectAbility(connectAbilityCB->abilityConnection); +} + +void DisConnectAbilityCallbackCompletedCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)data; + napi_value callback = 0; + napi_value undefined = 0; + napi_value result[ARGS_TWO] = {0}; + napi_value callResult = 0; + napi_get_undefined(env, &undefined); + result[PARAM0] = GetCallbackErrorValue(env, connectAbilityCB->errCode); + if (connectAbilityCB->errCode == NAPI_ERR_NO_ERROR) { + result[PARAM1] = WrapVoidToJS(env); + } else { + result[PARAM1] = WrapUndefinedToJS(env); + } + + NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, connectAbilityCB->cbBase.cbInfo.callback, &callback)); + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult)); + + if (connectAbilityCB->cbBase.cbInfo.callback != nullptr) { + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, connectAbilityCB->cbBase.cbInfo.callback)); + } + NAPI_CALL_RETURN_VOID(env, napi_delete_async_work(env, connectAbilityCB->cbBase.asyncWork)); + delete connectAbilityCB; + connectAbilityCB = nullptr; +} + +void DisConnectAbilityPromiseCompletedCB(napi_env env, napi_status status, void *data) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = (ConnectAbilityCB *)data; + napi_value result = 0; + if (connectAbilityCB->errCode == NAPI_ERR_NO_ERROR) { + result = WrapVoidToJS(env); + napi_resolve_deferred(env, connectAbilityCB->cbBase.deferred, result); + } else { + result = GetCallbackErrorValue(env, connectAbilityCB->errCode); + napi_reject_deferred(env, connectAbilityCB->cbBase.deferred, result); + } + + napi_delete_async_work(env, connectAbilityCB->cbBase.asyncWork); + delete connectAbilityCB; +} + +napi_value DisConnectAbilityAsync( + napi_env env, napi_value *args, size_t argcAsync, const size_t argcPromise, ConnectAbilityCB *connectAbilityCB) +{ + HILOG_INFO("%{public}s asyncCallback.", __func__); + if (args == nullptr || connectAbilityCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName = 0; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, args[argcPromise], &valuetype)); + if (valuetype == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[argcPromise], 1, &connectAbilityCB->cbBase.cbInfo.callback)); + } + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + DisConnectAbilityExecuteCB, + DisConnectAbilityCallbackCompletedCB, + (void *)connectAbilityCB, + &connectAbilityCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, connectAbilityCB->cbBase.asyncWork)); + + return WrapVoidToJS(env); +} + +napi_value DisConnectAbilityPromise(napi_env env, ConnectAbilityCB *connectAbilityCB) +{ + HILOG_INFO("%{public}s, promise.", __func__); + if (connectAbilityCB == nullptr) { + HILOG_ERROR("%{public}s, param == nullptr.", __func__); + return nullptr; + } + napi_value resourceName; + NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName)); + napi_deferred deferred; + napi_value promise = 0; + NAPI_CALL(env, napi_create_promise(env, &deferred, &promise)); + connectAbilityCB->cbBase.deferred = deferred; + + NAPI_CALL(env, + napi_create_async_work(env, + nullptr, + resourceName, + DisConnectAbilityExecuteCB, + DisConnectAbilityPromiseCompletedCB, + (void *)connectAbilityCB, + &connectAbilityCB->cbBase.asyncWork)); + NAPI_CALL(env, napi_queue_async_work(env, connectAbilityCB->cbBase.asyncWork)); + return promise; +} + +/** + * @brief DisConnectAbility processing function. + * + * @param env The environment that the Node-API call is invoked under. + * @param connectAbilityCB Process data asynchronously. + * + * @return Return JS data successfully, otherwise return nullptr. + */ +napi_value DisConnectAbilityWrap(napi_env env, napi_callback_info info, ConnectAbilityCB *connectAbilityCB) +{ + HILOG_INFO("%{public}s called.", __func__); + size_t argcAsync = ARGS_TWO; + const size_t argcPromise = ARGS_ONE; + const size_t argCountWithAsync = argcPromise + ARGS_ASYNC_COUNT; + napi_value args[ARGS_MAX_COUNT] = {nullptr}; + napi_value ret = 0; + + NAPI_CALL(env, napi_get_cb_info(env, info, &argcAsync, args, nullptr, nullptr)); + if (argcAsync > argCountWithAsync || argcAsync > ARGS_MAX_COUNT) { + HILOG_ERROR("%{public}s, Wrong argument count.", __func__); + return nullptr; + } + + if (argcAsync > PARAM0) { + napi_valuetype valuetype = napi_undefined; + napi_typeof(env, args[PARAM0], &valuetype); + if (valuetype == napi_function) { + NAPI_CALL(env, + napi_create_reference(env, args[PARAM0], 1, &connectAbilityCB->abilityConnectionCB.cbInfo.callback)); + } + } + + if (argcAsync > argcPromise) { + ret = DisConnectAbilityAsync(env, args, argcAsync, argcPromise, connectAbilityCB); + } else { + ret = DisConnectAbilityPromise(env, connectAbilityCB); + } + + return ret; +} + +/** + * @brief DisConnectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_DisConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType) +{ + HILOG_INFO("%{public}s called.", __func__); + ConnectAbilityCB *connectAbilityCB = CreateConnectAbilityCBInfo(env); + if (connectAbilityCB == nullptr) { + return WrapVoidToJS(env); + } + + connectAbilityCB->errCode = NAPI_ERR_NO_ERROR; + connectAbilityCB->cbBase.abilityType = abilityType; + napi_value ret = DisConnectAbilityWrap(env, info, connectAbilityCB); + if (ret == nullptr) { + if (connectAbilityCB != nullptr) { + delete connectAbilityCB; + connectAbilityCB = nullptr; + } + ret = WrapVoidToJS(env); + } + return ret; +} + +void NAPIAbilityConnection::SetEnv(const napi_env &env) +{ + env_ = env; +} + +void NAPIAbilityConnection::SetCallbackRef(const napi_ref &ref) +{ + ref_ = ref; +} + +void NAPIAbilityConnection::OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) +{ + HILOG_INFO("%{public}s, called.", __func__); + + uv_loop_s *loop = nullptr; + +#if NAPI_VERSION >= 2 + napi_get_uv_event_loop(env_, &loop); +#endif // NAPI_VERSION >= 2 + + uv_work_t *work = new uv_work_t; + ConnectAbilityCB *connectAbilityCB = + new (std::nothrow) ConnectAbilityCB{.cbBase.cbInfo.env = env_, .cbBase.cbInfo.callback = ref_}; + connectAbilityCB->abilityConnectionCB.elementName = element; + connectAbilityCB->abilityConnectionCB.resultCode = resultCode; + work->data = (void *)connectAbilityCB; + + uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + HILOG_INFO("OnAbilityConnectDone, uv_queue_work"); + // JS Thread + ConnectAbilityCB *event = (ConnectAbilityCB *)work->data; + napi_value proValue = nullptr; + napi_value result[ARGS_TWO] = {0}; + result[PARAM0] = GetCallbackErrorValue(event->cbBase.cbInfo.env, NO_ERROR); + + napi_create_object(event->cbBase.cbInfo.env, &result[PARAM1]); + napi_value jsElementName = + WrapElementName(event->cbBase.cbInfo.env, event->abilityConnectionCB.elementName); + napi_set_named_property(event->cbBase.cbInfo.env, result[PARAM1], "element", jsElementName); +#if NAPI_OHOS_RPC + napi_value jsRemoteObject = + NAPI_ohos_rpc_CreateJsRemoteObject(event->cbBase.cbInfo.env, event->abilityConnectionCB.connection); + napi_set_named_property(event->cbBase.cbInfo.env, result[PARAM1], "remote", jsRemoteObject); +#endif + napi_create_int32(event->cbBase.cbInfo.env, event->abilityConnectionCB.resultCode, &proValue); + napi_set_named_property(event->cbBase.cbInfo.env, result[PARAM1], "code", proValue); + + napi_value callback = 0; + napi_value undefined = 0; + napi_get_undefined(event->cbBase.cbInfo.env, &undefined); + napi_value callResult = 0; + napi_get_reference_value(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback, &callback); + + napi_call_function(event->cbBase.cbInfo.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + if (event->cbBase.cbInfo.callback != nullptr) { + napi_delete_reference(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback); + } + delete event; + delete work; + }); +} + +void NAPIAbilityConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + HILOG_INFO("%{public}s, called.", __func__); + + uv_loop_s *loop = nullptr; + +#if NAPI_VERSION >= 2 + napi_get_uv_event_loop(env_, &loop); +#endif // NAPI_VERSION >= 2 + + uv_work_t *work = new uv_work_t; + ConnectAbilityCB *connectAbilityCB = + new (std::nothrow) ConnectAbilityCB{.cbBase.cbInfo.env = env_, .cbBase.cbInfo.callback = ref_}; + connectAbilityCB->abilityConnectionCB.elementName = element; + connectAbilityCB->abilityConnectionCB.resultCode = resultCode; + work->data = (void *)connectAbilityCB; + + uv_queue_work( + loop, + work, + [](uv_work_t *work) {}, + [](uv_work_t *work, int status) { + HILOG_INFO("OnAbilityDisconnectDone, uv_queue_work"); + // JS Thread + ConnectAbilityCB *event = (ConnectAbilityCB *)work->data; + napi_value proValue = nullptr; + napi_value result[ARGS_TWO] = {0}; + result[PARAM0] = GetCallbackErrorValue(event->cbBase.cbInfo.env, NO_ERROR); + + napi_create_object(event->cbBase.cbInfo.env, &result[PARAM1]); + napi_value jsElementName = + WrapElementName(event->cbBase.cbInfo.env, event->abilityConnectionCB.elementName); + napi_set_named_property(event->cbBase.cbInfo.env, result[PARAM1], "element", jsElementName); + napi_create_int32(event->cbBase.cbInfo.env, event->abilityConnectionCB.resultCode, &proValue); + napi_set_named_property(event->cbBase.cbInfo.env, result[PARAM1], "code", proValue); + + napi_value callback = 0; + napi_value undefined = 0; + napi_get_undefined(event->cbBase.cbInfo.env, &undefined); + napi_value callResult = 0; + napi_get_reference_value(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback, &callback); + + napi_call_function(event->cbBase.cbInfo.env, undefined, callback, ARGS_TWO, &result[PARAM0], &callResult); + if (event->cbBase.cbInfo.callback != nullptr) { + napi_delete_reference(event->cbBase.cbInfo.env, event->cbBase.cbInfo.callback); + } + delete event; + delete work; + }); +} + +} // namespace AppExecFwk +} // namespace OHOS diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h new file mode 100755 index 00000000000..ffa6794ec11 --- /dev/null +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_ability.h @@ -0,0 +1,209 @@ +/* + * 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. + */ + +#ifndef OHOS_APPEXECFWK_NAPI_COMMON_ABILITY_H +#define OHOS_APPEXECFWK_NAPI_COMMON_ABILITY_H +#include "feature_ability_common.h" +#include "ability_info.h" +#include "ability_connect_callback_stub.h" + +namespace OHOS { +namespace AppExecFwk { +napi_value *GetGlobalClassContext(void); +void SaveAppInfo(AppInfo_ &appInfo, const ApplicationInfo &appInfoOrg); +napi_value WrapAppInfo(napi_env env, const AppInfo_ &appInfo); +/** + * @brief Obtains information about the current application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetApplicationInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Get bundle name. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetBundleNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the process Info this application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetProcessInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the type of this application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAppTypeCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the elementName object of the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetElementNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains information about the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAbilityInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the HapModuleInfo object of the application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetHapModuleInfoCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the name of the current process. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetProcessNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the bundle name of the ability that called the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetCallingBundleCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Create asynchronous data. + * + * @param env The environment that the Node-API call is invoked under. + * + * @return Return a pointer to AsyncCallbackInfo on success, nullptr on failure + */ +AsyncCallbackInfo *CreateAsyncCallbackInfo(napi_env env); +/** + * @brief Get context. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetContextCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Get want. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetWantCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief Obtains the class name in this ability name, without the prefixed bundle name. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_GetAbilityNameCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief startAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_StartAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief stopAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_StopAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +class NAPIAbilityConnection : public AAFwk::AbilityConnectionStub { +public: + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; + void SetEnv(const napi_env &env); + void SetCallbackRef(const napi_ref &ref); + +private: + napi_env env_; + napi_ref ref_; +}; + +/** + * @brief connectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_ConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); + +/** + * @brief disconnectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_DisConnectAbilityCommon(napi_env env, napi_callback_info info, AbilityType abilityType); +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_APPEXECFWK_NAPI_COMMON_ABILITY_H diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_data.h b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_data.h index d6acdb2e489..125dfa2852a 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_data.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_data.h @@ -62,10 +62,12 @@ struct CallbackInfo { struct CallAbilityParamData { PacMap paramArgs; + Want want; }; typedef enum { NVT_NONE = 0, + NVT_UNDEFINED, NVT_INT32, NVT_BOOL, NVT_STRING, @@ -87,6 +89,7 @@ typedef struct __AsyncJSCallbackInfo { napi_async_work asyncWork; napi_deferred deferred; Ability *ability; + AbilityType abilityType; CallAbilityParamData param; ThreadReturnData native_data; napi_value result; diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_error.h b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_error.h index f0efb69f97f..07f660d0b7b 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_error.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_error.h @@ -21,11 +21,12 @@ namespace AppExecFwk { #define NAPI_ERR_NO_ERROR 0 #define NAPI_ERR_NO_PERMISSION -100 -#define NAPI_ERR_INNER_DATA -101 // Internal data error, data allocation fail -#define NAPI_ERR_ACE_ABILITY -102 // Failed to acquire ability object -#define NAPI_ERR_LONG_CALLBACK -103 // Failed to acquire long callack object -#define NAPI_ERR_PARAM_INVALID -104 // Parameter is invalid - +#define NAPI_ERR_INNER_DATA -101 // Internal data error, data allocation fail +#define NAPI_ERR_ACE_ABILITY -102 // Failed to acquire ability object +#define NAPI_ERR_LONG_CALLBACK -103 // Failed to acquire long callack object +#define NAPI_ERR_PARAM_INVALID -104 // Parameter is invalid +#define NAPI_ERR_ABILITY_TYPE_INVALID -105 // Type of ability is invalid +#define NAPI_ERR_ABILITY_CALL_INVALID -106 // Call the ability interface, the return value is wrong } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_APPEXECFWK_NAPI_COMMON_ERROR_H diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp index 52be95fc89f..56ec2b67744 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.cpp @@ -16,6 +16,7 @@ #include #include "napi_common_util.h" #include "napi_common_data.h" +#include "napi_common_error.h" #include "hilog_wrapper.h" #include "securec.h" @@ -55,6 +56,13 @@ napi_value WrapVoidToJS(napi_env env) return result; } +napi_value WrapUndefinedToJS(napi_env env) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_get_undefined(env, &result)); + return result; +} + napi_value CreateJSObject(napi_env env) { napi_value result = nullptr; @@ -784,6 +792,7 @@ AsyncJSCallbackInfo *CreateAsyncJSCallbackInfo(napi_env env) .asyncWork = nullptr, .deferred = nullptr, .ability = ability, + .abilityType = AbilityType::UNKNOWN, .aceCallback = nullptr, }; if (asyncCallbackInfo != nullptr) { @@ -792,6 +801,25 @@ AsyncJSCallbackInfo *CreateAsyncJSCallbackInfo(napi_env env) return asyncCallbackInfo; } +void FreeAsyncJSCallbackInfo(AsyncJSCallbackInfo **asyncCallbackInfo) +{ + if (asyncCallbackInfo == nullptr) { + return; + } + if (*asyncCallbackInfo == nullptr) { + return; + } + + if ((*asyncCallbackInfo)->cbInfo.callback != nullptr && (*asyncCallbackInfo)->cbInfo.env != nullptr) { + napi_delete_reference((*asyncCallbackInfo)->cbInfo.env, (*asyncCallbackInfo)->cbInfo.callback); + (*asyncCallbackInfo)->cbInfo.callback = nullptr; + (*asyncCallbackInfo)->cbInfo.env = nullptr; + } + + delete (*asyncCallbackInfo); + *asyncCallbackInfo = nullptr; +} + /** * @brief Convert local data to JS data. * @@ -808,6 +836,9 @@ bool WrapThreadReturnData(napi_env env, const ThreadReturnData *data, napi_value } switch (data->data_type) { + case NVT_UNDEFINED: + NAPI_CALL_BASE(env, napi_get_undefined(env, value), false); + break; case NVT_INT32: NAPI_CALL_BASE(env, napi_create_int32(env, data->int32_value, value), false); break; @@ -996,8 +1027,13 @@ void CompletePromiseCallbackWork(napi_env env, napi_status status, void *data) } napi_value result = 0; - WrapThreadReturnData(env, &asyncCallbackInfo->native_data, &result); - napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + if (asyncCallbackInfo->error_code == NAPI_ERR_NO_ERROR) { + WrapThreadReturnData(env, &asyncCallbackInfo->native_data, &result); + napi_resolve_deferred(env, asyncCallbackInfo->deferred, result); + } else { + result = GetCallbackErrorValue(env, asyncCallbackInfo->error_code); + napi_reject_deferred(env, asyncCallbackInfo->deferred, result); + } napi_delete_async_work(env, asyncCallbackInfo->asyncWork); delete asyncCallbackInfo; asyncCallbackInfo = nullptr; diff --git a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h index 18ec2ae1a64..24dd48c904b 100755 --- a/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h +++ b/interfaces/kits/napi/aafwk/inner/napi_common/napi_common_util.h @@ -27,6 +27,7 @@ bool IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectTyp bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arraySize); napi_value WrapVoidToJS(napi_env env); +napi_value WrapUndefinedToJS(napi_env env); napi_value CreateJSObject(napi_env env); @@ -164,6 +165,7 @@ napi_value GetCallbackErrorValue(napi_env env, int errCode); * @return Return a pointer to AsyncJSCallbackInfo on success, nullptr on failure */ AsyncJSCallbackInfo *CreateAsyncJSCallbackInfo(napi_env env); +void FreeAsyncJSCallbackInfo(AsyncJSCallbackInfo **asyncCallbackInfo); /** * @brief Convert local data to JS data. diff --git a/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn new file mode 100755 index 00000000000..db9864702e1 --- /dev/null +++ b/interfaces/kits/napi/aafwk/particleAbility/BUILD.gn @@ -0,0 +1,57 @@ +# 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("//build/config/ohos/rules.gni") +import("//build/ohos.gni") +ohos_shared_library("particleability") { + include_dirs = [ + "//foundation/ace/napi/interfaces/kits", + "//third_party/node/src", + "//third_party/libuv/include", + "//foundation/aafwk/standard/frameworks/kits/ability/native/include", + "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common", + "//foundation/aafwk/standard/services/common/include", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager/include", + ] + + sources = [ + "native_module.cpp", + "particle_ability.cpp", + ] + + deps = [ + "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", + "//foundation/aafwk/standard/interfaces/innerkits/base:base", + "//foundation/aafwk/standard/interfaces/kits/napi/aafwk/inner/napi_common:napi_common", + "//foundation/ace/napi:ace_napi", + "//foundation/appexecfwk/standard/kits:appkit_native", + "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", + "//third_party/libuv:uv_static", + "//utils/native/base:utils", + ] + + external_deps = [ + "aafwk_standard:ability_manager", + "aafwk_standard:want", + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:appexecfwk_core", + "appexecfwk_standard:libeventhandler", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + + relative_install_dir = "module/ability" + + subsystem_name = "aafwk" + part_name = "aafwk_standard" +} diff --git a/interfaces/kits/napi/aafwk/particleAbility/native_module.cpp b/interfaces/kits/napi/aafwk/particleAbility/native_module.cpp new file mode 100755 index 00000000000..2b856a4f689 --- /dev/null +++ b/interfaces/kits/napi/aafwk/particleAbility/native_module.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "particle_ability.h" +namespace OHOS { +namespace AppExecFwk { +EXTERN_C_START +/* + * The module initialization. + */ +static napi_value ParticleInit(napi_env env, napi_value exports) +{ + ParticleAbilityInit(env, exports); + return exports; +} +EXTERN_C_END + +/* + * The module definition. + */ +static napi_module _module = {.nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = ParticleInit, + .nm_modname = "ability.particleability", + .nm_priv = ((void *)0), + .reserved = {0}}; + +/* + * The module registration. + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp b/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp new file mode 100755 index 00000000000..0150609d7e2 --- /dev/null +++ b/interfaces/kits/napi/aafwk/particleAbility/particle_ability.cpp @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "particle_ability.h" +#include +#include +#include +#include "securec.h" +#include "hilog_wrapper.h" +#include "../inner/napi_common/napi_common_ability.h" + +using namespace OHOS::AAFwk; +using namespace OHOS::AppExecFwk; + +namespace OHOS { +namespace AppExecFwk { + +napi_value NAPI_PAGetApplicationInfo(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetApplicationInfoCommon(env, info, AbilityType::UNKNOWN); +} + +napi_value NAPI_PAGetBundleName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetBundleNameCommon(env, info, AbilityType::UNKNOWN); +} + +napi_value NAPI_PAGetProcessInfo(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetProcessInfoCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains the type of this application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetAppType(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetAppTypeCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains the elementName object of the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetElementName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetElementNameCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains information about the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetAbilityInfo(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetAbilityInfoCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains the HapModuleInfo object of the application. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetHapModuleInfo(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetHapModuleInfoCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains the name of the current process. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAProcessName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetProcessNameCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains the bundle name of the ability that called the current ability. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetCallingBundle(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetCallingBundleCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Get context. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetContext(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetContextCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Get want. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetWant(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetWantCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief Obtains the class name in this ability name, without the prefixed bundle name. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAGetAbilityName(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_GetAbilityNameCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief ParticleAbility NAPI method : startAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAStartAbility(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_StartAbilityCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief ParticleAbility NAPI method : stopAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAStopAbility(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_StopAbilityCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief ParticleAbility NAPI method : connectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PAConnectAbility(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_ConnectAbilityCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief ParticleAbility NAPI method : disconnectAbility. + * + * @param env The environment that the Node-API call is invoked under. + * @param info The callback info passed into the callback function. + * + * @return The return value from NAPI C++ to JS for the module. + */ +napi_value NAPI_PADisConnectAbility(napi_env env, napi_callback_info info) +{ + HILOG_INFO("%{public}s called.", __func__); + return NAPI_DisConnectAbilityCommon(env, info, AbilityType::UNKNOWN); +} + +/** + * @brief ParticleAbility NAPI module registration. + * + * @param env The environment that the Node-API call is invoked under. + * @param exports An empty object via the exports parameter as a convenience. + * + * @return The return value from Init is treated as the exports object for the module. + */ +napi_value ParticleAbilityInit(napi_env env, napi_value exports) +{ + HILOG_INFO("%{public}s called.", __func__); + napi_property_descriptor properties[] = { + DECLARE_NAPI_FUNCTION("getApplicationInfo", NAPI_PAGetApplicationInfo), + DECLARE_NAPI_FUNCTION("getBundleName", NAPI_PAGetBundleName), + DECLARE_NAPI_FUNCTION("getProcessInfo", NAPI_PAGetProcessInfo), + DECLARE_NAPI_FUNCTION("getAppType", NAPI_PAGetAppType), + DECLARE_NAPI_FUNCTION("getElementName", NAPI_PAGetElementName), + DECLARE_NAPI_FUNCTION("getAbilityInfo", NAPI_PAGetAbilityInfo), + DECLARE_NAPI_FUNCTION("getHapModuleInfo", NAPI_PAGetHapModuleInfo), + DECLARE_NAPI_FUNCTION("getProcessName", NAPI_PAProcessName), + DECLARE_NAPI_FUNCTION("getCallingBundle", NAPI_PAGetCallingBundle), + DECLARE_NAPI_FUNCTION("getContext", NAPI_PAGetContext), + DECLARE_NAPI_FUNCTION("getWant", NAPI_PAGetWant), + DECLARE_NAPI_FUNCTION("getAbilityName", NAPI_PAGetAbilityName), + DECLARE_NAPI_FUNCTION("startAbility", NAPI_PAStartAbility), + DECLARE_NAPI_FUNCTION("stopAbility", NAPI_PAStopAbility), + DECLARE_NAPI_FUNCTION("connectAbility", NAPI_PAConnectAbility), + DECLARE_NAPI_FUNCTION("disconnectAbility", NAPI_PADisConnectAbility), + }; + napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties); + + return exports; +} + +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/aafwk/particleAbility/particle_ability.h b/interfaces/kits/napi/aafwk/particleAbility/particle_ability.h new file mode 100755 index 00000000000..0ee568145ac --- /dev/null +++ b/interfaces/kits/napi/aafwk/particleAbility/particle_ability.h @@ -0,0 +1,36 @@ +/* + * 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. + */ + +#ifndef OHOS_APPEXECFWK_PARTICLE_ABILITY_H +#define OHOS_APPEXECFWK_PARTICLE_ABILITY_H + +#include "napi/native_common.h" +#include "napi/native_node_api.h" + +namespace OHOS { +namespace AppExecFwk { +/** + * @brief ParticleAbility NAPI module registration. + * + * @param env The environment that the Node-API call is invoked under. + * @param exports An empty object via the exports parameter as a convenience. + * + * @return The return value from Init is treated as the exports object for the module. + */ +napi_value ParticleAbilityInit(napi_env env, napi_value exports); + +} // namespace AppExecFwk +} // namespace OHOS +#endif /* OHOS_APPEXECFWK_PARTICLE_ABILITY_H */ diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 5120e9c3799..21802557ac1 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -56,4 +56,5 @@ abilityms_files = [ "${services_path}/abilitymgr/src/pending_want_manager.cpp", "${services_path}/abilitymgr/src/pending_want_common_event.cpp", "${services_path}/abilitymgr/src/lock_mission_container.cpp", + "${services_path}/abilitymgr/src/global_configuration.cpp", ] diff --git a/services/abilitymgr/include/ability_manager_proxy.h b/services/abilitymgr/include/ability_manager_proxy.h index c67fa7582de..123f1975460 100644 --- a/services/abilitymgr/include/ability_manager_proxy.h +++ b/services/abilitymgr/include/ability_manager_proxy.h @@ -339,6 +339,14 @@ public: */ virtual int GetMissionLockModeState() override; + /** + * Updates the configuration by modifying the configuration. + * + * @param config Indicates the new configuration + * @return Returns ERR_OK on success, others on failure. + */ + virtual int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) override; + virtual sptr GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken) override; diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index b44deaff6de..a62dad43963 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -383,6 +383,14 @@ public: */ virtual int GetMissionLockModeState() override; + /** + * Updates the configuration by modifying the configuration. + * + * @param config Indicates the new configuration + * @return Returns ERR_OK on success, others on failure. + */ + virtual int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) override; + /** * remove all service record. * diff --git a/services/abilitymgr/include/ability_manager_stub.h b/services/abilitymgr/include/ability_manager_stub.h index 52a154b760e..d3ceb443b7f 100644 --- a/services/abilitymgr/include/ability_manager_stub.h +++ b/services/abilitymgr/include/ability_manager_stub.h @@ -71,6 +71,7 @@ private: int UnlockMissionInner(MessageParcel &data, MessageParcel &reply); int SetMissionDescriptionInfoInner(MessageParcel &data, MessageParcel &reply); int GetMissionLockModeStateInner(MessageParcel &data, MessageParcel &reply); + int UpdateConfigurationInner(MessageParcel &data, MessageParcel &reply); int GetWantSenderInner(MessageParcel &data, MessageParcel &reply); int SendWantSenderInner(MessageParcel &data, MessageParcel &reply); diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index 71c3d7a05a6..a42565d4776 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -409,9 +409,24 @@ public: */ void CommandAbility(); + /** + * save ability state. + * + */ void SaveAbilityState(); + + /** + * restore ability state. + * + */ void RestoreAbilityState(); + /** + * notify ability configuration updated. + * + */ + void UpdateConfiguration(const GlobalConfiguration &config); + /** * set the want for start ability. * @@ -609,6 +624,9 @@ public: void SetPowerState(const bool isPower); bool GetPowerState() const; + void SetRestarting(const bool isRestart); + bool IsRestarting() const; + private: /** * get system time. @@ -677,6 +695,9 @@ private: bool isKernalSystemAbility = false; bool isLauncherRoot_ = false; bool isPowerState_ = false; // ability to change state when poweroff and poweron. + + PacMap stateDatas_; // ability saved ability state data + bool isRestarting_ = false; // is restarting ? }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/include/ability_scheduler_proxy.h b/services/abilitymgr/include/ability_scheduler_proxy.h index 67156c51e63..24697322130 100644 --- a/services/abilitymgr/include/ability_scheduler_proxy.h +++ b/services/abilitymgr/include/ability_scheduler_proxy.h @@ -78,6 +78,8 @@ public: */ void ScheduleRestoreAbilityState(const PacMap &inState) override; + void ScheduleUpdateConfiguration(const GlobalConfiguration &config) override; + /** * @brief Obtains the MIME types of files supported. * diff --git a/services/abilitymgr/include/ability_scheduler_stub.h b/services/abilitymgr/include/ability_scheduler_stub.h index 50193495c08..42e2cf6a1e2 100644 --- a/services/abilitymgr/include/ability_scheduler_stub.h +++ b/services/abilitymgr/include/ability_scheduler_stub.h @@ -54,6 +54,7 @@ private: int GetTypeInner(MessageParcel &data, MessageParcel &reply); int ReloadInner(MessageParcel &data, MessageParcel &reply); int BatchInsertInner(MessageParcel &data, MessageParcel &reply); + int UpdateConfigurationInner(MessageParcel &data, MessageParcel &reply); using RequestFuncType = int (AbilitySchedulerStub::*)(MessageParcel &data, MessageParcel &reply); std::map requestFuncMap_; }; diff --git a/services/abilitymgr/include/ability_stack_manager.h b/services/abilitymgr/include/ability_stack_manager.h index 2f9a451ac01..f7e989502d2 100644 --- a/services/abilitymgr/include/ability_stack_manager.h +++ b/services/abilitymgr/include/ability_stack_manager.h @@ -25,6 +25,7 @@ #include "ability_info.h" #include "ability_record.h" #include "application_info.h" +#include "global_configuration.h" #include "mission_record.h" #include "mission_stack.h" #include "ability_mission_info.h" @@ -328,6 +329,12 @@ public: const std::shared_ptr &abilityRecord, const MissionDescriptionInfo &missionDescriptionInfo); int GetMissionLockModeState(); + /** + * update configuration to ability + * @return Returns ERR_OK on success, others on failure. + */ + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType); + private: /** * dispatch ability life cycle . @@ -533,6 +540,8 @@ private: bool CanStopInLockMissionState(const std::shared_ptr &terminateAbility) const; void SendUnlockMissionMessage(); + int RestartAbility(std::shared_ptr &abilityRecord); + private: const std::string MISSION_NAME_MARK_HEAD = "#"; const std::string MISSION_NAME_SEPARATOR = ":"; diff --git a/services/abilitymgr/include/lifecycle_deal.h b/services/abilitymgr/include/lifecycle_deal.h index b5b418eef10..8ae43705f89 100644 --- a/services/abilitymgr/include/lifecycle_deal.h +++ b/services/abilitymgr/include/lifecycle_deal.h @@ -51,6 +51,9 @@ public: void DisconnectAbility(const Want &want); void Terminate(const Want &want, LifeCycleStateInfo &stateInfo); void CommandAbility(const Want &want, bool reStart, int startId); + void SaveAbilityState(PacMap &outState); + void RestoreAbilityState(const PacMap &inState); + void UpdateConfiguration(const GlobalConfiguration &config); private: sptr abilityScheduler_; // kit interface used to schedule ability life diff --git a/services/abilitymgr/include/pending_want_manager.h b/services/abilitymgr/include/pending_want_manager.h index 97e9fa9bb49..42750914381 100644 --- a/services/abilitymgr/include/pending_want_manager.h +++ b/services/abilitymgr/include/pending_want_manager.h @@ -133,10 +133,11 @@ public: virtual ~PendingWantManager(); public: - sptr GetWantSender(const int32_t callingUid, const int32_t uid, const WantSenderInfo &wantSenderInfo, - const sptr &callerToken); + sptr GetWantSender(const int32_t callingUid, const int32_t uid, const bool isSystemApp, + const WantSenderInfo &wantSenderInfo, const sptr &callerToken); int32_t SendWantSender(const sptr &target, const SenderInfo &senderInfo); - void CancelWantSender(const int32_t callingUid, const int32_t uid, const sptr &sender); + void CancelWantSender( + const int32_t callingUid, const int32_t uid, const bool isSystemApp, const sptr &sender); int32_t GetPendingWantUid(const sptr &target); int32_t GetPendingWantUserId(const sptr &target); diff --git a/services/abilitymgr/src/ability_manager_client.cpp b/services/abilitymgr/src/ability_manager_client.cpp index 54ea6298390..9cd728a7946 100644 --- a/services/abilitymgr/src/ability_manager_client.cpp +++ b/services/abilitymgr/src/ability_manager_client.cpp @@ -29,6 +29,22 @@ namespace AAFwk { std::shared_ptr AbilityManagerClient::instance_ = nullptr; std::mutex AbilityManagerClient::mutex_; +#define CHECK_REMOTE_OBJECT(object) \ + if (!object) { \ + if (ERR_OK != Connect()) { \ + HILOG_ERROR("ability service can't connect."); \ + return; \ + } \ + } + +#define CHECK_REMOTE_OBJECT_AND_RETURN(object, value) \ + if (!object) { \ + if (ERR_OK != Connect()) { \ + HILOG_ERROR("ability service can't connect."); \ + return value; \ + } \ + } + std::shared_ptr AbilityManagerClient::GetInstance() { if (instance_ == nullptr) { @@ -49,22 +65,14 @@ AbilityManagerClient::~AbilityManagerClient() ErrCode AbilityManagerClient::AttachAbilityThread( const sptr &scheduler, const sptr &token) { - if (remoteObject_ == nullptr) { - ErrCode err = Connect(); - if (err != ERR_OK) { - return ABILITY_SERVICE_NOT_CONNECTED; - } - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->AttachAbilityThread(scheduler, token); } ErrCode AbilityManagerClient::AbilityTransitionDone(const sptr &token, int state) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->AbilityTransitionDone(token, state); } @@ -72,90 +80,63 @@ ErrCode AbilityManagerClient::AbilityTransitionDone(const sptr &t ErrCode AbilityManagerClient::ScheduleConnectAbilityDone( const sptr &token, const sptr &remoteObject) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->ScheduleConnectAbilityDone(token, remoteObject); } ErrCode AbilityManagerClient::ScheduleDisconnectAbilityDone(const sptr &token) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->ScheduleDisconnectAbilityDone(token); } ErrCode AbilityManagerClient::ScheduleCommandAbilityDone(const sptr &token) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not command", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->ScheduleCommandAbilityDone(token); } void AbilityManagerClient::AddWindowInfo(const sptr &token, int32_t windowToken) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return; - } + CHECK_REMOTE_OBJECT(remoteObject_); sptr abms = iface_cast(remoteObject_); abms->AddWindowInfo(token, windowToken); } ErrCode AbilityManagerClient::StartAbility(const Want &want, int requestCode) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->StartAbility(want, requestCode); } ErrCode AbilityManagerClient::StartAbility(const Want &want, const sptr &callerToken, int requestCode) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->StartAbility(want, callerToken, requestCode); } ErrCode AbilityManagerClient::TerminateAbility(const sptr &token, int resultCode, const Want *resultWant) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->TerminateAbility(token, resultCode, resultWant); } ErrCode AbilityManagerClient::TerminateAbility(const sptr &callerToken, int requestCode) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->TerminateAbility(callerToken, requestCode); } ErrCode AbilityManagerClient::TerminateAbilityResult(const sptr &token, int startId) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->TerminateAbilityResult(token, startId); } @@ -163,20 +144,14 @@ ErrCode AbilityManagerClient::TerminateAbilityResult(const sptr & ErrCode AbilityManagerClient::ConnectAbility( const Want &want, const sptr &connect, const sptr &callerToken) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->ConnectAbility(want, connect, callerToken); } ErrCode AbilityManagerClient::DisconnectAbility(const sptr &connect) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->DisconnectAbility(connect); } @@ -184,10 +159,7 @@ ErrCode AbilityManagerClient::DisconnectAbility(const sptr & sptr AbilityManagerClient::AcquireDataAbility( const Uri &uri, bool tryBind, const sptr &callerToken) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return nullptr; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, nullptr); sptr abms = iface_cast(remoteObject_); return abms->AcquireDataAbility(uri, tryBind, callerToken); } @@ -195,20 +167,14 @@ sptr AbilityManagerClient::AcquireDataAbility( ErrCode AbilityManagerClient::ReleaseDataAbility( sptr dataAbilityScheduler, const sptr &callerToken) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->ReleaseDataAbility(dataAbilityScheduler, callerToken); } ErrCode AbilityManagerClient::DumpState(const std::string &args, std::vector &state) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); abms->DumpState(args, state); return ERR_OK; @@ -236,21 +202,14 @@ ErrCode AbilityManagerClient::Connect() ErrCode AbilityManagerClient::GetAllStackInfo(StackInfo &stackInfo) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } - + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->GetAllStackInfo(stackInfo); } ErrCode AbilityManagerClient::StopServiceAbility(const Want &want) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->StopServiceAbility(want); } @@ -258,51 +217,35 @@ ErrCode AbilityManagerClient::StopServiceAbility(const Want &want) ErrCode AbilityManagerClient::GetRecentMissions( const int32_t numMax, const int32_t flags, std::vector &recentList) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->GetRecentMissions(numMax, flags, recentList); } ErrCode AbilityManagerClient::GetMissionSnapshot(const int32_t missionId, MissionSnapshotInfo &snapshot) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->GetMissionSnapshot(missionId, snapshot); } ErrCode AbilityManagerClient::MoveMissionToTop(int32_t missionId) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->MoveMissionToTop(missionId); } ErrCode AbilityManagerClient::MoveMissionToEnd(const sptr &token, const bool nonFirst) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->MoveMissionToEnd(token, nonFirst); } ErrCode AbilityManagerClient::RemoveMissions(std::vector missionId) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } - + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); int error = ERR_OK; for (auto it : missionId) { @@ -318,20 +261,14 @@ ErrCode AbilityManagerClient::RemoveMissions(std::vector missionId) ErrCode AbilityManagerClient::RemoveStack(int id) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->RemoveStack(id); } ErrCode AbilityManagerClient::KillProcess(const std::string &bundleName) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->KillProcess(bundleName); } @@ -342,10 +279,7 @@ ErrCode AbilityManagerClient::IsFirstInMission(const sptr &token) HILOG_ERROR("%{private}s:ability service not connect", __func__); return ERR_NULL_OBJECT; } - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); if (!(abms->IsFirstInMission(token))) { return NO_FIRST_IN_MISSION; @@ -356,50 +290,35 @@ ErrCode AbilityManagerClient::IsFirstInMission(const sptr &token) ErrCode AbilityManagerClient::CompelVerifyPermission( const std::string &permission, int pid, int uid, std::string &message) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->CompelVerifyPermission(permission, pid, uid, message); } ErrCode AbilityManagerClient::PowerOff() { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->PowerOff(); } ErrCode AbilityManagerClient::PowerOn() { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->PowerOn(); } ErrCode AbilityManagerClient::LockMission(int missionId) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->LockMission(missionId); } ErrCode AbilityManagerClient::UnlockMission(int missionId) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->UnlockMission(missionId); } @@ -407,69 +326,50 @@ ErrCode AbilityManagerClient::UnlockMission(int missionId) ErrCode AbilityManagerClient::SetMissionDescriptionInfo( const sptr &token, const MissionDescriptionInfo &missionDescriptionInfo) { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->SetMissionDescriptionInfo(token, missionDescriptionInfo); } -int AbilityManagerClient::GetMissionLockModeState() +ErrCode AbilityManagerClient::GetMissionLockModeState() { - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->GetMissionLockModeState(); } +ErrCode AbilityManagerClient::UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) +{ + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); + sptr abms = iface_cast(remoteObject_); + return abms->UpdateConfiguration(config, changeType); +} + sptr AbilityManagerClient::GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return nullptr; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, nullptr); sptr abms = iface_cast(remoteObject_); return abms->GetWantSender(wantSenderInfo, callerToken); } ErrCode AbilityManagerClient::SendWantSender(const sptr &target, const SenderInfo &senderInfo) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); sptr abms = iface_cast(remoteObject_); return abms->SendWantSender(target, senderInfo); } void AbilityManagerClient::CancelWantSender(const sptr &sender) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return; - } + CHECK_REMOTE_OBJECT(remoteObject_); sptr abms = iface_cast(remoteObject_); abms->CancelWantSender(sender); } ErrCode AbilityManagerClient::GetPendingWantUid(const sptr &target, int32_t &uid) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); if (target == nullptr) { HILOG_ERROR("%{private}s:target is nullptr", __func__); return ABILITY_SERVICE_NOT_CONNECTED; @@ -481,12 +381,7 @@ ErrCode AbilityManagerClient::GetPendingWantUid(const sptr &target, ErrCode AbilityManagerClient::GetPendingWantUserId(const sptr &target, int32_t &userId) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); if (target == nullptr) { HILOG_ERROR("%{private}s:target is nullptr", __func__); return ABILITY_SERVICE_NOT_CONNECTED; @@ -498,12 +393,7 @@ ErrCode AbilityManagerClient::GetPendingWantUserId(const sptr &targ ErrCode AbilityManagerClient::GetPendingWantBundleName(const sptr &target, std::string &bundleName) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); if (target == nullptr) { HILOG_ERROR("%{private}s:target is nullptr", __func__); return ABILITY_SERVICE_NOT_CONNECTED; @@ -515,12 +405,7 @@ ErrCode AbilityManagerClient::GetPendingWantBundleName(const sptr & ErrCode AbilityManagerClient::GetPendingWantCode(const sptr &target, int32_t &code) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); if (target == nullptr) { HILOG_ERROR("%{private}s:target is nullptr", __func__); return ABILITY_SERVICE_NOT_CONNECTED; @@ -532,29 +417,20 @@ ErrCode AbilityManagerClient::GetPendingWantCode(const sptr &target ErrCode AbilityManagerClient::GetPendingWantType(const sptr &target, int32_t &type) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); if (target == nullptr) { HILOG_ERROR("%{private}s:target is nullptr", __func__); return ABILITY_SERVICE_NOT_CONNECTED; } sptr abms = iface_cast(remoteObject_); type = abms->GetPendingWantType(target); + type < 0 ? type = 0 : type; return ERR_OK; } void AbilityManagerClient::RegisterCancelListener(const sptr &sender, const sptr &recevier) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return; - } + CHECK_REMOTE_OBJECT(remoteObject_); if (sender == nullptr) { HILOG_ERROR("%{private}s:sender is nullptr", __func__); return; @@ -570,12 +446,7 @@ void AbilityManagerClient::RegisterCancelListener(const sptr &sende void AbilityManagerClient::UnregisterCancelListener( const sptr &sender, const sptr &recevier) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return; - } + CHECK_REMOTE_OBJECT(remoteObject_); if (sender == nullptr) { HILOG_ERROR("%{private}s:sender is nullptr", __func__); return; @@ -590,12 +461,7 @@ void AbilityManagerClient::UnregisterCancelListener( ErrCode AbilityManagerClient::GetPendingRequestWant(const sptr &target, std::shared_ptr &want) { - HILOG_INFO("%{public}s:begin.", __func__); - - if (remoteObject_ == nullptr) { - HILOG_ERROR("%{private}s:ability service not connect", __func__); - return ABILITY_SERVICE_NOT_CONNECTED; - } + CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED); if (target == nullptr) { HILOG_ERROR("%{private}s:target is nullptr", __func__); return ABILITY_SERVICE_NOT_CONNECTED; diff --git a/services/abilitymgr/src/ability_manager_proxy.cpp b/services/abilitymgr/src/ability_manager_proxy.cpp index dca43305a1c..926841b44fe 100644 --- a/services/abilitymgr/src/ability_manager_proxy.cpp +++ b/services/abilitymgr/src/ability_manager_proxy.cpp @@ -863,6 +863,28 @@ int AbilityManagerProxy::GetMissionLockModeState() return reply.ReadInt32(); } +int AbilityManagerProxy::UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) +{ + int error; + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!WriteInterfaceToken(data)) { + return INNER_ERR; + } + if (!data.WriteParcelable(&config)) { + return INNER_ERR; + } + data.WriteString16(Str8ToStr16(changeType)); + error = Remote()->SendRequest(IAbilityManager::UPDATE_CONFIGURATION, data, reply, option); + if (error != NO_ERROR) { + HILOG_ERROR("update configuration, error: %d", error); + return error; + } + return reply.ReadInt32(); +} + sptr AbilityManagerProxy::GetWantSender( const WantSenderInfo &wantSenderInfo, const sptr &callerToken) { diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 09b92eebb60..6dcd557675c 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -316,6 +316,13 @@ int AbilityManagerService::GetMissionLockModeState() return currentStackManager_->GetMissionLockModeState(); } +int AbilityManagerService::UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) +{ + HILOG_INFO("%{public}s called", __func__); + CHECK_POINTER_AND_RETURN(currentStackManager_, ERR_INVALID_VALUE); + return currentStackManager_->UpdateConfiguration(config, changeType); +} + int AbilityManagerService::MoveMissionToTop(int32_t missionId) { HILOG_INFO("%{public}s mission id: %d", __func__, missionId); @@ -422,7 +429,8 @@ sptr AbilityManagerService::GetWantSender( } HILOG_INFO("AbilityManagerService::GetWantSender: bundleName = %{public}s", wantSenderInfo.bundleName.c_str()); - return pendingWantManager_->GetWantSender(callerUid, bundleInfo.uid, wantSenderInfo, callerToken); + return pendingWantManager_->GetWantSender( + callerUid, bundleInfo.uid, bundleInfo.applicationInfo.isSystemApp, wantSenderInfo, callerToken); } int AbilityManagerService::SendWantSender(const sptr &target, const SenderInfo &senderInfo) @@ -469,7 +477,7 @@ void AbilityManagerService::CancelWantSender(const sptr &sender) return; } - pendingWantManager_->CancelWantSender(callerUid, bundleInfo.uid, sender); + pendingWantManager_->CancelWantSender(callerUid, bundleInfo.uid, bundleInfo.applicationInfo.isSystemApp, sender); } int AbilityManagerService::GetPendingWantUid(const sptr &target) @@ -478,11 +486,11 @@ int AbilityManagerService::GetPendingWantUid(const sptr &target) if (pendingWantManager_ == nullptr) { HILOG_ERROR("%s, pendingWantManager_ is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } if (target == nullptr) { HILOG_ERROR("%s, target is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } return pendingWantManager_->GetPendingWantUid(target); } @@ -493,11 +501,11 @@ int AbilityManagerService::GetPendingWantUserId(const sptr &target) if (pendingWantManager_ == nullptr) { HILOG_ERROR("%s, pendingWantManager_ is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } if (target == nullptr) { HILOG_ERROR("%s, target is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } return pendingWantManager_->GetPendingWantUserId(target); } @@ -523,11 +531,11 @@ int AbilityManagerService::GetPendingWantCode(const sptr &target) if (pendingWantManager_ == nullptr) { HILOG_ERROR("%s, pendingWantManager_ is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } if (target == nullptr) { HILOG_ERROR("%s, target is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } return pendingWantManager_->GetPendingWantCode(target); } @@ -538,11 +546,11 @@ int AbilityManagerService::GetPendingWantType(const sptr &target) if (pendingWantManager_ == nullptr) { HILOG_ERROR("%s, pendingWantManager_ is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } if (target == nullptr) { HILOG_ERROR("%s, target is nullptr", __func__); - return ERR_INVALID_VALUE; + return -1; } return pendingWantManager_->GetPendingWantType(target); } diff --git a/services/abilitymgr/src/ability_manager_stub.cpp b/services/abilitymgr/src/ability_manager_stub.cpp index 9281226edb7..6dcdce5e0f2 100644 --- a/services/abilitymgr/src/ability_manager_stub.cpp +++ b/services/abilitymgr/src/ability_manager_stub.cpp @@ -74,6 +74,7 @@ AbilityManagerStub::AbilityManagerStub() requestFuncMap_[GET_PENDING_REQUEST_WANT] = &AbilityManagerStub::GetPendingRequestWantInner; requestFuncMap_[SET_MISSION_INFO] = &AbilityManagerStub::SetMissionDescriptionInfoInner; requestFuncMap_[GET_MISSION_LOCK_MODE_STATE] = &AbilityManagerStub::GetMissionLockModeStateInner; + requestFuncMap_[UPDATE_CONFIGURATION] = &AbilityManagerStub::UpdateConfigurationInner; } AbilityManagerStub::~AbilityManagerStub() @@ -490,6 +491,22 @@ int AbilityManagerStub::GetMissionLockModeStateInner(MessageParcel &data, Messag return NO_ERROR; } +int AbilityManagerStub::UpdateConfigurationInner(MessageParcel &data, MessageParcel &reply) +{ + GlobalConfiguration *config = data.ReadParcelable(); + if (config == nullptr) { + HILOG_ERROR("AbilityManagerStub: config is nullptr"); + return ERR_INVALID_VALUE; + } + auto changeType = Str16ToStr8(data.ReadString16()); + int result = UpdateConfiguration(*config, changeType); + if (!reply.WriteInt32(result)) { + HILOG_ERROR("AbilityManagerStub: update configuration failed."); + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + int AbilityManagerStub::GetWantSenderInner(MessageParcel &data, MessageParcel &reply) { HILOG_INFO("%{public}s:begin.", __func__); diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 1240817fadd..9212df2f6a5 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -204,6 +204,14 @@ void AbilityRecord::SetScheduler(const sptr &scheduler) } else { HILOG_ERROR("scheduler is nullptr"); isReady_ = false; + isWindowAttached_ = false; + if (scheduler_ != nullptr && schedulerDeathRecipient_ != nullptr) { + auto schedulerObject = scheduler_->AsObject(); + if (schedulerObject != nullptr) { + schedulerObject->RemoveDeathRecipient(schedulerDeathRecipient_); + } + } + scheduler_ = scheduler; } } @@ -392,6 +400,12 @@ void AbilityRecord::MoveToBackground(const Closure &task) // eventId_ is a unique id of the task. handler->PostTask(task, std::to_string(eventId_), AbilityManagerService::BACKGROUND_TIMEOUT); } + + if (!IsTerminating() || IsRestarting()) { + // schedule save ability state before moving to background. + SaveAbilityState(); + } + // schedule background after updating AbilityState and sending timeout message to avoid ability async callback // earlier than above actions. currentState_ = AbilityState::MOVING_BACKGROUND; @@ -443,11 +457,22 @@ void AbilityRecord::CommandAbility() void AbilityRecord::SaveAbilityState() { HILOG_INFO("%{public}s", __func__); + CHECK_POINTER(lifecycleDeal_); + lifecycleDeal_->SaveAbilityState(stateDatas_); } void AbilityRecord::RestoreAbilityState() { HILOG_INFO("%{public}s", __func__); + lifecycleDeal_->RestoreAbilityState(stateDatas_); + stateDatas_.Clear(); + isRestarting_ = false; +} + +void AbilityRecord::UpdateConfiguration(const GlobalConfiguration &config) +{ + HILOG_INFO("%{public}s", __func__); + lifecycleDeal_->UpdateConfiguration(config); } void AbilityRecord::SetWant(const Want &want) @@ -569,6 +594,7 @@ std::list> AbilityRecord::GetCallerRecordList() co void AbilityRecord::AddWindowInfo(int windowToken) { windowInfo_ = std::make_shared(windowToken); + isWindowAttached_ = true; } void AbilityRecord::RemoveWindowInfo() @@ -788,6 +814,7 @@ void AbilityRecord::OnSchedulerDied(const wptr &remote) scheduler_.clear(); CHECK_POINTER(lifecycleDeal_); lifecycleDeal_->SetScheduler(nullptr); + isWindowAttached_ = false; auto abilityManagerService = DelayedSingleton::GetInstance(); CHECK_POINTER(abilityManagerService); @@ -874,5 +901,14 @@ bool AbilityRecord::GetPowerState() const { return isPowerState_; } + +void AbilityRecord::SetRestarting(const bool isRestart) +{ + isRestarting_ = isRestart; +} +bool AbilityRecord::IsRestarting() const +{ + return isRestarting_; +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/ability_scheduler_proxy.cpp b/services/abilitymgr/src/ability_scheduler_proxy.cpp index a744a282d40..c119f538133 100644 --- a/services/abilitymgr/src/ability_scheduler_proxy.cpp +++ b/services/abilitymgr/src/ability_scheduler_proxy.cpp @@ -135,6 +135,12 @@ void AbilitySchedulerProxy::ScheduleSaveAbilityState(PacMap &outState) if (err != NO_ERROR) { HILOG_ERROR("ScheduleSaveAbilityState fail to SendRequest. err: %d", err); } + std::unique_ptr pacMap(reply.ReadParcelable()); + if (!pacMap) { + HILOG_ERROR("readParcelableInfo failed"); + return; + } + outState = *pacMap; } void AbilitySchedulerProxy::ScheduleRestoreAbilityState(const PacMap &inState) @@ -145,12 +151,34 @@ void AbilitySchedulerProxy::ScheduleRestoreAbilityState(const PacMap &inState) if (!WriteInterfaceToken(data)) { return; } + if (!data.WriteParcelable(&inState)) { + HILOG_ERROR("WriteParcelable error"); + return; + } int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_RESTORE_ABILITY_STATE, data, reply, option); if (err != NO_ERROR) { HILOG_ERROR("ScheduleRestoreAbilityState fail to SendRequest. err: %d", err); } } +void AbilitySchedulerProxy::ScheduleUpdateConfiguration(const GlobalConfiguration &config) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!WriteInterfaceToken(data)) { + return; + } + if (!data.WriteParcelable(&config)) { + HILOG_ERROR("fail to WriteParcelable"); + return; + } + int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_UPDATE_CONFIGURATION, data, reply, option); + if (err != NO_ERROR) { + HILOG_ERROR("ScheduleRestoreAbilityState fail to SendRequest. err: %d", err); + } +} + /** * @brief Obtains the MIME types of files supported. * diff --git a/services/abilitymgr/src/ability_scheduler_stub.cpp b/services/abilitymgr/src/ability_scheduler_stub.cpp index ff5d236cb44..090ffe30c0c 100644 --- a/services/abilitymgr/src/ability_scheduler_stub.cpp +++ b/services/abilitymgr/src/ability_scheduler_stub.cpp @@ -41,6 +41,7 @@ AbilitySchedulerStub::AbilitySchedulerStub() requestFuncMap_[SCHEDULE_GETTYPE] = &AbilitySchedulerStub::GetTypeInner; requestFuncMap_[SCHEDULE_RELOAD] = &AbilitySchedulerStub::ReloadInner; requestFuncMap_[SCHEDULE_BATCHINSERT] = &AbilitySchedulerStub::BatchInsertInner; + requestFuncMap_[SCHEDULE_UPDATE_CONFIGURATION] = &AbilitySchedulerStub::UpdateConfigurationInner; } AbilitySchedulerStub::~AbilitySchedulerStub() @@ -141,12 +142,24 @@ int AbilitySchedulerStub::CommandAbilityInner(MessageParcel &data, MessageParcel int AbilitySchedulerStub::SaveAbilityStateInner(MessageParcel &data, MessageParcel &reply) { + PacMap pacMap; + ScheduleSaveAbilityState(pacMap); + if (!reply.WriteParcelable(&pacMap)) { + HILOG_ERROR("AbilityManagerStub: SaveAbilityState error"); + return ERR_INVALID_VALUE; + } return NO_ERROR; } int AbilitySchedulerStub::RestoreAbilityStateInner(MessageParcel &data, MessageParcel &reply) { - HILOG_INFO("RestoreAbilityStateInner"); + PacMap *pacMap = data.ReadParcelable(); + if (pacMap == nullptr) { + HILOG_ERROR("AbilitySchedulerStub RestoreAbilityState is nullptr"); + return ERR_INVALID_VALUE; + } + ScheduleRestoreAbilityState(*pacMap); + delete pacMap; return NO_ERROR; } @@ -385,6 +398,18 @@ int AbilitySchedulerStub::BatchInsertInner(MessageParcel &data, MessageParcel &r return NO_ERROR; } +int AbilitySchedulerStub::UpdateConfigurationInner(MessageParcel &data, MessageParcel &reply) +{ + GlobalConfiguration *globalConfiguration = data.ReadParcelable(); + if (globalConfiguration == nullptr) { + HILOG_ERROR("AbilitySchedulerStub globalConfiguration is nullptr"); + return ERR_INVALID_VALUE; + } + ScheduleUpdateConfiguration(*globalConfiguration); + delete globalConfiguration; + return NO_ERROR; +} + void AbilitySchedulerRecipient::OnRemoteDied(const wptr &remote) { HILOG_ERROR("recv AbilitySchedulerRecipient death notice"); diff --git a/services/abilitymgr/src/ability_stack_manager.cpp b/services/abilitymgr/src/ability_stack_manager.cpp index a6ad13e9efd..3961c1135ad 100644 --- a/services/abilitymgr/src/ability_stack_manager.cpp +++ b/services/abilitymgr/src/ability_stack_manager.cpp @@ -604,6 +604,9 @@ int AbilityStackManager::AttachAbilityThread(const sptr &sche handler->RemoveEvent(AbilityManagerService::LOAD_TIMEOUT_MSG, abilityRecord->GetEventId()); abilityRecord->SetScheduler(scheduler); + if (abilityRecord->IsRestarting()) { + abilityRecord->RestoreAbilityState(); + } DelayedSingleton::GetInstance()->MoveToForground(token); return ERR_OK; @@ -947,7 +950,14 @@ void AbilityStackManager::CompleteInactive(const std::shared_ptr topAbilityRecord->ProcessActivate(); return; } - // 2. it may be callback of start ability. + + // 2. it may be callback of restart ability. + if (abilityRecord->IsRestarting()) { + HILOG_INFO("%{public}s, back ability record: %{public}s", __func__, element.c_str()); + MoveToBackgroundTask(abilityRecord); + return; + } + // 3. it may be callback of start ability. // if next ability has been launched and is in bottom of mission, just resume other than loading ability. std::shared_ptr nextAbilityRecord = abilityRecord->GetNextAbilityRecord(); if (nextAbilityRecord == nullptr) { @@ -985,6 +995,14 @@ void AbilityStackManager::CompleteBackground(const std::shared_ptrTerminate(timeoutTask); } } + + if (abilityRecord->IsRestarting() && abilityRecord->IsAbilityState(AbilityState::BACKGROUND)) { + auto timeoutTask = [abilityRecord, stackManager = shared_from_this()]() { + HILOG_WARN("disconnect ability terminate timeout."); + stackManager->CompleteTerminate(abilityRecord); + }; + abilityRecord->Terminate(timeoutTask); + } } void AbilityStackManager::CompleteTerminate(const std::shared_ptr &abilityRecord) @@ -1007,6 +1025,12 @@ void AbilityStackManager::CompleteTerminate(const std::shared_ptr if (windowInfo != nullptr) { windowTokenToAbilityMap_.erase(windowInfo->windowToken_); } + + if (abilityRecord->IsRestarting()) { + abilityRecord->SetAbilityState(AbilityState::INITIAL); + abilityRecord->SetScheduler(nullptr); + abilityRecord->LoadAbility(); + } for (auto it : terminateAbilityRecordList_) { if (it == abilityRecord) { terminateAbilityRecordList_.remove(it); @@ -1385,6 +1409,39 @@ int AbilityStackManager::GetMissionLockModeState() return lockMissionContainer_->GetLockedMissionState(); } +int AbilityStackManager::UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) +{ + HILOG_INFO("%{public}s called, changeType : %{public}s", __FUNCTION__, changeType.c_str()); + std::lock_guard guard(stackLock_); + + // get top ability or active ability config.json + auto abilityRecord = GetCurrentTopAbility(); + CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); + auto abilityInfo = abilityRecord->GetAbilityInfo(); + + // check abilityInfo whether the ability focus on this changeType ? + if (std::find(abilityInfo.configChanges.begin(), abilityInfo.configChanges.end(), changeType) != + abilityInfo.configChanges.end()) { + abilityRecord->UpdateConfiguration(config); + return ERR_OK; + } + + return RestartAbility(abilityRecord); +} + +int AbilityStackManager::RestartAbility(std::shared_ptr &abilityRecord) +{ + HILOG_INFO("%{public}s called", __FUNCTION__); + CHECK_POINTER_AND_RETURN(abilityRecord, ERR_INVALID_VALUE); + if (!abilityRecord->IsAbilityState(AbilityState::ACTIVE)) { + HILOG_ERROR("target ability can't be restarted."); + return ERR_INVALID_VALUE; + } + abilityRecord->SetRestarting(true); + abilityRecord->Inactivate(); + return ERR_OK; +} + int AbilityStackManager::MoveMissionToTop(int32_t missionId) { HILOG_INFO("%{public}s,%{public}d", __PRETTY_FUNCTION__, __LINE__); diff --git a/services/abilitymgr/src/global_configuration.cpp b/services/abilitymgr/src/global_configuration.cpp new file mode 100644 index 00000000000..77b663b979c --- /dev/null +++ b/services/abilitymgr/src/global_configuration.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "global_configuration.h" + +#include "string_ex.h" + +namespace OHOS { +namespace AAFwk { + +GlobalConfiguration::GlobalConfiguration(const std::string &name) : testInfostr_(name) +{} + +bool GlobalConfiguration::ReadFromParcel(Parcel &parcel) +{ + testInfostr_ = Str16ToStr8(parcel.ReadString16()); + return true; +} + +GlobalConfiguration *GlobalConfiguration::Unmarshalling(Parcel &parcel) +{ + GlobalConfiguration *globalConfiguration = new (std::nothrow) GlobalConfiguration(); + if (globalConfiguration && !globalConfiguration->ReadFromParcel(parcel)) { + delete globalConfiguration; + globalConfiguration = nullptr; + } + return globalConfiguration; +} + +bool GlobalConfiguration::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString16(Str8ToStr16(testInfostr_))) { + return false; + } + return true; +} + +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file diff --git a/services/abilitymgr/src/lifecycle_deal.cpp b/services/abilitymgr/src/lifecycle_deal.cpp index 65d73e68cb5..8a57bbe83a2 100644 --- a/services/abilitymgr/src/lifecycle_deal.cpp +++ b/services/abilitymgr/src/lifecycle_deal.cpp @@ -88,5 +88,26 @@ void LifecycleDeal::CommandAbility(const Want &want, bool reStart, int startId) CHECK_POINTER(abilityScheduler_); abilityScheduler_->ScheduleCommandAbility(want, reStart, startId); } + +void LifecycleDeal::SaveAbilityState(PacMap &outState) +{ + HILOG_INFO("%{public}s, %{public}d", __func__, __LINE__); + CHECK_POINTER(abilityScheduler_); + abilityScheduler_->ScheduleSaveAbilityState(outState); +} + +void LifecycleDeal::RestoreAbilityState(const PacMap &inState) +{ + HILOG_INFO("%{public}s, %{public}d", __func__, __LINE__); + CHECK_POINTER(abilityScheduler_); + abilityScheduler_->ScheduleRestoreAbilityState(inState); +} + +void LifecycleDeal::UpdateConfiguration(const GlobalConfiguration &config) +{ + HILOG_INFO("%{public}s, %{public}d", __func__, __LINE__); + CHECK_POINTER(abilityScheduler_); + abilityScheduler_->ScheduleUpdateConfiguration(config); +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/src/pending_want_manager.cpp b/services/abilitymgr/src/pending_want_manager.cpp index 8923936c4a1..1a84f5ddbe9 100644 --- a/services/abilitymgr/src/pending_want_manager.cpp +++ b/services/abilitymgr/src/pending_want_manager.cpp @@ -37,13 +37,13 @@ PendingWantManager::~PendingWantManager() HILOG_DEBUG("%{public}s(%{public}d)", __PRETTY_FUNCTION__, __LINE__); } -sptr PendingWantManager::GetWantSender(const int32_t callingUid, const int32_t uid, +sptr PendingWantManager::GetWantSender(const int32_t callingUid, const int32_t uid, const bool isSystemApp, const WantSenderInfo &wantSenderInfo, const sptr &callerToken) { HILOG_INFO("PendingWantManager::GetWantSender begin."); std::lock_guard locker(mutex_); - if (callingUid != 0 && callingUid != SYSTEM_UID) { + if (callingUid != 0 && callingUid != SYSTEM_UID && !isSystemApp) { if (callingUid != uid) { HILOG_INFO("is not allowed to send"); return nullptr; @@ -180,7 +180,8 @@ int32_t PendingWantManager::SendWantSender(const sptr &target, cons return record->SenderInner(info); } -void PendingWantManager::CancelWantSender(const int32_t callingUid, const int32_t uid, const sptr &sender) +void PendingWantManager::CancelWantSender( + const int32_t callingUid, const int32_t uid, const bool isSystemApp, const sptr &sender) { HILOG_INFO("%{public}s:begin.", __func__); @@ -190,9 +191,11 @@ void PendingWantManager::CancelWantSender(const int32_t callingUid, const int32_ } std::lock_guard locker(mutex_); - if (callingUid != uid) { - HILOG_DEBUG("is not allowed to send"); - return; + if (callingUid != 0 && callingUid != SYSTEM_UID && !isSystemApp) { + if (callingUid != uid) { + HILOG_INFO("is not allowed to send"); + return; + } } sptr record = iface_cast(sender->AsObject()); CancelWantSenderLocked(*record, true); diff --git a/services/abilitymgr/test/mock/libs/aakit/include/ability_scheduler.h b/services/abilitymgr/test/mock/libs/aakit/include/ability_scheduler.h index 4fec9c0536d..5aeac9e871b 100644 --- a/services/abilitymgr/test/mock/libs/aakit/include/ability_scheduler.h +++ b/services/abilitymgr/test/mock/libs/aakit/include/ability_scheduler.h @@ -50,6 +50,8 @@ public: void ScheduleRestoreAbilityState(const PacMap &inState) override; + void ScheduleUpdateConfiguration(const GlobalConfiguration &config) override; + std::vector GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) override; int OpenFile(const Uri &uri, const std::string &mode) override; diff --git a/services/abilitymgr/test/mock/libs/aakit/src/ability_scheduler.cpp b/services/abilitymgr/test/mock/libs/aakit/src/ability_scheduler.cpp index 9be782990c3..c5a067dbef5 100644 --- a/services/abilitymgr/test/mock/libs/aakit/src/ability_scheduler.cpp +++ b/services/abilitymgr/test/mock/libs/aakit/src/ability_scheduler.cpp @@ -60,6 +60,9 @@ void AbilityScheduler::ScheduleSaveAbilityState(PacMap &outState) void AbilityScheduler::ScheduleRestoreAbilityState(const PacMap &inState) {} +void AbilityScheduler::ScheduleUpdateConfiguration(const GlobalConfiguration &config) +{} + std::vector AbilityScheduler::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) { std::vector values; diff --git a/services/abilitymgr/test/mock/libs/ability_scheduler_mock/ability_scheduler_mock.h b/services/abilitymgr/test/mock/libs/ability_scheduler_mock/ability_scheduler_mock.h index 912c9ef255b..b9a39a77f18 100755 --- a/services/abilitymgr/test/mock/libs/ability_scheduler_mock/ability_scheduler_mock.h +++ b/services/abilitymgr/test/mock/libs/ability_scheduler_mock/ability_scheduler_mock.h @@ -36,6 +36,7 @@ public: MOCK_METHOD1(ScheduleDisconnectAbility, void(const Want &)); MOCK_METHOD1(ScheduleSaveAbilityState, void(PacMap &)); MOCK_METHOD1(ScheduleRestoreAbilityState, void(const PacMap &)); + MOCK_METHOD1(ScheduleUpdateConfiguration, void(const GlobalConfiguration &)); MOCK_METHOD1(ScheduleNewWant, void(const Want &)); MOCK_METHOD4(SendRequest, int(uint32_t, MessageParcel &, MessageParcel &, MessageOption &)); MOCK_METHOD3(ScheduleCommandAbility, void(const Want &, bool, int)); diff --git a/services/abilitymgr/test/unittest/phone/ability_manager_proxy_test/ability_manager_stub_mock.h b/services/abilitymgr/test/unittest/phone/ability_manager_proxy_test/ability_manager_stub_mock.h index 8f32c4824f3..233dfca42fd 100755 --- a/services/abilitymgr/test/unittest/phone/ability_manager_proxy_test/ability_manager_stub_mock.h +++ b/services/abilitymgr/test/unittest/phone/ability_manager_proxy_test/ability_manager_stub_mock.h @@ -232,6 +232,11 @@ public: return 0; } + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) + { + return 0; + } + MOCK_METHOD2(TerminateAbilityByCaller, int(const sptr &callerToken, int requestCode)); MOCK_METHOD3(StartAbility, int(const Want &want, const sptr &callerToken, int requestCode)); MOCK_METHOD2( diff --git a/services/abilitymgr/test/unittest/phone/ability_manager_stub_test/ability_manager_stub_impl_mock.h b/services/abilitymgr/test/unittest/phone/ability_manager_stub_test/ability_manager_stub_impl_mock.h index 45b2faae77b..e737b0b998c 100755 --- a/services/abilitymgr/test/unittest/phone/ability_manager_stub_test/ability_manager_stub_impl_mock.h +++ b/services/abilitymgr/test/unittest/phone/ability_manager_stub_test/ability_manager_stub_impl_mock.h @@ -229,6 +229,11 @@ public: { return 0; } + + int UpdateConfiguration(const GlobalConfiguration &config, std::string changeType) + { + return 0; + } }; } // namespace AAFwk diff --git a/services/abilitymgr/test/unittest/phone/ability_manager_test/ability_manager_stub_mock.h b/services/abilitymgr/test/unittest/phone/ability_manager_test/ability_manager_stub_mock.h index cf48cd111a1..ac2246a99b4 100644 --- a/services/abilitymgr/test/unittest/phone/ability_manager_test/ability_manager_stub_mock.h +++ b/services/abilitymgr/test/unittest/phone/ability_manager_test/ability_manager_stub_mock.h @@ -68,6 +68,7 @@ public: MOCK_METHOD1(UnlockMission, int(int)); MOCK_METHOD2(SetMissionDescriptionInfo, int(const sptr &token, const MissionDescriptionInfo &info)); MOCK_METHOD0(GetMissionLockModeState, int()); + MOCK_METHOD2(UpdateConfiguration, int(const GlobalConfiguration &, std::string)); MOCK_METHOD2( GetWantSender, sptr(const WantSenderInfo &wantSenderInfo, const sptr &callerToken)); diff --git a/services/abilitymgr/test/unittest/phone/ability_scheduler_stub_test/ability_schedule_stub_mock.h b/services/abilitymgr/test/unittest/phone/ability_scheduler_stub_test/ability_schedule_stub_mock.h index d3e93a97f84..8990263bb7f 100644 --- a/services/abilitymgr/test/unittest/phone/ability_scheduler_stub_test/ability_schedule_stub_mock.h +++ b/services/abilitymgr/test/unittest/phone/ability_scheduler_stub_test/ability_schedule_stub_mock.h @@ -41,6 +41,8 @@ public: {} virtual void ScheduleRestoreAbilityState(const PacMap &inState) override {} + virtual void ScheduleUpdateConfiguration(const GlobalConfiguration &config) override + {} virtual std::vector GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) override { diff --git a/services/abilitymgr/test/unittest/phone/ability_stack_manager_test/ability_stack_manager_test.cpp b/services/abilitymgr/test/unittest/phone/ability_stack_manager_test/ability_stack_manager_test.cpp index 1e53cfbc192..db767ffc398 100644 --- a/services/abilitymgr/test/unittest/phone/ability_stack_manager_test/ability_stack_manager_test.cpp +++ b/services/abilitymgr/test/unittest/phone/ability_stack_manager_test/ability_stack_manager_test.cpp @@ -35,6 +35,15 @@ using namespace testing::ext; using namespace OHOS::AppExecFwk; namespace OHOS { namespace AAFwk { + +namespace { +const std::string LANGUAGE = "locale"; +const std::string LAYOUT = "layout"; +const std::string FONTSIZE = "fontsize"; +const std::string ORIENTATION = "orientation"; +const std::string DENSITY = "density"; +} // namespace + class AbilityStackManagerTest : public testing::Test { public: static void SetUpTestCase(void); @@ -44,7 +53,7 @@ public: void init(); AbilityRequest GenerateAbilityRequest(const std::string &deviceName, const std::string &abilityName, - const std::string &appName, const std::string &bundleName); + const std::string &appName, const std::string &bundleName, const std::vector config); void makeScene(const std::string &abilityName, const std::string &bundleName, AbilityInfo &abilityInfo, Want &want); @@ -84,17 +93,18 @@ void AbilityStackManagerTest::SetUp() void AbilityStackManagerTest::init() { - launcherAbilityRequest_ = GenerateAbilityRequest("device", "LauncherAbility", "launcher", "com.ix.hiworld"); + std::vector config; + launcherAbilityRequest_ = GenerateAbilityRequest("device", "LauncherAbility", "launcher", "com.ix.hiworld", config); - musicAbilityRequest_ = GenerateAbilityRequest("device", "MusicAbility", "music", "com.ix.hiMusic"); + musicAbilityRequest_ = GenerateAbilityRequest("device", "MusicAbility", "music", "com.ix.hiMusic", config); - musicTopAbilityRequest_ = GenerateAbilityRequest("device", "MusicTopAbility", "music", "com.ix.hiMusic"); + musicTopAbilityRequest_ = GenerateAbilityRequest("device", "MusicTopAbility", "music", "com.ix.hiMusic", config); - musicSAbilityRequest_ = GenerateAbilityRequest("device", "MusicSAbility", "music", "com.ix.hiMusic"); + musicSAbilityRequest_ = GenerateAbilityRequest("device", "MusicSAbility", "music", "com.ix.hiMusic", config); - radioAbilityRequest_ = GenerateAbilityRequest("device", "RadioAbility", "radio", "com.ix.hiRadio"); + radioAbilityRequest_ = GenerateAbilityRequest("device", "RadioAbility", "radio", "com.ix.hiRadio", config); - radioTopAbilityRequest_ = GenerateAbilityRequest("device", "RadioTopAbility", "radio", "com.ix.hiRadio"); + radioTopAbilityRequest_ = GenerateAbilityRequest("device", "RadioTopAbility", "radio", "com.ix.hiRadio", config); } void AbilityStackManagerTest::makeScene( @@ -139,7 +149,8 @@ void AbilityStackManagerTest::makeScene( } AbilityRequest AbilityStackManagerTest::GenerateAbilityRequest(const std::string &deviceName, - const std::string &abilityName, const std::string &appName, const std::string &bundleName) + const std::string &abilityName, const std::string &appName, const std::string &bundleName, + const std::vector config) { ElementName element(deviceName, abilityName, bundleName); Want want; @@ -153,6 +164,7 @@ AbilityRequest AbilityStackManagerTest::GenerateAbilityRequest(const std::string abilityInfo.applicationName = appName; abilityInfo.applicationInfo.bundleName = bundleName; abilityInfo.applicationInfo.name = appName; + abilityInfo.configChanges = config; makeScene(abilityName, bundleName, abilityInfo, want); @@ -848,7 +860,8 @@ HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_022, TestSize. std::string abilityName = "otherAbility"; std::string appName = "otherApp"; std::string bundleName = "com.ix.other"; - auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName); + std::vector config; + auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, config); auto record = AbilityRecord::CreateAbilityRecord(abilityReq); auto nullToken = new Token(record); EXPECT_NE(0, stackManager_->TerminateAbility(nullToken, -1, &want)); @@ -912,7 +925,8 @@ HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_024, TestSize. std::string abilityName = "otherAbility"; std::string appName = "otherApp"; std::string bundleName = "com.ix.other"; - auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName); + std::vector config; + auto abilityReq = GenerateAbilityRequest(deviceName, abilityName, appName, bundleName, config); auto record = AbilityRecord::CreateAbilityRecord(abilityReq); auto token = topAbility->GetToken(); stackManager_->GetTopMissionRecord()->RemoveTopAbilityRecord(); @@ -1865,7 +1879,9 @@ HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_053, TestSize. topAbilityRecord->SetAbilityState(OHOS::AAFwk::ACTIVE); // 2 - auto musicAbilityRequest2th = GenerateAbilityRequest("device", "MusicAbility2th", "music", "com.ix.hiMusic"); + std::vector config; + auto musicAbilityRequest2th = + GenerateAbilityRequest("device", "MusicAbility2th", "music", "com.ix.hiMusic", config); result = stackManager_->StartAbility(musicAbilityRequest2th); EXPECT_EQ(0, result); auto musicMisionRecord2th = stackManager_->GetTopMissionRecord(); @@ -2120,25 +2136,96 @@ HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_060, TestSize. auto result1 = stackManager_->StartLockMission(99, MisionRecordId, false, true); EXPECT_EQ(LOCK_MISSION_DENY_FAILED, result1); - result1 = stackManager_->StartLockMission(100, MisionRecordId, false, true); + result1 = stackManager_->StartLockMission(1000, MisionRecordId, false, true); EXPECT_EQ(ERR_OK, result1); // unlock defferent uid(99) result1 = stackManager_->StartLockMission(99, MisionRecordId, false, false); - EXPECT_EQ(LOCK_MISSION_DENY_FAILED, result1); + EXPECT_EQ(UNLOCK_MISSION_DENY_FAILED, result1); - result1 = stackManager_->StartLockMission(100, MisionRecordId, false, false); + result1 = stackManager_->StartLockMission(1000, MisionRecordId, false, false); EXPECT_EQ(ERR_OK, result1); } /* * Feature: AbilityStackManager - * Function: SetMissionDescriptionInfo + * Function: UpdateConfiguration * SubFunction: NA - * FunctionPoints: SetMissionDescriptionInfo + * FunctionPoints: Notify after system attribute changes * EnvConditions: NA - * CaseDescription: when the uid defferent + * CaseDescription: Environmental judgment + */ +HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_061, TestSize.Level1) +{ + stackManager_->Init(); + + GlobalConfiguration globalConfiguration(LANGUAGE); + auto ref = stackManager_->UpdateConfiguration(globalConfiguration, LANGUAGE); + EXPECT_EQ(ERR_INVALID_VALUE, ref); +} + +/* + * Feature: AbilityStackManager + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Notify after system attribute changes + * EnvConditions: NA + * CaseDescription: Restart ability + */ +HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_062, TestSize.Level1) +{ + stackManager_->Init(); + + auto result = stackManager_->StartAbility(musicAbilityRequest_); + EXPECT_EQ(0, result); + + auto topMissionRecord = stackManager_->GetTopMissionRecord(); + auto topAbilityRecord = topMissionRecord->GetTopAbilityRecord(); + EXPECT_TRUE(topMissionRecord); + EXPECT_TRUE(topAbilityRecord); + topAbilityRecord->SetAbilityState(OHOS::AAFwk::ACTIVE); + + GlobalConfiguration globalConfiguration(LANGUAGE); + auto ref = stackManager_->UpdateConfiguration(globalConfiguration, LANGUAGE); + EXPECT_EQ(ERR_OK, ref); + + auto abilityRecord = stackManager_->GetCurrentTopAbility(); + EXPECT_TRUE(abilityRecord->IsRestarting()); +} + +/* + * Feature: AbilityStackManager + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Notify after system attribute changes + * EnvConditions: NA + * CaseDescription: Restart ability */ +HWTEST_F(AbilityStackManagerTest, ability_stack_manager_operating_063, TestSize.Level1) +{ + stackManager_->Init(); + + std::vector config; + config.emplace_back(LANGUAGE); + config.emplace_back(ORIENTATION); + + musicAbilityRequest_ = GenerateAbilityRequest("device", "MusicAbility", "music", "com.ix.hiMusic", config); + auto result = stackManager_->StartAbility(musicAbilityRequest_); + EXPECT_EQ(0, result); + + auto topMissionRecord = stackManager_->GetTopMissionRecord(); + auto topAbilityRecord = topMissionRecord->GetTopAbilityRecord(); + EXPECT_TRUE(topMissionRecord); + EXPECT_TRUE(topAbilityRecord); + topAbilityRecord->SetAbilityState(OHOS::AAFwk::ACTIVE); + + GlobalConfiguration globalConfiguration(LANGUAGE); + auto ref = stackManager_->UpdateConfiguration(globalConfiguration, ORIENTATION); + EXPECT_EQ(ERR_OK, ref); + + auto abilityRecord = stackManager_->GetCurrentTopAbility(); + EXPECT_FALSE(abilityRecord->IsRestarting()); +} } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/test/unittest/phone/ability_with_applications_test/mock_ability_scheduler.cpp b/services/abilitymgr/test/unittest/phone/ability_with_applications_test/mock_ability_scheduler.cpp index 39bf55f2c1c..e9222aead86 100644 --- a/services/abilitymgr/test/unittest/phone/ability_with_applications_test/mock_ability_scheduler.cpp +++ b/services/abilitymgr/test/unittest/phone/ability_with_applications_test/mock_ability_scheduler.cpp @@ -52,6 +52,9 @@ void AbilityScheduler::ScheduleSaveAbilityState(PacMap &outState) void AbilityScheduler::ScheduleRestoreAbilityState(const PacMap &inState) {} +void AbilityScheduler::ScheduleUpdateConfiguration(const GlobalConfiguration &config) +{} + std::vector AbilityScheduler::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) { std::vector types; diff --git a/services/abilitymgr/test/unittest/phone/pending_want_manager_test/pending_want_manager_test.cpp b/services/abilitymgr/test/unittest/phone/pending_want_manager_test/pending_want_manager_test.cpp index deb0a4289c3..11da80350b6 100644 --- a/services/abilitymgr/test/unittest/phone/pending_want_manager_test/pending_want_manager_test.cpp +++ b/services/abilitymgr/test/unittest/phone/pending_want_manager_test/pending_want_manager_test.cpp @@ -158,7 +158,7 @@ HWTEST_F(PendingWantManagerTest, PendingWantManagerTest_0100, TestSize.Level1) WantSenderInfo wantSenderInfo; pendingManager_ = std::make_shared(); EXPECT_NE(pendingManager_, nullptr); - EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, wantSenderInfo, nullptr), nullptr); + EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, false, wantSenderInfo, nullptr), nullptr); } /* @@ -173,7 +173,7 @@ HWTEST_F(PendingWantManagerTest, PendingWantManagerTest_0200, TestSize.Level1) WantSenderInfo wantSenderInfo; pendingManager_ = std::make_shared(); EXPECT_NE(pendingManager_, nullptr); - EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, wantSenderInfo, nullptr), nullptr); + EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, false, wantSenderInfo, nullptr), nullptr); } /* @@ -188,7 +188,7 @@ HWTEST_F(PendingWantManagerTest, PendingWantManagerTest_0300, TestSize.Level1) WantSenderInfo wantSenderInfo; pendingManager_ = std::make_shared(); EXPECT_NE(pendingManager_, nullptr); - EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, wantSenderInfo, nullptr), nullptr); + EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, false, wantSenderInfo, nullptr), nullptr); } /* @@ -203,7 +203,7 @@ HWTEST_F(PendingWantManagerTest, PendingWantManagerTest_0400, TestSize.Level1) WantSenderInfo wantSenderInfo; pendingManager_ = std::make_shared(); EXPECT_NE(pendingManager_, nullptr); - EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, wantSenderInfo, nullptr), nullptr); + EXPECT_EQ(pendingManager_->GetWantSender(callingUid, uid, false, wantSenderInfo, nullptr), nullptr); } /* @@ -777,7 +777,7 @@ HWTEST_F(PendingWantManagerTest, PendingWantManagerTest_3100, TestSize.Level1) pendingManager_->GetWantSenderLocked(1, 1, wantSenderInfo.userId, wantSenderInfo, nullptr)->AsObject()); EXPECT_NE(pendingRecord, nullptr); pendingManager_->RegisterCancelListener(pendingRecord, cance); - pendingManager_->CancelWantSender(0, 0, pendingRecord); + pendingManager_->CancelWantSender(0, 0, false, pendingRecord); EXPECT_TRUE(CancelReceiver::sendCount == 100); EXPECT_TRUE((int)pendingManager_->wantRecords_.size() == 0); } diff --git a/services/test/mock/include/mock_ability_mgr_service.h b/services/test/mock/include/mock_ability_mgr_service.h index fe3ff466222..d7e93e5ca11 100644 --- a/services/test/mock/include/mock_ability_mgr_service.h +++ b/services/test/mock/include/mock_ability_mgr_service.h @@ -61,6 +61,7 @@ public: MOCK_METHOD1(UnlockMission, int(int)); MOCK_METHOD2(SetMissionDescriptionInfo, int(const sptr &token, const MissionDescriptionInfo &info)); MOCK_METHOD0(GetMissionLockModeState, int()); + MOCK_METHOD2(UpdateConfiguration, int(const GlobalConfiguration &, std::string)); MOCK_METHOD2( GetWantSender, sptr(const WantSenderInfo &wantSenderInfo, const sptr &callerToken)); MOCK_METHOD2(SendWantSender, int(const sptr &target, const SenderInfo &senderInfo)); diff --git a/services/test/mock/include/mock_ability_scheduler.h b/services/test/mock/include/mock_ability_scheduler.h index 88b60273fc1..26ec29e0ef7 100644 --- a/services/test/mock/include/mock_ability_scheduler.h +++ b/services/test/mock/include/mock_ability_scheduler.h @@ -34,6 +34,7 @@ public: MOCK_METHOD0(AsObject, sptr()); MOCK_METHOD1(ScheduleSaveAbilityState, void(PacMap &outState)); MOCK_METHOD1(ScheduleRestoreAbilityState, void(const PacMap &inState)); + MOCK_METHOD1(ScheduleUpdateConfiguration, void(const GlobalConfiguration &)); MOCK_METHOD1(ScheduleNewWant, void(const Want &want)); std::vector GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter) diff --git a/services/test/mock/include/mock_ability_scheduler_stub.h b/services/test/mock/include/mock_ability_scheduler_stub.h index bd061029672..af0de829460 100644 --- a/services/test/mock/include/mock_ability_scheduler_stub.h +++ b/services/test/mock/include/mock_ability_scheduler_stub.h @@ -30,6 +30,7 @@ public: MOCK_METHOD3(ScheduleCommandAbility, void(const Want &, bool, int)); MOCK_METHOD1(ScheduleSaveAbilityState, void(PacMap &)); MOCK_METHOD1(ScheduleRestoreAbilityState, void(const PacMap &)); + MOCK_METHOD1(ScheduleUpdateConfiguration, void(const GlobalConfiguration &)); MOCK_METHOD2(GetFileTypes, std::vector(const Uri &, const std::string &)); MOCK_METHOD2(OpenFile, int(const Uri &, const std::string &)); MOCK_METHOD2(Insert, int(const Uri &, const ValuesBucket &)); diff --git a/services/test/moduletest/ability_mgr_service_test/BUILD.gn b/services/test/moduletest/ability_mgr_service_test/BUILD.gn index 92214c2afcc..78bf9451e2a 100644 --- a/services/test/moduletest/ability_mgr_service_test/BUILD.gn +++ b/services/test/moduletest/ability_mgr_service_test/BUILD.gn @@ -41,6 +41,7 @@ ohos_moduletest("ability_mgr_module_test") { "//foundation/aafwk/standard/services/abilitymgr/src/connection_record.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_manager.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/global_configuration.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/image_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/kernal_system_app_manager.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_deal.cpp", diff --git a/services/test/moduletest/ability_record_test/BUILD.gn b/services/test/moduletest/ability_record_test/BUILD.gn index 6962574439a..d786afd7f79 100644 --- a/services/test/moduletest/ability_record_test/BUILD.gn +++ b/services/test/moduletest/ability_record_test/BUILD.gn @@ -44,6 +44,7 @@ ohos_moduletest("AbilityRecordModuleTest") { "${services_path}/abilitymgr/src/connection_record.cpp", "${services_path}/abilitymgr/src/data_ability_manager.cpp", "${services_path}/abilitymgr/src/data_ability_record.cpp", + "${services_path}/abilitymgr/src/global_configuration.cpp", "${services_path}/abilitymgr/src/image_info.cpp", "${services_path}/abilitymgr/src/kernal_system_app_manager.cpp", "${services_path}/abilitymgr/src/lifecycle_deal.cpp", diff --git a/services/test/moduletest/ability_stack_test/BUILD.gn b/services/test/moduletest/ability_stack_test/BUILD.gn index bdd09eac61d..74e7f418edb 100644 --- a/services/test/moduletest/ability_stack_test/BUILD.gn +++ b/services/test/moduletest/ability_stack_test/BUILD.gn @@ -41,6 +41,7 @@ ohos_moduletest("ability_stack_module_test") { "//foundation/aafwk/standard/services/abilitymgr/src/connection_record.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_manager.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/data_ability_record.cpp", + "//foundation/aafwk/standard/services/abilitymgr/src/global_configuration.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/image_info.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/kernal_system_app_manager.cpp", "//foundation/aafwk/standard/services/abilitymgr/src/lifecycle_deal.cpp", diff --git a/services/test/moduletest/ability_stack_test/ability_stack_module_test.cpp b/services/test/moduletest/ability_stack_test/ability_stack_module_test.cpp index 7c67a41c1e1..2e1a1658aa2 100644 --- a/services/test/moduletest/ability_stack_test/ability_stack_module_test.cpp +++ b/services/test/moduletest/ability_stack_test/ability_stack_module_test.cpp @@ -37,6 +37,14 @@ using namespace testing; namespace OHOS { namespace AAFwk { + +namespace { +const std::string LANGUAGE = "locale"; +const std::string LAYOUT = "layout"; +const std::string FONTSIZE = "fontsize"; +const std::string ORIENTATION = "orientation"; +const std::string DENSITY = "density"; +} // namespace class AbilityStackModuleTest : public testing::Test { public: static void SetUpTestCase(void); @@ -1171,11 +1179,12 @@ HWTEST_F(AbilityStackModuleTest, ability_stack_test_017, TestSize.Level1) /* * Feature: AaFwk - * Function: StartLockMission + * Function: OnAbilityDied * SubFunction: NA - * FunctionPoints: lock a mission , It's locked mission top ability will be active + * FunctionPoints: Execute when application dies * EnvConditions: NA - * CaseDescription: when locked mission died + * CaseDescription: When the system attribute is changed, if the listener is not registered, the application needs to be + * restarted */ HWTEST_F(AbilityStackModuleTest, ability_stack_test_018, TestSize.Level1) { @@ -1215,5 +1224,130 @@ HWTEST_F(AbilityStackModuleTest, ability_stack_test_018, TestSize.Level1) testing::Mock::AllowLeak(bundleObject_); } +/* + * Feature: AaFwk + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: + * EnvConditions: NA + * CaseDescription: When the system attribute is changed, if the listener is not registered, the application needs to be + * restarted + */ +HWTEST_F(AbilityStackModuleTest, ability_stack_test_019, TestSize.Level1) +{ + stackManager_->Init(); + + auto appScheduler = OHOS::DelayedSingleton::GetInstance(); + MockAppMgrClient *mockAppMgrClient = new MockAppMgrClient(); + appScheduler->appMgrClient_.reset(mockAppMgrClient); + + OHOS::sptr scheduler(new MockAbilityScheduler()); + + // When the application restarts, it is called + auto restartCall = [&](const sptr &token, + const sptr &preToken, + const AbilityInfo &abilityInfo, + const ApplicationInfo &appInfo) { + return (AppMgrResultCode)stackManager_->AttachAbilityThread(scheduler, token); + }; + + EXPECT_CALL(*mockAppMgrClient, LoadAbility(_, _, _, _)) + .Times(AtLeast(1)) + .WillOnce(Return(AppMgrResultCode::RESULT_OK)) + .WillOnce(testing::Invoke(restartCall)); + + EXPECT_CALL(*mockAppMgrClient, TerminateAbility(_)).Times(AtLeast(1)).WillOnce(Return(AppMgrResultCode::RESULT_OK)); + EXPECT_CALL(*mockAppMgrClient, UpdateAbilityState(_, _)) + .Times(AtLeast(2)) + .WillOnce(Return(AppMgrResultCode::RESULT_OK)) + .WillOnce(Return(AppMgrResultCode::RESULT_OK)); + + auto launcherAbilityRequest_ = GenerateAbilityRequest("device", "LauncherAbility", "launcher", "com.ix.hiworld"); + stackManager_->StartAbility(launcherAbilityRequest_); + auto firstTopAbility = stackManager_->GetCurrentTopAbility(); + firstTopAbility->SetAbilityState(OHOS::AAFwk::ACTIVE); + firstTopAbility->SetScheduler(scheduler); + + auto transactionDoneInactive = [&](const Want &want, const LifeCycleStateInfo &targetState) { + stackManager_->CompleteInactive(firstTopAbility); + }; + + auto transactionDoneBackground = [&](const Want &want, const LifeCycleStateInfo &targetState) { + stackManager_->CompleteBackground(firstTopAbility); + }; + + auto transactionDoneTerminate = [&](const Want &want, const LifeCycleStateInfo &targetState) { + stackManager_->CompleteTerminate(firstTopAbility); + }; + + // first time call + EXPECT_CALL(*scheduler, ScheduleAbilityTransaction(testing::_, testing::_)) + .Times(testing::AtLeast(3)) + .WillOnce(testing::Invoke(transactionDoneInactive)) + .WillOnce(testing::Invoke(transactionDoneBackground)) + .WillOnce(testing::Invoke(transactionDoneTerminate)); + + // when restart ability save the ability state + EXPECT_CALL(*scheduler, ScheduleSaveAbilityState(testing::_)).Times(testing::AtLeast(1)); + // when restart ability restore the ability state + EXPECT_CALL(*scheduler, ScheduleRestoreAbilityState(testing::_)).Times(testing::AtLeast(1)); + + GlobalConfiguration globalConfiguration(LANGUAGE); + auto ref = stackManager_->UpdateConfiguration(globalConfiguration, ORIENTATION); + EXPECT_EQ(ERR_OK, ref); + + testing::Mock::AllowLeak(mockAppMgrClient); +} + +/* + * Feature: AaFwk + * Function: UpdateConfiguration + * SubFunction: NA + * FunctionPoints: Notification application attribute change + * EnvConditions: NA + * CaseDescription: Notify if the system property change notification is registered + */ +HWTEST_F(AbilityStackModuleTest, ability_stack_test_020, TestSize.Level1) +{ + stackManager_->Init(); + auto appScheduler = OHOS::DelayedSingleton::GetInstance(); + MockAppMgrClient *mockAppMgrClient = new MockAppMgrClient(); + appScheduler->appMgrClient_.reset(mockAppMgrClient); + + EXPECT_CALL(*mockAppMgrClient, LoadAbility(_, _, _, _)) + .Times(AtLeast(2)) + .WillOnce(Return(AppMgrResultCode::RESULT_OK)) + .WillOnce(Return(AppMgrResultCode::RESULT_OK)); + + auto launcherAbilityRequest_ = GenerateAbilityRequest("device", "LauncherAbility", "launcher", "com.ix.hiworld"); + auto ref = stackManager_->StartAbility(launcherAbilityRequest_); + EXPECT_EQ(ERR_OK, ref); + auto firstTopAbility = stackManager_->GetCurrentTopAbility(); + firstTopAbility->SetAbilityState(OHOS::AAFwk::ACTIVE); + + auto musicAbilityRequest_ = GenerateAbilityRequest("device", "MusicSAbility", "music", "com.ix.hiMusic"); + + std::vector config; + config.emplace_back(LANGUAGE); + config.emplace_back(ORIENTATION); + musicAbilityRequest_.abilityInfo.configChanges = config; + + ref = stackManager_->StartAbility(musicAbilityRequest_); + EXPECT_EQ(ERR_OK, ref); + auto musicAbility = stackManager_->GetCurrentTopAbility(); + musicAbility->SetAbilityState(OHOS::AAFwk::ACTIVE); + + // must be Notification should be triggered + OHOS::sptr scheduler(new MockAbilityScheduler()); + musicAbility->SetScheduler(scheduler); + EXPECT_CALL(*scheduler, ScheduleUpdateConfiguration(testing::_)).Times(testing::AtLeast(1)); + + GlobalConfiguration globalConfiguration(LANGUAGE); + ref = stackManager_->UpdateConfiguration(globalConfiguration, ORIENTATION); + EXPECT_EQ(ERR_OK, ref); + + testing::Mock::AllowLeak(mockAppMgrClient); +} + } // namespace AAFwk } // namespace OHOS diff --git a/tools/test/mock/mock_ability_manager_stub.h b/tools/test/mock/mock_ability_manager_stub.h index a389a832015..3604ad1edec 100644 --- a/tools/test/mock/mock_ability_manager_stub.h +++ b/tools/test/mock/mock_ability_manager_stub.h @@ -98,6 +98,7 @@ public: MOCK_METHOD1(UnlockMission, int(int)); MOCK_METHOD2(SetMissionDescriptionInfo, int(const sptr &token, const MissionDescriptionInfo &info)); MOCK_METHOD0(GetMissionLockModeState, int()); + MOCK_METHOD2(UpdateConfiguration, int(const GlobalConfiguration &, std::string)); public: std::string powerState_; -- Gitee