1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ARK_WEB_HANDLER_WRAPPER_H_
17 #define ARK_WEB_HANDLER_WRAPPER_H_
18 #pragma once
19 
20 #include "include/nweb_handler.h"
21 #include "ohos_nweb/include/ark_web_handler.h"
22 
23 namespace OHOS::ArkWeb {
24 
25 using ArkWebSslError = OHOS::NWeb::SslError;
26 using ArkWebCursorType = OHOS::NWeb::CursorType;
27 using ArkWebActivityType = OHOS::NWeb::ActivityType;
28 using ArkWebImageColorType = OHOS::NWeb::ImageColorType;
29 using ArkWebImageAlphaType = OHOS::NWeb::ImageAlphaType;
30 using ArkWebRenderExitReason = OHOS::NWeb::RenderExitReason;
31 using ArkWebDragOperation = OHOS::NWeb::NWebDragData::DragOperation;
32 using ArkWebRenderProcessNotRespondingReason = OHOS::NWeb::RenderProcessNotRespondingReason;
33 using ArkWebViewportFit = OHOS::NWeb::ViewportFit;
34 using ArkWebFocusSource = OHOS::NWeb::NWebFocusSource;
35 
36 class ArkWebHandlerWrapper : public OHOS::NWeb::NWebHandler {
37 public:
38     ArkWebHandlerWrapper(ArkWebRefPtr<ArkWebHandler> ark_web_handler);
39     ~ArkWebHandlerWrapper() = default;
40 
41     void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override;
42 
43     /**
44      * @brief Request display and focus for a new nweb.
45      *
46      */
47     bool OnFocus() override;
48 
49     void OnMessage(const std::string& param) override;
50 
51     /**
52      * @brief Notify the SDK that the nweb will load the resource specified by the
53      *        given url.
54      *
55      * @param url The url of the resource.
56      */
57     void OnResource(const std::string& url) override;
58 
59     /**
60      * @brief Notify the SDK of a new favicon for the current web site.
61      *
62      * @param data The raw image data for the icon.
63      * @param width The width of the icon in pixel.
64      * @param height The height of the icon in pixel.
65      * @param color_type The color data encoding type.
66      * @param alpha_type The alpha value of any pixel.
67      */
68     void OnPageIcon(const void* data, size_t width, size_t height, ArkWebImageColorType color_type,
69         ArkWebImageAlphaType alpha_type) override;
70 
71     /**
72      * @brief Notify the SDK of the changed document title.
73      *
74      * @param title The document title.
75      */
76     void OnPageTitle(const std::string& title) override;
77 
78     void OnProxyDied() override;
79 
80     /**
81      * @brief Notify the SDK that an HTTP error has been received from the server
82      *        while loading a resource.
83      *
84      * @param request The request information.
85      * @param response The error occurred.
86      */
87     void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
88         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
89 
90     /**
91      * @brief Report a JavaScript console message to the host application.
92      *
93      * @param console_log Details of the console message.
94      *
95      * @return Return true to stop the message from being output to the console.
96      */
97     bool OnConsoleLog(std::shared_ptr<OHOS::NWeb::NWebConsoleLog> console_log) override;
98 
99     void OnRouterPush(const std::string& param) override;
100 
101     /**
102      * @brief Notify the SDK that a web site has finished loading. This method is
103      *        called only for main frame.
104      *
105      * @param http_status_code The status code for the http request.
106      * @param url The url of the web site.
107      */
108     void OnPageLoadEnd(int http_status_code, const std::string& url) override;
109 
110     /**
111      * @brief Notify the SDK that a web site has started loading. This method is
112      *        called once for each main frame load.
113      *
114      * @param url The url to be loaded.
115      */
116     void OnPageLoadBegin(const std::string& url) override;
117 
118     /**
119      * @brief Report a load error to the SDK.
120      *
121      * @param error_code The error code.
122      * @param description The error description.
123      * @param url The failed url.
124      */
125     void OnPageLoadError(int error_code, const std::string& description, const std::string& url) override;
126 
127     /**
128      * @brief Notify the SDK of the url for a touch icon.
129      *
130      * @param icon_url The icon url.
131      * @param precomposed The touch icon type.
132      */
133     void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) override;
134 
135     /**
136      * @brief Notify the SDK the current progress of loading a web site.
137      *
138      * @param new_progress Loading progress, an integer between 0 and 100.
139      */
140     void OnLoadingProgress(int new_progress) override;
141 
142     /**
143      * @brief Show prompt to ask for the geo location permission.
144      *
145      * @param origin String: the origin of the resource to get geo location
146      * @param callback GeolocationCallbackInterface: callback to report geo
147      *        location
148      */
149     void OnGeolocationShow(
150         const std::string& origin, std::shared_ptr<OHOS::NWeb::NWebGeolocationCallbackInterface> callback) override;
151 
152     /**
153      * @brief Hide prompt to ask for the geolocation permission.
154      */
155     void OnGeolocationHide() override;
156 
157     /**
158      * @brief inform application to show a file selector.
159      *
160      * @param callback the file list to select.
161      * @param params the params of file selector.
162      */
163     bool OnFileSelectorShow(std::shared_ptr<OHOS::NWeb::NWebStringVectorValueCallback> callback,
164         std::shared_ptr<OHOS::NWeb::NWebFileSelectorParams> params) override;
165 
166     /**
167      * @brief Report web resource loading error to the SDK. These errors usually
168      *        indicate inability to connect to the server.
169      *
170      * @param request The request information.
171      * @param error The error information.
172      */
173     void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
174         std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override;
175 
176     /**
177      * @brief Ask for the permission.
178      *
179      * @param request std::shared_ptr<NWebAccessRequest>: A request to ask for the
180      *        permission.
181      */
182     void OnPermissionRequest(std::shared_ptr<OHOS::NWeb::NWebAccessRequest> request) override;
183 
184     void OnQuickMenuDismissed() override;
185 
186     void OnContextMenuDismissed() override;
187 
188     void OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insert_handle,
189         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> start_selection_handle,
190         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> end_selection_handle) override;
191 
192     /**
193      * @brief Notify the host application of a resource request and allow the
194      *        application to return the data.
195      *
196      * @param request request information
197      * @param response response information
198      *
199      * @retval true if handle success, otherwise false.
200      */
201     bool OnHandleInterceptRequest(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request,
202         std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override;
203 
204     /**
205      * @brief inform application to update its visited links database.
206      *
207      * @param url the url being visited.
208      * @param is_reload true if the url is being reload.
209      */
210     void OnRefreshAccessedHistory(const std::string& url, bool is_reload) override;
211 
212     /**
213      * @brief Cancel the request to ask for the permission.
214      *
215      * @param request std::shared_ptr<NWebAccessRequest>: A request to ask for the
216      *        permission.
217      */
218     void OnPermissionRequestCanceled(std::shared_ptr<OHOS::NWeb::NWebAccessRequest> request) override;
219 
220     /**
221      * @brief Give the SDK a chance to decide whether to continue loading the url.
222      *
223      * @param url The url to be loaded.
224      *
225      * @return true to cancel the loading, false to continue the loading.
226      */
227     bool OnHandleInterceptUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
228 
229     bool RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
230         std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback) override;
231 
232     bool RunContextMenu(std::shared_ptr<OHOS::NWeb::NWebContextMenuParams> params,
233         std::shared_ptr<OHOS::NWeb::NWebContextMenuCallback> callback) override;
234 
235     void UpdateDragCursor(ArkWebDragOperation op) override;
236 
237     bool FilterScrollEvent(const float x, const float y, const float x_velocity, const float y_velocity) override;
238 
239     /**
240      * @brief Obtains a list of all visited history items, used for link coloring
241      *
242      * @return visited history
243      */
244     std::vector<std::string> VisitedUrlHistory() override;
245 
246     /**
247      * @brief called when new window required.
248      *
249      * @param target_url URL to be loaded in the new window.
250      * @param is_alert dialog window or not.
251      * @param is_user_trigger triggered by User.
252      * @param handler set the new web object.
253      */
254     void OnWindowNewByJS(const std::string& target_url, bool is_alert, bool is_user_trigger,
255         std::shared_ptr<OHOS::NWeb::NWebControllerHandler> handler) override;
256 
257     /**
258      * @brief called when window exit.
259      */
260     void OnWindowExitByJS() override;
261 
262     /**
263      * @brief Notify the host application that the web page wants to display a
264      *        JavaScript alert() dialog.
265      *
266      * @param url String: The url of the page requesting the dialog.
267      * @param message String: The message of the dialog.
268      * @param result std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
269      *        confirm that the user closed the window.
270      *
271      * @return To show a custom dialog, the app should return true.
272      */
273     bool OnAlertDialogByJS(const std::string& url, const std::string& message,
274         std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) override;
275 
276     /**
277      * @brief Notify the host application that the web page wants to handle
278      *        JavaScript onbeforeunload.
279      *
280      * @param url String: The url of the page requesting.
281      * @param message String: The message of the dialog.
282      * @param result std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
283      *        confirm that the user closed the window.
284      *
285      * @return To show a custom dialog, the app should return true.
286      */
287     bool OnBeforeUnloadByJS(const std::string& url, const std::string& message,
288         std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) override;
289 
290     /**
291      * @brief Notify the host application that the web page wants to display a
292      *        JavaScript prompt() dialog.
293      *
294      * @param url String: The url of the page requesting the dialog.
295      * @param message String: The message of the dialog.
296      * @param default_value String: The default value of the input message.
297      * @param result std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
298      *        confirm that the user closed the window.
299      *
300      * @return To show a custom dialog, the app should return true.
301      */
302     bool OnPromptDialogByJS(const std::string& url, const std::string& message, const std::string& default_value,
303         std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) override;
304 
305     /**
306      * @brief Notify the host application that the web page wants to display a
307      *        JavaScript Confirm() dialog.
308      *
309      * @param url String: The url of the page requesting the dialog.
310      * @param message String: The message of the dialog.
311      * @param result std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
312      *        confirm that the user closed the window.
313      *
314      * @return To show a custom dialog, the app should return true.
315      */
316     bool OnConfirmDialogByJS(const std::string& url, const std::string& message,
317         std::shared_ptr<OHOS::NWeb::NWebJSDialogResult> result) override;
318 
319     bool OnHttpAuthRequestByJS(std::shared_ptr<OHOS::NWeb::NWebJSHttpAuthResult> result, const std::string& host,
320         const std::string& realm) override;
321 
322     bool OnSslErrorRequestByJS(std::shared_ptr<OHOS::NWeb::NWebJSSslErrorResult> result, ArkWebSslError error) override;
323 
324     bool OnSslSelectCertRequestByJS(std::shared_ptr<OHOS::NWeb::NWebJSSslSelectCertResult> result,
325         const std::string& host, int port, const std::vector<std::string>& key_types,
326         const std::vector<std::string>& issuers) override;
327 
328     void OnScroll(double x_offset, double y_offset) override;
329 
330     void OnOverScroll(float x_offset, float y_offset) override;
331 
332     void OnScrollState(bool scroll_state) override;
333 
334     /**
335      * @brief called when the page being loaded is about to be made visible.
336      */
337     void OnPageVisible(const std::string& url) override;
338 
339     /**
340      * @brief Give the host application a chance to handle the key event
341      *        synchronousl.
342      *
343      * @param event The key event.
344      *
345      * @return True if the host application wants to handle the key event itself,
346      *         otherwise return false.
347      */
348     bool OnPreKeyEvent(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event) override;
349 
350     void OnScaleChanged(float old_scale_factor, float new_scale_factor) override;
351 
352     /**
353      * @brief Called when the browser's cursor has changed.
354      *
355      * @param type Cursor type.
356      * @param info If |type| is CT_CUSTOM then |info| will be populated with the
357      *        custom cursor information.
358      *
359      * @return True if the cursor change was handled or false for default
360      *         handling.
361      */
362     bool OnCursorChange(const ArkWebCursorType& type, std::shared_ptr<OHOS::NWeb::NWebCursorInfo> info) override;
363 
364     /**
365      * @brief called when the render process exit.
366      *
367      * @param reason the detail reason why render process exit, the implementation
368      *        of this callback should attempt to clean up the specific nweb that
369      *        was set by SetNWeb interface.
370      */
371     void OnRenderExited(ArkWebRenderExitReason reason) override;
372 
373     /**
374      * @brief Called when resize not work.
375      */
376     void OnResizeNotWork() override;
377 
378     /**
379      * @brief called when the page exit the full-screen mode.
380      */
381     void OnFullScreenExit() override;
382 
383     /**
384      * @brief called when the page enter the full-screen mode.
385      *
386      * @param handler to exit full-screen mode.
387      */
388     void OnFullScreenEnter(std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler) override;
389 
390     bool OnDragAndDropData(const void* data, size_t len, std::shared_ptr<OHOS::NWeb::NWebImageOptions> opt) override;
391 
392     void OnSelectPopupMenu(std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,
393         std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback) override;
394 
395     /**
396      * @brief shows the repost form confirmation dialog box.
397      *
398      * @param handler sets whether to resend data.
399      */
400     void OnDataResubmission(std::shared_ptr<OHOS::NWeb::NWebDataResubmissionCallback> handler) override;
401 
402     void OnRootLayerChanged(int width, int height) override;
403 
404     /**
405      * @brief Called when the audio playing state on web page changed.
406      *
407      * @param playing Whether the audio is playing or not.
408      */
409     void OnAudioStateChanged(bool playing) override;
410 
411     void OnOverScrollFlingEnd() override;
412 
413     /**
414      * @brief Notify the host application that a key was not handled by the
415      *        WebView.
416      *
417      * @param event The key event.
418      *
419      * @return True if the host application wants to handle the key event itself,
420      *         otherwise return false.
421      */
422     bool OnUnProcessedKeyEvent(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event) override;
423 
424     bool OnDragAndDropDataUdmf(std::shared_ptr<OHOS::NWeb::NWebDragData> drag_data) override;
425 
426     /**
427      * @brief Called when the first content rendering of web page.
428      *
429      * @param navigation_start_tick Absolute navigation start time, as TimeTicks.
430      * @param first_contentful_paint_ms Time to first contentful paint from
431      *        navigation start.
432      */
433     void OnFirstContentfulPaint(int64_t navigation_start_tick, int64_t first_contentful_paint_ms) override;
434 
435     void OnDateTimeChooserPopup(std::shared_ptr<OHOS::NWeb::NWebDateTimeChooser> chooser,
436         const std::vector<std::shared_ptr<OHOS::NWeb::NWebDateTimeSuggestion>>& suggestions,
437         std::shared_ptr<OHOS::NWeb::NWebDateTimeChooserCallback> callback) override;
438 
439     void OnDateTimeChooserClose() override;
440 
441     /**
442      * @brief Ask for the screen capture permission.
443      *
444      * @param request std::shared_ptr<NWebScreenCaptureAccessRequest>: A request
445      *        to ask for the screen capture permission.
446      */
447     void OnScreenCaptureRequest(std::shared_ptr<OHOS::NWeb::NWebScreenCaptureAccessRequest> request) override;
448 
449     /**
450      * @brief Called when the media or form state on the web page changed.
451      *
452      * @param state state of the media or form. Refer to the enum class
453      *        MediaPlayingState and FormState
454      * @param type it can be form, media, or audio
455      */
456     void OnActivityStateChanged(int state, ArkWebActivityType type) override;
457 
458     void OnGetTouchHandleHotZone(std::shared_ptr<OHOS::NWeb::NWebTouchHandleHotZone> hot_zone) override;
459 
460     /**
461      * @brief Called when swap buffer completed with new size.
462      */
463     void OnCompleteSwapWithNewSize() override;
464 
465     void OnOverScrollFlingVelocity(float x_velocity, float y_velocity, bool is_fling) override;
466 
467     /**
468      * @brief called when the navigation entry has been committed.
469      *
470      * @param details represents the details of a committed navigation entry.
471      */
472     void OnNavigationEntryCommitted(std::shared_ptr<OHOS::NWeb::NWebLoadCommittedDetails> details) override;
473 
474     void OnNativeEmbedLifecycleChange(std::shared_ptr<OHOS::NWeb::NWebNativeEmbedDataInfo> dataInfo) override;
475 
476     void OnNativeEmbedGestureEvent(std::shared_ptr<OHOS::NWeb::NWebNativeEmbedTouchEvent> touchEvent) override;
477 
478     /**
479      * @brief Called when received website security risk check result.
480      *
481      * @param threat_type The threat type of website.
482      */
483     void OnSafeBrowsingCheckResult(int threat_type) override;
484 
485     /**
486      * @brief called when the page enter the full-screen mode.
487      *
488      * @param handler to exit full-screen mode.
489      * @param video_natural_width indicates the width of the <video> element
490      * entering full screen.
491      * @param video_natural_height indicates the height of the <video> element
492      * entering full screen.
493      */
494     void OnFullScreenEnterWithVideoSize(std::shared_ptr<OHOS::NWeb::NWebFullScreenExitHandler> handler,
495         int video_natural_width, int video_natural_height) override;
496 
497     /**
498      * @brief Give the application a chance to decide whether to override loading
499      * the url.
500      *
501      * @param request The request information.
502      * @return true to abort loading the url, false to continue loading the url
503      * as usual.
504      */
505     bool OnHandleOverrideUrlLoading(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request) override;
506 
507     /**
508      * @brief Called when tacker's cookie is prevented.
509      *
510      * @param website_host The host of website url.
511      *
512      * @param tracker_host The host of tracker url.
513      */
514     void OnIntelligentTrackingPreventionResult(
515         const std::string& website_host, const std::string& tracker_host) override;
516 
517     /**
518      * @brief Called when the first meaningful paint rendering of web page.
519      *
520      * @param details represents the details of first meaningful paint.
521      */
522     void OnFirstMeaningfulPaint(std::shared_ptr<OHOS::NWeb::NWebFirstMeaningfulPaintDetails> details) override;
523 
524     /**
525      * @brief Called when the largest contentful paint rendering of web page.
526      *
527      * @param details represents the details of largest contentful paint.
528      */
529     void OnLargestContentfulPaint(std::shared_ptr<OHOS::NWeb::NWebLargestContentfulPaintDetails> details) override;
530 
531     bool OnAllSslErrorRequestByJS(std::shared_ptr<OHOS::NWeb::NWebJSAllSslErrorResult> result, ArkWebSslError error,
532         const std::string& url, const std::string& originalUrl, const std::string& referrer, bool isFatalError,
533         bool isMainFrame) override;
534 
535     void OnTooltip(const std::string& tooltip) override;
536 
537     /**
538      * @brief called when resizehold is released.
539      *
540      */
541     void ReleaseResizeHold() override;
542 
543     /**
544      * @brief Called when select a word.
545      *
546      * @param text The content of the text.
547      * @param offset The offset of the point.
548      */
549     std::vector<int8_t> GetWordSelection(const std::string& text, int8_t offset) override;
550 
551     void UpdateClippedSelectionBounds(int x, int y, int w, int h) override;
552 
553     /**
554      * @brief called when the render process  not responding.
555      *
556      * @param js_stack
557      * @param pid
558      * @param reason
559      */
560     void OnRenderProcessNotResponding(
561         const std::string& js_stack, int pid, ArkWebRenderProcessNotRespondingReason reason) override;
562 
563     /**
564      * @brief called when the render process  not responding.
565      *
566      */
567     void OnRenderProcessResponding() override;
568 
569     bool OnOpenAppLink(const std::string& url, std::shared_ptr<OHOS::NWeb::NWebAppLinkCallback> callback) override;
570 
571     void OnShowAutofillPopup(
572         const float offsetX, const float offsetY, const std::vector<std::string>& menu_items) override;
573 
574     void OnHideAutofillPopup() override;
575 
576     void OnViewportFitChange(ArkWebViewportFit viewportFit) override;
577 
578     bool OnFocus(ArkWebFocusSource source) override;
579 
580     bool OnOverScroll(float xOffset, float yOffset, float xVelocity, float yVelocity) override;
581 
582     void CreateOverlay(void* data, size_t len, int width, int height, int offset_x, int offset_y, int rect_width,
583         int rect_height, int point_x, int point_y) override;
584 
585     void OnOverlayStateChanged(int offset_x, int offset_y, int rect_width, int rect_height) override;
586 
587     void OnAdsBlocked(const std::string& url, const std::vector<std::string>& adsBlocked) override;
588 
589     void OnInterceptKeyboardAttach(const std::shared_ptr<OHOS::NWeb::NWebCustomKeyboardHandler> keyboardHandler,
590         const std::map<std::string, std::string> &attributes, bool &useSystemKeyboard, int32_t &enterKeyType) override;
591 
592     void OnCustomKeyboardAttach() override;
593 
594     void OnCustomKeyboardClose() override;
595 
596     void KeyboardReDispatch(std::shared_ptr<OHOS::NWeb::NWebKeyEvent> event, bool isUsed) override;
597 
598     void HideHandleAndQuickMenuIfNecessary(bool hide) override;
599 
600     void OnCursorUpdate(double x, double y, double width, double height) override;
601 
602     void ChangeVisibilityOfQuickMenu() override;
603 
604     void StartVibraFeedback(const std::string& vibratorType) override;
605 
606     void OnNativeEmbedVisibilityChange(const std::string& embed_id, bool visibility) override;
607 
608     bool CloseImageOverlaySelection() override;
609 
610     void GetVisibleRectToWeb(int& visibleX, int& visibleY, int& visibleWidth, int& visibleHeight) override;
611 
612     void OnScrollStart(const float x, const float y) override;
613 private:
614     ArkWebRefPtr<ArkWebHandler> ark_web_handler_;
615 };
616 
617 } // namespace OHOS::ArkWeb
618 
619 #endif // ARK_WEB_HANDLER_WRAPPER_H_
620