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;
39 namespace Internal DALI_INTERNAL
42 } // namespace DALI_INTERNAL
45 * @addtogroup dali_toolkit_controls_web_view
50 * @brief WebView is a control for displaying web content.
52 * This enables embedding web pages in the application.
54 * For working WebView, a web engine plugin for a platform should be provided.
57 class DALI_TOOLKIT_API WebView : public Control
61 * @brief Enumeration for the start and end property ranges for this control.
65 PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
66 PROPERTY_END_INDEX = PROPERTY_START_INDEX + 1000,
70 * @brief Enumeration for the instance of properties belonging to the WebView class.
77 * @brief The url to load.
78 * @details name "url", type Property::STRING.
80 URL = PROPERTY_START_INDEX,
83 * @brief The user agent string.
84 * @details name "userAgent", type Property::STRING.
89 * @brief The current position of scroll.
90 * @details name "scrollPosition", type Property::VECTOR2.
95 * @brief The current size of scroll.
96 * @details name "scrollSize", type Property::VECTOR2.
97 * @note The value is read-only.
102 * @brief The current size of content.
103 * @details name "contentSize", type Property::VECTOR2.
104 * @note The value is read-only.
109 * @brief The title of web page.
110 * @details name "title", type Property::STRING.
111 * @note The value is read-only.
116 * @brief Whether video hole is enabled or not.
117 * @details name "videoHoleEnabled", type Property::BOOLEAN.
118 * @note False by default.
123 * @brief Whether mouse event is enabled.
124 * @details name "mouseEventsEnabled", type Property::BOOLEAN.
125 * @note Default is true.
127 MOUSE_EVENTS_ENABLED,
130 * @brief Whether key event is enabled.
131 * @details name "keyEventsEnabled", type Property::BOOLEAN.
132 * @note Default is true.
137 * @brief The background color of web page.
138 * @details name "documentBackgroundColor", type Property::VECTOR4.
140 DOCUMENT_BACKGROUND_COLOR,
143 * @brief Whether tiles can be cleared or not when hidden.
144 * @details name "tilesClearedWhenHidden", type BOOLEAN.
146 TILES_CLEARED_WHEN_HIDDEN,
149 * @brief The multiplier of cover area of tile when rendering web page.
150 * @details name "tileCoverAreaMultiplier", type FLOAT.
152 TILE_COVER_AREA_MULTIPLIER,
155 * @brief Whether cursor is enabled or not by client.
156 * @details name "cursorEnabledByClient", type BOOLEAN.
158 CURSOR_ENABLED_BY_CLIENT,
161 * @brief The selected text of web page.
162 * @details name "selectedText", type Property::STRING.
163 * @note The value is read-only.
168 * @brief Zoom factor of web page.
169 * @details name "pageZoomFactor", type Property::FLOAT.
174 * @brief Zoom factor of text.
175 * @details name "textZoomFactor", type Property::FLOAT.
180 * @brief progress percentage of loading a web page.
181 * @details name "loadProgressPercentage", type Property::FLOAT.
182 * @note The value is read-only.
184 LOAD_PROGRESS_PERCENTAGE,
189 * @brief WebView callback related with screen-shot captured.
191 using WebViewScreenshotCapturedCallback = std::function<void(Dali::Toolkit::ImageView)>;
195 * @brief Create an initialized WebView.
196 * @return A handle to a newly allocated Dali WebView
198 * @note WebView will not display anything
200 static WebView New();
203 * @brief Create an initialized WebView.
205 * @param [in] locale The locale of Web
206 * @param [in] timezoneId The timezoneId of Web
208 static WebView New(const std::string& locale, const std::string& timezoneId);
211 * @brief Create an initialized WebView.
213 * @param [in] argc The count of arguments of Applications
214 * @param [in] argv The string array of arguments of Applications
216 static WebView New(uint32_t argc, char** argv);
219 * @brief Create an uninitialized WebView.
226 * This is non-virtual since derived Handel types must not contain data or virtual methods.
231 * @brief Copy constructor.
233 * @param[in] WebView WebView to copy. The copied WebView will point at the same implementation
235 WebView(const WebView& WebView);
238 * @brief Assignment operator.
240 * @param[in] WebView The WebView to assign from
241 * @return The updated WebView
243 WebView& operator=(const WebView& WebView);
246 * @brief Downcast a handle to WebView handle.
248 * If handle points to a WebView, the downcast produces valid handle.
249 * If not, the returned handle is left uninitialized.
251 * @param[in] handle Handle to an object
252 * @return Handle to a WebView or an uninitialized handle
254 static WebView DownCast(BaseHandle handle);
257 * @brief Get WebSettings of WebEngine.
259 Dali::Toolkit::WebSettings* GetSettings() const;
262 * @brief Get WebContext of WebEngine.
264 Dali::Toolkit::WebContext* GetContext() const;
267 * @brief Get CookieManager of WebEngine.
269 Dali::Toolkit::WebCookieManager* GetCookieManager() const;
272 * @brief Get WebBackForwardList of WebEngine.
274 Dali::Toolkit::WebBackForwardList* GetBackForwardList() const;
277 * @brief Get favicon of web page.
279 * @return Handle to a favicon
281 Dali::Toolkit::ImageView GetFavicon() const;
284 * @brief Load a web page based on a given URL.
286 * @param [in] url The URL of the resource to load
288 void LoadUrl(const std::string& url);
291 * @brief Load a given string as web contents.
293 * @param [in] htmlString The string to use as the contents of the web page
295 void LoadHtmlString(const std::string& htmlString);
298 * @brief Load the specified html string as the content of the view overriding current history entry
300 * @param[in] html HTML data to load
301 * @param[in] basicUri Base URL used for relative paths to external objects
302 * @param[in] unreachableUrl URL that could not be reached
304 * @return true if successfully loaded, false otherwise
306 bool LoadHtmlStringOverrideCurrentEntry(const std::string& html, const std::string& basicUri, const std::string& unreachableUrl);
309 * @brief Request loading the given contents by MIME type into the view object
311 * @param[in] contents The content to load
312 * @param[in] contentSize The size of contents (in bytes)
313 * @param[in] mimeType The type of contents, if 0 is given "text/html" is assumed
314 * @param[in] encoding The encoding for contents, if 0 is given "UTF-8" is assumed
315 * @param[in] baseUri The base URI to use for relative resources
317 * @return true if successfully request, false otherwise
319 bool LoadContents(const std::string& contents, uint32_t contentSize, const std::string& mimeType, const std::string& encoding, const std::string& baseUri);
322 * @brief Reload the Web.
327 * @brief Reload the current page's document without cache
329 bool ReloadWithoutCache();
332 * @brief Stop loading web contents on the current page.
337 * @brief Suspend the operation associated with the view.
342 * @brief Resume the operation associated with the view object after calling Suspend().
347 * @brief To suspend all url loading
349 void SuspendNetworkLoading();
352 * @brief To resume new url network loading
354 void ResumeNetworkLoading();
357 * @brief Add custom header
359 * @param[in] name custom header name to add the custom header
360 * @param[in] value custom header value to add the custom header
362 * @return true if succeeded, false otherwise
364 bool AddCustomHeader(const std::string& name, const std::string& value);
367 * @brief Remove custom header
369 * @param[in] name custom header name to remove the custom header
371 * @return true if succeeded, false otherwise
373 bool RemoveCustomHeader(const std::string& name);
376 * @brief Start the inspector server
378 * @param[in] port port number
380 * @return the port number
382 uint32_t StartInspectorServer(uint32_t port);
385 * @brief Stop the inspector server
387 * @return true if succeeded, false otherwise
389 bool StopInspectorServer();
392 * @brief Scroll web page of view by deltaX and deltaY.
393 * @param[in] deltaX The delta x of scroll
394 * @param[in] deltaY The delta y of scroll
396 void ScrollBy(int32_t deltaX, int32_t deltaY);
399 * @brief Scroll edge of view by deltaX and deltaY.
401 * @param[in] deltaX horizontal offset to scroll
402 * @param[in] deltaY vertical offset to scroll
404 * @return true if succeeded, false otherwise
406 bool ScrollEdgeBy(int32_t deltaX, int32_t deltaY);
409 * @brief Return whether forward is possible.
411 * @return True if forward is possible, false otherwise
416 * @brief Go forward in the navigation history.
421 * @brief Return whether backward is possible.
423 * @return True if backward is possible, false otherwise
428 * @brief Go back in the navigation history.
433 * @brief Evaluate JavaScript code represented as a string.
435 * @param[in] script The JavaScript code
436 * @param[in] resultHandler The callback to be called by the JavaScript runtime. This carries evaluation result
438 void EvaluateJavaScript(const std::string& script, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback resultHandler);
441 * @brief Evaluate JavaScript code represented as a string.
443 * @param[in] script The JavaScript code
445 void EvaluateJavaScript(const std::string& script);
448 * @brief Inject a JavaScript object with a message handler into the WebView.
450 * @note The injected object will appear in the JavaScript context to be loaded next.
456 * webview.AddJavaScriptMessageHandler( "myObject", []( const std::string& message ) {
457 * printf( "Received a message from JS: %s", message.c_str() );
460 * // Start WebView by loading URL
461 * webview.LoadUrl( url );
465 * myObject.postMessage( "Hello World!" ); // "Received a message from JS: Hello World!"
468 * @param[in] exposedObjectName The name of exposed object
469 * @param[in] handler The callback function
471 void AddJavaScriptMessageHandler(const std::string& exposedObjectName, Dali::WebEnginePlugin::JavaScriptMessageHandlerCallback handler);
474 * @brief Register alert callback for javascript.
476 * @param[in] callback The callback function to be called by the JavaScript runtime.
478 void RegisterJavaScriptAlertCallback(Dali::WebEnginePlugin::JavaScriptAlertCallback callback);
481 * @brief Reply for JavaScript alert.
483 void JavaScriptAlertReply();
486 * @brief Register confirm callback for javascript.
488 * @param[in] callback The callback function to be called by the JavaScript runtime.
490 void RegisterJavaScriptConfirmCallback(Dali::WebEnginePlugin::JavaScriptConfirmCallback callback);
493 * @brief Reply for JavaScript confirm.
494 * @param[in] confirmed True if confirmed, false otherwise
496 void JavaScriptConfirmReply(bool confirmed);
499 * @brief Register prompt callback for javascript.
501 * @param[in] callback The callback function to be called by the JavaScript runtime.
503 void RegisterJavaScriptPromptCallback(Dali::WebEnginePlugin::JavaScriptPromptCallback callback);
506 * @brief Reply for JavaScript prompt.
507 * @param[in] result The result from input-field of prompt popup.
509 void JavaScriptPromptReply(const std::string& result);
512 * @brief Create a new hit test.
514 * @param[in] x the horizontal position to query
515 * @param[in] y the vertical position to query
516 * @param[in] mode the mode of hit test
518 * @return a new hit test object.
520 std::unique_ptr<Dali::WebEngineHitTest> CreateHitTest(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode);
523 * @brief Create a hit test asynchronously.
525 * @param[in] x the horizontal position to query
526 * @param[in] y the vertical position to query
527 * @param[in] mode the mode of hit test
528 * @param[in] callback The callback function
530 * @return true if succeeded, false otherwise.
532 bool CreateHitTestAsynchronously(int32_t x, int32_t y, Dali::WebEngineHitTest::HitTestMode mode, Dali::WebEnginePlugin::WebEngineHitTestCreatedCallback callback);
535 * @brief Clear the history of Web.
540 * @brief Clear all tiles resources of Web.
542 void ClearAllTilesResources();
545 * @brief Scale the current page, centered at the given point.
546 * @param[in] scaleFactor a new factor to be scaled.
547 * @param[in] point a center coordinate.
549 void SetScaleFactor(float scaleFactor, Dali::Vector2 point);
552 * @brief Get the current scale factor of the page.
553 * @return The current scale factor.
555 float GetScaleFactor() const;
558 * @brief Request to activate/deactivate the accessibility usage set by web app.
559 * @param[in] activated Activate accessibility or not.
561 void ActivateAccessibility(bool activated);
564 * @brief Search and highlights the given string in the document.
565 * @param[in] text The text to find
566 * @param[in] options The options to find
567 * @param[in] maxMatchCount The maximum match count to find
569 * @return true if found & highlighted, false otherwise
571 bool HighlightText(const std::string& text, Dali::WebEnginePlugin::FindOption options, uint32_t maxMatchCount);
574 * @brief Add dynamic certificate path.
575 * @param[in] host host that required client authentication
576 * @param[in] certPath the file path stored certificate
578 void AddDynamicCertificatePath(const std::string& host, const std::string& certPath);
581 * @brief Get snapshot of the specified viewArea of page.
583 * @param[in] viewArea The rectangle of screen shot
584 * @param[in] scaleFactor The scale factor
586 * @return image view of screen shot
588 Dali::Toolkit::ImageView GetScreenshot(Dali::Rect<int32_t> viewArea, float scaleFactor);
591 * @brief Request to get snapshot of the specified viewArea of page asynchronously.
593 * @param[in] viewArea The rectangle of screen shot
594 * @param[in] scaleFactor The scale factor
595 * @param[in] callback The callback for screen shot
597 * @return true if requested successfully, false otherwise
599 bool GetScreenshotAsynchronously(Dali::Rect<int32_t> viewArea, float scaleFactor, WebViewScreenshotCapturedCallback callback);
602 * @brief Asynchronous request to check if there is a video playing in the given view.
604 * @param[in] callback The callback called after checking if video is playing or not
606 * @return true if requested successfully, false otherwise
608 bool CheckVideoPlayingAsynchronously(Dali::WebEnginePlugin::VideoPlayingCallback callback);
611 * @brief Set callback which will be called upon geolocation permission request.
613 * @param[in] callback The callback for requesting geolocation permission
615 void RegisterGeolocationPermissionCallback(Dali::WebEnginePlugin::GeolocationPermissionCallback callback);
618 * @brief Set or unset TTS focus of the webview.
619 * @param[in] focused True if it is gained, false lost.
620 * @note It only works when the webview does not have keyinput focus. If it has keyinput focus, the TTS focus is set automatically.
622 void SetTtsFocus(bool focused);
625 * @brief Callback to be called when page loading is started.
627 * @param[in] callback
629 void RegisterPageLoadStartedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
632 * @brief Callback to be called when page loading is in progress.
634 * @param[in] callback
636 void RegisterPageLoadInProgressCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
639 * @brief Callback to be called when page loading is finished.
641 * @param[in] callback
643 void RegisterPageLoadFinishedCallback(Dali::WebEnginePlugin::WebEnginePageLoadCallback callback);
646 * @brief Callback to be called when an error occurs in page loading.
648 * @param[in] callback
650 void RegisterPageLoadErrorCallback(Dali::WebEnginePlugin::WebEnginePageLoadErrorCallback callback);
653 * @brief Callback to be called when scroll edge is reached.
655 * @param[in] callback
657 void RegisterScrollEdgeReachedCallback(Dali::WebEnginePlugin::WebEngineScrollEdgeReachedCallback callback);
660 * @brief Callback to be called when url is changed.
662 * @param[in] callback
664 void RegisterUrlChangedCallback(Dali::WebEnginePlugin::WebEngineUrlChangedCallback callback);
667 * @brief Callback to be called when form repost decision is requested.
669 * @param[in] callback
671 void RegisterFormRepostDecidedCallback(Dali::WebEnginePlugin::WebEngineFormRepostDecidedCallback callback);
674 * @brief Callback to be called when frame is rendered.
676 * @param[in] callback
678 void RegisterFrameRenderedCallback(Dali::WebEnginePlugin::WebEngineFrameRenderedCallback callback);
681 * @brief Callback to be called when console message will be logged.
683 * @param[in] callback
685 void RegisterConsoleMessageReceivedCallback(Dali::WebEnginePlugin::WebEngineConsoleMessageReceivedCallback callback);
688 * @brief Callback to be called when response policy would be decided.
690 * @param[in] callback
692 void RegisterResponsePolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineResponsePolicyDecidedCallback callback);
695 * @brief Callback to be called when navigation policy would be decided.
697 * @param[in] callback
699 void RegisterNavigationPolicyDecidedCallback(Dali::WebEnginePlugin::WebEngineNavigationPolicyDecidedCallback callback);
702 * @brief Callback to be called when certificate need be confirmed.
704 * @param[in] callback
706 void RegisterCertificateConfirmedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
709 * @brief Callback to be called when ssl certificate is changed.
711 * @param[in] callback
713 void RegisterSslCertificateChangedCallback(Dali::WebEnginePlugin::WebEngineCertificateCallback callback);
716 * @brief Callback to be called when http authentication need be confirmed.
718 * @param[in] callback
720 void RegisterHttpAuthHandlerCallback(Dali::WebEnginePlugin::WebEngineHttpAuthHandlerCallback callback);
723 * @brief Callback to be called when context menu would be shown.
725 * @param[in] callback
727 void RegisterContextMenuShownCallback(Dali::WebEnginePlugin::WebEngineContextMenuShownCallback callback);
730 * @brief Callback to be called when context menu would be hidden.
732 * @param[in] callback
734 void RegisterContextMenuHiddenCallback(Dali::WebEnginePlugin::WebEngineContextMenuHiddenCallback callback);
737 * @brief Get a plain text of current web page asynchronously.
739 * @param[in] callback The callback function called asynchronously.
741 void GetPlainTextAsynchronously(Dali::WebEnginePlugin::PlainTextReceivedCallback callback);
743 public: // Not intended for application developers
746 * @brief Create a handle using the Toolkit::Internal implementation.
748 * @param[in] implementation The WebView implementation
750 DALI_INTERNAL WebView(Internal::WebView& implementation);
753 * @brief Allow the creation of this WebView from an Internal::CustomActor pointer.
755 * @param[in] internal A pointer to the internal CustomActor
757 explicit DALI_INTERNAL WebView(Dali::Internal::CustomActor* internal);
765 } // namespace Toolkit
769 #endif // DALI_TOOLKIT_WEB_VIEW_H