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 #include "systemcapability.h"
16 #include "syscap_ts.h"
17 #include "beget_ext.h"
18 
19 #ifdef NAPI_ENABLE
20 
21 static constexpr int SYSCAP_MAX_SIZE = 100;
22 
BindNativeFunction(napi_env env,napi_value object,const char * name,const char * moduleName,napi_callback func)23 void BindNativeFunction(napi_env env, napi_value object, const char *name,
24                         const char *moduleName, napi_callback func)
25 {
26     std::string fullName(moduleName);
27     fullName += ".";
28     fullName += name;
29     napi_value result = nullptr;
30     napi_create_function(env, fullName.c_str(), fullName.length(), func, nullptr, &result);
31     napi_set_named_property(env, object, name, result);
32 }
33 
CreateJsUndefined(napi_env env)34 inline napi_value CreateJsUndefined(napi_env env)
35 {
36     napi_value result = nullptr;
37     napi_get_undefined(env, &result);
38     return result;
39 }
40 
CreateJsValue(napi_env env,const bool & value)41 inline napi_value CreateJsValue(napi_env env, const bool &value)
42 {
43     napi_value result;
44     napi_get_boolean(env, value, &result);
45     return result;
46 }
47 
48 
CanIUse(napi_env env,napi_callback_info info)49 napi_value CanIUse(napi_env env, napi_callback_info info)
50 {
51     if (env == nullptr || info == nullptr) {
52         BEGET_LOGE("get syscap failed since env or callback info is nullptr.");
53         return nullptr;
54     }
55     napi_value undefined = CreateJsUndefined(env);
56 
57     size_t argc = 1;
58     napi_value argv[1] = {nullptr};
59     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
60     if (argc != 1) {
61         BEGET_LOGE("get syscap failed with invalid parameter");
62         return undefined;
63     }
64 
65     napi_valuetype valueType = napi_undefined;
66     napi_typeof(env, argv[0], &valueType);
67     if (valueType != napi_string) {
68         BEGET_LOGE("%{public}s called, Params is invalid.", __func__);
69         return undefined;
70     }
71 
72     char syscap[SYSCAP_MAX_SIZE] = {0};
73 
74     size_t strLen = 0;
75     napi_get_value_string_utf8(env, argv[0], syscap, sizeof(syscap), &strLen);
76 
77     bool ret = HasSystemCapability(syscap);
78     return CreateJsValue(env, ret);
79 }
80 
81 #endif
82 
InitSyscapModule(napi_env env)83 void InitSyscapModule(napi_env env)
84 {
85     #ifdef NAPI_ENABLE
86     napi_value globalObject = nullptr;
87     napi_get_global(env, &globalObject);
88     const char *moduleName = "JsRuntime";
89     BindNativeFunction(env, globalObject, "canIUse", moduleName, CanIUse);
90     #endif
91 }