123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 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 "interfaces/inner_api/ace/ui_content.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include "utils.h"
1923b3eb3cSopenharmony_ci#include "ace_forward_compatibility.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_ci#ifdef UICAST_COMPONENT_SUPPORTED
2223b3eb3cSopenharmony_ci#include "interfaces/inner_api/ace/uicast/uicast_subscriber.h"
2323b3eb3cSopenharmony_ci#endif
2423b3eb3cSopenharmony_ci
2523b3eb3cSopenharmony_cinamespace OHOS::Ace {
2623b3eb3cSopenharmony_ci
2723b3eb3cSopenharmony_ciusing CreateCardFunc = UIContent* (*)(void*, void*, bool);
2823b3eb3cSopenharmony_ciusing CreateFunc = UIContent* (*)(void*, void*);
2923b3eb3cSopenharmony_ciusing CreateFunction = UIContent* (*)(void*);
3023b3eb3cSopenharmony_ciusing GetUIContentFunc = UIContent* (*)(int32_t);
3123b3eb3cSopenharmony_ciusing GetCurrentUIStackInfoFunction = char* (*)();
3223b3eb3cSopenharmony_ciconstexpr char UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateUIContent";
3323b3eb3cSopenharmony_ciconstexpr char Card_CREATE_FUNC[] = "OHOS_ACE_CreateFormContent";
3423b3eb3cSopenharmony_ciconstexpr char SUB_WINDOW_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateSubWindowUIContent";
3523b3eb3cSopenharmony_ciconstexpr char GET_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_GetUIContent";
3623b3eb3cSopenharmony_ci
3723b3eb3cSopenharmony_ciOHOS::AbilityRuntime::Context* context_ = nullptr;
3823b3eb3cSopenharmony_ci
3923b3eb3cSopenharmony_ciUIContent* CreateUIContent(void* context, void* runtime, bool isFormRender)
4023b3eb3cSopenharmony_ci{
4123b3eb3cSopenharmony_ci    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
4223b3eb3cSopenharmony_ci    if (handle == nullptr) {
4323b3eb3cSopenharmony_ci        return nullptr;
4423b3eb3cSopenharmony_ci    }
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ci    auto entry = reinterpret_cast<CreateCardFunc>(LOADSYM(handle, Card_CREATE_FUNC));
4723b3eb3cSopenharmony_ci    if (entry == nullptr) {
4823b3eb3cSopenharmony_ci        FREELIB(handle);
4923b3eb3cSopenharmony_ci        return nullptr;
5023b3eb3cSopenharmony_ci    }
5123b3eb3cSopenharmony_ci
5223b3eb3cSopenharmony_ci    auto content = entry(context, runtime, isFormRender);
5323b3eb3cSopenharmony_ci    return content;
5423b3eb3cSopenharmony_ci}
5523b3eb3cSopenharmony_ci
5623b3eb3cSopenharmony_ciUIContent* CreateUIContent(void* context, void* runtime)
5723b3eb3cSopenharmony_ci{
5823b3eb3cSopenharmony_ci    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
5923b3eb3cSopenharmony_ci    if (handle == nullptr) {
6023b3eb3cSopenharmony_ci        return nullptr;
6123b3eb3cSopenharmony_ci    }
6223b3eb3cSopenharmony_ci
6323b3eb3cSopenharmony_ci    auto entry = reinterpret_cast<CreateFunc>(LOADSYM(handle, UI_CONTENT_CREATE_FUNC));
6423b3eb3cSopenharmony_ci    if (entry == nullptr) {
6523b3eb3cSopenharmony_ci        FREELIB(handle);
6623b3eb3cSopenharmony_ci        return nullptr;
6723b3eb3cSopenharmony_ci    }
6823b3eb3cSopenharmony_ci
6923b3eb3cSopenharmony_ci    auto content = entry(context, runtime);
7023b3eb3cSopenharmony_ci#ifdef UICAST_COMPONENT_SUPPORTED
7123b3eb3cSopenharmony_ci    UICastEventSubscribeProxy::GetInstance()->SubscribeStartEvent(content);
7223b3eb3cSopenharmony_ci#endif
7323b3eb3cSopenharmony_ci
7423b3eb3cSopenharmony_ci    return content;
7523b3eb3cSopenharmony_ci}
7623b3eb3cSopenharmony_ci
7723b3eb3cSopenharmony_ciUIContent* CreateUIContent(void* ability)
7823b3eb3cSopenharmony_ci{
7923b3eb3cSopenharmony_ci    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
8023b3eb3cSopenharmony_ci    if (handle == nullptr) {
8123b3eb3cSopenharmony_ci        return nullptr;
8223b3eb3cSopenharmony_ci    }
8323b3eb3cSopenharmony_ci
8423b3eb3cSopenharmony_ci    auto entry = reinterpret_cast<CreateFunction>(LOADSYM(handle, SUB_WINDOW_UI_CONTENT_CREATE_FUNC));
8523b3eb3cSopenharmony_ci    if (entry == nullptr) {
8623b3eb3cSopenharmony_ci        FREELIB(handle);
8723b3eb3cSopenharmony_ci        return nullptr;
8823b3eb3cSopenharmony_ci    }
8923b3eb3cSopenharmony_ci
9023b3eb3cSopenharmony_ci    auto content = entry(ability);
9123b3eb3cSopenharmony_ci#ifdef UICAST_COMPONENT_SUPPORTED
9223b3eb3cSopenharmony_ci    UICastEventSubscribeProxy::GetInstance()->SubscribeStartEvent(content);
9323b3eb3cSopenharmony_ci#endif
9423b3eb3cSopenharmony_ci
9523b3eb3cSopenharmony_ci    return content;
9623b3eb3cSopenharmony_ci}
9723b3eb3cSopenharmony_ci
9823b3eb3cSopenharmony_cistd::unique_ptr<UIContent> UIContent::Create(OHOS::AbilityRuntime::Context* context, NativeEngine* runtime)
9923b3eb3cSopenharmony_ci{
10023b3eb3cSopenharmony_ci    std::unique_ptr<UIContent> content;
10123b3eb3cSopenharmony_ci    content.reset(CreateUIContent(reinterpret_cast<void*>(context), reinterpret_cast<void*>(runtime)));
10223b3eb3cSopenharmony_ci    return content;
10323b3eb3cSopenharmony_ci}
10423b3eb3cSopenharmony_ci
10523b3eb3cSopenharmony_cistd::unique_ptr<UIContent> UIContent::Create(
10623b3eb3cSopenharmony_ci    OHOS::AbilityRuntime::Context* context, NativeEngine* runtime, bool isFormRender)
10723b3eb3cSopenharmony_ci{
10823b3eb3cSopenharmony_ci    std::unique_ptr<UIContent> content;
10923b3eb3cSopenharmony_ci    content.reset(CreateUIContent(reinterpret_cast<void*>(context), reinterpret_cast<void*>(runtime), isFormRender));
11023b3eb3cSopenharmony_ci    return content;
11123b3eb3cSopenharmony_ci}
11223b3eb3cSopenharmony_ci
11323b3eb3cSopenharmony_cistd::unique_ptr<UIContent> UIContent::Create(OHOS::AppExecFwk::Ability* ability)
11423b3eb3cSopenharmony_ci{
11523b3eb3cSopenharmony_ci    std::unique_ptr<UIContent> content;
11623b3eb3cSopenharmony_ci    content.reset(CreateUIContent(reinterpret_cast<void*>(ability)));
11723b3eb3cSopenharmony_ci    return content;
11823b3eb3cSopenharmony_ci}
11923b3eb3cSopenharmony_ci
12023b3eb3cSopenharmony_civoid UIContent::ShowDumpHelp(std::vector<std::string>& info)
12123b3eb3cSopenharmony_ci{
12223b3eb3cSopenharmony_ci    info.emplace_back(" -element                       |show element tree");
12323b3eb3cSopenharmony_ci    info.emplace_back(" -render                        |show render tree");
12423b3eb3cSopenharmony_ci    info.emplace_back(" -inspector                     |show inspector tree");
12523b3eb3cSopenharmony_ci    info.emplace_back(" -frontend                      |show path and components count of current page");
12623b3eb3cSopenharmony_ci    info.emplace_back(" -navigation                    |show navigation path stack");
12723b3eb3cSopenharmony_ci}
12823b3eb3cSopenharmony_ci
12923b3eb3cSopenharmony_ciUIContent* UIContent::GetUIContent(int32_t instanceId)
13023b3eb3cSopenharmony_ci{
13123b3eb3cSopenharmony_ci    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
13223b3eb3cSopenharmony_ci    if (handle == nullptr) {
13323b3eb3cSopenharmony_ci        return nullptr;
13423b3eb3cSopenharmony_ci    }
13523b3eb3cSopenharmony_ci
13623b3eb3cSopenharmony_ci    auto entry = reinterpret_cast<GetUIContentFunc>(LOADSYM(handle, GET_UI_CONTENT_CREATE_FUNC));
13723b3eb3cSopenharmony_ci    if (entry == nullptr) {
13823b3eb3cSopenharmony_ci        FREELIB(handle);
13923b3eb3cSopenharmony_ci        return nullptr;
14023b3eb3cSopenharmony_ci    }
14123b3eb3cSopenharmony_ci
14223b3eb3cSopenharmony_ci    auto content = entry(instanceId);
14323b3eb3cSopenharmony_ci    return content;
14423b3eb3cSopenharmony_ci}
14523b3eb3cSopenharmony_ci
14623b3eb3cSopenharmony_cistd::string UIContent::GetCurrentUIStackInfo()
14723b3eb3cSopenharmony_ci{
14823b3eb3cSopenharmony_ci    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
14923b3eb3cSopenharmony_ci    if (handle == nullptr) {
15023b3eb3cSopenharmony_ci        return std::string();
15123b3eb3cSopenharmony_ci    }
15223b3eb3cSopenharmony_ci
15323b3eb3cSopenharmony_ci    auto entry = reinterpret_cast<GetCurrentUIStackInfoFunction>(LOADSYM(handle, "OHOS_ACE_GetCurrentUIStackInfo"));
15423b3eb3cSopenharmony_ci    if (entry == nullptr) {
15523b3eb3cSopenharmony_ci        FREELIB(handle);
15623b3eb3cSopenharmony_ci        return std::string();
15723b3eb3cSopenharmony_ci    }
15823b3eb3cSopenharmony_ci
15923b3eb3cSopenharmony_ci    auto content = entry();
16023b3eb3cSopenharmony_ci    if (content == nullptr) {
16123b3eb3cSopenharmony_ci        return std::string();
16223b3eb3cSopenharmony_ci    }
16323b3eb3cSopenharmony_ci
16423b3eb3cSopenharmony_ci    return content;
16523b3eb3cSopenharmony_ci}
16623b3eb3cSopenharmony_ci} // namespace OHOS::Ace
167