• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "base/memory/referenced.h"
20 #include "base/utils/utils.h"
21 #include "bridge/common/utils/engine_helper.h"
22 #include "bridge/declarative_frontend/engine/js_converter.h"
23 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
25 #include "bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
26 #include "bridge/declarative_frontend/jsview/models/xcomponent_model_impl.h"
27 #include "core/components/common/layout/constants.h"
28 #include "core/components_ng/base/view_stack_processor.h"
29 #include "core/components_ng/pattern/xcomponent/xcomponent_model.h"
30 #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h"
31 #include "frameworks/core/components_ng/base/view_abstract_model.h"
32 #include "frameworks/core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
33 
34 namespace OHOS::Ace {
35 namespace {
ConvertToXComponentType(const std::string & type)36 XComponentType ConvertToXComponentType(const std::string& type)
37 {
38     if (type == "surface") {
39         return XComponentType::SURFACE;
40     }
41     if (type == "component") {
42         return XComponentType::COMPONENT;
43     }
44     if (type == "node") {
45         return XComponentType::NODE;
46     }
47     return XComponentType::SURFACE;
48 }
49 } // namespace
50 
GetInstance()51 XComponentModel* XComponentModel::GetInstance()
52 {
53 #ifdef NG_BUILD
54     static NG::XComponentModelNG instance;
55     return &instance;
56 #else
57     if (Container::IsCurrentUseNewPipeline()) {
58         static NG::XComponentModelNG instance;
59         return &instance;
60     } else {
61         static Framework::XComponentModelImpl instance;
62         return &instance;
63     }
64 #endif
65 }
66 } // namespace OHOS::Ace
67 
68 namespace OHOS::Ace::Framework {
SetControllerOnCreated(const WeakPtr<NG::FrameNode> & targetNode,const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)69 void SetControllerOnCreated(
70     const WeakPtr<NG::FrameNode>& targetNode, const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
71 {
72     auto jsCreatedFunc = object->GetProperty("onSurfaceCreated");
73     if (jsCreatedFunc->IsFunction()) {
74         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsCreatedFunc));
75         auto onSurfaceCreated = [execCtx, func = std::move(jsFunc), node = targetNode](
76                                     const std::string& surfaceId, const std::string& xcomponentId) {
77             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
78             ACE_SCORING_EVENT("XComponentController.onSurfaceCreated");
79             PipelineContext::SetCallBackNode(node);
80             auto jsVal = JSRef<JSVal>::Make(ToJSValue(surfaceId));
81             func->ExecuteJS(1, &jsVal);
82             TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] ControllerOnCreated surfaceId:%{public}s",
83                 xcomponentId.c_str(), surfaceId.c_str());
84         };
85         XComponentModel::GetInstance()->SetControllerOnCreated(std::move(onSurfaceCreated));
86     }
87 }
88 
SetControllerOnChanged(const WeakPtr<NG::FrameNode> & targetNode,const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)89 void SetControllerOnChanged(
90     const WeakPtr<NG::FrameNode>& targetNode, const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
91 {
92     auto jsChangedFunc = object->GetProperty("onSurfaceChanged");
93     if (jsChangedFunc->IsFunction()) {
94         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsChangedFunc));
95         auto onSurfaceChanged = [execCtx, func = std::move(jsFunc), node = targetNode](
96                                     const std::string& surfaceId, const NG::RectF& rect) {
97             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
98             ACE_SCORING_EVENT("XComponentController.onSurfaceChanged");
99             PipelineContext::SetCallBackNode(node);
100             JSRef<JSObject> rectObj = JSRef<JSObject>::New();
101             rectObj->SetProperty("offsetX", rect.Left());
102             rectObj->SetProperty("offsetY", rect.Top());
103             rectObj->SetProperty("surfaceWidth", rect.Width());
104             rectObj->SetProperty("surfaceHeight", rect.Height());
105             auto jsSurfaceId = JSRef<JSVal>::Make(ToJSValue(surfaceId));
106             JSRef<JSVal> params[2] = { jsSurfaceId, rectObj };
107             func->ExecuteJS(2, params);
108         };
109         XComponentModel::GetInstance()->SetControllerOnChanged(std::move(onSurfaceChanged));
110     }
111 }
112 
SetControllerOnDestroyed(const WeakPtr<NG::FrameNode> & targetNode,const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)113 void SetControllerOnDestroyed(
114     const WeakPtr<NG::FrameNode>& targetNode, const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
115 {
116     auto jsDestroyedFunc = object->GetProperty("onSurfaceDestroyed");
117     if (jsDestroyedFunc->IsFunction()) {
118         auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(object), JSRef<JSFunc>::Cast(jsDestroyedFunc));
119         auto onSurfaceDestroyed = [execCtx, func = std::move(jsFunc), node = targetNode](
120                                       const std::string& surfaceId, const std::string& xcomponentId) {
121             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
122             ACE_SCORING_EVENT("XComponentController.onSurfaceDestroyed");
123             PipelineContext::SetCallBackNode(node);
124             auto jsVal = JSRef<JSVal>::Make(ToJSValue(surfaceId));
125             func->ExecuteJS(1, &jsVal);
126             TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] ControllerOnDestroyed surfaceId:%{public}s",
127                 xcomponentId.c_str(), surfaceId.c_str());
128         };
129         XComponentModel::GetInstance()->SetControllerOnDestroyed(std::move(onSurfaceDestroyed));
130     }
131 }
132 
SetControllerCallback(const JSRef<JSObject> & object,const JsiExecutionContext & execCtx)133 void SetControllerCallback(const JSRef<JSObject>& object, const JsiExecutionContext& execCtx)
134 {
135     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
136     SetControllerOnCreated(targetNode, object, execCtx);
137     SetControllerOnChanged(targetNode, object, execCtx);
138     SetControllerOnDestroyed(targetNode, object, execCtx);
139 }
140 
GetXComponentController(const JSRef<JSObject> & controller,std::optional<std::string> & id,const JsiExecutionContext & execCtx)141 std::shared_ptr<InnerXComponentController> GetXComponentController(
142     const JSRef<JSObject>& controller, std::optional<std::string>& id, const JsiExecutionContext& execCtx)
143 {
144     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
145     auto* jsXComponentController = controller->Unwrap<JSXComponentController>();
146     if (jsXComponentController) {
147         jsXComponentController->SetInstanceId(Container::CurrentId());
148         if (id.has_value()) {
149             XComponentClient::GetInstance().AddControllerToJSXComponentControllersMap(
150                 id.value(), jsXComponentController);
151         }
152         xcomponentController = jsXComponentController->GetController();
153     }
154     return xcomponentController;
155 }
156 
JSBind(BindingTarget globalObj)157 void JSXComponent::JSBind(BindingTarget globalObj)
158 {
159     JSClass<JSXComponent>::Declare("XComponent");
160     JSClass<JSXComponent>::StaticMethod("create", &JSXComponent::Create);
161     JSClass<JSXComponent>::StaticMethod("onLoad", &JSXComponent::JsOnLoad);
162     JSClass<JSXComponent>::StaticMethod("onDestroy", &JSXComponent::JsOnDestroy);
163     JSClass<JSXComponent>::StaticMethod("onAppear", &JSXComponent::JsOnAppear);
164     JSClass<JSXComponent>::StaticMethod("onDisAppear", &JSXComponent::JsOnDisAppear);
165     JSClass<JSXComponent>::StaticMethod("onAttach", &JSXComponent::JsOnAttach);
166     JSClass<JSXComponent>::StaticMethod("onDetach", &JSXComponent::JsOnDetach);
167 
168     JSClass<JSXComponent>::StaticMethod("onTouch", &JSXComponent::JsOnTouch);
169     JSClass<JSXComponent>::StaticMethod("onClick", &JSXComponent::JsOnClick);
170     JSClass<JSXComponent>::StaticMethod("onKeyEvent", &JSXComponent::JsOnKeyEvent);
171     JSClass<JSXComponent>::StaticMethod("onMouse", &JSXComponent::JsOnMouse);
172     JSClass<JSXComponent>::StaticMethod("onHover", &JSXComponent::JsOnHover);
173     JSClass<JSXComponent>::StaticMethod("onFocus", &JSXComponent::JsOnFocus);
174     JSClass<JSXComponent>::StaticMethod("onBlur", &JSXComponent::JsOnBlur);
175 
176     JSClass<JSXComponent>::StaticMethod("backgroundColor", &JSXComponent::JsBackgroundColor);
177     JSClass<JSXComponent>::StaticMethod("backgroundImage", &JSXComponent::JsBackgroundImage);
178     JSClass<JSXComponent>::StaticMethod("backgroundImageSize", &JSXComponent::JsBackgroundImageSize);
179     JSClass<JSXComponent>::StaticMethod("backgroundImagePosition", &JSXComponent::JsBackgroundImagePosition);
180     JSClass<JSXComponent>::StaticMethod("opacity", &JSXComponent::JsOpacity);
181     JSClass<JSXComponent>::StaticMethod("blur", &JSXComponent::JsBlur);
182     JSClass<JSXComponent>::StaticMethod("backdropBlur", &JSXComponent::JsBackdropBlur);
183     JSClass<JSXComponent>::StaticMethod("grayscale", &JSXComponent::JsGrayscale);
184     JSClass<JSXComponent>::StaticMethod("brightness", &JSXComponent::JsBrightness);
185     JSClass<JSXComponent>::StaticMethod("saturate", &JSXComponent::JsSaturate);
186     JSClass<JSXComponent>::StaticMethod("contrast", &JSXComponent::JsContrast);
187     JSClass<JSXComponent>::StaticMethod("invert", &JSXComponent::JsInvert);
188     JSClass<JSXComponent>::StaticMethod("sepia", &JSXComponent::JsSepia);
189     JSClass<JSXComponent>::StaticMethod("hueRotate", &JSXComponent::JsHueRotate);
190     JSClass<JSXComponent>::StaticMethod("colorBlend", &JSXComponent::JsColorBlend);
191     JSClass<JSXComponent>::StaticMethod("sphericalEffect", &JSXComponent::JsSphericalEffect);
192     JSClass<JSXComponent>::StaticMethod("lightUpEffect", &JSXComponent::JsLightUpEffect);
193     JSClass<JSXComponent>::StaticMethod("pixelStretchEffect", &JSXComponent::JsPixelStretchEffect);
194     JSClass<JSXComponent>::StaticMethod("linearGradientBlur", &JSXComponent::JsLinearGradientBlur);
195     JSClass<JSXComponent>::StaticMethod("enableAnalyzer", &JSXComponent::JsEnableAnalyzer);
196     JSClass<JSXComponent>::StaticMethod("renderFit", &JSXComponent::JsRenderFit);
197     JSClass<JSXComponent>::StaticMethod("enableSecure", &JSXComponent::JsEnableSecure);
198     JSClass<JSXComponent>::StaticMethod("hdrBrightness", &JSXComponent::JsHdrBrightness);
199     JSClass<JSXComponent>::StaticMethod("blendMode", &JSXComponent::JsBlendMode);
200     JSClass<JSXComponent>::StaticMethod("enableTransparentLayer", &JSXComponent::JsEnableTransparentLayer);
201 
202     JSClass<JSXComponent>::InheritAndBind<JSContainerBase>(globalObj);
203 }
204 
Create(const JSCallbackInfo & info)205 void JSXComponent::Create(const JSCallbackInfo& info)
206 {
207     if (info.Length() < 1 || !info[0]->IsObject()) {
208         return;
209     }
210     XComponentOptions options;
211     JSRef<JSObject> controllerObj;
212     auto paramObject = JSRef<JSObject>::Cast(info[0]);
213     auto aiOptions = paramObject->GetProperty("imageAIOptions");
214     ExtractInfoToXComponentOptions(options, controllerObj, paramObject, info);
215     if (options.id == std::nullopt && options.xcomponentController == nullptr &&
216         (options.xcomponentType == XComponentType::SURFACE || options.xcomponentType == XComponentType::TEXTURE)) {
217         XComponentModel::GetInstance()->Create(options.xcomponentType);
218     } else {
219         XComponentModel::GetInstance()->Create(
220             options.id, options.xcomponentType, options.libraryName, options.xcomponentController);
221     }
222     if (!options.libraryName.has_value() && options.xcomponentController && !controllerObj->IsUndefined()) {
223         SetControllerCallback(controllerObj, info.GetExecutionContext());
224     }
225 
226     auto detachCallback = [](const std::string& xcomponentId) {
227         XComponentClient::GetInstance().DeleteControllerFromJSXComponentControllersMap(xcomponentId);
228         XComponentClient::GetInstance().DeleteFromJsValMapById(xcomponentId);
229     };
230     XComponentModel::GetInstance()->SetDetachCallback(std::move(detachCallback));
231 
232     if (info.Length() > 1 && info[1]->IsString()) {
233         auto soPath = info[1]->ToString();
234         XComponentModel::GetInstance()->SetSoPath(soPath);
235     }
236     ParseImageAIOptions(aiOptions);
237 
238     if (options.xcomponentType == XComponentType::SURFACE && options.screenId.has_value()) {
239         XComponentModel::GetInstance()->SetScreenId(options.screenId.value());
240     }
241 }
242 
ExtractInfoToXComponentOptions(XComponentOptions & options,JSRef<JSObject> & controllerObj,const JSRef<JSObject> & paramObject,const JSCallbackInfo & info)243 void JSXComponent::ExtractInfoToXComponentOptions(
244     XComponentOptions& options, JSRef<JSObject>& controllerObj,
245     const JSRef<JSObject>& paramObject, const JSCallbackInfo& info)
246 {
247     auto id = paramObject->GetProperty("id");
248     auto type = paramObject->GetProperty("type");
249     auto libraryNameValue = paramObject->GetProperty("libraryname");
250     auto controller = paramObject->GetProperty("controller");
251     auto screenIdValue = paramObject->GetProperty("screenId");
252 
253     if (id->IsString()) {
254         options.id = id->ToString();
255     }
256     if (libraryNameValue->IsString()) {
257         options.libraryName = libraryNameValue->ToString();
258     }
259     if (controller->IsObject()) {
260         controllerObj = JSRef<JSObject>::Cast(controller);
261         options.xcomponentController = GetXComponentController(controllerObj, options.id, info.GetExecutionContext());
262     }
263     if (type->IsString()) {
264         options.xcomponentType = ConvertToXComponentType(type->ToString());
265     } else if (type->IsNumber()) {
266         options.xcomponentType = static_cast<XComponentType>(type->ToNumber<int32_t>());
267     }
268     if (screenIdValue->IsNumber()) {
269         options.screenId = screenIdValue->ToNumber<uint64_t>();
270     }
271 }
272 
Create(const XComponentParams & params)273 void* JSXComponent::Create(const XComponentParams& params)
274 {
275     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
276     if (params.controller) {
277         xcomponentController = params.controller->GetController();
278     }
279     auto frameNode = AceType::DynamicCast<NG::FrameNode>(XComponentModel::GetInstance()->Create(params.elmtId,
280         static_cast<float>(params.width), static_cast<float>(params.height), params.xcomponentId,
281         static_cast<XComponentType>(params.xcomponentType), params.libraryName, xcomponentController));
282     frameNode->SetIsArkTsFrameNode(true);
283     auto pattern = frameNode->GetPattern<NG::XComponentPattern>();
284     CHECK_NULL_RETURN(pattern, nullptr);
285     pattern->SetRenderType(static_cast<NodeRenderType>(params.renderType));
286     pattern->SetExportTextureSurfaceId(params.surfaceId);
287 
288     auto container = Container::Current();
289     CHECK_NULL_RETURN(container, nullptr);
290     auto pipelineContext = AceType::DynamicCast<NG::PipelineContext>(container->GetPipelineContext());
291     CHECK_NULL_RETURN(pipelineContext, nullptr);
292     auto taskExecutor = pipelineContext->GetTaskExecutor();
293     CHECK_NULL_RETURN(taskExecutor, nullptr);
294     auto* jsXComponent = new JSXComponent();
295     jsXComponent->SetFrameNode(frameNode);
296     taskExecutor->PostTask(
297         [weak = AceType::WeakClaim(AceType::RawPtr(frameNode))]() {
298             auto frameNode = weak.Upgrade();
299             CHECK_NULL_VOID(frameNode);
300             auto xcPattern = frameNode->GetPattern<NG::XComponentPattern>();
301             CHECK_NULL_VOID(xcPattern);
302             xcPattern->XComponentSizeInit();
303             xcPattern->SetXcomponentInit(true);
304         },
305         TaskExecutor::TaskType::JS, "ArkUIXComponentCreate");
306 
307     return jsXComponent;
308 }
309 
ParseImageAIOptions(const JSRef<JSVal> & jsValue)310 void JSXComponent::ParseImageAIOptions(const JSRef<JSVal>& jsValue)
311 {
312     if (!jsValue->IsObject()) {
313         return;
314     }
315     auto engine = EngineHelper::GetCurrentEngine();
316     CHECK_NULL_VOID(engine);
317     NativeEngine* nativeEngine = engine->GetNativeEngine();
318     CHECK_NULL_VOID(nativeEngine);
319     panda::Local<JsiValue> value = jsValue.Get().GetLocalHandle();
320     JSValueWrapper valueWrapper = value;
321     ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
322     napi_value optionsValue = nativeEngine->ValueToNapiValue(valueWrapper);
323     XComponentModel::GetInstance()->SetImageAIOptions(optionsValue);
324 }
325 
ChangeRenderType(int32_t renderType)326 bool JSXComponent::ChangeRenderType(int32_t renderType)
327 {
328     auto xcFrameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
329     CHECK_NULL_RETURN(xcFrameNode, false);
330     auto pattern = xcFrameNode->GetPattern<NG::XComponentPattern>();
331     CHECK_NULL_RETURN(pattern, false);
332     return pattern->ChangeRenderType(static_cast<NodeRenderType>(renderType));
333 }
334 
JsOnLoad(const JSCallbackInfo & args)335 void JSXComponent::JsOnLoad(const JSCallbackInfo& args)
336 {
337     if (args.Length() < 1 || !args[0]->IsFunction()) {
338         return;
339     }
340     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
341     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
342     auto onLoad = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
343                       const std::string& xcomponentId) {
344         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
345         ACE_SCORING_EVENT("XComponent.onLoad");
346         PipelineContext::SetCallBackNode(node);
347         std::vector<std::string> keys = { "load", xcomponentId };
348         func->ExecuteNew(keys, "");
349         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] onLoad triggers", xcomponentId.c_str());
350     };
351     XComponentModel::GetInstance()->SetOnLoad(std::move(onLoad));
352 }
353 
RegisterOnCreate(const JsiExecutionContext & execCtx,const Local<JSValueRef> & func)354 void JSXComponent::RegisterOnCreate(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)
355 {
356     auto frameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
357     CHECK_NULL_VOID(frameNode);
358 
359     if (!func->IsFunction(execCtx.vm_)) {
360         return;
361     }
362 
363     auto jsFunc = panda::Global<panda::FunctionRef>(execCtx.vm_, Local<panda::FunctionRef>(func));
364     auto onLoad = [execCtx, funcRef = std::move(jsFunc), node = AceType::WeakClaim(AceType::RawPtr(frameNode))](
365                       const std::string& xcomponentId) {
366         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
367         ACE_SCORING_EVENT("XComponentNode.onCreate");
368         PipelineContext::SetCallBackNode(node);
369         std::vector<Local<JSValueRef>> argv;
370         JSRef<JSVal> jsVal;
371         if (XComponentClient::GetInstance().GetJSVal(xcomponentId, jsVal)) {
372             argv.emplace_back(jsVal->GetLocalHandle());
373         }
374         funcRef->Call(execCtx.vm_, JSNApi::GetGlobalObject(execCtx.vm_), argv.data(), argv.size());
375     };
376     XComponentModel::GetInstance()->RegisterOnCreate(frameNode, std::move(onLoad));
377 }
378 
RegisterOnDestroy(const JsiExecutionContext & execCtx,const Local<JSValueRef> & func)379 void JSXComponent::RegisterOnDestroy(const JsiExecutionContext& execCtx, const Local<JSValueRef>& func)
380 {
381     auto frameNode = AceType::DynamicCast<NG::FrameNode>(frameNode_);
382     CHECK_NULL_VOID(frameNode);
383 
384     if (!func->IsFunction(execCtx.vm_)) {
385         return;
386     }
387 
388     auto jsFunc = panda::Global<panda::FunctionRef>(execCtx.vm_, Local<panda::FunctionRef>(func));
389     auto onDestroy = [execCtx, funcRef = std::move(jsFunc), node = AceType::WeakClaim(AceType::RawPtr(frameNode))](
390                          const std::string& xcomponentId) {
391         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
392         ACE_SCORING_EVENT("XComponentNode.onDestroy");
393         PipelineContext::SetCallBackNode(node);
394         funcRef->Call(execCtx.vm_, JSNApi::GetGlobalObject(execCtx.vm_), nullptr, 0);
395     };
396     XComponentModel::GetInstance()->RegisterOnDestroy(frameNode, std::move(onDestroy));
397 }
398 
JsOnDestroy(const JSCallbackInfo & args)399 void JSXComponent::JsOnDestroy(const JSCallbackInfo& args)
400 {
401     if (args.Length() < 1 || !args[0]->IsFunction()) {
402         return;
403     }
404     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
405     WeakPtr<NG::FrameNode> targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
406     auto onDestroy = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
407                          const std::string& xcomponentId) {
408         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
409         ACE_SCORING_EVENT("XComponent.onDestroy");
410         PipelineContext::SetCallBackNode(node);
411         std::vector<std::string> keys = { "destroy" };
412         func->Execute(keys, "");
413         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponent[%{public}s] onDestroy", xcomponentId.c_str());
414     };
415     XComponentModel::GetInstance()->SetOnDestroy(std::move(onDestroy));
416 }
417 
JsOnAppear(const JSCallbackInfo & args)418 void JSXComponent::JsOnAppear(const JSCallbackInfo& args)
419 {
420     auto type = XComponentModel::GetInstance()->GetType();
421     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
422     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
423         return;
424     }
425     JSInteractableView::JsOnAppear(args);
426 }
427 
JsOnDisAppear(const JSCallbackInfo & args)428 void JSXComponent::JsOnDisAppear(const JSCallbackInfo& args)
429 {
430     auto type = XComponentModel::GetInstance()->GetType();
431     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
432     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
433         return;
434     }
435     JSInteractableView::JsOnDisAppear(args);
436 }
437 
JsOnAttach(const JSCallbackInfo & args)438 void JSXComponent::JsOnAttach(const JSCallbackInfo& args)
439 {
440     auto type = XComponentModel::GetInstance()->GetType();
441     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
442     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
443         return;
444     }
445     JSInteractableView::JsOnAttach(args);
446 }
447 
JsOnDetach(const JSCallbackInfo & args)448 void JSXComponent::JsOnDetach(const JSCallbackInfo& args)
449 {
450     auto type = XComponentModel::GetInstance()->GetType();
451     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
452     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
453         return;
454     }
455     JSInteractableView::JsOnDetach(args);
456 }
457 
JsOnTouch(const JSCallbackInfo & args)458 void JSXComponent::JsOnTouch(const JSCallbackInfo& args)
459 {
460     auto type = XComponentModel::GetInstance()->GetType();
461     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
462     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
463         return;
464     }
465     JSInteractableView::JsOnTouch(args);
466 }
467 
JsOnClick(const JSCallbackInfo & args)468 void JSXComponent::JsOnClick(const JSCallbackInfo& args)
469 {
470     auto type = XComponentModel::GetInstance()->GetType();
471     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
472     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
473         return;
474     }
475     JSViewAbstract::JsOnClick(args);
476 }
477 
JsOnKeyEvent(const JSCallbackInfo & args)478 void JSXComponent::JsOnKeyEvent(const JSCallbackInfo& args)
479 {
480     auto type = XComponentModel::GetInstance()->GetType();
481     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
482     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
483         return;
484     }
485     JSViewAbstract::JsOnKeyEvent(args);
486 }
487 
JsOnMouse(const JSCallbackInfo & args)488 void JSXComponent::JsOnMouse(const JSCallbackInfo& args)
489 {
490     auto type = XComponentModel::GetInstance()->GetType();
491     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
492     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
493         return;
494     }
495     JSViewAbstract::JsOnMouse(args);
496 }
497 
JsOnHover(const JSCallbackInfo & args)498 void JSXComponent::JsOnHover(const JSCallbackInfo& args)
499 {
500     auto type = XComponentModel::GetInstance()->GetType();
501     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
502     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
503         return;
504     }
505     JSViewAbstract::JsOnHover(args);
506 }
507 
508 
JsOnFocus(const JSCallbackInfo & args)509 void JSXComponent::JsOnFocus(const JSCallbackInfo& args)
510 {
511     auto type = XComponentModel::GetInstance()->GetType();
512     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
513     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
514         return;
515     }
516     JSViewAbstract::JsOnFocus(args);
517 }
518 
JsOnBlur(const JSCallbackInfo & args)519 void JSXComponent::JsOnBlur(const JSCallbackInfo& args)
520 {
521     auto type = XComponentModel::GetInstance()->GetType();
522     auto libraryName = XComponentModel::GetInstance()->GetLibraryName();
523     if (!XComponentModel::IsCommonEventAvailable(type, libraryName)) {
524         return;
525     }
526     JSViewAbstract::JsOnBlur(args);
527 }
528 
JsBackgroundColor(const JSCallbackInfo & args)529 void JSXComponent::JsBackgroundColor(const JSCallbackInfo& args)
530 {
531     auto type = XComponentModel::GetInstance()->GetType();
532     if (!XComponentModel::IsBackGroundColorAvailable(type)) {
533         return;
534     }
535     if (args.Length() < 1) {
536         return;
537     }
538     Color backgroundColor;
539     if (!ParseJsColor(args[0], backgroundColor)) {
540         backgroundColor = (type == XComponentType::SURFACE) ? Color::BLACK : Color::TRANSPARENT;
541     }
542     ViewAbstractModel::GetInstance()->SetBackgroundColor(backgroundColor);
543 }
544 
JsBackgroundImage(const JSCallbackInfo & args)545 void JSXComponent::JsBackgroundImage(const JSCallbackInfo& args)
546 {
547     auto type = XComponentModel::GetInstance()->GetType();
548     if (type != XComponentType::NODE) {
549         return;
550     }
551     JSViewAbstract::JsBackgroundImage(args);
552 }
553 
JsBackgroundImageSize(const JSCallbackInfo & args)554 void JSXComponent::JsBackgroundImageSize(const JSCallbackInfo& args)
555 {
556     auto type = XComponentModel::GetInstance()->GetType();
557     if (type != XComponentType::NODE) {
558         return;
559     }
560     JSViewAbstract::JsBackgroundImageSize(args);
561 }
562 
JsBackgroundImagePosition(const JSCallbackInfo & args)563 void JSXComponent::JsBackgroundImagePosition(const JSCallbackInfo& args)
564 {
565     auto type = XComponentModel::GetInstance()->GetType();
566     if (type != XComponentType::NODE) {
567         return;
568     }
569     JSViewAbstract::JsBackgroundImagePosition(args);
570 }
571 
572 
JsOpacity(const JSCallbackInfo & args)573 void JSXComponent::JsOpacity(const JSCallbackInfo& args)
574 {
575     auto type = XComponentModel::GetInstance()->GetType();
576     if (type == XComponentType::SURFACE || type == XComponentType::COMPONENT) {
577         return;
578     }
579     JSViewAbstract::JsOpacity(args);
580 }
581 
JsBlur(const JSCallbackInfo & args)582 void JSXComponent::JsBlur(const JSCallbackInfo& args)
583 {
584     auto type = XComponentModel::GetInstance()->GetType();
585     if (type != XComponentType::NODE) {
586         return;
587     }
588     JSViewAbstract::JsBlur(args);
589 }
590 
JsBackdropBlur(const JSCallbackInfo & args)591 void JSXComponent::JsBackdropBlur(const JSCallbackInfo& args)
592 {
593     auto type = XComponentModel::GetInstance()->GetType();
594     if (type != XComponentType::NODE) {
595         return;
596     }
597     JSViewAbstract::JsBackdropBlur(args);
598 }
599 
JsGrayscale(const JSCallbackInfo & args)600 void JSXComponent::JsGrayscale(const JSCallbackInfo& args)
601 {
602     auto type = XComponentModel::GetInstance()->GetType();
603     if (type != XComponentType::NODE) {
604         return;
605     }
606    // JSViewAbstract::JsGrayscale(args);
607 }
608 
JsBrightness(const JSCallbackInfo & args)609 void JSXComponent::JsBrightness(const JSCallbackInfo& args)
610 {
611     auto type = XComponentModel::GetInstance()->GetType();
612     if (type != XComponentType::NODE) {
613         return;
614     }
615     JSViewAbstract::JsBrightness(args);
616 }
617 
JsSaturate(const JSCallbackInfo & args)618 void JSXComponent::JsSaturate(const JSCallbackInfo& args)
619 {
620     auto type = XComponentModel::GetInstance()->GetType();
621     if (type != XComponentType::NODE) {
622         return;
623     }
624     JSViewAbstract::JsSaturate(args);
625 }
626 
JsContrast(const JSCallbackInfo & args)627 void JSXComponent::JsContrast(const JSCallbackInfo& args)
628 {
629     auto type = XComponentModel::GetInstance()->GetType();
630     if (type != XComponentType::NODE) {
631         return;
632     }
633     JSViewAbstract::JsContrast(args);
634 }
635 
JsInvert(const JSCallbackInfo & args)636 void JSXComponent::JsInvert(const JSCallbackInfo& args)
637 {
638     auto type = XComponentModel::GetInstance()->GetType();
639     if (type != XComponentType::NODE) {
640         return;
641     }
642     JSViewAbstract::JsInvert(args);
643 }
644 
JsSepia(const JSCallbackInfo & args)645 void JSXComponent::JsSepia(const JSCallbackInfo& args)
646 {
647     auto type = XComponentModel::GetInstance()->GetType();
648     if (type != XComponentType::NODE) {
649         return;
650     }
651     JSViewAbstract::JsSepia(args);
652 }
653 
JsHueRotate(const JSCallbackInfo & args)654 void JSXComponent::JsHueRotate(const JSCallbackInfo& args)
655 {
656     auto type = XComponentModel::GetInstance()->GetType();
657     if (type != XComponentType::NODE) {
658         return;
659     }
660     JSViewAbstract::JsHueRotate(args);
661 }
662 
JsColorBlend(const JSCallbackInfo & args)663 void JSXComponent::JsColorBlend(const JSCallbackInfo& args)
664 {
665     auto type = XComponentModel::GetInstance()->GetType();
666     if (type != XComponentType::NODE) {
667         return;
668     }
669     JSViewAbstract::JsColorBlend(args);
670 }
671 
JsSphericalEffect(const JSCallbackInfo & args)672 void JSXComponent::JsSphericalEffect(const JSCallbackInfo& args)
673 {
674     auto type = XComponentModel::GetInstance()->GetType();
675     if (type != XComponentType::NODE) {
676         return;
677     }
678     JSViewAbstract::JsSphericalEffect(args);
679 }
680 
JsLightUpEffect(const JSCallbackInfo & args)681 void JSXComponent::JsLightUpEffect(const JSCallbackInfo& args)
682 {
683     auto type = XComponentModel::GetInstance()->GetType();
684     if (type != XComponentType::NODE) {
685         return;
686     }
687     JSViewAbstract::JsLightUpEffect(args);
688 }
689 
JsPixelStretchEffect(const JSCallbackInfo & args)690 void JSXComponent::JsPixelStretchEffect(const JSCallbackInfo& args)
691 {
692     auto type = XComponentModel::GetInstance()->GetType();
693     if (type != XComponentType::NODE) {
694         return;
695     }
696     JSViewAbstract::JsPixelStretchEffect(args);
697 }
698 
JsLinearGradientBlur(const JSCallbackInfo & args)699 void JSXComponent::JsLinearGradientBlur(const JSCallbackInfo& args)
700 {
701     auto type = XComponentModel::GetInstance()->GetType();
702     if (type != XComponentType::NODE) {
703         return;
704     }
705     JSViewAbstract::JsLinearGradientBlur(args);
706 }
707 
JsEnableAnalyzer(bool enable)708 void JSXComponent::JsEnableAnalyzer(bool enable)
709 {
710     auto type = XComponentModel::GetInstance()->GetType();
711     if (type == XComponentType::COMPONENT || type == XComponentType::NODE) {
712         return;
713     }
714     XComponentModel::GetInstance()->EnableAnalyzer(enable);
715 }
716 
JsRenderFit(const JSCallbackInfo & args)717 void JSXComponent::JsRenderFit(const JSCallbackInfo& args)
718 {
719     auto type = XComponentModel::GetInstance()->GetType();
720     if (type == XComponentType::COMPONENT || type == XComponentType::NODE || args.Length() != 1) {
721         return;
722     }
723     if (type == XComponentType::TEXTURE) {
724         JSViewAbstract::JSRenderFit(args);
725         return;
726     }
727 
728     // set RenderFit on SurfaceNode when type is SURFACE
729     RenderFit renderFit = RenderFit::RESIZE_FILL;
730     if (args[0]->IsNumber()) {
731         int32_t fitNumber = args[0]->ToNumber<int32_t>();
732         if (fitNumber >= static_cast<int32_t>(RenderFit::CENTER) &&
733             fitNumber <= static_cast<int32_t>(RenderFit::RESIZE_COVER_BOTTOM_RIGHT)) {
734             renderFit = static_cast<RenderFit>(fitNumber);
735         }
736     }
737     XComponentModel::GetInstance()->SetRenderFit(renderFit);
738 }
739 
JsEnableSecure(const JSCallbackInfo & args)740 void JSXComponent::JsEnableSecure(const JSCallbackInfo& args)
741 {
742     auto type = XComponentModel::GetInstance()->GetType();
743     if (type != XComponentType::SURFACE || args.Length() != 1) {
744         return;
745     }
746     // set isSecure on SurfaceNode when type is SURFACE
747     if (args[0]->IsBoolean()) {
748         bool isSecure = args[0]->ToBoolean();
749         XComponentModel::GetInstance()->EnableSecure(isSecure);
750     }
751 }
752 
JsHdrBrightness(const JSCallbackInfo & args)753 void JSXComponent::JsHdrBrightness(const JSCallbackInfo& args)
754 {
755     auto type = XComponentModel::GetInstance()->GetType();
756     if (type != XComponentType::SURFACE || args.Length() != 1) {
757         return;
758     }
759     // set hdrBrightness on SurfaceNode when type is SURFACE
760     if (args[0]->IsNumber()) {
761         float hdrBrightness = args[0]->ToNumber<float>();
762         XComponentModel::GetInstance()->HdrBrightness(std::clamp(hdrBrightness, 0.0f, 1.0f));
763     }
764 }
765 
JsBlendMode(const JSCallbackInfo & args)766 void JSXComponent::JsBlendMode(const JSCallbackInfo& args)
767 {
768     auto type = XComponentModel::GetInstance()->GetType();
769     if (type == XComponentType::TEXTURE && Container::LessThanAPITargetVersion(PlatformVersion::VERSION_EIGHTEEN)) {
770         return;
771     }
772 
773     JSViewAbstract::JsBlendMode(args);
774 }
775 
JsEnableTransparentLayer(const JSCallbackInfo & args)776 void JSXComponent::JsEnableTransparentLayer(const JSCallbackInfo& args)
777 {
778     auto type = XComponentModel::GetInstance()->GetType();
779     if (type != XComponentType::SURFACE || args.Length() != 1) {
780         return;
781     }
782     // set isTransparentLayer on SurfaceNode when type is SURFACE
783     if (args[0]->IsBoolean()) {
784         bool isTransparentLayer = args[0]->ToBoolean();
785         XComponentModel::GetInstance()->EnableTransparentLayer(isTransparentLayer);
786     }
787 }
788 } // namespace OHOS::Ace::Framework
789