• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
17 #ifndef APPACCOUNT_COMMON_H
18 #define APPACCOUNT_COMMON_H
19 
20 #include <mutex>
21 #include <thread>
22 #include "app_account_authenticator_callback_stub.h"
23 #include "app_account_common.h"
24 #include "app_account_manager.h"
25 #include "app_account_subscriber.h"
26 #include "cj_common_ffi.h"
27 #include "want.h"
28 #include "appaccount_defination.h"
29 
30 namespace OHOS::AccountSA {
31 class SubscribePtr : public AppAccountSubscriber {
32 public:
33     explicit SubscribePtr(const AppAccountSubscribeInfo &subscribeInfo);
34     void SetCallbackRef(std::function<void(CArrAppAccountInfo)> callbackRef);
35     void OnAccountsChanged(const std::vector<AppAccountInfo>  &accounts) override;
36     std::function<void(CArrAppAccountInfo)> ref_;
37 };
38 
39 struct AsyncContextForSubscribe {
40     std::string type;
41     std::vector<std::string> owners;
42     std::shared_ptr<SubscribePtr> subscriber = nullptr;
43     std::function<void(CArrAppAccountInfo)> callbackRef;
44 };
45 
46 struct AsyncContextForUnSubscribe {
47     std::string type;
48     std::vector<std::shared_ptr<SubscribePtr>> subscribers = {nullptr};
49     std::function<void(CArrAppAccountInfo)> callbackRef;
50 };
51 
52 struct CheckAccountLabelsContext {
53     std::string name;
54     std::string owner;
55     std::vector<std::string> labels;
56     std::function<void(RetDataBool)> callbackRef = nullptr;
57     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
58     ErrCode errCode = ERR_OK;
59 };
60 
61 class AuthenticatorAsyncCallback : public AppAccountAuthenticatorCallbackStub {
62 public:
63     explicit AuthenticatorAsyncCallback(
64         std::function<void(RetDataBool)> cRef, std::function<void(ErrCArrAppAccountInfo)> sRef);
65     ~AuthenticatorAsyncCallback();
66 
67     void OnResult(int32_t resultCode, const AAFwk::Want &result) override;
68     void OnRequestRedirected(AAFwk::Want &request) override;
69     void OnRequestContinued() override;
70 
71     bool onResultRetBool = false;
72     std::vector<std::string> onResultRetNames;
73     std::vector<std::string> onResultRetOwners;
74     AAFwk::Want result_;
75     AAFwk::Want request_;
76     ErrCode errCode = ERR_OK;
77 
78 private:
79     std::mutex mutex_;
80     bool isDone = false;
81     std::function<void(RetDataBool)> checkAccountLabelsCallbackRef = nullptr;
82     std::function<void(ErrCArrAppAccountInfo)> selectAccountsCallbackRef = nullptr;
83 };
84 
85 struct SelectAccountsContext {
86     SelectAccountsOptions options;
87     std::vector<AppAccountInfo> appAccountInfos;
88     std::function<void(ErrCArrAppAccountInfo)> callbackRef = nullptr;
89     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
90     ErrCode errCode = ERR_OK;
91 };
92 
93 struct JSAuthCallback {
94     std::function<void(int32_t, CAuthResult)> onResult = nullptr;
95     std::function<void(WantHandle)> onRequestRedirected = nullptr;
96     std::function<void()> onRequestContinued = nullptr;
97 };
98 
99 class AppAccountManagerCallback : public AppAccountAuthenticatorCallbackStub {
100 public:
101     explicit AppAccountManagerCallback(JSAuthCallback callback);
102     ~AppAccountManagerCallback();
103 
104     void OnResult(int32_t resultCode, const AAFwk::Want &result) override;
105     void OnRequestRedirected(AAFwk::Want &request) override;
106     void OnRequestContinued() override;
107 
108     AAFwk::Want result_;
109     JSAuthCallback callback_;
110     ErrCode errCode = ERR_OK;
111     // account: AppAccountInfo
112     std::string nameResult;
113     std::string ownerResult;
114     //tokenInfo: AuthTokenInfo
115     std::string authTypeResult;
116     std::string tokenResult;
117 
118 private:
119     std::mutex mutex_;
120     bool isDone = false;
121 };
122 } // namespace OHOS::AccountSA
123 #endif