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 OnMessage(int destRole, const std::vector<uint8_t> &msg) override;
45     void OnAcquireInfo(int32_t acquire, const std::vector<uint8_t> &extraInfo) override;
46     int32_t GetAuthType();
47 
48 protected:
49     static uint32_t GenerateCommandId();
50     virtual ResultCode SendRequest() = 0;
51     virtual void OnResultInner(ResultCode result, const std::vector<uint8_t> &extraInfo) = 0;
52     virtual void OnAcquireInfoInner(int32_t acquire, const std::vector<uint8_t> &extraInfo);
53     virtual void OnMessageInner(int destRole, const std::vector<uint8_t> &msg);
54     std::shared_ptr<IAuthExecutorHdi> GetExecutorHdi();
55     int32_t MessengerSendData(uint64_t scheduleId, ExecutorRole dstType,
56         std::shared_ptr<AuthMessage> msg);
57     int32_t MessengerFinish(uint64_t scheduleId, int32_t resultCode,
58         std::shared_ptr<Attributes> finalResult);
59 
60     const char *GetDescription();
61     uint64_t scheduleId_ = 0;
62 
63 private:
64     void EndProcess();
65     std::string description_;
66     std::weak_ptr<Executor> executor_;
67     std::shared_ptr<ExecutorMessenger> executorMessenger_ {nullptr};
68     std::mutex mutex_;
69     bool isFinished_ = false;
70 };
71 } // namespace UserAuth
72 } // namespace UserIam
73 } // namespace OHOS
74 
75 #endif // ASYNC_COMMAND_BASE_H