• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "resource_manager_addon.h"
17 
18 #include <fstream>
19 #include <memory>
20 #include <mutex>
21 #include <vector>
22 
23 #include "hilog/log_c.h"
24 #include "hilog/log_cpp.h"
25 #include "js_native_api.h"
26 #include "napi/native_common.h"
27 #include "node_api.h"
28 
29 #include "hisysevent_adapter.h"
30 #include "hitrace_meter.h"
31 #include "utils/utils.h"
32 
33 namespace OHOS {
34 namespace Global {
35 namespace Resource {
36 #define GET_PARAMS(env, info, num)    \
37     size_t argc = num;                \
38     napi_value argv[num] = {nullptr}; \
39     napi_value thisVar = nullptr;     \
40     void *data = nullptr;             \
41     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
42 
43 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "ResourceManagerJs" };
44 using namespace OHOS::HiviewDFX;
45 static thread_local napi_ref* g_constructor = nullptr;
46 
47 std::map<std::string, std::shared_ptr<ResourceManager>> g_resourceMgr;
48 std::mutex g_resMapLock;
49 
50 static const std::unordered_map<int32_t, std::string> ErrorCodeToMsg {
51     {ERROR_CODE_INVALID_INPUT_PARAMETER, "Invalid input parameter"},
52     {ERROR_CODE_RES_ID_NOT_FOUND, "Resource id invalid"},
53     {ERROR_CODE_RES_NAME_NOT_FOUND, "Resource name invalid"},
54     {ERROR_CODE_RES_NOT_FOUND_BY_ID, "Resource not found by id"},
55     {ERROR_CODE_RES_NOT_FOUND_BY_NAME, "Resource not found by name"},
56     {ERROR_CODE_RES_PATH_INVALID, "Rawfile path is invalid"},
57     {ERROR_CODE_RES_REF_TOO_MUCH, "Resource re-ref too much"},
58     {ERROR, "Unknow error"}
59 };
60 
Create(napi_env env,const std::string & bundleName,const std::shared_ptr<ResourceManager> & resMgr,std::shared_ptr<AbilityRuntime::Context> context)61 napi_value ResourceManagerAddon::Create(
62     napi_env env, const std::string& bundleName, const std::shared_ptr<ResourceManager>& resMgr,
63     std::shared_ptr<AbilityRuntime::Context> context)
64 {
65     std::shared_ptr<ResourceManagerAddon> addon = std::make_shared<ResourceManagerAddon>(bundleName, resMgr, context);
66 
67     if (!Init(env)) {
68         HiLog::Error(LABEL, "Failed to init resource manager addon");
69         return nullptr;
70     }
71 
72     napi_value constructor;
73     napi_status status = napi_get_reference_value(env, *g_constructor, &constructor);
74     if (status != napi_ok) {
75         return nullptr;
76     }
77     napi_value result;
78     status = napi_new_instance(env, constructor, 0, nullptr, &result);
79     if (status != napi_ok) {
80         return nullptr;
81     }
82 
83     auto addonPtr = std::make_unique<std::shared_ptr<ResourceManagerAddon>>(addon);
84     status = napi_wrap(env, result, reinterpret_cast<void *>(addonPtr.get()), ResourceManagerAddon::Destructor,
85         nullptr, nullptr);
86     if (status != napi_ok) {
87         return nullptr;
88     }
89     addonPtr.release();
90     return result;
91 }
92 
ResourceManagerAddon(const std::string & bundleName,const std::shared_ptr<ResourceManager> & resMgr,const std::shared_ptr<AbilityRuntime::Context> & context)93 ResourceManagerAddon::ResourceManagerAddon(
94     const std::string& bundleName, const std::shared_ptr<ResourceManager>& resMgr,
95     const std::shared_ptr<AbilityRuntime::Context>& context)
96     : bundleName_(bundleName), resMgr_(resMgr), context_(context)
97 {}
98 
~ResourceManagerAddon()99 ResourceManagerAddon::~ResourceManagerAddon()
100 {
101     HiLog::Info(LABEL, "~ResourceManagerAddon %{public}s", bundleName_.c_str());
102 }
103 
Destructor(napi_env env,void * nativeObject,void * hint)104 void ResourceManagerAddon::Destructor(napi_env env, void *nativeObject, void *hint)
105 {
106     std::unique_ptr<std::shared_ptr<ResourceManagerAddon>> addonPtr;
107     addonPtr.reset(static_cast<std::shared_ptr<ResourceManagerAddon>*>(nativeObject));
108 }
109 
SetErrorMsg(const std::string & msg,bool withResId,int32_t errCode)110 void ResMgrAsyncContext::SetErrorMsg(const std::string &msg, bool withResId, int32_t errCode)
111 {
112     errMsg_ = msg;
113     success_ = false;
114     errCode_ = errCode;
115     if (withResId) {
116         HiLog::Error(LABEL, "%{public}s id = %{public}d", msg.c_str(), resId_);
117     } else {
118         HiLog::Error(LABEL, "%{public}s name = %{public}s", msg.c_str(), resName_.c_str());
119     }
120 }
121 
Init(napi_env env)122 bool ResourceManagerAddon::Init(napi_env env)
123 {
124     if (g_constructor != nullptr) {
125         return true;
126     }
127 
128     napi_property_descriptor properties[] = {
129         DECLARE_NAPI_FUNCTION("getString", GetString),
130         DECLARE_NAPI_FUNCTION("getStringByName", GetStringByName),
131         DECLARE_NAPI_FUNCTION("getStringArray", GetStringArray),
132         DECLARE_NAPI_FUNCTION("getStringArrayByName", GetStringArrayByName),
133         DECLARE_NAPI_FUNCTION("getMedia", GetMedia),
134         DECLARE_NAPI_FUNCTION("getMediaByName", GetMediaByName),
135         DECLARE_NAPI_FUNCTION("getMediaBase64", GetMediaBase64),
136         DECLARE_NAPI_FUNCTION("getMediaBase64ByName", GetMediaBase64ByName),
137         DECLARE_NAPI_FUNCTION("getConfiguration", GetConfiguration),
138         DECLARE_NAPI_FUNCTION("getDeviceCapability", GetDeviceCapability),
139         DECLARE_NAPI_FUNCTION("getPluralString", GetPluralString),
140         DECLARE_NAPI_FUNCTION("getPluralStringByName", GetPluralStringByName),
141         DECLARE_NAPI_FUNCTION("getRawFile", GetRawFile),
142         DECLARE_NAPI_FUNCTION("getRawFileDescriptor", GetRawFileDescriptor),
143         DECLARE_NAPI_FUNCTION("closeRawFileDescriptor", CloseRawFileDescriptor),
144         DECLARE_NAPI_FUNCTION("getStringSync", GetStringSync),
145         DECLARE_NAPI_FUNCTION("getStringByNameSync", GetStringByNameSync),
146         DECLARE_NAPI_FUNCTION("getBoolean", GetBoolean),
147         DECLARE_NAPI_FUNCTION("getNumber", GetNumber),
148         DECLARE_NAPI_FUNCTION("getBooleanByName", GetBooleanByName),
149         DECLARE_NAPI_FUNCTION("getNumberByName", GetNumberByName),
150         DECLARE_NAPI_FUNCTION("release", Release),
151         DECLARE_NAPI_FUNCTION("getStringValue", GetStringValue),
152         DECLARE_NAPI_FUNCTION("getStringArrayValue", GetStringArrayValue),
153         DECLARE_NAPI_FUNCTION("getPluralStringValue", GetPluralStringValue),
154         DECLARE_NAPI_FUNCTION("getMediaContent", GetMediaContent),
155         DECLARE_NAPI_FUNCTION("getMediaContentBase64", GetMediaContentBase64),
156         DECLARE_NAPI_FUNCTION("getRawFileContent", GetRawFileContent),
157         DECLARE_NAPI_FUNCTION("getRawFd", GetRawFd),
158         DECLARE_NAPI_FUNCTION("closeRawFd", CloseRawFd)
159 
160     };
161 
162     napi_value constructor;
163     napi_status status = napi_define_class(env, "ResourceManager", NAPI_AUTO_LENGTH, New, nullptr,
164         sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
165     if (status != napi_ok) {
166         HiLog::Error(LABEL, "Failed to define class at Init");
167         return false;
168     }
169 
170     g_constructor = new (std::nothrow) napi_ref;
171     if (g_constructor == nullptr) {
172         HiLog::Error(LABEL, "Failed to create ref at init");
173         return false;
174     }
175     status = napi_create_reference(env, constructor, 1, g_constructor);
176     if (status != napi_ok) {
177         HiLog::Error(LABEL, "Failed to create reference at init");
178         return false;
179     }
180     return true;
181 }
182 
FindErrMsg(int32_t errCode)183 std::string FindErrMsg(int32_t errCode)
184 {
185     auto iter = ErrorCodeToMsg.find(errCode);
186     std::string errMsg = iter != ErrorCodeToMsg.end() ? iter->second : "";
187     return errMsg;
188 }
189 
NapiThrow(napi_env env,int32_t errCode)190 void ResMgrAsyncContext::NapiThrow(napi_env env, int32_t errCode)
191 {
192     napi_value code = nullptr;
193     napi_create_string_latin1(env, std::to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &code);
194 
195     napi_value message = nullptr;
196     std::string errMsg = FindErrMsg(errCode);
197     napi_create_string_latin1(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
198     if (errMsg != "") {
199         napi_value error = nullptr;
200         napi_create_error(env, code, message, &error);
201         napi_throw(env, error);
202     }
203 }
204 
GetCallbackErrorCode(napi_env env,const int32_t errCode,const std::string errMsg)205 napi_value GetCallbackErrorCode(napi_env env, const int32_t errCode, const std::string errMsg)
206 {
207     napi_value error = nullptr;
208     napi_value eCode = nullptr;
209     napi_value eMsg = nullptr;
210     napi_create_int32(env, errCode, &eCode);
211     napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &eMsg);
212     napi_create_object(env, &error);
213     napi_set_named_property(env, error, "code", eCode);
214     napi_set_named_property(env, error, "message", eMsg);
215     return error;
216 }
217 
Complete(napi_env env,napi_status status,void * data)218 void ResMgrAsyncContext::Complete(napi_env env, napi_status status, void* data)
219 {
220     ResMgrAsyncContext* asyncContext = static_cast<ResMgrAsyncContext*>(data);
221 
222     napi_value finalResult = nullptr;
223     if (asyncContext->createValueFunc_ != nullptr) {
224         finalResult = asyncContext->createValueFunc_(env, *asyncContext);
225     }
226 
227     napi_value result[] = { nullptr, nullptr };
228     if (asyncContext->success_) {
229         napi_get_undefined(env, &result[0]);
230         result[1] = finalResult;
231     } else {
232         result[0] = GetCallbackErrorCode(env, asyncContext->errCode_, asyncContext->errMsg_.c_str());
233         napi_get_undefined(env, &result[1]);
234     }
235     if (asyncContext->deferred_) {
236         if (asyncContext->success_) {
237             if (napi_resolve_deferred(env, asyncContext->deferred_, result[1]) != napi_ok) {
238                 HiLog::Error(LABEL, "napi_resolve_deferred failed");
239             }
240         } else {
241             result[0] = GetCallbackErrorCode(env, asyncContext->errCode_, asyncContext->errMsg_.c_str());
242             if (napi_reject_deferred(env, asyncContext->deferred_, result[0]) != napi_ok) {
243                 HiLog::Error(LABEL, "napi_reject_deferred failed");
244             }
245         }
246     } else {
247         do {
248             napi_value callback = nullptr;
249             napi_status status = napi_get_reference_value(env, asyncContext->callbackRef_, &callback);
250             if (status != napi_ok) {
251                 HiLog::Error(LABEL, "napi_get_reference_value failed status=%{public}d", status);
252                 break;
253             }
254             napi_value userRet = nullptr;
255             status = napi_call_function(env, nullptr, callback, sizeof(result) / sizeof(napi_value), result, &userRet);
256             if (status != napi_ok) {
257                 HiLog::Error(LABEL, "napi_call_function failed status=%{public}d", status);
258                 break;
259             }
260             status = napi_delete_reference(env, asyncContext->callbackRef_);
261             if (status != napi_ok) {
262                 HiLog::Error(LABEL, "napi_call_function failed status=%{public}d", status);
263                 break;
264             }
265         } while (false);
266     }
267     napi_delete_async_work(env, asyncContext->work_);
268     delete asyncContext;
269 };
270 
New(napi_env env,napi_callback_info info)271 napi_value ResourceManagerAddon::New(napi_env env, napi_callback_info info)
272 {
273     GET_PARAMS(env, info, 1);
274 
275     napi_value target = nullptr;
276     napi_get_new_target(env, info, &target);
277     if (target != nullptr) {
278         return thisVar;
279     }
280 
281     return nullptr;
282 }
283 
IsLoadHap(ResMgrAsyncContext * asyncContext)284 bool IsLoadHap(ResMgrAsyncContext *asyncContext)
285 {
286     RState state = asyncContext->addon_->GetResMgr()->IsLoadHap();
287     if (state != RState::SUCCESS) {
288         return false;
289     }
290     return true;
291 }
292 
getResourceManagerAddon(napi_env env,napi_callback_info info)293 std::shared_ptr<ResourceManagerAddon> getResourceManagerAddon(napi_env env, napi_callback_info info)
294 {
295     GET_PARAMS(env, info, 2);
296 
297     std::shared_ptr<ResourceManagerAddon> *addonPtr = nullptr;
298     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&addonPtr));
299     if (status != napi_ok) {
300         HiLog::Error(LABEL, "Failed to unwrap");
301         return nullptr;
302     }
303     return *addonPtr;
304 }
305 
isNapiNumber(napi_env env,napi_callback_info info)306 bool isNapiNumber(napi_env env, napi_callback_info info)
307 {
308     GET_PARAMS(env, info, 2);
309 
310     napi_valuetype valueType = napi_valuetype::napi_undefined;
311     napi_typeof(env, argv[0], &valueType);
312     if (valueType != napi_number) {
313         HiLog::Error(LABEL, "Parameter type is not napi_number");
314         return false;
315     }
316     return true;
317 }
318 
isNapiObject(napi_env env,napi_callback_info info)319 bool isNapiObject(napi_env env, napi_callback_info info)
320 {
321     GET_PARAMS(env, info, 2);
322 
323     napi_valuetype valueType = napi_valuetype::napi_undefined;
324     napi_typeof(env, argv[0], &valueType);
325     if (valueType != napi_object) {
326         HiLog::Error(LABEL, "Parameter type is not napi_object");
327         return false;
328     }
329     return true;
330 }
331 
isNapiString(napi_env env,napi_callback_info info)332 bool isNapiString(napi_env env, napi_callback_info info)
333 {
334     GET_PARAMS(env, info, 2);
335 
336     napi_valuetype valueType = napi_valuetype::napi_undefined;
337     napi_typeof(env, argv[0], &valueType);
338     if (valueType != napi_string) {
339         HiLog::Error(LABEL, "Parameter type is not napi_string");
340         return false;
341     }
342     return true;
343 }
344 
GetResId(napi_env env,size_t argc,napi_value * argv)345 int ResourceManagerAddon::GetResId(napi_env env, size_t argc, napi_value *argv)
346 {
347     if (argc == 0 || argv == nullptr) {
348         return 0;
349     }
350 
351     napi_valuetype valuetype;
352     napi_status status = napi_typeof(env, argv[0], &valuetype);
353     if (status != napi_ok) {
354         HiLog::Error(LABEL, "Failed to get value type");
355         return 0;
356     }
357     if (valuetype != napi_number) {
358         HiLog::Error(LABEL, "Invalid param, not number");
359         return 0;
360     }
361     int resId = 0;
362     status = napi_get_value_int32(env, argv[0], &resId);
363     if (status != napi_ok) {
364         HiLog::Error(LABEL, "Failed to get id number");
365         return 0;
366     }
367 
368     return resId;
369 }
370 
GetResNameOrPath(napi_env env,size_t argc,napi_value * argv)371 std::string ResourceManagerAddon::GetResNameOrPath(napi_env env, size_t argc, napi_value *argv)
372 {
373     if (argc == 0 || argv == nullptr) {
374         return "";
375     }
376 
377     napi_valuetype valuetype;
378     napi_typeof(env, argv[0], &valuetype);
379     if (valuetype != napi_string) {
380         HiLog::Error(LABEL, "Invalid param, not string");
381         return "";
382     }
383     size_t len = 0;
384     napi_status status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len);
385     if (status != napi_ok) {
386         HiLog::Error(LABEL, "Failed to get resName or rawfile path length");
387         return "";
388     }
389     std::vector<char> buf(len + 1);
390     status = napi_get_value_string_utf8(env, argv[0], buf.data(), len + 1, &len);
391     if (status != napi_ok) {
392         HiLog::Error(LABEL, "Failed to get resName or raw file path");
393         return "";
394     }
395     return buf.data();
396 }
397 
ProcessNameParamV9(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)398 napi_value ResourceManagerAddon::ProcessNameParamV9(napi_env env, napi_callback_info info, const std::string &name,
399     napi_async_execute_callback execute)
400 {
401     GET_PARAMS(env, info, 2);
402     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
403     asyncContext->addon_ = getResourceManagerAddon(env, info);
404     if (asyncContext->addon_ == nullptr) {
405         return nullptr;
406     }
407     for (size_t i = 0; i < argc; i++) {
408         napi_valuetype valueType;
409         napi_typeof(env, argv[i], &valueType);
410         if (i == 0 && valueType == napi_string) {
411             asyncContext->resName_ = GetResNameOrPath(env, argc, argv);
412         } else if (i == 1 && valueType == napi_function) {
413             napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
414             break;
415         } else {
416             // self resourcemanager with promise
417         }
418     }
419 
420     napi_value result = ResMgrAsyncContext::getResult(env, asyncContext, name, execute);
421     return result;
422 }
423 
ProcessIdParamV9(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)424 napi_value ResourceManagerAddon::ProcessIdParamV9(napi_env env, napi_callback_info info, const std::string &name,
425     napi_async_execute_callback execute)
426 {
427     GET_PARAMS(env, info, 2);
428 
429     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
430     asyncContext->addon_ = getResourceManagerAddon(env, info);
431     if (asyncContext->addon_ == nullptr) {
432         return nullptr;
433     }
434     for (size_t i = 0; i < argc; i++) {
435         napi_valuetype valueType;
436         napi_typeof(env, argv[i], &valueType);
437         if (i == 0 && valueType == napi_number) {
438             asyncContext->resId_ = GetResId(env, argc, argv);
439         } else if (i == 1 && valueType == napi_function) {
440             napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
441             break;
442         } else {
443             // self resourcemanager with promise
444         }
445     }
446 
447     napi_value result = ResMgrAsyncContext::getResult(env, asyncContext, name, execute);
448     return result;
449 }
450 
ProcessResourceParamV9(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)451 napi_value ResourceManagerAddon::ProcessResourceParamV9(napi_env env, napi_callback_info info, const std::string &name,
452     napi_async_execute_callback execute)
453 {
454     GET_PARAMS(env, info, 2);
455 
456     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
457     asyncContext->addon_ = getResourceManagerAddon(env, info);
458     if (asyncContext->addon_ == nullptr) {
459         return nullptr;
460     }
461     for (size_t i = 0; i < argc; i++) {
462         napi_valuetype valueType;
463         napi_typeof(env, argv[i], &valueType);
464         if (i == 0 && valueType == napi_object) {
465             auto resourcePtr = std::make_shared<ResourceManager::Resource>();
466             int32_t retCode = GetResourceObject(env, resourcePtr, argv[0]);
467             if (retCode != SUCCESS) {
468                 HiLog::Error(LABEL, "Failed to get native Resource object");
469                 ResMgrAsyncContext::NapiThrow(env, retCode);
470                 return nullptr;
471             }
472             asyncContext->resource_ = resourcePtr;
473         } else if (i == 1 && valueType == napi_function) {
474             napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
475             break;
476         } else {
477             // self resourcemanager with promise
478         }
479     }
480 
481     napi_value result = ResMgrAsyncContext::getResult(env, asyncContext, name, execute);
482     return result;
483 }
484 
ProcessOnlyIdParam(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)485 napi_value ResourceManagerAddon::ProcessOnlyIdParam(napi_env env, napi_callback_info info, const std::string &name,
486     napi_async_execute_callback execute)
487 {
488     GET_PARAMS(env, info, 2);
489 
490     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
491     asyncContext->addon_ = getResourceManagerAddon(env, info);
492     if (asyncContext->addon_ == nullptr) {
493         return nullptr;
494     }
495     for (size_t i = 0; i < argc; i++) {
496         napi_valuetype valueType;
497         napi_typeof(env, argv[i], &valueType);
498         if (i == 0 && valueType == napi_number) {
499             asyncContext->resId_ = GetResId(env, argc, argv);
500         } else if (i == 1 && valueType == napi_function) {
501             napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
502             break;
503         } else if (i == 0 && valueType == napi_string) {
504             asyncContext->path_ = GetResNameOrPath(env, argc, argv);
505         } else if (i == 0 && valueType == napi_object) {
506             std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
507             int32_t retCode = GetResourceObject(env, resourcePtr, argv[0]);
508             if (retCode != SUCCESS) {
509                 ResMgrAsyncContext::NapiThrow(env, retCode);
510                 return nullptr;
511             }
512             asyncContext->resource_ = resourcePtr;
513         } else {
514             // self resourcemanager with promise
515         }
516     }
517     napi_value result = ResMgrAsyncContext::getResult(env, asyncContext, name, execute);
518     return result;
519 }
520 
__anon14aac4540102(napi_env env, void* data) 521 auto getStringByNameFunc = [](napi_env env, void* data) {
522     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
523     RState state = asyncContext->addon_->GetResMgr()->GetStringByName(asyncContext->resName_.c_str(),
524         asyncContext->value_);
525     if (state != RState::SUCCESS) {
526         asyncContext->SetErrorMsg("GetStringByName failed state", false, state);
527         ReportGetResourceByNameFail(asyncContext->resName_, asyncContext->value_,
528             "failed in getStringByNameFunc");
529         return;
530     }
531     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) {
532         napi_value jsValue = nullptr;
533         if (napi_create_string_utf8(env, context.value_.c_str(), NAPI_AUTO_LENGTH, &jsValue) != napi_ok) {
534             context.SetErrorMsg("GetStringByName failed to create result");
535             return jsValue;
536         }
537         return jsValue;
538     };
539 };
540 
GetStringByName(napi_env env,napi_callback_info info)541 napi_value ResourceManagerAddon::GetStringByName(napi_env env, napi_callback_info info)
542 {
543     if (!isNapiString(env, info)) {
544         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
545         return nullptr;
546     }
547     return ProcessNameParamV9(env, info, "getStringByName", getStringByNameFunc);
548 }
549 
__anon14aac4540302(napi_env env, void* data) 550 auto getStringFunc = [](napi_env env, void* data) {
551     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
552     std::shared_ptr<ResourceManager> resMgr = nullptr;
553     int32_t resId = 0;
554 
555     bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext, resMgr, resId);
556     if (!ret) {
557         HiLog::Error(LABEL, "Failed to GetHapResourceManager in getStringFunc");
558         return;
559     }
560     RState state = resMgr->GetStringById(resId, asyncContext->value_);
561     if (state != RState::SUCCESS) {
562         asyncContext->SetErrorMsg("GetStringById failed state", true, state);
563         ReportGetResourceByIdFail(resId, asyncContext->value_, "failed in getStringFunc");
564         return;
565     }
566     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) {
567         napi_value jsValue = nullptr;
568         if (napi_create_string_utf8(env, context.value_.c_str(), NAPI_AUTO_LENGTH, &jsValue) != napi_ok) {
569             context.SetErrorMsg("GetStringById failed to create result");
570             return jsValue;
571         }
572         return jsValue;
573     };
574 };
575 
GetString(napi_env env,napi_callback_info info)576 napi_value ResourceManagerAddon::GetString(napi_env env, napi_callback_info info)
577 {
578     return ProcessOnlyIdParam(env, info, "getString", getStringFunc);
579 }
580 
__anon14aac4540502(napi_env env, void* data) 581 auto getStringArrayFunc = [](napi_env env, void* data) {
582     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
583     RState state;
584     int32_t resId = 0;
585     std::shared_ptr<ResourceManager> resMgr = nullptr;
586     if (asyncContext->resId_ != 0 || asyncContext->resource_ != nullptr) {
587         bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext, resMgr, resId);
588         if (!ret) {
589             HiLog::Error(LABEL, "Failed to GetHapResourceManager in getStringArrayFunc");
590             return;
591         }
592         state = resMgr->GetStringArrayById(resId, asyncContext->arrayValue_);
593         if (state != RState::SUCCESS) {
594             asyncContext->SetErrorMsg("GetStringArrayById failed state", true, state);
595             return;
596         }
597     } else {
598         state = asyncContext->addon_->GetResMgr()->GetStringArrayByName(asyncContext->resName_.c_str(),
599             asyncContext->arrayValue_);
600         if (state != RState::SUCCESS) {
601             asyncContext->SetErrorMsg("GetStringArrayByName failed state", false, state);
602             return;
603         }
604     }
605     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext &context) -> napi_value {
606         napi_value result;
607         napi_status status = napi_create_array_with_length(env, context.arrayValue_.size(), &result);
608         if (status != napi_ok) {
609             context.SetErrorMsg("Failed to create array");
610             return nullptr;
611         }
612         for (size_t i = 0; i < context.arrayValue_.size(); i++) {
613             napi_value value;
614             status = napi_create_string_utf8(env, context.arrayValue_[i].c_str(), NAPI_AUTO_LENGTH, &value);
615             if (status != napi_ok) {
616                 context.SetErrorMsg("Failed to create string item");
617                 return nullptr;
618             }
619             status = napi_set_element(env, result, i, value);
620             if (status != napi_ok) {
621                 context.SetErrorMsg("Failed to set array item");
622                 return nullptr;
623             }
624         }
625 
626         return result;
627     };
628 };
629 
GetStringArrayByName(napi_env env,napi_callback_info info)630 napi_value ResourceManagerAddon::GetStringArrayByName(napi_env env, napi_callback_info info)
631 {
632     if (!isNapiString(env, info)) {
633         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
634         return nullptr;
635     }
636     return ProcessNameParamV9(env, info, "GetStringArrayByName", getStringArrayFunc);
637 }
638 
GetStringArray(napi_env env,napi_callback_info info)639 napi_value ResourceManagerAddon::GetStringArray(napi_env env, napi_callback_info info)
640 {
641     return ProcessOnlyIdParam(env, info, "getStringArray", getStringArrayFunc);
642 }
643 
CreateValue(ResMgrAsyncContext * asyncContext)644 void CreateValue(ResMgrAsyncContext *asyncContext)
645 {
646     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) -> napi_value {
647         napi_value buffer;
648         napi_status status = napi_create_external_arraybuffer(env, context.mediaData.get(), context.len_,
649             [](napi_env env, void *data, void *hint) {
650                 HiLog::Error(LABEL, "Media buffer finalized");
651                 delete[] static_cast<char*>(data);
652             }, nullptr, &buffer);
653         if (status != napi_ok) {
654             context.SetErrorMsg("Failed to create media external array buffer");
655             return nullptr;
656         }
657 
658         napi_value result = nullptr;
659         status = napi_create_typedarray(env, napi_uint8_array, context.len_, buffer, 0, &result);
660         if (status != napi_ok) {
661             context.SetErrorMsg("Failed to create media typed array");
662             return nullptr;
663         }
664         context.mediaData.release();
665         return result;
666     };
667 }
668 
GetResourcesBufferData(std::string path,ResMgrAsyncContext & asyncContext)669 void GetResourcesBufferData(std::string path, ResMgrAsyncContext &asyncContext)
670 {
671     asyncContext.mediaData = Utils::LoadResourceFile(path, asyncContext.len_);
672     if (asyncContext.mediaData == nullptr) {
673         return;
674     }
675     CreateValue(&asyncContext);
676 }
677 
__anon14aac4540902(napi_env env, void *data) 678 auto getMediaByNameFunc = [](napi_env env, void *data) {
679     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
680     std::string path;
681     RState state;
682     if (IsLoadHap(asyncContext)) {
683         size_t tmpLen;
684         state = asyncContext->addon_->GetResMgr()->GetMediaDataByName(asyncContext->resName_.c_str(), tmpLen,
685             asyncContext->mediaData);
686         if (state != RState::SUCCESS) {
687             asyncContext->SetErrorMsg("GetMedia path byName failed", false, state);
688             return;
689         }
690         asyncContext->len_ = static_cast<int>(tmpLen);
691         CreateValue(asyncContext);
692         return;
693     }
694     state = asyncContext->addon_->GetResMgr()->GetMediaByName(asyncContext->resName_.c_str(), path);
695     if (state != RState::SUCCESS) {
696         asyncContext->SetErrorMsg("GetMediabyName path failed", false, state);
697         ReportGetResourceByNameFail(asyncContext->resName_, path, "failed in getMediaByNameFunc");
698         return;
699     }
700     GetResourcesBufferData(path, *asyncContext);
701 };
702 
GetMediaByName(napi_env env,napi_callback_info info)703 napi_value ResourceManagerAddon::GetMediaByName(napi_env env, napi_callback_info info)
704 {
705     if (!isNapiString(env, info)) {
706         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
707         return nullptr;
708     }
709     std::string traceVal = "ResourceManagerAddon::GetMediaByName";
710     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
711     napi_value media = ProcessNameParamV9(env, info, "getMediaByName", getMediaByNameFunc);
712     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
713     return media;
714 }
715 
__anon14aac4540a02(napi_env env, void *data) 716 auto getMediaFunc = [](napi_env env, void *data) {
717     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
718     std::string path;
719     int32_t resId = 0;
720     RState state;
721     std::shared_ptr<ResourceManager> resMgr = nullptr;
722     bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext, resMgr, resId);
723     if (!ret) {
724         HiLog::Error(LABEL, "Failed to GetHapResourceManager in getMediaFunc");
725         return;
726     }
727     if (IsLoadHap(asyncContext)) {
728         size_t tmpLen;
729         state = resMgr->GetMediaDataById(resId, tmpLen, asyncContext->mediaData);
730         if (state != RState::SUCCESS) {
731             asyncContext->SetErrorMsg("GetMedia path by id failed", true, state);
732             return;
733         }
734         asyncContext->len_ = static_cast<int>(tmpLen);
735         CreateValue(asyncContext);
736         return;
737     }
738     state = resMgr->GetMediaById(resId, path);
739     if (state != RState::SUCCESS) {
740         asyncContext->SetErrorMsg("GetMedia path by id failed", true, state);
741         ReportGetResourceByIdFail(resId, path, "failed in getMediaFunc");
742         return;
743     }
744     GetResourcesBufferData(path, *asyncContext);
745 };
746 
GetMedia(napi_env env,napi_callback_info info)747 napi_value ResourceManagerAddon::GetMedia(napi_env env, napi_callback_info info)
748 {
749     std::string traceVal = "ResourceManagerAddon::GetMedia";
750     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
751     napi_value media = ProcessOnlyIdParam(env, info, "getMedia", getMediaFunc);
752     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
753     return media;
754 }
755 
CreateStringValue(ResMgrAsyncContext * asyncContext)756 void CreateStringValue(ResMgrAsyncContext *asyncContext)
757 {
758     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext &context) {
759         napi_value result;
760         if (napi_create_string_utf8(env, context.value_.c_str(), NAPI_AUTO_LENGTH, &result) != napi_ok) {
761             context.SetErrorMsg("Failed to create result");
762             return result;
763         }
764         return result;
765     };
766 }
767 
__anon14aac4540c02(napi_env env, void *data) 768 auto getMediaBase64Func = [](napi_env env, void *data) {
769     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
770     std::string path;
771     RState state;
772     if (asyncContext->resId_ != 0 || asyncContext->resource_ != nullptr) {
773         int32_t resId = 0;
774         std::shared_ptr<ResourceManager> resMgr = nullptr;
775         bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext, resMgr, resId);
776         if (!ret) {
777             HiLog::Error(LABEL, "Failed to GetHapResourceManager in getMediaBase64Func");
778             return;
779         }
780         if (IsLoadHap(asyncContext)) {
781             state = resMgr->GetMediaBase64DataById(resId, asyncContext->value_);
782             if (state != RState::SUCCESS) {
783                 asyncContext->SetErrorMsg("GetMedia path failed", true, state);
784                 return;
785             }
786             CreateStringValue(asyncContext);
787             return;
788         }
789         state = resMgr->GetMediaById(resId, path);
790         if (state != RState::SUCCESS) {
791             asyncContext->SetErrorMsg("GetMedia path failed", true, state);
792             return;
793         }
794     } else {
795         if (IsLoadHap(asyncContext)) {
796             state = asyncContext->addon_->GetResMgr()->GetMediaBase64DataByName(asyncContext->resName_.c_str(),
797                 asyncContext->value_);
798             if (state != RState::SUCCESS) {
799                 asyncContext->SetErrorMsg("GetMedia path failed", false, state);
800                 return;
801             }
802             CreateStringValue(asyncContext);
803             return;
804         }
805         state = asyncContext->addon_->GetResMgr()->GetMediaByName(asyncContext->resName_.c_str(), path);
806         if (state != RState::SUCCESS) {
807             asyncContext->SetErrorMsg("GetMedia path failed", false, state);
808             return;
809         }
810     }
811     int len = 0;
812     std::unique_ptr<uint8_t[]> tempData = Utils::LoadResourceFile(path, len);
813     if (tempData == nullptr) {
814         return;
815     }
816     auto pos = path.find_last_of('.');
817     std::string imgType;
818     if (pos != std::string::npos) {
819         imgType = path.substr(pos + 1);
820     }
821     std::string base64Data;
822     Utils::EncodeBase64(tempData, len, imgType, base64Data);
823     asyncContext->value_ = base64Data;
824     CreateStringValue(asyncContext);
825 };
826 
GetMediaBase64(napi_env env,napi_callback_info info)827 napi_value ResourceManagerAddon::GetMediaBase64(napi_env env, napi_callback_info info)
828 {
829     std::string traceVal = "ResourceManagerAddon::GetMediaBase64";
830     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
831     napi_value mediaBase64 = ProcessOnlyIdParam(env, info, "GetMediaBase64", getMediaBase64Func);
832     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
833     return mediaBase64;
834 }
835 
GetMediaBase64ByName(napi_env env,napi_callback_info info)836 napi_value ResourceManagerAddon::GetMediaBase64ByName(napi_env env, napi_callback_info info)
837 {
838     if (!isNapiString(env, info)) {
839         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
840         return nullptr;
841     }
842     std::string traceVal = "ResourceManagerAddon::GetMediaBase64ByName";
843     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
844     napi_value mediaBase64 = ProcessNameParamV9(env, info, "GetMediaBase64ByName", getMediaBase64Func);
845     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
846     return mediaBase64;
847 }
848 
Release(napi_env env,napi_callback_info info)849 napi_value ResourceManagerAddon::Release(napi_env env, napi_callback_info info)
850 {
851     napi_value undefined;
852     if (napi_get_undefined(env, &undefined) != napi_ok) {
853         return nullptr;
854     }
855     return undefined;
856 }
857 
GetLocale(std::unique_ptr<ResConfig> & cfg)858 std::string ResourceManagerAddon::GetLocale(std::unique_ptr<ResConfig> &cfg)
859 {
860     std::string result;
861 #ifdef SUPPORT_GRAPHICS
862     const icu::Locale *localeInfo = cfg->GetLocaleInfo();
863     if (localeInfo == nullptr) {
864         return result;
865     }
866     const char *lang = localeInfo->getLanguage();
867     if (lang == nullptr) {
868         return result;
869     }
870     result = lang;
871 
872     const char *script = localeInfo->getScript();
873     if (script != nullptr) {
874         result += std::string("_") + script;
875     }
876 
877     const char *region = localeInfo->getCountry();
878     if (region != nullptr) {
879         result += std::string("_") + region;
880     }
881 #endif
882     return result;
883 }
884 
ProcessNoParam(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)885 napi_value ResourceManagerAddon::ProcessNoParam(napi_env env, napi_callback_info info, const std::string &name,
886     napi_async_execute_callback execute)
887 {
888     GET_PARAMS(env, info, 1);
889 
890     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
891     std::shared_ptr<ResourceManagerAddon> *addonPtr = nullptr;
892     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&addonPtr));
893     if (status != napi_ok) {
894         HiLog::Error(LABEL, "Failed to unwrap ProcessNoParam");
895         return nullptr;
896     }
897     asyncContext->addon_ = *addonPtr;
898     napi_valuetype valueType;
899     napi_typeof(env, argv[0], &valueType);
900     if (valueType == napi_function) {
901         napi_create_reference(env, argv[0], 1, &asyncContext->callbackRef_);
902     }
903 
904     napi_value result = ResMgrAsyncContext::getResult(env, asyncContext, name, execute);
905     return result;
906 }
907 
__anon14aac4540d02(napi_env env, void *data) 908 auto getConfigFunc = [](napi_env env, void *data) {
909     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
910     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) -> napi_value {
911         std::unique_ptr<ResConfig> cfg(CreateResConfig());
912         context.addon_->GetResMgr()->GetResConfig(*cfg);
913 
914         napi_value result;
915         napi_status status = napi_create_object(env, &result);
916         if (status != napi_ok) {
917             context.SetErrorMsg("Failed to create Configuration object");
918             return nullptr;
919         }
920 
921         napi_value direction;
922         status = napi_create_int32(env, static_cast<int>(cfg->GetDirection()), &direction);
923         if (status != napi_ok) {
924             context.SetErrorMsg("Failed to create direction");
925             return nullptr;
926         }
927         status = napi_set_named_property(env, result, "direction", direction);
928         if (status != napi_ok) {
929             context.SetErrorMsg("Failed to set direction property");
930             return nullptr;
931         }
932 
933         napi_value locale;
934         status = napi_create_string_utf8(env, context.addon_->GetLocale(cfg).c_str(), NAPI_AUTO_LENGTH, &locale);
935         if (status != napi_ok) {
936             context.SetErrorMsg("Failed to create locale");
937             return nullptr;
938         }
939         status = napi_set_named_property(env, result, "locale", locale);
940         if (status != napi_ok) {
941             context.SetErrorMsg("Failed to set locale property");
942             return nullptr;
943         }
944         return result;
945     };
946 };
947 
GetConfiguration(napi_env env,napi_callback_info info)948 napi_value ResourceManagerAddon::GetConfiguration(napi_env env, napi_callback_info info)
949 {
950     return ProcessNoParam(env, info, "GetConfiguration", getConfigFunc);
951 }
952 
__anon14aac4540f02(napi_env env, void *data) 953 auto getDeviceCapFunc = [](napi_env env, void *data) {
954     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
955     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) -> napi_value {
956         std::unique_ptr<ResConfig> cfg(CreateResConfig());
957         context.addon_->GetResMgr()->GetResConfig(*cfg);
958 
959         napi_value result;
960         napi_status status = napi_create_object(env, &result);
961         if (status != napi_ok) {
962             context.SetErrorMsg("Failed to create GetDeviceCapability object");
963             return nullptr;
964         }
965 
966         napi_value deviceType;
967         status = napi_create_int32(env, static_cast<int>(cfg->GetDeviceType()), &deviceType);
968         if (status != napi_ok) {
969             context.SetErrorMsg("Failed to create deviceType");
970             return nullptr;
971         }
972         status = napi_set_named_property(env, result, "deviceType", deviceType);
973         if (status != napi_ok) {
974             context.SetErrorMsg("Failed to set deviceType property");
975             return nullptr;
976         }
977 
978         napi_value screenDensity;
979         status = napi_create_int32(env, static_cast<int>(cfg->GetScreenDensity()), &screenDensity);
980         if (status != napi_ok) {
981             context.SetErrorMsg("Failed to create screenDensity");
982             return nullptr;
983         }
984         status = napi_set_named_property(env, result, "screenDensity", screenDensity);
985         if (status != napi_ok) {
986             context.SetErrorMsg("Failed to set screenDensity property");
987             return nullptr;
988         }
989         return result;
990     };
991 };
992 
GetDeviceCapability(napi_env env,napi_callback_info info)993 napi_value ResourceManagerAddon::GetDeviceCapability(napi_env env, napi_callback_info info)
994 {
995     return ProcessNoParam(env, info, "GetDeviceCapability", getDeviceCapFunc);
996 }
997 
__anon14aac4541102(napi_env env, void *data) 998 auto getPluralCapFunc = [](napi_env env, void *data) {
999     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
1000     RState state;
1001     if (asyncContext->resId_ != 0 || asyncContext->resource_ != nullptr) {
1002         int32_t resId = 0;
1003         std::shared_ptr<ResourceManager> resMgr = nullptr;
1004         bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext, resMgr, resId);
1005         if (!ret) {
1006             HiLog::Error(LABEL, "Failed to GetHapResourceManager in getPluralCapFunc");
1007             return;
1008         }
1009         state = resMgr->GetPluralStringByIdFormat(asyncContext->value_,
1010             resId, asyncContext->param_, asyncContext->param_);
1011         if (state != RState::SUCCESS) {
1012             asyncContext->SetErrorMsg("GetPluralString failed", true, state);
1013             return;
1014         }
1015     } else {
1016         state = asyncContext->addon_->GetResMgr()->GetPluralStringByNameFormat(asyncContext->value_,
1017             asyncContext->resName_.c_str(), asyncContext->param_, asyncContext->param_);
1018         if (state != RState::SUCCESS) {
1019             asyncContext->SetErrorMsg("GetPluralString failed", false, state);
1020             return;
1021         }
1022     }
1023     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) -> napi_value {
1024         napi_value result;
1025         napi_status status = napi_create_string_utf8(env, context.value_.c_str(), NAPI_AUTO_LENGTH, &result);
1026         if (status != napi_ok) {
1027             context.SetErrorMsg("Failed to create plural string");
1028             return nullptr;
1029         }
1030         return result;
1031     };
1032 };
1033 
ProcessIdNameParam(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)1034 napi_value ResourceManagerAddon::ProcessIdNameParam(napi_env env, napi_callback_info info, const std::string& name,
1035     napi_async_execute_callback execute)
1036 {
1037     GET_PARAMS(env, info, 3);
1038 
1039     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1040     asyncContext->addon_ = getResourceManagerAddon(env, info);
1041     for (size_t i = 0; i < argc; i++) {
1042         napi_valuetype valueType;
1043         napi_typeof(env, argv[i], &valueType);
1044 
1045         if (i == 0 && valueType == napi_number) {
1046             asyncContext->resId_ = GetResId(env, argc, argv);
1047         } else if (i == 0 && valueType == napi_string) {
1048             asyncContext->resName_ = GetResNameOrPath(env, argc, argv);
1049         } else if (i == 0 && valueType == napi_object) {
1050             std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
1051             int32_t retCode = GetResourceObject(env, resourcePtr, argv[0]);
1052             if (retCode != SUCCESS) {
1053                 HiLog::Error(LABEL, "Failed to get native Resource object");
1054                 return nullptr;
1055             }
1056             asyncContext->resource_ = resourcePtr;
1057         } else if (i == 1 && valueType == napi_number) {
1058             napi_get_value_int32(env, argv[i], &asyncContext->param_);
1059         } else if (i == 2 && valueType == napi_function) { // the third callback param
1060             napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef_);
1061             break;
1062         } else {
1063             // self resourcemanager with promise
1064         }
1065     }
1066     napi_value result = ResMgrAsyncContext::getResult(env, asyncContext, name, execute);
1067     return result;
1068 }
1069 
GetPluralStringByName(napi_env env,napi_callback_info info)1070 napi_value ResourceManagerAddon::GetPluralStringByName(napi_env env, napi_callback_info info)
1071 {
1072     if (!isNapiString(env, info)) {
1073         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1074         return nullptr;
1075     }
1076     return ProcessIdNameParam(env, info, "GetPluralStringByName", getPluralCapFunc);
1077 }
1078 
GetPluralString(napi_env env,napi_callback_info info)1079 napi_value ResourceManagerAddon::GetPluralString(napi_env env, napi_callback_info info)
1080 {
1081     return ProcessIdNameParam(env, info, "GetPluralString", getPluralCapFunc);
1082 }
1083 
__anon14aac4541302(napi_env env, void* data) 1084 auto g_getRawFileFunc = [](napi_env env, void* data) {
1085     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
1086     auto rawFile = std::make_unique<ResourceManager::RawFile>();
1087     if (IsLoadHap(asyncContext)) {
1088         asyncContext->addon_->GetResMgr()->GetRawFileFromHap(asyncContext->path_, rawFile);
1089         asyncContext->len_ = static_cast<int>(rawFile->length);
1090         asyncContext->mediaData = std::move(rawFile->buffer);
1091         CreateValue(asyncContext);
1092         return;
1093     }
1094     std::string path;
1095     RState state = asyncContext->addon_->GetResMgr()->GetRawFilePathByName(asyncContext->path_, path);
1096     if (path.empty()) {
1097         asyncContext->SetErrorMsg("GetRawFile path failed", true, state);
1098         return;
1099     }
1100     GetResourcesBufferData(path, *asyncContext);
1101 };
1102 
GetRawFile(napi_env env,napi_callback_info info)1103 napi_value ResourceManagerAddon::GetRawFile(napi_env env, napi_callback_info info)
1104 {
1105     return ProcessOnlyIdParam(env, info, "getRawFile", g_getRawFileFunc);
1106 }
1107 
__anon14aac4541402(napi_env env, void* data) 1108 auto g_getRawFileDescriptorFunc = [](napi_env env, void* data) {
1109     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
1110     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) -> napi_value {
1111         ResourceManager::RawFileDescriptor descriptor;
1112         RState state;
1113         if (IsLoadHap(&context)) {
1114             state = context.addon_->GetResMgr()->GetRawFileDescriptorFromHap(context.path_, descriptor);
1115         } else {
1116             state = context.addon_->GetResMgr()->GetRawFileDescriptor(context.path_, descriptor);
1117         }
1118         if (state != RState::SUCCESS) {
1119             context.SetErrorMsg("GetRawFileDescriptor failed state", true, state);
1120             return nullptr;
1121         }
1122         napi_value result;
1123         napi_status status = napi_create_object(env, &result);
1124         if (status != napi_ok) {
1125             context.SetErrorMsg("Failed to create result");
1126             return result;
1127         }
1128 
1129         napi_value fd;
1130         status = napi_create_int32(env, descriptor.fd, &fd);
1131         if (status != napi_ok) {
1132             context.SetErrorMsg("Failed to create fd");
1133             return result;
1134         }
1135         status = napi_set_named_property(env, result, "fd", fd);
1136         if (status != napi_ok) {
1137             context.SetErrorMsg("Failed to set fd");
1138             return result;
1139         }
1140 
1141         napi_value offset;
1142         status = napi_create_int64(env, descriptor.offset, &offset);
1143         if (status != napi_ok) {
1144             context.SetErrorMsg("Failed to create offset");
1145             return result;
1146         }
1147         status = napi_set_named_property(env, result, "offset", offset);
1148         if (status != napi_ok) {
1149             context.SetErrorMsg("Failed to set offset");
1150             return result;
1151         }
1152 
1153         napi_value length;
1154         status = napi_create_int64(env, descriptor.length, &length);
1155         if (status != napi_ok) {
1156             context.SetErrorMsg("Failed to create length");
1157             return result;
1158         }
1159         status = napi_set_named_property(env, result, "length", length);
1160         if (status != napi_ok) {
1161             context.SetErrorMsg("Failed to set length");
1162             return result;
1163         }
1164         return result;
1165     };
1166 };
1167 
GetRawFileDescriptor(napi_env env,napi_callback_info info)1168 napi_value ResourceManagerAddon::GetRawFileDescriptor(napi_env env, napi_callback_info info)
1169 {
1170     return ProcessOnlyIdParam(env, info, "getRawFileDescriptor", g_getRawFileDescriptorFunc);
1171 }
1172 
__anon14aac4541602(napi_env env, void* data) 1173 auto closeRawFileDescriptorFunc = [](napi_env env, void* data) {
1174     ResMgrAsyncContext *asyncContext = static_cast<ResMgrAsyncContext*>(data);
1175     asyncContext->createValueFunc_ = [](napi_env env, ResMgrAsyncContext& context) -> napi_value {
1176         napi_value undefined;
1177         if (napi_get_undefined(env, &undefined) != napi_ok) {
1178             return nullptr;
1179         }
1180         RState state = context.addon_->GetResMgr()->CloseRawFileDescriptor(context.path_);
1181         if (state != RState::SUCCESS) {
1182             context.SetErrorMsg("CloseRawFileDescriptor failed state", true, state);
1183             return nullptr;
1184         }
1185         return undefined;
1186     };
1187 };
1188 
CloseRawFileDescriptor(napi_env env,napi_callback_info info)1189 napi_value ResourceManagerAddon::CloseRawFileDescriptor(napi_env env, napi_callback_info info)
1190 {
1191     return ProcessOnlyIdParam(env, info, "closeRawFileDescriptor", closeRawFileDescriptorFunc);
1192 }
1193 
GetStringSync(napi_env env,napi_callback_info info)1194 napi_value ResourceManagerAddon::GetStringSync(napi_env env, napi_callback_info info)
1195 {
1196     GET_PARAMS(env, info, 2);
1197 
1198     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1199     asyncContext->addon_ = getResourceManagerAddon(env, info);
1200     if (isNapiNumber(env, info)) {
1201         asyncContext->resId_ = GetResId(env, argc, argv);
1202     } else if (isNapiObject(env, info)) {
1203         std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
1204         int32_t retCode = GetResourceObject(env, resourcePtr, argv[0]);
1205         if (retCode != SUCCESS) {
1206             HiLog::Error(LABEL, "GetStringSync failed to get native Resource object");
1207             ResMgrAsyncContext::NapiThrow(env, retCode);
1208             return nullptr;
1209         }
1210         asyncContext->resource_ = resourcePtr;
1211     } else {
1212         HiLog::Error(LABEL, "GetStringSync type is invalid");
1213         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1214         return nullptr;
1215     }
1216 
1217     std::shared_ptr<ResourceManager> resMgr = nullptr;
1218     int32_t resId = 0;
1219     bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext.get(), resMgr, resId);
1220     if (!ret) {
1221         HiLog::Error(LABEL, "Failed to GetHapResourceManager in GetStringSync");
1222         return nullptr;
1223     }
1224     RState state = resMgr->GetStringById(resId, asyncContext->value_);
1225     if (state != RState::SUCCESS) {
1226         asyncContext->SetErrorMsg("GetStringSync failed state", true);
1227         ResMgrAsyncContext::NapiThrow(env, state);
1228         return nullptr;
1229     }
1230 
1231     napi_value jsValue = nullptr;
1232     if (napi_create_string_utf8(env, asyncContext->value_.c_str(), NAPI_AUTO_LENGTH, &jsValue) != napi_ok) {
1233         asyncContext->SetErrorMsg("Failed to create jsValue");
1234     }
1235     return jsValue;
1236 }
1237 
GetStringByNameSync(napi_env env,napi_callback_info info)1238 napi_value ResourceManagerAddon::GetStringByNameSync(napi_env env, napi_callback_info info)
1239 {
1240     GET_PARAMS(env, info, 2);
1241 
1242     if (!isNapiString(env, info)) {
1243         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1244         return nullptr;
1245     }
1246 
1247     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1248     asyncContext->addon_ = getResourceManagerAddon(env, info);
1249     asyncContext->resName_ = GetResNameOrPath(env, argc, argv);
1250 
1251     RState state = asyncContext->addon_->GetResMgr()->GetStringByName(asyncContext->resName_.c_str(),
1252         asyncContext->value_);
1253     if (state != RState::SUCCESS) {
1254         asyncContext->SetErrorMsg("GetStringByNameSync failed state", true);
1255         ResMgrAsyncContext::NapiThrow(env, state);
1256         return nullptr;
1257     }
1258 
1259     napi_value jsValue = nullptr;
1260     if (napi_create_string_utf8(env, asyncContext->value_.c_str(), NAPI_AUTO_LENGTH, &jsValue) != napi_ok) {
1261         asyncContext->SetErrorMsg("Failed to create jsValue");
1262     }
1263     return jsValue;
1264 }
1265 
GetBoolean(napi_env env,napi_callback_info info)1266 napi_value ResourceManagerAddon::GetBoolean(napi_env env, napi_callback_info info)
1267 {
1268     GET_PARAMS(env, info, 2);
1269 
1270     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1271     asyncContext->addon_ = getResourceManagerAddon(env, info);
1272     if (isNapiNumber(env, info)) {
1273         asyncContext->resId_ = GetResId(env, argc, argv);
1274     } else if (isNapiObject(env, info)) {
1275         std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
1276         int32_t retCode = GetResourceObject(env, resourcePtr, argv[0]);
1277         if (retCode != SUCCESS) {
1278             HiLog::Error(LABEL, "GetBoolean failed to get native Resource object");
1279             ResMgrAsyncContext::NapiThrow(env, retCode);
1280             return nullptr;
1281         }
1282         asyncContext->resource_ = resourcePtr;
1283     } else {
1284         HiLog::Error(LABEL, "GetBoolean type is invalid");
1285         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1286         return nullptr;
1287     }
1288     std::shared_ptr<ResourceManager> resMgr = nullptr;
1289     int32_t resId = 0;
1290     bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext.get(), resMgr, resId);
1291     if (!ret) {
1292         HiLog::Error(LABEL, "Failed to GetHapResourceManager in GetBoolean");
1293         return nullptr;
1294     }
1295     RState state = resMgr->GetBooleanById(resId, asyncContext->bValue_);
1296     if (state != RState::SUCCESS) {
1297         asyncContext->SetErrorMsg("GetBoolean failed state", true);
1298         ResMgrAsyncContext::NapiThrow(env, state);
1299         return nullptr;
1300     }
1301 
1302     napi_value jsValue = nullptr;
1303     if (napi_get_boolean(env, asyncContext->bValue_, &jsValue) != napi_ok) {
1304         asyncContext->SetErrorMsg("Failed to create result", true);
1305     }
1306     return jsValue;
1307 }
1308 
GetBooleanByName(napi_env env,napi_callback_info info)1309 napi_value ResourceManagerAddon::GetBooleanByName(napi_env env, napi_callback_info info)
1310 {
1311     GET_PARAMS(env, info, 2);
1312 
1313     if (!isNapiString(env, info)) {
1314         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1315         return nullptr;
1316     }
1317 
1318     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1319     asyncContext->addon_ = getResourceManagerAddon(env, info);
1320     asyncContext->resName_ = GetResNameOrPath(env, argc, argv);
1321 
1322     RState state = asyncContext->addon_->GetResMgr()->GetBooleanByName(asyncContext->resName_.c_str(),
1323         asyncContext->bValue_);
1324     if (state != RState::SUCCESS) {
1325         asyncContext->SetErrorMsg("GetBooleanByName failed state", true);
1326         ResMgrAsyncContext::NapiThrow(env, state);
1327         return nullptr;
1328     }
1329 
1330     napi_value jsValue = nullptr;
1331     if (napi_get_boolean(env, asyncContext->bValue_, &jsValue) != napi_ok) {
1332         asyncContext->SetErrorMsg("Failed to create result", true);
1333     }
1334     return jsValue;
1335 }
1336 
GetNumber(napi_env env,napi_callback_info info)1337 napi_value ResourceManagerAddon::GetNumber(napi_env env, napi_callback_info info)
1338 {
1339     GET_PARAMS(env, info, 2);
1340 
1341     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1342     asyncContext->addon_ = getResourceManagerAddon(env, info);
1343     if (isNapiNumber(env, info)) {
1344         asyncContext->resId_ = GetResId(env, argc, argv);
1345     } else if (isNapiObject(env, info)) {
1346         std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
1347         int32_t retCode = GetResourceObject(env, resourcePtr, argv[0]);
1348         if (retCode != SUCCESS) {
1349             HiLog::Error(LABEL, "GetNumber failed to get native Resource object");
1350             ResMgrAsyncContext::NapiThrow(env, retCode);
1351             return nullptr;
1352         }
1353         asyncContext->resource_ = resourcePtr;
1354     } else {
1355         HiLog::Error(LABEL, "GetNumber type is invalid");
1356         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1357         return nullptr;
1358     }
1359     std::shared_ptr<ResourceManager> resMgr = nullptr;
1360     int32_t resId = 0;
1361     bool ret = ResMgrAsyncContext::GetHapResourceManager(asyncContext.get(), resMgr, resId);
1362     if (!ret) {
1363         HiLog::Error(LABEL, "Failed to GetHapResourceManager in GetNumber");
1364         return nullptr;
1365     }
1366 
1367     RState state = resMgr->GetIntegerById(resId, asyncContext->iValue_);
1368     napi_value jsValue = nullptr;
1369     if (state == RState::SUCCESS) {
1370         if (napi_create_int32(env, asyncContext->iValue_, &jsValue) != napi_ok) {
1371             asyncContext->SetErrorMsg("GetIntegerById failed to create result", true);
1372         }
1373     } else {
1374         state = asyncContext->addon_->GetResMgr()->GetFloatById(resId,
1375         asyncContext->fValue_);
1376         if (state != RState::SUCCESS) {
1377             asyncContext->SetErrorMsg("GetFloatById failed state", true);
1378             ResMgrAsyncContext::NapiThrow(env, state);
1379             return nullptr;
1380         }
1381         if (napi_create_double(env, asyncContext->fValue_, &jsValue) != napi_ok) {
1382             asyncContext->SetErrorMsg("GetFloatById failed to create result", true);
1383         }
1384     }
1385     return jsValue;
1386 }
1387 
GetNumberByName(napi_env env,napi_callback_info info)1388 napi_value ResourceManagerAddon::GetNumberByName(napi_env env, napi_callback_info info)
1389 {
1390     GET_PARAMS(env, info, 2);
1391 
1392     if (!isNapiString(env, info)) {
1393         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1394         return nullptr;
1395     }
1396 
1397     std::unique_ptr<ResMgrAsyncContext> asyncContext = std::make_unique<ResMgrAsyncContext>();
1398     asyncContext->addon_ = getResourceManagerAddon(env, info);
1399     asyncContext->resName_ = GetResNameOrPath(env, argc, argv);
1400 
1401     RState state;
1402     napi_value jsValue = nullptr;
1403     state = asyncContext->addon_->GetResMgr()->GetIntegerByName(asyncContext->resName_.c_str(),
1404         asyncContext->iValue_);
1405     if (state == RState::SUCCESS) {
1406         if (napi_create_int32(env, asyncContext->iValue_, &jsValue) != napi_ok) {
1407             asyncContext->SetErrorMsg("GetIntegerByName failed to create result", true);
1408         }
1409     } else {
1410         state = asyncContext->addon_->GetResMgr()->GetFloatByName(asyncContext->resName_.c_str(),
1411         asyncContext->fValue_);
1412         if (state != RState::SUCCESS) {
1413             asyncContext->SetErrorMsg("GetFloatByName failed state", false);
1414             ResMgrAsyncContext::NapiThrow(env, state);
1415             return nullptr;
1416         }
1417         if (napi_create_double(env, asyncContext->fValue_, &jsValue) != napi_ok) {
1418             asyncContext->SetErrorMsg("GetFloatByName failed to create result", true);
1419         }
1420     }
1421     return jsValue;
1422 }
1423 
GetType(napi_env env,napi_value value)1424 napi_valuetype ResourceManagerAddon::GetType(napi_env env, napi_value value)
1425 {
1426     napi_valuetype valueType = napi_valuetype::napi_undefined;
1427     napi_typeof(env, value, &valueType);
1428     return valueType;
1429 }
1430 
GetResourceObjectName(napi_env env,std::shared_ptr<ResourceManager::Resource> & resourcePtr,napi_value & value,int32_t type)1431 bool ResourceManagerAddon::GetResourceObjectName(napi_env env, std::shared_ptr<ResourceManager::Resource> &resourcePtr,
1432     napi_value &value, int32_t type)
1433 {
1434     std::string typeName("moduleName");
1435     if (type == 0) {
1436         typeName = std::string("bundleName");
1437     }
1438     napi_value name;
1439     napi_status status = napi_get_named_property(env, value, typeName.c_str(), &name);
1440     if (status != napi_ok || name == nullptr) {
1441         HiLog::Error(LABEL, "Failed to get resource name property");
1442         return false;
1443     }
1444     if (GetType(env, name) != napi_string) {
1445         HiLog::Error(LABEL, "Failed to get resource name string");
1446         return false;
1447     }
1448     size_t len = 0;
1449     status = napi_get_value_string_utf8(env, name, nullptr, 0, &len);
1450     if (status != napi_ok || len <= 0) {
1451         HiLog::Error(LABEL, "Failed to get resource len");
1452         return false;
1453     }
1454     std::vector<char> buf(len + 1);
1455     status = napi_get_value_string_utf8(env, name, buf.data(), len + 1, &len);
1456     if (status != napi_ok) {
1457         HiLog::Error(LABEL, "Failed to get resource name value");
1458         return false;
1459     }
1460     if (type == 0) {
1461         resourcePtr->bundleName = buf.data();
1462     } else {
1463         resourcePtr->moduleName = buf.data();
1464     }
1465     return true;
1466 }
1467 
GetResourceObjectId(napi_env env,std::shared_ptr<ResourceManager::Resource> & resourcePtr,napi_value & value)1468 bool ResourceManagerAddon::GetResourceObjectId(napi_env env, std::shared_ptr<ResourceManager::Resource> &resourcePtr,
1469     napi_value &value)
1470 {
1471     napi_value id;
1472     napi_status status = napi_get_named_property(env, value, "id", &id);
1473     if (status != napi_ok || id == nullptr) {
1474         HiLog::Error(LABEL, "Failed to get resource id property");
1475         return false;
1476     }
1477     if (GetType(env, id) != napi_number) {
1478         HiLog::Error(LABEL, "Failed to get resource id number");
1479         return false;
1480     }
1481     int32_t resId = 0;
1482     status = napi_get_value_int32(env, id, &resId);
1483     if (status != napi_ok) {
1484         HiLog::Error(LABEL, "Failed to get resource id value");
1485         return false;
1486     }
1487     resourcePtr->id = resId;
1488     return true;
1489 }
1490 
GetResourceObject(napi_env env,std::shared_ptr<ResourceManager::Resource> & resourcePtr,napi_value & value)1491 int32_t ResourceManagerAddon::GetResourceObject(napi_env env, std::shared_ptr<ResourceManager::Resource> &resourcePtr,
1492     napi_value &value)
1493 {
1494     if (resourcePtr == nullptr) {
1495         HiLog::Error(LABEL, "resourcePtr == nullptr");
1496         return ERROR;
1497     }
1498     if (!GetResourceObjectName(env, resourcePtr, value, 0)) {
1499         HiLog::Error(LABEL, "Failed to get bundleName");
1500         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1501     }
1502     if (!GetResourceObjectName(env, resourcePtr, value, 1)) {
1503         HiLog::Error(LABEL, "Failed to get moduleName");
1504         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1505     }
1506     if (!GetResourceObjectId(env, resourcePtr, value)) {
1507         HiLog::Error(LABEL, "Failed to get id");
1508         return ERROR_CODE_INVALID_INPUT_PARAMETER;
1509     }
1510 
1511     return SUCCESS;
1512 }
1513 
GetHapResourceManager(const ResMgrAsyncContext * asyncContext,std::shared_ptr<ResourceManager> & resMgr,int32_t & resId)1514 bool ResMgrAsyncContext::GetHapResourceManager(const ResMgrAsyncContext* asyncContext,
1515     std::shared_ptr<ResourceManager> &resMgr, int32_t &resId)
1516 {
1517     std::shared_ptr<ResourceManager::Resource> resource = asyncContext->resource_;
1518     if (resource == nullptr) {
1519         resMgr = asyncContext->addon_->GetResMgr();
1520         resId = asyncContext->resId_;
1521         return true;
1522     }
1523 
1524     resId = resource->id;
1525     std::string key(resource->bundleName + "/" + resource->moduleName);
1526     std::lock_guard<std::mutex> lock(g_resMapLock);
1527     auto iter = g_resourceMgr.find(key);
1528     if (iter != g_resourceMgr.end()) {
1529         resMgr = g_resourceMgr[key];
1530         return true;
1531     }
1532     auto context = asyncContext->addon_->GetContext();
1533     if (context == nullptr) {
1534         HiLog::Error(LABEL, "GetHapResourceManager context == nullptr");
1535         return false;
1536     }
1537     auto moduleContext = context->CreateModuleContext(resource->bundleName, resource->moduleName);
1538     if (moduleContext == nullptr) {
1539         HiLog::Error(LABEL, "GetHapResourceManager moduleContext == nullptr");
1540         return false;
1541     }
1542     resMgr = moduleContext->GetResourceManager();
1543     g_resourceMgr[key] = resMgr;
1544     return true;
1545 }
1546 
getResult(napi_env env,std::unique_ptr<ResMgrAsyncContext> & asyncContext,const std::string & name,napi_async_execute_callback & execute)1547 napi_value ResMgrAsyncContext::getResult(napi_env env, std::unique_ptr<ResMgrAsyncContext> &asyncContext,
1548     const std::string &name, napi_async_execute_callback &execute)
1549 {
1550     napi_value result = nullptr;
1551     if (asyncContext->callbackRef_ == nullptr) {
1552         napi_create_promise(env, &asyncContext->deferred_, &result);
1553     } else {
1554         napi_get_undefined(env, &result);
1555     }
1556     napi_value resource = nullptr;
1557     napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
1558     if (napi_create_async_work(env, nullptr, resource, execute, ResMgrAsyncContext::Complete,
1559         static_cast<void*>(asyncContext.get()), &asyncContext->work_) != napi_ok) {
1560         HiLog::Error(LABEL, "Failed to create async work for %{public}s", name.c_str());
1561         return result;
1562     }
1563     if (napi_queue_async_work(env, asyncContext->work_) != napi_ok) {
1564         HiLog::Error(LABEL, "Failed to queue async work for %{public}s", name.c_str());
1565         return result;
1566     }
1567     asyncContext.release();
1568     return result;
1569 }
1570 
GetStringValue(napi_env env,napi_callback_info info)1571 napi_value ResourceManagerAddon::GetStringValue(napi_env env, napi_callback_info info)
1572 {
1573     if (isNapiNumber(env, info)) {
1574         return ProcessIdParamV9(env, info, "getStringValue", getStringFunc);
1575     } else if (isNapiObject(env, info)) {
1576         return ProcessResourceParamV9(env, info, "getStringValue", getStringFunc);
1577     } else {
1578         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1579         return nullptr;
1580     }
1581 }
1582 
GetStringArrayValue(napi_env env,napi_callback_info info)1583 napi_value ResourceManagerAddon::GetStringArrayValue(napi_env env, napi_callback_info info)
1584 {
1585     if (isNapiNumber(env, info)) {
1586         return ProcessIdParamV9(env, info, "getStringArrayValue", getStringArrayFunc);
1587     } else if (isNapiObject(env, info)) {
1588         return ProcessResourceParamV9(env, info, "getStringArrayValue", getStringArrayFunc);
1589     } else {
1590         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1591         return nullptr;
1592     }
1593 }
1594 
GetMediaContent(napi_env env,napi_callback_info info)1595 napi_value ResourceManagerAddon::GetMediaContent(napi_env env, napi_callback_info info)
1596 {
1597     if (isNapiNumber(env, info)) {
1598         return ProcessIdParamV9(env, info, "getMediaContent", getMediaFunc);
1599     } else if (isNapiObject(env, info)) {
1600         return ProcessResourceParamV9(env, info, "getMediaContent", getMediaFunc);
1601     } else {
1602         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1603         return nullptr;
1604     }
1605 }
1606 
GetMediaContentBase64(napi_env env,napi_callback_info info)1607 napi_value ResourceManagerAddon::GetMediaContentBase64(napi_env env, napi_callback_info info)
1608 {
1609     if (isNapiNumber(env, info)) {
1610         return ProcessIdParamV9(env, info, "getMediaContentBase64", getMediaBase64Func);
1611     } else if (isNapiObject(env, info)) {
1612         return ProcessResourceParamV9(env, info, "getMediaContentBase64", getMediaBase64Func);
1613     } else {
1614         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1615         return nullptr;
1616     }
1617 }
1618 
GetPluralStringValue(napi_env env,napi_callback_info info)1619 napi_value ResourceManagerAddon::GetPluralStringValue(napi_env env, napi_callback_info info)
1620 {
1621     if (isNapiString(env, info)) {
1622         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1623         return nullptr;
1624     }
1625     return ProcessIdNameParam(env, info, "getPluralStringValue", getPluralCapFunc);
1626 }
1627 
GetRawFileContent(napi_env env,napi_callback_info info)1628 napi_value ResourceManagerAddon::GetRawFileContent(napi_env env, napi_callback_info info)
1629 {
1630     if (!isNapiString(env, info)) {
1631         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1632         return nullptr;
1633     }
1634     return ProcessOnlyIdParam(env, info, "getRawFileContent", g_getRawFileFunc);
1635 }
1636 
GetRawFd(napi_env env,napi_callback_info info)1637 napi_value ResourceManagerAddon::GetRawFd(napi_env env, napi_callback_info info)
1638 {
1639     if (!isNapiString(env, info)) {
1640         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1641         return nullptr;
1642     }
1643     return ProcessOnlyIdParam(env, info, "getRawFd", g_getRawFileDescriptorFunc);
1644 }
1645 
CloseRawFd(napi_env env,napi_callback_info info)1646 napi_value ResourceManagerAddon::CloseRawFd(napi_env env, napi_callback_info info)
1647 {
1648     if (!isNapiString(env, info)) {
1649         ResMgrAsyncContext::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
1650         return nullptr;
1651     }
1652     return ProcessOnlyIdParam(env, info, "closeRawFd", closeRawFileDescriptorFunc);
1653 }
1654 
1655 } // namespace Resource
1656 } // namespace Global
1657 } // namespace OHOS