1 /*
2  * Copyright (c) 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 "native_module.h"
16 #include "permission_record_manager_napi.h"
17 #include "permission_used_request.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
22 EXTERN_C_START
23 /*
24  * function for module exports
25  */
Init(napi_env env,napi_value exports)26 static napi_value Init(napi_env env, napi_value exports)
27 {
28     napi_property_descriptor descriptor[] = {
29         DECLARE_NAPI_FUNCTION("addPermissionUsedRecord", AddPermissionUsedRecord),
30         DECLARE_NAPI_FUNCTION("startUsingPermission", StartUsingPermission),
31         DECLARE_NAPI_FUNCTION("stopUsingPermission", StopUsingPermission),
32         DECLARE_NAPI_FUNCTION("getPermissionUsedRecord", GetPermissionUsedRecords),
33         DECLARE_NAPI_FUNCTION("getPermissionUsedRecords", GetPermissionUsedRecords),
34         DECLARE_NAPI_FUNCTION("on", RegisterPermActiveChangeCallback),
35         DECLARE_NAPI_FUNCTION("off", UnregisterPermActiveChangeCallback),
36         DECLARE_NAPI_FUNCTION("getPermissionUsedTypeInfos", GetPermissionUsedTypeInfos)
37     };
38 
39     napi_define_properties(env, exports, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
40 
41     napi_value permissionUsageFlag = nullptr;
42     napi_create_object(env, &permissionUsageFlag);
43 
44     napi_value prop = nullptr;
45     napi_create_int32(env, FLAG_PERMISSION_USAGE_SUMMARY, &prop);
46     napi_set_named_property(env, permissionUsageFlag, "FLAG_PERMISSION_USAGE_SUMMARY", prop);
47 
48     prop = nullptr;
49     napi_create_int32(env, FLAG_PERMISSION_USAGE_DETAIL, &prop);
50     napi_set_named_property(env, permissionUsageFlag, "FLAG_PERMISSION_USAGE_DETAIL", prop);
51 
52     napi_value permActiveStatus = nullptr;
53     napi_create_object(env, &permActiveStatus); // create enmu permActiveStatus
54 
55     prop = nullptr;
56     napi_create_int32(env, PERM_INACTIVE, &prop);
57     napi_set_named_property(env, permActiveStatus, "PERM_INACTIVE", prop);
58 
59     prop = nullptr;
60     napi_create_int32(env, PERM_ACTIVE_IN_FOREGROUND, &prop);
61     napi_set_named_property(env, permActiveStatus, "PERM_ACTIVE_IN_FOREGROUND", prop);
62 
63     prop = nullptr;
64     napi_create_int32(env, PERM_ACTIVE_IN_BACKGROUND, &prop);
65     napi_set_named_property(env, permActiveStatus, "PERM_ACTIVE_IN_BACKGROUND", prop);
66 
67     napi_value permissionUsedType = nullptr;
68     napi_create_object(env, &permissionUsedType); // create enmu PermissionUsedType
69 
70     prop = nullptr;
71     napi_create_int32(env, NORMAL_TYPE, &prop);
72     napi_set_named_property(env, permissionUsedType, "NORMAL_TYPE", prop);
73 
74     prop = nullptr;
75     napi_create_int32(env, PICKER_TYPE, &prop);
76     napi_set_named_property(env, permissionUsedType, "PICKER_TYPE", prop);
77 
78     prop = nullptr;
79     napi_create_int32(env, SECURITY_COMPONENT_TYPE, &prop);
80     napi_set_named_property(env, permissionUsedType, "SECURITY_COMPONENT_TYPE", prop);
81 
82     napi_property_descriptor exportFuncs[] = {
83         DECLARE_NAPI_PROPERTY("PermissionUsageFlag", permissionUsageFlag),
84         DECLARE_NAPI_PROPERTY("PermissionActiveStatus", permActiveStatus),
85         DECLARE_NAPI_PROPERTY("PermissionUsedType", permissionUsedType)
86     };
87     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(exportFuncs[0]), exportFuncs);
88 
89     return exports;
90 }
91 EXTERN_C_END
92 
93 /*
94  * Module define
95  */
96 static napi_module g_module = {
97     .nm_version = 1,
98     .nm_flags = 0,
99     .nm_filename = nullptr,
100     .nm_register_func = Init,
101     .nm_modname = "privacyManager",
102     .nm_priv = ((void *)0),
103     .reserved = {0}
104 };
105 
106 /*
107  * Module register function
108  */
RegisterPrivacyModule(void)109 extern "C" __attribute__((constructor)) void RegisterPrivacyModule(void)
110 {
111     napi_module_register(&g_module);
112 }
113 }  // namespace AccessToken
114 }  // namespace Security
115 }  // namespace OHOS