1 #ifndef DALI_TOOLKIT_WEB_VIEW_H
2 #define DALI_TOOLKIT_WEB_VIEW_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <dali-toolkit/public-api/controls/control.h>
26 #include <dali/devel-api/adaptor-framework/web-engine-plugin.h>
33 class WebBackForwardList;
35 class WebCookieManager;
38 namespace Internal DALI_INTERNAL
41 } // namespace DALI_INTERNAL
44 * @addtogroup dali_toolkit_controls_web_view
49 * @brief WebView is a control for displaying web content.
51 * This enables embedding web pages in the application.
53 * For working WebView, a web engine plugin for a platform should be provided.
56 class DALI_TOOLKIT_API WebView : public Control
60 * @brief Enumeration for the start and end property ranges for this control.
64 PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
65 PROPERTY_END_INDEX = PROPERTY_START_INDEX + 1000,
69 * @brief Enumeration for the instance of properties belonging to the WebView class.
76 * @brief The url to load.
77 * @details name "url", type Property::STRING.
79 URL = PROPERTY_START_INDEX,
82 * @brief The user agent string.
83 * @details name "userAgent", type Property::STRING.
88 * @brief The current position of scroll.
89 * @details name "scrollPosition", type Property::VECTOR2.
94 * @brief The current size of scroll.
95 * @details name "scrollSize", type Property::VECTOR2.
96 * @note The value is read-only.
101 * @brief The current size of content.
102 * @details name "contentSize", type Property::VECTOR2.
103 * @note The value is read-only.
108 * @brief The title of web page.
109 * @details name "title", type Property::STRING.
110 * @note The value is read-only.
115 * @brief Whether video hole is enabled or not.
116 * @details name "videoHoleEnabled", type Property::BOOLEAN.
117 * @note The value is read-only.
124 * @brief Enumeration for indicating error code of page loading.
126 enum class LoadErrorCode
128 UNKNOWN = 0, ///< Unknown.
129 CANCELED, ///< User canceled.
130 CANT_SUPPORT_MIMETYPE, ///< Can't show the page for this MIME type.
131 FAILED_FILE_IO, ///< File IO error.
132 CANT_CONNECT, ///< Cannot connect to the network.
133 CANT_LOOKUP_HOST, ///< Fail to look up host from the DNS.
134 FAILED_TLS_HANDSHAKE, ///< Fail to SSL/TLS handshake.
135 INVALID_CERTIFICATE, ///< Received certificate is invalid.
136 REQUEST_TIMEOUT, ///< Connection timeout.
137 TOO_MANY_REDIRECTS, ///< Too many redirects.
138 TOO_MANY_REQUESTS, ///< Too many requests during this load.
139 BAD_URL, ///< Malformed URL.
140 UNSUPPORTED_SCHEME, ///< Unsupported scheme.
141 AUTHENTICATION, ///< User authentication failed on the server.
142 INTERNAL_SERVER ///< Web server has an internal server error.
146 * @brief WebView signal type related with page loading.
148 using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
151 * @brief WebView signal type related with page loading error.
153 using WebViewPageLoadErrorSignalType = Signal<void(WebView, const std::string&, LoadErrorCode)>;
156 * @brief WebView signal type related with scroll edge reached.
158 using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
161 * @brief WebView signal type related with url changed.
163 using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
167 * @brief Creates an initialized WebView.
168 * @return A handle to a newly allocated Dali WebView
170 * @note WebView will not display anything
172 static WebView New();
175 * @brief Creates an initialized WebView.
177 * @param [in] locale The locale of Web
178 * @param [in] timezoneId The timezoneId of Web
180 static WebView New(const std::string& locale, const std::string& timezoneId);
183 * @brief Creates an initialized WebView.
185 * @param [in] argc The count of arguments of Applications
186 * @param [in] argv The string array of arguments of Applications
188 static WebView New(int argc, char** argv);
191 * @brief Creates an uninitialized WebView.
198 * This is non-virtual since derived Handel types must not contain data or virtual methods.
203 * @brief Copy constructor.
205 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
207 WebView(const WebView& WebView);
210 * @brief Assignment operator.
212 * @param[in] WebView The WebView to assign from
213 * @return The updated WebView
215 WebView& operator=(const WebView& WebView);
218 * @brief Downcasts a handle to WebView handle.
220 * If handle points to a WebView, the downcast produces valid handle.
221 * If not, the returned handle is left uninitialized.
223 * @param[in] handle Handle to an object
224 * @return Handle to a WebView or an uninitialized handle
226 static WebView DownCast(BaseHandle handle);
229 * @brief Get WebSettings of WebEngine.
231 Dali::Toolkit::WebSettings* GetSettings() const;
234 * @brief Get WebContext of WebEngine.
236 Dali::Toolkit::WebContext* GetContext() const;
239 * @brief Get CookieManager of WebEngine.
241 Dali::Toolkit::WebCookieManager* GetCookieManager() const;
244 * @brief Get WebBackForwardList of WebEngine.
246 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
249 * @brief Get Favicon of web page.
251 * @return Handle to a fav icon
253 Dali::Toolkit::ImageView& GetFavicon();
256 * @brief Loads a web page based on a given URL.
258 * @param [in] url The URL of the resource to load
260 void LoadUrl(const std::string& url);
263 * @brief Loads a given string as web contents.
265 * @param [in] htmlString The string to use as the contents of the web page
267 void LoadHtmlString(const std::string& htmlString);
270 * @brief Reloads the Web.
275 * @brief Stops loading web contents on the current page.
280 * @brief Suspends the operation associated with the view.
285 * @brief Resumes the operation associated with the view object after calling Suspend().
290 * @brief Scrolls the webpage of view by deltaX and deltaY.
291 * @param[in] deltaX The delta x of scroll
292 * @param[in] deltaY The delta y of scroll
294 void ScrollBy(int deltaX, int deltaY);
297 * @brief Returns whether forward is possible.
299 * @return True if forward is possible, false otherwise
304 * @brief Goes forward in the navigation history.
309 * @brief Returns whether backward is possible.
311 * @return True if backward is possible, false otherwise
316 * @brief Goes back in the navigation history.
321 * @brief Evaluates JavaScript code represented as a string.
323 * @param[in] script The JavaScript code
324 * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result.
326 void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
329 * @brief Evaluates JavaScript code represented as a string.
331 * @param[in] script The JavaScript code
333 void EvaluateJavaScript(const std::string& script);
336 * @brief Inject a JavaScript object with a message handler into the WebView.
338 * @note The injected object will appear in the JavaScript context to be loaded next.
344 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
345 * printf( "Received a message from JS: %s", message.c_str() );
348 * // Start WebView by loading URL
349 * webview.LoadUrl( url );
353 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
356 * @param[in] exposedObjectName The name of exposed object
357 * @param[in] handler The callback function
359 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
362 * @brief Register alert callback for javascript.
364 * @param[in] callback The callback function to be called by the JavaScript runtime.
366 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
369 * @brief Reply for JavaScript alert.
371 void JavaScriptAlertReply();
374 * @brief Register confirm callback for javascript.
376 * @param[in] callback The callback function to be called by the JavaScript runtime.
378 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
381 * @brief Reply for JavaScript confirm.
382 * @param[in] confirmed True if confirmed, false otherwise
384 void JavaScriptConfirmReply(bool confirmed);
387 * @brief Register prompt callback for javascript.
389 * @param[in] callback The callback function to be called by the JavaScript runtime.
391 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
394 * @brief Reply for JavaScript prompt.
395 * @param[in] result The result from input-field of prompt popup.
397 void JavaScriptPromptReply(const std::string& result);
400 * @brief Clears the history of Web.
405 * @brief Clears all tiles resources of Web.
407 void ClearAllTilesResources();
410 * @brief Connects to this signal to be notified when page loading is started.
412 * @return A signal object to connect with
414 WebViewPageLoadSignalType& PageLoadStartedSignal();
417 * @brief Connects to this signal to be notified when page loading is in progress.
419 * @return A signal object to connect with.
421 WebViewPageLoadSignalType& PageLoadInProgressSignal();
424 * @brief Connects to this signal to be notified when page loading is finished.
426 * @return A signal object to connect with
428 WebViewPageLoadSignalType& PageLoadFinishedSignal();
431 * @brief Connects to this signal to be notified when an error occurs in page loading.
433 * @return A signal object to connect with.
435 WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
438 * @brief Connects to this signal to be notified when scroll edge is reached.
440 * @return A signal object to connect with.
442 WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
445 * @brief Connects to this signal to be notified when url is changed.
447 * @return A signal object to connect with.
449 WebViewUrlChangedSignalType& UrlChangedSignal();
451 public: // Not intended for application developers
454 * @brief Creates a handle using the Toolkit::Internal implementation.
456 * @param[in] implementation The WebView implementation
458 DALI_INTERNAL WebView(Internal::WebView& implementation);
461 * @brief Allows the creation of this WebView from an Internal::CustomActor pointer.
463 * @param[in] internal A pointer to the internal CustomActor
465 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
473 } // namespace Toolkit
477 #endif // DALI_TOOLKIT_WEB_VIEW_H