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