1 /*
2 * Copyright (c) 2022-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 #include "acelite_config.h"
16 #if (FEATURE_SYSCAP_MODULE == 1)
17 #include <string.h>
18 #include "syscap_module.h"
19 #include "ace_mem_base.h"
20 #include "systemcapability.h"
21
22 namespace OHOS {
23 namespace ACELite {
24 const uint16_t SYSCAP_BUFFER_SIZE = 128;
Init()25 void SyscapModule::Init()
26 {
27 const char * const canIUse = "canIUse";
28 CreateNamedFunction(canIUse, CanIUse);
29 }
30
CheckSyscap(const jerry_value_t func,const jerry_value_t context,const jerry_value_t * args,const jerry_length_t argsNum,bool repeated)31 jerry_value_t SyscapModule::CheckSyscap(const jerry_value_t func,
32 const jerry_value_t context,
33 const jerry_value_t *args,
34 const jerry_length_t argsNum,
35 bool repeated)
36 {
37 (void)func; /* unused */
38 (void)context; /* unused */
39 (void)repeated; /* unused */
40
41 const uint8_t leastArguments = 1;
42 if ((argsNum != leastArguments)) {
43 return UNDEFINED;
44 }
45
46 jerry_value_t strVal;
47 if (jerry_value_is_symbol(args[0])) {
48 strVal = jerry_get_symbol_descriptive_string(args[0]);
49 } else {
50 strVal = jerry_value_to_string(args[0]);
51 }
52
53 if (jerry_value_is_error(strVal)) {
54 jerry_release_value(strVal);
55 return jerry_create_boolean(false);
56 }
57
58 jerry_length_t length = jerry_get_utf8_string_length(strVal);
59 if (length >= SYSCAP_BUFFER_SIZE) {
60 jerry_release_value(strVal);
61 return jerry_create_boolean(false);
62 }
63
64 jerry_char_t buffer[SYSCAP_BUFFER_SIZE] = {0};
65 jerry_size_t syscapStrSize = jerry_string_to_utf8_char_buffer(strVal, buffer, length);
66 if (syscapStrSize == 0) {
67 jerry_release_value(strVal);
68 return jerry_create_boolean(false);
69 }
70 const char *syscapString = (char *)buffer;
71 bool ret = HasSystemCapability(syscapString);
72
73 jerry_release_value(strVal);
74 return jerry_create_boolean(ret);
75 }
76 } // namespace ACELite
77 } // namespace OHOS
78 #endif // FEATURE_SYSCAP_MODULE
79