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 EXCECUTOR_H
17 #define EXCECUTOR_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 
24 #include "nocopyable.h"
25 
26 #include "co_auth_client_callback.h"
27 #include "executor_mgr_wrapper.h"
28 #include "iasync_command.h"
29 #include "iam_defines.h"
30 #include "iam_executor_iauth_executor_hdi.h"
31 
32 namespace OHOS {
33 namespace UserIam {
34 namespace UserAuth {
35 class Executor : public std::enable_shared_from_this<Executor>, public NoCopyable {
36 public:
37     Executor(std::shared_ptr<ExecutorMgrWrapper> executorMgrWrapper, std::shared_ptr<IAuthExecutorHdi> executorHdi,
38         uint16_t hdiId);
39     ~Executor() override = default;
40 
41     void OnHdiConnect();
42     void OnHdiDisconnect();
43     void OnFrameworkReady();
44     void AddCommand(std::shared_ptr<IAsyncCommand> command);
45     void RemoveCommand(std::shared_ptr<IAsyncCommand> command);
46     void SetExecutorIndex(uint64_t executorIndex);
47     std::shared_ptr<IAuthExecutorHdi> GetExecutorHdi();
48     const char *GetDescription();
49     int32_t GetAuthType() const;
50     int32_t GetExecutorRole() const;
51 
52 private:
53     void RegisterExecutorCallback(ExecutorInfo &executorInfo);
54     void UnregisterExecutorCallback();
55     void RespondCallbackOnDisconnect();
56     std::recursive_mutex registerMutex_;
57     std::recursive_mutex mutex_;
58     std::shared_ptr<ExecutorRegisterCallback> executorCallback_ {nullptr};
59     std::set<std::shared_ptr<IAsyncCommand>> command2Respond_;
60     std::shared_ptr<ExecutorMgrWrapper> executorMgrWrapper_ {nullptr};
61     std::shared_ptr<IAuthExecutorHdi> executorHdi_ {nullptr};
62     std::string description_;
63     uint16_t hdiId_ = 0;
64     int32_t authType_ = INVALID_AUTH_TYPE;
65     int32_t executorRole_ = -1;
66     std::optional<uint64_t> executorIndex_ = std::nullopt;
67 };
68 } // namespace UserAuth
69 } // namespace UserIam
70 } // namespace OHOS
71 
72 #endif // EXCECUTOR_H