From 3d26ce4b8b0f0cd2f98e26467e3c1a3db86a398a Mon Sep 17 00:00:00 2001 From: yang-jingbo1985 Date: Thu, 7 Dec 2023 16:54:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DgetLocalCapabilities=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E9=97=AE=E9=A2=98=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If6e3536ff35db1af4c342c9f4f842cde17019c61 --- .../include/module_ipc/svc_session_manager.h | 15 +++++---- services/backup_sa/src/module_ipc/service.cpp | 31 +++++++++---------- .../src/module_ipc/svc_session_manager.cpp | 17 +++++----- .../module_ipc/svc_session_manager_mock.cpp | 10 ++---- 4 files changed, 34 insertions(+), 39 deletions(-) diff --git a/services/backup_sa/include/module_ipc/svc_session_manager.h b/services/backup_sa/include/module_ipc/svc_session_manager.h index 5050bbc23..0c530e3cc 100644 --- a/services/backup_sa/include/module_ipc/svc_session_manager.h +++ b/services/backup_sa/include/module_ipc/svc_session_manager.h @@ -71,7 +71,6 @@ public: sptr clientProxy; bool isBackupStart {false}; bool isAppendFinish {false}; - bool isBusy {false}; /* Note: Multi user scenario: Caller must complete all processes before next user tigger. [RESTORE] Support for multiple users, incoming during restore process. @@ -393,19 +392,18 @@ public: void BundleExtTimerStop(const std::string &bundleName); /** - * @brief 设置Busy状态 + * @brief sessionCnt加计数 * - * @param isBusy + * @param sessionCnt */ - void SetIsBusy(bool isBusy); + void IncreaseSessionCnt(); /** - * @brief 获取Busy状态 + * @brief sessionCnt加计数 * - * @return true - * @return false + * @param sessionCnt */ - bool GetIsBusy(); + void DecreaseSessionCnt(); private: /** @@ -460,6 +458,7 @@ private: Impl impl_; uint32_t extConnectNum_ {0}; Utils::Timer extBundleTimer {"backupBundleExtTimer"}; + std::atomic sessionCnt_ {0}; }; } // namespace OHOS::FileManagement::Backup diff --git a/services/backup_sa/src/module_ipc/service.cpp b/services/backup_sa/src/module_ipc/service.cpp index 7b1613e0d..4c33eed8d 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -101,7 +101,7 @@ UniqueFd Service::GetLocalCapabilities() Only called by restore app before InitBackupSession, so there must be set init userId. */ - session_->SetIsBusy(true); + session_->IncreaseSessionCnt(); session_->SetSessionUserId(GetUserIdDefault()); VerifyCaller(); string path = BConstants::GetSaBundleBackupRootDir(session_->GetSessionUserId()); @@ -116,19 +116,18 @@ UniqueFd Service::GetLocalCapabilities() auto bundleInfos = BundleMgrAdapter::GetBundleInfos(session_->GetSessionUserId()); cache.SetBundleInfos(bundleInfos); cachedEntity.Persist(); - session_->SetIsBusy(false); - + session_->DecreaseSessionCnt(); return move(cachedEntity.GetFd()); } catch (const BError &e) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGE("GetLocalCapabilities failed, errCode = %{public}d", e.GetCode()); return UniqueFd(-e.GetCode()); } catch (const exception &e) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); return UniqueFd(-EPERM); } catch (...) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); return UniqueFd(-EPERM); } @@ -288,7 +287,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, { try { HILOGI("Begin"); - session_->SetIsBusy(true); + session_->IncreaseSessionCnt(); if (userId != DEFAULT_INVAL_VALUE) { /* multi user scenario */ session_->SetSessionUserId(userId); } @@ -296,7 +295,7 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, auto restoreInfos = GetRestoreBundleNames(move(fd), session_, bundleNames); auto restoreBundleNames = SvcRestoreDepsManager::GetInstance().GetRestoreBundleNames(restoreInfos, restoreType); if (restoreBundleNames.empty()) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); } session_->AppendBundles(restoreBundleNames); @@ -322,13 +321,13 @@ ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd, session_->SetBackupExtName(restoreInfo.name, restoreInfo.extensionName); } OnStartSched(); - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); } catch (const BError &e) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); return e.GetCode(); } catch (...) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); return EPERM; } @@ -338,7 +337,7 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName { try { HILOGI("Begin"); - session_->SetIsBusy(true); // BundleMgrAdapter::GetBundleInfos可能耗时 + session_->IncreaseSessionCnt(); // BundleMgrAdapter::GetBundleInfos可能耗时 VerifyCaller(IServiceReverse::Scenario::BACKUP); auto backupInfos = BundleMgrAdapter::GetBundleInfos(bundleNames, session_->GetSessionUserId()); session_->AppendBundles(bundleNames); @@ -352,18 +351,18 @@ ErrCode Service::AppendBundlesBackupSession(const vector &bundleName } } OnStartSched(); - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); return BError(BError::Codes::OK); } catch (const BError &e) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGE("Failed, errCode = %{public}d", e.GetCode()); return e.GetCode(); } catch (const exception &e) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGI("Catched an unexpected low-level exception %{public}s", e.what()); return EPERM; } catch (...) { - session_->SetIsBusy(false); + session_->DecreaseSessionCnt(); HILOGI("Unexpected exception"); return EPERM; } 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 59d98db8e..ee96d8efe 100644 --- a/services/backup_sa/src/module_ipc/svc_session_manager.cpp +++ b/services/backup_sa/src/module_ipc/svc_session_manager.cpp @@ -502,7 +502,7 @@ bool SvcSessionManager::GetNeedToInstall(const std::string &bundleName) bool SvcSessionManager::NeedToUnloadService() { unique_lock lock(lock_); - bool isNeedToUnloadService = (!impl_.backupExtNameMap.size() && !impl_.isBusy); + bool isNeedToUnloadService = (!impl_.backupExtNameMap.size() && (sessionCnt_.load() <= 0)); if (impl_.scenario == IServiceReverse::Scenario::RESTORE) { bool isAllBundlesRestored = SvcRestoreDepsManager::GetInstance().IsAllBundlesRestored(); isNeedToUnloadService = (isNeedToUnloadService && isAllBundlesRestored); @@ -644,15 +644,18 @@ void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) } } -void SvcSessionManager::SetIsBusy(bool isBusy) +void SvcSessionManager::IncreaseSessionCnt() { - unique_lock lock(lock_); - impl_.isBusy = isBusy; + sessionCnt_++; } -bool SvcSessionManager::GetIsBusy() +void SvcSessionManager::DecreaseSessionCnt() { - shared_lock lock(lock_); - return impl_.isBusy; + unique_lock lock(lock_); + if (sessionCnt_.load() > 0) { + sessionCnt_--; + } else { + HILOGE("Invalid sessionCount."); + } } } // namespace OHOS::FileManagement::Backup diff --git a/tests/mock/module_ipc/svc_session_manager_mock.cpp b/tests/mock/module_ipc/svc_session_manager_mock.cpp index 519646b28..604c4e743 100644 --- a/tests/mock/module_ipc/svc_session_manager_mock.cpp +++ b/tests/mock/module_ipc/svc_session_manager_mock.cpp @@ -322,13 +322,7 @@ void SvcSessionManager::BundleExtTimerStop(const std::string &bundleName) {} void SvcSessionManager::SetBackupExtName(const string &bundleName, const string &backupExtName) {} -void SvcSessionManager::SetIsBusy(bool isBusy) -{ - impl_.isBusy = isBusy; -} +void SvcSessionManager::IncreaseSessionCnt() {} -bool SvcSessionManager::GetIsBusy() -{ - return impl_.isBusy; -} +void SvcSessionManager::DecreaseSessionCnt() {} } // namespace OHOS::FileManagement::Backup -- Gitee