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 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "interfaces/inner_api/ace/ui_event_observer.h"
24 
25 #include "base/log/log.h"
26 #include "base/thread/background_task_executor.h"
27 #include "core/common/container.h"
28 #include "core/common/recorder/event_controller.h"
29 #include "core/common/recorder/event_recorder.h"
30 #include "core/common/recorder/node_data_cache.h"
31 #include "core/components_ng/base/inspector.h"
32 
33 namespace OHOS::Ace {
OHOS_ACE_RegisterUIEventObserver(const std::string & config,const std::shared_ptr<UIEventObserver> & observer)34 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_RegisterUIEventObserver(
35     const std::string& config, const std::shared_ptr<UIEventObserver>& observer)
36 {
37     TAG_LOGI(AceLogTag::ACE_UIEVENT, "RegisterUIEventObserver");
38     Recorder::EventController::Get().Register(config, observer);
39 }
40 
OHOS_ACE_UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver> & observer)41 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver>& observer)
42 {
43     TAG_LOGI(AceLogTag::ACE_UIEVENT, "UnregisterUIEventObserver.");
44     Recorder::EventController::Get().Unregister(observer);
45 }
46 
OHOS_ACE_GetNodeProperty(const std::string & pageUrl,std::unordered_map<std::string,std::string> & nodeProperties)47 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetNodeProperty(
48     const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodeProperties)
49 {
50     TAG_LOGI(AceLogTag::ACE_UIEVENT, "GetNodeProperty.");
51     Recorder::NodeDataCache::Get().GetNodeData(pageUrl, nodeProperties);
52 }
53 
OHOS_ACE_GetSimplifiedInspectorTree(std::string & tree)54 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetSimplifiedInspectorTree(std::string& tree)
55 {
56     TAG_LOGD(AceLogTag::ACE_UIEVENT, "GetSimplifiedInspectorTree.");
57     auto containerId = Recorder::EventRecorder::Get().GetContainerId();
58     auto container = Container::GetContainer(containerId);
59     if (!container) {
60         return;
61     }
62     if (container->IsUseNewPipeline()) {
63         tree = NG::Inspector::GetSimplifiedInspector(containerId);
64     }
65 }
66 
67 namespace Recorder {
68 constexpr char HA_CLIENT_SO_PATH[] = "libha_ace_engine.z.so";
69 
70 static bool g_loaded = false;
71 static void* g_handle = nullptr;
72 static std::once_flag g_loadFlag;
73 
InitHandler()74 void InitHandler()
75 {
76     if (g_handle) {
77         return;
78     }
79     TAG_LOGI(AceLogTag::ACE_UIEVENT, "report ace loaded");
80     auto handle = dlopen(HA_CLIENT_SO_PATH, RTLD_LAZY);
81     if (handle == nullptr) {
82         TAG_LOGI(AceLogTag::ACE_UIEVENT, "Failed to open shared library %{public}s, reason: %{public}sn",
83             HA_CLIENT_SO_PATH, dlerror());
84         return;
85     }
86 
87     auto func = reinterpret_cast<void(*)()>(dlsym(handle, "OnAceLoaded"));
88     if (func == nullptr) {
89         TAG_LOGI(AceLogTag::ACE_UIEVENT, "Failed to find func, reason: %{public}sn", dlerror());
90         dlclose(handle);
91         return;
92     }
93     func();
94     g_handle = handle;
95 }
96 
Init()97 void Init()
98 {
99     if (g_loaded) {
100         return;
101     }
102     std::call_once(g_loadFlag, [] { InitHandler(); });
103     g_loaded = true;
104 }
105 
DeInit()106 void DeInit()
107 {
108     if (g_handle) {
109         dlclose(g_handle);
110         g_handle = nullptr;
111         g_loaded = false;
112     }
113 }
114 } // namespace OHOS::Ace::Recorder
115 } // namespace OHOS::Ace
116