1 /*
2 * Copyright (c) 2023 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 <cstdint>
17 #include <js_native_api.h>
18 #include <js_native_api_types.h>
19 #include <string>
20 #include <vector>
21 #include "napi/native_api.h"
22 #include "rawfile/raw_file_manager.h"
23 #include "rawfile/raw_file.h"
24 #include "rawfile/raw_dir.h"
25 #include "hilog/log.h"
26
27 const int GLOBAL_RESMGR = 0xFF00;
28 const char *TAG = "[Sample_rawfile]";
GetFileList(napi_env env,napi_callback_info info)29 static napi_value GetFileList(napi_env env, napi_callback_info info)
30 {
31 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileList Begin");
32 size_t argc = 2;
33 napi_value argv[2] = {nullptr};
34 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
35
36 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
37 size_t strSize;
38 char strBuf[256];
39 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
40 std::string filename(strBuf, strSize);
41 RawDir *rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, filename.c_str());
42 int count = OH_ResourceManager_GetRawFileCount(rawDir);
43 std::vector<std::string> tempArray;
44 for (int i = 0; i < count; i++) {
45 std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
46 tempArray.emplace_back(filename);
47 }
48
49 napi_value fileList;
50 napi_create_array(env, &fileList);
51 for (size_t i = 0; i < tempArray.size(); i++) {
52 napi_value jsString;
53 napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
54 napi_set_element(env, fileList, i, jsString);
55 }
56 OH_ResourceManager_CloseRawDir(rawDir);
57 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
58 return fileList;
59 }
60
61 namespace {
CreateJsArrayValue(napi_env env,std::unique_ptr<uint8_t[]> & data,long length)62 napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
63 {
64 napi_value buffer;
65 napi_status status = napi_create_external_arraybuffer(
66 env, data.get(), length,
67 [](napi_env env, void *data, void *hint) {
68 delete[] static_cast<char *>(data);
69 },
70 nullptr, &buffer);
71 if (status != napi_ok) {
72 OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create external array buffer");
73 return nullptr;
74 }
75 napi_value result = nullptr;
76 status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
77 if (status != napi_ok) {
78 OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create media typed array");
79 return nullptr;
80 }
81 data.release();
82 return result;
83 }
84 }
85
86
GetRawFileContent(napi_env env,napi_callback_info info)87 static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
88 {
89 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileContent Begin");
90 size_t argc = 4;
91 napi_value argv[4] = {nullptr};
92 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
93
94 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
95 size_t strSize;
96 char strBuf[256];
97 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
98 std::string filename(strBuf, strSize);
99 int64_t offset = 0;
100 int64_t length = 0;
101 napi_get_value_int64(env, argv[2], &offset);
102 napi_get_value_int64(env, argv[3], &length);
103
104 RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
105 if (rawFile != nullptr) {
106 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success");
107 }
108 long fileSize = OH_ResourceManager_GetRawFileSize(rawFile);
109 long readLen = fileSize;
110 if (offset + length <= fileSize) {
111 OH_ResourceManager_SeekRawFile(rawFile, offset, 0);
112 if (length != 0) {
113 readLen = length;
114 }
115 long currentOffset = OH_ResourceManager_GetRawFileOffset(rawFile);
116 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileContent, current rawfile offset = %{public}ld",
117 currentOffset);
118 }
119 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(readLen);
120 int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), readLen);
121 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileContent, read size = %{public}d", res);
122 OH_ResourceManager_CloseRawFile(rawFile);
123 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
124 return CreateJsArrayValue(env, data, fileSize);
125 }
126
GetRawFileContent64(napi_env env,napi_callback_info info)127 static napi_value GetRawFileContent64(napi_env env, napi_callback_info info)
128 {
129 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileContent64 Begin");
130 size_t argc = 4;
131 napi_value argv[4] = {nullptr};
132 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
133
134 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
135 size_t strSize;
136 char strBuf[256];
137 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
138 std::string filename(strBuf, strSize);
139 int64_t offset = 0;
140 int64_t length = 0;
141 napi_get_value_int64(env, argv[2], &offset);
142 napi_get_value_int64(env, argv[3], &length);
143
144 RawFile64 *rawFile = OH_ResourceManager_OpenRawFile64(mNativeResMgr, filename.c_str());
145 if (rawFile != nullptr) {
146 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile64 success");
147 }
148 long fileSize = OH_ResourceManager_GetRawFileSize64(rawFile);
149 long readLen = fileSize;
150 if (offset + length <= fileSize) {
151 OH_ResourceManager_SeekRawFile64(rawFile, offset, 0);
152 if (length != 0) {
153 readLen = length;
154 }
155 long currentOffset = OH_ResourceManager_GetRawFileOffset64(rawFile);
156 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileContent64, current rawfile offset = %{public}ld",
157 currentOffset);
158 }
159 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(readLen);
160 int res = OH_ResourceManager_ReadRawFile64(rawFile, data.get(), readLen);
161 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileContent64, read size = %{public}d", res);
162 OH_ResourceManager_CloseRawFile64(rawFile);
163 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
164 return CreateJsArrayValue(env, data, fileSize);
165 }
166
167 namespace {
createJsFileDescriptor(napi_env env,int navtieFd,int64_t nativeOffset,int64_t nativeLength)168 napi_value createJsFileDescriptor(napi_env env, int navtieFd, int64_t nativeOffset, int64_t nativeLength)
169 {
170 napi_value result;
171 napi_status status = napi_create_object(env, &result);
172 if (status != napi_ok) {
173 return result;
174 }
175
176 napi_value fd;
177 status = napi_create_int32(env, navtieFd, &fd);
178 if (status != napi_ok) {
179 return result;
180 }
181 status = napi_set_named_property(env, result, "fd", fd);
182 if (status != napi_ok) {
183 return result;
184 }
185
186 napi_value offset;
187 status = napi_create_int64(env, nativeOffset, &offset);
188 if (status != napi_ok) {
189 return result;
190 }
191 status = napi_set_named_property(env, result, "offset", offset);
192 if (status != napi_ok) {
193 return result;
194 }
195
196 napi_value length;
197 status = napi_create_int64(env, nativeLength, &length);
198 if (status != napi_ok) {
199 return result;
200 }
201 status = napi_set_named_property(env, result, "length", length);
202 if (status != napi_ok) {
203 return result;
204 }
205 return result;
206 }
207 }
208
GetRawFileDescriptor(napi_env env,napi_callback_info info)209 static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
210 {
211 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileDescriptor Begin");
212 size_t argc = 2;
213 napi_value argv[2] = {nullptr};
214 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
215
216 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
217 size_t strSize;
218 char strBuf[256];
219 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
220 std::string filename(strBuf, strSize);
221 RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
222 if (rawFile != nullptr) {
223 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success");
224 }
225 RawFileDescriptor descriptor;
226 OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
227
228 OH_ResourceManager_CloseRawFile(rawFile);
229 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
230 return createJsFileDescriptor(env, descriptor.fd, descriptor.start, descriptor.length);
231 }
232
GetRawFileDescriptor64(napi_env env,napi_callback_info info)233 static napi_value GetRawFileDescriptor64(napi_env env, napi_callback_info info)
234 {
235 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileDescriptor64 Begin");
236 size_t argc = 2;
237 napi_value argv[2] = {nullptr};
238 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
239
240 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
241 size_t strSize;
242 char strBuf[256];
243 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
244 std::string filename(strBuf, strSize);
245 RawFile64 *rawFile = OH_ResourceManager_OpenRawFile64(mNativeResMgr, filename.c_str());
246 if (rawFile != nullptr) {
247 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile64 success");
248 }
249 RawFileDescriptor64 descriptor;
250 OH_ResourceManager_GetRawFileDescriptor64(rawFile, &descriptor);
251
252 OH_ResourceManager_CloseRawFile64(rawFile);
253 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
254 return createJsFileDescriptor(env, descriptor.fd, descriptor.start, descriptor.length);
255 }
256
257 namespace {
GetValueType(napi_env env,napi_value value)258 napi_valuetype GetValueType(napi_env env, napi_value value)
259 {
260 napi_valuetype valueType = napi_valuetype::napi_undefined;
261 napi_typeof(env, value, &valueType);
262 return valueType;
263 }
264
GetRawFileDescritorObject(napi_env env,napi_value value,int & nativeFd,int64_t & nativeOffset,int64_t & nativeLength)265 void GetRawFileDescritorObject(napi_env env, napi_value value, int &nativeFd, int64_t &nativeOffset,
266 int64_t &nativeLength)
267 {
268 napi_value fd;
269 napi_status status = napi_get_named_property(env, value, "fd", &fd);
270 if (status == napi_ok && fd != nullptr && GetValueType(env, fd) == napi_number) {
271 napi_get_value_int32(env, fd, &nativeFd);
272 }
273
274 napi_value offset;
275 status = napi_get_named_property(env, value, "offset", &offset);
276 if (status == napi_ok && offset != nullptr && GetValueType(env, offset) == napi_number) {
277 napi_get_value_int64(env, offset, &nativeOffset);
278 }
279
280 napi_value length;
281 status = napi_get_named_property(env, value, "length", &length);
282 if (status == napi_ok && length != nullptr && GetValueType(env, length) == napi_number) {
283 napi_get_value_int64(env, length, &nativeLength);
284 }
285 }
286 }
287
ReleaseRawFileDescriptor(napi_env env,napi_callback_info info)288 static napi_value ReleaseRawFileDescriptor(napi_env env, napi_callback_info info)
289 {
290 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "ReleaseRawFileDescriptor Begin");
291 size_t argc = 2;
292 napi_value argv[2] = {nullptr};
293 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
294
295 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
296 bool succeed = false;
297 if (GetValueType(env, argv[1]) == napi_object) {
298 RawFileDescriptor descriptor;
299 int64_t offset = 0;
300 int64_t length = 0;
301 GetRawFileDescritorObject(env, argv[1], descriptor.fd, offset, length);
302 descriptor.start = static_cast<long>(offset);
303 descriptor.length = static_cast<long>(length);
304 succeed = OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
305 }
306 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
307 napi_value result;
308 napi_get_boolean(env, succeed, &result);
309 return result;
310 }
311
ReleaseRawFileDescriptor64(napi_env env,napi_callback_info info)312 static napi_value ReleaseRawFileDescriptor64(napi_env env, napi_callback_info info)
313 {
314 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "ReleaseRawFileDescriptor64 Begin");
315 size_t argc = 2;
316 napi_value argv[2] = {nullptr};
317 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
318
319 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
320
321 bool succeed = false;
322 if (GetValueType(env, argv[1]) == napi_object) {
323 RawFileDescriptor64 descriptor;
324 GetRawFileDescritorObject(env, argv[1], descriptor.fd, descriptor.start, descriptor.length);
325 succeed = OH_ResourceManager_ReleaseRawFileDescriptor64(&descriptor);
326 }
327 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
328 napi_value result;
329 napi_get_boolean(env, succeed, &result);
330 return result;
331 }
332
333 EXTERN_C_START
Init(napi_env env,napi_value exports)334 static napi_value Init(napi_env env, napi_value exports)
335 {
336 napi_property_descriptor desc[] = {
337 {"getFileList", nullptr, GetFileList, nullptr, nullptr, nullptr, napi_default, nullptr},
338 {"getRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr},
339 {"getRawFileContent64", nullptr, GetRawFileContent64, nullptr, nullptr, nullptr, napi_default, nullptr},
340 {"getRawFileDescriptor", nullptr, GetRawFileDescriptor, nullptr, nullptr, nullptr, napi_default, nullptr},
341 {"getRawFileDescriptor64", nullptr, GetRawFileDescriptor64, nullptr, nullptr, nullptr, napi_default, nullptr},
342 {"releaseRawFileDescriptor", nullptr, ReleaseRawFileDescriptor, nullptr, nullptr, nullptr, napi_default,
343 nullptr},
344 {"releaseRawFileDescriptor64", nullptr, ReleaseRawFileDescriptor64, nullptr, nullptr, nullptr, napi_default,
345 nullptr},
346 };
347 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
348 return exports;
349 }
350 EXTERN_C_END
351
352 static napi_module demoModule = {
353 .nm_version = 1,
354 .nm_flags = 0,
355 .nm_filename = nullptr,
356 .nm_register_func = Init,
357 .nm_modname = "entry",
358 .nm_priv = ((void *)0),
359 .reserved = {0},
360 };
361
RegisterEntryModule(void)362 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
363 {
364 napi_module_register(&demoModule);
365 }
366