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