• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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     ErrCode OnResult(int32_t resultCode, const AAFwk::Want &result) override;
68     ErrCode OnRequestRedirected(const AAFwk::Want &request) override;
69     ErrCode OnRequestContinued() override;
70     ErrCode CallbackEnter([[maybe_unused]] uint32_t code) override;
71     ErrCode CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override;
72 
73     bool onResultRetBool = false;
74     std::vector<std::string> onResultRetNames;
75     std::vector<std::string> onResultRetOwners;
76     AAFwk::Want result_;
77     AAFwk::Want request_;
78     ErrCode errCode = ERR_OK;
79 
80 private:
81     std::mutex mutex_;
82     bool isDone = false;
83     std::function<void(RetDataBool)> checkAccountLabelsCallbackRef = nullptr;
84     std::function<void(ErrCArrAppAccountInfo)> selectAccountsCallbackRef = nullptr;
85 };
86 
87 struct SelectAccountsContext {
88     SelectAccountsOptions options;
89     std::vector<AppAccountInfo> appAccountInfos;
90     std::function<void(ErrCArrAppAccountInfo)> callbackRef = nullptr;
91     sptr<IAppAccountAuthenticatorCallback> appAccountMgrCb = nullptr;
92     ErrCode errCode = ERR_OK;
93 };
94 
95 struct JSAuthCallback {
96     std::function<void(int32_t, CAuthResult)> onResult = nullptr;
97     std::function<void(WantHandle)> onRequestRedirected = nullptr;
98     std::function<void()> onRequestContinued = nullptr;
99 };
100 
101 class AppAccountManagerCallback : public AppAccountAuthenticatorCallbackStub {
102 public:
103     explicit AppAccountManagerCallback(JSAuthCallback callback);
104     ~AppAccountManagerCallback();
105 
106     ErrCode OnResult(int32_t resultCode, const AAFwk::Want &result) override;
107     ErrCode OnRequestRedirected(const AAFwk::Want &request) override;
108     ErrCode OnRequestContinued() override;
109     ErrCode CallbackEnter([[maybe_unused]] uint32_t code) override;
110     ErrCode CallbackExit([[maybe_unused]] uint32_t code, [[maybe_unused]] int32_t result) override;
111 
112     AAFwk::Want result_;
113     JSAuthCallback callback_;
114     ErrCode errCode = ERR_OK;
115     // account: AppAccountInfo
116     std::string nameResult;
117     std::string ownerResult;
118     //tokenInfo: AuthTokenInfo
119     std::string authTypeResult;
120     std::string tokenResult;
121 
122 private:
123     std::mutex mutex_;
124     bool isDone = false;
125 };
126 } // namespace OHOS::AccountSA
127 #endif