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 WebCookieManager;
38 class WebFormRepostDecision;
41 namespace Internal DALI_INTERNAL
44 } // namespace DALI_INTERNAL
47 * @addtogroup dali_toolkit_controls_web_view
52 * @brief WebView is a control for displaying web content.
54 * This enables embedding web pages in the application.
56 * For working WebView, a web engine plugin for a platform should be provided.
59 class DALI_TOOLKIT_API WebView : public Control
63 * @brief Enumeration for the start and end property ranges for this control.
67 PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
68 PROPERTY_END_INDEX = PROPERTY_START_INDEX + 1000,
72 * @brief Enumeration for the instance of properties belonging to the WebView class.
79 * @brief The url to load.
80 * @details name "url", type Property::STRING.
82 URL = PROPERTY_START_INDEX,
85 * @brief The user agent string.
86 * @details name "userAgent", type Property::STRING.
91 * @brief The current position of scroll.
92 * @details name "scrollPosition", type Property::VECTOR2.
97 * @brief The current size of scroll.
98 * @details name "scrollSize", type Property::VECTOR2.
99 * @note The value is read-only.
104 * @brief The current size of content.
105 * @details name "contentSize", type Property::VECTOR2.
106 * @note The value is read-only.
111 * @brief The title of web page.
112 * @details name "title", type Property::STRING.
113 * @note The value is read-only.
118 * @brief Whether video hole is enabled or not.
119 * @details name "videoHoleEnabled", type Property::BOOLEAN.
120 * @note The value is read-only.
125 * @brief Whether mouse event is enabled.
126 * @details name "mouseEventsEnabled", type Property::BOOLEAN.
127 * @note Default is true.
129 MOUSE_EVENTS_ENABLED,
132 * @brief Whether key event is enabled.
133 * @details name "keyEventsEnabled", type Property::BOOLEAN.
134 * @note Default is true.
139 * @brief The background color of web page.
140 * @details name "documentBackgroundColor", type Property::VECTOR4.
142 DOCUMENT_BACKGROUND_COLOR,
145 * @brief Whether tiles can be cleared or not when hidden.
146 * @details name "tilesClearedWhenHidden", type BOOLEAN.
148 TILES_CLEARED_WHEN_HIDDEN,
151 * @brief The multiplier of cover area of tile when rendering web page.
152 * @details name "tileCoverAreaMultiplier", type FLOAT.
154 TILE_COVER_AREA_MULTIPLIER,
157 * @brief Whether cursor is enabled or not by client.
158 * @details name "cursorEnabledByClient", type BOOLEAN.
160 CURSOR_ENABLED_BY_CLIENT,
163 * @brief The selected text of web page.
164 * @details name "selectedText", type Property::STRING.
165 * @note The value is read-only.
170 * @brief Zoom factor of web page.
171 * @details name "pageZoomFactor", type Property::FLOAT.
176 * @brief Zoom factor of text.
177 * @details name "textZoomFactor", type Property::FLOAT.
182 * @brief progress percentage of loading a web page.
183 * @details name "loadProgressPercentage", type Property::FLOAT.
184 * @note The value is read-only.
186 LOAD_PROGRESS_PERCENTAGE,
191 * @brief WebView callback related with screen-shot captured.
193 using WebViewScreenshotCapturedCallback = std::function<void(Dali::Toolkit::ImageView)>;
196 * @brief WebView signal type related with page loading.
198 using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
201 * @brief WebView signal type related with page loading error.
203 using WebViewPageLoadErrorSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineLoadError>)>;
206 * @brief WebView signal type related with scroll edge reached.
208 using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
211 * @brief WebView signal type related with url changed.
213 using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
216 * @brief WebView signal type related with form repost decision.
218 using WebViewFormRepostDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineFormRepostDecision>)>;
221 * @brief WebView signal type related with frame rendered.
223 using WebViewFrameRenderedSignalType = Signal<void(WebView)>;
226 * @brief WebView signal type related with http request interceptor.
228 using WebViewRequestInterceptorSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineRequestInterceptor>)>;
231 * @brief WebView signal type related with console message.
233 using WebViewConsoleMessageSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineConsoleMessage>)>;
236 * @brief WebView signal type related with response policy decision.
238 using WebViewResponsePolicyDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEnginePolicyDecision>)>;
241 * @brief WebView signal type related with certificate changed.
243 using WebViewCertificateSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineCertificate>)>;
246 * @brief WebView signal type related with http authentication.
248 using WebViewHttpAuthHandlerSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineHttpAuthHandler>)>;
251 * @brief WebView signal type related with context menu shown.
253 using WebViewContextMenuShownSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineContextMenu>)>;
256 * @brief WebView signal type related with context menu hidden.
258 using WebViewContextMenuHiddenSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineContextMenu>)>;
262 * @brief Create an initialized WebView.
263 * @return A handle to a newly allocated Dali WebView
265 * @note WebView will not display anything
267 static WebView New();
270 * @brief Create an initialized WebView.
272 * @param [in] locale The locale of Web
273 * @param [in] timezoneId The timezoneId of Web
275 static WebView New(const std::string& locale, const std::string& timezoneId);
278 * @brief Create an initialized WebView.
280 * @param [in] argc The count of arguments of Applications
281 * @param [in] argv The string array of arguments of Applications
283 static WebView New(uint32_t argc, char** argv);
286 * @brief Create an uninitialized WebView.
293 * This is non-virtual since derived Handel types must not contain data or virtual methods.
298 * @brief Copy constructor.
300 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
302 WebView(const WebView& WebView);
305 * @brief Assignment operator.
307 * @param[in] WebView The WebView to assign from
308 * @return The updated WebView
310 WebView& operator=(const WebView& WebView);
313 * @brief Downcast a handle to WebView handle.
315 * If handle points to a WebView, the downcast produces valid handle.
316 * If not, the returned handle is left uninitialized.
318 * @param[in] handle Handle to an object
319 * @return Handle to a WebView or an uninitialized handle
321 static WebView DownCast(BaseHandle handle);
324 * @brief Get WebSettings of WebEngine.
326 Dali::Toolkit::WebSettings* GetSettings() const;
329 * @brief Get WebContext of WebEngine.
331 Dali::Toolkit::WebContext* GetContext() const;
334 * @brief Get CookieManager of WebEngine.
336 Dali::Toolkit::WebCookieManager* GetCookieManager() const;
339 * @brief Get WebBackForwardList of WebEngine.
341 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
344 * @brief Get favicon of web page.
346 * @return Handle to a favicon
348 Dali::Toolkit::ImageView GetFavicon() const;
351 * @brief Load a web page based on a given URL.
353 * @param [in] url The URL of the resource to load
355 void LoadUrl(const std::string& url);
358 * @brief Load a given string as web contents.
360 * @param [in] htmlString The string to use as the contents of the web page
362 void LoadHtmlString(const std::string& htmlString);
365 * @brief Load the specified html string as the content of the view overriding current history entry
367 * @param[in] html HTML data to load
368 * @param[in] basicUri Base URL used for relative paths to external objects
369 * @param[in] unreachableUrl URL that could not be reached
371 * @return true if successfully loaded, false otherwise
373 bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
376 * @brief Request loading the given contents by MIME type into the view object
378 * @param[in] contents The content to load
379 * @param[in] contentSize The size of contents (in bytes)
380 * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
381 * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
382 * @param[in] baseUri The base URI to use for relative resources
384 * @return true if successfully request, false otherwise
386 bool LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
389 * @brief Reload the Web.
394 * @brief Reload the current page's document without cache
396 bool ReloadWithoutCache();
399 * @brief Stop loading web contents on the current page.
404 * @brief Suspend the operation associated with the view.
409 * @brief Resume the operation associated with the view object after calling Suspend().
414 * @brief To suspend all url loading
416 void SuspendNetworkLoading();
419 * @brief To resume new url network loading
421 void ResumeNetworkLoading();
424 * @brief Add custom header
426 * @param[in] name custom header name to add the custom header
427 * @param[in] value custom header value to add the custom header
429 * @return true if succeeded, false otherwise
431 bool AddCustomHeader(const std::string& name, const std::string& value);
434 * @brief Remove custom header
436 * @param[in] name custom header name to remove the custom header
438 * @return true if succeeded, false otherwise
440 bool RemoveCustomHeader(const std::string& name);
443 * @brief Start the inspector server
445 * @param[in] port port number
447 * @return the port number
449 uint32_t StartInspectorServer(uint32_t port);
452 * @brief Stop the inspector server
454 * @return true if succeeded, false otherwise
456 bool StopInspectorServer();
459 * @brief Scroll web page of view by deltaX and deltaY.
460 * @param[in] deltaX The delta x of scroll
461 * @param[in] deltaY The delta y of scroll
463 void ScrollBy(int32_t deltaX, int32_t deltaY);
466 * @brief Scroll edge of view by deltaX and deltaY.
468 * @param[in] deltaX horizontal offset to scroll
469 * @param[in] deltaY vertical offset to scroll
471 * @return true if succeeded, false otherwise
473 bool ScrollEdgeBy(int32_t deltaX, int32_t deltaY);
476 * @brief Return whether forward is possible.
478 * @return True if forward is possible, false otherwise
483 * @brief Go forward in the navigation history.
488 * @brief Return whether backward is possible.
490 * @return True if backward is possible, false otherwise
495 * @brief Go back in the navigation history.
500 * @brief Evaluate JavaScript code represented as a string.
502 * @param[in] script The JavaScript code
503 * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result
505 void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
508 * @brief Evaluate JavaScript code represented as a string.
510 * @param[in] script The JavaScript code
512 void EvaluateJavaScript(const std::string& script);
515 * @brief Inject a JavaScript object with a message handler into the WebView.
517 * @note The injected object will appear in the JavaScript context to be loaded next.
523 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
524 * printf( "Received a message from JS: %s", message.c_str() );
527 * // Start WebView by loading URL
528 * webview.LoadUrl( url );
532 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
535 * @param[in] exposedObjectName The name of exposed object
536 * @param[in] handler The callback function
538 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
541 * @brief Register alert callback for javascript.
543 * @param[in] callback The callback function to be called by the JavaScript runtime.
545 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
548 * @brief Reply for JavaScript alert.
550 void JavaScriptAlertReply();
553 * @brief Register confirm callback for javascript.
555 * @param[in] callback The callback function to be called by the JavaScript runtime.
557 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
560 * @brief Reply for JavaScript confirm.
561 * @param[in] confirmed True if confirmed, false otherwise
563 void JavaScriptConfirmReply(bool confirmed);
566 * @brief Register prompt callback for javascript.
568 * @param[in] callback The callback function to be called by the JavaScript runtime.
570 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
573 * @brief Reply for JavaScript prompt.
574 * @param[in] result The result from input-field of prompt popup.
576 void JavaScriptPromptReply(const std::string& result);
579 * @brief Create a new hit test.
581 * @param[in] x the horizontal position to query
582 * @param[in] y the vertical position to query
583 * @param[in] mode the mode of hit test
585 * @return a new hit test object.
587 std::unique_ptr<Dali::WebEngineHitTest> CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode);
590 * @brief Create a hit test asynchronously.
592 * @param[in] x the horizontal position to query
593 * @param[in] y the vertical position to query
594 * @param[in] mode the mode of hit test
595 * @param[in] callback The callback function
597 * @return true if succeeded, false otherwise.
599 bool CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback);
602 * @brief Clear the history of Web.
607 * @brief Clear all tiles resources of Web.
609 void ClearAllTilesResources();
612 * @brief Scale the current page, centered at the given point.
613 * @param[in] scaleFactor a new factor to be scaled.
614 * @param[in] point a center coordinate.
616 void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
619 * @brief Get the current scale factor of the page.
620 * @return The current scale factor.
622 float GetScaleFactor() const;
625 * @brief Request to activate/deactivate the accessibility usage set by web app.
626 * @param[in] activated Activate accessibility or not.
628 void ActivateAccessibility(bool activated);
631 * @brief Search and highlights the given string in the document.
632 * @param[in] text The text to find
633 * @param[in] options The options to find
634 * @param[in] maxMatchCount The maximum match count to find
636 * @return true if found & highlighted, false otherwise
638 bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
641 * @brief Add dynamic certificate path.
642 * @param[in] host host that required client authentication
643 * @param[in] certPath the file path stored certificate
645 void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
648 * @brief Get snapshot of the specified viewArea of page.
650 * @param[in] viewArea The rectangle of screen shot
651 * @param[in] scaleFactor The scale factor
653 * @return image view of screen shot
655 Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor);
658 * @brief Request to get snapshot of the specified viewArea of page asynchronously.
660 * @param[in] viewArea The rectangle of screen shot
661 * @param[in] scaleFactor The scale factor
662 * @param[in] callback The callback for screen shot
664 * @return true if requested successfully, false otherwise
666 bool GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
669 * @brief Asynchronous request to check if there is a video playing in the given view.
671 * @param[in] callback The callback called after checking if video is playing or not
673 * @return true if requested successfully, false otherwise
675 bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
678 * @brief Set callback which will be called upon geolocation permission request.
680 * @param[in] callback The callback for requesting geolocation permission
682 void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
685 * @brief Connect to this signal to be notified when page loading is started.
687 * @return A signal object to connect with
689 WebViewPageLoadSignalType& PageLoadStartedSignal();
692 * @brief Connect to this signal to be notified when page loading is in progress.
694 * @return A signal object to connect with
696 WebViewPageLoadSignalType& PageLoadInProgressSignal();
699 * @brief Connect to this signal to be notified when page loading is finished.
701 * @return A signal object to connect with
703 WebViewPageLoadSignalType& PageLoadFinishedSignal();
706 * @brief Connect to this signal to be notified when an error occurs in page loading.
708 * @return A signal object to connect with
710 WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
713 * @brief Connect to this signal to be notified when scroll edge is reached.
715 * @return A signal object to connect with
717 WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
720 * @brief Connect to this signal to be notified when url is changed.
722 * @return A signal object to connect with
724 WebViewUrlChangedSignalType& UrlChangedSignal();
727 * @brief Connect to this signal to be notified when form repost decision is requested.
729 * @return A signal object to connect with.
731 WebViewFormRepostDecisionSignalType& FormRepostDecisionSignal();
734 * @brief Connect to this signal to be notified when frame is rendered.
736 * @return A signal object to connect with.
738 WebViewFrameRenderedSignalType& FrameRenderedSignal();
741 * @brief Connect to this signal to be notified when http request need be intercepted.
743 * @return A signal object to connect with.
745 WebViewRequestInterceptorSignalType& RequestInterceptorSignal();
748 * @brief Connect to this signal to be notified when console message will be logged.
750 * @return A signal object to connect with.
752 WebViewConsoleMessageSignalType& ConsoleMessageSignal();
755 * @brief Connect to this signal to be notified when response policy would be decided.
757 * @return A signal object to connect with.
759 WebViewResponsePolicyDecisionSignalType& ResponsePolicyDecisionSignal();
762 * @brief Connect to this signal to be notified when certificate need be confirmed.
764 * @return A signal object to connect with.
766 WebViewCertificateSignalType& CertificateConfirmSignal();
769 * @brief Connect to this signal to be notified when ssl certificate is changed.
771 * @return A signal object to connect with.
773 WebViewCertificateSignalType& SslCertificateChangedSignal();
776 * @brief Connect to this signal to be notified when http authentication need be confirmed.
778 * @return A signal object to connect with.
780 WebViewHttpAuthHandlerSignalType& HttpAuthHandlerSignal();
783 * @brief Connect to this signal to be notified when context menu would be shown.
785 * @return A signal object to connect with.
787 WebViewContextMenuShownSignalType& ContextMenuShownSignal();
790 * @brief Connect to this signal to be notified when context menu would be hidden.
792 * @return A signal object to connect with.
794 WebViewContextMenuHiddenSignalType& ContextMenuHiddenSignal();
796 public: // Not intended for application developers
799 * @brief Create a handle using the Toolkit::Internal implementation.
801 * @param[in] implementation The WebView implementation
803 DALI_INTERNAL WebView(Internal::WebView& implementation);
806 * @brief Allow the creation of this WebView from an Internal::CustomActor pointer.
808 * @param[in] internal A pointer to the internal CustomActor
810 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
818 } // namespace Toolkit
822 #endif // DALI_TOOLKIT_WEB_VIEW_H