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