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     AppAccountAuthenticatorSessionManager::GetInstance().OnSessionResult(sessionId_, resultCode, result);
46 }
47 
CheckLabels()48 ErrCode AppAccountCheckLabelsCallback::CheckLabels()
49 {
50     std::lock_guard<std::recursive_mutex> lock(mutex_);
51     auto &sessionManager = AppAccountAuthenticatorSessionManager::GetInstance();
52     while (index_ < accounts_.size()) {
53         AppAccountInfo account = accounts_[index_];
54         AuthenticatorSessionRequest newRequest = request_;
55         account.GetOwner(newRequest.owner);
56         account.GetName(newRequest.name);
57         newRequest.callback = this;
58         isRequesting_ = true;
59         if (sessionManager.CheckAccountLabels(newRequest) == ERR_OK) {
60             break;
61         }
62         isRequesting_ = false;
63         index_++;
64     }
65     if (index_ >= accounts_.size()) {
66         SendResult(ERR_JS_SUCCESS);
67         sessionManager.CloseSession(sessionId_);
68         return ERR_OK;
69     }
70     return ERR_OK;
71 }
72 
OnResult(int32_t resultCode,const AAFwk::Want & result)73 void AppAccountCheckLabelsCallback::OnResult(int32_t resultCode, const AAFwk::Want &result)
74 {
75     std::lock_guard<std::recursive_mutex> lock(mutex_);
76     if (!isRequesting_) {
77         ACCOUNT_LOGE("Invalid request");
78         return;
79     }
80     isRequesting_ = false;
81     if (result.GetBoolParam(Constants::KEY_BOOLEAN_RESULT, false) && (index_ < accounts_.size())) {
82         accountsWithLabels_.push_back(accounts_[index_]);
83     }
84     index_++;
85     CheckLabels();
86 }
87 
OnRequestRedirected(AAFwk::Want & request)88 void AppAccountCheckLabelsCallback::OnRequestRedirected(AAFwk::Want &request)
89 {
90     std::lock_guard<std::recursive_mutex> lock(mutex_);
91     if (!isRequesting_) {
92         ACCOUNT_LOGE("Invalid request");
93         return;
94     }
95     isRequesting_ = false;
96     index_++;
97     CheckLabels();
98 }
99 
OnRequestContinued()100 void AppAccountCheckLabelsCallback::OnRequestContinued()
101 {
102     std::lock_guard<std::recursive_mutex> lock(mutex_);
103     if (!isRequesting_) {
104         ACCOUNT_LOGE("Invalid request");
105         return;
106     }
107     isRequesting_ = false;
108     index_++;
109     CheckLabels();
110 }
111 }  // namespace AccountSA
112 }  // namespace OHOS
113