From d585381ed8552fe73cba6fa519ca30fed45a82a2 Mon Sep 17 00:00:00 2001 From: "yaoruozi1@huawei.com" Date: Mon, 30 Sep 2024 00:10:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E8=BA=AB=E5=BA=94=E7=94=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yaoruozi1@huawei.com --- services/backup_sa/BUILD.gn | 1 + services/backup_sa/src/module_ipc/service.cpp | 355 +-------------- .../backup_sa/src/module_ipc/sub_service.cpp | 420 ++++++++++++++++++ tests/unittests/backup_sa/module_ipc/BUILD.gn | 2 + .../module_ipc/service_other_test.cpp | 1 + .../backup_sa/module_ipc/service_test.cpp | 1 + 6 files changed, 432 insertions(+), 348 deletions(-) create mode 100644 services/backup_sa/src/module_ipc/sub_service.cpp diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index 027216afe..21b78cac5 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -36,6 +36,7 @@ ohos_shared_library("backup_sa") { "src/module_ipc/service_incremental_reverse_proxy.cpp", "src/module_ipc/service_reverse_proxy.cpp", "src/module_ipc/service_stub.cpp", + "src/module_ipc/sub_service.cpp", "src/module_ipc/svc_backup_connection.cpp", "src/module_ipc/svc_extension_incremental_proxy.cpp", "src/module_ipc/svc_extension_proxy.cpp", diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 9c4bc77df..0b5c0a2d9 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -74,8 +74,6 @@ REGISTER_SYSTEM_ABILITY_BY_ID(Service, FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, fals namespace { constexpr int32_t DEBUG_ID = 100; -constexpr int32_t INDEX = 3; -constexpr int32_t MS_1000 = 1000; const static string BROADCAST_TYPE = "broadcast"; const std::string FILE_BACKUP_EVENTS = "FILE_BACKUP_EVENTS"; const static string UNICAST_TYPE = "unicast"; @@ -665,14 +663,15 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName auto backupInfos = BundleMgrAdapter::GetBundleInfosForAppend(bundleDetails, session_->GetSessionUserId()); session_->AppendBundles(bundleNames); for (auto info : backupInfos) { - HILOGI("Current backupInfo bundleName:%{public}s, extName:%{public}s", info.name.c_str(), - info.extensionName.c_str()); - session_->SetBundleDataSize(info.name, info.spaceOccupied); - session_->SetBackupExtName(info.name, info.extensionName); + HILOGI("Current backupInfo bundleName:%{public}s, extName:%{public}s, appIndex:%{public}d", + info.name.c_str(), info.extensionName.c_str(), info.appIndex); + std::string bundleNameIndexInfo = BJsonUtil::BuildBundleNameIndexInfo(info.name, info.appIndex); + session_->SetBundleDataSize(bundleNameIndexInfo, info.spaceOccupied); + session_->SetBackupExtName(bundleNameIndexInfo, info.extensionName); if (info.allToBackup == false) { session_->GetServiceReverseProxy()->BackupOnBundleStarted( - BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), info.name); - session_->RemoveExtInfo(info.name); + BError(BError::Codes::SA_FORBID_BACKUP_RESTORE), bundleNameIndexInfo); + session_->RemoveExtInfo(bundleNameIndexInfo); } } SetCurrentBackupSessProperties(bundleNames, session_->GetSessionUserId()); @@ -1713,77 +1712,6 @@ ErrCode Service::GetBackupInfoCmdHandle(BundleName &bundleName, std::string &res return BError(BError::Codes::OK); } -ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) -{ - try { - HILOGI("Service::GetBackupInfo begin."); - if (session_ == nullptr || isOccupyingSession_.load()) { - HILOGE("Get BackupInfo error, session is empty."); - return BError(BError::Codes::SA_INVAL_ARG); - } - if (session_->GetImpl().clientToken) { - return BError(BError::Codes::SA_REFUSED_ACT, "Already have an active session"); - } - session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - auto ret = GetBackupInfoCmdHandle(bundleName, result); - HILOGI("Service::GetBackupInfo end. result: %s", result.c_str()); - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return ret; - } catch (...) { - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - HILOGI("Unexpected exception"); - return EPERM; - } -} - -ErrCode Service::StartExtTimer(bool &isExtStart) -{ - try { - HILOGI("Service::StartExtTimer begin."); - string bundleName = VerifyCallerAndGetCallerName(); - if (session_ == nullptr) { - HILOGE("StartExtTimer error, session_ is nullptr."); - isExtStart = false; - return BError(BError::Codes::SA_INVAL_ARG); - } - session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); - session_->StopFwkTimer(bundleName); - isExtStart = session_->StartExtTimer(bundleName, timeoutCallback); - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return BError(BError::Codes::OK); - } catch (...) { - isExtStart = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - HILOGI("Unexpected exception"); - return EPERM; - } -} - -ErrCode Service::StartFwkTimer(bool &isFwkStart) -{ - try { - HILOGI("Service::StartFwkTimer begin."); - string bundleName = VerifyCallerAndGetCallerName(); - if (session_ == nullptr) { - HILOGE("StartFwkTimer error, session_ is nullptr."); - isFwkStart = false; - return BError(BError::Codes::SA_INVAL_ARG); - } - session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); - session_->StopExtTimer(bundleName); - isFwkStart = session_->StartFwkTimer(bundleName, timeoutCallback); - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return BError(BError::Codes::OK); - } catch (...) { - isFwkStart = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - HILOGI("Unexpected exception"); - return EPERM; - } -} - ErrCode Service::AppendBundlesClearSession(const std::vector &bundleNames) { HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); @@ -1819,239 +1747,6 @@ ErrCode Service::AppendBundlesClearSession(const std::vector &bundle } } -ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &result) -{ - try { - HILOGI("Service::UpdateTimer begin."); - if (session_ == nullptr || isOccupyingSession_.load()) { - HILOGE("Update Timer error, session is empty."); - result = false; - return BError(BError::Codes::SA_INVAL_ARG); - } - session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - VerifyCaller(); - auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); - result = session_->UpdateTimer(bundleName, timeout, timeoutCallback); - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return BError(BError::Codes::OK); - } catch (...) { - result = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - HILOGI("Unexpected exception"); - return EPERM; - } -} - -ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) -{ - try { - HILOGI("Begin, bundle name:%{public}s, sendRate is:%{public}d", bundleName.c_str(), sendRate); - if (session_ == nullptr || isOccupyingSession_.load()) { - HILOGE("Update Send Rate error, session is empty."); - result = false; - return BError(BError::Codes::SA_INVAL_ARG); - } - session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); - VerifyCaller(); - IServiceReverse::Scenario scenario = session_ -> GetScenario(); - if (scenario != IServiceReverse::Scenario::BACKUP) { - HILOGE("This method is applicable to the backup scenario"); - result = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return BError(BError::Codes::SA_INVAL_ARG); - } - auto backupConnection = session_->GetExtConnection(bundleName); - auto proxy = backupConnection->GetBackupExtProxy(); - if (!proxy) { - throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); - } - auto ret = proxy->UpdateFdSendRate(bundleName, sendRate); - if (ret != NO_ERROR) { - result = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return BError(BError::Codes::EXT_BROKEN_IPC); - } - result = true; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - return BError(BError::Codes::OK); - } catch (...) { - result = false; - session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); - HILOGI("Unexpected exception"); - return EPERM; - } -} - -AAFwk::Want Service::CreateConnectWant (BundleName &bundleName) -{ - BConstants::ExtensionAction action = BConstants::ExtensionAction::BACKUP; - AAFwk::Want want; - string backupExtName = BundleMgrAdapter::GetExtName(bundleName, session_->GetSessionUserId()); - want.SetElementName(bundleName, backupExtName); - want.SetParam(BConstants::EXTENSION_ACTION_PARA, static_cast(action)); - return want; -} - -ErrCode Service::BackupSA(std::string bundleName) -{ - HILOGI("BackupSA begin %{public}s", bundleName.c_str()); - IServiceReverse::Scenario scenario = session_->GetScenario(); - auto backUpConnection = session_->GetSAExtConnection(bundleName); - std::shared_ptr saConnection = backUpConnection.lock(); - if (saConnection == nullptr) { - HILOGE("lock sa connection ptr is nullptr"); - return BError(BError::Codes::SA_INVAL_ARG); - } - if (scenario == IServiceReverse::Scenario::BACKUP) { - auto ret = saConnection->CallBackupSA(); - session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName); - if (ret) { - HILOGI("BackupSA ret is %{public}d", ret); - ClearSessionAndSchedInfo(bundleName); - NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); - return BError(ret); - } - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(BError(BError::Codes::OK), bundleName); - } - return BError(BError::Codes::OK); -} - -void Service::OnSABackup(const std::string &bundleName, const int &fd, const std::string &result, - const ErrCode &errCode) -{ - auto task = [bundleName, fd, result, errCode, this]() { - HILOGI("OnSABackup bundleName: %{public}s, fd: %{public}d, result: %{public}s, err: %{public}d", - bundleName.c_str(), fd, result.c_str(), errCode); - session_->GetServiceReverseProxy()->BackupOnFileReady(bundleName, "", move(fd), errCode); - SAResultReport(bundleName, result, errCode, BackupRestoreScenario::FULL_BACKUP); - }; - threadPool_.AddTask([task]() { - try { - task(); - } catch (...) { - HILOGE("Failed to add task to thread pool"); - } - }); -} - -void Service::OnSARestore(const std::string &bundleName, const std::string &result, const ErrCode &errCode) -{ - auto task = [bundleName, result, errCode, this]() { - HILOGI("OnSARestore bundleName: %{public}s, result: %{public}s, err: %{public}d", - bundleName.c_str(), result.c_str(), errCode); - SAResultReport(bundleName, result, errCode, BackupRestoreScenario::INCREMENTAL_RESTORE); - }; - threadPool_.AddTask([task]() { - try { - task(); - } catch (...) { - HILOGE("Failed to add task to thread pool"); - } - }); -} - -ErrCode Service::SADone(ErrCode errCode, std::string bundleName) -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - try { - if (session_->OnBundleFileReady(bundleName)) { - auto backupConnection = session_->GetSAExtConnection(bundleName); - std::shared_ptr saConnection = backupConnection.lock(); - if (saConnection == nullptr) { - HILOGE("lock sa connection ptr is nullptr"); - return BError(BError::Codes::SA_INVAL_ARG); - } - session_->StopFwkTimer(bundleName); - session_->StopExtTimer(bundleName); - saConnection->DisconnectBackupSAExt(); - ClearSessionAndSchedInfo(bundleName); - } - OnAllBundlesFinished(BError(BError::Codes::OK)); - return BError(BError::Codes::OK); - } catch (const BError &e) { - ReleaseOnException(); - return e.GetCode(); // 任意异常产生,终止监听该任务 - } catch (const exception &e) { - ReleaseOnException(); - HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); - return EPERM; - } catch(...) { - ReleaseOnException(); - HILOGE("Unexpected exception"); - return EPERM; - } -} - -void Service::NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerName) -{ - IServiceReverse::Scenario scenario = session_->GetScenario(); - if (scenario == IServiceReverse::Scenario::BACKUP) { - HILOGI("will notify clone data, scenario is Backup"); - session_->GetServiceReverseProxy()->BackupOnBundleFinished(errCode, callerName); - auto now = std::chrono::system_clock::now(); - auto time = std::chrono::system_clock::to_time_t(now); - auto ms = std::chrono::duration_cast(now.time_since_epoch()); - std::stringstream strTime; - strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0')) - << (std::setw(INDEX)) << (ms.count() % MS_1000); - HiSysEventWrite( - OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, - FILE_BACKUP_EVENTS, - OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, - "PROC_NAME", "ohos.appfileservice", - "BUNDLENAME", callerName, - "PID", getpid(), - "TIME", strTime.str() - ); - } else if (scenario == IServiceReverse::Scenario::RESTORE) { - HILOGI("will notify clone data, scenario is Restore"); - session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, callerName); - } -} - -ErrCode Service::ReportAppProcessInfo(const std::string processInfo, BackupRestoreScenario sennario) -{ - HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); - try { - string bundleName = VerifyCallerAndGetCallerName(); - if (sennario == BackupRestoreScenario::FULL_RESTORE) { - session_->GetServiceReverseProxy()->RestoreOnProcessInfo(bundleName, processInfo); - } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { - session_->GetServiceReverseProxy()->IncrementalRestoreOnProcessInfo(bundleName, processInfo); - } else if (sennario == BackupRestoreScenario::FULL_BACKUP) { - session_->GetServiceReverseProxy()->BackupOnProcessInfo(bundleName, processInfo); - } else if (sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { - session_->GetServiceReverseProxy()->IncrementalBackupOnProcessInfo(bundleName, processInfo); - } - return BError(BError::Codes::OK); - } catch (const BError &e) { - return e.GetCode(); // 任意异常产生,终止监听该任务 - } catch (const exception &e) { - HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); - return EPERM; - } -} - -std::function Service::TimeOutCallback(wptr ptr, std::string bundleName) -{ - return [ptr, bundleName, this]() { - HILOGI("begin timeoutCallback bundleName = %{public}s", bundleName.c_str()); - auto thisPtr = ptr.promote(); - if (!thisPtr) { - HILOGE("ServicePtr is nullptr."); - return; - } - try { - DoTimeout(thisPtr, bundleName); - } catch (...) { - HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); - thisPtr->ClearSessionAndSchedInfo(bundleName); - thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); - } - }; -} - void Service::DoTimeout(wptr ptr, std::string bundleName) { auto thisPtr = ptr.promote(); @@ -2098,40 +1793,4 @@ void Service::DoTimeout(wptr ptr, std::string bundleName) thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); } } - -void Service::AddClearBundleRecord(const std::string &bundleName) -{ - // 添加清理记录 - if (!clearRecorder_->InsertClearBundleRecord(bundleName)) { - HILOGE("Failed to add clear bundle record, bundleName=%{public}s", bundleName.c_str()); - return; - } - HILOGI("Add clear bundle record OK, bundleName=%{public}s", bundleName.c_str()); -} - -void Service::DelClearBundleRecord(const std::vector &bundleNames) -{ - // 删除清理记录 - for (const auto &it : bundleNames) { - if (!clearRecorder_->DeleteClearBundleRecord(it)) { - HILOGE("Failed to delete clear bundle record, bundleName=%{public}s", it.c_str()); - continue; - } - HILOGI("Delete clear bundle record OK, bundleName=%{public}s", it.c_str()); - } -} - -void Service::ReleaseOnException() -{ - try { - if (session_->IsOnAllBundlesFinished()) { - IServiceReverse::Scenario scenario = session_->GetScenario(); - if (isInRelease_.load() && (scenario == IServiceReverse::Scenario::RESTORE)) { - SessionDeactive(); - } - } - } catch (...) { - HILOGE("Unexpected exception"); - } -} } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/sub_service.cpp b/services/backup_sa/src/module_ipc/sub_service.cpp new file mode 100644 index 000000000..c91a141a1 --- /dev/null +++ b/services/backup_sa/src/module_ipc/sub_service.cpp @@ -0,0 +1,420 @@ +/* + * Copyright (c) 2022-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. + */ + +/* + * 注意: + * - 注意点1:本文件原则上只处理与IPC无关的业务逻辑 + * - 注意点2:This document, in principle, captures all exceptions. + * Prevent exceptions from spreading to insecure modules. + */ +#include "module_ipc/service.h" + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "ability_manager_client.h" +#include "accesstoken_kit.h" +#include "b_anony/b_anony.h" +#include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" +#include "b_file_info.h" +#include "b_json/b_json_cached_entity.h" +#include "b_jsonutil/b_jsonutil.h" +#include "b_ohos/startup/backup_para.h" +#include "b_process/b_multiuser.h" +#include "b_radar/b_radar.h" +#include "b_resources/b_constants.h" +#include "b_sa/b_sa_utils.h" +#include "bundle_mgr_client.h" +#include "filemgmt_libhilog.h" +#include "hisysevent.h" +#include "hitrace_meter.h" +#include "ipc_skeleton.h" +#include "access_token.h" +#include "tokenid_kit.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" +#include "module_external/bms_adapter.h" +#include "module_external/sms_adapter.h" +#include "module_ipc/svc_backup_connection.h" +#include "module_ipc/svc_restore_deps_manager.h" +#include "module_notify/notify_work_service.h" +#include "parameter.h" +#include "parameters.h" +#include "system_ability_definition.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +REGISTER_SYSTEM_ABILITY_BY_ID(Service, FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, false); + +namespace { +constexpr int32_t INDEX_THREE = 3; +constexpr int32_t MS_1000 = 1000; +const std::string FILE_BACKUP_RESTORE_EVENTS = "FILE_BACKUP_EVENTS"; +} // namespace + +ErrCode Service::GetBackupInfo(BundleName &bundleName, std::string &result) +{ + try { + HILOGI("Service::GetBackupInfo begin."); + if (session_ == nullptr || isOccupyingSession_.load()) { + HILOGE("Get BackupInfo error, session is empty."); + return BError(BError::Codes::SA_INVAL_ARG); + } + if (session_->GetImpl().clientToken) { + return BError(BError::Codes::SA_REFUSED_ACT, "Already have an active session"); + } + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + auto ret = GetBackupInfoCmdHandle(bundleName, result); + HILOGI("Service::GetBackupInfo end. result: %s", result.c_str()); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return ret; + } catch (...) { + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::StartExtTimer(bool &isExtStart) +{ + try { + HILOGI("Service::StartExtTimer begin."); + string bundleName = VerifyCallerAndGetCallerName(); + if (session_ == nullptr) { + HILOGE("StartExtTimer error, session_ is nullptr."); + isExtStart = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); + session_->StopFwkTimer(bundleName); + isExtStart = session_->StartExtTimer(bundleName, timeoutCallback); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); + } catch (...) { + isExtStart = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::StartFwkTimer(bool &isFwkStart) +{ + try { + HILOGI("Service::StartFwkTimer begin."); + string bundleName = VerifyCallerAndGetCallerName(); + if (session_ == nullptr) { + HILOGE("StartFwkTimer error, session_ is nullptr."); + isFwkStart = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); + session_->StopExtTimer(bundleName); + isFwkStart = session_->StartFwkTimer(bundleName, timeoutCallback); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); + } catch (...) { + isFwkStart = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &result) +{ + try { + HILOGI("Service::UpdateTimer begin."); + if (session_ == nullptr || isOccupyingSession_.load()) { + HILOGE("Update Timer error, session is empty."); + result = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + VerifyCaller(); + auto timeoutCallback = TimeOutCallback(wptr(this), bundleName); + result = session_->UpdateTimer(bundleName, timeout, timeoutCallback); + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); + } catch (...) { + result = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +ErrCode Service::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) +{ + try { + HILOGI("Begin, bundle name:%{public}s, sendRate is:%{public}d", bundleName.c_str(), sendRate); + if (session_ == nullptr || isOccupyingSession_.load()) { + HILOGE("Update Send Rate error, session is empty."); + result = false; + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->IncreaseSessionCnt(__PRETTY_FUNCTION__); + VerifyCaller(); + IServiceReverse::Scenario scenario = session_ -> GetScenario(); + if (scenario != IServiceReverse::Scenario::BACKUP) { + HILOGE("This method is applicable to the backup scenario"); + result = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::SA_INVAL_ARG); + } + auto backupConnection = session_->GetExtConnection(bundleName); + auto proxy = backupConnection->GetBackupExtProxy(); + if (!proxy) { + throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty"); + } + auto ret = proxy->UpdateFdSendRate(bundleName, sendRate); + if (ret != NO_ERROR) { + result = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::EXT_BROKEN_IPC); + } + result = true; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + return BError(BError::Codes::OK); + } catch (...) { + result = false; + session_->DecreaseSessionCnt(__PRETTY_FUNCTION__); + HILOGI("Unexpected exception"); + return EPERM; + } +} + +AAFwk::Want Service::CreateConnectWant (BundleName &bundleName) +{ + BConstants::ExtensionAction action = BConstants::ExtensionAction::BACKUP; + AAFwk::Want want; + string backupExtName = BundleMgrAdapter::GetExtName(bundleName, session_->GetSessionUserId()); + want.SetElementName(bundleName, backupExtName); + want.SetParam(BConstants::EXTENSION_ACTION_PARA, static_cast(action)); + return want; +} + +ErrCode Service::BackupSA(std::string bundleName) +{ + HILOGI("BackupSA begin %{public}s", bundleName.c_str()); + IServiceReverse::Scenario scenario = session_->GetScenario(); + auto backUpConnection = session_->GetSAExtConnection(bundleName); + std::shared_ptr saConnection = backUpConnection.lock(); + if (saConnection == nullptr) { + HILOGE("lock sa connection ptr is nullptr"); + return BError(BError::Codes::SA_INVAL_ARG); + } + if (scenario == IServiceReverse::Scenario::BACKUP) { + auto ret = saConnection->CallBackupSA(); + session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName); + if (ret) { + HILOGI("BackupSA ret is %{public}d", ret); + ClearSessionAndSchedInfo(bundleName); + NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_DIED)); + return BError(ret); + } + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnBundleStarted(BError(BError::Codes::OK), bundleName); + } + return BError(BError::Codes::OK); +} + +void Service::OnSABackup(const std::string &bundleName, const int &fd, const std::string &result, + const ErrCode &errCode) +{ + auto task = [bundleName, fd, result, errCode, this]() { + HILOGI("OnSABackup bundleName: %{public}s, fd: %{public}d, result: %{public}s, err: %{public}d", + bundleName.c_str(), fd, result.c_str(), errCode); + session_->GetServiceReverseProxy()->BackupOnFileReady(bundleName, "", move(fd), errCode); + SAResultReport(bundleName, result, errCode, BackupRestoreScenario::FULL_BACKUP); + }; + threadPool_.AddTask([task]() { + try { + task(); + } catch (...) { + HILOGE("Failed to add task to thread pool"); + } + }); +} + +void Service::OnSARestore(const std::string &bundleName, const std::string &result, const ErrCode &errCode) +{ + auto task = [bundleName, result, errCode, this]() { + HILOGI("OnSARestore bundleName: %{public}s, result: %{public}s, err: %{public}d", + bundleName.c_str(), result.c_str(), errCode); + SAResultReport(bundleName, result, errCode, BackupRestoreScenario::INCREMENTAL_RESTORE); + }; + threadPool_.AddTask([task]() { + try { + task(); + } catch (...) { + HILOGE("Failed to add task to thread pool"); + } + }); +} + +ErrCode Service::SADone(ErrCode errCode, std::string bundleName) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + try { + if (session_->OnBundleFileReady(bundleName)) { + auto backupConnection = session_->GetSAExtConnection(bundleName); + std::shared_ptr saConnection = backupConnection.lock(); + if (saConnection == nullptr) { + HILOGE("lock sa connection ptr is nullptr"); + return BError(BError::Codes::SA_INVAL_ARG); + } + session_->StopFwkTimer(bundleName); + session_->StopExtTimer(bundleName); + saConnection->DisconnectBackupSAExt(); + ClearSessionAndSchedInfo(bundleName); + } + OnAllBundlesFinished(BError(BError::Codes::OK)); + return BError(BError::Codes::OK); + } catch (const BError &e) { + ReleaseOnException(); + return e.GetCode(); // 任意异常产生,终止监听该任务 + } catch (const exception &e) { + ReleaseOnException(); + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch(...) { + ReleaseOnException(); + HILOGE("Unexpected exception"); + return EPERM; + } +} + +void Service::NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerName) +{ + IServiceReverse::Scenario scenario = session_->GetScenario(); + if (scenario == IServiceReverse::Scenario::BACKUP) { + HILOGI("will notify clone data, scenario is Backup"); + session_->GetServiceReverseProxy()->BackupOnBundleFinished(errCode, callerName); + auto now = std::chrono::system_clock::now(); + auto time = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast(now.time_since_epoch()); + std::stringstream strTime; + strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0')) + << (std::setw(INDEX_THREE)) << (ms.count() % MS_1000); + HiSysEventWrite( + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, + FILE_BACKUP_RESTORE_EVENTS, + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "PROC_NAME", "ohos.appfileservice", + "BUNDLENAME", callerName, + "PID", getpid(), + "TIME", strTime.str() + ); + } else if (scenario == IServiceReverse::Scenario::RESTORE) { + HILOGI("will notify clone data, scenario is Restore"); + session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, callerName); + } +} + +ErrCode Service::ReportAppProcessInfo(const std::string processInfo, BackupRestoreScenario sennario) +{ + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + try { + string bundleName = VerifyCallerAndGetCallerName(); + if (sennario == BackupRestoreScenario::FULL_RESTORE) { + session_->GetServiceReverseProxy()->RestoreOnProcessInfo(bundleName, processInfo); + } else if (sennario == BackupRestoreScenario::INCREMENTAL_RESTORE) { + session_->GetServiceReverseProxy()->IncrementalRestoreOnProcessInfo(bundleName, processInfo); + } else if (sennario == BackupRestoreScenario::FULL_BACKUP) { + session_->GetServiceReverseProxy()->BackupOnProcessInfo(bundleName, processInfo); + } else if (sennario == BackupRestoreScenario::INCREMENTAL_BACKUP) { + session_->GetServiceReverseProxy()->IncrementalBackupOnProcessInfo(bundleName, processInfo); + } + return BError(BError::Codes::OK); + } catch (const BError &e) { + return e.GetCode(); // 任意异常产生,终止监听该任务 + } catch (const exception &e) { + HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } +} + +std::function Service::TimeOutCallback(wptr ptr, std::string bundleName) +{ + return [ptr, bundleName, this]() { + HILOGI("begin timeoutCallback bundleName = %{public}s", bundleName.c_str()); + auto thisPtr = ptr.promote(); + if (!thisPtr) { + HILOGE("ServicePtr is nullptr."); + return; + } + try { + DoTimeout(thisPtr, bundleName); + } catch (...) { + HILOGE("Unexpected exception, bundleName: %{public}s", bundleName.c_str()); + thisPtr->ClearSessionAndSchedInfo(bundleName); + thisPtr->NoticeClientFinish(bundleName, BError(BError::Codes::EXT_ABILITY_TIMEOUT)); + } + }; +} + +void Service::AddClearBundleRecord(const std::string &bundleName) +{ + // 添加清理记录 + if (!clearRecorder_->InsertClearBundleRecord(bundleName)) { + HILOGE("Failed to add clear bundle record, bundleName=%{public}s", bundleName.c_str()); + return; + } + HILOGI("Add clear bundle record OK, bundleName=%{public}s", bundleName.c_str()); +} + +void Service::DelClearBundleRecord(const std::vector &bundleNames) +{ + // 删除清理记录 + for (const auto &it : bundleNames) { + if (!clearRecorder_->DeleteClearBundleRecord(it)) { + HILOGE("Failed to delete clear bundle record, bundleName=%{public}s", it.c_str()); + continue; + } + HILOGI("Delete clear bundle record OK, bundleName=%{public}s", it.c_str()); + } +} + +void Service::ReleaseOnException() +{ + try { + if (session_->IsOnAllBundlesFinished()) { + IServiceReverse::Scenario scenario = session_->GetScenario(); + if (isInRelease_.load() && (scenario == IServiceReverse::Scenario::RESTORE)) { + SessionDeactive(); + } + } + } catch (...) { + HILOGE("Unexpected exception"); + } +} +} // namespace OHOS::FileManagement::Backup diff --git a/tests/unittests/backup_sa/module_ipc/BUILD.gn b/tests/unittests/backup_sa/module_ipc/BUILD.gn index 7a6c75f82..a71ade0a8 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -170,6 +170,7 @@ ohos_unittest("backup_service_throw_test") { "${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", + "${path_backup}/services/backup_sa/src/module_ipc/sub_service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", "${path_backup}/services/backup_sa/src/module_notify/notify_work_service.cpp", "service_throw_test.cpp", @@ -354,6 +355,7 @@ ohos_unittest("backup_restore_deps_manager_test") { "${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", + "${path_backup}/services/backup_sa/src/module_ipc/sub_service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", "${path_backup}/services/backup_sa/src/module_notify/notify_work_service.cpp", "svc_restore_deps_manager_test.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 9b8fc031e..497fb97d5 100644 --- a/tests/unittests/backup_sa/module_ipc/service_other_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_other_test.cpp @@ -30,6 +30,7 @@ #include "module_ipc/service.h" #include "service.cpp" +#in namespace OHOS::FileManagement::Backup { using namespace std; diff --git a/tests/unittests/backup_sa/module_ipc/service_test.cpp b/tests/unittests/backup_sa/module_ipc/service_test.cpp index c1d2d468a..a0b52e088 100644 --- a/tests/unittests/backup_sa/module_ipc/service_test.cpp +++ b/tests/unittests/backup_sa/module_ipc/service_test.cpp @@ -22,6 +22,7 @@ #include "module_ipc/service.h" #include "service.cpp" #include "service_reverse_mock.h" +#include "sub_service.cpp" #include "test_manager.h" namespace OHOS::FileManagement::Backup { -- Gitee