1 /*
2 * Copyright (c) 2023-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 #include "widget_context.h"
16
17 #include <algorithm>
18
19 #include "accesstoken_kit.h"
20 #include "auth_widget_helper.h"
21 #include "context_helper.h"
22 #include "context_pool.h"
23 #include "context_death_recipient.h"
24 #include "iam_check.h"
25 #include "iam_logger.h"
26 #include "iam_para2str.h"
27 #include "iam_ptr.h"
28 #include "iam_time.h"
29 #include "schedule_node.h"
30 #include "schedule_node_callback.h"
31 #include "widget_schedule_node_impl.h"
32 #include "widget_context_callback_impl.h"
33 #include "widget_client.h"
34 #include "bool_wrapper.h"
35 #include "double_wrapper.h"
36 #include "int_wrapper.h"
37 #include "string_wrapper.h"
38 #include "want_params_wrapper.h"
39 #include "ability_connection.h"
40 #include "ability_connect_callback.h"
41 #include "refbase.h"
42 #include "hisysevent_adapter.h"
43 #include "system_ability_definition.h"
44
45 #define LOG_TAG "USER_AUTH_SA"
46
47 namespace OHOS {
48 namespace UserIam {
49 namespace UserAuth {
50 constexpr int32_t DEFAULT_VALUE = -1;
51 const std::string UI_EXTENSION_TYPE_SET = "sysDialog/userAuth";
52 const uint32_t SYSDIALOG_ZORDER_DEFAULT = 1;
53 const uint32_t SYSDIALOG_ZORDER_UPPER = 2;
54 const uint32_t ORIENTATION_LANDSCAPE = 1;
55 const uint32_t ORIENTATION_PORTRAIT_INVERTED = 2;
56 const uint32_t ORIENTATION_LANDSCAPE_INVERTED = 3;
57 const std::string TO_PORTRAIT = "90";
58 const std::string TO_INVERTED = "180";
59 const std::string TO_PORTRAIT_INVERTED = "270";
60 const uint32_t NOT_SUPPORT_ORIENTATION_INVERTED = 2;
61
WidgetContext(uint64_t contextId,const ContextFactory::AuthWidgetContextPara & para,std::shared_ptr<ContextCallback> callback)62 WidgetContext::WidgetContext(uint64_t contextId, const ContextFactory::AuthWidgetContextPara ¶,
63 std::shared_ptr<ContextCallback> callback)
64 : contextId_(contextId), description_("UserAuthWidget"), callerCallback_(callback), hasStarted_(false),
65 latestError_(ResultCode::GENERAL_ERROR), para_(para), schedule_(nullptr), connection_(nullptr)
66 {
67 AddDeathRecipient(callerCallback_, contextId_);
68 SubscribeAppState(callerCallback_, contextId_);
69 }
70
~WidgetContext()71 WidgetContext::~WidgetContext()
72 {
73 IAM_LOGI("release WidgetContext");
74 RemoveDeathRecipient(callerCallback_);
75 UnSubscribeAppState();
76 }
77
Start()78 bool WidgetContext::Start()
79 {
80 std::lock_guard<std::recursive_mutex> lock(mutex_);
81 IAM_LOGI("%{public}s start", description_.c_str());
82 if (hasStarted_) {
83 IAM_LOGI("%{public}s context has started, cannot start again", description_.c_str());
84 return false;
85 }
86 hasStarted_ = true;
87 return OnStart();
88 }
89
Stop()90 bool WidgetContext::Stop()
91 {
92 IAM_LOGI("%{public}s start", description_.c_str());
93 return OnStop();
94 }
95
GetContextId() const96 uint64_t WidgetContext::GetContextId() const
97 {
98 return contextId_;
99 }
100
GetContextType() const101 ContextType WidgetContext::GetContextType() const
102 {
103 return WIDGET_AUTH_CONTEXT;
104 }
105
GetScheduleNode(uint64_t scheduleId) const106 std::shared_ptr<ScheduleNode> WidgetContext::GetScheduleNode(uint64_t scheduleId) const
107 {
108 return nullptr;
109 }
110
GetTokenId() const111 uint32_t WidgetContext::GetTokenId() const
112 {
113 return para_.tokenId;
114 }
115
GetUserId() const116 int32_t WidgetContext::GetUserId() const
117 {
118 return para_.userId;
119 }
120
GetLatestError() const121 int32_t WidgetContext::GetLatestError() const
122 {
123 return latestError_;
124 }
125
SetLatestError(int32_t error)126 void WidgetContext::SetLatestError(int32_t error)
127 {
128 if (error != ResultCode::SUCCESS) {
129 latestError_ = error;
130 }
131 }
132
BuildSchedule()133 bool WidgetContext::BuildSchedule()
134 {
135 schedule_ = Common::MakeShared<WidgetScheduleNodeImpl>();
136 IF_FALSE_LOGE_AND_RETURN_VAL(schedule_ != nullptr, false);
137 schedule_->SetCallback(shared_from_this());
138 return true;
139 }
140
GetAuthContextCallback(AuthType authType,AuthTrustLevel authTrustLevel,sptr<IamCallbackInterface> & iamCallback)141 std::shared_ptr<ContextCallback> WidgetContext::GetAuthContextCallback(AuthType authType,
142 AuthTrustLevel authTrustLevel, sptr<IamCallbackInterface> &iamCallback)
143 {
144 auto widgetCallback = ContextCallback::NewInstance(iamCallback, TRACE_AUTH_USER_SECURITY);
145 if (widgetCallback == nullptr) {
146 IAM_LOGE("failed to construct context callback");
147 Attributes extraInfo;
148 iamCallback->OnResult(ResultCode::GENERAL_ERROR, extraInfo);
149 return nullptr;
150 }
151 widgetCallback->SetTraceCallerName(para_.callerName);
152 widgetCallback->SetTraceCallerType(para_.callerType);
153 widgetCallback->SetTraceRequestContextId(contextId_);
154 widgetCallback->SetTraceAuthTrustLevel(authTrustLevel);
155 widgetCallback->SetTraceAuthType(authType);
156 return widgetCallback;
157 }
158
BuildTask(const std::vector<uint8_t> & challenge,AuthType authType,AuthTrustLevel authTrustLevel,bool endAfterFirstFail,AuthIntent authIntent)159 std::shared_ptr<Context> WidgetContext::BuildTask(const std::vector<uint8_t> &challenge,
160 AuthType authType, AuthTrustLevel authTrustLevel, bool endAfterFirstFail, AuthIntent authIntent)
161 {
162 IF_FALSE_LOGE_AND_RETURN_VAL(callerCallback_ != nullptr, nullptr);
163 auto userId = para_.userId;
164 auto tokenId = WidgetClient::Instance().GetAuthTokenId();
165 IAM_LOGI("Real userId: %{public}d, Real tokenId: %{public}s", userId, GET_MASKED_STRING(tokenId).c_str());
166 sptr<IamCallbackInterface> iamCallback(new (std::nothrow) WidgetContextCallbackImpl(weak_from_this(),
167 static_cast<int32_t>(authType)));
168 IF_FALSE_LOGE_AND_RETURN_VAL(iamCallback != nullptr, nullptr);
169 auto widgetCallback = GetAuthContextCallback(authType, authTrustLevel, iamCallback);
170 IF_FALSE_LOGE_AND_RETURN_VAL(widgetCallback != nullptr, nullptr);
171
172 Authentication::AuthenticationPara para = {};
173 para.tokenId = tokenId;
174 para.userId = userId;
175 para.authType = authType;
176 para.atl = authTrustLevel;
177 para.challenge = challenge;
178 para.endAfterFirstFail = endAfterFirstFail;
179 para.callerName = para_.callerName;
180 para.callerType = para_.callerType;
181 para.sdkVersion = para_.sdkVersion;
182 para.authIntent = authIntent;
183 para.isOsAccountVerified = para_.isOsAccountVerified;
184 auto context = ContextFactory::CreateSimpleAuthContext(para, widgetCallback);
185 if (context == nullptr || !ContextPool::Instance().Insert(context)) {
186 IAM_LOGE("failed to insert context");
187 Attributes extraInfo;
188 widgetCallback->OnResult(ResultCode::GENERAL_ERROR, extraInfo);
189 return nullptr;
190 }
191 widgetCallback->SetTraceAuthContextId(context->GetContextId());
192 widgetCallback->SetCleaner(ContextHelper::Cleaner(context));
193 std::lock_guard<std::recursive_mutex> lock(mutex_);
194 return context;
195 }
196
OnStart()197 bool WidgetContext::OnStart()
198 {
199 IAM_LOGI("%{public}s start", description_.c_str());
200 if (!BuildSchedule()) {
201 IAM_LOGE("failed to create widget schedule");
202 return false;
203 }
204 IF_FALSE_LOGE_AND_RETURN_VAL(schedule_ != nullptr, false);
205 WidgetClient::Instance().SetWidgetContextId(GetContextId());
206 WidgetClient::Instance().SetWidgetParam(para_.widgetParam);
207 WidgetClient::Instance().SetAuthTypeList(para_.authTypeList);
208 WidgetClient::Instance().SetWidgetSchedule(schedule_);
209 WidgetClient::Instance().SetChallenge(para_.challenge);
210 WidgetClient::Instance().SetCallingBundleName(GetCallingBundleName());
211 schedule_->StartSchedule();
212
213 IAM_LOGI("WidgetContext start success.");
214 return true;
215 }
216
OnResult(int32_t resultCode,const std::shared_ptr<Attributes> & scheduleResultAttr)217 void WidgetContext::OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr)
218 {
219 IAM_LOGI("%{public}s receive result code %{public}d", description_.c_str(), resultCode);
220 }
221
OnStop()222 bool WidgetContext::OnStop()
223 {
224 // response app.cancel()
225 IAM_LOGI("%{public}s start", description_.c_str());
226 End(ResultCode::CANCELED);
227 return true;
228 }
229
AuthResult(int32_t resultCode,int32_t authType,const Attributes & finalResult)230 void WidgetContext::AuthResult(int32_t resultCode, int32_t authType, const Attributes &finalResult)
231 {
232 IAM_LOGI("recv task result: %{public}d, authType: %{public}d", resultCode, authType);
233 std::lock_guard<std::recursive_mutex> lock(mutex_);
234 int32_t remainTimes = -1;
235 int32_t freezingTime = -1;
236 if (!finalResult.GetInt32Value(Attributes::ATTR_REMAIN_TIMES, remainTimes)) {
237 IAM_LOGI("get remainTimes failed.");
238 }
239 if (!finalResult.GetInt32Value(Attributes::ATTR_FREEZING_TIME, freezingTime)) {
240 IAM_LOGI("get freezingTime failed.");
241 }
242 AuthType authTypeTmp = static_cast<AuthType>(authType);
243 WidgetClient::Instance().ReportWidgetResult(resultCode, authTypeTmp, freezingTime, remainTimes);
244 IF_FALSE_LOGE_AND_RETURN(schedule_ != nullptr);
245 IF_FALSE_LOGE_AND_RETURN(callerCallback_ != nullptr);
246 callerCallback_->SetTraceAuthType(authTypeTmp);
247 IAM_LOGI("call schedule:");
248 if (resultCode == ResultCode::SUCCESS) {
249 finalResult.GetUint8ArrayValue(Attributes::ATTR_SIGNATURE, authResultInfo_.token);
250 finalResult.GetUint64Value(Attributes::ATTR_CREDENTIAL_DIGEST, authResultInfo_.credentialDigest);
251 finalResult.GetUint16Value(Attributes::ATTR_CREDENTIAL_COUNT, authResultInfo_.credentialCount);
252 authResultInfo_.authType = authTypeTmp;
253 schedule_->SuccessAuth(authTypeTmp);
254 } else {
255 // failed
256 SetLatestError(resultCode);
257 schedule_->StopAuthList({authTypeTmp});
258 }
259 }
260
AuthTipInfo(int32_t tipType,int32_t authType,const Attributes & extraInfo)261 void WidgetContext::AuthTipInfo(int32_t tipType, int32_t authType, const Attributes &extraInfo)
262 {
263 IAM_LOGI("recv tip: %{public}d, authType: %{public}d", tipType, authType);
264 std::lock_guard<std::recursive_mutex> lock(mutex_);
265 std::vector<uint8_t> tipInfo;
266 bool getTipInfoRet = extraInfo.GetUint8ArrayValue(Attributes::ATTR_EXTRA_INFO, tipInfo);
267 IF_FALSE_LOGE_AND_RETURN(getTipInfoRet);
268 WidgetClient::Instance().ReportWidgetTip(tipType, static_cast<AuthType>(authType), tipInfo);
269 }
270
271 // WidgetScheduleNodeCallback
LaunchWidget()272 bool WidgetContext::LaunchWidget()
273 {
274 IAM_LOGI("launch widget");
275 WidgetRotatePara widgetRotatePara;
276 widgetRotatePara.isReload = false;
277 widgetRotatePara.needRotate = 0;
278 if (!ConnectExtension(widgetRotatePara)) {
279 IAM_LOGE("failed to launch widget.");
280 return false;
281 }
282 return true;
283 }
284
ExecuteAuthList(const std::set<AuthType> & authTypeList,bool endAfterFirstFail,AuthIntent authIntent)285 void WidgetContext::ExecuteAuthList(const std::set<AuthType> &authTypeList, bool endAfterFirstFail,
286 AuthIntent authIntent)
287 {
288 IAM_LOGI("execute auth list");
289 // create task, and start it
290 std::lock_guard<std::recursive_mutex> lock(mutex_);
291 for (auto &authType : authTypeList) {
292 auto task = BuildTask(para_.challenge, authType, para_.atl, endAfterFirstFail, authIntent);
293 if (task == nullptr) {
294 IAM_LOGE("failed to create task, authType: %{public}s", AuthType2Str(authType).c_str());
295 continue;
296 }
297 if (!task->Start()) {
298 IAM_LOGE("BeginAuthentication failed");
299 static const int32_t INVALID_VAL = -1;
300 WidgetClient::Instance().ReportWidgetResult(task->GetLatestError(), authType, INVALID_VAL, INVALID_VAL);
301 return;
302 }
303 if (authType == FACE) {
304 faceReload_ = 1;
305 IAM_LOGI("faceReload_: %{public}d", faceReload_);
306 }
307 TaskInfo taskInfo {
308 .authType = authType,
309 .task = task
310 };
311 runTaskInfoList_.push_back(taskInfo);
312 }
313 }
314
EndAuthAsCancel()315 void WidgetContext::EndAuthAsCancel()
316 {
317 IAM_LOGI("end auth as cancel");
318 std::lock_guard<std::recursive_mutex> lock(mutex_);
319 if (latestError_ == COMPLEXITY_CHECK_FAILED) {
320 IAM_LOGE("complexity check failed");
321 return End(TRUST_LEVEL_NOT_SUPPORT);
322 }
323 // report CANCELED to App
324 End(ResultCode::CANCELED);
325 }
326
EndAuthAsNaviPin()327 void WidgetContext::EndAuthAsNaviPin()
328 {
329 IAM_LOGI("end auth as navi pin");
330 std::lock_guard<std::recursive_mutex> lock(mutex_);
331 // report CANCELED_FROM_WIDGET to App
332 End(ResultCode::CANCELED_FROM_WIDGET);
333 }
334
EndAuthAsWidgetParaInvalid()335 void WidgetContext::EndAuthAsWidgetParaInvalid()
336 {
337 IAM_LOGI("end auth as widget para invalid");
338 std::lock_guard<std::recursive_mutex> lock(mutex_);
339 End(ResultCode::INVALID_PARAMETERS);
340 }
341
AuthWidgetReloadInit()342 void WidgetContext::AuthWidgetReloadInit()
343 {
344 IAM_LOGI("auth widget reload init");
345 std::lock_guard<std::recursive_mutex> lock(mutex_);
346 if (!DisconnectExtension()) {
347 IAM_LOGE("failed to release launch widget");
348 }
349 }
350
AuthWidgetReload(uint32_t orientation,uint32_t needRotate,uint32_t alreadyLoad,AuthType & rotateAuthType)351 bool WidgetContext::AuthWidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad,
352 AuthType &rotateAuthType)
353 {
354 IAM_LOGI("auth widget reload");
355 std::lock_guard<std::recursive_mutex> lock(mutex_);
356 WidgetRotatePara widgetRotatePara;
357 widgetRotatePara.isReload = true;
358 widgetRotatePara.orientation = orientation;
359 widgetRotatePara.needRotate = needRotate;
360 widgetRotatePara.alreadyLoad = alreadyLoad;
361 widgetRotatePara.rotateAuthType = rotateAuthType;
362 if (alreadyLoad) {
363 widgetAlreadyLoad_ = 1;
364 }
365 if (!isValidRotate(widgetRotatePara)) {
366 IAM_LOGE("check rotate failed");
367 return false;
368 }
369 if (!ConnectExtension(widgetRotatePara)) {
370 IAM_LOGE("failed to reload widget");
371 return false;
372 }
373 return true;
374 }
375
isValidRotate(const WidgetRotatePara & widgetRotatePara)376 bool WidgetContext::isValidRotate(const WidgetRotatePara &widgetRotatePara)
377 {
378 IAM_LOGI("check rotate, needRotate: %{public}u, orientation: %{public}u, orientation_: %{public}u",
379 widgetRotatePara.needRotate, widgetRotatePara.orientation, widgetRotateOrientation_);
380 if (widgetRotatePara.needRotate) {
381 IAM_LOGI("check rotate, widgetAlreadyLoad_: %{public}u", widgetAlreadyLoad_);
382 if (widgetRotatePara.orientation == ORIENTATION_PORTRAIT_INVERTED && !widgetAlreadyLoad_) {
383 IAM_LOGI("only support first");
384 return true;
385 }
386 if (widgetRotatePara.orientation > widgetRotateOrientation_ &&
387 widgetRotatePara.orientation - widgetRotateOrientation_ == NOT_SUPPORT_ORIENTATION_INVERTED) {
388 return false;
389 }
390 if (widgetRotatePara.orientation < widgetRotateOrientation_ &&
391 widgetRotateOrientation_ - widgetRotatePara.orientation == NOT_SUPPORT_ORIENTATION_INVERTED) {
392 return false;
393 }
394 }
395 return true;
396 }
397
StopAuthList(const std::vector<AuthType> & authTypeList)398 void WidgetContext::StopAuthList(const std::vector<AuthType> &authTypeList)
399 {
400 IAM_LOGI("stop auth list");
401 std::lock_guard<std::recursive_mutex> lock(mutex_);
402 for (auto &authType : authTypeList) {
403 auto it = std::find_if(runTaskInfoList_.begin(),
404 runTaskInfoList_.end(), [authType] (const TaskInfo &taskInfo) {
405 return (taskInfo.authType == authType);
406 });
407 if (it != runTaskInfoList_.end()) {
408 if (it->task == nullptr) {
409 IAM_LOGE("task is nullptr");
410 return;
411 }
412 it->task->Stop();
413 runTaskInfoList_.erase(it);
414 }
415 }
416 }
417
SuccessAuth(AuthType authType)418 void WidgetContext::SuccessAuth(AuthType authType)
419 {
420 IAM_LOGI("success auth. authType:%{public}d", static_cast<int32_t>(authType));
421 std::lock_guard<std::recursive_mutex> lock(mutex_);
422 // report success to App
423 End(ResultCode::SUCCESS);
424 }
425
ConnectExtensionAbility(const AAFwk::Want & want,const std::string commandStr)426 int32_t WidgetContext::ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr)
427 {
428 IAM_LOGI("ConnectExtensionAbility start");
429 if (connection_ != nullptr) {
430 IAM_LOGE("invalid connection_");
431 return ERR_INVALID_OPERATION;
432 }
433 connection_ = sptr<UIExtensionAbilityConnection>(new (std::nothrow) UIExtensionAbilityConnection(commandStr));
434 if (connection_ == nullptr) {
435 IAM_LOGE("new connection error.");
436 return ERR_NO_MEMORY;
437 }
438
439 std::string identity = IPCSkeleton::ResetCallingIdentity();
440 auto ret = AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection_, nullptr,
441 DEFAULT_VALUE);
442 IPCSkeleton::SetCallingIdentity(identity);
443 IAM_LOGI("ConnectExtensionAbility errCode=%{public}d", ret);
444 return ret;
445 }
446
ConnectExtension(const WidgetRotatePara & widgetRotatePara)447 bool WidgetContext::ConnectExtension(const WidgetRotatePara &widgetRotatePara)
448 {
449 if (widgetRotatePara.isReload) {
450 for (auto &authType : para_.authTypeList) {
451 ContextFactory::AuthProfile profile;
452 if (!AuthWidgetHelper::GetUserAuthProfile(para_.userId, authType, profile)) {
453 IAM_LOGE("get user authType:%{public}d profile failed", static_cast<int32_t>(authType));
454 return false;
455 }
456 para_.authProfileMap[authType] = profile;
457 }
458 }
459 std::string tmp = BuildStartCommand(widgetRotatePara);
460 IAM_LOGI("start command: %{public}s", tmp.c_str());
461
462 AAFwk::Want want;
463 std::string bundleName = "com.ohos.systemui";
464 std::string abilityName = "com.ohos.systemui.dialog";
465 want.SetElementName(bundleName, abilityName);
466 auto ret = ConnectExtensionAbility(want, tmp);
467 if (ret != ERR_OK) {
468 UserIam::UserAuth::ReportSystemFault(Common::GetNowTimeString(), "userauthservice");
469 IAM_LOGE("ConnectExtensionAbility failed.");
470 return false;
471 }
472 return true;
473 }
474
DisconnectExtension()475 bool WidgetContext::DisconnectExtension()
476 {
477 if (connection_ == nullptr) {
478 IAM_LOGE("invalid connection handle");
479 return false;
480 }
481 WidgetClient::Instance().ForceStopAuth();
482 connection_->ReleaseUIExtensionComponent();
483 ErrCode ret = AAFwk::ExtensionManagerClient::GetInstance().DisconnectAbility(connection_);
484 connection_ = nullptr;
485 if (ret != ERR_OK) {
486 IAM_LOGE("disconnect extension ability failed ret: %{public}d.", ret);
487 return false;
488 }
489 return true;
490 }
491
End(const ResultCode & resultCode)492 void WidgetContext::End(const ResultCode &resultCode)
493 {
494 IAM_LOGI("in End, resultCode: %{public}d", static_cast<int32_t>(resultCode));
495 StopAllRunTask(resultCode);
496 IF_FALSE_LOGE_AND_RETURN(callerCallback_ != nullptr);
497 Attributes attr;
498 if (resultCode == ResultCode::SUCCESS) {
499 if (!attr.SetInt32Value(Attributes::ATTR_AUTH_TYPE, authResultInfo_.authType)) {
500 IAM_LOGE("set auth type failed.");
501 callerCallback_->OnResult(ResultCode::GENERAL_ERROR, attr);
502 return;
503 }
504 if (authResultInfo_.token.size() > 0) {
505 if (!attr.SetUint8ArrayValue(Attributes::ATTR_SIGNATURE, authResultInfo_.token)) {
506 IAM_LOGE("set signature token failed.");
507 callerCallback_->OnResult(ResultCode::GENERAL_ERROR, attr);
508 return;
509 }
510 }
511 if (!attr.SetUint64Value(Attributes::ATTR_CREDENTIAL_DIGEST, authResultInfo_.credentialDigest)) {
512 IAM_LOGE("set credential digest failed.");
513 callerCallback_->OnResult(ResultCode::GENERAL_ERROR, attr);
514 return;
515 }
516 if (!attr.SetUint16Value(Attributes::ATTR_CREDENTIAL_COUNT, authResultInfo_.credentialCount)) {
517 IAM_LOGE("set credential count failed.");
518 callerCallback_->OnResult(ResultCode::GENERAL_ERROR, attr);
519 return;
520 }
521 }
522 callerCallback_->OnResult(resultCode, attr);
523 }
524
StopAllRunTask(const ResultCode & resultCode)525 void WidgetContext::StopAllRunTask(const ResultCode &resultCode)
526 {
527 std::lock_guard<std::recursive_mutex> lock(mutex_);
528 WidgetClient::Instance().Reset();
529 for (auto &taskInfo : runTaskInfoList_) {
530 IAM_LOGI("stop task");
531 if (taskInfo.task == nullptr) {
532 IAM_LOGE("task is null");
533 continue;
534 }
535 taskInfo.task->Stop();
536 }
537 runTaskInfoList_.clear();
538 if (resultCode != ResultCode::SUCCESS) {
539 IAM_LOGI("Try to disconnect extesnion");
540 if (!DisconnectExtension()) {
541 IAM_LOGE("failed to release launch widget.");
542 }
543 }
544 }
545
BuildStartCommand(const WidgetRotatePara & widgetRotatePara)546 std::string WidgetContext::BuildStartCommand(const WidgetRotatePara &widgetRotatePara)
547 {
548 WidgetCmdParameters widgetCmdParameters;
549 widgetCmdParameters.uiExtensionType = UI_EXTENSION_TYPE_SET;
550 widgetCmdParameters.useriamCmdData.widgetContextId = GetContextId();
551 widgetCmdParameters.useriamCmdData.title = para_.widgetParam.title;
552 widgetCmdParameters.useriamCmdData.windowModeType = WinModeType2Str(para_.widgetParam.windowMode);
553 widgetCmdParameters.useriamCmdData.navigationButtonText = para_.widgetParam.navigationButtonText;
554 auto it = para_.authProfileMap.find(AuthType::PIN);
555 if (it != para_.authProfileMap.end()) {
556 widgetCmdParameters.useriamCmdData.pinSubType = PinSubType2Str(static_cast<PinSubType>(it->second.pinSubType));
557 }
558 widgetCmdParameters.sysDialogZOrder = SYSDIALOG_ZORDER_DEFAULT;
559 if (ContextAppStateObserverManager::GetInstance().GetScreenLockState()) {
560 IAM_LOGI("the screen is currently locked, set zOrder");
561 widgetCmdParameters.sysDialogZOrder = SYSDIALOG_ZORDER_UPPER;
562 }
563 std::vector<std::string> typeList;
564 for (auto &item : para_.authProfileMap) {
565 auto &at = item.first;
566 auto &profile = item.second;
567 typeList.push_back(AuthType2Str(at));
568 WidgetCommand::Cmd cmd {
569 .event = CMD_NOTIFY_AUTH_START,
570 .version = NOTICE_VERSION_STR,
571 .type = AuthType2Str(at)
572 };
573 if (at == AuthType::FINGERPRINT && !profile.sensorInfo.empty()) {
574 cmd.sensorInfo = profile.sensorInfo;
575 }
576 if (para_.isPinExpired) {
577 cmd.result = PIN_EXPIRED;
578 }
579 cmd.remainAttempts = profile.remainTimes;
580 cmd.lockoutDuration = profile.freezingTime;
581 widgetCmdParameters.useriamCmdData.cmdList.push_back(cmd);
582 }
583 widgetCmdParameters.useriamCmdData.typeList = typeList;
584 widgetCmdParameters.useriamCmdData.callingAppID = para_.callingAppID;
585 ProcessRotatePara(widgetCmdParameters, widgetRotatePara);
586 nlohmann::json root = widgetCmdParameters;
587 std::string cmdData = root.dump();
588 return cmdData;
589 }
590
ProcessRotatePara(WidgetCmdParameters & widgetCmdParameters,const WidgetRotatePara & widgetRotatePara)591 void WidgetContext::ProcessRotatePara(WidgetCmdParameters &widgetCmdParameters,
592 const WidgetRotatePara &widgetRotatePara)
593 {
594 if (widgetRotatePara.isReload) {
595 widgetCmdParameters.useriamCmdData.isReload = 1;
596 if (widgetRotatePara.rotateAuthType == FACE) {
597 widgetCmdParameters.useriamCmdData.isReload = faceReload_;
598 }
599 widgetCmdParameters.useriamCmdData.rotateAuthType = AuthType2Str(widgetRotatePara.rotateAuthType);
600 }
601 IAM_LOGI("needRotate: %{public}u, orientation: %{public}u", widgetRotatePara.needRotate,
602 widgetRotatePara.orientation);
603 widgetRotateOrientation_ = widgetRotatePara.orientation;
604 if (widgetRotatePara.needRotate) {
605 if (widgetRotatePara.orientation == ORIENTATION_LANDSCAPE) {
606 widgetCmdParameters.uiExtNodeAngle = TO_PORTRAIT;
607 }
608 if (widgetRotatePara.orientation == ORIENTATION_PORTRAIT_INVERTED) {
609 widgetCmdParameters.uiExtNodeAngle = TO_INVERTED;
610 }
611 if (widgetRotatePara.orientation == ORIENTATION_LANDSCAPE_INVERTED) {
612 widgetCmdParameters.uiExtNodeAngle = TO_PORTRAIT_INVERTED;
613 }
614 }
615 }
616
GetCallingBundleName()617 std::string WidgetContext::GetCallingBundleName()
618 {
619 if (para_.callerType == Security::AccessToken::TOKEN_HAP) {
620 return para_.callerName;
621 }
622 return "";
623 }
624 } // namespace UserAuth
625 } // namespace UserIam
626 } // namespace OHOS
627