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 <dlfcn.h>
19 #include <vector>
20
21 namespace OHOS::Ace {
22
23 using CreateCardFunc = UIContent* (*)(void*, void*, bool);
24 using CreateFunc = UIContent* (*)(void*, void*);
25 using CreateFunction = UIContent* (*)(void*);
26 constexpr char UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateUIContent";
27 constexpr char Card_CREATE_FUNC[] = "OHOS_ACE_CreateFormContent";
28 constexpr char SUB_WINDOW_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateSubWindowUIContent";
29
30 OHOS::AbilityRuntime::Context* context_ = nullptr;
31
CreateUIContent(void * context,void * runtime,bool isFormRender)32 UIContent* CreateUIContent(void* context, void* runtime, bool isFormRender)
33 {
34 void* handle = dlopen("libace.z.so", RTLD_LAZY);
35 if (handle == nullptr) {
36 return nullptr;
37 }
38
39 auto entry = reinterpret_cast<CreateCardFunc>(dlsym(handle, Card_CREATE_FUNC));
40 if (entry == nullptr) {
41 dlclose(handle);
42 return nullptr;
43 }
44
45 auto content = entry(context, runtime, isFormRender);
46 if (content == nullptr) {
47 dlclose(handle);
48 }
49
50 return content;
51 }
52
CreateUIContent(void * context,void * runtime)53 UIContent* CreateUIContent(void* context, void* runtime)
54 {
55 void* handle = dlopen("libace.z.so", RTLD_LAZY);
56 if (handle == nullptr) {
57 return nullptr;
58 }
59
60 auto entry = reinterpret_cast<CreateFunc>(dlsym(handle, UI_CONTENT_CREATE_FUNC));
61 if (entry == nullptr) {
62 dlclose(handle);
63 return nullptr;
64 }
65
66 auto content = entry(context, runtime);
67 if (content == nullptr) {
68 dlclose(handle);
69 }
70
71 return content;
72 }
73
CreateUIContent(void * ability)74 UIContent* CreateUIContent(void* ability)
75 {
76 void* handle = dlopen("libace.z.so", RTLD_LAZY);
77 if (handle == nullptr) {
78 return nullptr;
79 }
80
81 auto entry = reinterpret_cast<CreateFunction>(dlsym(handle, SUB_WINDOW_UI_CONTENT_CREATE_FUNC));
82 if (entry == nullptr) {
83 dlclose(handle);
84 return nullptr;
85 }
86
87 auto content = entry(ability);
88 if (content == nullptr) {
89 dlclose(handle);
90 }
91
92 return content;
93 }
94
Create(OHOS::AbilityRuntime::Context * context,NativeEngine * runtime)95 std::unique_ptr<UIContent> UIContent::Create(OHOS::AbilityRuntime::Context* context, NativeEngine* runtime)
96 {
97 std::unique_ptr<UIContent> content;
98 content.reset(CreateUIContent(reinterpret_cast<void*>(context), reinterpret_cast<void*>(runtime)));
99 return content;
100 }
101
Create(OHOS::AbilityRuntime::Context * context,NativeEngine * runtime,bool isFormRender)102 std::unique_ptr<UIContent> UIContent::Create(OHOS::AbilityRuntime::Context* context,
103 NativeEngine* runtime,
104 bool isFormRender)
105 {
106 std::unique_ptr<UIContent> content;
107 content.reset(CreateUIContent(reinterpret_cast<void*>(context), reinterpret_cast<void*>(runtime), isFormRender));
108 return content;
109 }
110
Create(OHOS::AppExecFwk::Ability * ability)111 std::unique_ptr<UIContent> UIContent::Create(OHOS::AppExecFwk::Ability* ability)
112 {
113 std::unique_ptr<UIContent> content;
114 content.reset(CreateUIContent(reinterpret_cast<void*>(ability)));
115 return content;
116 }
117
ShowDumpHelp(std::vector<std::string> & info)118 void UIContent::ShowDumpHelp(std::vector<std::string>& info)
119 {
120 info.emplace_back(" -element |show element tree");
121 info.emplace_back(" -render |show render tree");
122 info.emplace_back(" -inspector |show inspector tree");
123 info.emplace_back(" -frontend |show path and components count of current page");
124 }
125
126 } // namespace OHOS::Ace
127