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.
This commit is contained in:
Marshall Greenblatt 2019-03-25 14:14:23 -04:00
parent b65f336f81
commit 85c34c4dcf
10 changed files with 419 additions and 100 deletions

View File

@ -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",

View File

@ -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();

View File

@ -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 <set>
#include <string>
@ -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<VisitCookiesCallback> {
public:
explicit VisitCookiesCallback(
const CefCookieManagerImpl::CookieStoreGetter& cookie_store_getter,
const CefCookieManagerOldImpl::CookieStoreGetter& cookie_store_getter,
CefRefPtr<CefCookieVisitor> visitor)
: cookie_store_getter_(cookie_store_getter), visitor_(visitor) {}
@ -50,7 +49,7 @@ class VisitCookiesCallback : public base::RefCounted<VisitCookiesCallback> {
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> {
~VisitCookiesCallback() {}
CefCookieManagerImpl::CookieStoreGetter cookie_store_getter_;
CefCookieManagerOldImpl::CookieStoreGetter cookie_store_getter_;
CefRefPtr<CefCookieVisitor> visitor_;
};
@ -119,46 +118,42 @@ void SetCookieCallbackImpl(CefRefPtr<CefSetCookieCallback> callback,
} // namespace
CefCookieManagerImpl::CefCookieManagerImpl() : weak_ptr_factory_(this) {}
CefCookieManagerOldImpl::CefCookieManagerOldImpl() {}
CefCookieManagerImpl::~CefCookieManagerImpl() {
CefCookieManagerOldImpl::~CefCookieManagerOldImpl() {
CEF_REQUIRE_IOT();
}
void CefCookieManagerImpl::Initialize(
void CefCookieManagerOldImpl::Initialize(
CefRefPtr<CefRequestContextImpl> request_context,
const CefString& path,
bool persist_session_cookies,
CefRefPtr<CefCompletionCallback> 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<base::SingleThreadTaskRunner> 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<CefString>& schemes,
CefRefPtr<CefCompletionCallback> 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<CefCookieVisitor> 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<CefCookieVisitor> 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<CefSetCookieCallback> callback) {
bool CefCookieManagerOldImpl::SetCookie(
const CefString& url,
const CefCookie& cookie,
CefRefPtr<CefSetCookieCallback> 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<CefDeleteCookiesCallback> 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<CefCompletionCallback> 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<std::string>& 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<CefCompletionCallback> callback,
scoped_refptr<CefURLRequestContextGetter> request_context) {
CEF_REQUIRE_IOT();
@ -351,7 +347,7 @@ void CefCookieManagerImpl::InitWithContext(
RunAsyncCompletionOnIOThread(callback);
}
void CefCookieManagerImpl::SetSupportedSchemesWithContext(
void CefCookieManagerOldImpl::SetSupportedSchemesWithContext(
const std::vector<std::string>& schemes,
CefRefPtr<CefCompletionCallback> callback,
scoped_refptr<CefURLRequestContextGetter> request_context) {
@ -362,7 +358,7 @@ void CefCookieManagerImpl::SetSupportedSchemesWithContext(
RunAsyncCompletionOnIOThread(callback);
}
void CefCookieManagerImpl::GetCookieStoreWithContext(
void CefCookieManagerOldImpl::GetCookieStoreWithContext(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const CookieStoreCallback& callback,
scoped_refptr<CefURLRequestContextGetter> request_context) {
@ -381,22 +377,17 @@ void CefCookieManagerImpl::GetCookieStoreWithContext(
}
}
void CefCookieManagerImpl::SetSupportedSchemesInternal(
void CefCookieManagerOldImpl::SetSupportedSchemesInternal(
const std::vector<std::string>& schemes,
CefRefPtr<CefCompletionCallback> 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<CefCookieVisitor> 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<CefCookieVisitor> 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<CefSetCookieCallback> 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<CefDeleteCookiesCallback> callback,
@ -508,7 +499,7 @@ void CefCookieManagerImpl::DeleteCookiesInternal(
}
}
void CefCookieManagerImpl::FlushStoreInternal(
void CefCookieManagerOldImpl::FlushStoreInternal(
CefRefPtr<CefCompletionCallback> 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> CefCookieManager::GetGlobalManager(
CefRefPtr<CefCompletionCallback> 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);
}

View File

@ -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 <set>
@ -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<CefRequestContextImpl> request_context_;
scoped_refptr<CefURLRequestContextGetter> request_context_impl_;
// Must be the last member.
base::WeakPtrFactory<CefCookieManagerImpl> 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_

View File

@ -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(),

View File

@ -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;

View File

@ -9,7 +9,7 @@
#include <vector>
#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<net::CookieMonster*>(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));

View File

@ -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<CefCompletionCallback> callback) {
if (!callback.get())
return;
CEF_POST_TASK(CEF_IOT,
base::Bind(&CefCompletionCallback::OnComplete, callback.get()));
}
// Always execute the callback asynchronously.
void SetCookieCallbackImpl(CefRefPtr<CefSetCookieCallback> 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<CefDeleteCookiesCallback> 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<CefRequestContextImpl> 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<CefCookieVisitor> visitor,
CefRefPtr<CefRequestContextImpl> request_context,
const std::vector<net::CanonicalCookie>& 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<CefCookieVisitor> visitor,
CefRefPtr<CefRequestContextImpl> request_context,
const std::vector<net::CanonicalCookie>& cookies) {
CEF_POST_TASK(CEF_IOT,
base::Bind(&ExecuteVisitor, visitor, request_context, cookies));
}
} // namespace
CefCookieManagerImpl::CefCookieManagerImpl() {}
void CefCookieManagerImpl::Initialize(
CefRefPtr<CefRequestContextImpl> request_context,
CefRefPtr<CefCompletionCallback> callback) {
DCHECK(request_context);
DCHECK(!request_context_);
request_context_ = request_context;
RunAsyncCompletionOnIOThread(callback);
}
void CefCookieManagerImpl::SetSupportedSchemes(
const std::vector<CefString>& schemes,
CefRefPtr<CefCompletionCallback> 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<CefCookieVisitor> 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<CefCookieVisitor> 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<CefSetCookieCallback> 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<CefDeleteCookiesCallback> 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<CefCompletionCallback> 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> CefCookieManager::GetGlobalManager(
CefRefPtr<CefCompletionCallback> callback) {
CefRefPtr<CefRequestContext> context = CefRequestContext::GetGlobalContext();
return context ? context->GetCookieManager(callback) : nullptr;
}

View File

@ -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<CefRequestContextImpl> request_context,
CefRefPtr<CefCompletionCallback> callback);
// CefCookieManager methods.
void SetSupportedSchemes(const std::vector<CefString>& schemes,
CefRefPtr<CefCompletionCallback> callback) override;
bool VisitAllCookies(CefRefPtr<CefCookieVisitor> visitor) override;
bool VisitUrlCookies(const CefString& url,
bool includeHttpOnly,
CefRefPtr<CefCookieVisitor> visitor) override;
bool SetCookie(const CefString& url,
const CefCookie& cookie,
CefRefPtr<CefSetCookieCallback> callback) override;
bool DeleteCookies(const CefString& url,
const CefString& cookie_name,
CefRefPtr<CefDeleteCookiesCallback> callback) override;
bool FlushStore(CefRefPtr<CefCompletionCallback> callback) override;
private:
// Context that owns the cookie manager.
CefRefPtr<CefRequestContextImpl> request_context_;
IMPLEMENT_REFCOUNTING(CefCookieManagerImpl);
};
#endif // CEF_LIBCEF_BROWSER_NET_SERVICE_COOKIE_MANAGER_IMPL_H_

View File

@ -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<CefCookieManager> CefRequestContextImpl::GetCookieManager(
CefRefPtr<CefCompletionCallback> callback) {
CefRefPtr<CefCookieManagerImpl> cookie_manager = new CefCookieManagerImpl();
cookie_manager->Initialize(this, CefString(), false, callback);
return cookie_manager.get();
if (!net_service::IsEnabled()) {
CefRefPtr<CefCookieManagerOldImpl> cookie_manager =
new CefCookieManagerOldImpl();
cookie_manager->Initialize(this, CefString(), false, callback);
return cookie_manager.get();
} else {
CefRefPtr<CefCookieManagerImpl> cookie_manager = new CefCookieManagerImpl();
cookie_manager->Initialize(this, callback);
return cookie_manager.get();
}
}
bool CefRequestContextImpl::RegisterSchemeHandlerFactory(