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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_JS_COMMAND_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_JS_COMMAND_H
18
19 #include <string>
20 #include <unordered_map>
21
22 #include "base/memory/referenced.h"
23 #include "base/utils/macros.h"
24 #include "core/accessibility/accessibility_manager.h"
25 #include "core/pipeline/pipeline_context.h"
26 #include "frameworks/bridge/common/dom/dom_badge.h"
27 #include "frameworks/bridge/common/dom/dom_canvas.h"
28 #include "frameworks/bridge/common/dom/dom_chart.h"
29 #include "frameworks/bridge/common/dom/dom_clock.h"
30 #include "frameworks/bridge/common/dom/dom_document.h"
31 #include "frameworks/bridge/common/dom/dom_image_animator.h"
32 #include "frameworks/bridge/common/dom/dom_input.h"
33 #include "frameworks/bridge/common/dom/dom_proxy.h"
34 #include "frameworks/bridge/common/dom/dom_stepper.h"
35 #include "frameworks/bridge/common/dom/dom_stepper_item.h"
36 #include "frameworks/bridge/common/dom/dom_xcomponent.h"
37
38 namespace OHOS::Ace::Framework {
39
40 class JsAcePage;
41
42 // Basic class of command from JS framework
43 class ACE_EXPORT JsCommand : public Referenced {
44 public:
JsCommand()45 JsCommand() : Referenced(false) {}
46 ~JsCommand() override = default;
47
48 virtual void Execute(const RefPtr<JsAcePage>& page) const = 0;
49 };
50
51 class ACE_EXPORT JsCommandDomElementOperator : public JsCommand {
52 public:
53 ~JsCommandDomElementOperator() override = default;
54
SetAttributes(std::vector<std::pair<std::string,std::string>> && attrs)55 void SetAttributes(std::vector<std::pair<std::string, std::string>>&& attrs)
56 {
57 attrs_ = std::move(attrs);
58 }
59
SetStyles(std::vector<std::pair<std::string,std::string>> && styles)60 void SetStyles(std::vector<std::pair<std::string, std::string>>&& styles)
61 {
62 styles_ = std::move(styles);
63 }
64
AddEvents(std::vector<std::string> && events)65 void AddEvents(std::vector<std::string>&& events)
66 {
67 events_ = std::move(events);
68 }
69
SetAnimationStyles(std::vector<std::unordered_map<std::string,std::string>> && animationStyles)70 void SetAnimationStyles(std::vector<std::unordered_map<std::string, std::string>>&& animationStyles)
71 {
72 if (!animationStyles.empty()) {
73 animationStyles_ =
74 std::make_unique<std::vector<std::unordered_map<std::string, std::string>>>(std::move(animationStyles));
75 }
76 }
77
SetTransitionEnter(std::vector<std::unordered_map<std::string,std::string>> && transitionEnter)78 void SetTransitionEnter(std::vector<std::unordered_map<std::string, std::string>>&& transitionEnter)
79 {
80 if (!transitionEnter.empty()) {
81 transitionEnter_ =
82 std::make_unique<std::vector<std::unordered_map<std::string, std::string>>>(std::move(transitionEnter));
83 }
84 }
85
SetTransitionExit(std::vector<std::unordered_map<std::string,std::string>> && transitionExit)86 void SetTransitionExit(std::vector<std::unordered_map<std::string, std::string>>&& transitionExit)
87 {
88 if (!transitionExit.empty()) {
89 transitionExit_ =
90 std::make_unique<std::vector<std::unordered_map<std::string, std::string>>>(std::move(transitionExit));
91 }
92 }
93
SetSharedTransitionName(std::vector<std::unordered_map<std::string,std::string>> && sharedTransitionName)94 void SetSharedTransitionName(std::vector<std::unordered_map<std::string, std::string>>&& sharedTransitionName)
95 {
96 if (!sharedTransitionName.empty()) {
97 sharedTransitionName_ = std::make_unique<std::vector<std::unordered_map<std::string, std::string>>>(
98 std::move(sharedTransitionName));
99 }
100 }
101
SetId(const std::string & id)102 void SetId(const std::string& id)
103 {
104 id_ = id;
105 }
106
SetShareId(const std::string & shareId)107 void SetShareId(const std::string& shareId)
108 {
109 shareId_ = shareId;
110 }
111
SetTarget(const std::string & target)112 void SetTarget(const std::string& target)
113 {
114 target_ = target;
115 }
116
SetSegments(const std::vector<Segment> & segments)117 void SetSegments(const std::vector<Segment>& segments)
118 {
119 segments_ = std::make_unique<std::vector<Segment>>(segments);
120 }
121
SetOptions(const ChartOptions & chartOptions)122 void SetOptions(const ChartOptions& chartOptions)
123 {
124 chartOptions_ = std::make_unique<ChartOptions>(chartOptions);
125 }
126
SetDatasets(const std::vector<MainChart> & datasets)127 void SetDatasets(const std::vector<MainChart>& datasets)
128 {
129 if (!datasets.empty()) {
130 chartDatasets_ = std::make_unique<std::vector<MainChart>>(datasets);
131 }
132 }
133
SetImagesAttr(std::vector<ImageProperties> && images)134 void SetImagesAttr(std::vector<ImageProperties>&& images)
135 {
136 if (!images.empty()) {
137 images_ = std::make_unique<std::vector<ImageProperties>>(std::move(images));
138 }
139 }
140
SetClockConfig(const ClockConfig & clockConfig)141 void SetClockConfig(const ClockConfig& clockConfig)
142 {
143 clockConfig_ = std::make_unique<ClockConfig>(clockConfig);
144 }
145
SetBadgeConfig(const BadgeConfig & badgeConfig)146 void SetBadgeConfig(const BadgeConfig& badgeConfig)
147 {
148 badgeConfig_ = std::make_unique<BadgeConfig>(badgeConfig);
149 }
150
SetStepperLabel(const StepperLabels & stepperLabel)151 void SetStepperLabel(const StepperLabels& stepperLabel)
152 {
153 stepperLabel_ = std::make_unique<StepperLabels>(stepperLabel);
154 }
155
SetInputOptions(const std::vector<InputOption> & inputOptions)156 void SetInputOptions(const std::vector<InputOption>& inputOptions)
157 {
158 inputOptions_ = std::make_unique<std::vector<InputOption>>(inputOptions);
159 }
160
SetForIndex(const int32_t & itemIndex)161 void SetForIndex(const int32_t& itemIndex)
162 {
163 itemIndex_ = itemIndex;
164 }
165
SetPipelineContext(const WeakPtr<PipelineContext> & pipelineContext)166 void SetPipelineContext(const WeakPtr<PipelineContext>& pipelineContext)
167 {
168 pipelineContext_ = pipelineContext;
169 }
170
SetIsCustomComponent(bool isCustom)171 void SetIsCustomComponent(bool isCustom)
172 {
173 isCustomComponent_ = isCustom;
174 }
175
176 protected:
JsCommandDomElementOperator(NodeId nodeId)177 explicit JsCommandDomElementOperator(NodeId nodeId) : nodeId_(nodeId) {}
178
179 void UpdateForChart(const RefPtr<DOMNode>& node) const;
180 void UpdateForImageAnimator(const RefPtr<DOMNode>& node) const;
181 void UpdateForClock(const RefPtr<DOMNode>& node) const;
182 void UpdateForBadge(const RefPtr<DOMNode>& node) const;
183 void UpdateForStepperLabel(const RefPtr<DOMNode>& node) const;
184 void UpdateForInput(const RefPtr<DOMNode>& node) const;
185
186 NodeId nodeId_ = -1;
187 int32_t itemIndex_ = -1;
188 std::vector<std::pair<std::string, std::string>> attrs_;
189 std::vector<std::pair<std::string, std::string>> styles_;
190 std::vector<std::string> events_;
191 bool isCustomComponent_ = false;
192 std::unique_ptr<std::vector<std::unordered_map<std::string, std::string>>> animationStyles_;
193 std::unique_ptr<std::vector<std::unordered_map<std::string, std::string>>> transitionEnter_;
194 std::unique_ptr<std::vector<std::unordered_map<std::string, std::string>>> transitionExit_;
195 std::unique_ptr<std::vector<std::unordered_map<std::string, std::string>>> sharedTransitionName_;
196 std::string id_;
197 std::string shareId_;
198 std::string target_;
199 std::unique_ptr<std::vector<Segment>> segments_;
200 std::unique_ptr<ChartOptions> chartOptions_;
201 std::unique_ptr<std::vector<MainChart>> chartDatasets_;
202 std::unique_ptr<std::vector<ImageProperties>> images_;
203 std::unique_ptr<ClockConfig> clockConfig_;
204 std::unique_ptr<BadgeConfig> badgeConfig_;
205 std::unique_ptr<StepperLabels> stepperLabel_;
206 std::unique_ptr<std::vector<InputOption>> inputOptions_;
207 WeakPtr<PipelineContext> pipelineContext_;
208 bool useLiteStyle_ = false;
209 };
210
211 class ACE_EXPORT JsCommandDomElementCreator : public JsCommandDomElementOperator {
212 public:
213 ~JsCommandDomElementCreator() override = default;
214
215 protected:
JsCommandDomElementCreator(const std::string & tagName,NodeId nodeId)216 JsCommandDomElementCreator(const std::string& tagName, NodeId nodeId)
217 : JsCommandDomElementOperator(nodeId), tagName_(tagName)
218 {}
219
220 RefPtr<DOMNode> CreateDomNode(const RefPtr<JsAcePage>& page, NodeId parentNodeId = -1) const;
221
222 void MountDomNode(const RefPtr<DOMNode>& node, const RefPtr<DOMDocument>& domDocument, NodeId parentNodeId) const;
223
224 RefPtr<DOMProxy> CreateDomProxy(const RefPtr<DOMDocument>& domDocument, NodeId parentNodeId = -1) const;
225
226 void ScheduleUpdateForFixedNode(const RefPtr<DOMDocument>& domDocument) const;
227
228 RefPtr<DOMNode> CreateDomElement(const RefPtr<JsAcePage>& page) const;
229 std::string tagName_;
230 };
231
232 // JS command, which used to create body of DOM tree.
233 class ACE_EXPORT JsCommandCreateDomBody final : public JsCommandDomElementCreator {
234 public:
JsCommandCreateDomBody(const std::string & tagName,NodeId nodeId)235 JsCommandCreateDomBody(const std::string& tagName, NodeId nodeId) : JsCommandDomElementCreator(tagName, nodeId) {}
236 ~JsCommandCreateDomBody() override = default;
237
238 void Execute(const RefPtr<JsAcePage>& page) const final;
239 };
240
241 // JS command, which used to create a new element
242 class ACE_EXPORT JsCommandCreateDomElement final : public JsCommandDomElementCreator {
243 public:
JsCommandCreateDomElement(const std::string & tagName,NodeId nodeId)244 JsCommandCreateDomElement(const std::string& tagName, NodeId nodeId)
245 : JsCommandDomElementCreator(tagName, nodeId) {}
246 ~JsCommandCreateDomElement() override = default;
247 void Execute(const RefPtr<JsAcePage>& page) const final;
248 };
249
250 // JS command, which used to create and add new element into DOM tree.
251 class ACE_EXPORT JsCommandAddDomElement final : public JsCommandDomElementCreator {
252 public:
JsCommandAddDomElement(const std::string & tagName,NodeId nodeId,NodeId parentNodeId)253 JsCommandAddDomElement(const std::string& tagName, NodeId nodeId, NodeId parentNodeId)
254 : JsCommandDomElementCreator(tagName, nodeId), parentNodeId_(parentNodeId)
255 {}
256 ~JsCommandAddDomElement() override = default;
257
258 void Execute(const RefPtr<JsAcePage>& page) const final;
259
260 private:
261 NodeId parentNodeId_ = -1;
262 };
263
264 class ACE_EXPORT JsCommandAppendElement final : public JsCommandDomElementCreator {
265 public:
JsCommandAppendElement(const std::string & tagName,NodeId nodeId,NodeId parentNodeId)266 JsCommandAppendElement(const std::string& tagName, NodeId nodeId, NodeId parentNodeId)
267 : JsCommandDomElementCreator(tagName, nodeId), parentNodeId_(parentNodeId)
268 {}
269 ~JsCommandAppendElement() override = default;
270
271 void Execute(const RefPtr<JsAcePage>& page) const final;
272
273 private:
274 NodeId parentNodeId_ = -1;
275 };
276
277 // JS command, which used to remove element from DOM tree.
278 class ACE_EXPORT JsCommandRemoveDomElement final : public JsCommand {
279 public:
JsCommandRemoveDomElement(NodeId nodeId)280 explicit JsCommandRemoveDomElement(NodeId nodeId) : nodeId_(nodeId) {}
281 ~JsCommandRemoveDomElement() final = default;
282
283 void Execute(const RefPtr<JsAcePage>& page) const final;
284
285 private:
286 NodeId nodeId_ = -1;
287 };
288
289 // JS command, which used to update attributes of element in DOM tree.
290 class ACE_EXPORT JsCommandUpdateDomElementAttrs final : public JsCommandDomElementOperator {
291 public:
JsCommandUpdateDomElementAttrs(NodeId nodeId)292 explicit JsCommandUpdateDomElementAttrs(NodeId nodeId) : JsCommandDomElementOperator(nodeId) {}
293 ~JsCommandUpdateDomElementAttrs() final = default;
294
295 void Execute(const RefPtr<JsAcePage>& page) const final;
296 };
297
298 // JS command, which used to update styles of element in DOM tree.
299 class ACE_EXPORT JsCommandUpdateDomElementStyles final : public JsCommandDomElementOperator {
300 public:
JsCommandUpdateDomElementStyles(NodeId nodeId)301 explicit JsCommandUpdateDomElementStyles(NodeId nodeId) : JsCommandDomElementOperator(nodeId) {}
302 ~JsCommandUpdateDomElementStyles() final = default;
303
304 void Execute(const RefPtr<JsAcePage>& page) const final;
305 };
306
307 // JS command, which used to call native method of element in DOM tree.
308 class ACE_EXPORT JsCommandCallDomElementMethod final : public JsCommand {
309 public:
JsCommandCallDomElementMethod(NodeId nodeId,const std::string & method,const std::string & param)310 JsCommandCallDomElementMethod(NodeId nodeId, const std::string& method, const std::string& param)
311 : nodeId_(nodeId), method_(method), param_(param)
312 {}
313 ~JsCommandCallDomElementMethod() final = default;
314
315 void Execute(const RefPtr<JsAcePage>& page) const final;
316
317 private:
318 NodeId nodeId_ = -1;
319 std::string method_;
320 std::string param_;
321 };
322
323 class ACE_EXPORT JsCommandContextOperation final : public JsCommand {
324 public:
JsCommandContextOperation(NodeId nodeId,std::function<void (const RefPtr<CanvasTaskPool> &)> task)325 JsCommandContextOperation(NodeId nodeId, std::function<void(const RefPtr<CanvasTaskPool>&)> task)
326 : nodeId_(nodeId), task_(std::move(task))
327 {}
328 ~JsCommandContextOperation() final = default;
329 void Execute(const RefPtr<JsAcePage>& page) const final;
330
331 private:
332 NodeId nodeId_ = -1;
333 std::function<void(const RefPtr<CanvasTaskPool>&)> task_;
334 };
335
336 class ACE_EXPORT JsCommandXComponentOperation final : public JsCommand {
337 public:
JsCommandXComponentOperation(NodeId nodeId,std::function<void (const RefPtr<XComponentTaskPool> &)> task)338 JsCommandXComponentOperation(NodeId nodeId, std::function<void(const RefPtr<XComponentTaskPool>&)> task)
339 : nodeId_(nodeId), task_(std::move(task))
340 {}
341 ~JsCommandXComponentOperation() final = default;
342 void Execute(const RefPtr<JsAcePage>& page) const final;
343
344 private:
345 NodeId nodeId_ = -1;
346 std::function<void(const RefPtr<XComponentTaskPool>&)> task_;
347 };
348
349 class ACE_EXPORT AnimationBridgeTask : public AceType {
350 DECLARE_ACE_TYPE(AnimationBridgeTask, AceType)
351
352 public:
353 AnimationBridgeTask() = default;
354 ~AnimationBridgeTask() override = default;
355 virtual void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>&, NodeId) = 0;
356 };
357
358 class ACE_EXPORT JsCommandAnimation final : public JsCommand {
359 public:
JsCommandAnimation(NodeId nodeId,RefPtr<AnimationBridgeTask> task)360 JsCommandAnimation(NodeId nodeId, RefPtr<AnimationBridgeTask> task) : nodeId_(nodeId), task_(std::move(task)) {}
361 ~JsCommandAnimation() final = default;
362
363 void Execute(const RefPtr<JsAcePage>& page) const final;
364
365 private:
366 NodeId nodeId_ = -1;
367 RefPtr<AnimationBridgeTask> task_;
368 };
369
370 class ACE_EXPORT AnimatorBridgeTask : public AceType {
371 DECLARE_ACE_TYPE(AnimatorBridgeTask, AceType)
372
373 public:
374 AnimatorBridgeTask() = default;
375 ~AnimatorBridgeTask() override = default;
376 virtual void AnimatorBridgeTaskFunc(const RefPtr<JsAcePage>& page, int32_t bridgeId) = 0;
377 };
378
379 class ACE_EXPORT JsCommandAnimator final : public JsCommand {
380 public:
JsCommandAnimator(int32_t bridgeId,RefPtr<AnimatorBridgeTask> task)381 JsCommandAnimator(int32_t bridgeId, RefPtr<AnimatorBridgeTask> task) : bridgeId_(bridgeId),
382 task_(std::move(task)) {}
383 ~JsCommandAnimator() final = default;
384
385 void Execute(const RefPtr<JsAcePage>& page) const final;
386
387 private:
388 int32_t bridgeId_ = -1;
389 RefPtr<AnimatorBridgeTask> task_;
390 };
391
392 } // namespace OHOS::Ace::Framework
393
394 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_JS_COMMAND_H
395