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 if (!(container && container->IsUIExtensionWindow()) || !pipelineContext->GetOnFoucs()) {
53 return;
54 }
55 detectCount--;
56 auto task = [weakNode = AceType::WeakClaim(AceType::RawPtr(rootNode)), detectCount, currentId, pageUrl]() {
57 ContainerScope scope(currentId);
58 auto root = weakNode.Upgrade();
59 CHECK_NULL_VOID(root);
60 auto pipeline = root->GetContext();
61 CHECK_NULL_VOID(pipeline);
62 if (pipeline->GetRootWidth() * pipeline->GetRootHeight() < SystemProperties::GetDeviceWidth() *
63 SystemProperties::GetDeviceHeight() * TRANSPARENT_NODE_SIZE_THRESHOLD || !root->IsContextTransparent()) {
64 return;
65 }
66 if (detectCount > 0) {
67 LOGW("try detect again");
68 TransparentNodeDetector::GetInstance().PostCheckNodeTransparentTask(root, pageUrl, detectCount);
69 return;
70 }
71 LOGW("transparent node detected");
72 auto window = pipeline->GetWindow();
73 CHECK_NULL_VOID(window);
74 TransparentNodeDetector::GetInstance().DumpNodeInfo(root, window);
75 auto container = Container::GetContainer(currentId);
76 std::string bundleName = container ? container->GetBundleName() : "";
77 std::string moduleName = container ? container->GetModuleName() : "";
78 EventReport::ReportUiExtensionTransparentEvent(pageUrl, bundleName, moduleName);
79 window->NotifyExtensionTimeout(ERROR_CODE_UIEXTENSION_TRANSPARENT);
80 };
81 executor->PostDelayedTask(std::move(task), TaskExecutor::TaskType::UI, DELAY_TIME, "ExtensionTransparentDetector");
82 }
83
DumpNodeInfo(const RefPtr<FrameNode> & node,Window * window)84 void TransparentNodeDetector::DumpNodeInfo(const RefPtr<FrameNode>& node, Window* window)
85 {
86 std::string path = AceApplicationInfo::GetInstance().GetDataFileDirPath() + "/dump_info.log";
87 std::unique_ptr<std::ofstream> out = std::make_unique<std::ofstream>(path);
88 if (out) {
89 DumpLog::GetInstance().Reset();
90 DumpLog::GetInstance().SetDumpFile(std::move(out));
91 DumpLog::GetInstance().Print(std::string("WindowId: ").append(std::to_string(window->GetWindowId())));
92 DumpLog::GetInstance().Print(std::string("WindowName: ").append(window->GetWindowName()));
93 node->DumpTree(0, true);
94 DumpLog::GetInstance().OutPutDefault();
95 }
96 }
97 } // namespace OHOS::Ace::NG
98
99