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_probe.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "base/memory/ace_type.h"
20 #include "core/components_ng/pattern/security_component/security_component_handler.h"
21 #include "core/components_ng/pattern/security_component/security_component_log.h"
22 #include "core/components_v2/inspector/inspector_constants.h"
23 #include "core/pipeline/base/element_register.h"
24 #include "core/pipeline_ng/pipeline_context.h"
25 
26 namespace OHOS::Ace::NG {
27 namespace {
28 constexpr uint64_t MAX_CALLBACK_WAITING_TIME = 500; // 500ms
29 }
30 
InitProbeTask()31 void SecurityComponentProbe::InitProbeTask()
32 {
33     if (taskExec_.has_value()) {
34         return;
35     }
36     tmux_.lock();
37     if (taskExec_.has_value()) {
38         tmux_.unlock();
39         return;
40     }
41     auto context = PipelineContext::GetCurrentContextSafely();
42     CHECK_NULL_VOID(context);
43     taskExec_ = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::UI);
44     tmux_.unlock();
45     return;
46 }
47 
GetComponentInfo(int32_t nodeId,std::string & compInfoStr)48 int32_t SecurityComponentProbe::GetComponentInfo(int32_t nodeId, std::string& compInfoStr)
49 {
50     if (!taskExec_.has_value()) {
51         SC_LOG_WARN("callback task do not exist.");
52         return -1;
53     }
54     if (!tmux_.try_lock_for(std::chrono::milliseconds(MAX_CALLBACK_WAITING_TIME))) {
55         SC_LOG_WARN("callback task timeout.");
56         return -1;
57     }
58     int taskRes;
59     taskExec_.value().PostSyncTask(
60         [nodeId, &compInfoStr, &taskRes] {
61             auto node = AceType::DynamicCast<FrameNode>(ElementRegister::GetInstance()->GetNodeById(nodeId));
62             if (!node) {
63                 SC_LOG_WARN("node do not exist.");
64                 taskRes = -1;
65                 return;
66             }
67 
68             if ((node->GetTag() != V2::LOCATION_BUTTON_ETS_TAG) && (node->GetTag() != V2::PASTE_BUTTON_ETS_TAG) &&
69                 (node->GetTag() != V2::SAVE_BUTTON_ETS_TAG)) {
70                 SC_LOG_WARN("node is not security component.");
71                 taskRes = -1;
72                 return;
73             }
74             Security::SecurityComponent::SecCompType scType;
75             if (!SecurityComponentHandler::InitButtonInfo(compInfoStr, node, scType)) {
76                 SC_LOG_WARN("node init info failed.");
77                 taskRes = -1;
78                 return;
79             }
80             taskRes = 0;
81         },
82         "ArkUISecurityComponentInitButtonInfo");
83     tmux_.unlock();
84     return taskRes;
85 }
86 } // namespace OHOS::Ace::NG
87