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     InitPolicyInfo(env, exports);
34     InitPolicyErrorCode(env, exports);
35     InitPolicyErrorResult(env, exports);
36     static napi_property_descriptor desc[] = {
37         DECLARE_NAPI_FUNCTION("grantUriPermission", GrantUriPermission::Async),
38         DECLARE_NAPI_FUNCTION("persistPermission", PersistPermission),
39         DECLARE_NAPI_FUNCTION("revokePermission", RevokePermission),
40         DECLARE_NAPI_FUNCTION("activatePermission", ActivatePermission),
41         DECLARE_NAPI_FUNCTION("deactivatePermission", DeactivatePermission),
42         DECLARE_NAPI_FUNCTION("checkPersistentPermission", CheckPersistentPermission),
43     };
44     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
45     return exports;
46 }
47 
InitPolicyConstructor(napi_env env,napi_callback_info info)48 static napi_value InitPolicyConstructor(napi_env env, napi_callback_info info)
49 {
50     size_t argc = 0;
51     napi_value args[1] = {0};
52     napi_value res = nullptr;
53     void *data = nullptr;
54     napi_status status = napi_get_cb_info(env, info, &argc, args, &res, &data);
55     if (status != napi_ok) {
56         HILOGE("InitPolicyConstructor, status is not napi_ok");
57         return nullptr;
58     }
59     return res;
60 }
61 
InitOperationMode(napi_env env,napi_value exports)62 void InitOperationMode(napi_env env, napi_value exports)
63 {
64     char propertyName[] = "OperationMode";
65     napi_property_descriptor desc[] = {
66         DECLARE_NAPI_STATIC_PROPERTY("READ_MODE",
67                                      NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::READ_MODE)).val_),
68         DECLARE_NAPI_STATIC_PROPERTY("WRITE_MODE",
69                                      NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::WRITE_MODE)).val_),
70     };
71     napi_value obj = nullptr;
72     napi_status status = napi_create_object(env, &obj);
73     if (status != napi_ok) {
74         HILOGE("Failed to create object at initializing OperationMode");
75         return;
76     }
77     status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc);
78     if (status != napi_ok) {
79         HILOGE("Failed to set properties of character at initializing OperationMode");
80         return;
81     }
82     status = napi_set_named_property(env, exports, propertyName, obj);
83     if (status != napi_ok) {
84         HILOGE("Failed to set direction property at initializing OperationMode");
85         return;
86     }
87 }
88 
InitPolicyErrorCode(napi_env env,napi_value exports)89 void InitPolicyErrorCode(napi_env env, napi_value exports)
90 {
91     char propertyName[] = "PolicyErrorCode";
92     napi_property_descriptor desc[] = {
93         DECLARE_NAPI_STATIC_PROPERTY(
94             "PERSISTENCE_FORBIDDEN",
95             NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::PERSISTENCE_FORBIDDEN)).val_),
96         DECLARE_NAPI_STATIC_PROPERTY("INVALID_MODE",
97                                      NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::INVALID_MODE)).val_),
98         DECLARE_NAPI_STATIC_PROPERTY("INVALID_PATH",
99                                      NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::INVALID_PATH)).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 InitPolicyErrorCode");
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 InitPolicyErrorCode");
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 InitPolicyErrorCode");
115         return;
116     }
117 }
118 
InitPolicyErrorResult(napi_env env,napi_value exports)119 void InitPolicyErrorResult(napi_env env, napi_value exports)
120 {
121     char className[] = "PolicyErrorResult";
122     napi_property_descriptor desc[] = {
123         DECLARE_NAPI_STATIC_PROPERTY("uri", NVal::CreateUTF8String(env, "uri").val_),
124         DECLARE_NAPI_STATIC_PROPERTY(
125             "code", NVal::CreateInt32(env, static_cast<int32_t>(PolicyErrorCode::PERSISTENCE_FORBIDDEN)).val_),
126         DECLARE_NAPI_STATIC_PROPERTY("message", NVal::CreateUTF8String(env, "message").val_),
127     };
128     napi_value obj = nullptr;
129     napi_status status = napi_define_class(env, className, NAPI_AUTO_LENGTH, InitPolicyConstructor, nullptr,
130                                            sizeof(desc) / sizeof(desc[0]), desc, &obj);
131     napi_create_object(env, &obj);
132     if (status != napi_ok) {
133         HILOGE("Failed to define class at initializing PolicyErrorResult");
134         return;
135     }
136     status = napi_set_named_property(env, exports, className, obj);
137     if (status != napi_ok) {
138         HILOGE("Failed to set direction property at initializing PolicyErrorResult");
139         return;
140     }
141 }
142 
InitPolicyInfo(napi_env env,napi_value exports)143 void InitPolicyInfo(napi_env env, napi_value exports)
144 {
145     char className[] = "PolicyInfo";
146     napi_property_descriptor desc[] = {
147         DECLARE_NAPI_STATIC_PROPERTY("uri", NVal::CreateUTF8String(env, "uri").val_),
148         DECLARE_NAPI_STATIC_PROPERTY("operationMode",
149                                      NVal::CreateUint32(env, static_cast<uint32_t>(OperationMode::READ_MODE)).val_),
150     };
151     napi_value obj = nullptr;
152     napi_status status = napi_define_class(env, className, NAPI_AUTO_LENGTH, InitPolicyConstructor, nullptr,
153                                            sizeof(desc) / sizeof(desc[0]), desc, &obj);
154     napi_create_object(env, &obj);
155     if (status != napi_ok) {
156         HILOGE("Failed to define class at initializing PolicyFlag");
157         return;
158     }
159     status = napi_set_named_property(env, exports, className, obj);
160     if (status != napi_ok) {
161         HILOGE("Failed to set direction property at initializing PolicyFlag");
162         return;
163     }
164 }
165 
166 NAPI_MODULE(fileshare, FileShareExport)
167 } // namespace ModuleFileShare
168 } // namespace AppFileService
169 } // namespace OHOS
170