From 10a83fe5532255211315abbc67ad7bade12dca97 Mon Sep 17 00:00:00 2001 From: 18795846185 Date: Wed, 6 Aug 2025 10:36:44 +0800 Subject: [PATCH] get parameter Signed-off-by: 18795846185 Change-Id: If786a7745b580da9c098bda5b19f5c418ce5158f --- .../src/application_context.cpp | 46 ++++++ .../native/ability_runtime/js_ui_ability.cpp | 3 + .../context/application_context.cpp | 28 ++++ .../ability_runtime/context/context_impl.cpp | 28 ++++ .../c/ability_runtime/application_context.h | 33 +++++ .../context/application_context.h | 4 + .../ability_runtime/context/context_impl.h | 11 ++ ...ility_runtime_application_context_test.cpp | 140 ++++++++++++++++++ 8 files changed, 293 insertions(+) diff --git a/frameworks/c/ability_runtime/src/application_context.cpp b/frameworks/c/ability_runtime/src/application_context.cpp index b4a34372ee4..249022329a2 100644 --- a/frameworks/c/ability_runtime/src/application_context.cpp +++ b/frameworks/c/ability_runtime/src/application_context.cpp @@ -342,4 +342,50 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(Ab OHOS::AAFwk::StartOptions startOptions = options->GetInnerStartOptions(); return ConvertToAPI18BusinessErrorCode(AbilityManagerClient::GetInstance()->StartSelfUIAbilityWithStartOptions( abilityWant, startOptions)); +} + +AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLaunchParameter( + char* buffer, const int32_t bufferSize, int32_t* writeLength) +{ + TAG_LOGD(AAFwkTag::APPKIT, "ApplicationContextGetLaunchParameter called"); + auto ret = CheckParameters(buffer, writeLength); + if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) { + return ret; + } + const auto appContext = Context::GetApplicationContext(); + if (appContext == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "appContext is null"); + return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST; + } + + const std::string launchParameter = appContext->GetLaunchParameter(); + if (launchParameter.empty()) { + TAG_LOGE(AAFwkTag::APPKIT, "launchParameter is empty"); + return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST; + } + + return WriteStringToBuffer(launchParameter, buffer, bufferSize, writeLength); +} + +AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( + char* buffer, const int32_t bufferSize, int32_t* writeLength) +{ + TAG_LOGD(AAFwkTag::APPKIT, "ApplicationContextGetLatestParameter called"); + auto ret = CheckParameters(buffer, writeLength); + if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) { + return ret; + } + const auto appContext = Context::GetApplicationContext(); + if (appContext == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "appContext is null"); + return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST; + } + + const std::string latestParameter = appContext->GetLatestParameter(); + if (latestParameter.empty()) { + TAG_LOGE(AAFwkTag::APPKIT, "latestParameter is empty"); + return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST; + } + + return WriteStringToBuffer(latestParameter, buffer, bufferSize, writeLength); } \ No newline at end of file diff --git a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp index 7809a6a7e5a..9b0eaa6f8a1 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp @@ -399,6 +399,8 @@ void JsUIAbility::OnStart(const Want &want, sptr sessionInfo auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { + applicationContext->SetLaunchParameter(want); + applicationContext->SetLatestParameter(want); applicationContext->DispatchOnAbilityWillCreate(jsAbilityObj_); } @@ -1714,6 +1716,7 @@ void JsUIAbility::OnNewWant(const Want &want) auto applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { + applicationContext->SetLatestParameter(want); applicationContext->DispatchOnWillNewWant(jsAbilityObj_); } diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index 9bf0dfe0465..3b02a7966fb 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -670,6 +670,34 @@ std::string ApplicationContext::GetCloudFileDir() return (contextImpl_ != nullptr) ? contextImpl_->GetCloudFileDir() : ""; } +std::string ApplicationContext::GetLaunchParameter() +{ + return (contextImpl_ != nullptr) ? contextImpl_->GetLaunchParameter() : ""; +} + +void ApplicationContext::SetLaunchParameter(const AAFwk::Want &want) +{ + if (contextImpl_ == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl_"); + return; + } + contextImpl_->SetLaunchParameter(want); +} + +std::string ApplicationContext::GetLatestParameter() +{ + return (contextImpl_ != nullptr) ? contextImpl_->GetLatestParameter() : ""; +} + +void ApplicationContext::SetLatestParameter(const AAFwk::Want &want) +{ + if (contextImpl_ == nullptr) { + TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl_"); + return; + } + contextImpl_->SetLatestParameter(want); +} + sptr ApplicationContext::GetToken() { return (contextImpl_ != nullptr) ? contextImpl_->GetToken() : nullptr; diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index fd846ca59d5..d7c48454d84 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -1757,6 +1757,34 @@ bool ContextImpl::UpdateDisplayConfiguration(std::shared_ptr &conte return true; } +void ContextImpl::SetLaunchParameter(const AAFwk::Want& want) +{ + launchParameter_ = std::make_shared(want); +} + +std::string ContextImpl::GetLaunchParameter() +{ + if (launchParameter_ != nullptr) { + return launchParameter_->ToFullString(); + } + TAG_LOGD(AAFwkTag::APPKIT, "launchParameter_ is null"); + return ""; +} + +void ContextImpl::SetLatestParameter(const AAFwk::Want& want) +{ + latestParameter_ = std::make_shared(want); +} + +std::string ContextImpl::GetLatestParameter() +{ + if (latestParameter_ != nullptr) { + return latestParameter_->ToFullString(); + } + TAG_LOGD(AAFwkTag::APPKIT, "latestParameter_ is null"); + return ""; +} + #ifdef SUPPORT_GRAPHICS std::shared_ptr ContextImpl::CreateDisplayContext(uint64_t displayId) { diff --git a/interfaces/kits/c/ability_runtime/application_context.h b/interfaces/kits/c/ability_runtime/application_context.h index 95356eb1266..6195fe68439 100644 --- a/interfaces/kits/c/ability_runtime/application_context.h +++ b/interfaces/kits/c/ability_runtime/application_context.h @@ -296,6 +296,39 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(Ab */ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(const char* moduleName, char* buffer, const int32_t bufferSize, int32_t* writeLength); +/** + * @brief Obtain the launch parameter of starting UIAbility. + * + * @param buffer A pointer to a buffer that receives the launch parameter of starting UIAbility. + * @param bufferSize The length of the buffer. + * @param writeLength The string length actually written to the buffer, + * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}. + * @return The error code. + * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful. + * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null, + * or the buffer size is less than the minimum buffer size. + * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the application context does not exist. + * @since 20 + */ +AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLaunchParameter( + char* buffer, const int32_t bufferSize, int32_t* writeLength); + +/** + * @brief Obtain the latest parameter of starting UIAbility. + * + * @param buffer A pointer to a buffer that receives the latest parameter of starting UIAbility. + * @param bufferSize The length of the buffer. + * @param writeLength The string length actually written to the buffer, + * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}. + * @return The error code. + * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful. + * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null, + * or the buffer size is less than the minimum buffer size. + * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the application context does not exist. + * @since 20 + */ +AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( + char* buffer, const int32_t bufferSize, int32_t* writeLength); #ifdef __cplusplus } // extern "C" diff --git a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h index de9011e7968..00193f2062f 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -165,6 +165,10 @@ public: using SelfType = ApplicationContext; static const size_t CONTEXT_TYPE_ID; std::string GetDataDir(); + void SetLaunchParameter(const AAFwk::Want &want); + void SetLatestParameter(const AAFwk::Want &want); + std::string GetLaunchParameter(); + std::string GetLatestParameter(); protected: bool IsContext(size_t contextTypeId) override diff --git a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h index 3e61977f846..b1b75541029 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h @@ -423,6 +423,14 @@ public: */ std::shared_ptr CreateAreaModeContext(int areaMode) override; + void SetLaunchParameter(const AAFwk::Want& want); + + std::string GetLaunchParameter(); + + void SetLatestParameter(const AAFwk::Want& want); + + std::string GetLatestParameter(); + #ifdef SUPPORT_GRAPHICS /** * @brief Create a context by displayId. This Context updates the density and direction properties @@ -559,6 +567,9 @@ private: std::mutex overlaySubscriberMutex_; std::shared_ptr overlaySubscriber_; std::string processName_; + std::shared_ptr launchParameter_ = nullptr; + std::shared_ptr latestParameter_ = nullptr; + #ifdef SUPPORT_GRAPHICS static std::mutex getDisplayConfigCallbackMutex_; static GetDisplayConfigCallback getDisplayConfigCallback_; 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 97c9b3d77f9..ceae6d44904 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 @@ -2393,6 +2393,146 @@ HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_StartSelfUI EXPECT_EQ(ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID, result); } +/** + * @tc.number: OH_AbilityRuntime_ApplicationContextGetLaunchParameter_001 + * @tc.desc: Function test with buffer is nullptr and applicationContext is nullptr + * @tc.type: FUNC + */ +HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_ApplicationContextGetLaunchParameter_001, + TestSize.Level2) +{ + char buffer[BUFFER_SIZE] = { 0 }; + int32_t writeLength = 0; + + AbilityRuntime_ErrorCode code = + OH_AbilityRuntime_ApplicationContextGetLaunchParameter(NULL, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID); + ASSERT_EQ(writeLength, 0); + + code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, BUFFER_SIZE, NULL); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID); + + code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, BUFFER_SIZE, nullptr); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID); + + code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, -1, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(writeLength, 0); + + code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, 0, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(writeLength, 0); + + code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(writeLength, 0); +} + +/** + * @tc.number: OH_AbilityRuntime_ApplicationContextGetLaunchParameter_002 + * @tc.desc: Function test + * @tc.type: FUNC + */ +HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_ApplicationContextGetLaunchParameter_002, + TestSize.Level2) +{ + char buffer[BUFFER_SIZE] = { 0 }; + int32_t writeLength = 0; + OHOS::AAFwk::Want want; + std::string abilityName = "testAbility"; + std::string deviceId = "testDeviceId"; + std::string bundleName = "testBundleName"; + want.SetElementName(deviceId, bundleName, abilityName); + EXPECT_EQ(abilityName, want.GetElement().GetAbilityName()); + EXPECT_EQ(deviceId, want.GetElement().GetDeviceID()); + EXPECT_EQ(bundleName, want.GetElement().GetBundleName()); + auto applicationContext = ApplicationContext::GetInstance(); + ASSERT_NE(applicationContext, nullptr); + auto contextImpl = std::make_shared(TEST_BUNDLE_NAME); + ASSERT_NE(contextImpl, nullptr); + AbilityRuntime_ErrorCode code = + OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + + applicationContext->AttachContextImpl(contextImpl); + applicationContext->SetLaunchParameter(want); + code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR); + ASSERT_EQ(want.ToFullString(), buffer); + ASSERT_GT(writeLength, 0); + ASSERT_LT(writeLength, BUFFER_SIZE); +} + +/** + * @tc.number: OH_AbilityRuntime_ApplicationContextGetLatestParameter_001 + * @tc.desc: Function test with buffer is nullptr and applicationContext is nullptr + * @tc.type: FUNC + */ +HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_ApplicationContextGetLatestParameter_001, + TestSize.Level2) +{ + char buffer[BUFFER_SIZE] = { 0 }; + int32_t writeLength = 0; + + AbilityRuntime_ErrorCode code = + OH_AbilityRuntime_ApplicationContextGetLatestParameter(NULL, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID); + ASSERT_EQ(writeLength, 0); + + code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, BUFFER_SIZE, NULL); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID); + + code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, BUFFER_SIZE, nullptr); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID); + + code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, -1, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(writeLength, 0); + + code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, 0, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(writeLength, 0); + + code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(writeLength, 0); +} + +/** + * @tc.number: OH_AbilityRuntime_ApplicationContextGetLatestParameter_002 + * @tc.desc: Function test + * @tc.type: FUNC + */ +HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_ApplicationContextGetLatestParameter_002, + TestSize.Level2) +{ + char buffer[BUFFER_SIZE] = { 0 }; + int32_t writeLength = 0; + OHOS::AAFwk::Want want; + std::string abilityName = "testAbility"; + std::string deviceId = "testDeviceId"; + std::string bundleName = "testBundleName"; + want.SetElementName(deviceId, bundleName, abilityName); + EXPECT_EQ(abilityName, want.GetElement().GetAbilityName()); + EXPECT_EQ(deviceId, want.GetElement().GetDeviceID()); + EXPECT_EQ(bundleName, want.GetElement().GetBundleName()); + auto applicationContext = ApplicationContext::GetInstance(); + ASSERT_NE(applicationContext, nullptr); + auto contextImpl = std::make_shared(TEST_BUNDLE_NAME); + ASSERT_NE(contextImpl, nullptr); + AbilityRuntime_ErrorCode code = + OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + + applicationContext->AttachContextImpl(contextImpl); + applicationContext->SetLatestParameter(want); + code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, BUFFER_SIZE, &writeLength); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR); + ASSERT_EQ(want.ToFullString(), buffer); + ASSERT_GT(writeLength, 0); + ASSERT_LT(writeLength, BUFFER_SIZE); +} + /** * @tc.number: ConvertToCommonBusinessErrorCode_001 * @tc.desc: ConvertToCommonBusinessErrorCode -- Gitee