From 03d087167ceac9acf09a9cb7eb86bc06c0a5d4e7 Mon Sep 17 00:00:00 2001 From: "huayong.xu" Date: Mon, 22 Feb 2021 19:55:47 +0800 Subject: [PATCH] Add APIs for console message & loading error in web view. This patch is to add some APIs into web view for console message and loading error. Change-Id: I122871ca03ca164c8778f0d4af07100dd945c284 --- .../dali-toolkit-test-utils/toolkit-web-engine.cpp | 92 ++++++++++++++++++++-- .../src/dali-toolkit/utc-Dali-WebView.cpp | 71 ++++++++++++++++- .../devel-api/controls/web-view/web-view.cpp | 5 ++ .../devel-api/controls/web-view/web-view.h | 36 +++------ .../internal/controls/web-view/web-view-impl.cpp | 27 ++++++- .../internal/controls/web-view/web-view-impl.h | 48 ++++++----- 6 files changed, 227 insertions(+), 52 deletions(-) diff --git a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp index db94ec0..f400f75 100755 --- a/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp +++ b/automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp @@ -20,10 +20,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -265,6 +267,66 @@ public: } }; +class MockWebEngineConsoleMessage : public Dali::WebEngineConsoleMessage +{ +public: + MockWebEngineConsoleMessage() + { + } + + std::string GetSource() const override + { + return "source"; + } + + uint32_t GetLine() const override + { + return 10; + } + + SeverityLevel GetSeverityLevel() const override + { + return SeverityLevel::EMPTY; + } + + std::string GetText() const override + { + return "This is a text."; + } +}; + +class MockWebEngineLoadError : public Dali::WebEngineLoadError +{ +public: + MockWebEngineLoadError(const std::string& url) + : mockUrl(url) + { + } + + std::string GetUrl() const override + { + return mockUrl; + } + + ErrorCode GetCode() const override + { + return ErrorCode::UNKNOWN; + } + + std::string GetDescription() const override + { + return "This is an error."; + } + + ErrorType GetType() const override + { + return ErrorType::NONE; + } + +private: + std::string mockUrl; +}; + class MockWebEngineSettings : public WebEngineSettings { public: @@ -835,6 +897,11 @@ public: return mRequestInterceptorSignal; } + Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType& ConsoleMessageSignal() + { + return mConsoleMessageSignal; + } + std::string mUrl; std::vector mHistory; size_t mCurrentPlusOnePos; @@ -849,6 +916,7 @@ public: Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType mFormRepostDecisionSignal; Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType mFrameRenderedSignal; Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType mRequestInterceptorSignal; + Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType mConsoleMessageSignal; bool mEvaluating; float mPageZoomFactor; @@ -863,13 +931,13 @@ public: WebEngineCookieManager* mockWebEngineCookieManager; WebEngineSettings* mockWebEngineSettings; - std::vector mResultCallbacks; - Dali::WebEnginePlugin::JavaScriptAlertCallback mJavaScriptAlertCallback; - Dali::WebEnginePlugin::JavaScriptConfirmCallback mJavaScriptConfirmCallback; - Dali::WebEnginePlugin::JavaScriptPromptCallback mJavaScriptPromptCallback; - Dali::WebEnginePlugin::ScreenshotCapturedCallback mScreenshotCapturedCallback; - Dali::WebEnginePlugin::VideoPlayingCallback mVideoPlayingCallback; - Dali::WebEnginePlugin::GeolocationPermissionCallback mGeolocationPermissionCallback; + std::vector mResultCallbacks; + Dali::WebEnginePlugin::JavaScriptAlertCallback mJavaScriptAlertCallback; + Dali::WebEnginePlugin::JavaScriptConfirmCallback mJavaScriptConfirmCallback; + Dali::WebEnginePlugin::JavaScriptPromptCallback mJavaScriptPromptCallback; + Dali::WebEnginePlugin::ScreenshotCapturedCallback mScreenshotCapturedCallback; + Dali::WebEnginePlugin::VideoPlayingCallback mVideoPlayingCallback; + Dali::WebEnginePlugin::GeolocationPermissionCallback mGeolocationPermissionCallback; }; @@ -920,6 +988,11 @@ bool OnLoadUrl() gInstance->mFrameRenderedSignal.Emit(); std::shared_ptr interceptor(new MockWebEngineRequestInterceptor()); gInstance->mRequestInterceptorSignal.Emit(std::move(interceptor)); + + std::shared_ptr error(new MockWebEngineLoadError(gInstance->mUrl)); + gInstance->mPageLoadErrorSignal.Emit(std::move(error)); + std::shared_ptr message(new MockWebEngineConsoleMessage()); + gInstance->mConsoleMessageSignal.Emit(std::move(message)); } return false; } @@ -1492,5 +1565,10 @@ Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType& WebEngine::Request return Internal::Adaptor::GetImplementation(*this).RequestInterceptorSignal(); } +Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType& WebEngine::ConsoleMessageSignal() +{ + return Internal::Adaptor::GetImplementation(*this).ConsoleMessageSignal(); +} + } // namespace Dali; diff --git a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp index a97eb79..912bc62 100755 --- a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp @@ -23,6 +23,8 @@ #include #include +#include +#include #include #include #include @@ -50,6 +52,8 @@ const char* const TEST_URL2( "http://www.somewhere.valid2.com" ); static int gPageLoadStartedCallbackCalled = 0; static int gPageLoadInProgressCallbackCalled = 0; static int gPageLoadFinishedCallbackCalled = 0; +static int gPageLoadErrorCallbackCalled = 0; +static std::shared_ptr gPageLoadErrorInstance = nullptr; static int gScrollEdgeReachedCallbackCalled = 0; static int gUrlChangedCallbackCalled = 0; static int gEvaluateJavaScriptCallbackCalled = 0; @@ -67,6 +71,8 @@ static std::shared_ptr gFormRepostDecision static int gFrameRenderedCallbackCalled = 0; static int gRequestInterceptorCallbackCalled = 0; static std::shared_ptr gRequestInterceptorInstance = nullptr; +static int gConsoleMessageCallbackCalled = 0; +static std::shared_ptr gConsoleMessageInstance = nullptr; struct CallbackFunctor { @@ -107,8 +113,10 @@ static void OnUrlChanged( WebView view, const std::string& url ) gUrlChangedCallbackCalled++; } -static void OnPageLoadError( WebView view, const std::string& url, WebView::LoadErrorCode errorCode ) +static void OnPageLoadError(WebView view, std::shared_ptr error) { + gPageLoadErrorCallbackCalled++; + gPageLoadErrorInstance = std::move(error); } static void OnEvaluateJavaScript( const std::string& result ) @@ -185,6 +193,12 @@ static void OnRequestInterceptor(WebView view, std::shared_ptr message) +{ + gConsoleMessageCallbackCalled++; + gConsoleMessageInstance = std::move(message); +} + } // namespace void web_view_startup(void) @@ -261,7 +275,6 @@ int UtcDaliWebViewPageNavigation(void) view.PageLoadStartedSignal().Connect( &OnPageLoadStarted ); view.PageLoadInProgressSignal().Connect( &OnPageLoadInProgress ); view.PageLoadFinishedSignal().Connect( &OnPageLoadFinished ); - view.PageLoadErrorSignal().Connect( &OnPageLoadError ); view.UrlChangedSignal().Connect( &OnUrlChanged ); bool signal1 = false; bool signal2 = false; @@ -323,6 +336,60 @@ int UtcDaliWebViewPageNavigation(void) END_TEST; } +int UtcDaliWebViewPageLoadErrorConsoleMessage(void) +{ + ToolkitTestApplication application; + + WebView view = WebView::New(); + view.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT ); + view.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT ); + view.SetProperty( Actor::Property::POSITION, Vector2( 0, 0 )); + view.SetProperty( Actor::Property::SIZE, Vector2( 800, 600 ) ); + application.GetScene().Add( view ); + application.SendNotification(); + application.Render(); + DALI_TEST_CHECK( view ); + + ConnectionTracker* testTracker = new ConnectionTracker(); + view.PageLoadErrorSignal().Connect( &OnPageLoadError ); + view.ConsoleMessageSignal().Connect( &OnConsoleMessage ); + bool signal1 = false; + bool signal2 = false; + view.ConnectSignal( testTracker, "pageLoadError", CallbackFunctor(&signal1) ); + view.ConnectSignal( testTracker, "consoleMessage", CallbackFunctor(&signal2) ); + DALI_TEST_EQUALS( gPageLoadErrorCallbackCalled, 0, TEST_LOCATION ); + DALI_TEST_EQUALS( gConsoleMessageCallbackCalled, 0, TEST_LOCATION ); + + view.LoadUrl( TEST_URL1 ); + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS( gPageLoadErrorCallbackCalled, 1, TEST_LOCATION ); + DALI_TEST_EQUALS( gConsoleMessageCallbackCalled, 1, TEST_LOCATION ); + DALI_TEST_CHECK( signal1 & signal2); + + // error code. + DALI_TEST_CHECK(gPageLoadErrorInstance); + DALI_TEST_EQUALS(gPageLoadErrorInstance->GetUrl(), TEST_URL1, TEST_LOCATION); + DALI_TEST_EQUALS(gPageLoadErrorInstance->GetCode(), Dali::WebEngineLoadError::ErrorCode::UNKNOWN, TEST_LOCATION); + std::string testErrorDescription("This is an error."); + DALI_TEST_EQUALS(gPageLoadErrorInstance->GetDescription(), testErrorDescription, TEST_LOCATION); + DALI_TEST_EQUALS(gPageLoadErrorInstance->GetType(), Dali::WebEngineLoadError::ErrorType::NONE, TEST_LOCATION); + + // console message. + DALI_TEST_CHECK(gConsoleMessageInstance); + std::string testConsoleSource("source"); + DALI_TEST_EQUALS(gConsoleMessageInstance->GetSource(), testConsoleSource, TEST_LOCATION); + DALI_TEST_EQUALS(gConsoleMessageInstance->GetLine(), 10, TEST_LOCATION); + DALI_TEST_EQUALS(gConsoleMessageInstance->GetSeverityLevel(), Dali::WebEngineConsoleMessage::SeverityLevel::EMPTY, TEST_LOCATION); + std::string testConsoleText("This is a text."); + DALI_TEST_EQUALS(gConsoleMessageInstance->GetText(), testConsoleText, TEST_LOCATION); + + // reset + gPageLoadErrorInstance = nullptr; + gConsoleMessageInstance = nullptr; + + END_TEST; +} + int UtcDaliWebViewTouchAndKeys(void) { ToolkitTestApplication application; diff --git a/dali-toolkit/devel-api/controls/web-view/web-view.cpp b/dali-toolkit/devel-api/controls/web-view/web-view.cpp index 504cabb..886e6d1 100755 --- a/dali-toolkit/devel-api/controls/web-view/web-view.cpp +++ b/dali-toolkit/devel-api/controls/web-view/web-view.cpp @@ -344,6 +344,11 @@ WebView::WebViewRequestInterceptorSignalType& WebView::RequestInterceptorSignal( return Dali::Toolkit::GetImpl(*this).RequestInterceptorSignal(); } +WebView::WebViewConsoleMessageSignalType& WebView::ConsoleMessageSignal() +{ + return Dali::Toolkit::GetImpl(*this).ConsoleMessageSignal(); +} + WebView::WebView(Internal::WebView& implementation) : Control(implementation) { diff --git a/dali-toolkit/devel-api/controls/web-view/web-view.h b/dali-toolkit/devel-api/controls/web-view/web-view.h index a64e1ca..bc48d61 100755 --- a/dali-toolkit/devel-api/controls/web-view/web-view.h +++ b/dali-toolkit/devel-api/controls/web-view/web-view.h @@ -187,28 +187,6 @@ public: }; /** - * @brief Enumeration for indicating error code of page loading. - */ - enum class LoadErrorCode - { - UNKNOWN = 0, ///< Unknown. - CANCELED, ///< User canceled. - CANT_SUPPORT_MIMETYPE, ///< Can't show the page for this MIME type. - FAILED_FILE_IO, ///< File IO error. - CANT_CONNECT, ///< Cannot connect to the network. - CANT_LOOKUP_HOST, ///< Fail to look up host from the DNS. - FAILED_TLS_HANDSHAKE, ///< Fail to SSL/TLS handshake. - INVALID_CERTIFICATE, ///< Received certificate is invalid. - REQUEST_TIMEOUT, ///< Connection timeout. - TOO_MANY_REDIRECTS, ///< Too many redirects. - TOO_MANY_REQUESTS, ///< Too many requests during this load. - BAD_URL, ///< Malformed URL. - UNSUPPORTED_SCHEME, ///< Unsupported scheme. - AUTHENTICATION, ///< User authentication failed on the server. - INTERNAL_SERVER ///< Web server has an internal server error. - }; - - /** * @brief WebView callback related with screen-shot captured. */ using WebViewScreenshotCapturedCallback = std::function; @@ -221,7 +199,7 @@ public: /** * @brief WebView signal type related with page loading error. */ - using WebViewPageLoadErrorSignalType = Signal; + using WebViewPageLoadErrorSignalType = Signal)>; /** * @brief WebView signal type related with scroll edge reached. @@ -248,6 +226,11 @@ public: */ using WebViewRequestInterceptorSignalType = Signal)>; + /** + * @brief WebView signal type related with console message. + */ + using WebViewConsoleMessageSignalType = Signal)>; + public: /** * @brief Creates an initialized WebView. @@ -712,6 +695,13 @@ public: */ WebViewRequestInterceptorSignalType& RequestInterceptorSignal(); + /** + * @brief Connects to this signal to be notified when console message will be logged. + * + * @return A signal object to connect with. + */ + WebViewConsoleMessageSignalType& ConsoleMessageSignal(); + public: // Not intended for application developers /// @cond internal /** diff --git a/dali-toolkit/internal/controls/web-view/web-view-impl.cpp b/dali-toolkit/internal/controls/web-view/web-view-impl.cpp index 74e498b..eb25d3b 100644 --- a/dali-toolkit/internal/controls/web-view/web-view-impl.cpp +++ b/dali-toolkit/internal/controls/web-view/web-view-impl.cpp @@ -20,10 +20,12 @@ // EXTERNAL INCLUDES #include +#include #include #include #include #include +#include #include #include #include @@ -89,6 +91,7 @@ DALI_SIGNAL_REGISTRATION(Toolkit, WebView, "urlChanged", URL_CHANGED_SIG DALI_SIGNAL_REGISTRATION(Toolkit, WebView, "formRepostDecision", FORM_REPOST_DECISION_SIGNAL ) DALI_SIGNAL_REGISTRATION(Toolkit, WebView, "frameRendered", FRAME_RENDERED_SIGNAL ) DALI_SIGNAL_REGISTRATION(Toolkit, WebView, "requestInterceptor", REQUEST_INTERCEPTOR_SIGNAL ) +DALI_SIGNAL_REGISTRATION(Toolkit, WebView, "consoleMessage", CONSOLE_MESSAGE_SIGNAL ) DALI_TYPE_REGISTRATION_END() // clang-format on @@ -219,6 +222,7 @@ void WebView::OnInitialize() mWebEngine.FormRepostDecisionSignal().Connect(this, &WebView::OnFormRepostDecision); mWebEngine.FrameRenderedSignal().Connect(this, &WebView::OnFrameRendered); mWebEngine.RequestInterceptorSignal().Connect(this, &WebView::OnInterceptRequest); + mWebEngine.ConsoleMessageSignal().Connect(this, &WebView::OnConsoleMessage); mWebContext = std::unique_ptr(new WebContext(mWebEngine.GetContext())); mWebCookieManager = std::unique_ptr(new WebCookieManager(mWebEngine.GetCookieManager())); @@ -717,6 +721,11 @@ Dali::Toolkit::WebView::WebViewRequestInterceptorSignalType& WebView::RequestInt return mRequestInterceptorSignal; } +Dali::Toolkit::WebView::WebViewConsoleMessageSignalType& WebView::ConsoleMessageSignal() +{ + return mConsoleMessageSignal; +} + void WebView::OnPageLoadStarted(const std::string& url) { if(!mPageLoadStartedSignal.Empty()) @@ -744,12 +753,12 @@ void WebView::OnPageLoadFinished(const std::string& url) } } -void WebView::OnPageLoadError(const std::string& url, int errorCode) +void WebView::OnPageLoadError(std::shared_ptr error) { if(!mPageLoadErrorSignal.Empty()) { Dali::Toolkit::WebView handle(GetOwner()); - mPageLoadErrorSignal.Emit(handle, url, static_cast(errorCode)); + mPageLoadErrorSignal.Emit(handle, std::move(error)); } } @@ -816,6 +825,15 @@ void WebView::OnInterceptRequest(std::shared_ptr message) +{ + if(!mConsoleMessageSignal.Empty()) + { + Dali::Toolkit::WebView handle(GetOwner()); + mConsoleMessageSignal.Emit(handle, std::move(message)); + } +} + bool WebView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor) { Dali::BaseHandle handle(object); @@ -868,6 +886,11 @@ bool WebView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tr webView.RequestInterceptorSignal().Connect(tracker, functor); connected = true; } + else if(0 == strcmp(signalName.c_str(), CONSOLE_MESSAGE_SIGNAL)) + { + webView.ConsoleMessageSignal().Connect(tracker, functor); + connected = true; + } return connected; } diff --git a/dali-toolkit/internal/controls/web-view/web-view-impl.h b/dali-toolkit/internal/controls/web-view/web-view-impl.h index 2630e47..cf5319f 100755 --- a/dali-toolkit/internal/controls/web-view/web-view-impl.h +++ b/dali-toolkit/internal/controls/web-view/web-view-impl.h @@ -344,6 +344,11 @@ public: */ Dali::Toolkit::WebView::WebViewRequestInterceptorSignalType& RequestInterceptorSignal(); + /** + * @copydoc Dali::Toolkit::WebView::ConsoleMessageSignal() + */ + Dali::Toolkit::WebView::WebViewConsoleMessageSignalType& ConsoleMessageSignal(); + public: // Properties /** * @brief Called when a property of an object of this type is set. @@ -412,7 +417,7 @@ private: WebView& operator=(const WebView& webView); /** - * @brief Sets an absolute scroll of the given view. + * @brief Set an absolute scroll of the given view. * @param[in] x The coordinate x of scroll * @param[in] y The coordinate y of scroll */ @@ -426,54 +431,54 @@ private: Dali::Vector2 GetScrollPosition() const; /** - * @brief Gets the possible scroll size of the given view. + * @brief Get the possible scroll size of the given view. * @param[out] width The width of scroll size * @param[out] height The height of scroll size */ Dali::Vector2 GetScrollSize() const; /** - * @brief Gets the last known content's size. + * @brief Get the last known content's size. * @param[out] width The width of content's size * @param[out] height The height of content's size */ Dali::Vector2 GetContentSize() const; /** - * @brief Returns the title of the Web. + * @brief Return the title of the Web. * * @return The title of web page */ std::string GetTitle() const; /** - * @brief Sets the background color of web page. + * @brief Set the background color of web page. * @param[in] color The value of background color. */ void SetDocumentBackgroundColor(Dali::Vector4 color); /** - * @brief Clears tiles when hidden. + * @brief Clear tiles when hidden. * * @param[in] cleared Whether tiles are cleared or not */ void ClearTilesWhenHidden(bool cleared); /** - * @brief Sets multiplier of cover area of tile. + * @brief Set multiplier of cover area of tile. * * @param[in] multiplier The multiplier of cover area */ void SetTileCoverAreaMultiplier(float multiplier); /** - * @brief Enables cursor by client. + * @brief Enable cursor by client. * @param[in] enabled Whether cursor is enabled or not. */ void EnableCursorByClient(bool enabled); /** - * @brief Gets the selected text. + * @brief Get the selected text. * @return The selected text */ std::string GetSelectedText() const; @@ -491,31 +496,31 @@ private: void SetUserAgent(const std::string& userAgent); /** - * @brief Sets zoom factor of the current page. + * @brief Set zoom factor of the current page. * @param[in] zoomFactor a new factor to be set. */ void SetPageZoomFactor(float zoomFactor); /** - * @brief Queries the current zoom factor of the page。 + * @brief Query the current zoom factor of the page。 * @return The current page zoom factor. */ float GetPageZoomFactor() const; /** - * @brief Sets the current text zoom level。. + * @brief Set the current text zoom level。. * @param[in] zoomFactor a new factor to be set. */ void SetTextZoomFactor(float zoomFactor); /** - * @brief Gets the current text zoom level. + * @brief Get the current text zoom level. * @return The current text zoom factor. */ float GetTextZoomFactor() const; /** - * @brief Gets the current load progress of the page. + * @brief Get the current load progress of the page. * @return The load progress of the page. */ float GetLoadProgressPercentage() const; @@ -529,7 +534,7 @@ private: bool SetVisibility(bool visible); /** - * @brief Updates display area of web view. + * @brief Update display area of web view. * @param[in] source The soource triggers Notification. */ void UpdateDisplayArea(Dali::PropertyNotification& source); @@ -547,14 +552,14 @@ private: void EnableBlendMode(bool blendEnabled); /** - * @brief Enables/disables mouse events. The default is enabled. + * @brief Enable/disable mouse events. The default is enabled. * * @param[in] enabled True if mouse events are enabled, false otherwise */ void EnableMouseEvents(bool enabled); /** - * @brief Enables/disables key events. The default is enabled. + * @brief Enable/disable key events. The default is enabled. * * @param[in] enabled True if key events enabled, false otherwise */ @@ -590,7 +595,7 @@ private: * @param[in] url The url currently being loaded * @param[in] errorCode The error code */ - void OnPageLoadError(const std::string& url, int errorCode); + void OnPageLoadError(std::shared_ptr error); /** * @brief Callback function to be called when scroll edge is reached. @@ -659,6 +664,12 @@ private: */ void OnInterceptRequest(std::shared_ptr interceptor); + /** + * @brief Callback function to be called when console message will be logged. + * @param[in] message The message logged. + */ + void OnConsoleMessage(std::shared_ptr message); + private: std::string mUrl; Dali::Toolkit::Visual::Base mVisual; @@ -674,6 +685,7 @@ private: Dali::Toolkit::WebView::WebViewFormRepostDecisionSignalType mFormRepostDecisionSignal; Dali::Toolkit::WebView::WebViewFrameRenderedSignalType mFrameRenderedSignal; Dali::Toolkit::WebView::WebViewRequestInterceptorSignalType mRequestInterceptorSignal; + Dali::Toolkit::WebView::WebViewConsoleMessageSignalType mConsoleMessageSignal; std::unique_ptr mWebContext; std::unique_ptr mWebCookieManager; -- 2.7.4