1 /*
2 * Copyright (c) 2024 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 "ark_interop_helper.h"
17 #include "ark_native_engine.h"
18 #include "inner_api/cjffi/ark_interop/ark_interop_internal.h"
19 #include "inner_api/cjffi/ark_interop/ark_interop_log.h"
20 #ifndef PREVIEW
21 #include "core/common/container_scope.h"
22 #include "custom_scope.h"
23 #include "napi_base_context.h"
24 #include "ability.h"
25 #endif
26
27 using namespace panda::ecmascript;
28
29 extern "C" {
ArkTsValuetoNapiValue(napi_env env,ARKTS_Value arkValue)30 napi_value ArkTsValuetoNapiValue(napi_env env, ARKTS_Value arkValue)
31 {
32 LOGI("ArkTsValuetoNapiValue start");
33 if (env == nullptr) {
34 LOGE("FfiOHOSArkTsValuetoNapiValue Error: env is null!");
35 return nullptr;
36 }
37 Local<JSValueRef> js_value_ref = ARKTS_ToHandle<JSValueRef>(arkValue);
38 auto ark_native_obj = ArkNativeEngine::ArkValueToNapiValue(env, js_value_ref);
39 return ark_native_obj;
40 };
41
NapiValueToArkTsValue(napi_value value)42 ARKTS_Value NapiValueToArkTsValue(napi_value value)
43 {
44 auto ref = BIT_CAST(value, Local<JSValueRef>);
45 return ARKTS_FromHandle(ref);
46 }
47
IsStageMode(napi_env env,napi_value context)48 bool IsStageMode(napi_env env, napi_value context)
49 {
50 #ifndef PREVIEW
51 LOGI("IsStageMode start");
52 bool isStageMode = false;
53 napi_status status = OHOS::AbilityRuntime::IsStageContext(env, context, isStageMode);
54 if (status != napi_ok || !isStageMode) {
55 LOGI("IsStageMode false");
56 return false;
57 } else {
58 LOGI("IsStageMode true");
59 return true;
60 }
61 #else
62 return false;
63 #endif
64 }
65
GetContextStageMode(napi_env env,napi_value context)66 void* GetContextStageMode(napi_env env, napi_value context)
67 {
68 #ifndef PREVIEW
69 LOGI("GetContextStageMode start");
70 if (!env || !context) {
71 LOGE("argument invalid");
72 return nullptr;
73 }
74 napi_valuetype type;
75 if (napi_typeof(env, context, &type) != napi_ok) {
76 LOGE("invalid napi value");
77 return nullptr;
78 }
79 if (type != napi_object) {
80 LOGE("not a object");
81 return nullptr;
82 }
83 void* data;
84 if (napi_unwrap(env, context, &data) != napi_ok) {
85 LOGE("no bind native object");
86 return nullptr;
87 }
88 if (!data) {
89 LOGE("native object is null");
90 return nullptr;
91 }
92 auto ability = OHOS::AbilityRuntime::GetStageModeContext(env, context);
93 if (ability == nullptr) {
94 LOGE("Failed to get native ability instance");
95 return nullptr;
96 }
97 LOGI("GetContextStageMode success");
98 return ability.get();
99 #else
100 return nullptr;
101 #endif
102 }
103 #ifndef PREVIEW
Enter()104 void CustomScope::Enter()
105 {
106 last_ = OHOS::Ace::ContainerScope::CurrentId();
107 OHOS::Ace::ContainerScope::UpdateCurrent(id_);
108 }
109
Exit() const110 void CustomScope::Exit() const
111 {
112 OHOS::Ace::ContainerScope::UpdateCurrent(last_);
113 }
114
ARKTS_GetCurrentContainerId()115 int32_t ARKTS_GetCurrentContainerId()
116 {
117 return OHOS::Ace::ContainerScope::CurrentId();
118 }
119
ARKTS_CreateContainerScope(int32_t id)120 ContainerScope ARKTS_CreateContainerScope(int32_t id)
121 {
122 return new CustomScope(id);
123 }
124
ARKTS_DestroyContainerScope(ContainerScope scope)125 void ARKTS_DestroyContainerScope(ContainerScope scope)
126 {
127 delete scope;
128 }
129
ARKTS_EnterContainerScope(ContainerScope scope)130 void ARKTS_EnterContainerScope(ContainerScope scope)
131 {
132 scope->Enter();
133 }
134
ARKTS_ExitContainerScope(ContainerScope scope)135 void ARKTS_ExitContainerScope(ContainerScope scope)
136 {
137 scope->Exit();
138 }
139 #else
ARKTS_GetCurrentContainerId()140 int32_t ARKTS_GetCurrentContainerId() { return 0; }
ARKTS_CreateContainerScope(int32_t id)141 ContainerScope ARKTS_CreateContainerScope(int32_t id) { return nullptr; }
ARKTS_DestroyContainerScope(ContainerScope scope)142 void ARKTS_DestroyContainerScope(ContainerScope scope) {}
ARKTS_EnterContainerScope(ContainerScope scope)143 void ARKTS_EnterContainerScope(ContainerScope scope) {}
ARKTS_ExitContainerScope(ContainerScope scope)144 void ARKTS_ExitContainerScope(ContainerScope scope) {}
145 #endif
146 }