From 05097dd6e5558f83995ce90a8544f0e2d3a6ee4b Mon Sep 17 00:00:00 2001 From: swx1134754 Date: Tue, 14 Mar 2023 18:52:04 +0800 Subject: [PATCH 1/2] add GetValidAbilityInfo Signed-off-by: swx1134754 --- .../native/backup_ext/src/ext_backup_js.cpp | 20 +++--- .../backup_ext/src/ext_extension_stub.cpp | 4 +- utils/include/b_error/b_error.h | 8 --- utils/include/b_error/b_excep_utils.h | 63 +++++++++++++++++++ utils/src/b_error/b_error.cpp | 15 ----- 5 files changed, 76 insertions(+), 34 deletions(-) create mode 100644 utils/include/b_error/b_excep_utils.h diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 2e946b33e..3ce5a7fd9 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -21,6 +21,7 @@ #include #include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" #include "b_json/b_json_cached_entity.h" #include "b_json/b_json_entity_extension_config.h" #include "b_resources/b_constants.h" @@ -60,11 +61,8 @@ void ExtBackupJs::Init(const shared_ptr &record, HILOGI("Init the BackupExtensionAbility(JS)"); try { ExtBackup::Init(record, application, handler, token); - - if (!abilityInfo_) { - throw BError(BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); - } - + BExcepUltils::CheckValidAbilityInfo>( + abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); // 获取应用扩展的 BackupExtensionAbility 的路径 const AppExecFwk::AbilityInfo &info = *abilityInfo_; string bundleName = info.bundleName; @@ -139,6 +137,8 @@ string ExtBackupJs::GetUsrConfig() const { vector config; AppExecFwk::BundleMgrClient client; + BExcepUltils::CheckValidAbilityInfo>( + abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); const AppExecFwk::AbilityInfo &info = *abilityInfo_; if (!client.GetProfileFromAbility(info, "ohos.extension.backup", config)) { throw BError(BError::Codes::EXT_INVAL_ARG, "Failed to invoke the GetProfileFromAbility method."); @@ -166,11 +166,9 @@ BConstants::ExtensionAction ExtBackupJs::GetExtensionAction() const static BConstants::ExtensionAction VerifyAndGetAction(const AAFwk::Want &want, std::shared_ptr abilityInfo) { - if (!abilityInfo) { - string pendingMsg = "Received an empty ability. You must missed the init proc"; - throw BError(BError::Codes::EXT_INVAL_ARG, pendingMsg); - } - + string pendingMsg = "Received an empty ability. You must missed the init proc"; + BExcepUltils::CheckValidAbilityInfo>( + abilityInfo, BError::Codes::EXT_INVAL_ARG, pendingMsg); using namespace BConstants; ExtensionAction extAction {want.GetIntParam(EXTENSION_ACTION_PARA, static_cast(ExtensionAction::INVALID))}; if (extAction == ExtensionAction::INVALID) { @@ -186,6 +184,8 @@ sptr ExtBackupJs::OnConnect(const AAFwk::Want &want) { try { HILOGI("begin"); + BExcepUltils::CheckValidAbilityInfo>( + abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); // 发起者必须是备份服务 auto extAction = VerifyAndGetAction(want, abilityInfo_); if (extAction_ != BConstants::ExtensionAction::INVALID && extAction == BConstants::ExtensionAction::INVALID && diff --git a/frameworks/native/backup_ext/src/ext_extension_stub.cpp b/frameworks/native/backup_ext/src/ext_extension_stub.cpp index b47515b07..02059a4a8 100644 --- a/frameworks/native/backup_ext/src/ext_extension_stub.cpp +++ b/frameworks/native/backup_ext/src/ext_extension_stub.cpp @@ -19,6 +19,7 @@ #include #include "b_error/b_error.h" +#include "b_error/b_excep_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::Backup { @@ -50,7 +51,8 @@ int32_t ExtExtensionStub::OnRemoteRequest(uint32_t code, return BError(BError::Codes::EXT_INVAL_ARG, "Invalid remote descriptor"); } - return BError::ExceptionCatcherLocked([&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); + return BExcepUltils::ExceptionCatcherLocked( + [&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); } ErrCode ExtExtensionStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply) diff --git a/utils/include/b_error/b_error.h b/utils/include/b_error/b_error.h index e12434f65..058361d2b 100644 --- a/utils/include/b_error/b_error.h +++ b/utils/include/b_error/b_error.h @@ -114,14 +114,6 @@ public: return msg_.c_str(); } - /** - * @brief 异常捕获 - * - * @param callBack 回调 - * @return ErrCode 错误码 - */ - static ErrCode ExceptionCatcherLocked(std::function callBack); - public: /** * @brief 重载bool操作符,判断当前错误是否是错误 diff --git a/utils/include/b_error/b_excep_utils.h b/utils/include/b_error/b_excep_utils.h new file mode 100644 index 000000000..8dd6226d0 --- /dev/null +++ b/utils/include/b_error/b_excep_utils.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022-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_B_EXCEP_UTILES_H +#define OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H + +#include "b_error/b_error.h" +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement::Backup { +class BExcepUltils : public std::exception { +public: + /** + * @brief 异常捕获 + * + * @param callBack 回调 + * @return ErrCode 错误码 + */ + static ErrCode ExceptionCatcherLocked(std::function callBack) + { + try { + return callBack(); + } catch (const BError &e) { + return e.GetCode(); + } catch (const exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGE("Unexpected exception"); + return EPERM; + } + } + + /** + * @brief 检查 AbilityInfo 是否有效 + * + * @param AbilityInfo + * @param code 错误码 + * @param msg 错误信息 + * @return 无 + */ + template + static void CheckValidAbilityInfo(const T &t, const BError::Codes &code, const std::string_view msg = "") + { + if (!t) { + throw BError(code, msg); + } + } +}; +} // namespace OHOS::FileManagement::Backup +#endif // OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H \ No newline at end of file diff --git a/utils/src/b_error/b_error.cpp b/utils/src/b_error/b_error.cpp index f9745d5d8..e6e108153 100644 --- a/utils/src/b_error/b_error.cpp +++ b/utils/src/b_error/b_error.cpp @@ -49,19 +49,4 @@ string BError::WrapMessageWithExtraInfos(const char *fileName, HiviewDFX::HiLog::Error(FILEMGMT_LOG_LABEL, "%{public}s", res.c_str()); return res; } - -ErrCode BError::ExceptionCatcherLocked(std::function callBack) -{ - try { - return callBack(); - } catch (const BError &e) { - return e.GetCode(); - } catch (const exception &e) { - HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); - return EPERM; - } catch (...) { - HILOGE("Unexpected exception"); - return EPERM; - } -} } // namespace OHOS::FileManagement::Backup \ No newline at end of file -- Gitee From 3bd06b0ba32c04c5b305ed597fdd54a6d3345869 Mon Sep 17 00:00:00 2001 From: swx1134754 Date: Wed, 15 Mar 2023 12:44:28 +0000 Subject: [PATCH 2/2] update frameworks/native/backup_ext/src/ext_backup_js.cpp. Signed-off-by: swx1134754 --- .../native/backup_ext/src/ext_backup_js.cpp | 15 ++-- .../backup_ext/src/ext_extension_stub.cpp | 5 +- utils/include/b_error/b_excep_utils.h | 78 ++++++++++--------- 3 files changed, 50 insertions(+), 48 deletions(-) diff --git a/frameworks/native/backup_ext/src/ext_backup_js.cpp b/frameworks/native/backup_ext/src/ext_backup_js.cpp index 3ce5a7fd9..5c03fa628 100644 --- a/frameworks/native/backup_ext/src/ext_backup_js.cpp +++ b/frameworks/native/backup_ext/src/ext_backup_js.cpp @@ -33,6 +33,7 @@ namespace OHOS::FileManagement::Backup { using namespace std; +using namespace BExcepUltils; void ExtBackupJs::OnStart(const AAFwk::Want &want) { @@ -61,8 +62,7 @@ void ExtBackupJs::Init(const shared_ptr &record, HILOGI("Init the BackupExtensionAbility(JS)"); try { ExtBackup::Init(record, application, handler, token); - BExcepUltils::CheckValidAbilityInfo>( - abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); + BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); // 获取应用扩展的 BackupExtensionAbility 的路径 const AppExecFwk::AbilityInfo &info = *abilityInfo_; string bundleName = info.bundleName; @@ -137,8 +137,7 @@ string ExtBackupJs::GetUsrConfig() const { vector config; AppExecFwk::BundleMgrClient client; - BExcepUltils::CheckValidAbilityInfo>( - abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); + BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); const AppExecFwk::AbilityInfo &info = *abilityInfo_; if (!client.GetProfileFromAbility(info, "ohos.extension.backup", config)) { throw BError(BError::Codes::EXT_INVAL_ARG, "Failed to invoke the GetProfileFromAbility method."); @@ -167,13 +166,12 @@ static BConstants::ExtensionAction VerifyAndGetAction(const AAFwk::Want &want, std::shared_ptr abilityInfo) { string pendingMsg = "Received an empty ability. You must missed the init proc"; - BExcepUltils::CheckValidAbilityInfo>( - abilityInfo, BError::Codes::EXT_INVAL_ARG, pendingMsg); + BAssert(abilityInfo, BError::Codes::EXT_INVAL_ARG, pendingMsg); using namespace BConstants; ExtensionAction extAction {want.GetIntParam(EXTENSION_ACTION_PARA, static_cast(ExtensionAction::INVALID))}; if (extAction == ExtensionAction::INVALID) { int extActionInt = static_cast(extAction); - string pendingMsg = string("Want must specify a valid action instead of ").append(to_string(extActionInt)); + pendingMsg = string("Want must specify a valid action instead of ").append(to_string(extActionInt)); throw BError(BError::Codes::EXT_INVAL_ARG, pendingMsg); } @@ -184,8 +182,7 @@ sptr ExtBackupJs::OnConnect(const AAFwk::Want &want) { try { HILOGI("begin"); - BExcepUltils::CheckValidAbilityInfo>( - abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); + BAssert(abilityInfo_, BError::Codes::EXT_BROKEN_FRAMEWORK, "Invalid abilityInfo_"); // 发起者必须是备份服务 auto extAction = VerifyAndGetAction(want, abilityInfo_); if (extAction_ != BConstants::ExtensionAction::INVALID && extAction == BConstants::ExtensionAction::INVALID && diff --git a/frameworks/native/backup_ext/src/ext_extension_stub.cpp b/frameworks/native/backup_ext/src/ext_extension_stub.cpp index 02059a4a8..f78037b07 100644 --- a/frameworks/native/backup_ext/src/ext_extension_stub.cpp +++ b/frameworks/native/backup_ext/src/ext_extension_stub.cpp @@ -24,6 +24,8 @@ namespace OHOS::FileManagement::Backup { using namespace std; +using namespace BExcepUltils; + ExtExtensionStub::ExtExtensionStub() { opToInterfaceMap_[CMD_GET_FILE_HANDLE] = &ExtExtensionStub::CmdGetFileHandle; @@ -51,8 +53,7 @@ int32_t ExtExtensionStub::OnRemoteRequest(uint32_t code, return BError(BError::Codes::EXT_INVAL_ARG, "Invalid remote descriptor"); } - return BExcepUltils::ExceptionCatcherLocked( - [&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); + return ExceptionCatcherLocked([&]() { return ErrCode((this->*(interfaceIndex->second))(data, reply)); }); } ErrCode ExtExtensionStub::CmdGetFileHandle(MessageParcel &data, MessageParcel &reply) diff --git a/utils/include/b_error/b_excep_utils.h b/utils/include/b_error/b_excep_utils.h index 8dd6226d0..57e1baaa6 100644 --- a/utils/include/b_error/b_excep_utils.h +++ b/utils/include/b_error/b_excep_utils.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * 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 @@ -17,47 +17,51 @@ #define OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H #include "b_error/b_error.h" + +#include + #include "filemgmt_libhilog.h" -namespace OHOS::FileManagement::Backup { -class BExcepUltils : public std::exception { -public: - /** - * @brief 异常捕获 - * - * @param callBack 回调 - * @return ErrCode 错误码 - */ - static ErrCode ExceptionCatcherLocked(std::function callBack) - { - try { - return callBack(); - } catch (const BError &e) { - return e.GetCode(); - } catch (const exception &e) { - HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); - return EPERM; - } catch (...) { - HILOGE("Unexpected exception"); - return EPERM; - } +namespace OHOS::FileManagement::Backup::BExcepUltils { +/** + * @brief 异常捕获 + * + * @param callBack 回调 + * @return ErrCode 错误码 + */ +[[maybe_unused]] static ErrCode ExceptionCatcherLocked(std::function callBack) +{ + try { + return callBack(); + } catch (const BError &e) { + return e.GetCode(); + } catch (const std::exception &e) { + HILOGE("Catched an unexpected low-level exception %{public}s", e.what()); + return EPERM; + } catch (...) { + HILOGE("Unexpected exception"); + return EPERM; } +} - /** - * @brief 检查 AbilityInfo 是否有效 - * - * @param AbilityInfo - * @param code 错误码 - * @param msg 错误信息 - * @return 无 - */ - template - static void CheckValidAbilityInfo(const T &t, const BError::Codes &code, const std::string_view msg = "") - { - if (!t) { +/** + * @brief 检查 AbilityInfo 是否有效 + * + * @param AbilityInfo + * @param code 错误码 + * @param msg 错误信息 + * @return 无 + */ +template +[[maybe_unused]] static void BAssert(const T &t, const BError::Codes &code, const std::string_view msg = "") +{ + if (!t) { + if (msg.empty()) { + throw BError(code); + } else { throw BError(code, msg); } } -}; -} // namespace OHOS::FileManagement::Backup +} +} // namespace OHOS::FileManagement::Backup::BExcepUltils #endif // OHOS_FILEMGMT_BACKUP_B_EXCEP_UTILES_H \ No newline at end of file -- Gitee