From 242b6035470198be70b98f6f94042c3cb0674b8d Mon Sep 17 00:00:00 2001 From: zhangjiameng Date: Thu, 27 Jan 2022 10:16:13 +0800 Subject: [PATCH] coauth add broadcast 1.add broadcast 2.add timeout process 3.add attribute type 4.delete schedule callback Signed-off-by: jiameng.zhang --- .../innerkits/include/coauth_info_define.h | 6 ++- interfaces/innerkits/src/auth_attributes.cpp | 4 +- services/BUILD.gn | 4 ++ services/include/call_monitor.h | 44 ++++++++++++++++++ services/include/coauth_manager.h | 8 +++- services/include/coauth_service.h | 2 + services/src/auth_res_pool.cpp | 2 +- services/src/call_monitor.cpp | 36 +++++++++++++++ services/src/coauth_manager.cpp | 45 ++++++++++++++++--- services/src/coauth_service.cpp | 13 ++++++ 10 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 services/include/call_monitor.h create mode 100644 services/src/call_monitor.cpp diff --git a/interfaces/innerkits/include/coauth_info_define.h b/interfaces/innerkits/include/coauth_info_define.h index d16b83c..2e54b52 100644 --- a/interfaces/innerkits/include/coauth_info_define.h +++ b/interfaces/innerkits/include/coauth_info_define.h @@ -65,7 +65,9 @@ enum AuthAttributeType { /* calleruid */ AUTH_CALLER_UID = 100027, /* result */ - AUTH_RESULT = 100028 + AUTH_RESULT = 100028, + /* algorithm setinfo */ + ALGORITHM_INFO }; enum AuthType { @@ -159,6 +161,8 @@ enum ResultCode { }; const uint64_t INVALID_EXECUTOR_ID = 0; +const std::string REGISTER_NOTIFICATION = "EXECUTOR_REGISTER_NOTIFICATION"; + } // namespace UserIAM } // namespace OHOS #endif // COAUTH_INFO_DEFINE_H diff --git a/interfaces/innerkits/src/auth_attributes.cpp b/interfaces/innerkits/src/auth_attributes.cpp index 2d47f9a..d5815ca 100644 --- a/interfaces/innerkits/src/auth_attributes.cpp +++ b/interfaces/innerkits/src/auth_attributes.cpp @@ -40,6 +40,7 @@ AuthAttributes::AuthAttributes() InsertMap(AUTH_CONTROLLER, BOOLTYPE); InsertMap(AUTH_CALLER_UID, UINT64TYPE); InsertMap(AUTH_RESULT, UINT8ARRAYTYPE); + InsertMap(ALGORITHM_INFO, UINT8ARRAYTYPE); } void AuthAttributes::clear() @@ -319,7 +320,7 @@ int32_t AuthAttributes::Pack(std::vector &buffer) tag = authAttributesPosition_.find(existAttributes_[i])->first; writePointer = static_cast(static_cast(&tag)); buffer.insert(buffer.end(), writePointer, writePointer + sizeof(AuthAttributeType)); - COAUTH_HILOGE(MODULE_INNERKIT, "data Write tag : %{public}d.", tag); + COAUTH_HILOGD(MODULE_INNERKIT, "data Write tag : %{public}d.", tag); PackToBuffer(authAttributesPosition_.find(existAttributes_[i]), dataLength, writePointer, buffer); } @@ -385,7 +386,6 @@ void AuthAttributes::PackToBuffer(std::map::itera WriteDataLength(buffer, writePointer, sizeof(uint32_t)); writePointer = static_cast(static_cast(&uint32Value)); buffer.insert(buffer.end(), writePointer, writePointer + sizeof(uint32_t)); - COAUTH_HILOGE(MODULE_INNERKIT, "uint32Value : %{public}d.", uint32Value); break; case UINT64TYPE: GetUint64Value(iter->first, uint64Value); diff --git a/services/BUILD.gn b/services/BUILD.gn index 644db9d..0e8a486 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -35,6 +35,7 @@ ohos_shared_library("coauthservice") { "src/coauth_stub.cpp", "src/coauth_thread_pool.cpp", "src/executor_messenger.cpp", + "src/call_monitor.cpp", ] configs = [ @@ -49,6 +50,7 @@ ohos_shared_library("coauthservice") { "//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara", "//base/user_iam/auth_executor_mgr/common:useriam_common_lib", "//drivers/peripheral/display/hal:hdi_display_device", + "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler:libeventhandler", "//utils/native/base:utils", ] @@ -57,6 +59,8 @@ ohos_shared_library("coauthservice") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "ability_runtime:want", + "ces_standard:cesfwk_innerkits", ] part_name = "${coauth_native_part_name}" diff --git a/services/include/call_monitor.h b/services/include/call_monitor.h new file mode 100644 index 0000000..3c7b4b5 --- /dev/null +++ b/services/include/call_monitor.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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 CALL_MONITOR_H +#define CALL_MONITOR_H + +#include + +#include "event_handler.h" + +namespace OHOS { +namespace UserIAM { +namespace CoAuth { +using Callback = OHOS::AppExecFwk::EventHandler::Callback; + +class CallMonitor { +public: + CallMonitor(); + + ~CallMonitor() = default; + + void MonitorCall(int64_t waitTime, Callback &timeoutFun); + +private: + Callback timeoutFun_; + std::shared_ptr eventHandler_; +}; +} // namespace CoAuth +} // namespace UserIAM +} // namespace OHOS + +#endif // CALL_MONITOR_H \ No newline at end of file diff --git a/services/include/coauth_manager.h b/services/include/coauth_manager.h index bb7811f..b5aecdb 100644 --- a/services/include/coauth_manager.h +++ b/services/include/coauth_manager.h @@ -19,12 +19,14 @@ #include "auth_res_pool.h" #include "auth_executor.h" #include "iexecutor_callback.h" +#include "call_monitor.h" #include "iquery_callback.h" #include "auth_res_manager.h" namespace OHOS { namespace UserIAM { namespace CoAuth { +const int64_t delay_time = 300 * 1000; class CoAuthManager { public: void coAuth(uint64_t scheduleId, AuthInfo &authInfo, sptr callback); @@ -34,9 +36,12 @@ public: void RegistResourceManager(AuthResManager* resMgr); void CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptr callback); + void TimeOut(uint64_t scheduleId); private: void SetAuthAttributes(std::shared_ptr commandAttrs, - ScheduleInfo &scheduleInfo, AuthInfo &authInfo); + ScheduleInfo &scheduleInfo, AuthInfo &authInfo); + void BeginExecute(ScheduleInfo &scheduleInfo, std::size_t executorNum, uint64_t scheduleId, + AuthInfo &authInfo, int32_t &executeRet); class ResICoAuthCallbackDeathRecipient : public IRemoteObject::DeathRecipient { public: ResICoAuthCallbackDeathRecipient(uint64_t scheduleId, CoAuthManager* parent); @@ -49,6 +54,7 @@ private: DISALLOW_COPY_AND_MOVE(ResICoAuthCallbackDeathRecipient); }; AuthResManager* coAuthResMgrPtr_; + std::shared_ptr monitor_ = nullptr; }; } // namespace CoAuth } // namespace UserIAM diff --git a/services/include/coauth_service.h b/services/include/coauth_service.h index 93874c6..25165b4 100755 --- a/services/include/coauth_service.h +++ b/services/include/coauth_service.h @@ -49,6 +49,8 @@ public: virtual void SetExecutorProp(ResAuthAttributes &conditions, const sptr &callback) override; private: + void SendRegisterBroadcast(); + CoAuthRunningState state_ = CoAuthRunningState::STATE_STOPPED; AuthResManager authResMgr_; CoAuthManager coAuthMgr_; diff --git a/services/src/auth_res_pool.cpp b/services/src/auth_res_pool.cpp index e03a6a9..e861fa5 100644 --- a/services/src/auth_res_pool.cpp +++ b/services/src/auth_res_pool.cpp @@ -47,7 +47,7 @@ int32_t AuthResPool::Insert(uint64_t scheduleId, uint64_t executorNum, sptr runner = AppExecFwk::EventRunner::Create(true); + runner->Run(); + eventHandler_ = std::make_shared(runner); +} + +void CallMonitor::MonitorCall(int64_t waitTime, Callback &timeoutFun) +{ + eventHandler_->PostHighPriorityTask(timeoutFun, waitTime); +} +} // namespace PinAuth +} // namespace UserIAM +} // namespace OHOS \ No newline at end of file diff --git a/services/src/coauth_manager.cpp b/services/src/coauth_manager.cpp index 6604548..e2bf390 100644 --- a/services/src/coauth_manager.cpp +++ b/services/src/coauth_manager.cpp @@ -14,6 +14,7 @@ */ #include "coauth_manager.h" +#include "inner_event.h" #include "coauth_thread_pool.h" namespace OHOS { @@ -27,8 +28,11 @@ void CoAuthManager::coAuth(uint64_t scheduleId, AuthInfo &authInfo, sptr callback) { + if (monitor_ == nullptr) { + monitor_ = std::make_shared(); + } if (callback == nullptr) { - COAUTH_HILOGI(MODULE_SERVICE, "Schedule callback is null."); + COAUTH_HILOGE(MODULE_SERVICE, "Schedule callback is null."); return; } int32_t executeRet = SUCCESS; @@ -53,6 +57,23 @@ void CoAuthManager::CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptrOnFinish(saveRet, scheduleToken); } + OHOS::AppExecFwk::InnerEvent::Callback task = std::bind(&CoAuthManager::TimeOut, this, scheduleId); + if (monitor_ != nullptr) { + COAUTH_HILOGW(MODULE_SERVICE, "CoAuthManager::Excute MonitorCall"); + monitor_->MonitorCall(delay_time, task); + } + BeginExecute(scheduleInfo, executorNum, scheduleId, authInfo, executeRet); + // set timeout + if (executeRet != SUCCESS) { + COAUTH_HILOGW(MODULE_SERVICE, "There are one or more failures in execution."); + callback->OnFinish(executeRet, scheduleToken); + coAuthResMgrPtr_->DeleteScheduleCallback(scheduleId); + } +} + +void CoAuthManager::BeginExecute(ScheduleInfo &scheduleInfo, std::size_t executorNum, uint64_t scheduleId, + AuthInfo &authInfo, int32_t &executeRet) +{ for (std::size_t i = 0; i < executorNum; i++) { uint32_t authType = scheduleInfo.executors[i].authType; COAUTH_HILOGD(MODULE_SERVICE, "get authType = XXXX%{public}04d", authType); @@ -60,7 +81,7 @@ void CoAuthManager::CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptr publicKey(scheduleInfo.executors[i].publicKey, scheduleInfo.executors[i].publicKey + PUBLIC_KEY_LEN); int32_t findRet = coAuthResMgrPtr_->FindExecutorCallback(authType, executorCallback); - if ((findRet != 0) || (executorCallback == nullptr)) { + if ((findRet != SUCCESS) || (executorCallback == nullptr)) { COAUTH_HILOGE(MODULE_SERVICE, "executor callback not found."); continue; } @@ -68,11 +89,6 @@ void CoAuthManager::CoAuthHandle(uint64_t scheduleId, AuthInfo &authInfo, sptrOnBeginExecute(scheduleId, publicKey, commandAttrs); } - // set timeout - if (executeRet != SUCCESS) { - COAUTH_HILOGW(MODULE_SERVICE, "There are one or more failures in execution."); - return callback->OnFinish(executeRet, scheduleToken); - } } void CoAuthManager::SetAuthAttributes(std::shared_ptr commandAttrs, @@ -193,6 +209,7 @@ void CoAuthManager::RegistResourceManager(AuthResManager* resMgr) { coAuthResMgrPtr_ = resMgr; } + CoAuthManager::ResICoAuthCallbackDeathRecipient::ResICoAuthCallbackDeathRecipient( uint64_t scheduleId, CoAuthManager* parent) : scheduleId(scheduleId), parent_(parent) { @@ -210,6 +227,20 @@ void CoAuthManager::ResICoAuthCallbackDeathRecipient::OnRemoteDied(const wptr callback; + int32_t findRet = coAuthResMgrPtr_->FindScheduleCallback(scheduleId, callback); + if (findRet == SUCCESS && callback != nullptr) { + std::vector scheduleToken; + callback->OnFinish(TIMEOUT, scheduleToken); + COAUTH_HILOGW(MODULE_SERVICE, "Schedule timeout"); + coAuthResMgrPtr_->DeleteScheduleCallback(scheduleId); + } else { + COAUTH_HILOGD(MODULE_SERVICE, "Schedule has ended"); + } +} } // namespace CoAuth } // namespace UserIAM } // namespace OHOS diff --git a/services/src/coauth_service.cpp b/services/src/coauth_service.cpp index a923d6f..02ed97d 100755 --- a/services/src/coauth_service.cpp +++ b/services/src/coauth_service.cpp @@ -22,6 +22,7 @@ #include #include #include "useriam_common.h" +#include "common_event_manager.h" #include "coauth_service.h" namespace OHOS { @@ -84,6 +85,8 @@ void CoAuthService::OnStart() } // Send registration broadcast + SendRegisterBroadcast(); + // Start other sevice std::thread checkThread(OHOS::UserIAM::CoAuth::CheckSystemAbility); checkThread.join(); @@ -172,6 +175,16 @@ int32_t CoAuthService::GetExecutorProp(ResAuthAttributes &conditions, std::share } return coAuthMgr_.GetExecutorProp(conditions, values); } + +void CoAuthService::SendRegisterBroadcast() +{ + EventFwk::Want want; + want.SetAction(REGISTER_NOTIFICATION); + EventFwk::CommonEventData data; + data.SetWant(want); + bool ret = EventFwk::CommonEventManager::PublishCommonEvent(data); + COAUTH_HILOGI(MODULE_SERVICE, "send broadcast result = %{public}d", ret); +} } // namespace CoAu } // namespace UserIAM } // namespace OHOS -- Gitee