1 /*
2 * Copyright (c) 2021 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 "napi_base_context.h"
17
18 #define OHOS_CALL_NAPI_RETURN(call) \
19 do { \
20 napi_status ret = (call); \
21 if (ret != napi_ok) { \
22 return ret; \
23 } \
24 } while (0)
25
26 namespace OHOS {
27 namespace AbilityRuntime {
GetFAModeContextClassObject()28 napi_value* GetFAModeContextClassObject()
29 {
30 static thread_local napi_value contextClassObject = {0};
31 return &contextClassObject;
32 }
33
IsStageContext(napi_env env,napi_value object,bool & stageMode)34 napi_status IsStageContext(napi_env env, napi_value object, bool& stageMode)
35 {
36 napi_value boolValue;
37 OHOS_CALL_NAPI_RETURN(napi_get_named_property(env, object, "stageMode", &boolValue));
38
39 bool value = false;
40 OHOS_CALL_NAPI_RETURN(napi_get_value_bool(env, boolValue, &value));
41
42 stageMode = value;
43 return napi_ok;
44 }
45
GetStageModeContext(napi_env env,napi_value object)46 std::shared_ptr<Context> GetStageModeContext(napi_env env, napi_value object)
47 {
48 void* wrapped = nullptr;
49 napi_status ret = napi_unwrap(env, object, &wrapped);
50 if (ret != napi_ok) {
51 return nullptr;
52 }
53
54 auto weakContext = static_cast<std::weak_ptr<Context>*>(wrapped);
55 return weakContext != nullptr ? weakContext->lock() : nullptr;
56 }
57
GetCurrentAbility(napi_env env)58 AppExecFwk::Ability* GetCurrentAbility(napi_env env)
59 {
60 napi_value global;
61 napi_status status = napi_get_global(env, &global);
62 if (status != napi_ok) {
63 return nullptr;
64 }
65
66 napi_value abilityObj;
67 status = napi_get_named_property(env, global, "ability", &abilityObj);
68 if (status != napi_ok || abilityObj == nullptr) {
69 return nullptr;
70 }
71
72 void* pointer = nullptr;
73 status = napi_get_value_external(env, abilityObj, &pointer);
74 if (status != napi_ok) {
75 return nullptr;
76 }
77
78 return static_cast<AppExecFwk::Ability*>(pointer);
79 }
80 } // namespace AbilityRuntime
81 } // namespace OHOS