From 14ae88c99e91820ce4a65e268e36c3ecb347ef55 Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Tue, 28 Nov 2023 11:09:09 +0000 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!370=20:?= =?UTF-8?q?=20=E5=8F=8C=E5=8D=87=E5=8D=95=E6=81=A2=E5=A4=8D=E5=9C=BA?= =?UTF-8?q?=E6=99=AF=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=B9=B6=E8=A1=8C=E5=A4=84?= =?UTF-8?q?=E7=90=86'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/b_session_restore_async.cpp | 88 ++++++++++--------- .../impl/b_session_restore_async.h | 10 +-- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp index dc43c86d5..c25212f5d 100644 --- a/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp +++ b/frameworks/native/backup_kit_inner/src/b_session_restore_async.cpp @@ -43,41 +43,6 @@ BSessionRestoreAsync::~BSessionRestoreAsync() deathRecipient_ = nullptr; } -void BSessionRestoreAsync::InitSession(std::vector bundlesToRestore) -{ - std::unique_lock lock(mutex_); - if (isAlive_.load() == true) { - return; - } - ServiceProxy::InvaildInstance(); - auto proxy = ServiceProxy::GetInstance(); - if (proxy == nullptr) { - return OnBundleStarted(BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(), - bundlesToRestore); - } - std::function onBackupServiceDied; - try { - onBackupServiceDied = bind(&BSessionRestoreAsync::OnBackupServiceDied, shared_from_this()); - } catch(const std::bad_function_call &err) { - HILOGE("Failed to Init Restore because of %{public}s", err.what()); - return OnBundleStarted(BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(), - bundlesToRestore); - } - RegisterBackupServiceDied(onBackupServiceDied); - - BSessionRestore::Callbacks callbacksTmp {.onFileReady = callbacks_.onFileReady, - .onBundleStarted = callbacks_.onBundleStarted, - .onBundleFinished = callbacks_.onBundleFinished, - .onAllBundlesFinished = callbacks_.onAllBundlesFinished, - .onBackupServiceDied = onBackupServiceDied}; - int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacksTmp)); - if (res != 0) { - HILOGE("Failed to Init Restore because of %{public}d", res); - return OnBundleStarted(res, bundlesToRestore); - } - isAlive_.store(true); -} - shared_ptr BSessionRestoreAsync::Init(Callbacks callbacks) { try { @@ -113,12 +78,17 @@ ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap, RestoreTypeEnum restoreType, int32_t userId) { - InitSession(bundlesToRestore); - if (isAlive_.load() == true) { - AppendBundlesImpl({move(remoteCap), move(bundlesToRestore), restoreType, userId}); + { + std::unique_lock lock(mutex_); + workList_.push({move(remoteCap), move(bundlesToRestore), restoreType, userId}); + } + + if (isAppend_.exchange(true)) { + return ERR_OK; } else { - return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to int restore session").GetCode(); + PopBundleInfo(); } + return ERR_OK; } @@ -145,18 +115,54 @@ void BSessionRestoreAsync::OnBackupServiceDied() callbacks_.onBackupServiceDied(); } deathRecipient_ = nullptr; - isAlive_.store(false); + ServiceProxy::InvaildInstance(); + PopBundleInfo(); +} + +void BSessionRestoreAsync::PopBundleInfo() +{ + HILOGE("Start"); + AppendBundleInfo info; + isAppend_.store(true); + { + std::unique_lock lock(mutex_); + if (workList_.empty()) { + isAppend_.store(false); + return; + } + info = move(workList_.front()); + workList_.pop(); + } + AppendBundlesImpl(move(info)); } void BSessionRestoreAsync::AppendBundlesImpl(AppendBundleInfo info) { HILOGD("Start"); + ServiceProxy::InvaildInstance(); auto proxy = ServiceProxy::GetInstance(); if (proxy == nullptr) { return OnBundleStarted(BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(), info.bundlesToRestore); } - int32_t res = + auto onBackupServiceDied = bind(&BSessionRestoreAsync::OnBackupServiceDied, shared_from_this()); + RegisterBackupServiceDied(onBackupServiceDied); + + BSessionRestore::Callbacks callbacksTmp {.onFileReady = callbacks_.onFileReady, + .onBundleStarted = callbacks_.onBundleStarted, + .onBundleFinished = callbacks_.onBundleFinished, + .onAllBundlesFinished = callbacks_.onAllBundlesFinished, + .onBackupServiceDied = onBackupServiceDied}; + int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacksTmp)); + if (res != 0) { + HILOGE("Failed to Init Restore because of %{public}d", res); + BError(BError::Codes::SDK_BROKEN_IPC, "Failed to int restore session").GetCode(); + return OnBundleStarted(res, info.bundlesToRestore); + } + for (auto &bundleName : info.bundlesToRestore) { + HILOGD("Append bundleName: %{public}s", bundleName.c_str()); + } + res = proxy->AppendBundlesRestoreSession(move(info.remoteCap), info.bundlesToRestore, info.restoreType, info.userId); if (res != 0) { HILOGE("Failed to Restore because of %{public}d", res); diff --git a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h index fdbc46bad..0d547909e 100644 --- a/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h +++ b/interfaces/inner_api/native/backup_kit_inner/impl/b_session_restore_async.h @@ -119,18 +119,12 @@ private: */ void RegisterBackupServiceDied(std::function functor); - /** - * @brief Backup Session init - * - * @param bundlesToRestore - */ - void InitSession(const std::vector bundlesToRestore); - private: sptr deathRecipient_; Callbacks callbacks_; - std::atomic isAlive_ {false}; + std::atomic isAppend_ {false}; std::mutex mutex_; + std::queue workList_; }; } // namespace OHOS::FileManagement::Backup -- Gitee