From a90d87ac8922a8f0847cba9ab69476dd99602aae Mon Sep 17 00:00:00 2001 From: chensihan Date: Tue, 26 Nov 2024 17:17:27 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=A4=9A=E7=94=A8=E6=88=B7=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=B8=82=E5=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chensihan --- services/backup_sa/BUILD.gn | 1 + .../app_gallery_dispose_proxy.h | 22 +++-- .../app_gallery_service_connection.h | 61 +++--------- .../app_gallery_dispose_proxy.cpp | 92 +++++++++++++------ .../app_gallery_service_connection.cpp | 76 +++++++++++++++ services/backup_sa/src/module_ipc/service.cpp | 10 +- 6 files changed, 174 insertions(+), 88 deletions(-) create mode 100644 services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index b7ba886e9..8c877bed7 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -28,6 +28,7 @@ ohos_shared_library("backup_sa") { sources = [ "src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "src/module_app_gallery/app_gallery_service_connection.cpp", "src/module_external/bms_adapter.cpp", "src/module_external/sms_adapter.cpp", "src/module_ipc/sa_backup_connection.cpp", diff --git a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h index e98a16dbe..12f271baf 100644 --- a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h +++ b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h @@ -17,10 +17,12 @@ #define OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_H #include +#include #include #include +#include "app_gallery_service_connection.h" #include "i_appgallery_service.h" namespace OHOS::FileManagement::Backup { @@ -39,24 +41,24 @@ public: static sptr GetInstance(); - DisposeErr StartBackup(const std::string &bundleName); - DisposeErr EndBackup(const std::string &bundleName); - DisposeErr StartRestore(const std::string &bundleName); - DisposeErr EndRestore(const std::string &bundleName); + DisposeErr StartBackup(const std::string &bundleName, int32_t userId); + DisposeErr EndBackup(const std::string &bundleName, int32_t userId); + DisposeErr StartRestore(const std::string &bundleName, int32_t userId); + DisposeErr EndRestore(const std::string &bundleName, int32_t userId); + + bool HasAppGalleryConnection(int32_t userId); + sptr GetAppGalleryConnection(int32_t userId); + sptr ConnectAppGallery(int32_t userId); - static std::condition_variable conditionVal_; static std::string abilityName; static std::string pkgName; - static std::mutex appRemoteObjLock_; - static std::mutex connectMutex; - static sptr appRemoteObj_; + static std::map> appGalleryConnectionMap_; private: - DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation); + DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, int32_t userId); private: static std::mutex instanceLock_; - static std::mutex conditionMutex_; static sptr appGalleryDisposeProxyInstance_; }; } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h b/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h index c5a06b7fc..30a2982e6 100644 --- a/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h +++ b/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 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 @@ -18,67 +18,36 @@ #include #include -#include #include "ability_manager_client.h" #include "ability_connect_callback_stub.h" #include "want.h" #include "i_appgallery_service.h" -#include "module_external/bms_adapter.h" -#include "filemgmt_libhilog.h" namespace OHOS::FileManagement::Backup { using namespace OHOS::AppExecFwk; -const int32_t CONNECT_TIME = 3; -template class AppGralleryConnection : public AbilityConnectionStub { +class AppGalleryConnection : public AbilityConnectionStub { public: - void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, - int resultCode) - { - std::string uri = element.GetURI(); - HILOGI("OnAbilityConnectDone, uri = %{public}s", uri.c_str()); - T::appRemoteObj_ = remoteObject; - T::conditionVal_.notify_one(); - } + AppGalleryConnection(int32_t userId) : userId(userId) {} - void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) - { - std::string uri = element.GetURI(); - HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str()); - T::appRemoteObj_ = nullptr; - } -}; + void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr &remoteObject, + int resultCode); -template bool ConnectExtAbility() -{ - HILOGI("ConnectExtAbility called"); - std::lock_guard autoLock(T::appRemoteObjLock_); - if (T::appRemoteObj_ != nullptr) { - return true; - } + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode); - auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); - if (appGalleryBundleName.empty()) { - HILOGI("ConnectExtAbility GetAppGalleryBundleName failed"); - return false; - } + bool ConnectExtAbility(std::string abilityName); - Want want; - want.SetElementName(appGalleryBundleName.c_str(), T::abilityName); - sptr connect = new AppGralleryConnection(); - auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, connect, -1); + sptr GetRemoteObj(); - std::unique_lock uniqueLock(T::connectMutex); - T::conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME)); - if (ret != IAppGalleryService::ERR_OK || T::appRemoteObj_ == nullptr) { - HILOGI("ConnectExtAbility failed, ret=%{public}d", ret); - T::appRemoteObj_ = nullptr; - return false; - } - HILOGI("ConnectExtAbility success, ret=%{public}d", ret); - return true; +private: + int32_t userId; + std::mutex conditionMutex_; + std::condition_variable conditionVal_; + std::mutex connectMutex; + std::mutex appRemoteObjLock_; + sptr appRemoteObj_; }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H \ No newline at end of file diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index e6817ebd9..7a6fb815f 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -15,6 +15,7 @@ #include +#include "b_error/b_error.h" #include "b_radar/b_radar.h" #include "b_sa/b_sa_utils.h" #include "b_jsonutil/b_jsonutil.h" @@ -32,13 +33,9 @@ namespace { const auto APP_FOUNDATION_SERVICE = u"appgalleryservice.openapi.privacymanager.AppFoundationService"; } mutex AppGalleryDisposeProxy::instanceLock_; -mutex AppGalleryDisposeProxy::conditionMutex_; string AppGalleryDisposeProxy::abilityName = "AppFoundationService"; -sptr AppGalleryDisposeProxy::appRemoteObj_; -condition_variable AppGalleryDisposeProxy::conditionVal_; -mutex AppGalleryDisposeProxy::appRemoteObjLock_; -mutex AppGalleryDisposeProxy::connectMutex; +std::map> AppGalleryDisposeProxy::appGalleryConnectionMap_; sptr AppGalleryDisposeProxy::appGalleryDisposeProxyInstance_; AppGalleryDisposeProxy::AppGalleryDisposeProxy() @@ -63,81 +60,81 @@ sptr AppGalleryDisposeProxy::GetInstance() return appGalleryDisposeProxyInstance_; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, int32_t userId) { HILOGI("StartBackup, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::START_BACKUP); + DisposeErr res = DoDispose(bundleName, DisposeOperation::START_BACKUP, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", userId, BizStageBackup::BIZ_STAGE_START_DISPOSE, static_cast(res)); } return res; } -DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, int32_t userId) { HILOGI("EndBackup, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::END_BACKUP); + DisposeErr res = DoDispose(bundleName, DisposeOperation::END_BACKUP, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", userId, BizStageBackup::BIZ_STAGE_END_DISPOSE, static_cast(res)); } return res; } -DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, int32_t userId) { if (SAUtils::IsSABundleName(bundleName)) { HILOGI("SA does not need to StartRestore"); return DisposeErr::OK; } HILOGI("StartRestore, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::START_RESTORE); + DisposeErr res = DoDispose(bundleName, DisposeOperation::START_RESTORE, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", userId, BizStageRestore::BIZ_STAGE_START_DISPOSE, static_cast(res)); } return res; } -DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, int32_t userId) { if (SAUtils::IsSABundleName(bundleName)) { HILOGI("SA does not need to EndRestore"); return DisposeErr::OK; } HILOGI("EndRestore, app %{public}s", bundleName.c_str()); - DisposeErr res = DoDispose(bundleName, DisposeOperation::END_RESTORE); + DisposeErr res = DoDispose(bundleName, DisposeOperation::END_RESTORE, userId); if (res != DisposeErr::REQUEST_FAIL) { AppRadar::Info info(bundleName, "", ""); - AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", userId, BizStageRestore::BIZ_STAGE_END_DISPOSE, static_cast(res)); } return res; } void RecordDoDisposeRes(const std::string &bundleName, - AppGalleryDisposeProxy::DisposeOperation disposeOperation, int32_t err) + AppGalleryDisposeProxy::DisposeOperation disposeOperation, int32_t userId, int32_t err) { AppRadar::Info info (bundleName, "", "REQUEST FAIL"); switch (disposeOperation) { case AppGalleryDisposeProxy::DisposeOperation::START_BACKUP: - AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "StartBackup", userId, BizStageBackup::BIZ_STAGE_START_DISPOSE, err); break; case AppGalleryDisposeProxy::DisposeOperation::END_BACKUP: - AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordBackupFuncRes(info, "EndBackup", userId, BizStageBackup::BIZ_STAGE_END_DISPOSE, err); break; case AppGalleryDisposeProxy::DisposeOperation::START_RESTORE: - AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "StartRestore", userId, BizStageRestore::BIZ_STAGE_START_DISPOSE, err); break; case AppGalleryDisposeProxy::DisposeOperation::END_RESTORE: - AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", AppRadar::GetInstance().GetUserId(), + AppRadar::GetInstance().RecordRestoreFuncRes(info, "EndRestore", userId, BizStageRestore::BIZ_STAGE_END_DISPOSE, err); break; default: @@ -145,12 +142,53 @@ void RecordDoDisposeRes(const std::string &bundleName, } } -DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) +bool AppGalleryDisposeProxy::HasAppGalleryConnection(int32_t userId) +{ + auto it = appGalleryConnectionMap_.find(userId); + if (it == appGalleryConnectionMap_.end()) { + HILOGI("appGalleryConnectionMap not contain %{public}d", userId); + return false; + } + return true; +} + +sptr AppGalleryDisposeProxy::GetAppGalleryConnection(int32_t userId) +{ + if (HasAppGalleryConnection(userId)) { + HILOGI("appGalleryConnectionMap contain %{public}d", userId); + return appGalleryConnectionMap_[userId]; + } + return nullptr; +} + +sptr AppGalleryDisposeProxy::ConnectAppGallery(int32_t userId) +{ + sptr connection = GetAppGalleryConnection(userId); + if (connection == nullptr) { + connection = sptr(new AppGalleryConnection(userId)); + if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { + HILOGE("Can not connect to AppGallery"); + return nullptr; + } + appGalleryConnectionMap_[userId] = connection; + } else if (connection->GetRemoteObj() == nullptr) { + HILOGI("AppGalleryConnection try to reconnect, userId = %{public}d", userId); + if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { + HILOGE("Can not connect to AppGallery"); + return nullptr; + } + } + return connection; +} + +DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, + int32_t userId) { try { HILOGI("DoDispose, app %{public}s, operation %{public}d", bundleName.c_str(), disposeOperation); - if (!ConnectExtAbility() || appRemoteObj_ == nullptr) { - HILOGE("Can not connect to %{public}s", bundleName.c_str()); + sptr connection = ConnectAppGallery(userId); + if (connection == nullptr) { + HILOGE("Get AppGalleryConnection failed, userId = %{public}d", userId); return DisposeErr::CONN_FAIL; } @@ -172,11 +210,11 @@ DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, Disp MessageParcel reply; MessageOption option; - int32_t ret = appRemoteObj_->SendRequest(static_cast(disposeOperation), data, reply, option); + int32_t ret = connection->GetRemoteObj()->SendRequest(static_cast(disposeOperation), data, reply, option); if (ret != ERR_NONE) { HILOGE("SendRequest error, code=%{public}d, bundleName=%{public}s , appindex =%{public}d", ret, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex); - RecordDoDisposeRes(bundleName, disposeOperation, ret); + RecordDoDisposeRes(bundleName, disposeOperation, userId, ret); return DisposeErr::REQUEST_FAIL; } diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp new file mode 100644 index 000000000..7fd274f10 --- /dev/null +++ b/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 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 "module_app_gallery/app_gallery_service_connection.h" + +#include +#include + +#include "module_external/bms_adapter.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup { +const int32_t CONNECT_TIME = 3; + +void AppGalleryConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element, + const sptr &remoteObject, int resultCode) +{ + std::string uri = element.GetURI(); + HILOGI("OnAbilityConnectDone, uri = %{public}s", uri.c_str()); + appRemoteObj_ = remoteObject; + conditionVal_.notify_one(); +} + +void AppGalleryConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + std::string uri = element.GetURI(); + HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str()); + appRemoteObj_ = nullptr; +} + +bool AppGalleryConnection::ConnectExtAbility(std::string abilityName) +{ + HILOGI("ConnectExtAbility called"); + std::lock_guard autoLock(appRemoteObjLock_); + if (appRemoteObj_ != nullptr) { + return true; + } + + auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); + if (appGalleryBundleName.empty()) { + HILOGI("ConnectExtAbility GetAppGalleryBundleName failed"); + return false; + } + + Want want; + want.SetElementName(appGalleryBundleName.c_str(), abilityName); + auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, this, userId); + + std::unique_lock uniqueLock(connectMutex); + conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME)); + if (ret != IAppGalleryService::ERR_OK || appRemoteObj_ == nullptr) { + HILOGI("ConnectExtAbility failed, ret=%{public}d", ret); + appRemoteObj_ = nullptr; + return false; + } + HILOGI("ConnectExtAbility success, ret=%{public}d", ret); + return true; +} + +sptr AppGalleryConnection::GetRemoteObj() +{ + return appRemoteObj_; +} +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 463df88a0..909f07ab1 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1173,7 +1173,7 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, session_->ValidRestoreDataType(RestoreTypeEnum::RESTORE_DATA_WAIT_SEND)) { session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } else if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -1182,7 +1182,7 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1403,7 +1403,7 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) return; } HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, GetUserIdDefault()); HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1419,7 +1419,7 @@ void Service::SendEndAppGalleryNotify(const BundleName &bundleName) if (scenario != IServiceReverse::Scenario::RESTORE) { return; } - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); if (disposeErr != DisposeErr::OK) { @@ -1438,7 +1438,7 @@ void Service::TryToClearDispose(const BundleName &bundleName) int32_t maxAtt = MAX_TRY_CLEAR_DISPOSE_NUM; int32_t att = 0; while (att < maxAtt) { - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); if (disposeErr == DisposeErr::OK) { break; -- Gitee From 230cd0580751366a18fab60101183897c488ece3 Mon Sep 17 00:00:00 2001 From: chensihan Date: Thu, 28 Nov 2024 10:14:06 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dut=20Signed-off-by:=20che?= =?UTF-8?q?nsihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app_gallery_dispose_proxy.cpp | 15 +++++++------ .../app_gallery_service_connection.cpp | 8 +++---- .../app_gallery_dispose_proxy_mock.cpp | 19 ++++++++-------- .../include/app_gallery_dispose_proxy_mock.h | 16 +++++++------- .../src/app_gallery_dispose_proxy_mock.cpp | 16 +++++++------- tests/unittests/backup_sa/module_ipc/BUILD.gn | 1 + .../backup_sa/module_ipc/sub_service_test.cpp | 22 +++++++++---------- 7 files changed, 50 insertions(+), 47 deletions(-) diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index 7a6fb815f..e9c20f85c 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -167,14 +167,14 @@ sptr AppGalleryDisposeProxy::ConnectAppGallery(int32_t use if (connection == nullptr) { connection = sptr(new AppGalleryConnection(userId)); if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { - HILOGE("Can not connect to AppGallery"); + HILOGE("AppGalleryConnection failed to connect, userId = %{public}d", userId); return nullptr; } appGalleryConnectionMap_[userId] = connection; } else if (connection->GetRemoteObj() == nullptr) { HILOGI("AppGalleryConnection try to reconnect, userId = %{public}d", userId); if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { - HILOGE("Can not connect to AppGallery"); + HILOGE("AppGalleryConnection failed to connect, userId = %{public}d", userId); return nullptr; } } @@ -185,7 +185,8 @@ DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, Disp int32_t userId) { try { - HILOGI("DoDispose, app %{public}s, operation %{public}d", bundleName.c_str(), disposeOperation); + HILOGI("DoDispose, app %{public}s, operation %{public}d, userId %{public}d", bundleName.c_str(), + disposeOperation, userId); sptr connection = ConnectAppGallery(userId); if (connection == nullptr) { HILOGE("Get AppGalleryConnection failed, userId = %{public}d", userId); @@ -212,14 +213,14 @@ DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, Disp MessageOption option; int32_t ret = connection->GetRemoteObj()->SendRequest(static_cast(disposeOperation), data, reply, option); if (ret != ERR_NONE) { - HILOGE("SendRequest error, code=%{public}d, bundleName=%{public}s , appindex =%{public}d", - ret, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex); + HILOGE("SendRequest error, code=%{public}d, bundleName=%{public}s , appindex=%{public}d, userId=%{public}d", + ret, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex, userId); RecordDoDisposeRes(bundleName, disposeOperation, userId, ret); return DisposeErr::REQUEST_FAIL; } - HILOGI("SendRequest success, dispose=%{public}d, bundleName=%{public}s, appindex =%{public}d", - disposeOperation, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex); + HILOGI("SendRequest success, dispose=%{public}d, bundleName=%{public}s, appindex=%{public}d, userId=%{public}d", + disposeOperation, bundleDetailInfo.bundleName.c_str(), bundleDetailInfo.bundleIndex, userId); return DisposeErr::OK; } catch (const BError &e) { HILOGE("Catch exception, errCode = %{public}d", e.GetCode()); diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp index 7fd274f10..10b2c19c1 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp @@ -42,7 +42,7 @@ void AppGalleryConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName bool AppGalleryConnection::ConnectExtAbility(std::string abilityName) { - HILOGI("ConnectExtAbility called"); + HILOGI("ConnectExtAbility called, userId = %{public}d", userId); std::lock_guard autoLock(appRemoteObjLock_); if (appRemoteObj_ != nullptr) { return true; @@ -50,7 +50,7 @@ bool AppGalleryConnection::ConnectExtAbility(std::string abilityName) auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); if (appGalleryBundleName.empty()) { - HILOGI("ConnectExtAbility GetAppGalleryBundleName failed"); + HILOGI("ConnectExtAbility GetAppGalleryBundleName failed, userId = %{public}d", userId); return false; } @@ -61,11 +61,11 @@ bool AppGalleryConnection::ConnectExtAbility(std::string abilityName) std::unique_lock uniqueLock(connectMutex); conditionVal_.wait_for(uniqueLock, std::chrono::seconds(CONNECT_TIME)); if (ret != IAppGalleryService::ERR_OK || appRemoteObj_ == nullptr) { - HILOGI("ConnectExtAbility failed, ret=%{public}d", ret); + HILOGI("ConnectExtAbility failed, ret=%{public}d, userId = %{public}d", ret, userId); appRemoteObj_ = nullptr; return false; } - HILOGI("ConnectExtAbility success, ret=%{public}d", ret); + HILOGI("ConnectExtAbility success, ret=%{public}d, userId = %{public}d", ret, userId); return true; } diff --git a/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp b/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp index ca6ce6a2b..f0d9ec85c 100644 --- a/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp +++ b/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp @@ -41,27 +41,28 @@ sptr AppGalleryDisposeProxy::GetInstance() return new AppGalleryDisposeProxy; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, int32_t userId) { - return DoDispose(bundleName, DisposeOperation::START_BACKUP); + return DoDispose(bundleName, DisposeOperation::START_BACKUP, userId); } -DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, int32_t userId) { - return DoDispose(bundleName, DisposeOperation::END_BACKUP); + return DoDispose(bundleName, DisposeOperation::END_BACKUP, userId); } -DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, int32_t userId) { - return DoDispose(bundleName, DisposeOperation::START_RESTORE); + return DoDispose(bundleName, DisposeOperation::START_RESTORE, userId); } -DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, int32_t userId) { - return DoDispose(bundleName, DisposeOperation::END_RESTORE); + return DoDispose(bundleName, DisposeOperation::END_RESTORE, userId); } -DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) +DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, + int32_t userId) { return DisposeErr::OK; } diff --git a/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h b/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h index 190bea71a..4ba078e91 100644 --- a/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h +++ b/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h @@ -23,10 +23,10 @@ namespace OHOS::FileManagement::Backup { class BAppGalleryDisposeProxy { public: - virtual DisposeErr StartBackup(const std::string&) = 0; - virtual DisposeErr EndBackup(const std::string&) = 0; - virtual DisposeErr StartRestore(const std::string&) = 0; - virtual DisposeErr EndRestore(const std::string&) = 0; + virtual DisposeErr StartBackup(const std::string&, int32_t) = 0; + virtual DisposeErr EndBackup(const std::string&, int32_t) = 0; + virtual DisposeErr StartRestore(const std::string&, int32_t) = 0; + virtual DisposeErr EndRestore(const std::string&, int32_t) = 0; public: BAppGalleryDisposeProxy() = default; virtual ~BAppGalleryDisposeProxy() = default; @@ -36,10 +36,10 @@ public: class AppGalleryDisposeProxyMock : public BAppGalleryDisposeProxy { public: - MOCK_METHOD(DisposeErr, StartBackup, (const std::string&)); - MOCK_METHOD(DisposeErr, EndBackup, (const std::string&)); - MOCK_METHOD(DisposeErr, StartRestore, (const std::string&)); - MOCK_METHOD(DisposeErr, EndRestore, (const std::string&)); + MOCK_METHOD(DisposeErr, StartBackup, (const std::string&, int32_t)); + MOCK_METHOD(DisposeErr, EndBackup, (const std::string&, int32_t)); + MOCK_METHOD(DisposeErr, StartRestore, (const std::string&, int32_t)); + MOCK_METHOD(DisposeErr, EndRestore, (const std::string&, int32_t)); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_MOCK_H \ No newline at end of file diff --git a/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp b/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp index 945676d7e..eab1cfcae 100644 --- a/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp +++ b/tests/mock/module_ipc/src/app_gallery_dispose_proxy_mock.cpp @@ -42,23 +42,23 @@ sptr AppGalleryDisposeProxy::GetInstance() return appGalleryDisposeProxyInstance_; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->StartBackup(bundleName); + return BAppGalleryDisposeProxy::proxy->StartBackup(bundleName, userId); } -DisposeErr AppGalleryDisposeProxy::EndBackup(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndBackup(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->EndBackup(bundleName); + return BAppGalleryDisposeProxy::proxy->EndBackup(bundleName, userId); } -DisposeErr AppGalleryDisposeProxy::StartRestore(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartRestore(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->StartRestore(bundleName); + return BAppGalleryDisposeProxy::proxy->StartRestore(bundleName, userId); } -DisposeErr AppGalleryDisposeProxy::EndRestore(const string &bundleName) +DisposeErr AppGalleryDisposeProxy::EndRestore(const string &bundleName, int32_t userId) { - return BAppGalleryDisposeProxy::proxy->EndRestore(bundleName); + return BAppGalleryDisposeProxy::proxy->EndRestore(bundleName, userId); } } // namespace OHOS::FileManagement::Backup diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index 400061e87..51dfb58f4 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -352,6 +352,7 @@ ohos_unittest("backup_restore_deps_manager_test") { sources = [ "${path_backup_mock}/accesstoken/accesstoken_kit_mock.cpp", "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp", "${path_backup}/services/backup_sa/src/module_ipc/sa_backup_connection.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/service_incremental.cpp", diff --git a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp index dcd9e4407..b03426e15 100644 --- a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp @@ -612,7 +612,7 @@ HWTEST_F(ServiceTest, SUB_Service_ReportOnExtConnectFailed_0000, TestSize.Level1 EXPECT_CALL(*session, ValidRestoreDataType(_)).WillOnce(Return(true)); EXPECT_CALL(*session, GetServiceReverseProxy()).WillOnce(Return(srProxy)); EXPECT_CALL(*srProxy, IncrementalRestoreOnBundleStarted(_, _)).WillOnce(Return()); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->ReportOnExtConnectFailed(IServiceReverse::Scenario::RESTORE, bundleName, 0); EXPECT_TRUE(true); @@ -620,14 +620,14 @@ HWTEST_F(ServiceTest, SUB_Service_ReportOnExtConnectFailed_0000, TestSize.Level1 EXPECT_CALL(*session, ValidRestoreDataType(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetServiceReverseProxy()).WillOnce(Return(srProxy)); EXPECT_CALL(*srProxy, RestoreOnBundleStarted(_, _)).WillOnce(Return()); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->ReportOnExtConnectFailed(IServiceReverse::Scenario::RESTORE, bundleName, 0); EXPECT_TRUE(true); EXPECT_CALL(*param, GetBackupOverrideIncrementalRestore()).WillOnce(Return(false)); EXPECT_CALL(*session, GetServiceReverseProxy()).WillOnce(Return(srProxy)); EXPECT_CALL(*srProxy, RestoreOnBundleStarted(_, _)).WillOnce(Return()); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->ReportOnExtConnectFailed(IServiceReverse::Scenario::RESTORE, bundleName, 0); EXPECT_TRUE(true); } catch (...) { @@ -1062,7 +1062,7 @@ HWTEST_F(ServiceTest, SUB_Service_SendStartAppGalleryNotify_0000, TestSize.Level EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); EXPECT_CALL(*jdConfig, IfBundleNameInDisposalConfigFile(_)).WillOnce(Return(true)); - EXPECT_CALL(*gallery, StartRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, StartRestore(_, _)).WillOnce(Return(DisposeErr::OK)); service->SendStartAppGalleryNotify(bundleName); EXPECT_TRUE(true); } catch (...) { @@ -1097,20 +1097,20 @@ HWTEST_F(ServiceTest, SUB_Service_SendEndAppGalleryNotify_0000, TestSize.Level1) EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::REQUEST_FAIL)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::REQUEST_FAIL)); service->SendEndAppGalleryNotify(bundleName); EXPECT_TRUE(true); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(false)); service->SendEndAppGalleryNotify(bundleName); EXPECT_TRUE(true); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(false)); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->SendEndAppGalleryNotify(bundleName); EXPECT_TRUE(true); @@ -1135,12 +1135,12 @@ HWTEST_F(ServiceTest, SUB_Service_TryToClearDispose_0000, TestSize.Level1) GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_TryToClearDispose_0000"; try { BundleName bundleName; - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->TryToClearDispose(bundleName); EXPECT_TRUE(true); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::REQUEST_FAIL)) + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::REQUEST_FAIL)) .WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(false)); service->TryToClearDispose(bundleName); @@ -1178,7 +1178,7 @@ HWTEST_F(ServiceTest, SUB_Service_SendErrAppGalleryNotify_0000, TestSize.Level1) bundleNameList.emplace_back("bundleName"); EXPECT_CALL(*session, GetScenario()).WillOnce(Return(IServiceReverse::Scenario::RESTORE)); EXPECT_CALL(*jdConfig, GetBundleNameFromConfigFile()).WillOnce(Return(bundleNameList)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->SendErrAppGalleryNotify(); EXPECT_TRUE(true); @@ -1209,7 +1209,7 @@ HWTEST_F(ServiceTest, SUB_Service_ClearDisposalOnSaStart_0000, TestSize.Level1) bundleNameList.emplace_back("bundleName"); EXPECT_CALL(*jdConfig, GetBundleNameFromConfigFile()).WillOnce(Return(bundleNameList)); - EXPECT_CALL(*gallery, EndRestore(_)).WillOnce(Return(DisposeErr::OK)); + EXPECT_CALL(*gallery, EndRestore(_, _)).WillOnce(Return(DisposeErr::OK)); EXPECT_CALL(*jdConfig, DeleteFromDisposalConfigFile(_)).WillOnce(Return(true)); service->ClearDisposalOnSaStart(); EXPECT_TRUE(true); -- Gitee From 53e5841db30c759b30607fae5489674ca5744c1c Mon Sep 17 00:00:00 2001 From: chensihan Date: Thu, 28 Nov 2024 17:43:32 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E5=8A=A0=E9=94=81=20Signed-off-by:=20chens?= =?UTF-8?q?ihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module_app_gallery/app_gallery_dispose_proxy.h | 1 + .../module_app_gallery/app_gallery_dispose_proxy.cpp | 1 + services/backup_sa/src/module_ipc/service.cpp | 10 +++++----- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h index 12f271baf..d08a3e262 100644 --- a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h +++ b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h @@ -59,6 +59,7 @@ private: private: static std::mutex instanceLock_; + static std::mutex appGalleryConnectionLock_; static sptr appGalleryDisposeProxyInstance_; }; } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index e9c20f85c..09567dcb8 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -163,6 +163,7 @@ sptr AppGalleryDisposeProxy::GetAppGalleryConnection(int32 sptr AppGalleryDisposeProxy::ConnectAppGallery(int32_t userId) { + std::unique_lock lock(appGalleryConnectionLock_); sptr connection = GetAppGalleryConnection(userId); if (connection == nullptr) { connection = sptr(new AppGalleryConnection(userId)); diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 909f07ab1..0206bea4a 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1173,7 +1173,7 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, session_->ValidRestoreDataType(RestoreTypeEnum::RESTORE_DATA_WAIT_SEND)) { session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } else if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -1182,7 +1182,7 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1403,7 +1403,7 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) return; } HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, GetUserIdDefault()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, session_->GetSessionUserId()); HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1419,7 +1419,7 @@ void Service::SendEndAppGalleryNotify(const BundleName &bundleName) if (scenario != IServiceReverse::Scenario::RESTORE) { return; } - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); if (disposeErr != DisposeErr::OK) { @@ -1438,7 +1438,7 @@ void Service::TryToClearDispose(const BundleName &bundleName) int32_t maxAtt = MAX_TRY_CLEAR_DISPOSE_NUM; int32_t att = 0; while (att < maxAtt) { - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, GetUserIdDefault()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); if (disposeErr == DisposeErr::OK) { break; -- Gitee From 34f7309c60736fc00dc6c127469d837af429a655 Mon Sep 17 00:00:00 2001 From: chensihan Date: Thu, 28 Nov 2024 18:37:39 +0800 Subject: [PATCH 4/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chensihan --- .../app_gallery_dispose_proxy.h | 15 ++++--- .../app_gallery_dispose_proxy.cpp | 40 +++++++------------ .../app_gallery_dispose_proxy_mock.cpp | 10 ++--- .../include/app_gallery_dispose_proxy_mock.h | 16 ++++---- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h index d08a3e262..fdd41a59d 100644 --- a/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h +++ b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h @@ -41,21 +41,20 @@ public: static sptr GetInstance(); - DisposeErr StartBackup(const std::string &bundleName, int32_t userId); - DisposeErr EndBackup(const std::string &bundleName, int32_t userId); - DisposeErr StartRestore(const std::string &bundleName, int32_t userId); - DisposeErr EndRestore(const std::string &bundleName, int32_t userId); + DisposeErr StartBackup(const std::string &bundleName, const int32_t userId); + DisposeErr EndBackup(const std::string &bundleName, const int32_t userId); + DisposeErr StartRestore(const std::string &bundleName, const int32_t userId); + DisposeErr EndRestore(const std::string &bundleName, const int32_t userId); - bool HasAppGalleryConnection(int32_t userId); - sptr GetAppGalleryConnection(int32_t userId); - sptr ConnectAppGallery(int32_t userId); + sptr GetAppGalleryConnection(const int32_t userId); + sptr ConnectAppGallery(const int32_t userId); static std::string abilityName; static std::string pkgName; static std::map> appGalleryConnectionMap_; private: - DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, int32_t userId); + DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, const int32_t userId); private: static std::mutex instanceLock_; diff --git a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index 09567dcb8..1b34d155b 100644 --- a/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -33,6 +33,7 @@ namespace { const auto APP_FOUNDATION_SERVICE = u"appgalleryservice.openapi.privacymanager.AppFoundationService"; } mutex AppGalleryDisposeProxy::instanceLock_; +mutex AppGalleryDisposeProxy::appGalleryConnectionLock_; string AppGalleryDisposeProxy::abilityName = "AppFoundationService"; std::map> AppGalleryDisposeProxy::appGalleryConnectionMap_; @@ -60,7 +61,7 @@ sptr AppGalleryDisposeProxy::GetInstance() return appGalleryDisposeProxyInstance_; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, const int32_t userId) { HILOGI("StartBackup, app %{public}s", bundleName.c_str()); DisposeErr res = DoDispose(bundleName, DisposeOperation::START_BACKUP, userId); @@ -72,7 +73,7 @@ DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, in return res; } -DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, const int32_t userId) { HILOGI("EndBackup, app %{public}s", bundleName.c_str()); DisposeErr res = DoDispose(bundleName, DisposeOperation::END_BACKUP, userId); @@ -84,7 +85,7 @@ DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, int3 return res; } -DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, const int32_t userId) { if (SAUtils::IsSABundleName(bundleName)) { HILOGI("SA does not need to StartRestore"); @@ -100,7 +101,7 @@ DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, i return res; } -DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, const int32_t userId) { if (SAUtils::IsSABundleName(bundleName)) { HILOGI("SA does not need to EndRestore"); @@ -142,38 +143,27 @@ void RecordDoDisposeRes(const std::string &bundleName, } } -bool AppGalleryDisposeProxy::HasAppGalleryConnection(int32_t userId) +sptr AppGalleryDisposeProxy::GetAppGalleryConnection(const int32_t userId) { auto it = appGalleryConnectionMap_.find(userId); if (it == appGalleryConnectionMap_.end()) { - HILOGI("appGalleryConnectionMap not contain %{public}d", userId); - return false; + HILOGI("appGalleryConnectionMap not contain %{public}d, will register", userId); + return nullptr; } - return true; + HILOGI("appGalleryConnectionMap contain %{public}d", userId); + return appGalleryConnectionMap_[userId]; } -sptr AppGalleryDisposeProxy::GetAppGalleryConnection(int32_t userId) -{ - if (HasAppGalleryConnection(userId)) { - HILOGI("appGalleryConnectionMap contain %{public}d", userId); - return appGalleryConnectionMap_[userId]; - } - return nullptr; -} - -sptr AppGalleryDisposeProxy::ConnectAppGallery(int32_t userId) +sptr AppGalleryDisposeProxy::ConnectAppGallery(const int32_t userId) { std::unique_lock lock(appGalleryConnectionLock_); sptr connection = GetAppGalleryConnection(userId); if (connection == nullptr) { connection = sptr(new AppGalleryConnection(userId)); - if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { - HILOGE("AppGalleryConnection failed to connect, userId = %{public}d", userId); - return nullptr; - } appGalleryConnectionMap_[userId] = connection; - } else if (connection->GetRemoteObj() == nullptr) { - HILOGI("AppGalleryConnection try to reconnect, userId = %{public}d", userId); + } + if (connection->GetRemoteObj() == nullptr) { + HILOGI("AppGalleryConnection try to connect, userId = %{public}d", userId); if (!connection->ConnectExtAbility(abilityName) || connection->GetRemoteObj() == nullptr) { HILOGE("AppGalleryConnection failed to connect, userId = %{public}d", userId); return nullptr; @@ -183,7 +173,7 @@ sptr AppGalleryDisposeProxy::ConnectAppGallery(int32_t use } DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, - int32_t userId) + const int32_t userId) { try { HILOGI("DoDispose, app %{public}s, operation %{public}d, userId %{public}d", bundleName.c_str(), diff --git a/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp b/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp index f0d9ec85c..28ba2d824 100644 --- a/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp +++ b/tests/mock/module_ipc/app_gallery_dispose_proxy_mock.cpp @@ -41,28 +41,28 @@ sptr AppGalleryDisposeProxy::GetInstance() return new AppGalleryDisposeProxy; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, const int32_t userId) { return DoDispose(bundleName, DisposeOperation::START_BACKUP, userId); } -DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName, const int32_t userId) { return DoDispose(bundleName, DisposeOperation::END_BACKUP, userId); } -DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName, const int32_t userId) { return DoDispose(bundleName, DisposeOperation::START_RESTORE, userId); } -DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, int32_t userId) +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName, const int32_t userId) { return DoDispose(bundleName, DisposeOperation::END_RESTORE, userId); } DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, - int32_t userId) + const int32_t userId) { return DisposeErr::OK; } diff --git a/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h b/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h index 4ba078e91..4dae0f493 100644 --- a/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h +++ b/tests/mock/module_ipc/include/app_gallery_dispose_proxy_mock.h @@ -23,10 +23,10 @@ namespace OHOS::FileManagement::Backup { class BAppGalleryDisposeProxy { public: - virtual DisposeErr StartBackup(const std::string&, int32_t) = 0; - virtual DisposeErr EndBackup(const std::string&, int32_t) = 0; - virtual DisposeErr StartRestore(const std::string&, int32_t) = 0; - virtual DisposeErr EndRestore(const std::string&, int32_t) = 0; + virtual DisposeErr StartBackup(const std::string&, const int32_t) = 0; + virtual DisposeErr EndBackup(const std::string&, const int32_t) = 0; + virtual DisposeErr StartRestore(const std::string&, const int32_t) = 0; + virtual DisposeErr EndRestore(const std::string&, const int32_t) = 0; public: BAppGalleryDisposeProxy() = default; virtual ~BAppGalleryDisposeProxy() = default; @@ -36,10 +36,10 @@ public: class AppGalleryDisposeProxyMock : public BAppGalleryDisposeProxy { public: - MOCK_METHOD(DisposeErr, StartBackup, (const std::string&, int32_t)); - MOCK_METHOD(DisposeErr, EndBackup, (const std::string&, int32_t)); - MOCK_METHOD(DisposeErr, StartRestore, (const std::string&, int32_t)); - MOCK_METHOD(DisposeErr, EndRestore, (const std::string&, int32_t)); + MOCK_METHOD(DisposeErr, StartBackup, (const std::string&, const int32_t)); + MOCK_METHOD(DisposeErr, EndBackup, (const std::string&, const int32_t)); + MOCK_METHOD(DisposeErr, StartRestore, (const std::string&, const int32_t)); + MOCK_METHOD(DisposeErr, EndRestore, (const std::string&, const int32_t)); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_MOCK_H \ No newline at end of file -- Gitee From a5bad5d85fd77156fc3889391e16b474ad48ccf2 Mon Sep 17 00:00:00 2001 From: chensihan Date: Fri, 29 Nov 2024 09:05:55 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BF=AE=E6=94=B9=20Sign?= =?UTF-8?q?ed-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/backup_sa/src/module_ipc/service.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 0206bea4a..4cb0f9c09 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1173,7 +1173,8 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, session_->ValidRestoreDataType(RestoreTypeEnum::RESTORE_DATA_WAIT_SEND)) { session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, + session_->GetSessionUserId()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } else if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -1182,7 +1183,8 @@ void Service::ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, + session_->GetSessionUserId()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1403,7 +1405,8 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) return; } HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, session_->GetSessionUserId()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, + session_->GetSessionUserId()); HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1438,7 +1441,8 @@ void Service::TryToClearDispose(const BundleName &bundleName) int32_t maxAtt = MAX_TRY_CLEAR_DISPOSE_NUM; int32_t att = 0; while (att < maxAtt) { - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, session_->GetSessionUserId()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, + session_->GetSessionUserId()); HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); if (disposeErr == DisposeErr::OK) { break; -- Gitee From ad3f44b38bbab25903ff63acb813e6c278f1aefd Mon Sep 17 00:00:00 2001 From: chensihan Date: Tue, 3 Dec 2024 16:00:40 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0userid=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chensihan --- .../include/module_ipc/svc_session_manager.h | 20 ++++++++- services/backup_sa/src/module_ipc/service.cpp | 42 ++++++++++++++----- .../src/module_ipc/service_incremental.cpp | 9 +++- .../src/module_ipc/svc_session_manager.cpp | 38 ++++++++++++++--- 4 files changed, 89 insertions(+), 20 deletions(-) diff --git a/services/backup_sa/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index a3656084e..08a2580c1 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -68,6 +68,7 @@ struct BackupExtInfo { std::string backupParameters; int32_t backupPriority; std::string extInfo; + int32_t userId {0}; int32_t appendNum {1}; bool isClearData {true}; bool isInPublishFile {false}; @@ -281,12 +282,29 @@ public: */ std::string GetBackupExtInfo(const std::string &bundleName); + /** + * @brief 暂存应用用户id + * + * @param bundleName 应用名称 + * @param userId 用户id + */ + void SetBundleUserId(const std::string &bundleName, const int32_t userId); + + /** + * @brief 获取应用用户id + * + * @param bundleName 应用名称 + * @return int32_t userId + */ + int32_t GetBundleUserId(const std::string &bundleName); + /** * @brief 追加应用 * * @param bundleNames 应用名称 + * @param failedBundles 返回失败应用名称 */ - void AppendBundles(const std::vector &bundleNames); + void AppendBundles(const std::vector &bundleNames, std::vector &failedBundles); /** * @brief 添加指定应用 diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 4cb0f9c09..360a56feb 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -630,7 +630,9 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vectorDecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::OK); } - session_->AppendBundles(restoreBundleNames); + std::vector failedBundles; + session_->AppendBundles(restoreBundleNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); SetCurrentSessProperties(restoreInfos, restoreBundleNames, bundleNameDetailMap, isClearDataFlags, restoreType); OnStartSched(); @@ -678,6 +680,7 @@ void Service::SetCurrentSessProperties(std::vector if (BundleMgrAdapter::IsUser0BundleName(bundleNameIndexInfo, session_->GetSessionUserId())) { SendUserIdToApp(bundleNameIndexInfo, session_->GetSessionUserId()); } + session_->SetBundleUserId(bundleNameIndexInfo, session_->GetSessionUserId()); session_->SetBackupExtName(bundleNameIndexInfo, restoreInfo.extensionName); session_->SetIsReadyLaunch(bundleNameIndexInfo); } @@ -711,7 +714,9 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, HILOGW("RestoreBundleNames is empty."); return BError(BError::Codes::OK); } - session_->AppendBundles(restoreBundleNames); + std::vector failedBundles; + session_->AppendBundles(restoreBundleNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); SetCurrentSessProperties(restoreInfos, restoreBundleNames, restoreType); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -758,6 +763,7 @@ void Service::SetCurrentSessProperties(std::vector session_->SetBundleVersionCode(bundleNameIndexInfo, restoreInfo.versionCode); session_->SetBundleVersionName(bundleNameIndexInfo, restoreInfo.versionName); session_->SetBundleDataSize(bundleNameIndexInfo, restoreInfo.spaceOccupied); + session_->SetBundleUserId(bundleNameIndexInfo, session_->GetSessionUserId()); auto iter = isClearDataFlags.find(bundleNameIndexInfo); if (iter != isClearDataFlags.end()) { session_->SetClearDataFlag(bundleNameIndexInfo, iter->second); @@ -796,7 +802,9 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); - session_->AppendBundles(supportBackupNames); + std::vector failedBundles; + session_->AppendBundles(supportBackupNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, false); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -838,7 +846,9 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); - session_->AppendBundles(supportBackupNames); + std::vector failedBundles; + session_->AppendBundles(supportBackupNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); HandleCurGroupBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -871,6 +881,7 @@ void Service::HandleCurGroupBackupInfos(std::vector bundleNameIndexInfo.c_str(), GetAnonyString(uniCastInfo.detail).c_str(), uniCastInfo.detail.size()); session_->SetBackupExtInfo(bundleNameIndexInfo, uniCastInfo.detail); } + session_->SetBundleUserId(bundleNameIndexInfo, session_->GetSessionUserId()); session_->SetBackupExtName(bundleNameIndexInfo, info.extensionName); session_->SetIsReadyLaunch(bundleNameIndexInfo); } @@ -885,11 +896,13 @@ ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, callerName = VerifyCallerAndGetCallerName(); SendEndAppGalleryNotify(callerName); if (sennario == BackupRestoreScenario::FULL_RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo, callerName, errCode); NotifyCloneBundleFinish(callerName, sennario); + session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo, callerName, errCode); + OnAllBundlesFinished(BError(BError::Codes::OK)); } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { - session_->GetServiceReverseProxy()->IncrementalRestoreOnResultReport(restoreRetInfo, callerName, errCode); NotifyCloneBundleFinish(callerName, sennario); + session_->GetServiceReverseProxy()->IncrementalRestoreOnResultReport(restoreRetInfo, callerName, errCode); + OnAllBundlesFinished(BError(BError::Codes::OK)); } else if (sennario == BackupRestoreScenario::FULL_BACKUP) { session_->GetServiceReverseProxy()->BackupOnResultReport(restoreRetInfo, callerName); } else if (sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { @@ -898,13 +911,16 @@ ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, return BError(BError::Codes::OK); } catch (const BError &e) { NotifyCloneBundleFinish(callerName, sennario); + OnAllBundlesFinished(BError(BError::Codes::OK)); return e.GetCode(); // 任意异常产生,终止监听该任务 } catch (const exception &e) { NotifyCloneBundleFinish(callerName, sennario); + OnAllBundlesFinished(BError(BError::Codes::OK)); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); return EPERM; } catch (...) { NotifyCloneBundleFinish(callerName, sennario); + OnAllBundlesFinished(BError(BError::Codes::OK)); HILOGI("Unexpected exception"); return EPERM; } @@ -913,6 +929,7 @@ ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, ErrCode Service::SAResultReport(const std::string bundleName, const std::string restoreRetInfo, const ErrCode errCode, const BackupRestoreScenario sennario) { + SADone(errCode, bundleName); if (sennario == BackupRestoreScenario::FULL_RESTORE) { session_->GetServiceReverseProxy()->RestoreOnResultReport(restoreRetInfo, bundleName); } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { @@ -923,13 +940,14 @@ ErrCode Service::SAResultReport(const std::string bundleName, const std::string } else if (sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { session_->GetServiceReverseProxy()->IncrementalBackupOnResultReport(restoreRetInfo, bundleName); } + OnAllBundlesFinished(BError(BError::Codes::OK)); if (sennario == BackupRestoreScenario::FULL_RESTORE || sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { BundleEndRadarReport(bundleName, errCode, IServiceReverse::Scenario::RESTORE); } else if (sennario == BackupRestoreScenario::FULL_BACKUP || sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { BundleEndRadarReport(bundleName, errCode, IServiceReverse::Scenario::BACKUP); } - return SADone(errCode, bundleName); + return BError(BError::Codes::OK); } void Service::NotifyCloneBundleFinish(std::string bundleName, const BackupRestoreScenario sennario) @@ -961,7 +979,6 @@ void Service::NotifyCloneBundleFinish(std::string bundleName, const BackupRestor ClearSessionAndSchedInfo(bundleName); } RemoveExtensionMutex(bundleName); - OnAllBundlesFinished(BError(BError::Codes::OK)); } catch (...) { HILOGI("Unexpected exception"); ReleaseOnException(); @@ -1331,7 +1348,9 @@ void Service::HandleRestoreDepsBundle(const string &bundleName) HILOGI("Start restore session, bundle: %{public}s", bundle.first.c_str()); restoreBundleNames.emplace_back(bundle.first); } - session_->AppendBundles(restoreBundleNames); + std::vector failedBundles; + session_->AppendBundles(restoreBundleNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); for (const auto &bundle : restoreBundleMap) { for (auto &bundleInfo : SvcRestoreDepsManager::GetInstance().GetAllBundles()) { if (bundle.first != bundleInfo.name) { @@ -1747,7 +1766,9 @@ ErrCode Service::AppendBundlesClearSession(const std::vector &bundle std::string bundleNameIndexStr = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); supportBundleNames.emplace_back(bundleNameIndexStr); } - session_->AppendBundles(supportBundleNames); + std::vector failedBundles; + session_->AppendBundles(supportBundleNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); for (auto info : backupInfos) { std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); session_->SetBackupExtName(bundleNameIndexInfo, info.extensionName); @@ -1924,7 +1945,6 @@ ErrCode Service::SADone(ErrCode errCode, std::string bundleName) saConnection->DisconnectBackupSAExt(); ClearSessionAndSchedInfo(bundleName); } - OnAllBundlesFinished(BError(BError::Codes::OK)); return BError(BError::Codes::OK); } catch (const BError &e) { ReleaseOnException(); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 8583f06e2..0f1d02c69 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -351,7 +351,9 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); - session_->AppendBundles(supportBackupNames); + std::vector failedBundles; + session_->AppendBundles(supportBackupNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, true); OnStartSched(); @@ -389,7 +391,9 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); - session_->AppendBundles(supportBackupNames); + std::vector failedBundles; + session_->AppendBundles(supportBackupNames, failedBundles); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); HandleCurGroupIncBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -738,6 +742,7 @@ void Service::SetCurrentBackupSessProperties(const vector &bundleNames, } else { session_->SetBundleDataSize(bundleName, bundleInfo.spaceOccupied); } + session_->SetBundleUserId(bundleName, userId); session_->SetBackupExtName(bundleName, bundleInfo.extensionName); session_->SetIsReadyLaunch(bundleName); } diff --git a/services/backup_sa/src/module_ipc/svc_session_manager.cpp b/services/backup_sa/src/module_ipc/svc_session_manager.cpp index f47d3e151..50b52ef76 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -520,7 +520,29 @@ std::string SvcSessionManager::GetBackupExtInfo(const string &bundleName) return it->second.extInfo; } -void SvcSessionManager::AppendBundles(const vector &bundleNames) +void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t userId) +{ + unique_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + it->second.userId = userId; +} + +int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) +{ + shared_lock lock(lock_); + if (!impl_.clientToken) { + throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + } + + auto it = GetBackupExtNameMap(bundleName); + return it->second.userId; +} + +void SvcSessionManager::AppendBundles(const vector &bundleNames, vector &failedBundles) { unique_lock lock(lock_); if (!impl_.clientToken) { @@ -532,11 +554,15 @@ void SvcSessionManager::AppendBundles(const vector &bundleNames) BackupExtInfo info {}; auto it = impl_.backupExtNameMap.find(bundleName); if (it != impl_.backupExtNameMap.end()) { - HILOGI("BackupExtNameMap already contain %{public}s", bundleName.c_str()); - info.backUpConnection = impl_.backupExtNameMap[bundleName].backUpConnection; - info.saBackupConnection = impl_.backupExtNameMap[bundleName].saBackupConnection; - info.appendNum = impl_.backupExtNameMap[bundleName].appendNum + 1; - impl_.backupExtNameMap[bundleName] = info; + if (impl_.backupExtNameMap[bundleName].userId == GetSessionUserId()) { + HILOGI("BackupExtNameMap already contain %{public}s", bundleName.c_str()); + info.backUpConnection = impl_.backupExtNameMap[bundleName].backUpConnection; + info.saBackupConnection = impl_.backupExtNameMap[bundleName].saBackupConnection; + info.appendNum = impl_.backupExtNameMap[bundleName].appendNum + 1; + impl_.backupExtNameMap[bundleName] = info; + } else { + failedBundles.push_back(bundleName); + } continue; } if (SAUtils::IsSABundleName(bundleName)) { -- Gitee From be9313b7f2c9f7df50bc4bb9fa3a2d8d3dfaaedf Mon Sep 17 00:00:00 2001 From: chensihan Date: Wed, 25 Dec 2024 16:16:47 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chensihan --- .../src/svc_session_manager_mock.cpp | 9 +- .../module_ipc/svc_session_manager_mock.cpp | 20 +- .../svc_session_manager_throw_mock.cpp | 14 +- .../svc_session_manager_throw_mock.h | 8 +- tests/unittests/backup_sa/BUILD.gn | 1 + .../backup_sa/module_app_gallery/BUILD.gn | 73 ++++++ .../app_gallery_dispose_proxy_test.cpp | 246 ++++++++++++++++++ .../module_ipc/service_other_test.cpp | 5 + .../module_ipc/svc_session_manager_test.cpp | 79 +++++- 9 files changed, 445 insertions(+), 10 deletions(-) create mode 100644 tests/unittests/backup_sa/module_app_gallery/BUILD.gn create mode 100644 tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp diff --git a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp index 59c9e2a34..13518f048 100644 --- a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp @@ -110,7 +110,14 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu return BSvcSessionManager::sessionManager->GetSAExtConnection(bundleName); } -void SvcSessionManager::AppendBundles(const vector &) {} +int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) +{ + return 0; +} + +void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t userId) {} + +void SvcSessionManager::AppendBundles(const vector &, vector &) {} sptr SvcSessionManager::CreateBackupConnection(BundleName &bundleName) { diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 257ad09c2..05007ced0 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -241,7 +241,7 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu return std::weak_ptr(it->second.saBackupConnection); } -void SvcSessionManager::AppendBundles(const vector &bundleNames) +void SvcSessionManager::AppendBundles(const vector &bundleNames, vector &failedBundles) { GTEST_LOG_(INFO) << "AppendBundles"; BackupExtInfo info {}; @@ -345,6 +345,24 @@ std::string SvcSessionManager::GetBundleVersionName(const std::string &bundleNam return it->second.versionName; } +void SvcSessionManager::SetBundleUserId(const std::string &bundleName, const int32_t userId) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return; + } + it->second.userId = userId; +} + +int32_t SvcSessionManager::GetBundleUserId(const std::string &bundleName) +{ + auto it = impl_.backupExtNameMap.find(bundleName); + if (it == impl_.backupExtNameMap.end()) { + return 0; + } + return it->second.userId; +} + void SvcSessionManager::SetBundleDataSize(const std::string &bundleName, int64_t dataSize) { auto it = impl_.backupExtNameMap.find(bundleName); diff --git a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp index 8a0b29390..c229e90e4 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.cpp @@ -139,9 +139,19 @@ std::weak_ptr SvcSessionManager::GetSAExtConnection(const Bu return BackupSvcSessionManager::session->GetSAExtConnection(bundleName); } -void SvcSessionManager::AppendBundles(const vector &bundleNames) +int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) { - BackupSvcSessionManager::session->AppendBundles(bundleNames); + return BackupSvcSessionManager::session->GetBundleUserId(bundleName); +} + +void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t userId) +{ + BackupSvcSessionManager::session->SetBundleUserId(bundleName, userId); +} + +void SvcSessionManager::AppendBundles(const vector &bundleNames, vector &failedBundles) +{ + BackupSvcSessionManager::session->AppendBundles(bundleNames, failedBundles); } sptr SvcSessionManager::CreateBackupConnection(BundleName &bundleName) diff --git a/tests/mock/module_ipc/svc_session_manager_throw_mock.h b/tests/mock/module_ipc/svc_session_manager_throw_mock.h index cb9904f73..98fd937c2 100644 --- a/tests/mock/module_ipc/svc_session_manager_throw_mock.h +++ b/tests/mock/module_ipc/svc_session_manager_throw_mock.h @@ -51,7 +51,7 @@ public: virtual std::string GetBackupExtInfo(const std::string &) = 0; virtual void SetBackupExtName(const std::string &, const std::string &) = 0; virtual std::weak_ptr GetSAExtConnection(const BundleName &) = 0; - virtual void AppendBundles(const std::vector &) = 0; + virtual void AppendBundles(const std::vector &, std::vector &) = 0; virtual sptr CreateBackupConnection(BundleName &) = 0; virtual void Start() = 0; virtual void Finish() = 0; @@ -66,6 +66,8 @@ public: virtual int64_t GetBundleVersionCode(const std::string &) = 0; virtual void SetBundleVersionName(const std::string &, std::string) = 0; virtual std::string GetBundleVersionName(const std::string &) = 0; + virtual void SetBundleUserId(const std::string &, const int32_t) = 0; + virtual int32_t GetBundleUserId(const std::string &) = 0; virtual void SetBundleDataSize(const std::string &, int64_t) = 0; virtual bool StartFwkTimer(const std::string &, const Utils::Timer::TimerCallback &) = 0; virtual bool StopFwkTimer(const std::string &) = 0; @@ -120,7 +122,7 @@ public: MOCK_METHOD(std::string, GetBackupExtInfo, (const std::string &)); MOCK_METHOD(void, SetBackupExtName, (const std::string &, const std::string &)); MOCK_METHOD(std::weak_ptr, GetSAExtConnection, (const BundleName &)); - MOCK_METHOD(void, AppendBundles, (const std::vector &)); + MOCK_METHOD(void, AppendBundles, (const std::vector &, std::vector &)); MOCK_METHOD(sptr, CreateBackupConnection, (BundleName &)); MOCK_METHOD(void, Start, ()); MOCK_METHOD(void, Finish, ()); @@ -135,6 +137,8 @@ public: MOCK_METHOD(int64_t, GetBundleVersionCode, (const std::string &)); MOCK_METHOD(void, SetBundleVersionName, (const std::string &, std::string)); MOCK_METHOD(std::string, GetBundleVersionName, (const std::string &)); + MOCK_METHOD(void, SetBundleUserId, (const std::string &, const int32_t)); + MOCK_METHOD(int32_t, GetBundleUserId, (const std::string &)); MOCK_METHOD(void, SetBundleDataSize, (const std::string &, int64_t)); MOCK_METHOD(bool, StartFwkTimer, (const std::string &, const Utils::Timer::TimerCallback &)); MOCK_METHOD(bool, StopFwkTimer, (const std::string &)); diff --git a/tests/unittests/backup_sa/BUILD.gn b/tests/unittests/backup_sa/BUILD.gn index 370b91517..c637fbf3a 100644 --- a/tests/unittests/backup_sa/BUILD.gn +++ b/tests/unittests/backup_sa/BUILD.gn @@ -18,6 +18,7 @@ group("backup_sa_test") { testonly = true deps = [ + "module_app_gallery:app_gallery_test", "module_ipc:backup_sa_ipc_test", "session:session_test", ] diff --git a/tests/unittests/backup_sa/module_app_gallery/BUILD.gn b/tests/unittests/backup_sa/module_app_gallery/BUILD.gn new file mode 100644 index 000000000..24d57884c --- /dev/null +++ b/tests/unittests/backup_sa/module_app_gallery/BUILD.gn @@ -0,0 +1,73 @@ +# Copyright (c) 2024 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/test.gni") +import("//foundation/filemanagement/app_file_service/backup.gni") + +ohos_unittest("app_gallery_dispose_proxy_test") { + branch_protector_ret = "pac_ret" + + module_out_path = path_module_out_tests + + sources = [ + "${path_backup_mock}/module_external/src/bms_adapter_mock.cpp", + "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp", + "${path_backup}/services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp", + "app_gallery_dispose_proxy_test.cpp", + ] + + include_dirs = [ + "${path_backup}/services/backup_sa/include", + "${path_backup}/interfaces/inner_api/native/backup_kit_inner/impl", + "${path_backup}/tests/unittests/backup_api/backup_impl/include", + "${path_backup_mock}/module_external/include", + ] + + deps = [ + "${path_backup}/interfaces/inner_api/native/backup_kit_inner:backup_kit_inner", + "${path_backup}/tests/utils:backup_test_utils", + "${path_backup}/utils:backup_utils", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "hisysevent:libhisysevent", + "hitrace:hitrace_meter", + "init:libbegetutil", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + "storage_service:storage_manager_sa_proxy", + ] + + defines = [ + "LOG_TAG=\"app_file_service\"", + "LOG_DOMAIN=0xD004303", + "private = public", + ] + + use_exceptions = true +} + +group("app_gallery_test") { + testonly = true + deps = [ ":app_gallery_dispose_proxy_test" ] +} diff --git a/tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp b/tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp new file mode 100644 index 000000000..73a31317e --- /dev/null +++ b/tests/unittests/backup_sa/module_app_gallery/app_gallery_dispose_proxy_test.cpp @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2024 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 "b_error/b_error.h" +#include "b_radar/b_radar.h" +#include "b_sa/b_sa_utils.h" +#include "b_jsonutil/b_jsonutil.h" +#include "filemgmt_libhilog.h" +#include "message_parcel.h" + +#include "bms_adapter_mock.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" +#include "module_app_gallery/app_gallery_service_connection.h" +#include "want.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; +using namespace testing; +using namespace testing::ext; + +class AppGalleryTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(); + void SetUp() {}; + void TearDown() {}; + + static inline shared_ptr bms = nullptr; +}; + +void AppGalleryTest::SetUpTestCase(void) +{ + bms = make_shared(); + BundleMgrAdapterMock::bms = bms; +} + +void AppGalleryTest::TearDownTestCase() +{ + BundleMgrAdapterMock::bms = nullptr; + bms = nullptr; +} + +/** + * @tc.number: App_Gallery_GetInstance_0100 + * @tc.name: App_Gallery_GetInstance_0100 + * @tc.desc: 测试 GetInstance + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_GetInstance_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_GetInstance_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + bool result = instance != nullptr; + EXPECT_TRUE(result); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_GetInstance_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_GetInstance_0100"; +} + +/** + * @tc.number: App_Gallery_GetInstance_0200 + * @tc.name: App_Gallery_GetInstance_0200 + * @tc.desc: 测试 GetInstance + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_GetInstance_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_GetInstance_0200"; + try { + auto instance1 = AppGalleryDisposeProxy::GetInstance(); + auto instance2 = AppGalleryDisposeProxy::GetInstance(); + bool result = instance1 == instance2; + EXPECT_TRUE(result); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_GetInstance_0200."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_GetInstance_0200"; +} + +/** + * @tc.number: App_Gallery_GetAppGalleryConnection_0100 + * @tc.name: App_Gallery_GetAppGalleryConnection_0100 + * @tc.desc: 测试 GetAppGalleryConnection + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_GetAppGalleryConnection_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_GetAppGalleryConnection_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + int32_t userId = 100; + auto result = instance->GetAppGalleryConnection(userId); + EXPECT_TRUE(result == nullptr); + instance->appGalleryConnectionMap_[userId] = sptr(new AppGalleryConnection(userId)); + result = instance->GetAppGalleryConnection(userId); + EXPECT_TRUE(result != nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_GetAppGalleryConnection_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_GetAppGalleryConnection_0100"; +} + +/** + * @tc.number: App_Gallery_ConnectAppGallery_0100 + * @tc.name: App_Gallery_ConnectAppGallery_0100 + * @tc.desc: 测试 ConnectAppGallery + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_ConnectAppGallery_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_ConnectAppGallery_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + int32_t userId = 101; + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + auto result = instance->ConnectAppGallery(userId); + EXPECT_TRUE(result == nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_ConnectAppGallery_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_ConnectAppGallery_0100"; +} + +/** + * @tc.number: App_Gallery_StartRestore_0100 + * @tc.name: App_Gallery_StartRestore_0100 + * @tc.desc: 测试 StartRestore + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_StartRestore_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_StartRestore_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + std::string bundleName = "1234"; + int32_t userId = 102; + auto result = instance->StartRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::OK); + result = instance->EndRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_StartRestore_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_StartRestore_0100"; +} + +/** + * @tc.number: App_Gallery_StartRestore_0200 + * @tc.name: App_Gallery_StartRestore_0200 + * @tc.desc: 测试 StartRestore + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_StartRestore_0200, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_StartRestore_0200"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + std::string bundleName = "testapp"; + int32_t userId = 102; + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + auto result = instance->StartRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + result = instance->EndRestore(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_StartRestore_0200."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_StartRestore_0200"; +} + +/** + * @tc.number: App_Gallery_StartBackup_0100 + * @tc.name: App_Gallery_StartBackup_0100 + * @tc.desc: 测试 StartBackup + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: issueIAKC3I + */ +HWTEST_F(AppGalleryTest, App_Gallery_StartBackup_0100, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ServiceTest-begin App_Gallery_StartBackup_0100"; + try { + auto instance = AppGalleryDisposeProxy::GetInstance(); + EXPECT_TRUE(instance != nullptr); + std::string bundleName = "1234"; + int32_t userId = 102; + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + auto result = instance->StartBackup(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + EXPECT_CALL(*bms, GetAppGalleryBundleName()).WillOnce(Return("")); + result = instance->EndBackup(bundleName, userId); + EXPECT_EQ(result, DisposeErr::CONN_FAIL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ServiceTest-an exception occurred by App_Gallery_StartBackup_0100."; + } + GTEST_LOG_(INFO) << "ServiceTest-end App_Gallery_StartBackup_0100"; +} +} \ No newline at end of file diff --git a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp index 1c8d8e720..46c917feb 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -916,6 +916,7 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0300, TestSize.Level1 map isClearDataFlags; RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND; + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(true)); @@ -925,6 +926,7 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0300, TestSize.Level1 EXPECT_TRUE(true); restoreBundleInfos[0].extensionName = "extensionName"; + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*jsonUtil, FindBundleInfoByName(_, _, _, _)).WillOnce(Return(false)).WillOnce(Return(false)); @@ -932,12 +934,14 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0300, TestSize.Level1 isClearDataFlags, restoreType); EXPECT_TRUE(true); + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*jsonUtil, FindBundleInfoByName(_, _, _, _)).WillOnce(Return(false)).WillOnce(Return(false)); service->SetCurrentSessProperties(restoreBundleInfos, restoreBundleNames, bundleNameDetailMap, isClearDataFlags, restoreType); + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*jsonUtil, FindBundleInfoByName(_, _, _, _)).WillOnce(Return(true)).WillOnce(Return(false)); @@ -945,6 +949,7 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0300, TestSize.Level1 service->SetCurrentSessProperties(restoreBundleInfos, restoreBundleNames, bundleNameDetailMap, isClearDataFlags, restoreType); + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*jsonUtil, FindBundleInfoByName(_, _, _, _)).WillOnce(Return(false)).WillOnce(Return(true)); diff --git a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp index 6ddbbe2e6..fe7a73615 100644 --- a/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/svc_session_manager_test.cpp @@ -70,13 +70,14 @@ void SvcSessionManagerTest::TearDownTestCase(void) void SvcSessionManagerTest::Init(IServiceReverse::Scenario scenario) { vector bundleNames; + vector failedBundles; map backupExtNameMap; bundleNames.emplace_back(BUNDLE_NAME); EXPECT_TRUE(sessionManagerPtr_ != nullptr); sessionManagerPtr_->Active( {.clientToken = CLIENT_TOKEN_ID, .scenario = scenario, .backupExtNameMap = {}, .clientProxy = remote_}); sessionManagerPtr_->IsOnAllBundlesFinished(); - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); sessionManagerPtr_->Finish(); sessionManagerPtr_->IsOnAllBundlesFinished(); } @@ -1142,10 +1143,11 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_AppendBundles_0100, testin GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_AppendBundles_0100"; try { vector bundleNames; + vector failedBundles; try { EXPECT_TRUE(sessionManagerPtr_ != nullptr); sessionManagerPtr_->impl_.clientToken = 0; - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); EXPECT_TRUE(false); } catch (BError &err) { EXPECT_EQ(err.GetRawCode(), BError::Codes::SA_INVAL_ARG); @@ -1155,14 +1157,14 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_AppendBundles_0100, testin bundleNames.emplace_back("app1"); sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); EXPECT_EQ(sessionManagerPtr_->impl_.backupExtNameMap.size(), 1); bundleNames.clear(); bundleNames.emplace_back("123"); sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; sessionManagerPtr_->impl_.backupExtNameMap.clear(); - sessionManagerPtr_->AppendBundles(bundleNames); + sessionManagerPtr_->AppendBundles(bundleNames, failedBundles); EXPECT_EQ(sessionManagerPtr_->impl_.backupExtNameMap.size(), 1); } catch (...) { EXPECT_FALSE(false); @@ -1526,6 +1528,75 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_GetBundleVersionName_0100, GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_GetBundleVersionName_0100"; } +/** + * @tc.number: SUB_backup_sa_session_SetBundleUserId_0100 + * @tc.name: SUB_backup_sa_session_SetBundleUserId_0100 + * @tc.desc: 测试 SetBundleUserId 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6VA38 + */ +HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_SetBundleUserId_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_SetBundleUserId_0100"; + try { + int32_t userId = 100; + try { + EXPECT_TRUE(sessionManagerPtr_ != nullptr); + sessionManagerPtr_->impl_.clientToken = 0; + sessionManagerPtr_->SetBundleUserId(BUNDLE_NAME, userId); + EXPECT_TRUE(false); + } catch (BError &err) { + EXPECT_EQ(err.GetRawCode(), BError::Codes::SA_INVAL_ARG); + } + + sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; + sessionManagerPtr_->impl_.backupExtNameMap.clear(); + sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = {}; + sessionManagerPtr_->SetBundleUserId(BUNDLE_NAME, userId); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by SetBundleUserId."; + } + GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_SetBundleUserId_0100"; +} + +/** + * @tc.number: SUB_backup_sa_session_GetBundleUserId_0100 + * @tc.name: SUB_backup_sa_session_GetBundleUserId_0100 + * @tc.desc: 测试 GetBundleUserId 接口 + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: I6VA38 + */ +HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_GetBundleUserId_0100, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SvcSessionManagerTest-begin SUB_backup_sa_session_GetBundleUserId_0100"; + try { + try { + EXPECT_TRUE(sessionManagerPtr_ != nullptr); + sessionManagerPtr_->impl_.clientToken = 0; + sessionManagerPtr_->GetBundleUserId(BUNDLE_NAME); + EXPECT_TRUE(false); + } catch (BError &err) { + EXPECT_EQ(err.GetRawCode(), BError::Codes::SA_INVAL_ARG); + } + + sessionManagerPtr_->impl_.clientToken = CLIENT_TOKEN_ID; + sessionManagerPtr_->impl_.backupExtNameMap.clear(); + sessionManagerPtr_->impl_.backupExtNameMap[BUNDLE_NAME] = {}; + sessionManagerPtr_->GetBundleUserId(BUNDLE_NAME); + EXPECT_TRUE(true); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by GetBundleUserId."; + } + GTEST_LOG_(INFO) << "SvcSessionManagerTest-end SUB_backup_sa_session_GetBundleUserId_0100"; +} + /** * @tc.number: SUB_backup_sa_session_Start_0100 * @tc.name: SUB_backup_sa_session_Start_0100 -- Gitee From 68910f2df57f1bc6a1f396b2a05da5206935ade2 Mon Sep 17 00:00:00 2001 From: chensihan Date: Wed, 25 Dec 2024 09:22:58 +0000 Subject: [PATCH 8/8] update services/backup_sa/src/module_ipc/service.cpp. Signed-off-by: chensihan --- services/backup_sa/src/module_ipc/service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 97c1455ff..eba816804 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -714,7 +714,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, std::vector failedBundles; session_->AppendBundles(restoreBundleNames, failedBundles); HandleExceptionOnAppendBundles(session_, failedBundles, {}); - SetCurrentSessProperties(restoreInfos, restoreBundleNames, restoreType); + SetCurrentSessProperties(restoreInfos, restoreBundleNames, restoreType, oldBackupVersion); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::OK); -- Gitee