• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_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 #if !defined(PREVIEW)
34 class ScopeRAII {
35 public:
ScopeRAII(NativeScopeManager * manager)36     explicit ScopeRAII(NativeScopeManager* manager) : manager_(manager)
37     {
38         scope_ = manager_->Open();
39     }
~ScopeRAII()40     ~ScopeRAII()
41     {
42         manager_->Close(scope_);
43     }
44 
45 private:
46     NativeScopeManager* manager_;
47     NativeScope* scope_;
48 };
49 
CreatePixelMapFromNapiValue(JSRef<JSVal> obj)50 RefPtr<PixelMap> CreatePixelMapFromNapiValue(JSRef<JSVal> obj)
51 {
52     if (!obj->IsObject()) {
53         LOGE("info[0] is not an object when try CreatePixelMapFromNapiValue");
54         return nullptr;
55     }
56     auto engine = EngineHelper::GetCurrentEngine();
57     if (!engine) {
58         LOGE("CreatePixelMapFromNapiValue engine is null");
59         return nullptr;
60     }
61     auto* nativeEngine = engine->GetNativeEngine();
62     if (nativeEngine == nullptr) {
63         LOGE("nativeEngine is nullptr.");
64         return nullptr;
65     }
66 #ifdef USE_V8_ENGINE
67     v8::Local<v8::Value> value = obj->operator v8::Local<v8::Value>();
68 #elif USE_QUICKJS_ENGINE
69     JSValue value = obj.Get().GetHandle();
70 #elif USE_ARK_ENGINE
71     panda::Local<JsiValue> value = obj.Get().GetLocalHandle();
72 #endif
73     JSValueWrapper valueWrapper = value;
74 
75     ScopeRAII scope(nativeEngine->GetScopeManager());
76     NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper);
77 
78     PixelMapNapiEntry pixelMapNapiEntry = JsEngine::GetPixelMapNapiEntry();
79     if (!pixelMapNapiEntry) {
80         LOGE("pixelMapNapiEntry is null");
81         return nullptr;
82     }
83 
84     void* pixmapPtrAddr =
85         pixelMapNapiEntry(reinterpret_cast<napi_env>(nativeEngine), reinterpret_cast<napi_value>(nativeValue));
86     if (pixmapPtrAddr == nullptr) {
87         LOGE("Failed to get pixmap pointer");
88         return nullptr;
89     }
90     return PixelMap::CreatePixelMap(pixmapPtrAddr);
91 }
92 
CreateRSNodeFromNapiValue(JSRef<JSVal> obj)93 const std::shared_ptr<Rosen::RSNode> CreateRSNodeFromNapiValue(JSRef<JSVal> obj)
94 {
95 #ifdef ENABLE_ROSEN_BACKEND
96     if (!obj->IsObject()) {
97         LOGE("info[0] is not an object when try CreateRSNodeFromNapiValue");
98         return nullptr;
99     }
100     auto engine = EngineHelper::GetCurrentEngine();
101     if (!engine) {
102         LOGE("CreateRSNodeFromNapiValue engine is null");
103         return nullptr;
104     }
105     auto nativeEngine = engine->GetNativeEngine();
106     if (nativeEngine == nullptr) {
107         LOGE("nativeEngine is nullptr.");
108         return nullptr;
109     }
110 #ifdef USE_V8_ENGINE
111     v8::Local<v8::Value> value = obj->operator v8::Local<v8::Value>();
112 #elif USE_QUICKJS_ENGINE
113     JSValue value = obj.Get().GetHandle();
114 #elif USE_ARK_ENGINE
115     panda::Local<JsiValue> value = obj.Get().GetLocalHandle();
116 #endif
117     JSValueWrapper valueWrapper = value;
118 
119     ScopeRAII scope(nativeEngine->GetScopeManager());
120     NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper);
121     if (nativeValue == nullptr) {
122         LOGE("nativeValue is nullptr.");
123         return nullptr;
124     }
125     NativeObject* object = static_cast<NativeObject*>(nativeValue->GetInterface(NativeObject::INTERFACE_ID));
126     if (object == nullptr) {
127         return nullptr;
128     }
129 
130     auto nodePtr = static_cast<std::shared_ptr<Rosen::RSNode>*>(object->GetNativePointer());
131     if (nodePtr == nullptr) {
132         return nullptr;
133     }
134     return *nodePtr;
135 #else
136     return nullptr;
137 #endif
138 }
139 
CreateWantWrapFromNapiValue(JSRef<JSVal> obj)140 RefPtr<OHOS::Ace::WantWrap> CreateWantWrapFromNapiValue(JSRef<JSVal> obj)
141 {
142     if (!obj->IsObject()) {
143         LOGE("invalid object when try CreateWantWrapFromNapiValue");
144         return nullptr;
145     }
146     auto engine = EngineHelper::GetCurrentEngine();
147     CHECK_NULL_RETURN(engine, nullptr);
148 
149     NativeEngine* nativeEngine = engine->GetNativeEngine();
150     CHECK_NULL_RETURN(nativeEngine, nullptr);
151 
152 #ifdef USE_V8_ENGINE
153     v8::Local<v8::Value> value = obj->operator v8::Local<v8::Value>();
154 #elif USE_QUICKJS_ENGINE
155     JSValue value = obj.Get().GetHandle();
156 #elif USE_ARK_ENGINE
157     panda::Local<JsiValue> value = obj.Get().GetLocalHandle();
158 #endif
159     JSValueWrapper valueWrapper = value;
160 
161     ScopeRAII scope(nativeEngine->GetScopeManager());
162     NativeValue* nativeValue = nativeEngine->ValueToNativeValue(valueWrapper);
163 
164     return WantWrap::CreateWantWrap(reinterpret_cast<void*>(nativeEngine), reinterpret_cast<void*>(nativeValue));
165 }
166 
167 #endif
168 } // namespace OHOS::Ace::Framework
169