• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_xcomponent_bridge.h"
16 
17 #include "base/log/ace_scoring_log.h"
18 #include "base/utils/utils.h"
19 #include "bridge/common/utils/engine_helper.h"
20 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
21 #include "bridge/declarative_frontend/jsview/models/indexer_model_impl.h"
22 #include "bridge/declarative_frontend/jsview/js_xcomponent.h"
23 #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h"
24 
25 namespace OHOS::Ace::NG {
26 namespace {
SetControllerOnCreatedCallback(EcmaVM * vm,FrameNode * frameNode,const Local<JSValueRef> & createdFunc)27 void SetControllerOnCreatedCallback(EcmaVM* vm, FrameNode* frameNode, const Local<JSValueRef>& createdFunc)
28 {
29     if (createdFunc->IsFunction(vm)) {
30         panda::Local<panda::FunctionRef> func = createdFunc;
31         auto onSurfaceCreated = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
32                                     const std::string& surfaceId, const std::string& /* xcomponentId */) {
33             panda::LocalScope pandaScope(vm);
34             panda::TryCatch trycatch(vm);
35             PipelineContext::SetCallBackNode(node);
36             panda::Local<panda::JSValueRef> para[1] = { panda::StringRef::NewFromUtf8(vm, surfaceId.c_str()) };
37             func->Call(vm, func.ToLocal(), para, 1);
38         };
39         XComponentModelNG::SetControllerOnCreated(frameNode, std::move(onSurfaceCreated));
40     }
41 }
42 
SetControllerOnChangedCallback(EcmaVM * vm,FrameNode * frameNode,const Local<JSValueRef> & changedFunc)43 void SetControllerOnChangedCallback(EcmaVM* vm, FrameNode* frameNode, const Local<JSValueRef>& changedFunc)
44 {
45     if (changedFunc->IsFunction(vm)) {
46         panda::Local<panda::FunctionRef> func = changedFunc;
47         auto onSurfaceChanged = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
48                                     const std::string& surfaceId, const NG::RectF& rect) {
49             panda::LocalScope pandaScope(vm);
50             panda::TryCatch trycatch(vm);
51             PipelineContext::SetCallBackNode(node);
52             const char* keys[] = { "offsetX", "offsetY", "surfaceWidth", "surfaceHeight" };
53             Local<JSValueRef> rectValues[] = { panda::NumberRef::New(vm, rect.Left()),
54                 panda::NumberRef::New(vm, rect.Top()), panda::NumberRef::New(vm, rect.Width()),
55                 panda::NumberRef::New(vm, rect.Height()) };
56             auto rectObj = panda::ObjectRef::NewWithNamedProperties(vm, ArraySize(keys), keys, rectValues);
57             panda::Local<panda::JSValueRef> para[2] = { panda::StringRef::NewFromUtf8(vm, surfaceId.c_str()), rectObj };
58             func->Call(vm, func.ToLocal(), para, 2);
59         };
60         XComponentModelNG::SetControllerOnChanged(frameNode, std::move(onSurfaceChanged));
61     }
62 }
63 
SetControllerOnDestroyedCallback(EcmaVM * vm,FrameNode * frameNode,const Local<JSValueRef> & destroyedFunc)64 void SetControllerOnDestroyedCallback(EcmaVM* vm, FrameNode* frameNode, const Local<JSValueRef>& destroyedFunc)
65 {
66     if (destroyedFunc->IsFunction(vm)) {
67         panda::Local<panda::FunctionRef> func = destroyedFunc;
68         auto onSurfaceDestroyed = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
69                                       const std::string& surfaceId, const std::string& /* xcomponentId */) {
70             panda::LocalScope pandaScope(vm);
71             panda::TryCatch trycatch(vm);
72             PipelineContext::SetCallBackNode(node);
73             panda::Local<panda::JSValueRef> para[1] = { panda::StringRef::NewFromUtf8(vm, surfaceId.c_str()) };
74             func->Call(vm, func.ToLocal(), para, 1);
75         };
76         XComponentModelNG::SetControllerOnDestroyed(frameNode, std::move(onSurfaceDestroyed));
77     }
78 }
79 } // namespace
80 
81 enum ArgIndices {
82     ARG_FIRST = 0,
83     ARG_ID = 1,
84     ARG_TYPE = 2,
85     ARG_IMAGE_AI_OPTIONS = 3,
86     ARG_LIBRARY_NAME = 4,
87     ARG_CONTROLLER = 5
88 };
89 
ConvertToXComponentType(const std::string & type)90 XComponentType XComponentBridge::ConvertToXComponentType(const std::string& type)
91 {
92     if (type == "surface") {
93         return XComponentType::SURFACE;
94     }
95     if (type == "component") {
96         return XComponentType::COMPONENT;
97     }
98     if (type == "node") {
99         return XComponentType::NODE;
100     }
101     return XComponentType::SURFACE;
102 }
103 
ParseParams(ArkUIRuntimeCallInfo * runtimeCallInfo,ArkUI_Params & params)104 void XComponentBridge::ParseParams(ArkUIRuntimeCallInfo* runtimeCallInfo, ArkUI_Params& params)
105 {
106     ArkUI_XComponent_Params* xcParams = (ArkUI_XComponent_Params*)(&params);
107     EcmaVM* vm = runtimeCallInfo->GetVM();
108     CHECK_NULL_VOID(vm);
109     Local<JSValueRef> paramsArg = runtimeCallInfo->GetCallArgRef(ARG_TYPE);
110     CHECK_NULL_VOID(paramsArg->IsObject(vm));
111     auto obj = Local<panda::ObjectRef>(paramsArg);
112 
113     auto idArg = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "id"));
114     auto typeArg = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "type"));
115     auto libraryNameArg = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "libraryname"));
116     auto controllerArg = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "controller"));
117     auto imageAIOptionsArg = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "imageAIOptions"));
118     auto screenIdArg = obj->Get(vm, panda::StringRef::NewFromUtf8(vm, "screenId"));
119 
120     xcParams->id = idArg->IsString(vm) ? idArg->ToString(vm)->ToString(vm) : "";
121     if (libraryNameArg->IsString(vm)) {
122         xcParams->libraryName = libraryNameArg->ToString(vm)->ToString(vm);
123     }
124     if (typeArg->IsString(vm)) {
125         xcParams->type = ConvertToXComponentType(typeArg->ToString(vm)->ToString(vm));
126     } else if (typeArg->IsNumber()) {
127         xcParams->type = static_cast<XComponentType>(typeArg->Int32Value(vm));
128     }
129     xcParams->controller = nullptr;
130     if (controllerArg->IsObject(vm)) {
131         Framework::JSXComponentController* jsXComponentController = static_cast<Framework::JSXComponentController*>(
132             Local<panda::ObjectRef>(controllerArg)->GetNativePointerField(vm, 0));
133         if (jsXComponentController) {
134             jsXComponentController->SetInstanceId(Container::CurrentId());
135             Framework::XComponentClient::GetInstance().AddControllerToJSXComponentControllersMap(
136                 xcParams->id, jsXComponentController);
137             xcParams->controller = jsXComponentController->GetController();
138         }
139     }
140     xcParams->aiOptions = nullptr;
141     if (imageAIOptionsArg->IsObject(vm)) {
142         auto engine = EngineHelper::GetCurrentEngine();
143         CHECK_NULL_VOID(engine);
144         NativeEngine* nativeEngine = engine->GetNativeEngine();
145         CHECK_NULL_VOID(nativeEngine);
146         Local<JSValueRef> value = imageAIOptionsArg;
147         JSValueWrapper valueWrapper = value;
148         Framework::ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
149         napi_value optionsValue = nativeEngine->ValueToNapiValue(valueWrapper);
150         xcParams->aiOptions = optionsValue;
151     }
152     if (screenIdArg->IsNumber()) {
153         xcParams->screenId = screenIdArg->ToNumber(vm)->Value();
154     }
155 }
156 
SetControllerOnCreated(ArkUIRuntimeCallInfo * runtimeCallInfo,FrameNode * frameNode)157 void XComponentBridge::SetControllerOnCreated(ArkUIRuntimeCallInfo* runtimeCallInfo, FrameNode* frameNode)
158 {
159     CHECK_NULL_VOID(frameNode);
160     EcmaVM* vm = runtimeCallInfo->GetVM();
161     Local<JSValueRef> paramsArg = runtimeCallInfo->GetCallArgRef(ARG_TYPE);
162     CHECK_NULL_VOID(paramsArg->IsObject(vm));
163     auto obj = Local<panda::ObjectRef>(paramsArg);
164     auto controllerStr = panda::StringRef::NewFromUtf8(vm, "controller");
165     auto controllerArg = obj->Get(vm, controllerStr);
166     CHECK_NULL_VOID(controllerArg->IsObject(vm));
167     auto object = controllerArg->ToObject(vm);
168     auto createdFunc = object->Get(vm, panda::StringRef::NewFromUtf8(vm, "onSurfaceCreated"));
169     if (createdFunc->IsFunction(vm)) {
170         panda::Local<panda::FunctionRef> func = createdFunc;
171         auto onSurfaceCreated = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
172                                     const std::string& surfaceId, const std::string& xcomponentId) {
173             panda::LocalScope pandaScope(vm);
174             panda::TryCatch trycatch(vm);
175             PipelineContext::SetCallBackNode(node);
176             panda::Local<panda::JSValueRef> para[1] = { panda::StringRef::NewFromUtf8(vm, surfaceId.c_str()) };
177             func->Call(vm, func.ToLocal(), para, 1);
178             TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponentNode[%{public}s] ControllerOnCreated surfaceId:%{public}s",
179                 xcomponentId.c_str(), surfaceId.c_str());
180         };
181         XComponentModelNG::SetControllerOnCreated(frameNode, std::move(onSurfaceCreated));
182     }
183 }
184 
SetControllerOnChanged(ArkUIRuntimeCallInfo * runtimeCallInfo,FrameNode * frameNode)185 void XComponentBridge::SetControllerOnChanged(ArkUIRuntimeCallInfo* runtimeCallInfo, FrameNode* frameNode)
186 {
187     CHECK_NULL_VOID(frameNode);
188     EcmaVM* vm = runtimeCallInfo->GetVM();
189     Local<JSValueRef> paramsArg = runtimeCallInfo->GetCallArgRef(ARG_TYPE);
190     CHECK_NULL_VOID(paramsArg->IsObject(vm));
191     auto obj = Local<panda::ObjectRef>(paramsArg);
192     auto controllerStr = panda::StringRef::NewFromUtf8(vm, "controller");
193     auto controllerArg = obj->Get(vm, controllerStr);
194     CHECK_NULL_VOID(controllerArg->IsObject(vm));
195     auto object = controllerArg->ToObject(vm);
196     auto changedFunc = object->Get(vm, panda::StringRef::NewFromUtf8(vm, "onSurfaceChanged"));
197     if (changedFunc->IsFunction(vm)) {
198         panda::Local<panda::FunctionRef> func = changedFunc;
199         auto onSurfaceChanged = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
200                                     const std::string& surfaceId, const NG::RectF& rect) {
201             panda::LocalScope pandaScope(vm);
202             panda::TryCatch trycatch(vm);
203             PipelineContext::SetCallBackNode(node);
204             const char* keys[] = { "offsetX", "offsetY", "surfaceWidth", "surfaceHeight" };
205             Local<JSValueRef> rectValues[] = { panda::NumberRef::New(vm, rect.Left()),
206                 panda::NumberRef::New(vm, rect.Top()), panda::NumberRef::New(vm, rect.Width()),
207                 panda::NumberRef::New(vm, rect.Height()) };
208             auto rectObj = panda::ObjectRef::NewWithNamedProperties(vm, ArraySize(keys), keys, rectValues);
209             panda::Local<panda::JSValueRef> para[2] = { panda::StringRef::NewFromUtf8(vm, surfaceId.c_str()), rectObj };
210             func->Call(vm, func.ToLocal(), para, 2);
211         };
212         XComponentModelNG::SetControllerOnChanged(frameNode, std::move(onSurfaceChanged));
213     }
214 }
215 
SetControllerOnDestroyed(ArkUIRuntimeCallInfo * runtimeCallInfo,FrameNode * frameNode)216 void XComponentBridge::SetControllerOnDestroyed(ArkUIRuntimeCallInfo* runtimeCallInfo, FrameNode* frameNode)
217 {
218     CHECK_NULL_VOID(frameNode);
219     EcmaVM* vm = runtimeCallInfo->GetVM();
220     Local<JSValueRef> paramsArg = runtimeCallInfo->GetCallArgRef(ARG_TYPE);
221     CHECK_NULL_VOID(paramsArg->IsObject(vm));
222     auto obj = Local<panda::ObjectRef>(paramsArg);
223     auto controllerStr = panda::StringRef::NewFromUtf8(vm, "controller");
224     auto controllerArg = obj->Get(vm, controllerStr);
225     CHECK_NULL_VOID(controllerArg->IsObject(vm));
226     auto object = controllerArg->ToObject(vm);
227     auto destroyedFunc = object->Get(vm, panda::StringRef::NewFromUtf8(vm, "onSurfaceDestroyed"));
228     if (destroyedFunc->IsFunction(vm)) {
229         panda::Local<panda::FunctionRef> func = destroyedFunc;
230         auto onDestroyed = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
231                                const std::string& surfaceId, const std::string& xcomponentId) {
232             panda::LocalScope pandaScope(vm);
233             panda::TryCatch trycatch(vm);
234             PipelineContext::SetCallBackNode(node);
235             panda::Local<panda::JSValueRef> para[1] = { panda::StringRef::NewFromUtf8(vm, surfaceId.c_str()) };
236             func->Call(vm, func.ToLocal(), para, 1);
237             TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponentNode[%{public}s] ControllerOnDestroyed surfaceId:%{public}s",
238                 xcomponentId.c_str(), surfaceId.c_str());
239         };
240         XComponentModelNG::SetControllerOnDestroyed(frameNode, std::move(onDestroyed));
241     }
242 }
243 
SetControllerCallback(ArkUIRuntimeCallInfo * runtimeCallInfo,FrameNode * frameNode)244 void XComponentBridge::SetControllerCallback(ArkUIRuntimeCallInfo* runtimeCallInfo, FrameNode* frameNode)
245 {
246     SetControllerOnCreated(runtimeCallInfo, frameNode);
247     SetControllerOnChanged(runtimeCallInfo, frameNode);
248     SetControllerOnDestroyed(runtimeCallInfo, frameNode);
249 }
250 
SetControllerCallback(ArkUIRuntimeCallInfo * runtimeCallInfo)251 void XComponentBridge::SetControllerCallback(ArkUIRuntimeCallInfo* runtimeCallInfo)
252 {
253     EcmaVM* vm = runtimeCallInfo->GetVM();
254     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
255     Local<JSValueRef> controllerArg = runtimeCallInfo->GetCallArgRef(ARG_CONTROLLER);
256     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
257     auto object = controllerArg->ToObject(vm);
258     auto createdFunc = object->Get(vm, panda::StringRef::NewFromUtf8(vm, "onSurfaceCreated"));
259     SetControllerOnCreatedCallback(vm, frameNode, createdFunc);
260     auto changedFunc = object->Get(vm, panda::StringRef::NewFromUtf8(vm, "onSurfaceChanged"));
261     SetControllerOnChangedCallback(vm, frameNode, changedFunc);
262     auto destroyedFunc = object->Get(vm, panda::StringRef::NewFromUtf8(vm, "onSurfaceDestroyed"));
263     SetControllerOnDestroyedCallback(vm, frameNode, destroyedFunc);
264 }
265 
SetXComponentInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)266 ArkUINativeModuleValue XComponentBridge::SetXComponentInitialize(ArkUIRuntimeCallInfo* runtimeCallInfo)
267 {
268     EcmaVM* vm = runtimeCallInfo->GetVM();
269     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
270     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
271     Local<JSValueRef> idArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
272     Local<JSValueRef> typeArg = runtimeCallInfo->GetCallArgRef(ARG_TYPE);
273     Local<JSValueRef> librarynameArg = runtimeCallInfo->GetCallArgRef(ARG_LIBRARY_NAME);
274     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
275     if (!idArg->IsString(vm)) {
276         return panda::JSValueRef::Undefined(vm);
277     }
278     Framework::JsiCallbackInfo info = Framework::JsiCallbackInfo(runtimeCallInfo);
279     Framework::JSRef<Framework::JSVal> args = info[ARG_CONTROLLER];
280     Framework::JSRef<Framework::JSObject> controllerObj;
281     std::shared_ptr<InnerXComponentController> xcomponentController = nullptr;
282     if (args->IsObject()) {
283         controllerObj = Framework::JSRef<Framework::JSObject>::Cast(args);
284         Framework::JSXComponentController* jsXComponentController =
285             controllerObj->Unwrap<Framework::JSXComponentController>();
286         if (jsXComponentController) {
287             jsXComponentController->SetInstanceId(Container::CurrentId());
288             Framework::XComponentClient::GetInstance().AddControllerToJSXComponentControllersMap(
289                 idArg->ToString(vm)->ToString(vm), jsXComponentController);
290             xcomponentController = jsXComponentController->GetController();
291         }
292     }
293     XComponentType xcomponentType = XComponentType::SURFACE;
294     if (typeArg->IsString(vm)) {
295         xcomponentType = ConvertToXComponentType(typeArg->ToString(vm)->ToString(vm));
296     } else if (typeArg->IsNumber()) {
297         xcomponentType = static_cast<XComponentType>(typeArg->Int32Value(vm));
298     }
299     GetArkUINodeModifiers()->getXComponentModifier()->setXComponentId(
300         nativeNode, idArg->ToString(vm)->ToString(vm).c_str());
301     GetArkUINodeModifiers()->getXComponentModifier()->setXComponentType(
302         nativeNode, static_cast<int32_t>(xcomponentType));
303     if (librarynameArg->IsString(vm)) {
304         auto libraryName = librarynameArg->ToString(vm)->ToString(vm);
305         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentLibraryname(nativeNode, libraryName.c_str());
306     }
307     if ((librarynameArg->IsNull() || librarynameArg->IsUndefined()) && xcomponentController &&
308         !controllerObj->IsUndefined()) {
309         SetControllerCallback(runtimeCallInfo);
310     }
311     HandleDetachCallback(runtimeCallInfo);
312     HandleImageAIOptions(runtimeCallInfo);
313     GetArkUINodeModifiers()->getXComponentModifier()->initXComponent(nativeNode);
314     return panda::JSValueRef::Undefined(vm);
315 }
316 
HandleDetachCallback(ArkUIRuntimeCallInfo * runtimeCallInfo)317 void XComponentBridge::HandleDetachCallback(ArkUIRuntimeCallInfo *runtimeCallInfo)
318 {
319     EcmaVM* vm = runtimeCallInfo->GetVM();
320     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
321     auto detachCallback = [](const std::string& xcomponentId) {
322         Framework::XComponentClient::GetInstance().DeleteControllerFromJSXComponentControllersMap(xcomponentId);
323         Framework::XComponentClient::GetInstance().DeleteFromJsValMapById(xcomponentId);
324     };
325     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
326     XComponentModelNG::SetDetachCallback(frameNode, std::move(detachCallback));
327 }
328 
HandleImageAIOptions(ArkUIRuntimeCallInfo * runtimeCallInfo)329 void XComponentBridge::HandleImageAIOptions(ArkUIRuntimeCallInfo *runtimeCallInfo)
330 {
331     EcmaVM* vm = runtimeCallInfo->GetVM();
332     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
333     Local<JSValueRef> imageAIOptionsArg = runtimeCallInfo->GetCallArgRef(ARG_IMAGE_AI_OPTIONS);
334     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
335     if (imageAIOptionsArg->IsObject(vm)) {
336         auto engine = EngineHelper::GetCurrentEngine();
337         CHECK_NULL_VOID(engine);
338         NativeEngine* nativeEngine = engine->GetNativeEngine();
339         CHECK_NULL_VOID(nativeEngine);
340         Local<JSValueRef> value = imageAIOptionsArg;
341         JSValueWrapper valueWrapper = value;
342         Framework::ScopeRAII scope(reinterpret_cast<napi_env>(nativeEngine));
343         napi_value optionsValue = nativeEngine->ValueToNapiValue(valueWrapper);
344         GetArkUINodeModifiers()->getXComponentModifier()->setImageAIOptions(nativeNode, optionsValue);
345     }
346 }
347 
ResetXComponentInitialize(ArkUIRuntimeCallInfo * runtimeCallInfo)348 ArkUINativeModuleValue XComponentBridge::ResetXComponentInitialize(ArkUIRuntimeCallInfo *runtimeCallInfo)
349 {
350     EcmaVM *vm = runtimeCallInfo->GetVM();
351     return panda::JSValueRef::Undefined(vm);
352 }
353 
SetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)354 ArkUINativeModuleValue XComponentBridge::SetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
355 {
356     EcmaVM *vm = runtimeCallInfo->GetVM();
357     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
358     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
359     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
360     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
361     Color color;
362     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
363         GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentBackgroundColor(nativeNode);
364     } else {
365         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentBackgroundColor(nativeNode, color.GetValue());
366     }
367     return panda::JSValueRef::Undefined(vm);
368 }
369 
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)370 ArkUINativeModuleValue XComponentBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
371 {
372     EcmaVM *vm = runtimeCallInfo->GetVM();
373     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
374     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
375     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
376     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentBackgroundColor(nativeNode);
377     return panda::JSValueRef::Undefined(vm);
378 }
379 
SetOpacity(ArkUIRuntimeCallInfo * runtimeCallInfo)380 ArkUINativeModuleValue XComponentBridge::SetOpacity(ArkUIRuntimeCallInfo *runtimeCallInfo)
381 {
382     EcmaVM *vm = runtimeCallInfo->GetVM();
383     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
384     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
385     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
386     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
387     double opacity;
388     if (!ArkTSUtils::ParseJsDouble(vm, secondArg, opacity)) {
389         GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentOpacity(nativeNode);
390     } else {
391         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentOpacity(nativeNode, opacity);
392     }
393     return panda::JSValueRef::Undefined(vm);
394 }
395 
ResetOpacity(ArkUIRuntimeCallInfo * runtimeCallInfo)396 ArkUINativeModuleValue XComponentBridge::ResetOpacity(ArkUIRuntimeCallInfo *runtimeCallInfo)
397 {
398     EcmaVM *vm = runtimeCallInfo->GetVM();
399     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
400     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
401     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
402     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentOpacity(nativeNode);
403     return panda::JSValueRef::Undefined(vm);
404 }
405 
SetGrayscale(ArkUIRuntimeCallInfo * runtimeCallInfo)406 ArkUINativeModuleValue XComponentBridge::SetGrayscale(ArkUIRuntimeCallInfo *runtimeCallInfo)
407 {
408     EcmaVM* vm = runtimeCallInfo->GetVM();
409     return panda::JSValueRef::Undefined(vm);
410 }
411 
ResetGrayscale(ArkUIRuntimeCallInfo * runtimeCallInfo)412 ArkUINativeModuleValue XComponentBridge::ResetGrayscale(ArkUIRuntimeCallInfo *runtimeCallInfo)
413 {
414     EcmaVM* vm = runtimeCallInfo->GetVM();
415     return panda::JSValueRef::Undefined(vm);
416 }
417 
SetOnLoad(ArkUIRuntimeCallInfo * runtimeCallInfo)418 ArkUINativeModuleValue XComponentBridge::SetOnLoad(ArkUIRuntimeCallInfo *runtimeCallInfo)
419 {
420     EcmaVM* vm = runtimeCallInfo->GetVM();
421     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
422     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
423     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
424     CHECK_NULL_RETURN(secondArg->IsFunction(vm), panda::JSValueRef::Undefined(vm));
425     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
426     CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
427     auto obj = secondArg->ToObject(vm);
428     panda::Local<panda::FunctionRef> func = obj;
429     auto onLoad = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
430                       const std::string& xcomponentId) {
431         panda::LocalScope pandaScope(vm);
432         panda::TryCatch trycatch(vm);
433         PipelineContext::SetCallBackNode(node);
434         std::vector<Local<JSValueRef>> argv;
435         Framework::JSRef<Framework::JSVal> jsVal;
436         if (Framework::XComponentClient::GetInstance().GetJSVal(xcomponentId, jsVal)) {
437             argv.emplace_back(jsVal->GetLocalHandle());
438         }
439         func->Call(vm, func.ToLocal(), argv.data(), argv.size());
440         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponentNode[%{public}s] onLoad", xcomponentId.c_str());
441     };
442     XComponentModelNG::SetOnLoad(frameNode, std::move(onLoad));
443     return panda::JSValueRef::Undefined(vm);
444 }
445 
ResetOnLoad(ArkUIRuntimeCallInfo * runtimeCallInfo)446 ArkUINativeModuleValue XComponentBridge::ResetOnLoad(ArkUIRuntimeCallInfo *runtimeCallInfo)
447 {
448     EcmaVM* vm = runtimeCallInfo->GetVM();
449     return panda::JSValueRef::Undefined(vm);
450 }
451 
SetOnDestroy(ArkUIRuntimeCallInfo * runtimeCallInfo)452 ArkUINativeModuleValue XComponentBridge::SetOnDestroy(ArkUIRuntimeCallInfo *runtimeCallInfo)
453 {
454     EcmaVM* vm = runtimeCallInfo->GetVM();
455     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
456     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
457     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
458     CHECK_NULL_RETURN(secondArg->IsFunction(vm), panda::JSValueRef::Undefined(vm));
459     auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
460     CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
461     auto obj = secondArg->ToObject(vm);
462     panda::Local<panda::FunctionRef> func = obj;
463     auto onDestroy = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
464                          const std::string& xcomponentId) {
465         panda::LocalScope pandaScope(vm);
466         panda::TryCatch trycatch(vm);
467         PipelineContext::SetCallBackNode(node);
468         func->Call(vm, func.ToLocal(), nullptr, 0);
469         TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponentNode[%{public}s] onDestroy", xcomponentId.c_str());
470     };
471     XComponentModelNG::SetOnDestroy(frameNode, std::move(onDestroy));
472     return panda::JSValueRef::Undefined(vm);
473 }
474 
ResetOnDestroy(ArkUIRuntimeCallInfo * runtimeCallInfo)475 ArkUINativeModuleValue XComponentBridge::ResetOnDestroy(ArkUIRuntimeCallInfo *runtimeCallInfo)
476 {
477     EcmaVM* vm = runtimeCallInfo->GetVM();
478     return panda::JSValueRef::Undefined(vm);
479 }
480 
SetEnableAnalyzer(ArkUIRuntimeCallInfo * runtimeCallInfo)481 ArkUINativeModuleValue XComponentBridge::SetEnableAnalyzer(ArkUIRuntimeCallInfo *runtimeCallInfo)
482 {
483     EcmaVM *vm = runtimeCallInfo->GetVM();
484     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
485     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
486     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
487     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
488     if (secondArg->IsBoolean()) {
489         bool boolValue = secondArg->ToBoolean(vm)->Value();
490         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentEnableAnalyzer(nativeNode, boolValue);
491     } else {
492         GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableAnalyzer(nativeNode);
493     }
494     return panda::JSValueRef::Undefined(vm);
495 }
496 
ResetEnableAnalyzer(ArkUIRuntimeCallInfo * runtimeCallInfo)497 ArkUINativeModuleValue XComponentBridge::ResetEnableAnalyzer(ArkUIRuntimeCallInfo *runtimeCallInfo)
498 {
499     EcmaVM *vm = runtimeCallInfo->GetVM();
500     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
501     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
502     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
503     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableAnalyzer(nativeNode);
504     return panda::JSValueRef::Undefined(vm);
505 }
506 
SetEnableSecure(ArkUIRuntimeCallInfo * runtimeCallInfo)507 ArkUINativeModuleValue XComponentBridge::SetEnableSecure(ArkUIRuntimeCallInfo *runtimeCallInfo)
508 {
509     EcmaVM *vm = runtimeCallInfo->GetVM();
510     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
511     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
512     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
513     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
514     if (secondArg->IsBoolean()) {
515         bool boolValue = secondArg->ToBoolean(vm)->Value();
516         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentEnableSecure(nativeNode, boolValue);
517     } else {
518         GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableSecure(nativeNode);
519     }
520     return panda::JSValueRef::Undefined(vm);
521 }
522 
ResetEnableSecure(ArkUIRuntimeCallInfo * runtimeCallInfo)523 ArkUINativeModuleValue XComponentBridge::ResetEnableSecure(ArkUIRuntimeCallInfo *runtimeCallInfo)
524 {
525     EcmaVM *vm = runtimeCallInfo->GetVM();
526     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
527     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
528     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
529     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableSecure(nativeNode);
530     return panda::JSValueRef::Undefined(vm);
531 }
532 
SetHdrBrightness(ArkUIRuntimeCallInfo * runtimeCallInfo)533 ArkUINativeModuleValue XComponentBridge::SetHdrBrightness(ArkUIRuntimeCallInfo *runtimeCallInfo)
534 {
535     EcmaVM *vm = runtimeCallInfo->GetVM();
536     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
537     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
538     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
539     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
540     if (secondArg->IsNumber()) {
541         float hdrBrightness = secondArg->ToNumber(vm)->Value();
542         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentHdrBrightness(nativeNode, hdrBrightness);
543     } else {
544         GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentHdrBrightness(nativeNode);
545     }
546     return panda::JSValueRef::Undefined(vm);
547 }
548 
ResetHdrBrightness(ArkUIRuntimeCallInfo * runtimeCallInfo)549 ArkUINativeModuleValue XComponentBridge::ResetHdrBrightness(ArkUIRuntimeCallInfo *runtimeCallInfo)
550 {
551     EcmaVM *vm = runtimeCallInfo->GetVM();
552     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
553     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
554     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
555     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentHdrBrightness(nativeNode);
556     return panda::JSValueRef::Undefined(vm);
557 }
558 
SetEnableTransparentLayer(ArkUIRuntimeCallInfo * runtimeCallInfo)559 ArkUINativeModuleValue XComponentBridge::SetEnableTransparentLayer(ArkUIRuntimeCallInfo *runtimeCallInfo)
560 {
561     EcmaVM *vm = runtimeCallInfo->GetVM();
562     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
563     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
564     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
565     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
566     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
567     if (secondArg->IsBoolean()) {
568         bool enableTransparentLayer = secondArg->ToBoolean(vm)->Value();
569         GetArkUINodeModifiers()->getXComponentModifier()->setXComponentEnableTransparentLayer(
570             nativeNode, enableTransparentLayer);
571     } else {
572         GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableTransparentLayer(nativeNode);
573     }
574     return panda::JSValueRef::Undefined(vm);
575 }
576 
ResetEnableTransparentLayer(ArkUIRuntimeCallInfo * runtimeCallInfo)577 ArkUINativeModuleValue XComponentBridge::ResetEnableTransparentLayer(ArkUIRuntimeCallInfo *runtimeCallInfo)
578 {
579     EcmaVM *vm = runtimeCallInfo->GetVM();
580     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
581     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
582     CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
583     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
584     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableTransparentLayer(nativeNode);
585     return panda::JSValueRef::Undefined(vm);
586 }
587 
SetRenderFit(ArkUIRuntimeCallInfo * runtimeCallInfo)588 ArkUINativeModuleValue XComponentBridge::SetRenderFit(ArkUIRuntimeCallInfo* runtimeCallInfo)
589 {
590     EcmaVM *vm = runtimeCallInfo->GetVM();
591     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
592     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
593     auto fitModeArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
594     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
595     auto renderFit = static_cast<int32_t>(RenderFit::TOP_LEFT);
596     if (fitModeArg->IsNumber()) {
597         renderFit = fitModeArg->Int32Value(vm);
598     }
599     GetArkUINodeModifiers()->getXComponentModifier()->setXComponentRenderFit(nativeNode, renderFit);
600     return panda::JSValueRef::Undefined(vm);
601 }
602 
ResetRenderFit(ArkUIRuntimeCallInfo * runtimeCallInfo)603 ArkUINativeModuleValue XComponentBridge::ResetRenderFit(ArkUIRuntimeCallInfo* runtimeCallInfo)
604 {
605     EcmaVM *vm = runtimeCallInfo->GetVM();
606     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
607     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
608     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
609     GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentRenderFit(nativeNode);
610     return panda::JSValueRef::Undefined(vm);
611 }
612 } // namespace OHOS::Ace::NG
613