[M120 Migration][VD] Implement InterceptRequestCancelCallback 21/308521/2
authorfang fengrong <fr.fang@samsung.com>
Wed, 27 Mar 2024 03:14:36 +0000 (11:14 +0800)
committerBot Blink <blinkbot@samsung.com>
Thu, 28 Mar 2024 17:51:36 +0000 (17:51 +0000)
1. Implement ewk_context_intercept_request_cancel_callback_set
2. Use scoped_refptr to manager lifecycle of _Ewk_Intercept_Request

Reference:
https://review.tizen.org/gerrit/#/c/291896

Change-Id: I8b28f6059723a4bb8d78f6b789f2ced4ec900ba4
Signed-off-by: fang fengrong <fr.fang@samsung.com>
13 files changed:
tizen_src/ewk/efl_integration/browser/network_service/proxying_url_loader_efl.cc
tizen_src/ewk/efl_integration/browser/network_service/proxying_url_loader_efl.h
tizen_src/ewk/efl_integration/browser/network_service/proxying_url_loader_factory_efl.cc
tizen_src/ewk/efl_integration/browser_context_efl.cc
tizen_src/ewk/efl_integration/browser_context_efl.h
tizen_src/ewk/efl_integration/content_browser_client_efl.cc
tizen_src/ewk/efl_integration/eweb_context.cc
tizen_src/ewk/efl_integration/eweb_context.h
tizen_src/ewk/efl_integration/private/ewk_context_private.cc
tizen_src/ewk/efl_integration/private/ewk_context_private.h
tizen_src/ewk/efl_integration/private/ewk_intercept_request_private.cc
tizen_src/ewk/efl_integration/private/ewk_intercept_request_private.h
tizen_src/ewk/efl_integration/public/ewk_context.cc

index a8170da..d7d3d91 100644 (file)
 
 // static
 void ProxyingURLLoaderEfl::Create(
-    std::unique_ptr<_Ewk_Intercept_Request> intercept_request,
+    scoped_refptr<_Ewk_Intercept_Request> intercept_request,
+#if BUILDFLAG(IS_TIZEN_TV)
+    content::BrowserContextEfl::ResourceContextEfl* resource_context_efl,
+#endif
     mojo::PendingReceiver<::network::mojom::URLLoader> loader,
     mojo::PendingRemote<network::mojom::URLLoaderClient> client) {
+#if BUILDFLAG(IS_TIZEN_TV)
+  auto* proxying_url_loader_efl = new ProxyingURLLoaderEfl(
+      std::move(intercept_request), resource_context_efl, std::move(loader),
+      std::move(client));
+#else
   auto* proxying_url_loader_efl = new ProxyingURLLoaderEfl(
       std::move(intercept_request), std::move(loader), std::move(client));
+#endif
 }
 
 ProxyingURLLoaderEfl::ProxyingURLLoaderEfl(
-    std::unique_ptr<_Ewk_Intercept_Request> intercept_request,
+    scoped_refptr<_Ewk_Intercept_Request> intercept_request,
+#if BUILDFLAG(IS_TIZEN_TV)
+    content::BrowserContextEfl::ResourceContextEfl* resource_context_efl,
+#endif
     mojo::PendingReceiver<::network::mojom::URLLoader> loader,
     mojo::PendingRemote<network::mojom::URLLoaderClient> client)
     : intercept_request_(std::move(intercept_request)),
