123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#include "adapter/ohos/entrance/ui_event_impl.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include <dlfcn.h>
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_ci#include "core/common/container.h"
2123b3eb3cSopenharmony_ci#include "core/common/recorder/event_controller.h"
2223b3eb3cSopenharmony_ci#include "core/common/recorder/node_data_cache.h"
2323b3eb3cSopenharmony_ci#include "core/components_ng/base/inspector.h"
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_cinamespace OHOS::Ace {
2623b3eb3cSopenharmony_ciextern "C" ACE_FORCE_EXPORT void OHOS_ACE_RegisterUIEventObserver(
2723b3eb3cSopenharmony_ci    const std::string& config, const std::shared_ptr<UIEventObserver>& observer)
2823b3eb3cSopenharmony_ci{
2923b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_UIEVENT, "RegisterUIEventObserver");
3023b3eb3cSopenharmony_ci    Recorder::EventController::Get().Register(config, observer);
3123b3eb3cSopenharmony_ci}
3223b3eb3cSopenharmony_ci
3323b3eb3cSopenharmony_ciextern "C" ACE_FORCE_EXPORT void OHOS_ACE_UnregisterUIEventObserver(const std::shared_ptr<UIEventObserver>& observer)
3423b3eb3cSopenharmony_ci{
3523b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_UIEVENT, "UnregisterUIEventObserver.");
3623b3eb3cSopenharmony_ci    Recorder::EventController::Get().Unregister(observer);
3723b3eb3cSopenharmony_ci}
3823b3eb3cSopenharmony_ci
3923b3eb3cSopenharmony_ciextern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetNodeProperty(
4023b3eb3cSopenharmony_ci    const std::string& pageUrl, std::unordered_map<std::string, std::string>& nodeProperties)
4123b3eb3cSopenharmony_ci{
4223b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_UIEVENT, "GetNodeProperty.");
4323b3eb3cSopenharmony_ci    Recorder::NodeDataCache::Get().GetNodeData(pageUrl, nodeProperties);
4423b3eb3cSopenharmony_ci}
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ciextern "C" ACE_FORCE_EXPORT void OHOS_ACE_GetSimplifiedInspectorTree(std::string& tree)
4723b3eb3cSopenharmony_ci{
4823b3eb3cSopenharmony_ci    TAG_LOGD(AceLogTag::ACE_UIEVENT, "GetSimplifiedInspectorTree.");
4923b3eb3cSopenharmony_ci    auto containerId = Recorder::EventRecorder::Get().GetContainerId();
5023b3eb3cSopenharmony_ci    auto container = Container::GetContainer(containerId);
5123b3eb3cSopenharmony_ci    if (!container) {
5223b3eb3cSopenharmony_ci        return;
5323b3eb3cSopenharmony_ci    }
5423b3eb3cSopenharmony_ci    if (container->IsUseNewPipeline()) {
5523b3eb3cSopenharmony_ci        tree = NG::Inspector::GetSimplifiedInspector(containerId);
5623b3eb3cSopenharmony_ci    }
5723b3eb3cSopenharmony_ci}
5823b3eb3cSopenharmony_ci
5923b3eb3cSopenharmony_cinamespace Recorder {
6023b3eb3cSopenharmony_ciconstexpr char HA_CLIENT_SO_PATH[] = "libha_ace_engine.z.so";
6123b3eb3cSopenharmony_ci
6223b3eb3cSopenharmony_cistatic bool g_loaded = false;
6323b3eb3cSopenharmony_cistatic void* g_handle = nullptr;
6423b3eb3cSopenharmony_cistatic std::once_flag g_loadFlag;
6523b3eb3cSopenharmony_ci
6623b3eb3cSopenharmony_civoid InitHandler()
6723b3eb3cSopenharmony_ci{
6823b3eb3cSopenharmony_ci    if (g_handle) {
6923b3eb3cSopenharmony_ci        return;
7023b3eb3cSopenharmony_ci    }
7123b3eb3cSopenharmony_ci    TAG_LOGI(AceLogTag::ACE_UIEVENT, "report ace loaded");
7223b3eb3cSopenharmony_ci    auto handle = dlopen(HA_CLIENT_SO_PATH, RTLD_LAZY);
7323b3eb3cSopenharmony_ci    if (handle == nullptr) {
7423b3eb3cSopenharmony_ci        TAG_LOGI(AceLogTag::ACE_UIEVENT, "Failed to open shared library %{public}s, reason: %{public}sn",
7523b3eb3cSopenharmony_ci            HA_CLIENT_SO_PATH, dlerror());
7623b3eb3cSopenharmony_ci        return;
7723b3eb3cSopenharmony_ci    }
7823b3eb3cSopenharmony_ci
7923b3eb3cSopenharmony_ci    auto func = reinterpret_cast<void(*)()>(dlsym(handle, "OnAceLoaded"));
8023b3eb3cSopenharmony_ci    if (func == nullptr) {
8123b3eb3cSopenharmony_ci        TAG_LOGI(AceLogTag::ACE_UIEVENT, "Failed to find func, reason: %{public}sn", dlerror());
8223b3eb3cSopenharmony_ci        dlclose(handle);
8323b3eb3cSopenharmony_ci        return;
8423b3eb3cSopenharmony_ci    }
8523b3eb3cSopenharmony_ci    func();
8623b3eb3cSopenharmony_ci    g_handle = handle;
8723b3eb3cSopenharmony_ci}
8823b3eb3cSopenharmony_ci
8923b3eb3cSopenharmony_civoid Init()
9023b3eb3cSopenharmony_ci{
9123b3eb3cSopenharmony_ci    if (g_loaded) {
9223b3eb3cSopenharmony_ci        return;
9323b3eb3cSopenharmony_ci    }
9423b3eb3cSopenharmony_ci    std::call_once(g_loadFlag, [] { InitHandler(); });
9523b3eb3cSopenharmony_ci    g_loaded = true;
9623b3eb3cSopenharmony_ci}
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_civoid DeInit()
9923b3eb3cSopenharmony_ci{
10023b3eb3cSopenharmony_ci    if (g_handle) {
10123b3eb3cSopenharmony_ci        dlclose(g_handle);
10223b3eb3cSopenharmony_ci        g_handle = nullptr;
10323b3eb3cSopenharmony_ci        g_loaded = false;
10423b3eb3cSopenharmony_ci    }
10523b3eb3cSopenharmony_ci}
10623b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Recorder
10723b3eb3cSopenharmony_ci} // namespace OHOS::Ace
108