1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/declarative_frontend/jsview/js_web.h"
17
18 #include <optional>
19 #include <string>
20
21 #include "pixel_map.h"
22 #include "pixel_map_napi.h"
23 #include "securec.h"
24
25 #include "base/log/ace_scoring_log.h"
26 #include "base/memory/ace_type.h"
27 #include "base/memory/referenced.h"
28 #include "base/utils/system_properties.h"
29 #include "base/utils/utils.h"
30 #if !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
31 #include "base/web/webview/ohos_nweb/include/nweb.h"
32 #else
33 #include "base/web/webview/ohos_interface/include/ohos_nweb/nweb.h"
34 #endif
35 #include "bridge/common/utils/engine_helper.h"
36 #include "bridge/declarative_frontend/engine/functions/js_click_function.h"
37 #include "bridge/declarative_frontend/engine/functions/js_drag_function.h"
38 #include "bridge/declarative_frontend/engine/functions/js_key_function.h"
39 #include "bridge/declarative_frontend/engine/js_converter.h"
40 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
41 #include "bridge/declarative_frontend/jsview/js_utils.h"
42 #include "bridge/declarative_frontend/jsview/js_web_controller.h"
43 #include "bridge/declarative_frontend/jsview/models/web_model_impl.h"
44 #include "bridge/declarative_frontend/view_stack_processor.h"
45 #include "core/common/ace_application_info.h"
46 #include "core/common/container.h"
47 #include "core/common/container_scope.h"
48 #include "core/components/web/web_event.h"
49 #include "core/components_ng/pattern/web/web_model_ng.h"
50
51 namespace OHOS::Ace {
52 namespace {
53 const std::string RAWFILE_PREFIX = "resource://RAWFILE/";
54 const std::string BUNDLE_NAME_PREFIX = "bundleName:";
55 const std::string MODULE_NAME_PREFIX = "moduleName:";
56
57 const int32_t SELECTION_MENU_OPTION_PARAM_INDEX = 3;
58 const int32_t SELECTION_MENU_CONTENT_PARAM_INDEX = 2;
59
EraseSpace(std::string& data)60 void EraseSpace(std::string& data)
61 {
62 auto iter = data.begin();
63 while (iter != data.end()) {
64 if (isspace(*iter)) {
65 iter = data.erase(iter);
66 } else {
67 ++iter;
68 }
69 }
70 }
71 }
72
73 std::unique_ptr<WebModel> WebModel::instance_ = nullptr;
74 std::mutex WebModel::mutex_;
GetInstance()75 WebModel* WebModel::GetInstance()
76 {
77 if (!instance_) {
78 std::lock_guard<std::mutex> lock(mutex_);
79 if (!instance_) {
80 #ifdef NG_BUILD
81 instance_.reset(new NG::WebModelNG());
82 #else
83 if (Container::IsCurrentUseNewPipeline()) {
84 instance_.reset(new NG::WebModelNG());
85 } else {
86 instance_.reset(new Framework::WebModelImpl());
87 }
88 #endif
89 }
90 }
91 return instance_.get();
92 }
93
94 } // namespace OHOS::Ace
95
96 namespace OHOS::Ace::Framework {
97 bool JSWeb::webDebuggingAccess_ = false;
98 class JSWebDialog : public Referenced {
99 public:
JSBind(BindingTarget globalObj)100 static void JSBind(BindingTarget globalObj)
101 {
102 JSClass<JSWebDialog>::Declare("WebDialog");
103 JSClass<JSWebDialog>::CustomMethod("handleConfirm", &JSWebDialog::Confirm);
104 JSClass<JSWebDialog>::CustomMethod("handleCancel", &JSWebDialog::Cancel);
105 JSClass<JSWebDialog>::CustomMethod("handlePromptConfirm", &JSWebDialog::PromptConfirm);
106 JSClass<JSWebDialog>::Bind(globalObj, &JSWebDialog::Constructor, &JSWebDialog::Destructor);
107 }
108
SetResult(const RefPtr<Result>& result)109 void SetResult(const RefPtr<Result>& result)
110 {
111 result_ = result;
112 }
113
Confirm(const JSCallbackInfo& args)114 void Confirm(const JSCallbackInfo& args)
115 {
116 if (result_) {
117 result_->Confirm();
118 }
119 }
120
PromptConfirm(const JSCallbackInfo& args)121 void PromptConfirm(const JSCallbackInfo& args)
122 {
123 std::string message;
124 if (!result_) {
125 return;
126 }
127 if (args.Length() == 1 && args[0]->IsString()) {
128 message = args[0]->ToString();
129 result_->Confirm(message);
130 }
131 }
132
Cancel(const JSCallbackInfo& args)133 void Cancel(const JSCallbackInfo& args)
134 {
135 if (result_) {
136 result_->Cancel();
137 }
138 }
139
140 private:
Constructor(const JSCallbackInfo& args)141 static void Constructor(const JSCallbackInfo& args)
142 {
143 auto jsWebDialog = Referenced::MakeRefPtr<JSWebDialog>();
144 jsWebDialog->IncRefCount();
145 args.SetReturnValue(Referenced::RawPtr(jsWebDialog));
146 }
147
Destructor(JSWebDialog* jsWebDialog)148 static void Destructor(JSWebDialog* jsWebDialog)
149 {
150 if (jsWebDialog != nullptr) {
151 jsWebDialog->DecRefCount();
152 }
153 }
154
155 RefPtr<Result> result_;
156 };
157
158 class JSFullScreenExitHandler : public Referenced {
159 public:
JSBind(BindingTarget globalObj)160 static void JSBind(BindingTarget globalObj)
161 {
162 JSClass<JSFullScreenExitHandler>::Declare("FullScreenExitHandler");
163 JSClass<JSFullScreenExitHandler>::CustomMethod("exitFullScreen", &JSFullScreenExitHandler::ExitFullScreen);
164 JSClass<JSFullScreenExitHandler>::Bind(
165 globalObj, &JSFullScreenExitHandler::Constructor, &JSFullScreenExitHandler::Destructor);
166 }
167
SetHandler(const RefPtr<FullScreenExitHandler>& handler)168 void SetHandler(const RefPtr<FullScreenExitHandler>& handler)
169 {
170 fullScreenExitHandler_ = handler;
171 }
172
ExitFullScreen(const JSCallbackInfo& args)173 void ExitFullScreen(const JSCallbackInfo& args)
174 {
175 if (fullScreenExitHandler_) {
176 fullScreenExitHandler_->ExitFullScreen();
177 }
178 }
179
180 private:
Constructor(const JSCallbackInfo& args)181 static void Constructor(const JSCallbackInfo& args)
182 {
183 auto jsFullScreenExitHandler = Referenced::MakeRefPtr<JSFullScreenExitHandler>();
184 jsFullScreenExitHandler->IncRefCount();
185 args.SetReturnValue(Referenced::RawPtr(jsFullScreenExitHandler));
186 }
187
Destructor(JSFullScreenExitHandler* jsFullScreenExitHandler)188 static void Destructor(JSFullScreenExitHandler* jsFullScreenExitHandler)
189 {
190 if (jsFullScreenExitHandler != nullptr) {
191 jsFullScreenExitHandler->DecRefCount();
192 }
193 }
194 RefPtr<FullScreenExitHandler> fullScreenExitHandler_;
195 };
196
197 class JSWebKeyboardController : public Referenced {
198 public:
JSBind(BindingTarget globalObj)199 static void JSBind(BindingTarget globalObj)
200 {
201 JSClass<JSWebKeyboardController>::Declare("WebKeyboardController");
202 JSClass<JSWebKeyboardController>::CustomMethod("insertText", &JSWebKeyboardController::InsertText);
203 JSClass<JSWebKeyboardController>::CustomMethod("deleteForward", &JSWebKeyboardController::DeleteForward);
204 JSClass<JSWebKeyboardController>::CustomMethod("deleteBackward", &JSWebKeyboardController::DeleteBackward);
205 JSClass<JSWebKeyboardController>::CustomMethod("sendFunctionKey", &JSWebKeyboardController::SendFunctionKey);
206 JSClass<JSWebKeyboardController>::CustomMethod("close", &JSWebKeyboardController::Close);
207 JSClass<JSWebKeyboardController>::Bind(
208 globalObj, &JSWebKeyboardController::Constructor, &JSWebKeyboardController::Destructor);
209 }
210
SeWebKeyboardController(const RefPtr<WebCustomKeyboardHandler>& controller)211 void SeWebKeyboardController(const RefPtr<WebCustomKeyboardHandler>& controller)
212 {
213 webKeyboardController_ = controller;
214 }
215
InsertText(const JSCallbackInfo& args)216 void InsertText(const JSCallbackInfo& args)
217 {
218 if (!webKeyboardController_) {
219 return;
220 }
221
222 if (args.Length() < 1 || !(args[0]->IsString())) {
223 return;
224 }
225
226 webKeyboardController_->InsertText(args[0]->ToString());
227 }
228
DeleteForward(const JSCallbackInfo& args)229 void DeleteForward(const JSCallbackInfo& args)
230 {
231 if (!webKeyboardController_) {
232 return;
233 }
234
235 if (args.Length() < 1 || !(args[0]->IsNumber())) {
236 return;
237 }
238
239 webKeyboardController_->DeleteForward(args[0]->ToNumber<int32_t>());
240 }
241
DeleteBackward(const JSCallbackInfo& args)242 void DeleteBackward(const JSCallbackInfo& args)
243 {
244 if (!webKeyboardController_) {
245 return;
246 }
247
248 if (args.Length() < 1 || !(args[0]->IsNumber())) {
249 return;
250 }
251
252 webKeyboardController_->DeleteBackward(args[0]->ToNumber<int32_t>());
253 }
254
SendFunctionKey(const JSCallbackInfo& args)255 void SendFunctionKey(const JSCallbackInfo& args)
256 {
257 if (!webKeyboardController_) {
258 return;
259 }
260
261 if (args.Length() < 1 || !(args[0]->IsNumber())) {
262 return;
263 }
264
265 webKeyboardController_->SendFunctionKey(args[0]->ToNumber<int32_t>());
266 }
267
Close(const JSCallbackInfo& args)268 void Close(const JSCallbackInfo& args)
269 {
270 webKeyboardController_->Close();
271 }
272
273 private:
Constructor(const JSCallbackInfo& args)274 static void Constructor(const JSCallbackInfo& args)
275 {
276 auto jSWebKeyboardController = Referenced::MakeRefPtr<JSWebKeyboardController>();
277 jSWebKeyboardController->IncRefCount();
278 args.SetReturnValue(Referenced::RawPtr(jSWebKeyboardController));
279 }
280
Destructor(JSWebKeyboardController* jSWebKeyboardController)281 static void Destructor(JSWebKeyboardController* jSWebKeyboardController)
282 {
283 if (jSWebKeyboardController != nullptr) {
284 jSWebKeyboardController->DecRefCount();
285 }
286 }
287 RefPtr<WebCustomKeyboardHandler> webKeyboardController_;
288 };
289
290 class JSWebHttpAuth : public Referenced {
291 public:
JSBind(BindingTarget globalObj)292 static void JSBind(BindingTarget globalObj)
293 {
294 JSClass<JSWebHttpAuth>::Declare("WebHttpAuthResult");
295 JSClass<JSWebHttpAuth>::CustomMethod("confirm", &JSWebHttpAuth::Confirm);
296 JSClass<JSWebHttpAuth>::CustomMethod("cancel", &JSWebHttpAuth::Cancel);
297 JSClass<JSWebHttpAuth>::CustomMethod("isHttpAuthInfoSaved", &JSWebHttpAuth::IsHttpAuthInfoSaved);
298 JSClass<JSWebHttpAuth>::Bind(globalObj, &JSWebHttpAuth::Constructor, &JSWebHttpAuth::Destructor);
299 }
300
SetResult(const RefPtr<AuthResult>& result)301 void SetResult(const RefPtr<AuthResult>& result)
302 {
303 result_ = result;
304 }
305
Confirm(const JSCallbackInfo& args)306 void Confirm(const JSCallbackInfo& args)
307 {
308 if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsString()) {
309 auto code = JSVal(ToJSValue(false));
310 auto descriptionRef = JSRef<JSVal>::Make(code);
311 args.SetReturnValue(descriptionRef);
312 return;
313 }
314 std::string userName = args[0]->ToString();
315 std::string password = args[1]->ToString();
316 bool ret = false;
317 if (result_) {
318 result_->Confirm(userName, password);
319 ret = true;
320 }
321 auto code = JSVal(ToJSValue(ret));
322 auto descriptionRef = JSRef<JSVal>::Make(code);
323 args.SetReturnValue(descriptionRef);
324 }
325
Cancel(const JSCallbackInfo& args)326 void Cancel(const JSCallbackInfo& args)
327 {
328 if (result_) {
329 result_->Cancel();
330 }
331 }
332
IsHttpAuthInfoSaved(const JSCallbackInfo& args)333 void IsHttpAuthInfoSaved(const JSCallbackInfo& args)
334 {
335 bool ret = false;
336 if (result_) {
337 ret = result_->IsHttpAuthInfoSaved();
338 }
339 auto code = JSVal(ToJSValue(ret));
340 auto descriptionRef = JSRef<JSVal>::Make(code);
341 args.SetReturnValue(descriptionRef);
342 }
343
344 private:
Constructor(const JSCallbackInfo& args)345 static void Constructor(const JSCallbackInfo& args)
346 {
347 auto jsWebHttpAuth = Referenced::MakeRefPtr<JSWebHttpAuth>();
348 jsWebHttpAuth->IncRefCount();
349 args.SetReturnValue(Referenced::RawPtr(jsWebHttpAuth));
350 }
351
Destructor(JSWebHttpAuth* jsWebHttpAuth)352 static void Destructor(JSWebHttpAuth* jsWebHttpAuth)
353 {
354 if (jsWebHttpAuth != nullptr) {
355 jsWebHttpAuth->DecRefCount();
356 }
357 }
358
359 RefPtr<AuthResult> result_;
360 };
361
362 class JSWebSslError : public Referenced {
363 public:
JSBind(BindingTarget globalObj)364 static void JSBind(BindingTarget globalObj)
365 {
366 JSClass<JSWebSslError>::Declare("WebSslErrorResult");
367 JSClass<JSWebSslError>::CustomMethod("handleConfirm", &JSWebSslError::HandleConfirm);
368 JSClass<JSWebSslError>::CustomMethod("handleCancel", &JSWebSslError::HandleCancel);
369 JSClass<JSWebSslError>::Bind(globalObj, &JSWebSslError::Constructor, &JSWebSslError::Destructor);
370 }
371
SetResult(const RefPtr<SslErrorResult>& result)372 void SetResult(const RefPtr<SslErrorResult>& result)
373 {
374 result_ = result;
375 }
376
HandleConfirm(const JSCallbackInfo& args)377 void HandleConfirm(const JSCallbackInfo& args)
378 {
379 if (result_) {
380 result_->HandleConfirm();
381 }
382 }
383
HandleCancel(const JSCallbackInfo& args)384 void HandleCancel(const JSCallbackInfo& args)
385 {
386 if (result_) {
387 result_->HandleCancel();
388 }
389 }
390
391 private:
Constructor(const JSCallbackInfo& args)392 static void Constructor(const JSCallbackInfo& args)
393 {
394 auto jsWebSslError = Referenced::MakeRefPtr<JSWebSslError>();
395 jsWebSslError->IncRefCount();
396 args.SetReturnValue(Referenced::RawPtr(jsWebSslError));
397 }
398
Destructor(JSWebSslError* jsWebSslError)399 static void Destructor(JSWebSslError* jsWebSslError)
400 {
401 if (jsWebSslError != nullptr) {
402 jsWebSslError->DecRefCount();
403 }
404 }
405
406 RefPtr<SslErrorResult> result_;
407 };
408
409 class JSWebAllSslError : public Referenced {
410 public:
JSBind(BindingTarget globalObj)411 static void JSBind(BindingTarget globalObj)
412 {
413 JSClass<JSWebAllSslError>::Declare("WebAllSslErrorResult");
414 JSClass<JSWebAllSslError>::CustomMethod("handleConfirm", &JSWebAllSslError::HandleConfirm);
415 JSClass<JSWebAllSslError>::CustomMethod("handleCancel", &JSWebAllSslError::HandleCancel);
416 JSClass<JSWebAllSslError>::Bind(globalObj, &JSWebAllSslError::Constructor, &JSWebAllSslError::Destructor);
417 }
418
SetResult(const RefPtr<AllSslErrorResult>& result)419 void SetResult(const RefPtr<AllSslErrorResult>& result)
420 {
421 result_ = result;
422 }
423
HandleConfirm(const JSCallbackInfo& args)424 void HandleConfirm(const JSCallbackInfo& args)
425 {
426 if (result_) {
427 result_->HandleConfirm();
428 }
429 }
430
HandleCancel(const JSCallbackInfo& args)431 void HandleCancel(const JSCallbackInfo& args)
432 {
433 if (result_) {
434 result_->HandleCancel();
435 }
436 }
437
438 private:
Constructor(const JSCallbackInfo& args)439 static void Constructor(const JSCallbackInfo& args)
440 {
441 auto jsWebAllSslError = Referenced::MakeRefPtr<JSWebAllSslError>();
442 jsWebAllSslError->IncRefCount();
443 args.SetReturnValue(Referenced::RawPtr(jsWebAllSslError));
444 }
445
Destructor(JSWebAllSslError* jsWebAllSslError)446 static void Destructor(JSWebAllSslError* jsWebAllSslError)
447 {
448 if (jsWebAllSslError != nullptr) {
449 jsWebAllSslError->DecRefCount();
450 }
451 }
452
453 RefPtr<AllSslErrorResult> result_;
454 };
455
456 class JSWebSslSelectCert : public Referenced {
457 public:
JSBind(BindingTarget globalObj)458 static void JSBind(BindingTarget globalObj)
459 {
460 JSClass<JSWebSslSelectCert>::Declare("WebSslSelectCertResult");
461 JSClass<JSWebSslSelectCert>::CustomMethod("confirm", &JSWebSslSelectCert::HandleConfirm);
462 JSClass<JSWebSslSelectCert>::CustomMethod("cancel", &JSWebSslSelectCert::HandleCancel);
463 JSClass<JSWebSslSelectCert>::CustomMethod("ignore", &JSWebSslSelectCert::HandleIgnore);
464 JSClass<JSWebSslSelectCert>::Bind(globalObj, &JSWebSslSelectCert::Constructor, &JSWebSslSelectCert::Destructor);
465 }
466
SetResult(const RefPtr<SslSelectCertResult>& result)467 void SetResult(const RefPtr<SslSelectCertResult>& result)
468 {
469 result_ = result;
470 }
471
HandleConfirm(const JSCallbackInfo& args)472 void HandleConfirm(const JSCallbackInfo& args)
473 {
474 std::string privateKeyFile;
475 std::string certChainFile;
476 if (args.Length() == 1 && args[0]->IsString()) {
477 privateKeyFile = args[0]->ToString();
478 } else if (args.Length() == 2 && args[0]->IsString() && args[1]->IsString()) {
479 privateKeyFile = args[0]->ToString();
480 certChainFile = args[1]->ToString();
481 } else {
482 return;
483 }
484
485 if (result_) {
486 result_->HandleConfirm(privateKeyFile, certChainFile);
487 }
488 }
489
HandleCancel(const JSCallbackInfo& args)490 void HandleCancel(const JSCallbackInfo& args)
491 {
492 if (result_) {
493 result_->HandleCancel();
494 }
495 }
496
HandleIgnore(const JSCallbackInfo& args)497 void HandleIgnore(const JSCallbackInfo& args)
498 {
499 if (result_) {
500 result_->HandleIgnore();
501 }
502 }
503
504 private:
Constructor(const JSCallbackInfo& args)505 static void Constructor(const JSCallbackInfo& args)
506 {
507 auto jsWebSslSelectCert = Referenced::MakeRefPtr<JSWebSslSelectCert>();
508 jsWebSslSelectCert->IncRefCount();
509 args.SetReturnValue(Referenced::RawPtr(jsWebSslSelectCert));
510 }
511
Destructor(JSWebSslSelectCert* jsWebSslSelectCert)512 static void Destructor(JSWebSslSelectCert* jsWebSslSelectCert)
513 {
514 if (jsWebSslSelectCert != nullptr) {
515 jsWebSslSelectCert->DecRefCount();
516 }
517 }
518
519 RefPtr<SslSelectCertResult> result_;
520 };
521
522 class JSWebConsoleLog : public Referenced {
523 public:
JSBind(BindingTarget globalObj)524 static void JSBind(BindingTarget globalObj)
525 {
526 JSClass<JSWebConsoleLog>::Declare("ConsoleMessage");
527 JSClass<JSWebConsoleLog>::CustomMethod("getLineNumber", &JSWebConsoleLog::GetLineNumber);
528 JSClass<JSWebConsoleLog>::CustomMethod("getMessage", &JSWebConsoleLog::GetLog);
529 JSClass<JSWebConsoleLog>::CustomMethod("getMessageLevel", &JSWebConsoleLog::GetLogLevel);
530 JSClass<JSWebConsoleLog>::CustomMethod("getSourceId", &JSWebConsoleLog::GetSourceId);
531 JSClass<JSWebConsoleLog>::Bind(globalObj, &JSWebConsoleLog::Constructor, &JSWebConsoleLog::Destructor);
532 }
533
SetMessage(const RefPtr<WebConsoleLog>& message)534 void SetMessage(const RefPtr<WebConsoleLog>& message)
535 {
536 message_ = message;
537 }
538
GetLineNumber(const JSCallbackInfo& args)539 void GetLineNumber(const JSCallbackInfo& args)
540 {
541 auto code = JSVal(ToJSValue(message_->GetLineNumber()));
542 auto descriptionRef = JSRef<JSVal>::Make(code);
543 args.SetReturnValue(descriptionRef);
544 }
545
GetLog(const JSCallbackInfo& args)546 void GetLog(const JSCallbackInfo& args)
547 {
548 auto code = JSVal(ToJSValue(message_->GetLog()));
549 auto descriptionRef = JSRef<JSVal>::Make(code);
550 args.SetReturnValue(descriptionRef);
551 }
552
GetLogLevel(const JSCallbackInfo& args)553 void GetLogLevel(const JSCallbackInfo& args)
554 {
555 auto code = JSVal(ToJSValue(message_->GetLogLevel()));
556 auto descriptionRef = JSRef<JSVal>::Make(code);
557 args.SetReturnValue(descriptionRef);
558 }
559
GetSourceId(const JSCallbackInfo& args)560 void GetSourceId(const JSCallbackInfo& args)
561 {
562 auto code = JSVal(ToJSValue(message_->GetSourceId()));
563 auto descriptionRef = JSRef<JSVal>::Make(code);
564 args.SetReturnValue(descriptionRef);
565 }
566
567 private:
Constructor(const JSCallbackInfo& args)568 static void Constructor(const JSCallbackInfo& args)
569 {
570 auto jsWebConsoleLog = Referenced::MakeRefPtr<JSWebConsoleLog>();
571 jsWebConsoleLog->IncRefCount();
572 args.SetReturnValue(Referenced::RawPtr(jsWebConsoleLog));
573 }
574
Destructor(JSWebConsoleLog* jsWebConsoleLog)575 static void Destructor(JSWebConsoleLog* jsWebConsoleLog)
576 {
577 if (jsWebConsoleLog != nullptr) {
578 jsWebConsoleLog->DecRefCount();
579 }
580 }
581
582 RefPtr<WebConsoleLog> message_;
583 };
584
585 class JSWebGeolocation : public Referenced {
586 public:
JSBind(BindingTarget globalObj)587 static void JSBind(BindingTarget globalObj)
588 {
589 JSClass<JSWebGeolocation>::Declare("WebGeolocation");
590 JSClass<JSWebGeolocation>::CustomMethod("invoke", &JSWebGeolocation::Invoke);
591 JSClass<JSWebGeolocation>::Bind(globalObj, &JSWebGeolocation::Constructor, &JSWebGeolocation::Destructor);
592 }
593
SetEvent(const LoadWebGeolocationShowEvent& eventInfo)594 void SetEvent(const LoadWebGeolocationShowEvent& eventInfo)
595 {
596 webGeolocation_ = eventInfo.GetWebGeolocation();
597 }
598
Invoke(const JSCallbackInfo& args)599 void Invoke(const JSCallbackInfo& args)
600 {
601 std::string origin;
602 bool allow = false;
603 bool retain = false;
604 if (args[0]->IsString()) {
605 origin = args[0]->ToString();
606 }
607 if (args[1]->IsBoolean()) {
608 allow = args[1]->ToBoolean();
609 }
610 if (args[2]->IsBoolean()) {
611 retain = args[2]->ToBoolean();
612 }
613 if (webGeolocation_) {
614 webGeolocation_->Invoke(origin, allow, retain);
615 }
616 }
617
618 private:
Constructor(const JSCallbackInfo& args)619 static void Constructor(const JSCallbackInfo& args)
620 {
621 auto jsWebGeolocation = Referenced::MakeRefPtr<JSWebGeolocation>();
622 jsWebGeolocation->IncRefCount();
623 args.SetReturnValue(Referenced::RawPtr(jsWebGeolocation));
624 }
625
Destructor(JSWebGeolocation* jsWebGeolocation)626 static void Destructor(JSWebGeolocation* jsWebGeolocation)
627 {
628 if (jsWebGeolocation != nullptr) {
629 jsWebGeolocation->DecRefCount();
630 }
631 }
632
633 RefPtr<WebGeolocation> webGeolocation_;
634 };
635
636 class JSWebPermissionRequest : public Referenced {
637 public:
JSBind(BindingTarget globalObj)638 static void JSBind(BindingTarget globalObj)
639 {
640 JSClass<JSWebPermissionRequest>::Declare("WebPermissionRequest");
641 JSClass<JSWebPermissionRequest>::CustomMethod("deny", &JSWebPermissionRequest::Deny);
642 JSClass<JSWebPermissionRequest>::CustomMethod("getOrigin", &JSWebPermissionRequest::GetOrigin);
643 JSClass<JSWebPermissionRequest>::CustomMethod("getAccessibleResource", &JSWebPermissionRequest::GetResources);
644 JSClass<JSWebPermissionRequest>::CustomMethod("grant", &JSWebPermissionRequest::Grant);
645 JSClass<JSWebPermissionRequest>::Bind(
646 globalObj, &JSWebPermissionRequest::Constructor, &JSWebPermissionRequest::Destructor);
647 }
648
SetEvent(const WebPermissionRequestEvent& eventInfo)649 void SetEvent(const WebPermissionRequestEvent& eventInfo)
650 {
651 webPermissionRequest_ = eventInfo.GetWebPermissionRequest();
652 }
653
Deny(const JSCallbackInfo& args)654 void Deny(const JSCallbackInfo& args)
655 {
656 if (webPermissionRequest_) {
657 webPermissionRequest_->Deny();
658 }
659 }
660
GetOrigin(const JSCallbackInfo& args)661 void GetOrigin(const JSCallbackInfo& args)
662 {
663 std::string origin;
664 if (webPermissionRequest_) {
665 origin = webPermissionRequest_->GetOrigin();
666 }
667 auto originJs = JSVal(ToJSValue(origin));
668 auto originJsRef = JSRef<JSVal>::Make(originJs);
669 args.SetReturnValue(originJsRef);
670 }
671
GetResources(const JSCallbackInfo& args)672 void GetResources(const JSCallbackInfo& args)
673 {
674 JSRef<JSArray> result = JSRef<JSArray>::New();
675 if (webPermissionRequest_) {
676 std::vector<std::string> resources = webPermissionRequest_->GetResources();
677 uint32_t index = 0;
678 for (auto iterator = resources.begin(); iterator != resources.end(); ++iterator) {
679 auto valueStr = JSVal(ToJSValue(*iterator));
680 auto value = JSRef<JSVal>::Make(valueStr);
681 result->SetValueAt(index++, value);
682 }
683 }
684 args.SetReturnValue(result);
685 }
686
Grant(const JSCallbackInfo& args)687 void Grant(const JSCallbackInfo& args)
688 {
689 if (args.Length() < 1) {
690 if (webPermissionRequest_) {
691 webPermissionRequest_->Deny();
692 }
693 }
694 std::vector<std::string> resources;
695 if (args[0]->IsArray()) {
696 JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
697 for (size_t i = 0; i < array->Length(); i++) {
698 JSRef<JSVal> val = array->GetValueAt(i);
699 if (!val->IsString()) {
700 continue;
701 }
702 std::string res;
703 if (!ConvertFromJSValue(val, res)) {
704 continue;
705 }
706 resources.push_back(res);
707 }
708 }
709
710 if (webPermissionRequest_) {
711 webPermissionRequest_->Grant(resources);
712 }
713 }
714
715 private:
Constructor(const JSCallbackInfo& args)716 static void Constructor(const JSCallbackInfo& args)
717 {
718 auto jsWebPermissionRequest = Referenced::MakeRefPtr<JSWebPermissionRequest>();
719 jsWebPermissionRequest->IncRefCount();
720 args.SetReturnValue(Referenced::RawPtr(jsWebPermissionRequest));
721 }
722
Destructor(JSWebPermissionRequest* jsWebPermissionRequest)723 static void Destructor(JSWebPermissionRequest* jsWebPermissionRequest)
724 {
725 if (jsWebPermissionRequest != nullptr) {
726 jsWebPermissionRequest->DecRefCount();
727 }
728 }
729
730 RefPtr<WebPermissionRequest> webPermissionRequest_;
731 };
732
733 class JSScreenCaptureRequest : public Referenced {
734 public:
JSBind(BindingTarget globalObj)735 static void JSBind(BindingTarget globalObj)
736 {
737 JSClass<JSScreenCaptureRequest>::Declare("ScreenCaptureRequest");
738 JSClass<JSScreenCaptureRequest>::CustomMethod("deny", &JSScreenCaptureRequest::Deny);
739 JSClass<JSScreenCaptureRequest>::CustomMethod("getOrigin", &JSScreenCaptureRequest::GetOrigin);
740 JSClass<JSScreenCaptureRequest>::CustomMethod("grant", &JSScreenCaptureRequest::Grant);
741 JSClass<JSScreenCaptureRequest>::Bind(
742 globalObj, &JSScreenCaptureRequest::Constructor, &JSScreenCaptureRequest::Destructor);
743 }
744
SetEvent(const WebScreenCaptureRequestEvent& eventInfo)745 void SetEvent(const WebScreenCaptureRequestEvent& eventInfo)
746 {
747 request_ = eventInfo.GetWebScreenCaptureRequest();
748 }
749
Deny(const JSCallbackInfo& args)750 void Deny(const JSCallbackInfo& args)
751 {
752 if (request_) {
753 request_->Deny();
754 }
755 }
756
GetOrigin(const JSCallbackInfo& args)757 void GetOrigin(const JSCallbackInfo& args)
758 {
759 std::string origin;
760 if (request_) {
761 origin = request_->GetOrigin();
762 }
763 auto originJs = JSVal(ToJSValue(origin));
764 auto originJsRef = JSRef<JSVal>::Make(originJs);
765 args.SetReturnValue(originJsRef);
766 }
767
Grant(const JSCallbackInfo& args)768 void Grant(const JSCallbackInfo& args)
769 {
770 if (!request_) {
771 return;
772 }
773 if (args.Length() < 1 || !args[0]->IsObject()) {
774 request_->Deny();
775 return;
776 }
777 JSRef<JSObject> paramObject = JSRef<JSObject>::Cast(args[0]);
778 auto captureModeObj = paramObject->GetProperty("captureMode");
779 if (!captureModeObj->IsNumber()) {
780 request_->Deny();
781 return;
782 }
783 int32_t captureMode = captureModeObj->ToNumber<int32_t>();
784 request_->SetCaptureMode(captureMode);
785 request_->SetSourceId(-1);
786 request_->Grant();
787 }
788
789 private:
Constructor(const JSCallbackInfo& args)790 static void Constructor(const JSCallbackInfo& args)
791 {
792 auto jsScreenCaptureRequest = Referenced::MakeRefPtr<JSScreenCaptureRequest>();
793 jsScreenCaptureRequest->IncRefCount();
794 args.SetReturnValue(Referenced::RawPtr(jsScreenCaptureRequest));
795 }
796
Destructor(JSScreenCaptureRequest* jsScreenCaptureRequest)797 static void Destructor(JSScreenCaptureRequest* jsScreenCaptureRequest)
798 {
799 if (jsScreenCaptureRequest != nullptr) {
800 jsScreenCaptureRequest->DecRefCount();
801 }
802 }
803
804 RefPtr<WebScreenCaptureRequest> request_;
805 };
806
807 class JSNativeEmbedGestureRequest : public Referenced {
808 public:
JSBind(BindingTarget globalObj)809 static void JSBind(BindingTarget globalObj)
810 {
811 JSClass<JSNativeEmbedGestureRequest>::Declare("NativeEmbedGesture");
812 JSClass<JSNativeEmbedGestureRequest>::CustomMethod(
813 "setGestureEventResult", &JSNativeEmbedGestureRequest::SetGestureEventResult);
814 JSClass<JSNativeEmbedGestureRequest>::Bind(
815 globalObj, &JSNativeEmbedGestureRequest::Constructor, &JSNativeEmbedGestureRequest::Destructor);
816 }
817
SetResult(const RefPtr<GestureEventResult>& result)818 void SetResult(const RefPtr<GestureEventResult>& result)
819 {
820 eventResult_ = result;
821 }
822
SetGestureEventResult(const JSCallbackInfo& args)823 void SetGestureEventResult(const JSCallbackInfo& args)
824 {
825 if (eventResult_) {
826 bool result = true;
827 if (args.Length() == 1 && args[0]->IsBoolean()) {
828 result = args[0]->ToBoolean();
829 eventResult_->SetGestureEventResult(result);
830 }
831 }
832 }
833
834 private:
Constructor(const JSCallbackInfo& args)835 static void Constructor(const JSCallbackInfo& args)
836 {
837 auto jSNativeEmbedGestureRequest = Referenced::MakeRefPtr<JSNativeEmbedGestureRequest>();
838 jSNativeEmbedGestureRequest->IncRefCount();
839 args.SetReturnValue(Referenced::RawPtr(jSNativeEmbedGestureRequest));
840 }
841
Destructor(JSNativeEmbedGestureRequest* jSNativeEmbedGestureRequest)842 static void Destructor(JSNativeEmbedGestureRequest* jSNativeEmbedGestureRequest)
843 {
844 if (jSNativeEmbedGestureRequest != nullptr) {
845 jSNativeEmbedGestureRequest->DecRefCount();
846 }
847 }
848
849 RefPtr<GestureEventResult> eventResult_;
850 };
851
852 class JSWebWindowNewHandler : public Referenced {
853 public:
854 struct ChildWindowInfo {
855 int32_t parentWebId_ = -1;
856 JSRef<JSObject> controller_;
857 };
858
JSBind(BindingTarget globalObj)859 static void JSBind(BindingTarget globalObj)
860 {
861 JSClass<JSWebWindowNewHandler>::Declare("WebWindowNewHandler");
862 JSClass<JSWebWindowNewHandler>::CustomMethod("setWebController", &JSWebWindowNewHandler::SetWebController);
863 JSClass<JSWebWindowNewHandler>::Bind(
864 globalObj, &JSWebWindowNewHandler::Constructor, &JSWebWindowNewHandler::Destructor);
865 }
866
SetEvent(const WebWindowNewEvent& eventInfo)867 void SetEvent(const WebWindowNewEvent& eventInfo)
868 {
869 handler_ = eventInfo.GetWebWindowNewHandler();
870 }
871
PopController(int32_t id, int32_t* parentId = nullptr)872 static JSRef<JSObject> PopController(int32_t id, int32_t* parentId = nullptr)
873 {
874 auto iter = controller_map_.find(id);
875 if (iter == controller_map_.end()) {
876 return JSRef<JSVal>::Make();
877 }
878 auto controller = iter->second.controller_;
879 if (parentId) {
880 *parentId = iter->second.parentWebId_;
881 }
882 controller_map_.erase(iter);
883 return controller;
884 }
885
ExistController(JSRef<JSObject>& controller, int32_t& parentWebId)886 static bool ExistController(JSRef<JSObject>& controller, int32_t& parentWebId)
887 {
888 auto getThisVarFunction = controller->GetProperty("innerGetThisVar");
889 if (!getThisVarFunction->IsFunction()) {
890 parentWebId = -1;
891 return false;
892 }
893 auto func = JSRef<JSFunc>::Cast(getThisVarFunction);
894 auto thisVar = func->Call(controller, 0, {});
895 int64_t thisPtr = thisVar->ToNumber<int64_t>();
896 for (auto iter = controller_map_.begin(); iter != controller_map_.end(); iter++) {
897 auto getThisVarFunction1 = iter->second.controller_->GetProperty("innerGetThisVar");
898 if (getThisVarFunction1->IsFunction()) {
899 auto func1 = JSRef<JSFunc>::Cast(getThisVarFunction1);
900 auto thisVar1 = func1->Call(iter->second.controller_, 0, {});
901 if (thisPtr == thisVar1->ToNumber<int64_t>()) {
902 parentWebId = iter->second.parentWebId_;
903 return true;
904 }
905 }
906 }
907 parentWebId = -1;
908 return false;
909 }
910
SetWebController(const JSCallbackInfo& args)911 void SetWebController(const JSCallbackInfo& args)
912 {
913 if (handler_) {
914 int32_t parentNWebId = handler_->GetParentNWebId();
915 if (parentNWebId == -1) {
916 return;
917 }
918 if (args.Length() < 1 || !args[0]->IsObject()) {
919 WebModel::GetInstance()->NotifyPopupWindowResult(parentNWebId, false);
920 return;
921 }
922 auto controller = JSRef<JSObject>::Cast(args[0]);
923 if (controller.IsEmpty()) {
924 WebModel::GetInstance()->NotifyPopupWindowResult(parentNWebId, false);
925 return;
926 }
927 auto getWebIdFunction = controller->GetProperty("innerGetWebId");
928 if (!getWebIdFunction->IsFunction()) {
929 WebModel::GetInstance()->NotifyPopupWindowResult(parentNWebId, false);
930 return;
931 }
932 auto func = JSRef<JSFunc>::Cast(getWebIdFunction);
933 auto webId = func->Call(controller, 0, {});
934 int32_t childWebId = webId->ToNumber<int32_t>();
935 if (childWebId == parentNWebId || childWebId != -1) {
936 WebModel::GetInstance()->NotifyPopupWindowResult(parentNWebId, false);
937 return;
938 }
939 controller_map_.insert(
940 std::pair<int32_t, ChildWindowInfo>(handler_->GetId(), { parentNWebId, controller }));
941 }
942 }
943
944 private:
Constructor(const JSCallbackInfo& args)945 static void Constructor(const JSCallbackInfo& args)
946 {
947 auto jsWebWindowNewHandler = Referenced::MakeRefPtr<JSWebWindowNewHandler>();
948 jsWebWindowNewHandler->IncRefCount();
949 args.SetReturnValue(Referenced::RawPtr(jsWebWindowNewHandler));
950 }
951
Destructor(JSWebWindowNewHandler* jsWebWindowNewHandler)952 static void Destructor(JSWebWindowNewHandler* jsWebWindowNewHandler)
953 {
954 if (jsWebWindowNewHandler != nullptr) {
955 jsWebWindowNewHandler->DecRefCount();
956 }
957 }
958
959 RefPtr<WebWindowNewHandler> handler_;
960 static std::unordered_map<int32_t, ChildWindowInfo> controller_map_;
961 };
962 std::unordered_map<int32_t, JSWebWindowNewHandler::ChildWindowInfo> JSWebWindowNewHandler::controller_map_;
963
964 class JSDataResubmitted : public Referenced {
965 public:
JSBind(BindingTarget globalObj)966 static void JSBind(BindingTarget globalObj)
967 {
968 JSClass<JSDataResubmitted>::Declare("DataResubmissionHandler");
969 JSClass<JSDataResubmitted>::CustomMethod("resend", &JSDataResubmitted::Resend);
970 JSClass<JSDataResubmitted>::CustomMethod("cancel", &JSDataResubmitted::Cancel);
971 JSClass<JSDataResubmitted>::Bind(globalObj, &JSDataResubmitted::Constructor, &JSDataResubmitted::Destructor);
972 }
973
SetHandler(const RefPtr<DataResubmitted>& handler)974 void SetHandler(const RefPtr<DataResubmitted>& handler)
975 {
976 dataResubmitted_ = handler;
977 }
978
Resend(const JSCallbackInfo& args)979 void Resend(const JSCallbackInfo& args)
980 {
981 if (dataResubmitted_) {
982 dataResubmitted_->Resend();
983 }
984 }
985
Cancel(const JSCallbackInfo& args)986 void Cancel(const JSCallbackInfo& args)
987 {
988 if (dataResubmitted_) {
989 dataResubmitted_->Cancel();
990 }
991 }
992
993 private:
Constructor(const JSCallbackInfo& args)994 static void Constructor(const JSCallbackInfo& args)
995 {
996 auto jsDataResubmitted = Referenced::MakeRefPtr<JSDataResubmitted>();
997 jsDataResubmitted->IncRefCount();
998 args.SetReturnValue(Referenced::RawPtr(jsDataResubmitted));
999 }
1000
Destructor(JSDataResubmitted* jsDataResubmitted)1001 static void Destructor(JSDataResubmitted* jsDataResubmitted)
1002 {
1003 if (jsDataResubmitted != nullptr) {
1004 jsDataResubmitted->DecRefCount();
1005 }
1006 }
1007 RefPtr<DataResubmitted> dataResubmitted_;
1008 };
1009
1010 class JSWebResourceError : public Referenced {
1011 public:
JSBind(BindingTarget globalObj)1012 static void JSBind(BindingTarget globalObj)
1013 {
1014 JSClass<JSWebResourceError>::Declare("WebResourceError");
1015 JSClass<JSWebResourceError>::CustomMethod("getErrorCode", &JSWebResourceError::GetErrorCode);
1016 JSClass<JSWebResourceError>::CustomMethod("getErrorInfo", &JSWebResourceError::GetErrorInfo);
1017 JSClass<JSWebResourceError>::Bind(globalObj, &JSWebResourceError::Constructor, &JSWebResourceError::Destructor);
1018 }
1019
SetEvent(const ReceivedErrorEvent& eventInfo)1020 void SetEvent(const ReceivedErrorEvent& eventInfo)
1021 {
1022 error_ = eventInfo.GetError();
1023 }
1024
GetErrorCode(const JSCallbackInfo& args)1025 void GetErrorCode(const JSCallbackInfo& args)
1026 {
1027 auto code = JSVal(ToJSValue(error_->GetCode()));
1028 auto descriptionRef = JSRef<JSVal>::Make(code);
1029 args.SetReturnValue(descriptionRef);
1030 }
1031
GetErrorInfo(const JSCallbackInfo& args)1032 void GetErrorInfo(const JSCallbackInfo& args)
1033 {
1034 auto info = JSVal(ToJSValue(error_->GetInfo()));
1035 auto descriptionRef = JSRef<JSVal>::Make(info);
1036 args.SetReturnValue(descriptionRef);
1037 }
1038
1039 private:
Constructor(const JSCallbackInfo& args)1040 static void Constructor(const JSCallbackInfo& args)
1041 {
1042 auto jSWebResourceError = Referenced::MakeRefPtr<JSWebResourceError>();
1043 jSWebResourceError->IncRefCount();
1044 args.SetReturnValue(Referenced::RawPtr(jSWebResourceError));
1045 }
1046
Destructor(JSWebResourceError* jSWebResourceError)1047 static void Destructor(JSWebResourceError* jSWebResourceError)
1048 {
1049 if (jSWebResourceError != nullptr) {
1050 jSWebResourceError->DecRefCount();
1051 }
1052 }
1053
1054 RefPtr<WebError> error_;
1055 };
1056
1057 class JSWebResourceResponse : public Referenced {
1058 public:
JSBind(BindingTarget globalObj)1059 static void JSBind(BindingTarget globalObj)
1060 {
1061 JSClass<JSWebResourceResponse>::Declare("WebResourceResponse");
1062 JSClass<JSWebResourceResponse>::CustomMethod("getResponseData", &JSWebResourceResponse::GetResponseData);
1063 JSClass<JSWebResourceResponse>::CustomMethod("getResponseDataEx", &JSWebResourceResponse::GetResponseDataEx);
1064 JSClass<JSWebResourceResponse>::CustomMethod(
1065 "getResponseEncoding", &JSWebResourceResponse::GetResponseEncoding);
1066 JSClass<JSWebResourceResponse>::CustomMethod(
1067 "getResponseMimeType", &JSWebResourceResponse::GetResponseMimeType);
1068 JSClass<JSWebResourceResponse>::CustomMethod("getReasonMessage", &JSWebResourceResponse::GetReasonMessage);
1069 JSClass<JSWebResourceResponse>::CustomMethod("getResponseCode", &JSWebResourceResponse::GetResponseCode);
1070 JSClass<JSWebResourceResponse>::CustomMethod("getResponseHeader", &JSWebResourceResponse::GetResponseHeader);
1071 JSClass<JSWebResourceResponse>::CustomMethod("setResponseData", &JSWebResourceResponse::SetResponseData);
1072 JSClass<JSWebResourceResponse>::CustomMethod(
1073 "setResponseEncoding", &JSWebResourceResponse::SetResponseEncoding);
1074 JSClass<JSWebResourceResponse>::CustomMethod(
1075 "setResponseMimeType", &JSWebResourceResponse::SetResponseMimeType);
1076 JSClass<JSWebResourceResponse>::CustomMethod("setReasonMessage", &JSWebResourceResponse::SetReasonMessage);
1077 JSClass<JSWebResourceResponse>::CustomMethod("setResponseCode", &JSWebResourceResponse::SetResponseCode);
1078 JSClass<JSWebResourceResponse>::CustomMethod("setResponseHeader", &JSWebResourceResponse::SetResponseHeader);
1079 JSClass<JSWebResourceResponse>::CustomMethod("setResponseIsReady", &JSWebResourceResponse::SetResponseIsReady);
1080 JSClass<JSWebResourceResponse>::CustomMethod("getResponseIsReady", &JSWebResourceResponse::GetResponseIsReady);
1081 JSClass<JSWebResourceResponse>::Bind(
1082 globalObj, &JSWebResourceResponse::Constructor, &JSWebResourceResponse::Destructor);
1083 }
1084
JSWebResourceResponse()1085 JSWebResourceResponse() : response_(AceType::MakeRefPtr<WebResponse>()) {}
1086
SetEvent(const ReceivedHttpErrorEvent& eventInfo)1087 void SetEvent(const ReceivedHttpErrorEvent& eventInfo)
1088 {
1089 response_ = eventInfo.GetResponse();
1090 }
1091
GetResponseData(const JSCallbackInfo& args)1092 void GetResponseData(const JSCallbackInfo& args)
1093 {
1094 auto data = JSVal(ToJSValue(response_->GetData()));
1095 auto descriptionRef = JSRef<JSVal>::Make(data);
1096 args.SetReturnValue(descriptionRef);
1097 }
1098
GetResponseDataEx(const JSCallbackInfo& args)1099 void GetResponseDataEx(const JSCallbackInfo& args)
1100 {
1101 args.SetReturnValue(responseData_);
1102 }
1103
GetResponseIsReady(const JSCallbackInfo& args)1104 void GetResponseIsReady(const JSCallbackInfo& args)
1105 {
1106 auto status = JSVal(ToJSValue(response_->GetResponseStatus()));
1107 auto descriptionRef = JSRef<JSVal>::Make(status);
1108 args.SetReturnValue(descriptionRef);
1109 }
1110
GetResponseEncoding(const JSCallbackInfo& args)1111 void GetResponseEncoding(const JSCallbackInfo& args)
1112 {
1113 auto encoding = JSVal(ToJSValue(response_->GetEncoding()));
1114 auto descriptionRef = JSRef<JSVal>::Make(encoding);
1115 args.SetReturnValue(descriptionRef);
1116 }
1117
GetResponseMimeType(const JSCallbackInfo& args)1118 void GetResponseMimeType(const JSCallbackInfo& args)
1119 {
1120 auto mimeType = JSVal(ToJSValue(response_->GetMimeType()));
1121 auto descriptionRef = JSRef<JSVal>::Make(mimeType);
1122 args.SetReturnValue(descriptionRef);
1123 }
1124
GetReasonMessage(const JSCallbackInfo& args)1125 void GetReasonMessage(const JSCallbackInfo& args)
1126 {
1127 auto reason = JSVal(ToJSValue(response_->GetReason()));
1128 auto descriptionRef = JSRef<JSVal>::Make(reason);
1129 args.SetReturnValue(descriptionRef);
1130 }
1131
GetResponseCode(const JSCallbackInfo& args)1132 void GetResponseCode(const JSCallbackInfo& args)
1133 {
1134 auto code = JSVal(ToJSValue(response_->GetStatusCode()));
1135 auto descriptionRef = JSRef<JSVal>::Make(code);
1136 args.SetReturnValue(descriptionRef);
1137 }
1138
GetResponseHeader(const JSCallbackInfo& args)1139 void GetResponseHeader(const JSCallbackInfo& args)
1140 {
1141 auto map = response_->GetHeaders();
1142 std::map<std::string, std::string>::iterator iterator;
1143 uint32_t index = 0;
1144 JSRef<JSArray> headers = JSRef<JSArray>::New();
1145 for (iterator = map.begin(); iterator != map.end(); ++iterator) {
1146 JSRef<JSObject> header = JSRef<JSObject>::New();
1147 header->SetProperty("headerKey", iterator->first);
1148 header->SetProperty("headerValue", iterator->second);
1149 headers->SetValueAt(index++, header);
1150 }
1151 args.SetReturnValue(headers);
1152 }
1153
GetResponseObj() const1154 RefPtr<WebResponse> GetResponseObj() const
1155 {
1156 return response_;
1157 }
1158
SetResponseData(const JSCallbackInfo& args)1159 void SetResponseData(const JSCallbackInfo& args)
1160 {
1161 if (args.Length() <= 0) {
1162 return;
1163 }
1164
1165 responseData_ = args[0];
1166 if (args[0]->IsNumber()) {
1167 auto fd = args[0]->ToNumber<int32_t>();
1168 response_->SetFileHandle(fd);
1169 return;
1170 }
1171 if (args[0]->IsString()) {
1172 auto data = args[0]->ToString();
1173 response_->SetData(data);
1174 return;
1175 }
1176 if (args[0]->IsArrayBuffer()) {
1177 JsiRef<JsiArrayBuffer> arrayBuffer = JsiRef<JsiArrayBuffer>::Cast(args[0]);
1178 int32_t bufferSize = arrayBuffer->ByteLength();
1179 void* buffer = arrayBuffer->GetBuffer();
1180 const char* charPtr = static_cast<const char*>(buffer);
1181 std::string data(charPtr, bufferSize);
1182 response_->SetData(data);
1183 response_->SetBuffer(static_cast<char*>(buffer), bufferSize);
1184 return;
1185 }
1186 if (args[0]->IsObject()) {
1187 std::string resourceUrl;
1188 std::string url;
1189 if (!JSViewAbstract::ParseJsMedia(args[0], resourceUrl)) {
1190 return;
1191 }
1192 auto np = resourceUrl.find_first_of("/");
1193 url = (np == std::string::npos) ? resourceUrl : resourceUrl.erase(np, 1);
1194 response_->SetResourceUrl(url);
1195 return;
1196 }
1197 }
1198
SetResponseEncoding(const JSCallbackInfo& args)1199 void SetResponseEncoding(const JSCallbackInfo& args)
1200 {
1201 if ((args.Length() <= 0) || !(args[0]->IsString())) {
1202 return;
1203 }
1204 auto encode = args[0]->ToString();
1205 response_->SetEncoding(encode);
1206 }
1207
SetResponseMimeType(const JSCallbackInfo& args)1208 void SetResponseMimeType(const JSCallbackInfo& args)
1209 {
1210 if ((args.Length() <= 0) || !(args[0]->IsString())) {
1211 return;
1212 }
1213 auto mineType = args[0]->ToString();
1214 response_->SetMimeType(mineType);
1215 }
1216
SetReasonMessage(const JSCallbackInfo& args)1217 void SetReasonMessage(const JSCallbackInfo& args)
1218 {
1219 if ((args.Length() <= 0) || !(args[0]->IsString())) {
1220 return;
1221 }
1222 auto reason = args[0]->ToString();
1223 response_->SetReason(reason);
1224 }
1225
SetResponseCode(const JSCallbackInfo& args)1226 void SetResponseCode(const JSCallbackInfo& args)
1227 {
1228 if ((args.Length() <= 0) || !(args[0]->IsNumber())) {
1229 return;
1230 }
1231 auto statusCode = args[0]->ToNumber<int32_t>();
1232 response_->SetStatusCode(statusCode);
1233 }
1234
SetResponseHeader(const JSCallbackInfo& args)1235 void SetResponseHeader(const JSCallbackInfo& args)
1236 {
1237 if ((args.Length() <= 0) || !(args[0]->IsArray())) {
1238 return;
1239 }
1240 JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
1241 for (size_t i = 0; i < array->Length(); i++) {
1242 if (!(array->GetValueAt(i)->IsObject())) {
1243 return;
1244 }
1245 auto obj = JSRef<JSObject>::Cast(array->GetValueAt(i));
1246 auto headerKey = obj->GetProperty("headerKey");
1247 auto headerValue = obj->GetProperty("headerValue");
1248 if (!headerKey->IsString() || !headerValue->IsString()) {
1249 return;
1250 }
1251 auto keystr = headerKey->ToString();
1252 auto valstr = headerValue->ToString();
1253 response_->SetHeadersVal(keystr, valstr);
1254 }
1255 }
1256
SetResponseIsReady(const JSCallbackInfo& args)1257 void SetResponseIsReady(const JSCallbackInfo& args)
1258 {
1259 if ((args.Length() <= 0) || !(args[0]->IsBoolean())) {
1260 return;
1261 }
1262 bool isReady = false;
1263 if (!ConvertFromJSValue(args[0], isReady)) {
1264 return;
1265 }
1266 response_->SetResponseStatus(isReady);
1267 }
1268
1269 private:
Constructor(const JSCallbackInfo& args)1270 static void Constructor(const JSCallbackInfo& args)
1271 {
1272 auto jSWebResourceResponse = Referenced::MakeRefPtr<JSWebResourceResponse>();
1273 jSWebResourceResponse->IncRefCount();
1274 args.SetReturnValue(Referenced::RawPtr(jSWebResourceResponse));
1275 }
1276
Destructor(JSWebResourceResponse* jSWebResourceResponse)1277 static void Destructor(JSWebResourceResponse* jSWebResourceResponse)
1278 {
1279 if (jSWebResourceResponse != nullptr) {
1280 jSWebResourceResponse->DecRefCount();
1281 }
1282 }
1283
1284 RefPtr<WebResponse> response_;
1285 JSRef<JSVal> responseData_;
1286 };
1287
1288 class JSWebResourceRequest : public Referenced {
1289 public:
JSBind(BindingTarget globalObj)1290 static void JSBind(BindingTarget globalObj)
1291 {
1292 JSClass<JSWebResourceRequest>::Declare("WebResourceRequest");
1293 JSClass<JSWebResourceRequest>::CustomMethod("getRequestUrl", &JSWebResourceRequest::GetRequestUrl);
1294 JSClass<JSWebResourceRequest>::CustomMethod("getRequestHeader", &JSWebResourceRequest::GetRequestHeader);
1295 JSClass<JSWebResourceRequest>::CustomMethod("getRequestMethod", &JSWebResourceRequest::GetRequestMethod);
1296 JSClass<JSWebResourceRequest>::CustomMethod("isRequestGesture", &JSWebResourceRequest::IsRequestGesture);
1297 JSClass<JSWebResourceRequest>::CustomMethod("isMainFrame", &JSWebResourceRequest::IsMainFrame);
1298 JSClass<JSWebResourceRequest>::CustomMethod("isRedirect", &JSWebResourceRequest::IsRedirect);
1299 JSClass<JSWebResourceRequest>::Bind(
1300 globalObj, &JSWebResourceRequest::Constructor, &JSWebResourceRequest::Destructor);
1301 }
1302
SetErrorEvent(const ReceivedErrorEvent& eventInfo)1303 void SetErrorEvent(const ReceivedErrorEvent& eventInfo)
1304 {
1305 request_ = eventInfo.GetRequest();
1306 }
1307
SetHttpErrorEvent(const ReceivedHttpErrorEvent& eventInfo)1308 void SetHttpErrorEvent(const ReceivedHttpErrorEvent& eventInfo)
1309 {
1310 request_ = eventInfo.GetRequest();
1311 }
1312
SetOnInterceptRequestEvent(const OnInterceptRequestEvent& eventInfo)1313 void SetOnInterceptRequestEvent(const OnInterceptRequestEvent& eventInfo)
1314 {
1315 request_ = eventInfo.GetRequest();
1316 }
1317
SetLoadInterceptEvent(const LoadInterceptEvent& eventInfo)1318 void SetLoadInterceptEvent(const LoadInterceptEvent& eventInfo)
1319 {
1320 request_ = eventInfo.GetRequest();
1321 }
1322
IsRedirect(const JSCallbackInfo& args)1323 void IsRedirect(const JSCallbackInfo& args)
1324 {
1325 auto isRedirect = JSVal(ToJSValue(request_->IsRedirect()));
1326 auto descriptionRef = JSRef<JSVal>::Make(isRedirect);
1327 args.SetReturnValue(descriptionRef);
1328 }
1329
GetRequestUrl(const JSCallbackInfo& args)1330 void GetRequestUrl(const JSCallbackInfo& args)
1331 {
1332 auto url = JSVal(ToJSValue(request_->GetUrl()));
1333 auto descriptionRef = JSRef<JSVal>::Make(url);
1334 args.SetReturnValue(descriptionRef);
1335 }
1336
GetRequestMethod(const JSCallbackInfo& args)1337 void GetRequestMethod(const JSCallbackInfo& args)
1338 {
1339 auto method = JSVal(ToJSValue(request_->GetMethod()));
1340 auto descriptionRef = JSRef<JSVal>::Make(method);
1341 args.SetReturnValue(descriptionRef);
1342 }
1343
IsRequestGesture(const JSCallbackInfo& args)1344 void IsRequestGesture(const JSCallbackInfo& args)
1345 {
1346 auto isRequestGesture = JSVal(ToJSValue(request_->HasGesture()));
1347 auto descriptionRef = JSRef<JSVal>::Make(isRequestGesture);
1348 args.SetReturnValue(descriptionRef);
1349 }
1350
IsMainFrame(const JSCallbackInfo& args)1351 void IsMainFrame(const JSCallbackInfo& args)
1352 {
1353 auto isMainFrame = JSVal(ToJSValue(request_->IsMainFrame()));
1354 auto descriptionRef = JSRef<JSVal>::Make(isMainFrame);
1355 args.SetReturnValue(descriptionRef);
1356 }
1357
GetRequestHeader(const JSCallbackInfo& args)1358 void GetRequestHeader(const JSCallbackInfo& args)
1359 {
1360 auto map = request_->GetHeaders();
1361 std::map<std::string, std::string>::iterator iterator;
1362 uint32_t index = 0;
1363 JSRef<JSArray> headers = JSRef<JSArray>::New();
1364 for (iterator = map.begin(); iterator != map.end(); ++iterator) {
1365 JSRef<JSObject> header = JSRef<JSObject>::New();
1366 header->SetProperty("headerKey", iterator->first);
1367 header->SetProperty("headerValue", iterator->second);
1368 headers->SetValueAt(index++, header);
1369 }
1370 args.SetReturnValue(headers);
1371 }
1372
SetLoadOverrideEvent(const LoadOverrideEvent& eventInfo)1373 void SetLoadOverrideEvent(const LoadOverrideEvent& eventInfo)
1374 {
1375 request_ = eventInfo.GetRequest();
1376 }
1377
1378 private:
Constructor(const JSCallbackInfo& args)1379 static void Constructor(const JSCallbackInfo& args)
1380 {
1381 auto jSWebResourceRequest = Referenced::MakeRefPtr<JSWebResourceRequest>();
1382 jSWebResourceRequest->IncRefCount();
1383 args.SetReturnValue(Referenced::RawPtr(jSWebResourceRequest));
1384 }
1385
Destructor(JSWebResourceRequest* jSWebResourceRequest)1386 static void Destructor(JSWebResourceRequest* jSWebResourceRequest)
1387 {
1388 if (jSWebResourceRequest != nullptr) {
1389 jSWebResourceRequest->DecRefCount();
1390 }
1391 }
1392
1393 RefPtr<WebRequest> request_;
1394 };
1395
1396 class JSFileSelectorParam : public Referenced {
1397 public:
JSBind(BindingTarget globalObj)1398 static void JSBind(BindingTarget globalObj)
1399 {
1400 JSClass<JSFileSelectorParam>::Declare("FileSelectorParam");
1401 JSClass<JSFileSelectorParam>::CustomMethod("getTitle", &JSFileSelectorParam::GetTitle);
1402 JSClass<JSFileSelectorParam>::CustomMethod("getMode", &JSFileSelectorParam::GetMode);
1403 JSClass<JSFileSelectorParam>::CustomMethod("getAcceptType", &JSFileSelectorParam::GetAcceptType);
1404 JSClass<JSFileSelectorParam>::CustomMethod("isCapture", &JSFileSelectorParam::IsCapture);
1405 JSClass<JSFileSelectorParam>::Bind(
1406 globalObj, &JSFileSelectorParam::Constructor, &JSFileSelectorParam::Destructor);
1407 }
1408
SetParam(const FileSelectorEvent& eventInfo)1409 void SetParam(const FileSelectorEvent& eventInfo)
1410 {
1411 param_ = eventInfo.GetParam();
1412 }
1413
GetTitle(const JSCallbackInfo& args)1414 void GetTitle(const JSCallbackInfo& args)
1415 {
1416 auto title = JSVal(ToJSValue(param_->GetTitle()));
1417 auto descriptionRef = JSRef<JSVal>::Make(title);
1418 args.SetReturnValue(descriptionRef);
1419 }
1420
GetMode(const JSCallbackInfo& args)1421 void GetMode(const JSCallbackInfo& args)
1422 {
1423 auto mode = JSVal(ToJSValue(param_->GetMode()));
1424 auto descriptionRef = JSRef<JSVal>::Make(mode);
1425 args.SetReturnValue(descriptionRef);
1426 }
1427
IsCapture(const JSCallbackInfo& args)1428 void IsCapture(const JSCallbackInfo& args)
1429 {
1430 auto isCapture = JSVal(ToJSValue(param_->IsCapture()));
1431 auto descriptionRef = JSRef<JSVal>::Make(isCapture);
1432 args.SetReturnValue(descriptionRef);
1433 }
1434
GetAcceptType(const JSCallbackInfo& args)1435 void GetAcceptType(const JSCallbackInfo& args)
1436 {
1437 auto acceptTypes = param_->GetAcceptType();
1438 JSRef<JSArray> result = JSRef<JSArray>::New();
1439 std::vector<std::string>::iterator iterator;
1440 uint32_t index = 0;
1441 for (iterator = acceptTypes.begin(); iterator != acceptTypes.end(); ++iterator) {
1442 auto valueStr = JSVal(ToJSValue(*iterator));
1443 auto value = JSRef<JSVal>::Make(valueStr);
1444 result->SetValueAt(index++, value);
1445 }
1446 args.SetReturnValue(result);
1447 }
1448
1449 private:
Constructor(const JSCallbackInfo& args)1450 static void Constructor(const JSCallbackInfo& args)
1451 {
1452 auto jSFilerSelectorParam = Referenced::MakeRefPtr<JSFileSelectorParam>();
1453 jSFilerSelectorParam->IncRefCount();
1454 args.SetReturnValue(Referenced::RawPtr(jSFilerSelectorParam));
1455 }
1456
Destructor(JSFileSelectorParam* jSFilerSelectorParam)1457 static void Destructor(JSFileSelectorParam* jSFilerSelectorParam)
1458 {
1459 if (jSFilerSelectorParam != nullptr) {
1460 jSFilerSelectorParam->DecRefCount();
1461 }
1462 }
1463
1464 RefPtr<WebFileSelectorParam> param_;
1465 };
1466
1467 class JSFileSelectorResult : public Referenced {
1468 public:
JSBind(BindingTarget globalObj)1469 static void JSBind(BindingTarget globalObj)
1470 {
1471 JSClass<JSFileSelectorResult>::Declare("FileSelectorResult");
1472 JSClass<JSFileSelectorResult>::CustomMethod("handleFileList", &JSFileSelectorResult::HandleFileList);
1473 JSClass<JSFileSelectorResult>::Bind(
1474 globalObj, &JSFileSelectorResult::Constructor, &JSFileSelectorResult::Destructor);
1475 }
1476
SetResult(const FileSelectorEvent& eventInfo)1477 void SetResult(const FileSelectorEvent& eventInfo)
1478 {
1479 result_ = eventInfo.GetFileSelectorResult();
1480 }
1481
HandleFileList(const JSCallbackInfo& args)1482 void HandleFileList(const JSCallbackInfo& args)
1483 {
1484 std::vector<std::string> fileList;
1485 if (args[0]->IsArray()) {
1486 JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
1487 for (size_t i = 0; i < array->Length(); i++) {
1488 JSRef<JSVal> val = array->GetValueAt(i);
1489 if (!val->IsString()) {
1490 continue;
1491 }
1492 std::string fileName;
1493 if (!ConvertFromJSValue(val, fileName)) {
1494 continue;
1495 }
1496 fileList.push_back(fileName);
1497 }
1498 }
1499
1500 if (result_) {
1501 result_->HandleFileList(fileList);
1502 }
1503 }
1504
1505 private:
Constructor(const JSCallbackInfo& args)1506 static void Constructor(const JSCallbackInfo& args)
1507 {
1508 auto jsFileSelectorResult = Referenced::MakeRefPtr<JSFileSelectorResult>();
1509 jsFileSelectorResult->IncRefCount();
1510 args.SetReturnValue(Referenced::RawPtr(jsFileSelectorResult));
1511 }
1512
Destructor(JSFileSelectorResult* jsFileSelectorResult)1513 static void Destructor(JSFileSelectorResult* jsFileSelectorResult)
1514 {
1515 if (jsFileSelectorResult != nullptr) {
1516 jsFileSelectorResult->DecRefCount();
1517 }
1518 }
1519
1520 RefPtr<FileSelectorResult> result_;
1521 };
1522
1523 class JSContextMenuParam : public Referenced {
1524 public:
JSBind(BindingTarget globalObj)1525 static void JSBind(BindingTarget globalObj)
1526 {
1527 JSClass<JSContextMenuParam>::Declare("WebContextMenuParam");
1528 JSClass<JSContextMenuParam>::CustomMethod("x", &JSContextMenuParam::GetXCoord);
1529 JSClass<JSContextMenuParam>::CustomMethod("y", &JSContextMenuParam::GetYCoord);
1530 JSClass<JSContextMenuParam>::CustomMethod("getLinkUrl", &JSContextMenuParam::GetLinkUrl);
1531 JSClass<JSContextMenuParam>::CustomMethod("getUnfilteredLinkUrl", &JSContextMenuParam::GetUnfilteredLinkUrl);
1532 JSClass<JSContextMenuParam>::CustomMethod("getSourceUrl", &JSContextMenuParam::GetSourceUrl);
1533 JSClass<JSContextMenuParam>::CustomMethod("existsImageContents", &JSContextMenuParam::HasImageContents);
1534 JSClass<JSContextMenuParam>::CustomMethod("getSelectionText", &JSContextMenuParam::GetSelectionText);
1535 JSClass<JSContextMenuParam>::CustomMethod("isEditable", &JSContextMenuParam::IsEditable);
1536 JSClass<JSContextMenuParam>::CustomMethod("getEditStateFlags", &JSContextMenuParam::GetEditStateFlags);
1537 JSClass<JSContextMenuParam>::CustomMethod("getSourceType", &JSContextMenuParam::GetSourceType);
1538 JSClass<JSContextMenuParam>::CustomMethod("getInputFieldType", &JSContextMenuParam::GetInputFieldType);
1539 JSClass<JSContextMenuParam>::CustomMethod("getMediaType", &JSContextMenuParam::GetMediaType);
1540 JSClass<JSContextMenuParam>::CustomMethod("getPreviewWidth", &JSContextMenuParam::GetPreviewWidth);
1541 JSClass<JSContextMenuParam>::CustomMethod("getPreviewHeight", &JSContextMenuParam::GetPreviewHeight);
1542 JSClass<JSContextMenuParam>::Bind(globalObj, &JSContextMenuParam::Constructor, &JSContextMenuParam::Destructor);
1543 }
1544
UpdatePreviewSize()1545 void UpdatePreviewSize()
1546 {
1547 if (previewWidth_ >= 0 && previewHeight_ >= 0) {
1548 return;
1549 }
1550 if (param_) {
1551 int32_t x = 0;
1552 int32_t y = 0;
1553 param_->GetImageRect(x, y, previewWidth_, previewHeight_);
1554 }
1555 }
1556
GetPreviewWidth(const JSCallbackInfo& args)1557 void GetPreviewWidth(const JSCallbackInfo& args)
1558 {
1559 auto ret = JSVal(ToJSValue(previewWidth_));
1560 auto descriptionRef = JSRef<JSVal>::Make(ret);
1561 args.SetReturnValue(descriptionRef);
1562 }
1563
GetPreviewHeight(const JSCallbackInfo& args)1564 void GetPreviewHeight(const JSCallbackInfo& args)
1565 {
1566 auto ret = JSVal(ToJSValue(previewHeight_));
1567 auto descriptionRef = JSRef<JSVal>::Make(ret);
1568 args.SetReturnValue(descriptionRef);
1569 }
1570
SetParam(const ContextMenuEvent& eventInfo)1571 void SetParam(const ContextMenuEvent& eventInfo)
1572 {
1573 param_ = eventInfo.GetParam();
1574 UpdatePreviewSize();
1575 }
1576
GetXCoord(const JSCallbackInfo& args)1577 void GetXCoord(const JSCallbackInfo& args)
1578 {
1579 int32_t ret = -1;
1580 if (param_) {
1581 ret = param_->GetXCoord();
1582 }
1583 auto xCoord = JSVal(ToJSValue(ret));
1584 auto descriptionRef = JSRef<JSVal>::Make(xCoord);
1585 args.SetReturnValue(descriptionRef);
1586 }
1587
GetYCoord(const JSCallbackInfo& args)1588 void GetYCoord(const JSCallbackInfo& args)
1589 {
1590 int32_t ret = -1;
1591 if (param_) {
1592 ret = param_->GetYCoord();
1593 }
1594 auto yCoord = JSVal(ToJSValue(ret));
1595 auto descriptionRef = JSRef<JSVal>::Make(yCoord);
1596 args.SetReturnValue(descriptionRef);
1597 }
1598
GetLinkUrl(const JSCallbackInfo& args)1599 void GetLinkUrl(const JSCallbackInfo& args)
1600 {
1601 std::string url;
1602 if (param_) {
1603 url = param_->GetLinkUrl();
1604 }
1605 auto linkUrl = JSVal(ToJSValue(url));
1606 auto descriptionRef = JSRef<JSVal>::Make(linkUrl);
1607 args.SetReturnValue(descriptionRef);
1608 }
1609
GetUnfilteredLinkUrl(const JSCallbackInfo& args)1610 void GetUnfilteredLinkUrl(const JSCallbackInfo& args)
1611 {
1612 std::string url;
1613 if (param_) {
1614 url = param_->GetUnfilteredLinkUrl();
1615 }
1616 auto unfilteredLinkUrl = JSVal(ToJSValue(url));
1617 auto descriptionRef = JSRef<JSVal>::Make(unfilteredLinkUrl);
1618 args.SetReturnValue(descriptionRef);
1619 }
1620
GetSourceUrl(const JSCallbackInfo& args)1621 void GetSourceUrl(const JSCallbackInfo& args)
1622 {
1623 std::string url;
1624 if (param_) {
1625 url = param_->GetSourceUrl();
1626 }
1627 auto sourceUrl = JSVal(ToJSValue(url));
1628 auto descriptionRef = JSRef<JSVal>::Make(sourceUrl);
1629 args.SetReturnValue(descriptionRef);
1630 }
1631
HasImageContents(const JSCallbackInfo& args)1632 void HasImageContents(const JSCallbackInfo& args)
1633 {
1634 bool ret = false;
1635 if (param_) {
1636 ret = param_->HasImageContents();
1637 }
1638 auto hasImageContents = JSVal(ToJSValue(ret));
1639 auto descriptionRef = JSRef<JSVal>::Make(hasImageContents);
1640 args.SetReturnValue(descriptionRef);
1641 }
1642
GetSelectionText(const JSCallbackInfo& args)1643 void GetSelectionText(const JSCallbackInfo& args)
1644 {
1645 std::string text;
1646 if (param_) {
1647 text = param_->GetSelectionText();
1648 }
1649 auto jsText = JSVal(ToJSValue(text));
1650 auto descriptionRef = JSRef<JSVal>::Make(jsText);
1651 args.SetReturnValue(descriptionRef);
1652 }
1653
IsEditable(const JSCallbackInfo& args)1654 void IsEditable(const JSCallbackInfo& args)
1655 {
1656 bool flag = false;
1657 if (param_) {
1658 flag = param_->IsEditable();
1659 }
1660 auto jsFlag = JSVal(ToJSValue(flag));
1661 auto descriptionRef = JSRef<JSVal>::Make(jsFlag);
1662 args.SetReturnValue(descriptionRef);
1663 }
1664
GetEditStateFlags(const JSCallbackInfo& args)1665 void GetEditStateFlags(const JSCallbackInfo& args)
1666 {
1667 int32_t flags = 0;
1668 if (param_) {
1669 flags = param_->GetEditStateFlags();
1670 }
1671 auto jsFlags = JSVal(ToJSValue(flags));
1672 auto descriptionRef = JSRef<JSVal>::Make(jsFlags);
1673 args.SetReturnValue(descriptionRef);
1674 }
1675
GetSourceType(const JSCallbackInfo& args)1676 void GetSourceType(const JSCallbackInfo& args)
1677 {
1678 int32_t type = 0;
1679 if (param_) {
1680 type = param_->GetSourceType();
1681 }
1682 auto jsType = JSVal(ToJSValue(type));
1683 auto descriptionRef = JSRef<JSVal>::Make(jsType);
1684 args.SetReturnValue(descriptionRef);
1685 }
1686
GetInputFieldType(const JSCallbackInfo& args)1687 void GetInputFieldType(const JSCallbackInfo& args)
1688 {
1689 int32_t type = 0;
1690 if (param_) {
1691 type = param_->GetInputFieldType();
1692 }
1693 auto jsType = JSVal(ToJSValue(type));
1694 auto descriptionRef = JSRef<JSVal>::Make(jsType);
1695 args.SetReturnValue(descriptionRef);
1696 }
1697
GetMediaType(const JSCallbackInfo& args)1698 void GetMediaType(const JSCallbackInfo& args)
1699 {
1700 int32_t type = 0;
1701 if (param_) {
1702 type = param_->GetMediaType();
1703 }
1704 auto jsType = JSVal(ToJSValue(type));
1705 auto descriptionRef = JSRef<JSVal>::Make(jsType);
1706 args.SetReturnValue(descriptionRef);
1707 }
1708
1709 private:
Constructor(const JSCallbackInfo& args)1710 static void Constructor(const JSCallbackInfo& args)
1711 {
1712 auto jSContextMenuParam = Referenced::MakeRefPtr<JSContextMenuParam>();
1713 jSContextMenuParam->IncRefCount();
1714 args.SetReturnValue(Referenced::RawPtr(jSContextMenuParam));
1715 }
1716
Destructor(JSContextMenuParam* jSContextMenuParam)1717 static void Destructor(JSContextMenuParam* jSContextMenuParam)
1718 {
1719 if (jSContextMenuParam != nullptr) {
1720 jSContextMenuParam->DecRefCount();
1721 }
1722 }
1723
1724 RefPtr<WebContextMenuParam> param_;
1725
1726 int32_t previewWidth_ = -1;
1727
1728 int32_t previewHeight_ = -1;
1729 };
1730
1731 class JSContextMenuResult : public Referenced {
1732 public:
JSBind(BindingTarget globalObj)1733 static void JSBind(BindingTarget globalObj)
1734 {
1735 JSClass<JSContextMenuResult>::Declare("WebContextMenuResult");
1736 JSClass<JSContextMenuResult>::CustomMethod("closeContextMenu", &JSContextMenuResult::Cancel);
1737 JSClass<JSContextMenuResult>::CustomMethod("copyImage", &JSContextMenuResult::CopyImage);
1738 JSClass<JSContextMenuResult>::CustomMethod("copy", &JSContextMenuResult::Copy);
1739 JSClass<JSContextMenuResult>::CustomMethod("paste", &JSContextMenuResult::Paste);
1740 JSClass<JSContextMenuResult>::CustomMethod("cut", &JSContextMenuResult::Cut);
1741 JSClass<JSContextMenuResult>::CustomMethod("selectAll", &JSContextMenuResult::SelectAll);
1742 JSClass<JSContextMenuResult>::Bind(
1743 globalObj, &JSContextMenuResult::Constructor, &JSContextMenuResult::Destructor);
1744 }
1745
SetResult(const ContextMenuEvent& eventInfo)1746 void SetResult(const ContextMenuEvent& eventInfo)
1747 {
1748 result_ = eventInfo.GetContextMenuResult();
1749 }
1750
Cancel(const JSCallbackInfo& args)1751 void Cancel(const JSCallbackInfo& args)
1752 {
1753 if (result_) {
1754 result_->Cancel();
1755 }
1756 }
1757
CopyImage(const JSCallbackInfo& args)1758 void CopyImage(const JSCallbackInfo& args)
1759 {
1760 if (result_) {
1761 result_->CopyImage();
1762 }
1763 }
1764
Copy(const JSCallbackInfo& args)1765 void Copy(const JSCallbackInfo& args)
1766 {
1767 if (result_) {
1768 result_->Copy();
1769 }
1770 }
1771
Paste(const JSCallbackInfo& args)1772 void Paste(const JSCallbackInfo& args)
1773 {
1774 if (result_) {
1775 result_->Paste();
1776 }
1777 }
1778
Cut(const JSCallbackInfo& args)1779 void Cut(const JSCallbackInfo& args)
1780 {
1781 if (result_) {
1782 result_->Cut();
1783 }
1784 }
1785
SelectAll(const JSCallbackInfo& args)1786 void SelectAll(const JSCallbackInfo& args)
1787 {
1788 if (result_) {
1789 result_->SelectAll();
1790 }
1791 }
1792
1793 private:
Constructor(const JSCallbackInfo& args)1794 static void Constructor(const JSCallbackInfo& args)
1795 {
1796 auto jsContextMenuResult = Referenced::MakeRefPtr<JSContextMenuResult>();
1797 jsContextMenuResult->IncRefCount();
1798 args.SetReturnValue(Referenced::RawPtr(jsContextMenuResult));
1799 }
1800
Destructor(JSContextMenuResult* jsContextMenuResult)1801 static void Destructor(JSContextMenuResult* jsContextMenuResult)
1802 {
1803 if (jsContextMenuResult != nullptr) {
1804 jsContextMenuResult->DecRefCount();
1805 }
1806 }
1807
1808 RefPtr<ContextMenuResult> result_;
1809 };
1810
1811 class JSWebAppLinkCallback : public Referenced {
1812 public:
JSBind(BindingTarget globalObj)1813 static void JSBind(BindingTarget globalObj)
1814 {
1815 JSClass<JSWebAppLinkCallback>::Declare("WebAppLinkCallback");
1816 JSClass<JSWebAppLinkCallback>::CustomMethod("continueLoad", &JSWebAppLinkCallback::ContinueLoad);
1817 JSClass<JSWebAppLinkCallback>::CustomMethod("cancelLoad", &JSWebAppLinkCallback::CancelLoad);
1818 JSClass<JSWebAppLinkCallback>::Bind(
1819 globalObj, &JSWebAppLinkCallback::Constructor, &JSWebAppLinkCallback::Destructor);
1820 }
1821
SetEvent(const WebAppLinkEvent& eventInfo)1822 void SetEvent(const WebAppLinkEvent& eventInfo)
1823 {
1824 callback_ = eventInfo.GetCallback();
1825 }
1826
ContinueLoad(const JSCallbackInfo& args)1827 void ContinueLoad(const JSCallbackInfo& args)
1828 {
1829 if (callback_) {
1830 callback_->ContinueLoad();
1831 }
1832 }
1833
CancelLoad(const JSCallbackInfo& args)1834 void CancelLoad(const JSCallbackInfo& args)
1835 {
1836 if (callback_) {
1837 callback_->CancelLoad();
1838 }
1839 }
1840
1841 private:
Constructor(const JSCallbackInfo& args)1842 static void Constructor(const JSCallbackInfo& args)
1843 {
1844 auto jsWebAppLinkCallback = Referenced::MakeRefPtr<JSWebAppLinkCallback>();
1845 jsWebAppLinkCallback->IncRefCount();
1846 args.SetReturnValue(Referenced::RawPtr(jsWebAppLinkCallback));
1847 }
1848
Destructor(JSWebAppLinkCallback* jsWebAppLinkCallback)1849 static void Destructor(JSWebAppLinkCallback* jsWebAppLinkCallback)
1850 {
1851 if (jsWebAppLinkCallback != nullptr) {
1852 jsWebAppLinkCallback->DecRefCount();
1853 }
1854 }
1855
1856 RefPtr<WebAppLinkCallback> callback_;
1857 };
1858
JSBind(BindingTarget globalObj)1859 void JSWeb::JSBind(BindingTarget globalObj)
1860 {
1861 JSClass<JSWeb>::Declare("Web");
1862 JSClass<JSWeb>::StaticMethod("create", &JSWeb::Create);
1863 JSClass<JSWeb>::StaticMethod("onAlert", &JSWeb::OnAlert);
1864 JSClass<JSWeb>::StaticMethod("onBeforeUnload", &JSWeb::OnBeforeUnload);
1865 JSClass<JSWeb>::StaticMethod("onConfirm", &JSWeb::OnConfirm);
1866 JSClass<JSWeb>::StaticMethod("onPrompt", &JSWeb::OnPrompt);
1867 JSClass<JSWeb>::StaticMethod("onConsole", &JSWeb::OnConsoleLog);
1868 JSClass<JSWeb>::StaticMethod("onFullScreenEnter", &JSWeb::OnFullScreenEnter);
1869 JSClass<JSWeb>::StaticMethod("onFullScreenExit", &JSWeb::OnFullScreenExit);
1870 JSClass<JSWeb>::StaticMethod("onPageBegin", &JSWeb::OnPageStart);
1871 JSClass<JSWeb>::StaticMethod("onPageEnd", &JSWeb::OnPageFinish);
1872 JSClass<JSWeb>::StaticMethod("onProgressChange", &JSWeb::OnProgressChange);
1873 JSClass<JSWeb>::StaticMethod("onTitleReceive", &JSWeb::OnTitleReceive);
1874 JSClass<JSWeb>::StaticMethod("onGeolocationHide", &JSWeb::OnGeolocationHide);
1875 JSClass<JSWeb>::StaticMethod("onGeolocationShow", &JSWeb::OnGeolocationShow);
1876 JSClass<JSWeb>::StaticMethod("onRequestSelected", &JSWeb::OnRequestFocus);
1877 JSClass<JSWeb>::StaticMethod("onShowFileSelector", &JSWeb::OnFileSelectorShow);
1878 JSClass<JSWeb>::StaticMethod("javaScriptAccess", &JSWeb::JsEnabled);
1879 JSClass<JSWeb>::StaticMethod("fileExtendAccess", &JSWeb::ContentAccessEnabled);
1880 JSClass<JSWeb>::StaticMethod("fileAccess", &JSWeb::FileAccessEnabled);
1881 JSClass<JSWeb>::StaticMethod("onDownloadStart", &JSWeb::OnDownloadStart);
1882 JSClass<JSWeb>::StaticMethod("onErrorReceive", &JSWeb::OnErrorReceive);
1883 JSClass<JSWeb>::StaticMethod("onHttpErrorReceive", &JSWeb::OnHttpErrorReceive);
1884 JSClass<JSWeb>::StaticMethod("onInterceptRequest", &JSWeb::OnInterceptRequest);
1885 JSClass<JSWeb>::StaticMethod("onUrlLoadIntercept", &JSWeb::OnUrlLoadIntercept);
1886 JSClass<JSWeb>::StaticMethod("onLoadIntercept", &JSWeb::OnLoadIntercept);
1887 JSClass<JSWeb>::StaticMethod("onlineImageAccess", &JSWeb::OnLineImageAccessEnabled);
1888 JSClass<JSWeb>::StaticMethod("domStorageAccess", &JSWeb::DomStorageAccessEnabled);
1889 JSClass<JSWeb>::StaticMethod("imageAccess", &JSWeb::ImageAccessEnabled);
1890 JSClass<JSWeb>::StaticMethod("mixedMode", &JSWeb::MixedMode);
1891 JSClass<JSWeb>::StaticMethod("enableNativeEmbedMode", &JSWeb::EnableNativeEmbedMode);
1892 JSClass<JSWeb>::StaticMethod("enableSmoothDragResize", &JSWeb::EnableSmoothDragResize);
1893 JSClass<JSWeb>::StaticMethod("registerNativeEmbedRule", &JSWeb::RegisterNativeEmbedRule);
1894 JSClass<JSWeb>::StaticMethod("zoomAccess", &JSWeb::ZoomAccessEnabled);
1895 JSClass<JSWeb>::StaticMethod("geolocationAccess", &JSWeb::GeolocationAccessEnabled);
1896 JSClass<JSWeb>::StaticMethod("javaScriptProxy", &JSWeb::JavaScriptProxy);
1897 JSClass<JSWeb>::StaticMethod("userAgent", &JSWeb::UserAgent);
1898 JSClass<JSWeb>::StaticMethod("onRenderExited", &JSWeb::OnRenderExited);
1899 JSClass<JSWeb>::StaticMethod("onRefreshAccessedHistory", &JSWeb::OnRefreshAccessedHistory);
1900 JSClass<JSWeb>::StaticMethod("cacheMode", &JSWeb::CacheMode);
1901 JSClass<JSWeb>::StaticMethod("overviewModeAccess", &JSWeb::OverviewModeAccess);
1902 JSClass<JSWeb>::StaticMethod("webDebuggingAccess", &JSWeb::WebDebuggingAccess);
1903 JSClass<JSWeb>::StaticMethod("wideViewModeAccess", &JSWeb::WideViewModeAccess);
1904 JSClass<JSWeb>::StaticMethod("fileFromUrlAccess", &JSWeb::FileFromUrlAccess);
1905 JSClass<JSWeb>::StaticMethod("databaseAccess", &JSWeb::DatabaseAccess);
1906 JSClass<JSWeb>::StaticMethod("textZoomRatio", &JSWeb::TextZoomRatio);
1907 JSClass<JSWeb>::StaticMethod("textZoomAtio", &JSWeb::TextZoomRatio);
1908 JSClass<JSWeb>::StaticMethod("initialScale", &JSWeb::InitialScale);
1909 JSClass<JSWeb>::StaticMethod("backgroundColor", &JSWeb::BackgroundColor);
1910 JSClass<JSWeb>::StaticMethod("onKeyEvent", &JSWeb::OnKeyEvent);
1911 JSClass<JSWeb>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
1912 JSClass<JSWeb>::StaticMethod("onMouse", &JSWeb::OnMouse);
1913 JSClass<JSWeb>::StaticMethod("onResourceLoad", &JSWeb::OnResourceLoad);
1914 JSClass<JSWeb>::StaticMethod("onScaleChange", &JSWeb::OnScaleChange);
1915 JSClass<JSWeb>::StaticMethod("password", &JSWeb::Password);
1916 JSClass<JSWeb>::StaticMethod("tableData", &JSWeb::TableData);
1917 JSClass<JSWeb>::StaticMethod("onFileSelectorShow", &JSWeb::OnFileSelectorShowAbandoned);
1918 JSClass<JSWeb>::StaticMethod("onHttpAuthRequest", &JSWeb::OnHttpAuthRequest);
1919 JSClass<JSWeb>::StaticMethod("onSslErrorReceive", &JSWeb::OnSslErrRequest);
1920 JSClass<JSWeb>::StaticMethod("onSslErrorEventReceive", &JSWeb::OnSslErrorRequest);
1921 JSClass<JSWeb>::StaticMethod("onSslErrorEvent", &JSWeb::OnAllSslErrorRequest);
1922 JSClass<JSWeb>::StaticMethod("onClientAuthenticationRequest", &JSWeb::OnSslSelectCertRequest);
1923 JSClass<JSWeb>::StaticMethod("onPermissionRequest", &JSWeb::OnPermissionRequest);
1924 JSClass<JSWeb>::StaticMethod("onContextMenuShow", &JSWeb::OnContextMenuShow);
1925 JSClass<JSWeb>::StaticMethod("onContextMenuHide", &JSWeb::OnContextMenuHide);
1926 JSClass<JSWeb>::StaticMethod("onSearchResultReceive", &JSWeb::OnSearchResultReceive);
1927 JSClass<JSWeb>::StaticMethod("mediaPlayGestureAccess", &JSWeb::MediaPlayGestureAccess);
1928 JSClass<JSWeb>::StaticMethod("onDragStart", &JSWeb::JsOnDragStart);
1929 JSClass<JSWeb>::StaticMethod("onDragEnter", &JSWeb::JsOnDragEnter);
1930 JSClass<JSWeb>::StaticMethod("onDragMove", &JSWeb::JsOnDragMove);
1931 JSClass<JSWeb>::StaticMethod("onDragLeave", &JSWeb::JsOnDragLeave);
1932 JSClass<JSWeb>::StaticMethod("onDrop", &JSWeb::JsOnDrop);
1933 JSClass<JSWeb>::StaticMethod("onScroll", &JSWeb::OnScroll);
1934 JSClass<JSWeb>::StaticMethod("rotate", &JSWeb::WebRotate);
1935 JSClass<JSWeb>::StaticMethod("pinchSmooth", &JSWeb::PinchSmoothModeEnabled);
1936 JSClass<JSWeb>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
1937 JSClass<JSWeb>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
1938 JSClass<JSWeb>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
1939 JSClass<JSWeb>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
1940 JSClass<JSWeb>::StaticMethod("onWindowNew", &JSWeb::OnWindowNew);
1941 JSClass<JSWeb>::StaticMethod("onWindowExit", &JSWeb::OnWindowExit);
1942 JSClass<JSWeb>::StaticMethod("multiWindowAccess", &JSWeb::MultiWindowAccessEnabled);
1943 JSClass<JSWeb>::StaticMethod("allowWindowOpenMethod", &JSWeb::AllowWindowOpenMethod);
1944 JSClass<JSWeb>::StaticMethod("webCursiveFont", &JSWeb::WebCursiveFont);
1945 JSClass<JSWeb>::StaticMethod("webFantasyFont", &JSWeb::WebFantasyFont);
1946 JSClass<JSWeb>::StaticMethod("webFixedFont", &JSWeb::WebFixedFont);
1947 JSClass<JSWeb>::StaticMethod("webSansSerifFont", &JSWeb::WebSansSerifFont);
1948 JSClass<JSWeb>::StaticMethod("webSerifFont", &JSWeb::WebSerifFont);
1949 JSClass<JSWeb>::StaticMethod("webStandardFont", &JSWeb::WebStandardFont);
1950 JSClass<JSWeb>::StaticMethod("defaultFixedFontSize", &JSWeb::DefaultFixedFontSize);
1951 JSClass<JSWeb>::StaticMethod("defaultFontSize", &JSWeb::DefaultFontSize);
1952 JSClass<JSWeb>::StaticMethod("defaultTextEncodingFormat", &JSWeb::DefaultTextEncodingFormat);
1953 JSClass<JSWeb>::StaticMethod("minFontSize", &JSWeb::MinFontSize);
1954 JSClass<JSWeb>::StaticMethod("minLogicalFontSize", &JSWeb::MinLogicalFontSize);
1955 JSClass<JSWeb>::StaticMethod("blockNetwork", &JSWeb::BlockNetwork);
1956 JSClass<JSWeb>::StaticMethod("onPageVisible", &JSWeb::OnPageVisible);
1957 JSClass<JSWeb>::StaticMethod("onInterceptKeyEvent", &JSWeb::OnInterceptKeyEvent);
1958 JSClass<JSWeb>::StaticMethod("onDataResubmitted", &JSWeb::OnDataResubmitted);
1959 JSClass<JSWeb>::StaticMethod("onFaviconReceived", &JSWeb::OnFaviconReceived);
1960 JSClass<JSWeb>::StaticMethod("onTouchIconUrlReceived", &JSWeb::OnTouchIconUrlReceived);
1961 JSClass<JSWeb>::StaticMethod("darkMode", &JSWeb::DarkMode);
1962 JSClass<JSWeb>::StaticMethod("forceDarkAccess", &JSWeb::ForceDarkAccess);
1963 JSClass<JSWeb>::StaticMethod("overScrollMode", &JSWeb::OverScrollMode);
1964 JSClass<JSWeb>::StaticMethod("horizontalScrollBarAccess", &JSWeb::HorizontalScrollBarAccess);
1965 JSClass<JSWeb>::StaticMethod("verticalScrollBarAccess", &JSWeb::VerticalScrollBarAccess);
1966 JSClass<JSWeb>::StaticMethod("onAudioStateChanged", &JSWeb::OnAudioStateChanged);
1967 JSClass<JSWeb>::StaticMethod("mediaOptions", &JSWeb::MediaOptions);
1968 JSClass<JSWeb>::StaticMethod("onFirstContentfulPaint", &JSWeb::OnFirstContentfulPaint);
1969 JSClass<JSWeb>::StaticMethod("onFirstMeaningfulPaint", &JSWeb::OnFirstMeaningfulPaint);
1970 JSClass<JSWeb>::StaticMethod("onLargestContentfulPaint", &JSWeb::OnLargestContentfulPaint);
1971 JSClass<JSWeb>::StaticMethod("onSafeBrowsingCheckResult", &JSWeb::OnSafeBrowsingCheckResult);
1972 JSClass<JSWeb>::StaticMethod("onNavigationEntryCommitted", &JSWeb::OnNavigationEntryCommitted);
1973 JSClass<JSWeb>::StaticMethod("onIntelligentTrackingPreventionResult",
1974 &JSWeb::OnIntelligentTrackingPreventionResult);
1975 JSClass<JSWeb>::StaticMethod("onControllerAttached", &JSWeb::OnControllerAttached);
1976 JSClass<JSWeb>::StaticMethod("onOverScroll", &JSWeb::OnOverScroll);
1977 JSClass<JSWeb>::StaticMethod("onNativeEmbedLifecycleChange", &JSWeb::OnNativeEmbedLifecycleChange);
1978 JSClass<JSWeb>::StaticMethod("onNativeEmbedVisibilityChange", &JSWeb::OnNativeEmbedVisibilityChange);
1979 JSClass<JSWeb>::StaticMethod("onNativeEmbedGestureEvent", &JSWeb::OnNativeEmbedGestureEvent);
1980 JSClass<JSWeb>::StaticMethod("copyOptions", &JSWeb::CopyOption);
1981 JSClass<JSWeb>::StaticMethod("onScreenCaptureRequest", &JSWeb::OnScreenCaptureRequest);
1982 JSClass<JSWeb>::StaticMethod("layoutMode", &JSWeb::SetLayoutMode);
1983 JSClass<JSWeb>::StaticMethod("nestedScroll", &JSWeb::SetNestedScroll);
1984 JSClass<JSWeb>::StaticMethod("metaViewport", &JSWeb::SetMetaViewport);
1985 JSClass<JSWeb>::StaticMethod("javaScriptOnDocumentStart", &JSWeb::JavaScriptOnDocumentStart);
1986 JSClass<JSWeb>::StaticMethod("javaScriptOnDocumentEnd", &JSWeb::JavaScriptOnDocumentEnd);
1987 JSClass<JSWeb>::StaticMethod("onOverrideUrlLoading", &JSWeb::OnOverrideUrlLoading);
1988 JSClass<JSWeb>::StaticMethod("textAutosizing", &JSWeb::TextAutosizing);
1989 JSClass<JSWeb>::StaticMethod("enableNativeMediaPlayer", &JSWeb::EnableNativeVideoPlayer);
1990 JSClass<JSWeb>::StaticMethod("onRenderProcessNotResponding", &JSWeb::OnRenderProcessNotResponding);
1991 JSClass<JSWeb>::StaticMethod("onRenderProcessResponding", &JSWeb::OnRenderProcessResponding);
1992 JSClass<JSWeb>::StaticMethod("selectionMenuOptions", &JSWeb::SelectionMenuOptions);
1993 JSClass<JSWeb>::StaticMethod("onViewportFitChanged", &JSWeb::OnViewportFitChanged);
1994 JSClass<JSWeb>::StaticMethod("onInterceptKeyboardAttach", &JSWeb::OnInterceptKeyboardAttach);
1995 JSClass<JSWeb>::StaticMethod("onAdsBlocked", &JSWeb::OnAdsBlocked);
1996 JSClass<JSWeb>::StaticMethod("forceDisplayScrollBar", &JSWeb::ForceDisplayScrollBar);
1997 JSClass<JSWeb>::StaticMethod("keyboardAvoidMode", &JSWeb::KeyboardAvoidMode);
1998 JSClass<JSWeb>::StaticMethod("editMenuOptions", &JSWeb::EditMenuOptions);
1999 JSClass<JSWeb>::StaticMethod("enableHapticFeedback", &JSWeb::EnableHapticFeedback);
2000 JSClass<JSWeb>::StaticMethod("bindSelectionMenu", &JSWeb::BindSelectionMenu);
2001
2002 JSClass<JSWeb>::InheritAndBind<JSViewAbstract>(globalObj);
2003 JSWebDialog::JSBind(globalObj);
2004 JSWebGeolocation::JSBind(globalObj);
2005 JSWebResourceRequest::JSBind(globalObj);
2006 JSWebResourceError::JSBind(globalObj);
2007 JSWebResourceResponse::JSBind(globalObj);
2008 JSWebConsoleLog::JSBind(globalObj);
2009 JSFileSelectorParam::JSBind(globalObj);
2010 JSFileSelectorResult::JSBind(globalObj);
2011 JSFullScreenExitHandler::JSBind(globalObj);
2012 JSWebHttpAuth::JSBind(globalObj);
2013 JSWebSslError::JSBind(globalObj);
2014 JSWebAllSslError::JSBind(globalObj);
2015 JSWebSslSelectCert::JSBind(globalObj);
2016 JSWebPermissionRequest::JSBind(globalObj);
2017 JSContextMenuParam::JSBind(globalObj);
2018 JSContextMenuResult::JSBind(globalObj);
2019 JSWebWindowNewHandler::JSBind(globalObj);
2020 JSDataResubmitted::JSBind(globalObj);
2021 JSScreenCaptureRequest::JSBind(globalObj);
2022 JSNativeEmbedGestureRequest::JSBind(globalObj);
2023 JSWebAppLinkCallback::JSBind(globalObj);
2024 JSWebKeyboardController::JSBind(globalObj);
2025 }
2026
LoadWebConsoleLogEventToJSValue(const LoadWebConsoleLogEvent& eventInfo)2027 JSRef<JSVal> LoadWebConsoleLogEventToJSValue(const LoadWebConsoleLogEvent& eventInfo)
2028 {
2029 JSRef<JSObject> obj = JSRef<JSObject>::New();
2030
2031 JSRef<JSObject> messageObj = JSClass<JSWebConsoleLog>::NewInstance();
2032 auto jsWebConsoleLog = Referenced::Claim(messageObj->Unwrap<JSWebConsoleLog>());
2033 jsWebConsoleLog->SetMessage(eventInfo.GetMessage());
2034
2035 obj->SetPropertyObject("message", messageObj);
2036
2037 return JSRef<JSVal>::Cast(obj);
2038 }
2039
WebDialogEventToJSValue(const WebDialogEvent& eventInfo)2040 JSRef<JSVal> WebDialogEventToJSValue(const WebDialogEvent& eventInfo)
2041 {
2042 JSRef<JSObject> obj = JSRef<JSObject>::New();
2043
2044 JSRef<JSObject> resultObj = JSClass<JSWebDialog>::NewInstance();
2045 auto jsWebDialog = Referenced::Claim(resultObj->Unwrap<JSWebDialog>());
2046 jsWebDialog->SetResult(eventInfo.GetResult());
2047
2048 obj->SetProperty("url", eventInfo.GetUrl());
2049 obj->SetProperty("message", eventInfo.GetMessage());
2050 if (eventInfo.GetType() == DialogEventType::DIALOG_EVENT_PROMPT) {
2051 obj->SetProperty("value", eventInfo.GetValue());
2052 }
2053 obj->SetPropertyObject("result", resultObj);
2054
2055 return JSRef<JSVal>::Cast(obj);
2056 }
2057
LoadWebPageFinishEventToJSValue(const LoadWebPageFinishEvent& eventInfo)2058 JSRef<JSVal> LoadWebPageFinishEventToJSValue(const LoadWebPageFinishEvent& eventInfo)
2059 {
2060 JSRef<JSObject> obj = JSRef<JSObject>::New();
2061 obj->SetProperty("url", eventInfo.GetLoadedUrl());
2062 return JSRef<JSVal>::Cast(obj);
2063 }
2064
ContextMenuHideEventToJSValue(const ContextMenuHideEvent& eventInfo)2065 JSRef<JSVal> ContextMenuHideEventToJSValue(const ContextMenuHideEvent& eventInfo)
2066 {
2067 JSRef<JSObject> obj = JSRef<JSObject>::New();
2068 obj->SetProperty("info", eventInfo.GetInfo());
2069 return JSRef<JSVal>::Cast(obj);
2070 }
2071
FullScreenEnterEventToJSValue(const FullScreenEnterEvent& eventInfo)2072 JSRef<JSVal> FullScreenEnterEventToJSValue(const FullScreenEnterEvent& eventInfo)
2073 {
2074 JSRef<JSObject> obj = JSRef<JSObject>::New();
2075 JSRef<JSObject> resultObj = JSClass<JSFullScreenExitHandler>::NewInstance();
2076 auto jsFullScreenExitHandler = Referenced::Claim(resultObj->Unwrap<JSFullScreenExitHandler>());
2077 if (!jsFullScreenExitHandler) {
2078 return JSRef<JSVal>::Cast(obj);
2079 }
2080 jsFullScreenExitHandler->SetHandler(eventInfo.GetHandler());
2081
2082 obj->SetPropertyObject("handler", resultObj);
2083 obj->SetProperty("videoWidth", eventInfo.GetVideoNaturalWidth());
2084 obj->SetProperty("videoHeight", eventInfo.GetVideoNaturalHeight());
2085 return JSRef<JSVal>::Cast(obj);
2086 }
2087
FullScreenExitEventToJSValue(const FullScreenExitEvent& eventInfo)2088 JSRef<JSVal> FullScreenExitEventToJSValue(const FullScreenExitEvent& eventInfo)
2089 {
2090 return JSRef<JSVal>::Make(ToJSValue(eventInfo.IsFullScreen()));
2091 }
2092
LoadWebPageStartEventToJSValue(const LoadWebPageStartEvent& eventInfo)2093 JSRef<JSVal> LoadWebPageStartEventToJSValue(const LoadWebPageStartEvent& eventInfo)
2094 {
2095 JSRef<JSObject> obj = JSRef<JSObject>::New();
2096 obj->SetProperty("url", eventInfo.GetLoadedUrl());
2097 return JSRef<JSVal>::Cast(obj);
2098 }
2099
LoadWebProgressChangeEventToJSValue(const LoadWebProgressChangeEvent& eventInfo)2100 JSRef<JSVal> LoadWebProgressChangeEventToJSValue(const LoadWebProgressChangeEvent& eventInfo)
2101 {
2102 JSRef<JSObject> obj = JSRef<JSObject>::New();
2103 obj->SetProperty("newProgress", eventInfo.GetNewProgress());
2104 return JSRef<JSVal>::Cast(obj);
2105 }
2106
LoadWebTitleReceiveEventToJSValue(const LoadWebTitleReceiveEvent& eventInfo)2107 JSRef<JSVal> LoadWebTitleReceiveEventToJSValue(const LoadWebTitleReceiveEvent& eventInfo)
2108 {
2109 JSRef<JSObject> obj = JSRef<JSObject>::New();
2110 obj->SetProperty("title", eventInfo.GetTitle());
2111 return JSRef<JSVal>::Cast(obj);
2112 }
2113
UrlLoadInterceptEventToJSValue(const UrlLoadInterceptEvent& eventInfo)2114 JSRef<JSVal> UrlLoadInterceptEventToJSValue(const UrlLoadInterceptEvent& eventInfo)
2115 {
2116 JSRef<JSObject> obj = JSRef<JSObject>::New();
2117 obj->SetProperty("data", eventInfo.GetData());
2118 return JSRef<JSVal>::Cast(obj);
2119 }
2120
LoadInterceptEventToJSValue(const LoadInterceptEvent& eventInfo)2121 JSRef<JSVal> LoadInterceptEventToJSValue(const LoadInterceptEvent& eventInfo)
2122 {
2123 JSRef<JSObject> obj = JSRef<JSObject>::New();
2124 JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
2125 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
2126 requestEvent->SetLoadInterceptEvent(eventInfo);
2127 obj->SetPropertyObject("data", requestObj);
2128 return JSRef<JSVal>::Cast(obj);
2129 }
2130
LoadWebGeolocationHideEventToJSValue(const LoadWebGeolocationHideEvent& eventInfo)2131 JSRef<JSVal> LoadWebGeolocationHideEventToJSValue(const LoadWebGeolocationHideEvent& eventInfo)
2132 {
2133 return JSRef<JSVal>::Make(ToJSValue(eventInfo.GetOrigin()));
2134 }
2135
LoadWebGeolocationShowEventToJSValue(const LoadWebGeolocationShowEvent& eventInfo)2136 JSRef<JSVal> LoadWebGeolocationShowEventToJSValue(const LoadWebGeolocationShowEvent& eventInfo)
2137 {
2138 JSRef<JSObject> obj = JSRef<JSObject>::New();
2139 obj->SetProperty("origin", eventInfo.GetOrigin());
2140 JSRef<JSObject> geolocationObj = JSClass<JSWebGeolocation>::NewInstance();
2141 auto geolocationEvent = Referenced::Claim(geolocationObj->Unwrap<JSWebGeolocation>());
2142 geolocationEvent->SetEvent(eventInfo);
2143 obj->SetPropertyObject("geolocation", geolocationObj);
2144 return JSRef<JSVal>::Cast(obj);
2145 }
2146
DownloadStartEventToJSValue(const DownloadStartEvent& eventInfo)2147 JSRef<JSVal> DownloadStartEventToJSValue(const DownloadStartEvent& eventInfo)
2148 {
2149 JSRef<JSObject> obj = JSRef<JSObject>::New();
2150 obj->SetProperty("url", eventInfo.GetUrl());
2151 obj->SetProperty("userAgent", eventInfo.GetUserAgent());
2152 obj->SetProperty("contentDisposition", eventInfo.GetContentDisposition());
2153 obj->SetProperty("mimetype", eventInfo.GetMimetype());
2154 obj->SetProperty("contentLength", eventInfo.GetContentLength());
2155 return JSRef<JSVal>::Cast(obj);
2156 }
2157
LoadWebRequestFocusEventToJSValue(const LoadWebRequestFocusEvent& eventInfo)2158 JSRef<JSVal> LoadWebRequestFocusEventToJSValue(const LoadWebRequestFocusEvent& eventInfo)
2159 {
2160 return JSRef<JSVal>::Make(ToJSValue(eventInfo.GetRequestFocus()));
2161 }
2162
WebHttpAuthEventToJSValue(const WebHttpAuthEvent& eventInfo)2163 JSRef<JSVal> WebHttpAuthEventToJSValue(const WebHttpAuthEvent& eventInfo)
2164 {
2165 JSRef<JSObject> obj = JSRef<JSObject>::New();
2166 JSRef<JSObject> resultObj = JSClass<JSWebHttpAuth>::NewInstance();
2167 auto jsWebHttpAuth = Referenced::Claim(resultObj->Unwrap<JSWebHttpAuth>());
2168 if (!jsWebHttpAuth) {
2169 return JSRef<JSVal>::Cast(obj);
2170 }
2171 jsWebHttpAuth->SetResult(eventInfo.GetResult());
2172 obj->SetPropertyObject("handler", resultObj);
2173 obj->SetProperty("host", eventInfo.GetHost());
2174 obj->SetProperty("realm", eventInfo.GetRealm());
2175 return JSRef<JSVal>::Cast(obj);
2176 }
2177
WebSslErrorEventToJSValue(const WebSslErrorEvent& eventInfo)2178 JSRef<JSVal> WebSslErrorEventToJSValue(const WebSslErrorEvent& eventInfo)
2179 {
2180 JSRef<JSObject> obj = JSRef<JSObject>::New();
2181 JSRef<JSObject> resultObj = JSClass<JSWebSslError>::NewInstance();
2182 auto jsWebSslError = Referenced::Claim(resultObj->Unwrap<JSWebSslError>());
2183 if (!jsWebSslError) {
2184 return JSRef<JSVal>::Cast(obj);
2185 }
2186 jsWebSslError->SetResult(eventInfo.GetResult());
2187 obj->SetPropertyObject("handler", resultObj);
2188 obj->SetProperty("error", eventInfo.GetError());
2189
2190 auto engine = EngineHelper::GetCurrentEngine();
2191 if (!engine) {
2192 return JSRef<JSVal>::Cast(obj);
2193 }
2194 NativeEngine* nativeEngine = engine->GetNativeEngine();
2195 napi_env env = reinterpret_cast<napi_env>(nativeEngine);
2196 std::vector<std::string> certChainDerData = eventInfo.GetCertChainData();
2197 JSRef<JSArray> certsArr = JSRef<JSArray>::New();
2198 for (uint8_t i = 0; i < certChainDerData.size(); i++) {
2199 if (i == UINT8_MAX) {
2200 TAG_LOGE(AceLogTag::ACE_WEB, "Cert chain data array reach max.");
2201 break;
2202 }
2203
2204 void *data = nullptr;
2205 napi_value buffer = nullptr;
2206 napi_value item = nullptr;
2207 napi_status status = napi_create_arraybuffer(env, certChainDerData[i].size(), &data, &buffer);
2208 if (status != napi_ok) {
2209 TAG_LOGE(AceLogTag::ACE_WEB, "Create array buffer failed, status = %{public}d.", status);
2210 continue;
2211 }
2212 int retCode = memcpy_s(data, certChainDerData[i].size(),
2213 certChainDerData[i].data(), certChainDerData[i].size());
2214 if (retCode != 0) {
2215 TAG_LOGE(AceLogTag::ACE_WEB, "Cert chain data failed, index = %{public}u.", i);
2216 continue;
2217 }
2218 status = napi_create_typedarray(env, napi_uint8_array, certChainDerData[i].size(), buffer, 0, &item);
2219 if (status != napi_ok) {
2220 TAG_LOGE(AceLogTag::ACE_WEB, "Create typed array failed, status = %{public}d.", status);
2221 continue;
2222 }
2223 JSRef<JSVal> cert = JsConverter::ConvertNapiValueToJsVal(item);
2224 certsArr->SetValueAt(i, cert);
2225 }
2226 obj->SetPropertyObject("certChainData", certsArr);
2227 return JSRef<JSVal>::Cast(obj);
2228 }
2229
WebAllSslErrorEventToJSValue(const WebAllSslErrorEvent& eventInfo)2230 JSRef<JSVal> WebAllSslErrorEventToJSValue(const WebAllSslErrorEvent& eventInfo)
2231 {
2232 JSRef<JSObject> obj = JSRef<JSObject>::New();
2233 JSRef<JSObject> resultObj = JSClass<JSWebAllSslError>::NewInstance();
2234 auto jsWebAllSslError = Referenced::Claim(resultObj->Unwrap<JSWebAllSslError>());
2235 if (!jsWebAllSslError) {
2236 return JSRef<JSVal>::Cast(obj);
2237 }
2238 jsWebAllSslError->SetResult(eventInfo.GetResult());
2239 obj->SetPropertyObject("handler", resultObj);
2240 obj->SetProperty("error", eventInfo.GetError());
2241 obj->SetProperty("url", eventInfo.GetUrl());
2242 obj->SetProperty("originalUrl", eventInfo.GetOriginalUrl());
2243 obj->SetProperty("referrer", eventInfo.GetReferrer());
2244 obj->SetProperty("isFatalError", eventInfo.GetIsFatalError());
2245 obj->SetProperty("isMainFrame", eventInfo.GetIsMainFrame());
2246 return JSRef<JSVal>::Cast(obj);
2247 }
2248
WebSslSelectCertEventToJSValue(const WebSslSelectCertEvent& eventInfo)2249 JSRef<JSVal> WebSslSelectCertEventToJSValue(const WebSslSelectCertEvent& eventInfo)
2250 {
2251 JSRef<JSObject> obj = JSRef<JSObject>::New();
2252 JSRef<JSObject> resultObj = JSClass<JSWebSslSelectCert>::NewInstance();
2253 auto jsWebSslSelectCert = Referenced::Claim(resultObj->Unwrap<JSWebSslSelectCert>());
2254 if (!jsWebSslSelectCert) {
2255 return JSRef<JSVal>::Cast(obj);
2256 }
2257 jsWebSslSelectCert->SetResult(eventInfo.GetResult());
2258 obj->SetPropertyObject("handler", resultObj);
2259 obj->SetProperty("host", eventInfo.GetHost());
2260 obj->SetProperty("port", eventInfo.GetPort());
2261
2262 JSRef<JSArray> keyTypesArr = JSRef<JSArray>::New();
2263 const std::vector<std::string>& keyTypes = eventInfo.GetKeyTypes();
2264 for (int32_t idx = 0; idx < static_cast<int32_t>(keyTypes.size()); ++idx) {
2265 JSRef<JSVal> keyType = JSRef<JSVal>::Make(ToJSValue(keyTypes[idx]));
2266 keyTypesArr->SetValueAt(idx, keyType);
2267 }
2268 obj->SetPropertyObject("keyTypes", keyTypesArr);
2269
2270 JSRef<JSArray> issuersArr = JSRef<JSArray>::New();
2271 const std::vector<std::string>& issuers = eventInfo.GetIssuers_();
2272 for (int32_t idx = 0; idx < static_cast<int32_t>(issuers.size()); ++idx) {
2273 JSRef<JSVal> issuer = JSRef<JSVal>::Make(ToJSValue(issuers[idx]));
2274 issuersArr->SetValueAt(idx, issuer);
2275 }
2276
2277 obj->SetPropertyObject("issuers", issuersArr);
2278
2279 return JSRef<JSVal>::Cast(obj);
2280 }
2281
SearchResultReceiveEventToJSValue(const SearchResultReceiveEvent& eventInfo)2282 JSRef<JSVal> SearchResultReceiveEventToJSValue(const SearchResultReceiveEvent& eventInfo)
2283 {
2284 JSRef<JSObject> obj = JSRef<JSObject>::New();
2285 obj->SetProperty("activeMatchOrdinal", eventInfo.GetActiveMatchOrdinal());
2286 obj->SetProperty("numberOfMatches", eventInfo.GetNumberOfMatches());
2287 obj->SetProperty("isDoneCounting", eventInfo.GetIsDoneCounting());
2288 return JSRef<JSVal>::Cast(obj);
2289 }
2290
LoadOverrideEventToJSValue(const LoadOverrideEvent& eventInfo)2291 JSRef<JSVal> LoadOverrideEventToJSValue(const LoadOverrideEvent& eventInfo)
2292 {
2293 JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
2294 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
2295 requestEvent->SetLoadOverrideEvent(eventInfo);
2296 return JSRef<JSVal>::Cast(requestObj);
2297 }
2298
AdsBlockedEventToJSValue(const AdsBlockedEvent& eventInfo)2299 JSRef<JSVal> AdsBlockedEventToJSValue(const AdsBlockedEvent& eventInfo)
2300 {
2301 JSRef<JSObject> obj = JSRef<JSObject>::New();
2302 obj->SetProperty("url", eventInfo.GetUrl());
2303
2304 JSRef<JSArray> adsBlockedArr = JSRef<JSArray>::New();
2305 const std::vector<std::string>& adsBlocked = eventInfo.GetAdsBlocked();
2306 for (int32_t idx = 0; idx < static_cast<int32_t>(adsBlocked.size()); ++idx) {
2307 JSRef<JSVal> blockedUrl = JSRef<JSVal>::Make(ToJSValue(adsBlocked[idx]));
2308 adsBlockedArr->SetValueAt(idx, blockedUrl);
2309 }
2310 obj->SetPropertyObject("adsBlocked", adsBlockedArr);
2311
2312 return JSRef<JSVal>::Cast(obj);
2313 }
2314
ParseRawfileWebSrc(const JSRef<JSVal>& srcValue, std::string& webSrc)2315 void JSWeb::ParseRawfileWebSrc(const JSRef<JSVal>& srcValue, std::string& webSrc)
2316 {
2317 if (!srcValue->IsObject() || webSrc.substr(0, RAWFILE_PREFIX.size()) != RAWFILE_PREFIX) {
2318 return;
2319 }
2320 std::string bundleName;
2321 std::string moduleName;
2322 GetJsMediaBundleInfo(srcValue, bundleName, moduleName);
2323 auto container = Container::Current();
2324 CHECK_NULL_VOID(container);
2325 if ((!bundleName.empty() && !moduleName.empty()) &&
2326 (bundleName != AceApplicationInfo::GetInstance().GetPackageName() ||
2327 moduleName != container->GetModuleName())) {
2328 webSrc = RAWFILE_PREFIX + BUNDLE_NAME_PREFIX + bundleName + "/" + MODULE_NAME_PREFIX + moduleName + "/" +
2329 webSrc.substr(RAWFILE_PREFIX.size());
2330 }
2331 }
2332
Create(const JSCallbackInfo& info)2333 void JSWeb::Create(const JSCallbackInfo& info)
2334 {
2335 if (info.Length() < 1 || !info[0]->IsObject()) {
2336 return;
2337 }
2338 auto paramObject = JSRef<JSObject>::Cast(info[0]);
2339 JSRef<JSVal> srcValue = paramObject->GetProperty("src");
2340 std::string webSrc;
2341 std::optional<std::string> dstSrc;
2342 if (srcValue->IsString()) {
2343 dstSrc = srcValue->ToString();
2344 } else if (ParseJsMedia(srcValue, webSrc)) {
2345 ParseRawfileWebSrc(srcValue, webSrc);
2346 int np = static_cast<int>(webSrc.find_first_of("/"));
2347 dstSrc = np < 0 ? webSrc : webSrc.erase(np, 1);
2348 }
2349 if (!dstSrc) {
2350 return;
2351 }
2352 auto controllerObj = paramObject->GetProperty("controller");
2353 if (!controllerObj->IsObject()) {
2354 return;
2355 }
2356 JsiRef<JsiValue> type = JsiRef<JsiValue>::Make();
2357 bool isHasType = paramObject->HasProperty("type");
2358 if (isHasType) {
2359 type = paramObject->GetProperty("type");
2360 } else {
2361 type = paramObject->GetProperty("renderMode");
2362 }
2363 RenderMode renderMode = RenderMode::ASYNC_RENDER;
2364 if (type->IsNumber() && (type->ToNumber<int32_t>() >= 0) && (type->ToNumber<int32_t>() <= 1)) {
2365 renderMode = static_cast<RenderMode>(type->ToNumber<int32_t>());
2366 }
2367 std::string debugRenderMode = SystemProperties::GetWebDebugRenderMode();
2368 if (debugRenderMode != "none") {
2369 if (debugRenderMode == "async") {
2370 renderMode = RenderMode::ASYNC_RENDER;
2371 } else if (debugRenderMode == "sync") {
2372 renderMode = RenderMode::SYNC_RENDER;
2373 } else {
2374 TAG_LOGW(AceLogTag::ACE_WEB, "JSWeb::Create unsupport debug render mode: %{public}s",
2375 debugRenderMode.c_str());
2376 }
2377 TAG_LOGI(AceLogTag::ACE_WEB, "JSWeb::Create use debug render mode: %{public}s", debugRenderMode.c_str());
2378 }
2379
2380 bool incognitoMode = false;
2381 ParseJsBool(paramObject->GetProperty("incognitoMode"), incognitoMode);
2382
2383 std::string sharedRenderProcessToken = "";
2384 ParseJsString(paramObject->GetProperty("sharedRenderProcessToken"), sharedRenderProcessToken);
2385
2386 auto controller = JSRef<JSObject>::Cast(controllerObj);
2387 auto setWebIdFunction = controller->GetProperty("setWebId");
2388 if (setWebIdFunction->IsFunction()) {
2389 auto setIdCallback = [webviewController = controller, func = JSRef<JSFunc>::Cast(setWebIdFunction)](
2390 int32_t webId) {
2391 JSRef<JSVal> argv[] = { JSRef<JSVal>::Make(ToJSValue(webId)) };
2392 func->Call(webviewController, 1, argv);
2393 };
2394
2395 auto setHapPathFunction = controller->GetProperty("innerSetHapPath");
2396 std::function<void(const std::string&)> setHapPathCallback = nullptr;
2397 if (setHapPathFunction->IsFunction()) {
2398 setHapPathCallback = [webviewController = controller, func = JSRef<JSFunc>::Cast(setHapPathFunction)](
2399 const std::string& hapPath) {
2400 JSRef<JSVal> argv[] = { JSRef<JSVal>::Make(ToJSValue(hapPath)) };
2401 func->Call(webviewController, 1, argv);
2402 };
2403 }
2404
2405 auto setRequestPermissionsFromUserFunction = controller->GetProperty("requestPermissionsFromUserWeb");
2406 std::function<void(const std::shared_ptr<BaseEventInfo>&)> requestPermissionsFromUserCallback = nullptr;
2407 if (setRequestPermissionsFromUserFunction->IsFunction()) {
2408 requestPermissionsFromUserCallback = [webviewController = controller,
2409 func = JSRef<JSFunc>::Cast(setRequestPermissionsFromUserFunction)]
2410 (const std::shared_ptr<BaseEventInfo>& info) {
2411 auto* eventInfo = TypeInfoHelper::DynamicCast<WebPermissionRequestEvent>(info.get());
2412 JSRef<JSObject> obj = JSRef<JSObject>::New();
2413 JSRef<JSObject> permissionObj = JSClass<JSWebPermissionRequest>::NewInstance();
2414 auto permissionEvent = Referenced::Claim(permissionObj->Unwrap<JSWebPermissionRequest>());
2415 permissionEvent->SetEvent(*eventInfo);
2416 obj->SetPropertyObject("request", permissionObj);
2417 JSRef<JSVal> argv[] = { JSRef<JSVal>::Cast(obj) };
2418 auto result = func->Call(webviewController, 1, argv);
2419 };
2420 }
2421
2422 auto setOpenAppLinkFunction = controller->GetProperty("openAppLink");
2423 std::function<void(const std::shared_ptr<BaseEventInfo>&)> openAppLinkCallback = nullptr;
2424 if (setOpenAppLinkFunction->IsFunction()) {
2425 openAppLinkCallback = [webviewController = controller,
2426 func = JSRef<JSFunc>::Cast(setOpenAppLinkFunction)]
2427 (const std::shared_ptr<BaseEventInfo>& info) {
2428 auto* eventInfo = TypeInfoHelper::DynamicCast<WebAppLinkEvent>(info.get());
2429 JSRef<JSObject> obj = JSRef<JSObject>::New();
2430 JSRef<JSObject> callbackObj = JSClass<JSWebAppLinkCallback>::NewInstance();
2431 auto callbackEvent = Referenced::Claim(callbackObj->Unwrap<JSWebAppLinkCallback>());
2432 callbackEvent->SetEvent(*eventInfo);
2433 obj->SetPropertyObject("result", callbackObj);
2434 JSRef<JSVal> urlVal = JSRef<JSVal>::Make(ToJSValue(eventInfo->GetUrl()));
2435 obj->SetPropertyObject("url", urlVal);
2436 JSRef<JSVal> argv[] = { JSRef<JSVal>::Cast(obj) };
2437 auto result = func->Call(webviewController, 1, argv);
2438 };
2439 }
2440 auto fileSelectorShowFromUserFunction = controller->GetProperty("fileSelectorShowFromUserWeb");
2441 std::function<void(const std::shared_ptr<BaseEventInfo>&)> fileSelectorShowFromUserCallback = nullptr;
2442 if (fileSelectorShowFromUserFunction->IsFunction()) {
2443 fileSelectorShowFromUserCallback = [webviewController = controller,
2444 func = JSRef<JSFunc>::Cast(fileSelectorShowFromUserFunction)]
2445 (const std::shared_ptr<BaseEventInfo>& info) {
2446 auto* eventInfo = TypeInfoHelper::DynamicCast<FileSelectorEvent>(info.get());
2447 JSRef<JSObject> obj = JSRef<JSObject>::New();
2448 JSRef<JSObject> paramObj = JSClass<JSFileSelectorParam>::NewInstance();
2449 auto fileSelectorParam = Referenced::Claim(paramObj->Unwrap<JSFileSelectorParam>());
2450 fileSelectorParam->SetParam(*eventInfo);
2451 obj->SetPropertyObject("fileparam", paramObj);
2452
2453 JSRef<JSObject> resultObj = JSClass<JSFileSelectorResult>::NewInstance();
2454 auto fileSelectorResult = Referenced::Claim(resultObj->Unwrap<JSFileSelectorResult>());
2455
2456 fileSelectorResult->SetResult(*eventInfo);
2457
2458 obj->SetPropertyObject("fileresult", resultObj);
2459 JSRef<JSVal> argv[] = { JSRef<JSVal>::Cast(obj) };
2460 auto result = func->Call(webviewController, 1, argv);
2461 };
2462 }
2463
2464 int32_t parentNWebId = -1;
2465 bool isPopup = JSWebWindowNewHandler::ExistController(controller, parentNWebId);
2466 WebModel::GetInstance()->Create(isPopup ? "" : dstSrc.value(), std::move(setIdCallback),
2467 std::move(setHapPathCallback), parentNWebId, isPopup, renderMode, incognitoMode, sharedRenderProcessToken);
2468
2469 WebModel::GetInstance()->SetPermissionClipboard(std::move(requestPermissionsFromUserCallback));
2470 WebModel::GetInstance()->SetOpenAppLinkFunction(std::move(openAppLinkCallback));
2471 WebModel::GetInstance()->SetDefaultFileSelectorShow(std::move(fileSelectorShowFromUserCallback));
2472 auto getCmdLineFunction = controller->GetProperty("getCustomeSchemeCmdLine");
2473 if (!getCmdLineFunction->IsFunction()) {
2474 return;
2475 }
2476 std::string cmdLine = JSRef<JSFunc>::Cast(getCmdLineFunction)->Call(controller, 0, {})->ToString();
2477 if (!cmdLine.empty()) {
2478 WebModel::GetInstance()->SetCustomScheme(cmdLine);
2479 }
2480
2481 auto updateInstanceIdFunction = controller->GetProperty("updateInstanceId");
2482 if (updateInstanceIdFunction->IsFunction()) {
2483 std::function<void(int32_t)> updateInstanceIdCallback = [webviewController = controller,
2484 func = JSRef<JSFunc>::Cast(updateInstanceIdFunction)](int32_t newId) {
2485 auto newIdVal = JSRef<JSVal>::Make(ToJSValue(newId));
2486 auto result = func->Call(webviewController, 1, &newIdVal);
2487 };
2488 NG::WebModelNG::GetInstance()->SetUpdateInstanceIdCallback(std::move(updateInstanceIdCallback));
2489 }
2490
2491 auto getWebDebugingFunction = controller->GetProperty("getWebDebuggingAccess");
2492 if (!getWebDebugingFunction->IsFunction()) {
2493 return;
2494 }
2495 bool webDebuggingAccess = JSRef<JSFunc>::Cast(getWebDebugingFunction)->Call(controller, 0, {})->ToBoolean();
2496 if (webDebuggingAccess == JSWeb::webDebuggingAccess_) {
2497 return;
2498 }
2499 WebModel::GetInstance()->SetWebDebuggingAccessEnabled(webDebuggingAccess);
2500 JSWeb::webDebuggingAccess_ = webDebuggingAccess;
2501 return;
2502
2503 } else {
2504 auto* jsWebController = controller->Unwrap<JSWebController>();
2505 CHECK_NULL_VOID(jsWebController);
2506 WebModel::GetInstance()->Create(
2507 dstSrc.value(), jsWebController->GetController(), renderMode, incognitoMode, sharedRenderProcessToken);
2508 }
2509
2510 WebModel::GetInstance()->SetFocusable(true);
2511 WebModel::GetInstance()->SetFocusNode(true);
2512 }
2513
WebRotate(const JSCallbackInfo& args)2514 void JSWeb::WebRotate(const JSCallbackInfo& args) {}
2515
OnAlert(const JSCallbackInfo& args)2516 void JSWeb::OnAlert(const JSCallbackInfo& args)
2517 {
2518 JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_ALERT);
2519 }
2520
OnBeforeUnload(const JSCallbackInfo& args)2521 void JSWeb::OnBeforeUnload(const JSCallbackInfo& args)
2522 {
2523 JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD);
2524 }
2525
OnConfirm(const JSCallbackInfo& args)2526 void JSWeb::OnConfirm(const JSCallbackInfo& args)
2527 {
2528 JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_CONFIRM);
2529 }
2530
OnPrompt(const JSCallbackInfo& args)2531 void JSWeb::OnPrompt(const JSCallbackInfo& args)
2532 {
2533 JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_PROMPT);
2534 }
2535
OnCommonDialog(const JSCallbackInfo& args, int dialogEventType)2536 void JSWeb::OnCommonDialog(const JSCallbackInfo& args, int dialogEventType)
2537 {
2538 if (!args[0]->IsFunction()) {
2539 return;
2540 }
2541 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2542 auto jsFunc =
2543 AceType::MakeRefPtr<JsEventFunction<WebDialogEvent, 1>>(JSRef<JSFunc>::Cast(args[0]), WebDialogEventToJSValue);
2544 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2545 const BaseEventInfo* info) -> bool {
2546 auto webNode = node.Upgrade();
2547 CHECK_NULL_RETURN(webNode, false);
2548 ContainerScope scope(webNode->GetInstanceId());
2549 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2550 auto pipelineContext = PipelineContext::GetCurrentContext();
2551 if (pipelineContext) {
2552 pipelineContext->UpdateCurrentActiveNode(node);
2553 }
2554 auto* eventInfo = TypeInfoHelper::DynamicCast<WebDialogEvent>(info);
2555 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2556 if (message->IsBoolean()) {
2557 return message->ToBoolean();
2558 } else {
2559 return false;
2560 }
2561 };
2562 WebModel::GetInstance()->SetOnCommonDialog(jsCallback, dialogEventType);
2563 }
2564
OnConsoleLog(const JSCallbackInfo& args)2565 void JSWeb::OnConsoleLog(const JSCallbackInfo& args)
2566 {
2567 if (!args[0]->IsFunction()) {
2568 return;
2569 }
2570 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebConsoleLogEvent, 1>>(
2571 JSRef<JSFunc>::Cast(args[0]), LoadWebConsoleLogEventToJSValue);
2572
2573 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2574 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2575 const BaseEventInfo* info) -> bool {
2576 bool result = false;
2577 auto webNode = node.Upgrade();
2578 CHECK_NULL_RETURN(webNode, false);
2579 ContainerScope scope(webNode->GetInstanceId());
2580 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, result);
2581 auto pipelineContext = PipelineContext::GetCurrentContext();
2582 if (pipelineContext) {
2583 pipelineContext->UpdateCurrentActiveNode(node);
2584 }
2585 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebConsoleLogEvent>(info);
2586 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2587 if (message->IsBoolean()) {
2588 result = message->ToBoolean();
2589 }
2590 return result;
2591 };
2592
2593 WebModel::GetInstance()->SetOnConsoleLog(jsCallback);
2594 }
2595
OnPageStart(const JSCallbackInfo& args)2596 void JSWeb::OnPageStart(const JSCallbackInfo& args)
2597 {
2598 if (!args[0]->IsFunction()) {
2599 return;
2600 }
2601 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebPageStartEvent, 1>>(
2602 JSRef<JSFunc>::Cast(args[0]), LoadWebPageStartEventToJSValue);
2603
2604 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2605 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2606 const BaseEventInfo* info) {
2607 auto webNode = node.Upgrade();
2608 CHECK_NULL_VOID(webNode);
2609 ContainerScope scope(webNode->GetInstanceId());
2610 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2611 auto pipelineContext = PipelineContext::GetCurrentContext();
2612 if (pipelineContext) {
2613 pipelineContext->UpdateCurrentActiveNode(node);
2614 }
2615 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebPageStartEvent>(info);
2616 func->Execute(*eventInfo);
2617 };
2618 WebModel::GetInstance()->SetOnPageStart(jsCallback);
2619 }
2620
OnPageFinish(const JSCallbackInfo& args)2621 void JSWeb::OnPageFinish(const JSCallbackInfo& args)
2622 {
2623 if (!args[0]->IsFunction()) {
2624 return;
2625 }
2626 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebPageFinishEvent, 1>>(
2627 JSRef<JSFunc>::Cast(args[0]), LoadWebPageFinishEventToJSValue);
2628
2629 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2630 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2631 const BaseEventInfo* info) {
2632 auto webNode = node.Upgrade();
2633 CHECK_NULL_VOID(webNode);
2634 ContainerScope scope(webNode->GetInstanceId());
2635 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2636 auto pipelineContext = PipelineContext::GetCurrentContext();
2637 if (pipelineContext) {
2638 pipelineContext->UpdateCurrentActiveNode(node);
2639 }
2640 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebPageFinishEvent>(info);
2641 func->Execute(*eventInfo);
2642 };
2643 WebModel::GetInstance()->SetOnPageFinish(jsCallback);
2644 }
2645
OnProgressChange(const JSCallbackInfo& args)2646 void JSWeb::OnProgressChange(const JSCallbackInfo& args)
2647 {
2648 if (args.Length() < 1 || !args[0]->IsFunction()) {
2649 return;
2650 }
2651 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebProgressChangeEvent, 1>>(
2652 JSRef<JSFunc>::Cast(args[0]), LoadWebProgressChangeEventToJSValue);
2653
2654 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2655 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2656 const BaseEventInfo* info) {
2657 auto webNode = node.Upgrade();
2658 CHECK_NULL_VOID(webNode);
2659 ContainerScope scope(webNode->GetInstanceId());
2660 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2661 auto pipelineContext = PipelineContext::GetCurrentContext();
2662 if (pipelineContext) {
2663 pipelineContext->UpdateCurrentActiveNode(node);
2664 }
2665 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebProgressChangeEvent>(info);
2666 func->ExecuteWithValue(*eventInfo);
2667 };
2668 WebModel::GetInstance()->SetOnProgressChange(jsCallback);
2669 }
2670
OnTitleReceive(const JSCallbackInfo& args)2671 void JSWeb::OnTitleReceive(const JSCallbackInfo& args)
2672 {
2673 if (!args[0]->IsFunction()) {
2674 return;
2675 }
2676 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2677 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebTitleReceiveEvent, 1>>(
2678 JSRef<JSFunc>::Cast(args[0]), LoadWebTitleReceiveEventToJSValue);
2679 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2680 const BaseEventInfo* info) {
2681 auto webNode = node.Upgrade();
2682 CHECK_NULL_VOID(webNode);
2683 ContainerScope scope(webNode->GetInstanceId());
2684 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2685 auto pipelineContext = PipelineContext::GetCurrentContext();
2686 if (pipelineContext) {
2687 pipelineContext->UpdateCurrentActiveNode(node);
2688 }
2689 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebTitleReceiveEvent>(info);
2690 func->Execute(*eventInfo);
2691 };
2692 WebModel::GetInstance()->SetOnTitleReceive(jsCallback);
2693 }
2694
OnFullScreenExit(const JSCallbackInfo& args)2695 void JSWeb::OnFullScreenExit(const JSCallbackInfo& args)
2696 {
2697 if (!args[0]->IsFunction()) {
2698 return;
2699 }
2700 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2701 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FullScreenExitEvent, 1>>(
2702 JSRef<JSFunc>::Cast(args[0]), FullScreenExitEventToJSValue);
2703 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2704 const BaseEventInfo* info) {
2705 auto webNode = node.Upgrade();
2706 CHECK_NULL_VOID(webNode);
2707 ContainerScope scope(webNode->GetInstanceId());
2708 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2709 auto pipelineContext = PipelineContext::GetCurrentContext();
2710 if (pipelineContext) {
2711 pipelineContext->UpdateCurrentActiveNode(node);
2712 }
2713 auto* eventInfo = TypeInfoHelper::DynamicCast<FullScreenExitEvent>(info);
2714 func->Execute(*eventInfo);
2715 };
2716 WebModel::GetInstance()->SetOnFullScreenExit(jsCallback);
2717 }
2718
OnFullScreenEnter(const JSCallbackInfo& args)2719 void JSWeb::OnFullScreenEnter(const JSCallbackInfo& args)
2720 {
2721 if (args.Length() < 1 || !args[0]->IsFunction()) {
2722 return;
2723 }
2724 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2725 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FullScreenEnterEvent, 1>>(
2726 JSRef<JSFunc>::Cast(args[0]), FullScreenEnterEventToJSValue);
2727 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2728 const BaseEventInfo* info) {
2729 auto webNode = node.Upgrade();
2730 CHECK_NULL_VOID(webNode);
2731 ContainerScope scope(webNode->GetInstanceId());
2732 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2733 CHECK_NULL_VOID(func);
2734 auto pipelineContext = PipelineContext::GetCurrentContext();
2735 if (pipelineContext) {
2736 pipelineContext->UpdateCurrentActiveNode(node);
2737 }
2738 auto* eventInfo = TypeInfoHelper::DynamicCast<FullScreenEnterEvent>(info);
2739 CHECK_NULL_VOID(eventInfo);
2740 func->Execute(*eventInfo);
2741 };
2742 WebModel::GetInstance()->SetOnFullScreenEnter(jsCallback);
2743 }
2744
OnGeolocationHide(const JSCallbackInfo& args)2745 void JSWeb::OnGeolocationHide(const JSCallbackInfo& args)
2746 {
2747 if (!args[0]->IsFunction()) {
2748 return;
2749 }
2750 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2751 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebGeolocationHideEvent, 1>>(
2752 JSRef<JSFunc>::Cast(args[0]), LoadWebGeolocationHideEventToJSValue);
2753 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2754 const BaseEventInfo* info) {
2755 auto webNode = node.Upgrade();
2756 CHECK_NULL_VOID(webNode);
2757 ContainerScope scope(webNode->GetInstanceId());
2758 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2759 auto pipelineContext = PipelineContext::GetCurrentContext();
2760 if (pipelineContext) {
2761 pipelineContext->UpdateCurrentActiveNode(node);
2762 }
2763 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebGeolocationHideEvent>(info);
2764 func->Execute(*eventInfo);
2765 };
2766 WebModel::GetInstance()->SetOnGeolocationHide(jsCallback);
2767 }
2768
OnGeolocationShow(const JSCallbackInfo& args)2769 void JSWeb::OnGeolocationShow(const JSCallbackInfo& args)
2770 {
2771 if (!args[0]->IsFunction()) {
2772 return;
2773 }
2774 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2775 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebGeolocationShowEvent, 1>>(
2776 JSRef<JSFunc>::Cast(args[0]), LoadWebGeolocationShowEventToJSValue);
2777 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2778 const BaseEventInfo* info) {
2779 auto webNode = node.Upgrade();
2780 CHECK_NULL_VOID(webNode);
2781 ContainerScope scope(webNode->GetInstanceId());
2782 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2783 auto pipelineContext = PipelineContext::GetCurrentContext();
2784 if (pipelineContext) {
2785 pipelineContext->UpdateCurrentActiveNode(node);
2786 }
2787 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebGeolocationShowEvent>(info);
2788 func->Execute(*eventInfo);
2789 };
2790 WebModel::GetInstance()->SetOnGeolocationShow(jsCallback);
2791 }
2792
OnRequestFocus(const JSCallbackInfo& args)2793 void JSWeb::OnRequestFocus(const JSCallbackInfo& args)
2794 {
2795 if (!args[0]->IsFunction()) {
2796 return;
2797 }
2798 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2799 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebRequestFocusEvent, 1>>(
2800 JSRef<JSFunc>::Cast(args[0]), LoadWebRequestFocusEventToJSValue);
2801 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2802 const BaseEventInfo* info) {
2803 auto webNode = node.Upgrade();
2804 CHECK_NULL_VOID(webNode);
2805 ContainerScope scope(webNode->GetInstanceId());
2806 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2807 auto pipelineContext = PipelineContext::GetCurrentContext();
2808 if (pipelineContext) {
2809 pipelineContext->UpdateCurrentActiveNode(node);
2810 }
2811 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebRequestFocusEvent>(info);
2812 func->Execute(*eventInfo);
2813 };
2814 WebModel::GetInstance()->SetOnRequestFocus(jsCallback);
2815 }
2816
OnDownloadStart(const JSCallbackInfo& args)2817 void JSWeb::OnDownloadStart(const JSCallbackInfo& args)
2818 {
2819 if (!args[0]->IsFunction()) {
2820 return;
2821 }
2822 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2823 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<DownloadStartEvent, 1>>(
2824 JSRef<JSFunc>::Cast(args[0]), DownloadStartEventToJSValue);
2825 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2826 const BaseEventInfo* info) {
2827 auto webNode = node.Upgrade();
2828 CHECK_NULL_VOID(webNode);
2829 ContainerScope scope(webNode->GetInstanceId());
2830 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2831 auto pipelineContext = PipelineContext::GetCurrentContext();
2832 if (pipelineContext) {
2833 pipelineContext->UpdateCurrentActiveNode(node);
2834 }
2835 auto* eventInfo = TypeInfoHelper::DynamicCast<DownloadStartEvent>(info);
2836 func->Execute(*eventInfo);
2837 };
2838 WebModel::GetInstance()->SetOnDownloadStart(jsCallback);
2839 }
2840
OnHttpAuthRequest(const JSCallbackInfo& args)2841 void JSWeb::OnHttpAuthRequest(const JSCallbackInfo& args)
2842 {
2843 if (args.Length() < 1 || !args[0]->IsFunction()) {
2844 return;
2845 }
2846 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2847 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebHttpAuthEvent, 1>>(
2848 JSRef<JSFunc>::Cast(args[0]), WebHttpAuthEventToJSValue);
2849 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2850 const BaseEventInfo* info) -> bool {
2851 auto webNode = node.Upgrade();
2852 CHECK_NULL_RETURN(webNode, false);
2853 ContainerScope scope(webNode->GetInstanceId());
2854 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2855 auto pipelineContext = PipelineContext::GetCurrentContext();
2856 if (pipelineContext) {
2857 pipelineContext->UpdateCurrentActiveNode(node);
2858 }
2859 auto* eventInfo = TypeInfoHelper::DynamicCast<WebHttpAuthEvent>(info);
2860 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2861 if (message->IsBoolean()) {
2862 return message->ToBoolean();
2863 }
2864 return false;
2865 };
2866 WebModel::GetInstance()->SetOnHttpAuthRequest(jsCallback);
2867 }
2868
OnSslErrRequest(const JSCallbackInfo& args)2869 void JSWeb::OnSslErrRequest(const JSCallbackInfo& args)
2870 {
2871 return;
2872 }
2873
OnSslErrorRequest(const JSCallbackInfo& args)2874 void JSWeb::OnSslErrorRequest(const JSCallbackInfo& args)
2875 {
2876 if (args.Length() < 1 || !args[0]->IsFunction()) {
2877 return;
2878 }
2879 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2880 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebSslErrorEvent, 1>>(
2881 JSRef<JSFunc>::Cast(args[0]), WebSslErrorEventToJSValue);
2882 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2883 const BaseEventInfo* info) -> bool {
2884 auto webNode = node.Upgrade();
2885 CHECK_NULL_RETURN(webNode, false);
2886 ContainerScope scope(webNode->GetInstanceId());
2887 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2888 auto pipelineContext = PipelineContext::GetCurrentContext();
2889 if (pipelineContext) {
2890 pipelineContext->UpdateCurrentActiveNode(node);
2891 }
2892 auto* eventInfo = TypeInfoHelper::DynamicCast<WebSslErrorEvent>(info);
2893 func->Execute(*eventInfo);
2894 return true;
2895 };
2896 WebModel::GetInstance()->SetOnSslErrorRequest(jsCallback);
2897 }
2898
OnAllSslErrorRequest(const JSCallbackInfo& args)2899 void JSWeb::OnAllSslErrorRequest(const JSCallbackInfo& args)
2900 {
2901 if (args.Length() < 1 || !args[0]->IsFunction()) {
2902 return;
2903 }
2904 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2905 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebAllSslErrorEvent, 1>>(
2906 JSRef<JSFunc>::Cast(args[0]), WebAllSslErrorEventToJSValue);
2907 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2908 const BaseEventInfo* info) -> bool {
2909 auto webNode = node.Upgrade();
2910 CHECK_NULL_RETURN(webNode, false);
2911 ContainerScope scope(webNode->GetInstanceId());
2912 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2913 auto pipelineContext = PipelineContext::GetCurrentContext();
2914 if (pipelineContext) {
2915 pipelineContext->UpdateCurrentActiveNode(node);
2916 }
2917 auto* eventInfo = TypeInfoHelper::DynamicCast<WebAllSslErrorEvent>(info);
2918 func->Execute(*eventInfo);
2919 return true;
2920 };
2921 WebModel::GetInstance()->SetOnAllSslErrorRequest(jsCallback);
2922 }
2923
OnSslSelectCertRequest(const JSCallbackInfo& args)2924 void JSWeb::OnSslSelectCertRequest(const JSCallbackInfo& args)
2925 {
2926 if (args.Length() < 1 || !args[0]->IsFunction()) {
2927 return;
2928 }
2929 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2930 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebSslSelectCertEvent, 1>>(
2931 JSRef<JSFunc>::Cast(args[0]), WebSslSelectCertEventToJSValue);
2932 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
2933 const BaseEventInfo* info) -> bool {
2934 auto webNode = node.Upgrade();
2935 CHECK_NULL_RETURN(webNode, false);
2936 ContainerScope scope(webNode->GetInstanceId());
2937 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2938 auto pipelineContext = PipelineContext::GetCurrentContext();
2939 if (pipelineContext) {
2940 pipelineContext->UpdateCurrentActiveNode(node);
2941 }
2942 auto* eventInfo = TypeInfoHelper::DynamicCast<WebSslSelectCertEvent>(info);
2943 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2944 if (message->IsBoolean()) {
2945 return message->ToBoolean();
2946 }
2947 return false;
2948 };
2949 WebModel::GetInstance()->SetOnSslSelectCertRequest(jsCallback);
2950 }
2951
MediaPlayGestureAccess(bool isNeedGestureAccess)2952 void JSWeb::MediaPlayGestureAccess(bool isNeedGestureAccess)
2953 {
2954 WebModel::GetInstance()->SetMediaPlayGestureAccess(isNeedGestureAccess);
2955 }
2956
OnKeyEvent(const JSCallbackInfo& args)2957 void JSWeb::OnKeyEvent(const JSCallbackInfo& args)
2958 {
2959 if (args.Length() < 1 || !args[0]->IsFunction()) {
2960 return;
2961 }
2962
2963 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
2964 RefPtr<JsKeyFunction> jsOnKeyEventFunc = AceType::MakeRefPtr<JsKeyFunction>(JSRef<JSFunc>::Cast(args[0]));
2965 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsOnKeyEventFunc), node = frameNode](
2966 KeyEventInfo& keyEventInfo) {
2967 auto webNode = node.Upgrade();
2968 CHECK_NULL_VOID(webNode);
2969 ContainerScope scope(webNode->GetInstanceId());
2970 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2971 auto pipelineContext = PipelineContext::GetCurrentContext();
2972 if (pipelineContext) {
2973 pipelineContext->UpdateCurrentActiveNode(node);
2974 }
2975 func->Execute(keyEventInfo);
2976 };
2977 WebModel::GetInstance()->SetOnKeyEvent(jsCallback);
2978 }
2979
ReceivedErrorEventToJSValue(const ReceivedErrorEvent& eventInfo)2980 JSRef<JSVal> ReceivedErrorEventToJSValue(const ReceivedErrorEvent& eventInfo)
2981 {
2982 JSRef<JSObject> obj = JSRef<JSObject>::New();
2983
2984 JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
2985 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
2986 requestEvent->SetErrorEvent(eventInfo);
2987
2988 JSRef<JSObject> errorObj = JSClass<JSWebResourceError>::NewInstance();
2989 auto errorEvent = Referenced::Claim(errorObj->Unwrap<JSWebResourceError>());
2990 errorEvent->SetEvent(eventInfo);
2991
2992 obj->SetPropertyObject("request", requestObj);
2993 obj->SetPropertyObject("error", errorObj);
2994
2995 return JSRef<JSVal>::Cast(obj);
2996 }
2997
ReceivedHttpErrorEventToJSValue(const ReceivedHttpErrorEvent& eventInfo)2998 JSRef<JSVal> ReceivedHttpErrorEventToJSValue(const ReceivedHttpErrorEvent& eventInfo)
2999 {
3000 JSRef<JSObject> obj = JSRef<JSObject>::New();
3001
3002 JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
3003 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
3004 requestEvent->SetHttpErrorEvent(eventInfo);
3005
3006 JSRef<JSObject> responseObj = JSClass<JSWebResourceResponse>::NewInstance();
3007 auto responseEvent = Referenced::Claim(responseObj->Unwrap<JSWebResourceResponse>());
3008 responseEvent->SetEvent(eventInfo);
3009
3010 obj->SetPropertyObject("request", requestObj);
3011 obj->SetPropertyObject("response", responseObj);
3012
3013 return JSRef<JSVal>::Cast(obj);
3014 }
3015
OnErrorReceive(const JSCallbackInfo& args)3016 void JSWeb::OnErrorReceive(const JSCallbackInfo& args)
3017 {
3018 if (!args[0]->IsFunction()) {
3019 return;
3020 }
3021 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3022 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ReceivedErrorEvent, 1>>(
3023 JSRef<JSFunc>::Cast(args[0]), ReceivedErrorEventToJSValue);
3024 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3025 const BaseEventInfo* info) {
3026 auto webNode = node.Upgrade();
3027 CHECK_NULL_VOID(webNode);
3028 ContainerScope scope(webNode->GetInstanceId());
3029 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3030 auto pipelineContext = PipelineContext::GetCurrentContext();
3031 if (pipelineContext) {
3032 pipelineContext->UpdateCurrentActiveNode(node);
3033 }
3034 auto* eventInfo = TypeInfoHelper::DynamicCast<ReceivedErrorEvent>(info);
3035 func->Execute(*eventInfo);
3036 };
3037 WebModel::GetInstance()->SetOnErrorReceive(jsCallback);
3038 }
3039
OnHttpErrorReceive(const JSCallbackInfo& args)3040 void JSWeb::OnHttpErrorReceive(const JSCallbackInfo& args)
3041 {
3042 if (!args[0]->IsFunction()) {
3043 return;
3044 }
3045 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3046 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ReceivedHttpErrorEvent, 1>>(
3047 JSRef<JSFunc>::Cast(args[0]), ReceivedHttpErrorEventToJSValue);
3048 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3049 const BaseEventInfo* info) {
3050 auto webNode = node.Upgrade();
3051 CHECK_NULL_VOID(webNode);
3052 ContainerScope scope(webNode->GetInstanceId());
3053 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3054 auto pipelineContext = PipelineContext::GetCurrentContext();
3055 if (pipelineContext) {
3056 pipelineContext->UpdateCurrentActiveNode(node);
3057 }
3058 auto* eventInfo = TypeInfoHelper::DynamicCast<ReceivedHttpErrorEvent>(info);
3059 func->Execute(*eventInfo);
3060 };
3061 WebModel::GetInstance()->SetOnHttpErrorReceive(jsCallback);
3062 }
3063
OnInterceptRequestEventToJSValue(const OnInterceptRequestEvent& eventInfo)3064 JSRef<JSVal> OnInterceptRequestEventToJSValue(const OnInterceptRequestEvent& eventInfo)
3065 {
3066 JSRef<JSObject> obj = JSRef<JSObject>::New();
3067 JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
3068 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
3069 requestEvent->SetOnInterceptRequestEvent(eventInfo);
3070 obj->SetPropertyObject("request", requestObj);
3071 return JSRef<JSVal>::Cast(obj);
3072 }
3073
OnInterceptRequest(const JSCallbackInfo& args)3074 void JSWeb::OnInterceptRequest(const JSCallbackInfo& args)
3075 {
3076 if ((args.Length() <= 0) || !args[0]->IsFunction()) {
3077 return;
3078 }
3079 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3080 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<OnInterceptRequestEvent, 1>>(
3081 JSRef<JSFunc>::Cast(args[0]), OnInterceptRequestEventToJSValue);
3082 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3083 const BaseEventInfo* info) -> RefPtr<WebResponse> {
3084 auto webNode = node.Upgrade();
3085 CHECK_NULL_RETURN(webNode, nullptr);
3086 ContainerScope scope(webNode->GetInstanceId());
3087 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, nullptr);
3088 auto pipelineContext = PipelineContext::GetCurrentContext();
3089 if (pipelineContext) {
3090 pipelineContext->UpdateCurrentActiveNode(node);
3091 }
3092 auto* eventInfo = TypeInfoHelper::DynamicCast<OnInterceptRequestEvent>(info);
3093 JSRef<JSVal> obj = func->ExecuteWithValue(*eventInfo);
3094 if (!obj->IsObject()) {
3095 return nullptr;
3096 }
3097 auto jsResponse = JSRef<JSObject>::Cast(obj)->Unwrap<JSWebResourceResponse>();
3098 if (jsResponse) {
3099 return jsResponse->GetResponseObj();
3100 }
3101 return nullptr;
3102 };
3103 WebModel::GetInstance()->SetOnInterceptRequest(jsCallback);
3104 }
3105
OnUrlLoadIntercept(const JSCallbackInfo& args)3106 void JSWeb::OnUrlLoadIntercept(const JSCallbackInfo& args)
3107 {
3108 if (!args[0]->IsFunction()) {
3109 return;
3110 }
3111 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3112 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<UrlLoadInterceptEvent, 1>>(
3113 JSRef<JSFunc>::Cast(args[0]), UrlLoadInterceptEventToJSValue);
3114 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3115 const BaseEventInfo* info) -> bool {
3116 auto webNode = node.Upgrade();
3117 CHECK_NULL_RETURN(webNode, false);
3118 ContainerScope scope(webNode->GetInstanceId());
3119 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
3120 auto pipelineContext = PipelineContext::GetCurrentContext();
3121 if (pipelineContext) {
3122 pipelineContext->UpdateCurrentActiveNode(node);
3123 }
3124 auto* eventInfo = TypeInfoHelper::DynamicCast<UrlLoadInterceptEvent>(info);
3125 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
3126 if (message->IsBoolean()) {
3127 return message->ToBoolean();
3128 }
3129 return false;
3130 };
3131 WebModel::GetInstance()->SetOnUrlLoadIntercept(jsCallback);
3132 }
3133
OnLoadIntercept(const JSCallbackInfo& args)3134 void JSWeb::OnLoadIntercept(const JSCallbackInfo& args)
3135 {
3136 if (!args[0]->IsFunction()) {
3137 return;
3138 }
3139 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadInterceptEvent, 1>>(
3140 JSRef<JSFunc>::Cast(args[0]), LoadInterceptEventToJSValue);
3141
3142 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3143 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3144 const BaseEventInfo* info) -> bool {
3145 auto webNode = node.Upgrade();
3146 CHECK_NULL_RETURN(webNode, false);
3147 ContainerScope scope(webNode->GetInstanceId());
3148 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
3149 auto pipelineContext = PipelineContext::GetCurrentContext();
3150 if (pipelineContext) {
3151 pipelineContext->UpdateCurrentActiveNode(node);
3152 }
3153 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadInterceptEvent>(info);
3154 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
3155 if (message->IsBoolean()) {
3156 return message->ToBoolean();
3157 }
3158 return false;
3159 };
3160 WebModel::GetInstance()->SetOnLoadIntercept(std::move(uiCallback));
3161 }
3162
FileSelectorEventToJSValue(const FileSelectorEvent& eventInfo)3163 JSRef<JSVal> FileSelectorEventToJSValue(const FileSelectorEvent& eventInfo)
3164 {
3165 JSRef<JSObject> obj = JSRef<JSObject>::New();
3166
3167 JSRef<JSObject> paramObj = JSClass<JSFileSelectorParam>::NewInstance();
3168 auto fileSelectorParam = Referenced::Claim(paramObj->Unwrap<JSFileSelectorParam>());
3169 fileSelectorParam->SetParam(eventInfo);
3170
3171 JSRef<JSObject> resultObj = JSClass<JSFileSelectorResult>::NewInstance();
3172 auto fileSelectorResult = Referenced::Claim(resultObj->Unwrap<JSFileSelectorResult>());
3173 fileSelectorResult->SetResult(eventInfo);
3174
3175 obj->SetPropertyObject("result", resultObj);
3176 obj->SetPropertyObject("fileSelector", paramObj);
3177 return JSRef<JSVal>::Cast(obj);
3178 }
3179
OnFileSelectorShow(const JSCallbackInfo& args)3180 void JSWeb::OnFileSelectorShow(const JSCallbackInfo& args)
3181 {
3182 if (!args[0]->IsFunction()) {
3183 return;
3184 }
3185
3186 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3187 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FileSelectorEvent, 1>>(
3188 JSRef<JSFunc>::Cast(args[0]), FileSelectorEventToJSValue);
3189 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3190 const BaseEventInfo* info) -> bool {
3191 auto webNode = node.Upgrade();
3192 CHECK_NULL_RETURN(webNode, false);
3193 ContainerScope scope(webNode->GetInstanceId());
3194 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
3195 auto pipelineContext = PipelineContext::GetCurrentContext();
3196 if (pipelineContext) {
3197 pipelineContext->UpdateCurrentActiveNode(node);
3198 }
3199 auto* eventInfo = TypeInfoHelper::DynamicCast<FileSelectorEvent>(info);
3200 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
3201 if (message->IsBoolean()) {
3202 return message->ToBoolean();
3203 }
3204 return false;
3205 };
3206 WebModel::GetInstance()->SetOnFileSelectorShow(jsCallback);
3207 }
3208
ContextMenuEventToJSValue(const ContextMenuEvent& eventInfo)3209 JSRef<JSVal> ContextMenuEventToJSValue(const ContextMenuEvent& eventInfo)
3210 {
3211 JSRef<JSObject> obj = JSRef<JSObject>::New();
3212
3213 JSRef<JSObject> paramObj = JSClass<JSContextMenuParam>::NewInstance();
3214 auto contextMenuParam = Referenced::Claim(paramObj->Unwrap<JSContextMenuParam>());
3215 contextMenuParam->SetParam(eventInfo);
3216
3217 JSRef<JSObject> resultObj = JSClass<JSContextMenuResult>::NewInstance();
3218 auto contextMenuResult = Referenced::Claim(resultObj->Unwrap<JSContextMenuResult>());
3219 contextMenuResult->SetResult(eventInfo);
3220
3221 obj->SetPropertyObject("result", resultObj);
3222 obj->SetPropertyObject("param", paramObj);
3223 return JSRef<JSVal>::Cast(obj);
3224 }
3225
OnContextMenuShow(const JSCallbackInfo& args)3226 void JSWeb::OnContextMenuShow(const JSCallbackInfo& args)
3227 {
3228 if (args.Length() < 1 || !args[0]->IsFunction()) {
3229 return;
3230 }
3231 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3232 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ContextMenuEvent, 1>>(
3233 JSRef<JSFunc>::Cast(args[0]), ContextMenuEventToJSValue);
3234 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3235 const BaseEventInfo* info) -> bool {
3236 auto webNode = node.Upgrade();
3237 CHECK_NULL_RETURN(webNode, false);
3238 ContainerScope scope(webNode->GetInstanceId());
3239 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
3240 auto pipelineContext = PipelineContext::GetCurrentContext();
3241 if (pipelineContext) {
3242 pipelineContext->UpdateCurrentActiveNode(node);
3243 }
3244 auto* eventInfo = TypeInfoHelper::DynamicCast<ContextMenuEvent>(info);
3245 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
3246 if (message->IsBoolean()) {
3247 return message->ToBoolean();
3248 }
3249 return false;
3250 };
3251 WebModel::GetInstance()->SetOnContextMenuShow(jsCallback);
3252 }
3253
ParseBindSelectionMenuParam( const JSCallbackInfo& info, const JSRef<JSObject>& menuOptions, NG::MenuParam& menuParam)3254 void ParseBindSelectionMenuParam(
3255 const JSCallbackInfo& info, const JSRef<JSObject>& menuOptions, NG::MenuParam& menuParam)
3256 {
3257 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3258 auto onDisappearValue = menuOptions->GetProperty("onDisappear");
3259 if (onDisappearValue->IsFunction()) {
3260 RefPtr<JsFunction> jsOnDisAppearFunc =
3261 AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(onDisappearValue));
3262 auto onDisappear = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDisAppearFunc),
3263 node = frameNode]() {
3264 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3265 ACE_SCORING_EVENT("onDisappear");
3266 PipelineContext::SetCallBackNode(node);
3267 func->Execute();
3268 };
3269 menuParam.onDisappear = std::move(onDisappear);
3270 }
3271
3272 auto onAppearValue = menuOptions->GetProperty("onAppear");
3273 if (onAppearValue->IsFunction()) {
3274 RefPtr<JsFunction> jsOnAppearFunc =
3275 AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(onAppearValue));
3276 auto onAppear = [execCtx = info.GetExecutionContext(), func = std::move(jsOnAppearFunc),
3277 node = frameNode]() {
3278 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3279 ACE_SCORING_EVENT("onAppear");
3280 PipelineContext::SetCallBackNode(node);
3281 func->Execute();
3282 };
3283 menuParam.onAppear = std::move(onAppear);
3284 }
3285 }
3286
ParseBindSelectionMenuOptionParam(const JSCallbackInfo& info, const JSRef<JSVal>& args, NG::MenuParam& menuParam, std::function<void()>& previewBuildFunc)3287 void ParseBindSelectionMenuOptionParam(const JSCallbackInfo& info, const JSRef<JSVal>& args,
3288 NG::MenuParam& menuParam, std::function<void()>& previewBuildFunc)
3289 {
3290 auto menuOptions = JSRef<JSObject>::Cast(args);
3291 ParseBindSelectionMenuParam(info, menuOptions, menuParam);
3292
3293 auto preview = menuOptions->GetProperty("preview");
3294 if (!preview->IsFunction()) {
3295 return;
3296 }
3297 auto menuType = menuOptions->GetProperty("menuType");
3298 bool isPreviewMenu = menuType->IsNumber() && menuType->ToNumber<int32_t>() == 1;
3299 if (isPreviewMenu) {
3300 menuParam.previewMode = MenuPreviewMode::CUSTOM;
3301 RefPtr<JsFunction> previewBuilderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(preview));
3302 CHECK_NULL_VOID(previewBuilderFunc);
3303 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3304 previewBuildFunc = [execCtx = info.GetExecutionContext(), func = std::move(previewBuilderFunc),
3305 node = frameNode]() {
3306 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3307 ACE_SCORING_EVENT("BindSelectionMenuPreviwer");
3308 PipelineContext::SetCallBackNode(node);
3309 func->Execute();
3310 };
3311 }
3312 }
3313
BindSelectionMenu(const JSCallbackInfo& info)3314 void JSWeb::BindSelectionMenu(const JSCallbackInfo& info)
3315 {
3316 if (info.Length() < SELECTION_MENU_OPTION_PARAM_INDEX) {
3317 return;
3318 }
3319 if (!info[0]->IsNumber() || !info[1]->IsObject()) {
3320 return;
3321 }
3322 WebElementType elementType = static_cast<WebElementType>(info[0]->ToNumber<int32_t>());
3323
3324 // Builder
3325 JSRef<JSObject> menuObj = JSRef<JSObject>::Cast(info[1]);
3326 auto builder = menuObj->GetProperty("builder");
3327 if (!builder->IsFunction()) {
3328 return;
3329 }
3330 auto builderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(builder));
3331 CHECK_NULL_VOID(builderFunc);
3332 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3333 std::function<void()> menuBuilder = [execCtx = info.GetExecutionContext(), func = std::move(builderFunc),
3334 node = frameNode]() {
3335 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3336 ACE_SCORING_EVENT("BindSelectionMenu");
3337 PipelineContext::SetCallBackNode(node);
3338 func->Execute();
3339 };
3340
3341 ResponseType responseType = ResponseType::LONG_PRESS;
3342 if (info[SELECTION_MENU_CONTENT_PARAM_INDEX]->IsNumber()) {
3343 auto response = info[SELECTION_MENU_CONTENT_PARAM_INDEX]->ToNumber<int32_t>();
3344 responseType = static_cast<ResponseType>(response);
3345 }
3346
3347 std::function<void()> previewBuilder = nullptr;
3348 NG::MenuParam menuParam;
3349 if (info.Length() > SELECTION_MENU_OPTION_PARAM_INDEX && info[SELECTION_MENU_OPTION_PARAM_INDEX]->IsObject()) {
3350 ParseBindSelectionMenuOptionParam(info, info[SELECTION_MENU_OPTION_PARAM_INDEX], menuParam, previewBuilder);
3351 }
3352
3353 if (responseType != ResponseType::LONG_PRESS) {
3354 menuParam.previewMode = MenuPreviewMode::NONE;
3355 menuParam.menuBindType = MenuBindingType::RIGHT_CLICK;
3356 }
3357 menuParam.contextMenuRegisterType = NG::ContextMenuRegisterType::CUSTOM_TYPE;
3358 menuParam.type = NG::MenuType::CONTEXT_MENU;
3359 menuParam.isShow = true;
3360 WebModel::GetInstance()->SetNewDragStyle(true);
3361 auto previewSelectionMenuParam = std::make_shared<WebPreviewSelectionMenuParam>(
3362 elementType, responseType, menuBuilder, previewBuilder, menuParam);
3363 WebModel::GetInstance()->SetPreviewSelectionMenu(previewSelectionMenuParam);
3364 }
3365
OnContextMenuHide(const JSCallbackInfo& args)3366 void JSWeb::OnContextMenuHide(const JSCallbackInfo& args)
3367 {
3368 if (!args[0]->IsFunction()) {
3369 return;
3370 }
3371 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3372 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ContextMenuHideEvent, 1>>(
3373 JSRef<JSFunc>::Cast(args[0]), ContextMenuHideEventToJSValue);
3374
3375 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3376 const BaseEventInfo* info) {
3377 auto webNode = node.Upgrade();
3378 CHECK_NULL_VOID(webNode);
3379 ContainerScope scope(webNode->GetInstanceId());
3380 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3381 auto pipelineContext = PipelineContext::GetCurrentContext();
3382 if (pipelineContext) {
3383 pipelineContext->UpdateCurrentActiveNode(node);
3384 }
3385 auto* eventInfo = TypeInfoHelper::DynamicCast<ContextMenuHideEvent>(info);
3386 func->Execute(*eventInfo);
3387 };
3388 WebModel::GetInstance()->SetOnContextMenuHide(jsCallback);
3389 }
3390
JsEnabled(bool isJsEnabled)3391 void JSWeb::JsEnabled(bool isJsEnabled)
3392 {
3393 WebModel::GetInstance()->SetJsEnabled(isJsEnabled);
3394 }
3395
ContentAccessEnabled(bool isContentAccessEnabled)3396 void JSWeb::ContentAccessEnabled(bool isContentAccessEnabled)
3397 {
3398 #if !defined(NG_BUILD) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
3399 auto stack = ViewStackProcessor::GetInstance();
3400 auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3401 if (!webComponent) {
3402 return;
3403 }
3404 webComponent->SetContentAccessEnabled(isContentAccessEnabled);
3405 #else
3406 TAG_LOGW(AceLogTag::ACE_WEB, "do not support components in new pipeline mode");
3407 #endif
3408 }
3409
FileAccessEnabled(bool isFileAccessEnabled)3410 void JSWeb::FileAccessEnabled(bool isFileAccessEnabled)
3411 {
3412 WebModel::GetInstance()->SetFileAccessEnabled(isFileAccessEnabled);
3413 }
3414
OnLineImageAccessEnabled(bool isOnLineImageAccessEnabled)3415 void JSWeb::OnLineImageAccessEnabled(bool isOnLineImageAccessEnabled)
3416 {
3417 WebModel::GetInstance()->SetOnLineImageAccessEnabled(isOnLineImageAccessEnabled);
3418 }
3419
DomStorageAccessEnabled(bool isDomStorageAccessEnabled)3420 void JSWeb::DomStorageAccessEnabled(bool isDomStorageAccessEnabled)
3421 {
3422 WebModel::GetInstance()->SetDomStorageAccessEnabled(isDomStorageAccessEnabled);
3423 }
3424
ImageAccessEnabled(bool isImageAccessEnabled)3425 void JSWeb::ImageAccessEnabled(bool isImageAccessEnabled)
3426 {
3427 WebModel::GetInstance()->SetImageAccessEnabled(isImageAccessEnabled);
3428 }
3429
MixedMode(int32_t mixedMode)3430 void JSWeb::MixedMode(int32_t mixedMode)
3431 {
3432 auto mixedContentMode = MixedModeContent::MIXED_CONTENT_NEVER_ALLOW;
3433 switch (mixedMode) {
3434 case 0:
3435 mixedContentMode = MixedModeContent::MIXED_CONTENT_ALWAYS_ALLOW;
3436 break;
3437 case 1:
3438 mixedContentMode = MixedModeContent::MIXED_CONTENT_COMPATIBILITY_MODE;
3439 break;
3440 default:
3441 mixedContentMode = MixedModeContent::MIXED_CONTENT_NEVER_ALLOW;
3442 break;
3443 }
3444 WebModel::GetInstance()->SetMixedMode(mixedContentMode);
3445 }
3446
ZoomAccessEnabled(bool isZoomAccessEnabled)3447 void JSWeb::ZoomAccessEnabled(bool isZoomAccessEnabled)
3448 {
3449 WebModel::GetInstance()->SetZoomAccessEnabled(isZoomAccessEnabled);
3450 }
3451
EnableNativeEmbedMode(bool isEmbedModeEnabled)3452 void JSWeb::EnableNativeEmbedMode(bool isEmbedModeEnabled)
3453 {
3454 WebModel::GetInstance()->SetNativeEmbedModeEnabled(isEmbedModeEnabled);
3455 }
3456
RegisterNativeEmbedRule(const std::string& tag, const std::string& type)3457 void JSWeb::RegisterNativeEmbedRule(const std::string& tag, const std::string& type)
3458 {
3459 WebModel::GetInstance()->RegisterNativeEmbedRule(tag, type);
3460 }
3461
EnableSmoothDragResize(bool isSmoothDragResizeEnabled)3462 void JSWeb::EnableSmoothDragResize(bool isSmoothDragResizeEnabled)
3463 {
3464 WebModel::GetInstance()->SetSmoothDragResizeEnabled(isSmoothDragResizeEnabled);
3465 }
3466
GeolocationAccessEnabled(bool isGeolocationAccessEnabled)3467 void JSWeb::GeolocationAccessEnabled(bool isGeolocationAccessEnabled)
3468 {
3469 WebModel::GetInstance()->SetGeolocationAccessEnabled(isGeolocationAccessEnabled);
3470 }
3471
JavaScriptProxy(const JSCallbackInfo& args)3472 void JSWeb::JavaScriptProxy(const JSCallbackInfo& args)
3473 {
3474 #if !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM)
3475 if (args.Length() < 1 || !args[0]->IsObject()) {
3476 return;
3477 }
3478 auto paramObject = JSRef<JSObject>::Cast(args[0]);
3479 auto controllerObj = paramObject->GetProperty("controller");
3480 auto object = JSRef<JSVal>::Cast(paramObject->GetProperty("object"));
3481 auto name = JSRef<JSVal>::Cast(paramObject->GetProperty("name"));
3482 auto methodList = JSRef<JSVal>::Cast(paramObject->GetProperty("methodList"));
3483 auto asyncMethodList = JSRef<JSVal>::Cast(paramObject->GetProperty("asyncMethodList"));
3484 auto permission = JSRef<JSVal>::Cast(paramObject->GetProperty("permission"));
3485 if (!controllerObj->IsObject()) {
3486 return;
3487 }
3488 auto controller = JSRef<JSObject>::Cast(controllerObj);
3489 auto jsProxyFunction = controller->GetProperty("jsProxy");
3490 if (jsProxyFunction->IsFunction()) {
3491 auto jsProxyCallback = [webviewController = controller, func = JSRef<JSFunc>::Cast(jsProxyFunction), object,
3492 name, methodList, asyncMethodList, permission]() {
3493 JSRef<JSVal> argv[] = { object, name, methodList, asyncMethodList, permission };
3494 func->Call(webviewController, 5, argv);
3495 };
3496
3497 WebModel::GetInstance()->SetJsProxyCallback(jsProxyCallback);
3498 }
3499 auto jsWebController = controller->Unwrap<JSWebController>();
3500 if (jsWebController) {
3501 jsWebController->SetJavascriptInterface(args);
3502 }
3503 #endif
3504 }
3505
UserAgent(const std::string& userAgent)3506 void JSWeb::UserAgent(const std::string& userAgent)
3507 {
3508 WebModel::GetInstance()->SetUserAgent(userAgent);
3509 }
3510
RenderExitedEventToJSValue(const RenderExitedEvent& eventInfo)3511 JSRef<JSVal> RenderExitedEventToJSValue(const RenderExitedEvent& eventInfo)
3512 {
3513 JSRef<JSObject> obj = JSRef<JSObject>::New();
3514 obj->SetProperty("renderExitReason", eventInfo.GetExitedReason());
3515 return JSRef<JSVal>::Cast(obj);
3516 }
3517
RefreshAccessedHistoryEventToJSValue(const RefreshAccessedHistoryEvent& eventInfo)3518 JSRef<JSVal> RefreshAccessedHistoryEventToJSValue(const RefreshAccessedHistoryEvent& eventInfo)
3519 {
3520 JSRef<JSObject> obj = JSRef<JSObject>::New();
3521 obj->SetProperty("url", eventInfo.GetVisitedUrl());
3522 obj->SetProperty("isRefreshed", eventInfo.IsRefreshed());
3523 return JSRef<JSVal>::Cast(obj);
3524 }
3525
OnRenderExited(const JSCallbackInfo& args)3526 void JSWeb::OnRenderExited(const JSCallbackInfo& args)
3527 {
3528 if (!args[0]->IsFunction()) {
3529 return;
3530 }
3531 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3532 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<RenderExitedEvent, 1>>(
3533 JSRef<JSFunc>::Cast(args[0]), RenderExitedEventToJSValue);
3534 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3535 const BaseEventInfo* info) {
3536 auto webNode = node.Upgrade();
3537 CHECK_NULL_VOID(webNode);
3538 ContainerScope scope(webNode->GetInstanceId());
3539 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3540 auto pipelineContext = PipelineContext::GetCurrentContext();
3541 if (pipelineContext) {
3542 pipelineContext->UpdateCurrentActiveNode(node);
3543 }
3544 auto* eventInfo = TypeInfoHelper::DynamicCast<RenderExitedEvent>(info);
3545 func->Execute(*eventInfo);
3546 };
3547 WebModel::GetInstance()->SetRenderExitedId(jsCallback);
3548 }
3549
OnRefreshAccessedHistory(const JSCallbackInfo& args)3550 void JSWeb::OnRefreshAccessedHistory(const JSCallbackInfo& args)
3551 {
3552 if (!args[0]->IsFunction()) {
3553 return;
3554 }
3555 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3556 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<RefreshAccessedHistoryEvent, 1>>(
3557 JSRef<JSFunc>::Cast(args[0]), RefreshAccessedHistoryEventToJSValue);
3558 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3559 const BaseEventInfo* info) {
3560 auto webNode = node.Upgrade();
3561 CHECK_NULL_VOID(webNode);
3562 ContainerScope scope(webNode->GetInstanceId());
3563 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3564 auto pipelineContext = PipelineContext::GetCurrentContext();
3565 if (pipelineContext) {
3566 pipelineContext->UpdateCurrentActiveNode(node);
3567 }
3568 auto* eventInfo = TypeInfoHelper::DynamicCast<RefreshAccessedHistoryEvent>(info);
3569 func->Execute(*eventInfo);
3570 };
3571 WebModel::GetInstance()->SetRefreshAccessedHistoryId(jsCallback);
3572 }
3573
CacheMode(int32_t cacheMode)3574 void JSWeb::CacheMode(int32_t cacheMode)
3575 {
3576 auto mode = WebCacheMode::DEFAULT;
3577 switch (cacheMode) {
3578 case 0:
3579 mode = WebCacheMode::DEFAULT;
3580 break;
3581 case 1:
3582 mode = WebCacheMode::USE_CACHE_ELSE_NETWORK;
3583 break;
3584 case 2:
3585 mode = WebCacheMode::USE_NO_CACHE;
3586 break;
3587 case 3:
3588 mode = WebCacheMode::USE_CACHE_ONLY;
3589 break;
3590 default:
3591 mode = WebCacheMode::DEFAULT;
3592 break;
3593 }
3594 WebModel::GetInstance()->SetCacheMode(mode);
3595 }
3596
OverScrollMode(int overScrollMode)3597 void JSWeb::OverScrollMode(int overScrollMode)
3598 {
3599 auto mode = OverScrollMode::NEVER;
3600 switch (overScrollMode) {
3601 case 0:
3602 mode = OverScrollMode::NEVER;
3603 break;
3604 case 1:
3605 mode = OverScrollMode::ALWAYS;
3606 break;
3607 default:
3608 mode = OverScrollMode::NEVER;
3609 break;
3610 }
3611 WebModel::GetInstance()->SetOverScrollMode(mode);
3612 }
3613
OverviewModeAccess(bool isOverviewModeAccessEnabled)3614 void JSWeb::OverviewModeAccess(bool isOverviewModeAccessEnabled)
3615 {
3616 WebModel::GetInstance()->SetOverviewModeAccessEnabled(isOverviewModeAccessEnabled);
3617 }
3618
FileFromUrlAccess(bool isFileFromUrlAccessEnabled)3619 void JSWeb::FileFromUrlAccess(bool isFileFromUrlAccessEnabled)
3620 {
3621 WebModel::GetInstance()->SetFileFromUrlAccessEnabled(isFileFromUrlAccessEnabled);
3622 }
3623
DatabaseAccess(bool isDatabaseAccessEnabled)3624 void JSWeb::DatabaseAccess(bool isDatabaseAccessEnabled)
3625 {
3626 WebModel::GetInstance()->SetDatabaseAccessEnabled(isDatabaseAccessEnabled);
3627 }
3628
TextZoomRatio(int32_t textZoomRatioNum)3629 void JSWeb::TextZoomRatio(int32_t textZoomRatioNum)
3630 {
3631 WebModel::GetInstance()->SetTextZoomRatio(textZoomRatioNum);
3632 }
3633
WebDebuggingAccessEnabled(bool isWebDebuggingAccessEnabled)3634 void JSWeb::WebDebuggingAccessEnabled(bool isWebDebuggingAccessEnabled)
3635 {
3636 WebModel::GetInstance()->SetWebDebuggingAccessEnabled(isWebDebuggingAccessEnabled);
3637 }
3638
OnMouse(const JSCallbackInfo& args)3639 void JSWeb::OnMouse(const JSCallbackInfo& args)
3640 {
3641 if (args.Length() < 1 || !args[0]->IsFunction()) {
3642 return;
3643 }
3644
3645 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3646 RefPtr<JsClickFunction> jsOnMouseFunc = AceType::MakeRefPtr<JsClickFunction>(JSRef<JSFunc>::Cast(args[0]));
3647 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsOnMouseFunc), node = frameNode](
3648 MouseInfo& info) {
3649 auto webNode = node.Upgrade();
3650 CHECK_NULL_VOID(webNode);
3651 ContainerScope scope(webNode->GetInstanceId());
3652 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3653 auto pipelineContext = PipelineContext::GetCurrentContext();
3654 if (pipelineContext) {
3655 pipelineContext->UpdateCurrentActiveNode(node);
3656 }
3657 func->Execute(info);
3658 };
3659 WebModel::GetInstance()->SetOnMouseEvent(jsCallback);
3660 }
3661
ResourceLoadEventToJSValue(const ResourceLoadEvent& eventInfo)3662 JSRef<JSVal> ResourceLoadEventToJSValue(const ResourceLoadEvent& eventInfo)
3663 {
3664 JSRef<JSObject> obj = JSRef<JSObject>::New();
3665 obj->SetProperty("url", eventInfo.GetOnResourceLoadUrl());
3666 return JSRef<JSVal>::Cast(obj);
3667 }
3668
OnResourceLoad(const JSCallbackInfo& args)3669 void JSWeb::OnResourceLoad(const JSCallbackInfo& args)
3670 {
3671 if (args.Length() < 1 || !args[0]->IsFunction()) {
3672 return;
3673 }
3674 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3675 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ResourceLoadEvent, 1>>(
3676 JSRef<JSFunc>::Cast(args[0]), ResourceLoadEventToJSValue);
3677 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3678 const BaseEventInfo* info) {
3679 auto webNode = node.Upgrade();
3680 CHECK_NULL_VOID(webNode);
3681 ContainerScope scope(webNode->GetInstanceId());
3682 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3683 auto pipelineContext = PipelineContext::GetCurrentContext();
3684 if (pipelineContext) {
3685 pipelineContext->UpdateCurrentActiveNode(node);
3686 }
3687 auto* eventInfo = TypeInfoHelper::DynamicCast<ResourceLoadEvent>(info);
3688 func->Execute(*eventInfo);
3689 };
3690 WebModel::GetInstance()->SetResourceLoadId(jsCallback);
3691 }
3692
ScaleChangeEventToJSValue(const ScaleChangeEvent& eventInfo)3693 JSRef<JSVal> ScaleChangeEventToJSValue(const ScaleChangeEvent& eventInfo)
3694 {
3695 JSRef<JSObject> obj = JSRef<JSObject>::New();
3696 obj->SetProperty("oldScale", eventInfo.GetOnScaleChangeOldScale());
3697 obj->SetProperty("newScale", eventInfo.GetOnScaleChangeNewScale());
3698 return JSRef<JSVal>::Cast(obj);
3699 }
3700
OnScaleChange(const JSCallbackInfo& args)3701 void JSWeb::OnScaleChange(const JSCallbackInfo& args)
3702 {
3703 if (args.Length() < 1 || !args[0]->IsFunction()) {
3704 return;
3705 }
3706 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3707 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ScaleChangeEvent, 1>>(
3708 JSRef<JSFunc>::Cast(args[0]), ScaleChangeEventToJSValue);
3709 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3710 const BaseEventInfo* info) {
3711 auto webNode = node.Upgrade();
3712 CHECK_NULL_VOID(webNode);
3713 ContainerScope scope(webNode->GetInstanceId());
3714 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3715 auto pipelineContext = PipelineContext::GetCurrentContext();
3716 if (pipelineContext) {
3717 pipelineContext->UpdateCurrentActiveNode(node);
3718 }
3719 auto* eventInfo = TypeInfoHelper::DynamicCast<ScaleChangeEvent>(info);
3720 func->Execute(*eventInfo);
3721 };
3722 WebModel::GetInstance()->SetScaleChangeId(jsCallback);
3723 }
3724
ScrollEventToJSValue(const WebOnScrollEvent& eventInfo)3725 JSRef<JSVal> ScrollEventToJSValue(const WebOnScrollEvent& eventInfo)
3726 {
3727 JSRef<JSObject> obj = JSRef<JSObject>::New();
3728 obj->SetProperty("xOffset", eventInfo.GetX());
3729 obj->SetProperty("yOffset", eventInfo.GetY());
3730 return JSRef<JSVal>::Cast(obj);
3731 }
3732
OnScroll(const JSCallbackInfo& args)3733 void JSWeb::OnScroll(const JSCallbackInfo& args)
3734 {
3735 if (args.Length() < 1 || !args[0]->IsFunction()) {
3736 return;
3737 }
3738
3739 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3740 auto jsFunc =
3741 AceType::MakeRefPtr<JsEventFunction<WebOnScrollEvent, 1>>(JSRef<JSFunc>::Cast(args[0]), ScrollEventToJSValue);
3742 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3743 const BaseEventInfo* info) {
3744 auto webNode = node.Upgrade();
3745 CHECK_NULL_VOID(webNode);
3746 ContainerScope scope(webNode->GetInstanceId());
3747 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3748 auto pipelineContext = PipelineContext::GetCurrentContext();
3749 if (pipelineContext) {
3750 pipelineContext->UpdateCurrentActiveNode(node);
3751 }
3752 auto* eventInfo = TypeInfoHelper::DynamicCast<WebOnScrollEvent>(info);
3753 func->Execute(*eventInfo);
3754 };
3755 WebModel::GetInstance()->SetScrollId(jsCallback);
3756 }
3757
PermissionRequestEventToJSValue(const WebPermissionRequestEvent& eventInfo)3758 JSRef<JSVal> PermissionRequestEventToJSValue(const WebPermissionRequestEvent& eventInfo)
3759 {
3760 JSRef<JSObject> obj = JSRef<JSObject>::New();
3761 JSRef<JSObject> permissionObj = JSClass<JSWebPermissionRequest>::NewInstance();
3762 auto permissionEvent = Referenced::Claim(permissionObj->Unwrap<JSWebPermissionRequest>());
3763 permissionEvent->SetEvent(eventInfo);
3764 obj->SetPropertyObject("request", permissionObj);
3765 return JSRef<JSVal>::Cast(obj);
3766 }
3767
OnPermissionRequest(const JSCallbackInfo& args)3768 void JSWeb::OnPermissionRequest(const JSCallbackInfo& args)
3769 {
3770 if (args.Length() < 1 || !args[0]->IsFunction()) {
3771 return;
3772 }
3773 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3774 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebPermissionRequestEvent, 1>>(
3775 JSRef<JSFunc>::Cast(args[0]), PermissionRequestEventToJSValue);
3776 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3777 const BaseEventInfo* info) {
3778 auto webNode = node.Upgrade();
3779 CHECK_NULL_VOID(webNode);
3780 ContainerScope scope(webNode->GetInstanceId());
3781 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3782 auto pipelineContext = PipelineContext::GetCurrentContext();
3783 if (pipelineContext) {
3784 pipelineContext->UpdateCurrentActiveNode(node);
3785 }
3786 auto* eventInfo = TypeInfoHelper::DynamicCast<WebPermissionRequestEvent>(info);
3787 func->Execute(*eventInfo);
3788 };
3789 WebModel::GetInstance()->SetPermissionRequestEventId(jsCallback);
3790 }
3791
ScreenCaptureRequestEventToJSValue(const WebScreenCaptureRequestEvent& eventInfo)3792 JSRef<JSVal> ScreenCaptureRequestEventToJSValue(const WebScreenCaptureRequestEvent& eventInfo)
3793 {
3794 JSRef<JSObject> obj = JSRef<JSObject>::New();
3795 JSRef<JSObject> requestObj = JSClass<JSScreenCaptureRequest>::NewInstance();
3796 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSScreenCaptureRequest>());
3797 requestEvent->SetEvent(eventInfo);
3798 obj->SetPropertyObject("handler", requestObj);
3799 return JSRef<JSVal>::Cast(obj);
3800 }
3801
OnScreenCaptureRequest(const JSCallbackInfo& args)3802 void JSWeb::OnScreenCaptureRequest(const JSCallbackInfo& args)
3803 {
3804 if (args.Length() < 1 || !args[0]->IsFunction()) {
3805 return;
3806 }
3807 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3808 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebScreenCaptureRequestEvent, 1>>(
3809 JSRef<JSFunc>::Cast(args[0]), ScreenCaptureRequestEventToJSValue);
3810 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3811 const BaseEventInfo* info) {
3812 auto webNode = node.Upgrade();
3813 CHECK_NULL_VOID(webNode);
3814 ContainerScope scope(webNode->GetInstanceId());
3815 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3816 auto pipelineContext = PipelineContext::GetCurrentContext();
3817 if (pipelineContext) {
3818 pipelineContext->UpdateCurrentActiveNode(node);
3819 }
3820 auto* eventInfo = TypeInfoHelper::DynamicCast<WebScreenCaptureRequestEvent>(info);
3821 func->Execute(*eventInfo);
3822 };
3823 WebModel::GetInstance()->SetScreenCaptureRequestEventId(jsCallback);
3824 }
3825
BackgroundColor(const JSCallbackInfo& info)3826 void JSWeb::BackgroundColor(const JSCallbackInfo& info)
3827 {
3828 if (info.Length() < 1) {
3829 return;
3830 }
3831 Color backgroundColor;
3832 if (!ParseJsColor(info[0], backgroundColor)) {
3833 backgroundColor = WebModel::GetInstance()->GetDefaultBackgroundColor();
3834 }
3835 WebModel::GetInstance()->SetBackgroundColor(backgroundColor);
3836 }
3837
InitialScale(float scale)3838 void JSWeb::InitialScale(float scale)
3839 {
3840 WebModel::GetInstance()->InitialScale(scale);
3841 }
3842
Password(bool password)3843 void JSWeb::Password(bool password) {}
3844
TableData(bool tableData)3845 void JSWeb::TableData(bool tableData) {}
3846
OnFileSelectorShowAbandoned(const JSCallbackInfo& args)3847 void JSWeb::OnFileSelectorShowAbandoned(const JSCallbackInfo& args) {}
3848
WideViewModeAccess(const JSCallbackInfo& args)3849 void JSWeb::WideViewModeAccess(const JSCallbackInfo& args) {}
3850
WebDebuggingAccess(const JSCallbackInfo& args)3851 void JSWeb::WebDebuggingAccess(const JSCallbackInfo& args) {}
3852
OnSearchResultReceive(const JSCallbackInfo& args)3853 void JSWeb::OnSearchResultReceive(const JSCallbackInfo& args)
3854 {
3855 if (!args[0]->IsFunction()) {
3856 return;
3857 }
3858 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3859 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<SearchResultReceiveEvent, 1>>(
3860 JSRef<JSFunc>::Cast(args[0]), SearchResultReceiveEventToJSValue);
3861 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
3862 const BaseEventInfo* info) {
3863 auto webNode = node.Upgrade();
3864 CHECK_NULL_VOID(webNode);
3865 ContainerScope scope(webNode->GetInstanceId());
3866 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3867 auto pipelineContext = PipelineContext::GetCurrentContext();
3868 if (pipelineContext) {
3869 pipelineContext->UpdateCurrentActiveNode(node);
3870 }
3871 auto* eventInfo = TypeInfoHelper::DynamicCast<SearchResultReceiveEvent>(info);
3872 func->Execute(*eventInfo);
3873 };
3874 WebModel::GetInstance()->SetSearchResultReceiveEventId(jsCallback);
3875 }
3876
JsOnDragStart(const JSCallbackInfo& info)3877 void JSWeb::JsOnDragStart(const JSCallbackInfo& info)
3878 {
3879 if (info.Length() < 1 || !info[0]->IsFunction()) {
3880 return;
3881 }
3882
3883 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3884 RefPtr<JsDragFunction> jsOnDragStartFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3885 auto onDragStartId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragStartFunc), node = frameNode](
3886 const RefPtr<DragEvent>& info, const std::string& extraParams) -> NG::DragDropBaseInfo {
3887 NG::DragDropBaseInfo itemInfo;
3888 auto webNode = node.Upgrade();
3889 CHECK_NULL_RETURN(webNode, itemInfo);
3890 ContainerScope scope(webNode->GetInstanceId());
3891 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, itemInfo);
3892 auto pipelineContext = PipelineContext::GetCurrentContext();
3893 if (pipelineContext) {
3894 pipelineContext->UpdateCurrentActiveNode(node);
3895 }
3896 auto ret = func->Execute(info, extraParams);
3897 if (!ret->IsObject()) {
3898 return itemInfo;
3899 }
3900 auto component = ParseDragNode(ret);
3901 if (component) {
3902 itemInfo.node = component;
3903 return itemInfo;
3904 }
3905
3906 auto builderObj = JSRef<JSObject>::Cast(ret);
3907 #if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM)
3908 auto pixmap_impl = builderObj->GetProperty("pixelMap");
3909 itemInfo.pixelMap = CreatePixelMapFromNapiValue(pixmap_impl);
3910 #endif
3911
3912 #if defined(PIXEL_MAP_SUPPORTED)
3913 auto pixmap_ng = builderObj->GetProperty("pixelMap");
3914 itemInfo.pixelMap = CreatePixelMapFromNapiValue(pixmap_ng);
3915 #endif
3916 auto extraInfo = builderObj->GetProperty("extraInfo");
3917 ParseJsString(extraInfo, itemInfo.extraInfo);
3918 component = ParseDragNode(builderObj->GetProperty("builder"));
3919 itemInfo.node = component;
3920 return itemInfo;
3921 };
3922
3923 WebModel::GetInstance()->SetOnDragStart(std::move(onDragStartId));
3924 }
3925
JsOnDragEnter(const JSCallbackInfo& info)3926 void JSWeb::JsOnDragEnter(const JSCallbackInfo& info)
3927 {
3928 if (info.Length() < 1 || !info[0]->IsFunction()) {
3929 return;
3930 }
3931
3932 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3933 RefPtr<JsDragFunction> jsOnDragEnterFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3934 auto onDragEnterId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragEnterFunc), node = frameNode](
3935 const RefPtr<DragEvent>& info, const std::string& extraParams) {
3936 auto webNode = node.Upgrade();
3937 CHECK_NULL_VOID(webNode);
3938 ContainerScope scope(webNode->GetInstanceId());
3939 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3940 ACE_SCORING_EVENT("onDragEnter");
3941 auto pipelineContext = PipelineContext::GetCurrentContext();
3942 if (pipelineContext) {
3943 pipelineContext->UpdateCurrentActiveNode(node);
3944 }
3945 func->Execute(info, extraParams);
3946 };
3947
3948 WebModel::GetInstance()->SetOnDragEnter(onDragEnterId);
3949 }
3950
JsOnDragMove(const JSCallbackInfo& info)3951 void JSWeb::JsOnDragMove(const JSCallbackInfo& info)
3952 {
3953 if (info.Length() < 1 || !info[0]->IsFunction()) {
3954 return;
3955 }
3956
3957 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3958 RefPtr<JsDragFunction> jsOnDragMoveFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3959 auto onDragMoveId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragMoveFunc), node = frameNode](
3960 const RefPtr<DragEvent>& info, const std::string& extraParams) {
3961 auto webNode = node.Upgrade();
3962 CHECK_NULL_VOID(webNode);
3963 ContainerScope scope(webNode->GetInstanceId());
3964 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3965 ACE_SCORING_EVENT("onDragMove");
3966 auto pipelineContext = PipelineContext::GetCurrentContext();
3967 if (pipelineContext) {
3968 pipelineContext->UpdateCurrentActiveNode(node);
3969 }
3970 func->Execute(info, extraParams);
3971 };
3972
3973 WebModel::GetInstance()->SetOnDragMove(onDragMoveId);
3974 }
3975
JsOnDragLeave(const JSCallbackInfo& info)3976 void JSWeb::JsOnDragLeave(const JSCallbackInfo& info)
3977 {
3978 if (info.Length() < 1 || !info[0]->IsFunction()) {
3979 return;
3980 }
3981
3982 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
3983 RefPtr<JsDragFunction> jsOnDragLeaveFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3984 auto onDragLeaveId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragLeaveFunc), node = frameNode](
3985 const RefPtr<DragEvent>& info, const std::string& extraParams) {
3986 auto webNode = node.Upgrade();
3987 CHECK_NULL_VOID(webNode);
3988 ContainerScope scope(webNode->GetInstanceId());
3989 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3990 ACE_SCORING_EVENT("onDragLeave");
3991 auto pipelineContext = PipelineContext::GetCurrentContext();
3992 if (pipelineContext) {
3993 pipelineContext->UpdateCurrentActiveNode(node);
3994 }
3995 func->Execute(info, extraParams);
3996 };
3997
3998 WebModel::GetInstance()->SetOnDragLeave(onDragLeaveId);
3999 }
4000
JsOnDrop(const JSCallbackInfo& info)4001 void JSWeb::JsOnDrop(const JSCallbackInfo& info)
4002 {
4003 if (info.Length() < 1 || !info[0]->IsFunction()) {
4004 return;
4005 }
4006
4007 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4008 RefPtr<JsDragFunction> jsOnDropFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
4009 auto onDropId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDropFunc), node = frameNode](
4010 const RefPtr<DragEvent>& info, const std::string& extraParams) {
4011 auto webNode = node.Upgrade();
4012 CHECK_NULL_VOID(webNode);
4013 ContainerScope scope(webNode->GetInstanceId());
4014 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4015 ACE_SCORING_EVENT("onDrop");
4016 auto pipelineContext = PipelineContext::GetCurrentContext();
4017 if (pipelineContext) {
4018 pipelineContext->UpdateCurrentActiveNode(node);
4019 }
4020 func->Execute(info, extraParams);
4021 };
4022
4023 WebModel::GetInstance()->SetOnDrop(onDropId);
4024 }
4025
PinchSmoothModeEnabled(bool isPinchSmoothModeEnabled)4026 void JSWeb::PinchSmoothModeEnabled(bool isPinchSmoothModeEnabled)
4027 {
4028 WebModel::GetInstance()->SetPinchSmoothModeEnabled(isPinchSmoothModeEnabled);
4029 }
4030
WindowNewEventToJSValue(const WebWindowNewEvent& eventInfo)4031 JSRef<JSVal> WindowNewEventToJSValue(const WebWindowNewEvent& eventInfo)
4032 {
4033 JSRef<JSObject> obj = JSRef<JSObject>::New();
4034 obj->SetProperty("isAlert", eventInfo.IsAlert());
4035 obj->SetProperty("isUserTrigger", eventInfo.IsUserTrigger());
4036 obj->SetProperty("targetUrl", eventInfo.GetTargetUrl());
4037 JSRef<JSObject> handlerObj = JSClass<JSWebWindowNewHandler>::NewInstance();
4038 auto handler = Referenced::Claim(handlerObj->Unwrap<JSWebWindowNewHandler>());
4039 handler->SetEvent(eventInfo);
4040 obj->SetPropertyObject("handler", handlerObj);
4041 return JSRef<JSVal>::Cast(obj);
4042 }
4043
HandleWindowNewEvent(const WebWindowNewEvent* eventInfo)4044 bool HandleWindowNewEvent(const WebWindowNewEvent* eventInfo)
4045 {
4046 if (eventInfo == nullptr) {
4047 return false;
4048 }
4049 auto handler = eventInfo->GetWebWindowNewHandler();
4050 if (handler && !handler->IsFrist()) {
4051 int32_t parentId = -1;
4052 auto controller = JSWebWindowNewHandler::PopController(handler->GetId(), &parentId);
4053 if (!controller.IsEmpty()) {
4054 auto getWebIdFunction = controller->GetProperty("innerGetWebId");
4055 if (getWebIdFunction->IsFunction()) {
4056 auto func = JSRef<JSFunc>::Cast(getWebIdFunction);
4057 auto webId = func->Call(controller, 0, {});
4058 handler->SetWebController(webId->ToNumber<int32_t>());
4059 }
4060 auto completeWindowNewFunction = controller->GetProperty("innerCompleteWindowNew");
4061 if (completeWindowNewFunction->IsFunction()) {
4062 auto func = JSRef<JSFunc>::Cast(completeWindowNewFunction);
4063 JSRef<JSVal> argv[] = { JSRef<JSVal>::Make(ToJSValue(parentId)) };
4064 func->Call(controller, 1, argv);
4065 }
4066 }
4067 return false;
4068 }
4069 return true;
4070 }
4071
OnWindowNew(const JSCallbackInfo& args)4072 void JSWeb::OnWindowNew(const JSCallbackInfo& args)
4073 {
4074 if (args.Length() < 1 || !args[0]->IsFunction()) {
4075 return;
4076 }
4077
4078 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4079 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebWindowNewEvent, 1>>(
4080 JSRef<JSFunc>::Cast(args[0]), WindowNewEventToJSValue);
4081 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4082 const std::shared_ptr<BaseEventInfo>& info) {
4083 ACE_SCORING_EVENT("OnWindowNew CallBack");
4084 auto webNode = node.Upgrade();
4085 CHECK_NULL_VOID(webNode);
4086 ContainerScope scope(webNode->GetInstanceId());
4087 auto pipelineContext = PipelineContext::GetCurrentContext();
4088 if (pipelineContext) {
4089 pipelineContext->UpdateCurrentActiveNode(node);
4090 }
4091 auto* eventInfo = TypeInfoHelper::DynamicCast<WebWindowNewEvent>(info.get());
4092 if (!func || !HandleWindowNewEvent(eventInfo)) {
4093 return;
4094 }
4095 func->Execute(*eventInfo);
4096 };
4097 WebModel::GetInstance()->SetWindowNewEvent(jsCallback);
4098 }
4099
WindowExitEventToJSValue(const WebWindowExitEvent& eventInfo)4100 JSRef<JSVal> WindowExitEventToJSValue(const WebWindowExitEvent& eventInfo)
4101 {
4102 JSRef<JSObject> obj = JSRef<JSObject>::New();
4103 return JSRef<JSVal>::Cast(obj);
4104 }
4105
OnWindowExit(const JSCallbackInfo& args)4106 void JSWeb::OnWindowExit(const JSCallbackInfo& args)
4107 {
4108 if (args.Length() < 1 || !args[0]->IsFunction()) {
4109 return;
4110 }
4111 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4112 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebWindowExitEvent, 1>>(
4113 JSRef<JSFunc>::Cast(args[0]), WindowExitEventToJSValue);
4114 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4115 const BaseEventInfo* info) {
4116 auto webNode = node.Upgrade();
4117 CHECK_NULL_VOID(webNode);
4118 ContainerScope scope(webNode->GetInstanceId());
4119 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4120 auto pipelineContext = PipelineContext::GetCurrentContext();
4121 if (pipelineContext) {
4122 pipelineContext->UpdateCurrentActiveNode(node);
4123 }
4124 auto* eventInfo = TypeInfoHelper::DynamicCast<WebWindowExitEvent>(info);
4125 func->Execute(*eventInfo);
4126 };
4127 WebModel::GetInstance()->SetWindowExitEventId(jsCallback);
4128 }
4129
MultiWindowAccessEnabled(bool isMultiWindowAccessEnable)4130 void JSWeb::MultiWindowAccessEnabled(bool isMultiWindowAccessEnable)
4131 {
4132 WebModel::GetInstance()->SetMultiWindowAccessEnabled(isMultiWindowAccessEnable);
4133 }
4134
AllowWindowOpenMethod(bool isAllowWindowOpenMethod)4135 void JSWeb::AllowWindowOpenMethod(bool isAllowWindowOpenMethod)
4136 {
4137 WebModel::GetInstance()->SetAllowWindowOpenMethod(isAllowWindowOpenMethod);
4138 }
4139
WebCursiveFont(const std::string& cursiveFontFamily)4140 void JSWeb::WebCursiveFont(const std::string& cursiveFontFamily)
4141 {
4142 WebModel::GetInstance()->SetWebCursiveFont(cursiveFontFamily);
4143 }
4144
WebFantasyFont(const std::string& fantasyFontFamily)4145 void JSWeb::WebFantasyFont(const std::string& fantasyFontFamily)
4146 {
4147 WebModel::GetInstance()->SetWebFantasyFont(fantasyFontFamily);
4148 }
4149
WebFixedFont(const std::string& fixedFontFamily)4150 void JSWeb::WebFixedFont(const std::string& fixedFontFamily)
4151 {
4152 WebModel::GetInstance()->SetWebFixedFont(fixedFontFamily);
4153 }
4154
WebSansSerifFont(const std::string& sansSerifFontFamily)4155 void JSWeb::WebSansSerifFont(const std::string& sansSerifFontFamily)
4156 {
4157 WebModel::GetInstance()->SetWebSansSerifFont(sansSerifFontFamily);
4158 }
4159
WebSerifFont(const std::string& serifFontFamily)4160 void JSWeb::WebSerifFont(const std::string& serifFontFamily)
4161 {
4162 WebModel::GetInstance()->SetWebSerifFont(serifFontFamily);
4163 }
4164
WebStandardFont(const std::string& standardFontFamily)4165 void JSWeb::WebStandardFont(const std::string& standardFontFamily)
4166 {
4167 WebModel::GetInstance()->SetWebStandardFont(standardFontFamily);
4168 }
4169
DefaultFixedFontSize(int32_t defaultFixedFontSize)4170 void JSWeb::DefaultFixedFontSize(int32_t defaultFixedFontSize)
4171 {
4172 WebModel::GetInstance()->SetDefaultFixedFontSize(defaultFixedFontSize);
4173 }
4174
DefaultFontSize(int32_t defaultFontSize)4175 void JSWeb::DefaultFontSize(int32_t defaultFontSize)
4176 {
4177 WebModel::GetInstance()->SetDefaultFontSize(defaultFontSize);
4178 }
4179
DefaultTextEncodingFormat(const JSCallbackInfo& args)4180 void JSWeb::DefaultTextEncodingFormat(const JSCallbackInfo& args)
4181 {
4182 if (args.Length() < 1 || !args[0]->IsString()) {
4183 return;
4184 }
4185 std::string textEncodingFormat = args[0]->ToString();
4186 EraseSpace(textEncodingFormat);
4187 if (textEncodingFormat.empty()) {
4188 WebModel::GetInstance()->SetDefaultTextEncodingFormat("UTF-8");
4189 return;
4190 }
4191 WebModel::GetInstance()->SetDefaultTextEncodingFormat(textEncodingFormat);
4192 }
4193
MinFontSize(int32_t minFontSize)4194 void JSWeb::MinFontSize(int32_t minFontSize)
4195 {
4196 WebModel::GetInstance()->SetMinFontSize(minFontSize);
4197 }
4198
MinLogicalFontSize(int32_t minLogicalFontSize)4199 void JSWeb::MinLogicalFontSize(int32_t minLogicalFontSize)
4200 {
4201 WebModel::GetInstance()->SetMinLogicalFontSize(minLogicalFontSize);
4202 }
4203
BlockNetwork(bool isNetworkBlocked)4204 void JSWeb::BlockNetwork(bool isNetworkBlocked)
4205 {
4206 WebModel::GetInstance()->SetBlockNetwork(isNetworkBlocked);
4207 }
4208
PageVisibleEventToJSValue(const PageVisibleEvent& eventInfo)4209 JSRef<JSVal> PageVisibleEventToJSValue(const PageVisibleEvent& eventInfo)
4210 {
4211 JSRef<JSObject> obj = JSRef<JSObject>::New();
4212 obj->SetProperty("url", eventInfo.GetUrl());
4213 return JSRef<JSVal>::Cast(obj);
4214 }
4215
OnPageVisible(const JSCallbackInfo& args)4216 void JSWeb::OnPageVisible(const JSCallbackInfo& args)
4217 {
4218 TAG_LOGI(AceLogTag::ACE_WEB, "JSWeb::OnPageVisible init by developer");
4219 if (!args[0]->IsFunction()) {
4220 return;
4221 }
4222 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4223 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<PageVisibleEvent, 1>>(
4224 JSRef<JSFunc>::Cast(args[0]), PageVisibleEventToJSValue);
4225
4226 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4227 const std::shared_ptr<BaseEventInfo>& info) {
4228 TAG_LOGI(AceLogTag::ACE_WEB, "JSWeb::OnPageVisible uiCallback enter");
4229 auto webNode = node.Upgrade();
4230 CHECK_NULL_VOID(webNode);
4231 ContainerScope scope(webNode->GetInstanceId());
4232 auto context = PipelineBase::GetCurrentContext();
4233 if (context) {
4234 context->UpdateCurrentActiveNode(node);
4235 }
4236 auto executor = Container::CurrentTaskExecutorSafely();
4237 CHECK_NULL_VOID(executor);
4238 executor->PostTask([execCtx, postFunc = func, info]() {
4239 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4240 TAG_LOGI(AceLogTag::ACE_WEB, "JSWeb::OnPageVisible async event execute");
4241 auto* eventInfo = TypeInfoHelper::DynamicCast<PageVisibleEvent>(info.get());
4242 postFunc->Execute(*eventInfo);
4243 }, TaskExecutor::TaskType::UI, "ArkUIWebPageVisible");
4244 };
4245 WebModel::GetInstance()->SetPageVisibleId(std::move(uiCallback));
4246 }
4247
OnInterceptKeyEvent(const JSCallbackInfo& args)4248 void JSWeb::OnInterceptKeyEvent(const JSCallbackInfo& args)
4249 {
4250 if (args.Length() < 1 || !args[0]->IsFunction()) {
4251 return;
4252 }
4253
4254 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4255 RefPtr<JsKeyFunction> jsOnPreKeyEventFunc = AceType::MakeRefPtr<JsKeyFunction>(JSRef<JSFunc>::Cast(args[0]));
4256 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsOnPreKeyEventFunc), node = frameNode](
4257 KeyEventInfo& keyEventInfo) -> bool {
4258 bool result = false;
4259 ACE_SCORING_EVENT("onPreKeyEvent");
4260 auto webNode = node.Upgrade();
4261 CHECK_NULL_RETURN(webNode, false);
4262 ContainerScope scope(webNode->GetInstanceId());
4263 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, result);
4264 auto pipelineContext = PipelineContext::GetCurrentContext();
4265 if (pipelineContext) {
4266 pipelineContext->UpdateCurrentActiveNode(node);
4267 }
4268 JSRef<JSVal> obj = func->ExecuteWithValue(keyEventInfo);
4269 if (obj->IsBoolean()) {
4270 result = obj->ToBoolean();
4271 }
4272 return result;
4273 };
4274 WebModel::GetInstance()->SetOnInterceptKeyEventCallback(uiCallback);
4275 }
4276
DataResubmittedEventToJSValue(const DataResubmittedEvent& eventInfo)4277 JSRef<JSVal> DataResubmittedEventToJSValue(const DataResubmittedEvent& eventInfo)
4278 {
4279 JSRef<JSObject> obj = JSRef<JSObject>::New();
4280 JSRef<JSObject> resultObj = JSClass<JSDataResubmitted>::NewInstance();
4281 auto jsDataResubmitted = Referenced::Claim(resultObj->Unwrap<JSDataResubmitted>());
4282 if (!jsDataResubmitted) {
4283 return JSRef<JSVal>::Cast(obj);
4284 }
4285 jsDataResubmitted->SetHandler(eventInfo.GetHandler());
4286 obj->SetPropertyObject("handler", resultObj);
4287 return JSRef<JSVal>::Cast(obj);
4288 }
4289
OnDataResubmitted(const JSCallbackInfo& args)4290 void JSWeb::OnDataResubmitted(const JSCallbackInfo& args)
4291 {
4292 if (args.Length() < 1 || !args[0]->IsFunction()) {
4293 return;
4294 }
4295 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<DataResubmittedEvent, 1>>(
4296 JSRef<JSFunc>::Cast(args[0]), DataResubmittedEventToJSValue);
4297
4298 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4299 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4300 const std::shared_ptr<BaseEventInfo>& info) {
4301 auto webNode = node.Upgrade();
4302 CHECK_NULL_VOID(webNode);
4303 ContainerScope scope(webNode->GetInstanceId());
4304 auto context = PipelineBase::GetCurrentContext();
4305 if (context) {
4306 context->UpdateCurrentActiveNode(node);
4307 }
4308 auto executor = Container::CurrentTaskExecutorSafely();
4309 CHECK_NULL_VOID(executor);
4310 executor->PostSyncTask([execCtx, postFunc = func, info]() {
4311 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4312 auto* eventInfo = TypeInfoHelper::DynamicCast<DataResubmittedEvent>(info.get());
4313 postFunc->Execute(*eventInfo);
4314 }, TaskExecutor::TaskType::UI, "ArkUIWebDataResubmitted");
4315 };
4316 WebModel::GetInstance()->SetOnDataResubmitted(uiCallback);
4317 }
4318
GetPixelFormat(NWeb::ImageColorType colorType)4319 Media::PixelFormat GetPixelFormat(NWeb::ImageColorType colorType)
4320 {
4321 Media::PixelFormat pixelFormat;
4322 switch (colorType) {
4323 case NWeb::ImageColorType::COLOR_TYPE_UNKNOWN:
4324 pixelFormat = Media::PixelFormat::UNKNOWN;
4325 break;
4326 case NWeb::ImageColorType::COLOR_TYPE_RGBA_8888:
4327 pixelFormat = Media::PixelFormat::RGBA_8888;
4328 break;
4329 case NWeb::ImageColorType::COLOR_TYPE_BGRA_8888:
4330 pixelFormat = Media::PixelFormat::BGRA_8888;
4331 break;
4332 default:
4333 pixelFormat = Media::PixelFormat::UNKNOWN;
4334 break;
4335 }
4336 return pixelFormat;
4337 }
4338
GetAlphaType(NWeb::ImageAlphaType alphaType)4339 Media::AlphaType GetAlphaType(NWeb::ImageAlphaType alphaType)
4340 {
4341 Media::AlphaType imageAlphaType;
4342 switch (alphaType) {
4343 case NWeb::ImageAlphaType::ALPHA_TYPE_UNKNOWN:
4344 imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
4345 break;
4346 case NWeb::ImageAlphaType::ALPHA_TYPE_OPAQUE:
4347 imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
4348 break;
4349 case NWeb::ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED:
4350 imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
4351 break;
4352 case NWeb::ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED:
4353 imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
4354 break;
4355 default:
4356 imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
4357 break;
4358 }
4359 return imageAlphaType;
4360 }
4361
FaviconReceivedEventToJSValue(const FaviconReceivedEvent& eventInfo)4362 JSRef<JSObject> FaviconReceivedEventToJSValue(const FaviconReceivedEvent& eventInfo)
4363 {
4364 JSRef<JSObject> obj = JSRef<JSObject>::New();
4365 auto data = eventInfo.GetHandler()->GetData();
4366 size_t width = eventInfo.GetHandler()->GetWidth();
4367 size_t height = eventInfo.GetHandler()->GetHeight();
4368 int colorType = eventInfo.GetHandler()->GetColorType();
4369 int alphaType = eventInfo.GetHandler()->GetAlphaType();
4370
4371 Media::InitializationOptions opt;
4372 opt.size.width = static_cast<int32_t>(width);
4373 opt.size.height = static_cast<int32_t>(height);
4374 opt.pixelFormat = GetPixelFormat(NWeb::ImageColorType(colorType));
4375 opt.alphaType = GetAlphaType(NWeb::ImageAlphaType(alphaType));
4376 opt.editable = true;
4377 auto pixelMap = Media::PixelMap::Create(opt);
4378 if (pixelMap == nullptr) {
4379 return JSRef<JSVal>::Cast(obj);
4380 }
4381 uint32_t stride = width << 2;
4382 uint64_t bufferSize = stride * height;
4383 pixelMap->WritePixels(static_cast<const uint8_t*>(data), bufferSize);
4384 std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release());
4385 auto engine = EngineHelper::GetCurrentEngine();
4386 if (!engine) {
4387 return JSRef<JSVal>::Cast(obj);
4388 }
4389 NativeEngine* nativeEngine = engine->GetNativeEngine();
4390 napi_env env = reinterpret_cast<napi_env>(nativeEngine);
4391 napi_value napiValue = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs);
4392 auto jsPixelMap = JsConverter::ConvertNapiValueToJsVal(napiValue);
4393 obj->SetPropertyObject("favicon", jsPixelMap);
4394 return JSRef<JSObject>::Cast(obj);
4395 }
4396
OnFaviconReceived(const JSCallbackInfo& args)4397 void JSWeb::OnFaviconReceived(const JSCallbackInfo& args)
4398 {
4399 if (args.Length() < 1 || !args[0]->IsFunction()) {
4400 return;
4401 }
4402 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4403 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FaviconReceivedEvent, 1>>(
4404 JSRef<JSFunc>::Cast(args[0]), FaviconReceivedEventToJSValue);
4405
4406 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4407 const std::shared_ptr<BaseEventInfo>& info) {
4408 auto webNode = node.Upgrade();
4409 CHECK_NULL_VOID(webNode);
4410 ContainerScope scope(webNode->GetInstanceId());
4411 auto context = PipelineBase::GetCurrentContext();
4412 if (context) {
4413 context->UpdateCurrentActiveNode(node);
4414 }
4415 auto executor = Container::CurrentTaskExecutorSafely();
4416 CHECK_NULL_VOID(executor);
4417 executor->PostTask([execCtx, postFunc = func, info]() {
4418 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4419 auto* eventInfo = TypeInfoHelper::DynamicCast<FaviconReceivedEvent>(info.get());
4420 postFunc->Execute(*eventInfo);
4421 }, TaskExecutor::TaskType::UI, "ArkUIWebFaviconReceived");
4422 };
4423 WebModel::GetInstance()->SetFaviconReceivedId(uiCallback);
4424 }
4425
TouchIconUrlEventToJSValue(const TouchIconUrlEvent& eventInfo)4426 JSRef<JSVal> TouchIconUrlEventToJSValue(const TouchIconUrlEvent& eventInfo)
4427 {
4428 JSRef<JSObject> obj = JSRef<JSObject>::New();
4429 obj->SetProperty("url", eventInfo.GetUrl());
4430 obj->SetProperty("precomposed", eventInfo.GetPreComposed());
4431 return JSRef<JSVal>::Cast(obj);
4432 }
4433
OnTouchIconUrlReceived(const JSCallbackInfo& args)4434 void JSWeb::OnTouchIconUrlReceived(const JSCallbackInfo& args)
4435 {
4436 if (!args[0]->IsFunction()) {
4437 return;
4438 }
4439 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4440 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<TouchIconUrlEvent, 1>>(
4441 JSRef<JSFunc>::Cast(args[0]), TouchIconUrlEventToJSValue);
4442
4443 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4444 const std::shared_ptr<BaseEventInfo>& info) {
4445 auto webNode = node.Upgrade();
4446 CHECK_NULL_VOID(webNode);
4447 ContainerScope scope(webNode->GetInstanceId());
4448 auto context = PipelineBase::GetCurrentContext();
4449 if (context) {
4450 context->UpdateCurrentActiveNode(node);
4451 }
4452 auto executor = Container::CurrentTaskExecutorSafely();
4453 CHECK_NULL_VOID(executor);
4454 executor->PostTask([execCtx, postFunc = func, info]() {
4455 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4456 auto* eventInfo = TypeInfoHelper::DynamicCast<TouchIconUrlEvent>(info.get());
4457 postFunc->Execute(*eventInfo);
4458 }, TaskExecutor::TaskType::UI, "ArkUIWebTouchIconUrlReceived");
4459 };
4460 WebModel::GetInstance()->SetTouchIconUrlId(uiCallback);
4461 }
4462
DarkMode(int32_t darkMode)4463 void JSWeb::DarkMode(int32_t darkMode)
4464 {
4465 auto mode = WebDarkMode::Off;
4466 switch (darkMode) {
4467 case 0:
4468 mode = WebDarkMode::Off;
4469 break;
4470 case 1:
4471 mode = WebDarkMode::On;
4472 break;
4473 case 2:
4474 mode = WebDarkMode::Auto;
4475 break;
4476 default:
4477 mode = WebDarkMode::Off;
4478 break;
4479 }
4480 WebModel::GetInstance()->SetDarkMode(mode);
4481 }
4482
ForceDarkAccess(bool access)4483 void JSWeb::ForceDarkAccess(bool access)
4484 {
4485 WebModel::GetInstance()->SetForceDarkAccess(access);
4486 }
4487
HorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled)4488 void JSWeb::HorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled)
4489 {
4490 WebModel::GetInstance()->SetHorizontalScrollBarAccessEnabled(isHorizontalScrollBarAccessEnabled);
4491 }
4492
VerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled)4493 void JSWeb::VerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled)
4494 {
4495 WebModel::GetInstance()->SetVerticalScrollBarAccessEnabled(isVerticalScrollBarAccessEnabled);
4496 }
4497
AudioStateChangedEventToJSValue(const AudioStateChangedEvent& eventInfo)4498 JSRef<JSVal> AudioStateChangedEventToJSValue(const AudioStateChangedEvent& eventInfo)
4499 {
4500 JSRef<JSObject> obj = JSRef<JSObject>::New();
4501 obj->SetProperty("playing", eventInfo.IsPlaying());
4502 return JSRef<JSVal>::Cast(obj);
4503 }
4504
OnAudioStateChanged(const JSCallbackInfo& args)4505 void JSWeb::OnAudioStateChanged(const JSCallbackInfo& args)
4506 {
4507 if (!args[0]->IsFunction()) {
4508 return;
4509 }
4510
4511 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4512 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<AudioStateChangedEvent, 1>>(
4513 JSRef<JSFunc>::Cast(args[0]), AudioStateChangedEventToJSValue);
4514
4515 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4516 const std::shared_ptr<BaseEventInfo>& info) {
4517 auto webNode = node.Upgrade();
4518 CHECK_NULL_VOID(webNode);
4519 ContainerScope scope(webNode->GetInstanceId());
4520 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4521 auto pipelineContext = PipelineContext::GetCurrentContext();
4522 if (pipelineContext) {
4523 pipelineContext->UpdateCurrentActiveNode(node);
4524 }
4525 auto* eventInfo = TypeInfoHelper::DynamicCast<AudioStateChangedEvent>(info.get());
4526 func->Execute(*eventInfo);
4527 };
4528 WebModel::GetInstance()->SetAudioStateChangedId(std::move(uiCallback));
4529 }
4530
MediaOptions(const JSCallbackInfo& args)4531 void JSWeb::MediaOptions(const JSCallbackInfo& args)
4532 {
4533 if (!args[0]->IsObject()) {
4534 return;
4535 }
4536 auto paramObject = JSRef<JSObject>::Cast(args[0]);
4537 auto resumeIntervalObj = paramObject->GetProperty("resumeInterval");
4538 if (resumeIntervalObj->IsNumber()) {
4539 int32_t resumeInterval = resumeIntervalObj->ToNumber<int32_t>();
4540 WebModel::GetInstance()->SetAudioResumeInterval(resumeInterval);
4541 }
4542
4543 auto audioExclusiveObj = paramObject->GetProperty("audioExclusive");
4544 if (audioExclusiveObj->IsBoolean()) {
4545 bool audioExclusive = audioExclusiveObj->ToBoolean();
4546 WebModel::GetInstance()->SetAudioExclusive(audioExclusive);
4547 }
4548 }
4549
FirstContentfulPaintEventToJSValue(const FirstContentfulPaintEvent& eventInfo)4550 JSRef<JSVal> FirstContentfulPaintEventToJSValue(const FirstContentfulPaintEvent& eventInfo)
4551 {
4552 JSRef<JSObject> obj = JSRef<JSObject>::New();
4553 obj->SetProperty<int64_t>("navigationStartTick", eventInfo.GetNavigationStartTick());
4554 obj->SetProperty<int64_t>("firstContentfulPaintMs", eventInfo.GetFirstContentfulPaintMs());
4555 return JSRef<JSVal>::Cast(obj);
4556 }
4557
OnFirstContentfulPaint(const JSCallbackInfo& args)4558 void JSWeb::OnFirstContentfulPaint(const JSCallbackInfo& args)
4559 {
4560 if (!args[0]->IsFunction()) {
4561 return;
4562 }
4563 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4564 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FirstContentfulPaintEvent, 1>>(
4565 JSRef<JSFunc>::Cast(args[0]), FirstContentfulPaintEventToJSValue);
4566
4567 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4568 const std::shared_ptr<BaseEventInfo>& info) {
4569 auto webNode = node.Upgrade();
4570 CHECK_NULL_VOID(webNode);
4571 ContainerScope scope(webNode->GetInstanceId());
4572 auto context = PipelineBase::GetCurrentContext();
4573 if (context) {
4574 context->UpdateCurrentActiveNode(node);
4575 }
4576 auto executor = Container::CurrentTaskExecutorSafely();
4577 CHECK_NULL_VOID(executor);
4578 executor->PostTask([execCtx, postFunc = func, info]() {
4579 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4580 auto* eventInfo = TypeInfoHelper::DynamicCast<FirstContentfulPaintEvent>(info.get());
4581 postFunc->Execute(*eventInfo);
4582 }, TaskExecutor::TaskType::UI, "ArkUIWebFirstContentfulPaint");
4583 };
4584 WebModel::GetInstance()->SetFirstContentfulPaintId(std::move(uiCallback));
4585 }
4586
FirstMeaningfulPaintEventToJSValue(const FirstMeaningfulPaintEvent& eventInfo)4587 JSRef<JSVal> FirstMeaningfulPaintEventToJSValue(const FirstMeaningfulPaintEvent& eventInfo)
4588 {
4589 JSRef<JSObject> obj = JSRef<JSObject>::New();
4590 obj->SetProperty("navigationStartTime", eventInfo.GetNavigationStartTime());
4591 obj->SetProperty("firstMeaningfulPaintTime", eventInfo.GetFirstMeaningfulPaintTime());
4592 return JSRef<JSVal>::Cast(obj);
4593 }
4594
OnFirstMeaningfulPaint(const JSCallbackInfo& args)4595 void JSWeb::OnFirstMeaningfulPaint(const JSCallbackInfo& args)
4596 {
4597 if (args.Length() < 1 || args[0]->IsUndefined() || args[0]->IsNull() || !args[0]->IsFunction()) {
4598 return;
4599 }
4600 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4601 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FirstMeaningfulPaintEvent, 1>>(
4602 JSRef<JSFunc>::Cast(args[0]), FirstMeaningfulPaintEventToJSValue);
4603 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4604 const std::shared_ptr<BaseEventInfo>& info) {
4605 auto webNode = node.Upgrade();
4606 CHECK_NULL_VOID(webNode);
4607 ContainerScope scope(webNode->GetInstanceId());
4608 auto context = PipelineBase::GetCurrentContext();
4609 if (context) {
4610 context->UpdateCurrentActiveNode(node);
4611 }
4612 auto executor = Container::CurrentTaskExecutorSafely();
4613 CHECK_NULL_VOID(executor);
4614 executor->PostTask([execCtx, postFunc = func, info]() {
4615 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4616 auto* eventInfo = TypeInfoHelper::DynamicCast<FirstMeaningfulPaintEvent>(info.get());
4617 postFunc->Execute(*eventInfo);
4618 }, TaskExecutor::TaskType::UI, "ArkUIWebFirstMeaningfulPaint");
4619 };
4620 WebModel::GetInstance()->SetFirstMeaningfulPaintId(std::move(uiCallback));
4621 }
4622
LargestContentfulPaintEventToJSValue(const LargestContentfulPaintEvent& eventInfo)4623 JSRef<JSVal> LargestContentfulPaintEventToJSValue(const LargestContentfulPaintEvent& eventInfo)
4624 {
4625 JSRef<JSObject> obj = JSRef<JSObject>::New();
4626 obj->SetProperty("navigationStartTime", eventInfo.GetNavigationStartTime());
4627 obj->SetProperty("largestImagePaintTime", eventInfo.GetLargestImagePaintTime());
4628 obj->SetProperty("largestTextPaintTime", eventInfo.GetLargestTextPaintTime());
4629 obj->SetProperty("largestImageLoadStartTime", eventInfo.GetLargestImageLoadStartTime());
4630 obj->SetProperty("largestImageLoadEndTime", eventInfo.GetLargestImageLoadEndTime());
4631 obj->SetProperty("imageBPP", eventInfo.GetImageBPP());
4632 return JSRef<JSVal>::Cast(obj);
4633 }
4634
OnLargestContentfulPaint(const JSCallbackInfo& args)4635 void JSWeb::OnLargestContentfulPaint(const JSCallbackInfo& args)
4636 {
4637 if (args.Length() < 1 || args[0]->IsUndefined() || args[0]->IsNull() || !args[0]->IsFunction()) {
4638 return;
4639 }
4640 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4641 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LargestContentfulPaintEvent, 1>>(
4642 JSRef<JSFunc>::Cast(args[0]), LargestContentfulPaintEventToJSValue);
4643 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4644 const std::shared_ptr<BaseEventInfo>& info) {
4645 auto webNode = node.Upgrade();
4646 CHECK_NULL_VOID(webNode);
4647 ContainerScope scope(webNode->GetInstanceId());
4648 auto context = PipelineBase::GetCurrentContext();
4649 if (context) {
4650 context->UpdateCurrentActiveNode(node);
4651 }
4652 auto executor = Container::CurrentTaskExecutorSafely();
4653 CHECK_NULL_VOID(executor);
4654 executor->PostTask([execCtx, postFunc = func, info]() {
4655 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4656 auto* eventInfo = TypeInfoHelper::DynamicCast<LargestContentfulPaintEvent>(info.get());
4657 postFunc->Execute(*eventInfo);
4658 }, TaskExecutor::TaskType::UI, "ArkUIWebLargestContentfulPaint");
4659 };
4660 WebModel::GetInstance()->SetLargestContentfulPaintId(std::move(uiCallback));
4661 }
4662
SafeBrowsingCheckResultEventToJSValue(const SafeBrowsingCheckResultEvent& eventInfo)4663 JSRef<JSVal> SafeBrowsingCheckResultEventToJSValue(const SafeBrowsingCheckResultEvent& eventInfo)
4664 {
4665 JSRef<JSObject> obj = JSRef<JSObject>::New();
4666 obj->SetProperty("threatType", eventInfo.GetThreatType());
4667 return JSRef<JSVal>::Cast(obj);
4668 }
4669
OnSafeBrowsingCheckResult(const JSCallbackInfo& args)4670 void JSWeb::OnSafeBrowsingCheckResult(const JSCallbackInfo& args)
4671 {
4672 if (args.Length() < 1 || !args[0]->IsFunction()) {
4673 return;
4674 }
4675 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4676 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<SafeBrowsingCheckResultEvent, 1>>(
4677 JSRef<JSFunc>::Cast(args[0]), SafeBrowsingCheckResultEventToJSValue);
4678
4679 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4680 const std::shared_ptr<BaseEventInfo>& info) {
4681 auto webNode = node.Upgrade();
4682 CHECK_NULL_VOID(webNode);
4683 ContainerScope scope(webNode->GetInstanceId());
4684 auto context = PipelineBase::GetCurrentContext();
4685 if (context) {
4686 context->UpdateCurrentActiveNode(node);
4687 }
4688 auto executor = Container::CurrentTaskExecutorSafely();
4689 CHECK_NULL_VOID(executor);
4690 executor->PostTask([execCtx, postFunc = func, info]() {
4691 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4692 auto* eventInfo = TypeInfoHelper::DynamicCast<SafeBrowsingCheckResultEvent>(info.get());
4693 postFunc->Execute(*eventInfo);
4694 }, TaskExecutor::TaskType::UI, "ArkUIWebSafeBrowsingCheckResult");
4695 };
4696 WebModel::GetInstance()->SetSafeBrowsingCheckResultId(std::move(uiCallback));
4697 }
4698
NavigationEntryCommittedEventToJSValue(const NavigationEntryCommittedEvent& eventInfo)4699 JSRef<JSVal> NavigationEntryCommittedEventToJSValue(const NavigationEntryCommittedEvent& eventInfo)
4700 {
4701 JSRef<JSObject> obj = JSRef<JSObject>::New();
4702 obj->SetProperty("isMainFrame", eventInfo.IsMainFrame());
4703 obj->SetProperty("isSameDocument", eventInfo.IsSameDocument());
4704 obj->SetProperty("didReplaceEntry", eventInfo.DidReplaceEntry());
4705 obj->SetProperty("navigationType", static_cast<int>(eventInfo.GetNavigationType()));
4706 obj->SetProperty("url", eventInfo.GetUrl());
4707 return JSRef<JSVal>::Cast(obj);
4708 }
4709
OnNavigationEntryCommitted(const JSCallbackInfo& args)4710 void JSWeb::OnNavigationEntryCommitted(const JSCallbackInfo& args)
4711 {
4712 if (!args[0]->IsFunction()) {
4713 return;
4714 }
4715 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4716 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<NavigationEntryCommittedEvent, 1>>(
4717 JSRef<JSFunc>::Cast(args[0]), NavigationEntryCommittedEventToJSValue);
4718
4719 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4720 const std::shared_ptr<BaseEventInfo>& info) {
4721 auto webNode = node.Upgrade();
4722 CHECK_NULL_VOID(webNode);
4723 ContainerScope scope(webNode->GetInstanceId());
4724 auto context = PipelineBase::GetCurrentContext();
4725 if (context) {
4726 context->UpdateCurrentActiveNode(node);
4727 }
4728 auto executor = Container::CurrentTaskExecutorSafely();
4729 CHECK_NULL_VOID(executor);
4730 executor->PostTask([execCtx, postFunc = func, info]() {
4731 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4732 auto* eventInfo = TypeInfoHelper::DynamicCast<NavigationEntryCommittedEvent>(info.get());
4733 postFunc->Execute(*eventInfo);
4734 }, TaskExecutor::TaskType::UI, "ArkUIWebNavigationEntryCommitted");
4735 };
4736 WebModel::GetInstance()->SetNavigationEntryCommittedId(std::move(uiCallback));
4737 }
4738
IntelligentTrackingPreventionResultEventToJSValue( const IntelligentTrackingPreventionResultEvent& eventInfo)4739 JSRef<JSVal> IntelligentTrackingPreventionResultEventToJSValue(
4740 const IntelligentTrackingPreventionResultEvent& eventInfo)
4741 {
4742 JSRef<JSObject> obj = JSRef<JSObject>::New();
4743 obj->SetProperty("host", eventInfo.GetHost());
4744 obj->SetProperty("trackerHost", eventInfo.GetTrackerHost());
4745 return JSRef<JSVal>::Cast(obj);
4746 }
4747
OnIntelligentTrackingPreventionResult(const JSCallbackInfo& args)4748 void JSWeb::OnIntelligentTrackingPreventionResult(const JSCallbackInfo& args)
4749 {
4750 if (args.Length() < 1 || !args[0]->IsFunction()) {
4751 return;
4752 }
4753 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4754 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<IntelligentTrackingPreventionResultEvent, 1>>(
4755 JSRef<JSFunc>::Cast(args[0]), IntelligentTrackingPreventionResultEventToJSValue);
4756
4757 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4758 const std::shared_ptr<BaseEventInfo>& info) {
4759 auto webNode = node.Upgrade();
4760 CHECK_NULL_VOID(webNode);
4761 ContainerScope scope(webNode->GetInstanceId());
4762 auto context = PipelineBase::GetCurrentContext();
4763 if (context) {
4764 context->UpdateCurrentActiveNode(node);
4765 }
4766 auto executor = Container::CurrentTaskExecutorSafely();
4767 CHECK_NULL_VOID(executor);
4768 executor->PostTask([execCtx, postFunc = func, info]() {
4769 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4770 auto* eventInfo = TypeInfoHelper::DynamicCast<IntelligentTrackingPreventionResultEvent>(info.get());
4771 postFunc->Execute(*eventInfo);
4772 }, TaskExecutor::TaskType::UI, "ArkUIWebIntelligentTrackingPreventionResult");
4773 };
4774 WebModel::GetInstance()->SetIntelligentTrackingPreventionResultId(std::move(uiCallback));
4775 }
4776
OnControllerAttached(const JSCallbackInfo& args)4777 void JSWeb::OnControllerAttached(const JSCallbackInfo& args)
4778 {
4779 if (!args[0]->IsFunction()) {
4780 return;
4781 }
4782 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4783 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(args[0]));
4784 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode]() {
4785 auto webNode = node.Upgrade();
4786 CHECK_NULL_VOID(webNode);
4787 ContainerScope scope(webNode->GetInstanceId());
4788 auto context = PipelineBase::GetCurrentContext();
4789 if (context) {
4790 context->UpdateCurrentActiveNode(node);
4791 }
4792 auto executor = Container::CurrentTaskExecutorSafely();
4793 CHECK_NULL_VOID(executor);
4794 executor->PostSyncTask([execCtx, postFunc = func]() {
4795 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4796 postFunc->Execute();
4797 }, TaskExecutor::TaskType::UI, "ArkUIWebControllerAttached");
4798 };
4799 WebModel::GetInstance()->SetOnControllerAttached(std::move(uiCallback));
4800 }
4801
EmbedLifecycleChangeToJSValue(const NativeEmbedDataInfo& eventInfo)4802 JSRef<JSVal> EmbedLifecycleChangeToJSValue(const NativeEmbedDataInfo& eventInfo)
4803 {
4804 JSRef<JSObject> obj = JSRef<JSObject>::New();
4805 obj->SetProperty("status", static_cast<int32_t>(eventInfo.GetStatus()));
4806 obj->SetProperty("surfaceId", eventInfo.GetSurfaceId());
4807 obj->SetProperty("embedId", eventInfo.GetEmbedId());
4808
4809 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
4810 JSRef<JSObject> requestObj = objectTemplate->NewInstance();
4811 requestObj->SetProperty("id", eventInfo.GetEmebdInfo().id);
4812 requestObj->SetProperty("type", eventInfo.GetEmebdInfo().type);
4813 requestObj->SetProperty("src", eventInfo.GetEmebdInfo().src);
4814 requestObj->SetProperty("tag", eventInfo.GetEmebdInfo().tag);
4815 requestObj->SetProperty("width", eventInfo.GetEmebdInfo().width);
4816 requestObj->SetProperty("height", eventInfo.GetEmebdInfo().height);
4817 requestObj->SetProperty("url", eventInfo.GetEmebdInfo().url);
4818
4819 JSRef<JSObject> positionObj = objectTemplate->NewInstance();
4820 positionObj->SetProperty("x", eventInfo.GetEmebdInfo().x);
4821 positionObj->SetProperty("y", eventInfo.GetEmebdInfo().y);
4822 requestObj->SetPropertyObject("position", positionObj);
4823
4824 auto params = eventInfo.GetEmebdInfo().params;
4825 JSRef<JSObject> paramsObj = objectTemplate->NewInstance();
4826 for (const auto& item : params) {
4827 paramsObj->SetProperty(item.first.c_str(), item.second.c_str());
4828 }
4829 requestObj->SetPropertyObject("params", paramsObj);
4830
4831 obj->SetPropertyObject("info", requestObj);
4832
4833 return JSRef<JSVal>::Cast(obj);
4834 }
4835
EmbedVisibilityChangeToJSValue(const NativeEmbedVisibilityInfo& visibilityInfo)4836 JSRef<JSVal> EmbedVisibilityChangeToJSValue(const NativeEmbedVisibilityInfo& visibilityInfo)
4837 {
4838 JSRef<JSObject> obj = JSRef<JSObject>::New();
4839 obj->SetProperty("visibility", visibilityInfo.GetVisibility());
4840 obj->SetProperty("embedId", visibilityInfo.GetEmbedId());
4841
4842 return JSRef<JSVal>::Cast(obj);
4843 }
4844
OnNativeEmbedLifecycleChange(const JSCallbackInfo& args)4845 void JSWeb::OnNativeEmbedLifecycleChange(const JSCallbackInfo& args)
4846 {
4847 if (args.Length() < 1 || !args[0]->IsFunction()) {
4848 return;
4849 }
4850 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<NativeEmbedDataInfo, 1>>(
4851 JSRef<JSFunc>::Cast(args[0]), EmbedLifecycleChangeToJSValue);
4852 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4853 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4854 const BaseEventInfo* info) {
4855 int32_t instanceId = Container::CurrentIdSafely();
4856 auto webNode = node.Upgrade();
4857 if (webNode) {
4858 instanceId = webNode->GetInstanceId();
4859 }
4860 ContainerScope scope(instanceId);
4861 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4862 auto* eventInfo = TypeInfoHelper::DynamicCast<NativeEmbedDataInfo>(info);
4863 func->Execute(*eventInfo);
4864 };
4865 WebModel::GetInstance()->SetNativeEmbedLifecycleChangeId(jsCallback);
4866 }
4867
OnNativeEmbedVisibilityChange(const JSCallbackInfo& args)4868 void JSWeb::OnNativeEmbedVisibilityChange(const JSCallbackInfo& args)
4869 {
4870 if (args.Length() < 1 || !args[0]->IsFunction()) {
4871 return;
4872 }
4873 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<NativeEmbedVisibilityInfo, 1>>(
4874 JSRef<JSFunc>::Cast(args[0]), EmbedVisibilityChangeToJSValue);
4875 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4876 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4877 const BaseEventInfo* info) {
4878 auto webNode = node.Upgrade();
4879 CHECK_NULL_VOID(webNode);
4880 ContainerScope scope(webNode->GetInstanceId());
4881 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4882 auto* eventInfo = TypeInfoHelper::DynamicCast<NativeEmbedVisibilityInfo>(info);
4883 func->Execute(*eventInfo);
4884 };
4885 WebModel::GetInstance()->SetNativeEmbedVisibilityChangeId(jsCallback);
4886 }
4887
CreateTouchInfo(const TouchLocationInfo& touchInfo, TouchEventInfo& info)4888 JSRef<JSObject> CreateTouchInfo(const TouchLocationInfo& touchInfo, TouchEventInfo& info)
4889 {
4890 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
4891 objectTemplate->SetInternalFieldCount(1);
4892 JSRef<JSObject> touchInfoObj = objectTemplate->NewInstance();
4893 const OHOS::Ace::Offset& globalLocation = touchInfo.GetGlobalLocation();
4894 const OHOS::Ace::Offset& localLocation = touchInfo.GetLocalLocation();
4895 const OHOS::Ace::Offset& screenLocation = touchInfo.GetScreenLocation();
4896 touchInfoObj->SetProperty<int32_t>("type", static_cast<int32_t>(touchInfo.GetTouchType()));
4897 touchInfoObj->SetProperty<int32_t>("id", touchInfo.GetFingerId());
4898 touchInfoObj->SetProperty<double>("displayX", screenLocation.GetX());
4899 touchInfoObj->SetProperty<double>("displayY", screenLocation.GetY());
4900 touchInfoObj->SetProperty<double>("windowX", globalLocation.GetX());
4901 touchInfoObj->SetProperty<double>("windowY", globalLocation.GetY());
4902 touchInfoObj->SetProperty<double>("screenX", globalLocation.GetX());
4903 touchInfoObj->SetProperty<double>("screenY", globalLocation.GetY());
4904 touchInfoObj->SetProperty<double>("x", localLocation.GetX());
4905 touchInfoObj->SetProperty<double>("y", localLocation.GetY());
4906 touchInfoObj->Wrap<TouchEventInfo>(&info);
4907 return touchInfoObj;
4908 }
4909
NativeEmbeadTouchToJSValue(const NativeEmbeadTouchInfo& eventInfo)4910 JSRef<JSVal> NativeEmbeadTouchToJSValue(const NativeEmbeadTouchInfo& eventInfo)
4911 {
4912 JSRef<JSObject> obj = JSRef<JSObject>::New();
4913 obj->SetProperty("embedId", eventInfo.GetEmbedId());
4914 auto info = eventInfo.GetTouchEventInfo();
4915 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
4916 JSRef<JSObject> eventObj = objectTemplate->NewInstance();
4917 JSRef<JSArray> touchArr = JSRef<JSArray>::New();
4918 JSRef<JSArray> changeTouchArr = JSRef<JSArray>::New();
4919 eventObj->SetProperty("source", static_cast<int32_t>(info.GetSourceDevice()));
4920 eventObj->SetProperty("timestamp", static_cast<double>(GetSysTimestamp()));
4921 auto target = CreateEventTargetObject(info);
4922 eventObj->SetPropertyObject("target", target);
4923 eventObj->SetProperty("pressure", info.GetForce());
4924 eventObj->SetProperty("sourceTool", static_cast<int32_t>(info.GetSourceTool()));
4925 eventObj->SetProperty("targetDisplayId", static_cast<int32_t>(info.GetTargetDisplayId()));
4926 eventObj->SetProperty("deviceId", static_cast<int64_t>(info.GetDeviceId()));
4927
4928 if (info.GetChangedTouches().empty()) {
4929 return JSRef<JSVal>::Cast(obj);
4930 }
4931 uint32_t index = 0;
4932 TouchLocationInfo changeTouch = info.GetChangedTouches().back();
4933 JSRef<JSObject> changeTouchElement = CreateTouchInfo(changeTouch, info);
4934 changeTouchArr->SetValueAt(index, changeTouchElement);
4935 if (info.GetChangedTouches().size() > 0) {
4936 eventObj->SetProperty("type", static_cast<int32_t>(changeTouch.GetTouchType()));
4937 }
4938
4939 const std::list<TouchLocationInfo>& touchList = info.GetTouches();
4940 for (const TouchLocationInfo& location : touchList) {
4941 if (location.GetFingerId() == changeTouch.GetFingerId()) {
4942 JSRef<JSObject> touchElement = CreateTouchInfo(changeTouch, info);
4943 touchArr->SetValueAt(index++, touchElement);
4944 } else {
4945 JSRef<JSObject> touchElement = CreateTouchInfo(location, info);
4946 touchArr->SetValueAt(index++, touchElement);
4947 }
4948 }
4949 eventObj->SetPropertyObject("touches", touchArr);
4950 eventObj->SetPropertyObject("changedTouches", changeTouchArr);
4951 obj->SetPropertyObject("touchEvent", eventObj);
4952 JSRef<JSObject> requestObj = JSClass<JSNativeEmbedGestureRequest>::NewInstance();
4953 auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSNativeEmbedGestureRequest>());
4954 requestEvent->SetResult(eventInfo.GetResult());
4955 obj->SetPropertyObject("result", requestObj);
4956 return JSRef<JSVal>::Cast(obj);
4957 }
4958
OnNativeEmbedGestureEvent(const JSCallbackInfo& args)4959 void JSWeb::OnNativeEmbedGestureEvent(const JSCallbackInfo& args)
4960 {
4961 if (args.Length() < 1 || !args[0]->IsFunction()) {
4962 return;
4963 }
4964 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<NativeEmbeadTouchInfo, 1>>(
4965 JSRef<JSFunc>::Cast(args[0]), NativeEmbeadTouchToJSValue);
4966 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4967 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4968 const BaseEventInfo* info) {
4969 auto webNode = node.Upgrade();
4970 CHECK_NULL_VOID(webNode);
4971 ContainerScope scope(webNode->GetInstanceId());
4972 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4973 auto* eventInfo = TypeInfoHelper::DynamicCast<NativeEmbeadTouchInfo>(info);
4974 func->Execute(*eventInfo);
4975 };
4976 WebModel::GetInstance()->SetNativeEmbedGestureEventId(jsCallback);
4977 }
4978
4979
OverScrollEventToJSValue(const WebOnOverScrollEvent& eventInfo)4980 JSRef<JSVal> OverScrollEventToJSValue(const WebOnOverScrollEvent& eventInfo)
4981 {
4982 JSRef<JSObject> obj = JSRef<JSObject>::New();
4983 obj->SetProperty("xOffset", eventInfo.GetX());
4984 obj->SetProperty("yOffset", eventInfo.GetY());
4985 return JSRef<JSVal>::Cast(obj);
4986 }
4987
OnOverScroll(const JSCallbackInfo& args)4988 void JSWeb::OnOverScroll(const JSCallbackInfo& args)
4989 {
4990 if (args.Length() < 1 || !args[0]->IsFunction()) {
4991 return;
4992 }
4993 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
4994 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebOnOverScrollEvent, 1>>(
4995 JSRef<JSFunc>::Cast(args[0]), OverScrollEventToJSValue);
4996 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
4997 const BaseEventInfo* info) {
4998 auto webNode = node.Upgrade();
4999 CHECK_NULL_VOID(webNode);
5000 ContainerScope scope(webNode->GetInstanceId());
5001 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5002 auto pipelineContext = PipelineContext::GetCurrentContext();
5003 if (pipelineContext) {
5004 pipelineContext->UpdateCurrentActiveNode(node);
5005 }
5006 auto* eventInfo = TypeInfoHelper::DynamicCast<WebOnOverScrollEvent>(info);
5007 func->Execute(*eventInfo);
5008 };
5009 WebModel::GetInstance()->SetOverScrollId(jsCallback);
5010 }
5011
SetLayoutMode(int32_t layoutMode)5012 void JSWeb::SetLayoutMode(int32_t layoutMode)
5013 {
5014 auto mode = WebLayoutMode::NONE;
5015 switch (layoutMode) {
5016 case 0:
5017 mode = WebLayoutMode::NONE;
5018 break;
5019 case 1:
5020 mode = WebLayoutMode::FIT_CONTENT;
5021 break;
5022 default:
5023 mode = WebLayoutMode::NONE;
5024 break;
5025 }
5026 WebModel::GetInstance()->SetLayoutMode(mode);
5027 }
5028
SetNestedScroll(const JSCallbackInfo& args)5029 void JSWeb::SetNestedScroll(const JSCallbackInfo& args)
5030 {
5031 NestedScrollOptionsExt nestedOpt = {
5032 .scrollUp = NestedScrollMode::SELF_FIRST,
5033 .scrollDown = NestedScrollMode::SELF_FIRST,
5034 .scrollLeft = NestedScrollMode::SELF_FIRST,
5035 .scrollRight = NestedScrollMode::SELF_FIRST,
5036 };
5037 if (args.Length() < 1 || !args[0]->IsObject()) {
5038 WebModel::GetInstance()->SetNestedScrollExt(nestedOpt);
5039 return;
5040 }
5041 JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
5042 int32_t froward = -1;
5043 JSViewAbstract::ParseJsInt32(obj->GetProperty("scrollForward"), froward);
5044 if (CheckNestedScrollMode(froward)) {
5045 nestedOpt.scrollDown = static_cast<NestedScrollMode>(froward);
5046 nestedOpt.scrollRight = static_cast<NestedScrollMode>(froward);
5047 }
5048 int32_t backward = -1;
5049 JSViewAbstract::ParseJsInt32(obj->GetProperty("scrollBackward"), backward);
5050 if (CheckNestedScrollMode(backward)) {
5051 nestedOpt.scrollUp = static_cast<NestedScrollMode>(backward);
5052 nestedOpt.scrollLeft = static_cast<NestedScrollMode>(backward);
5053 }
5054 int32_t scrollUp = -1;
5055 JSViewAbstract::ParseJsInt32(obj->GetProperty("scrollUp"), scrollUp);
5056 if (CheckNestedScrollMode(scrollUp)) {
5057 nestedOpt.scrollUp = static_cast<NestedScrollMode>(scrollUp);
5058 }
5059 int32_t scrollDown = -1;
5060 JSViewAbstract::ParseJsInt32(obj->GetProperty("scrollDown"), scrollDown);
5061 if (CheckNestedScrollMode(scrollDown)) {
5062 nestedOpt.scrollDown = static_cast<NestedScrollMode>(scrollDown);
5063 }
5064 int32_t scrollLeft = -1;
5065 JSViewAbstract::ParseJsInt32(obj->GetProperty("scrollLeft"), scrollLeft);
5066 if (CheckNestedScrollMode(scrollLeft)) {
5067 nestedOpt.scrollLeft = static_cast<NestedScrollMode>(scrollLeft);
5068 }
5069 int32_t scrollRight = -1;
5070 JSViewAbstract::ParseJsInt32(obj->GetProperty("scrollRight"), scrollRight);
5071 if (CheckNestedScrollMode(scrollRight)) {
5072 nestedOpt.scrollRight = static_cast<NestedScrollMode>(scrollRight);
5073 }
5074 WebModel::GetInstance()->SetNestedScrollExt(nestedOpt);
5075 args.ReturnSelf();
5076 }
5077
CheckNestedScrollMode(const int32_t& modeValue)5078 bool JSWeb::CheckNestedScrollMode(const int32_t& modeValue)
5079 {
5080 return modeValue >= static_cast<int32_t>(NestedScrollMode::SELF_ONLY) &&
5081 modeValue <= static_cast<int32_t>(NestedScrollMode::PARALLEL);
5082 }
5083
SetMetaViewport(const JSCallbackInfo& args)5084 void JSWeb::SetMetaViewport(const JSCallbackInfo& args)
5085 {
5086 if (args.Length() < 1 || !args[0]->IsBoolean()) {
5087 return;
5088 }
5089 bool enabled = args[0]->ToBoolean();
5090 WebModel::GetInstance()->SetMetaViewport(enabled);
5091 }
5092
ParseScriptItems(const JSCallbackInfo& args, ScriptItems& scriptItems)5093 void JSWeb::ParseScriptItems(const JSCallbackInfo& args, ScriptItems& scriptItems)
5094 {
5095 if (args.Length() != 1 || args[0]->IsUndefined() || args[0]->IsNull() || !args[0]->IsArray()) {
5096 return;
5097 }
5098 auto paramArray = JSRef<JSArray>::Cast(args[0]);
5099 size_t length = paramArray->Length();
5100 if (length == 0) {
5101 return;
5102 }
5103 std::string script;
5104 std::vector<std::string> scriptRules;
5105 for (size_t i = 0; i < length; i++) {
5106 auto item = paramArray->GetValueAt(i);
5107 if (!item->IsObject()) {
5108 return;
5109 }
5110 auto itemObject = JSRef<JSObject>::Cast(item);
5111 JSRef<JSVal> jsScript = itemObject->GetProperty("script");
5112 JSRef<JSVal> jsScriptRules = itemObject->GetProperty("scriptRules");
5113 if (!jsScriptRules->IsArray() || JSRef<JSArray>::Cast(jsScriptRules)->Length() == 0) {
5114 return;
5115 }
5116 if (!JSViewAbstract::ParseJsString(jsScript, script)) {
5117 return;
5118 }
5119 scriptRules.clear();
5120 if (!JSViewAbstract::ParseJsStrArray(jsScriptRules, scriptRules)) {
5121 return;
5122 }
5123 if (scriptItems.find(script) == scriptItems.end()) {
5124 scriptItems.insert(std::make_pair(script, scriptRules));
5125 }
5126 }
5127 }
5128
JavaScriptOnDocumentStart(const JSCallbackInfo& args)5129 void JSWeb::JavaScriptOnDocumentStart(const JSCallbackInfo& args)
5130 {
5131 ScriptItems scriptItems;
5132 ParseScriptItems(args, scriptItems);
5133 WebModel::GetInstance()->JavaScriptOnDocumentStart(scriptItems);
5134 }
5135
JavaScriptOnDocumentEnd(const JSCallbackInfo& args)5136 void JSWeb::JavaScriptOnDocumentEnd(const JSCallbackInfo& args)
5137 {
5138 ScriptItems scriptItems;
5139 ParseScriptItems(args, scriptItems);
5140 WebModel::GetInstance()->JavaScriptOnDocumentEnd(scriptItems);
5141 }
5142
OnOverrideUrlLoading(const JSCallbackInfo& args)5143 void JSWeb::OnOverrideUrlLoading(const JSCallbackInfo& args)
5144 {
5145 if (args.Length() < 1 || !args[0]->IsFunction()) {
5146 return;
5147 }
5148 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadOverrideEvent, 1>>(
5149 JSRef<JSFunc>::Cast(args[0]), LoadOverrideEventToJSValue);
5150
5151 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5152 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
5153 const BaseEventInfo* info) -> bool {
5154 auto webNode = node.Upgrade();
5155 CHECK_NULL_RETURN(webNode, false);
5156 ContainerScope scope(webNode->GetInstanceId());
5157 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
5158 auto pipelineContext = PipelineContext::GetCurrentContext();
5159 if (pipelineContext) {
5160 pipelineContext->UpdateCurrentActiveNode(node);
5161 }
5162 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadOverrideEvent>(info);
5163 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
5164 if (message->IsBoolean()) {
5165 return message->ToBoolean();
5166 }
5167 return false;
5168 };
5169 WebModel::GetInstance()->SetOnOverrideUrlLoading(std::move(uiCallback));
5170 }
5171
CopyOption(int32_t copyOption)5172 void JSWeb::CopyOption(int32_t copyOption)
5173 {
5174 auto mode = CopyOptions::Local;
5175 switch (copyOption) {
5176 case static_cast<int32_t>(CopyOptions::None):
5177 mode = CopyOptions::None;
5178 break;
5179 case static_cast<int32_t>(CopyOptions::InApp):
5180 mode = CopyOptions::InApp;
5181 break;
5182 case static_cast<int32_t>(CopyOptions::Local):
5183 mode = CopyOptions::Local;
5184 break;
5185 case static_cast<int32_t>(CopyOptions::Distributed):
5186 mode = CopyOptions::Distributed;
5187 break;
5188 default:
5189 mode = CopyOptions::Local;
5190 break;
5191 }
5192 WebModel::GetInstance()->SetCopyOptionMode(mode);
5193 }
5194
TextAutosizing(const JSCallbackInfo& args)5195 void JSWeb::TextAutosizing(const JSCallbackInfo& args)
5196 {
5197 if (args.Length() < 1 || !args[0]->IsBoolean()) {
5198 return;
5199 }
5200 bool isTextAutosizing = args[0]->ToBoolean();
5201 WebModel::GetInstance()->SetTextAutosizing(isTextAutosizing);
5202 }
5203
EnableNativeVideoPlayer(const JSCallbackInfo& args)5204 void JSWeb::EnableNativeVideoPlayer(const JSCallbackInfo& args)
5205 {
5206 if (args.Length() < 1 || !args[0]->IsObject()) {
5207 return;
5208 }
5209 auto paramObject = JSRef<JSObject>::Cast(args[0]);
5210 std::optional<bool> enable;
5211 std::optional<bool> shouldOverlay;
5212 JSRef<JSVal> enableJsValue = paramObject->GetProperty("enable");
5213 if (enableJsValue->IsBoolean()) {
5214 enable = enableJsValue->ToBoolean();
5215 }
5216 JSRef<JSVal> shouldOverlayJsValue = paramObject->GetProperty("shouldOverlay");
5217 if (shouldOverlayJsValue->IsBoolean()) {
5218 shouldOverlay = shouldOverlayJsValue->ToBoolean();
5219 }
5220 if (!enable || !shouldOverlay) {
5221 // invalid NativeVideoPlayerConfig
5222 return;
5223 }
5224 WebModel::GetInstance()->SetNativeVideoPlayerConfig(*enable, *shouldOverlay);
5225 }
5226
RenderProcessNotRespondingToJSValue(const RenderProcessNotRespondingEvent& eventInfo)5227 JSRef<JSVal> RenderProcessNotRespondingToJSValue(const RenderProcessNotRespondingEvent& eventInfo)
5228 {
5229 JSRef<JSObject> obj = JSRef<JSObject>::New();
5230 obj->SetProperty("jsStack", eventInfo.GetJsStack());
5231 obj->SetProperty("pid", eventInfo.GetPid());
5232 obj->SetProperty("reason", eventInfo.GetReason());
5233 return JSRef<JSVal>::Cast(obj);
5234 }
5235
OnRenderProcessNotResponding(const JSCallbackInfo& args)5236 void JSWeb::OnRenderProcessNotResponding(const JSCallbackInfo& args)
5237 {
5238 if (args.Length() < 1 || !args[0]->IsFunction()) {
5239 return;
5240 }
5241 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5242 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<RenderProcessNotRespondingEvent, 1>>(
5243 JSRef<JSFunc>::Cast(args[0]), RenderProcessNotRespondingToJSValue);
5244 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
5245 const BaseEventInfo* info) {
5246 auto webNode = node.Upgrade();
5247 CHECK_NULL_VOID(webNode);
5248 ContainerScope scope(webNode->GetInstanceId());
5249 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5250 auto pipelineContext = PipelineContext::GetCurrentContext();
5251 if (pipelineContext) {
5252 pipelineContext->UpdateCurrentActiveNode(node);
5253 }
5254 auto* eventInfo = TypeInfoHelper::DynamicCast<RenderProcessNotRespondingEvent>(info);
5255 func->Execute(*eventInfo);
5256 };
5257 WebModel::GetInstance()->SetRenderProcessNotRespondingId(jsCallback);
5258 }
5259
RenderProcessRespondingEventToJSValue(const RenderProcessRespondingEvent& eventInfo)5260 JSRef<JSVal> RenderProcessRespondingEventToJSValue(const RenderProcessRespondingEvent& eventInfo)
5261 {
5262 JSRef<JSObject> obj = JSRef<JSObject>::New();
5263 return JSRef<JSVal>::Cast(obj);
5264 }
5265
OnRenderProcessResponding(const JSCallbackInfo& args)5266 void JSWeb::OnRenderProcessResponding(const JSCallbackInfo& args)
5267 {
5268 if (args.Length() < 1 || !args[0]->IsFunction()) {
5269 return;
5270 }
5271 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5272 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<RenderProcessRespondingEvent, 1>>(
5273 JSRef<JSFunc>::Cast(args[0]), RenderProcessRespondingEventToJSValue);
5274 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
5275 const BaseEventInfo* info) {
5276 auto webNode = node.Upgrade();
5277 CHECK_NULL_VOID(webNode);
5278 ContainerScope scope(webNode->GetInstanceId());
5279 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5280 auto pipelineContext = PipelineContext::GetCurrentContext();
5281 if (pipelineContext) {
5282 pipelineContext->UpdateCurrentActiveNode(node);
5283 }
5284 auto* eventInfo = TypeInfoHelper::DynamicCast<RenderProcessRespondingEvent>(info);
5285 func->Execute(*eventInfo);
5286 };
5287 WebModel::GetInstance()->SetRenderProcessRespondingId(jsCallback);
5288 }
5289
SelectionMenuOptions(const JSCallbackInfo& args)5290 void JSWeb::SelectionMenuOptions(const JSCallbackInfo& args)
5291 {
5292 if (args.Length() != 1 || args[0]->IsUndefined() || args[0]->IsNull() || !args[0]->IsArray()) {
5293 return;
5294 }
5295 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5296 auto menuItamArray = JSRef<JSArray>::Cast(args[0]);
5297 WebMenuOptionsParam optionParam;
5298 NG::MenuOptionsParam menuOption;
5299 for (size_t i = 0; i < menuItamArray->Length(); i++) {
5300 auto menuItem = menuItamArray->GetValueAt(i);
5301 if (!menuItem->IsObject()) {
5302 return;
5303 }
5304 auto menuItemObject = JSRef<JSObject>::Cast(menuItem);
5305 auto jsContent = menuItemObject->GetProperty("content");
5306 auto jsStartIcon = menuItemObject->GetProperty("startIcon");
5307 std::string content;
5308 if (!ParseJsMedia(jsContent, content)) {
5309 return;
5310 }
5311 menuOption.content = content;
5312 std::string icon;
5313 menuOption.icon.reset();
5314 if (ParseJsMedia(jsStartIcon, icon)) {
5315 menuOption.icon = icon;
5316 }
5317 auto jsAction = menuItemObject->GetProperty("action");
5318 if (jsAction.IsEmpty() || !jsAction->IsFunction()) {
5319 return;
5320 }
5321 auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(jsAction));
5322 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc),
5323 node = frameNode](const std::string selectInfo) {
5324 auto webNode = node.Upgrade();
5325 CHECK_NULL_VOID(webNode);
5326 ContainerScope scope(webNode->GetInstanceId());
5327 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5328 auto pipelineContext = PipelineContext::GetCurrentContext();
5329 if (pipelineContext) {
5330 pipelineContext->UpdateCurrentActiveNode(node);
5331 pipelineContext->SetCallBackNode(node);
5332 }
5333 auto newSelectInfo = JSRef<JSVal>::Make(ToJSValue(selectInfo));
5334 func->ExecuteJS(1, &newSelectInfo);
5335 };
5336 menuOption.action = std::move(jsCallback);
5337 optionParam.menuOption.push_back(menuOption);
5338 }
5339 WebModel::GetInstance()->SetSelectionMenuOptions(std::move(optionParam));
5340 }
5341
ViewportFitChangedToJSValue(const ViewportFitChangedEvent& eventInfo)5342 JSRef<JSVal> ViewportFitChangedToJSValue(const ViewportFitChangedEvent& eventInfo)
5343 {
5344 JSRef<JSObject> obj = JSRef<JSObject>::New();
5345 obj->SetProperty("viewportFit", eventInfo.GetViewportFit());
5346 return JSRef<JSVal>::Cast(obj);
5347 }
5348
OnViewportFitChanged(const JSCallbackInfo& args)5349 void JSWeb::OnViewportFitChanged(const JSCallbackInfo& args)
5350 {
5351 if (args.Length() < 1 || !args[0]->IsFunction()) {
5352 return;
5353 }
5354 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5355 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ViewportFitChangedEvent, 1>>(
5356 JSRef<JSFunc>::Cast(args[0]), ViewportFitChangedToJSValue);
5357 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
5358 const BaseEventInfo* info) {
5359 auto webNode = node.Upgrade();
5360 CHECK_NULL_VOID(webNode);
5361 ContainerScope scope(webNode->GetInstanceId());
5362 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5363 auto pipelineContext = PipelineContext::GetCurrentContext();
5364 if (pipelineContext) {
5365 pipelineContext->UpdateCurrentActiveNode(node);
5366 }
5367 auto* eventInfo = TypeInfoHelper::DynamicCast<ViewportFitChangedEvent>(info);
5368 func->Execute(*eventInfo);
5369 };
5370 WebModel::GetInstance()->SetViewportFitChangedId(jsCallback);
5371 }
5372
InterceptKeyboardEventToJSValue(const InterceptKeyboardEvent& eventInfo)5373 JSRef<JSVal> InterceptKeyboardEventToJSValue(const InterceptKeyboardEvent& eventInfo)
5374 {
5375 JSRef<JSObject> obj = JSRef<JSObject>::New();
5376 JSRef<JSObject> webKeyboardControllerObj = JSClass<JSWebKeyboardController>::NewInstance();
5377 auto webKeyboardController = Referenced::Claim(webKeyboardControllerObj->Unwrap<JSWebKeyboardController>());
5378 webKeyboardController->SeWebKeyboardController(eventInfo.GetCustomKeyboardHandler());
5379 obj->SetPropertyObject("controller", webKeyboardControllerObj);
5380
5381 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
5382 JSRef<JSObject> attributesObj = objectTemplate->NewInstance();
5383 for (const auto& item : eventInfo.GetAttributesMap()) {
5384 attributesObj->SetProperty(item.first.c_str(), item.second.c_str());
5385 }
5386 obj->SetPropertyObject("attributes", attributesObj);
5387 return JSRef<JSVal>::Cast(obj);
5388 }
5389
ParseJsCustomKeyboardOption(const JsiExecutionContext& context, const JSRef<JSVal>& keyboardOpt, WebKeyboardOption& keyboardOption)5390 void JSWeb::ParseJsCustomKeyboardOption(const JsiExecutionContext& context, const JSRef<JSVal>& keyboardOpt,
5391 WebKeyboardOption& keyboardOption)
5392 {
5393 TAG_LOGI(AceLogTag::ACE_WEB, "WebCustomKeyboard ParseJsCustomKeyboardOption enter");
5394 if (!keyboardOpt->IsObject()) {
5395 return;
5396 }
5397
5398 JSRef<JSObject> keyboradOptObj = JSRef<JSObject>::Cast(keyboardOpt);
5399 auto useSystemKeyboardObj = keyboradOptObj->GetProperty("useSystemKeyboard");
5400 if (useSystemKeyboardObj->IsNull() || !useSystemKeyboardObj->IsBoolean()) {
5401 return;
5402 }
5403
5404 bool isSystemKeyboard = useSystemKeyboardObj->ToBoolean();
5405 keyboardOption.isSystemKeyboard_ = isSystemKeyboard;
5406 TAG_LOGI(AceLogTag::ACE_WEB, "WebCustomKeyboard ParseJsCustomKeyboardOption isSystemKeyboard is %{public}d",
5407 isSystemKeyboard);
5408 if (isSystemKeyboard) {
5409 auto enterKeyTypeObj = keyboradOptObj->GetProperty("enterKeyType");
5410 if (enterKeyTypeObj->IsNull() || !enterKeyTypeObj->IsNumber()) {
5411 return;
5412 }
5413 int32_t enterKeyType = enterKeyTypeObj->ToNumber<int32_t>();
5414 keyboardOption.enterKeyTpye_ = enterKeyType;
5415 TAG_LOGI(AceLogTag::ACE_WEB, "WebCustomKeyboard ParseJsCustomKeyboardOption \
5416 isSystemKeyboard is %{public}d, enterKeyType is %{public}d", isSystemKeyboard, enterKeyType);
5417 } else {
5418 auto builder = keyboradOptObj->GetProperty("customKeyboard");
5419 if (builder->IsNull()) {
5420 TAG_LOGE(AceLogTag::ACE_WEB, "WebCustomKeyboard ParseJsCustomKeyboardOption" \
5421 ", parse customKeyboard, builder is null");
5422 return;
5423 }
5424
5425 if (!builder->IsFunction()) {
5426 TAG_LOGE(AceLogTag::ACE_WEB, "WebCustomKeyboard ParseJsCustomKeyboardOption" \
5427 ", parse customKeyboard, builder is invalid");
5428 return;
5429 }
5430
5431 auto builderFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSFunc>::Cast(builder));
5432 CHECK_NULL_VOID(builderFunc);
5433 WeakPtr<NG::FrameNode> targetNode =
5434 AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5435 auto buildFunc = [execCtx = context, func = std::move(builderFunc), node = targetNode]() {
5436 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5437 ACE_SCORING_EVENT("WebCustomKeyboard");
5438 PipelineContext::SetCallBackNode(node);
5439 func->Execute();
5440 };
5441 keyboardOption.customKeyboardBuilder_ = buildFunc;
5442 TAG_LOGI(AceLogTag::ACE_WEB, "WebCustomKeyboard ParseJsCustomKeyboardOption" \
5443 ", isSystemKeyboard is %{public}d, parseCustomBuilder end", isSystemKeyboard);
5444 }
5445 }
5446
OnInterceptKeyboardAttach(const JSCallbackInfo& args)5447 void JSWeb::OnInterceptKeyboardAttach(const JSCallbackInfo& args)
5448 {
5449 TAG_LOGI(AceLogTag::ACE_WEB, "WebCustomKeyboard OnInterceptKeyboardAttach register enter");
5450 if (args.Length() < 1 || !args[0]->IsFunction()) {
5451 return;
5452 }
5453 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<InterceptKeyboardEvent, 1>>(
5454 JSRef<JSFunc>::Cast(args[0]), InterceptKeyboardEventToJSValue);
5455
5456 auto frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5457 auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
5458 const BaseEventInfo* info) -> WebKeyboardOption {
5459 TAG_LOGI(AceLogTag::ACE_WEB, "WebCustomKeyboard OnInterceptKeyboardAttach invoke enter");
5460 WebKeyboardOption opt;
5461 auto webNode = node.Upgrade();
5462 CHECK_NULL_RETURN(webNode, opt);
5463 ContainerScope scope(webNode->GetInstanceId());
5464 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, opt);
5465 auto pipelineContext = PipelineContext::GetCurrentContext();
5466 if (pipelineContext) {
5467 pipelineContext->UpdateCurrentActiveNode(node);
5468 }
5469 auto* eventInfo = TypeInfoHelper::DynamicCast<InterceptKeyboardEvent>(info);
5470 JSRef<JSVal> keyboardOpt = func->ExecuteWithValue(*eventInfo);
5471 ParseJsCustomKeyboardOption(execCtx, keyboardOpt, opt);
5472 return opt;
5473 };
5474 WebModel::GetInstance()->SetOnInterceptKeyboardAttach(std::move(uiCallback));
5475 }
5476
OnAdsBlocked(const JSCallbackInfo& args)5477 void JSWeb::OnAdsBlocked(const JSCallbackInfo& args)
5478 {
5479 if (args.Length() < 1 || !args[0]->IsFunction()) {
5480 return;
5481 }
5482
5483 WeakPtr<NG::FrameNode> frameNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
5484 auto jsFunc = AceType::MakeRefPtr<JsEventFunction<AdsBlockedEvent, 1>>(
5485 JSRef<JSFunc>::Cast(args[0]), AdsBlockedEventToJSValue);
5486 auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), node = frameNode](
5487 const BaseEventInfo* info) {
5488 auto webNode = node.Upgrade();
5489 CHECK_NULL_VOID(webNode);
5490 ContainerScope scope(webNode->GetInstanceId());
5491 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
5492 auto pipelineContext = PipelineContext::GetCurrentContext();
5493 if (pipelineContext) {
5494 pipelineContext->UpdateCurrentActiveNode(node);
5495 }
5496 auto* eventInfo = TypeInfoHelper::DynamicCast<AdsBlockedEvent>(info);
5497 func->Execute(*eventInfo);
5498 };
5499 WebModel::GetInstance()->SetAdsBlockedEventId(jsCallback);
5500 }
5501
ForceDisplayScrollBar(const JSCallbackInfo& args)5502 void JSWeb::ForceDisplayScrollBar(const JSCallbackInfo& args)
5503 {
5504 if (args.Length() < 1 || !args[0]->IsBoolean()) {
5505 return;
5506 }
5507 bool isEnabled = args[0]->ToBoolean();
5508 WebModel::GetInstance()->SetOverlayScrollbarEnabled(isEnabled);
5509 }
5510
KeyboardAvoidMode(int32_t mode)5511 void JSWeb::KeyboardAvoidMode(int32_t mode)
5512 {
5513 if (mode < static_cast<int32_t>(WebKeyboardAvoidMode::RESIZE_VISUAL) ||
5514 mode > static_cast<int32_t>(WebKeyboardAvoidMode::DEFAULT)) {
5515 TAG_LOGE(AceLogTag::ACE_WEB, "KeyboardAvoidMode param err");
5516 return;
5517 }
5518 WebKeyboardAvoidMode avoidMode = static_cast<WebKeyboardAvoidMode>(mode);
5519 WebModel::GetInstance()->SetKeyboardAvoidMode(avoidMode);
5520 }
5521
EditMenuOptions(const JSCallbackInfo& info)5522 void JSWeb::EditMenuOptions(const JSCallbackInfo& info)
5523 {
5524 NG::OnCreateMenuCallback onCreateMenuCallback;
5525 NG::OnMenuItemClickCallback onMenuItemClick;
5526 JSViewAbstract::ParseEditMenuOptions(info, onCreateMenuCallback, onMenuItemClick);
5527 WebModel::GetInstance()->SetEditMenuOptions(std::move(onCreateMenuCallback), std::move(onMenuItemClick));
5528 }
5529
EnableHapticFeedback(const JSCallbackInfo& args)5530 void JSWeb::EnableHapticFeedback(const JSCallbackInfo& args)
5531 {
5532 if (args.Length() < 1 || !args[0]->IsBoolean()) {
5533 return;
5534 }
5535 bool isEnabled = args[0]->ToBoolean();
5536 WebModel::GetInstance()->SetEnabledHapticFeedback(isEnabled);
5537 }
5538
5539 } // namespace OHOS::Ace::Framework
5540