• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "napi_parse_utils.h"
24 #include "nweb_value_callback.h"
25 #include "nweb_web_message.h"
26 
27 namespace OHOS::NWeb {
28 enum class JsMessageType : int {
29     NOTSUPPORT = 0,
30     STRING,
31     NUMBER,
32     BOOLEAN,
33     ARRAYBUFFER,
34     ARRAY
35 };
36 
37 class NapiJsMessageExt {
38 public:
39     NapiJsMessageExt() = default;
40     ~NapiJsMessageExt() = default;
41 
42     static napi_value JsConstructor(napi_env env, napi_callback_info info);
43 
44     static napi_value GetType(napi_env env, napi_callback_info info);
45     static napi_value GetString(napi_env env, napi_callback_info info);
46     static napi_value GetNumber(napi_env env, napi_callback_info info);
47     static napi_value GetBoolean(napi_env env, napi_callback_info info);
48     static napi_value GetArrayBuffer(napi_env env, napi_callback_info info);
49     static napi_value GetArray(napi_env env, napi_callback_info info);
50 };
51 
52 class WebJsMessageExt {
53 public:
WebJsMessageExt(std::shared_ptr<NWebMessage> value)54     explicit WebJsMessageExt(std::shared_ptr<NWebMessage> value) : value_(value) {};
55     ~WebJsMessageExt() = default;
56 
57     int32_t ConvertToJsType(NWebValue::Type type);
58     int32_t GetType();
59     std::string GetString();
60     double GetNumber();
61     bool GetBoolean();
GetJsMsgResult()62     std::shared_ptr<NWebMessage> GetJsMsgResult()
63     {
64         return value_;
65     }
66 
67 private:
68     std::shared_ptr<NWebMessage> value_ = nullptr;
69 };
70 
71 class WebviewJavaScriptExecuteCallback : public OHOS::NWeb::NWebValueCallback<std::shared_ptr<NWebMessage>> {
72 public:
WebviewJavaScriptExecuteCallback(napi_env env,napi_ref callbackRef,napi_deferred deferred,bool extention)73     explicit WebviewJavaScriptExecuteCallback(
74         napi_env env, napi_ref callbackRef, napi_deferred deferred, bool extention)
75         : env_(env), callbackRef_(callbackRef), deferred_(deferred), extention_(extention)
76     {}
77     ~WebviewJavaScriptExecuteCallback() = default;
78     void OnReceiveValue(std::shared_ptr<NWebMessage> result) override;
79 
80 private:
81     struct JavaScriptExecuteParam {
82         napi_env env_;
83         napi_ref callbackRef_;
84         napi_deferred deferred_;
85         std::shared_ptr<NWebMessage> result_;
86         bool extention_;
87     };
88 
89     napi_env env_ = nullptr;
90     napi_ref callbackRef_ = nullptr;
91     napi_deferred deferred_ = nullptr;
92     bool extention_ = false;
93 
94     static void UvAfterWorkCb(uv_work_t* work, int status);
95     static void UvAfterWorkCbAsync(
96         napi_env env, napi_ref callbackRef, std::shared_ptr<NWebMessage> result, bool extention);
97     static void UvAfterWorkCbPromise(
98         napi_env env, napi_deferred deferred, std::shared_ptr<NWebMessage> result, bool extention);
99 
100 public:
101     static void InitJSExcute(napi_env env, napi_value exports);
102 };
103 
104 } // namespace OHOS::NWeb
105 #endif
106