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 <js_native_api.h>
17 #include <js_native_api_types.h>
18 #include <vector>
19 #include "napi/native_api.h"
20 #include "rawfile/raw_file_manager.h"
21 #include "rawfile/raw_file.h"
22 #include "rawfile/raw_dir.h"
23 #include "hilog/log.h"
24
25 const int GLOBAL_RESMGR = 0xFF00;
26 const char *TAG = "[Sample_rawfile]";
GetFileList(napi_env env,napi_callback_info info)27 static napi_value GetFileList(napi_env env, napi_callback_info info)
28 {
29 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileList Begin");
30 size_t argc = 2;
31 napi_value argv[2] = {nullptr};
32
33 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
34
35 napi_valuetype valueType;
36 napi_typeof(env, argv[0], &valueType);
37 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
38 size_t strSize;
39 char strBuf[256];
40 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
41 std::string filename(strBuf, strSize);
42 RawDir *rawDir = OH_ResourceManager_OpenRawDir(mNativeResMgr, filename.c_str());
43 int count = OH_ResourceManager_GetRawFileCount(rawDir);
44 std::vector<std::string> tempArray;
45 for (int i = 0; i < count; i++) {
46 std::string filename = OH_ResourceManager_GetRawFileName(rawDir, i);
47 tempArray.emplace_back(filename);
48 }
49
50 napi_value fileList;
51 napi_create_array(env, &fileList);
52 for (size_t i = 0; i < tempArray.size(); i++) {
53 napi_value jsString;
54 napi_create_string_utf8(env, tempArray[i].c_str(), NAPI_AUTO_LENGTH, &jsString);
55 napi_set_element(env, fileList, i, jsString);
56 }
57 OH_ResourceManager_CloseRawDir(rawDir);
58 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
59 return fileList;
60 }
61
62 namespace {
CreateJsArrayValue(napi_env env,std::unique_ptr<uint8_t[]> & data,long length)63 napi_value CreateJsArrayValue(napi_env env, std::unique_ptr<uint8_t[]> &data, long length)
64 {
65 napi_value buffer;
66 napi_status status = napi_create_external_arraybuffer(
67 env, data.get(), length,
68 [](napi_env env, void *data, void *hint) {
69 delete[] static_cast<char *>(data);
70 },
71 nullptr, &buffer);
72 if (status != napi_ok) {
73 OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create external array buffer");
74 return nullptr;
75 }
76 napi_value result = nullptr;
77 status = napi_create_typedarray(env, napi_uint8_array, length, buffer, 0, &result);
78 if (status != napi_ok) {
79 OH_LOG_Print(LOG_APP, LOG_ERROR, GLOBAL_RESMGR, TAG, "Failed to create media typed array");
80 return nullptr;
81 }
82 data.release();
83 return result;
84 }
85 }
86
87
GetRawFileContent(napi_env env,napi_callback_info info)88 static napi_value GetRawFileContent(napi_env env, napi_callback_info info)
89 {
90 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetFileContent Begin");
91 size_t argc = 2;
92 napi_value argv[2] = {nullptr};
93
94 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
95
96 napi_valuetype valueType;
97 napi_typeof(env, argv[0], &valueType);
98 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
99 size_t strSize;
100 char strBuf[256];
101 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
102 std::string filename(strBuf, strSize);
103 RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
104 if (rawFile != nullptr) {
105 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success");
106 }
107 long len = OH_ResourceManager_GetRawFileSize(rawFile);
108 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(len);
109
110 int res = OH_ResourceManager_ReadRawFile(rawFile, data.get(), len);
111
112 OH_ResourceManager_CloseRawFile(rawFile);
113 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
114 return CreateJsArrayValue(env, data, len);
115 }
116
117 namespace {
createJsFileDescriptor(napi_env env,RawFileDescriptor & descriptor)118 napi_value createJsFileDescriptor(napi_env env, RawFileDescriptor &descriptor)
119 {
120 napi_value result;
121 napi_status status = napi_create_object(env, &result);
122 if (status != napi_ok) {
123 return result;
124 }
125
126 napi_value fd;
127 status = napi_create_int32(env, descriptor.fd, &fd);
128 if (status != napi_ok) {
129 return result;
130 }
131 status = napi_set_named_property(env, result, "fd", fd);
132 if (status != napi_ok) {
133 return result;
134 }
135
136 napi_value offset;
137 status = napi_create_int64(env, descriptor.start, &offset);
138 if (status != napi_ok) {
139 return result;
140 }
141 status = napi_set_named_property(env, result, "offset", offset);
142 if (status != napi_ok) {
143 return result;
144 }
145
146 napi_value length;
147 status = napi_create_int64(env, descriptor.length, &length);
148 if (status != napi_ok) {
149 return result;
150 }
151 status = napi_set_named_property(env, result, "length", length);
152 if (status != napi_ok) {
153 return result;
154 }
155 return result;
156 }
157 }
158
GetRawFileDescriptor(napi_env env,napi_callback_info info)159 static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info)
160 {
161 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "GetRawFileDescriptor Begin");
162 size_t argc = 2;
163 napi_value argv[2] = {nullptr};
164
165 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
166
167 napi_valuetype valueType;
168 napi_typeof(env, argv[0], &valueType);
169 NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, argv[0]);
170 size_t strSize;
171 char strBuf[256];
172 napi_get_value_string_utf8(env, argv[1], strBuf, sizeof(strBuf), &strSize);
173 std::string filename(strBuf, strSize);
174 RawFile *rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, filename.c_str());
175 if (rawFile != nullptr) {
176 OH_LOG_Print(LOG_APP, LOG_INFO, GLOBAL_RESMGR, TAG, "OH_ResourceManager_OpenRawFile success");
177 }
178 RawFileDescriptor descriptor;
179 OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
180
181 OH_ResourceManager_CloseRawFile(rawFile);
182 OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr);
183 return createJsFileDescriptor(env, descriptor);
184 }
185
186 EXTERN_C_START
Init(napi_env env,napi_value exports)187 static napi_value Init(napi_env env, napi_value exports)
188 {
189 napi_property_descriptor desc[] = {
190 {"getFileList", nullptr, GetFileList, nullptr, nullptr, nullptr, napi_default, nullptr},
191 {"getRawFileContent", nullptr, GetRawFileContent, nullptr, nullptr, nullptr, napi_default, nullptr},
192 {"getRawFileDescriptor", nullptr, GetRawFileDescriptor, nullptr, nullptr, nullptr, napi_default, nullptr}};
193 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
194 return exports;
195 }
196 EXTERN_C_END
197
198 static napi_module demoModule = {
199 .nm_version = 1,
200 .nm_flags = 0,
201 .nm_filename = nullptr,
202 .nm_register_func = Init,
203 .nm_modname = "entry",
204 .nm_priv = ((void *)0),
205 .reserved = {0},
206 };
207
RegisterEntryModule(void)208 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
209 {
210 napi_module_register(&demoModule);
211 }
212