1 /*
2  * Copyright (c) 2021-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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_STATE_MACHINE_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_STATE_MACHINE_H
18 
19 #include <iostream>
20 #include <map>
21 #include "account_state_action.h"
22 #include "account_info.h"
23 
24 namespace OHOS {
25 namespace AccountSA {
26 /**
27  * Account state machine
28  */
29 class AccountStateMachine {
30 public:
31 
32     /**
33      * exception event action, to re-init state machine, and get account state from account server.
34      */
35     class ExceptionAction : public AccountStateAction {
36     public:
37         /**
38          * exception event action Constructor.
39          */
ExceptionAction(int status)40         explicit ExceptionAction(int status) : AccountStateAction(status) {}
41 
42         /**
43           * exception event action Destructor.
44           */
~ExceptionAction()45         ~ExceptionAction() override {}
46     };
47 
48     /**
49      * unbound state action.
50      */
51     class UnboundAction : public AccountStateAction {
52     public:
53         /**
54          * unbound state action Constructor.
55          */
UnboundAction(int status)56         explicit UnboundAction(int status) : AccountStateAction(status) {}
57 
58         /**
59           * unbound state action Destructor.
60           */
~UnboundAction()61         ~UnboundAction() override {}
62     };
63 
64     /**
65      * login state action.
66      */
67     class LoginAction : public AccountStateAction {
68     public:
69         /**
70          * login state action Constructor.
71          */
LoginAction(int status)72         explicit LoginAction(int status) : AccountStateAction(status) {}
73 
74         /**
75           * login state action Destructor.
76           */
~LoginAction()77         ~LoginAction() override {}
78     };
79 
80     /**
81      * logout state action.
82      */
83     class LogoutAction : public AccountStateAction {
84     public:
85         /**
86          * logout state action Constructor.
87          */
LogoutAction(int status)88         explicit LogoutAction(int status) : AccountStateAction(status) {}
89 
90         /**
91           * logout state action Destructor.
92           */
~LogoutAction()93         ~LogoutAction() override {}
94     };
95 
96     /**
97      * logoff state action.
98      */
99     class LogoffAction : public AccountStateAction {
100     public:
101         /**
102          * logoff state action Constructor.
103          */
LogoffAction(int status)104         explicit LogoffAction(int status) : AccountStateAction(status) {}
105 
106         /**
107           * logoff state action Destructor.
108           */
~LogoffAction()109         ~LogoffAction() override {}
110     };
111 
112     /**
113       * Account state machine Constructor.
114       */
AccountStateMachine()115     AccountStateMachine() : currentState_(ACCOUNT_STATE_UNBOUND)
116     {
117         OnInitialize();
118     }
119 
120     /**
121       * Account state machine Destructor.
122       */
~AccountStateMachine()123     ~AccountStateMachine()
124     {
125         Clean();
126     }
127 
128     /**
129       * Account state machine initialize.
130       */
131     void OnInitialize();
132 
133     /**
134       * Account state machine clean.
135       */
136     void Clean();
137 
138     /**
139      * Get account current state
140      *
141      * @return account current state
142      */
GetAccountState()143     int GetAccountState()
144     {
145         return currentState_;
146     }
147 
148     /**
149      * Set account current state
150      *
151      * @param current state
152      */
SetAccountState(int currentState)153     void SetAccountState(int currentState)
154     {
155         currentState_ = currentState;
156     }
157 
158     /**
159      * Process an state change event.
160      *
161      * @param evt the event info
162      * @return true if the processing was completed, otherwise false
163      */
164     bool StateChangeProcess(int evt);
165 
166 private:
167      /**
168       * Account current state.
169       */
170     int currentState_;
171     std::map<int, std::map<int, AccountStateAction *>> stateMachineMap_;
172 };
173 }  // namespace AccountSA
174 }  // namespace OHOS
175 
176 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_STATE_MACHINE_H
177