Add some properties into 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
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/control.h>
26 #include <dali/devel-api/adaptor-framework/web-engine-plugin.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 class ImageView;
33 class WebBackForwardList;
34 class WebContext;
35 class WebCookieManager;
36 class WebSettings;
37
38 namespace Internal DALI_INTERNAL
39 {
40 class WebView;
41 } // namespace DALI_INTERNAL
42
43 /**
44  * @addtogroup dali_toolkit_controls_web_view
45  * @{
46  */
47
48 /**
49  * @brief WebView is a control for displaying web content.
50  *
51  * This enables embedding web pages in the application.
52  *
53  * For working WebView, a web engine plugin for a platform should be provided.
54  *
55  */
56 class DALI_TOOLKIT_API WebView : public Control
57 {
58 public:
59   /**
60    * @brief Enumeration for the start and end property ranges for this control.
61    */
62   enum PropertyRange
63   {
64     PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
65     PROPERTY_END_INDEX   = PROPERTY_START_INDEX + 1000,
66   };
67
68   /**
69    * @brief Enumeration for the instance of properties belonging to the WebView class.
70    */
71   struct Property
72   {
73     enum
74     {
75       /**
76        * @brief The url to load.
77        * @details name "url", type Property::STRING.
78        */
79       URL = PROPERTY_START_INDEX,
80
81       /**
82        * @brief The user agent string.
83        * @details name "userAgent", type Property::STRING.
84        */
85       USER_AGENT,
86
87       /**
88        * @brief The current position of scroll.
89        * @details name "scrollPosition", type Property::VECTOR2.
90        */
91       SCROLL_POSITION,
92
93       /**
94        * @brief The current size of scroll.
95        * @details name "scrollSize", type Property::VECTOR2.
96        * @note The value is read-only.
97        */
98       SCROLL_SIZE,
99
100       /**
101        * @brief The current size of content.
102        * @details name "contentSize", type Property::VECTOR2.
103        * @note The value is read-only.
104        */
105       CONTENT_SIZE,
106
107       /**
108        * @brief The title of web page.
109        * @details name "title", type Property::STRING.
110        * @note The value is read-only.
111        */
112       TITLE,
113
114       /**
115        * @brief Whether video hole is enabled or not.
116        * @details name "videoHoleEnabled", type Property::BOOLEAN.
117        * @note The value is read-only.
118        */
119       VIDEO_HOLE_ENABLED,
120
121       /**
122        * @brief Whether mouse event is enabled.
123        * @details name "mouseEventsEnabled", type Property::BOOLEAN.
124        * @note Default is true.
125        */
126       MOUSE_EVENTS_ENABLED,
127
128       /**
129        * @brief Whether key event is enabled.
130        * @details name "keyEventsEnabled", type Property::BOOLEAN.
131        * @note Default is true.
132        */
133       KEY_EVENTS_ENABLED,
134
135       /**
136        * @brief The background color of web page.
137        * @details name "documentBackgroundColor", type Property::VECTOR4.
138        */
139       DOCUMENT_BACKGROUND_COLOR,
140
141       /**
142        * @brief Whether tiles can be cleared or not when hidden.
143        * @details name "tilesClearedWhenHidden", type BOOLEAN.
144        */
145       TILES_CLEARED_WHEN_HIDDEN,
146
147       /**
148        * @brief The multiplier of cover area of tile when rendering web page.
149        * @details name "tileCoverAreaMultiplier", type FLOAT.
150        */
151       TILE_COVER_AREA_MULTIPLIER,
152
153       /**
154        * @brief Whether cursor is enabled or not by client.
155        * @details name "cursorEnabledByClient", type BOOLEAN.
156        */
157       CURSOR_ENABLED_BY_CLIENT,
158
159       /**
160        * @brief The selected text of web page.
161        * @details name "selectedText", type Property::STRING.
162        * @note The value is read-only.
163        */
164       SELECTED_TEXT,
165     };
166   };
167
168   /**
169    * @brief Enumeration for indicating error code of page loading.
170    */
171   enum class LoadErrorCode
172   {
173     UNKNOWN = 0,           ///< Unknown.
174     CANCELED,              ///< User canceled.
175     CANT_SUPPORT_MIMETYPE, ///< Can't show the page for this MIME type.
176     FAILED_FILE_IO,        ///< File IO error.
177     CANT_CONNECT,          ///< Cannot connect to the network.
178     CANT_LOOKUP_HOST,      ///< Fail to look up host from the DNS.
179     FAILED_TLS_HANDSHAKE,  ///< Fail to SSL/TLS handshake.
180     INVALID_CERTIFICATE,   ///< Received certificate is invalid.
181     REQUEST_TIMEOUT,       ///< Connection timeout.
182     TOO_MANY_REDIRECTS,    ///< Too many redirects.
183     TOO_MANY_REQUESTS,     ///< Too many requests during this load.
184     BAD_URL,               ///< Malformed URL.
185     UNSUPPORTED_SCHEME,    ///< Unsupported scheme.
186     AUTHENTICATION,        ///< User authentication failed on the server.
187     INTERNAL_SERVER        ///< Web server has an internal server error.
188   };
189
190   /**
191    * @brief WebView signal type related with page loading.
192    */
193   using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
194
195   /**
196    * @brief WebView signal type related with page loading error.
197    */
198   using WebViewPageLoadErrorSignalType = Signal<void(WebView, const std::string&, LoadErrorCode)>;
199
200   /**
201    * @brief WebView signal type related with scroll edge reached.
202    */
203   using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
204
205   /**
206    * @brief WebView signal type related with url changed.
207    */
208   using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
209
210 public:
211   /**
212    * @brief Creates an initialized WebView.
213    * @return A handle to a newly allocated Dali WebView
214    *
215    * @note WebView will not display anything
216    */
217   static WebView New();
218
219   /**
220    * @brief Creates an initialized WebView.
221    *
222    * @param [in] locale The locale of Web
223    * @param [in] timezoneId The timezoneId of Web
224    */
225   static WebView New(const std::string& locale, const std::string& timezoneId);
226
227   /**
228    * @brief Creates an initialized WebView.
229    *
230    * @param [in] argc The count of arguments of Applications
231    * @param [in] argv The string array of arguments of Applications
232    */
233   static WebView New(int argc, char** argv);
234
235   /**
236    * @brief Creates an uninitialized WebView.
237    */
238   WebView();
239
240   /**
241    * @brief Destructor.
242    *
243    * This is non-virtual since derived Handel types must not contain data or virtual methods.
244    */
245   ~WebView();
246
247   /*
248    * @brief Copy constructor.
249    *
250    * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
251    */
252   WebView(const WebView& WebView);
253
254   /**
255    * @brief Assignment operator.
256    *
257    * @param[in] WebView The WebView to assign from
258    * @return The updated WebView
259    */
260   WebView& operator=(const WebView& WebView);
261
262   /**
263    * @brief Downcasts a handle to WebView handle.
264    *
265    * If handle points to a WebView, the downcast produces valid handle.
266    * If not, the returned handle is left uninitialized.
267    *
268    * @param[in] handle Handle to an object
269    * @return Handle to a WebView or an uninitialized handle
270    */
271   static WebView DownCast(BaseHandle handle);
272
273   /**
274    * @brief Get WebSettings of WebEngine.
275    */
276   Dali::Toolkit::WebSettings* GetSettings() const;
277
278   /**
279    * @brief Get WebContext of WebEngine.
280    */
281   Dali::Toolkit::WebContext* GetContext() const;
282
283   /**
284    * @brief Get CookieManager of WebEngine.
285    */
286   Dali::Toolkit::WebCookieManager* GetCookieManager() const;
287
288   /**
289    * @brief Get WebBackForwardList of WebEngine.
290    */
291   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
292
293   /**
294    * @brief Get Favicon of web page.
295    *
296    * @return Handle to a fav icon
297    */
298   Dali::Toolkit::ImageView& GetFavicon();
299
300   /**
301    * @brief Loads a web page based on a given URL.
302    *
303    * @param [in] url The URL of the resource to load
304    */
305   void LoadUrl(const std::string& url);
306
307   /**
308    * @brief Loads a given string as web contents.
309    *
310    * @param [in] htmlString The string to use as the contents of the web page
311    */
312   void LoadHtmlString(const std::string& htmlString);
313
314   /**
315    * @brief Reloads the Web.
316    */
317   void Reload();
318
319   /**
320    * @brief Stops loading web contents on the current page.
321    */
322   void StopLoading();
323
324   /**
325    * @brief Suspends the operation associated with the view.
326    */
327   void Suspend();
328
329   /**
330    * @brief Resumes the operation associated with the view object after calling Suspend().
331    */
332   void Resume();
333
334   /**
335    * @brief Scrolls the webpage of view by deltaX and deltaY.
336    * @param[in] deltaX The delta x of scroll
337    * @param[in] deltaY The delta y of scroll
338    */
339   void ScrollBy(int deltaX, int deltaY);
340
341   /**
342    * @brief Returns whether forward is possible.
343    *
344    * @return True if forward is possible, false otherwise
345    */
346   bool CanGoForward();
347
348   /**
349    * @brief Goes forward in the navigation history.
350    */
351   void GoForward();
352
353   /**
354    * @brief Returns whether backward is possible.
355    *
356    * @return True if backward is possible, false otherwise
357    */
358   bool CanGoBack();
359
360   /**
361    * @brief Goes back in the navigation history.
362    */
363   void GoBack();
364
365   /**
366    * @brief Evaluates JavaScript code represented as a string.
367    *
368    * @param[in] script The JavaScript code
369    * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result.
370    */
371   void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
372
373   /**
374    * @brief Evaluates JavaScript code represented as a string.
375    *
376    * @param[in] script The JavaScript code
377    */
378   void EvaluateJavaScript(const std::string& script);
379
380   /**
381    * @brief Inject a JavaScript object with a message handler into the WebView.
382    *
383    * @note The injected object will appear in the JavaScript context to be loaded next.
384    *
385    * Example:
386    *
387    * 1. Native
388    *
389    *     webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
390    *         printf( "Received a message from JS: %s", message.c_str() );
391    *     });
392    *
393    *     // Start WebView by loading URL
394    *     webview.LoadUrl( url );
395    *
396    * 2. JavaScript
397    *
398    *     myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
399    *
400    *
401    * @param[in] exposedObjectName The name of exposed object
402    * @param[in] handler The callback function
403    */
404   void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
405
406   /**
407    * @brief Register alert callback for javascript.
408    *
409    * @param[in] callback The callback function to be called by the JavaScript runtime.
410    */
411   void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
412
413   /**
414    * @brief Reply for JavaScript alert.
415    */
416   void JavaScriptAlertReply();
417
418   /**
419    * @brief Register confirm callback for javascript.
420    *
421    * @param[in] callback The callback function to be called by the JavaScript runtime.
422    */
423   void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
424
425   /**
426    * @brief Reply for JavaScript confirm.
427    * @param[in] confirmed True if confirmed, false otherwise
428    */
429   void JavaScriptConfirmReply(bool confirmed);
430
431   /**
432    * @brief Register prompt callback for javascript.
433    *
434    * @param[in] callback The callback function to be called by the JavaScript runtime.
435    */
436   void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
437
438   /**
439    * @brief Reply for JavaScript prompt.
440    * @param[in] result The result from input-field of prompt popup.
441    */
442   void JavaScriptPromptReply(const std::string& result);
443
444   /**
445    * @brief Clears the history of Web.
446    */
447   void ClearHistory();
448
449   /**
450    * @brief Clears all tiles resources of Web.
451    */
452   void ClearAllTilesResources();
453
454   /**
455    * @brief Connects to this signal to be notified when page loading is started.
456    *
457    * @return A signal object to connect with
458    */
459   WebViewPageLoadSignalType& PageLoadStartedSignal();
460
461   /**
462    * @brief Connects to this signal to be notified when page loading is in progress.
463    *
464    * @return A signal object to connect with.
465    */
466   WebViewPageLoadSignalType& PageLoadInProgressSignal();
467
468   /**
469    * @brief Connects to this signal to be notified when page loading is finished.
470    *
471    * @return A signal object to connect with
472    */
473   WebViewPageLoadSignalType& PageLoadFinishedSignal();
474
475   /**
476    * @brief Connects to this signal to be notified when an error occurs in page loading.
477    *
478    * @return A signal object to connect with.
479    */
480   WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
481
482   /**
483    * @brief Connects to this signal to be notified when scroll edge is reached.
484    *
485    * @return A signal object to connect with.
486    */
487   WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
488
489   /**
490    * @brief Connects to this signal to be notified when url is changed.
491    *
492    * @return A signal object to connect with.
493    */
494   WebViewUrlChangedSignalType& UrlChangedSignal();
495
496 public: // Not intended for application developers
497   /// @cond internal
498   /**
499    * @brief Creates a handle using the Toolkit::Internal implementation.
500    *
501    * @param[in] implementation The WebView implementation
502    */
503   DALI_INTERNAL WebView(Internal::WebView& implementation);
504
505   /**
506    * @brief Allows the creation of this WebView from an Internal::CustomActor pointer.
507    *
508    * @param[in] internal A pointer to the internal CustomActor
509    */
510   explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
511   /// @endcond
512 };
513
514 /**
515  * @}
516  */
517
518 } // namespace Toolkit
519
520 } // namespace Dali
521
522 #endif // DALI_TOOLKIT_WEB_VIEW_H