Support policy decision in web view. 55/252955/10
authorhuayong.xu <huayong.xu@samsung.com>
Wed, 3 Feb 2021 08:45:16 +0000 (16:45 +0800)
committerhuayong.xu <huayong.xu@samsung.com>
Tue, 6 Apr 2021 06:41:28 +0000 (14:41 +0800)
This patch is to add policy decision feature in web view.

Change-Id: Iddf70088cd663a3362a995b317edda04cdf05817

automated-tests/src/dali-toolkit/dali-toolkit-test-utils/toolkit-web-engine.cpp
automated-tests/src/dali-toolkit/utc-Dali-WebView.cpp
dali-toolkit/devel-api/controls/web-view/web-view.cpp
dali-toolkit/devel-api/controls/web-view/web-view.h
dali-toolkit/internal/controls/web-view/web-view-impl.cpp
dali-toolkit/internal/controls/web-view/web-view-impl.h

index f400f75..00c08c0 100755 (executable)
@@ -24,6 +24,8 @@
 #include <dali/devel-api/adaptor-framework/web-engine-context.h>
 #include <dali/devel-api/adaptor-framework/web-engine-cookie-manager.h>
 #include <dali/devel-api/adaptor-framework/web-engine-form-repost-decision.h>
+#include <dali/devel-api/adaptor-framework/web-engine-frame.h>
+#include <dali/devel-api/adaptor-framework/web-engine-policy-decision.h>
 #include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
 #include <dali/devel-api/adaptor-framework/web-engine-load-error.h>
 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
@@ -234,6 +236,85 @@ public:
   void Reply(bool allowed) override {}
 };
 
+class MockWebEngineFrame : public Dali::WebEngineFrame
+{
+public:
+  MockWebEngineFrame()
+  {
+  }
+
+  bool IsMainFrame() const override
+  {
+    return true;
+  }
+};
+
+class MockWebEnginePolicyDecision : public Dali::WebEnginePolicyDecision
+{
+public:
+  MockWebEnginePolicyDecision()
+  {
+  }
+
+  std::string GetUrl() const override
+  {
+    return "http://test.html";
+  }
+
+  std::string GetCookie() const override
+  {
+    return "test:abc";
+  }
+
+  Dali::WebEnginePolicyDecision::DecisionType GetDecisionType() const
+  {
+    return Dali::WebEnginePolicyDecision::DecisionType::USE;
+  }
+
+  std::string GetResponseMime() const
+  {
+    return "txt/xml";
+  }
+
+  int32_t GetResponseStatusCode() const
+  {
+    return 500;
+  }
+
+  Dali::WebEnginePolicyDecision::NavigationType GetNavigationType() const
+  {
+    return Dali::WebEnginePolicyDecision::NavigationType::LINK_CLICKED;
+  }
+
+  Dali::WebEngineFrame& GetFrame() const
+  {
+    return *(Dali::WebEngineFrame*)(&mockWebFrame);
+  }
+
+  std::string GetScheme() const
+  {
+    return "test";
+  }
+
+  bool Use()
+  {
+    return true;
+  }
+
+  bool Ignore()
+  {
+    return true;
+  }
+
+  bool Suspend()
+  {
+    return true;
+  }
+
+private:
+  MockWebEngineFrame mockWebFrame;
+};
+
 class MockWebEngineRequestInterceptor : public WebEngineRequestInterceptor
 {
 public:
@@ -902,6 +983,11 @@ public:
     return mConsoleMessageSignal;
   }
 
+  Dali::WebEnginePlugin::WebEnginePolicyDecisionSignalType& PolicyDecisionSignal()
+  {
+    return mPolicyDecisionSignal;
+  }
+
   std::string              mUrl;
   std::vector<std::string> mHistory;
   size_t                   mCurrentPlusOnePos;
@@ -917,6 +1003,7 @@ public:
   Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType      mFrameRenderedSignal;
   Dali::WebEnginePlugin::WebEngineRequestInterceptorSignalType mRequestInterceptorSignal;
   Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType     mConsoleMessageSignal;
+  Dali::WebEnginePlugin::WebEnginePolicyDecisionSignalType     mPolicyDecisionSignal;
 
   bool  mEvaluating;
   float mPageZoomFactor;
