Merge "Add a callback for navigation policy in web view." into 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) 2021 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-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 Create an uninitialized WebView.
220    */
221   WebView();
222
223   /**
224    * @brief Destructor.
225    *
226    * This is non-virtual since derived Handel types must not contain data or virtual methods.
227    */
228   ~WebView();
229
230   /*
231    * @brief Copy constructor.
232    *
233    * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
234    */
235   WebView(const WebView& WebView);
236
237   /**
238    * @brief Assignment operator.
239    *
240    * @param[in] WebView The WebView to assign from
241    * @return The updated WebView
242    */
243   WebView& operator=(const WebView& WebView);
244
245   /**
246    * @brief Downcast a handle to WebView handle.
247    *
248    * If handle points to a WebView, the downcast produces valid handle.
249    * If not, the returned handle is left uninitialized.
250    *
251    * @param[in] handle Handle to an object
252    * @return Handle to a WebView or an uninitialized handle
253    */
254   static WebView DownCast(BaseHandle handle);
255
256   /**
257    * @brief Get WebSettings of WebEngine.
258    */
259   Dali::Toolkit::WebSettings* GetSettings() const;
260
261   /**
262    * @brief Get WebContext of WebEngine.
263    */
264   Dali::Toolkit::WebContext* GetContext() const;
265
266   /**
267    * @brief Get CookieManager of WebEngine.
268    */
269   Dali::Toolkit::WebCookieManager* GetCookieManager() const;
270
271   /**
272    * @brief Get WebBackForwardList of WebEngine.
273    */
274   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
275
276   /**
277    * @brief Get favicon of web page.
278    *
279    * @return Handle to a favicon
280    */
281   Dali::Toolkit::ImageView GetFavicon() const;
282
283   /**
284    * @brief Load a web page based on a given URL.
285    *
286    * @param [in] url The URL of the resource to load
287    */
288   void LoadUrl(const std::string& url);
289
290   /**
291    * @brief Load a given string as web contents.
292    *
293    * @param [in] htmlString The string to use as the contents of the web page
294    */
295   void LoadHtmlString(const std::string& htmlString);
296
297   /**
298    * @brief Load the specified html string as the content of the view overriding current history entry
299    *
300    * @param[in] html HTML data to load
301    * @param[in] basicUri Base URL used for relative paths to external objects
302    * @param[in] unreachableUrl URL that could not be reached
303    *
304    * @return true if successfully loaded, false otherwise
305    */
306   bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
307
308   /**
309    * @brief Request loading the given contents by MIME type into the view object
310    *
311    * @param[in] contents The content to load
312    * @param[in] contentSize The size of contents (in bytes)
313    * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
314    * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
315    * @param[in] baseUri The base URI to use for relative resources
316    *
317    * @return true if successfully request, false otherwise
318    */
319   bool LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
320
321   /**
322    * @brief Reload the Web.
323    */
324   void Reload();
325
326   /**
327    * @brief Reload the current page's document without cache
328    */
329   bool ReloadWithoutCache();
330
331   /**
332    * @brief Stop loading web contents on the current page.
333    */
334   void StopLoading();
335
336   /**
337    * @brief Suspend the operation associated with the view.
338    */
339   void Suspend();
340
341   /**
342    * @brief Resume the operation associated with the view object after calling Suspend().
343    */
344   void Resume();
345
346   /**
347    * @brief To suspend all url loading
348    */
349   void SuspendNetworkLoading();
350
351   /**
352    * @brief To resume new url network loading
353    */
354   void ResumeNetworkLoading();
355
356   /**
357    * @brief Add custom header
358    *
359    * @param[in] name custom header name to add the custom header
360    * @param[in] value custom header value to add the custom header
361    *
362    * @return true if succeeded, false otherwise
363    */
364   bool AddCustomHeader(const std::string& name, const std::string& value);
365
366   /**
367    * @brief Remove custom header
368    *
369    * @param[in] name custom header name to remove the custom header
370    *
371    * @return true if succeeded, false otherwise
372    */
373   bool RemoveCustomHeader(const std::string& name);
374
375   /**
376    * @brief Start the inspector server
377    *
378    * @param[in] port port number
379    *
380    * @return the port number
381    */
382   uint32_t StartInspectorServer(uint32_t port);
383
384   /**
385    * @brief Stop the inspector server
386    *
387    * @return true if succeeded, false otherwise
388    */
389   bool StopInspectorServer();
390
391   /**
392    * @brief Scroll web page of view by deltaX and deltaY.
393    * @param[in] deltaX The delta x of scroll
394    * @param[in] deltaY The delta y of scroll
395    */
396   void ScrollBy(int32_t deltaX, int32_t deltaY);
397
398   /**
399    * @brief Scroll edge of view by deltaX and deltaY.
400    *
401    * @param[in] deltaX horizontal offset to scroll
402    * @param[in] deltaY vertical offset to scroll
403    *
404    * @return true if succeeded, false otherwise
405    */
406   bool ScrollEdgeBy(int32_t deltaX, int32_t deltaY);
407
408   /**
409    * @brief Return whether forward is possible.
410    *
411    * @return True if forward is possible, false otherwise
412    */
413   bool CanGoForward();
414
415   /**
416    * @brief Go forward in the navigation history.
417    */
418   void GoForward();
419
420   /**
421    * @brief Return whether backward is possible.
422    *
423    * @return True if backward is possible, false otherwise
424    */
425   bool CanGoBack();
426
427   /**
428    * @brief Go back in the navigation history.
429    */
430   void GoBack();
431
432   /**
433    * @brief Evaluate JavaScript code represented as a string.
434    *
435    * @param[in] script The JavaScript code
436    * @param[in] resultHandler The callback to be called by the JavaScript runtime. This carries evaluation result
437    */
438   void EvaluateJavaScript(const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler);
439
440   /**
441    * @brief Evaluate JavaScript code represented as a string.
442    *
443    * @param[in] script The JavaScript code
444    */
445   void EvaluateJavaScript(const std::string& script);
446
447   /**
448    * @brief Inject a JavaScript object with a message handler into the WebView.
449    *
450    * @note The injected object will appear in the JavaScript context to be loaded next.
451    *
452    * Example:
453    *
454    * 1. Native
455    *
456    *     webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
457    *         printf( "Received a message from JS: %s", message.c_str() );
458    *     });
459    *
460    *     // Start WebView by loading URL
461    *     webview.LoadUrl( url );
462    *
463    * 2. JavaScript
464    *
465    *     myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
466    *
467    *
468    * @param[in] exposedObjectName The name of exposed object
469    * @param[in] handler The callback function
470    */
471   void AddJavaScriptMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler);
472
473   /**
474    * @brief Register alert callback for javascript.
475    *
476    * @param[in] callback The callback function to be called by the JavaScript runtime.
477    */
478   void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
479
480   /**
481    * @brief Reply for JavaScript alert.
482    */
483   void JavaScriptAlertReply();
484
485   /**
486    * @brief Register confirm callback for javascript.
487    *
488    * @param[in] callback The callback function to be called by the JavaScript runtime.
489    */
490   void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
491
492   /**
493    * @brief Reply for JavaScript confirm.
494    * @param[in] confirmed True if confirmed, false otherwise
495    */
496   void JavaScriptConfirmReply(bool confirmed);
497
498   /**
499    * @brief Register prompt callback for javascript.
500    *
501    * @param[in] callback The callback function to be called by the JavaScript runtime.
502    */
503   void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
504
505   /**
506    * @brief Reply for JavaScript prompt.
507    * @param[in] result The result from input-field of prompt popup.
508    */
509   void JavaScriptPromptReply(const std::string& result);
510
511   /**
512    * @brief Create a new hit test.
513    *
514    * @param[in] x the horizontal position to query
515    * @param[in] y the vertical position to query
516    * @param[in] mode the mode of hit test
517    *
518    * @return a new hit test object.
519    */
520   std::unique_ptr<Dali::WebEngineHitTest> CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode);
521
522   /**
523    * @brief Create a hit test asynchronously.
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    * @param[in] callback The callback function
529    *
530    * @return true if succeeded, false otherwise.
531    */
532   bool CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback);
533
534   /**
535    * @brief Clear the history of Web.
536    */
537   void ClearHistory();
538
539   /**
540    * @brief Clear all tiles resources of Web.
541    */
542   void ClearAllTilesResources();
543
544   /**
545    * @brief Scale the current page, centered at the given point.
546    * @param[in] scaleFactor a new factor to be scaled.
547    * @param[in] point a center coordinate.
548    */
549   void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
550
551   /**
552    * @brief Get the current scale factor of the page.
553    * @return The current scale factor.
554    */
555   float GetScaleFactor() const;
556
557   /**
558    * @brief Request to activate/deactivate the accessibility usage set by web app.
559    * @param[in] activated Activate accessibility or not.
560    */
561   void ActivateAccessibility(bool activated);
562
563   /**
564    * @brief Search and highlights the given string in the document.
565    * @param[in] text The text to find
566    * @param[in] options The options to find
567    * @param[in] maxMatchCount The maximum match count to find
568    *
569    * @return true if found & highlighted, false otherwise
570    */
571   bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
572
573   /**
574    * @brief Add dynamic certificate path.
575    * @param[in] host host that required client authentication
576    * @param[in] certPath the file path stored certificate
577    */
578   void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
579
580   /**
581    * @brief Get snapshot of the specified viewArea of page.
582    *
583    * @param[in] viewArea The rectangle of screen shot
584    * @param[in] scaleFactor The scale factor
585    *
586    * @return image view of screen shot
587    */
588   Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor);
589
590   /**
591    * @brief Request to get snapshot of the specified viewArea of page asynchronously.
592    *
593    * @param[in] viewArea The rectangle of screen shot
594    * @param[in] scaleFactor The scale factor
595    * @param[in] callback The callback for screen shot
596    *
597    * @return true if requested successfully, false otherwise
598    */
599   bool GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
600
601   /**
602    * @brief Asynchronous request to check if there is a video playing in the given view.
603    *
604    * @param[in] callback The callback called after checking if video is playing or not
605    *
606    * @return true if requested successfully, false otherwise
607    */
608   bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
609
610   /**
611    * @brief Set callback which will be called upon geolocation permission request.
612    *
613    * @param[in] callback The callback for requesting geolocation permission
614    */
615   void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
616
617   /**
618    * @brief Set or unset TTS focus of the webview.
619    * @param[in] focused True if it is gained, false lost.
620    * @note It only works when the webview does not have keyinput focus. If it has keyinput focus, the TTS focus is set automatically.
621    */
622   void SetTtsFocus(bool focused);
623
624   /**
625    * @brief Callback to be called when page loading is started.
626    *
627    * @param[in] callback
628    */
629   void RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
630
631   /**
632    * @brief Callback to be called when page loading is in progress.
633    *
634    * @param[in] callback
635    */
636   void RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
637
638   /**
639    * @brief Callback to be called when page loading is finished.
640    *
641    * @param[in] callback
642    */
643   void RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
644
645   /**
646    * @brief Callback to be called when an error occurs in page loading.
647    *
648    * @param[in] callback
649    */
650   void RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback);
651
652   /**
653    * @brief Callback to be called when scroll edge is reached.
654    *
655    * @param[in] callback
656    */
657   void RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback);
658
659   /**
660    * @brief Callback to be called when url is changed.
661    *
662    * @param[in] callback
663    */
664   void RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback);
665
666   /**
667    * @brief Callback to be called when form repost decision is requested.
668    *
669    * @param[in] callback
670    */
671   void RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback);
672
673   /**
674    * @brief Callback to be called when frame is rendered.
675    *
676    * @param[in] callback
677    */
678   void RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback);
679
680   /**
681    * @brief Callback to be called when console message will be logged.
682    *
683    * @param[in] callback
684    */
685   void RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback);
686
687   /**
688    * @brief Callback to be called when response policy would be decided.
689    *
690    * @param[in] callback
691    */
692   void RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback);
693
694   /**
695    * @brief Callback to be called when navigation policy would be decided.
696    *
697    * @param[in] callback
698    */
699   void RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback);
700
701   /**
702    * @brief Callback to be called when certificate need be confirmed.
703    *
704    * @param[in] callback
705    */
706   void RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
707
708   /**
709    * @brief Callback to be called when ssl certificate is changed.
710    *
711    * @param[in] callback
712    */
713   void RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
714
715   /**
716    * @brief Callback to be called when http authentication need be confirmed.
717    *
718    * @param[in] callback
719    */
720   void RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback);
721
722   /**
723    * @brief Callback to be called when context menu would be shown.
724    *
725    * @param[in] callback
726    */
727   void RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback);
728
729   /**
730    * @brief Callback to be called when context menu would be hidden.
731    *
732    * @param[in] callback
733    */
734   void RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback);
735
736   /**
737    * @brief Get a plain text of current web page asynchronously.
738    *
739    * @param[in] callback The callback function called asynchronously.
740    */
741   void GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback);
742
743 public: // Not intended for application developers
744   /// @cond internal
745   /**
746    * @brief Create a handle using the Toolkit::Internal implementation.
747    *
748    * @param[in] implementation The WebView implementation
749    */
750   DALI_INTERNAL WebView(Internal::WebView& implementation);
751
752   /**
753    * @brief Allow the creation of this WebView from an Internal::CustomActor pointer.
754    *
755    * @param[in] internal A pointer to the internal CustomActor
756    */
757   explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
758   /// @endcond
759 };
760
761 /**
762  * @}
763  */
764
765 } // namespace Toolkit
766
767 } // namespace Dali
768
769 #endif // DALI_TOOLKIT_WEB_VIEW_H