From 4b5b12ba34347c7352a7da8f59601a4eecbc87d4 Mon Sep 17 00:00:00 2001 From: chensihan Date: Fri, 27 Dec 2024 10:05:44 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E5=A4=9A=E7=94=A8=E6=88=B7=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E5=BA=94=E7=94=A8=E5=B8=82=E5=9C=BA=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=20Signed-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/backup_sa/BUILD.gn | 1 + .../app_gallery_dispose_proxy.h | 22 +- .../app_gallery_service_connection.h | 61 ++--- .../include/module_ipc/svc_session_manager.h | 20 +- .../app_gallery_dispose_proxy.cpp | 94 ++++--- .../app_gallery_service_connection.cpp | 76 ++++++ services/backup_sa/src/module_ipc/service.cpp | 58 +++-- .../src/module_ipc/service_incremental.cpp | 9 +- .../src/module_ipc/svc_session_manager.cpp | 38 ++- .../app_gallery_dispose_proxy_mock.cpp | 19 +- .../include/app_gallery_dispose_proxy_mock.h | 16 +- .../src/app_gallery_dispose_proxy_mock.cpp | 16 +- .../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 ++++++++++++++++++ tests/unittests/backup_sa/module_ipc/BUILD.gn | 1 + .../module_ipc/service_other_test.cpp | 2 + .../backup_sa/module_ipc/sub_service_test.cpp | 22 +- .../module_ipc/svc_session_manager_test.cpp | 79 +++++- 23 files changed, 744 insertions(+), 161 deletions(-) create mode 100644 services/backup_sa/src/module_app_gallery/app_gallery_service_connection.cpp 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/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..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 @@ -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, 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); + + sptr GetAppGalleryConnection(const int32_t userId); + sptr ConnectAppGallery(const 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, const int32_t userId); private: static std::mutex instanceLock_; - static std::mutex conditionMutex_; + static std::mutex appGalleryConnectionLock_; 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/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index 51b13b07e..7a28e0962 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}; @@ -282,12 +283,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_app_gallery/app_gallery_dispose_proxy.cpp b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp index e6817ebd9..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 @@ -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,10 @@ namespace { const auto APP_FOUNDATION_SERVICE = u"appgalleryservice.openapi.privacymanager.AppFoundationService"; } mutex AppGalleryDisposeProxy::instanceLock_; -mutex AppGalleryDisposeProxy::conditionMutex_; +mutex AppGalleryDisposeProxy::appGalleryConnectionLock_; 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 +61,81 @@ sptr AppGalleryDisposeProxy::GetInstance() return appGalleryDisposeProxyInstance_; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +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); + 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, const 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, const 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, const 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 +143,44 @@ void RecordDoDisposeRes(const std::string &bundleName, } } -DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) +sptr AppGalleryDisposeProxy::GetAppGalleryConnection(const int32_t userId) +{ + auto it = appGalleryConnectionMap_.find(userId); + if (it == appGalleryConnectionMap_.end()) { + HILOGI("appGalleryConnectionMap not contain %{public}d, will register", userId); + return nullptr; + } + HILOGI("appGalleryConnectionMap contain %{public}d", userId); + return appGalleryConnectionMap_[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)); + appGalleryConnectionMap_[userId] = connection; + } + 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; + } + } + return connection; +} + +DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation, + const 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()); + 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); return DisposeErr::CONN_FAIL; } @@ -172,16 +202,16 @@ 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); + 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 new file mode 100644 index 000000000..10b2c19c1 --- /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, userId = %{public}d", userId); + std::lock_guard autoLock(appRemoteObjLock_); + if (appRemoteObj_ != nullptr) { + return true; + } + + auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); + if (appGalleryBundleName.empty()) { + HILOGI("ConnectExtAbility GetAppGalleryBundleName failed, userId = %{public}d", userId); + 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, userId = %{public}d", ret, userId); + appRemoteObj_ = nullptr; + return false; + } + HILOGI("ConnectExtAbility success, ret=%{public}d, userId = %{public}d", ret, userId); + 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 335987159..1903c825f 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -637,7 +637,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, oldBackupVersion); OnStartSched(); @@ -688,6 +690,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); } @@ -723,7 +726,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, oldBackupVersion); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -771,6 +776,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); @@ -780,8 +786,7 @@ void Service::SetCurrentSessProperties(std::vector bool broadCastRet = BJsonUtil::FindBundleInfoByName(bundleNameDetailMap, bundleNameIndexInfo, BROADCAST_TYPE, broadCastInfo); if (broadCastRet) { - bool notifyRet = - DelayedSingleton::GetInstance()->NotifyBundleDetail(broadCastInfo); + bool notifyRet = DelayedSingleton::GetInstance()->NotifyBundleDetail(broadCastInfo); HILOGI("Publish event end, notify result is:%{public}d", notifyRet); } bool uniCastRet = BJsonUtil::FindBundleInfoByName(bundleNameDetailMap, bundleNameIndexInfo, UNICAST_TYPE, @@ -815,7 +820,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__); @@ -858,7 +865,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__); @@ -891,6 +900,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); } @@ -910,11 +920,13 @@ ErrCode Service::ServiceResultReport(const std::string restoreRetInfo, } 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) { @@ -923,9 +935,11 @@ 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 (...) { NotifyCloneBundleFinish(callerName, sennario); + OnAllBundlesFinished(BError(BError::Codes::OK)); HILOGE("Unexpected exception"); return EPERM; } @@ -934,6 +948,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) { @@ -944,13 +959,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) @@ -984,7 +1000,6 @@ void Service::NotifyCloneBundleFinish(std::string bundleName, const BackupRestor ClearSessionAndSchedInfo(bundleName); } RemoveExtensionMutex(bundleName); - OnAllBundlesFinished(BError(BError::Codes::OK)); } catch (...) { HILOGI("Unexpected exception"); ReleaseOnException(); @@ -1137,7 +1152,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); + 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) { @@ -1146,7 +1162,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); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, + session_->GetSessionUserId()); HILOGI("ExtConnectFailed EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1264,7 +1281,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) { @@ -1311,7 +1330,8 @@ 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, + session_->GetSessionUserId()); HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } @@ -1327,7 +1347,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, session_->GetSessionUserId()); HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); if (disposeErr != DisposeErr::OK) { @@ -1346,7 +1366,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); + 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; @@ -1675,7 +1696,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); @@ -1870,7 +1893,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 37f306372..5d14f7825 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -365,7 +365,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(); @@ -411,7 +413,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); HandleCurGroupIncBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); @@ -823,6 +827,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 8b79408c3..ebeb2b62f 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -541,7 +541,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) { @@ -553,11 +575,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)) { 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..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,27 +41,28 @@ sptr AppGalleryDisposeProxy::GetInstance() return new AppGalleryDisposeProxy; } -DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName, const 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, const 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, const 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, const 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, + 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 190bea71a..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&) = 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&, 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&)); - 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&, 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 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/mock/module_ipc/src/svc_session_manager_mock.cpp b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp index 8739d1da4..101b56b78 100644 --- a/tests/mock/module_ipc/src/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/src/svc_session_manager_mock.cpp @@ -123,7 +123,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 bb61902f8..3013bdc9b 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -245,7 +245,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 {}; @@ -355,6 +355,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 bfdf4a3d5..a7fc37476 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 2b3154b1a..4cf3ef3ae 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 ErrCode Start() = 0; virtual ErrCode 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; @@ -122,7 +124,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(ErrCode, Start, ()); MOCK_METHOD(ErrCode, Finish, ()); @@ -137,6 +139,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/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index d68914175..6685f522e 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -358,6 +358,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/service_other_test.cpp b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp index a2f71964b..fcde3efb2 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -931,6 +931,8 @@ HWTEST_F(ServiceTest, SUB_Service_SetCurrentSessProperties_0300, TestSize.Level1 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND; std::string backupVersion; + EXPECT_CALL(*session, GetSessionUserId()).WillOnce(Return(0)).WillOnce(Return(0)).WillOnce(Return(0)) + .WillOnce(Return(0)).WillOnce(Return(0)); EXPECT_CALL(*jsonUtil, BuildBundleNameIndexInfo(_, _)).WillOnce(Return("bundleName")) .WillOnce(Return("bundleName")); EXPECT_CALL(*saUtils, IsSABundleName(_)).WillOnce(Return(true)); 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 c995e219c..57a194b12 100644 --- a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp @@ -616,7 +616,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); @@ -624,14 +624,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 (...) { @@ -1066,7 +1066,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 (...) { @@ -1101,20 +1101,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); @@ -1139,12 +1139,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); @@ -1182,7 +1182,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); @@ -1213,7 +1213,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); 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 ad790f884..1aa90c65d 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(); } @@ -1082,10 +1083,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); @@ -1095,14 +1097,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); @@ -1517,6 +1519,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 aa6b6294bd5ad7b163486a94f8b240a46a0bc8cb Mon Sep 17 00:00:00 2001 From: chensihan Date: Fri, 27 Dec 2024 10:52:23 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B6=85=E5=A4=A7?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20Signed-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backup_sa/include/module_ipc/service.h | 1 + services/backup_sa/src/module_ipc/service.cpp | 34 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index d539c3840..866397305 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -304,6 +304,7 @@ public: */ void RemoveExtensionMutex(const BundleName &bundleName); void StartRunningTimer(const std::string &bundleName); + void AppendBundles(const std::vector &bundleNames); ErrCode AppIncrementalFileReady(const std::string &bundleName, const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, int32_t errCode); public: diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 1903c825f..ddf37990d 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -604,6 +604,16 @@ void Service::HandleExceptionOnAppendBundles(sptr session, } } +void Service::AppendBundles(const std::vector &bundleNames) +{ + std::vector failedBundles; + session_->AppendBundles(restoreBundleNames, failedBundles); + if (!failedBundles.empty()) { + HILOGE("Handle exception on failed bundles, size = %{public}zu", failedBundles.size()); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); + } +} + ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, const std::vector &bundleInfos, RestoreTypeEnum restoreType, int32_t userId) { @@ -637,9 +647,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vectorDecreaseSessionCnt(__PRETTY_FUNCTION__); return BError(BError::Codes::OK); } - std::vector failedBundles; - session_->AppendBundles(restoreBundleNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(restoreBundleNames); SetCurrentSessProperties(restoreInfos, restoreBundleNames, bundleNameDetailMap, isClearDataFlags, restoreType, oldBackupVersion); OnStartSched(); @@ -726,9 +734,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, HILOGW("RestoreBundleNames is empty."); return BError(BError::Codes::OK); } - std::vector failedBundles; - session_->AppendBundles(restoreBundleNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(restoreBundleNames); SetCurrentSessProperties(restoreInfos, restoreBundleNames, restoreType, oldBackupVersion); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -820,9 +826,7 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); - std::vector failedBundles; - session_->AppendBundles(supportBackupNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, false); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -865,9 +869,7 @@ ErrCode Service::AppendBundlesDetailsBackupSession(const vector &bun auto bundleDetails = MakeDetailList(bundleNames); auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, false, bundleNames); - std::vector failedBundles; - session_->AppendBundles(supportBackupNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(supportBackupNames); HandleCurGroupBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); @@ -1281,9 +1283,7 @@ void Service::HandleRestoreDepsBundle(const string &bundleName) HILOGI("Start restore session, bundle: %{public}s", bundle.first.c_str()); restoreBundleNames.emplace_back(bundle.first); } - std::vector failedBundles; - session_->AppendBundles(restoreBundleNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(restoreBundleNames); for (const auto &bundle : restoreBundleMap) { for (auto &bundleInfo : SvcRestoreDepsManager::GetInstance().GetAllBundles()) { if (bundle.first != bundleInfo.name) { @@ -1696,9 +1696,7 @@ ErrCode Service::AppendBundlesClearSession(const std::vector &bundle std::string bundleNameIndexStr = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); supportBundleNames.emplace_back(bundleNameIndexStr); } - std::vector failedBundles; - session_->AppendBundles(supportBundleNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(supportBundleNames); for (auto info : backupInfos) { std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); session_->SetBackupExtName(bundleNameIndexInfo, info.extensionName); -- Gitee From d1499f68044e56f5dfbf2475285a53f3b867eab0 Mon Sep 17 00:00:00 2001 From: chensihan Date: Fri, 27 Dec 2024 11:07:17 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=A5=E5=8F=82=20Si?= =?UTF-8?q?gned-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 | 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 ddf37990d..1d3c39d06 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -607,7 +607,7 @@ void Service::HandleExceptionOnAppendBundles(sptr session, void Service::AppendBundles(const std::vector &bundleNames) { std::vector failedBundles; - session_->AppendBundles(restoreBundleNames, failedBundles); + session_->AppendBundles(bundleNames, failedBundles); if (!failedBundles.empty()) { HILOGE("Handle exception on failed bundles, size = %{public}zu", failedBundles.size()); HandleExceptionOnAppendBundles(session_, failedBundles, {}); -- Gitee From 52002b4d357e221e051db5bd5fe8b7e102bf1b48 Mon Sep 17 00:00:00 2001 From: chensihan Date: Mon, 30 Dec 2024 02:57:59 +0000 Subject: [PATCH 04/13] update services/backup_sa/src/module_ipc/svc_session_manager.cpp. Signed-off-by: chensihan --- .../src/module_ipc/svc_session_manager.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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 486512fa8..94654fa3b 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -596,10 +596,15 @@ void SvcSessionManager::SetBundleUserId(const string &bundleName, const int32_t { unique_lock lock(lock_); if (!impl_.clientToken) { - throw BError(BError::Codes::SA_INVAL_ARG, "No caller token was specified"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return; + } it->second.userId = userId; } @@ -607,10 +612,15 @@ 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"); + HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); + return 0; } - auto it = GetBackupExtNameMap(bundleName); + auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); + if (!findBundleSuc) { + HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); + return 0; + } return it->second.userId; } -- Gitee From 285766bf01fde6100a10694b95cd521f3ce69b0f Mon Sep 17 00:00:00 2001 From: chensihan Date: Fri, 10 Jan 2025 17:06:24 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E6=B8=85=E7=90=86=E5=A4=84=E7=BD=AE?= =?UTF-8?q?=E4=BD=8D=20Signed-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 | 51 +++++++++++++------ .../src/module_ipc/service_incremental.cpp | 1 - .../backup_sa/src/module_ipc/sub_service.cpp | 1 + 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 3eaf7da9c..289dfef6a 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1364,15 +1364,21 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) if (scenario != IServiceReverse::Scenario::RESTORE) { return; } - if (!disposal_->IfBundleNameInDisposalConfigFile(bundleName)) { + int32_t userId = session_->GetBundleUserId(bundleName); + std::string bundleNameWithUserId = BundleNameWithUserId(bundleName, userId); + if (!disposal_->IfBundleNameInDisposalConfigFile(bundleNameWithUserId)) { HILOGE("WriteDisposalConfigFile Failed"); return; } - HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, - session_->GetBundleUserId(bundleName)); - HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, - bundleName.c_str()); + HILOGI("AppendIntoDisposalConfigFile OK, bundleName=%{public}s", bundleNameWithUserId.c_str()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName, userId); + HILOGI("StartRestore, code=%{public}d, bundleName=%{public}s, userId=%{public}d", disposeErr, bundleName.c_str(), + userId); +} + +static string BundleNameWithUserId(const string& bundleName, const int32_t userId) +{ + return to_string(userId) + "-" + bundleName; } void Service::SendEndAppGalleryNotify(const BundleName &bundleName) @@ -1386,28 +1392,42 @@ void Service::SendEndAppGalleryNotify(const BundleName &bundleName) if (scenario != IServiceReverse::Scenario::RESTORE) { return; } - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, - session_->GetBundleUserId(bundleName)); - HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); + int32_t userId = session_->GetBundleUserId(bundleName); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, userId); + HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s, userId=%{public}d", disposeErr, bundleName.c_str(), + userId); if (disposeErr != DisposeErr::OK) { HILOGE("Error code=%{public}d, disposal will be clear in the end", disposeErr); return; } - if (!disposal_->DeleteFromDisposalConfigFile(bundleName)) { - HILOGE("DeleteFromDisposalConfigFile Failed, bundleName=%{public}s", bundleName.c_str()); + std::string bundleNameWithUserId = BundleNameWithUserId(bundleName, userId); + if (!disposal_->DeleteFromDisposalConfigFile(bundleNameWithUserId)) { + HILOGE("DeleteFromDisposalConfigFile Failed, bundleName=%{public}s", bundleNameWithUserId.c_str()); return; } - HILOGI("DeleteFromDisposalConfigFile OK, bundleName=%{public}s", bundleName.c_str()); + HILOGI("DeleteFromDisposalConfigFile OK, bundleName=%{public}s", bundleNameWithUserId.c_str()); +} + +static std::tuple SplitBundleName(const string& bundleNameWithId) +{ + size_t found = bundleNameWithId.find('-'); + if (found == std::string::npos) { + return { bundleNameWithId, GetUserIdDefault() }; + } + std::string bundleName = bundleNameWithId.substr(found + 1, bundleNameWithId.length()); + int32_t userId = std::atoi(bundleNameWithId.substr(0, found)); + return { bundleName, userId}; } void Service::TryToClearDispose(const BundleName &bundleName) { + auto [bundle, userId] = SplitBundleName(bundleName); int32_t maxAtt = MAX_TRY_CLEAR_DISPOSE_NUM; int32_t att = 0; while (att < maxAtt) { - DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName, - session_->GetBundleUserId(bundleName)); - HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundle, userId); + HILOGI("EndRestore, code=%{public}d, bundleName=%{public}s, userId=%{public}d", disposeErr, bundle.c_str(), + userId); if (disposeErr == DisposeErr::OK) { break; } @@ -1959,7 +1979,6 @@ void Service::NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerN ); } else if (scenario == IServiceReverse::Scenario::RESTORE) { HILOGI("will notify clone data, scenario is Restore"); - SendEndAppGalleryNotify(callerName); session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, callerName); } BundleEndRadarReport(callerName, errCode, scenario); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index c9d86f1b9..e300d04e6 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -774,7 +774,6 @@ void Service::NotifyCallerCurAppIncrementDone(ErrCode errCode, const std::string ); } else if (scenario == IServiceReverse::Scenario::RESTORE) { HILOGI("will notify clone data, scenario is Restore"); - SendEndAppGalleryNotify(callerName); session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleFinished(errCode, callerName); BundleEndRadarReport(callerName, errCode, scenario); } diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 4664c5f98..419d86c87 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -610,6 +610,7 @@ ErrCode Service::HandleCurAppDone(ErrCode errCode, const std::string &bundleName proxy->HandleClear(); session_->StopFwkTimer(bundleName); session_->StopExtTimer(bundleName); + SendEndAppGalleryNotify(callerName); backUpConnection->DisconnectBackupExtAbility(); ClearSessionAndSchedInfo(bundleName); if (isIncBackup) { -- Gitee From bde27ea6bd2b0994536a3e7e5031b630c45eed95 Mon Sep 17 00:00:00 2001 From: chensihan Date: Fri, 10 Jan 2025 17:14:07 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=B8=85=E7=90=86=E5=89=8D=E5=A4=84?= =?UTF-8?q?=E7=BD=AE=20Signed-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 | 11 ++++++++++- .../backup_sa/src/module_ipc/service_incremental.cpp | 5 +++++ services/backup_sa/src/module_ipc/sub_service.cpp | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 680916245..0ed7acc7e 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1105,6 +1105,7 @@ void Service::ExtStart(const string &bundleName) StartCurBundleBackupOrRestore(bundleName); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_INVAL_ARG)); } @@ -1143,6 +1144,7 @@ void Service::StartCurBundleBackupOrRestore(const std::string &bundleName) session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, scenario); if (ret) { + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_INVAL_ARG)); } @@ -1217,6 +1219,7 @@ void Service::ExtConnectFailed(const string &bundleName, ErrCode ret) HILOGE("begin %{public}s", bundleName.data()); scenario = session_->GetScenario(); ReportOnExtConnectFailed(scenario, bundleName, ret); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return; @@ -1248,6 +1251,7 @@ void Service::ExtConnectDone(string bundleName) BConstants::ServiceSchedAction curSchedAction = session_->GetServiceSchedAction(bundleName); if (curSchedAction == BConstants::ServiceSchedAction::UNKNOWN) { HILOGE("Can not find bundle from this session, bundleName:%{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SA_REFUSED_ACT)); return; @@ -1271,6 +1275,7 @@ void Service::ExtConnectDone(string bundleName) sched_->Sched(bundleName); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::SDK_INVAL_ARG)); return; @@ -1418,7 +1423,7 @@ static std::tuple SplitBundleName(const string& bundleName } std::string bundleName = bundleNameWithId.substr(found + 1, bundleNameWithId.length()); int32_t userId = std::atoi(bundleNameWithId.substr(0, found)); - return { bundleName, userId}; + return { bundleName, userId }; } void Service::TryToClearDispose(const BundleName &bundleName) @@ -1887,6 +1892,7 @@ ErrCode Service::BackupSA(std::string bundleName) BundleBeginRadarReport(bundleName, ret, scenario); if (ret) { HILOGI("BackupSA ret is %{public}d", ret); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return BError(ret); @@ -2029,6 +2035,7 @@ std::function Service::TimeOutCallback(wptr ptr, std::string bu DoTimeout(thisPtr, bundleName); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } @@ -2088,10 +2095,12 @@ void Service::DoTimeout(wptr ptr, std::string bundleName) } sessionPtr->StopFwkTimer(bundleName); sessionPtr->StopExtTimer(bundleName); + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index e300d04e6..4a1f6f563 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -198,6 +198,7 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) if (BundleMgrAdapter::IsUser0BundleName(bundleName, session_->GetSessionUserId())) { auto ret = proxy->User0OnBackup(); if (ret) { + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -222,6 +223,7 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) UniqueFd lastManifestFd(session->GetIncrementalManifestFd(bundleName)); auto ret = proxy->HandleIncrementalBackup(move(fdLocal), move(lastManifestFd)); if (ret) { + SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -711,12 +713,14 @@ bool Service::IncrementalBackup(const string &bundleName) auto backUpConnection = session_->GetExtConnection(bundleName); if (backUpConnection == nullptr) { HILOGE("backUpConnection is empty, bundle:%{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return true; } auto proxy = backUpConnection->GetBackupExtProxy(); if (!proxy) { HILOGE("Increment backup error, extension proxy is empty, bundleName:%{public}s", bundleName.c_str()); + SendEndAppGalleryNotify(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return true; } @@ -725,6 +729,7 @@ bool Service::IncrementalBackup(const string &bundleName) session_->GetServiceReverseProxy()->IncrementalBackupOnBundleStarted(ret, bundleName); BundleBeginRadarReport(bundleName, ret, IServiceReverse::Scenario::BACKUP); if (ret) { + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); } diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index d6c422a39..156de3430 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -445,6 +445,7 @@ void Service::ExtConnectDied(const string &callName) bool needCleanData = session_->GetClearDataFlag(callName); if (!needCleanData) { HILOGE("Current extension is died, but not need clean data, bundleName:%{public}s", callName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(callName); NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); return; @@ -454,12 +455,14 @@ void Service::ExtConnectDied(const string &callName) if (ret) { /* Clear Session before notice client finish event */ HILOGE("Current bundle launch extension failed, bundleName:%{public}s", callName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(callName); } /* Notice Client Ext Ability Process Died */ NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", callName.c_str()); + SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(callName); NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -500,7 +503,6 @@ void Service::NoticeClientFinish(const string &bundleName, ErrCode errCode) HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); HILOGI("begin %{public}s", bundleName.c_str()); try { - SendEndAppGalleryNotify(bundleName); auto scenario = session_->GetScenario(); if (scenario == IServiceReverse::Scenario::BACKUP && session_->GetIsIncrementalBackup()) { session_->GetServiceReverseProxy()->IncrementalBackupOnBundleFinished(errCode, bundleName); -- Gitee From aab8f2ad7d85f4317a0505298da4c796615ff44d Mon Sep 17 00:00:00 2001 From: chensihan Date: Mon, 13 Jan 2025 14:25:55 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chensihan --- services/backup_sa/include/module_ipc/service.h | 2 ++ services/backup_sa/src/module_ipc/service.cpp | 6 +++--- services/backup_sa/src/module_ipc/sub_service.cpp | 8 ++++---- services/backup_sa/src/module_ipc/svc_session_manager.cpp | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index b954c2185..f9b05595f 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -308,6 +308,8 @@ public: */ void RemoveExtensionMutex(const BundleName &bundleName); void StartRunningTimer(const std::string &bundleName); + string BundleNameWithUserId(const string& bundleName, const int32_t userId); + std::tuple SplitBundleName(const string& bundleNameWithId); void AppendBundles(const std::vector &bundleNames); ErrCode AppIncrementalFileReady(const std::string &bundleName, const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, int32_t errCode); diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 0ed7acc7e..36eac0b3e 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1383,7 +1383,7 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) userId); } -static string BundleNameWithUserId(const string& bundleName, const int32_t userId) +string Service::BundleNameWithUserId(const string& bundleName, const int32_t userId) { return to_string(userId) + "-" + bundleName; } @@ -1415,14 +1415,14 @@ void Service::SendEndAppGalleryNotify(const BundleName &bundleName) HILOGI("DeleteFromDisposalConfigFile OK, bundleName=%{public}s", bundleNameWithUserId.c_str()); } -static std::tuple SplitBundleName(const string& bundleNameWithId) +std::tuple Service::SplitBundleName(const string& bundleNameWithId) { size_t found = bundleNameWithId.find('-'); if (found == std::string::npos) { return { bundleNameWithId, GetUserIdDefault() }; } std::string bundleName = bundleNameWithId.substr(found + 1, bundleNameWithId.length()); - int32_t userId = std::atoi(bundleNameWithId.substr(0, found)); + int32_t userId = std::atoi(bundleNameWithId.substr(0, found).c_str()); return { bundleName, userId }; } diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 156de3430..1947fb194 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -445,7 +445,7 @@ void Service::ExtConnectDied(const string &callName) bool needCleanData = session_->GetClearDataFlag(callName); if (!needCleanData) { HILOGE("Current extension is died, but not need clean data, bundleName:%{public}s", callName.c_str()); - SendEndAppGalleryNotify(bundleName); + SendEndAppGalleryNotify(callName); ClearSessionAndSchedInfo(callName); NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); return; @@ -455,14 +455,14 @@ void Service::ExtConnectDied(const string &callName) if (ret) { /* Clear Session before notice client finish event */ HILOGE("Current bundle launch extension failed, bundleName:%{public}s", callName.c_str()); - SendEndAppGalleryNotify(bundleName); + SendEndAppGalleryNotify(callName); ClearSessionAndSchedInfo(callName); } /* Notice Client Ext Ability Process Died */ NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); } catch (...) { HILOGE("Unexpected exception, bundleName: %{public}s", callName.c_str()); - SendEndAppGalleryNotify(bundleName); + SendEndAppGalleryNotify(callName); ClearSessionAndSchedInfo(callName); NoticeClientFinish(callName, BError(BError::Codes::EXT_ABILITY_DIED)); } @@ -620,7 +620,7 @@ ErrCode Service::HandleCurAppDone(ErrCode errCode, const std::string &bundleName proxy->HandleClear(); session_->StopFwkTimer(bundleName); session_->StopExtTimer(bundleName); - SendEndAppGalleryNotify(callerName); + SendEndAppGalleryNotify(bundleName); backUpConnection->DisconnectBackupExtAbility(); ClearSessionAndSchedInfo(bundleName); if (isIncBackup) { 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 28a905441..ad0a274dd 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -634,13 +634,13 @@ int32_t SvcSessionManager::GetBundleUserId(const string &bundleName) shared_lock lock(lock_); if (!impl_.clientToken) { HILOGE("No caller token was specified, bundleName:%{public}s", bundleName.c_str()); - return 0; + return BConstants::DEFAULT_USER_ID; } auto [findBundleSuc, it] = GetBackupExtNameMap(bundleName); if (!findBundleSuc) { HILOGE("BackupExtNameMap can not find bundle %{public}s", bundleName.c_str()); - return 0; + return GetSessionUserId(); } return it->second.userId; } -- Gitee From f20b83cd157dc12aa9dafe6eb1a3dcdaf3b90f4a Mon Sep 17 00:00:00 2001 From: chensihan Date: Mon, 13 Jan 2025 16:16:53 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=9B=91=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81=20Signed-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backup_sa/include/module_ipc/service.h | 1 + .../include/module_ipc/svc_session_manager.h | 2 +- services/backup_sa/src/module_ipc/service.cpp | 61 ++++++------------- .../src/module_ipc/service_incremental.cpp | 3 +- .../backup_sa/src/module_ipc/sub_service.cpp | 27 ++++++++ 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index f9b05595f..943dc3801 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -311,6 +311,7 @@ public: string BundleNameWithUserId(const string& bundleName, const int32_t userId); std::tuple SplitBundleName(const string& bundleNameWithId); void AppendBundles(const std::vector &bundleNames); + void ReportOnBundleStarted(IServiceReverse::Scenario, const std::string &bundleName); ErrCode AppIncrementalFileReady(const std::string &bundleName, const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, int32_t errCode); public: 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 846020e0c..a132e7641 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -68,7 +68,7 @@ struct BackupExtInfo { std::string backupParameters; int32_t backupPriority; std::string extInfo; - int32_t userId {0}; + int32_t userId {100}; int32_t appendNum {1}; bool isClearData {true}; bool isInPublishFile {false}; diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 36eac0b3e..e6c0111df 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -618,16 +618,6 @@ void Service::HandleExceptionOnAppendBundles(sptr session, } } -void Service::AppendBundles(const std::vector &bundleNames) -{ - std::vector failedBundles; - session_->AppendBundles(bundleNames, failedBundles); - if (!failedBundles.empty()) { - HILOGE("Handle exception on failed bundles, size = %{public}zu", failedBundles.size()); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); - } -} - ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, const vector &bundleNames, const std::vector &bundleInfos, RestoreTypeEnum restoreType, int32_t userId) { @@ -1111,6 +1101,17 @@ void Service::ExtStart(const string &bundleName) } } +void Service::ReportOnBundleStarted(IServiceReverse::Scenario, const std::string &bundleName) +{ + if (scenario == IServiceReverse::Scenario::BACKUP) { + session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), + bundleName); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), + bundleName); + } +} + void Service::StartCurBundleBackupOrRestore(const std::string &bundleName) { HILOGI("Begin handle current bundle full backup or full restore, bundleName:%{public}s", bundleName.c_str()); @@ -1118,25 +1119,13 @@ void Service::StartCurBundleBackupOrRestore(const std::string &bundleName) auto backUpConnection = session_->GetExtConnection(bundleName); if (backUpConnection == nullptr) { HILOGE("Error, backUpConnection is empty, bundle:%{public}s", bundleName.c_str()); - if (scenario == IServiceReverse::Scenario::BACKUP) { - session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } + ReportOnBundleStarted(scenario, bundleName); return; } auto proxy = backUpConnection->GetBackupExtProxy(); if (proxy == nullptr) { HILOGE("Error, Extension backup Proxy is empty, bundle:%{public}s", bundleName.c_str()); - if (scenario == IServiceReverse::Scenario::BACKUP) { - session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } + ReportOnBundleStarted(scenario, bundleName); return; } if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -1383,11 +1372,6 @@ void Service::SendStartAppGalleryNotify(const BundleName &bundleName) userId); } -string Service::BundleNameWithUserId(const string& bundleName, const int32_t userId) -{ - return to_string(userId) + "-" + bundleName; -} - void Service::SendEndAppGalleryNotify(const BundleName &bundleName) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -1415,20 +1399,16 @@ void Service::SendEndAppGalleryNotify(const BundleName &bundleName) HILOGI("DeleteFromDisposalConfigFile OK, bundleName=%{public}s", bundleNameWithUserId.c_str()); } -std::tuple Service::SplitBundleName(const string& bundleNameWithId) -{ - size_t found = bundleNameWithId.find('-'); - if (found == std::string::npos) { - return { bundleNameWithId, GetUserIdDefault() }; - } - std::string bundleName = bundleNameWithId.substr(found + 1, bundleNameWithId.length()); - int32_t userId = std::atoi(bundleNameWithId.substr(0, found).c_str()); - return { bundleName, userId }; -} - void Service::TryToClearDispose(const BundleName &bundleName) { auto [bundle, userId] = SplitBundleName(bundleName); + if (bundle.empty() || userId == -1) { + HILOGE("BundleName from disposal config is invalid, bundleName = %{public}s", bundleName.c_str()); + if (!disposal_->DeleteFromDisposalConfigFile(bundleName)) { + HILOGE("DeleteFromDisposalConfigFile Failed, bundleName=%{public}s", bundleName.c_str()); + } + return; + } int32_t maxAtt = MAX_TRY_CLEAR_DISPOSE_NUM; int32_t att = 0; while (att < maxAtt) { @@ -1892,7 +1872,6 @@ ErrCode Service::BackupSA(std::string bundleName) BundleBeginRadarReport(bundleName, ret, scenario); if (ret) { HILOGI("BackupSA ret is %{public}d", ret); - SendEndAppGalleryNotify(bundleName); ClearSessionAndSchedInfo(bundleName); NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); return BError(ret); diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index 4a1f6f563..e60ccc60b 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -196,8 +196,7 @@ void Service::StartGetFdTask(std::string bundleName, wptr ptr) } // distinguish whether it is 0 user if (BundleMgrAdapter::IsUser0BundleName(bundleName, session_->GetSessionUserId())) { - auto ret = proxy->User0OnBackup(); - if (ret) { + if (proxy->User0OnBackup()) { SendEndAppGalleryNotify(bundleName); thisPtr->ClearSessionAndSchedInfo(bundleName); thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 1947fb194..16b03ab00 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -66,6 +66,33 @@ namespace OHOS::FileManagement::Backup { using namespace std; +void Service::AppendBundles(const std::vector &bundleNames) +{ + std::vector failedBundles; + session_->AppendBundles(bundleNames, failedBundles); + if (!failedBundles.empty()) { + HILOGE("Handle exception on failed bundles, size = %{public}zu", failedBundles.size()); + HandleExceptionOnAppendBundles(session_, failedBundles, {}); + } +} + +string Service::BundleNameWithUserId(const string& bundleName, const int32_t userId) +{ + return to_string(userId) + "-" + bundleName; +} + +std::tuple Service::SplitBundleName(const string& bundleNameWithId) +{ + size_t found = bundleNameWithId.find('-'); + if (found == std::string::npos) { + HILOGE("Can not split bundleName = %{public}s", bundleNameWithId.c_str()); + return { "", -1 }; + } + std::string bundleName = bundleNameWithId.substr(found + 1, bundleNameWithId.length()); + int32_t userId = std::atoi(bundleNameWithId.substr(0, found).c_str()); + return { bundleName, userId }; +} + vector Service::MakeDetailList(const vector &bundleNames) { vector bundleDetails {}; -- Gitee From ed08159a55c988bfbc20e492ae69d3366e4561cc Mon Sep 17 00:00:00 2001 From: chensihan Date: Mon, 13 Jan 2025 16:37:43 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=AE=E9=A2=98=20Si?= =?UTF-8?q?gned-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/backup_sa/include/module_ipc/service.h | 2 +- services/backup_sa/src/module_ipc/service.cpp | 11 ----------- services/backup_sa/src/module_ipc/sub_service.cpp | 9 +++++++++ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/services/backup_sa/include/module_ipc/service.h b/services/backup_sa/include/module_ipc/service.h index 943dc3801..37c7d46f5 100644 --- a/services/backup_sa/include/module_ipc/service.h +++ b/services/backup_sa/include/module_ipc/service.h @@ -311,7 +311,7 @@ public: string BundleNameWithUserId(const string& bundleName, const int32_t userId); std::tuple SplitBundleName(const string& bundleNameWithId); void AppendBundles(const std::vector &bundleNames); - void ReportOnBundleStarted(IServiceReverse::Scenario, const std::string &bundleName); + void ReportOnBundleStarted(IServiceReverse::Scenario scenario, const std::string &bundleName); ErrCode AppIncrementalFileReady(const std::string &bundleName, const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, int32_t errCode); public: diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index e6c0111df..66749bce8 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -1101,17 +1101,6 @@ void Service::ExtStart(const string &bundleName) } } -void Service::ReportOnBundleStarted(IServiceReverse::Scenario, const std::string &bundleName) -{ - if (scenario == IServiceReverse::Scenario::BACKUP) { - session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), - bundleName); - } -} - void Service::StartCurBundleBackupOrRestore(const std::string &bundleName) { HILOGI("Begin handle current bundle full backup or full restore, bundleName:%{public}s", bundleName.c_str()); diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp index 16b03ab00..b59a57ddb 100644 --- a/services/backup_sa/src/module_ipc/sub_service.cpp +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -76,6 +76,15 @@ void Service::AppendBundles(const std::vector &bundleNames) } } +void Service::ReportOnBundleStarted(IServiceReverse::Scenario scenario, const std::string &bundleName) +{ + if (scenario == IServiceReverse::Scenario::BACKUP) { + session_->GetServiceReverseProxy()->BackupOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), bundleName); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::SA_INVAL_ARG), bundleName); + } +} + string Service::BundleNameWithUserId(const string& bundleName, const int32_t userId) { return to_string(userId) + "-" + bundleName; -- Gitee From 032cbf97830d26ab064a48a3522131aeeec82b73 Mon Sep 17 00:00:00 2001 From: chensihan Date: Mon, 13 Jan 2025 17:24:24 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chensihan --- .../module_ipc/service_throw_test.cpp | 35 ------------------- .../backup_sa/module_ipc/sub_service_test.cpp | 1 + .../module_ipc/svc_session_manager_test.cpp | 22 ++---------- 3 files changed, 3 insertions(+), 55 deletions(-) diff --git a/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp b/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp index 0dd449a16..05bd4ba71 100644 --- a/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_throw_test.cpp @@ -675,41 +675,6 @@ HWTEST_F(ServiceThrowTest, SUB_Service_throw_OnBackupExtensionDied_0100, testing GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_OnBackupExtensionDied_0100"; } -/** - * @tc.number: SUB_Service_throw_ExtStart_0100 - * @tc.name: SUB_Service_throw_ExtStart_0100 - * @tc.desc: 测试 ExtStart 接口的 catch 分支 - * @tc.size: MEDIUM - * @tc.type: FUNC - * @tc.level Level 1 - * @tc.require: issuesIAC04T - */ -HWTEST_F(ServiceThrowTest, SUB_Service_throw_ExtStart_0100, testing::ext::TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ServiceThrowTest-begin SUB_Service_throw_ExtStart_0100"; - try { - EXPECT_NE(service, nullptr); - string bundleName; - EXPECT_CALL(*sessionMock, GetScenario()).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - return IServiceReverse::Scenario::UNDEFINED; - })).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - return IServiceReverse::Scenario::UNDEFINED; - })); - EXPECT_CALL(*sessionMock, RemoveExtInfo(_)).WillOnce(Invoke([]() { - throw BError(BError::Codes::EXT_THROW_EXCEPTION); - })); - EXPECT_CALL(*sessionMock, IsOnAllBundlesFinished()).WillOnce(Return(false)); - service->ExtStart(bundleName); - EXPECT_TRUE(true); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ServiceThrowTest-an exception occurred by ExtStart."; - } - GTEST_LOG_(INFO) << "ServiceThrowTest-end SUB_Service_throw_ExtStart_0100"; -} - /** * @tc.number: SUB_Service_throw_ExtConnectFailed_0100 * @tc.name: SUB_Service_throw_ExtConnectFailed_0100 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 55574ba53..1363c4a4e 100644 --- a/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/sub_service_test.cpp @@ -747,6 +747,7 @@ HWTEST_F(ServiceTest, SUB_Service_ExtConnectDone_0000, TestSize.Level1) { GTEST_LOG_(INFO) << "ServiceTest-begin SUB_Service_ExtConnectDone_0000"; try { + EXPECT_CALL(*session, GetClearDataFlag(_)).WillOnce(Return(false)).WillOnce(Return(false)); EXPECT_CALL(*session, GetServiceSchedAction(_)).WillOnce(Return(BConstants::ServiceSchedAction::WAIT)); EXPECT_CALL(*cdConfig, InsertClearBundleRecord(_)).WillOnce(Return(true)); service->ExtConnectDone(""); 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 631233c0d..54c6a5067 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 @@ -1359,15 +1359,6 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_SetBundleUserId_0100, test 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] = {}; @@ -1393,20 +1384,11 @@ HWTEST_F(SvcSessionManagerTest, SUB_backup_sa_session_GetBundleUserId_0100, test { 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); + auto result = sessionManagerPtr_->GetBundleUserId(BUNDLE_NAME); + EXPECT_EQ(result, BConstants::DEFAULT_USER_ID); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "SvcSessionManagerTest-an exception occurred by GetBundleUserId."; -- Gitee From e7201500895cc99e56905c94fda5ff5bbcd4b943 Mon Sep 17 00:00:00 2001 From: chensihan Date: Tue, 14 Jan 2025 08:46:45 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Signed-off-by:=20che?= =?UTF-8?q?nsihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/backup_sa/src/module_ipc/service_incremental.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/services/backup_sa/src/module_ipc/service_incremental.cpp b/services/backup_sa/src/module_ipc/service_incremental.cpp index e60ccc60b..af75eb0c9 100644 --- a/services/backup_sa/src/module_ipc/service_incremental.cpp +++ b/services/backup_sa/src/module_ipc/service_incremental.cpp @@ -392,9 +392,7 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); - std::vector failedBundles; - session_->AppendBundles(supportBackupNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(supportBackupNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); SetCurrentBackupSessProperties(supportBackupNames, session_->GetSessionUserId(), backupInfos, true); OnStartSched(); @@ -440,9 +438,7 @@ ErrCode Service::AppendBundlesIncrementalBackupSession(const std::vectorGetSessionUserId()); std::vector supportBackupNames = GetSupportBackupBundleNames(backupInfos, true, bundleNames); - std::vector failedBundles; - session_->AppendBundles(supportBackupNames, failedBundles); - HandleExceptionOnAppendBundles(session_, failedBundles, {}); + AppendBundles(supportBackupNames); SetBundleIncDataInfo(bundlesToBackup, supportBackupNames); HandleCurGroupIncBackupInfos(backupInfos, bundleNameDetailMap, isClearDataFlags); OnStartSched(); -- Gitee From d652a96440f50b31817c85a30ea35646390f3371 Mon Sep 17 00:00:00 2001 From: chensihan Date: Tue, 14 Jan 2025 09:51:09 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dut=20Signed-off-by:=20c?= =?UTF-8?q?hensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../unittests/backup_sa/module_ipc/service_incremental_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp index a46f15816..ef80c5db3 100644 --- a/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_incremental_test.cpp @@ -316,6 +316,8 @@ UniqueFd Service::GetLocalCapabilitiesForBundleInfos() { return UniqueFd(-1); } + +void Service::AppendBundles(const std::vector &bundleNames) {} } // namespace OHOS::FileManagement::Backup namespace OHOS::FileManagement::Backup { -- Gitee From c6737816bdc759707dfeb3faf444cb3fefe5c998 Mon Sep 17 00:00:00 2001 From: chensihan Date: Tue, 14 Jan 2025 17:25:59 +0800 Subject: [PATCH 13/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=AD=94=E9=AC=BC?= =?UTF-8?q?=E6=95=B0=E5=AD=97=20Signed-off-by:=20chensihan=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/backup_sa/include/module_ipc/svc_session_manager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a132e7641..b2f6a3fa5 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -68,7 +68,7 @@ struct BackupExtInfo { std::string backupParameters; int32_t backupPriority; std::string extInfo; - int32_t userId {100}; + int32_t userId {BConstants::DEFAULT_USER_ID}; int32_t appendNum {1}; bool isClearData {true}; bool isInPublishFile {false}; -- Gitee