From 85c34c4dcf43b087dc284c1bde702470628b3ee3 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Mon, 25 Mar 2019 14:14:23 -0400 Subject: [PATCH] Add CookieManagerImpl for NetworkService (see issue #2622). To test: Run `ceftests --gtest_filter=CookieTest.*:-CookieTest.GetCookieManager* --enable-network-service` There should be no functional change when running without the NetworkService enabled. Known issues: - CefCookieManager::SetSupportedSchemes is not yet implemented. --- BUILD.gn | 6 +- libcef/browser/chrome_browser_process_stub.cc | 5 + .../cookie_manager_old_impl.cc} | 125 ++++---- .../cookie_manager_old_impl.h} | 18 +- libcef/browser/net/network_delegate.cc | 4 +- libcef/browser/net/resource_request_job.cc | 8 +- .../browser/net/url_request_context_getter.cc | 6 +- .../net_service/cookie_manager_impl.cc | 285 ++++++++++++++++++ .../browser/net_service/cookie_manager_impl.h | 46 +++ libcef/browser/request_context_impl.cc | 16 +- 10 files changed, 419 insertions(+), 100 deletions(-) rename libcef/browser/{cookie_manager_impl.cc => net/cookie_manager_old_impl.cc} (79%) rename libcef/browser/{cookie_manager_impl.h => net/cookie_manager_old_impl.h} (91%) create mode 100644 libcef/browser/net_service/cookie_manager_impl.cc create mode 100644 libcef/browser/net_service/cookie_manager_impl.h diff --git a/BUILD.gn b/BUILD.gn index 444908a1b..2ec98d61b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -292,8 +292,6 @@ static_library("libcef_static") { "libcef/browser/context.h", "libcef/browser/context_menu_params_impl.cc", "libcef/browser/context_menu_params_impl.h", - "libcef/browser/cookie_manager_impl.cc", - "libcef/browser/cookie_manager_impl.h", "libcef/browser/devtools/devtools_file_manager.cc", "libcef/browser/devtools/devtools_file_manager.h", "libcef/browser/devtools/devtools_frontend.cc", @@ -373,6 +371,8 @@ static_library("libcef_static") { "libcef/browser/navigation_entry_impl.h", "libcef/browser/net/chrome_scheme_handler.cc", "libcef/browser/net/chrome_scheme_handler.h", + "libcef/browser/net/cookie_manager_old_impl.cc", + "libcef/browser/net/cookie_manager_old_impl.h", "libcef/browser/net/crlset_file_util_impl.cc", "libcef/browser/net/devtools_scheme_handler.cc", "libcef/browser/net/devtools_scheme_handler.h", @@ -398,6 +398,8 @@ static_library("libcef_static") { "libcef/browser/net/url_request_manager.h", "libcef/browser/net/url_request_user_data.cc", "libcef/browser/net/url_request_user_data.h", + "libcef/browser/net_service/cookie_manager_impl.cc", + "libcef/browser/net_service/cookie_manager_impl.h", "libcef/browser/origin_whitelist_impl.cc", "libcef/browser/origin_whitelist_impl.h", "libcef/browser/osr/browser_platform_delegate_osr.cc", diff --git a/libcef/browser/chrome_browser_process_stub.cc b/libcef/browser/chrome_browser_process_stub.cc index e292ba798..0f89893e3 100644 --- a/libcef/browser/chrome_browser_process_stub.cc +++ b/libcef/browser/chrome_browser_process_stub.cc @@ -11,6 +11,7 @@ #include "libcef/browser/prefs/browser_prefs.h" #include "libcef/browser/thread_util.h" #include "libcef/common/cef_switches.h" +#include "libcef/common/net_service/util.h" #include "base/command_line.h" #include "chrome/browser/net/chrome_net_log_helper.h" @@ -80,6 +81,10 @@ void ChromeBrowserProcessStub::Shutdown() { profile_manager_.reset(); event_router_forwarder_ = nullptr; + if (net_service::IsEnabled()) { + SystemNetworkContextManager::DeleteInstance(); + } + local_state_.reset(); browser_policy_connector_.reset(); diff --git a/libcef/browser/cookie_manager_impl.cc b/libcef/browser/net/cookie_manager_old_impl.cc similarity index 79% rename from libcef/browser/cookie_manager_impl.cc rename to libcef/browser/net/cookie_manager_old_impl.cc index 78f8eb7cf..e006fb753 100644 --- a/libcef/browser/cookie_manager_impl.cc +++ b/libcef/browser/net/cookie_manager_old_impl.cc @@ -2,7 +2,7 @@ // reserved. Use of this source code is governed by a BSD-style license that can // be found in the LICENSE file. -#include "libcef/browser/cookie_manager_impl.h" +#include "libcef/browser/net/cookie_manager_old_impl.h" #include #include @@ -11,7 +11,6 @@ #include "libcef/browser/content_browser_client.h" #include "libcef/browser/context.h" #include "libcef/browser/net/network_delegate.h" -#include "libcef/common/net_service/util.h" #include "libcef/common/task_runner_impl.h" #include "libcef/common/time_util.h" @@ -36,7 +35,7 @@ namespace { class VisitCookiesCallback : public base::RefCounted { public: explicit VisitCookiesCallback( - const CefCookieManagerImpl::CookieStoreGetter& cookie_store_getter, + const CefCookieManagerOldImpl::CookieStoreGetter& cookie_store_getter, CefRefPtr visitor) : cookie_store_getter_(cookie_store_getter), visitor_(visitor) {} @@ -50,7 +49,7 @@ class VisitCookiesCallback : public base::RefCounted { for (; it != list.end(); ++it, ++count) { CefCookie cookie; const net::CanonicalCookie& cc = *(it); - CefCookieManagerImpl::GetCefCookie(cc, cookie); + CefCookieManagerOldImpl::GetCefCookie(cc, cookie); bool deleteCookie = false; bool keepLooping = visitor_->Visit(cookie, count, total, deleteCookie); @@ -71,7 +70,7 @@ class VisitCookiesCallback : public base::RefCounted { ~VisitCookiesCallback() {} - CefCookieManagerImpl::CookieStoreGetter cookie_store_getter_; + CefCookieManagerOldImpl::CookieStoreGetter cookie_store_getter_; CefRefPtr visitor_; }; @@ -119,46 +118,42 @@ void SetCookieCallbackImpl(CefRefPtr callback, } // namespace -CefCookieManagerImpl::CefCookieManagerImpl() : weak_ptr_factory_(this) {} +CefCookieManagerOldImpl::CefCookieManagerOldImpl() {} -CefCookieManagerImpl::~CefCookieManagerImpl() { +CefCookieManagerOldImpl::~CefCookieManagerOldImpl() { CEF_REQUIRE_IOT(); } -void CefCookieManagerImpl::Initialize( +void CefCookieManagerOldImpl::Initialize( CefRefPtr request_context, const CefString& path, bool persist_session_cookies, CefRefPtr callback) { DCHECK(request_context.get()); request_context_ = request_context; - if (!net_service::IsEnabled()) { - request_context_->GetRequestContextImpl( - base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefCookieManagerImpl::InitWithContext, this, callback)); - } else { - RunAsyncCompletionOnIOThread(callback); - } + request_context_->GetRequestContextImpl( + base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), + base::Bind(&CefCookieManagerOldImpl::InitWithContext, this, callback)); } -void CefCookieManagerImpl::GetCookieStore( +void CefCookieManagerOldImpl::GetCookieStore( scoped_refptr task_runner, const CookieStoreCallback& callback) { if (!task_runner.get()) task_runner = CefTaskRunnerImpl::GetCurrentTaskRunner(); if (!CEF_CURRENTLY_ON_IOT()) { - CEF_POST_TASK(CEF_IOT, base::Bind(&CefCookieManagerImpl::GetCookieStore, + CEF_POST_TASK(CEF_IOT, base::Bind(&CefCookieManagerOldImpl::GetCookieStore, this, task_runner, callback)); return; } RunMethodWithContext( - base::Bind(&CefCookieManagerImpl::GetCookieStoreWithContext, this, + base::Bind(&CefCookieManagerOldImpl::GetCookieStoreWithContext, this, task_runner, callback)); } -net::CookieStore* CefCookieManagerImpl::GetExistingCookieStore() { +net::CookieStore* CefCookieManagerOldImpl::GetExistingCookieStore() { CEF_REQUIRE_IOT(); if (request_context_impl_.get()) { net::CookieStore* cookie_store = @@ -171,13 +166,13 @@ net::CookieStore* CefCookieManagerImpl::GetExistingCookieStore() { return nullptr; } -void CefCookieManagerImpl::SetSupportedSchemes( +void CefCookieManagerOldImpl::SetSupportedSchemes( const std::vector& schemes, CefRefPtr callback) { if (!CEF_CURRENTLY_ON_IOT()) { - CEF_POST_TASK(CEF_IOT, - base::Bind(&CefCookieManagerImpl::SetSupportedSchemes, this, - schemes, callback)); + CEF_POST_TASK( + CEF_IOT, base::Bind(&CefCookieManagerOldImpl::SetSupportedSchemes, this, + schemes, callback)); return; } @@ -189,41 +184,42 @@ void CefCookieManagerImpl::SetSupportedSchemes( SetSupportedSchemesInternal(scheme_set, callback); } -bool CefCookieManagerImpl::VisitAllCookies( +bool CefCookieManagerOldImpl::VisitAllCookies( CefRefPtr visitor) { GetCookieStore( base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefCookieManagerImpl::VisitAllCookiesInternal, this, + base::Bind(&CefCookieManagerOldImpl::VisitAllCookiesInternal, this, visitor)); return true; } -bool CefCookieManagerImpl::VisitUrlCookies( +bool CefCookieManagerOldImpl::VisitUrlCookies( const CefString& url, bool includeHttpOnly, CefRefPtr visitor) { GetCookieStore( base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefCookieManagerImpl::VisitUrlCookiesInternal, this, url, + base::Bind(&CefCookieManagerOldImpl::VisitUrlCookiesInternal, this, url, includeHttpOnly, visitor)); return true; } -bool CefCookieManagerImpl::SetCookie(const CefString& url, - const CefCookie& cookie, - CefRefPtr callback) { +bool CefCookieManagerOldImpl::SetCookie( + const CefString& url, + const CefCookie& cookie, + CefRefPtr callback) { GURL gurl = GURL(url.ToString()); if (!gurl.is_valid()) return false; GetCookieStore( base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefCookieManagerImpl::SetCookieInternal, this, gurl, cookie, - callback)); + base::Bind(&CefCookieManagerOldImpl::SetCookieInternal, this, gurl, + cookie, callback)); return true; } -bool CefCookieManagerImpl::DeleteCookies( +bool CefCookieManagerOldImpl::DeleteCookies( const CefString& url, const CefString& cookie_name, CefRefPtr callback) { @@ -234,22 +230,22 @@ bool CefCookieManagerImpl::DeleteCookies( GetCookieStore( base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefCookieManagerImpl::DeleteCookiesInternal, this, gurl, + base::Bind(&CefCookieManagerOldImpl::DeleteCookiesInternal, this, gurl, cookie_name, callback)); return true; } -bool CefCookieManagerImpl::FlushStore( +bool CefCookieManagerOldImpl::FlushStore( CefRefPtr callback) { GetCookieStore( base::CreateSingleThreadTaskRunnerWithTraits({BrowserThread::IO}), - base::Bind(&CefCookieManagerImpl::FlushStoreInternal, this, callback)); + base::Bind(&CefCookieManagerOldImpl::FlushStoreInternal, this, callback)); return true; } // static -bool CefCookieManagerImpl::GetCefCookie(const net::CanonicalCookie& cc, - CefCookie& cookie) { +bool CefCookieManagerOldImpl::GetCefCookie(const net::CanonicalCookie& cc, + CefCookie& cookie) { CefString(&cookie.name).FromString(cc.Name()); CefString(&cookie.value).FromString(cc.Value()); CefString(&cookie.domain).FromString(cc.Domain()); @@ -266,9 +262,9 @@ bool CefCookieManagerImpl::GetCefCookie(const net::CanonicalCookie& cc, } // static -bool CefCookieManagerImpl::GetCefCookie(const GURL& url, - const std::string& cookie_line, - CefCookie& cookie) { +bool CefCookieManagerOldImpl::GetCefCookie(const GURL& url, + const std::string& cookie_line, + CefCookie& cookie) { // Parse the cookie. net::ParsedCookie pc(cookie_line); if (!pc.IsValid()) @@ -303,7 +299,7 @@ bool CefCookieManagerImpl::GetCefCookie(const GURL& url, } // static -void CefCookieManagerImpl::SetCookieMonsterSchemes( +void CefCookieManagerOldImpl::SetCookieMonsterSchemes( net::CookieMonster* cookie_monster, const std::vector& schemes) { CEF_REQUIRE_IOT(); @@ -319,7 +315,7 @@ void CefCookieManagerImpl::SetCookieMonsterSchemes( cookie_monster->SetCookieableSchemes(all_schemes); } -void CefCookieManagerImpl::RunMethodWithContext( +void CefCookieManagerOldImpl::RunMethodWithContext( const CefRequestContextImpl::RequestContextCallback& method) { CEF_REQUIRE_IOT(); if (request_context_impl_.get()) { @@ -334,7 +330,7 @@ void CefCookieManagerImpl::RunMethodWithContext( } } -void CefCookieManagerImpl::InitWithContext( +void CefCookieManagerOldImpl::InitWithContext( CefRefPtr callback, scoped_refptr request_context) { CEF_REQUIRE_IOT(); @@ -351,7 +347,7 @@ void CefCookieManagerImpl::InitWithContext( RunAsyncCompletionOnIOThread(callback); } -void CefCookieManagerImpl::SetSupportedSchemesWithContext( +void CefCookieManagerOldImpl::SetSupportedSchemesWithContext( const std::vector& schemes, CefRefPtr callback, scoped_refptr request_context) { @@ -362,7 +358,7 @@ void CefCookieManagerImpl::SetSupportedSchemesWithContext( RunAsyncCompletionOnIOThread(callback); } -void CefCookieManagerImpl::GetCookieStoreWithContext( +void CefCookieManagerOldImpl::GetCookieStoreWithContext( scoped_refptr task_runner, const CookieStoreCallback& callback, scoped_refptr request_context) { @@ -381,22 +377,17 @@ void CefCookieManagerImpl::GetCookieStoreWithContext( } } -void CefCookieManagerImpl::SetSupportedSchemesInternal( +void CefCookieManagerOldImpl::SetSupportedSchemesInternal( const std::vector& schemes, CefRefPtr callback) { CEF_REQUIRE_IOT(); - if (!net_service::IsEnabled()) { - RunMethodWithContext( - base::Bind(&CefCookieManagerImpl::SetSupportedSchemesWithContext, this, - schemes, callback)); - } else { - NOTIMPLEMENTED(); - RunAsyncCompletionOnIOThread(callback); - } + RunMethodWithContext( + base::Bind(&CefCookieManagerOldImpl::SetSupportedSchemesWithContext, this, + schemes, callback)); } -void CefCookieManagerImpl::VisitAllCookiesInternal( +void CefCookieManagerOldImpl::VisitAllCookiesInternal( CefRefPtr visitor, const CookieStoreGetter& cookie_store_getter) { CEF_REQUIRE_IOT(); @@ -412,7 +403,7 @@ void CefCookieManagerImpl::VisitAllCookiesInternal( base::Bind(&VisitCookiesCallback::Run, callback.get())); } -void CefCookieManagerImpl::VisitUrlCookiesInternal( +void CefCookieManagerOldImpl::VisitUrlCookiesInternal( const CefString& url, bool includeHttpOnly, CefRefPtr visitor, @@ -435,7 +426,7 @@ void CefCookieManagerImpl::VisitUrlCookiesInternal( gurl, options, base::Bind(&VisitCookiesCallback::Run, callback.get())); } -void CefCookieManagerImpl::SetCookieInternal( +void CefCookieManagerOldImpl::SetCookieInternal( const GURL& url, const CefCookie& cookie, CefRefPtr callback, @@ -472,7 +463,7 @@ void CefCookieManagerImpl::SetCookieInternal( base::Bind(SetCookieCallbackImpl, callback)); } -void CefCookieManagerImpl::DeleteCookiesInternal( +void CefCookieManagerOldImpl::DeleteCookiesInternal( const GURL& url, const CefString& cookie_name, CefRefPtr callback, @@ -508,7 +499,7 @@ void CefCookieManagerImpl::DeleteCookiesInternal( } } -void CefCookieManagerImpl::FlushStoreInternal( +void CefCookieManagerOldImpl::FlushStoreInternal( CefRefPtr callback, const CookieStoreGetter& cookie_store_getter) { CEF_REQUIRE_IOT(); @@ -521,17 +512,3 @@ void CefCookieManagerImpl::FlushStoreInternal( cookie_store->FlushStore(base::Bind(RunAsyncCompletionOnIOThread, callback)); } - -// CefCookieManager methods ---------------------------------------------------- - -// static -CefRefPtr CefCookieManager::GetGlobalManager( - CefRefPtr callback) { - // Verify that the context is in a valid state. - if (!CONTEXT_STATE_VALID()) { - NOTREACHED() << "context not valid"; - return NULL; - } - - return CefRequestContext::GetGlobalContext()->GetCookieManager(callback); -} diff --git a/libcef/browser/cookie_manager_impl.h b/libcef/browser/net/cookie_manager_old_impl.h similarity index 91% rename from libcef/browser/cookie_manager_impl.h rename to libcef/browser/net/cookie_manager_old_impl.h index 035fc2932..e9eea737d 100644 --- a/libcef/browser/cookie_manager_impl.h +++ b/libcef/browser/net/cookie_manager_old_impl.h @@ -2,8 +2,8 @@ // reserved. Use of this source code is governed by a BSD-style license that can // be found in the LICENSE file. -#ifndef CEF_LIBCEF_BROWSER_COOKIE_MANAGER_IMPL_H_ -#define CEF_LIBCEF_BROWSER_COOKIE_MANAGER_IMPL_H_ +#ifndef CEF_LIBCEF_BROWSER_NET_COOKIE_MANAGER_OLD_IMPL_H_ +#define CEF_LIBCEF_BROWSER_NET_COOKIE_MANAGER_OLD_IMPL_H_ #include @@ -12,14 +12,13 @@ #include "libcef/browser/thread_util.h" #include "base/files/file_path.h" -#include "base/memory/weak_ptr.h" #include "net/cookies/cookie_monster.h" // Implementation of the CefCookieManager interface. -class CefCookieManagerImpl : public CefCookieManager { +class CefCookieManagerOldImpl : public CefCookieManager { public: - CefCookieManagerImpl(); - ~CefCookieManagerImpl() override; + CefCookieManagerOldImpl(); + ~CefCookieManagerOldImpl() override; // Must be called immediately after this object is created when |is_blocking| // is false. @@ -107,10 +106,7 @@ class CefCookieManagerImpl : public CefCookieManager { CefRefPtr request_context_; scoped_refptr request_context_impl_; - // Must be the last member. - base::WeakPtrFactory weak_ptr_factory_; - - IMPLEMENT_REFCOUNTING_DELETE_ON_IOT(CefCookieManagerImpl); + IMPLEMENT_REFCOUNTING_DELETE_ON_IOT(CefCookieManagerOldImpl); }; -#endif // CEF_LIBCEF_BROWSER_COOKIE_MANAGER_IMPL_H_ +#endif // CEF_LIBCEF_BROWSER_NET_COOKIE_MANAGER_OLD_IMPL_H_ diff --git a/libcef/browser/net/network_delegate.cc b/libcef/browser/net/network_delegate.cc index b63d903af..b40c82bae 100644 --- a/libcef/browser/net/network_delegate.cc +++ b/libcef/browser/net/network_delegate.cc @@ -9,7 +9,7 @@ #include "include/cef_urlrequest.h" #include "libcef/browser/browser_host_impl.h" -#include "libcef/browser/cookie_manager_impl.h" +#include "libcef/browser/net/cookie_manager_old_impl.h" #include "libcef/browser/net/net_util.h" #include "libcef/browser/net/source_stream.h" #include "libcef/browser/net/url_request_user_data.h" @@ -504,7 +504,7 @@ bool CefNetworkDelegate::OnCanSetCookie(const net::URLRequest& request, cefRequest->SetReadOnly(true); CefCookie cefCookie; - if (!CefCookieManagerImpl::GetCefCookie(cookie, cefCookie)) + if (!CefCookieManagerOldImpl::GetCefCookie(cookie, cefCookie)) return true; return handler->CanSetCookie(browser.get(), frame, cefRequest.get(), diff --git a/libcef/browser/net/resource_request_job.cc b/libcef/browser/net/resource_request_job.cc index 5d89ff787..1c3f20611 100644 --- a/libcef/browser/net/resource_request_job.cc +++ b/libcef/browser/net/resource_request_job.cc @@ -10,7 +10,7 @@ #include "include/cef_callback.h" #include "include/cef_parser.h" -#include "libcef/browser/cookie_manager_impl.h" +#include "libcef/browser/net/cookie_manager_old_impl.h" #include "libcef/browser/thread_util.h" #include "libcef/common/request_impl.h" #include "libcef/common/response_impl.h" @@ -457,7 +457,7 @@ void CefResourceRequestJob::CheckCookiePolicyAndLoad( net::CookieList::const_iterator it = cookie_list.begin(); for (; it != cookie_list.end(); ++it) { CefCookie cookie; - if (!CefCookieManagerImpl::GetCefCookie(*it, cookie) || + if (!CefCookieManagerOldImpl::GetCefCookie(*it, cookie) || !handler_->CanGetCookie(cookie)) { can_get_cookies = false; break; @@ -556,8 +556,8 @@ void CefResourceRequestJob::SaveNextCookie() { bool can_set_cookie = cookie && CanSetCookie(*cookie, &options); if (can_set_cookie) { CefCookie cef_cookie; - if (CefCookieManagerImpl::GetCefCookie(request_->url(), cookie_line, - cef_cookie)) { + if (CefCookieManagerOldImpl::GetCefCookie(request_->url(), cookie_line, + cef_cookie)) { can_set_cookie = handler_->CanSetCookie(cef_cookie); } else { can_set_cookie = false; diff --git a/libcef/browser/net/url_request_context_getter.cc b/libcef/browser/net/url_request_context_getter.cc index eec3f0b93..e7872b42b 100644 --- a/libcef/browser/net/url_request_context_getter.cc +++ b/libcef/browser/net/url_request_context_getter.cc @@ -9,7 +9,7 @@ #include #include "libcef/browser/content_browser_client.h" -#include "libcef/browser/cookie_manager_impl.h" +#include "libcef/browser/net/cookie_manager_old_impl.h" #include "libcef/browser/net/network_delegate.h" #include "libcef/browser/net/scheme_handler.h" #include "libcef/browser/net/url_request_interceptor.h" @@ -484,7 +484,7 @@ void CefURLRequestContextGetter::SetCookieSupportedSchemes( CEF_REQUIRE_IOT(); io_state_->cookie_supported_schemes_ = schemes; - CefCookieManagerImpl::SetCookieMonsterSchemes( + CefCookieManagerOldImpl::SetCookieMonsterSchemes( static_cast(GetExistingCookieStore()), io_state_->cookie_supported_schemes_); } @@ -548,7 +548,7 @@ void CefURLRequestContextGetter::SetCookieStoragePath( io_state_->cookie_store_path_ = path; // Restore the previously supported schemes. - CefCookieManagerImpl::SetCookieMonsterSchemes( + CefCookieManagerOldImpl::SetCookieMonsterSchemes( cookie_monster.get(), io_state_->cookie_supported_schemes_); io_state_->storage_->set_cookie_store(std::move(cookie_monster)); diff --git a/libcef/browser/net_service/cookie_manager_impl.cc b/libcef/browser/net_service/cookie_manager_impl.cc new file mode 100644 index 000000000..9244f6719 --- /dev/null +++ b/libcef/browser/net_service/cookie_manager_impl.cc @@ -0,0 +1,285 @@ +// Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that can +// be found in the LICENSE file. + +#include "libcef/browser/net_service/cookie_manager_impl.h" + +#include "libcef/common/time_util.h" + +#include "base/bind.h" +#include "base/logging.h" +#include "content/public/browser/browser_context.h" +#include "content/public/browser/storage_partition.h" +#include "url/gurl.h" + +using network::mojom::CookieManager; + +namespace { + +// Do not keep a reference to the CookieManager returned by this method. +CookieManager* GetCookieManager(CefRequestContextImpl* request_context) { + CEF_REQUIRE_UIT(); + return content::BrowserContext::GetDefaultStoragePartition( + request_context->GetBrowserContext()) + ->GetCookieManagerForBrowserProcess(); +} + +// Always execute the callback asynchronously. +void RunAsyncCompletionOnIOThread(CefRefPtr callback) { + if (!callback.get()) + return; + CEF_POST_TASK(CEF_IOT, + base::Bind(&CefCompletionCallback::OnComplete, callback.get())); +} + +// Always execute the callback asynchronously. +void SetCookieCallbackImpl(CefRefPtr callback, + bool success) { + if (!callback.get()) + return; + CEF_POST_TASK(CEF_IOT, base::Bind(&CefSetCookieCallback::OnComplete, + callback.get(), success)); +} + +// Always execute the callback asynchronously. +void DeleteCookiesCallbackImpl(CefRefPtr callback, + uint32_t num_deleted) { + if (!callback.get()) + return; + CEF_POST_TASK(CEF_IOT, base::Bind(&CefDeleteCookiesCallback::OnComplete, + callback.get(), num_deleted)); +} + +void GetCefCookie(const net::CanonicalCookie& cc, CefCookie& cookie) { + CefString(&cookie.name).FromString(cc.Name()); + CefString(&cookie.value).FromString(cc.Value()); + CefString(&cookie.domain).FromString(cc.Domain()); + CefString(&cookie.path).FromString(cc.Path()); + cookie.secure = cc.IsSecure(); + cookie.httponly = cc.IsHttpOnly(); + cef_time_from_basetime(cc.CreationDate(), cookie.creation); + cef_time_from_basetime(cc.LastAccessDate(), cookie.last_access); + cookie.has_expires = cc.IsPersistent(); + if (cookie.has_expires) + cef_time_from_basetime(cc.ExpiryDate(), cookie.expires); +} + +void DeleteCanonicalCookie(CefRefPtr request_context, + const net::CanonicalCookie& cc) { + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK(CEF_UIT, + base::Bind(&DeleteCanonicalCookie, request_context, cc)); + return; + } + + GetCookieManager(request_context.get()) + ->DeleteCanonicalCookie(cc, + CookieManager::DeleteCanonicalCookieCallback()); +} + +void ExecuteVisitor(CefRefPtr visitor, + CefRefPtr request_context, + const std::vector& cookies) { + CEF_REQUIRE_IOT(); + + int total = cookies.size(), count = 0; + for (const auto& cc : cookies) { + CefCookie cookie; + GetCefCookie(cc, cookie); + + bool deleteCookie = false; + bool keepLooping = visitor->Visit(cookie, count, total, deleteCookie); + if (deleteCookie) { + DeleteCanonicalCookie(request_context, cc); + } + if (!keepLooping) + break; + count++; + } +} + +// Always execute the callback asynchronously. +void GetCookiesCallbackImpl(CefRefPtr visitor, + CefRefPtr request_context, + const std::vector& cookies) { + CEF_POST_TASK(CEF_IOT, + base::Bind(&ExecuteVisitor, visitor, request_context, cookies)); +} + +} // namespace + +CefCookieManagerImpl::CefCookieManagerImpl() {} + +void CefCookieManagerImpl::Initialize( + CefRefPtr request_context, + CefRefPtr callback) { + DCHECK(request_context); + DCHECK(!request_context_); + request_context_ = request_context; + RunAsyncCompletionOnIOThread(callback); +} + +void CefCookieManagerImpl::SetSupportedSchemes( + const std::vector& schemes, + CefRefPtr callback) { + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK(CEF_UIT, + base::Bind(&CefCookieManagerImpl::SetSupportedSchemes, this, + schemes, callback)); + return; + } + + // TODO(network): Figure out how to route this to + // CookieMonster::SetCookieableSchemes via the NetworkService. + NOTIMPLEMENTED(); + RunAsyncCompletionOnIOThread(callback); +} + +bool CefCookieManagerImpl::VisitAllCookies( + CefRefPtr visitor) { + if (!visitor.get()) + return false; + + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK( + CEF_UIT, + base::Bind(base::IgnoreResult(&CefCookieManagerImpl::VisitAllCookies), + this, visitor)); + return true; + } + + GetCookieManager(request_context_.get()) + ->GetAllCookies( + base::Bind(&GetCookiesCallbackImpl, visitor, request_context_)); + return true; +} + +bool CefCookieManagerImpl::VisitUrlCookies( + const CefString& url, + bool includeHttpOnly, + CefRefPtr visitor) { + if (!visitor.get()) + return false; + + GURL gurl = GURL(url.ToString()); + if (!gurl.is_valid()) + return false; + + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK( + CEF_UIT, + base::Bind(base::IgnoreResult(&CefCookieManagerImpl::VisitUrlCookies), + this, url, includeHttpOnly, visitor)); + return true; + } + + net::CookieOptions options; + if (includeHttpOnly) + options.set_include_httponly(); + + GetCookieManager(request_context_.get()) + ->GetCookieList( + gurl, options, + base::Bind(&GetCookiesCallbackImpl, visitor, request_context_)); + return true; +} + +bool CefCookieManagerImpl::SetCookie(const CefString& url, + const CefCookie& cookie, + CefRefPtr callback) { + GURL gurl = GURL(url.ToString()); + if (!gurl.is_valid()) + return false; + + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK( + CEF_UIT, + base::Bind(base::IgnoreResult(&CefCookieManagerImpl::SetCookie), this, + url, cookie, callback)); + return true; + } + + std::string name = CefString(&cookie.name).ToString(); + std::string value = CefString(&cookie.value).ToString(); + std::string domain = CefString(&cookie.domain).ToString(); + std::string path = CefString(&cookie.path).ToString(); + + base::Time expiration_time; + if (cookie.has_expires) + cef_time_to_basetime(cookie.expires, expiration_time); + + auto canonical_cookie = net::CanonicalCookie::CreateSanitizedCookie( + gurl, name, value, domain, path, + base::Time(), // Creation time. + expiration_time, + base::Time(), // Last access time. + cookie.secure ? true : false, cookie.httponly ? true : false, + net::CookieSameSite::DEFAULT_MODE, net::COOKIE_PRIORITY_DEFAULT); + + GetCookieManager(request_context_.get()) + ->SetCanonicalCookie(*canonical_cookie, gurl.scheme(), + cookie.httponly ? true : false, + base::Bind(SetCookieCallbackImpl, callback)); + return true; +} + +bool CefCookieManagerImpl::DeleteCookies( + const CefString& url, + const CefString& cookie_name, + CefRefPtr callback) { + // Empty URLs are allowed but not invalid URLs. + GURL gurl = GURL(url.ToString()); + if (!gurl.is_empty() && !gurl.is_valid()) + return false; + + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK( + CEF_UIT, + base::Bind(base::IgnoreResult(&CefCookieManagerImpl::DeleteCookies), + this, url, cookie_name, callback)); + return true; + } + + network::mojom::CookieDeletionFilterPtr deletion_filter = + network::mojom::CookieDeletionFilter::New(); + + if (gurl.is_empty()) { + // Delete all cookies. + } else if (cookie_name.empty()) { + // Delete all matching host cookies. + deletion_filter->host_name = gurl.host(); + } else { + // Delete all matching host and domain cookies. + deletion_filter->url = gurl; + deletion_filter->cookie_name = cookie_name; + } + + GetCookieManager(request_context_.get()) + ->DeleteCookies(std::move(deletion_filter), + base::Bind(DeleteCookiesCallbackImpl, callback)); + return true; +} + +bool CefCookieManagerImpl::FlushStore( + CefRefPtr callback) { + if (!CEF_CURRENTLY_ON_UIT()) { + CEF_POST_TASK( + CEF_UIT, + base::Bind(base::IgnoreResult(&CefCookieManagerImpl::FlushStore), this, + callback)); + return true; + } + + GetCookieManager(request_context_.get()) + ->FlushCookieStore(base::Bind(RunAsyncCompletionOnIOThread, callback)); + return true; +} + +// CefCookieManager methods ---------------------------------------------------- + +// static +CefRefPtr CefCookieManager::GetGlobalManager( + CefRefPtr callback) { + CefRefPtr context = CefRequestContext::GetGlobalContext(); + return context ? context->GetCookieManager(callback) : nullptr; +} diff --git a/libcef/browser/net_service/cookie_manager_impl.h b/libcef/browser/net_service/cookie_manager_impl.h new file mode 100644 index 000000000..68ecc24b6 --- /dev/null +++ b/libcef/browser/net_service/cookie_manager_impl.h @@ -0,0 +1,46 @@ +// Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that can +// be found in the LICENSE file. + +#ifndef CEF_LIBCEF_BROWSER_NET_SERVICE_COOKIE_MANAGER_IMPL_H_ +#define CEF_LIBCEF_BROWSER_NET_SERVICE_COOKIE_MANAGER_IMPL_H_ + +#include "include/cef_cookie.h" +#include "libcef/browser/request_context_impl.h" +#include "libcef/browser/thread_util.h" + +#include "base/files/file_path.h" + +// Implementation of the CefCookieManager interface. +class CefCookieManagerImpl : public CefCookieManager { + public: + CefCookieManagerImpl(); + + // Must be called immediately after this object is created when |is_blocking| + // is false. + void Initialize(CefRefPtr request_context, + CefRefPtr callback); + + // CefCookieManager methods. + void SetSupportedSchemes(const std::vector& schemes, + CefRefPtr callback) override; + bool VisitAllCookies(CefRefPtr visitor) override; + bool VisitUrlCookies(const CefString& url, + bool includeHttpOnly, + CefRefPtr visitor) override; + bool SetCookie(const CefString& url, + const CefCookie& cookie, + CefRefPtr callback) override; + bool DeleteCookies(const CefString& url, + const CefString& cookie_name, + CefRefPtr callback) override; + bool FlushStore(CefRefPtr callback) override; + + private: + // Context that owns the cookie manager. + CefRefPtr request_context_; + + IMPLEMENT_REFCOUNTING(CefCookieManagerImpl); +}; + +#endif // CEF_LIBCEF_BROWSER_NET_SERVICE_COOKIE_MANAGER_IMPL_H_ diff --git a/libcef/browser/request_context_impl.cc b/libcef/browser/request_context_impl.cc index f7e64577f..9675b54ec 100644 --- a/libcef/browser/request_context_impl.cc +++ b/libcef/browser/request_context_impl.cc @@ -6,8 +6,9 @@ #include "libcef/browser/browser_context.h" #include "libcef/browser/content_browser_client.h" #include "libcef/browser/context.h" -#include "libcef/browser/cookie_manager_impl.h" #include "libcef/browser/extensions/extension_system.h" +#include "libcef/browser/net/cookie_manager_old_impl.h" +#include "libcef/browser/net_service/cookie_manager_impl.h" #include "libcef/browser/thread_util.h" #include "libcef/common/extensions/extensions_util.h" #include "libcef/common/net_service/util.h" @@ -281,9 +282,16 @@ CefString CefRequestContextImpl::GetCachePath() { CefRefPtr CefRequestContextImpl::GetCookieManager( CefRefPtr callback) { - CefRefPtr cookie_manager = new CefCookieManagerImpl(); - cookie_manager->Initialize(this, CefString(), false, callback); - return cookie_manager.get(); + if (!net_service::IsEnabled()) { + CefRefPtr cookie_manager = + new CefCookieManagerOldImpl(); + cookie_manager->Initialize(this, CefString(), false, callback); + return cookie_manager.get(); + } else { + CefRefPtr cookie_manager = new CefCookieManagerImpl(); + cookie_manager->Initialize(this, callback); + return cookie_manager.get(); + } } bool CefRequestContextImpl::RegisterSchemeHandlerFactory(