From 24aacd34dce51003c4d942def3bf4e67d2c7253d Mon Sep 17 00:00:00 2001 From: huaqingsimeng <1004904143@qq.com> Date: Thu, 3 Aug 2023 10:10:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=87=E4=BB=BD=E6=81=A2?= =?UTF-8?q?=E5=A4=8Dproxy=E4=BE=A7=E9=87=8A=E6=94=BE=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huaqingsimeng --- .../backup_kit_inner/src/service_proxy.cpp | 34 +++++++++++++++---- .../backup_impl/service_proxy_test.cpp | 3 +- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/frameworks/native/backup_kit_inner/src/service_proxy.cpp b/frameworks/native/backup_kit_inner/src/service_proxy.cpp index 52eda3732..3ec9fafcf 100644 --- a/frameworks/native/backup_kit_inner/src/service_proxy.cpp +++ b/frameworks/native/backup_kit_inner/src/service_proxy.cpp @@ -15,12 +15,14 @@ #include "service_proxy.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + #include "b_error/b_error.h" #include "b_error/b_excep_utils.h" #include "b_resources/b_constants.h" #include "filemgmt_libhilog.h" -#include "iservice_registry.h" -#include "system_ability_definition.h" +#include "svc_death_recipient.h" namespace OHOS::FileManagement::Backup { using namespace std; @@ -324,8 +326,8 @@ sptr ServiceProxy::GetInstance() } int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, loadCallback); if (ret != ERR_OK) { - HILOGE("Failed to Load systemAbility, systemAbilityId:%d, ret code:%d", FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, - ret); + HILOGE("Failed to Load systemAbility, systemAbilityId:%{private}d, ret code:%{public}d", + FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, ret); return nullptr; } @@ -349,17 +351,37 @@ void ServiceProxy::InvaildInstance() void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const OHOS::sptr &remoteObject) { - HILOGI("Load backup sa success, systemAbilityId:%d, remoteObject result:%s", systemAbilityId, + HILOGI("Load backup sa success, systemAbilityId: %{private}d, remoteObject result:%{private}s", systemAbilityId, (remoteObject != nullptr) ? "true" : "false"); + if (systemAbilityId != FILEMANAGEMENT_BACKUP_SERVICE_SA_ID || remoteObject == nullptr) { + isLoadSuccess_.store(false); + proxyConVar_.notify_one(); + return; + } unique_lock lock(proxyMutex_); serviceProxy_ = iface_cast(remoteObject); + auto remoteObj = serviceProxy_->AsObject(); + if (!remoteObj) { + HILOGE("Failed to get remote object"); + serviceProxy_ = nullptr; + isLoadSuccess_.store(false); + proxyConVar_.notify_one(); + return; + } + + auto callback = [](const wptr &obj) { + ServiceProxy::InvaildInstance(); + HILOGE("Backup service died"); + }; + sptr deathRecipient = sptr(new SvcDeathRecipient(callback)); + remoteObj->AddDeathRecipient(deathRecipient); isLoadSuccess_.store(true); proxyConVar_.notify_one(); } void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) { - HILOGE("Load backup sa failed, systemAbilityId:%d", systemAbilityId); + HILOGE("Load backup sa failed, systemAbilityId:%{private}d", systemAbilityId); unique_lock lock(proxyMutex_); serviceProxy_ = nullptr; isLoadSuccess_.store(false); diff --git a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp index 43d82a76d..78c891237 100644 --- a/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp +++ b/tests/unittests/backup_api/backup_impl/service_proxy_test.cpp @@ -282,8 +282,7 @@ HWTEST_F(ServiceProxyTest, SUB_Service_proxy_AppendBundlesRestoreSession_0100, t GTEST_LOG_(INFO) << "ServiceProxyTest-begin SUB_Service_proxy_AppendBundlesRestoreSession_0100"; EXPECT_CALL(*mock_, SendRequest(_, _, _, _)) .Times(1) - .WillOnce(Invoke(mock_.GetRefPtr(), &IServiceMock::InvokeSendRequest)) - .WillOnce(Return(EPERM)); + .WillOnce(Invoke(mock_.GetRefPtr(), &IServiceMock::InvokeSendRequest)); std::vector bundleNames; TestManager tm("BackupSession_GetFd_0100"); -- Gitee