@@ -983,8 +1070,8 @@ bool OnLoadUrl()
     gInstance->mPageLoadFinishedSignal.Emit( gInstance->mUrl );
     gInstance->mUrlChangedSignal.Emit( "http://new-test" );
 
-    std::shared_ptr<Dali::WebEngineFormRepostDecision> decision(new MockWebEngineFormRepostDecision());
-    gInstance->mFormRepostDecisionSignal.Emit(std::move(decision));
+    std::shared_ptr<Dali::WebEngineFormRepostDecision> repostDecision(new MockWebEngineFormRepostDecision());
+    gInstance->mFormRepostDecisionSignal.Emit(std::move(repostDecision));
     gInstance->mFrameRenderedSignal.Emit();
     std::shared_ptr<Dali::WebEngineRequestInterceptor> interceptor(new MockWebEngineRequestInterceptor());
     gInstance->mRequestInterceptorSignal.Emit(std::move(interceptor));
@@ -993,6 +1080,8 @@ bool OnLoadUrl()
     gInstance->mPageLoadErrorSignal.Emit(std::move(error));
     std::shared_ptr<Dali::WebEngineConsoleMessage> message(new MockWebEngineConsoleMessage());
     gInstance->mConsoleMessageSignal.Emit(std::move(message));
+    std::shared_ptr<Dali::WebEnginePolicyDecision> policyDecision(new MockWebEnginePolicyDecision());
+    gInstance->mPolicyDecisionSignal.Emit(std::move(policyDecision));
   }
   return false;
 }
@@ -1552,7 +1641,7 @@ Dali::WebEnginePlugin::WebEngineUrlChangedSignalType& WebEngine::UrlChangedSigna
 
 Dali::WebEnginePlugin::WebEngineFormRepostDecisionSignalType& WebEngine::FormRepostDecisionSignal()
 {
-  return Internal::Adaptor::GetImplementation( *this ).FormRepostDecisionSignal();
+  return Internal::Adaptor::GetImplementation(*this).FormRepostDecisionSignal();
 }
 
 Dali::WebEnginePlugin::WebEngineFrameRenderedSignalType& WebEngine::FrameRenderedSignal()
@@ -1570,5 +1659,10 @@ Dali::WebEnginePlugin::WebEngineConsoleMessageSignalType& WebEngine::ConsoleMess
   return Internal::Adaptor::GetImplementation(*this).ConsoleMessageSignal();
 }
 
+Dali::WebEnginePlugin::WebEnginePolicyDecisionSignalType& WebEngine::PolicyDecisionSignal()
+{
+  return Internal::Adaptor::GetImplementation(*this).PolicyDecisionSignal();
+}
+
 } // namespace Dali;
 
index 912bc62..75f6977 100755 (executable)
@@ -22,6 +22,8 @@
 #include "dali-toolkit-test-utils/toolkit-timer.h"
 
 #include <dali.h>
+#include <dali/devel-api/adaptor-framework/web-engine-frame.h>
+#include <dali/devel-api/adaptor-framework/web-engine-policy-decision.h>
 #include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
 #include <dali/devel-api/adaptor-framework/web-engine-console-message.h>
 #include <dali/devel-api/adaptor-framework/web-engine-load-error.h>
@@ -67,12 +69,14 @@ static bool gTouched = false;
 static bool gHovered = false;
 static bool gWheelEventHandled = false;
 static int gFormRepostDecisionCallbackCalled = 0;
-static std::shared_ptr<Dali::Toolkit::WebFormRepostDecision> gFormRepostDecisionInstance;
+static std::shared_ptr<Dali::Toolkit::WebFormRepostDecision> gFormRepostDecisionInstance = nullptr;
 static int gFrameRenderedCallbackCalled = 0;
 static int gRequestInterceptorCallbackCalled = 0;
 static std::shared_ptr<Dali::WebEngineRequestInterceptor> gRequestInterceptorInstance = nullptr;
 static int gConsoleMessageCallbackCalled = 0;
 static std::shared_ptr<Dali::WebEngineConsoleMessage> gConsoleMessageInstance = nullptr;
