From f4e0f2637386a36e21c7af1e0dd5e824e1ead1cc Mon Sep 17 00:00:00 2001 From: "huayong.xu" Date: Fri, 2 Apr 2021 19:18:33 +0800 Subject: [PATCH] Add some APIs into web context. This patch is to add some APIs with callbacks into web context. Change-Id: I234c433a169569b944162edf5f8d4ac34733dacb --- .../dali-toolkit-test-utils/toolkit-web-engine.cpp | 184 ++++++++++++++++++++- .../src/dali-toolkit/utc-Dali-WebView.cpp | 115 ++++++++++++- .../devel-api/controls/web-view/web-context.cpp | 57 ++++++- .../devel-api/controls/web-view/web-context.h | 106 ++++++++++-- 4 files changed, 436 insertions(+), 26 deletions(-) mode change 100644 => 100755 dali-toolkit/devel-api/controls/web-view/web-context.h 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 14f2c1f..b33e169 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 @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -49,14 +50,16 @@ namespace Adaptor { class WebEngine; +class MockWebEngineContext; namespace { // Generally only one WebEngine instance exists. // If > 1, a new web engine has been created by CreateWindowSignal. -static WebEngine* gInstance = 0; +static WebEngine* gInstance = nullptr; static int gInstanceCount = 0; +static MockWebEngineContext* gWebEngineContextInstance = nullptr; bool OnGoBack(); bool OnGoForward(); @@ -70,6 +73,11 @@ bool OnScreenshotCaptured(); bool OnVideoPlaying(); bool OnGeolocationPermission(); bool OnClearHistory(); +bool OnSecurityOriginAcquired(); +bool OnStorageUsageAcquired(); +bool OnFormPasswordAcquired(); +bool OnDownloadStarted(); +bool OnMimeOverridden(); static void ConnectToGlobalSignal( bool ( *func )() ) { @@ -114,12 +122,52 @@ public: { } - void DeleteWebDatabase() override + void DeleteAllWebDatabase() override { } - void DeleteWebStorage() override + bool GetWebDatabaseOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback) { + if (callback) + { + ConnectToGlobalSignal(&OnSecurityOriginAcquired); + mSecurityOriginAcquiredCallback = callback; + } + return true; + } + + bool DeleteWebDatabase(Dali::WebEngineSecurityOrigin& origin) + { + return true; + } + + bool GetWebStorageOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback) + { + if (callback) + { + ConnectToGlobalSignal(&OnSecurityOriginAcquired); + mSecurityOriginAcquiredCallback = callback; + } + return true; + } + + bool GetWebStorageUsageForOrigin(Dali::WebEngineSecurityOrigin& origin, Dali::WebEngineContext::WebEngineStorageUsageAcquiredCallback callback) + { + if (callback) + { + ConnectToGlobalSignal(&OnStorageUsageAcquired); + mStorageUsageAcquiredCallback = callback; + } + return true; + } + + void DeleteAllWebStorage() override + { + } + + bool DeleteWebStorageOrigin(Dali::WebEngineSecurityOrigin& origin) + { + return true; } void DeleteLocalFileSystem() override @@ -134,10 +182,73 @@ public: { } + bool DeleteApplicationCache(Dali::WebEngineSecurityOrigin& origin) + { + return true; + } + + void GetFormPasswordList(Dali::WebEngineContext::WebEngineFormPasswordAcquiredCallback callback) + { + if (callback) + { + ConnectToGlobalSignal(&OnFormPasswordAcquired); + mFormPasswordAcquiredCallback = callback; + } + } + + void RegisterDownloadStartedCallback(Dali::WebEngineContext::WebEngineDownloadStartedCallback callback) + { + if (callback) + { + ConnectToGlobalSignal(&OnDownloadStarted); + mDownloadStartedCallback = callback; + } + } + + void RegisterMimeOverriddenCallback(Dali::WebEngineContext::WebEngineMimeOverriddenCallback callback) + { + if (callback) + { + ConnectToGlobalSignal(&OnMimeOverridden); + mMimeOverriddenCallback = callback; + } + } + +public: + Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback mSecurityOriginAcquiredCallback; + Dali::WebEngineContext::WebEngineStorageUsageAcquiredCallback mStorageUsageAcquiredCallback; + Dali::WebEngineContext::WebEngineFormPasswordAcquiredCallback mFormPasswordAcquiredCallback; + Dali::WebEngineContext::WebEngineDownloadStartedCallback mDownloadStartedCallback; + Dali::WebEngineContext::WebEngineMimeOverriddenCallback mMimeOverriddenCallback; + private: Dali::WebEngineContext::CacheModel mockModel; }; +class MockWebEngineSecurityOrigin : public Dali::WebEngineSecurityOrigin +{ +public: + MockWebEngineSecurityOrigin() + : mockUrl("https://test.html") + , mockPotocol("https") + { + } + + std::string GetHost() const + { + return mockUrl; + } + + std::string GetProtocol() const + { + return mockPotocol; + } + +private: + std::string mockUrl; + std::string mockPotocol; +}; + class MockWebEngineCookieManager : public Dali::WebEngineCookieManager { public: @@ -718,7 +829,12 @@ public: } mockWebEngineSettings = new MockWebEngineSettings(); - mockWebEngineContext = new MockWebEngineContext(); + MockWebEngineContext* engineContext = new MockWebEngineContext(); + mockWebEngineContext = engineContext; + if ( gInstanceCount == 1 ) + { + gWebEngineContextInstance = engineContext; + } mockWebEngineCookieManager = new MockWebEngineCookieManager(); mockWebEngineBackForwardList = new MockWebEngineBackForwardList(); } @@ -729,6 +845,7 @@ public: if( !gInstanceCount ) { gInstance = 0; + gWebEngineContextInstance = 0; } delete mockWebEngineSettings; @@ -1271,6 +1388,65 @@ bool OnClearHistory() return false; } +bool OnSecurityOriginAcquired() +{ + DisconnectFromGlobalSignal(&OnSecurityOriginAcquired); + if (gWebEngineContextInstance) + { + std::vector> securityOriginList; + std::unique_ptr origin(new MockWebEngineSecurityOrigin()); + securityOriginList.push_back(std::move(origin)); + gWebEngineContextInstance->mSecurityOriginAcquiredCallback(securityOriginList); + } + return false; +} + +bool OnStorageUsageAcquired() +{ + DisconnectFromGlobalSignal(&OnStorageUsageAcquired); + if (gWebEngineContextInstance) + { + gWebEngineContextInstance->mStorageUsageAcquiredCallback(0); + } + return false; +} + +bool OnFormPasswordAcquired() +{ + DisconnectFromGlobalSignal(&OnFormPasswordAcquired); + if (gWebEngineContextInstance) + { + std::vector> formPasswordList; + std::unique_ptr data(new Dali::WebEngineContext::PasswordData()); + data->url = "http://test.html"; + data->useFingerprint = false; + formPasswordList.push_back(std::move(data)); + gWebEngineContextInstance->mFormPasswordAcquiredCallback(formPasswordList); + } + return false; +} + +bool OnDownloadStarted() +{ + DisconnectFromGlobalSignal(&OnDownloadStarted); + if (gWebEngineContextInstance) + { + gWebEngineContextInstance->mDownloadStartedCallback("http://test.html"); + } + return false; +} + +bool OnMimeOverridden() +{ + DisconnectFromGlobalSignal(&OnMimeOverridden); + if (gWebEngineContextInstance) + { + std::string newMime; + gWebEngineContextInstance->mMimeOverriddenCallback("http://test.html", "txt/xml", newMime); + } + return false; +} + } // namespace inline WebEngine& GetImplementation( Dali::WebEngine& webEngine ) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp index 034e377..0d254ab 100755 --- a/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include #include #include @@ -85,6 +87,13 @@ static int gSslCertificateChangedCallbackCalled = 0; static std::shared_ptr gSslCertificateInstance = nullptr; static int gHttpAuthHandlerCallbackCalled = 0; static std::shared_ptr gHttpAuthInstance = nullptr; +static int gSecurityOriginsAcquiredCallbackCalled = 0; +static int gStorageUsageAcquiredCallbackCalled = 0; +static int gFormPasswordsAcquiredCallbackCalled = 0; +static int gDownloadStartedCallbackCalled = 0; +static int gMimeOverriddenCallbackCalled = 0; +static std::vector> gSecurityOriginList; +static std::vector> gPasswordDataList; struct CallbackFunctor { @@ -162,7 +171,7 @@ static bool OnJavaScriptPrompt( const std::string& meesage1, const std::string& static void OnScreenshotCaptured(Dali::Toolkit::ImageView) { - gScreenshotCapturedCallbackCalled++; + gScreenshotCapturedCallbackCalled++; } static void OnVideoPlaying(bool isPlaying) @@ -235,6 +244,36 @@ static void OnHttpAuthHandler( WebView view, std::shared_ptr>& origins) +{ + gSecurityOriginsAcquiredCallbackCalled++; + gSecurityOriginList.clear(); + gSecurityOriginList.swap(origins); +} + +static void OnStorageUsageAcquired(uint64_t usage) +{ + gStorageUsageAcquiredCallbackCalled++; +} + +static void OnFormPasswordsAcquired(std::vector>& passwords) +{ + gFormPasswordsAcquiredCallbackCalled++; + gPasswordDataList.clear(); + gPasswordDataList.swap(passwords); +} + +static void OnDownloadStarted(const std::string& url) +{ + gDownloadStartedCallbackCalled++; +} + +static bool OnMimeOverridden(const std::string&, const std::string&, std::string&) +{ + gMimeOverriddenCallbackCalled++; + return false; +} + } // namespace void web_view_startup(void) @@ -1398,8 +1437,8 @@ int UtcDaliWebContextGetSetCacheModel(void) context->SetCertificateFilePath( kDefaultValue ); context->DisableCache( false ); context->SetDefaultProxyAuth( kDefaultValue, kDefaultValue ); - context->DeleteWebDatabase(); - context->DeleteWebStorage(); + context->DeleteAllWebDatabase(); + context->DeleteAllWebStorage(); context->DeleteLocalFileSystem(); context->ClearCache(); @@ -1415,6 +1454,76 @@ int UtcDaliWebContextGetSetCacheModel(void) END_TEST; } +int UtcDaliWebContextGetWebDatabaseStorageOrigins(void) +{ + ToolkitTestApplication application; + + WebView view = WebView::New(); + DALI_TEST_CHECK( view ); + + Dali::Toolkit::WebContext* context = view.GetContext(); + DALI_TEST_CHECK( context != 0 ) + + std::string kDefaultValue; + + // get origins of web database + bool result = context->GetWebDatabaseOrigins(&OnSecurityOriginsAcquired); + DALI_TEST_CHECK( result ); + + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS( gSecurityOriginsAcquiredCallbackCalled, 1, TEST_LOCATION ); + DALI_TEST_CHECK(gSecurityOriginList.size() == 1); + + Dali::WebEngineSecurityOrigin* origin = gSecurityOriginList[0].get(); + DALI_TEST_CHECK( origin ); + + result = context->DeleteWebDatabase(*origin); + DALI_TEST_CHECK( result ); + + // get origins of web storage + result = context->GetWebStorageOrigins(&OnSecurityOriginsAcquired); + DALI_TEST_CHECK( result ); + + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS( gSecurityOriginsAcquiredCallbackCalled, 2, TEST_LOCATION ); + DALI_TEST_CHECK(gSecurityOriginList.size() == 1); + + origin = gSecurityOriginList[0].get(); + DALI_TEST_CHECK( origin ); + + result = context->GetWebStorageUsageForOrigin(*origin, &OnStorageUsageAcquired); + DALI_TEST_CHECK( result ); + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS( gStorageUsageAcquiredCallbackCalled, 1, TEST_LOCATION ); + + result = context->DeleteWebStorageOrigin(*origin); + DALI_TEST_CHECK( result ); + + result = context->DeleteApplicationCache(*origin); + DALI_TEST_CHECK( result ); + + // form passwords, download state, mime type. + context->GetFormPasswordList(&OnFormPasswordsAcquired); + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS(gFormPasswordsAcquiredCallbackCalled, 1, TEST_LOCATION); + DALI_TEST_CHECK(gPasswordDataList.size() == 1); + DALI_TEST_EQUALS(gPasswordDataList[0]->url, "http://test.html", TEST_LOCATION); + DALI_TEST_CHECK(gPasswordDataList[0]->useFingerprint == false); + + context->RegisterDownloadStartedCallback(&OnDownloadStarted); + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS(gDownloadStartedCallbackCalled, 1, TEST_LOCATION); + + context->RegisterMimeOverriddenCallback(&OnMimeOverridden); + Test::EmitGlobalTimerSignal(); + DALI_TEST_EQUALS(gMimeOverriddenCallbackCalled, 1, TEST_LOCATION); + + gSecurityOriginList.clear(); + gPasswordDataList.clear(); + + END_TEST; +} + // test cases for web cookie manager. int UtcDaliWebCookieManagerGetSetCookieAcceptPolicy(void) diff --git a/dali-toolkit/devel-api/controls/web-view/web-context.cpp b/dali-toolkit/devel-api/controls/web-view/web-context.cpp index c72012c..413f5ca 100644 --- a/dali-toolkit/devel-api/controls/web-view/web-context.cpp +++ b/dali-toolkit/devel-api/controls/web-view/web-context.cpp @@ -18,6 +18,9 @@ // CLASS HEADER #include +// EXTERNAL INCLUDES +#include + namespace Dali { namespace Toolkit @@ -61,14 +64,39 @@ void WebContext::SetDefaultProxyAuth(const std::string& username, const std::str mWebEngineContext.SetDefaultProxyAuth(username, password); } -void WebContext::DeleteWebDatabase() +void WebContext::DeleteAllWebDatabase() +{ + mWebEngineContext.DeleteAllWebDatabase(); +} + +bool WebContext::GetWebDatabaseOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback) { - mWebEngineContext.DeleteWebDatabase(); + return mWebEngineContext.GetWebDatabaseOrigins(callback); } -void WebContext::DeleteWebStorage() +bool WebContext::DeleteWebDatabase(Dali::WebEngineSecurityOrigin& origin) { - mWebEngineContext.DeleteWebStorage(); + return mWebEngineContext.DeleteWebDatabase(origin); +} + +bool WebContext::GetWebStorageOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback) +{ + return mWebEngineContext.GetWebStorageOrigins(callback); +} + +bool WebContext::GetWebStorageUsageForOrigin(Dali::WebEngineSecurityOrigin& origin, Dali::WebEngineContext::WebEngineStorageUsageAcquiredCallback callback) +{ + return mWebEngineContext.GetWebStorageUsageForOrigin(origin, callback); +} + +void WebContext::DeleteAllWebStorage() +{ + mWebEngineContext.DeleteAllWebStorage(); +} + +bool WebContext::DeleteWebStorageOrigin(Dali::WebEngineSecurityOrigin& origin) +{ + return mWebEngineContext.DeleteWebStorageOrigin(origin); } void WebContext::DeleteLocalFileSystem() @@ -81,6 +109,25 @@ void WebContext::ClearCache() mWebEngineContext.ClearCache(); } -} // namespace Toolkit +bool WebContext::DeleteApplicationCache(Dali::WebEngineSecurityOrigin& origin) +{ + return mWebEngineContext.DeleteApplicationCache(origin); +} + +void WebContext::GetFormPasswordList(Dali::WebEngineContext::WebEngineFormPasswordAcquiredCallback callback) +{ + mWebEngineContext.GetFormPasswordList(callback); +} +void WebContext::RegisterDownloadStartedCallback(Dali::WebEngineContext::WebEngineDownloadStartedCallback callback) +{ + mWebEngineContext.RegisterDownloadStartedCallback(callback); +} + +void WebContext::RegisterMimeOverriddenCallback(Dali::WebEngineContext::WebEngineMimeOverriddenCallback callback) +{ + mWebEngineContext.RegisterMimeOverriddenCallback(callback); +} + +} // namespace Toolkit } // namespace Dali diff --git a/dali-toolkit/devel-api/controls/web-view/web-context.h b/dali-toolkit/devel-api/controls/web-view/web-context.h old mode 100644 new mode 100755 index ead4efd..cdf7f0e --- a/dali-toolkit/devel-api/controls/web-view/web-context.h +++ b/dali-toolkit/devel-api/controls/web-view/web-context.h @@ -27,6 +27,8 @@ namespace Dali { +class WebEngineSecurityOrigin; + namespace Toolkit { /** @@ -35,16 +37,16 @@ namespace Toolkit */ /** - * @brief WebContext is a control for settings of WebView. + * @brief WebContext is a control for context of WebView. * - * For working WebContext, a WebView should be provided. + * For working WebContext, a WebEngineContext should be provided. * */ class DALI_TOOLKIT_API WebContext { public: /** - * @brief Creates a WebContext. + * @brief Create a WebContext. * * @param[in] context The context of web engine. */ @@ -56,28 +58,28 @@ public: virtual ~WebContext() final; /** - * @brief Returns the cache model type. + * @brief Return the cache model type. * * @return #Dali::WebEngineContext::CacheModel */ Dali::WebEngineContext::CacheModel GetCacheModel() const; /** - * @brief Requests to set the cache model. + * @brief Request to set the cache model. * * @param[in] cacheModel The cache model */ void SetCacheModel(Dali::WebEngineContext::CacheModel cacheModel); /** - * @brief Sets the given proxy URI to network backend of specific context. + * @brief Set the given proxy URI to network backend of specific context. * * @param[in] uri The proxy URI to set */ void SetProxyUri(const std::string& uri); /** - * Adds CA certificates to persistent NSS certificate database + * @brief Add CA certificates to persistent NSS certificate database * * Function accepts a path to a CA certificate file, a path to a directory * containing CA certificate files, or a colon-seprarated list of those. @@ -89,7 +91,7 @@ public: void SetCertificateFilePath(const std::string& certificatePath); /** - * Toggles the cache to be enabled or disabled + * @brief Toggle the cache to be enabled or disabled * * Function works asynchronously. * By default the cache is disabled resulting in not storing network data on disk. @@ -99,7 +101,7 @@ public: void DisableCache(bool cacheDisabled); /** - * @brief Sets a proxy auth credential to network backend of specific context. + * @brief Set a proxy auth credential to network backend of specific context. * * @param[in] username username to set * @param[in] password password to set @@ -107,20 +109,66 @@ public: void SetDefaultProxyAuth(const std::string& username, const std::string& password); /** - * Requests for deleting all web databases. + * @brief Requests for deleting all web databases. + */ + void DeleteAllWebDatabase(); + + /** + * @brief Request for getting web database origins. + * + * @param[in] callback callback called after getting web database origins + * + * @return true if succeeded, false otherwise + */ + bool GetWebDatabaseOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback); + + /** + * @brief Request for deleting web databases for origin. + * + * @param[in] origin database origin + * + * @return true if succeeded, false otherwise + */ + bool DeleteWebDatabase(Dali::WebEngineSecurityOrigin& origin); + + /** + * @brief Gets list of origins that is stored in web storage db. + * + * @param[in] callback callback called after getting web storage origins + * + * @return true if succeeded, false otherwise */ - void DeleteWebDatabase(); + bool GetWebStorageOrigins(Dali::WebEngineContext::WebEngineSecurityOriginAcquiredCallback callback); /** - * @brief Deletes web storage. + * @brief Get list of origins that is stored in web storage db. + * + * @param[in] origin storage origin + * @param[in] callback callback called after getting web storage origins + * + * @return true if succeeded, false otherwise + */ + bool GetWebStorageUsageForOrigin(Dali::WebEngineSecurityOrigin& origin, Dali::WebEngineContext::WebEngineStorageUsageAcquiredCallback callback); + + /** + * @brief Delete all web storage. * * @details This function does not ensure that all data will be removed. * Should be used to extend free physical memory. */ - void DeleteWebStorage(); + void DeleteAllWebStorage(); /** - * @brief Requests for deleting all local file systems. + * @brief Delete origin that is stored in web storage db. + * + * @param[in] origin origin of db + * + * @return true if succeeded, false otherwise + */ + bool DeleteWebStorageOrigin(Dali::WebEngineSecurityOrigin& origin); + + /** + * @brief Request for deleting all local file systems. */ void DeleteLocalFileSystem(); @@ -129,6 +177,36 @@ public: */ void ClearCache(); + /** + * @brief Request for deleting web application cache for origin. + * + * @param[in] origin application cache origin + * + * @return true if succeeded, false otherwise + */ + bool DeleteApplicationCache(Dali::WebEngineSecurityOrigin& origin); + + /** + * @brief Asynchronous request to get list of all password data. + * + * @param[in] callback callback called after getting form password + */ + void GetFormPasswordList(Dali::WebEngineContext::WebEngineFormPasswordAcquiredCallback callback); + + /** + * @brief Register callback for download started. + * + * @param[in] callback callback for download started + */ + void RegisterDownloadStartedCallback(Dali::WebEngineContext::WebEngineDownloadStartedCallback callback); + + /** + * @brief Register callback for mime overridden. + * + * @param[in] callback callback for mime overridden + */ + void RegisterMimeOverriddenCallback(Dali::WebEngineContext::WebEngineMimeOverriddenCallback callback); + private: Dali::WebEngineContext& mWebEngineContext; }; -- 2.7.4