Add an API for getting web view when request is intercepted.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / controls / web-view / web-view.h
1 #ifndef DALI_TOOLKIT_WEB_VIEW_H
2 #define DALI_TOOLKIT_WEB_VIEW_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <functional>
23 #include <memory>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/controls/control.h>
27 #include <dali/devel-api/adaptor-framework/web-engine/web-engine-plugin.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 class ImageView;
34 class WebBackForwardList;
35 class WebContext;
36 class WebCookieManager;
37 class WebSettings;
38
39 namespace Internal DALI_INTERNAL
40 {
41 class WebView;
42 } // namespace DALI_INTERNAL
43
44 /**
45  * @addtogroup dali_toolkit_controls_web_view
46  * @{
47  */
48
49 /**
50  * @brief WebView is a control for displaying web content.
51  *
52  * This enables embedding web pages in the application.
53  *
54  * For working WebView, a web engine plugin for a platform should be provided.
55  *
56  */
57 class DALI_TOOLKIT_API WebView : public Control
58 {
59 public:
60   /**
61    * @brief Enumeration for the start and end property ranges for this control.
62    */
63   enum PropertyRange
64   {
65     PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
66     PROPERTY_END_INDEX   = PROPERTY_START_INDEX + 1000,
67   };
68
69   /**
70    * @brief Enumeration for the instance of properties belonging to the WebView class.
71    */
72   struct Property
73   {
74     enum
75     {
76       /**
77        * @brief The url to load.
78        * @details name "url", type Property::STRING.
79        */
80       URL = PROPERTY_START_INDEX,
81
82       /**
83        * @brief The user agent string.
84        * @details name "userAgent", type Property::STRING.
85        */
86       USER_AGENT,
87
88       /**
89        * @brief The current position of scroll.
90        * @details name "scrollPosition", type Property::VECTOR2.
91        */
92       SCROLL_POSITION,
93
94       /**
95        * @brief The current size of scroll.
96        * @details name "scrollSize", type Property::VECTOR2.
97        * @note The value is read-only.
98        */
99       SCROLL_SIZE,
100
101       /**
102        * @brief The current size of content.
103        * @details name "contentSize", type Property::VECTOR2.
104        * @note The value is read-only.
105        */
106       CONTENT_SIZE,
107
108       /**
109        * @brief The title of web page.
110        * @details name "title", type Property::STRING.
111        * @note The value is read-only.
112        */
113       TITLE,
114
115       /**
116        * @brief Whether video hole is enabled or not.
117        * @details name "videoHoleEnabled", type Property::BOOLEAN.
118        * @note False by default.
119        */
120       VIDEO_HOLE_ENABLED,
121
122       /**
123        * @brief Whether mouse event is enabled.
124        * @details name "mouseEventsEnabled", type Property::BOOLEAN.
125        * @note Default is true.
126        */
127       MOUSE_EVENTS_ENABLED,
128
129       /**
130        * @brief Whether key event is enabled.
131        * @details name "keyEventsEnabled", type Property::BOOLEAN.
132        * @note Default is true.
133        */
134       KEY_EVENTS_ENABLED,
135
136       /**
137        * @brief The background color of web page.
138        * @details name "documentBackgroundColor", type Property::VECTOR4.
139        */
140       DOCUMENT_BACKGROUND_COLOR,
141
142       /**
143        * @brief Whether tiles can be cleared or not when hidden.
144        * @details name "tilesClearedWhenHidden", type BOOLEAN.
145        */
146       TILES_CLEARED_WHEN_HIDDEN,
147
148       /**
149        * @brief The multiplier of cover area of tile when rendering web page.
150        * @details name "tileCoverAreaMultiplier", type FLOAT.
151        */
152       TILE_COVER_AREA_MULTIPLIER,
153
154       /**
155        * @brief Whether cursor is enabled or not by client.
156        * @details name "cursorEnabledByClient", type BOOLEAN.
157        */
158       CURSOR_ENABLED_BY_CLIENT,
159
160       /**
161        * @brief The selected text of web page.
162        * @details name "selectedText", type Property::STRING.
163        * @note The value is read-only.
164        */
165       SELECTED_TEXT,
166
167       /**
168        * @brief Zoom factor of web page.
169        * @details name "pageZoomFactor", type Property::FLOAT.
170        */
171       PAGE_ZOOM_FACTOR,
172
173       /**
174        * @brief Zoom factor of text.
175        * @details name "textZoomFactor", type Property::FLOAT.
176        */
177       TEXT_ZOOM_FACTOR,
178
179       /**
180        * @brief progress percentage of loading a web page.
181        * @details name "loadProgressPercentage", type Property::FLOAT.
182        * @note The value is read-only.
183        */
184       LOAD_PROGRESS_PERCENTAGE,
185     };
186   };
187
188   /**
189    * @brief WebView callback related with screen-shot captured.
190    */
191   using WebViewScreenshotCapturedCallback = std::function<void(Dali::Toolkit::ImageView)>;
192
193 public:
194   /**
195    * @brief Create an initialized WebView.
196    * @return A handle to a newly allocated Dali WebView
197    *
198    * @note WebView will not display anything
199    */
200   static WebView New();
201
202   /**
203    * @brief Create an initialized WebView.
204    *
205    * @param [in] locale The locale of Web
206    * @param [in] timezoneId The timezoneId of Web
207    */
208   static WebView New(const std::string& locale, const std::string& timezoneId);
209
210   /**
211    * @brief Create an initialized WebView.
212    *
213    * @param [in] argc The count of arguments of Applications
214    * @param [in] argv The string array of arguments of Applications
215    */
216   static WebView New(uint32_t argc, char** argv);
217
218   /**
219    * @brief Find web view by web engine plugin.
220    */
221   static Toolkit::WebView FindWebView(Dali::WebEnginePlugin* plugin);
222
223   /**
224    * @brief Get context of web engine.
225    */
226   static Dali::WebEngineContext* GetContext();
227
228   /**
229    * @brief Get cookie manager of web engine.
230    */
231   static Dali::WebEngineCookieManager* GetCookieManager();
232
233   /**
234    * @brief Create an uninitialized WebView.
235    */
236   WebView();
237
238   /**
239    * @brief Destructor.
240    *
241    * This is non-virtual since derived Handel types must not contain data or virtual methods.
242    */
243   ~WebView();
244
245   /*
246    * @brief Copy constructor.
247    *
248    * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
249    */
250   WebView(const WebView& WebView);
251
252   /**
253    * @brief Assignment operator.
254    *
255    * @param[in] WebView The WebView to assign from
256    * @return The updated WebView
257    */
258   WebView& operator=(const WebView& WebView);
259
260   /**
261    * @brief Downcast a handle to WebView handle.
262    *
263    * If handle points to a WebView, the downcast produces valid handle.
264    * If not, the returned handle is left uninitialized.
265    *
266    * @param[in] handle Handle to an object
267    * @return Handle to a WebView or an uninitialized handle
268    */
269   static WebView DownCast(BaseHandle handle);
270
271   /**
272    * @brief Get WebSettings of WebEngine.
273    */
274   Dali::Toolkit::WebSettings* GetSettings() const;
275
276   /**
277    * @brief Get WebBackForwardList of WebEngine.
278    */
279   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
280
281   /**
282    * @brief Get favicon of web page.
283    *
284    * @return Handle to a favicon
285    */
286   Dali::Toolkit::ImageView GetFavicon() const;
287
288   /**
289    * @brief Load a web page based on a given URL.
290    *
291    * @param [in] url The URL of the resource to load
292    */
293   void LoadUrl(const std::string& url);
294
295   /**
296    * @brief Load a given string as web contents.
297    *
298    * @param [in] htmlString The string to use as the contents of the web page
299    */
300   void LoadHtmlString(const std::string& htmlString);
301
302   /**
303    * @brief Load the specified html string as the content of the view overriding current history entry
304    *
305    * @param[in] html HTML data to load
306    * @param[in] basicUri Base URL used for relative paths to external objects
307    * @param[in] unreachableUrl URL that could not be reached
308    *
309    * @return true if successfully loaded, false otherwise
310    */
311   bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
312
313   /**
314    * @brief Request loading the given contents by MIME type into the view object
315    *
316    * @param[in] contents The content to load
317    * @param[in] contentSize The size of contents (in bytes)
318    * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
319    * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
320    * @param[in] baseUri The base URI to use for relative resources
321    *
322    * @return true if successfully request, false otherwise
323    */
324   bool LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
325
326   /**
327    * @brief Reload the Web.
328    */
329   void Reload();
330
331   /**
332    * @brief Reload the current page's document without cache
333    */
334   bool ReloadWithoutCache();
335
336   /**
337    * @brief Stop loading web contents on the current page.
338    */
339   void StopLoading();
340
341   /**
342    * @brief Suspend the operation associated with the view.
343    */
344   void Suspend();
345
346   /**
347    * @brief Resume the operation associated with the view object after calling Suspend().
348    */
349   void Resume();
350
351   /**
352    * @brief To suspend all url loading
353    */
354   void SuspendNetworkLoading();
355
356   /**
357    * @brief To resume new url network loading
358    */
359   void ResumeNetworkLoading();
360
361   /**
362    * @brief Add custom header
363    *
364    * @param[in] name custom header name to add the custom header
365    * @param[in] value custom header value to add the custom header
366    *
367    * @return true if succeeded, false otherwise
368    */
369   bool AddCustomHeader(const std::string& name, const std::string& value);
370
371   /**
372    * @brief Remove custom header
373    *
374    * @param[in] name custom header name to remove the custom header
375    *
376    * @return true if succeeded, false otherwise
377    */
378   bool RemoveCustomHeader(const std::string& name);
379
380   /**
381    * @brief Start the inspector server
382    *
383    * @param[in] port port number
384    *
385    * @return the port number
386    */
387   uint32_t StartInspectorServer(uint32_t port);
388
389   /**
390    * @brief Stop the inspector server
391    *
392    * @return true if succeeded, false otherwise
393    */
394   bool StopInspectorServer();
395
396   /**
397    * @brief Scroll web page of view by deltaX and deltaY.
398    * @param[in] deltaX The delta x of scroll
399    * @param[in] deltaY The delta y of scroll
400    */
401   void ScrollBy(int32_t deltaX, int32_t deltaY);
402
403   /**
404    * @brief Scroll edge of view by deltaX and deltaY.
405    *
406    * @param[in] deltaX horizontal offset to scroll
407    * @param[in] deltaY vertical offset to scroll
408    *
409    * @return true if succeeded, false otherwise
410    */
411   bool ScrollEdgeBy(int32_t deltaX, int32_t deltaY);
412
413   /**
414    * @brief Return whether forward is possible.
415    *
416    * @return True if forward is possible, false otherwise
417    */
418   bool CanGoForward();
419
420   /**
421    * @brief Go forward in the navigation history.
422    */
423   void GoForward();
424
425   /**
426    * @brief Return whether backward is possible.
427    *
428    * @return True if backward is possible, false otherwise
429    */
430   bool CanGoBack();
431
432   /**
433    * @brief Go back in the navigation history.
434    */
435   void GoBack();
436
437   /**
438    * @brief Evaluate JavaScript code represented as a string.
439    *
440    * @param[in] script The JavaScript code
441    * @param[in] resultHandler The callback to be called by the JavaScript runtime. This carries evaluation result
442    */
443   void EvaluateJavaScript(const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler);
444
445   /**
446    * @brief Evaluate JavaScript code represented as a string.
447    *
448    * @param[in] script The JavaScript code
449    */
450   void EvaluateJavaScript(const std::string& script);
451
452   /**
453    * @brief Inject a JavaScript object with a message handler into the WebView.
454    *
455    * @note The injected object will appear in the JavaScript context to be loaded next.
456    *
457    * Example:
458    *
459    * 1. Native
460    *
461    *     webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
462    *         printf( "Received a message from JS: %s", message.c_str() );
463    *     });
464    *
465    *     // Start WebView by loading URL
466    *     webview.LoadUrl( url );
467    *
468    * 2. JavaScript
469    *
470    *     myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
471    *
472    *
473    * @param[in] exposedObjectName The name of exposed object
474    * @param[in] handler The callback function
475    */
476   void AddJavaScriptMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler);
477
478   /**
479    * @brief Register alert callback for javascript.
480    *
481    * @param[in] callback The callback function to be called by the JavaScript runtime.
482    */
483   void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
484
485   /**
486    * @brief Reply for JavaScript alert.
487    */
488   void JavaScriptAlertReply();
489
490   /**
491    * @brief Register confirm callback for javascript.
492    *
493    * @param[in] callback The callback function to be called by the JavaScript runtime.
494    */
495   void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
496
497   /**
498    * @brief Reply for JavaScript confirm.
499    * @param[in] confirmed True if confirmed, false otherwise
500    */
501   void JavaScriptConfirmReply(bool confirmed);
502
503   /**
504    * @brief Register prompt callback for javascript.
505    *
506    * @param[in] callback The callback function to be called by the JavaScript runtime.
507    */
508   void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
509
510   /**
511    * @brief Reply for JavaScript prompt.
512    * @param[in] result The result from input-field of prompt popup.
513    */
514   void JavaScriptPromptReply(const std::string& result);
515
516   /**
517    * @brief Create a new hit test.
518    *
519    * @param[in] x the horizontal position to query
520    * @param[in] y the vertical position to query
521    * @param[in] mode the mode of hit test
522    *
523    * @return a new hit test object.
524    */
525   std::unique_ptr<Dali::WebEngineHitTest> CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode);
526
527   /**
528    * @brief Create a hit test asynchronously.
529    *
530    * @param[in] x the horizontal position to query
531    * @param[in] y the vertical position to query
532    * @param[in] mode the mode of hit test
533    * @param[in] callback The callback function
534    *
535    * @return true if succeeded, false otherwise.
536    */
537   bool CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback);
538
539   /**
540    * @brief Clear the history of Web.
541    */
542   void ClearHistory();
543
544   /**
545    * @brief Clear all tiles resources of Web.
546    */
547   void ClearAllTilesResources();
548
549   /**
550    * @brief Scale the current page, centered at the given point.
551    * @param[in] scaleFactor a new factor to be scaled.
552    * @param[in] point a center coordinate.
553    */
554   void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
555
556   /**
557    * @brief Get the current scale factor of the page.
558    * @return The current scale factor.
559    */
560   float GetScaleFactor() const;
561
562   /**
563    * @brief Request to activate/deactivate the accessibility usage set by web app.
564    * @param[in] activated Activate accessibility or not.
565    */
566   void ActivateAccessibility(bool activated);
567
568   /**
569    * @brief Search and highlights the given string in the document.
570    * @param[in] text The text to find
571    * @param[in] options The options to find
572    * @param[in] maxMatchCount The maximum match count to find
573    *
574    * @return true if found & highlighted, false otherwise
575    */
576   bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
577
578   /**
579    * @brief Add dynamic certificate path.
580    * @param[in] host host that required client authentication
581    * @param[in] certPath the file path stored certificate
582    */
583   void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
584
585   /**
586    * @brief Get snapshot of the specified viewArea of page.
587    *
588    * @param[in] viewArea The rectangle of screen shot
589    * @param[in] scaleFactor The scale factor
590    *
591    * @return image view of screen shot
592    */
593   Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor);
594
595   /**
596    * @brief Request to get snapshot of the specified viewArea of page asynchronously.
597    *
598    * @param[in] viewArea The rectangle of screen shot
599    * @param[in] scaleFactor The scale factor
600    * @param[in] callback The callback for screen shot
601    *
602    * @return true if requested successfully, false otherwise
603    */
604   bool GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
605
606   /**
607    * @brief Asynchronous request to check if there is a video playing in the given view.
608    *
609    * @param[in] callback The callback called after checking if video is playing or not
610    *
611    * @return true if requested successfully, false otherwise
612    */
613   bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
614
615   /**
616    * @brief Set callback which will be called upon geolocation permission request.
617    *
618    * @param[in] callback The callback for requesting geolocation permission
619    */
620   void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
621
622   /**
623    * @brief Set or unset TTS focus of the webview.
624    * @param[in] focused True if it is gained, false lost.
625    * @note It only works when the webview does not have keyinput focus. If it has keyinput focus, the TTS focus is set automatically.
626    */
627   void SetTtsFocus(bool focused);
628
629   /**
630    * @brief Callback to be called when page loading is started.
631    *
632    * @param[in] callback
633    */
634   void RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
635
636   /**
637    * @brief Callback to be called when page loading is in progress.
638    *
639    * @param[in] callback
640    */
641   void RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
642
643   /**
644    * @brief Callback to be called when page loading is finished.
645    *
646    * @param[in] callback
647    */
648   void RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
649
650   /**
651    * @brief Callback to be called when an error occurs in page loading.
652    *
653    * @param[in] callback
654    */
655   void RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback);
656
657   /**
658    * @brief Callback to be called when scroll edge is reached.
659    *
660    * @param[in] callback
661    */
662   void RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback);
663
664   /**
665    * @brief Callback to be called when url is changed.
666    *
667    * @param[in] callback
668    */
669   void RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback);
670
671   /**
672    * @brief Callback to be called when form repost decision is requested.
673    *
674    * @param[in] callback
675    */
676   void RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback);
677
678   /**
679    * @brief Callback to be called when frame is rendered.
680    *
681    * @param[in] callback
682    */
683   void RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback);
684
685   /**
686    * @brief Callback to be called when console message will be logged.
687    *
688    * @param[in] callback
689    */
690   void RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback);
691
692   /**
693    * @brief Callback to be called when response policy would be decided.
694    *
695    * @param[in] callback
696    */
697   void RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback);
698
699   /**
700    * @brief Callback to be called when navigation policy would be decided.
701    *
702    * @param[in] callback
703    */
704   void RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback);
705
706   /**
707    * @brief Callback to be called when certificate need be confirmed.
708    *
709    * @param[in] callback
710    */
711   void RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
712
713   /**
714    * @brief Callback to be called when ssl certificate is changed.
715    *
716    * @param[in] callback
717    */
718   void RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
719
720   /**
721    * @brief Callback to be called when http authentication need be confirmed.
722    *
723    * @param[in] callback
724    */
725   void RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback);
726
727   /**
728    * @brief Callback to be called when context menu would be shown.
729    *
730    * @param[in] callback
731    */
732   void RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback);
733
734   /**
735    * @brief Callback to be called when context menu would be hidden.
736    *
737    * @param[in] callback
738    */
739   void RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback);
740
741   /**
742    * @brief Get a plain text of current web page asynchronously.
743    *
744    * @param[in] callback The callback function called asynchronously.
745    */
746   void GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback);
747
748 public: // Not intended for application developers
749   /// @cond internal
750   /**
751    * @brief Create a handle using the Toolkit::Internal implementation.
752    *
753    * @param[in] implementation The WebView implementation
754    */
755   DALI_INTERNAL WebView(Internal::WebView& implementation);
756
757   /**
758    * @brief Allow the creation of this WebView from an Internal::CustomActor pointer.
759    *
760    * @param[in] internal A pointer to the internal CustomActor
761    */
762   explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
763   /// @endcond
764 };
765
766 /**
767  * @}
768  */
769
770 } // namespace Toolkit
771
772 } // namespace Dali
773
774 #endif // DALI_TOOLKIT_WEB_VIEW_H