2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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/cookie_manager_impl.h"
|
|
|
|
|
2012-08-04 02:59:58 +02:00
|
|
|
#include <set>
|
2012-04-03 03:34:16 +02:00
|
|
|
#include <string>
|
2012-08-04 02:59:58 +02:00
|
|
|
#include <vector>
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-09-03 18:43:31 +02:00
|
|
|
#include "libcef/browser/content_browser_client.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/browser/context.h"
|
2015-12-09 17:10:16 +01:00
|
|
|
#include "libcef/browser/net/network_delegate.h"
|
2017-09-21 13:57:40 +02:00
|
|
|
#include "libcef/common/task_runner_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/common/time_util.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
2014-11-12 20:25:15 +01:00
|
|
|
#include "base/files/file_util.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
#include "base/format_macros.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/logging.h"
|
2012-05-18 17:04:56 +02:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "content/browser/storage_partition_impl.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
#include "net/cookies/cookie_util.h"
|
2012-08-04 02:59:58 +02:00
|
|
|
#include "net/cookies/parsed_cookie.h"
|
2015-06-06 00:06:48 +02:00
|
|
|
#include "net/extras/sqlite/sqlite_persistent_cookie_store.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "net/url_request/url_request_context.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "url/gurl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-03-12 21:23:24 +01:00
|
|
|
using content::BrowserThread;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Callback class for visiting cookies.
|
|
|
|
class VisitCookiesCallback : public base::RefCounted<VisitCookiesCallback> {
|
|
|
|
public:
|
2015-03-02 21:25:14 +01:00
|
|
|
explicit VisitCookiesCallback(
|
2016-03-16 03:55:59 +01:00
|
|
|
const CefCookieManagerImpl::CookieStoreGetter& cookie_store_getter,
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<CefCookieVisitor> visitor)
|
2017-05-17 11:29:28 +02:00
|
|
|
: cookie_store_getter_(cookie_store_getter), visitor_(visitor) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
void Run(const net::CookieList& list) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
int total = list.size(), count = 0;
|
|
|
|
|
|
|
|
net::CookieList::const_iterator it = list.begin();
|
|
|
|
for (; it != list.end(); ++it, ++count) {
|
|
|
|
CefCookie cookie;
|
2012-08-04 02:59:58 +02:00
|
|
|
const net::CanonicalCookie& cc = *(it);
|
2012-06-19 18:29:49 +02:00
|
|
|
CefCookieManagerImpl::GetCefCookie(cc, cookie);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
bool deleteCookie = false;
|
|
|
|
bool keepLooping = visitor_->Visit(cookie, count, total, deleteCookie);
|
|
|
|
if (deleteCookie) {
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store = cookie_store_getter_.Run();
|
|
|
|
if (cookie_store) {
|
2017-05-17 11:29:28 +02:00
|
|
|
cookie_store->DeleteCanonicalCookieAsync(
|
|
|
|
cc, net::CookieMonster::DeleteCallback());
|
2016-03-16 03:55:59 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
if (!keepLooping)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-12 20:25:15 +01:00
|
|
|
friend class base::RefCounted<VisitCookiesCallback>;
|
|
|
|
|
|
|
|
~VisitCookiesCallback() {}
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
CefCookieManagerImpl::CookieStoreGetter cookie_store_getter_;
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefCookieVisitor> visitor_;
|
|
|
|
};
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
// Methods extracted from net/cookies/cookie_store.cc
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Determine the cookie domain to use for setting the specified cookie.
|
|
|
|
bool GetCookieDomain(const GURL& url,
|
2012-08-04 02:59:58 +02:00
|
|
|
const net::ParsedCookie& pc,
|
2012-06-19 18:29:49 +02:00
|
|
|
std::string* result) {
|
|
|
|
std::string domain_string;
|
|
|
|
if (pc.HasDomain())
|
|
|
|
domain_string = pc.Domain();
|
|
|
|
return net::cookie_util::GetCookieDomainWithString(url, domain_string,
|
2017-05-17 11:29:28 +02:00
|
|
|
result);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Always execute the callback asynchronously.
|
|
|
|
void RunAsyncCompletionOnIOThread(CefRefPtr<CefCompletionCallback> callback) {
|
|
|
|
if (!callback.get())
|
|
|
|
return;
|
2013-02-13 20:53:41 +01:00
|
|
|
CEF_POST_TASK(CEF_IOT,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CefCompletionCallback::OnComplete, callback.get()));
|
2013-02-13 20:53:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Always execute the callback asynchronously.
|
|
|
|
void DeleteCookiesCallbackImpl(CefRefPtr<CefDeleteCookiesCallback> callback,
|
2017-07-27 01:19:27 +02:00
|
|
|
uint32_t num_deleted) {
|
2015-03-02 21:25:14 +01:00
|
|
|
if (!callback.get())
|
|
|
|
return;
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefDeleteCookiesCallback::OnComplete,
|
|
|
|
callback.get(), num_deleted));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always execute the callback asynchronously.
|
|
|
|
void SetCookieCallbackImpl(CefRefPtr<CefSetCookieCallback> callback,
|
|
|
|
bool success) {
|
|
|
|
if (!callback.get())
|
|
|
|
return;
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefSetCookieCallback::OnComplete,
|
|
|
|
callback.get(), success));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
2016-04-29 23:09:35 +02:00
|
|
|
net::CookieStore* GetExistingCookieStoreHelper(
|
|
|
|
base::WeakPtr<CefCookieManagerImpl> cookie_manager) {
|
|
|
|
if (cookie_manager.get())
|
|
|
|
return cookie_manager->GetExistingCookieStore();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
} // namespace
|
|
|
|
|
2018-03-15 02:40:37 +01:00
|
|
|
CefCookieManagerImpl::CefCookieManagerImpl(bool is_blocking)
|
|
|
|
: is_blocking_(is_blocking), weak_ptr_factory_(this) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefCookieManagerImpl::~CefCookieManagerImpl() {
|
2016-03-16 03:55:59 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-02-13 20:53:41 +01:00
|
|
|
void CefCookieManagerImpl::Initialize(
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<CefRequestContextImpl> request_context,
|
2013-02-13 20:53:41 +01:00
|
|
|
const CefString& path,
|
2015-03-02 21:25:14 +01:00
|
|
|
bool persist_session_cookies,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
2018-03-15 02:40:37 +01:00
|
|
|
CHECK(!is_blocking_);
|
2015-03-02 21:25:14 +01:00
|
|
|
if (request_context.get()) {
|
|
|
|
request_context_ = request_context;
|
|
|
|
request_context_->GetRequestContextImpl(
|
2016-07-21 23:21:32 +02:00
|
|
|
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(&CefCookieManagerImpl::InitWithContext, this, callback));
|
|
|
|
} else {
|
|
|
|
SetStoragePath(path, persist_session_cookies, callback);
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
void CefCookieManagerImpl::GetCookieStore(
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreCallback& callback) {
|
2015-03-02 21:25:14 +01:00
|
|
|
if (!task_runner.get())
|
2017-09-21 13:57:40 +02:00
|
|
|
task_runner = CefTaskRunnerImpl::GetCurrentTaskRunner();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
if (!CEF_CURRENTLY_ON_IOT()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefCookieManagerImpl::GetCookieStore,
|
|
|
|
this, task_runner, callback));
|
2015-03-02 21:25:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
if (HasContext()) {
|
|
|
|
RunMethodWithContext(
|
2016-03-16 03:55:59 +01:00
|
|
|
base::Bind(&CefCookieManagerImpl::GetCookieStoreWithContext, this,
|
2015-03-02 21:25:14 +01:00
|
|
|
task_runner, callback));
|
|
|
|
return;
|
|
|
|
}
|
2012-04-04 00:14:28 +02:00
|
|
|
|
2018-03-15 02:40:37 +01:00
|
|
|
DCHECK(is_blocking_ || cookie_store_.get());
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2016-04-29 23:09:35 +02:00
|
|
|
// Binding ref-counted |this| to CookieStoreGetter may result in
|
|
|
|
// heap-use-after-free if (a) the CookieStoreGetter contains the last
|
|
|
|
// CefCookieManagerImpl reference and (b) that reference is released during
|
|
|
|
// execution of a CookieMonster callback (which then results in the
|
|
|
|
// CookieManager being deleted). Use WeakPtr instead of |this| so that, in
|
|
|
|
// that case, the CookieStoreGetter will return nullptr instead of keeping
|
|
|
|
// the CefCookieManagerImpl alive (see issue #1882).
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreGetter& cookie_store_getter =
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(GetExistingCookieStoreHelper, weak_ptr_factory_.GetWeakPtr());
|
2016-03-16 03:55:59 +01:00
|
|
|
|
|
|
|
if (task_runner->BelongsToCurrentThread()) {
|
|
|
|
// Execute the callback immediately.
|
|
|
|
callback.Run(cookie_store_getter);
|
|
|
|
} else {
|
|
|
|
// Execute the callback on the target thread.
|
2017-05-17 11:29:28 +02:00
|
|
|
task_runner->PostTask(FROM_HERE, base::Bind(callback, cookie_store_getter));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-04 00:14:28 +02:00
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* CefCookieManagerImpl::GetExistingCookieStore() {
|
2015-03-02 21:25:14 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
2016-03-16 03:55:59 +01:00
|
|
|
if (cookie_store_.get()) {
|
|
|
|
return cookie_store_.get();
|
2015-03-02 21:25:14 +01:00
|
|
|
} else if (request_context_impl_.get()) {
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store =
|
|
|
|
request_context_impl_->GetExistingCookieStore();
|
|
|
|
DCHECK(cookie_store);
|
|
|
|
return cookie_store;
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
2012-04-04 00:14:28 +02:00
|
|
|
|
2018-03-15 02:40:37 +01:00
|
|
|
DCHECK(is_blocking_);
|
|
|
|
if (!is_blocking_)
|
|
|
|
LOG(ERROR) << "Cookie store does not exist";
|
2016-03-16 03:55:59 +01:00
|
|
|
return nullptr;
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
2012-04-04 00:14:28 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void CefCookieManagerImpl::SetSupportedSchemes(
|
|
|
|
const std::vector<CefString>& schemes,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
|
|
|
if (!CEF_CURRENTLY_ON_IOT()) {
|
2012-04-04 00:14:28 +02:00
|
|
|
CEF_POST_TASK(CEF_IOT,
|
2017-05-17 11:29:28 +02:00
|
|
|
base::Bind(&CefCookieManagerImpl::SetSupportedSchemes, this,
|
|
|
|
schemes, callback));
|
2015-03-02 21:25:14 +01:00
|
|
|
return;
|
2012-04-04 00:14:28 +02:00
|
|
|
}
|
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
std::vector<std::string> scheme_set;
|
2015-08-21 22:17:59 +02:00
|
|
|
std::vector<CefString>::const_iterator it = schemes.begin();
|
|
|
|
for (; it != schemes.end(); ++it)
|
2016-02-05 01:49:19 +01:00
|
|
|
scheme_set.push_back(*it);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-08-21 22:17:59 +02:00
|
|
|
SetSupportedSchemesInternal(scheme_set, callback);
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
bool CefCookieManagerImpl::VisitAllCookies(
|
|
|
|
CefRefPtr<CefCookieVisitor> visitor) {
|
2017-05-17 11:29:28 +02:00
|
|
|
GetCookieStore(BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
|
|
|
base::Bind(&CefCookieManagerImpl::VisitAllCookiesInternal,
|
|
|
|
this, visitor));
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
bool CefCookieManagerImpl::VisitUrlCookies(
|
|
|
|
const CefString& url,
|
|
|
|
bool includeHttpOnly,
|
|
|
|
CefRefPtr<CefCookieVisitor> visitor) {
|
2017-05-17 11:29:28 +02:00
|
|
|
GetCookieStore(BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
|
|
|
base::Bind(&CefCookieManagerImpl::VisitUrlCookiesInternal,
|
|
|
|
this, url, includeHttpOnly, visitor));
|
2015-03-02 21:25:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool CefCookieManagerImpl::SetCookie(const CefString& url,
|
|
|
|
const CefCookie& cookie,
|
|
|
|
CefRefPtr<CefSetCookieCallback> callback) {
|
2012-04-03 03:34:16 +02:00
|
|
|
GURL gurl = GURL(url.ToString());
|
|
|
|
if (!gurl.is_valid())
|
|
|
|
return false;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
GetCookieStore(BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
|
|
|
base::Bind(&CefCookieManagerImpl::SetCookieInternal, this,
|
|
|
|
gurl, cookie, callback));
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
bool CefCookieManagerImpl::DeleteCookies(
|
|
|
|
const CefString& url,
|
|
|
|
const CefString& cookie_name,
|
|
|
|
CefRefPtr<CefDeleteCookiesCallback> callback) {
|
|
|
|
// Empty URLs are allowed but not invalid URLs.
|
2012-04-03 03:34:16 +02:00
|
|
|
GURL gurl = GURL(url.ToString());
|
2015-03-02 21:25:14 +01:00
|
|
|
if (!gurl.is_empty() && !gurl.is_valid())
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
GetCookieStore(BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
|
|
|
base::Bind(&CefCookieManagerImpl::DeleteCookiesInternal, this,
|
|
|
|
gurl, cookie_name, callback));
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:53:41 +01:00
|
|
|
bool CefCookieManagerImpl::SetStoragePath(
|
|
|
|
const CefString& path,
|
2015-03-02 21:25:14 +01:00
|
|
|
bool persist_session_cookies,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
|
|
|
if (!CEF_CURRENTLY_ON_IOT()) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_IOT,
|
2012-04-03 03:34:16 +02:00
|
|
|
base::Bind(base::IgnoreResult(&CefCookieManagerImpl::SetStoragePath),
|
2015-03-02 21:25:14 +01:00
|
|
|
this, path, persist_session_cookies, callback));
|
|
|
|
return true;
|
2013-02-13 20:53:41 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
if (HasContext()) {
|
|
|
|
RunMethodWithContext(
|
|
|
|
base::Bind(&CefCookieManagerImpl::SetStoragePathWithContext, this, path,
|
|
|
|
persist_session_cookies, callback));
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-13 20:53:41 +01:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
base::FilePath new_path;
|
|
|
|
if (!path.empty())
|
|
|
|
new_path = base::FilePath(path);
|
2013-02-13 20:53:41 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
if (cookie_store_.get() &&
|
|
|
|
((storage_path_.empty() && path.empty()) || storage_path_ == new_path)) {
|
2015-03-02 21:25:14 +01:00
|
|
|
// The path has not changed so don't do anything.
|
|
|
|
RunAsyncCompletionOnIOThread(callback);
|
|
|
|
return true;
|
|
|
|
}
|
2013-02-13 20:53:41 +01:00
|
|
|
|
2015-06-06 00:06:48 +02:00
|
|
|
scoped_refptr<net::SQLitePersistentCookieStore> persistent_store;
|
2015-03-02 21:25:14 +01:00
|
|
|
if (!new_path.empty()) {
|
|
|
|
// TODO(cef): Move directory creation to the blocking pool instead of
|
|
|
|
// allowing file IO on this thread.
|
|
|
|
base::ThreadRestrictions::ScopedAllowIO allow_io;
|
2017-05-17 11:29:28 +02:00
|
|
|
if (base::DirectoryExists(new_path) || base::CreateDirectory(new_path)) {
|
2015-03-02 21:25:14 +01:00
|
|
|
const base::FilePath& cookie_path = new_path.AppendASCII("Cookies");
|
2017-05-17 11:29:28 +02:00
|
|
|
persistent_store = new net::SQLitePersistentCookieStore(
|
|
|
|
cookie_path, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
|
|
|
BrowserThread::GetTaskRunnerForThread(BrowserThread::DB),
|
|
|
|
persist_session_cookies, NULL);
|
2015-03-02 21:25:14 +01:00
|
|
|
} else {
|
|
|
|
NOTREACHED() << "The cookie storage directory could not be created";
|
|
|
|
storage_path_.clear();
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Set the new cookie store that will be used for all new requests. The old
|
|
|
|
// cookie store, if any, will be automatically flushed and closed when no
|
|
|
|
// longer referenced.
|
2016-03-16 03:55:59 +01:00
|
|
|
cookie_store_.reset(new net::CookieMonster(persistent_store.get(), NULL));
|
2015-03-02 21:25:14 +01:00
|
|
|
if (persistent_store.get() && persist_session_cookies)
|
2016-03-16 03:55:59 +01:00
|
|
|
cookie_store_->SetPersistSessionCookies(true);
|
2015-03-02 21:25:14 +01:00
|
|
|
storage_path_ = new_path;
|
|
|
|
|
|
|
|
// Restore the previously supported schemes.
|
2015-08-21 22:17:59 +02:00
|
|
|
SetSupportedSchemesInternal(supported_schemes_, callback);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
bool CefCookieManagerImpl::FlushStore(
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
2016-03-16 03:55:59 +01:00
|
|
|
GetCookieStore(
|
2016-07-21 23:21:32 +02:00
|
|
|
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO),
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(&CefCookieManagerImpl::FlushStoreInternal, this, callback));
|
|
|
|
return true;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// static
|
2012-08-04 02:59:58 +02:00
|
|
|
bool CefCookieManagerImpl::GetCefCookie(const net::CanonicalCookie& cc,
|
|
|
|
CefCookie& cookie) {
|
2012-06-19 18:29:49 +02:00
|
|
|
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);
|
2012-08-04 02:59:58 +02:00
|
|
|
cookie.has_expires = cc.IsPersistent();
|
2012-06-19 18:29:49 +02:00
|
|
|
if (cookie.has_expires)
|
|
|
|
cef_time_from_basetime(cc.ExpiryDate(), cookie.expires);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefCookieManagerImpl::GetCefCookie(const GURL& url,
|
|
|
|
const std::string& cookie_line,
|
|
|
|
CefCookie& cookie) {
|
|
|
|
// Parse the cookie.
|
2012-08-04 02:59:58 +02:00
|
|
|
net::ParsedCookie pc(cookie_line);
|
2012-06-19 18:29:49 +02:00
|
|
|
if (!pc.IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string cookie_domain;
|
|
|
|
if (!GetCookieDomain(url, pc, &cookie_domain))
|
|
|
|
return false;
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
std::string path_string;
|
|
|
|
if (pc.HasPath())
|
|
|
|
path_string = pc.Path();
|
|
|
|
std::string cookie_path =
|
|
|
|
net::CanonicalCookie::CanonPathWithString(url, path_string);
|
2012-06-19 18:29:49 +02:00
|
|
|
base::Time creation_time = base::Time::Now();
|
2012-08-04 02:59:58 +02:00
|
|
|
base::Time cookie_expires =
|
2012-11-05 21:18:20 +01:00
|
|
|
net::CanonicalCookie::CanonExpiration(pc, creation_time, creation_time);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
CefString(&cookie.name).FromString(pc.Name());
|
|
|
|
CefString(&cookie.value).FromString(pc.Value());
|
|
|
|
CefString(&cookie.domain).FromString(cookie_domain);
|
|
|
|
CefString(&cookie.path).FromString(cookie_path);
|
|
|
|
cookie.secure = pc.IsSecure();
|
|
|
|
cookie.httponly = pc.IsHttpOnly();
|
|
|
|
cef_time_from_basetime(creation_time, cookie.creation);
|
|
|
|
cef_time_from_basetime(creation_time, cookie.last_access);
|
|
|
|
cookie.has_expires = !cookie_expires.is_null();
|
|
|
|
if (cookie.has_expires)
|
|
|
|
cef_time_from_basetime(cookie_expires, cookie.expires);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:17:59 +02:00
|
|
|
// static
|
|
|
|
void CefCookieManagerImpl::SetCookieMonsterSchemes(
|
|
|
|
net::CookieMonster* cookie_monster,
|
2016-02-05 01:49:19 +01:00
|
|
|
const std::vector<std::string>& schemes) {
|
2015-08-21 22:17:59 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
std::vector<std::string> all_schemes = schemes;
|
2015-08-21 22:17:59 +02:00
|
|
|
|
|
|
|
// Add default schemes that should always support cookies.
|
2016-02-05 01:49:19 +01:00
|
|
|
all_schemes.push_back("http");
|
|
|
|
all_schemes.push_back("https");
|
|
|
|
all_schemes.push_back("ws");
|
|
|
|
all_schemes.push_back("wss");
|
2015-08-21 22:17:59 +02:00
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
cookie_monster->SetCookieableSchemes(all_schemes);
|
2015-08-21 22:17:59 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
bool CefCookieManagerImpl::HasContext() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
return (request_context_impl_.get() || request_context_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::RunMethodWithContext(
|
|
|
|
const CefRequestContextImpl::RequestContextCallback& method) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
if (request_context_impl_.get()) {
|
|
|
|
method.Run(request_context_impl_);
|
|
|
|
} else if (request_context_.get()) {
|
|
|
|
// Try again after the request context is initialized.
|
|
|
|
request_context_->GetRequestContextImpl(
|
2017-05-17 11:29:28 +02:00
|
|
|
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO), method);
|
2015-03-02 21:25:14 +01:00
|
|
|
} else {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::InitWithContext(
|
|
|
|
CefRefPtr<CefCompletionCallback> callback,
|
|
|
|
scoped_refptr<CefURLRequestContextGetterImpl> request_context) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
DCHECK(!request_context_impl_.get());
|
|
|
|
request_context_impl_ = request_context;
|
|
|
|
|
|
|
|
// Clear the CefRequestContextImpl reference here to avoid a potential
|
|
|
|
// reference loop between CefRequestContextImpl (which has a reference to
|
|
|
|
// CefRequestContextHandler), CefRequestContextHandler (which may keep a
|
|
|
|
// reference to this object) and this object.
|
|
|
|
request_context_ = NULL;
|
|
|
|
|
|
|
|
RunAsyncCompletionOnIOThread(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::SetStoragePathWithContext(
|
|
|
|
const CefString& path,
|
|
|
|
bool persist_session_cookies,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback,
|
|
|
|
scoped_refptr<CefURLRequestContextGetterImpl> request_context) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
base::FilePath new_path;
|
|
|
|
if (!path.empty())
|
|
|
|
new_path = base::FilePath(path);
|
|
|
|
|
|
|
|
request_context->SetCookieStoragePath(new_path, persist_session_cookies);
|
|
|
|
|
|
|
|
RunAsyncCompletionOnIOThread(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::SetSupportedSchemesWithContext(
|
2016-02-05 01:49:19 +01:00
|
|
|
const std::vector<std::string>& schemes,
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<CefCompletionCallback> callback,
|
|
|
|
scoped_refptr<CefURLRequestContextGetterImpl> request_context) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2015-08-21 22:17:59 +02:00
|
|
|
request_context->SetCookieSupportedSchemes(schemes);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
RunAsyncCompletionOnIOThread(callback);
|
|
|
|
}
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
void CefCookieManagerImpl::GetCookieStoreWithContext(
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreCallback& callback,
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<CefURLRequestContextGetterImpl> request_context) {
|
|
|
|
CEF_REQUIRE_IOT();
|
2016-03-16 03:55:59 +01:00
|
|
|
DCHECK(request_context->GetExistingCookieStore());
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
const CookieStoreGetter& cookie_store_getter = base::Bind(
|
|
|
|
&CefURLRequestContextGetterImpl::GetExistingCookieStore, request_context);
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
if (task_runner->BelongsToCurrentThread()) {
|
|
|
|
// Execute the callback immediately.
|
2016-03-16 03:55:59 +01:00
|
|
|
callback.Run(cookie_store_getter);
|
2015-03-02 21:25:14 +01:00
|
|
|
} else {
|
|
|
|
// Execute the callback on the target thread.
|
2016-03-16 03:55:59 +01:00
|
|
|
task_runner->PostTask(FROM_HERE, base::Bind(callback, cookie_store_getter));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 22:17:59 +02:00
|
|
|
void CefCookieManagerImpl::SetSupportedSchemesInternal(
|
2016-02-05 01:49:19 +01:00
|
|
|
const std::vector<std::string>& schemes,
|
2016-03-16 03:55:59 +01:00
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
2015-08-21 22:17:59 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
if (HasContext()) {
|
|
|
|
RunMethodWithContext(
|
|
|
|
base::Bind(&CefCookieManagerImpl::SetSupportedSchemesWithContext, this,
|
|
|
|
schemes, callback));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-15 02:40:37 +01:00
|
|
|
DCHECK(is_blocking_ || cookie_store_.get());
|
|
|
|
if (cookie_store_) {
|
|
|
|
supported_schemes_ = schemes;
|
|
|
|
SetCookieMonsterSchemes(cookie_store_.get(), supported_schemes_);
|
|
|
|
}
|
2015-08-21 22:17:59 +02:00
|
|
|
|
|
|
|
RunAsyncCompletionOnIOThread(callback);
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void CefCookieManagerImpl::VisitAllCookiesInternal(
|
|
|
|
CefRefPtr<CefCookieVisitor> visitor,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreGetter& cookie_store_getter) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store = cookie_store_getter.Run();
|
|
|
|
if (!cookie_store)
|
|
|
|
return;
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<VisitCookiesCallback> callback(
|
2016-03-16 03:55:59 +01:00
|
|
|
new VisitCookiesCallback(cookie_store_getter, visitor));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
cookie_store->GetAllCookiesAsync(
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(&VisitCookiesCallback::Run, callback.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::VisitUrlCookiesInternal(
|
|
|
|
const CefString& url,
|
|
|
|
bool includeHttpOnly,
|
|
|
|
CefRefPtr<CefCookieVisitor> visitor,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreGetter& cookie_store_getter) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store = cookie_store_getter.Run();
|
|
|
|
if (!cookie_store)
|
|
|
|
return;
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
net::CookieOptions options;
|
|
|
|
if (includeHttpOnly)
|
|
|
|
options.set_include_httponly();
|
|
|
|
|
|
|
|
scoped_refptr<VisitCookiesCallback> callback(
|
2016-03-16 03:55:59 +01:00
|
|
|
new VisitCookiesCallback(cookie_store_getter, visitor));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
GURL gurl = GURL(url.ToString());
|
2017-05-17 11:29:28 +02:00
|
|
|
cookie_store->GetCookieListWithOptionsAsync(
|
|
|
|
gurl, options, base::Bind(&VisitCookiesCallback::Run, callback.get()));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::SetCookieInternal(
|
|
|
|
const GURL& url,
|
|
|
|
const CefCookie& cookie,
|
|
|
|
CefRefPtr<CefSetCookieCallback> callback,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreGetter& cookie_store_getter) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store = cookie_store_getter.Run();
|
2018-03-15 02:40:37 +01:00
|
|
|
if (!cookie_store) {
|
|
|
|
if (callback.get()) {
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefSetCookieCallback::OnComplete,
|
|
|
|
callback.get(), false));
|
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
return;
|
2018-03-15 02:40:37 +01:00
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
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);
|
|
|
|
|
2017-12-07 22:44:24 +01:00
|
|
|
cookie_store->SetCanonicalCookieAsync(
|
|
|
|
net::CanonicalCookie::CreateSanitizedCookie(
|
|
|
|
url, 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),
|
2017-05-17 11:29:28 +02:00
|
|
|
cookie.secure ? true : false, cookie.httponly ? true : false,
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(SetCookieCallbackImpl, callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::DeleteCookiesInternal(
|
|
|
|
const GURL& url,
|
|
|
|
const CefString& cookie_name,
|
|
|
|
CefRefPtr<CefDeleteCookiesCallback> callback,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreGetter& cookie_store_getter) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store = cookie_store_getter.Run();
|
2018-03-15 02:40:37 +01:00
|
|
|
if (!cookie_store) {
|
|
|
|
if (callback.get()) {
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefDeleteCookiesCallback::OnComplete,
|
|
|
|
callback.get(), 0));
|
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
return;
|
2018-03-15 02:40:37 +01:00
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
if (url.is_empty()) {
|
|
|
|
// Delete all cookies.
|
2016-03-16 03:55:59 +01:00
|
|
|
cookie_store->DeleteAllAsync(
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(DeleteCookiesCallbackImpl, callback));
|
|
|
|
} else if (cookie_name.empty()) {
|
|
|
|
// Delete all matching host cookies.
|
2016-05-25 01:35:43 +02:00
|
|
|
cookie_store->DeleteAllCreatedBetweenWithPredicateAsync(
|
|
|
|
base::Time(), base::Time::Max(),
|
|
|
|
content::StoragePartitionImpl::CreatePredicateForHostCookies(url),
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(DeleteCookiesCallbackImpl, callback));
|
|
|
|
} else {
|
|
|
|
// Delete all matching host and domain cookies.
|
2017-05-17 11:29:28 +02:00
|
|
|
cookie_store->DeleteCookieAsync(
|
|
|
|
url, cookie_name, base::Bind(DeleteCookiesCallbackImpl, callback, -1));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefCookieManagerImpl::FlushStoreInternal(
|
|
|
|
CefRefPtr<CefCompletionCallback> callback,
|
2016-03-16 03:55:59 +01:00
|
|
|
const CookieStoreGetter& cookie_store_getter) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
net::CookieStore* cookie_store = cookie_store_getter.Run();
|
2018-03-15 02:40:37 +01:00
|
|
|
if (!cookie_store) {
|
|
|
|
RunAsyncCompletionOnIOThread(callback);
|
2016-03-16 03:55:59 +01:00
|
|
|
return;
|
2018-03-15 02:40:37 +01:00
|
|
|
}
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
cookie_store->FlushStore(base::Bind(RunAsyncCompletionOnIOThread, callback));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefCookieManager methods ----------------------------------------------------
|
|
|
|
|
|
|
|
// static
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<CefCookieManager> CefCookieManager::GetGlobalManager(
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
2012-04-03 03:34:16 +02:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
return CefRequestContext::GetGlobalContext()->GetDefaultCookieManager(
|
|
|
|
callback);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 02:40:37 +01:00
|
|
|
// static
|
|
|
|
CefRefPtr<CefCookieManager> CefCookieManager::GetBlockingManager() {
|
|
|
|
return new CefCookieManagerImpl(true);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// static
|
|
|
|
CefRefPtr<CefCookieManager> CefCookieManager::CreateManager(
|
2013-02-13 20:53:41 +01:00
|
|
|
const CefString& path,
|
2015-03-02 21:25:14 +01:00
|
|
|
bool persist_session_cookies,
|
|
|
|
CefRefPtr<CefCompletionCallback> callback) {
|
2012-04-03 03:34:16 +02:00
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
NOTREACHED() << "context not valid";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-15 02:40:37 +01:00
|
|
|
CefRefPtr<CefCookieManagerImpl> cookie_manager =
|
|
|
|
new CefCookieManagerImpl(false);
|
2015-03-02 21:25:14 +01:00
|
|
|
cookie_manager->Initialize(NULL, path, persist_session_cookies, callback);
|
|
|
|
return cookie_manager.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|