• 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 "native_common.h"
17 #include "rawfile/raw_file_manager.h"
18 #include <fstream>
19 #include <memory>
20 #include <vector>
21 
22 #include "hilog/log.h"
23 #include "node_api.h"
24 
25 namespace OHOS {
26     namespace Global {
27         namespace Resource {
28 #define GET_PARAMS(env, info, num)    \
29     size_t argc = num;                 \
30     napi_value argv[num] = {nullptr};  \
31     napi_value thisVar = nullptr;      \
32     void *data = nullptr;              \
33     napi_get_cb_info(env, info , &argc, argv, &thisVar, &data)
34 
SetErrorMsg(const std::string & msg)35             void NdkAsyncContext::SetErrorMsg(const std::string &msg)
36             {
37                 errMsg_ = msg;
38                 success_ = false;
39             }
40 
TestRawFileExecute()41             napi_async_execute_callback TestRawFileExecute()
42             {
43                 return [](napi_env env, void* data) {
44                     NdkAsyncContext *asyncContext = static_cast<NdkAsyncContext*>(data);
45 
46                     // test OH_ResourceManager_OpenRawDir
47                     RawDir* rawDir = OH_ResourceManager_OpenRawDir(asyncContext->ndk_, asyncContext->path_.c_str());
48 
49                     // test OH_ResourceManager_GetRawFileCount
50                     int count = OH_ResourceManager_GetRawFileCount(rawDir);
51 
52                     // test OH_ResourceManager_GetRawFileName
53                     for (int i = 0; i< count; i++) {
54                         std::string tempFileName = OH_ResourceManager_GetRawFileName(rawDir, i);
55                     }
56 
57                     // test OH_ResourceManager_OpenRawFile
58                     std::string fileName = OH_ResourceManager_GetRawFileName(rawDir, 0);
59                     asyncContext->path_ = fileName;
60                     RawFile* rawFile = OH_ResourceManager_OpenRawFile(asyncContext->ndk_, fileName.c_str());
61 
62                     // test OH_ResourceManager_GetRawFileSize
63                     asyncContext->len_ = OH_ResourceManager_GetRawFileSize(rawFile);
64 
65                     // // test OH_ResourceManager_SeekRawFile
66 
67                     // // test OH_ResourceManager_GetRawFileOffset
68 
69                     // test OH_ResourceManager_GetRawFileDescriptor
70                     RawFileDescriptor descriptor;
71                     bool getFd = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor);
72 
73                     // test OH_ResourceManager_ReadRawFile
74                     asyncContext->mediaData = std::make_unique<char[]>(asyncContext->len_);
75 //                    HiLog::Error(LABEL, "ReadRawFile:%{public}d", OH_ResourceManager_ReadRawFile(rawFile, asyncContext->mediaData.get(), asyncContext->len_));
76 
77                     // test OH_ResourceManager_ReleaseRawFileDescriptor
78                     OH_ResourceManager_ReleaseRawFileDescriptor(descriptor);
79 
80                     // test OH_ResourceManager_CloseRawFile
81                     OH_ResourceManager_CloseRawFile(rawFile);
82 
83                     // test OH_ResourceManager_CloseRawDir
84                     OH_ResourceManager_CloseRawDir(rawDir);
85 
86                     // test OH_ResourceManager_ReleaseNativeResourceManager
87                     OH_ResourceManager_ReleaseNativeResourceManager(asyncContext->ndk_);
88 
89                     asyncContext->createValueFunc_ = [](napi_env env, NdkAsyncContext &context) -> napi_value {
90                         napi_value buffer;
91                         napi_status status = napi_create_external_arraybuffer(env, context.mediaData.get(), context.len_,
92                                 [](napi_env env, void *data, void *hint) {
93                                     delete[] static_cast<char*>(data);
94                                 }, nullptr, &buffer);
95                         if (status != napi_ok) {
96                             context.SetErrorMsg("Failed to create media external array buffer");
97                             return nullptr;
98                         }
99 
100                         napi_value result = nullptr;
101                         status = napi_create_typedarray(env, napi_uint8_array, context.len_, buffer, 0, &result);
102                         if (status != napi_ok) {
103                             context.SetErrorMsg("Failed to create media typed array");
104                             return nullptr;
105                         }
106                         context.mediaData.release();
107                         return result;
108                     };
109                 };
110             }
111 
__anonfdb96b820402(napi_env env, napi_status status, void* data) 112             auto completeFunc = [](napi_env env, napi_status status, void* data) {
113                 NdkAsyncContext* asyncContext = static_cast<NdkAsyncContext*>(data);
114 
115                 napi_value finalResult = nullptr;
116                 if (asyncContext->createValueFunc_ != nullptr) {
117                     finalResult = asyncContext->createValueFunc_(env, *asyncContext);
118                 }
119 
120                 napi_value result[] = { nullptr, nullptr };
121                 if (asyncContext->success_) {
122                     napi_get_undefined(env, &result[0]);
123                     result[1] = finalResult;
124                 } else {
125                     napi_value message = nullptr;
126                     napi_create_string_utf8(env, asyncContext->errMsg_.c_str(), NAPI_AUTO_LENGTH, &message);
127                     napi_create_error(env, nullptr, message, &result[0]);
128                     napi_get_undefined(env, &result[1]);
129                 }
130                 do {
131                     if (asyncContext->deferred_) {
132                         if (asyncContext->success_) {
133                             if (napi_resolve_deferred(env, asyncContext->deferred_, result[1]) != napi_ok) {
134                             }
135                         } else {
136                             if (napi_reject_deferred(env, asyncContext->deferred_, result[0]) != napi_ok) {
137                             }
138                         }
139                     } else {
140                         napi_value callback = nullptr;
141                         napi_status status = napi_get_reference_value(env, asyncContext->callbackRef_, &callback);
142                         if (status != napi_ok) {
143                             break;
144                         }
145                         napi_value userRet = nullptr;
146                         status = napi_call_function(env, nullptr, callback, sizeof(result) / sizeof(napi_value), result, &userRet);
147                         if (status != napi_ok) {
148                             break;
149                         }
150                         status = napi_delete_reference(env, asyncContext->callbackRef_);
151                         if (status != napi_ok) {
152                             break;
153                         }
154                     }
155                 } while (false);
156                 napi_delete_async_work(env, asyncContext->work_);
157                 delete asyncContext;
158             };
159 
GetResourceManager(napi_env env,napi_callback_info info)160             napi_value GetResourceManager(napi_env env, napi_callback_info info)
161             {
162                 GET_PARAMS(env, info, 3);
163 
164                 std::unique_ptr<NdkAsyncContext> asyncContext = std::make_unique<NdkAsyncContext>();
165                 for (size_t i = 0; i < argc; i++) {
166                     napi_valuetype valueType;
167                     napi_typeof(env, argv[i], &valueType);
168                     if (i == 0 && valueType == napi_string) {
169                         size_t len = 0;
170                         napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
171                         if (status != napi_ok) {
172                             return nullptr;
173                         }
174                         std::vector<char> buf(len + 1);
175                         status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len);
176                         if (status != napi_ok) {
177                             return nullptr;
178                         }
179                         asyncContext->path_ = buf.data();
180                     } else if (i == 1 && valueType == napi_object) {
181                         // test OH_ResourceManager_InitNativeResourceManager
182                         asyncContext->ndk_ = OH_ResourceManager_InitNativeResourceManager(env, argv[i]);
183                     } else if (i == 2 && valueType == napi_function) {
184                         napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
185                         break;
186                     } else {
187                         // self resorucemanager with promise
188                     }
189                 }
190 
191                 napi_value result = nullptr;
192                 if (asyncContext->callbackRef_ == nullptr) {
193                     napi_create_promise(env, &asyncContext->deferred_, &result);
194                 } else {
195                     napi_get_undefined(env, &result);
196                 }
197 
198                 napi_value resource = nullptr;
199                 napi_create_string_utf8(env, "testRawFile", NAPI_AUTO_LENGTH, &resource);
200                 napi_status status = napi_create_async_work(env, nullptr, resource, TestRawFileExecute(), completeFunc,
201                         static_cast<void*>(asyncContext.get()), &asyncContext->work_);
202                 if (status != napi_ok) {
203                     return result;
204                 }
205                 status = napi_queue_async_work(env, asyncContext->work_);
206                 if (status != napi_ok) {
207                     return result;
208                 }
209 
210                 asyncContext.release();
211                 return result;
212             }
213 
214             EXTERN_C_START
Init(napi_env env,napi_value exports)215             static napi_value Init(napi_env env, napi_value exports)
216             {
217                 napi_property_descriptor desc[] = {
218                     { "testRawFile", nullptr, GetResourceManager, nullptr, nullptr, nullptr, napi_default, nullptr }
219                 };
220                 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
221                 return exports;
222             }
223             EXTERN_C_END
224 
225             static napi_module demoModule = {
226         .nm_version =1,
227         .nm_flags = 0,
228         .nm_filename = nullptr,
229         .nm_register_func = Init,
230         .nm_modname = "resmgrndk",
231         .nm_priv = ((void*)0),
232         .reserved = { 0 },
233     };
RegisterModule(void)234     extern "C" __attribute__((constructor)) void RegisterModule(void)
235     {
236         napi_module_register(&demoModule);
237     }
238 }
239 }
240 }