1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_H
18 
19 #include <string>
20 
21 #include "base/geometry/size.h"
22 #include "base/utils/utils.h"
23 #include "core/components/declaration/xcomponent/xcomponent_declaration.h"
24 #include "core/components/xcomponent/native_interface_xcomponent_impl.h"
25 #include "core/components/xcomponent/resource/native_texture.h"
26 #include "core/components_ng/pattern/xcomponent/inner_xcomponent_controller.h"
27 #include "core/pipeline/base/element.h"
28 
29 namespace OHOS::Ace {
30 class XComponentDelegate;
31 class RenderXComponent;
32 class XComponentComponent;
33 
34 using TaskFunction = std::function<void(RenderXComponent&, const Offset&)>;
35 using PushTaskFunction = std::function<void(const TaskFunction&)>;
36 
37 class ACE_EXPORT XComponentTaskPool : virtual public AceType {
38     DECLARE_ACE_TYPE(XComponentTaskPool, AceType);
39 
40 public:
PushTask(const TaskFunction & task)41     void PushTask(const TaskFunction& task)
42     {
43         if (pushToRenderNodeFunc_) {
44             pushToRenderNodeFunc_(task);
45         } else {
46             tasks_.emplace_back(task);
47         }
48     }
49 
SetPushToRenderNodeFunc(const PushTaskFunction & pushTaskFunc)50     void SetPushToRenderNodeFunc(const PushTaskFunction& pushTaskFunc)
51     {
52         pushToRenderNodeFunc_ = pushTaskFunc;
53     }
54 
GetTasks()55     const std::list<TaskFunction>& GetTasks() const
56     {
57         return tasks_;
58     }
59 
ClearTasks()60     void ClearTasks()
61     {
62         tasks_.clear();
63     }
64 
65     void SetRenderNode(const WeakPtr<RenderXComponent>& renderNode);
66     void NativeXComponentInit(
67         OH_NativeXComponent* nativeXComponent, WeakPtr<NativeXComponentImpl> nativeXComponentImpl);
68 
69     WeakPtr<XComponentComponent> component_;
70 
71 private:
72     PushTaskFunction pushToRenderNodeFunc_;
73     std::list<TaskFunction> tasks_;
74     WeakPtr<RenderXComponent> renderNode_;
75 };
76 
77 // A component can show different native view.
78 class ACE_EXPORT XComponentComponent : public RenderComponent {
79     DECLARE_ACE_TYPE(XComponentComponent, RenderComponent);
80 
81 public:
82     using CreatedCallback = std::function<void()>;
83     using ReleasedCallback = std::function<void(bool)>;
84     using DeleteCallback = std::function<void()>;
85     using ErrorCallback = std::function<void(const std::string&, const std::string&)>;
86     using MethodCall = std::function<void(const std::string&)>;
87     using Method = std::string;
88 
89     XComponentComponent() = default;
90     explicit XComponentComponent(const std::string& type);
~XComponentComponent()91     ~XComponentComponent() override
92     {
93         if (deleteCallback_) {
94             deleteCallback_();
95         }
96     }
97 
98     RefPtr<RenderNode> CreateRenderNode() override;
99     RefPtr<Element> CreateElement() override;
100 
SetType(const std::string & type)101     void SetType(const std::string& type)
102     {
103         type_ = type;
104     }
105 
GetType()106     const std::string& GetType() const
107     {
108         return type_;
109     }
110 
SetName(const std::string & name)111     void SetName(const std::string& name)
112     {
113         declaration_->SetXComponentName(name);
114     }
115 
GetName()116     const std::string& GetName() const
117     {
118         return declaration_->GetXComponentName();
119     }
120 
SetId(const std::string & id)121     void SetId(const std::string& id)
122     {
123         declaration_->SetXComponentId(id);
124     }
125 
GetId()126     const std::string& GetId() const
127     {
128         return declaration_->GetXComponentId();
129     }
130 
SetXComponentType(const std::string & type)131     void SetXComponentType(const std::string& type)
132     {
133         declaration_->SetXComponentType(type);
134     }
135 
GetXComponentType()136     const std::string& GetXComponentType() const
137     {
138         return declaration_->GetXComponentType();
139     }
140 
SetLibraryName(const std::string & libraryName)141     void SetLibraryName(const std::string& libraryName)
142     {
143         declaration_->SetXComponentLibraryName(libraryName);
144     }
145 
GetLibraryName()146     const std::string& GetLibraryName() const
147     {
148         return declaration_->GetXComponentLibraryName();
149     }
150 
SetXComponentInitEventId(const EventMarker & xComponentInitEventId)151     void SetXComponentInitEventId(const EventMarker& xComponentInitEventId)
152     {
153         declaration_->SetXComponentInitEventId(xComponentInitEventId);
154     }
155 
GetXComponentInitEventId()156     const EventMarker& GetXComponentInitEventId() const
157     {
158         return declaration_->GetXComponentInitEventId();
159     }
160 
SetXComponentDestroyEventId(const EventMarker & xComponentDestroyEventId)161     void SetXComponentDestroyEventId(const EventMarker& xComponentDestroyEventId)
162     {
163         declaration_->SetXComponentDestroyEventId(xComponentDestroyEventId);
164     }
165 
GetXComponentDestroyEventId()166     const EventMarker& GetXComponentDestroyEventId() const
167     {
168         return declaration_->GetXComponentDestroyEventId();
169     }
170 
SetDeclaration(const RefPtr<XComponentDeclaration> & declaration)171     void SetDeclaration(const RefPtr<XComponentDeclaration>& declaration)
172     {
173         if (declaration) {
174             declaration_ = declaration;
175         }
176     }
177 
GetTaskPool()178     const RefPtr<XComponentTaskPool>& GetTaskPool() const
179     {
180         return pool_;
181     }
182 
GetTextureId()183     int64_t GetTextureId() const
184     {
185         return textureId_;
186     }
187 
SetTextureId(int64_t textureId)188     void SetTextureId(int64_t textureId)
189     {
190         textureId_ = textureId;
191     }
192 
GetNodeId()193     int32_t GetNodeId() const
194     {
195         return nodeId_;
196     }
197 
SetNodeId(int32_t nodeId)198     void SetNodeId(int32_t nodeId)
199     {
200         nodeId_ = nodeId;
201     }
202 
GetTexture()203     RefPtr<NativeTexture> GetTexture() const
204     {
205         return texture_;
206     }
207 
SetTexture(RefPtr<NativeTexture> texture)208     void SetTexture(RefPtr<NativeTexture> texture)
209     {
210         texture_ = texture;
211     }
212 
GetXComponentController()213     std::shared_ptr<InnerXComponentController> GetXComponentController() const
214     {
215         return xcomponentController_;
216     }
217 
SetXComponentController(const std::shared_ptr<InnerXComponentController> & xcomponentController)218     void SetXComponentController(const std::shared_ptr<InnerXComponentController>& xcomponentController)
219     {
220         xcomponentController_ = xcomponentController;
221     }
222 
RegisterDeleteCallback(DeleteCallback && callback)223     void RegisterDeleteCallback(DeleteCallback&& callback)
224     {
225         deleteCallback_ = std::move(callback);
226     }
227 
SetDeleteCallbackToNull()228     void SetDeleteCallbackToNull()
229     {
230         deleteCallback_ = nullptr;
231     }
232 
GetSoPath()233     const std::optional<std::string>& GetSoPath() const
234     {
235         return soPath_;
236     }
237 
SetSoPath(const std::string & soPath)238     void SetSoPath(const std::string& soPath)
239     {
240         soPath_ = soPath;
241     }
242 
243 #ifdef OHOS_STANDARD_SYSTEM
SetNativeWindow(void * nativeWindow)244     void SetNativeWindow(void* nativeWindow)
245     {
246         nativeWindow_ = nativeWindow;
247     }
248 
GetNativeWindow()249     void* GetNativeWindow()
250     {
251         return nativeWindow_;
252     }
253 #endif
254 
255 private:
256     RefPtr<XComponentDeclaration> declaration_;
257     CreatedCallback createdCallback_ = nullptr;
258     ReleasedCallback releasedCallback_ = nullptr;
259     DeleteCallback deleteCallback_ = nullptr;
260     ErrorCallback errorCallback_ = nullptr;
261     RefPtr<XComponentTaskPool> pool_;
262     std::shared_ptr<InnerXComponentController> xcomponentController_;
263     std::string type_;
264     std::optional<std::string> soPath_;
265     int64_t textureId_ = -1;
266     int32_t nodeId_ = -1;
267     RefPtr<NativeTexture> texture_;
268 #ifdef OHOS_STANDARD_SYSTEM
269     void* nativeWindow_ = nullptr;
270 #endif
271 };
272 } // namespace OHOS::Ace
273 
274 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_XCOMPONENT_COMPONENT_H
275