1 /*
2 * Copyright (c) 2024-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
16 #include "core/components_ng/base/transparent_node_detector.h"
17
18 #include "base/error/error_code.h"
19 #include "base/log/dump_log.h"
20 #include "base/log/event_report.h"
21 #include "core/common/ace_application_info.h"
22 #include "core/pipeline_ng/pipeline_context.h"
23
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr uint32_t DELAY_TIME = 2000;
27 constexpr double TRANSPARENT_NODE_SIZE_THRESHOLD = 0.85;
28 } // namespace
29
GetInstance()30 TransparentNodeDetector& TransparentNodeDetector::GetInstance()
31 {
32 static TransparentNodeDetector instance;
33 return instance;
34 }
35
36
PostCheckNodeTransparentTask(const RefPtr<FrameNode> & node,const std::string & pageUrl,uint8_t detectCount)37 void TransparentNodeDetector::PostCheckNodeTransparentTask(const RefPtr<FrameNode>& node, const std::string& pageUrl,
38 uint8_t detectCount)
39 {
40 if (detectCount > TransparentNodeDetector::MAX_DETECT_COUNT || detectCount == 0) {
41 return;
42 }
43 CHECK_NULL_VOID(node);
44 auto pipelineContext = node->GetContext();
45 CHECK_NULL_VOID(pipelineContext);
46 auto rootNode = pipelineContext->GetRootElement();
47 CHECK_NULL_VOID(rootNode);
48 auto executor = pipelineContext->GetTaskExecutor();
49 CHECK_NULL_VOID(executor);
50 auto currentId = Container::CurrentIdSafely();
51 auto container = Container::GetContainer(currentId);
52 CHECK_NULL_VOID(container);
53 bool isUECWindow = container->IsUIExtensionWindow();
54 if (!(isUECWindow || container->IsHostSubWindow() ||
55 container->IsHostDialogWindow()) || !pipelineContext->GetOnFocus()) {
56 return;
57 }
58 detectCount--;
59 auto task = [weakNode = AceType::WeakClaim(AceType::RawPtr(rootNode)),
60 detectCount, currentId, pageUrl, isUECWindow]() {
61 ContainerScope scope(currentId);
62 auto root = weakNode.Upgrade();
63 CHECK_NULL_VOID(root);
64 auto pipeline = root->GetContext();
65 CHECK_NULL_VOID(pipeline);
66 if (pipeline->GetRootWidth() * pipeline->GetRootHeight() < SystemProperties::GetDeviceWidth() *
67 SystemProperties::GetDeviceHeight() * TRANSPARENT_NODE_SIZE_THRESHOLD || !root->IsContextTransparent()) {
68 return;
69 }
70 if (detectCount > 0) {
71 LOGW("try detect again");
72 TransparentNodeDetector::GetInstance().PostCheckNodeTransparentTask(root, pageUrl, detectCount);
73 return;
74 }
75 LOGW("transparent node detected");
76 auto window = pipeline->GetWindow();
77 CHECK_NULL_VOID(window);
78 TransparentNodeDetector::GetInstance().DumpNodeInfo(root, window);
79 auto container = Container::GetContainer(currentId);
80 std::string bundleName = container ? container->GetBundleName() : "";
81 std::string moduleName = container ? container->GetModuleName() : "";
82 EventReport::ReportUiExtensionTransparentEvent(pageUrl, bundleName, moduleName);
83 if (isUECWindow) {
84 window->NotifyExtensionTimeout(ERROR_CODE_UIEXTENSION_TRANSPARENT);
85 }
86 };
87 executor->PostDelayedTask(std::move(task), TaskExecutor::TaskType::UI, DELAY_TIME, "ExtensionTransparentDetector");
88 }
89
DumpNodeInfo(const RefPtr<FrameNode> & node,Window * window)90 void TransparentNodeDetector::DumpNodeInfo(const RefPtr<FrameNode>& node, Window* window)
91 {
92 std::string path = AceApplicationInfo::GetInstance().GetDataFileDirPath() + "/dump_info.log";
93 std::unique_ptr<std::ofstream> out = std::make_unique<std::ofstream>(path);
94 if (out) {
95 DumpLog::GetInstance().Reset();
96 DumpLog::GetInstance().SetDumpFile(std::move(out));
97 DumpLog::GetInstance().Print(std::string("WindowId: ").append(std::to_string(window->GetWindowId())));
98 DumpLog::GetInstance().Print(std::string("WindowName: ").append(window->GetWindowName()));
99 node->DumpTree(0, true);
100 DumpLog::GetInstance().OutPutDefault();
101 }
102 }
103 } // namespace OHOS::Ace::NG
104
105