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 #include "app_account_check_labels_callback.h"
17
18 #include "app_account_authenticator_session_manager.h"
19 #include "account_log_wrapper.h"
20 #include "app_account_constants.h"
21
22 namespace OHOS {
23 namespace AccountSA {
AppAccountCheckLabelsCallback(std::vector<AppAccountInfo> accounts,const AuthenticatorSessionRequest & request,const std::string & sessionId)24 AppAccountCheckLabelsCallback::AppAccountCheckLabelsCallback(std::vector<AppAccountInfo> accounts,
25 const AuthenticatorSessionRequest &request, const std::string &sessionId)
26 : accounts_(accounts), request_(request), sessionId_(sessionId)
27 {}
28
~AppAccountCheckLabelsCallback()29 AppAccountCheckLabelsCallback::~AppAccountCheckLabelsCallback()
30 {}
31
SendResult(int32_t resultCode)32 void AppAccountCheckLabelsCallback::SendResult(int32_t resultCode)
33 {
34 AAFwk::Want result;
35 if (resultCode == ERR_JS_SUCCESS) {
36 std::vector<std::string> names;
37 std::vector<std::string> owners;
38 for (auto account : accountsWithLabels_) {
39 names.push_back(account.GetName());
40 owners.push_back(account.GetOwner());
41 }
42 result.SetParam(Constants::KEY_ACCOUNT_NAMES, names);
43 result.SetParam(Constants::KEY_ACCOUNT_OWNERS, owners);
44 }
45 auto sessionManager = AppAccountAuthenticatorSessionManager::GetInstance();
46 if (sessionManager != nullptr) {
47 sessionManager->OnSessionResult(sessionId_, resultCode, result);
48 }
49 }
50
CheckLabels()51 ErrCode AppAccountCheckLabelsCallback::CheckLabels()
52 {
53 auto sessionManager = AppAccountAuthenticatorSessionManager::GetInstance();
54 if (sessionManager == nullptr) {
55 return ERR_APPACCOUNT_SERVICE_OAUTH_SERVICE_EXCEPTION;
56 }
57 while (index_ < accounts_.size()) {
58 AppAccountInfo account = accounts_[index_];
59 AuthenticatorSessionRequest newRequest = request_;
60 account.GetOwner(newRequest.owner);
61 account.GetName(newRequest.name);
62 newRequest.callback = this;
63 if (sessionManager->CheckAccountLabels(newRequest) == ERR_OK) {
64 break;
65 }
66 index_++;
67 }
68 if (index_ >= accounts_.size()) {
69 SendResult(ERR_JS_SUCCESS);
70 sessionManager->CloseSession(sessionId_);
71 return ERR_OK;
72 }
73 return ERR_OK;
74 }
75
OnResult(int32_t resultCode,const AAFwk::Want & result)76 void AppAccountCheckLabelsCallback::OnResult(int32_t resultCode, const AAFwk::Want &result)
77 {
78 if (result.GetBoolParam(Constants::KEY_BOOLEAN_RESULT, false)) {
79 accountsWithLabels_.push_back(accounts_[index_]);
80 }
81 index_++;
82 CheckLabels();
83 }
84
OnRequestRedirected(AAFwk::Want & request)85 void AppAccountCheckLabelsCallback::OnRequestRedirected(AAFwk::Want &request)
86 {
87 index_++;
88 CheckLabels();
89 }
90
OnRequestContinued()91 void AppAccountCheckLabelsCallback::OnRequestContinued()
92 {
93 index_++;
94 CheckLabels();
95 }
96 } // namespace AccountSA
97 } // namespace OHOS
98