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 FRAMEWORK_EXECUTOR_CALLBACK_H
17 #define FRAMEWORK_EXECUTOR_CALLBACK_H
18 
19 #include <cstdint>
20 #include <mutex>
21 #include <string>
22 
23 #include "nocopyable.h"
24 
25 #include "attributes.h"
26 #include "co_auth_client_callback.h"
27 #include "executor.h"
28 #include "iam_common_defines.h"
29 #include "executor_messenger_interface.h"
30 
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
34 class FrameworkExecutorCallback : public ExecutorRegisterCallback, public NoCopyable {
35 public:
36     explicit FrameworkExecutorCallback(std::weak_ptr<Executor> executor);
37     ~FrameworkExecutorCallback() override = default;
38 
39     void OnMessengerReady(const std::shared_ptr<ExecutorMessenger> &messenger,
40         const std::vector<uint8_t> &publicKey, const std::vector<uint64_t> &templateIdList) override;
41 
42     int32_t OnBeginExecute(uint64_t scheduleId, const std::vector<uint8_t> &publicKey,
43         const Attributes &commandAttrs) override;
44     int32_t OnEndExecute(uint64_t scheduleId, const Attributes &commandAttrs) override;
45     int32_t OnSetProperty(const Attributes &properties) override;
46 
47     int32_t OnGetProperty(const Attributes &conditions, Attributes &results) override;
48     int32_t OnSendData(uint64_t scheduleId, const Attributes &data) override;
49 
50 private:
51     static uint32_t GenerateExecutorCallbackId();
52     ResultCode OnBeginExecuteInner(uint64_t scheduleId, std::vector<uint8_t> &publicKey,
53         const Attributes &commandAttrs);
54     ResultCode OnEndExecuteInner(uint64_t scheduleId, const Attributes &consumerAttr);
55     ResultCode OnSetPropertyInner(const Attributes &properties);
56     ResultCode OnGetPropertyInner(std::shared_ptr<Attributes> conditions,
57         std::shared_ptr<Attributes> values);
58     ResultCode ProcessEnrollCommand(uint64_t scheduleId, const Attributes &properties);
59     ResultCode ProcessAuthCommand(uint64_t scheduleId, const Attributes &properties);
60     ResultCode ProcessIdentifyCommand(uint64_t scheduleId, const Attributes &properties);
61     ResultCode ProcessCancelCommand(uint64_t scheduleId);
62     ResultCode ProcessDeleteTemplateCommand(const Attributes &properties);
63     ResultCode ProcessSetCachedTemplates(const Attributes &properties);
64     ResultCode ProcessNotifyExecutorReady(const Attributes &properties);
65     ResultCode ProcessCustomCommand(const Attributes &properties);
66     ResultCode ProcessGetPropertyCommand(std::shared_ptr<Attributes> conditions, std::shared_ptr<Attributes> values);
67     ResultCode FillPropertyToAttribute(const std::vector<Attributes::AttributeKey> &keyList, const Property property,
68         std::shared_ptr<Attributes> values);
69     const char *GetDescription();
70     std::recursive_mutex mutex_;
71     std::shared_ptr<ExecutorMessenger> executorMessenger_ {nullptr};
72     std::weak_ptr<Executor> executor_;
73     std::string description_;
74 };
75 } // namespace UserAuth
76 } // namespace UserIam
77 } // namespace OHOS
78 
79 #endif // FRAMEWORK_EXECUTOR_CALLBACK_H