Merge "Implement some new ewk apis 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) 2020 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/devel-api/adaptor-framework/web-engine-plugin.h>
26 #include <dali-toolkit/public-api/controls/control.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 class WebBackForwardList;
33 class WebContext;
34 class WebCookieManager;
35 class WebSettings;
36
37 namespace Internal DALI_INTERNAL
38 {
39 class WebView;
40 } // namespace DALI_INTERNAL
41
42 /**
43  * @addtogroup dali_toolkit_controls_web_view
44  * @{
45  */
46
47 /**
48  * @brief WebView is a control for displaying web content.
49  *
50  * This enables embedding web pages in the application.
51  *
52  * For working WebView, a web engine plugin for a platform should be provided.
53  *
54  */
55 class DALI_TOOLKIT_API WebView : public Control
56 {
57 public:
58
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 position of scroll.
95        * @details Name "scrollSize", type Property::VECTOR2. Read-only.
96        */
97       SCROLL_SIZE,
98
99       /**
100        * @brief The current position of scroll.
101        * @details Name "contentSize", type Property::VECTOR2. Read-only.
102        */
103       CONTENT_SIZE,
104     };
105   };
106
107   /**
108    * @brief Enumeration for indicating error code of page loading.
109    */
110   enum class LoadErrorCode
111   {
112     /**
113      * @brief Unknown.
114      */
115     UNKNOWN = 0,
116
117     /**
118      * @brief User canceled.
119      */
120     CANCELED,
121
122     /**
123      * @brief Can't show the page for this MIME type.
124      */
125     CANT_SUPPORT_MIMETYPE,
126
127     /**
128      * @brief File IO error.
129      */
130     FAILED_FILE_IO,
131
132     /**
133      * @brief Cannot connect to the network.
134      */
135     CANT_CONNECT,
136
137     /**
138      * @brief Fail to look up host from the DNS.
139      */
140     CANT_LOOKUP_HOST,
141
142     /**
143      * @brief Fail to SSL/TLS handshake.
144      */
145     FAILED_TLS_HANDSHAKE,
146
147     /**
148      * @brief Received certificate is invalid.
149      */
150     INVALID_CERTIFICATE,
151
152     /**
153      * @brief Connection timeout.
154      */
155     REQUEST_TIMEOUT,
156
157     /**
158      * @brief Too many redirects.
159      */
160     TOO_MANY_REDIRECTS,
161
162     /**
163      * @brief Too many requests during this load.
164      */
165     TOO_MANY_REQUESTS,
166
167     /**
168      * @brief Malformed URL.
169      */
170     BAD_URL,
171
172     /**
173      * @brief Unsupported scheme.
174      */
175     UNSUPPORTED_SCHEME,
176
177     /**
178      * @brief User authentication failed on the server.
179      */
180     AUTHENTICATION,
181
182     /**
183      * @brief Web server has an internal server error.
184      */
185     INTERNAL_SERVER
186   };
187
188   /**
189    * @brief WebView signal type related with page loading.
190    */
191   typedef Signal<void(WebView, const std::string&)> WebViewPageLoadSignalType;
192
193   /**
194    * @brief WebView signal type related with page loading error.
195    */
196   typedef Signal<void(WebView, const std::string&, LoadErrorCode)> WebViewPageLoadErrorSignalType;
197
198   /**
199    * @brief WebView signal type related with scroll edge reached.
200    */
201   typedef Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)> WebViewScrollEdgeReachedSignalType;
202
203 public:
204   /**
205    * @brief Creates an initialized WebView.
206    * @return A handle to a newly allocated Dali WebView
207    *
208    * @note WebView will not display anything
209    */
210   static WebView New();
211
212   /**
213    * @brief Creates an initialized WebView.
214    *
215    * @param [in] locale The locale of Web
216    * @param [in] timezoneId The timezoneId of Web
217    */
218   static WebView New(const std::string& locale, const std::string& timezoneId);
219
220   /**
221    * @brief Creates an uninitialized WebView.
222    */
223   WebView();
224
225   /**
226    * @brief Destructor.
227    *
228    * This is non-virtual since derived Handel types must not contain data or virtual methods.
229    */
230   ~WebView();
231
232   /*
233    * @brief Copy constructor.
234    *
235    * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
236    */
237   WebView(const WebView& WebView);
238
239   /**
240    * @brief Assignment operator.
241    *
242    * @param[in] WebView The WebView to assign from
243    * @return The updated WebView
244    */
245   WebView& operator=(const WebView& WebView);
246
247   /**
248    * @brief Downcasts a handle to WebView handle.
249    *
250    * If handle points to a WebView, the downcast produces valid handle.
251    * If not, the returned handle is left uninitialized.
252    *
253    * @param[in] handle Handle to an object
254    * @return Handle to a WebView or an uninitialized handle
255    */
256   static WebView DownCast(BaseHandle handle);
257
258   /**
259    * @brief Get WebSettings of WebEngine.
260    */
261   Dali::Toolkit::WebSettings* GetSettings() const;
262
263   /**
264    * @brief Get WebContext of WebEngine.
265    */
266   Dali::Toolkit::WebContext* GetContext() const;
267
268   /**
269    * @brief Get CookieManager of WebEngine.
270    */
271   Dali::Toolkit::WebCookieManager* GetCookieManager() const;
272
273   /**
274    * @brief Get WebBackForwardList of WebEngine.
275    */
276   Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
277
278   /**
279    * @brief Loads a web page based on a given URL.
280    *
281    * @param [in] url The URL of the resource to load
282    */
283   void LoadUrl(const std::string& url);
284
285   /**
286    * @brief Loads a given string as web contents.
287    *
288    * @param [in] htmlString The string to use as the contents of the web page
289    */
290   void LoadHtmlString(const std::string& htmlString);
291
292   /**
293    * @brief Reloads the Web.
294    */
295   void Reload();
296
297   /**
298    * @brief Stops loading web contents on the current page.
299    */
300   void StopLoading();
301
302   /**
303    * @brief Suspends the operation associated with the view.
304    */
305   void Suspend();
306
307   /**
308    * @brief Resumes the operation associated with the view object after calling Suspend().
309    */
310   void Resume();
311
312   /**
313    * @brief Scrolls the webpage of view by deltaX and deltaY.
314    * @param[in] deltaX The delta x of scroll
315    * @param[in] deltaY The delta y of scroll
316    */
317   void ScrollBy( int deltaX, int deltaY );
318
319   /**
320    * @brief Returns whether forward is possible.
321    *
322    * @return True if forward is possible, false otherwise
323    */
324   bool CanGoForward();
325
326   /**
327    * @brief Goes forward in the navigation history.
328    */
329   void GoForward();
330
331   /**
332    * @brief Returns whether backward is possible.
333    *
334    * @return True if backward is possible, false otherwise
335    */
336   bool CanGoBack();
337
338   /**
339    * @brief Goes back in the navigation history.
340    */
341   void GoBack();
342
343   /**
344    * @brief Evaluates JavaScript code represented as a string.
345    *
346    * @param[in] script The JavaScript code
347    * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result.
348    */
349   void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
350
351   /**
352    * @brief Evaluates JavaScript code represented as a string.
353    *
354    * @param[in] script The JavaScript code
355    */
356   void EvaluateJavaScript(const std::string& script);
357
358   /**
359    * @brief Inject a JavaScript object with a message handler into the WebView.
360    *
361    * @note The injected object will appear in the JavaScript context to be loaded next.
362    *
363    * Example:
364    *
365    * 1. Native
366    *
367    *     webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
368    *         printf( "Received a message from JS: %s", message.c_str() );
369    *     });
370    *
371    *     // Start WebView by loading URL
372    *     webview.LoadUrl( url );
373    *
374    * 2. JavaScript
375    *
376    *     myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
377    *
378    *
379    * @param[in] exposedObjectName The name of exposed object
380    * @param[in] handler The callback function
381    */
382   void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
383
384   /**
385    * @brief Clears the history of Web.
386    */
387   void ClearHistory();
388
389   /**
390    * @brief Connects to this signal to be notified when page loading is started.
391    *
392    * @return A signal object to connect with
393    */
394   WebViewPageLoadSignalType& PageLoadStartedSignal();
395
396   /**
397    * @brief Connects to this signal to be notified when page loading is finished.
398    *
399    * @return A signal object to connect with
400    */
401   WebViewPageLoadSignalType& PageLoadFinishedSignal();
402
403   /**
404    * @brief Connects to this signal to be notified when an error occurs in page loading.
405    *
406    * @return A signal object to connect with.
407    */
408   WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
409
410   /**
411    * @brief Connects to this signal to be notified when scroll edge is reached.
412    *
413    * @return A signal object to connect with.
414    */
415   WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
416
417 public: // Not intended for application developers
418   /// @cond internal
419   /**
420    * @brief Creates a handle using the Toolkit::Internal implementation.
421    *
422    * @param[in] implementation The WebView implementation
423    */
424   DALI_INTERNAL WebView(Internal::WebView& implementation);
425
426   /**
427    * @brief Allows the creation of this WebView from an Internal::CustomActor pointer.
428    *
429    * @param[in] internal A pointer to the internal CustomActor
430    */
431   explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
432   /// @endcond
433 };
434
435 /**
436  * @}
437  */
438
439 } // namespace Toolkit
440
441 } // namespace Dali
442
443 #endif // DALI_TOOLKIT_WEB_VIEW_H