1 /*
2  * Copyright (c) 2023 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 IAM_WIDGET_SCHEDULE_NODE_IMPL_H
17 #define IAM_WIDGET_SCHEDULE_NODE_IMPL_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 #include <optional>
23 #include <set>
24 
25 #include "iam_hitrace_helper.h"
26 
27 #include "finite_state_machine.h"
28 #include "resource_node.h"
29 #include "widget_schedule_node.h"
30 
31 namespace OHOS {
32 namespace UserIam {
33 namespace UserAuth {
34 class WidgetScheduleNodeImpl final : public WidgetScheduleNode,
35                                public std::enable_shared_from_this<WidgetScheduleNodeImpl>,
36                                public NoCopyable {
37 public:
38     WidgetScheduleNodeImpl();
39     ~WidgetScheduleNodeImpl() override = default;
40     bool StartSchedule() override;
41     bool StopSchedule() override;
42     bool StartAuthList(const std::vector<AuthType> &authTypeList, bool endAfterFirstFail,
43         AuthIntent authIntent) override;
44     bool StopAuthList(const std::vector<AuthType> &authTypeList) override;
45     bool SuccessAuth(AuthType authType) override;
46     bool NaviPinAuth() override;
47     bool WidgetParaInvalid() override;
48     bool WidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad,
49         AuthType &rotateAuthType) override;
50     void SetCallback(std::shared_ptr<WidgetScheduleNodeCallback> callback) override;
51 
52 protected:
53     void OnStartSchedule(FiniteStateMachine &machine, uint32_t event);
54     void OnStopSchedule(FiniteStateMachine &machine, uint32_t event);
55     void OnStartAuth(FiniteStateMachine &machine, uint32_t event);
56     void OnStopAuthList(FiniteStateMachine &machine, uint32_t event);
57     void OnSuccessAuth(FiniteStateMachine &machine, uint32_t event);
58     void OnNaviPinAuth(FiniteStateMachine &machine, uint32_t event);
59     void OnWidgetParaInvalid(FiniteStateMachine &machine, uint32_t event);
60     void OnWidgetReload(FiniteStateMachine &machine, uint32_t event);
61     void OnWidgetReloadInit(FiniteStateMachine &machine, uint32_t event);
62 
63 private:
64     std::shared_ptr<FiniteStateMachine> MakeFiniteStateMachine();
65     bool TryKickMachine(Event event);
66 
67 private:
68     std::shared_ptr<ThreadHandler> threadHandler_ {nullptr};
69     std::shared_ptr<FiniteStateMachine> machine_ {nullptr};
70     std::mutex mutex_;
71     std::shared_ptr<IamHitraceHelper> iamHitraceHelper_ {nullptr};
72     std::weak_ptr<WidgetScheduleNodeCallback> callback_;
73     AuthType successAuthType_ {0};
74     std::vector<AuthType> startAuthTypeList_;
75     bool endAfterFirstFail_ {false};
76     std::vector<AuthType> stopAuthTypeList_;
77     std::set<AuthType> runningAuthTypeSet_;
78     uint32_t orientation_ {0};
79     uint32_t needRotate_ {0};
80     uint32_t alreadyLoad_ {0};
81     AuthType rotateAuthType_ {0};
82     AuthIntent authIntent_ {AuthIntent::DEFAULT};
83 };
84 } // namespace UserAuth
85 } // namespace UserIam
86 } // namespace OHOS
87 #endif // IAM_WIDGET_SCHEDULE_NODE_IMPL_H