• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/image/image_pattern.h"
20 #include "core/components_ng/pattern/security_component/security_component_handler.h"
21 #include "core/components_ng/pattern/security_component/security_component_theme.h"
22 #include "core/components_ng/pattern/text/text_layout_property.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_v2/inspector/inspector_constants.h"
25 
26 namespace OHOS::Ace::NG {
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)27 bool SecurityComponentPattern::OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty,
28     const DirtySwapConfig& config)
29 {
30     return !(config.skipMeasure || dirty->SkipMeasureContent());
31 }
32 
SetNodeHitTestMode(RefPtr<FrameNode> & node,HitTestMode mode)33 void SecurityComponentPattern::SetNodeHitTestMode(RefPtr<FrameNode>& node, HitTestMode mode)
34 {
35     if (node == nullptr) {
36         return;
37     }
38     auto gestureHub = node->GetOrCreateGestureEventHub();
39     CHECK_NULL_VOID(gestureHub);
40     gestureHub->SetHitTestMode(mode);
41 }
42 
InitSecurityComponentOnClick(RefPtr<FrameNode> & secCompNode,RefPtr<FrameNode> & icon,RefPtr<FrameNode> & text,RefPtr<FrameNode> & button)43 void SecurityComponentPattern::InitSecurityComponentOnClick(RefPtr<FrameNode>& secCompNode, RefPtr<FrameNode>& icon,
44     RefPtr<FrameNode>& text, RefPtr<FrameNode>& button)
45 {
46     if (clickListener_ != nullptr) {
47         return;
48     }
49     auto secCompGesture = secCompNode->GetOrCreateGestureEventHub();
50     CHECK_NULL_VOID(secCompGesture);
51     auto clickCallback = [weak = WeakClaim(this)](GestureEvent& info) {
52         auto buttonPattern = weak.Upgrade();
53         CHECK_NULL_VOID(buttonPattern);
54         auto frameNode = buttonPattern->GetHost();
55         CHECK_NULL_VOID(frameNode);
56         int32_t res = SecurityComponentHandler::ReportSecurityComponentClickEvent(buttonPattern->scId_,
57             frameNode, info);
58         if (res != 0) {
59             LOGE("ReportSecurityComponentClickEvent failed, errno %{public}d", res);
60             res = 1;
61         }
62 #ifdef SECURITY_COMPONENT_ENABLE
63         auto jsonNode = JsonUtil::Create(true);
64         jsonNode->Put("handleRes", res);
65         std::shared_ptr<JsonValue> jsonShrd(jsonNode.release());
66         info.SetSecCompHandleEvent(jsonShrd);
67 #endif
68     };
69 
70     clickListener_ = MakeRefPtr<ClickEvent>(std::move(clickCallback));
71     secCompGesture->AddClickEvent(clickListener_);
72     SetNodeHitTestMode(icon, HitTestMode::HTMTRANSPARENT);
73     SetNodeHitTestMode(text, HitTestMode::HTMTRANSPARENT);
74 }
75 
76 #ifdef SECURITY_COMPONENT_ENABLE
ToJsonValue(std::unique_ptr<JsonValue> & json) const77 void SecurityComponentPattern::ToJsonValue(std::unique_ptr<JsonValue>& json) const
78 {
79     auto node = GetHost();
80     CHECK_NULL_VOID(node);
81 
82     auto layoutProperty = AceType::DynamicCast<SecurityComponentLayoutProperty>(node->GetLayoutProperty());
83     CHECK_NULL_VOID(layoutProperty);
84     json->Put("text", layoutProperty->GetSecurityComponentDescription().value_or(0));
85     json->Put("icon", layoutProperty->GetIconStyle().value_or(0));
86     json->Put("buttonType", layoutProperty->GetBackgroundType().value_or(0));
87     json->Put("layoutDirection", static_cast<int64_t>(
88         layoutProperty->GetTextIconLayoutDirection().value_or(SecurityComponentLayoutDirection::VERTICAL)));
89     json->Put("type", node->GetTag().c_str());
90 
91     RefPtr<FrameNode> iconNode = GetSecCompChildNode(node, V2::IMAGE_ETS_TAG);
92     if (iconNode != nullptr) {
93         auto iconProp = iconNode->GetLayoutProperty<ImageLayoutProperty>();
94         CHECK_NULL_VOID(iconProp);
95         json->Put("iconSize",
96             iconProp->GetCalcLayoutConstraint()->selfIdealSize->Width()->GetDimension().ToString().c_str());
97         json->Put("iconColor",
98             iconProp->GetImageSourceInfo().value().GetFillColor().value_or(Color::WHITE).ColorToString().c_str());
99     }
100     RefPtr<FrameNode> textNode = GetSecCompChildNode(node, V2::TEXT_ETS_TAG);
101     if (textNode != nullptr) {
102         auto textProp = textNode->GetLayoutProperty<TextLayoutProperty>();
103         CHECK_NULL_VOID(textProp);
104         json->Put("fontSize", textProp->GetFontSize().value_or(Dimension(0.0)).ToString().c_str());
105         json->Put("fontWeight",
106             V2::ConvertWrapFontWeightToStirng(textProp->GetFontWeight().value_or(FontWeight::NORMAL)).c_str());
107         json->Put("fontFamily", "HarmonyOS Sans");
108         json->Put("fontStyle", static_cast<int64_t>(textProp->GetItalicFontStyle().value_or(Ace::FontStyle::NORMAL)));
109         json->Put("fontColor", textProp->GetTextColor().value_or(Color::WHITE).ColorToString().c_str());
110     }
111     auto paddingJson = JsonUtil::Create(true);
112     paddingJson->Put("top", layoutProperty->GetBackgroundTopPadding().value_or(Dimension(0.0)).ToString().c_str());
113     paddingJson->Put("bottom",
114         layoutProperty->GetBackgroundBottomPadding().value_or(Dimension(0.0)).ToString().c_str());
115     paddingJson->Put("left", layoutProperty->GetBackgroundLeftPadding().value_or(Dimension(0.0)).ToString().c_str());
116     paddingJson->Put("right", layoutProperty->GetBackgroundRightPadding().value_or(Dimension(0.0)).ToString().c_str());
117     json->Put("padding", paddingJson);
118     json->Put("textIconSpace", layoutProperty->GetTextIconSpace().value_or(Dimension(0.0)).ToString().c_str());
119     ToJsonValueRect(json);
120 }
121 
ToJsonValueRect(std::unique_ptr<JsonValue> & json) const122 void SecurityComponentPattern::ToJsonValueRect(std::unique_ptr<JsonValue>& json) const
123 {
124     auto node = GetHost();
125     CHECK_NULL_VOID(node);
126 
127     RefPtr<FrameNode> buttonNode = GetSecCompChildNode(node, V2::BUTTON_ETS_TAG);
128     if (buttonNode != nullptr) {
129         const auto& renderContext = buttonNode->GetRenderContext();
130         CHECK_NULL_VOID(renderContext);
131         json->Put("backgroundColor", renderContext->GetBackgroundColor().value().ColorToString().c_str());
132         json->Put("borderColor",
133             renderContext->GetBorderColor()->leftColor.value_or(Color::BLACK).ColorToString().c_str());
134         json->Put("borderStyle",
135             static_cast<int>(renderContext->GetBorderStyle()->styleLeft.value_or(BorderStyle::NONE)));
136         auto bgProp = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
137         CHECK_NULL_VOID(bgProp);
138         const auto& borderWidth = bgProp->GetBorderWidthProperty();
139         if (borderWidth != nullptr) {
140             json->Put("borderWidth", borderWidth->leftDimen.value_or(Dimension(0.0)).ToString().c_str());
141         }
142         auto borderRadius = bgProp->GetBorderRadius();
143         if (borderRadius.has_value()) {
144             json->Put("borderRadius",
145                 borderRadius->radiusTopLeft.value_or(Dimension(0.0, DimensionUnit::VP)).ToString().c_str());
146         } else {
147             json->Put("borderRadius", "0.00vp");
148         }
149     }
150 }
151 #endif
152 
OnModifyDone()153 void SecurityComponentPattern::OnModifyDone()
154 {
155     auto frameNode = GetHost();
156     CHECK_NULL_VOID(frameNode);
157 
158     RefPtr<FrameNode> iconNode = GetSecCompChildNode(frameNode, V2::IMAGE_ETS_TAG);
159     if (iconNode != nullptr) {
160         iconNode->MarkModifyDone();
161     }
162 
163     RefPtr<FrameNode> textNode = GetSecCompChildNode(frameNode, V2::TEXT_ETS_TAG);
164     if (textNode != nullptr) {
165         textNode->MarkModifyDone();
166     }
167 
168     RefPtr<FrameNode> buttonNode = GetSecCompChildNode(frameNode, V2::BUTTON_ETS_TAG);
169     if (buttonNode != nullptr) {
170         buttonNode->MarkModifyDone();
171     }
172 
173     InitSecurityComponentOnClick(frameNode, iconNode, textNode, buttonNode);
174     InitSecurityComponentAppearCallback(frameNode);
175     RegisterOrUpdateSecurityComponent(frameNode, scId_);
176 }
177 
RegisterOrUpdateSecurityComponent(RefPtr<FrameNode> & frameNode,int32_t & scId)178 void SecurityComponentPattern::RegisterOrUpdateSecurityComponent(RefPtr<FrameNode>& frameNode, int32_t& scId)
179 {
180     if (scId == -1) {
181         SecurityComponentHandler::RegisterSecurityComponent(frameNode, scId);
182     } else {
183         SecurityComponentHandler::UpdateSecurityComponent(frameNode, scId);
184     }
185 }
186 
InitSecurityComponentAppearCallback(RefPtr<FrameNode> & frameNode)187 void SecurityComponentPattern::InitSecurityComponentAppearCallback(RefPtr<FrameNode>& frameNode)
188 {
189     if (isAppearCallback_) {
190         return;
191     }
192     auto eventHub = frameNode->GetEventHub<EventHub>();
193     CHECK_NULL_VOID(eventHub);
194 
195     auto onAppear = [weak = WeakClaim(this)]() {
196         auto securityComponentPattern = weak.Upgrade();
197         CHECK_NULL_VOID(securityComponentPattern);
198         auto frameNode = securityComponentPattern->GetHost();
199         securityComponentPattern->RegisterOrUpdateSecurityComponent(frameNode,
200             securityComponentPattern->scId_);
201     };
202 
203     auto onDisAppear = [weak = WeakClaim(this)]() {
204         auto securityComponentPattern = weak.Upgrade();
205         CHECK_NULL_VOID(securityComponentPattern);
206         SecurityComponentHandler::UnregisterSecurityComponent(securityComponentPattern->scId_);
207     };
208     eventHub->SetOnAppear(std::move(onAppear));
209     eventHub->SetOnDisappear(std::move(onDisAppear));
210     isAppearCallback_ = true;
211 }
212 } // namespace OHOS::Ace::NG
213