1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ASYNC_COMMAND_BASE_H 17 #define ASYNC_COMMAND_BASE_H 18 19 #include <mutex> 20 21 #include "nocopyable.h" 22 23 #include "co_auth_client_defines.h" 24 #include "executor.h" 25 #include "iam_common_defines.h" 26 #include "iam_defines.h" 27 28 namespace OHOS { 29 namespace UserIam { 30 namespace UserAuth { 31 class AsyncCommandBase : public std::enable_shared_from_this<AsyncCommandBase>, 32 public IAsyncCommand, 33 public IExecuteCallback, 34 public NoCopyable { 35 public: 36 AsyncCommandBase(std::string type, uint64_t scheduleId, std::weak_ptr<Executor> executor, 37 std::shared_ptr<ExecutorMessenger> executorMessenger); 38 ~AsyncCommandBase() override = default; 39 40 void OnHdiDisconnect() override; 41 ResultCode StartProcess() override; 42 void OnResult(ResultCode result) override; 43 void OnResult(ResultCode result, const std::vector<uint8_t> &extraInfo) override; 44 void OnAcquireInfo(int32_t acquire, const std::vector<uint8_t> &extraInfo) override; 45 int32_t GetAuthType(); 46 47 protected: 48 static uint32_t GenerateCommandId(); 49 virtual ResultCode SendRequest() = 0; 50 virtual void OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo) = 0; 51 virtual void OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo) = 0; 52 std::shared_ptr<IAuthExecutorHdi> GetExecutorHdi(); 53 int32_t MessengerSendData(uint64_t scheduleId, uint64_t transNum, ExecutorRole srcType, ExecutorRole dstType, 54 std::shared_ptr<AuthMessage> msg); 55 int32_t MessengerFinish(uint64_t scheduleId, ExecutorRole srcType, int32_t resultCode, 56 std::shared_ptr<Attributes> finalResult); 57 58 const char *GetDescription(); 59 uint64_t scheduleId_; 60 61 private: 62 void EndProcess(); 63 std::string description_; 64 std::weak_ptr<Executor> executor_; 65 std::shared_ptr<ExecutorMessenger> executorMessenger_; 66 std::mutex mutex_; 67 bool isFinished_ = false; 68 }; 69 } // namespace UserAuth 70 } // namespace UserIam 71 } // namespace OHOS 72 73 #endif // ASYNC_COMMAND_BASE_H