+#if BUILDFLAG(IS_TIZEN_TV)
+      resource_context_efl_(resource_context_efl),
+#endif
       binding_(this),
       weak_ptr_factory_(this) {
   DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
@@ -38,6 +53,22 @@ ProxyingURLLoaderEfl::ProxyingURLLoaderEfl(
   client_.Bind(std::move(client));
 }
 
+ProxyingURLLoaderEfl::~ProxyingURLLoaderEfl() {
+#if BUILDFLAG(IS_TIZEN_TV)
+  LOG(INFO)
+      << "Destroyed ProxyingURLLoaderEfl, and notified app intercept url: "
+      << intercept_request_->request_url_get()
+      << ", resource_context_efl_: " << resource_context_efl_
+      << ", intercept_request_: " << intercept_request_.get();
+  if (resource_context_efl_ &&
+      resource_context_efl_->HasInterceptRequestCancelCallback()) {
+    resource_context_efl_->RunInterceptRequestCancelCallback(
+        intercept_request_.get());
+  }
+  intercept_request_->set_delegate(nullptr);
+#endif
+}
+
 void ProxyingURLLoaderEfl::ResponseDecided() {
   mojo::ScopedDataPipeProducerHandle pipe_producer_handle;
   mojo::ScopedDataPipeConsumerHandle pipe_consumer_handle;
index 50b79e9..206e5ee 100644 (file)
@@ -12,7 +12,9 @@
 #include "mojo/public/cpp/system/data_pipe.h"
 #include "private/ewk_intercept_request_private.h"
 #include "services/network/public/mojom/url_loader.mojom.h"
-
+#if BUILDFLAG(IS_TIZEN_TV)
+#include "browser_context_efl.h"
+#endif
 namespace mojo {
 class SimpleWatcher;
 }
@@ -25,11 +27,14 @@ class ProxyingURLLoaderEfl : public network::mojom::URLLoader,
                              public _Ewk_Intercept_Request::Delegate {
  public:
   static void Create(
-      std::unique_ptr<_Ewk_Intercept_Request> intercept_request,
+      scoped_refptr<_Ewk_Intercept_Request> intercept_request,
+#if BUILDFLAG(IS_TIZEN_TV)
+      content::BrowserContextEfl::ResourceContextEfl* resource_context_efl,
+#endif
       mojo::PendingReceiver<::network::mojom::URLLoader> loader,
       mojo::PendingRemote<network::mojom::URLLoaderClient> client);
 
-  ~ProxyingURLLoaderEfl() override = default;
+  ~ProxyingURLLoaderEfl() override;
 
  private:
   // network::mojom::URLLoader:
@@ -49,7 +54,10 @@ class ProxyingURLLoaderEfl : public network::mojom::URLLoader,
   void ChunkedReadDone() override;
 
   ProxyingURLLoaderEfl(
-      std::unique_ptr<_Ewk_Intercept_Request> intercept_request,
+      scoped_refptr<_Ewk_Intercept_Request> intercept_request,
+#if BUILDFLAG(IS_TIZEN_TV)
+      content::BrowserContextEfl::ResourceContextEfl* resource_context_efl,
+#endif
       mojo::PendingReceiver<::network::mojom::URLLoader> loader,
       mojo::PendingRemote<network::mojom::URLLoaderClient> client);
 
@@ -64,7 +72,10 @@ class ProxyingURLLoaderEfl : public network::mojom::URLLoader,
   void OnConnectionError();
   void MaybeDeleteSelf();
 
-  std::unique_ptr<_Ewk_Intercept_Request> intercept_request_;
+  scoped_refptr<_Ewk_Intercept_Request> intercept_request_;
+#if BUILDFLAG(IS_TIZEN_TV)
+  content::BrowserContextEfl::ResourceContextEfl* resource_context_efl_;
+#endif
 
   mojo::Receiver<network::mojom::URLLoader> binding_;
   mojo::Remote<network::mojom::URLLoaderClient> client_;
index cfc9226..29d537f 100644 (file)
@@ -158,18 +158,25 @@ void ProxyingURLLoaderFactoryEfl::CreateLoaderAndStart(
     params.upload = upload_data_stream.get();
   }
 
-  auto intercept_request = std::make_unique<_Ewk_Intercept_Request>(params);
-  resource_context_->RunInterceptRequestCallback(intercept_request.get());
+  auto intercept_request = new _Ewk_Intercept_Request(params);
+  resource_context_->RunInterceptRequestCallback(intercept_request);
   intercept_request->callback_ended();
   if (intercept_request->is_ignored()) {
     target_factory_->CreateLoaderAndStart(std::move(loader), request_id,
                                           options, request, std::move(client),
                                           traffic_annotation);
+    delete intercept_request;
     return;
   }
 
+#if BUILDFLAG(IS_TIZEN_TV)
+  ProxyingURLLoaderEfl::Create(base::WrapRefCounted(intercept_request),
+                               resource_context_, std::move(loader),
+                               std::move(client));
+#else
   ProxyingURLLoaderEfl::Create(std::move(intercept_request), std::move(loader),
                                std::move(client));
+#endif
 }
 
 void ProxyingURLLoaderFactoryEfl::Clone(
index c58550c..cd51e48 100644 (file)
@@ -67,6 +67,11 @@ void SendToAllRenderers(IPC::Message* message) {
 }
 
 #if BUILDFLAG(IS_TIZEN_TV)
+// On TV product, the cancel callback must always be registered.
+void DefaultCallback(Ewk_Context*, Ewk_Intercept_Request*, void*) {
+  NOTIMPLEMENTED();
+}
+
 void FormatHour(long time, std::ostringstream& ost) {
   static const double kMinutesPerHour = 60.0;
   ost.width(2);
@@ -119,7 +124,11 @@ static void CreateNetworkDelegateOnIOThread(BrowserContextEfl* context,
 BrowserContextEfl::ResourceContextEfl::ResourceContextEfl(
     scoped_refptr<CookieManager> cookie_manager)
     : cookie_manager_(cookie_manager),
-      intercept_request_callback_{nullptr, nullptr, nullptr} {}
+#if BUILDFLAG(IS_TIZEN_TV)
+      intercept_request_cancel_callback_{nullptr, DefaultCallback, nullptr},
+#endif
+      intercept_request_callback_{nullptr, nullptr, nullptr} {
+}
 
 BrowserContextEfl::~BrowserContextEfl() {
   NotifyWillBeDestroyed();
@@ -144,24 +153,24 @@ bool BrowserContextEfl::ResourceContextEfl::HTTPCustomHeaderAdd(
   if (name.empty())
     return false;
 
-  base::AutoLock locker(http_custom_headers_lock_);
+  base::AutoLock lock(http_custom_headers_lock_);
   return http_custom_headers_.insert(std::make_pair(name, value)).second;
 }
 
 bool BrowserContextEfl::ResourceContextEfl::HTTPCustomHeaderRemove(
     const std::string& name) {
-  base::AutoLock locker(http_custom_headers_lock_);
+  base::AutoLock lock(http_custom_headers_lock_);
   return http_custom_headers_.erase(name);
 }
 
 void BrowserContextEfl::ResourceContextEfl::HTTPCustomHeaderClear() {
-  base::AutoLock locker(http_custom_headers_lock_);
+  base::AutoLock lock(http_custom_headers_lock_);
   http_custom_headers_.clear();
 }
 
 const HTTPCustomHeadersEflMap
 BrowserContextEfl::ResourceContextEfl::GetHTTPCustomHeadersEflMap() const {
-  base::AutoLock locker(http_custom_headers_lock_);
+  base::AutoLock lock(http_custom_headers_lock_);
   return http_custom_headers_;
 }
 
@@ -170,24 +179,49 @@ void BrowserContextEfl::ResourceContextEfl::SetInterceptRequestCallback(
     Ewk_Context_Intercept_Request_Callback callback,
     void* user_data) {
   DCHECK_CURRENTLY_ON(BrowserThread::UI);
-  base::AutoLock locker(intercept_request_callback_lock_);
+  base::AutoLock lock(intercept_request_callback_lock_);
   intercept_request_callback_ = {ewk_context, callback, user_data};
 }
 
 bool BrowserContextEfl::ResourceContextEfl::HasInterceptRequestCallback()
     const {
-  base::AutoLock locker(intercept_request_callback_lock_);
+  base::AutoLock lock(intercept_request_callback_lock_);
   return !!intercept_request_callback_.callback;
 }
 
 void BrowserContextEfl::ResourceContextEfl::RunInterceptRequestCallback(
     _Ewk_Intercept_Request* intercept_request) const {
   DCHECK_CURRENTLY_ON(BrowserThread::IO);
-  base::AutoLock locker(intercept_request_callback_lock_);
+  base::AutoLock lock(intercept_request_callback_lock_);
   if (intercept_request_callback_.callback)
     intercept_request_callback_.Run(intercept_request);
 }
 
+#if BUILDFLAG(IS_TIZEN_TV)
+void BrowserContextEfl::ResourceContextEfl::SetInterceptRequestCancelCallback(
+    Ewk_Context* ewk_context,
+    Ewk_Context_Intercept_Request_Callback callback,
+    void* user_data) {
+  DCHECK_CURRENTLY_ON(BrowserThread::UI);
+  base::AutoLock lock(intercept_request_cancel_callback_lock_);
+  intercept_request_cancel_callback_ = {ewk_context, callback, user_data};
+}
+
+bool BrowserContextEfl::ResourceContextEfl::HasInterceptRequestCancelCallback()
+    const {
+  base::AutoLock lock(intercept_request_cancel_callback_lock_);
+  return !!intercept_request_cancel_callback_.callback;
+}
+
+void BrowserContextEfl::ResourceContextEfl::RunInterceptRequestCancelCallback(
+    _Ewk_Intercept_Request* intercept_request) const {
+  DCHECK_CURRENTLY_ON(BrowserThread::IO);
+  base::AutoLock lock(intercept_request_cancel_callback_lock_);
+  if (intercept_request && intercept_request_cancel_callback_.callback)
+    intercept_request_cancel_callback_.Run(intercept_request);
+}
+#endif
+
 scoped_refptr<CookieManager>
 BrowserContextEfl::ResourceContextEfl::GetCookieManager() const {
   return cookie_manager_;
index 43e4254..9f14f97 100644 (file)
@@ -60,6 +60,16 @@ class BrowserContextEfl : public BrowserContext,
     void RunInterceptRequestCallback(
         _Ewk_Intercept_Request* intercept_request) const;
 
+#if BUILDFLAG(IS_TIZEN_TV)
+    void SetInterceptRequestCancelCallback(
+        Ewk_Context* ewk_context,
+        Ewk_Context_Intercept_Request_Callback callback,
+        void* user_data);
+    bool HasInterceptRequestCancelCallback() const;
+    void RunInterceptRequestCancelCallback(
+        _Ewk_Intercept_Request* intercept_request) const;
+#endif
+
    private:
     struct InterceptRequestCallbackWithData {
       Ewk_Context* context;
@@ -77,6 +87,10 @@ class BrowserContextEfl : public BrowserContext,
 
     InterceptRequestCallbackWithData intercept_request_callback_;
     mutable base::Lock intercept_request_callback_lock_;
+#if BUILDFLAG(IS_TIZEN_TV)
+    InterceptRequestCallbackWithData intercept_request_cancel_callback_;
+    mutable base::Lock intercept_request_cancel_callback_lock_;
+#endif
   };
 
   BrowserContextEfl(EWebContext*, bool incognito = false);
index 8dd4543..1cae81a 100644 (file)
@@ -608,8 +608,14 @@ bool ContentBrowserClientEfl::WillCreateURLLoaderFactory(
       static_cast<content::BrowserContextEfl*>(browser_context)
           ->GetResourceContextEfl();
 
+#if BUILDFLAG(IS_TIZEN_TV)
+  if (!resource_context_efl ||
+      !resource_context_efl->HasInterceptRequestCallback() ||
+      !resource_context_efl->HasInterceptRequestCancelCallback()) {
+#else
   if (!resource_context_efl ||
       !resource_context_efl->HasInterceptRequestCallback()) {
+#endif
     return false;
   }
 
index dd84743..2fa967f 100644 (file)
@@ -1052,6 +1052,20 @@ void EWebContext::SetTimeZoneOffset(double time_zone_offset,
                                     double daylight_saving_time) {
   browser_context_->SetTimeZoneOffset(time_zone_offset, daylight_saving_time);
 }
+
+void EWebContext::SetInterceptRequestCancelCallback(
+    Ewk_Context* ewk_context,
+    Ewk_Context_Intercept_Request_Callback callback,
+    void* user_data) {
+  BrowserContextEfl::ResourceContextEfl* resource_context_efl =
+      browser_context_->GetResourceContextEfl();
+  if (!resource_context_efl) {
+    LOG(ERROR) << "Unable to get ResourceContextEfl.";
+    return;
+  }
+  resource_context_efl->SetInterceptRequestCancelCallback(ewk_context, callback,
+                                                          user_data);
+}
 #endif
 
 void EWebContext::SetMaxRefreshRate(int max_refresh_rate) {
index 448ed84..eba47ac 100644 (file)
@@ -209,6 +209,10 @@ class EWebContext {
   void RegisterURLSchemesAsCORSEnabled(const Eina_List* schemes_list);
   void SetTimeOffset(double time_offset);
   void SetTimeZoneOffset(double time_zone_offset, double daylight_saving_time);
+  void SetInterceptRequestCancelCallback(
+      Ewk_Context* ewk_context,
+      Ewk_Context_Intercept_Request_Callback callback,
+      void* user_data);
 #endif
 
   void EnableAppControl(bool enabled);
index 368f769..85951b9 100644 (file)
@@ -357,6 +357,12 @@ void Ewk_Context::SetTimeZoneOffset(double time_zone_offset,
                                     double daylight_saving_time) {
   impl->SetTimeZoneOffset(time_zone_offset, daylight_saving_time);
 }
+
+void Ewk_Context::SetContextInterceptRequestCancelCallback(
+    Ewk_Context_Intercept_Request_Callback callback,
+    void* user_data) {
+  impl->SetInterceptRequestCancelCallback(this, callback, user_data);
+}
 #endif
 
 void Ewk_Context::SetMaxRefreshRate(int max_refresh_rate) {
index d1da005..33ac56a 100644 (file)
@@ -167,6 +167,9 @@ struct Ewk_Context : public base::RefCounted<Ewk_Context> {
   void SetTimeOffset(double time_offset);
   // Sets time zone offset for HBBTV
   void SetTimeZoneOffset(double time_zone_offset, double daylight_saving_time);
+  void SetContextInterceptRequestCancelCallback(
+      Ewk_Context_Intercept_Request_Callback callback,
+      void* user_data);
 #endif
 
   void EnableAppControl(bool enabled);
index ba31ed8..07ca4bb 100644 (file)
@@ -160,8 +160,8 @@ void _Ewk_Intercept_Request::response_decided() {
 
 void _Ewk_Intercept_Request::post_response_decided() {
   content::GetIOThreadTaskRunner({})->PostTask(
-      FROM_HERE, base::BindRepeating(&_Ewk_Intercept_Request::response_decided,
-                                     base::Unretained(this)));
+      FROM_HERE,
+      base::BindRepeating(&_Ewk_Intercept_Request::response_decided, this));
 }
 
 void _Ewk_Intercept_Request::put_chunk(const char* data, size_t length) {
@@ -173,8 +173,8 @@ void _Ewk_Intercept_Request::put_chunk(const char* data, size_t length) {
 
 void _Ewk_Intercept_Request::post_put_chunk(const char* data, size_t length) {
   content::GetIOThreadTaskRunner({})->PostTask(
-      FROM_HERE, base::BindRepeating(&_Ewk_Intercept_Request::put_chunk,
-                                     base::Unretained(this), data, length));
+      FROM_HERE, base::BindRepeating(&_Ewk_Intercept_Request::put_chunk, this,
+                                     data, length));
 }
 
 void _Ewk_Intercept_Request::chunked_read_done() {
@@ -184,8 +184,8 @@ void _Ewk_Intercept_Request::chunked_read_done() {
 
 void _Ewk_Intercept_Request::post_chunked_read_done() {
   content::GetIOThreadTaskRunner({})->PostTask(
-      FROM_HERE, base::BindRepeating(&_Ewk_Intercept_Request::chunked_read_done,
-                                     base::Unretained(this)));
+      FROM_HERE,
+      base::BindRepeating(&_Ewk_Intercept_Request::chunked_read_done, this));
 }
 
 const char* _Ewk_Intercept_Request::request_body_get() const {
index a3a7fc8..e5e8c00 100644 (file)
@@ -12,7 +12,7 @@
 
 #include <Eina.h>
 #include <Evas.h>
-
+#include "base/memory/ref_counted.h"
 #include "base/synchronization/atomic_flag.h"
 
 namespace net {
@@ -21,7 +21,8 @@ class UploadDataStream;
 
 struct InterceptRequestParams;
 
-struct _Ewk_Intercept_Request {
+struct _Ewk_Intercept_Request
+    : public base::RefCountedThreadSafe<_Ewk_Intercept_Request> {
  public:
   class Delegate {
    public:
index 5222929..26b0ec8 100644 (file)
@@ -967,7 +967,12 @@ void ewk_context_service_worker_register(Ewk_Context* context, const char* scope
 
 void ewk_context_intercept_request_cancel_callback_set(Ewk_Context* ewk_context, Ewk_Context_Intercept_Request_Cancel_Callback callback, void* user_data)
 {
+#if BUILDFLAG(IS_TIZEN_TV)
+  EINA_SAFETY_ON_NULL_RETURN(ewk_context);
+  ewk_context->SetContextInterceptRequestCancelCallback(callback, user_data);
+#else
   LOG_EWK_API_MOCKUP();
+#endif
 }
 
 void ewk_context_set_xwalk_extension_profile(Ewk_Context* ewk_context,