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