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