+static int gPolicyDecisionCallbackCalled = 0;
+static std::shared_ptr<Dali::WebEnginePolicyDecision> gPolicyDecisionInstance = nullptr;
 
 struct CallbackFunctor
 {
@@ -108,6 +112,12 @@ static void OnScrollEdgeReached( WebView view, Dali::WebEnginePlugin::ScrollEdge
   gScrollEdgeReachedCallbackCalled++;
 }
 
+static void OnPolicyDecisionRequest(WebView view, std::shared_ptr<Dali::WebEnginePolicyDecision> decision)
+{
+  gPolicyDecisionCallbackCalled++;
+  gPolicyDecisionInstance = std::move(decision);
+}
+
 static void OnUrlChanged( WebView view, const std::string& url )
 {
   gUrlChangedCallbackCalled++;
@@ -179,7 +189,7 @@ static bool OnWheelEvent( Actor actor, const Dali::WheelEvent& wheel )
 static void OnFormRepostDecision(WebView, std::shared_ptr<Dali::Toolkit::WebFormRepostDecision> decision)
 {
   gFormRepostDecisionCallbackCalled++;
-  gFormRepostDecisionInstance = decision;
+  gFormRepostDecisionInstance = std::move(decision);
 }
 
 static void OnFrameRendered(WebView)
@@ -229,7 +239,6 @@ int UtcDaliWebViewBasics(void)
   assign = copy;
   DALI_TEST_CHECK( assign == view );
 
-
   // DownCast Test
   tet_infoline( "UtcDaliWebViewBasic DownCast Test" );
   BaseHandle handle(view);
@@ -239,7 +248,6 @@ int UtcDaliWebViewBasics(void)
   DALI_TEST_CHECK( view2 );
   DALI_TEST_CHECK( view == view2 );
 
-
   // TypeRegistry Test
   tet_infoline( "UtcDaliWebViewBasic TypeRegistry Test" );
   TypeRegistry typeRegistry = TypeRegistry::Get();
@@ -1076,6 +1084,55 @@ int UtcDaliWebViewHttpRequestInterceptor(void)
   END_TEST;
 }
 
+int UtcDaliWebViewPolicyDecisionRequest(void)
+{
+  ToolkitTestApplication application;
+
+  WebView view = WebView::New();
+  DALI_TEST_CHECK( view );
+
+  // load url.
+  ConnectionTracker* testTracker = new ConnectionTracker();
+  view.PolicyDecisionSignal().Connect( &OnPolicyDecisionRequest );
+  bool signal1 = false;
+  view.ConnectSignal( testTracker, "policyDecision", CallbackFunctor(&signal1) );
+  DALI_TEST_EQUALS( gPolicyDecisionCallbackCalled, 0, TEST_LOCATION );
+  DALI_TEST_CHECK(gPolicyDecisionInstance == 0);
+
+  view.LoadUrl( TEST_URL1 );
+  Test::EmitGlobalTimerSignal();
+  DALI_TEST_EQUALS( gPolicyDecisionCallbackCalled, 1, TEST_LOCATION );
+  DALI_TEST_CHECK( signal1 );
+
+  // check policy decision & its frame.
+  DALI_TEST_CHECK(gPolicyDecisionInstance != 0);
+  std::string testUrl("http://test.html");
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetUrl(), testUrl, TEST_LOCATION);
+  std::string testCookie("test:abc");
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetCookie(), testCookie, TEST_LOCATION);
+  Dali::WebEnginePolicyDecision::DecisionType testDecisionType = Dali::WebEnginePolicyDecision::DecisionType::USE;
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetDecisionType(), testDecisionType, TEST_LOCATION);
+  std::string testResponseMime("txt/xml");
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetResponseMime(), testResponseMime, TEST_LOCATION);
+  int32_t ResponseStatusCode = 500;
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetResponseStatusCode(), ResponseStatusCode, TEST_LOCATION);
+  Dali::WebEnginePolicyDecision::NavigationType testNavigationType = Dali::WebEnginePolicyDecision::NavigationType::LINK_CLICKED;
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetNavigationType(), testNavigationType, TEST_LOCATION);
+  std::string testScheme("test");
+  DALI_TEST_EQUALS(gPolicyDecisionInstance->GetScheme(), testScheme, TEST_LOCATION);
+  DALI_TEST_CHECK(gPolicyDecisionInstance->Use());
+  DALI_TEST_CHECK(gPolicyDecisionInstance->Ignore());
+  DALI_TEST_CHECK(gPolicyDecisionInstance->Suspend());
+
+  Dali::WebEngineFrame* webFrame = &(gPolicyDecisionInstance->GetFrame());
+  DALI_TEST_CHECK(webFrame);
+  DALI_TEST_CHECK(webFrame->IsMainFrame());
+
+  gPolicyDecisionInstance = nullptr;
+
+  END_TEST;
+}
+
 int UtcDaliWebViewEvaluteJavaScript(void)
 {
   ToolkitTestApplication application;
index 886e6d1..ebdb218 100755 (executable)
@@ -349,6 +349,11 @@ WebView::WebViewConsoleMessageSignalType& WebView::ConsoleMessageSignal()
   return Dali::Toolkit::GetImpl(*this).ConsoleMessageSignal();
 }
 
