1 #ifndef DALI_TOOLKIT_WEB_VIEW_H
2 #define DALI_TOOLKIT_WEB_VIEW_H
5 * Copyright (c) 2024 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/web-engine-plugin.h>
31 class WebEngineContext;
32 class WebEngineCookieManager;
37 class WebBackForwardList;
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 False by default.
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.
169 * @brief Zoom factor of web page.
170 * @details name "pageZoomFactor", type Property::FLOAT.
175 * @brief Zoom factor of text.
176 * @details name "textZoomFactor", type Property::FLOAT.
181 * @brief progress percentage of loading a web page.
182 * @details name "loadProgressPercentage", type Property::FLOAT.
183 * @note The value is read-only.
185 LOAD_PROGRESS_PERCENTAGE,
190 * @brief WebView callback related with screen-shot captured.
192 using WebViewScreenshotCapturedCallback = std::function<void(Dali::Toolkit::ImageView)>;
196 * @brief Create an initialized WebView.
197 * @return A handle to a newly allocated Dali WebView
199 * @note WebView will not display anything
201 static WebView New();
204 * @brief Create an initialized WebView.
206 * @param [in] locale The locale of Web
207 * @param [in] timezoneId The timezoneId of Web
209 static WebView New(const std::string& locale, const std::string& timezoneId);
212 * @brief Create an initialized WebView.
214 * @param [in] argc The count of arguments of Applications
215 * @param [in] argv The string array of arguments of Applications
217 static WebView New(uint32_t argc, char** argv);
220 * @brief Find web view by web engine plugin.
222 static Toolkit::WebView FindWebView(Dali::WebEnginePlugin* plugin);
225 * @brief Get context of web engine.
227 static Dali::WebEngineContext* GetContext();
230 * @brief Get cookie manager of web engine.
232 static Dali::WebEngineCookieManager* GetCookieManager();
235 * @brief Create an uninitialized WebView.
242 * This is non-virtual since derived Handel types must not contain data or virtual methods.
247 * @brief Copy constructor.
249 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
251 WebView(const WebView& WebView);
254 * @brief Assignment operator.
256 * @param[in] WebView The WebView to assign from
257 * @return The updated WebView
259 WebView& operator=(const WebView& WebView);
262 * @brief Downcast a handle to WebView handle.
264 * If handle points to a WebView, the downcast produces valid handle.
265 * If not, the returned handle is left uninitialized.
267 * @param[in] handle Handle to an object
268 * @return Handle to a WebView or an uninitialized handle
270 static WebView DownCast(BaseHandle handle);
273 * @brief Change orientation.
275 void ChangeOrientation(int orientation);
278 * @brief Get WebSettings of WebEngine.
280 Dali::Toolkit::WebSettings* GetSettings() const;
283 * @brief Get WebBackForwardList of WebEngine.
285 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
288 * @brief Gets web engine plugin.
290 Dali::WebEnginePlugin* GetPlugin() const;
293 * @brief Get favicon of web page.
295 * @return Handle to a favicon
297 Dali::Toolkit::ImageView GetFavicon() const;
300 * @brief Load a web page based on a given URL.
302 * @param [in] url The URL of the resource to load
304 void LoadUrl(const std::string& url);
307 * @brief Load a given string as web contents.
309 * @param [in] htmlString The string to use as the contents of the web page
311 void LoadHtmlString(const std::string& htmlString);
314 * @brief Load the specified html string as the content of the view overriding current history entry
316 * @param[in] html HTML data to load
317 * @param[in] basicUri Base URL used for relative paths to external objects
318 * @param[in] unreachableUrl URL that could not be reached
320 * @return true if successfully loaded, false otherwise
322 bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
325 * @brief Request loading the given contents by MIME type into the view object
327 * @param[in] contents The content to load
328 * @param[in] contentSize The size of contents (in bytes)
329 * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
330 * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
331 * @param[in] baseUri The base URI to use for relative resources
333 * @return true if successfully request, false otherwise
335 bool LoadContents(const int8_t* contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
338 * @brief Reload the Web.
343 * @brief Reload the current page's document without cache
345 bool ReloadWithoutCache();
348 * @brief Stop loading web contents on the current page.
353 * @brief Suspend the operation associated with the view.
358 * @brief Resume the operation associated with the view object after calling Suspend().
363 * @brief To suspend all url loading
365 void SuspendNetworkLoading();
368 * @brief To resume new url network loading
370 void ResumeNetworkLoading();
373 * @brief Add custom header
375 * @param[in] name custom header name to add the custom header
376 * @param[in] value custom header value to add the custom header
378 * @return true if succeeded, false otherwise
380 bool AddCustomHeader(const std::string& name, const std::string& value);
383 * @brief Remove custom header
385 * @param[in] name custom header name to remove the custom header
387 * @return true if succeeded, false otherwise
389 bool RemoveCustomHeader(const std::string& name);
392 * @brief Start the inspector server
394 * @param[in] port port number
396 * @return the port number
398 uint32_t StartInspectorServer(uint32_t port);
401 * @brief Stop the inspector server
403 * @return true if succeeded, false otherwise
405 bool StopInspectorServer();
408 * @brief Scroll web page of view by deltaX and deltaY.
409 * @param[in] deltaX The delta x of scroll
410 * @param[in] deltaY The delta y of scroll
412 void ScrollBy(int32_t deltaX, int32_t deltaY);
415 * @brief Scroll edge of view by deltaX and deltaY.
417 * @param[in] deltaX horizontal offset to scroll
418 * @param[in] deltaY vertical offset to scroll
420 * @return true if succeeded, false otherwise
422 bool ScrollEdgeBy(int32_t deltaX, int32_t deltaY);
425 * @brief Return whether forward is possible.
427 * @return True if forward is possible, false otherwise
432 * @brief Go forward in the navigation history.
437 * @brief Return whether backward is possible.
439 * @return True if backward is possible, false otherwise
444 * @brief Go back in the navigation history.
449 * @brief Evaluate JavaScript code represented as a string.
451 * @param[in] script The JavaScript code
452 * @param[in] resultHandler The callback to be called by the JavaScript runtime. This carries evaluation result
454 void EvaluateJavaScript(const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler);
457 * @brief Evaluate JavaScript code represented as a string.
459 * @param[in] script The JavaScript code
461 void EvaluateJavaScript(const std::string& script);
464 * @brief Inject a JavaScript object with a message handler into the WebView.
466 * @note The injected object will appear in the JavaScript context to be loaded next.
472 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
473 * printf( "Received a message from JS: %s", message.c_str() );
476 * // Start WebView by loading URL
477 * webview.LoadUrl( url );
481 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
484 * @param[in] exposedObjectName The name of exposed object
485 * @param[in] handler The callback function
487 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler);
490 * @brief Inject a JavaScript object with a message handler into the WebView.
492 * @param[in] exposedObjectName The name of exposed object
493 * @param[in] handler The callback function
495 void AddJavaScriptEntireMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptEntireMessageHandlerCallback handler);
498 * @brief Register alert callback for javascript.
500 * @param[in] callback The callback function to be called by the JavaScript runtime.
502 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
505 * @brief Reply for JavaScript alert.
507 void JavaScriptAlertReply();
510 * @brief Register confirm callback for javascript.
512 * @param[in] callback The callback function to be called by the JavaScript runtime.
514 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
517 * @brief Reply for JavaScript confirm.
518 * @param[in] confirmed True if confirmed, false otherwise
520 void JavaScriptConfirmReply(bool confirmed);
523 * @brief Register prompt callback for javascript.
525 * @param[in] callback The callback function to be called by the JavaScript runtime.
527 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
530 * @brief Reply for JavaScript prompt.
531 * @param[in] result The result from input-field of prompt popup.
533 void JavaScriptPromptReply(const std::string& result);
536 * @brief Create a new hit test.
538 * @param[in] x the horizontal position to query
539 * @param[in] y the vertical position to query
540 * @param[in] mode the mode of hit test
542 * @return a new hit test object.
544 std::unique_ptr<Dali::WebEngineHitTest> CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode);
547 * @brief Create a hit test asynchronously.
549 * @param[in] x the horizontal position to query
550 * @param[in] y the vertical position to query
551 * @param[in] mode the mode of hit test
552 * @param[in] callback The callback function
554 * @return true if succeeded, false otherwise.
556 bool CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback);
559 * @brief Exit fullscreen.
561 void ExitFullscreen();
564 * @brief Clear the history of Web.
569 * @brief Clear all tiles resources of Web.
571 void ClearAllTilesResources();
574 * @brief Scale the current page, centered at the given point.
575 * @param[in] scaleFactor a new factor to be scaled.
576 * @param[in] point a center coordinate.
578 void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
581 * @brief Get the current scale factor of the page.
582 * @return The current scale factor.
584 float GetScaleFactor() const;
587 * @brief Request to activate/deactivate the accessibility usage set by web app.
588 * @param[in] activated Activate accessibility or not.
590 void ActivateAccessibility(bool activated);
593 * @brief Search and highlights the given string in the document.
594 * @param[in] text The text to find
595 * @param[in] options The options to find
596 * @param[in] maxMatchCount The maximum match count to find
598 * @return true if found & highlighted, false otherwise
600 bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
603 * @brief Add dynamic certificate path.
604 * @param[in] host host that required client authentication
605 * @param[in] certPath the file path stored certificate
607 void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
610 * @brief Get snapshot of the specified viewArea of page.
612 * @param[in] viewArea The rectangle of screen shot
613 * @param[in] scaleFactor The scale factor
615 * @return image view of screen shot
617 Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor);
620 * @brief Request to get snapshot of the specified viewArea of page asynchronously.
622 * @param[in] viewArea The rectangle of screen shot
623 * @param[in] scaleFactor The scale factor
624 * @param[in] callback The callback for screen shot
626 * @return true if requested successfully, false otherwise
628 bool GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
631 * @brief Asynchronous request to check if there is a video playing in the given view.
633 * @param[in] callback The callback called after checking if video is playing or not
635 * @return true if requested successfully, false otherwise
637 bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
640 * @brief Set callback which will be called upon geolocation permission request.
642 * @param[in] callback The callback for requesting geolocation permission
644 void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
647 * @brief Set or unset TTS focus of the webview.
648 * @param[in] focused True if it is gained, false lost.
649 * @note It only works when the webview does not have keyinput focus. If it has keyinput focus, the TTS focus is set automatically.
651 void SetTtsFocus(bool focused);
654 * @brief Callback to be called when page loading is started.
656 * @param[in] callback
658 void RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
661 * @brief Callback to be called when page loading is in progress.
663 * @param[in] callback
665 void RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
668 * @brief Callback to be called when page loading is finished.
670 * @param[in] callback
672 void RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
675 * @brief Callback to be called when an error occurs in page loading.
677 * @param[in] callback
679 void RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback);
682 * @brief Callback to be called when scroll edge is reached.
684 * @param[in] callback
686 void RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback);
689 * @brief Callback to be called when url is changed.
691 * @param[in] callback
693 void RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback);
696 * @brief Callback to be called when form repost decision is requested.
698 * @param[in] callback
700 void RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback);
703 * @brief Callback to be called when frame is rendered.
705 * @param[in] callback
707 void RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback);
710 * @brief Callback to be called when console message will be logged.
712 * @param[in] callback
714 void RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback);
717 * @brief Callback to be called when response policy would be decided.
719 * @param[in] callback
721 void RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback);
724 * @brief Callback to be called when navigation policy would be decided.
726 * @param[in] callback
728 void RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback);
731 * @brief Callback to be called when new window policy would be decided.
733 * @param[in] callback
735 void RegisterNewWindowPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNewWindowPolicyDecidedCallback callback);
738 * @brief Callback to be called when a new window would be created.
740 * @param[in] callback
742 void RegisterNewWindowCreatedCallback(Dali::WebEnginePlugin::WebEngineNewWindowCreatedCallback callback);
745 * @brief Callback to be called when certificate need be confirmed.
747 * @param[in] callback
749 void RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
752 * @brief Callback to be called when ssl certificate is changed.
754 * @param[in] callback
756 void RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
759 * @brief Callback to be called when http authentication need be confirmed.
761 * @param[in] callback
763 void RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback);
766 * @brief Callback to be called when context menu would be shown.
768 * @param[in] callback
770 void RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback);
773 * @brief Callback to be called when context menu would be hidden.
775 * @param[in] callback
777 void RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback);
780 * @brief Callback to be called when fullscreen would be entered.
782 * @param[in] callback
784 void RegisterFullscreenEnteredCallback(Dali::WebEnginePlugin::WebEngineFullscreenEnteredCallback callback);
787 * @brief Callback to be called when fullscreen would be exited.
789 * @param[in] callback
791 void RegisterFullscreenExitedCallback(Dali::WebEnginePlugin::WebEngineFullscreenExitedCallback callback);
794 * @brief Callback to be called when text would be found.
796 * @param[in] callback
798 void RegisterTextFoundCallback(Dali::WebEnginePlugin::WebEngineTextFoundCallback callback);
801 * @brief Get a plain text of current web page asynchronously.
803 * @param[in] callback The callback function called asynchronously.
805 void GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback);
807 public: // Not intended for application developers
810 * @brief Create a handle using the Toolkit::Internal implementation.
812 * @param[in] implementation The WebView implementation
814 DALI_INTERNAL WebView(Internal::WebView& implementation);
817 * @brief Allow the creation of this WebView from an Internal::CustomActor pointer.
819 * @param[in] internal A pointer to the internal CustomActor
821 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
829 } // namespace Toolkit
833 #endif // DALI_TOOLKIT_WEB_VIEW_H