1/*
2 * Copyright (c) 2021 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 "interfaces/inner_api/ace/ui_content.h"
17
18#include "utils.h"
19#include "ace_forward_compatibility.h"
20
21#ifdef UICAST_COMPONENT_SUPPORTED
22#include "interfaces/inner_api/ace/uicast/uicast_subscriber.h"
23#endif
24
25namespace OHOS::Ace {
26
27using CreateCardFunc = UIContent* (*)(void*, void*, bool);
28using CreateFunc = UIContent* (*)(void*, void*);
29using CreateFunction = UIContent* (*)(void*);
30using GetUIContentFunc = UIContent* (*)(int32_t);
31using GetCurrentUIStackInfoFunction = char* (*)();
32constexpr char UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateUIContent";
33constexpr char Card_CREATE_FUNC[] = "OHOS_ACE_CreateFormContent";
34constexpr char SUB_WINDOW_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateSubWindowUIContent";
35constexpr char GET_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_GetUIContent";
36
37OHOS::AbilityRuntime::Context* context_ = nullptr;
38
39UIContent* CreateUIContent(void* context, void* runtime, bool isFormRender)
40{
41    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
42    if (handle == nullptr) {
43        return nullptr;
44    }
45
46    auto entry = reinterpret_cast<CreateCardFunc>(LOADSYM(handle, Card_CREATE_FUNC));
47    if (entry == nullptr) {
48        FREELIB(handle);
49        return nullptr;
50    }
51
52    auto content = entry(context, runtime, isFormRender);
53    return content;
54}
55
56UIContent* CreateUIContent(void* context, void* runtime)
57{
58    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
59    if (handle == nullptr) {
60        return nullptr;
61    }
62
63    auto entry = reinterpret_cast<CreateFunc>(LOADSYM(handle, UI_CONTENT_CREATE_FUNC));
64    if (entry == nullptr) {
65        FREELIB(handle);
66        return nullptr;
67    }
68
69    auto content = entry(context, runtime);
70#ifdef UICAST_COMPONENT_SUPPORTED
71    UICastEventSubscribeProxy::GetInstance()->SubscribeStartEvent(content);
72#endif
73
74    return content;
75}
76
77UIContent* CreateUIContent(void* ability)
78{
79    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
80    if (handle == nullptr) {
81        return nullptr;
82    }
83
84    auto entry = reinterpret_cast<CreateFunction>(LOADSYM(handle, SUB_WINDOW_UI_CONTENT_CREATE_FUNC));
85    if (entry == nullptr) {
86        FREELIB(handle);
87        return nullptr;
88    }
89
90    auto content = entry(ability);
91#ifdef UICAST_COMPONENT_SUPPORTED
92    UICastEventSubscribeProxy::GetInstance()->SubscribeStartEvent(content);
93#endif
94
95    return content;
96}
97
98std::unique_ptr<UIContent> UIContent::Create(OHOS::AbilityRuntime::Context* context, NativeEngine* runtime)
99{
100    std::unique_ptr<UIContent> content;
101    content.reset(CreateUIContent(reinterpret_cast<void*>(context), reinterpret_cast<void*>(runtime)));
102    return content;
103}
104
105std::unique_ptr<UIContent> UIContent::Create(
106    OHOS::AbilityRuntime::Context* context, NativeEngine* runtime, bool isFormRender)
107{
108    std::unique_ptr<UIContent> content;
109    content.reset(CreateUIContent(reinterpret_cast<void*>(context), reinterpret_cast<void*>(runtime), isFormRender));
110    return content;
111}
112
113std::unique_ptr<UIContent> UIContent::Create(OHOS::AppExecFwk::Ability* ability)
114{
115    std::unique_ptr<UIContent> content;
116    content.reset(CreateUIContent(reinterpret_cast<void*>(ability)));
117    return content;
118}
119
120void UIContent::ShowDumpHelp(std::vector<std::string>& info)
121{
122    info.emplace_back(" -element                       |show element tree");
123    info.emplace_back(" -render                        |show render tree");
124    info.emplace_back(" -inspector                     |show inspector tree");
125    info.emplace_back(" -frontend                      |show path and components count of current page");
126    info.emplace_back(" -navigation                    |show navigation path stack");
127}
128
129UIContent* UIContent::GetUIContent(int32_t instanceId)
130{
131    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
132    if (handle == nullptr) {
133        return nullptr;
134    }
135
136    auto entry = reinterpret_cast<GetUIContentFunc>(LOADSYM(handle, GET_UI_CONTENT_CREATE_FUNC));
137    if (entry == nullptr) {
138        FREELIB(handle);
139        return nullptr;
140    }
141
142    auto content = entry(instanceId);
143    return content;
144}
145
146std::string UIContent::GetCurrentUIStackInfo()
147{
148    LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
149    if (handle == nullptr) {
150        return std::string();
151    }
152
153    auto entry = reinterpret_cast<GetCurrentUIStackInfoFunction>(LOADSYM(handle, "OHOS_ACE_GetCurrentUIStackInfo"));
154    if (entry == nullptr) {
155        FREELIB(handle);
156        return std::string();
157    }
158
159    auto content = entry();
160    if (content == nullptr) {
161        return std::string();
162    }
163
164    return content;
165}
166} // namespace OHOS::Ace
167