1 /*
2 * Copyright (c) 2020-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 "perm_module.h"
17
18 #include "log.h"
19 #include "pms_interface.h"
20
21 namespace OHOS {
22 namespace ACELite {
CheckSelfPerm(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)23 JSIValue PermModule::CheckSelfPerm(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
24 {
25 JSIValue undefValue = JSI::CreateUndefined();
26 if ((args == nullptr) || argsNum == 0 || JSI::ValueIsUndefined(args[0])) {
27 return undefValue;
28 }
29
30 JSIValue success = JSI::GetNamedProperty(args[0], CB_SUCCESS);
31 JSIValue fail = JSI::GetNamedProperty(args[0], CB_FAIL);
32 JSIValue complete = JSI::GetNamedProperty(args[0], CB_COMPLETE);
33 char *permission = JSI::GetStringProperty(args[0], "permission");
34
35 int ret = CheckSelfPermission(permission);
36 JSIValue code = JSI::CreateNumber(ret);
37 JSIValue argv[ARGC_ONE] = { code };
38 if (ret != GRANTED || ret != NOT_GRANTED) {
39 HILOG_ERROR(HILOG_MODULE_ACE, "Check permission failed.");
40 JSI::CallFunction(fail, thisVal, argv, ARGC_ONE);
41 goto RELEASE;
42 }
43
44 JSI::CallFunction(success, thisVal, argv, ARGC_ONE);
45
46 RELEASE:
47 JSI::ReleaseString(permission);
48 JSI::CallFunction(complete, thisVal, nullptr, 0);
49 JSI::ReleaseValueList(success, fail, complete, code, ARGS_END);
50
51 return undefValue;
52 }
53 } // namespace ACELite
54 } // namespace OHOS
55