• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         DECLARE_NAPI_FUNCTION("setPermissionUsedRecordToggleStatus", SetPermissionUsedRecordToggleStatus),
38         DECLARE_NAPI_FUNCTION("getPermissionUsedRecordToggleStatus", GetPermissionUsedRecordToggleStatus)
39     };
40 
41     napi_define_properties(env, exports, sizeof(descriptor) / sizeof(descriptor[0]), descriptor);
42 
43     CreateObjects(env, exports);
44 
45     return exports;
46 }
47 
CreateObjects(const napi_env & env,const napi_value & exports)48 static void CreateObjects(const napi_env& env, const napi_value& exports)
49 {
50     napi_value permissionUsageFlag = nullptr;
51     napi_create_object(env, &permissionUsageFlag);
52 
53     napi_value prop = nullptr;
54     napi_create_int32(env, FLAG_PERMISSION_USAGE_SUMMARY, &prop);
55     napi_set_named_property(env, permissionUsageFlag, "FLAG_PERMISSION_USAGE_SUMMARY", prop);
56 
57     prop = nullptr;
58     napi_create_int32(env, FLAG_PERMISSION_USAGE_DETAIL, &prop);
59     napi_set_named_property(env, permissionUsageFlag, "FLAG_PERMISSION_USAGE_DETAIL", prop);
60 
61     napi_value permActiveStatus = nullptr;
62     napi_create_object(env, &permActiveStatus); // create enmu permActiveStatus
63 
64     prop = nullptr;
65     napi_create_int32(env, PERM_INACTIVE, &prop);
66     napi_set_named_property(env, permActiveStatus, "PERM_INACTIVE", prop);
67 
68     prop = nullptr;
69     napi_create_int32(env, PERM_ACTIVE_IN_FOREGROUND, &prop);
70     napi_set_named_property(env, permActiveStatus, "PERM_ACTIVE_IN_FOREGROUND", prop);
71 
72     prop = nullptr;
73     napi_create_int32(env, PERM_ACTIVE_IN_BACKGROUND, &prop);
74     napi_set_named_property(env, permActiveStatus, "PERM_ACTIVE_IN_BACKGROUND", prop);
75 
76     napi_value permissionUsedType = nullptr;
77     napi_create_object(env, &permissionUsedType); // create enmu PermissionUsedType
78 
79     prop = nullptr;
80     napi_create_int32(env, NORMAL_TYPE, &prop);
81     napi_set_named_property(env, permissionUsedType, "NORMAL_TYPE", prop);
82 
83     prop = nullptr;
84     napi_create_int32(env, PICKER_TYPE, &prop);
85     napi_set_named_property(env, permissionUsedType, "PICKER_TYPE", prop);
86 
87     prop = nullptr;
88     napi_create_int32(env, SECURITY_COMPONENT_TYPE, &prop);
89     napi_set_named_property(env, permissionUsedType, "SECURITY_COMPONENT_TYPE", prop);
90 
91     napi_property_descriptor exportFuncs[] = {
92         DECLARE_NAPI_PROPERTY("PermissionUsageFlag", permissionUsageFlag),
93         DECLARE_NAPI_PROPERTY("PermissionActiveStatus", permActiveStatus),
94         DECLARE_NAPI_PROPERTY("PermissionUsedType", permissionUsedType)
95     };
96     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(exportFuncs[0]), exportFuncs);
97 }
98 EXTERN_C_END
99 
100 /*
101  * Module define
102  */
103 static napi_module g_module = {
104     .nm_version = 1,
105     .nm_flags = 0,
106     .nm_filename = nullptr,
107     .nm_register_func = Init,
108     .nm_modname = "privacyManager",
109     .nm_priv = ((void*)0),
110     .reserved = {0}
111 };
112 
113 /*
114  * Module register function
115  */
RegisterPrivacyModule(void)116 extern "C" __attribute__((constructor)) void RegisterPrivacyModule(void)
117 {
118     napi_module_register(&g_module);
119 }
120 }  // namespace AccessToken
121 }  // namespace Security
122 }  // namespace OHOS