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