1 /*
2 * Copyright (c) 2021-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 "bridge/declarative_frontend/jsview/js_utils.h"
17
18 #include "scope_manager/native_scope_manager.h"
19
20 #if !defined(PREVIEW)
21 #include <dlfcn.h>
22 #endif
23
24 #include "base/image/pixel_map.h"
25 #include "base/log/ace_trace.h"
26 #include "base/want/want_wrap.h"
27 #include "frameworks/bridge/common/utils/engine_helper.h"
28 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
29 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
30 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
31
32 namespace OHOS::Ace::Framework {
33 namespace {
34 // Min disable event api version.
35 constexpr int32_t MIN_DISABLE_EVENT_VERSION = 10;
36 } // namespace
37
38 #if !defined(PREVIEW)
CreatePixelMapFromNapiValue(JSRef<JSVal> obj)39 RefPtr<PixelMap> CreatePixelMapFromNapiValue(JSRef<JSVal> obj)
40 {
41 if (!obj->IsObject()) {
42 LOGE("info[0] is not an object when try CreatePixelMapFromNapiValue");
43 return nullptr;
44 }
45 auto engine = EngineHelper::GetCurrentEngine();
46 if (!engine) {
47 LOGE("CreatePixelMapFromNapiValue engine is null");
48 return nullptr;
49 }
50 auto* nativeEngine = engine->GetNativeEngine();
51 if (nativeEngine == nullptr) {
52 LOGE("nativeEngine is nullptr.");
53 return nullptr;
54 }
55 #ifdef USE_ARK_ENGINE
56 panda::Local<JsiValue> value = obj.Get().GetLocalHandle();
57 #endif
58 JSValueWrapper valueWrapper = value;
59
60 ScopeRAII scope(nativeEngine->GetScopeManager());
61 NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper);
62
63 PixelMapNapiEntry pixelMapNapiEntry = JsEngine::GetPixelMapNapiEntry();
64 if (!pixelMapNapiEntry) {
65 LOGE("pixelMapNapiEntry is null");
66 return nullptr;
67 }
68
69 void* pixmapPtrAddr =
70 pixelMapNapiEntry(reinterpret_cast<napi_env>(nativeEngine), reinterpret_cast<napi_value>(nativeValue));
71 if (pixmapPtrAddr == nullptr) {
72 LOGE("Failed to get pixmap pointer");
73 return nullptr;
74 }
75 return PixelMap::CreatePixelMap(pixmapPtrAddr);
76 }
77
78 namespace {
UnwrapNapiValue(const JSRef<JSVal> & obj)79 void* UnwrapNapiValue(const JSRef<JSVal>& obj)
80 {
81 #ifdef ENABLE_ROSEN_BACKEND
82 if (!obj->IsObject()) {
83 LOGE("info[0] is not an object when try CreateFromNapiValue");
84 return nullptr;
85 }
86 auto engine = EngineHelper::GetCurrentEngine();
87 CHECK_NULL_RETURN(engine, nullptr);
88 auto nativeEngine = engine->GetNativeEngine();
89 CHECK_NULL_RETURN(nativeEngine, nullptr);
90 #ifdef USE_ARK_ENGINE
91 panda::Local<JsiValue> value = obj.Get().GetLocalHandle();
92 #endif
93 JSValueWrapper valueWrapper = value;
94
95 ScopeRAII scope(nativeEngine->GetScopeManager());
96 NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper);
97 CHECK_NULL_RETURN(nativeValue, nullptr);
98 NativeObject* object = static_cast<NativeObject*>(nativeValue->GetInterface(NativeObject::INTERFACE_ID));
99 CHECK_NULL_RETURN(object, nullptr);
100 return object->GetNativePointer();
101 }
102 } // namespace
103
GetDrawablePixmap(JSRef<JSVal> obj)104 RefPtr<PixelMap> GetDrawablePixmap(JSRef<JSVal> obj)
105 {
106 return PixelMap::GetFromDrawable(UnwrapNapiValue(obj));
107 }
108
CreateRSNodeFromNapiValue(JSRef<JSVal> obj)109 const std::shared_ptr<Rosen::RSNode> CreateRSNodeFromNapiValue(JSRef<JSVal> obj)
110 {
111 auto nodePtr = static_cast<std::shared_ptr<Rosen::RSNode>*>(UnwrapNapiValue(obj));
112 if (nodePtr == nullptr) {
113 return nullptr;
114 }
115 return *nodePtr;
116 #else
117 return nullptr;
118 }
119 #endif
120 } // namespace
121
CreateWantWrapFromNapiValue(JSRef<JSVal> obj)122 RefPtr<OHOS::Ace::WantWrap> CreateWantWrapFromNapiValue(JSRef<JSVal> obj)
123 {
124 if (!obj->IsObject()) {
125 LOGE("invalid object when try CreateWantWrapFromNapiValue");
126 return nullptr;
127 }
128 auto engine = EngineHelper::GetCurrentEngine();
129 CHECK_NULL_RETURN(engine, nullptr);
130
131 NativeEngine* nativeEngine = engine->GetNativeEngine();
132 CHECK_NULL_RETURN(nativeEngine, nullptr);
133
134 #ifdef USE_ARK_ENGINE
135 panda::Local<JsiValue> value = obj.Get().GetLocalHandle();
136 #endif
137 JSValueWrapper valueWrapper = value;
138
139 ScopeRAII scope(nativeEngine->GetScopeManager());
140 NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper);
141
142 return WantWrap::CreateWantWrap(reinterpret_cast<void*>(nativeEngine), reinterpret_cast<void*>(nativeValue));
143 }
144
145 #endif
146
147 // When the api version >= 10, it is disable event version.
IsDisableEventVersion()148 bool IsDisableEventVersion()
149 {
150 auto container = Container::Current();
151 if (!container) {
152 LOGW("container is null");
153 return false;
154 }
155 auto pipelineContext = container->GetPipelineContext();
156 if (!pipelineContext) {
157 LOGW("pipelineContext is null!");
158 return false;
159 }
160 if (pipelineContext->GetMinPlatformVersion() >= MIN_DISABLE_EVENT_VERSION) {
161 LOGD("The version supports disable event callback.");
162 return true;
163 }
164 LOGW("The version doesn't support disable event callback.");
165 return false;
166 }
167 } // namespace OHOS::Ace::Framework
168