From 6a9bd6503d3cc680ef87636f514bafbf2cd95833 Mon Sep 17 00:00:00 2001 From: zhangyuhang72 Date: Tue, 17 Jun 2025 18:03:17 +0800 Subject: [PATCH] =?UTF-8?q?OTA=E5=8D=87=E7=BA=A7=E5=90=8E=E5=B8=B8?= =?UTF-8?q?=E9=A9=BB=E5=8F=98=E4=B8=BA=E9=9D=9E=E5=B8=B8=E9=A9=BB=E6=97=B6?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=95=B0=E6=8D=AE=E5=BA=93=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangyuhang72 Change-Id: I927399ecfc924959d9876b8494362b2a6df5f08e --- .../rdb/ability_resident_process_rdb.h | 2 +- .../src/rdb/ability_resident_process_rdb.cpp | 2 +- .../resident_process_manager.cpp | 6 ++ .../abilitymgr/src/utils/keep_alive_utils.cpp | 4 +- .../include/ability_resident_process_rdb.h | 58 +++++++++++++++++++ .../resident_process_manager_test.cpp | 37 ++++++++++++ 6 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 test/unittest/resident_process_manager_test/mock/include/ability_resident_process_rdb.h diff --git a/services/abilitymgr/include/rdb/ability_resident_process_rdb.h b/services/abilitymgr/include/rdb/ability_resident_process_rdb.h index c471f86e93..095fb425b7 100644 --- a/services/abilitymgr/include/rdb/ability_resident_process_rdb.h +++ b/services/abilitymgr/include/rdb/ability_resident_process_rdb.h @@ -78,7 +78,7 @@ public: int32_t VerifyConfigurationPermissions(const std::string &bundleName, const std::string &callerName); int32_t GetResidentProcessEnable(const std::string &bundleName, bool &enable); int32_t UpdateResidentProcessEnable(const std::string &bundleName, bool enable); - int32_t RemoveData(std::string &bundleName); + int32_t RemoveData(const std::string &bundleName); int32_t GetResidentProcessRawData(const std::string &bundleName, const std::string &callerName); private: std::unique_ptr rdbMgr_ = nullptr; diff --git a/services/abilitymgr/src/rdb/ability_resident_process_rdb.cpp b/services/abilitymgr/src/rdb/ability_resident_process_rdb.cpp index f5cebd9712..fe7b11fe0b 100644 --- a/services/abilitymgr/src/rdb/ability_resident_process_rdb.cpp +++ b/services/abilitymgr/src/rdb/ability_resident_process_rdb.cpp @@ -236,7 +236,7 @@ int32_t AmsResidentProcessRdb::UpdateResidentProcessEnable(const std::string &bu return rdbMgr_->UpdateData(valuesBucket, absRdbPredicates); } -int32_t AmsResidentProcessRdb::RemoveData(std::string &bundleName) +int32_t AmsResidentProcessRdb::RemoveData(const std::string &bundleName) { if (bundleName.empty()) { TAG_LOGE(AAFwkTag::ABILITYMGR, "null bundleName"); diff --git a/services/abilitymgr/src/resident_process/resident_process_manager.cpp b/services/abilitymgr/src/resident_process/resident_process_manager.cpp index 015c34e689..a6f77c07a1 100644 --- a/services/abilitymgr/src/resident_process/resident_process_manager.cpp +++ b/services/abilitymgr/src/resident_process/resident_process_manager.cpp @@ -93,6 +93,12 @@ void ResidentProcessManager::StartResidentProcessWithMainElementPerBundle(const bool keepAliveEnable = bundleInfo.isKeepAlive; // Check startup permissions AmsResidentProcessRdb::GetInstance().GetResidentProcessEnable(bundleInfo.name, keepAliveEnable); + if (!bundleInfo.isKeepAlive && keepAliveEnable) { + auto &bundleName = bundleInfo.name; + TAG_LOGI(AAFwkTag::ABILITYMGR, "keepAliveBundle changed, bundle:%{public}s", bundleName.c_str()); + AmsResidentProcessRdb::GetInstance().RemoveData(bundleName); + keepAliveEnable = false; + } TAG_LOGI(AAFwkTag::ABILITYMGR, "Precheck,bundle:%{public}s, process:%{public}s, keepAlive:%{public}d, enable:%{public}d", bundleInfo.name.c_str(), processName.c_str(), bundleInfo.isKeepAlive, keepAliveEnable); diff --git a/services/abilitymgr/src/utils/keep_alive_utils.cpp b/services/abilitymgr/src/utils/keep_alive_utils.cpp index 20c5fab15c..d57eb6f265 100644 --- a/services/abilitymgr/src/utils/keep_alive_utils.cpp +++ b/services/abilitymgr/src/utils/keep_alive_utils.cpp @@ -49,7 +49,9 @@ bool KeepAliveUtils::IsKeepAliveBundle(const AppExecFwk::BundleInfo &bundleInfo, } bool keepAliveEnable = bundleInfo.isKeepAlive; - AbilityRuntime::AmsResidentProcessRdb::GetInstance().GetResidentProcessEnable(bundleInfo.name, keepAliveEnable); + if (keepAliveEnable) { + AbilityRuntime::AmsResidentProcessRdb::GetInstance().GetResidentProcessEnable(bundleInfo.name, keepAliveEnable); + } if (keepAliveEnable) { type = KeepAliveType::RESIDENT_PROCESS; } diff --git a/test/unittest/resident_process_manager_test/mock/include/ability_resident_process_rdb.h b/test/unittest/resident_process_manager_test/mock/include/ability_resident_process_rdb.h new file mode 100644 index 0000000000..e9bbf30bcb --- /dev/null +++ b/test/unittest/resident_process_manager_test/mock/include/ability_resident_process_rdb.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 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 + * + * 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 MOCK_OHOS_ABILITY_RUNTIME_RDB_ABILITY_RESIDENT_PROCESS_RDB_H +#define MOCK_OHOS_ABILITY_RUNTIME_RDB_ABILITY_RESIDENT_PROCESS_RDB_H + +#include +#include + +namespace OHOS { +namespace AbilityRuntime { +enum RdbResult : int32_t { + Rdb_OK = 0, + /* Representative database initialization failed */ + Rdb_Init_Err, + /* Failed to parse initialization file */ + Rdb_Parse_File_Err, + /* Parameter check failed */ + Rdb_Parameter_Err, + /* Failed to query permission settings for resident processes */ + Rdb_Permissions_Err, + /* Database query failed, key may not exist */ + Rdb_Search_Record_Err +}; + +class AmsResidentProcessRdb final { +public: + AmsResidentProcessRdb() {} + ~AmsResidentProcessRdb() {} + + static AmsResidentProcessRdb &GetInstance() + { + static AmsResidentProcessRdb instance; + return instance; + } + + MOCK_METHOD0(Init, int32_t()); + MOCK_METHOD2(VerifyConfigurationPermissions, int32_t(const std::string &bundleName, const std::string &callerName)); + MOCK_METHOD2(GetResidentProcessEnable, int32_t(const std::string &bundleName, bool &enable)); + MOCK_METHOD2(UpdateResidentProcessEnable, int32_t(const std::string &bundleName, bool enable)); + MOCK_METHOD1(RemoveData, int32_t(const std::string &bundleName)); + MOCK_METHOD2(GetResidentProcessRawData, int32_t(const std::string &bundleName, const std::string &callerName)); +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_RDB_ABILITY_RESIDENT_PROCESS_RDB_H \ No newline at end of file diff --git a/test/unittest/resident_process_manager_test/resident_process_manager_test.cpp b/test/unittest/resident_process_manager_test/resident_process_manager_test.cpp index e6e71246e1..fcfc7f512c 100755 --- a/test/unittest/resident_process_manager_test/resident_process_manager_test.cpp +++ b/test/unittest/resident_process_manager_test/resident_process_manager_test.cpp @@ -185,6 +185,43 @@ HWTEST_F(ResidentProcessManagerTest, StartResidentProcessWithMainElementPerBundl EXPECT_EQ((int)needEraseIndexSet.size(), 1); } +/* + * Feature: ResidentProcessManager + * Function: StartResidentProcessWithMainElementPerBundle + * SubFunction: NA + * FunctionPoints:ResidentProcessManager StartResidentProcessWithMainElementPerBundle + * EnvConditions: NA + * CaseDescription: Verify StartResidentProcessWithMainElementPerBundle + */ +HWTEST_F(ResidentProcessManagerTest, StartResidentProcessWithMainElementPerBundle_002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StartResidentProcessWithMainElementPerBundle_002 start"; + + std::shared_ptr manager = std::make_shared(); + AppExecFwk::BundleInfo bundleInfo; + bundleInfo.isKeepAlive = false; + bundleInfo.name = "com.example.test"; + size_t index = 1; + std::set needEraseIndexSet; + int32_t userId = 0; + + bool keepAliveEnable = true; + EXPECT_CALL(AmsResidentProcessRdb::GetInstance(), GetResidentProcessEnable(_, _)) + .Times(1) + .WillOnce(DoAll(SetArgReferee<1>(keepAliveEnable), Return(RdbResult::Rdb_OK))); + + EXPECT_CALL(AmsResidentProcessRdb::GetInstance(), RemoveData(_)) + .Times(1) + .WillOnce(Return(RdbResult::Rdb_OK)); + + manager->StartResidentProcessWithMainElementPerBundle(bundleInfo, index, needEraseIndexSet, userId); + + + EXPECT_EQ(needEraseIndexSet.size(), 1); + + GTEST_LOG_(INFO) << "StartResidentProcessWithMainElementPerBundle_002 end"; +} + /* * Feature: ResidentProcessManager * Function: PutResidentAbility