• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "frameworks/bridge/declarative_frontend/jsview/js_web.h"
17 
18 #include <optional>
19 #include <string>
20 
21 #include "base/log/ace_scoring_log.h"
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/utils.h"
25 #include "base/web/webview/ohos_nweb/include/nweb.h"
26 #include "bridge/common/utils/engine_helper.h"
27 #include "bridge/declarative_frontend/engine/functions/js_click_function.h"
28 #include "bridge/declarative_frontend/engine/functions/js_drag_function.h"
29 #include "bridge/declarative_frontend/engine/functions/js_key_function.h"
30 #include "bridge/declarative_frontend/engine/js_converter.h"
31 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
32 #include "bridge/declarative_frontend/jsview/js_utils.h"
33 #include "bridge/declarative_frontend/jsview/js_web_controller.h"
34 #include "bridge/declarative_frontend/view_stack_processor.h"
35 #include "core/common/container.h"
36 #include "core/common/container_scope.h"
37 #include "core/components/web/web_event.h"
38 #include "core/components_ng/pattern/web/web_view.h"
39 #include "core/pipeline/pipeline_base.h"
40 #include "pixel_map.h"
41 #include "pixel_map_napi.h"
42 
43 namespace OHOS::Ace::Framework {
44 bool JSWeb::webDebuggingAccess_ = false;
45 class JSWebDialog : public Referenced {
46 public:
JSBind(BindingTarget globalObj)47     static void JSBind(BindingTarget globalObj)
48     {
49         JSClass<JSWebDialog>::Declare("WebDialog");
50         JSClass<JSWebDialog>::CustomMethod("handleConfirm", &JSWebDialog::Confirm);
51         JSClass<JSWebDialog>::CustomMethod("handleCancel", &JSWebDialog::Cancel);
52         JSClass<JSWebDialog>::CustomMethod("handlePromptConfirm", &JSWebDialog::PromptConfirm);
53         JSClass<JSWebDialog>::Bind(globalObj, &JSWebDialog::Constructor, &JSWebDialog::Destructor);
54     }
55 
SetResult(const RefPtr<Result> & result)56     void SetResult(const RefPtr<Result>& result)
57     {
58         result_ = result;
59     }
60 
Confirm(const JSCallbackInfo & args)61     void Confirm(const JSCallbackInfo& args)
62     {
63         if (result_) {
64             result_->Confirm();
65         }
66     }
67 
PromptConfirm(const JSCallbackInfo & args)68     void PromptConfirm(const JSCallbackInfo& args)
69     {
70         std::string message;
71         if (!result_) {
72             return;
73         }
74         if (args.Length() == 1 && args[0]->IsString()) {
75             message = args[0]->ToString();
76             result_->Confirm(message);
77         }
78     }
79 
Cancel(const JSCallbackInfo & args)80     void Cancel(const JSCallbackInfo& args)
81     {
82         if (result_) {
83             result_->Cancel();
84         }
85     }
86 
87 private:
Constructor(const JSCallbackInfo & args)88     static void Constructor(const JSCallbackInfo& args)
89     {
90         auto jsWebDialog = Referenced::MakeRefPtr<JSWebDialog>();
91         jsWebDialog->IncRefCount();
92         args.SetReturnValue(Referenced::RawPtr(jsWebDialog));
93     }
94 
Destructor(JSWebDialog * jsWebDialog)95     static void Destructor(JSWebDialog* jsWebDialog)
96     {
97         if (jsWebDialog != nullptr) {
98             jsWebDialog->DecRefCount();
99         }
100     }
101 
102     RefPtr<Result> result_;
103 };
104 
105 class JSFullScreenExitHandler : public Referenced {
106 public:
JSBind(BindingTarget globalObj)107     static void JSBind(BindingTarget globalObj)
108     {
109         JSClass<JSFullScreenExitHandler>::Declare("FullScreenExitHandler");
110         JSClass<JSFullScreenExitHandler>::CustomMethod("exitFullScreen", &JSFullScreenExitHandler::ExitFullScreen);
111         JSClass<JSFullScreenExitHandler>::Bind(
112             globalObj, &JSFullScreenExitHandler::Constructor, &JSFullScreenExitHandler::Destructor);
113     }
114 
SetHandler(const RefPtr<FullScreenExitHandler> & handler)115     void SetHandler(const RefPtr<FullScreenExitHandler>& handler)
116     {
117         fullScreenExitHandler_ = handler;
118     }
119 
ExitFullScreen(const JSCallbackInfo & args)120     void ExitFullScreen(const JSCallbackInfo& args)
121     {
122         if (fullScreenExitHandler_) {
123             fullScreenExitHandler_->ExitFullScreen();
124         }
125     }
126 
127 private:
Constructor(const JSCallbackInfo & args)128     static void Constructor(const JSCallbackInfo& args)
129     {
130         auto jsFullScreenExitHandler = Referenced::MakeRefPtr<JSFullScreenExitHandler>();
131         jsFullScreenExitHandler->IncRefCount();
132         args.SetReturnValue(Referenced::RawPtr(jsFullScreenExitHandler));
133     }
134 
Destructor(JSFullScreenExitHandler * jsFullScreenExitHandler)135     static void Destructor(JSFullScreenExitHandler* jsFullScreenExitHandler)
136     {
137         if (jsFullScreenExitHandler != nullptr) {
138             jsFullScreenExitHandler->DecRefCount();
139         }
140     }
141     RefPtr<FullScreenExitHandler> fullScreenExitHandler_;
142 };
143 
144 class JSWebHttpAuth : public Referenced {
145 public:
JSBind(BindingTarget globalObj)146     static void JSBind(BindingTarget globalObj)
147     {
148         JSClass<JSWebHttpAuth>::Declare("WebHttpAuthResult");
149         JSClass<JSWebHttpAuth>::CustomMethod("confirm", &JSWebHttpAuth::Confirm);
150         JSClass<JSWebHttpAuth>::CustomMethod("cancel", &JSWebHttpAuth::Cancel);
151         JSClass<JSWebHttpAuth>::CustomMethod("isHttpAuthInfoSaved", &JSWebHttpAuth::IsHttpAuthInfoSaved);
152         JSClass<JSWebHttpAuth>::Bind(globalObj, &JSWebHttpAuth::Constructor, &JSWebHttpAuth::Destructor);
153     }
154 
SetResult(const RefPtr<AuthResult> & result)155     void SetResult(const RefPtr<AuthResult>& result)
156     {
157         result_ = result;
158     }
159 
Confirm(const JSCallbackInfo & args)160     void Confirm(const JSCallbackInfo& args)
161     {
162         if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsString()) {
163             LOGW("web http auth confirm list is not string");
164             auto code = JSVal(ToJSValue(false));
165             auto descriptionRef = JSRef<JSVal>::Make(code);
166             args.SetReturnValue(descriptionRef);
167             return;
168         }
169         std::string userName = args[0]->ToString();
170         std::string password = args[1]->ToString();
171         bool ret = false;
172         if (result_) {
173             result_->Confirm(userName, password);
174             ret = true;
175         }
176         auto code = JSVal(ToJSValue(ret));
177         auto descriptionRef = JSRef<JSVal>::Make(code);
178         args.SetReturnValue(descriptionRef);
179     }
180 
Cancel(const JSCallbackInfo & args)181     void Cancel(const JSCallbackInfo& args)
182     {
183         if (result_) {
184             result_->Cancel();
185         }
186     }
187 
IsHttpAuthInfoSaved(const JSCallbackInfo & args)188     void IsHttpAuthInfoSaved(const JSCallbackInfo& args)
189     {
190         bool ret = false;
191         if (result_) {
192             ret = result_->IsHttpAuthInfoSaved();
193         }
194         auto code = JSVal(ToJSValue(ret));
195         auto descriptionRef = JSRef<JSVal>::Make(code);
196         args.SetReturnValue(descriptionRef);
197     }
198 
199 private:
Constructor(const JSCallbackInfo & args)200     static void Constructor(const JSCallbackInfo& args)
201     {
202         auto jsWebHttpAuth = Referenced::MakeRefPtr<JSWebHttpAuth>();
203         jsWebHttpAuth->IncRefCount();
204         args.SetReturnValue(Referenced::RawPtr(jsWebHttpAuth));
205     }
206 
Destructor(JSWebHttpAuth * jsWebHttpAuth)207     static void Destructor(JSWebHttpAuth* jsWebHttpAuth)
208     {
209         if (jsWebHttpAuth != nullptr) {
210             jsWebHttpAuth->DecRefCount();
211         }
212     }
213 
214     RefPtr<AuthResult> result_;
215 };
216 
217 class JSWebSslError : public Referenced {
218 public:
JSBind(BindingTarget globalObj)219     static void JSBind(BindingTarget globalObj)
220     {
221         JSClass<JSWebSslError>::Declare("WebSslErrorResult");
222         JSClass<JSWebSslError>::CustomMethod("handleConfirm", &JSWebSslError::HandleConfirm);
223         JSClass<JSWebSslError>::CustomMethod("handleCancel", &JSWebSslError::HandleCancel);
224         JSClass<JSWebSslError>::Bind(globalObj, &JSWebSslError::Constructor, &JSWebSslError::Destructor);
225     }
226 
SetResult(const RefPtr<SslErrorResult> & result)227     void SetResult(const RefPtr<SslErrorResult>& result)
228     {
229         result_ = result;
230     }
231 
HandleConfirm(const JSCallbackInfo & args)232     void HandleConfirm(const JSCallbackInfo& args)
233     {
234         if (result_) {
235             result_->HandleConfirm();
236         }
237     }
238 
HandleCancel(const JSCallbackInfo & args)239     void HandleCancel(const JSCallbackInfo& args)
240     {
241         if (result_) {
242             result_->HandleCancel();
243         }
244     }
245 
246 private:
Constructor(const JSCallbackInfo & args)247     static void Constructor(const JSCallbackInfo& args)
248     {
249         auto jsWebSslError = Referenced::MakeRefPtr<JSWebSslError>();
250         jsWebSslError->IncRefCount();
251         args.SetReturnValue(Referenced::RawPtr(jsWebSslError));
252     }
253 
Destructor(JSWebSslError * jsWebSslError)254     static void Destructor(JSWebSslError* jsWebSslError)
255     {
256         if (jsWebSslError != nullptr) {
257             jsWebSslError->DecRefCount();
258         }
259     }
260 
261     RefPtr<SslErrorResult> result_;
262 };
263 
264 class JSWebSslSelectCert : public Referenced {
265 public:
JSBind(BindingTarget globalObj)266     static void JSBind(BindingTarget globalObj)
267     {
268         JSClass<JSWebSslSelectCert>::Declare("WebSslSelectCertResult");
269         JSClass<JSWebSslSelectCert>::CustomMethod("confirm", &JSWebSslSelectCert::HandleConfirm);
270         JSClass<JSWebSslSelectCert>::CustomMethod("cancel", &JSWebSslSelectCert::HandleCancel);
271         JSClass<JSWebSslSelectCert>::CustomMethod("ignore", &JSWebSslSelectCert::HandleIgnore);
272         JSClass<JSWebSslSelectCert>::Bind(globalObj, &JSWebSslSelectCert::Constructor, &JSWebSslSelectCert::Destructor);
273     }
274 
SetResult(const RefPtr<SslSelectCertResult> & result)275     void SetResult(const RefPtr<SslSelectCertResult>& result)
276     {
277         result_ = result;
278     }
279 
HandleConfirm(const JSCallbackInfo & args)280     void HandleConfirm(const JSCallbackInfo& args)
281     {
282         if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsString()) {
283             return;
284         }
285         std::string privateKeyFile = args[0]->ToString();
286         std::string certChainFile = args[1]->ToString();
287         if (result_) {
288             result_->HandleConfirm(privateKeyFile, certChainFile);
289         }
290     }
291 
HandleCancel(const JSCallbackInfo & args)292     void HandleCancel(const JSCallbackInfo& args)
293     {
294         if (result_) {
295             result_->HandleCancel();
296         }
297     }
298 
HandleIgnore(const JSCallbackInfo & args)299     void HandleIgnore(const JSCallbackInfo& args)
300     {
301         if (result_) {
302             result_->HandleIgnore();
303         }
304     }
305 
306 private:
Constructor(const JSCallbackInfo & args)307     static void Constructor(const JSCallbackInfo& args)
308     {
309         auto jsWebSslSelectCert = Referenced::MakeRefPtr<JSWebSslSelectCert>();
310         jsWebSslSelectCert->IncRefCount();
311         args.SetReturnValue(Referenced::RawPtr(jsWebSslSelectCert));
312     }
313 
Destructor(JSWebSslSelectCert * jsWebSslSelectCert)314     static void Destructor(JSWebSslSelectCert* jsWebSslSelectCert)
315     {
316         if (jsWebSslSelectCert != nullptr) {
317             jsWebSslSelectCert->DecRefCount();
318         }
319     }
320 
321     RefPtr<SslSelectCertResult> result_;
322 };
323 
324 class JSWebConsoleLog : public Referenced {
325 public:
JSBind(BindingTarget globalObj)326     static void JSBind(BindingTarget globalObj)
327     {
328         JSClass<JSWebConsoleLog>::Declare("ConsoleMessage");
329         JSClass<JSWebConsoleLog>::CustomMethod("getLineNumber", &JSWebConsoleLog::GetLineNumber);
330         JSClass<JSWebConsoleLog>::CustomMethod("getMessage", &JSWebConsoleLog::GetLog);
331         JSClass<JSWebConsoleLog>::CustomMethod("getMessageLevel", &JSWebConsoleLog::GetLogLevel);
332         JSClass<JSWebConsoleLog>::CustomMethod("getSourceId", &JSWebConsoleLog::GetSourceId);
333         JSClass<JSWebConsoleLog>::Bind(globalObj, &JSWebConsoleLog::Constructor, &JSWebConsoleLog::Destructor);
334     }
335 
SetMessage(const RefPtr<WebConsoleLog> & message)336     void SetMessage(const RefPtr<WebConsoleLog>& message)
337     {
338         message_ = message;
339     }
340 
GetLineNumber(const JSCallbackInfo & args)341     void GetLineNumber(const JSCallbackInfo& args)
342     {
343         auto code = JSVal(ToJSValue(message_->GetLineNumber()));
344         auto descriptionRef = JSRef<JSVal>::Make(code);
345         args.SetReturnValue(descriptionRef);
346     }
347 
GetLog(const JSCallbackInfo & args)348     void GetLog(const JSCallbackInfo& args)
349     {
350         auto code = JSVal(ToJSValue(message_->GetLog()));
351         auto descriptionRef = JSRef<JSVal>::Make(code);
352         args.SetReturnValue(descriptionRef);
353     }
354 
GetLogLevel(const JSCallbackInfo & args)355     void GetLogLevel(const JSCallbackInfo& args)
356     {
357         auto code = JSVal(ToJSValue(message_->GetLogLevel()));
358         auto descriptionRef = JSRef<JSVal>::Make(code);
359         args.SetReturnValue(descriptionRef);
360     }
361 
GetSourceId(const JSCallbackInfo & args)362     void GetSourceId(const JSCallbackInfo& args)
363     {
364         auto code = JSVal(ToJSValue(message_->GetSourceId()));
365         auto descriptionRef = JSRef<JSVal>::Make(code);
366         args.SetReturnValue(descriptionRef);
367     }
368 
369 private:
Constructor(const JSCallbackInfo & args)370     static void Constructor(const JSCallbackInfo& args)
371     {
372         auto jsWebConsoleLog = Referenced::MakeRefPtr<JSWebConsoleLog>();
373         jsWebConsoleLog->IncRefCount();
374         args.SetReturnValue(Referenced::RawPtr(jsWebConsoleLog));
375     }
376 
Destructor(JSWebConsoleLog * jsWebConsoleLog)377     static void Destructor(JSWebConsoleLog* jsWebConsoleLog)
378     {
379         if (jsWebConsoleLog != nullptr) {
380             jsWebConsoleLog->DecRefCount();
381         }
382     }
383 
384     RefPtr<WebConsoleLog> message_;
385 };
386 
387 class JSWebGeolocation : public Referenced {
388 public:
JSBind(BindingTarget globalObj)389     static void JSBind(BindingTarget globalObj)
390     {
391         JSClass<JSWebGeolocation>::Declare("WebGeolocation");
392         JSClass<JSWebGeolocation>::CustomMethod("invoke", &JSWebGeolocation::Invoke);
393         JSClass<JSWebGeolocation>::Bind(globalObj, &JSWebGeolocation::Constructor, &JSWebGeolocation::Destructor);
394     }
395 
SetEvent(const LoadWebGeolocationShowEvent & eventInfo)396     void SetEvent(const LoadWebGeolocationShowEvent& eventInfo)
397     {
398         webGeolocation_ = eventInfo.GetWebGeolocation();
399     }
400 
Invoke(const JSCallbackInfo & args)401     void Invoke(const JSCallbackInfo& args)
402     {
403         std::string origin;
404         bool allow = false;
405         bool retain = false;
406         if (args[0]->IsString()) {
407             origin = args[0]->ToString();
408         }
409         if (args[1]->IsBoolean()) {
410             allow = args[1]->ToBoolean();
411         }
412         if (args[2]->IsBoolean()) {
413             retain = args[2]->ToBoolean();
414         }
415         if (webGeolocation_) {
416             webGeolocation_->Invoke(origin, allow, retain);
417         }
418     }
419 
420 private:
Constructor(const JSCallbackInfo & args)421     static void Constructor(const JSCallbackInfo& args)
422     {
423         auto jsWebGeolocation = Referenced::MakeRefPtr<JSWebGeolocation>();
424         jsWebGeolocation->IncRefCount();
425         args.SetReturnValue(Referenced::RawPtr(jsWebGeolocation));
426     }
427 
Destructor(JSWebGeolocation * jsWebGeolocation)428     static void Destructor(JSWebGeolocation* jsWebGeolocation)
429     {
430         if (jsWebGeolocation != nullptr) {
431             jsWebGeolocation->DecRefCount();
432         }
433     }
434 
435     RefPtr<WebGeolocation> webGeolocation_;
436 };
437 
438 class JSWebPermissionRequest : public Referenced {
439 public:
JSBind(BindingTarget globalObj)440     static void JSBind(BindingTarget globalObj)
441     {
442         JSClass<JSWebPermissionRequest>::Declare("WebPermissionRequest");
443         JSClass<JSWebPermissionRequest>::CustomMethod("deny", &JSWebPermissionRequest::Deny);
444         JSClass<JSWebPermissionRequest>::CustomMethod("getOrigin", &JSWebPermissionRequest::GetOrigin);
445         JSClass<JSWebPermissionRequest>::CustomMethod("getAccessibleResource", &JSWebPermissionRequest::GetResources);
446         JSClass<JSWebPermissionRequest>::CustomMethod("grant", &JSWebPermissionRequest::Grant);
447         JSClass<JSWebPermissionRequest>::Bind(
448             globalObj, &JSWebPermissionRequest::Constructor, &JSWebPermissionRequest::Destructor);
449     }
450 
SetEvent(const WebPermissionRequestEvent & eventInfo)451     void SetEvent(const WebPermissionRequestEvent& eventInfo)
452     {
453         webPermissionRequest_ = eventInfo.GetWebPermissionRequest();
454     }
455 
Deny(const JSCallbackInfo & args)456     void Deny(const JSCallbackInfo& args)
457     {
458         if (webPermissionRequest_) {
459             webPermissionRequest_->Deny();
460         }
461     }
462 
GetOrigin(const JSCallbackInfo & args)463     void GetOrigin(const JSCallbackInfo& args)
464     {
465         std::string origin;
466         if (webPermissionRequest_) {
467             origin = webPermissionRequest_->GetOrigin();
468         }
469         auto originJs = JSVal(ToJSValue(origin));
470         auto originJsRef = JSRef<JSVal>::Make(originJs);
471         args.SetReturnValue(originJsRef);
472     }
473 
GetResources(const JSCallbackInfo & args)474     void GetResources(const JSCallbackInfo& args)
475     {
476         JSRef<JSArray> result = JSRef<JSArray>::New();
477         if (webPermissionRequest_) {
478             std::vector<std::string> resources = webPermissionRequest_->GetResources();
479             uint32_t index = 0;
480             for (auto iterator = resources.begin(); iterator != resources.end(); ++iterator) {
481                 auto valueStr = JSVal(ToJSValue(*iterator));
482                 auto value = JSRef<JSVal>::Make(valueStr);
483                 result->SetValueAt(index++, value);
484             }
485         }
486         args.SetReturnValue(result);
487     }
488 
Grant(const JSCallbackInfo & args)489     void Grant(const JSCallbackInfo& args)
490     {
491         if (args.Length() < 1) {
492             if (webPermissionRequest_) {
493                 webPermissionRequest_->Deny();
494             }
495         }
496         std::vector<std::string> resources;
497         if (args[0]->IsArray()) {
498             JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
499             for (size_t i = 0; i < array->Length(); i++) {
500                 JSRef<JSVal> val = array->GetValueAt(i);
501                 if (!val->IsString()) {
502                     LOGW("resources list is not string at index %{public}zu", i);
503                     continue;
504                 }
505                 std::string res;
506                 if (!ConvertFromJSValue(val, res)) {
507                     LOGW("can't convert resource at index %{public}zu of JSWebPermissionRequest, so skip it.", i);
508                     continue;
509                 }
510                 resources.push_back(res);
511             }
512         }
513 
514         if (webPermissionRequest_) {
515             webPermissionRequest_->Grant(resources);
516         }
517     }
518 
519 private:
Constructor(const JSCallbackInfo & args)520     static void Constructor(const JSCallbackInfo& args)
521     {
522         auto jsWebPermissionRequest = Referenced::MakeRefPtr<JSWebPermissionRequest>();
523         jsWebPermissionRequest->IncRefCount();
524         args.SetReturnValue(Referenced::RawPtr(jsWebPermissionRequest));
525     }
526 
Destructor(JSWebPermissionRequest * jsWebPermissionRequest)527     static void Destructor(JSWebPermissionRequest* jsWebPermissionRequest)
528     {
529         if (jsWebPermissionRequest != nullptr) {
530             jsWebPermissionRequest->DecRefCount();
531         }
532     }
533 
534     RefPtr<WebPermissionRequest> webPermissionRequest_;
535 };
536 
537 class JSWebWindowNewHandler : public Referenced {
538 public:
539     struct ChildWindowInfo {
540         int32_t parentWebId_ = -1;
541         JSRef<JSObject> controller_;
542     };
543 
JSBind(BindingTarget globalObj)544     static void JSBind(BindingTarget globalObj)
545     {
546         JSClass<JSWebWindowNewHandler>::Declare("WebWindowNewHandler");
547         JSClass<JSWebWindowNewHandler>::CustomMethod("setWebController", &JSWebWindowNewHandler::SetWebController);
548         JSClass<JSWebWindowNewHandler>::Bind(
549             globalObj, &JSWebWindowNewHandler::Constructor, &JSWebWindowNewHandler::Destructor);
550     }
551 
SetEvent(const WebWindowNewEvent & eventInfo)552     void SetEvent(const WebWindowNewEvent& eventInfo)
553     {
554         handler_ = eventInfo.GetWebWindowNewHandler();
555     }
556 
PopController(int32_t id)557     static JSRef<JSObject> PopController(int32_t id)
558     {
559         LOGI("PopController");
560         auto iter = controller_map_.find(id);
561         if (iter == controller_map_.end()) {
562             LOGI("JSWebWindowNewHandler not find web controller");
563             return JSRef<JSVal>::Make();
564         }
565         auto controller = iter->second.controller_;
566         controller_map_.erase(iter);
567         return controller;
568     }
569 
ExistController(JSRef<JSObject> & controller,int32_t & parentWebId)570     static bool ExistController(JSRef<JSObject>& controller, int32_t& parentWebId)
571     {
572         auto getThisVarFunction = controller->GetProperty("innerGetThisVar");
573         if (!getThisVarFunction->IsFunction()) {
574             LOGE("get innerGetThisVar failed");
575             parentWebId = -1;
576             return false;
577         }
578         auto func = JSRef<JSFunc>::Cast(getThisVarFunction);
579         auto thisVar = func->Call(controller, 0, {});
580         int64_t thisPtr = thisVar->ToNumber<int64_t>();
581         for (auto iter = controller_map_.begin(); iter != controller_map_.end(); iter++) {
582             auto getThisVarFunction1 = iter->second.controller_->GetProperty("innerGetThisVar");
583             if (getThisVarFunction1->IsFunction()) {
584                 auto func1 = JSRef<JSFunc>::Cast(getThisVarFunction1);
585                 auto thisVar1 = func1->Call(iter->second.controller_, 0, {});
586                 if (thisPtr == thisVar1->ToNumber<int64_t>()) {
587                     parentWebId = iter->second.parentWebId_;
588                     return true;
589                 }
590             }
591         }
592         parentWebId = -1;
593         return false;
594     }
595 
SetWebController(const JSCallbackInfo & args)596     void SetWebController(const JSCallbackInfo& args)
597     {
598         LOGI("JSWebWindowNewHandler SetWebController");
599         if (handler_) {
600             int32_t parentNWebId = handler_->GetParentNWebId();
601             if (parentNWebId == -1) {
602                 LOGE("SetWebController parent web id err");
603                 return;
604             }
605             if (args.Length() < 1 || !args[0]->IsObject()) {
606                 LOGE("SetWebController param err");
607                 NG::WebView::NotifyPopupWindowResult(parentNWebId, false);
608                 return;
609             }
610             auto controller = JSRef<JSObject>::Cast(args[0]);
611             if (controller.IsEmpty()) {
612                 LOGI("SetWebController controller is empty");
613                 NG::WebView::NotifyPopupWindowResult(parentNWebId, false);
614                 return;
615             }
616             auto getWebIdFunction = controller->GetProperty("innerGetWebId");
617             if (!getWebIdFunction->IsFunction()) {
618                 LOGI("SetWebController get innerGetWebId failed");
619                 NG::WebView::NotifyPopupWindowResult(parentNWebId, false);
620                 return;
621             }
622             auto func = JSRef<JSFunc>::Cast(getWebIdFunction);
623             auto webId = func->Call(controller, 0, {});
624             int32_t childWebId = webId->ToNumber<int32_t>();
625             if (childWebId == parentNWebId || childWebId != -1) {
626                 LOGE("The child window is initialized or the parent window is the same as the child window");
627                 NG::WebView::NotifyPopupWindowResult(parentNWebId, false);
628                 return;
629             }
630             controller_map_.insert(std::pair<int32_t, ChildWindowInfo>(handler_->GetId(),
631                 { parentNWebId, controller }));
632         }
633     }
634 
635 private:
Constructor(const JSCallbackInfo & args)636     static void Constructor(const JSCallbackInfo& args)
637     {
638         auto jsWebWindowNewHandler = Referenced::MakeRefPtr<JSWebWindowNewHandler>();
639         jsWebWindowNewHandler->IncRefCount();
640         args.SetReturnValue(Referenced::RawPtr(jsWebWindowNewHandler));
641     }
642 
Destructor(JSWebWindowNewHandler * jsWebWindowNewHandler)643     static void Destructor(JSWebWindowNewHandler* jsWebWindowNewHandler)
644     {
645         if (jsWebWindowNewHandler != nullptr) {
646             jsWebWindowNewHandler->DecRefCount();
647         }
648     }
649 
650     RefPtr<WebWindowNewHandler> handler_;
651     static std::unordered_map<int32_t, ChildWindowInfo> controller_map_;
652 };
653 std::unordered_map<int32_t, JSWebWindowNewHandler::ChildWindowInfo> JSWebWindowNewHandler::controller_map_;
654 
655 class JSDataResubmitted : public Referenced {
656 public:
JSBind(BindingTarget globalObj)657     static void JSBind(BindingTarget globalObj)
658     {
659         JSClass<JSDataResubmitted>::Declare("DataResubmissionHandler");
660         JSClass<JSDataResubmitted>::CustomMethod("resend", &JSDataResubmitted::Resend);
661         JSClass<JSDataResubmitted>::CustomMethod("cancel", &JSDataResubmitted::Cancel);
662         JSClass<JSDataResubmitted>::Bind(globalObj, &JSDataResubmitted::Constructor, &JSDataResubmitted::Destructor);
663     }
664 
SetHandler(const RefPtr<DataResubmitted> & handler)665     void SetHandler(const RefPtr<DataResubmitted>& handler)
666     {
667         dataResubmitted_ = handler;
668     }
669 
Resend(const JSCallbackInfo & args)670     void Resend(const JSCallbackInfo& args)
671     {
672         if (dataResubmitted_) {
673             dataResubmitted_->Resend();
674         }
675     }
676 
Cancel(const JSCallbackInfo & args)677     void Cancel(const JSCallbackInfo& args)
678     {
679         if (dataResubmitted_) {
680             dataResubmitted_->Cancel();
681         }
682     }
683 
684 private:
Constructor(const JSCallbackInfo & args)685     static void Constructor(const JSCallbackInfo& args)
686     {
687         auto jsDataResubmitted = Referenced::MakeRefPtr<JSDataResubmitted>();
688         jsDataResubmitted->IncRefCount();
689         args.SetReturnValue(Referenced::RawPtr(jsDataResubmitted));
690     }
691 
Destructor(JSDataResubmitted * jsDataResubmitted)692     static void Destructor(JSDataResubmitted* jsDataResubmitted)
693     {
694         if (jsDataResubmitted != nullptr) {
695             jsDataResubmitted->DecRefCount();
696         }
697     }
698     RefPtr<DataResubmitted> dataResubmitted_;
699 };
700 
701 class JSWebResourceError : public Referenced {
702 public:
JSBind(BindingTarget globalObj)703     static void JSBind(BindingTarget globalObj)
704     {
705         JSClass<JSWebResourceError>::Declare("WebResourceError");
706         JSClass<JSWebResourceError>::CustomMethod("getErrorCode", &JSWebResourceError::GetErrorCode);
707         JSClass<JSWebResourceError>::CustomMethod("getErrorInfo", &JSWebResourceError::GetErrorInfo);
708         JSClass<JSWebResourceError>::Bind(globalObj, &JSWebResourceError::Constructor, &JSWebResourceError::Destructor);
709     }
710 
SetEvent(const ReceivedErrorEvent & eventInfo)711     void SetEvent(const ReceivedErrorEvent& eventInfo)
712     {
713         error_ = eventInfo.GetError();
714     }
715 
GetErrorCode(const JSCallbackInfo & args)716     void GetErrorCode(const JSCallbackInfo& args)
717     {
718         auto code = JSVal(ToJSValue(error_->GetCode()));
719         auto descriptionRef = JSRef<JSVal>::Make(code);
720         args.SetReturnValue(descriptionRef);
721     }
722 
GetErrorInfo(const JSCallbackInfo & args)723     void GetErrorInfo(const JSCallbackInfo& args)
724     {
725         auto info = JSVal(ToJSValue(error_->GetInfo()));
726         auto descriptionRef = JSRef<JSVal>::Make(info);
727         args.SetReturnValue(descriptionRef);
728     }
729 
730 private:
Constructor(const JSCallbackInfo & args)731     static void Constructor(const JSCallbackInfo& args)
732     {
733         auto jSWebResourceError = Referenced::MakeRefPtr<JSWebResourceError>();
734         jSWebResourceError->IncRefCount();
735         args.SetReturnValue(Referenced::RawPtr(jSWebResourceError));
736     }
737 
Destructor(JSWebResourceError * jSWebResourceError)738     static void Destructor(JSWebResourceError* jSWebResourceError)
739     {
740         if (jSWebResourceError != nullptr) {
741             jSWebResourceError->DecRefCount();
742         }
743     }
744 
745     RefPtr<WebError> error_;
746 };
747 
748 class JSWebResourceResponse : public Referenced {
749 public:
JSBind(BindingTarget globalObj)750     static void JSBind(BindingTarget globalObj)
751     {
752         JSClass<JSWebResourceResponse>::Declare("WebResourceResponse");
753         JSClass<JSWebResourceResponse>::CustomMethod("getResponseData", &JSWebResourceResponse::GetResponseData);
754         JSClass<JSWebResourceResponse>::CustomMethod(
755             "getResponseEncoding", &JSWebResourceResponse::GetResponseEncoding);
756         JSClass<JSWebResourceResponse>::CustomMethod(
757             "getResponseMimeType", &JSWebResourceResponse::GetResponseMimeType);
758         JSClass<JSWebResourceResponse>::CustomMethod("getReasonMessage", &JSWebResourceResponse::GetReasonMessage);
759         JSClass<JSWebResourceResponse>::CustomMethod("getResponseCode", &JSWebResourceResponse::GetResponseCode);
760         JSClass<JSWebResourceResponse>::CustomMethod("getResponseHeader", &JSWebResourceResponse::GetResponseHeader);
761         JSClass<JSWebResourceResponse>::CustomMethod("setResponseData", &JSWebResourceResponse::SetResponseData);
762         JSClass<JSWebResourceResponse>::CustomMethod(
763             "setResponseEncoding", &JSWebResourceResponse::SetResponseEncoding);
764         JSClass<JSWebResourceResponse>::CustomMethod(
765             "setResponseMimeType", &JSWebResourceResponse::SetResponseMimeType);
766         JSClass<JSWebResourceResponse>::CustomMethod("setReasonMessage", &JSWebResourceResponse::SetReasonMessage);
767         JSClass<JSWebResourceResponse>::CustomMethod("setResponseCode", &JSWebResourceResponse::SetResponseCode);
768         JSClass<JSWebResourceResponse>::CustomMethod("setResponseHeader", &JSWebResourceResponse::SetResponseHeader);
769         JSClass<JSWebResourceResponse>::CustomMethod("setResponseIsReady", &JSWebResourceResponse::SetResponseIsReady);
770         JSClass<JSWebResourceResponse>::Bind(
771             globalObj, &JSWebResourceResponse::Constructor, &JSWebResourceResponse::Destructor);
772     }
773 
JSWebResourceResponse()774     JSWebResourceResponse()
775     {
776         response_ = AceType::MakeRefPtr<WebResponse>();
777     }
778 
SetEvent(const ReceivedHttpErrorEvent & eventInfo)779     void SetEvent(const ReceivedHttpErrorEvent& eventInfo)
780     {
781         response_ = eventInfo.GetResponse();
782     }
783 
GetResponseData(const JSCallbackInfo & args)784     void GetResponseData(const JSCallbackInfo& args)
785     {
786         auto data = JSVal(ToJSValue(response_->GetData()));
787         auto descriptionRef = JSRef<JSVal>::Make(data);
788         args.SetReturnValue(descriptionRef);
789     }
790 
GetResponseEncoding(const JSCallbackInfo & args)791     void GetResponseEncoding(const JSCallbackInfo& args)
792     {
793         auto encoding = JSVal(ToJSValue(response_->GetEncoding()));
794         auto descriptionRef = JSRef<JSVal>::Make(encoding);
795         args.SetReturnValue(descriptionRef);
796     }
797 
GetResponseMimeType(const JSCallbackInfo & args)798     void GetResponseMimeType(const JSCallbackInfo& args)
799     {
800         auto mimeType = JSVal(ToJSValue(response_->GetMimeType()));
801         auto descriptionRef = JSRef<JSVal>::Make(mimeType);
802         args.SetReturnValue(descriptionRef);
803     }
804 
GetReasonMessage(const JSCallbackInfo & args)805     void GetReasonMessage(const JSCallbackInfo& args)
806     {
807         auto reason = JSVal(ToJSValue(response_->GetReason()));
808         auto descriptionRef = JSRef<JSVal>::Make(reason);
809         args.SetReturnValue(descriptionRef);
810     }
811 
GetResponseCode(const JSCallbackInfo & args)812     void GetResponseCode(const JSCallbackInfo& args)
813     {
814         auto code = JSVal(ToJSValue(response_->GetStatusCode()));
815         auto descriptionRef = JSRef<JSVal>::Make(code);
816         args.SetReturnValue(descriptionRef);
817     }
818 
GetResponseHeader(const JSCallbackInfo & args)819     void GetResponseHeader(const JSCallbackInfo& args)
820     {
821         auto map = response_->GetHeaders();
822         std::map<std::string, std::string>::iterator iterator;
823         uint32_t index = 0;
824         JSRef<JSArray> headers = JSRef<JSArray>::New();
825         for (iterator = map.begin(); iterator != map.end(); ++iterator) {
826             JSRef<JSObject> header = JSRef<JSObject>::New();
827             header->SetProperty("headerKey", iterator->first);
828             header->SetProperty("headerValue", iterator->second);
829             headers->SetValueAt(index++, header);
830         }
831         args.SetReturnValue(headers);
832     }
833 
GetResponseObj() const834     RefPtr<WebResponse> GetResponseObj() const
835     {
836         return response_;
837     }
838 
SetResponseData(const JSCallbackInfo & args)839     void SetResponseData(const JSCallbackInfo& args)
840     {
841         if (args.Length() <= 0) {
842             return;
843         }
844         if (args[0]->IsNumber()) {
845             auto fd = args[0]->ToNumber<int32_t>();
846             LOGI("intercept set data file handle %{public}d", fd);
847             response_->SetFileHandle(fd);
848             return;
849         }
850         if (args[0]->IsString()) {
851             LOGI("intercept set data string");
852             auto data = args[0]->ToString();
853             response_->SetData(data);
854             return;
855         }
856     }
857 
SetResponseEncoding(const JSCallbackInfo & args)858     void SetResponseEncoding(const JSCallbackInfo& args)
859     {
860         if ((args.Length() <= 0) || !(args[0]->IsString())) {
861             return;
862         }
863         auto encode = args[0]->ToString();
864         response_->SetEncoding(encode);
865     }
866 
SetResponseMimeType(const JSCallbackInfo & args)867     void SetResponseMimeType(const JSCallbackInfo& args)
868     {
869         if ((args.Length() <= 0) || !(args[0]->IsString())) {
870             return;
871         }
872         auto mineType = args[0]->ToString();
873         response_->SetMimeType(mineType);
874     }
875 
SetReasonMessage(const JSCallbackInfo & args)876     void SetReasonMessage(const JSCallbackInfo& args)
877     {
878         if ((args.Length() <= 0) || !(args[0]->IsString())) {
879             return;
880         }
881         auto reason = args[0]->ToString();
882         response_->SetReason(reason);
883     }
884 
SetResponseCode(const JSCallbackInfo & args)885     void SetResponseCode(const JSCallbackInfo& args)
886     {
887         if ((args.Length() <= 0) || !(args[0]->IsNumber())) {
888             return;
889         }
890         auto statusCode = args[0]->ToNumber<int32_t>();
891         response_->SetStatusCode(statusCode);
892     }
893 
SetResponseHeader(const JSCallbackInfo & args)894     void SetResponseHeader(const JSCallbackInfo& args)
895     {
896         if ((args.Length() <= 0) || !(args[0]->IsArray())) {
897             return;
898         }
899         JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
900         for (size_t i = 0; i < array->Length(); i++) {
901             auto obj = JSRef<JSObject>::Cast(array->GetValueAt(i));
902             auto headerKey = obj->GetProperty("headerKey");
903             auto headerValue = obj->GetProperty("headerValue");
904             if (!headerKey->IsString() || !headerValue->IsString()) {
905                 LOGE("headerKey or headerValue is undefined");
906                 return;
907             }
908             auto keystr = headerKey->ToString();
909             auto valstr = headerValue->ToString();
910             LOGI("Set Response Header %{public}s:%{public}s", keystr.c_str(), valstr.c_str());
911             response_->SetHeadersVal(keystr, valstr);
912         }
913     }
914 
SetResponseIsReady(const JSCallbackInfo & args)915     void SetResponseIsReady(const JSCallbackInfo& args)
916     {
917         if ((args.Length() <= 0) || !(args[0]->IsBoolean())) {
918             return;
919         }
920         bool isReady = false;
921         if (!ConvertFromJSValue(args[0], isReady)) {
922             LOGE("get response status fail");
923             return;
924         }
925         LOGI("intercept set response status is %{public}d", isReady);
926         response_->SetResponseStatus(isReady);
927     }
928 
929 private:
Constructor(const JSCallbackInfo & args)930     static void Constructor(const JSCallbackInfo& args)
931     {
932         auto jSWebResourceResponse = Referenced::MakeRefPtr<JSWebResourceResponse>();
933         jSWebResourceResponse->IncRefCount();
934         args.SetReturnValue(Referenced::RawPtr(jSWebResourceResponse));
935     }
936 
Destructor(JSWebResourceResponse * jSWebResourceResponse)937     static void Destructor(JSWebResourceResponse* jSWebResourceResponse)
938     {
939         if (jSWebResourceResponse != nullptr) {
940             jSWebResourceResponse->DecRefCount();
941         }
942     }
943 
944     RefPtr<WebResponse> response_;
945 };
946 
947 class JSWebResourceRequest : public Referenced {
948 public:
JSBind(BindingTarget globalObj)949     static void JSBind(BindingTarget globalObj)
950     {
951         JSClass<JSWebResourceRequest>::Declare("WebResourceRequest");
952         JSClass<JSWebResourceRequest>::CustomMethod("getRequestUrl", &JSWebResourceRequest::GetRequestUrl);
953         JSClass<JSWebResourceRequest>::CustomMethod("getRequestHeader", &JSWebResourceRequest::GetRequestHeader);
954         JSClass<JSWebResourceRequest>::CustomMethod("getRequestMethod", &JSWebResourceRequest::GetRequestMethod);
955         JSClass<JSWebResourceRequest>::CustomMethod("isRequestGesture", &JSWebResourceRequest::IsRequestGesture);
956         JSClass<JSWebResourceRequest>::CustomMethod("isMainFrame", &JSWebResourceRequest::IsMainFrame);
957         JSClass<JSWebResourceRequest>::CustomMethod("isRedirect", &JSWebResourceRequest::IsRedirect);
958         JSClass<JSWebResourceRequest>::Bind(
959             globalObj, &JSWebResourceRequest::Constructor, &JSWebResourceRequest::Destructor);
960     }
961 
SetErrorEvent(const ReceivedErrorEvent & eventInfo)962     void SetErrorEvent(const ReceivedErrorEvent& eventInfo)
963     {
964         request_ = eventInfo.GetRequest();
965     }
966 
SetHttpErrorEvent(const ReceivedHttpErrorEvent & eventInfo)967     void SetHttpErrorEvent(const ReceivedHttpErrorEvent& eventInfo)
968     {
969         request_ = eventInfo.GetRequest();
970     }
971 
SetOnInterceptRequestEvent(const OnInterceptRequestEvent & eventInfo)972     void SetOnInterceptRequestEvent(const OnInterceptRequestEvent& eventInfo)
973     {
974         request_ = eventInfo.GetRequest();
975     }
976 
IsRedirect(const JSCallbackInfo & args)977     void IsRedirect(const JSCallbackInfo& args)
978     {
979         auto isRedirect = JSVal(ToJSValue(request_->IsRedirect()));
980         auto descriptionRef = JSRef<JSVal>::Make(isRedirect);
981         args.SetReturnValue(descriptionRef);
982     }
983 
GetRequestUrl(const JSCallbackInfo & args)984     void GetRequestUrl(const JSCallbackInfo& args)
985     {
986         auto url = JSVal(ToJSValue(request_->GetUrl()));
987         auto descriptionRef = JSRef<JSVal>::Make(url);
988         args.SetReturnValue(descriptionRef);
989     }
990 
GetRequestMethod(const JSCallbackInfo & args)991     void GetRequestMethod(const JSCallbackInfo& args)
992     {
993         auto method = JSVal(ToJSValue(request_->GetMethod()));
994         auto descriptionRef = JSRef<JSVal>::Make(method);
995         args.SetReturnValue(descriptionRef);
996     }
997 
IsRequestGesture(const JSCallbackInfo & args)998     void IsRequestGesture(const JSCallbackInfo& args)
999     {
1000         auto isRequestGesture = JSVal(ToJSValue(request_->HasGesture()));
1001         auto descriptionRef = JSRef<JSVal>::Make(isRequestGesture);
1002         args.SetReturnValue(descriptionRef);
1003     }
1004 
IsMainFrame(const JSCallbackInfo & args)1005     void IsMainFrame(const JSCallbackInfo& args)
1006     {
1007         auto isMainFrame = JSVal(ToJSValue(request_->IsMainFrame()));
1008         auto descriptionRef = JSRef<JSVal>::Make(isMainFrame);
1009         args.SetReturnValue(descriptionRef);
1010     }
1011 
GetRequestHeader(const JSCallbackInfo & args)1012     void GetRequestHeader(const JSCallbackInfo& args)
1013     {
1014         auto map = request_->GetHeaders();
1015         std::map<std::string, std::string>::iterator iterator;
1016         uint32_t index = 0;
1017         JSRef<JSArray> headers = JSRef<JSArray>::New();
1018         for (iterator = map.begin(); iterator != map.end(); ++iterator) {
1019             JSRef<JSObject> header = JSRef<JSObject>::New();
1020             header->SetProperty("headerKey", iterator->first);
1021             header->SetProperty("headerValue", iterator->second);
1022             headers->SetValueAt(index++, header);
1023         }
1024         args.SetReturnValue(headers);
1025     }
1026 
1027 private:
Constructor(const JSCallbackInfo & args)1028     static void Constructor(const JSCallbackInfo& args)
1029     {
1030         auto jSWebResourceRequest = Referenced::MakeRefPtr<JSWebResourceRequest>();
1031         jSWebResourceRequest->IncRefCount();
1032         args.SetReturnValue(Referenced::RawPtr(jSWebResourceRequest));
1033     }
1034 
Destructor(JSWebResourceRequest * jSWebResourceRequest)1035     static void Destructor(JSWebResourceRequest* jSWebResourceRequest)
1036     {
1037         if (jSWebResourceRequest != nullptr) {
1038             jSWebResourceRequest->DecRefCount();
1039         }
1040     }
1041 
1042     RefPtr<WebRequest> request_;
1043 };
1044 
1045 class JSFileSelectorParam : public Referenced {
1046 public:
JSBind(BindingTarget globalObj)1047     static void JSBind(BindingTarget globalObj)
1048     {
1049         JSClass<JSFileSelectorParam>::Declare("FileSelectorParam");
1050         JSClass<JSFileSelectorParam>::CustomMethod("getTitle", &JSFileSelectorParam::GetTitle);
1051         JSClass<JSFileSelectorParam>::CustomMethod("getMode", &JSFileSelectorParam::GetMode);
1052         JSClass<JSFileSelectorParam>::CustomMethod("getAcceptType", &JSFileSelectorParam::GetAcceptType);
1053         JSClass<JSFileSelectorParam>::CustomMethod("isCapture", &JSFileSelectorParam::IsCapture);
1054         JSClass<JSFileSelectorParam>::Bind(
1055             globalObj, &JSFileSelectorParam::Constructor, &JSFileSelectorParam::Destructor);
1056     }
1057 
SetParam(const FileSelectorEvent & eventInfo)1058     void SetParam(const FileSelectorEvent& eventInfo)
1059     {
1060         param_ = eventInfo.GetParam();
1061     }
1062 
GetTitle(const JSCallbackInfo & args)1063     void GetTitle(const JSCallbackInfo& args)
1064     {
1065         auto title = JSVal(ToJSValue(param_->GetTitle()));
1066         auto descriptionRef = JSRef<JSVal>::Make(title);
1067         args.SetReturnValue(descriptionRef);
1068     }
1069 
GetMode(const JSCallbackInfo & args)1070     void GetMode(const JSCallbackInfo& args)
1071     {
1072         auto mode = JSVal(ToJSValue(param_->GetMode()));
1073         auto descriptionRef = JSRef<JSVal>::Make(mode);
1074         args.SetReturnValue(descriptionRef);
1075     }
1076 
IsCapture(const JSCallbackInfo & args)1077     void IsCapture(const JSCallbackInfo& args)
1078     {
1079         auto isCapture = JSVal(ToJSValue(param_->IsCapture()));
1080         auto descriptionRef = JSRef<JSVal>::Make(isCapture);
1081         args.SetReturnValue(descriptionRef);
1082     }
1083 
GetAcceptType(const JSCallbackInfo & args)1084     void GetAcceptType(const JSCallbackInfo& args)
1085     {
1086         auto acceptTypes = param_->GetAcceptType();
1087         JSRef<JSArray> result = JSRef<JSArray>::New();
1088         std::vector<std::string>::iterator iterator;
1089         uint32_t index = 0;
1090         for (iterator = acceptTypes.begin(); iterator != acceptTypes.end(); ++iterator) {
1091             auto valueStr = JSVal(ToJSValue(*iterator));
1092             auto value = JSRef<JSVal>::Make(valueStr);
1093             result->SetValueAt(index++, value);
1094         }
1095         args.SetReturnValue(result);
1096     }
1097 
1098 private:
Constructor(const JSCallbackInfo & args)1099     static void Constructor(const JSCallbackInfo& args)
1100     {
1101         auto jSFilerSelectorParam = Referenced::MakeRefPtr<JSFileSelectorParam>();
1102         jSFilerSelectorParam->IncRefCount();
1103         args.SetReturnValue(Referenced::RawPtr(jSFilerSelectorParam));
1104     }
1105 
Destructor(JSFileSelectorParam * jSFilerSelectorParam)1106     static void Destructor(JSFileSelectorParam* jSFilerSelectorParam)
1107     {
1108         if (jSFilerSelectorParam != nullptr) {
1109             jSFilerSelectorParam->DecRefCount();
1110         }
1111     }
1112 
1113     RefPtr<WebFileSelectorParam> param_;
1114 };
1115 
1116 class JSFileSelectorResult : public Referenced {
1117 public:
JSBind(BindingTarget globalObj)1118     static void JSBind(BindingTarget globalObj)
1119     {
1120         JSClass<JSFileSelectorResult>::Declare("FileSelectorResult");
1121         JSClass<JSFileSelectorResult>::CustomMethod("handleFileList", &JSFileSelectorResult::HandleFileList);
1122         JSClass<JSFileSelectorResult>::Bind(
1123             globalObj, &JSFileSelectorResult::Constructor, &JSFileSelectorResult::Destructor);
1124     }
1125 
SetResult(const FileSelectorEvent & eventInfo)1126     void SetResult(const FileSelectorEvent& eventInfo)
1127     {
1128         result_ = eventInfo.GetFileSelectorResult();
1129     }
1130 
HandleFileList(const JSCallbackInfo & args)1131     void HandleFileList(const JSCallbackInfo& args)
1132     {
1133         std::vector<std::string> fileList;
1134         if (args[0]->IsArray()) {
1135             JSRef<JSArray> array = JSRef<JSArray>::Cast(args[0]);
1136             for (size_t i = 0; i < array->Length(); i++) {
1137                 JSRef<JSVal> val = array->GetValueAt(i);
1138                 if (!val->IsString()) {
1139                     LOGW("file selector list is not string at index %{public}zu", i);
1140                     continue;
1141                 }
1142                 std::string fileName;
1143                 if (!ConvertFromJSValue(val, fileName)) {
1144                     LOGW("can't convert file name at index %{public}zu of JSFileSelectorResult, so skip it.", i);
1145                     continue;
1146                 }
1147                 fileList.push_back(fileName);
1148             }
1149         }
1150 
1151         if (result_) {
1152             result_->HandleFileList(fileList);
1153         }
1154     }
1155 
1156 private:
Constructor(const JSCallbackInfo & args)1157     static void Constructor(const JSCallbackInfo& args)
1158     {
1159         auto jsFileSelectorResult = Referenced::MakeRefPtr<JSFileSelectorResult>();
1160         jsFileSelectorResult->IncRefCount();
1161         args.SetReturnValue(Referenced::RawPtr(jsFileSelectorResult));
1162     }
1163 
Destructor(JSFileSelectorResult * jsFileSelectorResult)1164     static void Destructor(JSFileSelectorResult* jsFileSelectorResult)
1165     {
1166         if (jsFileSelectorResult != nullptr) {
1167             jsFileSelectorResult->DecRefCount();
1168         }
1169     }
1170 
1171     RefPtr<FileSelectorResult> result_;
1172 };
1173 
1174 class JSContextMenuParam : public Referenced {
1175 public:
JSBind(BindingTarget globalObj)1176     static void JSBind(BindingTarget globalObj)
1177     {
1178         JSClass<JSContextMenuParam>::Declare("WebContextMenuParam");
1179         JSClass<JSContextMenuParam>::CustomMethod("x", &JSContextMenuParam::GetXCoord);
1180         JSClass<JSContextMenuParam>::CustomMethod("y", &JSContextMenuParam::GetYCoord);
1181         JSClass<JSContextMenuParam>::CustomMethod("getLinkUrl", &JSContextMenuParam::GetLinkUrl);
1182         JSClass<JSContextMenuParam>::CustomMethod("getUnfilteredLinkUrl", &JSContextMenuParam::GetUnfilteredLinkUrl);
1183         JSClass<JSContextMenuParam>::CustomMethod("getSourceUrl", &JSContextMenuParam::GetSourceUrl);
1184         JSClass<JSContextMenuParam>::CustomMethod("existsImageContents", &JSContextMenuParam::HasImageContents);
1185         JSClass<JSContextMenuParam>::CustomMethod("getSelectionText", &JSContextMenuParam::GetSelectionText);
1186         JSClass<JSContextMenuParam>::CustomMethod("isEditable", &JSContextMenuParam::IsEditable);
1187         JSClass<JSContextMenuParam>::CustomMethod("getEditStateFlags", &JSContextMenuParam::GetEditStateFlags);
1188         JSClass<JSContextMenuParam>::CustomMethod("getSourceType", &JSContextMenuParam::GetSourceType);
1189         JSClass<JSContextMenuParam>::CustomMethod("getInputFieldType", &JSContextMenuParam::GetInputFieldType);
1190         JSClass<JSContextMenuParam>::CustomMethod("getMediaType", &JSContextMenuParam::GetMediaType);
1191         JSClass<JSContextMenuParam>::Bind(globalObj, &JSContextMenuParam::Constructor, &JSContextMenuParam::Destructor);
1192     }
1193 
SetParam(const ContextMenuEvent & eventInfo)1194     void SetParam(const ContextMenuEvent& eventInfo)
1195     {
1196         param_ = eventInfo.GetParam();
1197     }
1198 
GetXCoord(const JSCallbackInfo & args)1199     void GetXCoord(const JSCallbackInfo& args)
1200     {
1201         int32_t ret = -1;
1202         if (param_) {
1203             ret = param_->GetXCoord();
1204         }
1205         auto xCoord = JSVal(ToJSValue(ret));
1206         auto descriptionRef = JSRef<JSVal>::Make(xCoord);
1207         args.SetReturnValue(descriptionRef);
1208     }
1209 
GetYCoord(const JSCallbackInfo & args)1210     void GetYCoord(const JSCallbackInfo& args)
1211     {
1212         int32_t ret = -1;
1213         if (param_) {
1214             ret = param_->GetYCoord();
1215         }
1216         auto yCoord = JSVal(ToJSValue(ret));
1217         auto descriptionRef = JSRef<JSVal>::Make(yCoord);
1218         args.SetReturnValue(descriptionRef);
1219     }
1220 
GetLinkUrl(const JSCallbackInfo & args)1221     void GetLinkUrl(const JSCallbackInfo& args)
1222     {
1223         std::string url;
1224         if (param_) {
1225             url = param_->GetLinkUrl();
1226         }
1227         auto linkUrl = JSVal(ToJSValue(url));
1228         auto descriptionRef = JSRef<JSVal>::Make(linkUrl);
1229         args.SetReturnValue(descriptionRef);
1230     }
1231 
GetUnfilteredLinkUrl(const JSCallbackInfo & args)1232     void GetUnfilteredLinkUrl(const JSCallbackInfo& args)
1233     {
1234         std::string url;
1235         if (param_) {
1236             url = param_->GetUnfilteredLinkUrl();
1237         }
1238         auto unfilteredLinkUrl = JSVal(ToJSValue(url));
1239         auto descriptionRef = JSRef<JSVal>::Make(unfilteredLinkUrl);
1240         args.SetReturnValue(descriptionRef);
1241     }
1242 
GetSourceUrl(const JSCallbackInfo & args)1243     void GetSourceUrl(const JSCallbackInfo& args)
1244     {
1245         std::string url;
1246         if (param_) {
1247             url = param_->GetSourceUrl();
1248         }
1249         auto sourceUrl = JSVal(ToJSValue(url));
1250         auto descriptionRef = JSRef<JSVal>::Make(sourceUrl);
1251         args.SetReturnValue(descriptionRef);
1252     }
1253 
HasImageContents(const JSCallbackInfo & args)1254     void HasImageContents(const JSCallbackInfo& args)
1255     {
1256         bool ret = false;
1257         if (param_) {
1258             ret = param_->HasImageContents();
1259         }
1260         auto hasImageContents = JSVal(ToJSValue(ret));
1261         auto descriptionRef = JSRef<JSVal>::Make(hasImageContents);
1262         args.SetReturnValue(descriptionRef);
1263     }
1264 
GetSelectionText(const JSCallbackInfo & args)1265     void GetSelectionText(const JSCallbackInfo& args)
1266     {
1267         std::string text;
1268         if (param_) {
1269             text = param_->GetSelectionText();
1270         }
1271         auto jsText = JSVal(ToJSValue(text));
1272         auto descriptionRef = JSRef<JSVal>::Make(jsText);
1273         args.SetReturnValue(descriptionRef);
1274     }
1275 
IsEditable(const JSCallbackInfo & args)1276     void IsEditable(const JSCallbackInfo& args)
1277     {
1278         bool flag = false;
1279         if (param_) {
1280             flag = param_->IsEditable();
1281         }
1282         auto jsFlag = JSVal(ToJSValue(flag));
1283         auto descriptionRef = JSRef<JSVal>::Make(jsFlag);
1284         args.SetReturnValue(descriptionRef);
1285     }
1286 
GetEditStateFlags(const JSCallbackInfo & args)1287     void GetEditStateFlags(const JSCallbackInfo& args)
1288     {
1289         int32_t flags = 0;
1290         if (param_) {
1291             flags = param_->GetEditStateFlags();
1292         }
1293         auto jsFlags = JSVal(ToJSValue(flags));
1294         auto descriptionRef = JSRef<JSVal>::Make(jsFlags);
1295         args.SetReturnValue(descriptionRef);
1296     }
1297 
GetSourceType(const JSCallbackInfo & args)1298     void GetSourceType(const JSCallbackInfo& args)
1299     {
1300         int32_t type = 0;
1301         if (param_) {
1302             type = param_->GetSourceType();
1303         }
1304         auto jsType = JSVal(ToJSValue(type));
1305         auto descriptionRef = JSRef<JSVal>::Make(jsType);
1306         args.SetReturnValue(descriptionRef);
1307     }
1308 
GetInputFieldType(const JSCallbackInfo & args)1309     void GetInputFieldType(const JSCallbackInfo& args)
1310     {
1311         int32_t type = 0;
1312         if (param_) {
1313             type = param_->GetInputFieldType();
1314         }
1315         auto jsType = JSVal(ToJSValue(type));
1316         auto descriptionRef = JSRef<JSVal>::Make(jsType);
1317         args.SetReturnValue(descriptionRef);
1318     }
1319 
GetMediaType(const JSCallbackInfo & args)1320     void GetMediaType(const JSCallbackInfo& args)
1321     {
1322         int32_t type = 0;
1323         if (param_) {
1324             type = param_->GetMediaType();
1325         }
1326         auto jsType = JSVal(ToJSValue(type));
1327         auto descriptionRef = JSRef<JSVal>::Make(jsType);
1328         args.SetReturnValue(descriptionRef);
1329     }
1330 
1331 private:
Constructor(const JSCallbackInfo & args)1332     static void Constructor(const JSCallbackInfo& args)
1333     {
1334         auto jSContextMenuParam = Referenced::MakeRefPtr<JSContextMenuParam>();
1335         jSContextMenuParam->IncRefCount();
1336         args.SetReturnValue(Referenced::RawPtr(jSContextMenuParam));
1337     }
1338 
Destructor(JSContextMenuParam * jSContextMenuParam)1339     static void Destructor(JSContextMenuParam* jSContextMenuParam)
1340     {
1341         if (jSContextMenuParam != nullptr) {
1342             jSContextMenuParam->DecRefCount();
1343         }
1344     }
1345 
1346     RefPtr<WebContextMenuParam> param_;
1347 };
1348 
1349 class JSContextMenuResult : public Referenced {
1350 public:
JSBind(BindingTarget globalObj)1351     static void JSBind(BindingTarget globalObj)
1352     {
1353         JSClass<JSContextMenuResult>::Declare("WebContextMenuResult");
1354         JSClass<JSContextMenuResult>::CustomMethod("closeContextMenu", &JSContextMenuResult::Cancel);
1355         JSClass<JSContextMenuResult>::CustomMethod("copyImage", &JSContextMenuResult::CopyImage);
1356         JSClass<JSContextMenuResult>::CustomMethod("copy", &JSContextMenuResult::Copy);
1357         JSClass<JSContextMenuResult>::CustomMethod("paste", &JSContextMenuResult::Paste);
1358         JSClass<JSContextMenuResult>::CustomMethod("cut", &JSContextMenuResult::Cut);
1359         JSClass<JSContextMenuResult>::CustomMethod("selectAll", &JSContextMenuResult::SelectAll);
1360         JSClass<JSContextMenuResult>::Bind(
1361             globalObj, &JSContextMenuResult::Constructor, &JSContextMenuResult::Destructor);
1362     }
1363 
SetResult(const ContextMenuEvent & eventInfo)1364     void SetResult(const ContextMenuEvent& eventInfo)
1365     {
1366         result_ = eventInfo.GetContextMenuResult();
1367     }
1368 
Cancel(const JSCallbackInfo & args)1369     void Cancel(const JSCallbackInfo& args)
1370     {
1371         if (result_) {
1372             result_->Cancel();
1373         }
1374     }
1375 
CopyImage(const JSCallbackInfo & args)1376     void CopyImage(const JSCallbackInfo& args)
1377     {
1378         if (result_) {
1379             result_->CopyImage();
1380         }
1381     }
1382 
Copy(const JSCallbackInfo & args)1383     void Copy(const JSCallbackInfo& args)
1384     {
1385         if (result_) {
1386             result_->Copy();
1387         }
1388     }
1389 
Paste(const JSCallbackInfo & args)1390     void Paste(const JSCallbackInfo& args)
1391     {
1392         if (result_) {
1393             result_->Paste();
1394         }
1395     }
1396 
Cut(const JSCallbackInfo & args)1397     void Cut(const JSCallbackInfo& args)
1398     {
1399         if (result_) {
1400             result_->Cut();
1401         }
1402     }
1403 
SelectAll(const JSCallbackInfo & args)1404     void SelectAll(const JSCallbackInfo& args)
1405     {
1406         if (result_) {
1407             result_->SelectAll();
1408         }
1409     }
1410 
1411 private:
Constructor(const JSCallbackInfo & args)1412     static void Constructor(const JSCallbackInfo& args)
1413     {
1414         auto jsContextMenuResult = Referenced::MakeRefPtr<JSContextMenuResult>();
1415         jsContextMenuResult->IncRefCount();
1416         args.SetReturnValue(Referenced::RawPtr(jsContextMenuResult));
1417     }
1418 
Destructor(JSContextMenuResult * jsContextMenuResult)1419     static void Destructor(JSContextMenuResult* jsContextMenuResult)
1420     {
1421         if (jsContextMenuResult != nullptr) {
1422             jsContextMenuResult->DecRefCount();
1423         }
1424     }
1425 
1426     RefPtr<ContextMenuResult> result_;
1427 };
1428 
JSBind(BindingTarget globalObj)1429 void JSWeb::JSBind(BindingTarget globalObj)
1430 {
1431     JSClass<JSWeb>::Declare("Web");
1432     JSClass<JSWeb>::StaticMethod("create", &JSWeb::Create);
1433     JSClass<JSWeb>::StaticMethod("onAlert", &JSWeb::OnAlert);
1434     JSClass<JSWeb>::StaticMethod("onBeforeUnload", &JSWeb::OnBeforeUnload);
1435     JSClass<JSWeb>::StaticMethod("onConfirm", &JSWeb::OnConfirm);
1436     JSClass<JSWeb>::StaticMethod("onPrompt", &JSWeb::OnPrompt);
1437     JSClass<JSWeb>::StaticMethod("onConsole", &JSWeb::OnConsoleLog);
1438     JSClass<JSWeb>::StaticMethod("onFullScreenEnter", &JSWeb::OnFullScreenEnter);
1439     JSClass<JSWeb>::StaticMethod("onFullScreenExit", &JSWeb::OnFullScreenExit);
1440     JSClass<JSWeb>::StaticMethod("onPageBegin", &JSWeb::OnPageStart);
1441     JSClass<JSWeb>::StaticMethod("onPageEnd", &JSWeb::OnPageFinish);
1442     JSClass<JSWeb>::StaticMethod("onProgressChange", &JSWeb::OnProgressChange);
1443     JSClass<JSWeb>::StaticMethod("onTitleReceive", &JSWeb::OnTitleReceive);
1444     JSClass<JSWeb>::StaticMethod("onGeolocationHide", &JSWeb::OnGeolocationHide);
1445     JSClass<JSWeb>::StaticMethod("onGeolocationShow", &JSWeb::OnGeolocationShow);
1446     JSClass<JSWeb>::StaticMethod("onRequestSelected", &JSWeb::OnRequestFocus);
1447     JSClass<JSWeb>::StaticMethod("onShowFileSelector", &JSWeb::OnFileSelectorShow);
1448     JSClass<JSWeb>::StaticMethod("javaScriptAccess", &JSWeb::JsEnabled);
1449     JSClass<JSWeb>::StaticMethod("fileExtendAccess", &JSWeb::ContentAccessEnabled);
1450     JSClass<JSWeb>::StaticMethod("fileAccess", &JSWeb::FileAccessEnabled);
1451     JSClass<JSWeb>::StaticMethod("onDownloadStart", &JSWeb::OnDownloadStart);
1452     JSClass<JSWeb>::StaticMethod("onErrorReceive", &JSWeb::OnErrorReceive);
1453     JSClass<JSWeb>::StaticMethod("onHttpErrorReceive", &JSWeb::OnHttpErrorReceive);
1454     JSClass<JSWeb>::StaticMethod("onInterceptRequest", &JSWeb::OnInterceptRequest);
1455     JSClass<JSWeb>::StaticMethod("onUrlLoadIntercept", &JSWeb::OnUrlLoadIntercept);
1456     JSClass<JSWeb>::StaticMethod("onlineImageAccess", &JSWeb::OnLineImageAccessEnabled);
1457     JSClass<JSWeb>::StaticMethod("domStorageAccess", &JSWeb::DomStorageAccessEnabled);
1458     JSClass<JSWeb>::StaticMethod("imageAccess", &JSWeb::ImageAccessEnabled);
1459     JSClass<JSWeb>::StaticMethod("mixedMode", &JSWeb::MixedMode);
1460     JSClass<JSWeb>::StaticMethod("zoomAccess", &JSWeb::ZoomAccessEnabled);
1461     JSClass<JSWeb>::StaticMethod("geolocationAccess", &JSWeb::GeolocationAccessEnabled);
1462     JSClass<JSWeb>::StaticMethod("javaScriptProxy", &JSWeb::JavaScriptProxy);
1463     JSClass<JSWeb>::StaticMethod("userAgent", &JSWeb::UserAgent);
1464     JSClass<JSWeb>::StaticMethod("onRenderExited", &JSWeb::OnRenderExited);
1465     JSClass<JSWeb>::StaticMethod("onRefreshAccessedHistory", &JSWeb::OnRefreshAccessedHistory);
1466     JSClass<JSWeb>::StaticMethod("cacheMode", &JSWeb::CacheMode);
1467     JSClass<JSWeb>::StaticMethod("overviewModeAccess", &JSWeb::OverviewModeAccess);
1468     JSClass<JSWeb>::StaticMethod("wideViewModeAccess", &JSWeb::WideViewModeAccess);
1469     JSClass<JSWeb>::StaticMethod("fileFromUrlAccess", &JSWeb::FileFromUrlAccess);
1470     JSClass<JSWeb>::StaticMethod("databaseAccess", &JSWeb::DatabaseAccess);
1471     JSClass<JSWeb>::StaticMethod("textZoomRatio", &JSWeb::TextZoomRatio);
1472     JSClass<JSWeb>::StaticMethod("textZoomAtio", &JSWeb::TextZoomRatio);
1473     JSClass<JSWeb>::StaticMethod("webDebuggingAccess", &JSWeb::WebDebuggingAccessEnabled);
1474     JSClass<JSWeb>::StaticMethod("initialScale", &JSWeb::InitialScale);
1475     JSClass<JSWeb>::StaticMethod("backgroundColor", &JSWeb::BackgroundColor);
1476     JSClass<JSWeb>::StaticMethod("onKeyEvent", &JSWeb::OnKeyEvent);
1477     JSClass<JSWeb>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
1478     JSClass<JSWeb>::StaticMethod("onMouse", &JSWeb::OnMouse);
1479     JSClass<JSWeb>::StaticMethod("onResourceLoad", &JSWeb::OnResourceLoad);
1480     JSClass<JSWeb>::StaticMethod("onScaleChange", &JSWeb::OnScaleChange);
1481     JSClass<JSWeb>::StaticMethod("password", &JSWeb::Password);
1482     JSClass<JSWeb>::StaticMethod("tableData", &JSWeb::TableData);
1483     JSClass<JSWeb>::StaticMethod("onFileSelectorShow", &JSWeb::OnFileSelectorShowAbandoned);
1484     JSClass<JSWeb>::StaticMethod("onHttpAuthRequest", &JSWeb::OnHttpAuthRequest);
1485     JSClass<JSWeb>::StaticMethod("onSslErrorEventReceive", &JSWeb::OnSslErrorRequest);
1486     JSClass<JSWeb>::StaticMethod("onClientAuthenticationRequest", &JSWeb::OnSslSelectCertRequest);
1487     JSClass<JSWeb>::StaticMethod("onPermissionRequest", &JSWeb::OnPermissionRequest);
1488     JSClass<JSWeb>::StaticMethod("onContextMenuShow", &JSWeb::OnContextMenuShow);
1489     JSClass<JSWeb>::StaticMethod("onSearchResultReceive", &JSWeb::OnSearchResultReceive);
1490     JSClass<JSWeb>::StaticMethod("mediaPlayGestureAccess", &JSWeb::MediaPlayGestureAccess);
1491     JSClass<JSWeb>::StaticMethod("onDragStart", &JSWeb::JsOnDragStart);
1492     JSClass<JSWeb>::StaticMethod("onDragEnter", &JSWeb::JsOnDragEnter);
1493     JSClass<JSWeb>::StaticMethod("onDragMove", &JSWeb::JsOnDragMove);
1494     JSClass<JSWeb>::StaticMethod("onDragLeave", &JSWeb::JsOnDragLeave);
1495     JSClass<JSWeb>::StaticMethod("onDrop", &JSWeb::JsOnDrop);
1496     JSClass<JSWeb>::StaticMethod("onScroll", &JSWeb::OnScroll);
1497     JSClass<JSWeb>::StaticMethod("pinchSmooth", &JSWeb::PinchSmoothModeEnabled);
1498     JSClass<JSWeb>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
1499     JSClass<JSWeb>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
1500     JSClass<JSWeb>::StaticMethod("onWindowNew", &JSWeb::OnWindowNew);
1501     JSClass<JSWeb>::StaticMethod("onWindowExit", &JSWeb::OnWindowExit);
1502     JSClass<JSWeb>::StaticMethod("multiWindowAccess", &JSWeb::MultiWindowAccessEnabled);
1503     JSClass<JSWeb>::StaticMethod("webCursiveFont", &JSWeb::WebCursiveFont);
1504     JSClass<JSWeb>::StaticMethod("webFantasyFont", &JSWeb::WebFantasyFont);
1505     JSClass<JSWeb>::StaticMethod("webFixedFont", &JSWeb::WebFixedFont);
1506     JSClass<JSWeb>::StaticMethod("webSansSerifFont", &JSWeb::WebSansSerifFont);
1507     JSClass<JSWeb>::StaticMethod("webSerifFont", &JSWeb::WebSerifFont);
1508     JSClass<JSWeb>::StaticMethod("webStandardFont", &JSWeb::WebStandardFont);
1509     JSClass<JSWeb>::StaticMethod("defaultFixedFontSize", &JSWeb::DefaultFixedFontSize);
1510     JSClass<JSWeb>::StaticMethod("defaultFontSize", &JSWeb::DefaultFontSize);
1511     JSClass<JSWeb>::StaticMethod("minFontSize", &JSWeb::MinFontSize);
1512     JSClass<JSWeb>::StaticMethod("minLogicalFontSize", &JSWeb::MinLogicalFontSize);
1513     JSClass<JSWeb>::StaticMethod("blockNetwork", &JSWeb::BlockNetwork);
1514     JSClass<JSWeb>::StaticMethod("onPageVisible", &JSWeb::OnPageVisible);
1515     JSClass<JSWeb>::StaticMethod("onInterceptKeyEvent", &JSWeb::OnInterceptKeyEvent);
1516     JSClass<JSWeb>::StaticMethod("onDataResubmitted", &JSWeb::OnDataResubmitted);
1517     JSClass<JSWeb>::StaticMethod("onFaviconReceived", &JSWeb::OnFaviconReceived);
1518     JSClass<JSWeb>::StaticMethod("onTouchIconUrlReceived", &JSWeb::OnTouchIconUrlReceived);
1519     JSClass<JSWeb>::StaticMethod("darkMode", &JSWeb::DarkMode);
1520     JSClass<JSWeb>::StaticMethod("forceDarkAccess", &JSWeb::ForceDarkAccess);
1521     JSClass<JSWeb>::StaticMethod("horizontalScrollBarAccess", &JSWeb::HorizontalScrollBarAccess);
1522     JSClass<JSWeb>::StaticMethod("verticalScrollBarAccess", &JSWeb::VerticalScrollBarAccess);
1523     JSClass<JSWeb>::Inherit<JSViewAbstract>();
1524     JSClass<JSWeb>::Bind(globalObj);
1525     JSWebDialog::JSBind(globalObj);
1526     JSWebGeolocation::JSBind(globalObj);
1527     JSWebResourceRequest::JSBind(globalObj);
1528     JSWebResourceError::JSBind(globalObj);
1529     JSWebResourceResponse::JSBind(globalObj);
1530     JSWebConsoleLog::JSBind(globalObj);
1531     JSFileSelectorParam::JSBind(globalObj);
1532     JSFileSelectorResult::JSBind(globalObj);
1533     JSFullScreenExitHandler::JSBind(globalObj);
1534     JSWebHttpAuth::JSBind(globalObj);
1535     JSWebSslError::JSBind(globalObj);
1536     JSWebSslSelectCert::JSBind(globalObj);
1537     JSWebPermissionRequest::JSBind(globalObj);
1538     JSContextMenuParam::JSBind(globalObj);
1539     JSContextMenuResult::JSBind(globalObj);
1540     JSWebWindowNewHandler::JSBind(globalObj);
1541     JSDataResubmitted::JSBind(globalObj);
1542 }
1543 
LoadWebConsoleLogEventToJSValue(const LoadWebConsoleLogEvent & eventInfo)1544 JSRef<JSVal> LoadWebConsoleLogEventToJSValue(const LoadWebConsoleLogEvent& eventInfo)
1545 {
1546     JSRef<JSObject> obj = JSRef<JSObject>::New();
1547 
1548     JSRef<JSObject> messageObj = JSClass<JSWebConsoleLog>::NewInstance();
1549     auto jsWebConsoleLog = Referenced::Claim(messageObj->Unwrap<JSWebConsoleLog>());
1550     jsWebConsoleLog->SetMessage(eventInfo.GetMessage());
1551 
1552     obj->SetPropertyObject("message", messageObj);
1553 
1554     return JSRef<JSVal>::Cast(obj);
1555 }
1556 
WebDialogEventToJSValue(const WebDialogEvent & eventInfo)1557 JSRef<JSVal> WebDialogEventToJSValue(const WebDialogEvent& eventInfo)
1558 {
1559     JSRef<JSObject> obj = JSRef<JSObject>::New();
1560 
1561     JSRef<JSObject> resultObj = JSClass<JSWebDialog>::NewInstance();
1562     auto jsWebDialog = Referenced::Claim(resultObj->Unwrap<JSWebDialog>());
1563     jsWebDialog->SetResult(eventInfo.GetResult());
1564 
1565     obj->SetProperty("url", eventInfo.GetUrl());
1566     obj->SetProperty("message", eventInfo.GetMessage());
1567     if (eventInfo.GetType() == DialogEventType::DIALOG_EVENT_PROMPT) {
1568         obj->SetProperty("value", eventInfo.GetValue());
1569     }
1570     obj->SetPropertyObject("result", resultObj);
1571 
1572     return JSRef<JSVal>::Cast(obj);
1573 }
1574 
LoadWebPageFinishEventToJSValue(const LoadWebPageFinishEvent & eventInfo)1575 JSRef<JSVal> LoadWebPageFinishEventToJSValue(const LoadWebPageFinishEvent& eventInfo)
1576 {
1577     JSRef<JSObject> obj = JSRef<JSObject>::New();
1578     obj->SetProperty("url", eventInfo.GetLoadedUrl());
1579     return JSRef<JSVal>::Cast(obj);
1580 }
1581 
FullScreenEnterEventToJSValue(const FullScreenEnterEvent & eventInfo)1582 JSRef<JSVal> FullScreenEnterEventToJSValue(const FullScreenEnterEvent& eventInfo)
1583 {
1584     JSRef<JSObject> obj = JSRef<JSObject>::New();
1585     JSRef<JSObject> resultObj = JSClass<JSFullScreenExitHandler>::NewInstance();
1586     auto jsFullScreenExitHandler = Referenced::Claim(resultObj->Unwrap<JSFullScreenExitHandler>());
1587     if (!jsFullScreenExitHandler) {
1588         LOGE("jsFullScreenExitHandler is nullptr");
1589         return JSRef<JSVal>::Cast(obj);
1590     }
1591     jsFullScreenExitHandler->SetHandler(eventInfo.GetHandler());
1592 
1593     obj->SetPropertyObject("handler", resultObj);
1594     return JSRef<JSVal>::Cast(obj);
1595 }
1596 
FullScreenExitEventToJSValue(const FullScreenExitEvent & eventInfo)1597 JSRef<JSVal> FullScreenExitEventToJSValue(const FullScreenExitEvent& eventInfo)
1598 {
1599     return JSRef<JSVal>::Make(ToJSValue(eventInfo.IsFullScreen()));
1600 }
1601 
LoadWebPageStartEventToJSValue(const LoadWebPageStartEvent & eventInfo)1602 JSRef<JSVal> LoadWebPageStartEventToJSValue(const LoadWebPageStartEvent& eventInfo)
1603 {
1604     JSRef<JSObject> obj = JSRef<JSObject>::New();
1605     obj->SetProperty("url", eventInfo.GetLoadedUrl());
1606     return JSRef<JSVal>::Cast(obj);
1607 }
1608 
LoadWebProgressChangeEventToJSValue(const LoadWebProgressChangeEvent & eventInfo)1609 JSRef<JSVal> LoadWebProgressChangeEventToJSValue(const LoadWebProgressChangeEvent& eventInfo)
1610 {
1611     JSRef<JSObject> obj = JSRef<JSObject>::New();
1612     obj->SetProperty("newProgress", eventInfo.GetNewProgress());
1613     return JSRef<JSVal>::Cast(obj);
1614 }
1615 
LoadWebTitleReceiveEventToJSValue(const LoadWebTitleReceiveEvent & eventInfo)1616 JSRef<JSVal> LoadWebTitleReceiveEventToJSValue(const LoadWebTitleReceiveEvent& eventInfo)
1617 {
1618     JSRef<JSObject> obj = JSRef<JSObject>::New();
1619     obj->SetProperty("title", eventInfo.GetTitle());
1620     return JSRef<JSVal>::Cast(obj);
1621 }
1622 
UrlLoadInterceptEventToJSValue(const UrlLoadInterceptEvent & eventInfo)1623 JSRef<JSVal> UrlLoadInterceptEventToJSValue(const UrlLoadInterceptEvent& eventInfo)
1624 {
1625     JSRef<JSObject> obj = JSRef<JSObject>::New();
1626     obj->SetProperty("data", eventInfo.GetData());
1627     return JSRef<JSVal>::Cast(obj);
1628 }
1629 
LoadWebGeolocationHideEventToJSValue(const LoadWebGeolocationHideEvent & eventInfo)1630 JSRef<JSVal> LoadWebGeolocationHideEventToJSValue(const LoadWebGeolocationHideEvent& eventInfo)
1631 {
1632     return JSRef<JSVal>::Make(ToJSValue(eventInfo.GetOrigin()));
1633 }
1634 
LoadWebGeolocationShowEventToJSValue(const LoadWebGeolocationShowEvent & eventInfo)1635 JSRef<JSVal> LoadWebGeolocationShowEventToJSValue(const LoadWebGeolocationShowEvent& eventInfo)
1636 {
1637     JSRef<JSObject> obj = JSRef<JSObject>::New();
1638     obj->SetProperty("origin", eventInfo.GetOrigin());
1639     JSRef<JSObject> geolocationObj = JSClass<JSWebGeolocation>::NewInstance();
1640     auto geolocationEvent = Referenced::Claim(geolocationObj->Unwrap<JSWebGeolocation>());
1641     geolocationEvent->SetEvent(eventInfo);
1642     obj->SetPropertyObject("geolocation", geolocationObj);
1643     return JSRef<JSVal>::Cast(obj);
1644 }
1645 
DownloadStartEventToJSValue(const DownloadStartEvent & eventInfo)1646 JSRef<JSVal> DownloadStartEventToJSValue(const DownloadStartEvent& eventInfo)
1647 {
1648     JSRef<JSObject> obj = JSRef<JSObject>::New();
1649     obj->SetProperty("url", eventInfo.GetUrl());
1650     obj->SetProperty("userAgent", eventInfo.GetUserAgent());
1651     obj->SetProperty("contentDisposition", eventInfo.GetContentDisposition());
1652     obj->SetProperty("mimetype", eventInfo.GetMimetype());
1653     obj->SetProperty("contentLength", eventInfo.GetContentLength());
1654     return JSRef<JSVal>::Cast(obj);
1655 }
1656 
LoadWebRequestFocusEventToJSValue(const LoadWebRequestFocusEvent & eventInfo)1657 JSRef<JSVal> LoadWebRequestFocusEventToJSValue(const LoadWebRequestFocusEvent& eventInfo)
1658 {
1659     return JSRef<JSVal>::Make(ToJSValue(eventInfo.GetRequestFocus()));
1660 }
1661 
WebHttpAuthEventToJSValue(const WebHttpAuthEvent & eventInfo)1662 JSRef<JSVal> WebHttpAuthEventToJSValue(const WebHttpAuthEvent& eventInfo)
1663 {
1664     JSRef<JSObject> obj = JSRef<JSObject>::New();
1665     JSRef<JSObject> resultObj = JSClass<JSWebHttpAuth>::NewInstance();
1666     auto jsWebHttpAuth = Referenced::Claim(resultObj->Unwrap<JSWebHttpAuth>());
1667     if (!jsWebHttpAuth) {
1668         LOGE("jsWebHttpAuth is nullptr");
1669         return JSRef<JSVal>::Cast(obj);
1670     }
1671     jsWebHttpAuth->SetResult(eventInfo.GetResult());
1672     obj->SetPropertyObject("handler", resultObj);
1673     obj->SetProperty("host", eventInfo.GetHost());
1674     obj->SetProperty("realm", eventInfo.GetRealm());
1675     return JSRef<JSVal>::Cast(obj);
1676 }
1677 
WebSslErrorEventToJSValue(const WebSslErrorEvent & eventInfo)1678 JSRef<JSVal> WebSslErrorEventToJSValue(const WebSslErrorEvent& eventInfo)
1679 {
1680     JSRef<JSObject> obj = JSRef<JSObject>::New();
1681     JSRef<JSObject> resultObj = JSClass<JSWebSslError>::NewInstance();
1682     auto jsWebSslError = Referenced::Claim(resultObj->Unwrap<JSWebSslError>());
1683     if (!jsWebSslError) {
1684         LOGE("jsWebSslError is nullptr");
1685         return JSRef<JSVal>::Cast(obj);
1686     }
1687     jsWebSslError->SetResult(eventInfo.GetResult());
1688     obj->SetPropertyObject("handler", resultObj);
1689     obj->SetProperty("error", eventInfo.GetError());
1690     return JSRef<JSVal>::Cast(obj);
1691 }
1692 
WebSslSelectCertEventToJSValue(const WebSslSelectCertEvent & eventInfo)1693 JSRef<JSVal> WebSslSelectCertEventToJSValue(const WebSslSelectCertEvent& eventInfo)
1694 {
1695     JSRef<JSObject> obj = JSRef<JSObject>::New();
1696     JSRef<JSObject> resultObj = JSClass<JSWebSslSelectCert>::NewInstance();
1697     auto jsWebSslSelectCert = Referenced::Claim(resultObj->Unwrap<JSWebSslSelectCert>());
1698     if (!jsWebSslSelectCert) {
1699         LOGE("jsWebSslSelectCert is nullptr");
1700         return JSRef<JSVal>::Cast(obj);
1701     }
1702     jsWebSslSelectCert->SetResult(eventInfo.GetResult());
1703     obj->SetPropertyObject("handler", resultObj);
1704     obj->SetProperty("host", eventInfo.GetHost());
1705     obj->SetProperty("port", eventInfo.GetPort());
1706 
1707     JSRef<JSArray> keyTypesArr = JSRef<JSArray>::New();
1708     const std::vector<std::string>& keyTypes = eventInfo.GetKeyTypes();
1709     for (int32_t idx = 0; idx < static_cast<int32_t>(keyTypes.size()); ++idx) {
1710         JSRef<JSVal> keyType = JSRef<JSVal>::Make(ToJSValue(keyTypes[idx]));
1711         keyTypesArr->SetValueAt(idx, keyType);
1712     }
1713     obj->SetPropertyObject("keyTypes", keyTypesArr);
1714 
1715     JSRef<JSArray> issuersArr = JSRef<JSArray>::New();
1716     const std::vector<std::string>& issuers = eventInfo.GetIssuers_();
1717     for (int32_t idx = 0; idx < static_cast<int32_t>(issuers.size()); ++idx) {
1718         JSRef<JSVal> issuer = JSRef<JSVal>::Make(ToJSValue(issuers[idx]));
1719         issuersArr->SetValueAt(idx, issuer);
1720     }
1721 
1722     obj->SetPropertyObject("issuers", issuersArr);
1723 
1724     return JSRef<JSVal>::Cast(obj);
1725 }
1726 
SearchResultReceiveEventToJSValue(const SearchResultReceiveEvent & eventInfo)1727 JSRef<JSVal> SearchResultReceiveEventToJSValue(const SearchResultReceiveEvent& eventInfo)
1728 {
1729     JSRef<JSObject> obj = JSRef<JSObject>::New();
1730     obj->SetProperty("activeMatchOrdinal", eventInfo.GetActiveMatchOrdinal());
1731     obj->SetProperty("numberOfMatches", eventInfo.GetNumberOfMatches());
1732     obj->SetProperty("isDoneCounting", eventInfo.GetIsDoneCounting());
1733     return JSRef<JSVal>::Cast(obj);
1734 }
1735 
Create(const JSCallbackInfo & info)1736 void JSWeb::Create(const JSCallbackInfo& info)
1737 {
1738     if (info.Length() < 1 || !info[0]->IsObject()) {
1739         LOGI("web create error, info is invalid");
1740         return;
1741     }
1742     auto paramObject = JSRef<JSObject>::Cast(info[0]);
1743     JSRef<JSVal> srcValue = paramObject->GetProperty("src");
1744     std::string webSrc;
1745     std::optional<std::string> dstSrc;
1746     if (srcValue->IsString()) {
1747         dstSrc = srcValue->ToString();
1748     } else if (ParseJsMedia(srcValue, webSrc)) {
1749         int np = static_cast<int>(webSrc.find_first_of("/"));
1750         dstSrc = np < 0 ? webSrc : webSrc.erase(np, 1);
1751     }
1752     if (!dstSrc) {
1753         LOGE("Web component failed to parse src");
1754         return;
1755     }
1756     LOGI("JSWeb::Create src:%{public}s", dstSrc->c_str());
1757 
1758     auto controllerObj = paramObject->GetProperty("controller");
1759     if (!controllerObj->IsObject()) {
1760         LOGE("web create error, controllerObj is invalid");
1761         return;
1762     }
1763     auto controller = JSRef<JSObject>::Cast(controllerObj);
1764     auto setWebIdFunction = controller->GetProperty("setWebId");
1765 
1766     if (Container::IsCurrentUseNewPipeline()) {
1767         CreateInNewPipeline(controller, setWebIdFunction, dstSrc);
1768         return;
1769     }
1770     CreateInOldPipeline(controller, setWebIdFunction, dstSrc);
1771 }
1772 
CreateInNewPipeline(JSRef<JSObject> controller,JSRef<JSVal> setWebIdFunction,std::optional<std::string> dstSrc)1773 void JSWeb::CreateInNewPipeline(
1774     JSRef<JSObject> controller, JSRef<JSVal> setWebIdFunction, std::optional<std::string> dstSrc)
1775 {
1776     if (setWebIdFunction->IsFunction()) {
1777         auto setIdCallback = [webviewController = controller, func = JSRef<JSFunc>::Cast(setWebIdFunction)](
1778                                  int32_t webId) {
1779             JSRef<JSVal> argv[] = { JSRef<JSVal>::Make(ToJSValue(webId)) };
1780             func->Call(webviewController, 1, argv);
1781         };
1782         int32_t parentNWebId = -1;
1783         bool isPopup = JSWebWindowNewHandler::ExistController(controller, parentNWebId);
1784         NG::WebView::Create(dstSrc.value(), std::move(setIdCallback),
1785             parentNWebId, isPopup);
1786 
1787         auto getCmdLineFunction = controller->GetProperty("getCustomeSchemeCmdLine");
1788         std::string cmdLine = JSRef<JSFunc>::Cast(getCmdLineFunction)->Call(controller, 0, {})->ToString();
1789         if (!cmdLine.empty()) {
1790             NG::WebView::SetCustomScheme(cmdLine);
1791         }
1792 
1793         auto getWebDebugingFunction = controller->GetProperty("getWebDebuggingAccess");
1794         bool webDebuggingAccess = JSRef<JSFunc>::Cast(getWebDebugingFunction)->Call(controller, 0, {})->ToBoolean();
1795         if (webDebuggingAccess == JSWeb::webDebuggingAccess_) {
1796             LOGI("JS already set debug mode, no need to set again");
1797             return;
1798         }
1799         NG::WebView::SetWebDebuggingAccessEnabled(webDebuggingAccess);
1800         JSWeb::webDebuggingAccess_ = webDebuggingAccess;
1801         return;
1802     }
1803     auto* jsWebController = controller->Unwrap<JSWebController>();
1804     NG::WebView::Create(dstSrc.value(), jsWebController->GetController());
1805 }
1806 
CreateInOldPipeline(JSRef<JSObject> controller,JSRef<JSVal> setWebIdFunction,std::optional<std::string> dstSrc)1807 void JSWeb::CreateInOldPipeline(
1808     JSRef<JSObject> controller, JSRef<JSVal> setWebIdFunction, std::optional<std::string> dstSrc)
1809 {
1810     RefPtr<WebComponent> webComponent;
1811     webComponent = AceType::MakeRefPtr<WebComponent>(dstSrc.value());
1812     webComponent->SetSrc(dstSrc.value());
1813     if (setWebIdFunction->IsFunction()) {
1814         int32_t parentNWebId = -1;
1815         bool isPopup = JSWebWindowNewHandler::ExistController(controller, parentNWebId);
1816         webComponent->SetPopup(isPopup);
1817         webComponent->SetParentNWebId(parentNWebId);
1818         CreateWithWebviewController(controller, setWebIdFunction, webComponent);
1819     } else {
1820         CreateWithWebController(controller, webComponent);
1821     }
1822     ViewStackProcessor::GetInstance()->Push(webComponent);
1823     JSInteractableView::SetFocusable(true);
1824     JSInteractableView::SetFocusNode(true);
1825 }
1826 
CreateWithWebviewController(JSRef<JSObject> controller,JSRef<JSVal> setWebIdFunction,RefPtr<WebComponent> & webComponent)1827 void JSWeb::CreateWithWebviewController(
1828     JSRef<JSObject> controller, JSRef<JSVal> setWebIdFunction, RefPtr<WebComponent>& webComponent)
1829 {
1830     auto setIdCallback = [webviewController = controller, func = JSRef<JSFunc>::Cast(setWebIdFunction)](int32_t webId) {
1831         JSRef<JSVal> argv[] = { JSRef<JSVal>::Make(ToJSValue(webId)) };
1832         func->Call(webviewController, 1, argv);
1833     };
1834     webComponent->SetSetWebIdCallback(std::move(setIdCallback));
1835 
1836     auto getCmdLineFunction = controller->GetProperty("getCustomeSchemeCmdLine");
1837     std::string cmdLine = JSRef<JSFunc>::Cast(getCmdLineFunction)->Call(controller, 0, {})->ToString();
1838     if (!cmdLine.empty()) {
1839         webComponent->SetCustomScheme(cmdLine);
1840     }
1841 
1842     auto getWebDebugingFunction = controller->GetProperty("getWebDebuggingAccess");
1843     bool webDebuggingAccess = JSRef<JSFunc>::Cast(getWebDebugingFunction)->Call(controller, 0, {})->ToBoolean();
1844     if (webDebuggingAccess == JSWeb::webDebuggingAccess_) {
1845         LOGI("JS already set debug mode, no need to set again");
1846         return;
1847     }
1848     webComponent->SetWebDebuggingAccessEnabled(webDebuggingAccess);
1849     JSWeb::webDebuggingAccess_ = webDebuggingAccess;
1850 }
1851 
CreateWithWebController(JSRef<JSObject> controller,RefPtr<WebComponent> & webComponent)1852 void JSWeb::CreateWithWebController(JSRef<JSObject> controller, RefPtr<WebComponent>& webComponent)
1853 {
1854     auto* jsWebController = controller->Unwrap<JSWebController>();
1855     webComponent->SetWebController(jsWebController->GetController());
1856 }
1857 
OnAlert(const JSCallbackInfo & args)1858 void JSWeb::OnAlert(const JSCallbackInfo& args)
1859 {
1860     JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_ALERT);
1861 }
1862 
OnBeforeUnload(const JSCallbackInfo & args)1863 void JSWeb::OnBeforeUnload(const JSCallbackInfo& args)
1864 {
1865     JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_BEFORE_UNLOAD);
1866 }
1867 
OnConfirm(const JSCallbackInfo & args)1868 void JSWeb::OnConfirm(const JSCallbackInfo& args)
1869 {
1870     JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_CONFIRM);
1871 }
1872 
OnPrompt(const JSCallbackInfo & args)1873 void JSWeb::OnPrompt(const JSCallbackInfo& args)
1874 {
1875     JSWeb::OnCommonDialog(args, DialogEventType::DIALOG_EVENT_PROMPT);
1876 }
1877 
OnCommonDialog(const JSCallbackInfo & args,int dialogEventType)1878 void JSWeb::OnCommonDialog(const JSCallbackInfo& args, int dialogEventType)
1879 {
1880     LOGI("OnCommonDialog, event type is %{public}d", dialogEventType);
1881     if (!args[0]->IsFunction()) {
1882         LOGW("param is not funtion.");
1883         return;
1884     }
1885     auto jsFunc =
1886         AceType::MakeRefPtr<JsEventFunction<WebDialogEvent, 1>>(JSRef<JSFunc>::Cast(args[0]), WebDialogEventToJSValue);
1887     if (Container::IsCurrentUseNewPipeline()) {
1888         auto instanceId = Container::CurrentId();
1889         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
1890                               const std::shared_ptr<BaseEventInfo>& info) -> bool {
1891             ContainerScope scope(instanceId);
1892             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
1893             auto* eventInfo = TypeInfoHelper::DynamicCast<WebDialogEvent>(info.get());
1894             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
1895             if (message->IsBoolean()) {
1896                 return message->ToBoolean();
1897             } else {
1898                 return false;
1899             }
1900         };
1901         NG::WebView::SetOnCommonDialogImpl(std::move(uiCallback), static_cast<DialogEventType>(dialogEventType));
1902         return;
1903     }
1904     auto jsCallback = [func = std::move(jsFunc)](const BaseEventInfo* info) -> bool {
1905         ACE_SCORING_EVENT("OnCommonDialog CallBack");
1906         if (func == nullptr) {
1907             LOGW("function is null");
1908             return false;
1909         }
1910         auto eventInfo = TypeInfoHelper::DynamicCast<WebDialogEvent>(info);
1911         JSRef<JSVal> result = func->ExecuteWithValue(*eventInfo);
1912         if (result->IsBoolean()) {
1913             return result->ToBoolean();
1914         }
1915         return false;
1916     };
1917     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
1918     CHECK_NULL_VOID(webComponent);
1919     webComponent->SetOnCommonDialogImpl(std::move(jsCallback), static_cast<DialogEventType>(dialogEventType));
1920 }
1921 
OnConsoleLog(const JSCallbackInfo & args)1922 void JSWeb::OnConsoleLog(const JSCallbackInfo& args)
1923 {
1924     if (!args[0]->IsFunction()) {
1925         return;
1926     }
1927     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebConsoleLogEvent, 1>>(
1928         JSRef<JSFunc>::Cast(args[0]), LoadWebConsoleLogEventToJSValue);
1929     if (Container::IsCurrentUseNewPipeline()) {
1930         auto instanceId = Container::CurrentId();
1931         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
1932                               const std::shared_ptr<BaseEventInfo>& info) -> bool {
1933             ContainerScope scope(instanceId);
1934             auto context = PipelineBase::GetCurrentContext();
1935             CHECK_NULL_RETURN(context, false);
1936             bool result = false;
1937             context->PostSyncEvent([execCtx, func = func, info, &result]() {
1938                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
1939                 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebConsoleLogEvent>(info.get());
1940                 JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
1941                 if (message->IsBoolean()) {
1942                     result = message->ToBoolean();
1943                 } else {
1944                     result = false;
1945                 }
1946             });
1947             return result;
1948         };
1949         NG::WebView::SetOnConsole(std::move(uiCallback));
1950         return;
1951     }
1952     auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](
1953                           const BaseEventInfo* info) -> bool {
1954         auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebConsoleLogEvent>(info);
1955         JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
1956         if (message->IsBoolean()) {
1957             return message->ToBoolean();
1958         }
1959         return false;
1960     };
1961     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
1962     CHECK_NULL_VOID(webComponent);
1963     webComponent->SetOnConsoleImpl(std::move(jsCallback));
1964 }
1965 
OnPageStart(const JSCallbackInfo & args)1966 void JSWeb::OnPageStart(const JSCallbackInfo& args)
1967 {
1968     if (!args[0]->IsFunction()) {
1969         return;
1970     }
1971     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebPageStartEvent, 1>>(
1972         JSRef<JSFunc>::Cast(args[0]), LoadWebPageStartEventToJSValue);
1973 
1974     if (Container::IsCurrentUseNewPipeline()) {
1975         auto instanceId = Container::CurrentId();
1976         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
1977             const std::shared_ptr<BaseEventInfo>& info) {
1978             ContainerScope scope(instanceId);
1979             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
1980             auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebPageStartEvent>(info.get());
1981             func->Execute(*eventInfo);
1982         };
1983         NG::WebView::SetOnPageStart(std::move(uiCallback));
1984         return;
1985     }
1986     auto eventMarker =
1987         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
1988             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
1989             auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebPageStartEvent>(info);
1990             func->Execute(*eventInfo);
1991         });
1992     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
1993     CHECK_NULL_VOID(webComponent);
1994     webComponent->SetPageStartedEventId(eventMarker);
1995 }
1996 
OnPageFinish(const JSCallbackInfo & args)1997 void JSWeb::OnPageFinish(const JSCallbackInfo& args)
1998 {
1999     if (!args[0]->IsFunction()) {
2000         return;
2001     }
2002     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebPageFinishEvent, 1>>(
2003         JSRef<JSFunc>::Cast(args[0]), LoadWebPageFinishEventToJSValue);
2004 
2005     if (Container::IsCurrentUseNewPipeline()) {
2006         auto instanceId = Container::CurrentId();
2007         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2008             const std::shared_ptr<BaseEventInfo>& info) {
2009             ContainerScope scope(instanceId);
2010             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2011             auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebPageFinishEvent>(info.get());
2012             func->Execute(*eventInfo);
2013         };
2014         NG::WebView::SetOnPageFinish(std::move(uiCallback));
2015         return;
2016     }
2017     auto eventMarker =
2018         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2019             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2020             auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebPageFinishEvent>(info);
2021             func->Execute(*eventInfo);
2022         });
2023     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2024     if (!webComponent) {
2025         LOGE("webComponent is null");
2026         return;
2027     }
2028     webComponent->SetPageFinishedEventId(eventMarker);
2029 }
2030 
OnProgressChange(const JSCallbackInfo & args)2031 void JSWeb::OnProgressChange(const JSCallbackInfo& args)
2032 {
2033     if (args.Length() < 1 || !args[0]->IsFunction()) {
2034         return;
2035     }
2036     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebProgressChangeEvent, 1>>(
2037         JSRef<JSFunc>::Cast(args[0]), LoadWebProgressChangeEventToJSValue);
2038     if (Container::IsCurrentUseNewPipeline()) {
2039         auto instanceId = Container::CurrentId();
2040         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2041                               const std::shared_ptr<BaseEventInfo>& info) {
2042             ContainerScope scope(instanceId);
2043             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2044             auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebProgressChangeEvent>(info.get());
2045             func->ExecuteWithValue(*eventInfo);
2046         };
2047         NG::WebView::SetProgressChangeImpl(std::move(uiCallback));
2048         return;
2049     }
2050     auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2051         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2052         auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebProgressChangeEvent>(info);
2053         func->Execute(*eventInfo);
2054     };
2055     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2056     CHECK_NULL_VOID(webComponent);
2057     webComponent->SetProgressChangeImpl(std::move(jsCallback));
2058 }
2059 
OnTitleReceive(const JSCallbackInfo & args)2060 void JSWeb::OnTitleReceive(const JSCallbackInfo& args)
2061 {
2062     if (!args[0]->IsFunction()) {
2063         return;
2064     }
2065     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebTitleReceiveEvent, 1>>(
2066         JSRef<JSFunc>::Cast(args[0]), LoadWebTitleReceiveEventToJSValue);
2067     if (Container::IsCurrentUseNewPipeline()) {
2068         auto instanceId = Container::CurrentId();
2069         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2070             const std::shared_ptr<BaseEventInfo>& info) {
2071             ContainerScope scope(instanceId);
2072             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2073             auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebTitleReceiveEvent>(info.get());
2074             func->Execute(*eventInfo);
2075         };
2076         NG::WebView::SetTitleReceiveEventId(std::move(uiCallback));
2077         return;
2078     }
2079     auto eventMarker =
2080         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2081             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2082             auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebTitleReceiveEvent>(info);
2083             func->Execute(*eventInfo);
2084         });
2085     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2086     CHECK_NULL_VOID(webComponent);
2087     webComponent->SetTitleReceiveEventId(eventMarker);
2088 }
2089 
OnFullScreenExit(const JSCallbackInfo & args)2090 void JSWeb::OnFullScreenExit(const JSCallbackInfo& args)
2091 {
2092     if (!args[0]->IsFunction()) {
2093         LOGE("Param is not a function");
2094         return;
2095     }
2096     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FullScreenExitEvent, 1>>(
2097         JSRef<JSFunc>::Cast(args[0]), FullScreenExitEventToJSValue);
2098     if (Container::IsCurrentUseNewPipeline()) {
2099         auto instanceId = Container::CurrentId();
2100         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2101             const std::shared_ptr<BaseEventInfo>& info) {
2102             ContainerScope scope(instanceId);
2103             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2104             auto* eventInfo = TypeInfoHelper::DynamicCast<FullScreenExitEvent>(info.get());
2105             func->Execute(*eventInfo);
2106         };
2107         NG::WebView::SetFullScreenExitEventId(std::move(uiCallback));
2108         return;
2109     }
2110 
2111     auto eventMarker =
2112         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2113             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2114             auto eventInfo = TypeInfoHelper::DynamicCast<FullScreenExitEvent>(info);
2115             func->Execute(*eventInfo);
2116         });
2117     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2118     CHECK_NULL_VOID(webComponent);
2119     webComponent->SetOnFullScreenExitEventId(eventMarker);
2120 }
2121 
OnFullScreenEnter(const JSCallbackInfo & args)2122 void JSWeb::OnFullScreenEnter(const JSCallbackInfo& args)
2123 {
2124     if (args.Length() < 1 || !args[0]->IsFunction()) {
2125         LOGE("param is invalid");
2126         return;
2127     }
2128     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FullScreenEnterEvent, 1>>(
2129         JSRef<JSFunc>::Cast(args[0]), FullScreenEnterEventToJSValue);
2130     if (Container::IsCurrentUseNewPipeline()) {
2131         auto instanceId = Container::CurrentId();
2132         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2133             const std::shared_ptr<BaseEventInfo>& info) {
2134             ContainerScope scope(instanceId);
2135             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2136             CHECK_NULL_VOID(func);
2137             auto* eventInfo = TypeInfoHelper::DynamicCast<FullScreenEnterEvent>(info.get());
2138             CHECK_NULL_VOID(eventInfo);
2139             func->Execute(*eventInfo);
2140         };
2141         NG::WebView::SetOnFullScreenEnterImpl(std::move(uiCallback));
2142         return;
2143     }
2144     auto jsCallback = [func = std::move(jsFunc)](const BaseEventInfo* info) -> void {
2145         ACE_SCORING_EVENT("OnFullScreenEnter CallBack");
2146         CHECK_NULL_VOID(func);
2147         auto eventInfo = TypeInfoHelper::DynamicCast<FullScreenEnterEvent>(info);
2148         CHECK_NULL_VOID(eventInfo);
2149         func->Execute(*eventInfo);
2150     };
2151     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2152     CHECK_NULL_VOID(webComponent);
2153     webComponent->SetOnFullScreenEnterImpl(std::move(jsCallback));
2154 }
2155 
OnGeolocationHide(const JSCallbackInfo & args)2156 void JSWeb::OnGeolocationHide(const JSCallbackInfo& args)
2157 {
2158     if (!args[0]->IsFunction()) {
2159         return;
2160     }
2161     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebGeolocationHideEvent, 1>>(
2162         JSRef<JSFunc>::Cast(args[0]), LoadWebGeolocationHideEventToJSValue);
2163     if (Container::IsCurrentUseNewPipeline()) {
2164         auto instanceId = Container::CurrentId();
2165         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2166             const std::shared_ptr<BaseEventInfo>& info) {
2167             ContainerScope scope(instanceId);
2168             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2169             auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebGeolocationHideEvent>(info.get());
2170             func->Execute(*eventInfo);
2171         };
2172         NG::WebView::SetGeolocationHideEventId(std::move(uiCallback));
2173         return;
2174     }
2175     auto eventMarker =
2176         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2177             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2178             auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebGeolocationHideEvent>(info);
2179             func->Execute(*eventInfo);
2180         });
2181     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2182     CHECK_NULL_VOID(webComponent);
2183     webComponent->SetGeolocationHideEventId(eventMarker);
2184 }
2185 
OnGeolocationShow(const JSCallbackInfo & args)2186 void JSWeb::OnGeolocationShow(const JSCallbackInfo& args)
2187 {
2188     if (!args[0]->IsFunction()) {
2189         return;
2190     }
2191     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebGeolocationShowEvent, 1>>(
2192         JSRef<JSFunc>::Cast(args[0]), LoadWebGeolocationShowEventToJSValue);
2193     if (Container::IsCurrentUseNewPipeline()) {
2194         auto instanceId = Container::CurrentId();
2195         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2196             const std::shared_ptr<BaseEventInfo>& info) {
2197             ContainerScope scope(instanceId);
2198             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2199             auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebGeolocationShowEvent>(info.get());
2200             func->Execute(*eventInfo);
2201         };
2202         NG::WebView::SetGeolocationShowEventId(std::move(uiCallback));
2203         return;
2204     }
2205     auto eventMarker =
2206         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2207             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2208             auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebGeolocationShowEvent>(info);
2209             func->Execute(*eventInfo);
2210         });
2211     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2212     CHECK_NULL_VOID(webComponent);
2213     webComponent->SetGeolocationShowEventId(eventMarker);
2214 }
2215 
OnRequestFocus(const JSCallbackInfo & args)2216 void JSWeb::OnRequestFocus(const JSCallbackInfo& args)
2217 {
2218     if (!args[0]->IsFunction()) {
2219         return;
2220     }
2221     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<LoadWebRequestFocusEvent, 1>>(
2222         JSRef<JSFunc>::Cast(args[0]), LoadWebRequestFocusEventToJSValue);
2223     if (Container::IsCurrentUseNewPipeline()) {
2224         auto instanceId = Container::CurrentId();
2225         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2226                               const std::shared_ptr<BaseEventInfo>& info) {
2227             ContainerScope scope(instanceId);
2228             auto context = PipelineBase::GetCurrentContext();
2229             CHECK_NULL_VOID(context);
2230             context->PostAsyncEvent([execCtx, func = func, info]() {
2231                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2232                 auto* eventInfo = TypeInfoHelper::DynamicCast<LoadWebRequestFocusEvent>(info.get());
2233                 func->Execute(*eventInfo);
2234             });
2235         };
2236         NG::WebView::SetRequestFocusEventId(std::move(uiCallback));
2237         return;
2238     }
2239     auto eventMarker =
2240         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2241             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2242             auto eventInfo = TypeInfoHelper::DynamicCast<LoadWebRequestFocusEvent>(info);
2243             func->Execute(*eventInfo);
2244         });
2245     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2246     CHECK_NULL_VOID(webComponent);
2247     webComponent->SetRequestFocusEventId(eventMarker);
2248 }
2249 
OnDownloadStart(const JSCallbackInfo & args)2250 void JSWeb::OnDownloadStart(const JSCallbackInfo& args)
2251 {
2252     if (!args[0]->IsFunction()) {
2253         LOGE("Param is invalid");
2254         return;
2255     }
2256     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<DownloadStartEvent, 1>>(
2257         JSRef<JSFunc>::Cast(args[0]), DownloadStartEventToJSValue);
2258     if (Container::IsCurrentUseNewPipeline()) {
2259         auto instanceId = Container::CurrentId();
2260         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2261             const std::shared_ptr<BaseEventInfo>& info) {
2262             ContainerScope scope(instanceId);
2263             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2264             auto* eventInfo = TypeInfoHelper::DynamicCast<DownloadStartEvent>(info.get());
2265             func->Execute(*eventInfo);
2266         };
2267         NG::WebView::SetDownloadStartEventId(std::move(uiCallback));
2268         return;
2269     }
2270     auto eventMarker =
2271         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2272             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2273             auto eventInfo = TypeInfoHelper::DynamicCast<DownloadStartEvent>(info);
2274             func->Execute(*eventInfo);
2275         });
2276     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2277     CHECK_NULL_VOID(webComponent);
2278     webComponent->SetDownloadStartEventId(eventMarker);
2279 }
2280 
OnHttpAuthRequest(const JSCallbackInfo & args)2281 void JSWeb::OnHttpAuthRequest(const JSCallbackInfo& args)
2282 {
2283     if (args.Length() < 1 || !args[0]->IsFunction()) {
2284         LOGE("param is invalid.");
2285         return;
2286     }
2287     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebHttpAuthEvent, 1>>(
2288         JSRef<JSFunc>::Cast(args[0]), WebHttpAuthEventToJSValue);
2289     if (Container::IsCurrentUseNewPipeline()) {
2290         auto instanceId = Container::CurrentId();
2291         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2292             const std::shared_ptr<BaseEventInfo>& info) -> bool {
2293             ContainerScope scope(instanceId);
2294             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2295             auto* eventInfo = TypeInfoHelper::DynamicCast<WebHttpAuthEvent>(info.get());
2296             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2297             if (message->IsBoolean()) {
2298                 return message->ToBoolean();
2299             }
2300             return false;
2301         };
2302         NG::WebView::SetOnHttpAuthRequestImpl(std::move(uiCallback));
2303         return;
2304     }
2305     auto jsCallback = [func = std::move(jsFunc)](const BaseEventInfo* info) -> bool {
2306         ACE_SCORING_EVENT("onHttpAuthRequest CallBack");
2307         if (func == nullptr) {
2308             LOGW("function is null");
2309             return false;
2310         }
2311         auto eventInfo = TypeInfoHelper::DynamicCast<WebHttpAuthEvent>(info);
2312         if (eventInfo == nullptr) {
2313             LOGW("eventInfo is null");
2314             return false;
2315         }
2316         JSRef<JSVal> result = func->ExecuteWithValue(*eventInfo);
2317         if (result->IsBoolean()) {
2318             return result->ToBoolean();
2319         }
2320         return false;
2321     };
2322     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2323     CHECK_NULL_VOID(webComponent);
2324     webComponent->SetOnHttpAuthRequestImpl(std::move(jsCallback));
2325 }
2326 
OnSslErrorRequest(const JSCallbackInfo & args)2327 void JSWeb::OnSslErrorRequest(const JSCallbackInfo& args)
2328 {
2329     if (args.Length() < 1 || !args[0]->IsFunction()) {
2330         LOGE("param is invalid.");
2331         return;
2332     }
2333     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebSslErrorEvent, 1>>(
2334         JSRef<JSFunc>::Cast(args[0]), WebSslErrorEventToJSValue);
2335     if (Container::IsCurrentUseNewPipeline()) {
2336         auto instanceId = Container::CurrentId();
2337         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2338             const std::shared_ptr<BaseEventInfo>& info) -> bool {
2339             ContainerScope scope(instanceId);
2340             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2341             auto* eventInfo = TypeInfoHelper::DynamicCast<WebSslErrorEvent>(info.get());
2342             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2343             if (message->IsBoolean()) {
2344                 return message->ToBoolean();
2345             }
2346             return false;
2347         };
2348         NG::WebView::SetOnSslErrorRequestImpl(std::move(uiCallback));
2349         return;
2350     }
2351     auto jsCallback = [func = std::move(jsFunc)](const BaseEventInfo* info) -> bool {
2352         ACE_SCORING_EVENT("OnSslErrorRequest CallBack");
2353         if (func == nullptr) {
2354             LOGW("function is null");
2355             return false;
2356         }
2357         auto eventInfo = TypeInfoHelper::DynamicCast<WebSslErrorEvent>(info);
2358         if (eventInfo == nullptr) {
2359             LOGW("eventInfo is null");
2360             return false;
2361         }
2362         JSRef<JSVal> result = func->ExecuteWithValue(*eventInfo);
2363         if (result->IsBoolean()) {
2364             return result->ToBoolean();
2365         }
2366         return false;
2367     };
2368     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2369     CHECK_NULL_VOID(webComponent);
2370     webComponent->SetOnSslErrorRequestImpl(std::move(jsCallback));
2371 }
2372 
OnSslSelectCertRequest(const JSCallbackInfo & args)2373 void JSWeb::OnSslSelectCertRequest(const JSCallbackInfo& args)
2374 {
2375     LOGI("JSWeb::OnSslSelectCertRequest");
2376     if (args.Length() < 1 || !args[0]->IsFunction()) {
2377         LOGE("param is invalid.");
2378         return;
2379     }
2380     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebSslSelectCertEvent, 1>>(
2381         JSRef<JSFunc>::Cast(args[0]), WebSslSelectCertEventToJSValue);
2382     if (Container::IsCurrentUseNewPipeline()) {
2383         auto instanceId = Container::CurrentId();
2384         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2385             const std::shared_ptr<BaseEventInfo>& info) -> bool {
2386             ContainerScope scope(instanceId);
2387             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2388             auto* eventInfo = TypeInfoHelper::DynamicCast<WebSslSelectCertEvent>(info.get());
2389             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2390             if (message->IsBoolean()) {
2391                 return message->ToBoolean();
2392             }
2393             return false;
2394         };
2395         NG::WebView::SetOnSslSelectCertRequestImpl(std::move(uiCallback));
2396         return;
2397     }
2398     auto jsCallback = [func = std::move(jsFunc)](const BaseEventInfo* info) -> bool {
2399         ACE_SCORING_EVENT("OnSslSelectCertRequest CallBack");
2400         if (!func) {
2401             LOGW("function is null");
2402             return false;
2403         }
2404         auto eventInfo = TypeInfoHelper::DynamicCast<WebSslSelectCertEvent>(info);
2405         if (!eventInfo) {
2406             LOGW("eventInfo is null");
2407             return false;
2408         }
2409         JSRef<JSVal> result = func->ExecuteWithValue(*eventInfo);
2410         if (result->IsBoolean()) {
2411             return result->ToBoolean();
2412         }
2413         return false;
2414     };
2415     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2416     CHECK_NULL_VOID(webComponent);
2417     webComponent->SetOnSslSelectCertRequestImpl(std::move(jsCallback));
2418 }
2419 
MediaPlayGestureAccess(bool isNeedGestureAccess)2420 void JSWeb::MediaPlayGestureAccess(bool isNeedGestureAccess)
2421 {
2422     if (Container::IsCurrentUseNewPipeline()) {
2423         NG::WebView::SetMediaPlayGestureAccess(isNeedGestureAccess);
2424         return;
2425     }
2426     auto stack = ViewStackProcessor::GetInstance();
2427     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2428     if (!webComponent) {
2429         LOGE("JSWeb: MainComponent is null.");
2430         return;
2431     }
2432     webComponent->SetMediaPlayGestureAccess(isNeedGestureAccess);
2433 }
2434 
OnKeyEvent(const JSCallbackInfo & args)2435 void JSWeb::OnKeyEvent(const JSCallbackInfo& args)
2436 {
2437     LOGI("JSWeb OnKeyEvent");
2438     if (args.Length() < 1 || !args[0]->IsFunction()) {
2439         LOGE("Param is invalid, it is not a function");
2440         return;
2441     }
2442 
2443     RefPtr<JsKeyFunction> jsOnKeyEventFunc = AceType::MakeRefPtr<JsKeyFunction>(JSRef<JSFunc>::Cast(args[0]));
2444     if (Container::IsCurrentUseNewPipeline()) {
2445         auto instanceId = Container::CurrentId();
2446         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsOnKeyEventFunc), instanceId](
2447                               KeyEventInfo& keyEventInfo) {
2448             ContainerScope scope(instanceId);
2449             auto context = PipelineBase::GetCurrentContext();
2450             CHECK_NULL_VOID(context);
2451             context->PostSyncEvent([execCtx, func = func, &keyEventInfo]() {
2452                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2453                 func->Execute(keyEventInfo);
2454             });
2455         };
2456         NG::WebView::SetOnKeyEventCallback(std::move(uiCallback));
2457         return;
2458     }
2459     auto onKeyEventId = [execCtx = args.GetExecutionContext(), func = std::move(jsOnKeyEventFunc)](
2460                             KeyEventInfo& keyEventInfo) {
2461         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2462         ACE_SCORING_EVENT("onKeyEvent");
2463         func->Execute(keyEventInfo);
2464     };
2465 
2466     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2467     CHECK_NULL_VOID(webComponent);
2468     webComponent->SetOnKeyEventCallback(onKeyEventId);
2469 }
2470 
ReceivedErrorEventToJSValue(const ReceivedErrorEvent & eventInfo)2471 JSRef<JSVal> ReceivedErrorEventToJSValue(const ReceivedErrorEvent& eventInfo)
2472 {
2473     JSRef<JSObject> obj = JSRef<JSObject>::New();
2474 
2475     JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
2476     auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
2477     requestEvent->SetErrorEvent(eventInfo);
2478 
2479     JSRef<JSObject> errorObj = JSClass<JSWebResourceError>::NewInstance();
2480     auto errorEvent = Referenced::Claim(errorObj->Unwrap<JSWebResourceError>());
2481     errorEvent->SetEvent(eventInfo);
2482 
2483     obj->SetPropertyObject("request", requestObj);
2484     obj->SetPropertyObject("error", errorObj);
2485 
2486     return JSRef<JSVal>::Cast(obj);
2487 }
2488 
ReceivedHttpErrorEventToJSValue(const ReceivedHttpErrorEvent & eventInfo)2489 JSRef<JSVal> ReceivedHttpErrorEventToJSValue(const ReceivedHttpErrorEvent& eventInfo)
2490 {
2491     JSRef<JSObject> obj = JSRef<JSObject>::New();
2492 
2493     JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
2494     auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
2495     requestEvent->SetHttpErrorEvent(eventInfo);
2496 
2497     JSRef<JSObject> responseObj = JSClass<JSWebResourceResponse>::NewInstance();
2498     auto responseEvent = Referenced::Claim(responseObj->Unwrap<JSWebResourceResponse>());
2499     responseEvent->SetEvent(eventInfo);
2500 
2501     obj->SetPropertyObject("request", requestObj);
2502     obj->SetPropertyObject("response", responseObj);
2503 
2504     return JSRef<JSVal>::Cast(obj);
2505 }
2506 
OnErrorReceive(const JSCallbackInfo & args)2507 void JSWeb::OnErrorReceive(const JSCallbackInfo& args)
2508 {
2509     LOGI("JSWeb OnErrorReceive");
2510     if (!args[0]->IsFunction()) {
2511         LOGE("Param is invalid, it is not a function");
2512         return;
2513     }
2514     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ReceivedErrorEvent, 1>>(
2515         JSRef<JSFunc>::Cast(args[0]), ReceivedErrorEventToJSValue);
2516 
2517     if (Container::IsCurrentUseNewPipeline()) {
2518         auto instanceId = Container::CurrentId();
2519         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2520             const std::shared_ptr<BaseEventInfo>& info) {
2521             ContainerScope scope(instanceId);
2522             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2523             auto* eventInfo = TypeInfoHelper::DynamicCast<ReceivedErrorEvent>(info.get());
2524             func->Execute(*eventInfo);
2525         };
2526         NG::WebView::SetOnErrorReceive(std::move(uiCallback));
2527         return;
2528     }
2529 
2530     auto eventMarker =
2531         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2532             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2533             auto eventInfo = TypeInfoHelper::DynamicCast<ReceivedErrorEvent>(info);
2534             func->Execute(*eventInfo);
2535         });
2536     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2537     CHECK_NULL_VOID(webComponent);
2538     webComponent->SetPageErrorEventId(eventMarker);
2539 }
2540 
OnHttpErrorReceive(const JSCallbackInfo & args)2541 void JSWeb::OnHttpErrorReceive(const JSCallbackInfo& args)
2542 {
2543     LOGI("JSWeb OnHttpErrorReceive");
2544     if (!args[0]->IsFunction()) {
2545         LOGE("Param is invalid, it is not a function");
2546         return;
2547     }
2548     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ReceivedHttpErrorEvent, 1>>(
2549         JSRef<JSFunc>::Cast(args[0]), ReceivedHttpErrorEventToJSValue);
2550 
2551     if (Container::IsCurrentUseNewPipeline()) {
2552         auto instanceId = Container::CurrentId();
2553         auto callback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2554             const std::shared_ptr<BaseEventInfo>& info) {
2555             ContainerScope scope(instanceId);
2556             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2557             auto* eventInfo = TypeInfoHelper::DynamicCast<ReceivedHttpErrorEvent>(info.get());
2558             func->Execute(*eventInfo);
2559         };
2560         NG::WebView::SetOnHttpErrorReceive(std::move(callback));
2561         return;
2562     }
2563 
2564     auto eventMarker =
2565         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
2566             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
2567             auto eventInfo = TypeInfoHelper::DynamicCast<ReceivedHttpErrorEvent>(info);
2568             func->Execute(*eventInfo);
2569         });
2570     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2571     CHECK_NULL_VOID(webComponent);
2572     webComponent->SetHttpErrorEventId(eventMarker);
2573 }
2574 
OnInterceptRequestEventToJSValue(const OnInterceptRequestEvent & eventInfo)2575 JSRef<JSVal> OnInterceptRequestEventToJSValue(const OnInterceptRequestEvent& eventInfo)
2576 {
2577     JSRef<JSObject> obj = JSRef<JSObject>::New();
2578     JSRef<JSObject> requestObj = JSClass<JSWebResourceRequest>::NewInstance();
2579     auto requestEvent = Referenced::Claim(requestObj->Unwrap<JSWebResourceRequest>());
2580     requestEvent->SetOnInterceptRequestEvent(eventInfo);
2581     obj->SetPropertyObject("request", requestObj);
2582     return JSRef<JSVal>::Cast(obj);
2583 }
2584 
OnInterceptRequest(const JSCallbackInfo & args)2585 void JSWeb::OnInterceptRequest(const JSCallbackInfo& args)
2586 {
2587     LOGI("JSWeb OnInterceptRequest");
2588     if ((args.Length() <= 0) || !args[0]->IsFunction()) {
2589         return;
2590     }
2591     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<OnInterceptRequestEvent, 1>>(
2592         JSRef<JSFunc>::Cast(args[0]), OnInterceptRequestEventToJSValue);
2593     if (Container::IsCurrentUseNewPipeline()) {
2594         auto instanceId = Container::CurrentId();
2595         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2596             const std::shared_ptr<BaseEventInfo>& info) -> RefPtr<WebResponse> {
2597             ContainerScope scope(instanceId);
2598             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, nullptr);
2599             auto* eventInfo = TypeInfoHelper::DynamicCast<OnInterceptRequestEvent>(info.get());
2600             JSRef<JSVal> obj = func->ExecuteWithValue(*eventInfo);
2601             if (!obj->IsObject()) {
2602                 LOGI("hap return value is null");
2603                 return nullptr;
2604             }
2605             auto jsResponse = JSRef<JSObject>::Cast(obj)->Unwrap<JSWebResourceResponse>();
2606             if (jsResponse) {
2607                 return jsResponse->GetResponseObj();
2608             }
2609             return nullptr;
2610         };
2611         NG::WebView::SetOnInterceptRequest(std::move(uiCallback));
2612         return;
2613     }
2614     auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](
2615                           const BaseEventInfo* info) -> RefPtr<WebResponse> {
2616         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, nullptr);
2617         auto eventInfo = TypeInfoHelper::DynamicCast<OnInterceptRequestEvent>(info);
2618         if (eventInfo == nullptr) {
2619             return nullptr;
2620         }
2621         JSRef<JSVal> obj = func->ExecuteWithValue(*eventInfo);
2622         if (!obj->IsObject()) {
2623             LOGI("hap return value is null");
2624             return nullptr;
2625         }
2626         auto jsResponse = JSRef<JSObject>::Cast(obj)->Unwrap<JSWebResourceResponse>();
2627         if (jsResponse) {
2628             return jsResponse->GetResponseObj();
2629         }
2630         return nullptr;
2631     };
2632     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2633     CHECK_NULL_VOID(webComponent);
2634     webComponent->SetOnInterceptRequest(std::move(jsCallback));
2635 }
2636 
OnUrlLoadIntercept(const JSCallbackInfo & args)2637 void JSWeb::OnUrlLoadIntercept(const JSCallbackInfo& args)
2638 {
2639     LOGI("JSWeb OnUrlLoadIntercept");
2640     if (!args[0]->IsFunction()) {
2641         LOGE("Param is invalid, it is not a function");
2642         return;
2643     }
2644     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<UrlLoadInterceptEvent, 1>>(
2645         JSRef<JSFunc>::Cast(args[0]), UrlLoadInterceptEventToJSValue);
2646     if (Container::IsCurrentUseNewPipeline()) {
2647         auto instanceId = Container::CurrentId();
2648         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2649             const std::shared_ptr<BaseEventInfo>& info) -> bool {
2650             ContainerScope scope(instanceId);
2651             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2652             auto* eventInfo = TypeInfoHelper::DynamicCast<UrlLoadInterceptEvent>(info.get());
2653             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2654             if (message->IsBoolean()) {
2655                 return message->ToBoolean();
2656             }
2657             return false;
2658         };
2659         NG::WebView::SetOnUrlLoadIntercept(std::move(uiCallback));
2660         return;
2661     }
2662     auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](
2663                           const BaseEventInfo* info) -> bool {
2664         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2665         auto eventInfo = TypeInfoHelper::DynamicCast<UrlLoadInterceptEvent>(info);
2666         JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2667         if (message->IsBoolean()) {
2668             return message->ToBoolean();
2669         }
2670         return false;
2671     };
2672     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2673     CHECK_NULL_VOID(webComponent);
2674     webComponent->SetOnUrlLoadIntercept(std::move(jsCallback));
2675 }
2676 
FileSelectorEventToJSValue(const FileSelectorEvent & eventInfo)2677 JSRef<JSVal> FileSelectorEventToJSValue(const FileSelectorEvent& eventInfo)
2678 {
2679     JSRef<JSObject> obj = JSRef<JSObject>::New();
2680 
2681     JSRef<JSObject> paramObj = JSClass<JSFileSelectorParam>::NewInstance();
2682     auto fileSelectorParam = Referenced::Claim(paramObj->Unwrap<JSFileSelectorParam>());
2683     fileSelectorParam->SetParam(eventInfo);
2684 
2685     JSRef<JSObject> resultObj = JSClass<JSFileSelectorResult>::NewInstance();
2686     auto fileSelectorResult = Referenced::Claim(resultObj->Unwrap<JSFileSelectorResult>());
2687     fileSelectorResult->SetResult(eventInfo);
2688 
2689     obj->SetPropertyObject("result", resultObj);
2690     obj->SetPropertyObject("fileSelector", paramObj);
2691     return JSRef<JSVal>::Cast(obj);
2692 }
2693 
OnFileSelectorShow(const JSCallbackInfo & args)2694 void JSWeb::OnFileSelectorShow(const JSCallbackInfo& args)
2695 {
2696     LOGI("OnFileSelectorShow");
2697     if (!args[0]->IsFunction()) {
2698         return;
2699     }
2700 
2701     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FileSelectorEvent, 1>>(
2702         JSRef<JSFunc>::Cast(args[0]), FileSelectorEventToJSValue);
2703     if (Container::IsCurrentUseNewPipeline()) {
2704         auto instanceId = Container::CurrentId();
2705         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2706                               const std::shared_ptr<BaseEventInfo>& info) -> bool {
2707             ContainerScope scope(instanceId);
2708             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2709             auto* eventInfo = TypeInfoHelper::DynamicCast<FileSelectorEvent>(info.get());
2710             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2711             if (message->IsBoolean()) {
2712                 return message->ToBoolean();
2713             }
2714             return false;
2715         };
2716         NG::WebView::SetOnFileSelectorShow(std::move(uiCallback));
2717         return;
2718     }
2719     auto jsCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](
2720                           const BaseEventInfo* info) -> bool {
2721         ACE_SCORING_EVENT("OnFileSelectorShow CallBack");
2722         if (func == nullptr) {
2723             LOGW("function is null");
2724             return false;
2725         }
2726         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2727         auto eventInfo = TypeInfoHelper::DynamicCast<FileSelectorEvent>(info);
2728         JSRef<JSVal> result = func->ExecuteWithValue(*eventInfo);
2729         if (result->IsBoolean()) {
2730             return result->ToBoolean();
2731         }
2732         return false;
2733     };
2734     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2735     CHECK_NULL_VOID(webComponent);
2736     webComponent->SetOnFileSelectorShow(std::move(jsCallback));
2737 }
2738 
ContextMenuEventToJSValue(const ContextMenuEvent & eventInfo)2739 JSRef<JSVal> ContextMenuEventToJSValue(const ContextMenuEvent& eventInfo)
2740 {
2741     JSRef<JSObject> obj = JSRef<JSObject>::New();
2742 
2743     JSRef<JSObject> paramObj = JSClass<JSContextMenuParam>::NewInstance();
2744     auto contextMenuParam = Referenced::Claim(paramObj->Unwrap<JSContextMenuParam>());
2745     contextMenuParam->SetParam(eventInfo);
2746 
2747     JSRef<JSObject> resultObj = JSClass<JSContextMenuResult>::NewInstance();
2748     auto contextMenuResult = Referenced::Claim(resultObj->Unwrap<JSContextMenuResult>());
2749     contextMenuResult->SetResult(eventInfo);
2750 
2751     obj->SetPropertyObject("result", resultObj);
2752     obj->SetPropertyObject("param", paramObj);
2753     return JSRef<JSVal>::Cast(obj);
2754 }
2755 
OnContextMenuShow(const JSCallbackInfo & args)2756 void JSWeb::OnContextMenuShow(const JSCallbackInfo& args)
2757 {
2758     LOGI("JSWeb: OnContextMenuShow");
2759     if (args.Length() < 1 || !args[0]->IsFunction()) {
2760         LOGE("param is invalid.");
2761         return;
2762     }
2763     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ContextMenuEvent, 1>>(
2764         JSRef<JSFunc>::Cast(args[0]), ContextMenuEventToJSValue);
2765     if (Container::IsCurrentUseNewPipeline()) {
2766         auto instanceId = Container::CurrentId();
2767         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
2768                               const std::shared_ptr<BaseEventInfo>& info) -> bool {
2769             ContainerScope scope(instanceId);
2770             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, false);
2771             auto* eventInfo = TypeInfoHelper::DynamicCast<ContextMenuEvent>(info.get());
2772             JSRef<JSVal> message = func->ExecuteWithValue(*eventInfo);
2773             if (message->IsBoolean()) {
2774                 return message->ToBoolean();
2775             }
2776             return false;
2777         };
2778         NG::WebView::SetOnContextMenuShow(std::move(uiCallback));
2779         return;
2780     }
2781     auto jsCallback = [func = std::move(jsFunc)](const BaseEventInfo* info) -> bool {
2782         ACE_SCORING_EVENT("onContextMenuShow CallBack");
2783         if (func == nullptr) {
2784             LOGW("function is null");
2785             return false;
2786         }
2787         auto eventInfo = TypeInfoHelper::DynamicCast<ContextMenuEvent>(info);
2788         if (eventInfo == nullptr) {
2789             LOGW("eventInfo is null");
2790             return false;
2791         }
2792         JSRef<JSVal> result = func->ExecuteWithValue(*eventInfo);
2793         if (result->IsBoolean()) {
2794             return result->ToBoolean();
2795         }
2796         return false;
2797     };
2798     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
2799     CHECK_NULL_VOID(webComponent);
2800     webComponent->SetOnContextMenuShow(std::move(jsCallback));
2801 }
2802 
JsEnabled(bool isJsEnabled)2803 void JSWeb::JsEnabled(bool isJsEnabled)
2804 {
2805     if (Container::IsCurrentUseNewPipeline()) {
2806         NG::WebView::SetJsEnabled(isJsEnabled);
2807         return;
2808     }
2809     auto stack = ViewStackProcessor::GetInstance();
2810     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2811     if (!webComponent) {
2812         LOGE("JSWeb: MainComponent is null.");
2813         return;
2814     }
2815     webComponent->SetJsEnabled(isJsEnabled);
2816 }
2817 
ContentAccessEnabled(bool isContentAccessEnabled)2818 void JSWeb::ContentAccessEnabled(bool isContentAccessEnabled)
2819 {
2820     auto stack = ViewStackProcessor::GetInstance();
2821     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2822     if (!webComponent) {
2823         LOGE("JSWeb: MainComponent is null.");
2824         return;
2825     }
2826     webComponent->SetContentAccessEnabled(isContentAccessEnabled);
2827 }
2828 
FileAccessEnabled(bool isFileAccessEnabled)2829 void JSWeb::FileAccessEnabled(bool isFileAccessEnabled)
2830 {
2831     if (Container::IsCurrentUseNewPipeline()) {
2832         NG::WebView::SetFileAccessEnabled(isFileAccessEnabled);
2833         return;
2834     }
2835     auto stack = ViewStackProcessor::GetInstance();
2836     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2837     if (!webComponent) {
2838         LOGE("JSWeb: MainComponent is null.");
2839         return;
2840     }
2841     webComponent->SetFileAccessEnabled(isFileAccessEnabled);
2842 }
2843 
OnLineImageAccessEnabled(bool isOnLineImageAccessEnabled)2844 void JSWeb::OnLineImageAccessEnabled(bool isOnLineImageAccessEnabled)
2845 {
2846     if (Container::IsCurrentUseNewPipeline()) {
2847         NG::WebView::SetOnLineImageAccessEnabled(isOnLineImageAccessEnabled);
2848         return;
2849     }
2850     auto stack = ViewStackProcessor::GetInstance();
2851     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2852     if (!webComponent) {
2853         LOGE("JSWeb: MainComponent is null.");
2854         return;
2855     }
2856     webComponent->SetOnLineImageAccessEnabled(!isOnLineImageAccessEnabled);
2857 }
2858 
DomStorageAccessEnabled(bool isDomStorageAccessEnabled)2859 void JSWeb::DomStorageAccessEnabled(bool isDomStorageAccessEnabled)
2860 {
2861     if (Container::IsCurrentUseNewPipeline()) {
2862         NG::WebView::SetDomStorageAccessEnabled(isDomStorageAccessEnabled);
2863         return;
2864     }
2865     auto stack = ViewStackProcessor::GetInstance();
2866     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2867     if (!webComponent) {
2868         LOGE("JSWeb: MainComponent is null.");
2869         return;
2870     }
2871     webComponent->SetDomStorageAccessEnabled(isDomStorageAccessEnabled);
2872 }
2873 
ImageAccessEnabled(bool isImageAccessEnabled)2874 void JSWeb::ImageAccessEnabled(bool isImageAccessEnabled)
2875 {
2876     if (Container::IsCurrentUseNewPipeline()) {
2877         NG::WebView::SetImageAccessEnabled(isImageAccessEnabled);
2878         return;
2879     }
2880     auto stack = ViewStackProcessor::GetInstance();
2881     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2882     if (!webComponent) {
2883         LOGE("JSWeb: MainComponent is null.");
2884         return;
2885     }
2886     webComponent->SetImageAccessEnabled(isImageAccessEnabled);
2887 }
2888 
MixedMode(int32_t mixedMode)2889 void JSWeb::MixedMode(int32_t mixedMode)
2890 {
2891     auto mixedContentMode = MixedModeContent::MIXED_CONTENT_NEVER_ALLOW;
2892     switch (mixedMode) {
2893         case 0:
2894             mixedContentMode = MixedModeContent::MIXED_CONTENT_ALWAYS_ALLOW;
2895             break;
2896         case 1:
2897             mixedContentMode = MixedModeContent::MIXED_CONTENT_COMPATIBILITY_MODE;
2898             break;
2899         default:
2900             mixedContentMode = MixedModeContent::MIXED_CONTENT_NEVER_ALLOW;
2901             break;
2902     }
2903     if (Container::IsCurrentUseNewPipeline()) {
2904         NG::WebView::SetMixedMode(mixedContentMode);
2905         return;
2906     }
2907     auto stack = ViewStackProcessor::GetInstance();
2908     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2909     if (!webComponent) {
2910         LOGE("JSWeb: MainComponent is null.");
2911         return;
2912     }
2913     webComponent->SetMixedMode(mixedContentMode);
2914 }
2915 
ZoomAccessEnabled(bool isZoomAccessEnabled)2916 void JSWeb::ZoomAccessEnabled(bool isZoomAccessEnabled)
2917 {
2918     if (Container::IsCurrentUseNewPipeline()) {
2919         NG::WebView::SetZoomAccessEnabled(isZoomAccessEnabled);
2920         return;
2921     }
2922     auto stack = ViewStackProcessor::GetInstance();
2923     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2924     if (!webComponent) {
2925         LOGE("JSWeb: MainComponent is null.");
2926         return;
2927     }
2928     webComponent->SetZoomAccessEnabled(isZoomAccessEnabled);
2929 }
2930 
GeolocationAccessEnabled(bool isGeolocationAccessEnabled)2931 void JSWeb::GeolocationAccessEnabled(bool isGeolocationAccessEnabled)
2932 {
2933     if (Container::IsCurrentUseNewPipeline()) {
2934         NG::WebView::SetGeolocationAccessEnabled(isGeolocationAccessEnabled);
2935         return;
2936     }
2937     auto stack = ViewStackProcessor::GetInstance();
2938     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2939     if (!webComponent) {
2940         LOGE("JSWeb: MainComponent is null.");
2941         return;
2942     }
2943     webComponent->SetGeolocationAccessEnabled(isGeolocationAccessEnabled);
2944 }
2945 
JavaScriptProxy(const JSCallbackInfo & args)2946 void JSWeb::JavaScriptProxy(const JSCallbackInfo& args)
2947 {
2948     LOGI("JSWeb add js interface");
2949     if (args.Length() < 1 || !args[0]->IsObject()) {
2950         return;
2951     }
2952     auto paramObject = JSRef<JSObject>::Cast(args[0]);
2953     auto controllerObj = paramObject->GetProperty("controller");
2954     auto object = JSRef<JSVal>::Cast(paramObject->GetProperty("object"));
2955     auto name = JSRef<JSVal>::Cast(paramObject->GetProperty("name"));
2956     auto methodList = JSRef<JSVal>::Cast(paramObject->GetProperty("methodList"));
2957     if (!controllerObj->IsObject()) {
2958         LOGE("web create error, controllerObj is invalid");
2959         return;
2960     }
2961     auto controller = JSRef<JSObject>::Cast(controllerObj);
2962     auto jsProxyFunction = controller->GetProperty("jsProxy");
2963     if (jsProxyFunction->IsFunction()) {
2964         LOGI("The controller is WebviewController.");
2965         auto jsProxyCallback = [webviewController = controller, func = JSRef<JSFunc>::Cast(jsProxyFunction), object,
2966                                    name, methodList]() {
2967             JSRef<JSVal> argv[] = { object, name, methodList };
2968             func->Call(webviewController, 3, argv);
2969         };
2970         if (Container::IsCurrentUseNewPipeline()) {
2971             NG::WebView::SetJsProxyCallback(std::move(jsProxyCallback));
2972             return;
2973         }
2974         auto stack = ViewStackProcessor::GetInstance();
2975         auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2976         if (!webComponent) {
2977             LOGE("JSWeb: MainComponent is null.");
2978             return;
2979         }
2980         webComponent->SetJsProxyCallback(std::move(jsProxyCallback));
2981         return;
2982     }
2983     LOGI("The controller is WebController.");
2984     auto jsWebController = controller->Unwrap<JSWebController>();
2985     if (jsWebController) {
2986         jsWebController->SetJavascriptInterface(args);
2987     }
2988 }
2989 
UserAgent(const std::string & userAgent)2990 void JSWeb::UserAgent(const std::string& userAgent)
2991 {
2992     if (Container::IsCurrentUseNewPipeline()) {
2993         NG::WebView::SetUserAgent(userAgent);
2994         return;
2995     }
2996     auto stack = ViewStackProcessor::GetInstance();
2997     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
2998     if (!webComponent) {
2999         LOGE("JSWeb: MainComponent is null.");
3000         return;
3001     }
3002     webComponent->SetUserAgent(userAgent);
3003 }
3004 
RenderExitedEventToJSValue(const RenderExitedEvent & eventInfo)3005 JSRef<JSVal> RenderExitedEventToJSValue(const RenderExitedEvent& eventInfo)
3006 {
3007     JSRef<JSObject> obj = JSRef<JSObject>::New();
3008     obj->SetProperty("renderExitReason", eventInfo.GetExitedReason());
3009     return JSRef<JSVal>::Cast(obj);
3010 }
3011 
RefreshAccessedHistoryEventToJSValue(const RefreshAccessedHistoryEvent & eventInfo)3012 JSRef<JSVal> RefreshAccessedHistoryEventToJSValue(const RefreshAccessedHistoryEvent& eventInfo)
3013 {
3014     JSRef<JSObject> obj = JSRef<JSObject>::New();
3015     obj->SetProperty("url", eventInfo.GetVisitedUrl());
3016     obj->SetProperty("isRefreshed", eventInfo.IsRefreshed());
3017     return JSRef<JSVal>::Cast(obj);
3018 }
3019 
OnRenderExited(const JSCallbackInfo & args)3020 void JSWeb::OnRenderExited(const JSCallbackInfo& args)
3021 {
3022     LOGI("JSWeb OnRenderExited");
3023     if (!args[0]->IsFunction()) {
3024         LOGE("Param is invalid, it is not a function");
3025         return;
3026     }
3027     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<RenderExitedEvent, 1>>(
3028         JSRef<JSFunc>::Cast(args[0]), RenderExitedEventToJSValue);
3029     if (Container::IsCurrentUseNewPipeline()) {
3030         auto instanceId = Container::CurrentId();
3031         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3032             const std::shared_ptr<BaseEventInfo>& info) {
3033             ContainerScope scope(instanceId);
3034             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3035             auto* eventInfo = TypeInfoHelper::DynamicCast<RenderExitedEvent>(info.get());
3036             func->Execute(*eventInfo);
3037         };
3038         NG::WebView::SetRenderExitedId(std::move(uiCallback));
3039         return;
3040     }
3041     auto eventMarker =
3042         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3043             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3044             auto eventInfo = TypeInfoHelper::DynamicCast<RenderExitedEvent>(info);
3045             func->Execute(*eventInfo);
3046         });
3047     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3048     CHECK_NULL_VOID(webComponent);
3049     webComponent->SetRenderExitedId(eventMarker);
3050 }
3051 
OnRefreshAccessedHistory(const JSCallbackInfo & args)3052 void JSWeb::OnRefreshAccessedHistory(const JSCallbackInfo& args)
3053 {
3054     LOGI("JSWeb OnRefreshAccessedHistory");
3055     if (!args[0]->IsFunction()) {
3056         LOGE("Param is invalid, it is not a function");
3057         return;
3058     }
3059     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<RefreshAccessedHistoryEvent, 1>>(
3060         JSRef<JSFunc>::Cast(args[0]), RefreshAccessedHistoryEventToJSValue);
3061     if (Container::IsCurrentUseNewPipeline()) {
3062         auto instanceId = Container::CurrentId();
3063         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3064             const std::shared_ptr<BaseEventInfo>& info) {
3065             ContainerScope scope(instanceId);
3066             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3067             auto* eventInfo = TypeInfoHelper::DynamicCast<RefreshAccessedHistoryEvent>(info.get());
3068             func->Execute(*eventInfo);
3069         };
3070         NG::WebView::SetRefreshAccessedHistoryId(std::move(uiCallback));
3071         return;
3072     }
3073     auto eventMarker =
3074         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3075             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3076             auto eventInfo = TypeInfoHelper::DynamicCast<RefreshAccessedHistoryEvent>(info);
3077             func->Execute(*eventInfo);
3078         });
3079     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3080     CHECK_NULL_VOID(webComponent);
3081     webComponent->SetRefreshAccessedHistoryId(eventMarker);
3082 }
3083 
CacheMode(int32_t cacheMode)3084 void JSWeb::CacheMode(int32_t cacheMode)
3085 {
3086     auto mode = WebCacheMode::DEFAULT;
3087     switch (cacheMode) {
3088         case 0:
3089             mode = WebCacheMode::DEFAULT;
3090             break;
3091         case 1:
3092             mode = WebCacheMode::USE_CACHE_ELSE_NETWORK;
3093             break;
3094         case 2:
3095             mode = WebCacheMode::USE_NO_CACHE;
3096             break;
3097         case 3:
3098             mode = WebCacheMode::USE_CACHE_ONLY;
3099             break;
3100         default:
3101             mode = WebCacheMode::DEFAULT;
3102             break;
3103     }
3104     if (Container::IsCurrentUseNewPipeline()) {
3105         NG::WebView::SetCacheMode(mode);
3106         return;
3107     }
3108     auto stack = ViewStackProcessor::GetInstance();
3109     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3110     if (!webComponent) {
3111         LOGE("JSWeb: MainComponent is null.");
3112         return;
3113     }
3114     webComponent->SetCacheMode(mode);
3115 }
3116 
OverviewModeAccess(bool isOverviewModeAccessEnabled)3117 void JSWeb::OverviewModeAccess(bool isOverviewModeAccessEnabled)
3118 {
3119     if (Container::IsCurrentUseNewPipeline()) {
3120         NG::WebView::SetOverviewModeAccessEnabled(isOverviewModeAccessEnabled);
3121         return;
3122     }
3123     auto stack = ViewStackProcessor::GetInstance();
3124     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3125     if (!webComponent) {
3126         LOGE("JSWeb: MainComponent is null.");
3127         return;
3128     }
3129     webComponent->SetOverviewModeAccessEnabled(isOverviewModeAccessEnabled);
3130 }
3131 
FileFromUrlAccess(bool isFileFromUrlAccessEnabled)3132 void JSWeb::FileFromUrlAccess(bool isFileFromUrlAccessEnabled)
3133 {
3134     if (Container::IsCurrentUseNewPipeline()) {
3135         NG::WebView::SetFileFromUrlAccessEnabled(isFileFromUrlAccessEnabled);
3136         return;
3137     }
3138     auto stack = ViewStackProcessor::GetInstance();
3139     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3140     if (!webComponent) {
3141         LOGE("JSWeb: MainComponent is null.");
3142         return;
3143     }
3144     webComponent->SetFileFromUrlAccessEnabled(isFileFromUrlAccessEnabled);
3145 }
3146 
DatabaseAccess(bool isDatabaseAccessEnabled)3147 void JSWeb::DatabaseAccess(bool isDatabaseAccessEnabled)
3148 {
3149     if (Container::IsCurrentUseNewPipeline()) {
3150         NG::WebView::SetDatabaseAccessEnabled(isDatabaseAccessEnabled);
3151         return;
3152     }
3153     auto stack = ViewStackProcessor::GetInstance();
3154     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3155     if (!webComponent) {
3156         LOGE("JSWeb: MainComponent is null.");
3157         return;
3158     }
3159     webComponent->SetDatabaseAccessEnabled(isDatabaseAccessEnabled);
3160 }
3161 
TextZoomRatio(int32_t textZoomRatioNum)3162 void JSWeb::TextZoomRatio(int32_t textZoomRatioNum)
3163 {
3164     if (Container::IsCurrentUseNewPipeline()) {
3165         NG::WebView::SetTextZoomRatio(textZoomRatioNum);
3166         return;
3167     }
3168     auto stack = ViewStackProcessor::GetInstance();
3169     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3170     if (!webComponent) {
3171         LOGE("JSWeb: MainComponent is null.");
3172         return;
3173     }
3174     webComponent->SetTextZoomRatio(textZoomRatioNum);
3175 }
3176 
WebDebuggingAccessEnabled(bool isWebDebuggingAccessEnabled)3177 void JSWeb::WebDebuggingAccessEnabled(bool isWebDebuggingAccessEnabled)
3178 {
3179     if (Container::IsCurrentUseNewPipeline()) {
3180         NG::WebView::SetWebDebuggingAccessEnabled(isWebDebuggingAccessEnabled);
3181         return;
3182     }
3183     auto stack = ViewStackProcessor::GetInstance();
3184     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3185     if (!webComponent) {
3186         LOGE("JSWeb: MainComponent is null.");
3187         return;
3188     }
3189     webComponent->SetWebDebuggingAccessEnabled(isWebDebuggingAccessEnabled);
3190 }
3191 
OnMouse(const JSCallbackInfo & args)3192 void JSWeb::OnMouse(const JSCallbackInfo& args)
3193 {
3194     LOGI("JSWeb OnMouse");
3195     if (args.Length() < 1 || !args[0]->IsFunction()) {
3196         LOGE("Param is invalid, it is not a function");
3197         return;
3198     }
3199 
3200     RefPtr<JsClickFunction> jsOnMouseFunc = AceType::MakeRefPtr<JsClickFunction>(JSRef<JSFunc>::Cast(args[0]));
3201     if (Container::IsCurrentUseNewPipeline()) {
3202         auto instanceId = Container::CurrentId();
3203         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsOnMouseFunc), instanceId](
3204                               MouseInfo& info) {
3205             ContainerScope scope(instanceId);
3206             auto context = PipelineBase::GetCurrentContext();
3207             CHECK_NULL_VOID(context);
3208             context->PostSyncEvent([execCtx, func = func, &info]() {
3209                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3210                 func->Execute(info);
3211             });
3212         };
3213         NG::WebView::SetOnMouseEventCallback(std::move(uiCallback));
3214         return;
3215     }
3216     auto onMouseId = [execCtx = args.GetExecutionContext(), func = std::move(jsOnMouseFunc)](MouseInfo& info) {
3217         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3218         ACE_SCORING_EVENT("onMouse");
3219         func->Execute(info);
3220     };
3221 
3222     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3223     CHECK_NULL_VOID(webComponent);
3224     webComponent->SetOnMouseEventCallback(onMouseId);
3225 }
3226 
ResourceLoadEventToJSValue(const ResourceLoadEvent & eventInfo)3227 JSRef<JSVal> ResourceLoadEventToJSValue(const ResourceLoadEvent& eventInfo)
3228 {
3229     JSRef<JSObject> obj = JSRef<JSObject>::New();
3230     obj->SetProperty("url", eventInfo.GetOnResourceLoadUrl());
3231     return JSRef<JSVal>::Cast(obj);
3232 }
3233 
OnResourceLoad(const JSCallbackInfo & args)3234 void JSWeb::OnResourceLoad(const JSCallbackInfo& args)
3235 {
3236     LOGI("JSWeb OnRefreshAccessedHistory");
3237     if (args.Length() < 1 || !args[0]->IsFunction()) {
3238         LOGE("Param is invalid, it is not a function");
3239         return;
3240     }
3241     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ResourceLoadEvent, 1>>(
3242         JSRef<JSFunc>::Cast(args[0]), ResourceLoadEventToJSValue);
3243     if (Container::IsCurrentUseNewPipeline()) {
3244         auto instanceId = Container::CurrentId();
3245         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3246             const std::shared_ptr<BaseEventInfo>& info) {
3247             ContainerScope scope(instanceId);
3248             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3249             auto* eventInfo = TypeInfoHelper::DynamicCast<ResourceLoadEvent>(info.get());
3250             func->Execute(*eventInfo);
3251         };
3252         NG::WebView::SetResourceLoadId(std::move(uiCallback));
3253         return;
3254     }
3255     auto eventMarker =
3256         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3257             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3258             auto eventInfo = TypeInfoHelper::DynamicCast<ResourceLoadEvent>(info);
3259             func->Execute(*eventInfo);
3260         });
3261     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3262     CHECK_NULL_VOID(webComponent);
3263     webComponent->SetResourceLoadId(eventMarker);
3264 }
3265 
ScaleChangeEventToJSValue(const ScaleChangeEvent & eventInfo)3266 JSRef<JSVal> ScaleChangeEventToJSValue(const ScaleChangeEvent& eventInfo)
3267 {
3268     JSRef<JSObject> obj = JSRef<JSObject>::New();
3269     obj->SetProperty("oldScale", eventInfo.GetOnScaleChangeOldScale());
3270     obj->SetProperty("newScale", eventInfo.GetOnScaleChangeNewScale());
3271     return JSRef<JSVal>::Cast(obj);
3272 }
3273 
OnScaleChange(const JSCallbackInfo & args)3274 void JSWeb::OnScaleChange(const JSCallbackInfo& args)
3275 {
3276     if (args.Length() < 1 || !args[0]->IsFunction()) {
3277         LOGE("Param is invalid, it is not a function");
3278         return;
3279     }
3280     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<ScaleChangeEvent, 1>>(
3281         JSRef<JSFunc>::Cast(args[0]), ScaleChangeEventToJSValue);
3282     if (Container::IsCurrentUseNewPipeline()) {
3283         auto instanceId = Container::CurrentId();
3284         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3285             const std::shared_ptr<BaseEventInfo>& info) {
3286             ContainerScope scope(instanceId);
3287             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3288             auto* eventInfo = TypeInfoHelper::DynamicCast<ScaleChangeEvent>(info.get());
3289             func->Execute(*eventInfo);
3290         };
3291         NG::WebView::SetScaleChangeId(std::move(uiCallback));
3292         return;
3293     }
3294     auto eventMarker =
3295         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3296             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3297             auto eventInfo = TypeInfoHelper::DynamicCast<ScaleChangeEvent>(info);
3298             func->Execute(*eventInfo);
3299         });
3300     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3301     CHECK_NULL_VOID(webComponent);
3302     webComponent->SetScaleChangeId(eventMarker);
3303 }
3304 
ScrollEventToJSValue(const WebOnScrollEvent & eventInfo)3305 JSRef<JSVal> ScrollEventToJSValue(const WebOnScrollEvent& eventInfo)
3306 {
3307     JSRef<JSObject> obj = JSRef<JSObject>::New();
3308     obj->SetProperty("xOffset", eventInfo.GetX());
3309     obj->SetProperty("yOffset", eventInfo.GetY());
3310     return JSRef<JSVal>::Cast(obj);
3311 }
3312 
OnScroll(const JSCallbackInfo & args)3313 void JSWeb::OnScroll(const JSCallbackInfo& args)
3314 {
3315     if (args.Length() < 1 || !args[0]->IsFunction()) {
3316         LOGE("Param is invalid, it is not a function");
3317         return;
3318     }
3319     auto jsFunc =
3320         AceType::MakeRefPtr<JsEventFunction<WebOnScrollEvent, 1>>(JSRef<JSFunc>::Cast(args[0]), ScrollEventToJSValue);
3321     if (Container::IsCurrentUseNewPipeline()) {
3322         auto instanceId = Container::CurrentId();
3323         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3324             const std::shared_ptr<BaseEventInfo>& info) {
3325             ContainerScope scope(instanceId);
3326             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3327             auto* eventInfo = TypeInfoHelper::DynamicCast<WebOnScrollEvent>(info.get());
3328             func->Execute(*eventInfo);
3329         };
3330         NG::WebView::SetScrollId(std::move(uiCallback));
3331         return;
3332     }
3333     auto eventMarker =
3334         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3335             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3336             auto eventInfo = TypeInfoHelper::DynamicCast<WebOnScrollEvent>(info);
3337             func->Execute(*eventInfo);
3338         });
3339     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3340     CHECK_NULL_VOID(webComponent);
3341     webComponent->SetScrollId(eventMarker);
3342 }
3343 
PermissionRequestEventToJSValue(const WebPermissionRequestEvent & eventInfo)3344 JSRef<JSVal> PermissionRequestEventToJSValue(const WebPermissionRequestEvent& eventInfo)
3345 {
3346     JSRef<JSObject> obj = JSRef<JSObject>::New();
3347     JSRef<JSObject> permissionObj = JSClass<JSWebPermissionRequest>::NewInstance();
3348     auto permissionEvent = Referenced::Claim(permissionObj->Unwrap<JSWebPermissionRequest>());
3349     permissionEvent->SetEvent(eventInfo);
3350     obj->SetPropertyObject("request", permissionObj);
3351     return JSRef<JSVal>::Cast(obj);
3352 }
3353 
OnPermissionRequest(const JSCallbackInfo & args)3354 void JSWeb::OnPermissionRequest(const JSCallbackInfo& args)
3355 {
3356     if (args.Length() < 1 || !args[0]->IsFunction()) {
3357         LOGE("Param is invalid, it is not a function");
3358         return;
3359     }
3360     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebPermissionRequestEvent, 1>>(
3361         JSRef<JSFunc>::Cast(args[0]), PermissionRequestEventToJSValue);
3362     if (Container::IsCurrentUseNewPipeline()) {
3363         auto instanceId = Container::CurrentId();
3364         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3365             const std::shared_ptr<BaseEventInfo>& info) {
3366             ContainerScope scope(instanceId);
3367             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3368             auto* eventInfo = TypeInfoHelper::DynamicCast<WebPermissionRequestEvent>(info.get());
3369             func->Execute(*eventInfo);
3370         };
3371         NG::WebView::SetPermissionRequestEventId(std::move(uiCallback));
3372         return;
3373     }
3374     auto eventMarker =
3375         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3376             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3377             auto eventInfo = TypeInfoHelper::DynamicCast<WebPermissionRequestEvent>(info);
3378             func->Execute(*eventInfo);
3379         });
3380     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3381     CHECK_NULL_VOID(webComponent);
3382     webComponent->SetPermissionRequestEventId(eventMarker);
3383 }
3384 
BackgroundColor(const JSCallbackInfo & info)3385 void JSWeb::BackgroundColor(const JSCallbackInfo& info)
3386 {
3387     if (info.Length() < 1) {
3388         LOGE("The argv is wrong, it is supposed to have at least 1 argument");
3389         return;
3390     }
3391     Color backgroundColor;
3392     if (!ParseJsColor(info[0], backgroundColor)) {
3393         return;
3394     }
3395     if (Container::IsCurrentUseNewPipeline()) {
3396         NG::WebView::SetBackgroundColor(backgroundColor.GetValue());
3397         return;
3398     }
3399     auto stack = ViewStackProcessor::GetInstance();
3400     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3401     if (!webComponent) {
3402         LOGE("JSWeb: MainComponent is null.");
3403         return;
3404     }
3405     webComponent->SetBackgroundColor(backgroundColor.GetValue());
3406 }
3407 
InitialScale(float scale)3408 void JSWeb::InitialScale(float scale)
3409 {
3410     if (Container::IsCurrentUseNewPipeline()) {
3411         NG::WebView::SetInitialScale(scale);
3412         return;
3413     }
3414     auto stack = ViewStackProcessor::GetInstance();
3415     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3416     if (!webComponent) {
3417         LOGE("JSWeb: MainComponent is null.");
3418         return;
3419     }
3420     webComponent->SetInitialScale(scale);
3421 }
3422 
Password(bool password)3423 void JSWeb::Password(bool password)
3424 {
3425     LOGI("JSWeb: Password placeholder");
3426 }
3427 
TableData(bool tableData)3428 void JSWeb::TableData(bool tableData)
3429 {
3430     LOGI("JSWeb: TableData placeholder");
3431 }
3432 
OnFileSelectorShowAbandoned(const JSCallbackInfo & args)3433 void JSWeb::OnFileSelectorShowAbandoned(const JSCallbackInfo& args)
3434 {
3435     LOGI("JSWeb: OnFileSelectorShow Abandoned");
3436 }
3437 
WideViewModeAccess(const JSCallbackInfo & args)3438 void JSWeb::WideViewModeAccess(const JSCallbackInfo& args)
3439 {
3440     LOGI("JSWeb: WideViewModeAccess placeholder");
3441 }
3442 
OnSearchResultReceive(const JSCallbackInfo & args)3443 void JSWeb::OnSearchResultReceive(const JSCallbackInfo& args)
3444 {
3445     if (!args[0]->IsFunction()) {
3446         LOGE("Param is invalid");
3447         return;
3448     }
3449     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<SearchResultReceiveEvent, 1>>(
3450         JSRef<JSFunc>::Cast(args[0]), SearchResultReceiveEventToJSValue);
3451     if (Container::IsCurrentUseNewPipeline()) {
3452         auto instanceId = Container::CurrentId();
3453         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3454             const std::shared_ptr<BaseEventInfo>& info) {
3455             ContainerScope scope(instanceId);
3456             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3457             auto* eventInfo = TypeInfoHelper::DynamicCast<SearchResultReceiveEvent>(info.get());
3458             func->Execute(*eventInfo);
3459         };
3460         NG::WebView::SetSearchResultReceiveEventId(std::move(uiCallback));
3461         return;
3462     }
3463     auto eventMarker =
3464         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3465             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3466             auto eventInfo = TypeInfoHelper::DynamicCast<SearchResultReceiveEvent>(info);
3467             func->Execute(*eventInfo);
3468         });
3469     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3470     CHECK_NULL_VOID(webComponent);
3471     webComponent->SetSearchResultReceiveEventId(eventMarker);
3472 }
3473 
JsOnDragStart(const JSCallbackInfo & info)3474 void JSWeb::JsOnDragStart(const JSCallbackInfo& info)
3475 {
3476     if (Container::IsCurrentUseNewPipeline()) {
3477         JSViewAbstract::JsOnDragStart(info);
3478         return;
3479     }
3480 
3481     RefPtr<JsDragFunction> jsOnDragStartFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3482     auto onDragStartId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragStartFunc)](
3483                              const RefPtr<DragEvent>& info, const std::string& extraParams) -> DragItemInfo {
3484         DragItemInfo itemInfo;
3485         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, itemInfo);
3486 
3487         auto ret = func->Execute(info, extraParams);
3488         if (!ret->IsObject()) {
3489             LOGE("builder param is not an object.");
3490             return itemInfo;
3491         }
3492         auto component = ParseDragNode(ret);
3493         if (component) {
3494             LOGI("use custom builder param.");
3495             itemInfo.customComponent = AceType::DynamicCast<Component>(component);
3496             return itemInfo;
3497         }
3498 
3499         auto builderObj = JSRef<JSObject>::Cast(ret);
3500 #if !defined(WINDOWS_PLATFORM) and !defined(MAC_PLATFORM)
3501         auto pixmap = builderObj->GetProperty("pixelMap");
3502         itemInfo.pixelMap = CreatePixelMapFromNapiValue(pixmap);
3503 #endif
3504         auto extraInfo = builderObj->GetProperty("extraInfo");
3505         ParseJsString(extraInfo, itemInfo.extraInfo);
3506         component = ParseDragNode(builderObj->GetProperty("builder"));
3507         itemInfo.customComponent = AceType::DynamicCast<Component>(component);
3508         return itemInfo;
3509     };
3510     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3511     if (webComponent) {
3512         webComponent->SetOnDragStartId(onDragStartId);
3513     }
3514 }
3515 
JsOnDragEnter(const JSCallbackInfo & info)3516 void JSWeb::JsOnDragEnter(const JSCallbackInfo& info)
3517 {
3518     if (Container::IsCurrentUseNewPipeline()) {
3519         JSViewAbstract::JsOnDragEnter(info);
3520         return;
3521     }
3522 
3523     RefPtr<JsDragFunction> jsOnDragEnterFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3524     auto onDragEnterId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragEnterFunc)](
3525                              const RefPtr<DragEvent>& info, const std::string& extraParams) {
3526         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3527         ACE_SCORING_EVENT("onDragEnter");
3528         func->Execute(info, extraParams);
3529     };
3530     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3531     if (webComponent) {
3532         webComponent->SetOnDragEnterId(onDragEnterId);
3533     }
3534 }
3535 
JsOnDragMove(const JSCallbackInfo & info)3536 void JSWeb::JsOnDragMove(const JSCallbackInfo& info)
3537 {
3538     if (Container::IsCurrentUseNewPipeline()) {
3539         JSViewAbstract::JsOnDragMove(info);
3540         return;
3541     }
3542 
3543     RefPtr<JsDragFunction> jsOnDragMoveFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3544     auto onDragMoveId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragMoveFunc)](
3545                             const RefPtr<DragEvent>& info, const std::string& extraParams) {
3546         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3547         ACE_SCORING_EVENT("onDragMove");
3548         func->Execute(info, extraParams);
3549     };
3550     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3551     if (webComponent) {
3552         webComponent->SetOnDragMoveId(onDragMoveId);
3553     }
3554 }
3555 
JsOnDragLeave(const JSCallbackInfo & info)3556 void JSWeb::JsOnDragLeave(const JSCallbackInfo& info)
3557 {
3558     if (Container::IsCurrentUseNewPipeline()) {
3559         JSViewAbstract::JsOnDragLeave(info);
3560         return;
3561     }
3562 
3563     RefPtr<JsDragFunction> jsOnDragLeaveFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3564     auto onDragLeaveId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDragLeaveFunc)](
3565                              const RefPtr<DragEvent>& info, const std::string& extraParams) {
3566         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3567         ACE_SCORING_EVENT("onDragLeave");
3568         func->Execute(info, extraParams);
3569     };
3570     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3571     if (webComponent) {
3572         webComponent->SetOnDragLeaveId(onDragLeaveId);
3573     }
3574 }
3575 
JsOnDrop(const JSCallbackInfo & info)3576 void JSWeb::JsOnDrop(const JSCallbackInfo& info)
3577 {
3578     if (Container::IsCurrentUseNewPipeline()) {
3579         JSViewAbstract::JsOnDrop(info);
3580         return;
3581     }
3582 
3583     RefPtr<JsDragFunction> jsOnDropFunc = AceType::MakeRefPtr<JsDragFunction>(JSRef<JSFunc>::Cast(info[0]));
3584     auto onDropId = [execCtx = info.GetExecutionContext(), func = std::move(jsOnDropFunc)](
3585                         const RefPtr<DragEvent>& info, const std::string& extraParams) {
3586         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3587         ACE_SCORING_EVENT("onDrop");
3588         func->Execute(info, extraParams);
3589     };
3590     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3591     if (webComponent) {
3592         webComponent->SetOnDropId(onDropId);
3593     }
3594 }
3595 
PinchSmoothModeEnabled(bool isPinchSmoothModeEnabled)3596 void JSWeb::PinchSmoothModeEnabled(bool isPinchSmoothModeEnabled)
3597 {
3598     if (Container::IsCurrentUseNewPipeline()) {
3599         NG::WebView::SetPinchSmoothModeEnabled(isPinchSmoothModeEnabled);
3600         return;
3601     }
3602     auto stack = ViewStackProcessor::GetInstance();
3603     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3604     if (!webComponent) {
3605         LOGE("JSWeb: MainComponent is null.");
3606         return;
3607     }
3608     webComponent->SetPinchSmoothModeEnabled(isPinchSmoothModeEnabled);
3609 }
3610 
WindowNewEventToJSValue(const WebWindowNewEvent & eventInfo)3611 JSRef<JSVal> WindowNewEventToJSValue(const WebWindowNewEvent& eventInfo)
3612 {
3613     JSRef<JSObject> obj = JSRef<JSObject>::New();
3614     obj->SetProperty("isAlert", eventInfo.IsAlert());
3615     obj->SetProperty("isUserTrigger", eventInfo.IsUserTrigger());
3616     obj->SetProperty("targetUrl", eventInfo.GetTargetUrl());
3617     JSRef<JSObject> handlerObj = JSClass<JSWebWindowNewHandler>::NewInstance();
3618     auto handler = Referenced::Claim(handlerObj->Unwrap<JSWebWindowNewHandler>());
3619     handler->SetEvent(eventInfo);
3620     obj->SetPropertyObject("handler", handlerObj);
3621     return JSRef<JSVal>::Cast(obj);
3622 }
3623 
HandleWindowNewEvent(const WebWindowNewEvent * eventInfo)3624 bool HandleWindowNewEvent(const WebWindowNewEvent* eventInfo)
3625 {
3626     LOGI("HandleWindowNewEvent");
3627     if (eventInfo == nullptr) {
3628         LOGE("EventInfo is nullptr");
3629         return false;
3630     }
3631     auto handler = eventInfo->GetWebWindowNewHandler();
3632     if (handler && !handler->IsFrist()) {
3633         auto controller = JSWebWindowNewHandler::PopController(handler->GetId());
3634         if (!controller.IsEmpty()) {
3635             auto getWebIdFunction = controller->GetProperty("innerGetWebId");
3636             if (getWebIdFunction->IsFunction()) {
3637                 auto func = JSRef<JSFunc>::Cast(getWebIdFunction);
3638                 auto webId = func->Call(controller, 0, {});
3639                 handler->SetWebController(webId->ToNumber<int32_t>());
3640             }
3641         }
3642         return false;
3643     }
3644     return true;
3645 }
3646 
OnWindowNew(const JSCallbackInfo & args)3647 void JSWeb::OnWindowNew(const JSCallbackInfo& args)
3648 {
3649     if (args.Length() < 1 || !args[0]->IsFunction()) {
3650         return;
3651     }
3652     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebWindowNewEvent, 1>>(
3653         JSRef<JSFunc>::Cast(args[0]), WindowNewEventToJSValue);
3654     if (Container::IsCurrentUseNewPipeline()) {
3655         auto instanceId = Container::CurrentId();
3656         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3657             const std::shared_ptr<BaseEventInfo>& info) {
3658             ContainerScope scope(instanceId);
3659             auto* eventInfo = TypeInfoHelper::DynamicCast<WebWindowNewEvent>(info.get());
3660             if (!func || !HandleWindowNewEvent(eventInfo)) {
3661                 return;
3662             }
3663             func->Execute(*eventInfo);
3664         };
3665         NG::WebView::SetWindowNewEvent(std::move(uiCallback));
3666         return;
3667     }
3668     auto eventMarker = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](
3669         const std::shared_ptr<BaseEventInfo>& info) {
3670         ACE_SCORING_EVENT("OnWindowNew CallBack");
3671         auto* eventInfo = TypeInfoHelper::DynamicCast<WebWindowNewEvent>(info.get());
3672         if (!func || !HandleWindowNewEvent(eventInfo)) {
3673             return;
3674         }
3675         func->Execute(*eventInfo);
3676     };
3677     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3678     CHECK_NULL_VOID(webComponent);
3679     webComponent->SetWindowNewEvent(eventMarker);
3680 }
3681 
WindowExitEventToJSValue(const WebWindowExitEvent & eventInfo)3682 JSRef<JSVal> WindowExitEventToJSValue(const WebWindowExitEvent& eventInfo)
3683 {
3684     JSRef<JSObject> obj = JSRef<JSObject>::New();
3685     return JSRef<JSVal>::Cast(obj);
3686 }
3687 
OnWindowExit(const JSCallbackInfo & args)3688 void JSWeb::OnWindowExit(const JSCallbackInfo& args)
3689 {
3690     if (args.Length() < 1 || !args[0]->IsFunction()) {
3691         LOGE("Param is invalid, it is not a function");
3692         return;
3693     }
3694     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<WebWindowExitEvent, 1>>(
3695         JSRef<JSFunc>::Cast(args[0]), WindowExitEventToJSValue);
3696     if (Container::IsCurrentUseNewPipeline()) {
3697         auto instanceId = Container::CurrentId();
3698         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3699             const std::shared_ptr<BaseEventInfo>& info) {
3700             ContainerScope scope(instanceId);
3701             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3702             auto* eventInfo = TypeInfoHelper::DynamicCast<WebWindowExitEvent>(info.get());
3703             CHECK_NULL_VOID(func);
3704             func->Execute(*eventInfo);
3705         };
3706         NG::WebView::SetWindowExitEventId(std::move(uiCallback));
3707         return;
3708     }
3709     auto eventMarker =
3710         EventMarker([execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
3711             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3712             auto eventInfo = TypeInfoHelper::DynamicCast<WebWindowExitEvent>(info);
3713             CHECK_NULL_VOID(func);
3714             func->Execute(*eventInfo);
3715         });
3716     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3717     CHECK_NULL_VOID(webComponent);
3718     webComponent->SetWindowExitEventId(eventMarker);
3719 }
3720 
MultiWindowAccessEnabled(bool isMultiWindowAccessEnable)3721 void JSWeb::MultiWindowAccessEnabled(bool isMultiWindowAccessEnable)
3722 {
3723     if (Container::IsCurrentUseNewPipeline()) {
3724         NG::WebView::SetMultiWindowAccessEnabled(isMultiWindowAccessEnable);
3725         return;
3726     }
3727     auto stack = ViewStackProcessor::GetInstance();
3728     auto webComponent = AceType::DynamicCast<WebComponent>(stack->GetMainComponent());
3729     CHECK_NULL_VOID(webComponent);
3730     webComponent->SetMultiWindowAccessEnabled(isMultiWindowAccessEnable);
3731 }
3732 
WebCursiveFont(const std::string & cursiveFontFamily)3733 void JSWeb::WebCursiveFont(const std::string& cursiveFontFamily)
3734 {
3735     if (Container::IsCurrentUseNewPipeline()) {
3736         NG::WebView::SetWebCursiveFont(cursiveFontFamily);
3737         return;
3738     }
3739 }
3740 
WebFantasyFont(const std::string & fantasyFontFamily)3741 void JSWeb::WebFantasyFont(const std::string& fantasyFontFamily)
3742 {
3743     if (Container::IsCurrentUseNewPipeline()) {
3744         NG::WebView::SetWebFantasyFont(fantasyFontFamily);
3745         return;
3746     }
3747 }
3748 
WebFixedFont(const std::string & fixedFontFamily)3749 void JSWeb::WebFixedFont(const std::string& fixedFontFamily)
3750 {
3751     if (Container::IsCurrentUseNewPipeline()) {
3752         NG::WebView::SetWebFixedFont(fixedFontFamily);
3753         return;
3754     }
3755 }
3756 
WebSansSerifFont(const std::string & sansSerifFontFamily)3757 void JSWeb::WebSansSerifFont(const std::string& sansSerifFontFamily)
3758 {
3759     if (Container::IsCurrentUseNewPipeline()) {
3760         NG::WebView::SetWebSansSerifFont(sansSerifFontFamily);
3761         return;
3762     }
3763 }
3764 
WebSerifFont(const std::string & serifFontFamily)3765 void JSWeb::WebSerifFont(const std::string& serifFontFamily)
3766 {
3767     if (Container::IsCurrentUseNewPipeline()) {
3768         NG::WebView::SetWebSerifFont(serifFontFamily);
3769         return;
3770     }
3771 }
3772 
WebStandardFont(const std::string & standardFontFamily)3773 void JSWeb::WebStandardFont(const std::string& standardFontFamily)
3774 {
3775     if (Container::IsCurrentUseNewPipeline()) {
3776         NG::WebView::SetWebStandardFont(standardFontFamily);
3777         return;
3778     }
3779 }
3780 
DefaultFixedFontSize(int32_t defaultFixedFontSize)3781 void JSWeb::DefaultFixedFontSize(int32_t defaultFixedFontSize)
3782 {
3783     if (Container::IsCurrentUseNewPipeline()) {
3784         NG::WebView::SetDefaultFixedFontSize(defaultFixedFontSize);
3785         return;
3786     }
3787 }
3788 
DefaultFontSize(int32_t defaultFontSize)3789 void JSWeb::DefaultFontSize(int32_t defaultFontSize)
3790 {
3791     if (Container::IsCurrentUseNewPipeline()) {
3792         NG::WebView::SetDefaultFontSize(defaultFontSize);
3793         return;
3794     }
3795 }
3796 
MinFontSize(int32_t minFontSize)3797 void JSWeb::MinFontSize(int32_t minFontSize)
3798 {
3799     if (Container::IsCurrentUseNewPipeline()) {
3800         NG::WebView::SetMinFontSize(minFontSize);
3801         return;
3802     }
3803 }
3804 
MinLogicalFontSize(int32_t minLogicalFontSize)3805 void JSWeb::MinLogicalFontSize(int32_t minLogicalFontSize)
3806 {
3807     if (Container::IsCurrentUseNewPipeline()) {
3808         NG::WebView::SetMinLogicalFontSize(minLogicalFontSize);
3809         return;
3810     }
3811 }
3812 
BlockNetwork(bool isNetworkBlocked)3813 void JSWeb::BlockNetwork(bool isNetworkBlocked)
3814 {
3815     if (Container::IsCurrentUseNewPipeline()) {
3816         NG::WebView::SetBlockNetwork(isNetworkBlocked);
3817         return;
3818     }
3819 }
3820 
PageVisibleEventToJSValue(const PageVisibleEvent & eventInfo)3821 JSRef<JSVal> PageVisibleEventToJSValue(const PageVisibleEvent& eventInfo)
3822 {
3823     JSRef<JSObject> obj = JSRef<JSObject>::New();
3824     obj->SetProperty("url", eventInfo.GetUrl());
3825     return JSRef<JSVal>::Cast(obj);
3826 }
3827 
OnPageVisible(const JSCallbackInfo & args)3828 void JSWeb::OnPageVisible(const JSCallbackInfo& args)
3829 {
3830     if (!args[0]->IsFunction()) {
3831         LOGE("Param is invalid, it is not a function");
3832         return;
3833     }
3834     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<PageVisibleEvent, 1>>(
3835         JSRef<JSFunc>::Cast(args[0]), PageVisibleEventToJSValue);
3836     if (Container::IsCurrentUseNewPipeline()) {
3837         auto instanceId = Container::CurrentId();
3838         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3839                               const std::shared_ptr<BaseEventInfo>& info) {
3840             ContainerScope scope(instanceId);
3841             auto context = PipelineBase::GetCurrentContext();
3842             CHECK_NULL_VOID(context);
3843             context->PostAsyncEvent([execCtx, func = func, info]() {
3844                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3845                 auto* eventInfo = TypeInfoHelper::DynamicCast<PageVisibleEvent>(info.get());
3846                 func->Execute(*eventInfo);
3847             });
3848         };
3849         NG::WebView::SetPageVisibleId(std::move(uiCallback));
3850         return;
3851     }
3852 }
3853 
OnInterceptKeyEvent(const JSCallbackInfo & args)3854 void JSWeb::OnInterceptKeyEvent(const JSCallbackInfo& args)
3855 {
3856     LOGI("JSWeb OnInterceptKeyEvent");
3857     if (args.Length() < 1 || !args[0]->IsFunction()) {
3858         LOGE("Param is invalid, it is not a function");
3859         return;
3860     }
3861 
3862     RefPtr<JsKeyFunction> jsOnPreKeyEventFunc = AceType::MakeRefPtr<JsKeyFunction>(JSRef<JSFunc>::Cast(args[0]));
3863     if (Container::IsCurrentUseNewPipeline()) {
3864         auto instanceId = Container::CurrentId();
3865         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsOnPreKeyEventFunc), instanceId](
3866                               KeyEventInfo& keyEventInfo) -> bool {
3867             ContainerScope scope(instanceId);
3868             bool result = false;
3869             auto context = PipelineBase::GetCurrentContext();
3870             CHECK_NULL_RETURN(context, result);
3871             context->PostSyncEvent([execCtx, func = func, &keyEventInfo, &result]() {
3872                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3873                 JSRef<JSVal> obj = func->ExecuteWithValue(keyEventInfo);
3874                 if (obj->IsBoolean()) {
3875                     result = obj->ToBoolean();
3876                 }
3877             });
3878             return result;
3879         };
3880         NG::WebView::SetOnInterceptKeyEventCallback(std::move(uiCallback));
3881         return;
3882     }
3883     auto onKeyEventId = [execCtx = args.GetExecutionContext(), func = std::move(jsOnPreKeyEventFunc)](
3884                             KeyEventInfo& keyEventInfo) -> bool {
3885         bool result = false;
3886         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx, result);
3887         ACE_SCORING_EVENT("onPreKeyEvent");
3888         JSRef<JSVal> obj = func->ExecuteWithValue(keyEventInfo);
3889         if (obj->IsBoolean()) {
3890             result = obj->ToBoolean();
3891         }
3892         return result;
3893     };
3894 
3895     auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
3896     CHECK_NULL_VOID(webComponent);
3897     webComponent->SetOnInterceptKeyEventCallback(onKeyEventId);
3898 }
3899 
DataResubmittedEventToJSValue(const DataResubmittedEvent & eventInfo)3900 JSRef<JSVal> DataResubmittedEventToJSValue(const DataResubmittedEvent& eventInfo)
3901 {
3902     JSRef<JSObject> obj = JSRef<JSObject>::New();
3903     JSRef<JSObject> resultObj = JSClass<JSDataResubmitted>::NewInstance();
3904     auto jsDataResubmitted = Referenced::Claim(resultObj->Unwrap<JSDataResubmitted>());
3905     if (!jsDataResubmitted) {
3906         LOGE("jsDataResubmitted is nullptr");
3907         return JSRef<JSVal>::Cast(obj);
3908     }
3909     jsDataResubmitted->SetHandler(eventInfo.GetHandler());
3910     obj->SetPropertyObject("handler", resultObj);
3911     return JSRef<JSVal>::Cast(obj);
3912 }
3913 
OnDataResubmitted(const JSCallbackInfo & args)3914 void JSWeb::OnDataResubmitted(const JSCallbackInfo& args)
3915 {
3916     if (args.Length() < 1 || !args[0]->IsFunction()) {
3917         LOGE("Param is invalid, it is not a function");
3918         return;
3919     }
3920     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<DataResubmittedEvent, 1>>(
3921         JSRef<JSFunc>::Cast(args[0]), DataResubmittedEventToJSValue);
3922     if (Container::IsCurrentUseNewPipeline()) {
3923         auto instanceId = Container::CurrentId();
3924         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
3925                               const std::shared_ptr<BaseEventInfo>& info) {
3926             ContainerScope scope(instanceId);
3927             auto context = PipelineBase::GetCurrentContext();
3928             CHECK_NULL_VOID(context);
3929             context->PostSyncEvent([execCtx, func = func, info]() {
3930                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
3931                 auto* eventInfo = TypeInfoHelper::DynamicCast<DataResubmittedEvent>(info.get());
3932                 func->Execute(*eventInfo);
3933             });
3934         };
3935         NG::WebView::SetDataResubmittedId(std::move(uiCallback));
3936         return;
3937     }
3938 }
3939 
GetPixelFormat(NWeb::ImageColorType colorType)3940 Media::PixelFormat GetPixelFormat(NWeb::ImageColorType colorType)
3941 {
3942     Media::PixelFormat pixelFormat;
3943     switch (colorType) {
3944         case NWeb::ImageColorType::COLOR_TYPE_UNKNOWN:
3945             pixelFormat = Media::PixelFormat::UNKNOWN;
3946             break;
3947         case NWeb::ImageColorType::COLOR_TYPE_RGBA_8888:
3948             pixelFormat = Media::PixelFormat::RGBA_8888;
3949             break;
3950         case NWeb::ImageColorType::COLOR_TYPE_BGRA_8888:
3951             pixelFormat = Media::PixelFormat::BGRA_8888;
3952             break;
3953         default:
3954             pixelFormat = Media::PixelFormat::UNKNOWN;
3955             break;
3956     }
3957     return pixelFormat;
3958 }
3959 
GetAlphaType(NWeb::ImageAlphaType alphaType)3960 Media::AlphaType GetAlphaType(NWeb::ImageAlphaType alphaType)
3961 {
3962     Media::AlphaType imageAlphaType;
3963     switch (alphaType) {
3964         case NWeb::ImageAlphaType::ALPHA_TYPE_UNKNOWN:
3965             imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
3966             break;
3967         case NWeb::ImageAlphaType::ALPHA_TYPE_OPAQUE:
3968             imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
3969             break;
3970         case NWeb::ImageAlphaType::ALPHA_TYPE_PREMULTIPLIED:
3971             imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_PREMUL;
3972             break;
3973         case NWeb::ImageAlphaType::ALPHA_TYPE_POSTMULTIPLIED:
3974             imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNPREMUL;
3975             break;
3976         default:
3977             imageAlphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_UNKNOWN;
3978             break;
3979     }
3980     return imageAlphaType;
3981 }
3982 
FaviconReceivedEventToJSValue(const FaviconReceivedEvent & eventInfo)3983 JSRef<JSObject> FaviconReceivedEventToJSValue(const FaviconReceivedEvent& eventInfo)
3984 {
3985     JSRef<JSObject> obj = JSRef<JSObject>::New();
3986     auto data = eventInfo.GetHandler()->GetData();
3987     size_t width = eventInfo.GetHandler()->GetWidth();
3988     size_t height = eventInfo.GetHandler()->GetHeight();
3989     int colorType = eventInfo.GetHandler()->GetColorType();
3990     int alphaType = eventInfo.GetHandler()->GetAlphaType();
3991 
3992     Media::InitializationOptions opt;
3993     opt.size.width = static_cast<int32_t>(width);
3994     opt.size.height = static_cast<int32_t>(height);
3995     opt.pixelFormat = GetPixelFormat(NWeb::ImageColorType(colorType));
3996     opt.alphaType = GetAlphaType(NWeb::ImageAlphaType(alphaType));
3997     opt.editable = true;
3998     auto pixelMap = Media::PixelMap::Create(opt);
3999     if (pixelMap == nullptr) {
4000         LOGE("pixelMap is null");
4001         return JSRef<JSVal>::Cast(obj);
4002     }
4003     uint32_t stride = width << 2;
4004     uint64_t bufferSize = stride * height;
4005     pixelMap->WritePixels(static_cast<const uint8_t *>(data), bufferSize);
4006     std::shared_ptr<Media::PixelMap> pixelMapToJs(pixelMap.release());
4007     auto engine = EngineHelper::GetCurrentEngine();
4008     if (!engine) {
4009         LOGE("engine is null");
4010         return JSRef<JSVal>::Cast(obj);
4011     }
4012     NativeEngine* nativeEngine = engine->GetNativeEngine();
4013     napi_env env = reinterpret_cast<napi_env>(nativeEngine);
4014     napi_value napiValue = OHOS::Media::PixelMapNapi::CreatePixelMap(env, pixelMapToJs);
4015     NativeValue* nativeValue = reinterpret_cast<NativeValue*>(napiValue);
4016     (void)nativeValue;
4017 #ifdef USE_ARK_ENGINE
4018     auto jsPixelMap = JsConverter::ConvertNativeValueToJsVal(nativeValue);
4019     obj->SetPropertyObject("favicon", jsPixelMap);
4020 #endif
4021     return JSRef<JSObject>::Cast(obj);
4022 }
4023 
OnFaviconReceived(const JSCallbackInfo & args)4024 void JSWeb::OnFaviconReceived(const JSCallbackInfo& args)
4025 {
4026     if (args.Length() < 1 || !args[0]->IsFunction()) {
4027         LOGE("Param is invalid, it is not a function");
4028         return;
4029     }
4030     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<FaviconReceivedEvent, 1>>(
4031         JSRef<JSFunc>::Cast(args[0]), FaviconReceivedEventToJSValue);
4032     if (Container::IsCurrentUseNewPipeline()) {
4033         auto instanceId = Container::CurrentId();
4034         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
4035                               const std::shared_ptr<BaseEventInfo>& info) {
4036             ContainerScope scope(instanceId);
4037             auto context = PipelineBase::GetCurrentContext();
4038             CHECK_NULL_VOID(context);
4039             context->PostSyncEvent([execCtx, func = func, info]() {
4040                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4041                 auto* eventInfo = TypeInfoHelper::DynamicCast<FaviconReceivedEvent>(info.get());
4042                 func->Execute(*eventInfo);
4043             });
4044         };
4045         NG::WebView::SetFaviconReceivedId(std::move(uiCallback));
4046         return;
4047     }
4048 }
4049 
TouchIconUrlEventToJSValue(const TouchIconUrlEvent & eventInfo)4050 JSRef<JSVal> TouchIconUrlEventToJSValue(const TouchIconUrlEvent& eventInfo)
4051 {
4052     JSRef<JSObject> obj = JSRef<JSObject>::New();
4053     obj->SetProperty("url", eventInfo.GetUrl());
4054     obj->SetProperty("precomposed", eventInfo.GetPreComposed());
4055     return JSRef<JSVal>::Cast(obj);
4056 }
4057 
OnTouchIconUrlReceived(const JSCallbackInfo & args)4058 void JSWeb::OnTouchIconUrlReceived(const JSCallbackInfo& args)
4059 {
4060     if (!args[0]->IsFunction()) {
4061         LOGE("Param is invalid, it is not a function");
4062         return;
4063     }
4064     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<TouchIconUrlEvent, 1>>(
4065         JSRef<JSFunc>::Cast(args[0]), TouchIconUrlEventToJSValue);
4066     if (Container::IsCurrentUseNewPipeline()) {
4067         auto instanceId = Container::CurrentId();
4068         auto uiCallback = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc), instanceId](
4069                               const std::shared_ptr<BaseEventInfo>& info) {
4070             ContainerScope scope(instanceId);
4071             auto context = PipelineBase::GetCurrentContext();
4072             CHECK_NULL_VOID(context);
4073             context->PostAsyncEvent([execCtx, func = func, info]() {
4074                 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
4075                 auto* eventInfo = TypeInfoHelper::DynamicCast<TouchIconUrlEvent>(info.get());
4076                 func->Execute(*eventInfo);
4077             });
4078         };
4079         NG::WebView::SetTouchIconUrlId(std::move(uiCallback));
4080         return;
4081     }
4082 }
4083 
DarkMode(int32_t darkMode)4084 void JSWeb::DarkMode(int32_t darkMode)
4085 {
4086     auto mode = WebDarkMode::Off;
4087     switch (darkMode) {
4088         case 0:
4089             mode = WebDarkMode::Off;
4090             break;
4091         case 1:
4092             mode = WebDarkMode::On;
4093             break;
4094         case 2:
4095             mode = WebDarkMode::Auto;
4096             break;
4097         default:
4098             mode = WebDarkMode::Off;
4099             break;
4100     }
4101     if (Container::IsCurrentUseNewPipeline()) {
4102         NG::WebView::SetDarkMode(mode);
4103         return;
4104     }
4105 }
4106 
ForceDarkAccess(bool access)4107 void JSWeb::ForceDarkAccess(bool access)
4108 {
4109     if (Container::IsCurrentUseNewPipeline()) {
4110         NG::WebView::SetForceDarkAccess(access);
4111         return;
4112     }
4113 }
4114 
HorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled)4115 void JSWeb::HorizontalScrollBarAccess(bool isHorizontalScrollBarAccessEnabled)
4116 {
4117     if (Container::IsCurrentUseNewPipeline()) {
4118         NG::WebView::SetHorizontalScrollBarAccessEnabled(isHorizontalScrollBarAccessEnabled);
4119     }
4120 }
4121 
VerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled)4122 void JSWeb::VerticalScrollBarAccess(bool isVerticalScrollBarAccessEnabled)
4123 {
4124     if (Container::IsCurrentUseNewPipeline()) {
4125         NG::WebView::SetVerticalScrollBarAccessEnabled(isVerticalScrollBarAccessEnabled);
4126     }
4127 }
4128 } // namespace OHOS::Ace::Framework
4129