1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "resource_manager_napi_async_impl.h"
17 
18 #include <unordered_map>
19 #include "hisysevent_adapter.h"
20 #include "hitrace_meter.h"
21 #include "resource_manager_napi_utils.h"
22 namespace OHOS {
23 namespace Global {
24 namespace Resource {
25 using namespace std::placeholders;
26 constexpr int PARAMS_NUM_TWO = 2;
27 constexpr int PARAMS_NUM_THREE = 3;
ResourceManagerNapiAsyncImpl()28 ResourceManagerNapiAsyncImpl::ResourceManagerNapiAsyncImpl()
29 {}
30 
~ResourceManagerNapiAsyncImpl()31 ResourceManagerNapiAsyncImpl::~ResourceManagerNapiAsyncImpl()
32 {}
33 
34 std::unordered_map<std::string, std::function<napi_value(napi_env&, napi_callback_info&)>>
35     ResourceManagerNapiAsyncImpl::asyncFuncMatch {
36     /*===========================since 6 but deprecated since 9======================================*/
__anon06571fda0102(napi_env& env, napi_callback_info& info) 37     {"GetString", [](napi_env& env, napi_callback_info& info) -> napi_value {
38         return ResourceManagerNapiAsyncImpl::GetString(env, info);}},
__anon06571fda0202(napi_env& env, napi_callback_info& info) 39     {"GetStringArray", [](napi_env& env, napi_callback_info& info) -> napi_value {
40         return ResourceManagerNapiAsyncImpl::GetStringArray(env, info);}},
__anon06571fda0302(napi_env& env, napi_callback_info& info) 41     {"GetMedia", [](napi_env& env, napi_callback_info& info) -> napi_value {
42         return ResourceManagerNapiAsyncImpl::GetMedia(env, info);}},
__anon06571fda0402(napi_env& env, napi_callback_info& info) 43     {"GetMediaBase64", [](napi_env& env, napi_callback_info& info) -> napi_value {
44         return ResourceManagerNapiAsyncImpl::GetMediaBase64(env, info);}},
__anon06571fda0502(napi_env& env, napi_callback_info& info) 45     {"GetPluralString", [](napi_env& env, napi_callback_info& info) -> napi_value {
46         return ResourceManagerNapiAsyncImpl::GetPluralString(env, info);}},
__anon06571fda0602(napi_env& env, napi_callback_info& info) 47     {"GetRawFile", [](napi_env& env, napi_callback_info& info) -> napi_value {
48         return ResourceManagerNapiAsyncImpl::GetRawFile(env, info);}},
__anon06571fda0702(napi_env& env, napi_callback_info& info) 49     {"GetRawFileDescriptor", [](napi_env& env, napi_callback_info& info) -> napi_value {
50         return ResourceManagerNapiAsyncImpl::GetRawFileDescriptor(env, info);}},
__anon06571fda0802(napi_env& env, napi_callback_info& info) 51     {"CloseRawFileDescriptor", [](napi_env& env, napi_callback_info& info) -> napi_value {
52         return ResourceManagerNapiAsyncImpl::CloseRawFileDescriptor(env, info);}},
53     /*===========================since 6 but deprecated since 9========================================*/
54 
__anon06571fda0902(napi_env& env, napi_callback_info& info) 55     {"GetStringValue", [](napi_env& env, napi_callback_info& info) -> napi_value {
56         return ResourceManagerNapiAsyncImpl::GetStringValue(env, info);}},
__anon06571fda0a02(napi_env& env, napi_callback_info& info) 57     {"GetStringArrayValue", [](napi_env& env, napi_callback_info& info) -> napi_value {
58         return ResourceManagerNapiAsyncImpl::GetStringArrayValue(env, info);}},
__anon06571fda0b02(napi_env& env, napi_callback_info& info) 59     {"GetMediaContent", [](napi_env& env, napi_callback_info& info) -> napi_value {
60         return ResourceManagerNapiAsyncImpl::GetMediaContent(env, info);}},
__anon06571fda0c02(napi_env& env, napi_callback_info& info) 61     {"GetMediaContentBase64", [](napi_env& env, napi_callback_info& info) -> napi_value {
62         return ResourceManagerNapiAsyncImpl::GetMediaContentBase64(env, info);}},
__anon06571fda0d02(napi_env& env, napi_callback_info& info) 63     {"GetPluralStringValue", [](napi_env& env, napi_callback_info& info) -> napi_value {
64         return ResourceManagerNapiAsyncImpl::GetPluralStringValue(env, info);}},
__anon06571fda0e02(napi_env& env, napi_callback_info& info) 65     {"GetRawFileContent", [](napi_env& env, napi_callback_info& info) -> napi_value {
66         return ResourceManagerNapiAsyncImpl::GetRawFileContent(env, info);}},
__anon06571fda0f02(napi_env& env, napi_callback_info& info) 67     {"GetRawFd", [](napi_env& env, napi_callback_info& info) -> napi_value {
68         return ResourceManagerNapiAsyncImpl::GetRawFd(env, info);}},
__anon06571fda1002(napi_env& env, napi_callback_info& info) 69     {"CloseRawFd", [](napi_env& env, napi_callback_info& info) -> napi_value {
70         return ResourceManagerNapiAsyncImpl::CloseRawFd(env, info);}},
__anon06571fda1102(napi_env& env, napi_callback_info& info) 71     {"GetConfiguration", [](napi_env& env, napi_callback_info& info) -> napi_value {
72         return ResourceManagerNapiAsyncImpl::GetConfiguration(env, info);}},
__anon06571fda1202(napi_env& env, napi_callback_info& info) 73     {"GetDeviceCapability", [](napi_env& env, napi_callback_info& info) -> napi_value {
74         return ResourceManagerNapiAsyncImpl::GetDeviceCapability(env, info);}},
__anon06571fda1302(napi_env& env, napi_callback_info& info) 75     {"GetStringByName", [](napi_env& env, napi_callback_info& info) -> napi_value {
76         return ResourceManagerNapiAsyncImpl::GetStringByName(env, info);}},
__anon06571fda1402(napi_env& env, napi_callback_info& info) 77     {"GetStringArrayByName", [](napi_env& env, napi_callback_info& info) -> napi_value {
78         return ResourceManagerNapiAsyncImpl::GetStringArrayByName(env, info);}},
__anon06571fda1502(napi_env& env, napi_callback_info& info) 79     {"GetMediaByName", [](napi_env& env, napi_callback_info& info) -> napi_value {
80         return ResourceManagerNapiAsyncImpl::GetMediaByName(env, info);}},
__anon06571fda1602(napi_env& env, napi_callback_info& info) 81     {"GetMediaBase64ByName", [](napi_env& env, napi_callback_info& info) -> napi_value {
82         return ResourceManagerNapiAsyncImpl::GetMediaBase64ByName(env, info);}},
__anon06571fda1702(napi_env& env, napi_callback_info& info) 83     {"GetPluralStringByName", [](napi_env& env, napi_callback_info& info) -> napi_value {
84         return ResourceManagerNapiAsyncImpl::GetPluralStringByName(env, info);}},
__anon06571fda1802(napi_env& env, napi_callback_info& info) 85     {"GetRawFileList", [](napi_env& env, napi_callback_info& info) -> napi_value {
86         return ResourceManagerNapiAsyncImpl::GetRawFileList(env, info);}},
__anon06571fda1902(napi_env& env, napi_callback_info& info) 87     {"GetColor", [](napi_env& env, napi_callback_info& info) -> napi_value {
88         return ResourceManagerNapiAsyncImpl::GetColor(env, info);}},
__anon06571fda1a02(napi_env& env, napi_callback_info& info) 89     {"GetColorByName", [](napi_env& env, napi_callback_info& info) -> napi_value {
90         return ResourceManagerNapiAsyncImpl::GetColorByName(env, info);}},
91 };
92 
GetResource(napi_env env,napi_callback_info info,const std::string & functionName)93 napi_value ResourceManagerNapiAsyncImpl::GetResource(napi_env env, napi_callback_info info,
94     const std::string &functionName)
95 {
96     auto functionIndex = asyncFuncMatch.find(functionName);
97     if (functionIndex == asyncFuncMatch.end()) {
98         RESMGR_HILOGI(RESMGR_JS_TAG, "Invalid functionName, %{public}s", functionName.c_str());
99         return nullptr;
100     }
101     return functionIndex->second(env, info);
102 }
103 
GetCallbackErrorCode(napi_env env,const int32_t errCode,const std::string & errMsg)104 napi_value GetCallbackErrorCode(napi_env env, const int32_t errCode, const std::string &errMsg)
105 {
106     napi_value error = nullptr;
107     napi_value eCode = nullptr;
108     napi_value eMsg = nullptr;
109     napi_create_int32(env, errCode, &eCode);
110     napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &eMsg);
111     napi_create_object(env, &error);
112     napi_set_named_property(env, error, "code", eCode);
113     napi_set_named_property(env, error, "message", eMsg);
114     return error;
115 }
116 
Complete(napi_env env,napi_status status,void * data)117 void ResourceManagerNapiAsyncImpl::Complete(napi_env env, napi_status status, void* data)
118 {
119     ResMgrDataContext* dataContext = static_cast<ResMgrDataContext*>(data);
120 
121     napi_value finalResult = nullptr;
122     if (dataContext->createValueFunc_ != nullptr) {
123         finalResult = dataContext->createValueFunc_(env, *dataContext);
124     }
125 
126     napi_value result[] = { nullptr, nullptr };
127     if (dataContext->success_) {
128         napi_get_undefined(env, &result[0]);
129         result[1] = finalResult;
130     } else {
131         result[0] = GetCallbackErrorCode(env, dataContext->errCode_, dataContext->errMsg_.c_str());
132         napi_get_undefined(env, &result[1]);
133     }
134     if (dataContext->deferred_) {
135         if (dataContext->success_) {
136             if (napi_resolve_deferred(env, dataContext->deferred_, result[1]) != napi_ok) {
137                 RESMGR_HILOGE(RESMGR_JS_TAG, "napi_resolve_deferred failed");
138             }
139         } else {
140             result[0] = GetCallbackErrorCode(env, dataContext->errCode_, dataContext->errMsg_.c_str());
141             if (napi_reject_deferred(env, dataContext->deferred_, result[0]) != napi_ok) {
142                 RESMGR_HILOGE(RESMGR_JS_TAG, "napi_reject_deferred failed");
143             }
144         }
145     } else {
146         do {
147             napi_value callback = nullptr;
148             napi_status status = napi_get_reference_value(env, dataContext->callbackRef_, &callback);
149             if (status != napi_ok) {
150                 RESMGR_HILOGE(RESMGR_JS_TAG, "napi_get_reference_value failed status=%{public}d", status);
151                 break;
152             }
153             napi_value userRet = nullptr;
154             status = napi_call_function(env, nullptr, callback, sizeof(result) / sizeof(napi_value), result, &userRet);
155             if (status != napi_ok) {
156                 RESMGR_HILOGE(RESMGR_JS_TAG, "napi_call_function failed status=%{public}d", status);
157                 break;
158             }
159             status = napi_delete_reference(env, dataContext->callbackRef_);
160             if (status != napi_ok) {
161                 RESMGR_HILOGE(RESMGR_JS_TAG, "napi_call_function failed status=%{public}d", status);
162                 break;
163             }
164         } while (false);
165     }
166     napi_delete_async_work(env, dataContext->work_);
167     delete dataContext;
168 };
169 
GetResult(napi_env env,std::unique_ptr<ResMgrDataContext> & dataContext,const std::string & name,napi_async_execute_callback & execute)170 napi_value ResourceManagerNapiAsyncImpl::GetResult(napi_env env, std::unique_ptr<ResMgrDataContext> &dataContext,
171     const std::string &name, napi_async_execute_callback &execute)
172 {
173     napi_value result = nullptr;
174     if (dataContext->callbackRef_ == nullptr) {
175         napi_create_promise(env, &dataContext->deferred_, &result);
176     } else {
177         napi_get_undefined(env, &result);
178     }
179     napi_value resource = nullptr;
180     napi_create_string_utf8(env, name.c_str(), NAPI_AUTO_LENGTH, &resource);
181     if (napi_create_async_work(env, nullptr, resource, execute, ResourceManagerNapiAsyncImpl::Complete,
182         static_cast<void*>(dataContext.get()), &dataContext->work_) != napi_ok) {
183         RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to create async work for %{public}s", name.c_str());
184         return result;
185     }
186     if (napi_queue_async_work_with_qos(env, dataContext->work_, napi_qos_user_initiated) != napi_ok) {
187         RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to queue async work for %{public}s", name.c_str());
188         return result;
189     }
190     dataContext.release();
191     return result;
192 }
193 
ProcessNameParamV9(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)194 napi_value ResourceManagerNapiAsyncImpl::ProcessNameParamV9(napi_env env, napi_callback_info info,
195     const std::string &name, napi_async_execute_callback execute)
196 {
197     GET_PARAMS(env, info, PARAMS_NUM_THREE);
198     std::unique_ptr<ResMgrDataContext> dataContext = std::make_unique<ResMgrDataContext>();
199     dataContext->addon_ = ResMgrDataContext::GetResourceManagerAddon(env, info);
200     if (dataContext->addon_ == nullptr) {
201         return nullptr;
202     }
203     for (size_t i = 0; i < argc; i++) {
204         napi_valuetype valueType;
205         napi_typeof(env, argv[i], &valueType);
206         if (i == 0 && valueType == napi_string) {
207             dataContext->resName_ = ResourceManagerNapiUtils::GetResNameOrPath(env, argc, argv);
208         } else if (i == 1 && valueType == napi_function) {
209             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
210             break;
211         } else if (i == 1 && ResourceManagerNapiUtils::GetDataType(env, argv[i], dataContext->density_) != SUCCESS) {
212             ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
213             return nullptr;
214         } else if (i == 2 && valueType == napi_function) { // the third callback param
215             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
216             break;
217         } else {
218             // self resourcemanager with promise
219         }
220     }
221 
222     napi_value result = GetResult(env, dataContext, name, execute);
223     return result;
224 }
225 
ProcessIdParamV9(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)226 napi_value ResourceManagerNapiAsyncImpl::ProcessIdParamV9(napi_env env, napi_callback_info info,
227     const std::string &name, napi_async_execute_callback execute)
228 {
229     GET_PARAMS(env, info, PARAMS_NUM_THREE);
230 
231     std::unique_ptr<ResMgrDataContext> dataContext = std::make_unique<ResMgrDataContext>();
232     dataContext->addon_ = ResMgrDataContext::GetResourceManagerAddon(env, info);
233     if (dataContext->addon_ == nullptr) {
234         return nullptr;
235     }
236     for (size_t i = 0; i < argc; i++) {
237         napi_valuetype valueType;
238         napi_typeof(env, argv[i], &valueType);
239         if (i == 0 && valueType == napi_number) {
240             dataContext->resId_ = ResourceManagerNapiUtils::GetResId(env, argc, argv);
241         } else if (i == 1 && valueType == napi_function) {
242             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
243             break;
244         } else if (i == 1 && ResourceManagerNapiUtils::GetDataType(env, argv[i], dataContext->density_) != SUCCESS) {
245             ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
246             return nullptr;
247         } else if (i == 2 && valueType == napi_function) { // the third callback param
248             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
249             break;
250         } else {
251             // self resourcemanager with promise
252         }
253     }
254 
255     napi_value result = GetResult(env, dataContext, name, execute);
256     return result;
257 }
258 
ProcessResourceParamV9(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)259 napi_value ResourceManagerNapiAsyncImpl::ProcessResourceParamV9(napi_env env, napi_callback_info info,
260     const std::string &name, napi_async_execute_callback execute)
261 {
262     GET_PARAMS(env, info, PARAMS_NUM_THREE);
263 
264     std::unique_ptr<ResMgrDataContext> dataContext = std::make_unique<ResMgrDataContext>();
265     dataContext->addon_ = ResMgrDataContext::GetResourceManagerAddon(env, info);
266     if (dataContext->addon_ == nullptr) {
267         return nullptr;
268     }
269     for (size_t i = 0; i < argc; i++) {
270         napi_valuetype valueType;
271         napi_typeof(env, argv[i], &valueType);
272         if (i == 0 && valueType == napi_object) {
273             auto resourcePtr = std::make_shared<ResourceManager::Resource>();
274             int32_t retCode = ResourceManagerNapiUtils::GetResourceObject(env, resourcePtr, argv[0]);
275             if (retCode != SUCCESS) {
276                 RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to get native Resource object");
277                 ResourceManagerNapiUtils::NapiThrow(env, retCode);
278                 return nullptr;
279             }
280             dataContext->resource_ = resourcePtr;
281         } else if (i == 1 && valueType == napi_function) {
282             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
283             break;
284         } else if (i == 1 && ResourceManagerNapiUtils::GetDataType(env, argv[i], dataContext->density_) != SUCCESS) {
285             ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
286             return nullptr;
287         } else if (i == 2 && valueType == napi_function) { // the third callback param
288             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
289             break;
290         } else {
291             // self resourcemanager with promise
292         }
293     }
294 
295     napi_value result = GetResult(env, dataContext, name, execute);
296     return result;
297 }
298 
ProcessOnlyIdParam(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)299 napi_value ResourceManagerNapiAsyncImpl::ProcessOnlyIdParam(napi_env env, napi_callback_info info,
300     const std::string &name, napi_async_execute_callback execute)
301 {
302     GET_PARAMS(env, info, PARAMS_NUM_TWO);
303 
304     std::unique_ptr<ResMgrDataContext> dataContext = std::make_unique<ResMgrDataContext>();
305     dataContext->addon_ = ResMgrDataContext::GetResourceManagerAddon(env, info);
306     if (dataContext->addon_ == nullptr) {
307         return nullptr;
308     }
309     for (size_t i = 0; i < argc; i++) {
310         napi_valuetype valueType;
311         napi_typeof(env, argv[i], &valueType);
312         if (i == 0 && valueType == napi_number) {
313             dataContext->resId_ = ResourceManagerNapiUtils::GetResId(env, argc, argv);
314         } else if (i == 1 && valueType == napi_function) {
315             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
316             break;
317         } else if (i == 0 && valueType == napi_string) {
318             dataContext->path_ = ResourceManagerNapiUtils::GetResNameOrPath(env, argc, argv);
319         } else if (i == 0 && valueType == napi_object) {
320             std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
321             int32_t retCode = ResourceManagerNapiUtils::GetResourceObject(env, resourcePtr, argv[0]);
322             if (retCode != SUCCESS) {
323                 ResourceManagerNapiUtils::NapiThrow(env, retCode);
324                 return nullptr;
325             }
326             dataContext->resource_ = resourcePtr;
327         } else {
328             // self resourcemanager with promise
329         }
330     }
331     napi_value result = GetResult(env, dataContext, name, execute);
332     return result;
333 }
334 
ProcessIdNameParam(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)335 napi_value ResourceManagerNapiAsyncImpl::ProcessIdNameParam(napi_env env, napi_callback_info info,
336     const std::string& name, napi_async_execute_callback execute)
337 {
338     GET_PARAMS(env, info, PARAMS_NUM_THREE);
339 
340     std::unique_ptr<ResMgrDataContext> dataContext = std::make_unique<ResMgrDataContext>();
341     dataContext->addon_ = ResMgrDataContext::GetResourceManagerAddon(env, info);
342     for (size_t i = 0; i < argc; i++) {
343         napi_valuetype valueType;
344         napi_typeof(env, argv[i], &valueType);
345         if (i == 0 && valueType == napi_number) {
346             dataContext->resId_ = ResourceManagerNapiUtils::GetResId(env, argc, argv);
347         } else if (i == 0 && valueType == napi_string) {
348             dataContext->resName_ = ResourceManagerNapiUtils::GetResNameOrPath(env, argc, argv);
349         } else if (i == 0 && valueType == napi_object) {
350             std::shared_ptr<ResourceManager::Resource> resourcePtr = std::make_shared<ResourceManager::Resource>();
351             int32_t retCode = ResourceManagerNapiUtils::GetResourceObject(env, resourcePtr, argv[0]);
352             if (retCode != SUCCESS) {
353                 RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to get native Resource object");
354                 return nullptr;
355             }
356             dataContext->resource_ = resourcePtr;
357         } else if (i == 1 && valueType != napi_number) { // the second quantity param
358             RESMGR_HILOGE(RESMGR_JS_TAG, "Parameter type is not napi_number");
359             ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
360             return nullptr;
361         } else if (i == 1 && valueType == napi_number) {
362             // dataContext->param_ is the quantity to select plural string
363             napi_get_value_int32(env, argv[i], &dataContext->param_);
364             // param is the value to replace placeholder in the plural string
365             double param;
366             if (napi_get_value_double(env, argv[i], &param) != napi_ok) {
367                 RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to get parameter value in ProcessIdNameParam");
368                 return nullptr;
369             }
370             dataContext->jsParams_.push_back(std::make_tuple(ResourceManager::NapiValueType::NAPI_NUMBER,
371                 std::to_string(param)));
372         } else if (i == 2 && valueType == napi_function) { // the third callback param
373             napi_create_reference(env, argv[i], 1, &dataContext->callbackRef_);
374             break;
375         } else {
376             // self resourcemanager with promise
377         }
378     }
379     napi_value result = GetResult(env, dataContext, name, execute);
380     return result;
381 }
382 
ProcessNoParam(napi_env env,napi_callback_info info,const std::string & name,napi_async_execute_callback execute)383 napi_value ResourceManagerNapiAsyncImpl::ProcessNoParam(napi_env env, napi_callback_info info,
384     const std::string &name, napi_async_execute_callback execute)
385 {
386     GET_PARAMS(env, info, 1);
387 
388     std::unique_ptr<ResMgrDataContext> dataContext = std::make_unique<ResMgrDataContext>();
389     std::shared_ptr<ResourceManagerAddon> *addonPtr = nullptr;
390     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&addonPtr));
391     if (status != napi_ok) {
392         RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to unwrap ProcessNoParam");
393         return nullptr;
394     }
395     dataContext->addon_ = *addonPtr;
396     napi_valuetype valueType;
397     napi_typeof(env, argv[0], &valueType);
398     if (valueType == napi_function) {
399         napi_create_reference(env, argv[0], 1, &dataContext->callbackRef_);
400     }
401 
402     napi_value result = GetResult(env, dataContext, name, execute);
403     return result;
404 }
405 
__anon06571fda1b02(napi_env env, void* data) 406 auto getStringFunc = [](napi_env env, void* data) {
407     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
408     std::shared_ptr<ResourceManager> resMgr = nullptr;
409     int32_t resId = 0;
410 
411     bool ret = ResourceManagerNapiUtils::GetHapResourceManager(dataContext, resMgr, resId);
412     if (!ret) {
413         RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to GetHapResourceManager in getStringFunc");
414         dataContext->SetErrorMsg("Failed to get string, invalid bundleName or moduleName",
415             false, ERROR_CODE_INVALID_INPUT_PARAMETER);
416         return;
417     }
418     RState state = resMgr->GetStringById(resId, dataContext->value_);
419     if (state != RState::SUCCESS) {
420         dataContext->SetErrorMsg("GetStringById failed state", true, state);
421         ReportGetResourceByIdFail(resId, dataContext->value_, "failed in getStringFunc");
422         return;
423     }
424     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsString;
425 };
426 
GetStringValue(napi_env env,napi_callback_info info)427 napi_value ResourceManagerNapiAsyncImpl::GetStringValue(napi_env env, napi_callback_info info)
428 {
429     if (ResourceManagerNapiUtils::IsNapiNumber(env, info)) {
430         return ProcessIdParamV9(env, info, "getStringValue", getStringFunc);
431     } else if (ResourceManagerNapiUtils::IsNapiObject(env, info)) {
432         return ProcessResourceParamV9(env, info, "getStringValue", getStringFunc);
433     } else {
434         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
435         return nullptr;
436     }
437 }
438 
GetString(napi_env env,napi_callback_info info)439 napi_value ResourceManagerNapiAsyncImpl::GetString(napi_env env, napi_callback_info info)
440 {
441     return ProcessOnlyIdParam(env, info, "getString", getStringFunc);
442 }
443 
__anon06571fda1c02(napi_env env, void* data) 444 auto getStringByNameFunc = [](napi_env env, void* data) {
445     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
446     RState state = dataContext->addon_->GetResMgr()->GetStringByName(dataContext->resName_.c_str(),
447         dataContext->value_);
448     if (state != RState::SUCCESS) {
449         dataContext->SetErrorMsg("GetStringByName failed state", false, state);
450         ReportGetResourceByNameFail(dataContext->resName_, dataContext->value_,
451             "failed in getStringByNameFunc");
452         return;
453     }
454     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsString;
455 };
456 
GetStringByName(napi_env env,napi_callback_info info)457 napi_value ResourceManagerNapiAsyncImpl::GetStringByName(napi_env env, napi_callback_info info)
458 {
459     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
460         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
461         return nullptr;
462     }
463     return ProcessNameParamV9(env, info, "getStringByName", getStringByNameFunc);
464 }
465 
__anon06571fda1d02(napi_env env, void* data) 466 auto getStringArrayFunc = [](napi_env env, void* data) {
467     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
468     RState state;
469     int32_t resId = 0;
470     std::shared_ptr<ResourceManager> resMgr = nullptr;
471     if (dataContext->resId_ != 0 || dataContext->resource_ != nullptr) {
472         bool ret = ResourceManagerNapiUtils::GetHapResourceManager(dataContext, resMgr, resId);
473         if (!ret) {
474             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to GetHapResourceManager in getStringArrayFunc");
475             dataContext->SetErrorMsg("Failed to get string array, invalid bundleName or moduleName",
476                 false, ERROR_CODE_INVALID_INPUT_PARAMETER);
477             return;
478         }
479         state = resMgr->GetStringArrayById(resId, dataContext->arrayValue_);
480         if (state != RState::SUCCESS) {
481             dataContext->SetErrorMsg("GetStringArrayById failed state", true, state);
482             return;
483         }
484     } else {
485         state = dataContext->addon_->GetResMgr()->GetStringArrayByName(dataContext->resName_.c_str(),
486             dataContext->arrayValue_);
487         if (state != RState::SUCCESS) {
488             dataContext->SetErrorMsg("GetStringArrayByName failed state", false, state);
489             return;
490         }
491     }
492     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsArray;
493 };
494 
GetStringArray(napi_env env,napi_callback_info info)495 napi_value ResourceManagerNapiAsyncImpl::GetStringArray(napi_env env, napi_callback_info info)
496 {
497     return ProcessOnlyIdParam(env, info, "getStringArray", getStringArrayFunc);
498 }
499 
GetStringArrayValue(napi_env env,napi_callback_info info)500 napi_value ResourceManagerNapiAsyncImpl::GetStringArrayValue(napi_env env, napi_callback_info info)
501 {
502     if (ResourceManagerNapiUtils::IsNapiNumber(env, info)) {
503         return ProcessIdParamV9(env, info, "getStringArrayValue", getStringArrayFunc);
504     } else if (ResourceManagerNapiUtils::IsNapiObject(env, info)) {
505         return ProcessResourceParamV9(env, info, "getStringArrayValue", getStringArrayFunc);
506     } else {
507         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
508         return nullptr;
509     }
510 }
511 
GetStringArrayByName(napi_env env,napi_callback_info info)512 napi_value ResourceManagerNapiAsyncImpl::GetStringArrayByName(napi_env env, napi_callback_info info)
513 {
514     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
515         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
516         return nullptr;
517     }
518     return ProcessNameParamV9(env, info, "GetStringArrayByName", getStringArrayFunc);
519 }
520 
__anon06571fda1e02(napi_env env, void *data) 521 auto getMediaFunc = [](napi_env env, void *data) {
522     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
523     std::string path;
524     int32_t resId = 0;
525     std::shared_ptr<ResourceManager> resMgr = nullptr;
526     bool ret = ResourceManagerNapiUtils::GetHapResourceManager(dataContext, resMgr, resId);
527     if (!ret) {
528         RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to GetHapResourceManager in getMediaFunc");
529         dataContext->SetErrorMsg("Failed to get media data, invalid bundleName or moduleName",
530             false, ERROR_CODE_INVALID_INPUT_PARAMETER);
531         return;
532     }
533     RState state = resMgr->GetMediaDataById(resId, dataContext->len_, dataContext->mediaData,
534         dataContext->density_);
535     if (state != SUCCESS) {
536         dataContext->SetErrorMsg("Failed to get media data in getMediaFunc", true, state);
537         return;
538     }
539     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsUint8Array;
540 };
541 
GetMediaContent(napi_env env,napi_callback_info info)542 napi_value ResourceManagerNapiAsyncImpl::GetMediaContent(napi_env env, napi_callback_info info)
543 {
544     if (ResourceManagerNapiUtils::IsNapiNumber(env, info)) {
545         return ProcessIdParamV9(env, info, "getMediaContent", getMediaFunc);
546     } else if (ResourceManagerNapiUtils::IsNapiObject(env, info)) {
547         return ProcessResourceParamV9(env, info, "getMediaContent", getMediaFunc);
548     } else {
549         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
550         return nullptr;
551     }
552 }
553 
GetMedia(napi_env env,napi_callback_info info)554 napi_value ResourceManagerNapiAsyncImpl::GetMedia(napi_env env, napi_callback_info info)
555 {
556     std::string traceVal = "ResourceManagerAddon::GetMedia";
557     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
558     napi_value media = ProcessOnlyIdParam(env, info, "getMedia", getMediaFunc);
559     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
560     return media;
561 }
562 
__anon06571fda1f02(napi_env env, void *data) 563 auto getMediaBase64Func = [](napi_env env, void *data) {
564     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
565     std::string path;
566     RState state;
567     if (dataContext->resId_ != 0 || dataContext->resource_ != nullptr) {
568         int32_t resId = 0;
569         std::shared_ptr<ResourceManager> resMgr = nullptr;
570         bool ret = ResourceManagerNapiUtils::GetHapResourceManager(dataContext, resMgr, resId);
571         if (!ret) {
572             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to GetHapResourceManager in getMediaBase64Func");
573             dataContext->SetErrorMsg("Failed to get media data, invalid bundleName or moduleName",
574                 false, ERROR_CODE_INVALID_INPUT_PARAMETER);
575             return;
576         }
577         state = resMgr->GetMediaBase64DataById(resId, dataContext->value_, dataContext->density_);
578         if (state != RState::SUCCESS) {
579             dataContext->SetErrorMsg("Failed to get media data in getMediaBase64Func", true, state);
580             return;
581         }
582         dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsString;
583     } else {
584         state = dataContext->addon_->GetResMgr()->GetMediaBase64DataByName(dataContext->resName_.c_str(),
585             dataContext->value_, dataContext->density_);
586         if (state != RState::SUCCESS) {
587             dataContext->SetErrorMsg("Failed to get media data in getMediaBase64Func", false, state);
588             return;
589         }
590         dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsString;
591     }
592 };
593 
GetMediaContentBase64(napi_env env,napi_callback_info info)594 napi_value ResourceManagerNapiAsyncImpl::GetMediaContentBase64(napi_env env, napi_callback_info info)
595 {
596     if (ResourceManagerNapiUtils::IsNapiNumber(env, info)) {
597         return ProcessIdParamV9(env, info, "getMediaContentBase64", getMediaBase64Func);
598     } else if (ResourceManagerNapiUtils::IsNapiObject(env, info)) {
599         return ProcessResourceParamV9(env, info, "getMediaContentBase64", getMediaBase64Func);
600     } else {
601         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
602         return nullptr;
603     }
604 }
605 
GetMediaBase64(napi_env env,napi_callback_info info)606 napi_value ResourceManagerNapiAsyncImpl::GetMediaBase64(napi_env env, napi_callback_info info)
607 {
608     std::string traceVal = "ResourceManagerAddon::GetMediaBase64";
609     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
610     napi_value mediaBase64 = ProcessOnlyIdParam(env, info, "GetMediaBase64", getMediaBase64Func);
611     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
612     return mediaBase64;
613 }
614 
GetMediaBase64ByName(napi_env env,napi_callback_info info)615 napi_value ResourceManagerNapiAsyncImpl::GetMediaBase64ByName(napi_env env, napi_callback_info info)
616 {
617     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
618         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
619         return nullptr;
620     }
621     std::string traceVal = "ResourceManagerAddon::GetMediaBase64ByName";
622     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
623     napi_value mediaBase64 = ProcessNameParamV9(env, info, "GetMediaBase64ByName", getMediaBase64Func);
624     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
625     return mediaBase64;
626 }
627 
__anon06571fda2002(napi_env env, void *data) 628 auto getPluralCapFunc = [](napi_env env, void *data) {
629     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
630     RState state;
631     if (dataContext->resId_ != 0 || dataContext->resource_ != nullptr) {
632         int32_t resId = 0;
633         std::shared_ptr<ResourceManager> resMgr = nullptr;
634         bool ret = ResourceManagerNapiUtils::GetHapResourceManager(dataContext, resMgr, resId);
635         if (!ret) {
636             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to GetHapResourceManager in getPluralCapFunc");
637             dataContext->SetErrorMsg("Failed to get plural string, invalid bundleName or moduleName",
638                 false, ERROR_CODE_INVALID_INPUT_PARAMETER);
639             return;
640         }
641         state = resMgr->GetFormatPluralStringById(dataContext->value_, resId, dataContext->param_,
642             dataContext->jsParams_);
643         if (state != RState::SUCCESS) {
644             dataContext->SetErrorMsg("GetPluralString failed", true, state);
645             return;
646         }
647     } else {
648         state = dataContext->addon_->GetResMgr()->GetFormatPluralStringByName(dataContext->value_,
649             dataContext->resName_.c_str(), dataContext->param_, dataContext->jsParams_);
650         if (state != RState::SUCCESS) {
651             dataContext->SetErrorMsg("GetPluralString failed", false, state);
652             return;
653         }
654     }
655     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsString;
656 };
657 
GetPluralStringValue(napi_env env,napi_callback_info info)658 napi_value ResourceManagerNapiAsyncImpl::GetPluralStringValue(napi_env env, napi_callback_info info)
659 {
660     if (ResourceManagerNapiUtils::IsNapiString(env, info)) {
661         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
662         return nullptr;
663     }
664     return ProcessIdNameParam(env, info, "getPluralStringValue", getPluralCapFunc);
665 }
666 
GetPluralStringByName(napi_env env,napi_callback_info info)667 napi_value ResourceManagerNapiAsyncImpl::GetPluralStringByName(napi_env env, napi_callback_info info)
668 {
669     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
670         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
671         return nullptr;
672     }
673     return ProcessIdNameParam(env, info, "GetPluralStringByName", getPluralCapFunc);
674 }
675 
GetPluralString(napi_env env,napi_callback_info info)676 napi_value ResourceManagerNapiAsyncImpl::GetPluralString(napi_env env, napi_callback_info info)
677 {
678     return ProcessIdNameParam(env, info, "GetPluralString", getPluralCapFunc);
679 }
680 
__anon06571fda2102(napi_env env, void* data) 681 auto g_getRawFileFunc = [](napi_env env, void* data) {
682     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
683     RState state = dataContext->addon_->GetResMgr()->GetRawFileFromHap(dataContext->path_,
684         dataContext->len_, dataContext->mediaData);
685     if (state != RState::SUCCESS) {
686         dataContext->SetErrorMsg("GetRawFileContent failed", false, state);
687         return;
688     }
689     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsUint8Array;
690 };
691 
GetRawFileContent(napi_env env,napi_callback_info info)692 napi_value ResourceManagerNapiAsyncImpl::GetRawFileContent(napi_env env, napi_callback_info info)
693 {
694     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
695         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
696         return nullptr;
697     }
698     return ProcessOnlyIdParam(env, info, "getRawFileContent", g_getRawFileFunc);
699 }
700 
GetRawFile(napi_env env,napi_callback_info info)701 napi_value ResourceManagerNapiAsyncImpl::GetRawFile(napi_env env, napi_callback_info info)
702 {
703     return ProcessOnlyIdParam(env, info, "getRawFile", g_getRawFileFunc);
704 }
705 
__anon06571fda2202(napi_env env, void* data) 706 auto g_getRawFileDescriptorFunc = [](napi_env env, void* data) {
707     ResMgrDataContext *context = static_cast<ResMgrDataContext*>(data);
708     RState state = context->addon_->GetResMgr()->GetRawFileDescriptorFromHap(context->path_, context->descriptor_);
709     if (state != RState::SUCCESS) {
710         context->SetErrorMsg("Failed to get descriptor", false, state);
711         return;
712     }
713     context->createValueFunc_ = ResourceManagerNapiUtils::CreateJsRawFd;
714 };
715 
GetRawFd(napi_env env,napi_callback_info info)716 napi_value ResourceManagerNapiAsyncImpl::GetRawFd(napi_env env, napi_callback_info info)
717 {
718     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
719         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
720         return nullptr;
721     }
722     return ProcessOnlyIdParam(env, info, "getRawFd", g_getRawFileDescriptorFunc);
723 }
724 
GetRawFileDescriptor(napi_env env,napi_callback_info info)725 napi_value ResourceManagerNapiAsyncImpl::GetRawFileDescriptor(napi_env env, napi_callback_info info)
726 {
727     return ProcessOnlyIdParam(env, info, "getRawFileDescriptor", g_getRawFileDescriptorFunc);
728 }
729 
__anon06571fda2302(napi_env env, void* data) 730 auto closeRawFileDescriptorFunc = [](napi_env env, void* data) {
731     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
732     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CloseJsRawFd;
733 };
734 
CloseRawFd(napi_env env,napi_callback_info info)735 napi_value ResourceManagerNapiAsyncImpl::CloseRawFd(napi_env env, napi_callback_info info)
736 {
737     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
738         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
739         return nullptr;
740     }
741     return ProcessOnlyIdParam(env, info, "closeRawFd", closeRawFileDescriptorFunc);
742 }
743 
CloseRawFileDescriptor(napi_env env,napi_callback_info info)744 napi_value ResourceManagerNapiAsyncImpl::CloseRawFileDescriptor(napi_env env, napi_callback_info info)
745 {
746     return ProcessOnlyIdParam(env, info, "closeRawFileDescriptor", closeRawFileDescriptorFunc);
747 }
748 
__anon06571fda2402(napi_env env, void *data) 749 auto getMediaByNameFunc = [](napi_env env, void *data) {
750     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
751     RState state = dataContext->addon_->GetResMgr()->GetMediaDataByName(dataContext->resName_.c_str(),
752         dataContext->len_, dataContext->mediaData, dataContext->density_);
753     if (state != RState::SUCCESS) {
754         dataContext->SetErrorMsg("Failed to get media data in getMediaByNameFunc", false, state);
755         return;
756     }
757     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsUint8Array;
758     return;
759 };
760 
GetMediaByName(napi_env env,napi_callback_info info)761 napi_value ResourceManagerNapiAsyncImpl::GetMediaByName(napi_env env, napi_callback_info info)
762 {
763     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
764         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
765         return nullptr;
766     }
767     std::string traceVal = "ResourceManagerAddon::GetMediaByName";
768     StartTrace(HITRACE_TAG_GLOBAL_RESMGR, traceVal);
769     napi_value media = ProcessNameParamV9(env, info, "getMediaByName", getMediaByNameFunc);
770     FinishTrace(HITRACE_TAG_GLOBAL_RESMGR);
771     return media;
772 }
773 
__anon06571fda2502(napi_env env, void *data) 774 auto getConfigFunc = [](napi_env env, void *data) {
775     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
776     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsConfig;
777 };
778 
GetConfiguration(napi_env env,napi_callback_info info)779 napi_value ResourceManagerNapiAsyncImpl::GetConfiguration(napi_env env, napi_callback_info info)
780 {
781     return ProcessNoParam(env, info, "GetConfiguration", getConfigFunc);
782 }
783 
__anon06571fda2602(napi_env env, void *data) 784 auto getDeviceCapFunc = [](napi_env env, void *data) {
785     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
786     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsDeviceCap;
787 };
788 
GetDeviceCapability(napi_env env,napi_callback_info info)789 napi_value ResourceManagerNapiAsyncImpl::GetDeviceCapability(napi_env env, napi_callback_info info)
790 {
791     return ProcessNoParam(env, info, "GetDeviceCapability", getDeviceCapFunc);
792 }
793 
__anon06571fda2702(napi_env env, void* data) 794 auto getRawFileListFunc = [](napi_env env, void* data) {
795     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
796     RState state = dataContext->addon_->GetResMgr()->GetRawFileList(dataContext->path_, dataContext->arrayValue_);
797     if (state != SUCCESS) {
798         dataContext->SetErrorMsg("GetRawFileList failed state", false, state);
799         return;
800     }
801     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsArray;
802 };
803 
GetRawFileList(napi_env env,napi_callback_info info)804 napi_value ResourceManagerNapiAsyncImpl::GetRawFileList(napi_env env, napi_callback_info info)
805 {
806     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
807         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
808         return nullptr;
809     }
810     return ProcessOnlyIdParam(env, info, "getRawFileList", getRawFileListFunc);
811 }
812 
__anon06571fda2802(napi_env env, void* data) 813 auto getColorFunc = [](napi_env env, void* data) {
814     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
815     std::shared_ptr<ResourceManager> resMgr = nullptr;
816     int32_t resId = 0;
817 
818     if (!ResourceManagerNapiUtils::GetHapResourceManager(dataContext, resMgr, resId)) {
819         RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to GetHapResourceManager in getColorFunc");
820         dataContext->SetErrorMsg("Failed to get color, invalid bundleName or moduleName",
821             false, ERROR_CODE_INVALID_INPUT_PARAMETER);
822         return;
823     }
824     RState state = resMgr->GetColorById(resId, dataContext->colorValue_);
825     if (state != RState::SUCCESS) {
826         dataContext->SetErrorMsg("GetColorById failed state", true, state);
827         return;
828     }
829     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsColor;
830 };
831 
GetColor(napi_env env,napi_callback_info info)832 napi_value ResourceManagerNapiAsyncImpl::GetColor(napi_env env, napi_callback_info info)
833 {
834     if (ResourceManagerNapiUtils::IsNapiNumber(env, info)) {
835         return ProcessIdParamV9(env, info, "getColor", getColorFunc);
836     } else if (ResourceManagerNapiUtils::IsNapiObject(env, info)) {
837         return ProcessResourceParamV9(env, info, "getColor", getColorFunc);
838     } else {
839         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
840         return nullptr;
841     }
842 }
843 
__anon06571fda2902(napi_env env, void* data) 844 auto getColorByNameFunc = [](napi_env env, void* data) {
845     ResMgrDataContext *dataContext = static_cast<ResMgrDataContext*>(data);
846     RState state = dataContext->addon_->GetResMgr()->GetColorByName(dataContext->resName_.c_str(),
847         dataContext->colorValue_);
848     if (state != RState::SUCCESS) {
849         dataContext->SetErrorMsg("getColorByNameFunc failed state", false, state);
850         return;
851     }
852     dataContext->createValueFunc_ = ResourceManagerNapiUtils::CreateJsColor;
853 };
854 
GetColorByName(napi_env env,napi_callback_info info)855 napi_value ResourceManagerNapiAsyncImpl::GetColorByName(napi_env env, napi_callback_info info)
856 {
857     if (!ResourceManagerNapiUtils::IsNapiString(env, info)) {
858         ResourceManagerNapiUtils::NapiThrow(env, ERROR_CODE_INVALID_INPUT_PARAMETER);
859         return nullptr;
860     }
861     return ProcessNameParamV9(env, info, "getColorByName", getColorByNameFunc);
862 }
863 } // namespace Resource
864 } // namespace Global
865 } // namespace OHOS
866