From 15539c0a6443391d0be63e8aab34669025af226d Mon Sep 17 00:00:00 2001 From: z30054037 Date: Thu, 16 Nov 2023 06:24:52 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=9C=A8=E6=81=A2=E5=A4=8D=E6=97=B6=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=BA=94=E7=94=A8=E5=B8=82=E5=9C=BA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BC=A0=E9=80=92=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: I34884f146f63168865f5d26a8a87cfe02e488393 --- services/backup_sa/BUILD.gn | 1 + .../app_gallery_dispose_proxy.h | 64 ++++++++++ .../app_gallery_service_connection.h | 84 ++++++++++++ .../module_app_gallery/i_appgallery_service.h | 44 +++++++ .../include/module_external/bms_adapter.h | 5 + .../app_gallery_dispose_proxy.cpp | 120 ++++++++++++++++++ .../src/module_external/bms_adapter.cpp | 14 ++ services/backup_sa/src/module_ipc/service.cpp | 10 ++ .../src/module_ipc/service_reverse_proxy.cpp | 4 + .../mock/module_external/bms_adapter_mock.cpp | 5 + tests/unittests/backup_sa/module_ipc/BUILD.gn | 1 + 11 files changed, 352 insertions(+) create mode 100644 services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h create mode 100644 services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h create mode 100644 services/backup_sa/include/module_app_gallery/i_appgallery_service.h create mode 100644 services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp diff --git a/services/backup_sa/BUILD.gn b/services/backup_sa/BUILD.gn index d589c50d6..cf8de03ca 100644 --- a/services/backup_sa/BUILD.gn +++ b/services/backup_sa/BUILD.gn @@ -16,6 +16,7 @@ import("//foundation/filemanagement/app_file_service/backup.gni") ohos_shared_library("backup_sa") { sources = [ + "src/module_app_gallery/app_gallery_dispose_proxy.cpp", "src/module_external/bms_adapter.cpp", "src/module_external/inner_receiver_impl.cpp", "src/module_external/sms_adapter.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 new file mode 100644 index 000000000..e98a16dbe --- /dev/null +++ b/services/backup_sa/include/module_app_gallery/app_gallery_dispose_proxy.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_H +#define OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_H + +#include +#include + +#include + +#include "i_appgallery_service.h" + +namespace OHOS::FileManagement::Backup { +enum class DisposeErr { + OK = 0, + CONN_FAIL = 1, + IPC_FAIL = 2, + REQUEST_FAIL = 3, +}; + +class AppGalleryDisposeProxy : public IRemoteStub { +public: + DISALLOW_COPY_AND_MOVE(AppGalleryDisposeProxy); + AppGalleryDisposeProxy(); + ~AppGalleryDisposeProxy(); + + 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); + + static std::condition_variable conditionVal_; + static std::string abilityName; + static std::string pkgName; + static std::mutex appRemoteObjLock_; + static std::mutex connectMutex; + static sptr appRemoteObj_; + +private: + DisposeErr DoDispose(const std::string &bundleName, DisposeOperation disposeOperation); + +private: + static std::mutex instanceLock_; + static std::mutex conditionMutex_; + static sptr appGalleryDisposeProxyInstance_; +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_APP_GALLERY_DISPOSE_PROXY_H \ No newline at end of file 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 new file mode 100644 index 000000000..d653a8e53 --- /dev/null +++ b/services/backup_sa/include/module_app_gallery/app_gallery_service_connection.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H +#define OHOS_FILEMGMT_BACKUP_APP_GALLERY_SERVICE_CONNECTION_H + +#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 = 5; + +template class AppGralleryConnection : 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(); + } + + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) + { + std::string uri = element.GetURI(); + HILOGI("OnAbilityDisconnectDone, uri = %{public}s", uri.c_str()); + T::appRemoteObj_ = nullptr; + } +}; + +template bool ConnectExtAbility() +{ + HILOGI("ConnectExtAbility called"); + std::lock_guard autoLock(T::appRemoteObjLock_); + if (T::appRemoteObj_ != nullptr) { + return true; + } + + auto appGalleryBundleName = BundleMgrAdapter::GetAppGalleryBundleName(); + if (!appGalleryBundleName.c_str()) { + HILOGI("ConnectExtAbility GetAppGalleryBundleName failed"); + return false; + } + + Want want; + want.SetElementName(appGalleryBundleName.c_str(), T::abilityName); + sptr connect = new AppGralleryConnection(); + auto ret = AbilityManagerClient::GetInstance()->ConnectAbility(want, connect, -1); + + 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; +}; +} // 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_app_gallery/i_appgallery_service.h b/services/backup_sa/include/module_app_gallery/i_appgallery_service.h new file mode 100644 index 000000000..16a065cba --- /dev/null +++ b/services/backup_sa/include/module_app_gallery/i_appgallery_service.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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. + */ + +#ifndef OHOS_FILEMGMT_BACKUP_I_APP_GALLERY_SERVICE_H +#define OHOS_FILEMGMT_BACKUP_I_APP_GALLERY_SERVICE_H + +#include + +#include "iremote_proxy.h" + +namespace OHOS::FileManagement::Backup { +class IAppGalleryService : public OHOS::IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.FileManagement.Backup.IAppGalleryService"); + + enum class DisposeOperation { + START_RESTORE = 3, + END_RESTORE = 4, + START_BACKUP = 5, + END_BACKUP = 6, + }; + + enum Errcode { + ERR_BASE = (-99), + ERR_FAILED = (-1), + ERR_PERMISSION_DENIED = (-2), + ERR_OK = 0, + }; +}; +} // namespace OHOS::FileManagement::Backup + +#endif // OHOS_FILEMGMT_BACKUP_I_APP_GALLERY_SERVICE_H \ No newline at end of file diff --git a/services/backup_sa/include/module_external/bms_adapter.h b/services/backup_sa/include/module_external/bms_adapter.h index a531fccbd..e96aab688 100644 --- a/services/backup_sa/include/module_external/bms_adapter.h +++ b/services/backup_sa/include/module_external/bms_adapter.h @@ -53,6 +53,11 @@ public: * @param userId User ID */ static ErrCode Install(wptr statusReceiver, const std::string &bundleFilePath, int32_t userId); + + /** + * @brief Get app gallery bundle name + */ + static std::string GetAppGalleryBundleName(); }; } // namespace OHOS::FileManagement::Backup #endif // OHOS_FILEMGMT_BACKUP_BUNDLE_MGR_ADAPTER_H 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 new file mode 100644 index 000000000..f599cf521 --- /dev/null +++ b/services/backup_sa/src/module_app_gallery/app_gallery_dispose_proxy.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2023 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_dispose_proxy.h" + +#include + +#include "message_parcel.h" +#include "nlohmann/json.hpp" +#include "want.h" + +#include "module_app_gallery/app_gallery_service_connection.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup { +using namespace std; + +mutex AppGalleryDisposeProxy::instanceLock_; +mutex AppGalleryDisposeProxy::conditionMutex_; + +string AppGalleryDisposeProxy::abilityName = "AppFoundationServiceExtensionAbility"; +sptr AppGalleryDisposeProxy::appRemoteObj_; +condition_variable AppGalleryDisposeProxy::conditionVal_; +mutex AppGalleryDisposeProxy::appRemoteObjLock_; +mutex AppGalleryDisposeProxy::connectMutex; +sptr AppGalleryDisposeProxy::appGalleryDisposeProxyInstance_; + +AppGalleryDisposeProxy::AppGalleryDisposeProxy() +{ + HILOGI("AppGalleryDisposeProxy construct"); +} + +AppGalleryDisposeProxy::~AppGalleryDisposeProxy() +{ + if (appGalleryDisposeProxyInstance_ != nullptr) { + delete appGalleryDisposeProxyInstance_; + appGalleryDisposeProxyInstance_ = nullptr; + } +} + +sptr AppGalleryDisposeProxy::GetInstance() +{ + if (appGalleryDisposeProxyInstance_ == nullptr) { + lock_guard autoLock(instanceLock_); + if (appGalleryDisposeProxyInstance_ == nullptr) { + appGalleryDisposeProxyInstance_ = new AppGalleryDisposeProxy; + } + } + + return appGalleryDisposeProxyInstance_; +} + +DisposeErr AppGalleryDisposeProxy::StartBackup(const std::string &bundleName) +{ + HILOGI("StartBackup, app %{public}s", bundleName.c_str()); + return DoDispose(bundleName, DisposeOperation::START_BACKUP); +} + +DisposeErr AppGalleryDisposeProxy::EndBackup(const std::string &bundleName) +{ + HILOGI("EndBackup, app %{public}s", bundleName.c_str()); + return DoDispose(bundleName, DisposeOperation::END_BACKUP); +} + +DisposeErr AppGalleryDisposeProxy::StartRestore(const std::string &bundleName) +{ + HILOGI("StartRestore, app %{public}s", bundleName.c_str()); + return DoDispose(bundleName, DisposeOperation::START_RESTORE); +} + +DisposeErr AppGalleryDisposeProxy::EndRestore(const std::string &bundleName) +{ + HILOGI("EndRestore, app %{public}s", bundleName.c_str()); + return DoDispose(bundleName, DisposeOperation::END_RESTORE); +} + +DisposeErr AppGalleryDisposeProxy::DoDispose(const std::string &bundleName, DisposeOperation disposeOperation) +{ + HILOGI("DoDispose, app %{public}s, operation %{public}d", bundleName.c_str(), disposeOperation); + if (!ConnectExtAbility() || appRemoteObj_ == nullptr) { + HILOGI("Can not connect to %{public}s", bundleName.c_str()); + return DisposeErr::CONN_FAIL; + } + + MessageParcel data; + if (!data.WriteString16(Str8ToStr16(bundleName))) { + HILOGI("write ownerInfo and bundleName failed"); + return DisposeErr::IPC_FAIL; + } + + if (!data.WriteRemoteObject(this)) { + HILOGI("write RemoteObject failed"); + return DisposeErr::IPC_FAIL; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + auto ret = appRemoteObj_->SendRequest(static_cast(disposeOperation), data, reply, option); + if (ret != ERR_NONE) { + HILOGI("SendRequest error, code=%{public}d, bundleName=%{public}s", disposeOperation, bundleName.c_str()); + return DisposeErr::REQUEST_FAIL; + } + + HILOGI("SendRequest error, code=%{public}d, bundleName=%{public}s", disposeOperation, bundleName.c_str()); + return DisposeErr::OK; +} + +} // namespace OHOS::FileManagement::Backup \ No newline at end of file diff --git a/services/backup_sa/src/module_external/bms_adapter.cpp b/services/backup_sa/src/module_external/bms_adapter.cpp index 1e4da6bd4..2e2a25e94 100644 --- a/services/backup_sa/src/module_external/bms_adapter.cpp +++ b/services/backup_sa/src/module_external/bms_adapter.cpp @@ -177,4 +177,18 @@ ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const installParam.userId = userId; return install->StreamInstall({bundleFilePath}, installParam, receiver); } + +string BundleMgrAdapter::GetAppGalleryBundleName() +{ + auto bms = GetBundleManager(); + + string bundleName = ""; + auto ret = bms->QueryAppGalleryBundleName(bundleName); + if (!ret) { + HILOGI("Get App Gallery BundleName fail!"); + } else { + HILOGI("App Gallery BundleName: %{public}s", bundleName.c_str()); + } + return bundleName; +} } // 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 39b063594..b3edb6e5e 100644 --- a/services/backup_sa/src/module_ipc/service.cpp +++ b/services/backup_sa/src/module_ipc/service.cpp @@ -48,6 +48,7 @@ #include "bundle_mgr_client.h" #include "filemgmt_libhilog.h" #include "ipc_skeleton.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" #include "module_external/bms_adapter.h" #include "module_ipc/svc_backup_connection.h" #include "module_ipc/svc_restore_deps_manager.h" @@ -455,6 +456,11 @@ ErrCode Service::LaunchBackupExtension(const BundleName &bundleName) { try { HILOGE("begin %{public}s", bundleName.data()); + + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->StartRestore(bundleName); + HILOGI("AppGalleryDisposeProxy StartRestore, code=%{public}d, bundleName=%{public}s", disposeErr, + bundleName.c_str()); + IServiceReverse::Scenario scenario = session_->GetScenario(); BConstants::ExtensionAction action; if (scenario == IServiceReverse::Scenario::BACKUP) { @@ -626,6 +632,10 @@ void Service::ExtConnectFailed(const string &bundleName, ErrCode ret) session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName); } else if (scenario == IServiceReverse::Scenario::RESTORE) { session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName); + + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + HILOGI("AppGalleryDisposeProxy EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, + bundleName.c_str()); } ClearSessionAndSchedInfo(bundleName); return; diff --git a/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp b/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp index 6dd0e4100..d1846c8eb 100644 --- a/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp +++ b/services/backup_sa/src/module_ipc/service_reverse_proxy.cpp @@ -15,6 +15,7 @@ #include "module_ipc/service_reverse_proxy.h" +#include "module_app_gallery/app_gallery_dispose_proxy.h" #include "b_error/b_error.h" #include "b_error/b_excep_utils.h" #include "filemgmt_libhilog.h" @@ -133,6 +134,9 @@ void ServiceReverseProxy::RestoreOnBundleFinished(int32_t errCode, string bundle err != ERR_OK) { throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err)); } + + DisposeErr disposeErr = AppGalleryDisposeProxy::GetInstance()->EndRestore(bundleName); + HILOGI("AppGalleryDisposeProxy EndRestore, code=%{public}d, bundleName=%{public}s", disposeErr, bundleName.c_str()); } void ServiceReverseProxy::RestoreOnAllBundlesFinished(int32_t errCode) diff --git a/tests/mock/module_external/bms_adapter_mock.cpp b/tests/mock/module_external/bms_adapter_mock.cpp index d6f295373..60b632d10 100644 --- a/tests/mock/module_external/bms_adapter_mock.cpp +++ b/tests/mock/module_external/bms_adapter_mock.cpp @@ -49,4 +49,9 @@ ErrCode BundleMgrAdapter::Install(wptr statusReceiver, const { return 0; } + +string BundleMgrAdapter::GetAppGalleryBundleName() +{ + return ""; +} } // 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 a03c96a48..a7d14a1b4 100644 --- a/tests/unittests/backup_sa/module_ipc/BUILD.gn +++ b/tests/unittests/backup_sa/module_ipc/BUILD.gn @@ -58,6 +58,7 @@ ohos_unittest("backup_service_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_ipc/service.cpp", "${path_backup}/services/backup_sa/src/module_ipc/svc_restore_deps_manager.cpp", "service_test.cpp", -- Gitee From 00cad1fe5050a2a920b4a51a85a6e2d858ab9770 Mon Sep 17 00:00:00 2001 From: z30054037 Date: Sat, 18 Nov 2023 10:47:24 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=9C=A8=E6=81=A2=E5=A4=8D=E6=97=B6=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=BA=94=E7=94=A8=E5=B8=82=E5=9C=BA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BC=A0=E9=80=92=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: z30054037 Change-Id: I05d2ea2fc4f5c7d21d7a56cae63d14d0ee63c78e --- .../src/module_app_gallery/app_gallery_dispose_proxy.cpp | 1 - 1 file changed, 1 deletion(-) 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 f599cf521..0bf9ad703 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 @@ -18,7 +18,6 @@ #include #include "message_parcel.h" -#include "nlohmann/json.hpp" #include "want.h" #include "module_app_gallery/app_gallery_service_connection.h" -- Gitee