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.
26 #include <dali-toolkit/public-api/controls/control.h>
27 #include <dali/devel-api/adaptor-framework/web-engine-plugin.h>
34 class WebBackForwardList;
36 class WebCookieManager;
37 class WebFormRepostDecision;
40 namespace Internal DALI_INTERNAL
43 } // namespace DALI_INTERNAL
46 * @addtogroup dali_toolkit_controls_web_view
51 * @brief WebView is a control for displaying web content.
53 * This enables embedding web pages in the application.
55 * For working WebView, a web engine plugin for a platform should be provided.
58 class DALI_TOOLKIT_API WebView : public Control
62 * @brief Enumeration for the start and end property ranges for this control.
66 PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
67 PROPERTY_END_INDEX = PROPERTY_START_INDEX + 1000,
71 * @brief Enumeration for the instance of properties belonging to the WebView class.
78 * @brief The url to load.
79 * @details name "url", type Property::STRING.
81 URL = PROPERTY_START_INDEX,
84 * @brief The user agent string.
85 * @details name "userAgent", type Property::STRING.
90 * @brief The current position of scroll.
91 * @details name "scrollPosition", type Property::VECTOR2.
96 * @brief The current size of scroll.
97 * @details name "scrollSize", type Property::VECTOR2.
98 * @note The value is read-only.
103 * @brief The current size of content.
104 * @details name "contentSize", type Property::VECTOR2.
105 * @note The value is read-only.
110 * @brief The title of web page.
111 * @details name "title", type Property::STRING.
112 * @note The value is read-only.
117 * @brief Whether video hole is enabled or not.
118 * @details name "videoHoleEnabled", type Property::BOOLEAN.
119 * @note The value is read-only.
124 * @brief Whether mouse event is enabled.
125 * @details name "mouseEventsEnabled", type Property::BOOLEAN.
126 * @note Default is true.
128 MOUSE_EVENTS_ENABLED,
131 * @brief Whether key event is enabled.
132 * @details name "keyEventsEnabled", type Property::BOOLEAN.
133 * @note Default is true.
138 * @brief The background color of web page.
139 * @details name "documentBackgroundColor", type Property::VECTOR4.
141 DOCUMENT_BACKGROUND_COLOR,
144 * @brief Whether tiles can be cleared or not when hidden.
145 * @details name "tilesClearedWhenHidden", type BOOLEAN.
147 TILES_CLEARED_WHEN_HIDDEN,
150 * @brief The multiplier of cover area of tile when rendering web page.
151 * @details name "tileCoverAreaMultiplier", type FLOAT.
153 TILE_COVER_AREA_MULTIPLIER,
156 * @brief Whether cursor is enabled or not by client.
157 * @details name "cursorEnabledByClient", type BOOLEAN.
159 CURSOR_ENABLED_BY_CLIENT,
162 * @brief The selected text of web page.
163 * @details name "selectedText", type Property::STRING.
164 * @note The value is read-only.
171 * @brief Enumeration for indicating error code of page loading.
173 enum class LoadErrorCode
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.
193 * @brief WebView signal type related with page loading.
195 using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
198 * @brief WebView signal type related with page loading error.
200 using WebViewPageLoadErrorSignalType = Signal<void(WebView, const std::string&, LoadErrorCode)>;
203 * @brief WebView signal type related with scroll edge reached.
205 using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
208 * @brief WebView signal type related with url changed.
210 using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
213 * @brief WebView signal type related with form repost decision.
215 using WebViewFormRepostDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::Toolkit::WebFormRepostDecision>)>;
218 * @brief WebView signal type related with frame rendered.
220 using WebViewFrameRenderedSignalType = Signal<void(WebView)>;
224 * @brief Creates an initialized WebView.
225 * @return A handle to a newly allocated Dali WebView
227 * @note WebView will not display anything
229 static WebView New();
232 * @brief Creates an initialized WebView.
234 * @param [in] locale The locale of Web
235 * @param [in] timezoneId The timezoneId of Web
237 static WebView New(const std::string& locale, const std::string& timezoneId);
240 * @brief Creates an initialized WebView.
242 * @param [in] argc The count of arguments of Applications
243 * @param [in] argv The string array of arguments of Applications
245 static WebView New(int argc, char** argv);
248 * @brief Creates an uninitialized WebView.
255 * This is non-virtual since derived Handel types must not contain data or virtual methods.
260 * @brief Copy constructor.
262 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
264 WebView(const WebView& WebView);
267 * @brief Assignment operator.
269 * @param[in] WebView The WebView to assign from
270 * @return The updated WebView
272 WebView& operator=(const WebView& WebView);
275 * @brief Downcasts a handle to WebView handle.
277 * If handle points to a WebView, the downcast produces valid handle.
278 * If not, the returned handle is left uninitialized.
280 * @param[in] handle Handle to an object
281 * @return Handle to a WebView or an uninitialized handle
283 static WebView DownCast(BaseHandle handle);
286 * @brief Get WebSettings of WebEngine.
288 Dali::Toolkit::WebSettings* GetSettings() const;
291 * @brief Get WebContext of WebEngine.
293 Dali::Toolkit::WebContext* GetContext() const;
296 * @brief Get CookieManager of WebEngine.
298 Dali::Toolkit::WebCookieManager* GetCookieManager() const;
301 * @brief Get WebBackForwardList of WebEngine.
303 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
306 * @brief Get Favicon of web page.
308 * @return Handle to a fav icon
310 Dali::Toolkit::ImageView& GetFavicon();
313 * @brief Loads a web page based on a given URL.
315 * @param [in] url The URL of the resource to load
317 void LoadUrl(const std::string& url);
320 * @brief Loads a given string as web contents.
322 * @param [in] htmlString The string to use as the contents of the web page
324 void LoadHtmlString(const std::string& htmlString);
327 * @brief Reloads the Web.
332 * @brief Stops loading web contents on the current page.
337 * @brief Suspends the operation associated with the view.
342 * @brief Resumes the operation associated with the view object after calling Suspend().
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
351 void ScrollBy(int deltaX, int deltaY);
354 * @brief Returns whether forward is possible.
356 * @return True if forward is possible, false otherwise
361 * @brief Goes forward in the navigation history.
366 * @brief Returns whether backward is possible.
368 * @return True if backward is possible, false otherwise
373 * @brief Goes back in the navigation history.
378 * @brief Evaluates JavaScript code represented as a string.
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.
383 void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
386 * @brief Evaluates JavaScript code represented as a string.
388 * @param[in] script The JavaScript code
390 void EvaluateJavaScript(const std::string& script);
393 * @brief Inject a JavaScript object with a message handler into the WebView.
395 * @note The injected object will appear in the JavaScript context to be loaded next.
401 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
402 * printf( "Received a message from JS: %s", message.c_str() );
405 * // Start WebView by loading URL
406 * webview.LoadUrl( url );
410 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
413 * @param[in] exposedObjectName The name of exposed object
414 * @param[in] handler The callback function
416 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
419 * @brief Register alert callback for javascript.
421 * @param[in] callback The callback function to be called by the JavaScript runtime.
423 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
426 * @brief Reply for JavaScript alert.
428 void JavaScriptAlertReply();
431 * @brief Register confirm callback for javascript.
433 * @param[in] callback The callback function to be called by the JavaScript runtime.
435 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
438 * @brief Reply for JavaScript confirm.
439 * @param[in] confirmed True if confirmed, false otherwise
441 void JavaScriptConfirmReply(bool confirmed);
444 * @brief Register prompt callback for javascript.
446 * @param[in] callback The callback function to be called by the JavaScript runtime.
448 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
451 * @brief Reply for JavaScript prompt.
452 * @param[in] result The result from input-field of prompt popup.
454 void JavaScriptPromptReply(const std::string& result);
457 * @brief Clears the history of Web.
462 * @brief Clears all tiles resources of Web.
464 void ClearAllTilesResources();
467 * @brief Connects to this signal to be notified when page loading is started.
469 * @return A signal object to connect with
471 WebViewPageLoadSignalType& PageLoadStartedSignal();
474 * @brief Connects to this signal to be notified when page loading is in progress.
476 * @return A signal object to connect with.
478 WebViewPageLoadSignalType& PageLoadInProgressSignal();
481 * @brief Connects to this signal to be notified when page loading is finished.
483 * @return A signal object to connect with
485 WebViewPageLoadSignalType& PageLoadFinishedSignal();
488 * @brief Connects to this signal to be notified when an error occurs in page loading.
490 * @return A signal object to connect with.
492 WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
495 * @brief Connects to this signal to be notified when scroll edge is reached.
497 * @return A signal object to connect with.
499 WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
502 * @brief Connects to this signal to be notified when url is changed.
504 * @return A signal object to connect with.
506 WebViewUrlChangedSignalType& UrlChangedSignal();
509 * @brief Connects to this signal to be notified when form repost decision is requested.
511 * @return A signal object to connect with.
513 WebViewFormRepostDecisionSignalType& FormRepostDecisionSignal();
516 * @brief Connects to this signal to be notified when frame is rendered.
518 * @return A signal object to connect with.
520 WebViewFrameRenderedSignalType& FrameRenderedSignal();
522 public: // Not intended for application developers
525 * @brief Creates a handle using the Toolkit::Internal implementation.
527 * @param[in] implementation The WebView implementation
529 DALI_INTERNAL WebView(Internal::WebView& implementation);
532 * @brief Allows the creation of this WebView from an Internal::CustomActor pointer.
534 * @param[in] internal A pointer to the internal CustomActor
536 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
544 } // namespace Toolkit
548 #endif // DALI_TOOLKIT_WEB_VIEW_H