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 #ifndef NWEB_WEBVIEW_JAVA_SCRIPT_EXECUTE_CALLBACK_H 16 #define NWEB_WEBVIEW_JAVA_SCRIPT_EXECUTE_CALLBACK_H 17 18 #include <string> 19 #include <uv.h> 20 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "nweb_value_callback.h" 24 25 namespace OHOS::NWeb { 26 class WebviewJavaScriptExecuteCallback : public OHOS::NWeb::NWebValueCallback<std::string> { 27 public: WebviewJavaScriptExecuteCallback(napi_env env,napi_ref callbackRef,napi_deferred deferred)28 explicit WebviewJavaScriptExecuteCallback(napi_env env, napi_ref callbackRef, napi_deferred deferred) 29 : env_(env), callbackRef_(callbackRef), deferred_(deferred) {} 30 ~WebviewJavaScriptExecuteCallback() = default; 31 void OnReceiveValue(std::string result) override; 32 33 private: 34 struct JavaScriptExecuteParam { 35 napi_env env_; 36 napi_ref callbackRef_; 37 napi_deferred deferred_; 38 std::string result_; 39 }; 40 41 napi_env env_ = nullptr; 42 napi_ref callbackRef_ = nullptr; 43 napi_deferred deferred_ = nullptr; 44 45 static void UvAfterWorkCb(uv_work_t* work, int status); 46 static void UvAfterWorkCbAsync(napi_env env, napi_ref callbackRef, const std::string& result); 47 static void UvAfterWorkCbPromise(napi_env env, napi_deferred deferred, const std::string& result); 48 }; 49 50 } 51 #endif 52