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*)(¶ms);
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()->setXComponentBackgroundColorWithColorSpace(
366 nativeNode, color.GetValue(), color.GetColorSpace());
367 }
368 return panda::JSValueRef::Undefined(vm);
369 }
370
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)371 ArkUINativeModuleValue XComponentBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo *runtimeCallInfo)
372 {
373 EcmaVM *vm = runtimeCallInfo->GetVM();
374 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
375 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
376 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
377 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentBackgroundColor(nativeNode);
378 return panda::JSValueRef::Undefined(vm);
379 }
380
SetOpacity(ArkUIRuntimeCallInfo * runtimeCallInfo)381 ArkUINativeModuleValue XComponentBridge::SetOpacity(ArkUIRuntimeCallInfo *runtimeCallInfo)
382 {
383 EcmaVM *vm = runtimeCallInfo->GetVM();
384 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
385 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
386 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
387 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
388 double opacity;
389 if (!ArkTSUtils::ParseJsDouble(vm, secondArg, opacity)) {
390 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentOpacity(nativeNode);
391 } else {
392 GetArkUINodeModifiers()->getXComponentModifier()->setXComponentOpacity(nativeNode, opacity);
393 }
394 return panda::JSValueRef::Undefined(vm);
395 }
396
ResetOpacity(ArkUIRuntimeCallInfo * runtimeCallInfo)397 ArkUINativeModuleValue XComponentBridge::ResetOpacity(ArkUIRuntimeCallInfo *runtimeCallInfo)
398 {
399 EcmaVM *vm = runtimeCallInfo->GetVM();
400 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
401 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
402 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
403 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentOpacity(nativeNode);
404 return panda::JSValueRef::Undefined(vm);
405 }
406
SetGrayscale(ArkUIRuntimeCallInfo * runtimeCallInfo)407 ArkUINativeModuleValue XComponentBridge::SetGrayscale(ArkUIRuntimeCallInfo *runtimeCallInfo)
408 {
409 EcmaVM* vm = runtimeCallInfo->GetVM();
410 return panda::JSValueRef::Undefined(vm);
411 }
412
ResetGrayscale(ArkUIRuntimeCallInfo * runtimeCallInfo)413 ArkUINativeModuleValue XComponentBridge::ResetGrayscale(ArkUIRuntimeCallInfo *runtimeCallInfo)
414 {
415 EcmaVM* vm = runtimeCallInfo->GetVM();
416 return panda::JSValueRef::Undefined(vm);
417 }
418
SetOnLoad(ArkUIRuntimeCallInfo * runtimeCallInfo)419 ArkUINativeModuleValue XComponentBridge::SetOnLoad(ArkUIRuntimeCallInfo *runtimeCallInfo)
420 {
421 EcmaVM* vm = runtimeCallInfo->GetVM();
422 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
423 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
424 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
425 CHECK_NULL_RETURN(secondArg->IsFunction(vm), panda::JSValueRef::Undefined(vm));
426 auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
427 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
428 auto obj = secondArg->ToObject(vm);
429 panda::Local<panda::FunctionRef> func = obj;
430 auto onLoad = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
431 const std::string& xcomponentId) {
432 panda::LocalScope pandaScope(vm);
433 panda::TryCatch trycatch(vm);
434 PipelineContext::SetCallBackNode(node);
435 std::vector<Local<JSValueRef>> argv;
436 Framework::JSRef<Framework::JSVal> jsVal;
437 if (Framework::XComponentClient::GetInstance().GetJSVal(xcomponentId, jsVal)) {
438 argv.emplace_back(jsVal->GetLocalHandle());
439 }
440 func->Call(vm, func.ToLocal(), argv.data(), argv.size());
441 TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponentNode[%{public}s] onLoad", xcomponentId.c_str());
442 };
443 XComponentModelNG::SetOnLoad(frameNode, std::move(onLoad));
444 return panda::JSValueRef::Undefined(vm);
445 }
446
ResetOnLoad(ArkUIRuntimeCallInfo * runtimeCallInfo)447 ArkUINativeModuleValue XComponentBridge::ResetOnLoad(ArkUIRuntimeCallInfo *runtimeCallInfo)
448 {
449 EcmaVM* vm = runtimeCallInfo->GetVM();
450 return panda::JSValueRef::Undefined(vm);
451 }
452
SetOnDestroy(ArkUIRuntimeCallInfo * runtimeCallInfo)453 ArkUINativeModuleValue XComponentBridge::SetOnDestroy(ArkUIRuntimeCallInfo *runtimeCallInfo)
454 {
455 EcmaVM* vm = runtimeCallInfo->GetVM();
456 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
457 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
458 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
459 CHECK_NULL_RETURN(secondArg->IsFunction(vm), panda::JSValueRef::Undefined(vm));
460 auto* frameNode = reinterpret_cast<FrameNode*>(firstArg->ToNativePointer(vm)->Value());
461 CHECK_NULL_RETURN(frameNode, panda::JSValueRef::Undefined(vm));
462 auto obj = secondArg->ToObject(vm);
463 panda::Local<panda::FunctionRef> func = obj;
464 auto onDestroy = [vm, func = panda::CopyableGlobal(vm, func), node = AceType::WeakClaim(frameNode)](
465 const std::string& xcomponentId) {
466 panda::LocalScope pandaScope(vm);
467 panda::TryCatch trycatch(vm);
468 PipelineContext::SetCallBackNode(node);
469 func->Call(vm, func.ToLocal(), nullptr, 0);
470 TAG_LOGI(AceLogTag::ACE_XCOMPONENT, "XComponentNode[%{public}s] onDestroy", xcomponentId.c_str());
471 };
472 XComponentModelNG::SetOnDestroy(frameNode, std::move(onDestroy));
473 return panda::JSValueRef::Undefined(vm);
474 }
475
ResetOnDestroy(ArkUIRuntimeCallInfo * runtimeCallInfo)476 ArkUINativeModuleValue XComponentBridge::ResetOnDestroy(ArkUIRuntimeCallInfo *runtimeCallInfo)
477 {
478 EcmaVM* vm = runtimeCallInfo->GetVM();
479 return panda::JSValueRef::Undefined(vm);
480 }
481
SetEnableAnalyzer(ArkUIRuntimeCallInfo * runtimeCallInfo)482 ArkUINativeModuleValue XComponentBridge::SetEnableAnalyzer(ArkUIRuntimeCallInfo *runtimeCallInfo)
483 {
484 EcmaVM *vm = runtimeCallInfo->GetVM();
485 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
486 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
487 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
488 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
489 if (secondArg->IsBoolean()) {
490 bool boolValue = secondArg->ToBoolean(vm)->Value();
491 GetArkUINodeModifiers()->getXComponentModifier()->setXComponentEnableAnalyzer(nativeNode, boolValue);
492 } else {
493 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableAnalyzer(nativeNode);
494 }
495 return panda::JSValueRef::Undefined(vm);
496 }
497
ResetEnableAnalyzer(ArkUIRuntimeCallInfo * runtimeCallInfo)498 ArkUINativeModuleValue XComponentBridge::ResetEnableAnalyzer(ArkUIRuntimeCallInfo *runtimeCallInfo)
499 {
500 EcmaVM *vm = runtimeCallInfo->GetVM();
501 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
502 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
503 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
504 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableAnalyzer(nativeNode);
505 return panda::JSValueRef::Undefined(vm);
506 }
507
SetEnableSecure(ArkUIRuntimeCallInfo * runtimeCallInfo)508 ArkUINativeModuleValue XComponentBridge::SetEnableSecure(ArkUIRuntimeCallInfo *runtimeCallInfo)
509 {
510 EcmaVM *vm = runtimeCallInfo->GetVM();
511 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
512 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
513 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
514 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
515 if (secondArg->IsBoolean()) {
516 bool boolValue = secondArg->ToBoolean(vm)->Value();
517 GetArkUINodeModifiers()->getXComponentModifier()->setXComponentEnableSecure(nativeNode, boolValue);
518 } else {
519 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableSecure(nativeNode);
520 }
521 return panda::JSValueRef::Undefined(vm);
522 }
523
ResetEnableSecure(ArkUIRuntimeCallInfo * runtimeCallInfo)524 ArkUINativeModuleValue XComponentBridge::ResetEnableSecure(ArkUIRuntimeCallInfo *runtimeCallInfo)
525 {
526 EcmaVM *vm = runtimeCallInfo->GetVM();
527 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
528 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
529 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
530 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableSecure(nativeNode);
531 return panda::JSValueRef::Undefined(vm);
532 }
533
SetHdrBrightness(ArkUIRuntimeCallInfo * runtimeCallInfo)534 ArkUINativeModuleValue XComponentBridge::SetHdrBrightness(ArkUIRuntimeCallInfo *runtimeCallInfo)
535 {
536 EcmaVM *vm = runtimeCallInfo->GetVM();
537 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
538 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
539 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
540 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
541 if (secondArg->IsNumber()) {
542 float hdrBrightness = secondArg->ToNumber(vm)->Value();
543 GetArkUINodeModifiers()->getXComponentModifier()->setXComponentHdrBrightness(nativeNode, hdrBrightness);
544 } else {
545 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentHdrBrightness(nativeNode);
546 }
547 return panda::JSValueRef::Undefined(vm);
548 }
549
ResetHdrBrightness(ArkUIRuntimeCallInfo * runtimeCallInfo)550 ArkUINativeModuleValue XComponentBridge::ResetHdrBrightness(ArkUIRuntimeCallInfo *runtimeCallInfo)
551 {
552 EcmaVM *vm = runtimeCallInfo->GetVM();
553 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
554 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
555 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
556 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentHdrBrightness(nativeNode);
557 return panda::JSValueRef::Undefined(vm);
558 }
559
SetEnableTransparentLayer(ArkUIRuntimeCallInfo * runtimeCallInfo)560 ArkUINativeModuleValue XComponentBridge::SetEnableTransparentLayer(ArkUIRuntimeCallInfo *runtimeCallInfo)
561 {
562 EcmaVM *vm = runtimeCallInfo->GetVM();
563 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
564 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
565 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
566 CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
567 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
568 if (secondArg->IsBoolean()) {
569 bool enableTransparentLayer = secondArg->ToBoolean(vm)->Value();
570 GetArkUINodeModifiers()->getXComponentModifier()->setXComponentEnableTransparentLayer(
571 nativeNode, enableTransparentLayer);
572 } else {
573 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableTransparentLayer(nativeNode);
574 }
575 return panda::JSValueRef::Undefined(vm);
576 }
577
ResetEnableTransparentLayer(ArkUIRuntimeCallInfo * runtimeCallInfo)578 ArkUINativeModuleValue XComponentBridge::ResetEnableTransparentLayer(ArkUIRuntimeCallInfo *runtimeCallInfo)
579 {
580 EcmaVM *vm = runtimeCallInfo->GetVM();
581 CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
582 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
583 CHECK_NULL_RETURN(firstArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
584 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
585 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentEnableTransparentLayer(nativeNode);
586 return panda::JSValueRef::Undefined(vm);
587 }
588
SetRenderFit(ArkUIRuntimeCallInfo * runtimeCallInfo)589 ArkUINativeModuleValue XComponentBridge::SetRenderFit(ArkUIRuntimeCallInfo* runtimeCallInfo)
590 {
591 EcmaVM *vm = runtimeCallInfo->GetVM();
592 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
593 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
594 auto fitModeArg = runtimeCallInfo->GetCallArgRef(ARG_ID);
595 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
596 auto renderFit = static_cast<int32_t>(RenderFit::TOP_LEFT);
597 if (fitModeArg->IsNumber()) {
598 renderFit = fitModeArg->Int32Value(vm);
599 }
600 GetArkUINodeModifiers()->getXComponentModifier()->setXComponentRenderFit(nativeNode, renderFit);
601 return panda::JSValueRef::Undefined(vm);
602 }
603
ResetRenderFit(ArkUIRuntimeCallInfo * runtimeCallInfo)604 ArkUINativeModuleValue XComponentBridge::ResetRenderFit(ArkUIRuntimeCallInfo* runtimeCallInfo)
605 {
606 EcmaVM *vm = runtimeCallInfo->GetVM();
607 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
608 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(ARG_FIRST);
609 auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
610 GetArkUINodeModifiers()->getXComponentModifier()->resetXComponentRenderFit(nativeNode);
611 return panda::JSValueRef::Undefined(vm);
612 }
613 } // namespace OHOS::Ace::NG
614