[M108 Migration] Enable proper functionality for ewk_cookie_manager_* APIs. 54/286654/4
authorAyush Kumar <ayush.k123@samsung.com>
Wed, 11 Jan 2023 07:58:52 +0000 (13:28 +0530)
committerBot Blink <blinkbot@samsung.com>
Wed, 11 Jan 2023 11:49:12 +0000 (11:49 +0000)
This patch enables the proper functionality of ewk_cookies_manager_* APIs.

Reference: https://review.tizen.org/gerrit/273547/

Change-Id: I8a9f23a0cf017d57e80d57e052eba46973a099ae
Signed-off-by: Ayush Kumar<ayush.k123@samsung.com>
tizen_src/ewk/efl_integration/cookie_manager.cc
tizen_src/ewk/efl_integration/cookie_manager.h
tizen_src/ewk/efl_integration/eweb_context.cc
tizen_src/ewk/efl_integration/private/ewk_cookie_manager_private.h
tizen_src/ewk/efl_integration/public/ewk_cookie_manager.cc

index 9e5ae2a..bf62e59 100644 (file)
@@ -11,6 +11,7 @@
 #include "base/stl_util.h"
 #include "base/synchronization/waitable_event.h"
 #include "base/task/thread_pool.h"
+#include "browser_context_efl.h"
 #include "content/browser/storage_partition_impl.h"
 #include "content/public/browser/browser_task_traits.h"
 #include "content/public/browser/browser_thread.h"
 #include "net/cookies/canonical_cookie.h"
 #include "net/cookies/cookie_monster.h"
 #include "net/cookies/cookie_options.h"
+#include "net/cookies/cookie_store_test_callbacks.h"
 #include "net/cookies/cookie_util.h"
 #include "net/cookies/parsed_cookie.h"
 #include "net/cookies/static_cookie_policy.h"
 #include "net/url_request/url_request_context.h"
 #include "net/url_request/url_request_context_getter.h"
+#include "services/network/public/mojom/cookie_manager.mojom.h"
 #include "url/gurl.h"
 
-#include "net/cookies/cookie_store_test_callbacks.h"
-
-#include <Eina.h>
-
 using content::BrowserThread;
 using net::CookieList;
 using net::CookieMonster;
@@ -73,10 +72,10 @@ class CookieManager::EwkGetHostCallback {
     void* user_data_;
 };
 
