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;
37 class WebContextMenuItem;
38 class WebCookieManager;
39 class WebFormRepostDecision;
42 namespace Internal DALI_INTERNAL
45 } // namespace DALI_INTERNAL
48 * @addtogroup dali_toolkit_controls_web_view
53 * @brief WebView is a control for displaying web content.
55 * This enables embedding web pages in the application.
57 * For working WebView, a web engine plugin for a platform should be provided.
60 class DALI_TOOLKIT_API WebView : public Control
64 * @brief Enumeration for the start and end property ranges for this control.
68 PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
69 PROPERTY_END_INDEX = PROPERTY_START_INDEX + 1000,
73 * @brief Enumeration for the instance of properties belonging to the WebView class.
80 * @brief The url to load.
81 * @details name "url", type Property::STRING.
83 URL = PROPERTY_START_INDEX,
86 * @brief The user agent string.
87 * @details name "userAgent", type Property::STRING.
92 * @brief The current position of scroll.
93 * @details name "scrollPosition", type Property::VECTOR2.
98 * @brief The current size of scroll.
99 * @details name "scrollSize", type Property::VECTOR2.
100 * @note The value is read-only.
105 * @brief The current size of content.
106 * @details name "contentSize", type Property::VECTOR2.
107 * @note The value is read-only.
112 * @brief The title of web page.
113 * @details name "title", type Property::STRING.
114 * @note The value is read-only.
119 * @brief Whether video hole is enabled or not.
120 * @details name "videoHoleEnabled", type Property::BOOLEAN.
121 * @note The value is read-only.
126 * @brief Whether mouse event is enabled.
127 * @details name "mouseEventsEnabled", type Property::BOOLEAN.
128 * @note Default is true.
130 MOUSE_EVENTS_ENABLED,
133 * @brief Whether key event is enabled.
134 * @details name "keyEventsEnabled", type Property::BOOLEAN.
135 * @note Default is true.
140 * @brief The background color of web page.
141 * @details name "documentBackgroundColor", type Property::VECTOR4.
143 DOCUMENT_BACKGROUND_COLOR,
146 * @brief Whether tiles can be cleared or not when hidden.
147 * @details name "tilesClearedWhenHidden", type BOOLEAN.
149 TILES_CLEARED_WHEN_HIDDEN,
152 * @brief The multiplier of cover area of tile when rendering web page.
153 * @details name "tileCoverAreaMultiplier", type FLOAT.
155 TILE_COVER_AREA_MULTIPLIER,
158 * @brief Whether cursor is enabled or not by client.
159 * @details name "cursorEnabledByClient", type BOOLEAN.
161 CURSOR_ENABLED_BY_CLIENT,
164 * @brief The selected text of web page.
165 * @details name "selectedText", type Property::STRING.
166 * @note The value is read-only.
171 * @brief Zoom factor of web page.
172 * @details name "pageZoomFactor", type Property::FLOAT.
177 * @brief Zoom factor of text.
178 * @details name "textZoomFactor", type Property::FLOAT.
183 * @brief progress percentage of loading a web page.
184 * @details name "loadProgressPercentage", type Property::FLOAT.
185 * @note The value is read-only.
187 LOAD_PROGRESS_PERCENTAGE,
192 * @brief WebView callback related with screen-shot captured.
194 using WebViewScreenshotCapturedCallback = std::function<void(Dali::Toolkit::ImageView)>;
197 * @brief WebView signal type related with page loading.
199 using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
202 * @brief WebView signal type related with page loading error.
204 using WebViewPageLoadErrorSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineLoadError>)>;
207 * @brief WebView signal type related with scroll edge reached.
209 using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
212 * @brief WebView signal type related with url changed.
214 using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
217 * @brief WebView signal type related with form repost decision.
219 using WebViewFormRepostDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineFormRepostDecision>)>;
222 * @brief WebView signal type related with frame rendered.
224 using WebViewFrameRenderedSignalType = Signal<void(WebView)>;
227 * @brief WebView signal type related with http request interceptor.
229 using WebViewRequestInterceptorSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineRequestInterceptor>)>;
232 * @brief WebView signal type related with console message.
234 using WebViewConsoleMessageSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineConsoleMessage>)>;
237 * @brief WebView signal type related with response policy decision.
239 using WebViewResponsePolicyDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEnginePolicyDecision>)>;
242 * @brief WebView signal type related with certificate changed.
244 using WebViewCertificateSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineCertificate>)>;
247 * @brief WebView signal type related with http authentication.
249 using WebViewHttpAuthHandlerSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineHttpAuthHandler>)>;
252 * @brief WebView signal type related with context menu customized.
254 using WebViewContextMenuCustomizedSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineContextMenu>)>;
257 * @brief WebView signal type related with context menu item selected.
259 using WebViewContextMenuItemSelectedSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineContextMenuItem>)>;
263 * @brief Create an initialized WebView.
264 * @return A handle to a newly allocated Dali WebView
266 * @note WebView will not display anything
268 static WebView New();
271 * @brief Create an initialized WebView.
273 * @param [in] locale The locale of Web
274 * @param [in] timezoneId The timezoneId of Web
276 static WebView New(const std::string& locale, const std::string& timezoneId);
279 * @brief Create an initialized WebView.
281 * @param [in] argc The count of arguments of Applications
282 * @param [in] argv The string array of arguments of Applications
284 static WebView New(uint32_t argc, char** argv);
287 * @brief Create an uninitialized WebView.
294 * This is non-virtual since derived Handel types must not contain data or virtual methods.
299 * @brief Copy constructor.
301 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
303 WebView(const WebView& WebView);
306 * @brief Assignment operator.
308 * @param[in] WebView The WebView to assign from
309 * @return The updated WebView
311 WebView& operator=(const WebView& WebView);
314 * @brief Downcast a handle to WebView handle.
316 * If handle points to a WebView, the downcast produces valid handle.
317 * If not, the returned handle is left uninitialized.
319 * @param[in] handle Handle to an object
320 * @return Handle to a WebView or an uninitialized handle
322 static WebView DownCast(BaseHandle handle);
325 * @brief Get WebSettings of WebEngine.
327 Dali::Toolkit::WebSettings* GetSettings() const;
330 * @brief Get WebContext of WebEngine.
332 Dali::Toolkit::WebContext* GetContext() const;
335 * @brief Get CookieManager of WebEngine.
337 Dali::Toolkit::WebCookieManager* GetCookieManager() const;
340 * @brief Get WebBackForwardList of WebEngine.
342 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
345 * @brief Get favicon of web page.
347 * @return Handle to a favicon
349 Dali::Toolkit::ImageView GetFavicon() const;
352 * @brief Load a web page based on a given URL.
354 * @param [in] url The URL of the resource to load
356 void LoadUrl(const std::string& url);
359 * @brief Load a given string as web contents.
361 * @param [in] htmlString The string to use as the contents of the web page
363 void LoadHtmlString(const std::string& htmlString);
366 * @brief Load the specified html string as the content of the view overriding current history entry
368 * @param[in] html HTML data to load
369 * @param[in] basicUri Base URL used for relative paths to external objects
370 * @param[in] unreachableUrl URL that could not be reached
372 * @return true if successfully loaded, false otherwise
374 bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
377 * @brief Request loading the given contents by MIME type into the view object
379 * @param[in] contents The content to load
380 * @param[in] contentSize The size of contents (in bytes)
381 * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
382 * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
383 * @param[in] baseUri The base URI to use for relative resources
385 * @return true if successfully request, false otherwise
387 bool LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
390 * @brief Reload the Web.
395 * @brief Reload the current page's document without cache
397 bool ReloadWithoutCache();
400 * @brief Stop loading web contents on the current page.
405 * @brief Suspend the operation associated with the view.
410 * @brief Resume the operation associated with the view object after calling Suspend().
415 * @brief To suspend all url loading
417 void SuspendNetworkLoading();
420 * @brief To resume new url network loading
422 void ResumeNetworkLoading();
425 * @brief Add custom header
427 * @param[in] name custom header name to add the custom header
428 * @param[in] value custom header value to add the custom header
430 * @return true if succeeded, false otherwise
432 bool AddCustomHeader(const std::string& name, const std::string& value);
435 * @brief Remove custom header
437 * @param[in] name custom header name to remove the custom header
439 * @return true if succeeded, false otherwise
441 bool RemoveCustomHeader(const std::string& name);
444 * @brief Start the inspector server
446 * @param[in] port port number
448 * @return the port number
450 uint32_t StartInspectorServer(uint32_t port);
453 * @brief Stop the inspector server
455 * @return true if succeeded, false otherwise
457 bool StopInspectorServer();
460 * @brief Scroll web page of view by deltaX and deltaY.
461 * @param[in] deltaX The delta x of scroll
462 * @param[in] deltaY The delta y of scroll
464 void ScrollBy(int32_t deltaX, int32_t deltaY);
467 * @brief Scroll edge of view by deltaX and deltaY.
469 * @param[in] deltaX horizontal offset to scroll
470 * @param[in] deltaY vertical offset to scroll
472 * @return true if succeeded, false otherwise
474 bool ScrollEdgeBy(int32_t deltaX, int32_t deltaY);
477 * @brief Return whether forward is possible.
479 * @return True if forward is possible, false otherwise
484 * @brief Go forward in the navigation history.
489 * @brief Return whether backward is possible.
491 * @return True if backward is possible, false otherwise
496 * @brief Go back in the navigation history.
501 * @brief Evaluate JavaScript code represented as a string.
503 * @param[in] script The JavaScript code
504 * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result
506 void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
509 * @brief Evaluate JavaScript code represented as a string.
511 * @param[in] script The JavaScript code
513 void EvaluateJavaScript(const std::string& script);
516 * @brief Inject a JavaScript object with a message handler into the WebView.
518 * @note The injected object will appear in the JavaScript context to be loaded next.
524 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
525 * printf( "Received a message from JS: %s", message.c_str() );
528 * // Start WebView by loading URL
529 * webview.LoadUrl( url );
533 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
536 * @param[in] exposedObjectName The name of exposed object
537 * @param[in] handler The callback function
539 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
542 * @brief Register alert callback for javascript.
544 * @param[in] callback The callback function to be called by the JavaScript runtime.
546 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
549 * @brief Reply for JavaScript alert.
551 void JavaScriptAlertReply();
554 * @brief Register confirm callback for javascript.
556 * @param[in] callback The callback function to be called by the JavaScript runtime.
558 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
561 * @brief Reply for JavaScript confirm.
562 * @param[in] confirmed True if confirmed, false otherwise
564 void JavaScriptConfirmReply(bool confirmed);
567 * @brief Register prompt callback for javascript.
569 * @param[in] callback The callback function to be called by the JavaScript runtime.
571 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
574 * @brief Reply for JavaScript prompt.
575 * @param[in] result The result from input-field of prompt popup.
577 void JavaScriptPromptReply(const std::string& result);
580 * @brief Create a new hit test.
582 * @param[in] x the horizontal position to query
583 * @param[in] y the vertical position to query
584 * @param[in] mode the mode of hit test
586 * @return a new hit test object.
588 std::unique_ptr<Dali::WebEngineHitTest> CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode);
591 * @brief Create a hit test asynchronously.
593 * @param[in] x the horizontal position to query
594 * @param[in] y the vertical position to query
595 * @param[in] mode the mode of hit test
596 * @param[in] callback The callback function
598 * @return true if succeeded, false otherwise.
600 bool CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback);
603 * @brief Clear the history of Web.
608 * @brief Clear all tiles resources of Web.
610 void ClearAllTilesResources();
613 * @brief Scale the current page, centered at the given point.
614 * @param[in] scaleFactor a new factor to be scaled.
615 * @param[in] point a center coordinate.
617 void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
620 * @brief Get the current scale factor of the page.
621 * @return The current scale factor.
623 float GetScaleFactor() const;
626 * @brief Request to activate/deactivate the accessibility usage set by web app.
627 * @param[in] activated Activate accessibility or not.
629 void ActivateAccessibility(bool activated);
632 * @brief Search and highlights the given string in the document.
633 * @param[in] text The text to find
634 * @param[in] options The options to find
635 * @param[in] maxMatchCount The maximum match count to find
637 * @return true if found & highlighted, false otherwise
639 bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
642 * @brief Add dynamic certificate path.
643 * @param[in] host host that required client authentication
644 * @param[in] certPath the file path stored certificate
646 void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
649 * @brief Get snapshot of the specified viewArea of page.
651 * @param[in] viewArea The rectangle of screen shot
652 * @param[in] scaleFactor The scale factor
654 * @return image view of screen shot
656 Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor);
659 * @brief Request to get snapshot of the specified viewArea of page asynchronously.
661 * @param[in] viewArea The rectangle of screen shot
662 * @param[in] scaleFactor The scale factor
663 * @param[in] callback The callback for screen shot
665 * @return true if requested successfully, false otherwise
667 bool GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
670 * @brief Asynchronous request to check if there is a video playing in the given view.
672 * @param[in] callback The callback called after checking if video is playing or not
674 * @return true if requested successfully, false otherwise
676 bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
679 * @brief Set callback which will be called upon geolocation permission request.
681 * @param[in] callback The callback for requesting geolocation permission
683 void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
686 * @brief Connect to this signal to be notified when page loading is started.
688 * @return A signal object to connect with
690 WebViewPageLoadSignalType& PageLoadStartedSignal();
693 * @brief Connect to this signal to be notified when page loading is in progress.
695 * @return A signal object to connect with
697 WebViewPageLoadSignalType& PageLoadInProgressSignal();
700 * @brief Connect to this signal to be notified when page loading is finished.
702 * @return A signal object to connect with
704 WebViewPageLoadSignalType& PageLoadFinishedSignal();
707 * @brief Connect to this signal to be notified when an error occurs in page loading.
709 * @return A signal object to connect with
711 WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
714 * @brief Connect to this signal to be notified when scroll edge is reached.
716 * @return A signal object to connect with
718 WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
721 * @brief Connect to this signal to be notified when url is changed.
723 * @return A signal object to connect with
725 WebViewUrlChangedSignalType& UrlChangedSignal();
728 * @brief Connect to this signal to be notified when form repost decision is requested.
730 * @return A signal object to connect with.
732 WebViewFormRepostDecisionSignalType& FormRepostDecisionSignal();
735 * @brief Connect to this signal to be notified when frame is rendered.
737 * @return A signal object to connect with.
739 WebViewFrameRenderedSignalType& FrameRenderedSignal();
742 * @brief Connect to this signal to be notified when http request need be intercepted.
744 * @return A signal object to connect with.
746 WebViewRequestInterceptorSignalType& RequestInterceptorSignal();
749 * @brief Connect to this signal to be notified when console message will be logged.
751 * @return A signal object to connect with.
753 WebViewConsoleMessageSignalType& ConsoleMessageSignal();
756 * @brief Connect to this signal to be notified when response policy would be decided.
758 * @return A signal object to connect with.
760 WebViewResponsePolicyDecisionSignalType& ResponsePolicyDecisionSignal();
763 * @brief Connect to this signal to be notified when certificate need be confirmed.
765 * @return A signal object to connect with.
767 WebViewCertificateSignalType& CertificateConfirmSignal();
770 * @brief Connect to this signal to be notified when ssl certificate is changed.
772 * @return A signal object to connect with.
774 WebViewCertificateSignalType& SslCertificateChangedSignal();
777 * @brief Connect to this signal to be notified when http authentication need be confirmed.
779 * @return A signal object to connect with.
781 WebViewHttpAuthHandlerSignalType& HttpAuthHandlerSignal();
784 * @brief Connect to this signal to be notified when context menu would be customized.
786 * @return A signal object to connect with.
788 WebViewContextMenuCustomizedSignalType& ContextMenuCustomizedSignal();
791 * @brief Connect to this signal to be notified when context menu item is selected.
793 * @return A signal object to connect with.
795 WebViewContextMenuItemSelectedSignalType& ContextMenuItemSelectedSignal();
797 public: // Not intended for application developers
800 * @brief Create a handle using the Toolkit::Internal implementation.
802 * @param[in] implementation The WebView implementation
804 DALI_INTERNAL WebView(Internal::WebView& implementation);
807 * @brief Allow the creation of this WebView from an Internal::CustomActor pointer.
809 * @param[in] internal A pointer to the internal CustomActor
811 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
819 } // namespace Toolkit
823 #endif // DALI_TOOLKIT_WEB_VIEW_H