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 
25 namespace OHOS::Ace {
26 
27 using CreateCardFunc = UIContent* (*)(void*, void*, bool);
28 using CreateFunc = UIContent* (*)(void*, void*);
29 using CreateFunction = UIContent* (*)(void*);
30 using GetUIContentFunc = UIContent* (*)(int32_t);
31 using GetCurrentUIStackInfoFunction = char* (*)();
32 constexpr char UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateUIContent";
33 constexpr char Card_CREATE_FUNC[] = "OHOS_ACE_CreateFormContent";
34 constexpr char SUB_WINDOW_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateSubWindowUIContent";
35 constexpr char GET_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_GetUIContent";
36 
37 OHOS::AbilityRuntime::Context* context_ = nullptr;
38 
CreateUIContent(void * context,void * runtime,bool isFormRender)39 UIContent* 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 
CreateUIContent(void * context,void * runtime)56 UIContent* 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 
CreateUIContent(void * ability)77 UIContent* 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 
Create(OHOS::AbilityRuntime::Context * context,NativeEngine * runtime)98 std::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 
Create(OHOS::AbilityRuntime::Context * context,NativeEngine * runtime,bool isFormRender)105 std::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 
Create(OHOS::AppExecFwk::Ability * ability)113 std::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 
ShowDumpHelp(std::vector<std::string> & info)120 void 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 
GetUIContent(int32_t instanceId)129 UIContent* 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 
GetCurrentUIStackInfo()146 std::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