• 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 
16 #include "xcomponent_controller.h"
17 
18 #include "js_native_api.h"
19 #include "utils.h"
20 
21 #include "bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
22 #include "core/components_ng/pattern/xcomponent/xcomponent_pattern.h"
23 
24 namespace OHOS::Ace {
25 namespace {
26 const char* NODEPTR_OF_UINODE = "nodePtr_";
27 constexpr char XCOMPONENT_GET_CONTROLLER_FUNC[] = "OHOS_ACE_GetXComponentController";
28 constexpr char XCOMPONENT_CHANGE_SURFACE_CALLBACKMODE_FUNC[] = "OHOS_ACE_ChangeXComponentSurfaceCallbackMode";
29 constexpr char XCOMPONENT_SET_RENDER_FIT_FUNC[] = "OHOS_ACE_SetRenderFitBySurfaceId";
30 constexpr char XCOMPONENT_GET_RENDER_FIT_FUNC[] = "OHOS_ACE_GetRenderFitBySurfaceId";
31 constexpr char XCOMPONENT_GET_SURFACE_ROTATION_FUNC[] = "OHOS_ACE_GetSurfaceRotationBySurfaceId";
32 using GetControllerFunc = void (*)(void*, void*);
33 using ChangeSurfaceCallbackModeFunc = XComponentControllerErrorCode (*)(void*, char);
34 using SetRenderFitFunc = XComponentControllerErrorCode (*)(const std::string&, int32_t, bool);
35 using GetRenderFitFunc = XComponentControllerErrorCode (*)(const std::string&, int32_t&, bool&);
36 using GetSurfaceRotationFunc = XComponentControllerErrorCode (*)(const std::string&, bool&);
37 } // namespace
GetController(void * jsController,void * controller)38 void GetController(void* jsController, void* controller)
39 {
40     static GetControllerFunc entry = nullptr;
41     if (entry == nullptr) {
42         LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
43         CHECK_NULL_VOID(handle);
44         entry = reinterpret_cast<GetControllerFunc>(LOADSYM(handle, XCOMPONENT_GET_CONTROLLER_FUNC));
45         if (entry == nullptr) {
46             FREELIB(handle);
47             return;
48         }
49     }
50     entry(jsController, controller);
51 }
52 
ChangeSurfaceCallbackMode(void * frameNode,char mode)53 XComponentControllerErrorCode ChangeSurfaceCallbackMode(void* frameNode, char mode)
54 {
55     static ChangeSurfaceCallbackModeFunc entry = nullptr;
56     if (entry == nullptr) {
57         LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
58         CHECK_NULL_RETURN(handle, XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_BAD_PARAMETER);
59         entry = reinterpret_cast<ChangeSurfaceCallbackModeFunc>(
60             LOADSYM(handle, XCOMPONENT_CHANGE_SURFACE_CALLBACKMODE_FUNC));
61         if (entry == nullptr) {
62             FREELIB(handle);
63             return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_BAD_PARAMETER;
64         }
65     }
66     return entry(frameNode, mode);
67 }
GetXComponentControllerFromNapiValue(napi_env env,napi_value napiValue)68 std::shared_ptr<XComponentController> XComponentController::GetXComponentControllerFromNapiValue(
69     napi_env env, napi_value napiValue)
70 {
71     CHECK_NULL_RETURN(env, nullptr);
72     const auto* vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm();
73     auto localRef = NapiValueToLocalValue(napiValue);
74     if (localRef->IsNull()) {
75         return nullptr;
76     }
77     std::shared_ptr<XComponentController> controller;
78     GetController(Local<panda::ObjectRef>(localRef)->GetNativePointerField(vm, 0), &controller);
79     return controller;
80 }
81 
SetSurfaceCallbackMode(napi_env env,napi_value node,SurfaceCallbackMode mode)82 XComponentControllerErrorCode XComponentController::SetSurfaceCallbackMode(
83     napi_env env, napi_value node, SurfaceCallbackMode mode)
84 {
85     CHECK_NULL_RETURN(env, XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_BAD_PARAMETER);
86     const auto* vm = reinterpret_cast<NativeEngine*>(env)->GetEcmaVm();
87     auto nodeRef = NapiValueToLocalValue(node);
88     if (nodeRef.IsEmpty() || !nodeRef->IsObject(vm)) {
89         return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_BAD_PARAMETER;
90     }
91     auto nodeObj = nodeRef->ToObject(vm);
92     panda::Local<panda::JSValueRef> nodePtr = nodeObj->Get(vm, panda::StringRef::NewFromUtf8(vm, NODEPTR_OF_UINODE));
93     if (nodePtr.IsEmpty() || nodePtr->IsNull() || nodePtr->IsUndefined()) {
94         return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_BAD_PARAMETER;
95     }
96     if (!nodePtr->IsNativePointer(vm)) {
97         return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_BAD_PARAMETER;
98     }
99     return ChangeSurfaceCallbackMode(nodePtr->ToNativePointer(vm)->Value(), static_cast<char>(mode));
100 }
101 
SetRenderFitBySurfaceId(const std::string & surfaceId,int32_t renderFitNumber,bool isRenderFitNewVersionEnabled)102 XComponentControllerErrorCode XComponentController::SetRenderFitBySurfaceId(
103     const std::string& surfaceId, int32_t renderFitNumber, bool isRenderFitNewVersionEnabled)
104 {
105     static SetRenderFitFunc setRenderFitMethod = nullptr;
106     if (setRenderFitMethod == nullptr) {
107         LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
108         CHECK_NULL_RETURN(handle, XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_LOAD_LIB_FAILED);
109         setRenderFitMethod = reinterpret_cast<SetRenderFitFunc>(LOADSYM(handle, XCOMPONENT_SET_RENDER_FIT_FUNC));
110         if (setRenderFitMethod == nullptr) {
111             FREELIB(handle);
112             return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_LOAD_LIB_FAILED;
113         }
114     }
115     return setRenderFitMethod(surfaceId, renderFitNumber, isRenderFitNewVersionEnabled);
116 }
117 
GetRenderFitBySurfaceId(const std::string & surfaceId,int32_t & renderFitNumber,bool & isRenderFitNewVersionEnabled)118 XComponentControllerErrorCode XComponentController::GetRenderFitBySurfaceId(
119     const std::string& surfaceId, int32_t& renderFitNumber, bool& isRenderFitNewVersionEnabled)
120 {
121     static GetRenderFitFunc getRenderFitMethod = nullptr;
122     if (getRenderFitMethod == nullptr) {
123         LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
124         CHECK_NULL_RETURN(handle, XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_LOAD_LIB_FAILED);
125         getRenderFitMethod = reinterpret_cast<GetRenderFitFunc>(LOADSYM(handle, XCOMPONENT_GET_RENDER_FIT_FUNC));
126         if (getRenderFitMethod == nullptr) {
127             FREELIB(handle);
128             return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_LOAD_LIB_FAILED;
129         }
130     }
131     return getRenderFitMethod(surfaceId, renderFitNumber, isRenderFitNewVersionEnabled);
132 }
133 
GetSurfaceRotationBySurfaceId(const std::string & surfaceId,bool & isSurfaceLock)134 XComponentControllerErrorCode XComponentController::GetSurfaceRotationBySurfaceId(
135     const std::string& surfaceId, bool& isSurfaceLock)
136 {
137     static GetSurfaceRotationFunc getSurfaceRotationMethod = nullptr;
138     if (getSurfaceRotationMethod == nullptr) {
139         LIBHANDLE handle = LOADLIB(AceForwardCompatibility::GetAceLibName());
140         CHECK_NULL_RETURN(handle, XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_LOAD_LIB_FAILED);
141         getSurfaceRotationMethod =
142             reinterpret_cast<GetSurfaceRotationFunc>(LOADSYM(handle, XCOMPONENT_GET_SURFACE_ROTATION_FUNC));
143         if (getSurfaceRotationMethod == nullptr) {
144             FREELIB(handle);
145             return XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_LOAD_LIB_FAILED;
146         }
147     }
148     return getSurfaceRotationMethod(surfaceId, isSurfaceLock);
149 }
150 } // namespace OHOS::Ace
151