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