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