+WebView::WebViewPolicyDecisionSignalType& WebView::PolicyDecisionSignal()
+{
+  return Dali::Toolkit::GetImpl(*this).PolicyDecisionSignal();
+}
+
 WebView::WebView(Internal::WebView& implementation)
 : Control(implementation)
 {
index bc48d61..d307d76 100755 (executable)
@@ -231,6 +231,11 @@ public:
    */
   using WebViewConsoleMessageSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEngineConsoleMessage>)>;
 
+  /**
+   * @brief WebView signal type related with policy decision.
+   */
+  using WebViewPolicyDecisionSignalType = Signal<void(WebView, std::shared_ptr<Dali::WebEnginePolicyDecision>)>;
+
 public:
   /**
    * @brief Creates an initialized WebView.
@@ -702,6 +707,13 @@ public:
    */
   WebViewConsoleMessageSignalType& ConsoleMessageSignal();
 
+  /**
+   * @brief Connects to this signal to be notified when new policy would be decided.
+   *
+   * @return A signal object to connect with.
+   */
+  WebViewPolicyDecisionSignalType& PolicyDecisionSignal();
+
 public: // Not intended for application developers
   /// @cond internal
   /**
index eb25d3b..7db2689 100644 (file)
@@ -26,6 +26,7 @@
 #include <dali/devel-api/adaptor-framework/web-engine-form-repost-decision.h>
 #include <dali/devel-api/adaptor-framework/web-engine-request-interceptor.h>
 #include <dali/devel-api/adaptor-framework/web-engine-load-error.h>
+#include <dali/devel-api/adaptor-framework/web-engine-policy-decision.h>
 #include <dali/devel-api/adaptor-framework/web-engine-settings.h>
 #include <dali/devel-api/common/stage.h>
 #include <dali/devel-api/scripting/enum-helper.h>
@@ -92,6 +93,7 @@ DALI_SIGNAL_REGISTRATION(Toolkit, WebView, "formRepostDecision", FORM_REPOST_DEC
 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_SIGNAL_REGISTRATION(Toolkit, WebView, "policyDecision",     POLICY_DECISION             )
 
 DALI_TYPE_REGISTRATION_END()
 // clang-format on
@@ -116,8 +118,8 @@ WebView::WebView(const std::string& locale, const std::string& timezoneId)
   mPageLoadFinishedSignal(),
   mPageLoadErrorSignal(),
   mUrlChangedSignal(),
-  mVideoHoleEnabled(true),
   mWebViewArea(0, 0, mWebViewSize.width, mWebViewSize.height),
+  mVideoHoleEnabled(true),
   mMouseEventsEnabled(true),
   mKeyEventsEnabled(true)
 {
@@ -140,8 +142,8 @@ WebView::WebView(int argc, char** argv)
   mPageLoadFinishedSignal(),
   mPageLoadErrorSignal(),
   mUrlChangedSignal(),
-  mVideoHoleEnabled(true),
   mWebViewArea(0, 0, mWebViewSize.width, mWebViewSize.height),
+  mVideoHoleEnabled(true),
   mMouseEventsEnabled(true),
   mKeyEventsEnabled(true)
 {
@@ -223,6 +225,7 @@ void WebView::OnInitialize()
     mWebEngine.FrameRenderedSignal().Connect(this, &WebView::OnFrameRendered);
     mWebEngine.RequestInterceptorSignal().Connect(this, &WebView::OnInterceptRequest);
     mWebEngine.ConsoleMessageSignal().Connect(this, &WebView::OnConsoleMessage);
+    mWebEngine.PolicyDecisionSignal().Connect(this, &WebView::OnPolicyDecisionRequest);
 
     mWebContext         = std::unique_ptr<Dali::Toolkit::WebContext>(new WebContext(mWebEngine.GetContext()));
     mWebCookieManager   = std::unique_ptr<Dali::Toolkit::WebCookieManager>(new WebCookieManager(mWebEngine.GetCookieManager()));
@@ -726,6 +729,11 @@ Dali::Toolkit::WebView::WebViewConsoleMessageSignalType& WebView::ConsoleMessage
   return mConsoleMessageSignal;
 }
 
+Dali::Toolkit::WebView::WebViewPolicyDecisionSignalType& WebView::PolicyDecisionSignal()
+{
+  return mPolicyDecisionSignal;
+}
+
 void WebView::OnPageLoadStarted(const std::string& url)
 {
   if(!mPageLoadStartedSignal.Empty())
@@ -834,6 +842,15 @@ void WebView::OnConsoleMessage(std::shared_ptr<Dali::WebEngineConsoleMessage> me
   }
 }
 
+void WebView::OnPolicyDecisionRequest(std::shared_ptr<Dali::WebEnginePolicyDecision> decision)
+{
+  if(!mPolicyDecisionSignal.Empty())
+  {
+    Dali::Toolkit::WebView handle(GetOwner());
+    mPolicyDecisionSignal.Emit(handle, std::move(decision));
+  }
+}
+
 bool WebView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor)
 {
   Dali::BaseHandle handle(object);
@@ -891,6 +908,11 @@ bool WebView::DoConnectSignal(BaseObject* object, ConnectionTrackerInterface* tr
     webView.ConsoleMessageSignal().Connect(tracker, functor);
     connected = true;
   }
+  else if(0 == strcmp(signalName.c_str(), POLICY_DECISION))
+  {
+    webView.PolicyDecisionSignal().Connect(tracker, functor);
+    connected = true;
+  }
 
   return connected;
 }
index cf5319f..5a2751a 100755 (executable)
@@ -349,6 +349,11 @@ public:
    */
   Dali::Toolkit::WebView::WebViewConsoleMessageSignalType& ConsoleMessageSignal();
 
