• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "webview_javascript_execute_callback.h"
17 #include "webview_log.h"
18 #include "nweb_message_ext.h"
19 
20 using namespace OHOS::NWeb;
21 
22 namespace OHOS::Webview {
23 
ConvertToJsType(NWebValue::Type type)24 int32_t WebJsMessageExtImpl::ConvertToJsType(NWebValue::Type type)
25 {
26     JsMessageType jsMessageType = JsMessageType::NOTSUPPORT;
27     switch (type) {
28         case NWebValue::Type::STRING:
29             jsMessageType = JsMessageType::STRING;
30             break;
31         case NWebValue::Type::INTEGER:
32         case NWebValue::Type::DOUBLE:
33             jsMessageType = JsMessageType::NUMBER;
34             break;
35         case NWebValue::Type::BOOLEAN:
36             jsMessageType = JsMessageType::BOOLEAN;
37             break;
38         case NWebValue::Type::BINARY:
39             jsMessageType = JsMessageType::ARRAYBUFFER;
40             break;
41         case NWebValue::Type::STRINGARRAY:
42         case NWebValue::Type::BOOLEANARRAY:
43         case NWebValue::Type::DOUBLEARRAY:
44         case NWebValue::Type::INT64ARRAY:
45             jsMessageType = JsMessageType::ARRAY;
46             break;
47         default:
48             jsMessageType = JsMessageType::NOTSUPPORT;
49             break;
50     }
51     return static_cast<int32_t>(jsMessageType);
52 }
53 
GetType()54 int32_t WebJsMessageExtImpl::GetType()
55 {
56     if (value_) {
57         return ConvertToJsType(value_->GetType());
58     }
59     return static_cast<int32_t>(JsMessageType::NOTSUPPORT);
60 }
61 
GetString()62 std::string WebJsMessageExtImpl::GetString()
63 {
64     if (value_) {
65         return value_->GetString();
66     }
67     return "";
68 }
69 
GetNumber()70 double WebJsMessageExtImpl::GetNumber()
71 {
72     if (value_) {
73         return value_->GetDouble();
74     }
75     return 0;
76 }
77 
GetBoolean()78 bool WebJsMessageExtImpl::GetBoolean()
79 {
80     if (value_) {
81         return value_->GetBoolean();
82     }
83     return false;
84 }
85 
OnReceiveValue(std::shared_ptr<NWebMessage> result)86 void WebviewJavaScriptExecuteCallback::OnReceiveValue(std::shared_ptr<NWebMessage> result)
87 {
88     WEBVIEWLOGI("WebviewJavaScriptExecuteCallback::OnReceiveValue start");
89     RetDataCString ret = { .code = NWebError::INVALID_RESOURCE, .data = nullptr };
90     if (result == nullptr) {
91         callbackRef_(ret);
92         return;
93     }
94     if (result->GetType() == NWebValue::Type::STRING && result->GetString().empty()) {
95         callbackRef_(ret);
96         return;
97     }
98     ret.code = NWebError::NO_ERROR;
99     ret.data = MallocCString(result->GetString());
100     if (ret.data == nullptr) {
101         ret.code = NWebError::NEW_OOM;
102     }
103     callbackRef_(ret);
104 }
105 
OnReceiveValueV2(std::shared_ptr<NWebHapValue> value)106 void WebviewJavaScriptExecuteCallback::OnReceiveValueV2(std::shared_ptr<NWebHapValue> value)
107 {
108     WEBVIEWLOGI("WebviewJavaScriptExecuteCallback::OnReceiveValue2 start");
109     RetDataCString ret = { .code = NWebError::INVALID_RESOURCE, .data = nullptr };
110     if (value == nullptr) {
111         callbackRef_(ret);
112         return;
113     }
114     if (value->GetType() == NWebHapValue::Type::STRING && value->GetString().empty()) {
115         callbackRef_(ret);
116         return;
117     }
118     ret.code = NWebError::NO_ERROR;
119     ret.data = MallocCString(value->GetString());
120     if (ret.data == nullptr) {
121         ret.code = NWebError::NEW_OOM;
122     }
123     callbackRef_(ret);
124 }
125 
OnReceiveValue(std::shared_ptr<NWebMessage> result)126 void WebviewJavaScriptExtExecuteCallback::OnReceiveValue(std::shared_ptr<NWebMessage> result)
127 {
128     WEBVIEWLOGI("WebviewJavaScriptExtExecuteCallback::OnReceiveValue start");
129     RetDataI64 ret = { .code = NWebError::INVALID_RESOURCE, .data = 0 };
130     if (result == nullptr) {
131         callbackRef_(ret);
132         return;
133     }
134     WebJsMessageExtImpl *webJsMessageExtImpl = OHOS::FFI::FFIData::Create<WebJsMessageExtImpl>(result);
135     if (webJsMessageExtImpl == nullptr) {
136         WEBVIEWLOGE("new WebJsMessageExtImpl failed.");
137         callbackRef_(ret);
138         return;
139     }
140     ret.code = NWebError::NO_ERROR;
141     ret.data = webJsMessageExtImpl->GetID();
142     callbackRef_(ret);
143 }
144 
OnReceiveValueV2(std::shared_ptr<NWebHapValue> value)145 void WebviewJavaScriptExtExecuteCallback::OnReceiveValueV2(std::shared_ptr<NWebHapValue> value)
146 {
147     WEBVIEWLOGI("WebviewJavaScriptExtExecuteCallback::OnReceiveValue start");
148     RetDataI64 ret = { .code = NWebError::INVALID_RESOURCE, .data = 0 };
149     if (value == nullptr) {
150         callbackRef_(ret);
151         return;
152     }
153     WebJsMessageExtImpl *webJsMessageExtImpl = OHOS::FFI::FFIData::Create<WebJsMessageExtImpl>(value);
154     if (webJsMessageExtImpl == nullptr) {
155         WEBVIEWLOGE("new WebJsMessageExtImpl failed.");
156         callbackRef_(ret);
157         return;
158     }
159     ret.code = NWebError::NO_ERROR;
160     ret.data = webJsMessageExtImpl->GetID();
161     callbackRef_(ret);
162 }
163 
164 } // namespace OHOS::Webview
165