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