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 "adapter/ohos/entrance/ui_event_impl.h"
17
18 #include <dlfcn.h>
19
20 #include "ui_event_observer.h"
21 #include "core/common/container.h"
22 #include "core/common/container_scope.h"
23 #include "core/common/recorder/event_controller.h"
24 #include "core/common/recorder/event_recorder.h"
25 #include "core/common/recorder/inspector_tree_collector.h"
26 #include "core/common/recorder/node_data_cache.h"
27 #include "core/components_ng/base/simplified_inspector.h"
28 #include "core/components_ng/pattern/pattern.h"
29 #include "frameworks/bridge/common/utils/engine_helper.h"
30
31 namespace OHOS::Ace {
32 namespace {
GetWebLanguageByNodeId(int32_t nodeId)33 std::string GetWebLanguageByNodeId(int32_t nodeId)
34 {
35 auto& weakNodeCache = Recorder::EventRecorder::Get().GetWeakNodeMap();
36 auto iter = weakNodeCache.find(nodeId);
37 if (iter == weakNodeCache.end()) {
38 return "";
39 }
40 auto node = iter->second.Upgrade();
41 CHECK_NULL_RETURN(node, "");
42 auto pattern = node->GetPattern();
43 CHECK_NULL_RETURN(pattern, "");
44 return pattern->GetCurrentLanguage();
45 }
46
GetCurrentPageParam()47 std::string GetCurrentPageParam()
48 {
49 ContainerScope scope(Container::CurrentIdSafely());
50 auto container = Container::CurrentSafely();
51 CHECK_NULL_RETURN(container, "");
52 auto frontend = container->GetFrontend();
53 CHECK_NULL_RETURN(frontend, "");
54 auto result = frontend->GetTopNavDestinationInfo(false, true);
55 if (!result.empty() && result != "{}") {
56 return result;
57 }
58
59 auto delegate = EngineHelper::GetCurrentDelegate();
60 CHECK_NULL_RETURN(delegate, "");
61 auto paramJson = JsonUtil::Create();
62 result = delegate->GetParams();
63 if (result.empty() || result == "{}") {
64 result = delegate->GetInitParams();
65 }
66 paramJson->Put("params", result.c_str());
67 return paramJson->ToString();
68 }
69 } // namespace
70
OHOS_ACE_RegisterUIEventObserver(const std::string & config,const std::shared_ptr<UIEventObserver> & observer)71 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_RegisterUIEventObserver(
72 const std::string& config, const std::shared_ptr<UIEventObserver>& observer)
73 {
74 TAG_LOGI(AceLogTag::ACE_UIEVENT, "RegisterUIEventObserver");
75 Recorder::EventController::Get().Register(config, observer);
76 }
77
OHOS_ACE_UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver> & observer)78 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver>& observer)
79 {
80 TAG_LOGI(AceLogTag::ACE_UIEVENT, "UnregisterUIEventObserver.");
81 Recorder::EventController::Get().Unregister(observer);
82 }
83
OHOS_ACE_GetNodeProperty(const std::string & pageUrl,std::unordered_map<std::string,std::string> & nodeProperties)84 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetNodeProperty(
85 const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodeProperties)
86 {
87 TAG_LOGI(AceLogTag::ACE_UIEVENT, "GetNodeProperty.");
88 Recorder::NodeDataCache::Get().GetNodeData(pageUrl, nodeProperties);
89 }
90
OHOS_ACE_GetSimplifiedInspectorTree(const TreeParams & params,std::string & tree)91 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetSimplifiedInspectorTree(const TreeParams& params, std::string& tree)
92 {
93 if (params.infoType == InspectorInfoType::PAGE_PARAM) {
94 tree = GetCurrentPageParam();
95 return;
96 }
97 auto containerId = Recorder::EventRecorder::Get().GetContainerId(params.inspectorType == InspectorPageType::FOCUS);
98 auto container = Container::GetContainer(containerId);
99 if (!container) {
100 return;
101 }
102 if (params.isWindowIdOnly || params.infoType == InspectorInfoType::WINDOW_ID) {
103 tree = std::to_string(container->GetWindowId());
104 return;
105 }
106 if (params.infoType == InspectorInfoType::WEB_LANG) {
107 tree = GetWebLanguageByNodeId(params.webId);
108 return;
109 }
110 if (container->IsUseNewPipeline()) {
111 auto inspector = std::make_shared<NG::SimplifiedInspector>(containerId, params);
112 tree = inspector->GetInspector();
113 }
114 }
115
OHOS_ACE_GetSimplifiedInspectorTreeAsync(const TreeParams & params,OnInspectorTreeResult && callback)116 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetSimplifiedInspectorTreeAsync(
117 const TreeParams& params, OnInspectorTreeResult&& callback)
118 {
119 auto containerId = Recorder::EventRecorder::Get().GetContainerId(params.inspectorType == InspectorPageType::FOCUS);
120 auto container = Container::GetContainer(containerId);
121 if (!container) {
122 return;
123 }
124 if (container->IsUseNewPipeline()) {
125 auto inspector = std::make_shared<NG::SimplifiedInspector>(containerId, params);
126 if (params.enableBackground) {
127 auto collector = std::make_shared<Recorder::InspectorTreeCollector>(std::move(callback), true);
128 inspector->GetInspectorBackgroundAsync(collector);
129 } else {
130 auto collector = std::make_shared<Recorder::InspectorTreeCollector>(std::move(callback), false);
131 inspector->GetInspectorAsync(collector);
132 }
133 }
134 }
135
OHOS_ACE_ExecuteCommandAsync(const UICommandParams & params,UICommandResult && callback)136 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_ExecuteCommandAsync(const UICommandParams& params, UICommandResult&& callback)
137 {
138 auto inspector = std::make_shared<NG::SimplifiedInspector>(0, params);
139 auto collector = std::make_shared<Recorder::InspectorTreeCollector>(std::move(callback), false);
140 inspector->ExecuteUICommand(collector);
141 }
142
143 namespace Recorder {
144 constexpr char HA_CLIENT_SO_PATH[] = "libha_ace_engine.z.so";
145
146 static bool g_loaded = false;
147 static void* g_handle = nullptr;
148 static std::once_flag g_loadFlag;
149
InitHandler()150 void InitHandler()
151 {
152 if (g_handle) {
153 return;
154 }
155 TAG_LOGI(AceLogTag::ACE_UIEVENT, "report ace loaded");
156 auto handle = dlopen(HA_CLIENT_SO_PATH, RTLD_LAZY);
157 if (handle == nullptr) {
158 TAG_LOGI(AceLogTag::ACE_UIEVENT, "Failed to open shared library %{public}s, reason: %{public}sn",
159 HA_CLIENT_SO_PATH, dlerror());
160 return;
161 }
162
163 auto func = reinterpret_cast<void(*)()>(dlsym(handle, "OnAceLoaded"));
164 if (func == nullptr) {
165 TAG_LOGI(AceLogTag::ACE_UIEVENT, "Failed to find func, reason: %{public}sn", dlerror());
166 dlclose(handle);
167 return;
168 }
169 func();
170 g_handle = handle;
171 }
172
Init()173 void Init()
174 {
175 if (g_loaded) {
176 return;
177 }
178 std::call_once(g_loadFlag, [] { InitHandler(); });
179 g_loaded = true;
180 }
181
DeInit()182 void DeInit()
183 {
184 if (g_handle) {
185 dlclose(g_handle);
186 g_handle = nullptr;
187 g_loaded = false;
188 }
189 }
190 } // namespace OHOS::Ace::Recorder
191 } // namespace OHOS::Ace
192