1 /*
2 * Copyright (c) 2023 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 "core/components_ng/pattern/security_component/security_component_pattern.h"
17 #include "base/log/ace_scoring_log.h"
18 #include "core/components_ng/pattern/button/button_layout_property.h"
19 #include "core/components_ng/pattern/button/button_pattern.h"
20 #include "core/components_ng/pattern/image/image_pattern.h"
21 #ifdef SECURITY_COMPONENT_ENABLE
22 #include "core/components_ng/pattern/security_component/security_component_handler.h"
23 #endif
24 #include "core/components_ng/pattern/security_component/security_component_theme.h"
25 #include "core/components_ng/pattern/text/text_layout_property.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/components_v2/inspector/inspector_constants.h"
28 #ifdef SECURITY_COMPONENT_ENABLE
29 #include "pointer_event.h"
30 #endif
31
32 namespace OHOS::Ace::NG {
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)33 bool SecurityComponentPattern::OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty,
34 const DirtySwapConfig& config)
35 {
36 return !(config.skipMeasure || dirty->SkipMeasureContent());
37 }
38
SetNodeHitTestMode(RefPtr<FrameNode> & node,HitTestMode mode)39 void SecurityComponentPattern::SetNodeHitTestMode(RefPtr<FrameNode>& node, HitTestMode mode)
40 {
41 if (node == nullptr) {
42 return;
43 }
44 auto gestureHub = node->GetOrCreateGestureEventHub();
45 CHECK_NULL_VOID(gestureHub);
46 gestureHub->SetHitTestMode(mode);
47 }
48
OnKeyEvent(const KeyEvent & event)49 bool SecurityComponentPattern::OnKeyEvent(const KeyEvent& event)
50 {
51 if (event.action != KeyAction::DOWN) {
52 return false;
53 }
54 if ((event.code == KeyCode::KEY_SPACE) || (event.code == KeyCode::KEY_ENTER)) {
55 auto frameNode = GetHost();
56 CHECK_NULL_RETURN(frameNode, false);
57 int32_t res = 1;
58 #ifdef SECURITY_COMPONENT_ENABLE
59 res = ReportSecurityComponentClickEvent(event);
60 if (res != 0) {
61 LOGE("ReportSecurityComponentClickEvent failed, errno %{public}d", res);
62 res = 1;
63 }
64 #endif
65 auto jsonNode = JsonUtil::Create(true);
66 jsonNode->Put("handleRes", res);
67 std::shared_ptr<JsonValue> jsonShrd(jsonNode.release());
68 auto gestureEventHub = frameNode->GetOrCreateGestureEventHub();
69 gestureEventHub->ActClick(jsonShrd);
70 return true;
71 }
72 return false;
73 }
74
InitOnKeyEvent(RefPtr<FrameNode> & secCompNode)75 void SecurityComponentPattern::InitOnKeyEvent(RefPtr<FrameNode>& secCompNode)
76 {
77 if (isSetOnKeyEvent) {
78 return;
79 }
80 auto focusHub = secCompNode->GetOrCreateFocusHub();
81 auto onKeyEvent = [wp = WeakClaim(this)](const KeyEvent& event) -> bool {
82 auto pattern = wp.Upgrade();
83 if (!pattern) {
84 return false;
85 }
86 return pattern->OnKeyEvent(event);
87 };
88 focusHub->SetOnKeyEventInternal(std::move(onKeyEvent));
89 isSetOnKeyEvent = true;
90 }
91
IsParentMenu(RefPtr<FrameNode> & secCompNode)92 bool SecurityComponentPattern::IsParentMenu(RefPtr<FrameNode>& secCompNode)
93 {
94 auto parent = secCompNode->GetParent();
95 while (parent != nullptr) {
96 if (parent->GetTag() == V2::MENU_WRAPPER_ETS_TAG) {
97 return true;
98 }
99 parent = parent->GetParent();
100 }
101 return false;
102 }
103
HandleClickEventFromTouch(const TouchEventInfo & info)104 void SecurityComponentPattern::HandleClickEventFromTouch(const TouchEventInfo& info)
105 {
106 #ifdef SECURITY_COMPONENT_ENABLE
107 auto host = GetHost();
108 CHECK_NULL_VOID(host);
109
110 if (!IsParentMenu(host)) {
111 return;
112 }
113
114 auto pointerEvent = info.GetPointerEvent();
115 CHECK_NULL_VOID(pointerEvent);
116
117 int32_t pointerId = pointerEvent->GetPointerId();
118 MMI::PointerEvent::PointerItem item;
119 if (!pointerEvent->GetPointerItem(pointerId, item)) {
120 LOGW("Get pointer item failed");
121 return;
122 }
123
124 GestureEvent gestureInfo;
125 gestureInfo.SetDisplayX(item.GetDisplayX());
126 gestureInfo.SetDisplayY(item.GetDisplayY());
127 gestureInfo.SetPointerEvent(info.GetPointerEvent());
128 int res = ReportSecurityComponentClickEvent(gestureInfo);
129 if (res != 0) {
130 LOGW("ReportSecurityComponentClickEvent failed, errno %{public}d", res);
131 res = 1;
132 }
133 auto jsonNode = JsonUtil::Create(true);
134 jsonNode->Put("handleRes", res);
135 std::shared_ptr<JsonValue> jsonShrd(jsonNode.release());
136 auto gestureEventHub = host->GetOrCreateGestureEventHub();
137 gestureEventHub->ActClick(jsonShrd);
138 #endif
139 }
140
141 // When security component is a child node of menu wrapper, the menu is immediately hidden
142 // after being touched, and then the security component will trigger a click event.
143 // However, it will be considered to have been triggered in a hidden state,
144 // Therefore, we should report click event on UP touch event.
OnTouch(const TouchEventInfo & info)145 void SecurityComponentPattern::OnTouch(const TouchEventInfo& info)
146 {
147 auto touchType = info.GetTouches().front().GetTouchType();
148 if (touchType == TouchType::DOWN) {
149 lastTouchOffset_ = std::make_unique<Offset>(info.GetTouches().front().GetLocalLocation());
150 } else if (touchType == TouchType::UP) {
151 auto touchUpOffset = info.GetTouches().front().GetLocalLocation();
152 if (lastTouchOffset_ &&
153 (touchUpOffset - *lastTouchOffset_).GetDistance() <= DEFAULT_SECURITY_COMPONENT_CLICK_DISTANCE) {
154 HandleClickEventFromTouch(info);
155 }
156 lastTouchOffset_.reset();
157 }
158 }
159
InitOnTouch(RefPtr<FrameNode> & secCompNode)160 void SecurityComponentPattern::InitOnTouch(RefPtr<FrameNode>& secCompNode)
161 {
162 if (onTouchListener_ != nullptr) {
163 return;
164 }
165 auto gestureHub = secCompNode->GetOrCreateGestureEventHub();
166 CHECK_NULL_VOID(gestureHub);
167
168 auto touchCallback = [weak = WeakClaim(this)](const TouchEventInfo& info) {
169 auto pattern = weak.Upgrade();
170 CHECK_NULL_VOID(pattern);
171 pattern->OnTouch(info);
172 };
173 onTouchListener_ = MakeRefPtr<TouchEventImpl>(std::move(touchCallback));
174 gestureHub->AddTouchEvent(onTouchListener_);
175 }
176
InitOnClick(RefPtr<FrameNode> & secCompNode,RefPtr<FrameNode> & icon,RefPtr<FrameNode> & text,RefPtr<FrameNode> & button)177 void SecurityComponentPattern::InitOnClick(RefPtr<FrameNode>& secCompNode, RefPtr<FrameNode>& icon,
178 RefPtr<FrameNode>& text, RefPtr<FrameNode>& button)
179 {
180 if (clickListener_ != nullptr) {
181 return;
182 }
183 auto secCompGesture = secCompNode->GetOrCreateGestureEventHub();
184 CHECK_NULL_VOID(secCompGesture);
185 auto clickCallback = [weak = WeakClaim(this)](GestureEvent& info) {
186 #ifdef SECURITY_COMPONENT_ENABLE
187 auto buttonPattern = weak.Upgrade();
188 CHECK_NULL_VOID(buttonPattern);
189 auto frameNode = buttonPattern->GetHost();
190 CHECK_NULL_VOID(frameNode);
191 if (info.GetSecCompHandleEvent()) {
192 return;
193 }
194 auto jsonNode = JsonUtil::Create(true);
195 std::shared_ptr<JsonValue> jsonShrd(jsonNode.release());
196 int32_t res;
197 if (buttonPattern->IsParentMenu(frameNode)) {
198 res = static_cast<int32_t>(SecurityComponentHandleResult::DROP_CLICK);
199 } else {
200 res = buttonPattern->ReportSecurityComponentClickEvent(info);
201 if (res != 0) {
202 LOGW("ReportSecurityComponentClickEvent failed, errno %{public}d", res);
203 res = static_cast<int32_t>(SecurityComponentHandleResult::CLICK_GRANT_FAILED);
204 }
205 }
206 jsonShrd->Put("handleRes", res);
207 info.SetSecCompHandleEvent(jsonShrd);
208 #endif
209 };
210
211 clickListener_ = MakeRefPtr<ClickEvent>(std::move(clickCallback));
212 secCompGesture->AddClickEvent(clickListener_);
213 SetNodeHitTestMode(icon, HitTestMode::HTMTRANSPARENT);
214 SetNodeHitTestMode(text, HitTestMode::HTMTRANSPARENT);
215 }
216
ToJsonValue(std::unique_ptr<JsonValue> & json) const217 void SecurityComponentPattern::ToJsonValue(std::unique_ptr<JsonValue>& json) const
218 {
219 auto node = GetHost();
220 CHECK_NULL_VOID(node);
221
222 auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
223 CHECK_NULL_VOID(layoutProperty);
224 json->Put("text", layoutProperty->GetSecurityComponentDescription().value_or(0));
225 json->Put("icon", layoutProperty->GetIconStyle().value_or(0));
226 json->Put("buttonType", layoutProperty->GetBackgroundType().value_or(0));
227 json->Put("layoutDirection", static_cast<int64_t>(
228 layoutProperty->GetTextIconLayoutDirection().value_or(SecurityComponentLayoutDirection::VERTICAL)));
229 json->Put("type", node->GetTag().c_str());
230
231 RefPtr<FrameNode> iconNode = GetSecCompChildNode(node, V2::IMAGE_ETS_TAG);
232 if (iconNode != nullptr) {
233 auto iconProp = iconNode->GetLayoutProperty<ImageLayoutProperty>();
234 CHECK_NULL_VOID(iconProp);
235 json->Put("iconSize",
236 iconProp->GetCalcLayoutConstraint()->selfIdealSize->Width()->GetDimension().ToString().c_str());
237 json->Put("iconColor",
238 iconProp->GetImageSourceInfo().value().GetFillColor().value_or(Color::WHITE).ColorToString().c_str());
239 }
240 RefPtr<FrameNode> textNode = GetSecCompChildNode(node, V2::TEXT_ETS_TAG);
241 if (textNode != nullptr) {
242 auto textProp = textNode->GetLayoutProperty<TextLayoutProperty>();
243 CHECK_NULL_VOID(textProp);
244 json->Put("fontSize", textProp->GetFontSize().value_or(Dimension(0.0)).ToString().c_str());
245 json->Put("fontWeight",
246 V2::ConvertWrapFontWeightToStirng(textProp->GetFontWeight().value_or(FontWeight::NORMAL)).c_str());
247 json->Put("fontFamily", "HarmonyOS Sans");
248 json->Put("fontStyle", static_cast<int64_t>(textProp->GetItalicFontStyle().value_or(Ace::FontStyle::NORMAL)));
249 json->Put("fontColor", textProp->GetTextColor().value_or(Color::WHITE).ColorToString().c_str());
250 }
251 auto paddingJson = JsonUtil::Create(true);
252 paddingJson->Put("top", layoutProperty->GetBackgroundTopPadding().value_or(Dimension(0.0)).ToString().c_str());
253 paddingJson->Put("bottom",
254 layoutProperty->GetBackgroundBottomPadding().value_or(Dimension(0.0)).ToString().c_str());
255 paddingJson->Put("left", layoutProperty->GetBackgroundLeftPadding().value_or(Dimension(0.0)).ToString().c_str());
256 paddingJson->Put("right", layoutProperty->GetBackgroundRightPadding().value_or(Dimension(0.0)).ToString().c_str());
257 json->Put("padding", paddingJson);
258 json->Put("textIconSpace", layoutProperty->GetTextIconSpace().value_or(Dimension(0.0)).ToString().c_str());
259 ToJsonValueRect(json);
260 }
261
ToJsonValueRect(std::unique_ptr<JsonValue> & json) const262 void SecurityComponentPattern::ToJsonValueRect(std::unique_ptr<JsonValue>& json) const
263 {
264 auto node = GetHost();
265 CHECK_NULL_VOID(node);
266
267 RefPtr<FrameNode> buttonNode = GetSecCompChildNode(node, V2::BUTTON_ETS_TAG);
268 if (buttonNode != nullptr) {
269 const auto& renderContext = buttonNode->GetRenderContext();
270 CHECK_NULL_VOID(renderContext);
271 json->Put("backgroundColor", renderContext->GetBackgroundColor().value().ColorToString().c_str());
272 json->Put("borderColor",
273 renderContext->GetBorderColor()->leftColor.value_or(Color::BLACK).ColorToString().c_str());
274 json->Put("borderStyle",
275 static_cast<int>(renderContext->GetBorderStyle()->styleLeft.value_or(BorderStyle::NONE)));
276 auto bgProp = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
277 CHECK_NULL_VOID(bgProp);
278 const auto& borderWidth = bgProp->GetBorderWidthProperty();
279 if (borderWidth != nullptr) {
280 json->Put("borderWidth", borderWidth->leftDimen.value_or(Dimension(0.0)).ToString().c_str());
281 }
282 auto borderRadius = bgProp->GetBorderRadius();
283 if (borderRadius.has_value()) {
284 json->Put("borderRadius",
285 borderRadius->radiusTopLeft.value_or(Dimension(0.0, DimensionUnit::VP)).ToString().c_str());
286 } else {
287 json->Put("borderRadius", "0.00vp");
288 }
289 }
290 }
291
GetFocusPattern() const292 FocusPattern SecurityComponentPattern::GetFocusPattern() const
293 {
294 auto frameNode = GetHost();
295 RefPtr<FrameNode> buttonNode = GetSecCompChildNode(frameNode, V2::BUTTON_ETS_TAG);
296 if (buttonNode != nullptr) {
297 auto buttonPattern = buttonNode->GetPattern<ButtonPattern>();
298 return buttonPattern->GetFocusPattern();
299 }
300
301 return { FocusType::NODE, true, FocusStyleType::OUTER_BORDER };
302 }
303
UpdateIconProperty(RefPtr<FrameNode> & scNode,RefPtr<FrameNode> & iconNode)304 void SecurityComponentPattern::UpdateIconProperty(RefPtr<FrameNode>& scNode, RefPtr<FrameNode>& iconNode)
305 {
306 auto iconLayoutProp = iconNode->GetLayoutProperty<ImageLayoutProperty>();
307 auto scLayoutProp = scNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
308 CHECK_NULL_VOID(scLayoutProp);
309 if (scLayoutProp->GetIconSize().has_value()) {
310 auto iconSize = scLayoutProp->GetIconSize().value();
311 iconLayoutProp->UpdateUserDefinedIdealSize(CalcSize(NG::CalcLength(iconSize), NG::CalcLength(iconSize)));
312 }
313
314 auto scPaintProp = scNode->GetPaintProperty<SecurityComponentPaintProperty>();
315 CHECK_NULL_VOID(scPaintProp);
316 if (scPaintProp->GetIconColor().has_value()) {
317 auto iconSrcInfo = iconLayoutProp->GetImageSourceInfo().value();
318 iconSrcInfo.SetFillColor(scPaintProp->GetIconColor().value());
319 iconLayoutProp->UpdateImageSourceInfo(iconSrcInfo);
320 }
321 }
322
UpdateTextProperty(RefPtr<FrameNode> & scNode,RefPtr<FrameNode> & textNode)323 void SecurityComponentPattern::UpdateTextProperty(RefPtr<FrameNode>& scNode, RefPtr<FrameNode>& textNode)
324 {
325 auto scLayoutProp = scNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
326 auto textLayoutProp = textNode->GetLayoutProperty<TextLayoutProperty>();
327 if (scLayoutProp->GetFontSize().has_value()) {
328 textLayoutProp->UpdateFontSize(scLayoutProp->GetFontSize().value());
329 }
330 if (scLayoutProp->GetFontStyle().has_value()) {
331 textLayoutProp->UpdateItalicFontStyle(scLayoutProp->GetFontStyle().value());
332 }
333 if (scLayoutProp->GetFontWeight().has_value()) {
334 textLayoutProp->UpdateFontWeight(scLayoutProp->GetFontWeight().value());
335 }
336 if (scLayoutProp->GetFontFamily().has_value()) {
337 textLayoutProp->UpdateFontFamily(scLayoutProp->GetFontFamily().value());
338 }
339 auto scPaintProp = scNode->GetPaintProperty<SecurityComponentPaintProperty>();
340 if (scPaintProp->GetFontColor().has_value()) {
341 textLayoutProp->UpdateTextColor(scPaintProp->GetFontColor().value());
342 }
343 }
344
UpdateButtonProperty(RefPtr<FrameNode> & scNode,RefPtr<FrameNode> & buttonNode)345 void SecurityComponentPattern::UpdateButtonProperty(RefPtr<FrameNode>& scNode, RefPtr<FrameNode>& buttonNode)
346 {
347 auto scLayoutProp = scNode->GetLayoutProperty<SecurityComponentLayoutProperty>();
348 auto scPaintProp = scNode->GetPaintProperty<SecurityComponentPaintProperty>();
349 auto buttonLayoutProp = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
350 const auto& buttonRender = buttonNode->GetRenderContext();
351 CHECK_NULL_VOID(buttonRender);
352
353 if (scLayoutProp->GetBackgroundBorderWidth().has_value()) {
354 BorderWidthProperty widthProp;
355 widthProp.SetBorderWidth(scLayoutProp->GetBackgroundBorderWidth().value());
356 buttonLayoutProp->UpdateBorderWidth(widthProp);
357 }
358
359 if (scPaintProp->GetBackgroundBorderStyle().has_value()) {
360 BorderStyleProperty style;
361 style.SetBorderStyle(scPaintProp->GetBackgroundBorderStyle().value());
362 buttonRender->UpdateBorderStyle(style);
363 }
364 if (scLayoutProp->GetBackgroundBorderRadius().has_value()) {
365 buttonLayoutProp->UpdateBorderRadius(
366 BorderRadiusProperty(scLayoutProp->GetBackgroundBorderRadius().value()));
367 }
368 if (scPaintProp->GetBackgroundColor().has_value()) {
369 buttonRender->UpdateBackgroundColor(scPaintProp->GetBackgroundColor().value());
370 }
371 if (scPaintProp->GetBackgroundBorderColor().has_value()) {
372 BorderColorProperty borderColor;
373 borderColor.SetColor(scPaintProp->GetBackgroundBorderColor().value());
374 buttonRender->UpdateBorderColor(borderColor);
375 }
376 }
377
OnModifyDone()378 void SecurityComponentPattern::OnModifyDone()
379 {
380 auto frameNode = GetHost();
381 CHECK_NULL_VOID(frameNode);
382
383 RefPtr<FrameNode> iconNode = GetSecCompChildNode(frameNode, V2::IMAGE_ETS_TAG);
384 if (iconNode != nullptr) {
385 UpdateIconProperty(frameNode, iconNode);
386 iconNode->MarkModifyDone();
387 }
388
389 RefPtr<FrameNode> textNode = GetSecCompChildNode(frameNode, V2::TEXT_ETS_TAG);
390 if (textNode != nullptr) {
391 UpdateTextProperty(frameNode, textNode);
392 textNode->MarkModifyDone();
393 }
394
395 RefPtr<FrameNode> buttonNode = GetSecCompChildNode(frameNode, V2::BUTTON_ETS_TAG);
396 if (buttonNode != nullptr) {
397 UpdateButtonProperty(frameNode, buttonNode);
398 buttonNode->MarkModifyDone();
399 }
400
401 InitOnClick(frameNode, iconNode, textNode, buttonNode);
402 InitOnKeyEvent(frameNode);
403 InitAppearCallback(frameNode);
404 InitOnTouch(frameNode);
405 #ifdef SECURITY_COMPONENT_ENABLE
406 RegisterSecurityComponentAsync();
407 #endif
408 }
409
InitAppearCallback(RefPtr<FrameNode> & frameNode)410 void SecurityComponentPattern::InitAppearCallback(RefPtr<FrameNode>& frameNode)
411 {
412 if (isAppearCallback_) {
413 return;
414 }
415 auto eventHub = frameNode->GetEventHub<EventHub>();
416 CHECK_NULL_VOID(eventHub);
417
418 auto onAppear = [weak = WeakClaim(this)]() {
419 #ifdef SECURITY_COMPONENT_ENABLE
420 auto securityComponentPattern = weak.Upgrade();
421 CHECK_NULL_VOID(securityComponentPattern);
422 securityComponentPattern->isAppear_ = true;
423 securityComponentPattern->RegisterSecurityComponentAsync();
424 #endif
425 };
426
427 auto onDisAppear = [weak = WeakClaim(this)]() {
428 #ifdef SECURITY_COMPONENT_ENABLE
429 auto securityComponentPattern = weak.Upgrade();
430 CHECK_NULL_VOID(securityComponentPattern);
431 securityComponentPattern->isAppear_ = false;
432 securityComponentPattern->UnregisterSecurityComponent();
433 #endif
434 };
435 eventHub->SetOnAppear(std::move(onAppear));
436 eventHub->SetOnDisappear(std::move(onDisAppear));
437 isAppearCallback_ = true;
438 }
439
OnWindowHide()440 void SecurityComponentPattern::OnWindowHide()
441 {
442 #ifdef SECURITY_COMPONENT_ENABLE
443 UnregisterSecurityComponent();
444 #endif
445 }
446
OnWindowShow()447 void SecurityComponentPattern::OnWindowShow()
448 {
449 #ifdef SECURITY_COMPONENT_ENABLE
450 if (!isAppear_) {
451 return;
452 }
453 RegisterSecurityComponentAsync();
454 #endif
455 }
456
457 #ifdef SECURITY_COMPONENT_ENABLE
RegisterSecurityComponentAsync()458 void SecurityComponentPattern::RegisterSecurityComponentAsync()
459 {
460 if (regStatus_ == SecurityComponentRegisterStatus::REGISTERED ||
461 regStatus_ == SecurityComponentRegisterStatus::REGISTERING) {
462 return;
463 }
464
465 regMutex_.lock();
466 regStatus_ = SecurityComponentRegisterStatus::REGISTERING;
467 auto scTaskExecutor =
468 SingleTaskExecutor::Make(PipelineContext::GetCurrentContext()->GetTaskExecutor(),
469 TaskExecutor::TaskType::BACKGROUND);
470 scTaskExecutor.PostTask([weak = WeakClaim(this)] {
471 auto pattern = weak.Upgrade();
472 CHECK_NULL_VOID(pattern);
473 auto frameNode = pattern->GetHost();
474 // service is shutdowning, try to load it.
475 int32_t retryCount = MAX_RETRY_TIMES;
476 while (retryCount > 0) {
477 int32_t res = SecurityComponentHandler::RegisterSecurityComponent(frameNode, pattern->scId_);
478 if (res == Security::SecurityComponent::SCErrCode::SC_OK) {
479 pattern->regStatus_ = SecurityComponentRegisterStatus::REGISTERED;
480 pattern->regMutex_.unlock();
481 return;
482 } else if (res != Security::SecurityComponent::SCErrCode::SC_SERVICE_ERROR_SERVICE_NOT_EXIST) {
483 pattern->regStatus_ = SecurityComponentRegisterStatus::UNREGISTERED;
484 pattern->regMutex_.unlock();
485 return;
486 }
487
488 retryCount--;
489 std::this_thread::sleep_for(std::chrono::milliseconds(REGISTER_RETRY_INTERVAL));
490 }
491 pattern->regStatus_ = SecurityComponentRegisterStatus::UNREGISTERED;
492 pattern->regMutex_.unlock();
493 });
494 }
495
UnregisterSecurityComponent()496 void SecurityComponentPattern::UnregisterSecurityComponent()
497 {
498 if (regStatus_ == SecurityComponentRegisterStatus::UNREGISTERED) {
499 return;
500 }
501 if (regStatus_ == SecurityComponentRegisterStatus::REGISTERED) {
502 SecurityComponentHandler::UnregisterSecurityComponent(scId_);
503 scId_ = -1;
504 regStatus_ = SecurityComponentRegisterStatus::UNREGISTERED;
505 return;
506 }
507
508 if (!regMutex_.try_lock_for(std::chrono::milliseconds(MAX_REGISTER_WAITING_TIME))) {
509 LOGW("register task timeout.");
510 return;
511 }
512 SecurityComponentHandler::UnregisterSecurityComponent(scId_);
513 scId_ = -1;
514 regStatus_ = SecurityComponentRegisterStatus::UNREGISTERED;
515 regMutex_.unlock();
516 }
517
ReportSecurityComponentClickEvent(GestureEvent & event)518 int32_t SecurityComponentPattern::ReportSecurityComponentClickEvent(GestureEvent& event)
519 {
520 if (regStatus_ == SecurityComponentRegisterStatus::UNREGISTERED) {
521 LOGW("ClickEventHandler: security component has not registered.");
522 return -1;
523 }
524 auto frameNode = GetHost();
525 CHECK_NULL_RETURN(frameNode, -1);
526 if (regStatus_ == SecurityComponentRegisterStatus::REGISTERED) {
527 return SecurityComponentHandler::ReportSecurityComponentClickEvent(scId_,
528 frameNode, event);
529 }
530
531 if (!regMutex_.try_lock_for(std::chrono::milliseconds(MAX_REGISTER_WAITING_TIME))) {
532 LOGW("ClickEventHandler: wait for register task timeout.");
533 return -1;
534 }
535 int32_t res = SecurityComponentHandler::ReportSecurityComponentClickEvent(scId_,
536 frameNode, event);
537 regMutex_.unlock();
538 return res;
539 }
540
ReportSecurityComponentClickEvent(const KeyEvent & event)541 int32_t SecurityComponentPattern::ReportSecurityComponentClickEvent(const KeyEvent& event)
542 {
543 if (regStatus_ == SecurityComponentRegisterStatus::UNREGISTERED) {
544 LOGW("KeyEventHandler: security component has not registered.");
545 return -1;
546 }
547 auto frameNode = GetHost();
548 CHECK_NULL_RETURN(frameNode, -1);
549 if (regStatus_ == SecurityComponentRegisterStatus::REGISTERED) {
550 return SecurityComponentHandler::ReportSecurityComponentClickEvent(scId_,
551 frameNode, event);
552 }
553
554 if (!regMutex_.try_lock_for(std::chrono::milliseconds(MAX_REGISTER_WAITING_TIME))) {
555 LOGW("KeyEventHandler: wait for register task timeout.");
556 return -1;
557 }
558 int32_t res = SecurityComponentHandler::ReportSecurityComponentClickEvent(scId_,
559 frameNode, event);
560 regMutex_.unlock();
561 return res;
562 }
563 #endif
564 } // namespace OHOS::Ace::NG
565