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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_AUTHENTICATOR_SESSION_H
17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_AUTHENTICATOR_SESSION_H
18 
19 #include <string>
20 #include "ability_connect_callback_stub.h"
21 #include "app_account_authenticator_session_manager.h"
22 #include "app_account_control_manager.h"
23 #include "iremote_proxy.h"
24 #include "iapp_account_authenticator.h"
25 #include "iapp_account_authenticator_callback.h"
26 #include "want.h"
27 
28 namespace OHOS {
29 namespace AccountSA {
30 class AppAccountAuthenticatorSession;
31 
32 class SessionClientDeathRecipient : public IRemoteObject::DeathRecipient {
33 public:
34     explicit SessionClientDeathRecipient(const std::string &sessionId);
35     ~SessionClientDeathRecipient() override = default;
36 
37     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
38 
39 private:
40     std::string sessionId_;
41 };
42 
43 class SessionServerDeathRecipient : public IRemoteObject::DeathRecipient {
44 public:
45     explicit SessionServerDeathRecipient(const std::string &sessionId);
46     ~SessionServerDeathRecipient() override = default;
47 
48     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
49 
50 private:
51     std::string sessionId_;
52 };
53 
54 class SessionConnection : public AAFwk::AbilityConnectionStub {
55 public:
56     explicit SessionConnection(const std::string &sessionId);
57     ~SessionConnection() override;
58 
59     void OnAbilityConnectDone(
60         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode) override;
61     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
62 
63 private:
64     std::string sessionId_;
65 };
66 
67 class AppAccountAuthenticatorSession {
68 public:
69     AppAccountAuthenticatorSession(AuthenticatorAction action, const AuthenticatorSessionRequest &request);
70     virtual ~AppAccountAuthenticatorSession();
71     virtual ErrCode Open();
72     virtual void Close();
73     virtual void GetRequest(AuthenticatorSessionRequest &request) const;
74     std::string GetSessionId() const;
75     void OnAbilityConnectDone(
76         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int32_t resultCode);
77     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode);
78     void OnServerDied();
79     int32_t OnResult(int32_t resultCode, const AAFwk::Want &result) const;
80     int32_t OnRequestRedirected(AAFwk::Want &newRequest) const;
81     int32_t OnRequestContinued() const;
82     ErrCode GetAuthenticatorCallback(const AuthenticatorSessionRequest &request, sptr<IRemoteObject> &callback) const;
83     ErrCode AddClientDeathRecipient();
84 
85 protected:
86     AuthenticatorAction action_;
87     AuthenticatorSessionRequest request_;
88     std::string sessionId_;
89     bool isOpened_ = false;
90 
91 private:
92     void Init();
93     void CloseSelf() const;
94     int32_t OnAuthenticateDone(const AAFwk::Want &result) const;
95     int32_t OnAddAccountImplicitlyDone(const AAFwk::Want &result) const;
96 
97 private:
98     std::mutex mutex_;
99     sptr<SessionConnection> conn_ = nullptr;
100     sptr<SessionClientDeathRecipient> clientDeathRecipient_ = nullptr;
101     sptr<SessionServerDeathRecipient> serverDeathRecipient_ = nullptr;
102     sptr<IAppAccountAuthenticatorCallback> authenticatorCb_ = nullptr;
103     sptr<IAppAccountAuthenticator> authenticatorProxy_ = nullptr;
104     std::shared_ptr<AppAccountControlManager> controlManager_ = nullptr;
105     std::shared_ptr<AppAccountAuthenticatorManager> authenticatorMgr_ = nullptr;
106     int32_t userId_;
107     pid_t ownerUid_;
108     bool isInitialized_ = false;
109     bool isConnected_ = false;
110 };
111 }  // namespace AccountSA
112 }  // namespace OHOS
113 
114 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_APPACCOUNT_APP_ACCOUNT_AUTHENTICATOR_SESSION_H
115