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