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.
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)>;
195 * @brief WebView signal type related with page loading.
197 using WebViewPageLoadSignalType = Signal<void(WebView, const std::string&)>;
200 * @brief WebView signal type related with page loading error.
202 using WebViewPageLoadErrorSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineLoadError>)>;
205 * @brief WebView signal type related with scroll edge reached.
207 using WebViewScrollEdgeReachedSignalType = Signal<void(WebView, Dali::WebEnginePlugin::ScrollEdge)>;
210 * @brief WebView signal type related with url changed.
212 using WebViewUrlChangedSignalType = Signal<void(WebView, const std::string&)>;
215 * @brief WebView signal type related with form repost decision.
217 using WebViewFormRepostDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::Toolkit::WebFormRepostDecision>)>;
220 * @brief WebView signal type related with frame rendered.
222 using WebViewFrameRenderedSignalType = Signal<void(WebView)>;
225 * @brief WebView signal type related with http request interceptor.
227 using WebViewRequestInterceptorSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineRequestInterceptor>)>;
230 * @brief WebView signal type related with console message.
232 using WebViewConsoleMessageSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineConsoleMessage>)>;
235 * @brief WebView signal type related with policy decision.
237 using WebViewPolicyDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEnginePolicyDecision>)>;
240 * @brief WebView signal type related with certificate changed.
242 using WebViewCertificateSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineCertificate>)>;
245 * @brief WebView signal type related with http authentication.
247 using WebViewHttpAuthHandlerSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineHttpAuthHandler>)>;
251 * @brief Creates an initialized WebView.
252 * @return A handle to a newly allocated Dali WebView
254 * @note WebView will not display anything
256 static WebView New();
259 * @brief Creates an initialized WebView.
261 * @param [in] locale The locale of Web
262 * @param [in] timezoneId The timezoneId of Web
264 static WebView New(const std::string& locale, const std::string& timezoneId);
267 * @brief Creates an initialized WebView.
269 * @param [in] argc The count of arguments of Applications
270 * @param [in] argv The string array of arguments of Applications
272 static WebView New(int argc, char** argv);
275 * @brief Creates an uninitialized WebView.
282 * This is non-virtual since derived Handel types must not contain data or virtual methods.
287 * @brief Copy constructor.
289 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
291 WebView(const WebView& WebView);
294 * @brief Assignment operator.
296 * @param[in] WebView The WebView to assign from
297 * @return The updated WebView
299 WebView& operator=(const WebView& WebView);
302 * @brief Downcasts a handle to WebView handle.
304 * If handle points to a WebView, the downcast produces valid handle.
305 * If not, the returned handle is left uninitialized.
307 * @param[in] handle Handle to an object
308 * @return Handle to a WebView or an uninitialized handle
310 static WebView DownCast(BaseHandle handle);
313 * @brief Get WebSettings of WebEngine.
315 Dali::Toolkit::WebSettings* GetSettings() const;
318 * @brief Get WebContext of WebEngine.
320 Dali::Toolkit::WebContext* GetContext() const;
323 * @brief Get CookieManager of WebEngine.
325 Dali::Toolkit::WebCookieManager* GetCookieManager() const;
328 * @brief Get WebBackForwardList of WebEngine.
330 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
333 * @brief Get Favicon of web page.
335 * @return Handle to a fav icon
337 Dali::Toolkit::ImageView& GetFavicon();
340 * @brief Loads a web page based on a given URL.
342 * @param [in] url The URL of the resource to load
344 void LoadUrl(const std::string& url);
347 * @brief Loads a given string as web contents.
349 * @param [in] htmlString The string to use as the contents of the web page
351 void LoadHtmlString(const std::string& htmlString);
354 * @brief Load the specified html string as the content of the view overriding current history entry
356 * @param[in] html HTML data to load
357 * @param[in] basicUri Base URL used for relative paths to external objects
358 * @param[in] unreachableUrl URL that could not be reached
360 * @return true if successfully loaded, false otherwise
362 bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
365 * @brief Requests loading the given contents by MIME type into the view object
367 * @param[in] contents The content to load
368 * @param[in] contentSize The size of contents (in bytes)
369 * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
370 * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
371 * @param[in] baseUri The base URI to use for relative resources
373 * @return true if successfully request, false otherwise
375 bool LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
378 * @brief Reloads the Web.
383 * @brief Reloads the current page's document without cache
385 bool ReloadWithoutCache();
388 * @brief Stops loading web contents on the current page.
393 * @brief Suspends the operation associated with the view.
398 * @brief Resumes the operation associated with the view object after calling Suspend().
403 * @brief To suspend all url loading
405 void SuspendNetworkLoading();
408 * @brief To resume new url network loading
410 void ResumeNetworkLoading();
413 * @brief Add custom header
415 * @param[in] name custom header name to add the custom header
416 * @param[in] value custom header value to add the custom header
418 * @return true if succeeded, false otherwise
420 bool AddCustomHeader(const std::string& name, const std::string& value);
423 * @brief Remove custom header
425 * @param[in] name custom header name to remove the custom header
427 * @return true if succeeded, false otherwise
429 bool RemoveCustomHeader(const std::string& name);
432 * @brief Start the inspector server
434 * @param[in] port port number
436 * @return the port number
438 uint32_t StartInspectorServer(uint32_t port);
441 * @brief Stop the inspector server
443 * @return true if succeeded, false otherwise
445 bool StopInspectorServer();
448 * @brief Scrolls web page of view by deltaX and deltaY.
449 * @param[in] deltaX The delta x of scroll
450 * @param[in] deltaY The delta y of scroll
452 void ScrollBy(int deltaX, int deltaY);
455 * @brief Scrolls edge of view by deltaX and deltaY.
457 * @param[in] deltaX horizontal offset to scroll
458 * @param[in] deltaY vertical offset to scroll
460 * @return true if succeeded, false otherwise
462 bool ScrollEdgeBy(int deltaX, int deltaY);
465 * @brief Returns whether forward is possible.
467 * @return True if forward is possible, false otherwise
472 * @brief Goes forward in the navigation history.
477 * @brief Returns whether backward is possible.
479 * @return True if backward is possible, false otherwise
484 * @brief Goes back in the navigation history.
489 * @brief Evaluates JavaScript code represented as a string.
491 * @param[in] script The JavaScript code
492 * @param[in] resultHandler The callback function to be called by the JavaScript runtime. This carries evaluation result
494 void EvaluateJavaScript(const std::string& script, std::function<void(const std::string&)> resultHandler);
497 * @brief Evaluates JavaScript code represented as a string.
499 * @param[in] script The JavaScript code
501 void EvaluateJavaScript(const std::string& script);
504 * @brief Inject a JavaScript object with a message handler into the WebView.
506 * @note The injected object will appear in the JavaScript context to be loaded next.
512 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
513 * printf( "Received a message from JS: %s", message.c_str() );
516 * // Start WebView by loading URL
517 * webview.LoadUrl( url );
521 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
524 * @param[in] exposedObjectName The name of exposed object
525 * @param[in] handler The callback function
527 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, std::function<void(const std::string&)> handler);
530 * @brief Register alert callback for javascript.
532 * @param[in] callback The callback function to be called by the JavaScript runtime.
534 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
537 * @brief Reply for JavaScript alert.
539 void JavaScriptAlertReply();
542 * @brief Register confirm callback for javascript.
544 * @param[in] callback The callback function to be called by the JavaScript runtime.
546 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
549 * @brief Reply for JavaScript confirm.
550 * @param[in] confirmed True if confirmed, false otherwise
552 void JavaScriptConfirmReply(bool confirmed);
555 * @brief Register prompt callback for javascript.
557 * @param[in] callback The callback function to be called by the JavaScript runtime.
559 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
562 * @brief Reply for JavaScript prompt.
563 * @param[in] result The result from input-field of prompt popup.
565 void JavaScriptPromptReply(const std::string& result);
568 * @brief Clears the history of Web.
573 * @brief Clears all tiles resources of Web.
575 void ClearAllTilesResources();
578 * @brief Scales the current page, centered at the given point.
579 * @param[in] scaleFactor a new factor to be scaled.
580 * @param[in] point a center coordinate.
582 void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
585 * @brief Gets the current scale factor of the page.
586 * @return The current scale factor.
588 float GetScaleFactor() const;
591 * @brief Request to activate/deactivate the accessibility usage set by web app.
592 * @param[in] activated Activate accessibility or not.
594 void ActivateAccessibility(bool activated);
597 * @brief Searches and highlights the given string in the document.
598 * @param[in] text The text to find
599 * @param[in] options The options to find
600 * @param[in] maxMatchCount The maximum match count to find
602 * @return true if found & highlighted, false otherwise
604 bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
607 * @brief Add dynamic certificate path.
608 * @param[in] host host that required client authentication
609 * @param[in] certPath the file path stored certificate
611 void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
614 * @brief Get snapshot of the specified viewArea of page.
616 * @param[in] viewArea The rectangle of screen shot
617 * @param[in] scaleFactor The scale factor
619 * @return image view of screen shot
621 Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int> viewArea, float scaleFactor);
624 * @brief Request to get snapshot of the specified viewArea of page asynchronously.
626 * @param[in] viewArea The rectangle of screen shot
627 * @param[in] scaleFactor The scale factor
628 * @param[in] callback The callback for screen shot
630 * @return true if requested successfully, false otherwise
632 bool GetScreenshotAsynchronously(Dali::Rect<int> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
635 * @brief Asynchronous request to check if there is a video playing in the given view.
637 * @param[in] callback The callback called after checking if video is playing or not
639 * @return true if requested successfully, false otherwise
641 bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
644 * @brief Sets callback which will be called upon geolocation permission request.
646 * @param[in] callback The callback for requesting geolocation permission
648 void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
651 * @brief Connects to this signal to be notified when page loading is started.
653 * @return A signal object to connect with
655 WebViewPageLoadSignalType& PageLoadStartedSignal();
658 * @brief Connects to this signal to be notified when page loading is in progress.
660 * @return A signal object to connect with
662 WebViewPageLoadSignalType& PageLoadInProgressSignal();
665 * @brief Connects to this signal to be notified when page loading is finished.
667 * @return A signal object to connect with
669 WebViewPageLoadSignalType& PageLoadFinishedSignal();
672 * @brief Connects to this signal to be notified when an error occurs in page loading.
674 * @return A signal object to connect with
676 WebViewPageLoadErrorSignalType& PageLoadErrorSignal();
679 * @brief Connects to this signal to be notified when scroll edge is reached.
681 * @return A signal object to connect with
683 WebViewScrollEdgeReachedSignalType& ScrollEdgeReachedSignal();
686 * @brief Connects to this signal to be notified when url is changed.
688 * @return A signal object to connect with
690 WebViewUrlChangedSignalType& UrlChangedSignal();
693 * @brief Connects to this signal to be notified when form repost decision is requested.
695 * @return A signal object to connect with.
697 WebViewFormRepostDecisionSignalType& FormRepostDecisionSignal();
700 * @brief Connects to this signal to be notified when frame is rendered.
702 * @return A signal object to connect with.
704 WebViewFrameRenderedSignalType& FrameRenderedSignal();
707 * @brief Connects to this signal to be notified when http request need be intercepted.
709 * @return A signal object to connect with.
711 WebViewRequestInterceptorSignalType& RequestInterceptorSignal();
714 * @brief Connects to this signal to be notified when console message will be logged.
716 * @return A signal object to connect with.
718 WebViewConsoleMessageSignalType& ConsoleMessageSignal();
721 * @brief Connects to this signal to be notified when new policy would be decided.
723 * @return A signal object to connect with.
725 WebViewPolicyDecisionSignalType& PolicyDecisionSignal();
728 * @brief Connects to this signal to be notified when certificate need be confirmed.
730 * @return A signal object to connect with.
732 WebViewCertificateSignalType& CertificateConfirmSignal();
735 * @brief Connects to this signal to be notified when ssl certificate is changed.
737 * @return A signal object to connect with.
739 WebViewCertificateSignalType& SslCertificateChangedSignal();
742 * @brief Connects to this signal to be notified when http authentication need be confirmed.
744 * @return A signal object to connect with.
746 WebViewHttpAuthHandlerSignalType& HttpAuthHandlerSignal();
748 public: // Not intended for application developers
751 * @brief Creates a handle using the Toolkit::Internal implementation.
753 * @param[in] implementation The WebView implementation
755 DALI_INTERNAL WebView(Internal::WebView& implementation);
758 * @brief Allows the creation of this WebView from an Internal::CustomActor pointer.
760 * @param[in] internal A pointer to the internal CustomActor
762 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
770 } // namespace Toolkit
774 #endif // DALI_TOOLKIT_WEB_VIEW_H