1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/jsview/js_web_controller.h"
17
18 #include "base/log/ace_scoring_log.h"
19 #include "base/utils/linear_map.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/engine/functions/js_webview_function.h"
22 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "core/components_ng/base/view_stack_processor.h"
24
25 namespace OHOS::Ace::Framework {
26 namespace {
ToJsValueHelper(const EcmaVM * vm,std::shared_ptr<WebJSValue> argument)27 panda::Local<panda::JSValueRef> ToJsValueHelper(const EcmaVM* vm, std::shared_ptr<WebJSValue> argument)
28 {
29 if (!argument || !vm) {
30 TAG_LOGE(AceLogTag::ACE_WEB, "ToJsValueHelper: argument or vm is null");
31 return panda::JSValueRef::Undefined(vm);
32 }
33 switch (argument->GetType()) {
34 case WebJSValue::Type::INTEGER:
35 return ToJSValue(argument->GetInt());
36 case WebJSValue::Type::DOUBLE:
37 return ToJSValue(argument->GetDouble());
38 case WebJSValue::Type::BOOLEAN:
39 return ToJSValue(argument->GetBoolean());
40 case WebJSValue::Type::STRING:
41 return ToJSValue(argument->GetString());
42 case WebJSValue::Type::LIST: {
43 size_t length = argument->GetListValueSize();
44 auto arr = panda::ArrayRef::New(vm, length);
45 for (size_t i = 0; i < length; ++i) {
46 auto nPtr = std::make_shared<WebJSValue>(argument->GetListValue(i));
47 if (!panda::ArrayRef::SetValueAt(vm, arr, i, ToJsValueHelper(vm, nPtr))) {
48 TAG_LOGE(AceLogTag::ACE_WEB, "ToJsValueHelper: SetValueAt api call failed");
49 return panda::JSValueRef::Undefined(vm);
50 }
51 }
52 return arr;
53 }
54 case WebJSValue::Type::DICTIONARY: {
55 auto obj = panda::ObjectRef::New(vm);
56 auto dict = argument->GetDictionaryValue();
57 for (auto& item : dict) {
58 auto nPtr = std::make_shared<WebJSValue>(item.second);
59 obj->Set(vm, panda::StringRef::NewFromUtf8(vm, item.first.c_str()), ToJsValueHelper(vm, nPtr));
60 }
61 return obj;
62 }
63 case WebJSValue::Type::BINARY: {
64 auto size = argument->GetBinaryValueSize();
65 auto buff = argument->GetBinaryValue();
66 return panda::ArrayBufferRef::New(vm, (void*)buff, (int32_t)size, nullptr, (void*)buff);
67 }
68 case WebJSValue::Type::NONE:
69 return panda::JSValueRef::Undefined(vm);
70 default:
71 TAG_LOGE(AceLogTag::ACE_WEB, "ToJsValueHelper: jsvalue type not support!");
72 break;
73 }
74 return panda::JSValueRef::Undefined(vm);
75 }
76
ParseWebViewValueToJsValue(std::shared_ptr<WebJSValue> value,std::vector<JSRef<JSVal>> & argv)77 void ParseWebViewValueToJsValue(std::shared_ptr<WebJSValue> value, std::vector<JSRef<JSVal>>& argv)
78 {
79 if (!value) {
80 return;
81 }
82 auto vm = GetEcmaVm();
83 argv.push_back(JSRef<JSVal>::Make(ToJsValueHelper(vm, value)));
84 }
85
ParseValue(const JSRef<JSVal> & resultValue,std::shared_ptr<WebJSValue> webviewValue)86 std::shared_ptr<WebJSValue> ParseValue(const JSRef<JSVal>& resultValue, std::shared_ptr<WebJSValue> webviewValue)
87 {
88 if (!webviewValue) {
89 TAG_LOGE(AceLogTag::ACE_WEB, "ParseValue: webviewValue or resultValue is null");
90 return std::make_shared<WebJSValue>();
91 }
92
93 webviewValue->error_ = static_cast<int>(WebJavaScriptBridgeError::NO_ERROR0);
94 if (resultValue->IsBoolean()) {
95 webviewValue->SetType(WebJSValue::Type::BOOLEAN);
96 webviewValue->SetBoolean(resultValue->ToBoolean());
97 } else if (resultValue->IsNull()) {
98 webviewValue->SetType(WebJSValue::Type::NONE);
99 } else if (resultValue->IsString()) {
100 webviewValue->SetType(WebJSValue::Type::STRING);
101 webviewValue->SetString(resultValue->ToString());
102 } else if (resultValue->IsNumber()) {
103 webviewValue->SetType(WebJSValue::Type::DOUBLE);
104 webviewValue->SetDouble(resultValue->ToNumber<double>());
105 } else if (resultValue->IsArray()) {
106 webviewValue->SetType(WebJSValue::Type::LIST);
107 JSRef<JSArray> array = JSRef<JSArray>::Cast(resultValue);
108 for (size_t i = 0; i < array->Length(); i++) {
109 JSRef<JSVal> value = array->GetValueAt(i);
110 auto nVal = std::make_shared<WebJSValue>();
111 webviewValue->AddListValue(*ParseValue(value, nVal));
112 }
113 } else if (resultValue->IsObject()) {
114 webviewValue->SetType(WebJSValue::Type::DICTIONARY);
115 JSRef<JSObject> dic = JSRef<JSObject>::Cast(resultValue);
116 auto names = dic->GetPropertyNames();
117 for (size_t i = 0; i < names->Length(); i++) {
118 JSRef<JSVal> key = names->GetValueAt(i);
119 if (!(key->IsString())) {
120 continue;
121 }
122 JSRef<JSVal> property = dic->GetProperty(key->ToString().c_str());
123 auto nwebValueTmp = std::make_shared<WebJSValue>();
124 auto nwebKeyTmp = std::make_shared<WebJSValue>();
125 ParseValue(key, nwebKeyTmp);
126 ParseValue(property, nwebValueTmp);
127 webviewValue->AddDictionaryValue(nwebKeyTmp->GetString(), *nwebValueTmp);
128 }
129 } else if (resultValue->IsFunction()) {
130 TAG_LOGE(AceLogTag::ACE_WEB, "ParseValue: object is not supported");
131 webviewValue->SetType(WebJSValue::Type::NONE);
132 } else if (resultValue->IsUndefined()) {
133 webviewValue->SetType(WebJSValue::Type::NONE);
134 webviewValue->error_ = static_cast<int>(WebJavaScriptBridgeError::OBJECT_IS_GONE);
135 }
136 return webviewValue;
137 }
138 } // namespace
139
140 class JSWebCookie : public Referenced {
141 public:
JSBind(BindingTarget globalObj)142 static void JSBind(BindingTarget globalObj)
143 {
144 JSClass<JSWebCookie>::Declare("WebCookie");
145 JSClass<JSWebCookie>::CustomMethod("setCookie", &JSWebCookie::SetCookie);
146 JSClass<JSWebCookie>::CustomMethod("getCookie", &JSWebCookie::GetCookie);
147 JSClass<JSWebCookie>::CustomMethod("deleteEntireCookie", &JSWebCookie::DeleteEntirelyCookie);
148 JSClass<JSWebCookie>::CustomMethod("saveCookie", &JSWebCookie::SaveCookieSync);
149 JSClass<JSWebCookie>::Bind(globalObj, JSWebCookie::Constructor, JSWebCookie::Destructor);
150 }
151
SetWebCookie(WebCookie * manager)152 void SetWebCookie(WebCookie* manager)
153 {
154 if (manager != nullptr) {
155 manager_ = manager;
156 }
157 }
158
SetCookie(const JSCallbackInfo & args)159 void SetCookie(const JSCallbackInfo& args)
160 {
161 std::string url;
162 std::string value;
163 bool result = false;
164 if (args.Length() >= 2 && args[0]->IsString() && args[1]->IsString()) {
165 url = args[0]->ToString();
166 value = args[1]->ToString();
167 if (manager_ != nullptr) {
168 result = manager_->SetCookie(url, value);
169 }
170 }
171 auto jsVal = JSVal(ToJSValue(result));
172 auto returnValue = JSRef<JSVal>::Make(jsVal);
173 args.SetReturnValue(returnValue);
174 }
175
GetCookie(const JSCallbackInfo & args)176 void GetCookie(const JSCallbackInfo& args)
177 {
178 std::string result = "";
179 if (manager_ != nullptr && args.Length() >= 1 && args[0]->IsString()) {
180 std::string url = args[0]->ToString();
181 result = manager_->GetCookie(url);
182 }
183 auto jsVal = JSVal(ToJSValue(result));
184 auto returnValue = JSRef<JSVal>::Make(jsVal);
185 args.SetReturnValue(returnValue);
186 }
187
DeleteEntirelyCookie(const JSCallbackInfo & args)188 void DeleteEntirelyCookie(const JSCallbackInfo& args)
189 {
190 if (manager_ == nullptr) {
191 return;
192 }
193 manager_->DeleteEntirelyCookie();
194 }
195
SaveCookieSync(const JSCallbackInfo & args)196 void SaveCookieSync(const JSCallbackInfo& args)
197 {
198 bool result = false;
199 if (manager_ != nullptr) {
200 result = manager_->SaveCookieSync();
201 }
202 auto jsVal = JSVal(ToJSValue(result));
203 auto returnValue = JSRef<JSVal>::Make(jsVal);
204 args.SetReturnValue(returnValue);
205 }
206
207 private:
Constructor(const JSCallbackInfo & args)208 static void Constructor(const JSCallbackInfo& args)
209 {
210 auto jsWebCookie = Referenced::MakeRefPtr<JSWebCookie>();
211 jsWebCookie->IncRefCount();
212 args.SetReturnValue(Referenced::RawPtr(jsWebCookie));
213 }
214
Destructor(JSWebCookie * jsWebCookie)215 static void Destructor(JSWebCookie* jsWebCookie)
216 {
217 if (jsWebCookie != nullptr) {
218 jsWebCookie->DecRefCount();
219 }
220 }
221 WebCookie* manager_;
222 };
223
224 class JSHitTestValue : public Referenced {
225 public:
JSBind(BindingTarget globalObj)226 static void JSBind(BindingTarget globalObj)
227 {
228 JSClass<JSHitTestValue>::Declare("HitTestValue");
229 JSClass<JSHitTestValue>::CustomMethod("getType", &JSHitTestValue::GetType);
230 JSClass<JSHitTestValue>::CustomMethod("getExtra", &JSHitTestValue::GetExtra);
231 JSClass<JSHitTestValue>::Bind(globalObj, JSHitTestValue::Constructor, JSHitTestValue::Destructor);
232 }
233
SetType(int type)234 void SetType(int type)
235 {
236 type_ = type;
237 }
238
SetExtra(const std::string & extra)239 void SetExtra(const std::string& extra)
240 {
241 extra_ = extra;
242 }
243
GetType(const JSCallbackInfo & args)244 void GetType(const JSCallbackInfo& args)
245 {
246 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(type_)));
247 }
248
GetExtra(const JSCallbackInfo & args)249 void GetExtra(const JSCallbackInfo& args)
250 {
251 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(extra_)));
252 }
253
254 private:
Constructor(const JSCallbackInfo & args)255 static void Constructor(const JSCallbackInfo& args)
256 {
257 auto jSHitTestResult = Referenced::MakeRefPtr<JSHitTestValue>();
258 jSHitTestResult->IncRefCount();
259 args.SetReturnValue(Referenced::RawPtr(jSHitTestResult));
260 }
261
Destructor(JSHitTestValue * jSHitTestResult)262 static void Destructor(JSHitTestValue* jSHitTestResult)
263 {
264 if (jSHitTestResult != nullptr) {
265 jSHitTestResult->DecRefCount();
266 }
267 }
268
269 std::string extra_;
270 int type_ = static_cast<int>(WebHitTestType::UNKNOWN);
271 };
272
JSWebController()273 JSWebController::JSWebController()
274 {
275 instanceId_ = Container::CurrentId();
276 }
277
GetJavaScriptResult(const std::string & objectName,const std::string & objectMethod,const std::vector<std::shared_ptr<WebJSValue>> & args)278 std::shared_ptr<WebJSValue> JSWebController::GetJavaScriptResult(const std::string& objectName,
279 const std::string& objectMethod, const std::vector<std::shared_ptr<WebJSValue>>& args)
280 {
281 std::vector<JSRef<JSVal>> argv = {};
282 std::shared_ptr<WebJSValue> jsResult = std::make_shared<WebJSValue>(WebJSValue::Type::NONE);
283 auto iter = objectorMap_.find(objectName);
284 if (iter == objectorMap_.end()) {
285 return jsResult;
286 }
287 auto jsObject = iter->second;
288 if (jsObject->IsEmpty()) {
289 return jsResult;
290 }
291 for (std::shared_ptr<WebJSValue> input : args) {
292 ParseWebViewValueToJsValue(input, argv);
293 }
294
295 if (jsObject->GetProperty(objectMethod.c_str())->IsEmpty() ||
296 !(jsObject->GetProperty(objectMethod.c_str())->IsFunction())) {
297 return jsResult;
298 }
299
300 JSRef<JSFunc> func = JSRef<JSFunc>::Cast(jsObject->GetProperty(objectMethod.c_str()));
301 if (func->IsEmpty()) {
302 jsResult->error_ = static_cast<int>(WebJavaScriptBridgeError::OBJECT_IS_GONE);
303 return jsResult;
304 }
305 JSRef<JSVal> result = argv.empty() ? func->Call(jsObject, 0, {}) : func->Call(jsObject, argv.size(), &argv[0]);
306 return ParseValue(result, jsResult);
307 }
308
309 class JSWebMessageEvent;
310 class JSWebMessagePort : public Referenced {
311 public:
JSBind(BindingTarget globalObj)312 static void JSBind(BindingTarget globalObj)
313 {
314 JSClass<JSWebMessagePort>::Declare("WebMessagePort");
315 JSClass<JSWebMessagePort>::CustomMethod("close", &JSWebMessagePort::Close);
316 JSClass<JSWebMessagePort>::CustomMethod("postMessageEvent", &JSWebMessagePort::PostMessage);
317 JSClass<JSWebMessagePort>::CustomMethod("onMessageEvent", &JSWebMessagePort::SetWebMessageCallback);
318 JSClass<JSWebMessagePort>::Bind(globalObj, JSWebMessagePort::Constructor, JSWebMessagePort::Destructor);
319 }
320
321 void PostMessage(const JSCallbackInfo& args);
322
Close(const JSCallbackInfo & args)323 void Close(const JSCallbackInfo& args)
324 {
325 if (port_ != nullptr) {
326 port_->Close();
327 }
328 }
329
SetWebMessageCallback(const JSCallbackInfo & args)330 void SetWebMessageCallback(const JSCallbackInfo& args)
331 {
332 if (args.Length() < 1 || !args[0]->IsObject()) {
333 return;
334 }
335
336 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
337 JSRef<JSVal> tsCallback = JSRef<JSVal>::Cast(obj);
338 std::function<void(std::string)> callback = nullptr;
339 if (tsCallback->IsFunction()) {
340 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
341 auto jsCallback = AceType::MakeRefPtr<JsWebViewFunction>(JSRef<JSFunc>::Cast(tsCallback));
342 callback = [execCtx = args.GetExecutionContext(), func = std::move(jsCallback), node = frameNode](
343 std::string result) {
344 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
345 ACE_SCORING_EVENT("onMessageEvent CallBack");
346 auto pipelineContext = PipelineContext::GetCurrentContext();
347 CHECK_NULL_VOID(pipelineContext);
348 pipelineContext->UpdateCurrentActiveNode(node);
349 func->Execute(result);
350 };
351 if (port_ != nullptr) {
352 port_->SetWebMessageCallback(std::move(callback));
353 }
354 }
355 }
356
SetWebMessagePort(const RefPtr<WebMessagePort> & port)357 void SetWebMessagePort(const RefPtr<WebMessagePort>& port)
358 {
359 port_ = port;
360 }
361
GetWebMessagePort()362 RefPtr<WebMessagePort> GetWebMessagePort()
363 {
364 return port_;
365 }
366
367 private:
368 RefPtr<WebMessagePort> port_;
369
Constructor(const JSCallbackInfo & args)370 static void Constructor(const JSCallbackInfo& args)
371 {
372 auto jsWebMessagePort = Referenced::MakeRefPtr<JSWebMessagePort>();
373 jsWebMessagePort->IncRefCount();
374 args.SetReturnValue(Referenced::RawPtr(jsWebMessagePort));
375 }
376
Destructor(JSWebMessagePort * jsWebMessagePort)377 static void Destructor(JSWebMessagePort* jsWebMessagePort)
378 {
379 if (jsWebMessagePort != nullptr) {
380 jsWebMessagePort->DecRefCount();
381 }
382 }
383 };
384
385 class JSWebMessageEvent : public Referenced {
386 public:
JSBind(BindingTarget globalObj)387 static void JSBind(BindingTarget globalObj)
388 {
389 JSClass<JSWebMessageEvent>::Declare("WebMessageEvent");
390 JSClass<JSWebMessageEvent>::CustomMethod("getData", &JSWebMessageEvent::GetData);
391 JSClass<JSWebMessageEvent>::CustomMethod("setData", &JSWebMessageEvent::SetData);
392 JSClass<JSWebMessageEvent>::CustomMethod("getPorts", &JSWebMessageEvent::GetPorts);
393 JSClass<JSWebMessageEvent>::CustomMethod("setPorts", &JSWebMessageEvent::SetPorts);
394 JSClass<JSWebMessageEvent>::Bind(globalObj, JSWebMessageEvent::Constructor, JSWebMessageEvent::Destructor);
395 }
396
GetData(const JSCallbackInfo & args)397 void GetData(const JSCallbackInfo& args)
398 {
399 auto jsVal = JSVal(ToJSValue(data_));
400 auto retVal = JSRef<JSVal>::Make(jsVal);
401 args.SetReturnValue(retVal);
402 }
403
SetData(const JSCallbackInfo & args)404 void SetData(const JSCallbackInfo& args)
405 {
406 if (args.Length() < 1 || !args[0]->IsString()) {
407 return;
408 }
409 data_ = args[0]->ToString();
410 }
411
SetPorts(const JSCallbackInfo & args)412 void SetPorts(const JSCallbackInfo& args)
413 {
414 if (args.Length() <= 0) {
415 return;
416 }
417 if (!args[0]->IsArray()) {
418 return;
419 }
420 JSRef<JSArray> jsPorts = JSRef<JSArray>::Cast(args[0]);
421 std::vector<RefPtr<WebMessagePort>> sendPorts;
422 if (jsPorts->IsArray()) {
423 JSRef<JSArray> array = JSRef<JSArray>::Cast(jsPorts);
424 for (size_t i = 0; i < array->Length(); i++) {
425 JSRef<JSVal> jsValue = array->GetValueAt(i);
426 if (!jsValue->IsObject()) {
427 continue;
428 }
429 JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(jsValue);
430 RefPtr<JSWebMessagePort> jswebPort = Referenced::Claim(jsObj->Unwrap<JSWebMessagePort>());
431 if (jswebPort) {
432 ports_.push_back(jswebPort);
433 }
434 }
435 }
436 }
437
GetPorts(const JSCallbackInfo & args)438 void GetPorts(const JSCallbackInfo& args)
439 {
440 JSRef<JSArray> jsPorts = JSRef<JSArray>::New();
441 JSRef<JSObject> jsObj;
442 RefPtr<JSWebMessagePort> jswebPort;
443 for (size_t i = 0; i < ports_.size(); i++) {
444 jsObj = JSClass<JSWebMessagePort>::NewInstance();
445 jswebPort = Referenced::Claim(jsObj->Unwrap<JSWebMessagePort>());
446 jswebPort->SetWebMessagePort(ports_[i]->GetWebMessagePort());
447 jsPorts->SetValueAt(i, jsObj);
448 }
449 args.SetReturnValue(jsPorts);
450 }
451
GetDataInternal()452 std::string GetDataInternal()
453 {
454 return data_;
455 }
456
GetPortsInternal()457 std::vector<RefPtr<JSWebMessagePort>> GetPortsInternal()
458 {
459 return ports_;
460 }
461
462 private:
463 std::string data_;
464 std::vector<RefPtr<JSWebMessagePort>> ports_;
465
Constructor(const JSCallbackInfo & args)466 static void Constructor(const JSCallbackInfo& args)
467 {
468 auto jsWebMessageEvent = Referenced::MakeRefPtr<JSWebMessageEvent>();
469 jsWebMessageEvent->IncRefCount();
470 args.SetReturnValue(Referenced::RawPtr(jsWebMessageEvent));
471 }
472
Destructor(JSWebMessageEvent * jsWebMessageEvent)473 static void Destructor(JSWebMessageEvent* jsWebMessageEvent)
474 {
475 if (jsWebMessageEvent != nullptr) {
476 jsWebMessageEvent->DecRefCount();
477 }
478 }
479 };
480
PostMessage(const JSCallbackInfo & args)481 void JSWebMessagePort::PostMessage(const JSCallbackInfo& args)
482 {
483 if (args.Length() <= 0 || !(args[0]->IsObject())) {
484 return;
485 }
486 // get ports
487 JSRef<JSVal> jsPorts = JSRef<JSVal>::Cast(args[0]);
488 if (!jsPorts->IsObject()) {
489 return;
490 }
491 auto jsRes = Referenced::Claim(JSRef<JSObject>::Cast(jsPorts)->Unwrap<JSWebMessageEvent>());
492 std::string data = jsRes->GetDataInternal();
493 if (port_) {
494 port_->PostMessage(data);
495 }
496 }
497
JSBind(BindingTarget globalObj)498 void JSWebController::JSBind(BindingTarget globalObj)
499 {
500 JSClass<JSWebController>::Declare("WebController");
501 JSClass<JSWebController>::CustomMethod("loadUrl", &JSWebController::LoadUrl);
502 JSClass<JSWebController>::CustomMethod("runJavaScript", &JSWebController::ExecuteTypeScript);
503 JSClass<JSWebController>::CustomMethod("refresh", &JSWebController::Refresh);
504 JSClass<JSWebController>::CustomMethod("stop", &JSWebController::StopLoading);
505 JSClass<JSWebController>::CustomMethod("getHitTest", &JSWebController::GetHitTestResult);
506 JSClass<JSWebController>::CustomMethod("registerJavaScriptProxy", &JSWebController::AddJavascriptInterface);
507 JSClass<JSWebController>::CustomMethod("deleteJavaScriptRegister", &JSWebController::RemoveJavascriptInterface);
508 JSClass<JSWebController>::CustomMethod("onInactive", &JSWebController::OnInactive);
509 JSClass<JSWebController>::CustomMethod("onActive", &JSWebController::OnActive);
510 JSClass<JSWebController>::CustomMethod("zoom", &JSWebController::Zoom);
511 JSClass<JSWebController>::CustomMethod("requestFocus", &JSWebController::RequestFocus);
512 JSClass<JSWebController>::CustomMethod("loadData", &JSWebController::LoadDataWithBaseUrl);
513 JSClass<JSWebController>::CustomMethod("backward", &JSWebController::Backward);
514 JSClass<JSWebController>::CustomMethod("forward", &JSWebController::Forward);
515 JSClass<JSWebController>::CustomMethod("accessStep", &JSWebController::AccessStep);
516 JSClass<JSWebController>::CustomMethod("accessForward", &JSWebController::AccessForward);
517 JSClass<JSWebController>::CustomMethod("accessBackward", &JSWebController::AccessBackward);
518 JSClass<JSWebController>::CustomMethod("clearHistory", &JSWebController::ClearHistory);
519 JSClass<JSWebController>::CustomMethod("clearSslCache", &JSWebController::ClearSslCache);
520 JSClass<JSWebController>::CustomMethod(
521 "clearClientAuthenticationCache", &JSWebController::ClearClientAuthenticationCache);
522 JSClass<JSWebController>::CustomMethod("getCookieManager", &JSWebController::GetCookieManager);
523 JSClass<JSWebController>::CustomMethod("getHitTestValue", &JSWebController::GetHitTestValue);
524 JSClass<JSWebController>::CustomMethod("backOrForward", &JSWebController::BackOrForward);
525 JSClass<JSWebController>::CustomMethod("zoomIn", &JSWebController::ZoomIn);
526 JSClass<JSWebController>::CustomMethod("zoomOut", &JSWebController::ZoomOut);
527 JSClass<JSWebController>::CustomMethod("getPageHeight", &JSWebController::GetPageHeight);
528 JSClass<JSWebController>::CustomMethod("getTitle", &JSWebController::GetTitle);
529 JSClass<JSWebController>::CustomMethod("createWebMessagePorts", &JSWebController::CreateWebMessagePorts);
530 JSClass<JSWebController>::CustomMethod("postMessage", &JSWebController::PostWebMessage);
531 JSClass<JSWebController>::CustomMethod("getWebId", &JSWebController::GetWebId);
532 JSClass<JSWebController>::CustomMethod("getDefaultUserAgent", &JSWebController::GetDefaultUserAgent);
533 JSClass<JSWebController>::CustomMethod("searchAllAsync", &JSWebController::SearchAllAsync);
534 JSClass<JSWebController>::CustomMethod("clearMatches", &JSWebController::ClearMatches);
535 JSClass<JSWebController>::CustomMethod("searchNext", &JSWebController::SearchNext);
536 JSClass<JSWebController>::CustomMethod("getUrl", &JSWebController::GetUrl);
537 JSClass<JSWebController>::CustomMethod("getProgress", &JSWebController::GetProgress);
538 JSClass<JSWebController>::Bind(globalObj, JSWebController::Constructor, JSWebController::Destructor);
539 JSWebCookie::JSBind(globalObj);
540 JSHitTestValue::JSBind(globalObj);
541 JSWebMessagePort::JSBind(globalObj);
542 JSWebMessageEvent::JSBind(globalObj);
543 }
544
Constructor(const JSCallbackInfo & args)545 void JSWebController::Constructor(const JSCallbackInfo& args)
546 {
547 auto webController = Referenced::MakeRefPtr<JSWebController>();
548 webController->IncRefCount();
549 RefPtr<WebController> controller = AceType::MakeRefPtr<WebController>();
550 webController->SetController(controller);
551 args.SetReturnValue(Referenced::RawPtr(webController));
552 }
553
Destructor(JSWebController * webController)554 void JSWebController::Destructor(JSWebController* webController)
555 {
556 if (webController != nullptr) {
557 webController->DecRefCount();
558 }
559 }
560
Reload() const561 void JSWebController::Reload() const
562 {
563 if (webController_) {
564 webController_->Reload();
565 }
566 }
567
CreateWebMessagePorts(const JSCallbackInfo & args)568 void JSWebController::CreateWebMessagePorts(const JSCallbackInfo& args)
569 {
570 ContainerScope scope(instanceId_);
571 if (webController_) {
572 std::vector<RefPtr<WebMessagePort>> ports;
573 webController_->CreateMsgPorts(ports);
574 JSRef<JSObject> jsPort0 = JSClass<JSWebMessagePort>::NewInstance();
575 auto port0 = Referenced::Claim(jsPort0->Unwrap<JSWebMessagePort>());
576 port0->SetWebMessagePort(ports.at(0));
577
578 JSRef<JSObject> jsPort1 = JSClass<JSWebMessagePort>::NewInstance();
579 auto port1 = Referenced::Claim(jsPort1->Unwrap<JSWebMessagePort>());
580 port1->SetWebMessagePort(ports.at(1));
581
582 JSRef<JSArray> result = JSRef<JSArray>::New();
583 result->SetValueAt(0, jsPort0);
584 result->SetValueAt(1, jsPort1);
585 args.SetReturnValue(result);
586 }
587 }
588
PostWebMessage(const JSCallbackInfo & args)589 void JSWebController::PostWebMessage(const JSCallbackInfo& args)
590 {
591 if (args.Length() < 1 || !args[0]->IsObject()) {
592 return;
593 }
594
595 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
596 std::string uri;
597 if (!ConvertFromJSValue(obj->GetProperty("uri"), uri)) {
598 return;
599 }
600
601 JSRef<JSVal> jsPorts = obj->GetProperty("message");
602 if (!jsPorts->IsObject()) {
603 return;
604 }
605 auto jsRes = Referenced::Claim(JSRef<JSObject>::Cast(jsPorts)->Unwrap<JSWebMessageEvent>());
606 std::string eventData = jsRes->GetDataInternal();
607 std::vector<RefPtr<JSWebMessagePort>> eventPorts = jsRes->GetPortsInternal();
608 std::vector<RefPtr<WebMessagePort>> sendPorts;
609 for (auto jsport : eventPorts) {
610 auto webPort = jsport->GetWebMessagePort();
611 if (webPort) {
612 sendPorts.push_back(webPort);
613 }
614 }
615
616 if (webController_ && sendPorts.size() >= 1) {
617 webController_->PostWebMessage(eventData, sendPorts, uri);
618 }
619 }
620
LoadUrl(const JSCallbackInfo & args)621 void JSWebController::LoadUrl(const JSCallbackInfo& args)
622 {
623 ContainerScope scope(instanceId_);
624 if (args.Length() < 1 || !args[0]->IsObject()) {
625 return;
626 }
627
628 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
629 JSRef<JSVal> valUrl = obj->GetProperty("url");
630 std::string url;
631 if (valUrl->IsObject()) {
632 // same as src process of JSWeb::Create
633 std::string webSrc;
634 if (!JSViewAbstract::ParseJsMedia(valUrl, webSrc)) {
635 return;
636 }
637 auto np = webSrc.find_first_of("/");
638 url = (np == std::string::npos) ? webSrc : webSrc.erase(np, 1);
639 } else if (!ConvertFromJSValue(valUrl, url)) {
640 return;
641 }
642
643 JSRef<JSVal> headers = obj->GetProperty("headers");
644 std::map<std::string, std::string> httpHeaders;
645 if (headers->IsArray()) {
646 JSRef<JSArray> array = JSRef<JSArray>::Cast(headers);
647 for (size_t i = 0; i < array->Length(); i++) {
648 JSRef<JSVal> jsValue = array->GetValueAt(i);
649 if (!jsValue->IsObject()) {
650 continue;
651 }
652 JSRef<JSObject> obj = JSRef<JSObject>::Cast(jsValue);
653 std::string key;
654 if (!ConvertFromJSValue(obj->GetProperty("headerKey"), key)) {
655 continue;
656 }
657 std::string value;
658 if (!ConvertFromJSValue(obj->GetProperty("headerValue"), value)) {
659 continue;
660 }
661 httpHeaders[key] = value;
662 }
663 }
664 if (webController_) {
665 webController_->LoadUrl(url, httpHeaders);
666 }
667 }
668
ExecuteTypeScript(const JSCallbackInfo & args)669 void JSWebController::ExecuteTypeScript(const JSCallbackInfo& args)
670 {
671 ContainerScope scope(instanceId_);
672 if (args.Length() < 1 || !args[0]->IsObject()) {
673 return;
674 }
675
676 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
677 std::string script;
678 if (!ConvertFromJSValue(obj->GetProperty("script"), script)) {
679 return;
680 }
681 JSRef<JSVal> tsCallback = obj->GetProperty("callback");
682 std::function<void(std::string)> callback = nullptr;
683 if (tsCallback->IsFunction()) {
684 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
685 auto jsCallback = AceType::MakeRefPtr<JsWebViewFunction>(JSRef<JSFunc>::Cast(tsCallback));
686 callback = [execCtx = args.GetExecutionContext(), func = std::move(jsCallback), node = frameNode](
687 std::string result) {
688 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
689 ACE_SCORING_EVENT("ExecuteTypeScript CallBack");
690 auto pipelineContext = PipelineContext::GetCurrentContext();
691 CHECK_NULL_VOID(pipelineContext);
692 pipelineContext->UpdateCurrentActiveNode(node);
693 func->Execute(result);
694 };
695 }
696 if (webController_) {
697 webController_->ExecuteTypeScript(script, std::move(callback));
698 }
699 }
700
LoadDataWithBaseUrl(const JSCallbackInfo & args)701 void JSWebController::LoadDataWithBaseUrl(const JSCallbackInfo& args)
702 {
703 ContainerScope scope(instanceId_);
704 if (args.Length() >= 1 && args[0]->IsObject()) {
705 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
706
707 std::string data;
708 if (!ConvertFromJSValue(obj->GetProperty("data"), data)) {
709 return;
710 }
711
712 std::string mimeType;
713 if (!ConvertFromJSValue(obj->GetProperty("mimeType"), mimeType)) {
714 return;
715 }
716
717 std::string encoding;
718 if (!ConvertFromJSValue(obj->GetProperty("encoding"), encoding)) {
719 return;
720 }
721
722 std::string baseUrl;
723 std::string historyUrl;
724 ConvertFromJSValue(obj->GetProperty("baseUrl"), baseUrl);
725 ConvertFromJSValue(obj->GetProperty("historyUrl"), historyUrl);
726 if (webController_) {
727 webController_->LoadDataWithBaseUrl(baseUrl, data, mimeType, encoding, historyUrl);
728 }
729 }
730 }
731
Backward(const JSCallbackInfo & args)732 void JSWebController::Backward(const JSCallbackInfo& args)
733 {
734 ContainerScope scope(instanceId_);
735 if (webController_) {
736 webController_->Backward();
737 }
738 }
739
Forward(const JSCallbackInfo & args)740 void JSWebController::Forward(const JSCallbackInfo& args)
741 {
742 ContainerScope scope(instanceId_);
743 if (webController_) {
744 webController_->Forward();
745 }
746 }
747
AccessStep(const JSCallbackInfo & args)748 void JSWebController::AccessStep(const JSCallbackInfo& args)
749 {
750 ContainerScope scope(instanceId_);
751 int32_t step = 0;
752 if (args.Length() < 1 || !ConvertFromJSValue(args[0], step)) {
753 return;
754 }
755 if (webController_) {
756 auto canAccess = webController_->AccessStep(step);
757 auto jsVal = JSVal(ToJSValue(canAccess));
758 auto returnValue = JSRef<JSVal>::Make(jsVal);
759 args.SetReturnValue(returnValue);
760 }
761 }
762
AccessBackward(const JSCallbackInfo & args)763 void JSWebController::AccessBackward(const JSCallbackInfo& args)
764 {
765 ContainerScope scope(instanceId_);
766 if (webController_) {
767 auto canAccess = webController_->AccessBackward();
768 auto jsVal = JSVal(ToJSValue(canAccess));
769 auto returnValue = JSRef<JSVal>::Make(jsVal);
770 args.SetReturnValue(returnValue);
771 }
772 }
773
AccessForward(const JSCallbackInfo & args)774 void JSWebController::AccessForward(const JSCallbackInfo& args)
775 {
776 ContainerScope scope(instanceId_);
777 if (webController_) {
778 auto canAccess = webController_->AccessForward();
779 auto jsVal = JSVal(ToJSValue(canAccess));
780 auto returnValue = JSRef<JSVal>::Make(jsVal);
781 args.SetReturnValue(returnValue);
782 }
783 }
784
ClearHistory(const JSCallbackInfo & args)785 void JSWebController::ClearHistory(const JSCallbackInfo& args)
786 {
787 ContainerScope scope(instanceId_);
788 if (webController_) {
789 webController_->ClearHistory();
790 }
791 }
792
ClearSslCache(const JSCallbackInfo & args)793 void JSWebController::ClearSslCache(const JSCallbackInfo& args)
794 {
795 ContainerScope scope(instanceId_);
796 if (webController_) {
797 webController_->ClearSslCache();
798 }
799 }
800
ClearClientAuthenticationCache(const JSCallbackInfo & args)801 void JSWebController::ClearClientAuthenticationCache(const JSCallbackInfo& args)
802 {
803 ContainerScope scope(instanceId_);
804 if (webController_) {
805 webController_->ClearClientAuthenticationCache();
806 }
807 }
808
Refresh(const JSCallbackInfo & args)809 void JSWebController::Refresh(const JSCallbackInfo& args)
810 {
811 ContainerScope scope(instanceId_);
812 if (webController_) {
813 webController_->Refresh();
814 }
815 }
816
StopLoading(const JSCallbackInfo & args)817 void JSWebController::StopLoading(const JSCallbackInfo& args)
818 {
819 ContainerScope scope(instanceId_);
820 if (webController_) {
821 webController_->StopLoading();
822 }
823 }
824
GetHitTestResult(const JSCallbackInfo & args)825 void JSWebController::GetHitTestResult(const JSCallbackInfo& args)
826 {
827 ContainerScope scope(instanceId_);
828 if (webController_) {
829 int result = webController_->GetHitTestResult();
830 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
831 }
832 }
833
GetHitTestValue(const JSCallbackInfo & args)834 void JSWebController::GetHitTestValue(const JSCallbackInfo& args)
835 {
836 ContainerScope scope(instanceId_);
837 if (!webController_) {
838 return;
839 }
840 HitTestResult hitResult;
841 webController_->GetHitTestValue(hitResult);
842 JSRef<JSObject> resultObj = JSClass<JSHitTestValue>::NewInstance();
843 auto result = Referenced::Claim(resultObj->Unwrap<JSHitTestValue>());
844 result->SetType(hitResult.GetHitType());
845 result->SetExtra(hitResult.GetExtraData());
846 args.SetReturnValue(resultObj);
847 }
848
GetCookieManager(const JSCallbackInfo & args)849 void JSWebController::GetCookieManager(const JSCallbackInfo& args)
850 {
851 ContainerScope scope(instanceId_);
852 if (webController_) {
853 if (!webController_->GetCookieManager()) {
854 return;
855 }
856 if (!jsWebCookieInit_) {
857 jsWebCookie_ = JSClass<JSWebCookie>::NewInstance();
858 auto jsWebCookieVal = Referenced::Claim(jsWebCookie_->Unwrap<JSWebCookie>());
859 jsWebCookieVal->SetWebCookie(webController_->GetCookieManager());
860 jsWebCookieInit_ = true;
861 }
862 args.SetReturnValue(jsWebCookie_);
863 }
864 }
865
BackOrForward(const JSCallbackInfo & args)866 void JSWebController::BackOrForward(const JSCallbackInfo& args)
867 {
868 ContainerScope scope(instanceId_);
869 int32_t step = 0;
870 if (args.Length() < 1 || !ConvertFromJSValue(args[0], step)) {
871 return;
872 }
873 if (webController_) {
874 webController_->BackOrForward(step);
875 }
876 }
877
ZoomIn(const JSCallbackInfo & args)878 void JSWebController::ZoomIn(const JSCallbackInfo& args)
879 {
880 ContainerScope scope(instanceId_);
881 if (webController_) {
882 bool result = webController_->ZoomIn();
883 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
884 }
885 }
886
ZoomOut(const JSCallbackInfo & args)887 void JSWebController::ZoomOut(const JSCallbackInfo& args)
888 {
889 ContainerScope scope(instanceId_);
890 if (webController_) {
891 bool result = webController_->ZoomOut();
892 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
893 }
894 }
895
GetProgress(const JSCallbackInfo & args)896 void JSWebController::GetProgress(const JSCallbackInfo& args)
897 {
898 if (webController_) {
899 int result = webController_->GetProgress();
900 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
901 }
902 }
903
GetPageHeight(const JSCallbackInfo & args)904 void JSWebController::GetPageHeight(const JSCallbackInfo& args)
905 {
906 ContainerScope scope(instanceId_);
907 if (webController_) {
908 int result = webController_->GetPageHeight();
909 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
910 }
911 }
912
GetTitle(const JSCallbackInfo & args)913 void JSWebController::GetTitle(const JSCallbackInfo& args)
914 {
915 ContainerScope scope(instanceId_);
916 if (webController_) {
917 std::string result = webController_->GetTitle();
918 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
919 }
920 }
921
GetWebId(const JSCallbackInfo & args)922 void JSWebController::GetWebId(const JSCallbackInfo& args)
923 {
924 ContainerScope scope(instanceId_);
925 if (webController_) {
926 int result = webController_->GetWebId();
927 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
928 }
929 }
930
GetDefaultUserAgent(const JSCallbackInfo & args)931 void JSWebController::GetDefaultUserAgent(const JSCallbackInfo& args)
932 {
933 ContainerScope scope(instanceId_);
934 if (webController_) {
935 std::string result = webController_->GetDefaultUserAgent();
936 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
937 }
938 }
939
SetJavascriptCallBackImpl()940 void JSWebController::SetJavascriptCallBackImpl()
941 {
942 if (!webController_) {
943 return;
944 }
945
946 WebController::JavaScriptCallBackImpl callback = [weak = WeakClaim(this)](const std::string& objectName,
947 const std::string& objectMethod,
948 const std::vector<std::shared_ptr<WebJSValue>>& args) {
949 auto jsWebController = weak.Upgrade();
950 if (jsWebController == nullptr) {
951 return std::make_shared<WebJSValue>(WebJSValue::Type::NONE);
952 }
953 return jsWebController->GetJavaScriptResult(objectName, objectMethod, args);
954 };
955 webController_->SetJavaScriptCallBackImpl(std::move(callback));
956 }
957
AddJavascriptInterface(const JSCallbackInfo & args)958 void JSWebController::AddJavascriptInterface(const JSCallbackInfo& args)
959 {
960 ContainerScope scope(instanceId_);
961 if (args.Length() < 1 || !args[0]->IsObject()) {
962 return;
963 }
964 if (webController_ == nullptr) {
965 return;
966 }
967 // Init webview callback
968 SetJavascriptCallBackImpl();
969
970 // options
971 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
972 // options.name
973 std::string objName;
974 if (!ConvertFromJSValue(obj->GetProperty("name"), objName)) {
975 return;
976 }
977 // options.obj
978 JSRef<JSVal> jsClassObj = obj->GetProperty("object");
979 if (!jsClassObj->IsObject()) {
980 return;
981 }
982 if (objectorMap_.find(objName) == objectorMap_.end()) {
983 objectorMap_[objName] = JSRef<JSObject>::Cast(jsClassObj);
984 }
985 // options.methodList
986 std::vector<std::string> methods;
987 JSRef<JSVal> methodList = obj->GetProperty("methodList");
988 if (methodList->IsArray()) {
989 JSRef<JSArray> array = JSRef<JSArray>::Cast(methodList);
990 for (size_t i = 0; i < array->Length(); i++) {
991 JSRef<JSVal> method = array->GetValueAt(i);
992 if (method->IsString()) {
993 methods.push_back(method->ToString());
994 }
995 }
996 }
997
998 webController_->AddJavascriptInterface(objName, methods);
999 }
1000
InitJavascriptInterface()1001 void JSWebController::InitJavascriptInterface()
1002 {
1003 if (!webController_) {
1004 return;
1005 }
1006 // Init webview callback
1007 SetJavascriptCallBackImpl();
1008 for (auto& entry : methods_) {
1009 webController_->AddJavascriptInterface(entry.first, entry.second);
1010 }
1011 }
1012
SetJavascriptInterface(const JSCallbackInfo & args)1013 void JSWebController::SetJavascriptInterface(const JSCallbackInfo& args)
1014 {
1015 if (args.Length() < 1 || !args[0]->IsObject()) {
1016 return;
1017 }
1018 if (!webController_) {
1019 return;
1020 }
1021 // options
1022 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
1023 // options.name
1024 std::string objName;
1025 if (!ConvertFromJSValue(obj->GetProperty("name"), objName)) {
1026 return;
1027 }
1028 // options.obj
1029 JSRef<JSVal> jsClassObj = obj->GetProperty("object");
1030 if (!jsClassObj->IsObject()) {
1031 return;
1032 }
1033 objectorMap_[objName] = JSRef<JSObject>::Cast(jsClassObj);
1034 std::vector<std::string> methods;
1035 methods_.clear();
1036 JSRef<JSVal> methodList = obj->GetProperty("methodList");
1037 if (!methodList->IsArray()) {
1038 return;
1039 }
1040 JSRef<JSArray> array = JSRef<JSArray>::Cast(methodList);
1041 if (array->IsArray()) {
1042 for (size_t i = 0; i < array->Length(); i++) {
1043 JSRef<JSVal> method = array->GetValueAt(i);
1044 if (method->IsString()) {
1045 methods.emplace_back(method->ToString());
1046 }
1047 }
1048 }
1049
1050 JSRef<JSVal> asyncMethodList = obj->GetProperty("asyncMethodList");
1051 if (asyncMethodList->IsArray()) {
1052 JSRef<JSArray> asyncArray = JSRef<JSArray>::Cast(asyncMethodList);
1053 for (size_t i = 0; i < asyncArray->Length(); i++) {
1054 JSRef<JSVal> asyncMethod = asyncArray->GetValueAt(i);
1055 if (asyncMethod->IsString()) {
1056 methods.emplace_back(asyncMethod->ToString());
1057 }
1058 }
1059 }
1060 methods_[objName] = methods;
1061
1062 webController_->SetInitJavascriptInterface([weak = WeakClaim(this)]() {
1063 auto jsWebcontroller = weak.Upgrade();
1064 if (jsWebcontroller) {
1065 jsWebcontroller->InitJavascriptInterface();
1066 }
1067 });
1068 }
1069
RemoveJavascriptInterface(const JSCallbackInfo & args)1070 void JSWebController::RemoveJavascriptInterface(const JSCallbackInfo& args)
1071 {
1072 ContainerScope scope(instanceId_);
1073 std::string objName;
1074 if (args.Length() < 1 || !ConvertFromJSValue(args[0], objName)) {
1075 return;
1076 }
1077 if (objectorMap_.find(objName) == objectorMap_.end()) {
1078 return;
1079 }
1080 objectorMap_.erase(objName);
1081 if (webController_) {
1082 webController_->RemoveJavascriptInterface(objName, {});
1083 }
1084 }
1085
OnInactive(const JSCallbackInfo & args)1086 void JSWebController::OnInactive(const JSCallbackInfo& args)
1087 {
1088 ContainerScope scope(instanceId_);
1089 if (webController_) {
1090 webController_->OnInactive();
1091 }
1092 }
1093
OnActive(const JSCallbackInfo & args)1094 void JSWebController::OnActive(const JSCallbackInfo& args)
1095 {
1096 ContainerScope scope(instanceId_);
1097 if (webController_) {
1098 webController_->OnActive();
1099 }
1100 }
1101
Zoom(const JSCallbackInfo & args)1102 void JSWebController::Zoom(const JSCallbackInfo& args)
1103 {
1104 ContainerScope scope(instanceId_);
1105 float factor = 1.0;
1106 if (args.Length() < 1 || !ConvertFromJSValue(args[0], factor)) {
1107 return;
1108 }
1109 if (webController_) {
1110 webController_->Zoom(factor);
1111 }
1112 }
1113
RequestFocus(const JSCallbackInfo & args)1114 void JSWebController::RequestFocus(const JSCallbackInfo& args)
1115 {
1116 ContainerScope scope(instanceId_);
1117 if (webController_) {
1118 webController_->RequestFocus();
1119 }
1120 }
1121
SearchAllAsync(const JSCallbackInfo & args)1122 void JSWebController::SearchAllAsync(const JSCallbackInfo& args)
1123 {
1124 ContainerScope scope(instanceId_);
1125 std::string searchStr;
1126 if (args.Length() < 1 || !ConvertFromJSValue(args[0], searchStr)) {
1127 return;
1128 }
1129 if (webController_) {
1130 webController_->SearchAllAsync(searchStr);
1131 }
1132 }
ClearMatches(const JSCallbackInfo & args)1133 void JSWebController::ClearMatches(const JSCallbackInfo& args)
1134 {
1135 ContainerScope scope(instanceId_);
1136 if (webController_) {
1137 webController_->ClearMatches();
1138 }
1139 }
SearchNext(const JSCallbackInfo & args)1140 void JSWebController::SearchNext(const JSCallbackInfo& args)
1141 {
1142 ContainerScope scope(instanceId_);
1143 bool forward = false;
1144 if (args.Length() < 1 || !ConvertFromJSValue(args[0], forward)) {
1145 return;
1146 }
1147
1148 if (webController_) {
1149 webController_->SearchNext(forward);
1150 }
1151 }
GetUrl(const JSCallbackInfo & args)1152 void JSWebController::GetUrl(const JSCallbackInfo& args)
1153 {
1154 ContainerScope scope(instanceId_);
1155 if (webController_) {
1156 std::string result = webController_->GetUrl();
1157 args.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
1158 }
1159 }
1160
1161 } // namespace OHOS::Ace::Framework
1162