+  /**
+   * @copydoc Dali::Toolkit::WebView::PolicyDecisionSignal()
+   */
+  Dali::Toolkit::WebView::WebViewPolicyDecisionSignalType& PolicyDecisionSignal();
+
 public: // Properties
   /**
    * @brief Called when a property of an object of this type is set.
@@ -599,7 +604,7 @@ private:
 
   /**
    * @brief Callback function to be called when scroll edge is reached.
-   * @param[in] e The scroll edge reached.
+   * @param[in] edge The scroll edge reached.
    */
   void OnScrollEdgeReached(Dali::WebEnginePlugin::ScrollEdge edge);
 
@@ -670,6 +675,12 @@ private:
    */
   void OnConsoleMessage(std::shared_ptr<Dali::WebEngineConsoleMessage> message);
 
+  /**
+   * @brief Callback function to be called when policy need be decided.
+   * @param[in] decision The policy decided.
+   */
+  void OnPolicyDecisionRequest(std::shared_ptr<Dali::WebEnginePolicyDecision> decision);
+
 private:
   std::string                 mUrl;
   Dali::Toolkit::Visual::Base mVisual;
@@ -686,6 +697,7 @@ private:
   Dali::Toolkit::WebView::WebViewFrameRenderedSignalType      mFrameRenderedSignal;
   Dali::Toolkit::WebView::WebViewRequestInterceptorSignalType mRequestInterceptorSignal;
   Dali::Toolkit::WebView::WebViewConsoleMessageSignalType     mConsoleMessageSignal;
+  Dali::Toolkit::WebView::WebViewPolicyDecisionSignalType     mPolicyDecisionSignal;
 
   std::unique_ptr<Dali::Toolkit::WebContext>         mWebContext;
   std::unique_ptr<Dali::Toolkit::WebCookieManager>   mWebCookieManager;
@@ -697,8 +709,8 @@ private:
   Dali::PropertyNotification mPositionUpdateNotification;
   Dali::PropertyNotification mSizeUpdateNotification;
   Dali::PropertyNotification mScaleUpdateNotification;
-  bool                       mVideoHoleEnabled;
   Dali::Rect<int>            mWebViewArea;
+  bool                       mVideoHoleEnabled;
   bool                       mMouseEventsEnabled;
   bool                       mKeyEventsEnabled;