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