diff --git a/frameworks/c/ability_runtime/src/application_context.cpp b/frameworks/c/ability_runtime/src/application_context.cpp index e7ab30d5ae4307628d906419a29df586f555cf76..a46616f751ac97d13bade606d21d2fddd15d7256 100644 --- a/frameworks/c/ability_runtime/src/application_context.cpp +++ b/frameworks/c/ability_runtime/src/application_context.cpp @@ -41,6 +41,7 @@ using namespace OHOS; namespace { constexpr int32_t ATTACH_ABILITY_THREAD_TIMEOUT_TIME = 100 * 1000; // attach ability thread timeout, 100s +std::mutex g_appMgrMutex; sptr g_appMgr = nullptr; AbilityRuntime_ErrorCode WriteStringToBuffer( @@ -72,6 +73,7 @@ AbilityRuntime_ErrorCode CheckParameters(char* buffer, int32_t* writeLength) sptr GetAppMgr() { + std::unique_lock lock(g_appMgrMutex); if (g_appMgr) { return g_appMgr; } @@ -450,7 +452,7 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetVersionCode(int6 } AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(AbilityBase_Want *want, - AbilityRuntime_StartOptions *options, int32_t &targetPid) + AbilityRuntime_StartOptions *options, int32_t *targetPid) { TAG_LOGD(AAFwkTag::APPKIT, "StartSelfUIAbilityWithPidResult called"); auto ret = CheckAppMainThread(); @@ -463,8 +465,8 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(Abili TAG_LOGE(AAFwkTag::APPKIT, "CheckWant failed: %{public}d", ret); return ret; } - if (options == nullptr) { - TAG_LOGE(AAFwkTag::APPKIT, "null options"); + if (options == nullptr || targetPid == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null options or targetPid"); return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID; } Want abilityWant; @@ -476,8 +478,8 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(Abili StartOptions startOptions = options->GetInnerStartOptions(); ffrt::condition_variable callbackDoneCv; std::atomic_bool done = false; - auto task = [&targetPid, &callbackDoneCv, &done](int32_t pidResult) { - targetPid = pidResult; + auto task = [targetPid, &callbackDoneCv, &done](int32_t pidResult) { + *targetPid = pidResult; done.store(true); callbackDoneCv.notify_all(); }; @@ -494,7 +496,7 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(Abili ffrt::mutex callbackDoneMutex; std::unique_lock lock(callbackDoneMutex); if (!callbackDoneCv.wait_for(lock, std::chrono::milliseconds(ATTACH_ABILITY_THREAD_TIMEOUT_TIME), condition) || - targetPid < 0) { + *targetPid < 0) { callback->Cancel(); return ABILITY_RUNTIME_ERROR_CODE_START_TIMEOUT; } diff --git a/interfaces/kits/c/ability_runtime/application_context.h b/interfaces/kits/c/ability_runtime/application_context.h index 99dad12ff1622d1aeb5d11e2b3194895e8dfc403..cb512331ab65bc67f6693bb2adf630b7a1b6366e 100644 --- a/interfaces/kits/c/ability_runtime/application_context.h +++ b/interfaces/kits/c/ability_runtime/application_context.h @@ -347,12 +347,12 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( /** * @brief Starts self UIAbility with start options and receives the process ID. * - * @permission {@code ohos.permission.NDK_START_SELF_UI_ABILITY} + * @permission ohos.permission.NDK_START_SELF_UI_ABILITY * @param want The arguments passed to start self UIAbility. * For details, see {@link AbilityBase_Want}. * @param options The start options passed to start self UIAbility. * For details, see {@link AbilityRuntime_StartOptions}. - * @param pid The process ID of the started UIAbility. + * @param targetPid The process ID of the started UIAbility. * @return Returns {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the call is successful. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_PERMISSION_DENIED} if the caller has no correct permission. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the arguments provided is invalid. @@ -364,7 +364,7 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( * Returns {@link ABILITY_RUNTIME_ERROR_CODE_CONTROLLED} if the app is controlled. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_EDM_CONTROLLED} if the app is controlled by EDM. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_CROSS_APP} if the caller tries to start a different application. - * Returns {@link ABILITY_RUNTIME_ERROR_CODE_INTERNAL} if internal error occurs. + * Returns {@link ABILITY_RUNTIME_ERROR_CODE_INTERNAL} if internal error occurs. such as connect system service failed. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_NOT_TOP_ABILITY} if the caller is not foreground process. * Returns {@link ABILITY_RUNTIME_ERROR_VISIBILITY_SETTING_DISABLED} if setting visibility is disabled. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_MULTI_APP_NOT_SUPPORTED} @@ -374,14 +374,14 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( * Returns {@link ABILITY_RUNTIME_ERROR_MULTI_INSTANCE_NOT_SUPPORTED} if the multi-instance is not supported. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_APP_INSTANCE_KEY_NOT_SUPPORTED} * if the APP_INSTANCE_KEY cannot be specified. - * Returns {@link ABILITY_RUNTIME_ERROR_CODE_START_TIMEOUT} if starting UIAbility times out. + * Returns {@link ABILITY_RUNTIME_ERROR_CODE_START_TIMEOUT} if starting UIAbility time out. * Returns {@link ABILITY_RUNTIME_ERROR_CODE_MAIN_THREAD_NOT_SUPPORTED} * if the API is called in the main thread of the app. * For details, see {@link AbilityRuntime_ErrorCode}. * @since 21 */ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(AbilityBase_Want *want, - AbilityRuntime_StartOptions *options, int32_t &targetPid); + AbilityRuntime_StartOptions *options, int32_t *targetPid); #ifdef __cplusplus } // extern "C" diff --git a/test/unittest/capi_ability_runtime_application_context_test/capi_ability_runtime_application_context_test.cpp b/test/unittest/capi_ability_runtime_application_context_test/capi_ability_runtime_application_context_test.cpp index e39d9a5394c68f2b694a75d6ac2c903a036b951d..5220436476abf3d7e7eb68bb247667f3e2fb0c82 100644 --- a/test/unittest/capi_ability_runtime_application_context_test/capi_ability_runtime_application_context_test.cpp +++ b/test/unittest/capi_ability_runtime_application_context_test/capi_ability_runtime_application_context_test.cpp @@ -2832,7 +2832,7 @@ HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_StartSelfUI { // Act int32_t pid = -1; - AbilityRuntime_ErrorCode result = OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(nullptr, nullptr, pid); + AbilityRuntime_ErrorCode result = OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(nullptr, nullptr, &pid); // Assert EXPECT_EQ(ABILITY_RUNTIME_ERROR_CODE_MAIN_THREAD_NOT_SUPPORTED, result); @@ -2862,7 +2862,7 @@ HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_StartSelfUI // Act int32_t pid = -1; - AbilityRuntime_ErrorCode result = OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(&want, options, pid); + AbilityRuntime_ErrorCode result = OH_AbilityRuntime_StartSelfUIAbilityWithPidResult(&want, options, &pid); // Assert EXPECT_NE(ABILITY_RUNTIME_ERROR_CODE_NO_ERROR, result);