Always save cookies for http, https, ws and wss schemes (issue #1684)

This commit is contained in:
Marshall Greenblatt 2015-08-21 16:17:59 -04:00
parent e6e123d503
commit aa72f402ba
9 changed files with 91 additions and 86 deletions

View File

@ -60,10 +60,10 @@ typedef struct _cef_cookie_manager_t {
cef_base_t base;
///
// Set the schemes supported by this manager. By default only "http" and
// "https" schemes are supported. If |callback| is non-NULL it will be
// executed asnychronously on the IO thread after the change has been applied.
// Must be called before any cookies are accessed.
// Set the schemes supported by this manager. The default schemes ("http",
// "https", "ws" and "wss") will always be supported. If |callback| is non-
// NULL it will be executed asnychronously on the IO thread after the change
// has been applied. Must be called before any cookies are accessed.
///
void (CEF_CALLBACK *set_supported_schemes)(struct _cef_cookie_manager_t* self,
cef_string_list_t schemes, struct _cef_completion_callback_t* callback);

View File

@ -80,10 +80,10 @@ class CefCookieManager : public virtual CefBase {
CefRefPtr<CefCompletionCallback> callback);
///
// Set the schemes supported by this manager. By default only "http" and
// "https" schemes are supported. If |callback| is non-NULL it will be
// executed asnychronously on the IO thread after the change has been applied.
// Must be called before any cookies are accessed.
// Set the schemes supported by this manager. The default schemes ("http",
// "https", "ws" and "wss") will always be supported. If |callback| is non-
// NULL it will be executed asnychronously on the IO thread after the change
// has been applied. Must be called before any cookies are accessed.
///
/*--cef(optional_param=callback)--*/
virtual void SetSupportedSchemes(

View File

@ -174,8 +174,7 @@ CefCookieManagerImpl::GetExistingCookieMonster() {
return cookie_monster_;
} else if (request_context_impl_.get()) {
scoped_refptr<net::CookieMonster> cookie_monster =
request_context_impl_->GetURLRequestContext()->cookie_store()->
GetCookieMonster();
request_context_impl_->GetCookieMonster();
DCHECK(cookie_monster.get());
return cookie_monster;
}
@ -194,39 +193,12 @@ void CefCookieManagerImpl::SetSupportedSchemes(
return;
}
if (HasContext()) {
RunMethodWithContext(
base::Bind(&CefCookieManagerImpl::SetSupportedSchemesWithContext, this,
schemes, callback));
return;
}
DCHECK(cookie_monster_.get());
if (!cookie_monster_.get())
return;
supported_schemes_ = schemes;
if (supported_schemes_.empty()) {
supported_schemes_.push_back("http");
supported_schemes_.push_back("https");
}
std::set<std::string> scheme_set;
std::vector<CefString>::const_iterator it = supported_schemes_.begin();
for (; it != supported_schemes_.end(); ++it)
std::vector<CefString>::const_iterator it = schemes.begin();
for (; it != schemes.end(); ++it)
scheme_set.insert(*it);
const char** arr = new const char*[scheme_set.size()];
std::set<std::string>::const_iterator it2 = scheme_set.begin();
for (int i = 0; it2 != scheme_set.end(); ++it2, ++i)
arr[i] = it2->c_str();
cookie_monster_->SetCookieableSchemes(arr, scheme_set.size());
delete [] arr;
RunAsyncCompletionOnIOThread(callback);
SetSupportedSchemesInternal(scheme_set, callback);
}
bool CefCookieManagerImpl::VisitAllCookies(
@ -339,7 +311,7 @@ bool CefCookieManagerImpl::SetStoragePath(
storage_path_ = new_path;
// Restore the previously supported schemes.
SetSupportedSchemes(supported_schemes_, callback);
SetSupportedSchemesInternal(supported_schemes_, callback);
return true;
}
@ -403,6 +375,30 @@ bool CefCookieManagerImpl::GetCefCookie(const GURL& url,
return true;
}
// static
void CefCookieManagerImpl::SetCookieMonsterSchemes(
net::CookieMonster* cookie_monster,
const std::set<std::string>& schemes) {
CEF_REQUIRE_IOT();
std::set<std::string> all_schemes = schemes;
// Add default schemes that should always support cookies.
all_schemes.insert("http");
all_schemes.insert("https");
all_schemes.insert("ws");
all_schemes.insert("wss");
const char** arr = new const char*[all_schemes.size()];
std::set<std::string>::const_iterator it2 = all_schemes.begin();
for (int i = 0; it2 != all_schemes.end(); ++it2, ++i)
arr[i] = it2->c_str();
cookie_monster->SetCookieableSchemes(arr, all_schemes.size());
delete [] arr;
}
bool CefCookieManagerImpl::HasContext() {
CEF_REQUIRE_IOT();
return (request_context_impl_.get() || request_context_.get());
@ -457,17 +453,12 @@ void CefCookieManagerImpl::SetStoragePathWithContext(
}
void CefCookieManagerImpl::SetSupportedSchemesWithContext(
const std::vector<CefString>& schemes,
const std::set<std::string>& schemes,
CefRefPtr<CefCompletionCallback> callback,
scoped_refptr<CefURLRequestContextGetterImpl> request_context) {
CEF_REQUIRE_IOT();
std::vector<std::string> scheme_vec;
std::vector<CefString>::const_iterator it = schemes.begin();
for (; it != schemes.end(); ++it)
scheme_vec.push_back(it->ToString());
request_context->SetCookieSupportedSchemes(scheme_vec);
request_context->SetCookieSupportedSchemes(schemes);
RunAsyncCompletionOnIOThread(callback);
}
@ -479,8 +470,7 @@ void CefCookieManagerImpl::GetCookieMonsterWithContext(
CEF_REQUIRE_IOT();
scoped_refptr<net::CookieMonster> cookie_monster =
request_context->GetURLRequestContext()->cookie_store()->
GetCookieMonster();
request_context->GetCookieMonster();
if (task_runner->BelongsToCurrentThread()) {
// Execute the callback immediately.
@ -491,6 +481,28 @@ void CefCookieManagerImpl::GetCookieMonsterWithContext(
}
}
void CefCookieManagerImpl::SetSupportedSchemesInternal(
const std::set<std::string>& schemes,
CefRefPtr<CefCompletionCallback> callback){
CEF_REQUIRE_IOT();
if (HasContext()) {
RunMethodWithContext(
base::Bind(&CefCookieManagerImpl::SetSupportedSchemesWithContext, this,
schemes, callback));
return;
}
DCHECK(cookie_monster_.get());
if (!cookie_monster_.get())
return;
supported_schemes_ = schemes;
SetCookieMonsterSchemes(cookie_monster_.get(), supported_schemes_);
RunAsyncCompletionOnIOThread(callback);
}
void CefCookieManagerImpl::VisitAllCookiesInternal(
CefRefPtr<CefCookieVisitor> visitor,
scoped_refptr<net::CookieMonster> cookie_monster) {

View File

@ -5,6 +5,8 @@
#ifndef CEF_LIBCEF_BROWSER_COOKIE_MANAGER_IMPL_H_
#define CEF_LIBCEF_BROWSER_COOKIE_MANAGER_IMPL_H_
#include <set>
#include "include/cef_cookie.h"
#include "libcef/browser/request_context_impl.h"
@ -58,6 +60,11 @@ class CefCookieManagerImpl : public CefCookieManager {
static bool GetCefCookie(const GURL& url, const std::string& cookie_line,
CefCookie& cookie);
// Set the schemes supported by |cookie_monster|. Default schemes will always
// be supported.
static void SetCookieMonsterSchemes(net::CookieMonster* cookie_monster,
const std::set<std::string>& schemes);
private:
// Returns true if a context is or will be available.
bool HasContext();
@ -75,7 +82,7 @@ class CefCookieManagerImpl : public CefCookieManager {
CefRefPtr<CefCompletionCallback> callback,
scoped_refptr<CefURLRequestContextGetterImpl> request_context);
void SetSupportedSchemesWithContext(
const std::vector<CefString>& schemes,
const std::set<std::string>& schemes,
CefRefPtr<CefCompletionCallback> callback,
scoped_refptr<CefURLRequestContextGetterImpl> request_context);
void GetCookieMonsterWithContext(
@ -83,6 +90,9 @@ class CefCookieManagerImpl : public CefCookieManager {
const CookieMonsterCallback& callback,
scoped_refptr<CefURLRequestContextGetterImpl> request_context);
void SetSupportedSchemesInternal(
const std::set<std::string>& schemes,
CefRefPtr<CefCompletionCallback> callback);
void VisitAllCookiesInternal(
CefRefPtr<CefCookieVisitor> visitor,
scoped_refptr<net::CookieMonster> cookie_monster);
@ -111,7 +121,7 @@ class CefCookieManagerImpl : public CefCookieManager {
// Used for cookie monsters owned by this object.
base::FilePath storage_path_;
std::vector<CefString> supported_schemes_;
std::set<std::string> supported_schemes_;
scoped_refptr<net::CookieMonster> cookie_monster_;
IMPLEMENT_REFCOUNTING(CefCookieManagerImpl);

View File

@ -10,6 +10,7 @@
#include <string>
#include <vector>
#include "libcef/browser/cookie_manager_impl.h"
#include "libcef/browser/scheme_handler.h"
#include "libcef/browser/thread_util.h"
#include "libcef/browser/url_network_delegate.h"
@ -326,35 +327,17 @@ void CefURLRequestContextGetterImpl::SetCookieStoragePath(
cookie_store_path_ = path;
// Restore the previously supported schemes.
SetCookieSupportedSchemes(cookie_supported_schemes_);
CefCookieManagerImpl::SetCookieMonsterSchemes(cookie_monster.get(),
cookie_supported_schemes_);
}
void CefURLRequestContextGetterImpl::SetCookieSupportedSchemes(
const std::vector<std::string>& schemes) {
const std::set<std::string>& schemes) {
CEF_REQUIRE_IOT();
cookie_supported_schemes_ = schemes;
if (cookie_supported_schemes_.empty()) {
cookie_supported_schemes_.push_back("http");
cookie_supported_schemes_.push_back("https");
}
std::set<std::string> scheme_set;
std::vector<std::string>::const_iterator it =
cookie_supported_schemes_.begin();
for (; it != cookie_supported_schemes_.end(); ++it)
scheme_set.insert(*it);
const char** arr = new const char*[scheme_set.size()];
std::set<std::string>::const_iterator it2 = scheme_set.begin();
for (int i = 0; it2 != scheme_set.end(); ++it2, ++i)
arr[i] = it2->c_str();
url_request_context_->cookie_store()->GetCookieMonster()->
SetCookieableSchemes(arr, scheme_set.size());
delete [] arr;
CefCookieManagerImpl::SetCookieMonsterSchemes(GetCookieMonster(),
cookie_supported_schemes_);
}
void CefURLRequestContextGetterImpl::AddHandler(
@ -367,6 +350,11 @@ void CefURLRequestContextGetterImpl::AddHandler(
handler_list_.push_back(handler);
}
net::CookieMonster* CefURLRequestContextGetterImpl::GetCookieMonster() const {
CEF_REQUIRE_IOT();
return url_request_context_->cookie_store()->GetCookieMonster();
}
void CefURLRequestContextGetterImpl::CreateProxyConfigService() {
if (proxy_config_service_.get())
return;

View File

@ -6,8 +6,8 @@
#define CEF_LIBCEF_BROWSER_URL_REQUEST_CONTEXT_GETTER_IMPL_H_
#pragma once
#include <set>
#include <string>
#include <vector>
#include "include/internal/cef_types_wrappers.h"
#include "libcef/browser/url_request_context_getter.h"
@ -26,6 +26,7 @@ class MessageLoop;
}
namespace net {
class CookieMonster;
class FtpTransactionFactory;
class ProxyConfigService;
class URLRequestContextStorage;
@ -59,12 +60,14 @@ class CefURLRequestContextGetterImpl : public CefURLRequestContextGetter {
void SetCookieStoragePath(const base::FilePath& path,
bool persist_session_cookies);
void SetCookieSupportedSchemes(const std::vector<std::string>& schemes);
void SetCookieSupportedSchemes(const std::set<std::string>& schemes);
// Keep a reference to all handlers sharing this context so that they'll be
// kept alive until the context is destroyed.
void AddHandler(CefRefPtr<CefRequestContextHandler> handler);
net::CookieMonster* GetCookieMonster() const;
CefURLRequestManager* request_manager() const {
return url_request_manager_.get();
}
@ -87,7 +90,7 @@ class CefURLRequestContextGetterImpl : public CefURLRequestContextGetter {
content::URLRequestInterceptorScopedVector request_interceptors_;
base::FilePath cookie_store_path_;
std::vector<std::string> cookie_supported_schemes_;
std::set<std::string> cookie_supported_schemes_;
std::vector<CefRefPtr<CefRequestContextHandler> > handler_list_;

View File

@ -43,10 +43,6 @@ ClientApp::ProcessType ClientApp::GetProcessType(
void ClientApp::OnRegisterCustomSchemes(
CefRefPtr<CefSchemeRegistrar> registrar) {
// Default schemes that support cookies.
cookieable_schemes_.push_back("http");
cookieable_schemes_.push_back("https");
RegisterCustomSchemes(registrar, cookieable_schemes_);
}

View File

@ -1083,8 +1083,6 @@ class CookieTestSchemeHandler : public TestHandler {
if (scheme_ != "http") {
std::vector<CefString> schemes;
schemes.push_back("http");
schemes.push_back("https");
schemes.push_back(scheme_);
manager1_->SetSupportedSchemes(schemes, NULL);

View File

@ -1003,8 +1003,6 @@ class RequestTestHandler : public TestHandler,
// Set the schemes that are allowed to store cookies.
std::vector<CefString> supported_schemes;
supported_schemes.push_back("http");
supported_schemes.push_back("https");
supported_schemes.push_back(kRequestScheme);
// Continue the test once supported schemes has been set.