1/* 2 * Copyright (c) 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 "napi_webview_function.h" 17 18#include <unistd.h> 19#include <uv.h> 20#include "business_error.h" 21#include "napi_parse_utils.h" 22#include "nweb.h" 23#include "nweb_helper.h" 24#include "nweb_log.h" 25#include "web_errors.h" 26 27namespace OHOS { 28namespace NWeb { 29using namespace NWebError; 30 31std::unordered_map<std::string, std::function<void(napi_env, napi_ref)>> onceType = { 32 {"webInited", RegisterWebInitedCallback}, 33}; 34 35napi_value WebFunctionInit(napi_env env, napi_value exports) 36{ 37 napi_property_descriptor properties[] = { 38 DECLARE_NAPI_FUNCTION("once", JsOnce), 39 }; 40 napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties); 41 return exports; 42} 43 44napi_value JsOnce(napi_env env, napi_callback_info info) 45{ 46 napi_value thisVar = nullptr; 47 napi_value result = nullptr; 48 size_t argc = INTEGER_TWO; 49 napi_value argv[INTEGER_TWO] = { 0 }; 50 51 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); 52 if (argc != INTEGER_TWO) { 53 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 54 return result; 55 } 56 57 std::string type = ""; 58 napi_valuetype valueType = napi_undefined; 59 napi_typeof(env, argv[INTEGER_ONE], &valueType); 60 if (!(NapiParseUtils::ParseString(env, argv[0], type)) || (onceType.find(type) == onceType.end()) || 61 (valueType != napi_function)) { 62 BusinessError::ThrowErrorByErrcode(env, PARAM_CHECK_ERROR); 63 return result; 64 } 65 napi_ref callback = nullptr; 66 napi_create_reference(env, argv[INTEGER_ONE], 1, &callback); 67 auto foundCallback = onceType.find(type); 68 if (foundCallback != onceType.end()) { 69 foundCallback->second(env, callback); 70 } else { 71 BusinessError::ThrowErrorByErrcode(env, TYPE_NOT_MATCH_WITCH_VALUE); 72 return result; 73 } 74 return result; 75} 76 77void RegisterWebInitedCallback(napi_env env, napi_ref callback) 78{ 79 WebInitedCallbackParam *param = new (std::nothrow) WebInitedCallbackParam(env, callback); 80 if (param == nullptr) { 81 return; 82 } 83 WebRunInitedCallback *runWebInitedCallbackObj = new (std::nothrow) WebRunInitedCallbackImpl(param); 84 if (runWebInitedCallbackObj == nullptr) { 85 delete param; 86 return; 87 } 88 OhosAdapterHelper::GetInstance().GetInitWebAdapter()->SetRunWebInitedCallback(std::move(runWebInitedCallbackObj)); 89} 90} // namespace NWeb 91} // namespace OHOS 92