-CookieManager::CookieManager()
-    : cookie_policy_(EWK_COOKIE_ACCEPT_POLICY_ALWAYS),
-      weak_ptr_factory_(this) {
-}
+CookieManager::CookieManager(EWebContext* eweb_context)
+    : eweb_context_(eweb_context),
+      cookie_policy_(EWK_COOKIE_ACCEPT_POLICY_ALWAYS),
+      weak_ptr_factory_(this) {}
 
 CookieManager::~CookieManager() {
   DeleteSessionCookiesOnIOThread();
@@ -85,31 +84,36 @@ CookieManager::~CookieManager() {
 void CookieManager::DeleteSessionCookiesOnIOThread() {
   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
 
-  std::unique_ptr<net::CookieStore> cookie_store = GetCookieStore();
+  net::CookieStore* cookie_store = GetCookieStore();
   if (cookie_store) {
     cookie_store->DeleteSessionCookiesAsync(
         base::OnceCallback<void(uint32_t)>());
   }
 }
 
-static void DeleteCookiesOnIOThread(std::unique_ptr<net::CookieStore> cookie_store,
-                                    const std::string& url,
-                                    const std::string& cookie_name) {
+void CookieManager::DeleteCookiesOnIOThread(const std::string& url,
+                                            const std::string& cookie_name) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
 
+  net::CookieStore* cookie_store = GetCookieStore();
+  if (!cookie_store)
+    return;
+
   if (url.empty()) { // Delete all cookies.
     cookie_store->DeleteAllAsync(net::CookieMonster::DeleteCallback());
+    return;
   }
+
   GURL gurl(url);
   if (!gurl.is_valid())
     return;
+
   if (cookie_name.empty()) {
     // Delete all matching host cookies.
-#if !defined(EWK_BRINGUP)  // FIXME: m69 bringup
-    cookie_store->DeleteAllCreatedBetweenWithPredicateAsync(base::Time(), base::Time::Max(),
-        content::StoragePartitionImpl::CreatePredicateForHostCookies(gurl),
-        net::CookieMonster::DeleteCallback());
-#endif
+    net::CookieDeletionInfo delete_info;
+    delete_info.host = gurl.host();
+    cookie_store->DeleteAllMatchingInfoAsync(
+        std::move(delete_info), net::CookieStore::DeleteCallback());
   } else {
     // Delete all matching host and domain cookies.
 #if !defined(EWK_BRINGUP)  // FIXME: m76 bringup
@@ -119,16 +123,12 @@ static void DeleteCookiesOnIOThread(std::unique_ptr<net::CookieStore> cookie_sto
 }
 
 void CookieManager::DeleteCookiesAsync(const std::string& url,
-                                       const std::string& cookie_name)
-{
+                                       const std::string& cookie_name) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  std::unique_ptr<net::CookieStore> cookie_store = GetCookieStore();
-  if (cookie_store.get()) {
-    base::ThreadPool::PostTask(
-        FROM_HERE, {BrowserThread::IO},
-        base::BindOnce(DeleteCookiesOnIOThread, std::move(cookie_store), url,
-                       cookie_name));
-  }
+  base::ThreadPool::PostTask(
+      FROM_HERE, {BrowserThread::IO},
+      base::BindOnce(&CookieManager::DeleteCookiesOnIOThread,
+                     base::Unretained(this), url, cookie_name));
 }
 
 static void SetStoragePathOnIOThread(
@@ -170,20 +170,15 @@ void CookieManager::GetAcceptPolicyAsync(Ewk_Cookie_Manager_Policy_Async_Get_Cb
 void CookieManager::GetHostNamesWithCookiesAsync(AsyncHostnamesGetCb callback, void *data) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
   host_callback_queue_.push(new EwkGetHostCallback(callback, data));
-  std::unique_ptr<net::CookieStore> cookie_store = GetCookieStore();
-  if (cookie_store.get()) {
-    base::ThreadPool::PostTask(
-        FROM_HERE, {BrowserThread::IO},
-        base::BindOnce(&CookieManager::FetchCookiesOnIOThread, GetSharedThis(),
-                       std::move(cookie_store)));
-  }
+  base::ThreadPool::PostTask(
+      FROM_HERE, {BrowserThread::IO},
+      base::BindOnce(&CookieManager::FetchCookiesOnIOThread, GetSharedThis()));
 }
 
-void CookieManager::FetchCookiesOnIOThread(
-    std::unique_ptr<net::CookieStore> cookie_store) {
+void CookieManager::FetchCookiesOnIOThread() {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
-
-  if (cookie_store.get()) {
+  net::CookieStore* cookie_store = GetCookieStore();
+  if (cookie_store) {
     cookie_store->GetAllCookiesAsync(base::BindOnce(
         &CookieManager::OnFetchComplete, base::Unretained(this)));
   } else {
@@ -274,45 +269,51 @@ bool CookieManager::AllowSetCookie(const GURL& url,
 }
 
 bool CookieManager::IsFileSchemeCookiesAllowed() {
-  auto cookie_monster =
-      static_cast<net::CookieMonster*>(GetCookieStore().get());
-  if (!cookie_monster)
-    return false;
-#if !defined(EWK_BRINGUP)  // FIXME: m94 bringup
   AutoLock lock(file_scheme_lock_);
-  return cookie_monster->IsCookieableScheme(url::kFileScheme);
-#else
-  return false;
-#endif
+  return is_file_scheme_cookies_allowed_;
 }
 
-void CookieManager::SetAcceptFileSchemeCookies(bool accept) {
-  auto cookie_monster =
-      static_cast<net::CookieMonster*>(GetCookieStore().get());
-  if (!cookie_monster)
+void CookieManager::AllowFileSchemeCookies(bool allow) {
+  if (allow == is_file_scheme_cookies_allowed_)
     return;
 
-  std::vector<std::string> schemes(
-      CookieMonster::kDefaultCookieableSchemes,
-      CookieMonster::kDefaultCookieableSchemes +
-          CookieMonster::kDefaultCookieableSchemesCount);
-  if (accept)
-    schemes.push_back(url::kFileScheme);
+  auto* cookie_manager = eweb_context_->browser_context()
+                             ->GetDefaultStoragePartition()
+                             ->GetCookieManagerForBrowserProcess();
 
-  AutoLock lock(file_scheme_lock_);
-  cookie_monster->SetCookieableSchemes(schemes, base::DoNothing());
+  base::ThreadPool::PostTask(
+      FROM_HERE, {BrowserThread::IO},
+      base::BindOnce(&CookieManager::AllowFileSchemeCookiesOnIOThread,
+                     weak_ptr_factory_.GetWeakPtr(), cookie_manager, allow));
 }
 
-static void SignalGetCookieValueCompleted(base::WaitableEvent* completion,
-                                          std::string* result,
-                                          const std::string& value) {
+void CookieManager::AllowFileSchemeCookiesOnIOThread(
+    network::mojom::CookieManager* cookie_manager,
+    bool allow) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+  cookie_manager->AllowFileSchemeCookies(
+      allow, base::BindOnce(&CookieManager::OnAllowFileSchemeCookies,
+                            weak_ptr_factory_.GetWeakPtr(), allow));
+}
+
+void CookieManager::OnAllowFileSchemeCookies(bool allow, bool success) {
+  if (success)
+    is_file_scheme_cookies_allowed_ = allow;
+}
+
+static void SignalGetCookieValueCompleted(
+    base::WaitableEvent* completion,
+    std::string* result,
+    const net::CookieAccessResultList& cookie_list,
+    const net::CookieAccessResultList& excluded_cookies) {
   DCHECK(completion);
 
-  *result = value;
+  std::string cookie_line = net::CanonicalCookie::BuildCookieLine(cookie_list);
+  *result = cookie_line;
   completion->Signal();
 }
 
-static void GetCookieValueOnIOThread(std::unique_ptr<net::CookieStore> cookie_store,
+static void GetCookieValueOnIOThread(net::CookieStore* cookie_store,
                                      const GURL& host,
                                      std::string* result,
                                      base::WaitableEvent* completion) {
@@ -320,12 +321,10 @@ static void GetCookieValueOnIOThread(std::unique_ptr<net::CookieStore> cookie_st
   net::CookieOptions options;
   options.set_include_httponly();
 
-  if (cookie_store.get()) {
-#if !defined(EWK_BRINGUP)  // FIXME: m67 bringup
-    cookie_store->GetCookiesWithOptionsAsync(
-        host, options,
-        base::BindOnce(SignalGetCookieValueCompleted, completion, i result));
-#endif
+  if (cookie_store) {
+    cookie_store->GetCookieListWithOptionsAsync(
+        host, options, net::CookiePartitionKeyCollection::Todo(),
+        base::BindOnce(SignalGetCookieValueCompleted, completion, result));
   } else {
     DCHECK(completion);
     completion->Signal();
@@ -334,8 +333,8 @@ static void GetCookieValueOnIOThread(std::unique_ptr<net::CookieStore> cookie_st
 
 std::string CookieManager::GetCookiesForURL(const std::string& url) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
-  std::unique_ptr<net::CookieStore> cookie_store = GetCookieStore();
-  if (!cookie_store.get())
+  net::CookieStore* cookie_store = GetCookieStore();
+  if (!cookie_store)
     return std::string();
 
   std::string cookie_value;
@@ -343,8 +342,8 @@ std::string CookieManager::GetCookiesForURL(const std::string& url) {
                                  base::WaitableEvent::InitialState::NOT_SIGNALED);
   base::ThreadPool::PostTask(
       FROM_HERE, {BrowserThread::IO},
-      base::BindOnce(GetCookieValueOnIOThread, std::move(cookie_store),
-                     GURL(url), &cookie_value, &completion));
+      base::BindOnce(GetCookieValueOnIOThread, cookie_store, GURL(url),
+                     &cookie_value, &completion));
   // allow wait temporarily.
   completion.Wait();
   return cookie_value;
@@ -358,12 +357,8 @@ scoped_refptr<content::URLRequestContextGetterEfl> CookieManager::GetContextGett
   return scoped_refptr<content::URLRequestContextGetterEfl>(request_context_getter_.get());
 }
 
-std::unique_ptr<net::CookieStore> CookieManager::GetCookieStore() const {
-  scoped_refptr<content::URLRequestContextGetterEfl> request_context_getter = GetContextGetter();
-  if (request_context_getter.get()) {
-    return std::unique_ptr<net::CookieStore>(
-        request_context_getter->GetURLRequestContext()->cookie_store());
-  } else {
-    return std::unique_ptr<net::CookieStore>();
-  }
+net::CookieStore* CookieManager::GetCookieStore() const {
+  return request_context_getter_
+             ? request_context_getter_->GetURLRequestContext()->cookie_store()
+             : nullptr;
 }
index 94bd2da..cf67881 100644 (file)
@@ -14,7 +14,6 @@
 #include "net/cookies/cookie_monster.h"
 #include "net/cookies/cookie_options.h"
 #include "net/cookies/cookie_util.h"
-#include "public/ewk_cookie_manager.h"
 #include "url/gurl.h"
 #include "url_request_context_getter_efl.h"
 
@@ -22,13 +21,21 @@ namespace content {
 class ResourceContext;
 }
 
+namespace network {
+namespace mojom {
+class CookieManager;
+}
+}  // namespace network
+
+class EWebContext;
+
 struct _Ewk_Error;
 
 class CookieManager : public base::RefCountedThreadSafe<CookieManager> {
  public:
   typedef void (*AsyncHostnamesGetCb)(Eina_List*, _Ewk_Error*, void *);
 
-  CookieManager();
+  explicit CookieManager(EWebContext* eweb_context);
   virtual ~CookieManager();
 
   CookieManager(const CookieManager&) = delete;
@@ -94,32 +101,42 @@ class CookieManager : public base::RefCountedThreadSafe<CookieManager> {
 
   // file scheme
   bool IsFileSchemeCookiesAllowed();
-  void SetAcceptFileSchemeCookies(bool accept);
+  void AllowFileSchemeCookies(bool allow);
 
  private:
   struct EwkGetHostCallback;
 
   void DeleteSessionCookiesOnIOThread();
+  void DeleteCookiesOnIOThread(const std::string& url,
+                               const std::string& cookie_name);
 
   bool AllowCookies(const GURL& url,
                     const net::SiteForCookies& site_for_cookies,
                     bool setting_cookie);
   // Fetch the cookies. This must be called in the IO thread.
-  void FetchCookiesOnIOThread(std::unique_ptr<net::CookieStore> cookie_store);
+  void FetchCookiesOnIOThread();
   void OnFetchComplete(const net::CookieList& cookies);
 
+  void AllowFileSchemeCookiesOnIOThread(
+      network::mojom::CookieManager* cookie_manager,
+      bool allow);
+  void OnAllowFileSchemeCookies(bool allow, bool success);
+
   scoped_refptr<CookieManager> GetSharedThis();
   scoped_refptr<content::URLRequestContextGetterEfl> GetContextGetter() const;
-  std::unique_ptr<net::CookieStore> GetCookieStore() const;
+  net::CookieStore* GetCookieStore() const;
+
+  EWebContext* eweb_context_;
   scoped_refptr<content::URLRequestContextGetterEfl> request_context_getter_;
   //cookie policy information
   base::Lock lock_;
   Ewk_Cookie_Accept_Policy cookie_policy_;
   // This only mutates on the UI thread.
   std::queue< EwkGetHostCallback* > host_callback_queue_;
-  base::WeakPtrFactory<CookieManager> weak_ptr_factory_;
   // file scheme
   base::Lock file_scheme_lock_;
+  bool is_file_scheme_cookies_allowed_ = false;
+  base::WeakPtrFactory<CookieManager> weak_ptr_factory_;
 };
 
 #endif //cookie_manager_h
index 8e9fbda..59b02c2 100644 (file)
@@ -531,7 +531,7 @@ EwkDidStartDownloadCallback* EWebContext::DidStartDownloadCallback() {
 
 Ewk_Cookie_Manager* EWebContext::ewkCookieManager() {
   if (!ewk_cookie_manager_)
-    ewk_cookie_manager_.reset(Ewk_Cookie_Manager::create());
+    ewk_cookie_manager_.reset(Ewk_Cookie_Manager::create(this));
   return ewk_cookie_manager_.get();
 }
 
index 0a4d5a2..a6e04d9 100644 (file)
@@ -9,8 +9,8 @@
 
 class Ewk_Cookie_Manager {
  public:
-  static Ewk_Cookie_Manager* create() {
-    return new Ewk_Cookie_Manager;
+  static Ewk_Cookie_Manager* create(EWebContext* eweb_context) {
+    return new Ewk_Cookie_Manager(eweb_context);
   }
 
   ~Ewk_Cookie_Manager() { }
@@ -20,9 +20,8 @@ class Ewk_Cookie_Manager {
   }
 
  private:
-  Ewk_Cookie_Manager()
-    : cookie_manager_(new CookieManager) {
-  }
+  Ewk_Cookie_Manager(EWebContext* eweb_context)
+      : cookie_manager_(new CookieManager(eweb_context)) {}
 
   scoped_refptr<CookieManager> cookie_manager_;
 };
index 287843f..0b8d8fa 100644 (file)
@@ -103,7 +103,7 @@ Eina_Bool ewk_cookie_manager_file_scheme_cookies_allow_get(Ewk_Cookie_Manager* m
 void ewk_cookie_manager_file_scheme_cookies_allow_set(Ewk_Cookie_Manager* manager, Eina_Bool allow)
 {
   EWK_COOKIE_MANAGER_GET_OR_RETURN(manager, cookie_manager);
-  cookie_manager->SetAcceptFileSchemeCookies(allow);
+  cookie_manager->AllowFileSchemeCookies(allow);
 }