2019-03-25 19:14:23 +01:00
|
|
|
// 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"
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "libcef/common/net_service/net_service_util.h"
|
2019-03-25 19:14:23 +01:00
|
|
|
#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.
|
2019-03-25 19:35:51 +01:00
|
|
|
void RunAsyncCompletionOnUIThread(CefRefPtr<CefCompletionCallback> callback) {
|
2019-03-25 19:14:23 +01:00
|
|
|
if (!callback.get())
|
|
|
|
return;
|
2019-03-25 19:35:51 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2019-03-25 19:14:23 +01:00
|
|
|
base::Bind(&CefCompletionCallback::OnComplete, callback.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always execute the callback asynchronously.
|
|
|
|
void SetCookieCallbackImpl(CefRefPtr<CefSetCookieCallback> callback,
|
2019-06-05 16:15:45 +02:00
|
|
|
net::CanonicalCookie::CookieInclusionStatus status) {
|
2019-03-25 19:14:23 +01:00
|
|
|
if (!callback.get())
|
|
|
|
return;
|
2019-06-05 16:15:45 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
|
|
|
base::Bind(
|
|
|
|
&CefSetCookieCallback::OnComplete, callback.get(),
|
|
|
|
status == net::CanonicalCookie::CookieInclusionStatus::INCLUDE));
|
2019-03-25 19:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always execute the callback asynchronously.
|
|
|
|
void DeleteCookiesCallbackImpl(CefRefPtr<CefDeleteCookiesCallback> callback,
|
|
|
|
uint32_t num_deleted) {
|
|
|
|
if (!callback.get())
|
|
|
|
return;
|
2019-03-25 19:35:51 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT, base::Bind(&CefDeleteCookiesCallback::OnComplete,
|
2019-03-25 19:14:23 +01:00
|
|
|
callback.get(), num_deleted));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecuteVisitor(CefRefPtr<CefCookieVisitor> visitor,
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context,
|
|
|
|
const std::vector<net::CanonicalCookie>& cookies) {
|
2019-03-25 19:35:51 +01:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
CookieManager* cookie_manager = GetCookieManager(request_context.get());
|
2019-03-25 19:14:23 +01:00
|
|
|
|
|
|
|
int total = cookies.size(), count = 0;
|
|
|
|
for (const auto& cc : cookies) {
|
|
|
|
CefCookie cookie;
|
2019-04-24 04:50:25 +02:00
|
|
|
net_service::MakeCefCookie(cc, cookie);
|
2019-03-25 19:14:23 +01:00
|
|
|
|
|
|
|
bool deleteCookie = false;
|
|
|
|
bool keepLooping = visitor->Visit(cookie, count, total, deleteCookie);
|
|
|
|
if (deleteCookie) {
|
2019-03-25 19:35:51 +01:00
|
|
|
cookie_manager->DeleteCanonicalCookie(
|
|
|
|
cc, CookieManager::DeleteCanonicalCookieCallback());
|
2019-03-25 19:14:23 +01:00
|
|
|
}
|
|
|
|
if (!keepLooping)
|
|
|
|
break;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always execute the callback asynchronously.
|
|
|
|
void GetCookiesCallbackImpl(CefRefPtr<CefCookieVisitor> visitor,
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context,
|
2019-06-05 16:15:45 +02:00
|
|
|
const net::CookieList& cookies,
|
|
|
|
const net::CookieStatusList&) {
|
2019-03-25 19:35:51 +01:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
2019-03-25 19:14:23 +01:00
|
|
|
base::Bind(&ExecuteVisitor, visitor, request_context, cookies));
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:15:45 +02:00
|
|
|
void GetAllCookiesCallbackImpl(CefRefPtr<CefCookieVisitor> visitor,
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context,
|
|
|
|
const net::CookieList& cookies) {
|
|
|
|
GetCookiesCallbackImpl(visitor, request_context, cookies,
|
|
|
|
net::CookieStatusList());
|
|
|
|
}
|
|
|
|
|
2019-03-25 19:14:23 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefCookieManagerImpl::CefCookieManagerImpl() {}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::Initialize(
|
|
|
|
CefRefPtr<CefRequestContextImpl> request_context,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
|
|
|
DCHECK(request_context);
|
|
|
|
DCHECK(!request_context_);
|
|
|
|
request_context_ = request_context;
|
2019-03-25 19:35:51 +01:00
|
|
|
RunAsyncCompletionOnUIThread(callback);
|
2019-03-25 19:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::SetSupportedSchemes(
|
|
|
|
const std::vector<CefString>& schemes,
|
2019-05-03 00:23:57 +02:00
|
|
|
bool include_defaults,
|
2019-03-25 19:14:23 +01:00
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
|
|
|
if (!CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::Bind(&CefCookieManagerImpl::SetSupportedSchemes, this,
|
2019-05-03 00:23:57 +02:00
|
|
|
schemes, include_defaults, callback));
|
2019-03-25 19:14:23 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
std::vector<std::string> all_schemes;
|
|
|
|
for (const auto& scheme : schemes)
|
|
|
|
all_schemes.push_back(scheme);
|
|
|
|
|
2019-05-03 00:23:57 +02:00
|
|
|
if (include_defaults) {
|
|
|
|
// Add default schemes that should always support cookies.
|
|
|
|
// This list should match CookieMonster::kDefaultCookieableSchemes.
|
|
|
|
all_schemes.push_back("http");
|
|
|
|
all_schemes.push_back("https");
|
|
|
|
all_schemes.push_back("ws");
|
|
|
|
all_schemes.push_back("wss");
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// This will be forwarded to the CookieMonster that lives in the
|
|
|
|
// NetworkService process when the NetworkContext is created via
|
|
|
|
// CefContentBrowserClient::CreateNetworkContext.
|
2019-05-03 00:23:57 +02:00
|
|
|
request_context_->GetBrowserContext()->set_cookieable_schemes(
|
|
|
|
base::make_optional(all_schemes));
|
2019-03-25 19:35:51 +01:00
|
|
|
RunAsyncCompletionOnUIThread(callback);
|
2019-03-25 19:14:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2019-06-05 16:15:45 +02:00
|
|
|
base::Bind(&GetAllCookiesCallbackImpl, visitor, request_context_));
|
2019-03-25 19:14:23 +01:00
|
|
|
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,
|
2019-06-05 16:15:45 +02:00
|
|
|
net::CookieSameSite::UNSPECIFIED, net::COOKIE_PRIORITY_DEFAULT);
|
2019-03-25 19:14:23 +01:00
|
|
|
|
2019-05-08 20:12:21 +02:00
|
|
|
if (!canonical_cookie) {
|
2019-06-05 16:15:45 +02:00
|
|
|
SetCookieCallbackImpl(
|
|
|
|
callback,
|
|
|
|
net::CanonicalCookie::CookieInclusionStatus::EXCLUDE_UNKNOWN_ERROR);
|
2019-05-08 20:12:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:38:48 +02:00
|
|
|
net::CookieOptions options;
|
|
|
|
if (cookie.httponly)
|
|
|
|
options.set_include_httponly();
|
|
|
|
|
2019-03-25 19:14:23 +01:00
|
|
|
GetCookieManager(request_context_.get())
|
2019-04-16 16:38:48 +02:00
|
|
|
->SetCanonicalCookie(*canonical_cookie, gurl.scheme(), options,
|
2019-03-25 19:14:23 +01:00
|
|
|
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())
|
2019-03-25 19:35:51 +01:00
|
|
|
->FlushCookieStore(base::Bind(RunAsyncCompletionOnUIThread, callback));
|
2019-03-25 19:14:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CefCookieManager methods ----------------------------------------------------
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefCookieManager> CefCookieManager::GetGlobalManager(
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
|
|
|
CefRefPtr<CefRequestContext> context = CefRequestContext::GetGlobalContext();
|
|
|
|
return context ? context->GetCookieManager(callback) : nullptr;
|
|
|
|
}
|