From 312f4ae5b25f04137d8776006feb22e7402809d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E7=BB=B4?= Date: Wed, 20 Aug 2025 17:23:32 +0800 Subject: [PATCH 1/5] =?UTF-8?q?description:=E6=94=AF=E6=8C=81uiability?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=90=AF=E5=8A=A8=E5=8F=82=E6=95=B0=E5=8F=8A?= =?UTF-8?q?=E6=9C=80=E8=BF=91=E4=B8=80=E6=AC=A1=E5=90=AF=E5=8A=A8=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 查维 --- .../src/application_context.cpp | 46 ++++++ .../native/ability_runtime/js_ui_ability.cpp | 3 + .../context/application_context.cpp | 28 ++++ .../ability_runtime/context/context_impl.cpp | 29 ++++ .../c/ability_runtime/application_context.h | 35 ++++- .../context/application_context.h | 4 + .../ability_runtime/context/context_impl.h | 11 ++ ...ility_runtime_application_context_test.cpp | 143 ++++++++++++++++++ 8 files changed, 298 insertions(+), 1 deletion(-) diff --git a/frameworks/c/ability_runtime/src/application_context.cpp b/frameworks/c/ability_runtime/src/application_context.cpp index f7df0086b0d..859bcbfb95d 100644 --- a/frameworks/c/ability_runtime/src/application_context.cpp +++ b/frameworks/c/ability_runtime/src/application_context.cpp @@ -303,6 +303,52 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(cons return WriteStringToBuffer(resourceDir, buffer, bufferSize, writeLength); } +AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLaunchParameter( + char* buffer, const int32_t bufferSize, int32_t* writeLength) +{ + TAG_LOGD(AAFwkTag::APPKIT, "%{public}s called", __func__); + 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, "%{public}s called", __func__); + 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); +} + AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want *want) { TAG_LOGD(AAFwkTag::APPKIT, "StartSelfUIAbility called"); 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 cbc1fd0f01a..84ffc929b37 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp @@ -451,6 +451,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_); } @@ -1766,6 +1768,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 48f7491f382..269b1613d87 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -1718,6 +1718,7 @@ int32_t ContextImpl::CreateHspModuleResourceManager(const std::string &bundleNam UpdateResConfig(GetResourceManager(), resourceManager); return ERR_OK; } + bool ContextImpl::UpdateDisplayConfiguration(std::shared_ptr &contextImpl, uint64_t displayId, float density, std::string direction) { @@ -1763,6 +1764,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() const +{ + if (launchParameter_ != nullptr) { + return launchParameter_->ToString(); + } + TAG_LOGW(AAFwkTag::APPKIT, "launchParameter_ is null"); + return ""; +} + +void ContextImpl::SetLatestParameter(const AAFwk::Want& want) +{ + latestParameter_ = std::make_shared(want); +} + +std::string ContextImpl::GetLatestParameter() const +{ + if (latestParameter_ != nullptr) { + return latestParameter_->ToString(); + } + TAG_LOGW(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 0fcab7b5023..2ca83f33146 100644 --- a/interfaces/kits/c/ability_runtime/application_context.h +++ b/interfaces/kits/c/ability_runtime/application_context.h @@ -297,6 +297,40 @@ 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); + /** * @brief Obtain the version code of the application. * @@ -309,7 +343,6 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(cons * @since 21 */ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetVersionCode(int64_t* versionCode); - #ifdef __cplusplus } // extern "C" #endif 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 52a48ac299d..8567d36ba6d 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() const; + + void SetLatestParameter(const AAFwk::Want& want); + + std::string GetLatestParameter() const; + #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 2571a5e36c8..f622831e599 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 @@ -22,6 +22,7 @@ #include "context/application_context.h" #include "securec.h" #include "start_options_impl.h" +#include "string_wrapper.h" #include "want_manager.h" #include "want_utils.h" @@ -2393,6 +2394,148 @@ 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); + OHOS::AAFwk::WantParams wantParams; + wantParams.SetParam("key1", AAFwk::String::Box("value1")); + wantParams.SetParam("key2", AAFwk::String::Box("value2")); + want.SetParams(wantParams); + 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(wantParams.ToString(), 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); + OHOS::AAFwk::WantParams wantParams; + wantParams.SetParam("key1", AAFwk::String::Box("value1")); + wantParams.SetParam("key2", AAFwk::String::Box("value2")); + want.SetParams(wantParams); + 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(wantParams.ToString(), buffer); + ASSERT_GT(writeLength, 0); + ASSERT_LT(writeLength, BUFFER_SIZE); +} + /** * @tc.number: ConvertToCommonBusinessErrorCode_001 * @tc.desc: ConvertToCommonBusinessErrorCode -- Gitee From c13e480b364555a57da277f083f9f24952c44df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E7=BB=B4?= Date: Fri, 22 Aug 2025 16:37:18 +0800 Subject: [PATCH 2/5] =?UTF-8?q?description:=E5=86=85=E9=83=A8=E6=A3=80?= =?UTF-8?q?=E8=A7=86=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 查维 --- .../ability/native/ability_runtime/js_ui_ability.cpp | 1 - .../ability_runtime/context/application_context.cpp | 5 +++-- .../appkit/ability_runtime/context/context_impl.cpp | 11 +++++++++-- .../ability_runtime/context/application_context.h | 4 ++-- .../appkit/ability_runtime/context/context_impl.h | 4 ++-- 5 files changed, 16 insertions(+), 9 deletions(-) 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 84ffc929b37..c45475a3e9d 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp @@ -452,7 +452,6 @@ 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_); } diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index 3b02a7966fb..c3c2ceb27d1 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -670,7 +670,7 @@ std::string ApplicationContext::GetCloudFileDir() return (contextImpl_ != nullptr) ? contextImpl_->GetCloudFileDir() : ""; } -std::string ApplicationContext::GetLaunchParameter() +std::string ApplicationContext::GetLaunchParameter() const { return (contextImpl_ != nullptr) ? contextImpl_->GetLaunchParameter() : ""; } @@ -682,9 +682,10 @@ void ApplicationContext::SetLaunchParameter(const AAFwk::Want &want) return; } contextImpl_->SetLaunchParameter(want); + contextImpl_->SetLatestParameter(want); } -std::string ApplicationContext::GetLatestParameter() +std::string ApplicationContext::GetLatestParameter() const { return (contextImpl_ != nullptr) ? contextImpl_->GetLatestParameter() : ""; } diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index 269b1613d87..8d302a111a8 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -1766,7 +1766,10 @@ bool ContextImpl::UpdateDisplayConfiguration(std::shared_ptr &conte void ContextImpl::SetLaunchParameter(const AAFwk::Want& want) { - launchParameter_ = std::make_shared(want); + // only cold-start need save parameter + if (!launchParameter_) { + launchParameter_ = std::make_shared(want.GetParams()); + } } std::string ContextImpl::GetLaunchParameter() const @@ -1780,7 +1783,11 @@ std::string ContextImpl::GetLaunchParameter() const void ContextImpl::SetLatestParameter(const AAFwk::Want& want) { - latestParameter_ = std::make_shared(want); + if (!latestParameter_) { + latestParameter_ = std::make_shared(want.GetParams()); + } else { + *latestParameter_ = want.GetParams(); + } } std::string ContextImpl::GetLatestParameter() const 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 00193f2062f..db4897edd2d 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -167,8 +167,8 @@ public: std::string GetDataDir(); void SetLaunchParameter(const AAFwk::Want &want); void SetLatestParameter(const AAFwk::Want &want); - std::string GetLaunchParameter(); - std::string GetLatestParameter(); + std::string GetLaunchParameter() const; + std::string GetLatestParameter() const; 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 8567d36ba6d..2e6f84d84b8 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h @@ -550,6 +550,8 @@ private: std::shared_ptr resourceManager_ = nullptr; std::shared_ptr hapModuleInfo_ = nullptr; std::shared_ptr config_ = nullptr; + std::shared_ptr launchParameter_ = nullptr; + std::shared_ptr latestParameter_ = nullptr; std::string currArea_ = "el2"; std::vector overlayModuleInfos_; std::set checkedDirSet_; @@ -567,8 +569,6 @@ 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_; -- Gitee From 5be7ffd82f957955286d140d82726c632ef01419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E7=BB=B4?= Date: Fri, 22 Aug 2025 17:08:00 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 查维 --- .../appkit/ability_runtime/context/application_context.cpp | 2 +- .../native/appkit/ability_runtime/context/application_context.h | 2 +- .../capi_ability_runtime_application_context_test.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index c3c2ceb27d1..5f57c32f64d 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h index db4897edd2d..599d93f9d24 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/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 f622831e599..0f184b9d8cc 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 -- Gitee From 2f632e929af70cab8c12ebe0f56d63b10fdff76b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E7=BB=B4?= Date: Tue, 26 Aug 2025 09:53:58 +0800 Subject: [PATCH 4/5] =?UTF-8?q?description:=E6=A3=80=E8=A7=86=E6=84=8F?= =?UTF-8?q?=E8=A7=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 查维 --- .../src/application_context.cpp | 10 ------- .../ability_runtime/context/context_impl.cpp | 8 +++-- .../c/ability_runtime/application_context.h | 30 +++++++++---------- .../ability_runtime/context/context_impl.h | 10 ++++--- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/frameworks/c/ability_runtime/src/application_context.cpp b/frameworks/c/ability_runtime/src/application_context.cpp index 859bcbfb95d..a625ffc6a81 100644 --- a/frameworks/c/ability_runtime/src/application_context.cpp +++ b/frameworks/c/ability_runtime/src/application_context.cpp @@ -318,11 +318,6 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLaunchParameter( } 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); } @@ -341,11 +336,6 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( } 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); } diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index 8d302a111a8..57114de9547 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -1767,13 +1767,15 @@ bool ContextImpl::UpdateDisplayConfiguration(std::shared_ptr &conte void ContextImpl::SetLaunchParameter(const AAFwk::Want& want) { // only cold-start need save parameter + std::lock_guard lock(launchParameterMutex_); if (!launchParameter_) { launchParameter_ = std::make_shared(want.GetParams()); } } -std::string ContextImpl::GetLaunchParameter() const +std::string ContextImpl::GetLaunchParameter() { + std::lock_guard lock(launchParameterMutex_); if (launchParameter_ != nullptr) { return launchParameter_->ToString(); } @@ -1783,6 +1785,7 @@ std::string ContextImpl::GetLaunchParameter() const void ContextImpl::SetLatestParameter(const AAFwk::Want& want) { + std::lock_guard lock(latestParameterMutex_); if (!latestParameter_) { latestParameter_ = std::make_shared(want.GetParams()); } else { @@ -1790,8 +1793,9 @@ void ContextImpl::SetLatestParameter(const AAFwk::Want& want) } } -std::string ContextImpl::GetLatestParameter() const +std::string ContextImpl::GetLatestParameter() { + std::lock_guard lock(latestParameterMutex_); if (latestParameter_ != nullptr) { return latestParameter_->ToString(); } diff --git a/interfaces/kits/c/ability_runtime/application_context.h b/interfaces/kits/c/ability_runtime/application_context.h index 2ca83f33146..b89be845b3e 100644 --- a/interfaces/kits/c/ability_runtime/application_context.h +++ b/interfaces/kits/c/ability_runtime/application_context.h @@ -297,6 +297,19 @@ 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 version code of the application. + * + * @param versionCode The version code of the application. + * @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 versionCode is null. + * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the application context does not exist. + * {@link ABILITY_RUNTIME_ERROR_CODE_GET_APPLICATION_INFO_FAILED} if the application info does not exist. + * @since 21 + */ +AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetVersionCode(int64_t* versionCode); + /** * @brief Obtain the launch parameter of starting UIAbility. * @@ -309,7 +322,7 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(cons * {@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 + * @since 21 */ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLaunchParameter( char* buffer, const int32_t bufferSize, int32_t* writeLength); @@ -326,23 +339,10 @@ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLaunchParameter( * {@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 + * @since 21 */ AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetLatestParameter( char* buffer, const int32_t bufferSize, int32_t* writeLength); - -/** - * @brief Obtain the version code of the application. - * - * @param versionCode The version code of the application. - * @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 versionCode is null. - * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the application context does not exist. - * {@link ABILITY_RUNTIME_ERROR_CODE_GET_APPLICATION_INFO_FAILED} if the application info does not exist. - * @since 21 - */ -AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetVersionCode(int64_t* versionCode); #ifdef __cplusplus } // extern "C" #endif 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 2e6f84d84b8..26bd763825a 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h @@ -425,11 +425,11 @@ public: void SetLaunchParameter(const AAFwk::Want& want); - std::string GetLaunchParameter() const; + std::string GetLaunchParameter(); void SetLatestParameter(const AAFwk::Want& want); - std::string GetLatestParameter() const; + std::string GetLatestParameter(); #ifdef SUPPORT_GRAPHICS /** @@ -550,8 +550,6 @@ private: std::shared_ptr resourceManager_ = nullptr; std::shared_ptr hapModuleInfo_ = nullptr; std::shared_ptr config_ = nullptr; - std::shared_ptr launchParameter_ = nullptr; - std::shared_ptr latestParameter_ = nullptr; std::string currArea_ = "el2"; std::vector overlayModuleInfos_; std::set checkedDirSet_; @@ -569,6 +567,10 @@ private: std::mutex overlaySubscriberMutex_; std::shared_ptr overlaySubscriber_; std::string processName_; + std::mutex launchParameterMutex_; + std::shared_ptr launchParameter_ = nullptr; + std::mutex latestParameterMutex_; + std::shared_ptr latestParameter_ = nullptr; #ifdef SUPPORT_GRAPHICS static std::mutex getDisplayConfigCallbackMutex_; -- Gitee From 2abcafde5287a4ab8a2e8de6595a77426e42090a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E7=BB=B4?= Date: Tue, 26 Aug 2025 19:33:09 +0800 Subject: [PATCH 5/5] =?UTF-8?q?description:=E4=BF=AE=E6=94=B9tdd=20fail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 查维 --- .../capi_ability_runtime_application_context_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 0f184b9d8cc..ebde4411163 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 @@ -2454,7 +2454,7 @@ HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_Application ASSERT_NE(contextImpl, nullptr); AbilityRuntime_ErrorCode code = OH_AbilityRuntime_ApplicationContextGetLaunchParameter(buffer, BUFFER_SIZE, &writeLength); - ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR); applicationContext->AttachContextImpl(contextImpl); applicationContext->SetLaunchParameter(want); @@ -2525,7 +2525,7 @@ HWTEST_F(CapiAbilityRuntimeApplicationContextTest, OH_AbilityRuntime_Application ASSERT_NE(contextImpl, nullptr); AbilityRuntime_ErrorCode code = OH_AbilityRuntime_ApplicationContextGetLatestParameter(buffer, BUFFER_SIZE, &writeLength); - ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST); + ASSERT_EQ(code, ABILITY_RUNTIME_ERROR_CODE_NO_ERROR); applicationContext->AttachContextImpl(contextImpl); applicationContext->SetLatestParameter(want); -- Gitee