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 "fileshare_n_exporter.h"
16 #include "file_permission.h"
17 #include "grant_permissions.h"
18 #include "grant_uri_permission.h"
19 #include "log.h"
20
21 namespace OHOS {
22 namespace AppFileService {
23 namespace ModuleFileShare {
24 using namespace FileManagement;
25 using namespace FileManagement::LibN;
26
27 /***********************************************
28 * Module export and register
29 ***********************************************/
FileShareExport(napi_env env,napi_value exports)30 napi_value FileShareExport(napi_env env, napi_value exports)
31 {
32 InitOperationMode(env, exports);
33 InitPolicyType(env, exports);
34 InitPolicyInfo(env, exports);
35 InitPathPolicyInfo(env, exports);
36 InitPolicyErrorCode(env, exports);
37 InitPolicyErrorResult(env, exports);
38 static napi_property_descriptor desc[] = {
39 DECLARE_NAPI_FUNCTION("grantUriPermission", GrantUriPermission::Async),
40 DECLARE_NAPI_FUNCTION("persistPermission", PersistPermission),
41 DECLARE_NAPI_FUNCTION("revokePermission", RevokePermission),
42 DECLARE_NAPI_FUNCTION("activatePermission", ActivatePermission),
43 DECLARE_NAPI_FUNCTION("deactivatePermission", DeactivatePermission),
44 DECLARE_NAPI_FUNCTION("checkPersistentPermission", CheckPersistentPermission),
45 DECLARE_NAPI_FUNCTION("checkPathPermission", CheckPathPermission),
46 };
47 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
48 return exports;
49 }
50
InitPolicyConstructor(napi_env env,napi_callback_info info)51 static napi_value InitPolicyConstructor(napi_env env, napi_callback_info info)
52 {
53 size_t argc = 0;
54 napi_value args[1] = {0};
55 napi_value res = nullptr;
56 void *data = nullptr;
57 napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
58 if (status != napi_ok) {
59 HILOGE("InitPolicyConstructor, status is not napi_ok");
60 return nullptr;
61 }
62 return res;
63 }
64
InitOperationMode(napi_env env,napi_value exports)65 void InitOperationMode(napi_env env, napi_value exports)
66 {
67 char propertyName[] = "OperationMode";
68 napi_property_descriptor desc[] = {
69 DECLARE_NAPI_STATIC_PROPERTY("READ_MODE",
70 NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::READ_MODE)).val_),
71 DECLARE_NAPI_STATIC_PROPERTY("WRITE_MODE",
72 NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::WRITE_MODE)).val_),
73 };
74 napi_value obj = nullptr;
75 napi_status status = napi_create_object(env, &obj);
76 if (status != napi_ok) {
77 HILOGE("Failed to create object at initializing OperationMode");
78 return;
79 }
80 status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
81 if (status != napi_ok) {
82 HILOGE("Failed to set properties of character at initializing OperationMode");
83 return;
84 }
85 status = napi_set_named_property(env, exports, propertyName, obj);
86 if (status != napi_ok) {
87 HILOGE("Failed to set direction property at initializing OperationMode");
88 return;
89 }
90 }
91
InitPolicyType(napi_env env,napi_value exports)92 void InitPolicyType(napi_env env, napi_value exports)
93 {
94 char propertyName[] = "PolicyType";
95 napi_property_descriptor desc[] = {
96 DECLARE_NAPI_STATIC_PROPERTY("TEMPORARY_TYPE",
97 NVal::CreateUint32(env, static_cast<uint32_t>(TEMPORARY_TYPE)).val_),
98 DECLARE_NAPI_STATIC_PROPERTY("PERSISTENT_TYPE",
99 NVal::CreateUint32(env, static_cast<uint32_t>(PERSISTENT_TYPE)).val_),
100 };
101 napi_value obj = nullptr;
102 napi_status status = napi_create_object(env, &obj);
103 if (status != napi_ok) {
104 HILOGE("Failed to create object at initializing PolicyType");
105 return;
106 }
107 status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
108 if (status != napi_ok) {
109 HILOGE("Failed to set properties of character at initializing PolicyType");
110 return;
111 }
112 status = napi_set_named_property(env, exports, propertyName, obj);
113 if (status != napi_ok) {
114 HILOGE("Failed to set direction property at initializing PolicyType");
115 return;
116 }
117 }
118
119
InitPolicyErrorCode(napi_env env,napi_value exports)120 void InitPolicyErrorCode(napi_env env, napi_value exports)
121 {
122 char propertyName[] = "PolicyErrorCode";
123 napi_property_descriptor desc[] = {
124 DECLARE_NAPI_STATIC_PROPERTY(
125 "PERSISTENCE_FORBIDDEN",
126 NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::PERSISTENCE_FORBIDDEN)).val_),
127 DECLARE_NAPI_STATIC_PROPERTY("INVALID_MODE",
128 NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::INVALID_MODE)).val_),
129 DECLARE_NAPI_STATIC_PROPERTY("INVALID_PATH",
130 NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::INVALID_PATH)).val_),
131 };
132 napi_value obj = nullptr;
133 napi_status status = napi_create_object(env, &obj);
134 if (status != napi_ok) {
135 HILOGE("Failed to create object at initializing InitPolicyErrorCode");
136 return;
137 }
138 status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
139 if (status != napi_ok) {
140 HILOGE("Failed to set properties of character at initializing InitPolicyErrorCode");
141 return;
142 }
143 status = napi_set_named_property(env, exports, propertyName, obj);
144 if (status != napi_ok) {
145 HILOGE("Failed to set direction property at initializing InitPolicyErrorCode");
146 return;
147 }
148 }
149
InitPolicyErrorResult(napi_env env,napi_value exports)150 void InitPolicyErrorResult(napi_env env, napi_value exports)
151 {
152 char className[] = "PolicyErrorResult";
153 napi_property_descriptor desc[] = {
154 DECLARE_NAPI_STATIC_PROPERTY("uri", NVal::CreateUTF8String(env, "uri").val_),
155 DECLARE_NAPI_STATIC_PROPERTY(
156 "code", NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::PERSISTENCE_FORBIDDEN)).val_),
157 DECLARE_NAPI_STATIC_PROPERTY("message", NVal::CreateUTF8String(env, "message").val_),
158 };
159 napi_value obj = nullptr;
160 napi_status status = napi_define_class(env, className, NAPI_AUTO_LENGTH, InitPolicyConstructor, nullptr,
161 sizeof(desc) / sizeof(desc[0]), desc, &obj);
162 napi_create_object(env, &obj);
163 if (status != napi_ok) {
164 HILOGE("Failed to define class at initializing PolicyErrorResult");
165 return;
166 }
167 status = napi_set_named_property(env, exports, className, obj);
168 if (status != napi_ok) {
169 HILOGE("Failed to set direction property at initializing PolicyErrorResult");
170 return;
171 }
172 }
173
InitPolicyInfo(napi_env env,napi_value exports)174 void InitPolicyInfo(napi_env env, napi_value exports)
175 {
176 char className[] = "PolicyInfo";
177 napi_property_descriptor desc[] = {
178 DECLARE_NAPI_STATIC_PROPERTY("uri", NVal::CreateUTF8String(env, "uri").val_),
179 DECLARE_NAPI_STATIC_PROPERTY("operationMode",
180 NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::READ_MODE)).val_),
181 };
182 napi_value obj = nullptr;
183 napi_status status = napi_define_class(env, className, NAPI_AUTO_LENGTH, InitPolicyConstructor, nullptr,
184 sizeof(desc) / sizeof(desc[0]), desc, &obj);
185 napi_create_object(env, &obj);
186 if (status != napi_ok) {
187 HILOGE("Failed to define class at initializing PolicyFlag");
188 return;
189 }
190 status = napi_set_named_property(env, exports, className, obj);
191 if (status != napi_ok) {
192 HILOGE("Failed to set direction property at initializing PolicyFlag");
193 return;
194 }
195 }
196
InitPathPolicyInfo(napi_env env,napi_value exports)197 void InitPathPolicyInfo(napi_env env, napi_value exports)
198 {
199 char className[] = "PathPolicyInfo";
200 napi_property_descriptor desc[] = {
201 DECLARE_NAPI_STATIC_PROPERTY("path", NVal::CreateUTF8String(env, "path").val_),
202 DECLARE_NAPI_STATIC_PROPERTY("operationMode",
203 NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::READ_MODE)).val_),
204 };
205 napi_value obj = nullptr;
206 napi_status status = napi_define_class(env, className, NAPI_AUTO_LENGTH, InitPolicyConstructor, nullptr,
207 sizeof(desc) / sizeof(desc[0]), desc, &obj);
208 napi_create_object(env, &obj);
209 if (status != napi_ok) {
210 HILOGE("Failed to define class at initializing PathPolicyInfo");
211 return;
212 }
213 status = napi_set_named_property(env, exports, className, obj);
214 if (status != napi_ok) {
215 HILOGE("Failed to set direction property at initializing PathPolicyInfo");
216 return;
217 }
218 }
219
220 NAPI_MODULE(fileshare, FileShareExport)
221 } // namespace ModuleFileShare
222 } // namespace AppFileService
223 } // namespace OHOS
224