• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     Recorder::NodeDataCache::Get().GetNodeData(pageUrl, nodeProperties);
51 }
52 
OHOS_ACE_GetSimplifiedInspectorTree(std::string & tree)53 extern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetSimplifiedInspectorTree(std::string& tree)
54 {
55     auto containerId = Recorder::EventRecorder::Get().GetContainerId();
56     auto container = Container::GetContainer(containerId);
57     if (!container) {
58         return;
59     }
60     if (container->IsUseNewPipeline()) {
61         tree = NG::Inspector::GetSimplifiedInspector(containerId);
62     }
63 }
64 
65 namespace Recorder {
66 constexpr char HA_CLIENT_SO_PATH[] = "libha_ace_engine.z.so";
67 
68 static bool g_loaded = false;
69 static void* g_handle = nullptr;
70 static std::once_flag g_loadFlag;
71 
InitHandler()72 void InitHandler()
73 {
74     if (g_handle) {
75         return;
76     }
77     TAG_LOGI(AceLogTag::ACE_UIEVENT, "report ace loaded");
78     auto handle = dlopen(HA_CLIENT_SO_PATH, RTLD_LAZY);
79     if (handle == nullptr) {
80         TAG_LOGW(AceLogTag::ACE_UIEVENT, "Failed to open shared library %{public}s, reason: %{public}sn",
81             HA_CLIENT_SO_PATH, dlerror());
82         return;
83     }
84     g_handle = handle;
85 }
86 
Init()87 void Init()
88 {
89     if (g_loaded) {
90         return;
91     }
92     std::call_once(g_loadFlag, [] { InitHandler(); });
93     g_loaded = true;
94 }
95 
DeInit()96 void DeInit()
97 {
98     if (g_handle) {
99         dlclose(g_handle);
100         g_handle = nullptr;
101         g_loaded = false;
102     }
103 }
104 } // namespace OHOS::Ace::Recorder
105 } // namespace OHOS::Ace
106