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