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; cef_base_t base;
/// ///
// Set the schemes supported by this manager. By default only "http" and // Set the schemes supported by this manager. The default schemes ("http",
// "https" schemes are supported. If |callback| is non-NULL it will be // "https", "ws" and "wss") will always be supported. If |callback| is non-
// executed asnychronously on the IO thread after the change has been applied. // NULL it will be executed asnychronously on the IO thread after the change
// Must be called before any cookies are accessed. // has been applied. Must be called before any cookies are accessed.
/// ///
void (CEF_CALLBACK *set_supported_schemes)(struct _cef_cookie_manager_t* self, void (CEF_CALLBACK *set_supported_schemes)(struct _cef_cookie_manager_t* self,
cef_string_list_t schemes, struct _cef_completion_callback_t* callback); 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); CefRefPtr<CefCompletionCallback> callback);
/// ///
// Set the schemes supported by this manager. By default only "http" and // Set the schemes supported by this manager. The default schemes ("http",
// "https" schemes are supported. If |callback| is non-NULL it will be // "https", "ws" and "wss") will always be supported. If |callback| is non-
// executed asnychronously on the IO thread after the change has been applied. // NULL it will be executed asnychronously on the IO thread after the change
// Must be called before any cookies are accessed. // has been applied. Must be called before any cookies are accessed.
/// ///
/*--cef(optional_param=callback)--*/ /*--cef(optional_param=callback)--*/
virtual void SetSupportedSchemes( virtual void SetSupportedSchemes(

View File

@ -174,8 +174,7 @@ CefCookieManagerImpl::GetExistingCookieMonster() {
return cookie_monster_; return cookie_monster_;
} else if (request_context_impl_.get()) { } else if (request_context_impl_.get()) {
scoped_refptr<net::CookieMonster> cookie_monster = scoped_refptr<net::CookieMonster> cookie_monster =
request_context_impl_->GetURLRequestContext()->cookie_store()-> request_context_impl_->GetCookieMonster();
GetCookieMonster();
DCHECK(cookie_monster.get()); DCHECK(cookie_monster.get());
return cookie_monster; return cookie_monster;
} }
@ -194,39 +193,12 @@ void CefCookieManagerImpl::SetSupportedSchemes(
return; 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::set<std::string> scheme_set;
std::vector<CefString>::const_iterator it = supported_schemes_.begin(); std::vector<CefString>::const_iterator it = schemes.begin();
for (; it != supported_schemes_.end(); ++it) for (; it != schemes.end(); ++it)
scheme_set.insert(*it); scheme_set.insert(*it);
const char** arr = new const char*[scheme_set.size()]; SetSupportedSchemesInternal(scheme_set, callback);
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);
} }
bool CefCookieManagerImpl::VisitAllCookies( bool CefCookieManagerImpl::VisitAllCookies(
@ -339,7 +311,7 @@ bool CefCookieManagerImpl::SetStoragePath(
storage_path_ = new_path; storage_path_ = new_path;
// Restore the previously supported schemes. // Restore the previously supported schemes.
SetSupportedSchemes(supported_schemes_, callback); SetSupportedSchemesInternal(supported_schemes_, callback);
return true; return true;
} }
@ -403,6 +375,30 @@ bool CefCookieManagerImpl::GetCefCookie(const GURL& url,
return true; 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() { bool CefCookieManagerImpl::HasContext() {
CEF_REQUIRE_IOT(); CEF_REQUIRE_IOT();
return (request_context_impl_.get() || request_context_.get()); return (request_context_impl_.get() || request_context_.get());
@ -457,17 +453,12 @@ void CefCookieManagerImpl::SetStoragePathWithContext(
} }
void CefCookieManagerImpl::SetSupportedSchemesWithContext( void CefCookieManagerImpl::SetSupportedSchemesWithContext(
const std::vector<CefString>& schemes, const std::set<std::string>& schemes,
CefRefPtr<CefCompletionCallback> callback, CefRefPtr<CefCompletionCallback> callback,
scoped_refptr<CefURLRequestContextGetterImpl> request_context) { scoped_refptr<CefURLRequestContextGetterImpl> request_context) {
CEF_REQUIRE_IOT(); CEF_REQUIRE_IOT();
std::vector<std::string> scheme_vec; request_context->SetCookieSupportedSchemes(schemes);
std::vector<CefString>::const_iterator it = schemes.begin();
for (; it != schemes.end(); ++it)
scheme_vec.push_back(it->ToString());
request_context->SetCookieSupportedSchemes(scheme_vec);
RunAsyncCompletionOnIOThread(callback); RunAsyncCompletionOnIOThread(callback);
} }
@ -479,8 +470,7 @@ void CefCookieManagerImpl::GetCookieMonsterWithContext(
CEF_REQUIRE_IOT(); CEF_REQUIRE_IOT();
scoped_refptr<net::CookieMonster> cookie_monster = scoped_refptr<net::CookieMonster> cookie_monster =
request_context->GetURLRequestContext()->cookie_store()-> request_context->GetCookieMonster();
GetCookieMonster();
if (task_runner->BelongsToCurrentThread()) { if (task_runner->BelongsToCurrentThread()) {
// Execute the callback immediately. // 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( void CefCookieManagerImpl::VisitAllCookiesInternal(
CefRefPtr<CefCookieVisitor> visitor, CefRefPtr<CefCookieVisitor> visitor,
scoped_refptr<net::CookieMonster> cookie_monster) { scoped_refptr<net::CookieMonster> cookie_monster) {

View File

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

View File

@ -10,6 +10,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "libcef/browser/cookie_manager_impl.h"
#include "libcef/browser/scheme_handler.h" #include "libcef/browser/scheme_handler.h"
#include "libcef/browser/thread_util.h" #include "libcef/browser/thread_util.h"
#include "libcef/browser/url_network_delegate.h" #include "libcef/browser/url_network_delegate.h"
@ -326,35 +327,17 @@ void CefURLRequestContextGetterImpl::SetCookieStoragePath(
cookie_store_path_ = path; cookie_store_path_ = path;
// Restore the previously supported schemes. // Restore the previously supported schemes.
SetCookieSupportedSchemes(cookie_supported_schemes_); CefCookieManagerImpl::SetCookieMonsterSchemes(cookie_monster.get(),
cookie_supported_schemes_);
} }
void CefURLRequestContextGetterImpl::SetCookieSupportedSchemes( void CefURLRequestContextGetterImpl::SetCookieSupportedSchemes(
const std::vector<std::string>& schemes) { const std::set<std::string>& schemes) {
CEF_REQUIRE_IOT(); CEF_REQUIRE_IOT();
cookie_supported_schemes_ = schemes; cookie_supported_schemes_ = schemes;
CefCookieManagerImpl::SetCookieMonsterSchemes(GetCookieMonster(),
if (cookie_supported_schemes_.empty()) { cookie_supported_schemes_);
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;
} }
void CefURLRequestContextGetterImpl::AddHandler( void CefURLRequestContextGetterImpl::AddHandler(
@ -367,6 +350,11 @@ void CefURLRequestContextGetterImpl::AddHandler(
handler_list_.push_back(handler); handler_list_.push_back(handler);
} }
net::CookieMonster* CefURLRequestContextGetterImpl::GetCookieMonster() const {
CEF_REQUIRE_IOT();
return url_request_context_->cookie_store()->GetCookieMonster();
}
void CefURLRequestContextGetterImpl::CreateProxyConfigService() { void CefURLRequestContextGetterImpl::CreateProxyConfigService() {
if (proxy_config_service_.get()) if (proxy_config_service_.get())
return; return;

View File

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

View File

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

View File

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

View File

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