• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef NWEB_HANDLER_H
17 #define NWEB_HANDLER_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "nweb_access_request.h"
24 #include "nweb.h"
25 #include "nweb_console_log.h"
26 #include "nweb_context_menu_params.h"
27 #include "nweb_controller_handler.h"
28 #include "nweb_data_resubmission_callback.h"
29 #include "nweb_file_selector_params.h"
30 #include "nweb_full_screen_exit_handler.h"
31 #include "nweb_geolocation_callback_interface.h"
32 #include "nweb_js_dialog_result.h"
33 #include "nweb_js_http_auth_result.h"
34 #include "nweb_js_ssl_error_result.h"
35 #include "nweb_js_ssl_select_cert_result.h"
36 #include "nweb_select_popup_menu.h"
37 #include "nweb_touch_handle_state.h"
38 #include "nweb_url_resource_error.h"
39 #include "nweb_url_resource_request.h"
40 #include "nweb_url_resource_response.h"
41 
42 namespace OHOS::NWeb {
43 enum class RenderExitReason {
44     // Render process non-zero exit status
45     PROCESS_ABNORMAL_TERMINATION,
46 
47     // SIGKILL or task manager kill
48     PROCESS_WAS_KILLED,
49 
50     // Segmentation fault
51     PROCESS_CRASHED,
52 
53     // Out of memory
54     PROCESS_OOM,
55 
56     // Unknown reason
57     PROCESS_EXIT_UNKNOWN,
58 };
59 
60 enum class SslError {
61     // General error
62     INVALID,
63 
64     // Hostname mismatch
65     HOSTMISMATCH,
66 
67     // The certificate date is invalid
68     DATEINVALID,
69 
70     // The certificate authority is not trusted
71     UNTRUSTED,
72 };
73 
74 struct ImageOptions {
75     ImageColorType colorType;
76     ImageAlphaType alphaType;
77     size_t width;
78     size_t height;
79 };
80 
81 // Cursor type values.
82 enum class CursorType: int32_t {
83     CT_POINTER = 0,
84     CT_CROSS,
85     CT_HAND,
86     CT_IBEAM,
87     CT_WAIT,
88     CT_HELP,
89     CT_EASTRESIZE,
90     CT_NORTHRESIZE,
91     CT_NORTHEASTRESIZE,
92     CT_NORTHWESTRESIZE,
93     CT_SOUTHRESIZE,
94     CT_SOUTHEASTRESIZE,
95     CT_SOUTHWESTRESIZE,
96     CT_WESTRESIZE,
97     CT_NORTHSOUTHRESIZE,
98     CT_EASTWESTRESIZE,
99     CT_NORTHEASTSOUTHWESTRESIZE,
100     CT_NORTHWESTSOUTHEASTRESIZE,
101     CT_COLUMNRESIZE,
102     CT_ROWRESIZE,
103     CT_MIDDLEPANNING,
104     CT_EASTPANNING,
105     CT_NORTHPANNING,
106     CT_NORTHEASTPANNING,
107     CT_NORTHWESTPANNING,
108     CT_SOUTHPANNING,
109     CT_SOUTHEASTPANNING,
110     CT_SOUTHWESTPANNING,
111     CT_WESTPANNING,
112     CT_MOVE,
113     CT_VERTICALTEXT,
114     CT_CELL,
115     CT_CONTEXTMENU,
116     CT_ALIAS,
117     CT_PROGRESS,
118     CT_NODROP,
119     CT_COPY,
120     CT_NONE,
121     CT_NOTALLOWED,
122     CT_ZOOMIN,
123     CT_ZOOMOUT,
124     CT_GRAB,
125     CT_GRABBING,
126     CT_MIDDLE_PANNING_VERTICAL,
127     CT_MIDDLE_PANNING_HORIZONTAL,
128     CT_CUSTOM,
129     CT_DND_NONE,
130     CT_DND_MOVE,
131     CT_DND_COPY,
132     CT_DND_LINK,
133     CT_MAX_VALUE,
134 };
135 
136 struct NWebCursorInfo {
137     int32_t width = 0;
138     int32_t height = 0;
139     int32_t x = 0;
140     int32_t y = 0;
141     float scale = 1.0;
142     // buff will be width*height*4 bytes in size and represents a BGRA image with an upper-left origin.
143     std::unique_ptr<uint8_t[]> buff = nullptr;
144 };
145 
146 using FileSelectorCallback = NWebValueCallback<std::vector<std::string>&>;
147 
148 class OHOS_NWEB_EXPORT NWebHandler {
149 public:
150     NWebHandler() = default;
151 
152     virtual ~NWebHandler() = default;
153 
SetNWeb(std::shared_ptr<NWeb> nweb)154     virtual void SetNWeb(std::shared_ptr<NWeb> nweb) {}
155 
OnProxyDied()156     virtual void OnProxyDied() {}
157 
OnRouterPush(const std::string & param)158     virtual void OnRouterPush(const std::string& param) {}
159 
OnMessage(const std::string & param)160     virtual void OnMessage(const std::string& param) {}
161 
162     /**
163      * @brief Notify the SDK that a web site has finished loading. This method is
164      * called only for main frame.
165      *
166      * @param httpStatusCode The status code for the http request.
167      * @param url The url of the web site.
168      */
OnPageLoadEnd(int httpStatusCode,const std::string & url)169     virtual void OnPageLoadEnd(int httpStatusCode, const std::string& url) {}
170 
171     /**
172      * @brief Notify the SDK that a web site has started loading. This method is
173      * called once for each main frame load.
174      *
175      * @param url The url to be loaded.
176      */
OnPageLoadBegin(const std::string & url)177     virtual void OnPageLoadBegin(const std::string& url) {}
178 
179     /**
180      * @brief Report a load error to the SDK.
181      *
182      * @param errorCode The error code.
183      * @param description The error description.
184      * @param failingUrl The failed url.
185      */
OnPageLoadError(int errorCode,const std::string & description,const std::string & failingUrl)186     virtual void OnPageLoadError(int errorCode,
187                                  const std::string& description,
188                                  const std::string& failingUrl) {}
189 
190     /**
191      * @brief Give the SDK a chance to decide whether to continue loading the
192      * url.
193      *
194      * @param url The url to be loaded.
195      * @return true to cancel the loading, false to continue the loading.
196      */
OnHandleInterceptUrlLoading(const std::string & url)197     virtual bool OnHandleInterceptUrlLoading(const std::string& url) {
198         return false;
199     }
200 
201     /**
202      * @brief Notify the SDK that the nweb will load the resource specified by
203      * the given url.
204      *
205      * @param url The url of the resource.
206      */
OnResource(const std::string & url)207     virtual void OnResource(const std::string& url) {}
208 
209     /**
210      * @brief Notify the SDK of the changed document title.
211      *
212      * @param title The document title.
213      */
OnPageTitle(const std::string & title)214     virtual void OnPageTitle(const std::string& title) {}
215 
216     /**
217      * @brief Notify the SDK the current progress of loading a web site.
218      *
219      * @param newProgress Loading progress, an integer between 0 and 100.
220      */
OnLoadingProgress(int newProgress)221     virtual void OnLoadingProgress(int newProgress) {}
222 
223     /**
224      * @brief Request display and focus for a new nweb.
225      *
226      */
OnFocus()227     virtual void OnFocus() {}
228 
229     /**
230      * @brief Obtains a list of all visited history items, used for link coloring
231      *
232      * @retval visited history
233      */
VisitedUrlHistory()234     virtual const std::vector<std::string> VisitedUrlHistory() {
235         return std::vector<std::string>();
236     }
237 
238     /**
239      * @brief Notify the host application of a resource request and allow the
240      * application to return the data.
241      *
242      * @param request request information
243      *
244      * @retval std::shared_ptr<NWebUrlResourceResponse> response information
245      */
OnHandleInterceptRequest(std::shared_ptr<NWebUrlResourceRequest> request)246     virtual std::shared_ptr<NWebUrlResourceResponse> OnHandleInterceptRequest(
247         std::shared_ptr<NWebUrlResourceRequest> request) {
248         return nullptr;
249     }
250 
251     /**
252      * @brief Report web resource loading error to the SDK. These errors usually
253      * indicate inability to connect to the server.
254      *
255      * @param request The request information.
256      * @param error The error information.
257      */
OnResourceLoadError(std::shared_ptr<NWebUrlResourceRequest> request,std::shared_ptr<NWebUrlResourceError> error)258     virtual void OnResourceLoadError(std::shared_ptr<NWebUrlResourceRequest> request,
259                                      std::shared_ptr<NWebUrlResourceError> error) {}
260 
261     /**
262      * @brief Notify the SDK that an HTTP error has been received from the server
263      * while loading a resource.
264      *
265      * @param request The request information.
266      * @param errorResponse The error occurred.
267      */
OnHttpError(std::shared_ptr<NWebUrlResourceRequest> request,std::shared_ptr<NWebUrlResourceResponse> errorResponse)268     virtual void OnHttpError(std::shared_ptr<NWebUrlResourceRequest> request,
269                              std::shared_ptr<NWebUrlResourceResponse> errorResponse) {}
270 
271     /**
272      * @brief Notify the SDK of a new favicon for the current web site.
273      *
274      * @param data The raw image data for the icon.
275      * @param width The width of the icon in pixel.
276      * @param height The height of the icon in pixel.
277      * @param color_type The color data encoding type.
278      * @param alpha_type The alpha value of any pixel.
279      */
OnPageIcon(const void * data,size_t width,size_t height,ImageColorType color_type,ImageAlphaType alpha_type)280     virtual void OnPageIcon(const void* data,
281                             size_t width,
282                             size_t height,
283                             ImageColorType color_type,
284                             ImageAlphaType alpha_type) {}
285 
286     /**
287      * @brief Notify the SDK of the url for a touch icon.
288      *
289      * @param icon_url The icon url.
290      * @param precomposed The touch icon type.
291      */
OnDesktopIconUrl(const std::string & icon_url,bool precomposed)292     virtual void OnDesktopIconUrl(const std::string& icon_url, bool precomposed) {
293     }
294 
295     /**
296      * @brief Report a JavaScript console message to the host application.
297      *
298      * @param message Details of the console message.
299      * @return Return true to stop the message from being output to the console.
300      */
OnConsoleLog(const NWebConsoleLog & message)301     virtual bool OnConsoleLog(const NWebConsoleLog& message) { return false; }
302 
303     /**
304      * @brief Show prompt to ask for the geolocation permission.
305      *
306      * @param origin  String: the origin of the resource to get geolocation
307      * @param callback  GeolocationCallbackInterface: callback to report
308      * geolocation
309      */
OnGeolocationShow(const std::string & origin,std::shared_ptr<NWebGeolocationCallbackInterface> callback)310     virtual void OnGeolocationShow(const std::string& origin,
311                                    std::shared_ptr<NWebGeolocationCallbackInterface> callback) {}
312 
313     /**
314      * @brief Notify the host application that the web page wants to display a
315      * JavaScript alert() dialog.
316      *
317      * @param url  String: The url of the page requesting the dialog.
318      * @param message  String: The message of the dialog.
319      * @param result  std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
320      * confirm that the user closed the window.
321      * @return To show a custom dialog, the app should return true.
322      */
OnAlertDialogByJS(const std::string & url,const std::string & message,std::shared_ptr<NWebJSDialogResult> result)323     virtual bool OnAlertDialogByJS(const std::string& url,
324                                    const std::string& message,
325                                    std::shared_ptr<NWebJSDialogResult> result) {
326         return false;
327     }
328 
329     /**
330      * @brief Notify the host application that the web page wants to handle
331      * JavaScript onbeforeunload.
332      *
333      * @param url  String: The url of the page requesting.
334      * @param message  String: The message of the dialog.
335      * @param result  std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
336      * confirm that the user closed the window.
337      * @return To show a custom dialog, the app should return true.
338      */
OnBeforeUnloadByJS(const std::string & url,const std::string & message,std::shared_ptr<NWebJSDialogResult> result)339     virtual bool OnBeforeUnloadByJS(const std::string& url,
340                                     const std::string& message,
341                                     std::shared_ptr<NWebJSDialogResult> result) {
342         return false;
343     }
344 
345     /**
346      * @brief Notify the host application that the web page wants to display a
347      * JavaScript prompt() dialog.
348      *
349      * @param url  String: The url of the page requesting the dialog.
350      * @param message  String: The message of the dialog.
351      * @param defaultValue  String: The default value of the input message.
352      * @param result  std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
353      * confirm that the user closed the window.
354      * @return To show a custom dialog, the app should return true.
355      */
OnPromptDialogByJS(const std::string & url,const std::string & message,const std::string & defaultValue,std::shared_ptr<NWebJSDialogResult> result)356     virtual bool OnPromptDialogByJS(const std::string& url,
357                                     const std::string& message,
358                                     const std::string& defaultValue,
359                                     std::shared_ptr<NWebJSDialogResult> result) {
360         return false;
361     }
362 
363     /**
364      * @brief Notify the host application that the web page wants to display a
365      * JavaScript Confirm() dialog.
366      *
367      * @param url  String: The url of the page requesting the dialog.
368      * @param message  String: The message of the dialog.
369      * @param result  std::shared_ptr<NWebJSDialogResult>: A NWebJSDialogResult to
370      * confirm that the user closed the window.
371      * @return To show a custom dialog, the app should return true.
372      */
OnConfirmDialogByJS(const std::string & url,const std::string & message,std::shared_ptr<NWebJSDialogResult> result)373     virtual bool OnConfirmDialogByJS(const std::string& url,
374                                      const std::string& message,
375                                      std::shared_ptr<NWebJSDialogResult> result) {
376         return false;
377     }
378 
379     /**
380      * @brief Hide prompt to ask for the geolocation permission.
381      */
OnGeolocationHide()382     virtual void OnGeolocationHide() {}
383 
384     /**
385      * @brief Ask for the permission.
386      *
387      * @param request std::shared_ptr<NWebAccessRequest>: A request to ask for the
388      * permission.
389      */
OnPermissionRequest(std::shared_ptr<NWebAccessRequest> request)390     virtual void OnPermissionRequest(std::shared_ptr<NWebAccessRequest> request) {}
391 
392     /**
393      * @brief Cancel the request to ask for the permission.
394      *
395      * @param request std::shared_ptr<NWebAccessRequest>: A request to ask for the
396      * permission.
397      */
OnPermissionRequestCanceled(std::shared_ptr<NWebAccessRequest> request)398     virtual void OnPermissionRequestCanceled(std::shared_ptr<NWebAccessRequest> request) {}
399 
400     /**
401      * @brief called when the render process exit.
402      *
403      * @param reason  the detail reason why render process exit, the implementation of this callback
404      * should attempt to clean up the specific nweb that was set by SetNWeb interface.
405      */
OnRenderExited(RenderExitReason reason)406     virtual void OnRenderExited(RenderExitReason reason) {}
407 
408     /**
409      * @brief inform application to update its visited links database.
410      *
411      * @param url the url being visited.
412      * @param isReload true if the url is being reload.
413      */
OnRefreshAccessedHistory(const std::string & url,bool isReload)414     virtual void OnRefreshAccessedHistory(const std::string& url, bool isReload) {}
415 
416     /**
417      * @brief inform application to show a file selector.
418      * @param callback the file list to select.
419      * @param params the params of file selector.
420      */
OnFileSelectorShow(std::shared_ptr<FileSelectorCallback> callback,std::shared_ptr<NWebFileSelectorParams> params)421     virtual bool OnFileSelectorShow(std::shared_ptr<FileSelectorCallback> callback,
422                                     std::shared_ptr<NWebFileSelectorParams> params) {
423         return false;
424     }
425 
OnScaleChanged(float oldScaleFactor,float newScaleFactor)426     virtual void OnScaleChanged(float oldScaleFactor, float newScaleFactor) {}
427 
RunContextMenu(std::shared_ptr<NWebContextMenuParams> params,std::shared_ptr<NWebContextMenuCallback> callback)428     virtual bool RunContextMenu(std::shared_ptr<NWebContextMenuParams> params,
429                                 std::shared_ptr<NWebContextMenuCallback> callback) {
430         return false;
431     }
432 
OnContextMenuDismissed()433     virtual void OnContextMenuDismissed() {}
434 
RunQuickMenu(std::shared_ptr<NWebQuickMenuParams> params,std::shared_ptr<NWebQuickMenuCallback> callback)435     virtual bool RunQuickMenu(std::shared_ptr<NWebQuickMenuParams> params,
436                               std::shared_ptr<NWebQuickMenuCallback> callback) {
437         return false;
438     }
439 
OnQuickMenuDismissed()440     virtual void OnQuickMenuDismissed() {}
441 
OnTouchSelectionChanged(std::shared_ptr<NWebTouchHandleState> insertHandle,std::shared_ptr<NWebTouchHandleState> startSelectionHandle,std::shared_ptr<NWebTouchHandleState> endSelectionHandle)442     virtual void OnTouchSelectionChanged(
443         std::shared_ptr<NWebTouchHandleState> insertHandle,
444         std::shared_ptr<NWebTouchHandleState> startSelectionHandle,
445         std::shared_ptr<NWebTouchHandleState> endSelectionHandle) {}
446 
OnHttpAuthRequestByJS(std::shared_ptr<NWebJSHttpAuthResult> result,const std::string & host,const std::string & realm)447     virtual bool OnHttpAuthRequestByJS(std::shared_ptr<NWebJSHttpAuthResult> result,
448                                        const std::string& host,
449                                        const std::string& realm) {
450         return false;
451     }
452 
OnScroll(double xOffset,double yOffset)453     virtual void OnScroll(double xOffset, double yOffset) {}
454 
OnDragAndDropData(const void * data,size_t len,const ImageOptions & opt)455     virtual bool OnDragAndDropData(const void* data, size_t len, const ImageOptions& opt) {
456         return false;
457     }
458 
OnSslErrorRequestByJS(std::shared_ptr<NWebJSSslErrorResult> result,SslError error)459     virtual bool OnSslErrorRequestByJS(std::shared_ptr<NWebJSSslErrorResult> result,
460                                        SslError error) {
461         return false;
462     }
463 
OnSslSelectCertRequestByJS(std::shared_ptr<NWebJSSslSelectCertResult> result,const std::string & host,int port,const std::vector<std::string> & keyTypes,const std::vector<std::string> & issuers)464     virtual bool OnSslSelectCertRequestByJS(
465         std::shared_ptr<NWebJSSslSelectCertResult> result,
466         const std::string& host,
467         int port,
468         const std::vector<std::string>& keyTypes,
469         const std::vector<std::string>& issuers) {
470         return false;
471     }
472 
473     /**
474      * @brief called when the page enter the full-screen mode.
475      * @param handler to exit the full-screen mode.
476      */
OnFullScreenEnter(std::shared_ptr<NWebFullScreenExitHandler> handler)477     virtual void OnFullScreenEnter(std::shared_ptr<NWebFullScreenExitHandler> handler) {}
478 
479     /**
480      * @brief called when the page exit the full-screen mode.
481      */
OnFullScreenExit()482     virtual void OnFullScreenExit() {}
483 
484     /**
485      * @brief Notification window creation request.
486      * @param targetUrl target url.
487      * @param isAlert Whether it is a dialog box.
488      * @param isUserTrigger Whether it was triggered by the user.
489      * @param handler set the new web object.
490      */
OnWindowNewByJS(const std::string & targetUrl,bool isAlert,bool isUserTrigger,std::shared_ptr<NWebControllerHandler> handler)491     virtual void OnWindowNewByJS(
492         const std::string& targetUrl,
493         bool isAlert,
494         bool isUserTrigger,
495         std::shared_ptr<NWebControllerHandler> handler) {}
496 
497     /**
498      * @brief Notification window close request.
499      */
OnWindowExitByJS()500     virtual void OnWindowExitByJS() {}
501 
502     /**
503      * @brief called when the page beging loaded is about to be made visible.
504      * @param url url.
505      */
OnPageVisible(const std::string & url)506     virtual void OnPageVisible(const std::string& url) {}
507 
508     /**
509      * @brief shows the repost from confirmation dialog box.
510      * @param handler sets whether to resend data.
511      */
OnDataResubmission(std::shared_ptr<NWebDataResubmissionCallback> handler)512     virtual void OnDataResubmission(std::shared_ptr<NWebDataResubmissionCallback> handler) {}
513 
514     /**
515      * @brief Called when the browser is cursor has changed.
516      * @param type Cursor type.
517      * @param info If |type| is CT_CUSTOM then |info| will be populated with the custom cursor information.
518      * @return True if the cursor change was handled or false for default handling.
519      */
OnCursorChange(const CursorType & type,const NWebCursorInfo & info)520     virtual bool OnCursorChange(const CursorType& type, const NWebCursorInfo& info) {
521         return false;
522     }
523 
OnSelectPopupMenu(std::shared_ptr<NWebSelectPopupMenuParam> params,std::shared_ptr<NWebSelectPopupMenuCallback> callback)524     virtual void OnSelectPopupMenu(std::shared_ptr<NWebSelectPopupMenuParam> params,
525                                    std::shared_ptr<NWebSelectPopupMenuCallback> callback) {}
526 };
527 }  // namespace OHOS::NWeb
528 
529 #endif  // NWEB_HANDLER_H
530