Add callbacks for form repost decision and frame rendering.
[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
170   /**
171    * @brief Enumeration for indicating error code of page loading.
172    */
173   enum class LoadErrorCode
174   {
175     UNKNOWN = 0,           ///< Unknown.
176     CANCELED,              ///< User canceled.
177     CANT_SUPPORT_MIMETYPE, ///< Can't show the page for this MIME type.
178     FAILED_FILE_IO,        ///< File IO error.
179     CANT_CONNECT,          ///< Cannot connect to the network.
180     CANT_LOOKUP_HOST,      ///< Fail to look up host from the DNS.
181     FAILED_TLS_HANDSHAKE,  ///< Fail to SSL/TLS handshake.
182     INVALID_CERTIFICATE,   ///< Received certificate is invalid.
183     REQUEST_TIMEOUT,       ///< Connection timeout.
184     TOO_MANY_REDIRECTS,    ///< Too many redirects.
185     TOO_MANY_REQUESTS,     ///< Too many requests during this load.
186     BAD_URL,               ///< Malformed URL.
187     UNSUPPORTED_SCHEME,    ///< Unsupported scheme.
188     AUTHENTICATION,        ///< User authentication failed on the server.
189     INTERNAL_SERVER        ///< Web server has an internal server error.
190   };
191
192   /**
193    * @brief WebView signal type related with page loading.
194    */
195   using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
196
197   /**
198    * @brief WebView signal type related with page loading error.
199    */
200   using WebViewPageLoadErrorSignalType = Signal<void(WebView, const std::string&, LoadErrorCode)>;
201
202   /**
203    * @brief WebView signal type related with scroll edge reached.
204    */
205   using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
206
207   /**
208    * @brief WebView signal type related with url changed.
209    */
210   using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
211
212   /**
213    * @brief WebView signal type related with form repost decision.
214    */
215   using WebViewFormRepostDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::Toolkit::WebFormRepostDecision>)>;
216
217   /**
218    * @brief WebView signal type related with frame rendered.
219    */
220   using WebViewFrameRenderedSignalType = Signal<void(WebView)>;
221
222 public:
223   /**
224    * @brief Creates an initialized WebView.
225    * @return A handle to a newly allocated Dali WebView
226    *
227    * @note WebView will not display anything
228    */
229   static WebView New();
230
231   /**
232    * @brief Creates an initialized WebView.
233    *
234    * @param [in] locale The locale of Web
235    * @param [in] timezoneId The timezoneId of Web
236    */
237   static WebView New(const std::string& locale, const std::string& timezoneId);
238
239   /**
240    * @brief Creates an initialized WebView.
241    *
242    * @param [in] argc The count of arguments of Applications
243    * @param [in] argv The string array of arguments of Applications
244    */
245   static WebView New(int argc, char** argv);
246
247   /**
248    * @brief Creates an uninitialized WebView.
249    */
250   WebView();
251
252   /**
253    * @brief Destructor.
254    *
255    * This is non-virtual since derived Handel types must not contain data or virtual methods.
256    */
257   ~WebView();
258
259   /*
260    * @brief Copy constructor.
261    *
262    * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
263    */
264   WebView(const WebView& WebView);
265
266   /**
267    * @brief Assignment operator.
268    *
269    * @param[in] WebView The WebView to assign from
270    * @return The updated WebView
271    */
272   WebView& operator=(const WebView& WebView);
273
274   /**
275    * @brief Downcasts a handle to WebView handle.
276    *
277    * If handle points to a WebView, the downcast produces valid handle.
278    * If not, the returned handle is left uninitialized.
279    *
280    * @param[in] handle Handle to an object
281    * @return Handle to a WebView or an uninitialized handle
282    */
283   static WebView DownCast(BaseHandle handle);
284
285   /**
286    * @brief Get WebSettings of WebEngine.
287    */
288   Dali::Toolkit::WebSettings* GetSettings() const;
289
290   /**
291    * @brief Get WebContext of WebEngine.
292    */
293   Dali::Toolkit::WebContext* GetContext() const;
294
295   /**
296    * @brief Get CookieManager of WebEngine.
297    */
298   Dali::Toolkit::WebCookieManager* GetCookieManager() const;
299
300   /**
301    * @brief Get WebBackForwardList of WebEngine.
302    */
303   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
304
305   /**
306    * @brief Get Favicon of web page.
307    *
308    * @return Handle to a fav icon
309    */
310   Dali::Toolkit::ImageView& GetFavicon();
311
312   /**
313    * @brief Loads a web page based on a given URL.
314    *
315    * @param [in] url The URL of the resource to load
316    */
317   void LoadUrl(const std::string& url);
318
319   /**
320    * @brief Loads a given string as web contents.
321    *
322    * @param [in] htmlString The string to use as the contents of the web page
323    */
324   void LoadHtmlString(const std::string& htmlString);
325
326   /**
327    * @brief Reloads the Web.
328    */
329   void Reload();
330
331   /**
332    * @brief Stops loading web contents on the current page.
333    */
334   void StopLoading();
335
336   /**
337    * @brief Suspends the operation associated with the view.
338    */
339   void Suspend();
340
341   /**
342    * @brief Resumes the operation associated with the view object after calling Suspend().
343    */
344   void Resume();
345
346   /**
347    * @brief Scrolls the webpage of view by deltaX and deltaY.
348    * @param[in] deltaX The delta x of scroll
349    * @param[in] deltaY The delta y of scroll
350    */
351   void ScrollBy(int deltaX, int deltaY);
352
353   /**
354    * @brief Returns whether forward is possible.
355    *
356    * @return True if forward is possible, false otherwise
357    */
358   bool CanGoForward();
359
360   /**
361    * @brief Goes forward in the navigation history.
362    */
363   void GoForward();
364
365   /**
366    * @brief Returns whether backward is possible.
367    *
368    * @return True if backward is possible, false otherwise
369    */
370   bool CanGoBack();
371
372   /**
373    * @brief Goes back in the navigation history.
374    */
375   void GoBack();
376
377   /**
378    * @brief Evaluates JavaScript code represented as a string.
379    *
380    * @param[in] script The JavaScript code
381    * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result.
382    */
383   void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
384
385   /**
386    * @brief Evaluates JavaScript code represented as a string.
387    *
388    * @param[in] script The JavaScript code
389    */
390   void EvaluateJavaScript(const std::string& script);
391
392   /**
393    * @brief Inject a JavaScript object with a message handler into the WebView.
394    *
395    * @note The injected object will appear in the JavaScript context to be loaded next.
396    *
397    * Example:
398    *
399    * 1. Native
400    *
401    *     webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
402    *         printf( "Received a message from JS: %s", message.c_str() );
403    *     });
404    *
405    *     // Start WebView by loading URL
406    *     webview.LoadUrl( url );
407    *
408    * 2. JavaScript
409    *
410    *     myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
411    *
412    *
413    * @param[in] exposedObjectName The name of exposed object
414    * @param[in] handler The callback function
415    */
416   void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
417
418   /**
419    * @brief Register alert callback for javascript.
420    *
421    * @param[in] callback The callback function to be called by the JavaScript runtime.
422    */
423   void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
424
425   /**
426    * @brief Reply for JavaScript alert.
427    */
428   void JavaScriptAlertReply();
429
430   /**
431    * @brief Register confirm callback for javascript.
432    *
433    * @param[in] callback The callback function to be called by the JavaScript runtime.
434    */
435   void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
436
437   /**
438    * @brief Reply for JavaScript confirm.
439    * @param[in] confirmed True if confirmed, false otherwise
440    */
441   void JavaScriptConfirmReply(bool confirmed);
442
443   /**
444    * @brief Register prompt callback for javascript.
445    *
446    * @param[in] callback The callback function to be called by the JavaScript runtime.
447    */
448   void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
449
450   /**
451    * @brief Reply for JavaScript prompt.
452    * @param[in] result The result from input-field of prompt popup.
453    */
454   void JavaScriptPromptReply(const std::string& result);
455
456   /**
457    * @brief Clears the history of Web.
458    */
459   void ClearHistory();
460
461   /**
462    * @brief Clears all tiles resources of Web.
463    */
464   void ClearAllTilesResources();
465
466   /**
467    * @brief Connects to this signal to be notified when page loading is started.
468    *
469    * @return A signal object to connect with
470    */
471   WebViewPageLoadSignalType& PageLoadStartedSignal();
472
473   /**
474    * @brief Connects to this signal to be notified when page loading is in progress.
475    *
476    * @return A signal object to connect with.
477    */
478   WebViewPageLoadSignalType& PageLoadInProgressSignal();
479
480   /**
481    * @brief Connects to this signal to be notified when page loading is finished.
482    *
483    * @return A signal object to connect with
484    */
485   WebViewPageLoadSignalType& PageLoadFinishedSignal();
486
487   /**
488    * @brief Connects to this signal to be notified when an error occurs in page loading.
489    *
490    * @return A signal object to connect with.
491    */
492   WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
493
494   /**
495    * @brief Connects to this signal to be notified when scroll edge is reached.
496    *
497    * @return A signal object to connect with.
498    */
499   WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
500
501   /**
502    * @brief Connects to this signal to be notified when url is changed.
503    *
504    * @return A signal object to connect with.
505    */
506   WebViewUrlChangedSignalType& UrlChangedSignal();
507
508   /**
509    * @brief Connects to this signal to be notified when form repost decision is requested.
510    *
511    * @return A signal object to connect with.
512    */
513   WebViewFormRepostDecisionSignalType& FormRepostDecisionSignal();
514
515   /**
516    * @brief Connects to this signal to be notified when frame is rendered.
517    *
518    * @return A signal object to connect with.
519    */
520   WebViewFrameRenderedSignalType& FrameRenderedSignal();
521
522 public: // Not intended for application developers
523   /// @cond internal
524   /**
525    * @brief Creates a handle using the Toolkit::Internal implementation.
526    *
527    * @param[in] implementation The WebView implementation
528    */
529   DALI_INTERNAL WebView(Internal::WebView& implementation);
530
531   /**
532    * @brief Allows the creation of this WebView from an Internal::CustomActor pointer.
533    *
534    * @param[in] internal A pointer to the internal CustomActor
535    */
536   explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
537   /// @endcond
538 };
539
540 /**
541  * @}
542  */
543
544 } // namespace Toolkit
545
546 } // namespace Dali
547
548 #endif // DALI_TOOLKIT_WEB_VIEW_H