From a038925a7703c081b6724976308543267968eec2 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Wed, 13 Feb 2013 19:54:19 +0000 Subject: [PATCH] Merge revision 1098 changes: - Add the ability to persist session cookies (issue #881). git-svn-id: https://chromiumembedded.googlecode.com/svn/branches/1364@1099 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- cef3/cef_paths.gypi | 4 + cef3/include/capi/cef_callback_capi.h | 16 +++ cef3/include/capi/cef_cookie_capi.h | 26 +++- cef3/include/cef_callback.h | 13 ++ cef3/include/cef_cookie.h | 29 +++- cef3/include/internal/cef_types.h | 10 ++ cef3/include/internal/cef_types_wrappers.h | 2 + cef3/libcef/browser/cookie_manager_impl.cc | 57 ++++++-- cef3/libcef/browser/cookie_manager_impl.h | 7 +- .../browser/url_request_context_getter.cc | 22 ++- .../browser/url_request_context_getter.h | 3 +- cef3/libcef/common/cef_switches.cc | 3 + cef3/libcef/common/cef_switches.h | 1 + .../cpptoc/completion_handler_cpptoc.cc | 44 ++++++ .../cpptoc/completion_handler_cpptoc.h | 37 +++++ .../cpptoc/cookie_manager_cpptoc.cc | 30 +++- .../ctocpp/completion_handler_ctocpp.cc | 33 +++++ .../ctocpp/completion_handler_ctocpp.h | 42 ++++++ .../ctocpp/cookie_manager_ctocpp.cc | 29 +++- .../libcef_dll/ctocpp/cookie_manager_ctocpp.h | 4 +- cef3/libcef_dll/libcef_dll.cc | 2 + cef3/libcef_dll/wrapper/libcef_dll_wrapper.cc | 2 + cef3/tests/unittests/cookie_unittest.cc | 135 ++++++++++++++---- 23 files changed, 489 insertions(+), 62 deletions(-) create mode 100644 cef3/libcef_dll/cpptoc/completion_handler_cpptoc.cc create mode 100644 cef3/libcef_dll/cpptoc/completion_handler_cpptoc.h create mode 100644 cef3/libcef_dll/ctocpp/completion_handler_ctocpp.cc create mode 100644 cef3/libcef_dll/ctocpp/completion_handler_ctocpp.h diff --git a/cef3/cef_paths.gypi b/cef3/cef_paths.gypi index f3f282437..2f0745995 100644 --- a/cef3/cef_paths.gypi +++ b/cef3/cef_paths.gypi @@ -126,6 +126,8 @@ 'libcef_dll/ctocpp/client_ctocpp.h', 'libcef_dll/cpptoc/command_line_cpptoc.cc', 'libcef_dll/cpptoc/command_line_cpptoc.h', + 'libcef_dll/ctocpp/completion_handler_ctocpp.cc', + 'libcef_dll/ctocpp/completion_handler_ctocpp.h', 'libcef_dll/ctocpp/context_menu_handler_ctocpp.cc', 'libcef_dll/ctocpp/context_menu_handler_ctocpp.h', 'libcef_dll/cpptoc/context_menu_params_cpptoc.cc', @@ -276,6 +278,8 @@ 'libcef_dll/cpptoc/client_cpptoc.h', 'libcef_dll/ctocpp/command_line_ctocpp.cc', 'libcef_dll/ctocpp/command_line_ctocpp.h', + 'libcef_dll/cpptoc/completion_handler_cpptoc.cc', + 'libcef_dll/cpptoc/completion_handler_cpptoc.h', 'libcef_dll/cpptoc/context_menu_handler_cpptoc.cc', 'libcef_dll/cpptoc/context_menu_handler_cpptoc.h', 'libcef_dll/ctocpp/context_menu_params_ctocpp.cc', diff --git a/cef3/include/capi/cef_callback_capi.h b/cef3/include/capi/cef_callback_capi.h index a6731efd7..4220fc8b7 100644 --- a/cef3/include/capi/cef_callback_capi.h +++ b/cef3/include/capi/cef_callback_capi.h @@ -66,6 +66,22 @@ typedef struct _cef_callback_t { } cef_callback_t; +/// +// Generic callback structure used for asynchronous completion. +/// +typedef struct _cef_completion_handler_t { + /// + // Base structure. + /// + cef_base_t base; + + /// + // Method that will be called once the task is complete. + /// + void (CEF_CALLBACK *on_complete)(struct _cef_completion_handler_t* self); +} cef_completion_handler_t; + + #ifdef __cplusplus } #endif diff --git a/cef3/include/capi/cef_cookie_capi.h b/cef3/include/capi/cef_cookie_capi.h index de795e921..14470be0e 100644 --- a/cef3/include/capi/cef_cookie_capi.h +++ b/cef3/include/capi/cef_cookie_capi.h @@ -106,11 +106,23 @@ typedef struct _cef_cookie_manager_t { /// // Sets the directory path that will be used for storing cookie data. If - // |path| is NULL data will be stored in memory only. Returns false (0) if - // cookies cannot be accessed. + // |path| is NULL data will be stored in memory only. Otherwise, data will be + // stored at the specified |path|. To persist session cookies (cookies without + // an expiry date or validity interval) set |persist_session_cookies| to true + // (1). Session cookies are generally intended to be transient and most Web + // browsers do not persist them. Returns false (0) if cookies cannot be + // accessed. /// int (CEF_CALLBACK *set_storage_path)(struct _cef_cookie_manager_t* self, - const cef_string_t* path); + const cef_string_t* path, int persist_session_cookies); + + /// + // Flush the backing store (if any) to disk and execute the specified + // |handler| on the IO thread when done. Returns false (0) if cookies cannot + // be accessed. + /// + int (CEF_CALLBACK *flush_store)(struct _cef_cookie_manager_t* self, + struct _cef_completion_handler_t* handler); } cef_cookie_manager_t; @@ -122,10 +134,14 @@ CEF_EXPORT cef_cookie_manager_t* cef_cookie_manager_get_global_manager(); /// // Creates a new cookie manager. If |path| is NULL data will be stored in memory -// only. Returns NULL if creation fails. +// only. Otherwise, data will be stored at the specified |path|. To persist +// session cookies (cookies without an expiry date or validity interval) set +// |persist_session_cookies| to true (1). Session cookies are generally intended +// to be transient and most Web browsers do not persist them. Returns NULL if +// creation fails. /// CEF_EXPORT cef_cookie_manager_t* cef_cookie_manager_create_manager( - const cef_string_t* path); + const cef_string_t* path, int persist_session_cookies); /// diff --git a/cef3/include/cef_callback.h b/cef3/include/cef_callback.h index 34f67a6c6..9b4e5321e 100644 --- a/cef3/include/cef_callback.h +++ b/cef3/include/cef_callback.h @@ -59,4 +59,17 @@ class CefCallback : public virtual CefBase { virtual void Cancel() =0; }; +/// +// Generic callback interface used for asynchronous completion. +/// +/*--cef(source=client)--*/ +class CefCompletionHandler : public virtual CefBase { + public: + /// + // Method that will be called once the task is complete. + /// + /*--cef()--*/ + virtual void OnComplete() =0; +}; + #endif // CEF_INCLUDE_CEF_CALLBACK_H_ diff --git a/cef3/include/cef_cookie.h b/cef3/include/cef_cookie.h index d24325c39..8a7267d56 100644 --- a/cef3/include/cef_cookie.h +++ b/cef3/include/cef_cookie.h @@ -39,6 +39,7 @@ #pragma once #include "include/cef_base.h" +#include "include/cef_callback.h" #include class CefCookieVisitor; @@ -60,10 +61,16 @@ class CefCookieManager : public virtual CefBase { /// // Creates a new cookie manager. If |path| is empty data will be stored in - // memory only. Returns NULL if creation fails. + // memory only. Otherwise, data will be stored at the specified |path|. To + // persist session cookies (cookies without an expiry date or validity + // interval) set |persist_session_cookies| to true. Session cookies are + // generally intended to be transient and most Web browsers do not persist + // them. Returns NULL if creation fails. /// /*--cef(optional_param=path)--*/ - static CefRefPtr CreateManager(const CefString& path); + static CefRefPtr CreateManager( + const CefString& path, + bool persist_session_cookies); /// // Set the schemes supported by this manager. By default only "http" and @@ -117,11 +124,23 @@ class CefCookieManager : public virtual CefBase { /// // Sets the directory path that will be used for storing cookie data. If - // |path| is empty data will be stored in memory only. Returns false if - // cookies cannot be accessed. + // |path| is empty data will be stored in memory only. Otherwise, data will be + // stored at the specified |path|. To persist session cookies (cookies without + // an expiry date or validity interval) set |persist_session_cookies| to true. + // Session cookies are generally intended to be transient and most Web browsers + // do not persist them. Returns false if cookies cannot be accessed. /// /*--cef(optional_param=path)--*/ - virtual bool SetStoragePath(const CefString& path) =0; + virtual bool SetStoragePath(const CefString& path, + bool persist_session_cookies) =0; + + /// + // Flush the backing store (if any) to disk and execute the specified + // |handler| on the IO thread when done. Returns false if cookies cannot be + // accessed. + /// + /*--cef(optional_param=handler)--*/ + virtual bool FlushStore(CefRefPtr handler) =0; }; diff --git a/cef3/include/internal/cef_types.h b/cef3/include/internal/cef_types.h index beb02d285..a2a839c72 100644 --- a/cef3/include/internal/cef_types.h +++ b/cef3/include/internal/cef_types.h @@ -197,6 +197,16 @@ typedef struct _cef_settings_t { /// cef_string_t cache_path; + /// + // To persist session cookies (cookies without an expiry date or validity + // interval) by default when using the global cookie manager set this value to + // true. Session cookies are generally intended to be transient and most Web + // browsers do not persist them. A |cache_path| value must also be specified to + // enable this feature. Also configurable using the "persist-session-cookies" + // command-line switch. + /// + bool persist_session_cookies; + /// // Value that will be returned as the User-Agent HTTP header. If empty the // default User-Agent string will be used. Also configurable using the diff --git a/cef3/include/internal/cef_types_wrappers.h b/cef3/include/internal/cef_types_wrappers.h index 80dfa873b..aeae20f78 100644 --- a/cef3/include/internal/cef_types_wrappers.h +++ b/cef3/include/internal/cef_types_wrappers.h @@ -298,6 +298,8 @@ struct CefSettingsTraits { cef_string_set(src->cache_path.str, src->cache_path.length, &target->cache_path, copy); + target->persist_session_cookies = src->persist_session_cookies; + cef_string_set(src->user_agent.str, src->user_agent.length, &target->user_agent, copy); cef_string_set(src->product_version.str, src->product_version.length, diff --git a/cef3/libcef/browser/cookie_manager_impl.cc b/cef3/libcef/browser/cookie_manager_impl.cc index 51da7b52c..2cf1e0a95 100644 --- a/cef3/libcef/browser/cookie_manager_impl.cc +++ b/cef3/libcef/browser/cookie_manager_impl.cc @@ -77,6 +77,11 @@ bool GetCookieDomain(const GURL& url, result); } +void RunCompletionOnIOThread(CefRefPtr handler) { + CEF_POST_TASK(CEF_IOT, + base::Bind(&CefCompletionHandler::OnComplete, handler.get())); +} + } // namespace @@ -87,11 +92,13 @@ CefCookieManagerImpl::CefCookieManagerImpl(bool is_global) CefCookieManagerImpl::~CefCookieManagerImpl() { } -void CefCookieManagerImpl::Initialize(const CefString& path) { +void CefCookieManagerImpl::Initialize( + const CefString& path, + bool persist_session_cookies) { if (is_global_) SetGlobal(); else - SetStoragePath(path); + SetStoragePath(path, persist_session_cookies); } void CefCookieManagerImpl::SetSupportedSchemes( @@ -245,7 +252,9 @@ bool CefCookieManagerImpl::DeleteCookies(const CefString& url, return true; } -bool CefCookieManagerImpl::SetStoragePath(const CefString& path) { +bool CefCookieManagerImpl::SetStoragePath( + const CefString& path, + bool persist_session_cookies) { if (CEF_CURRENTLY_ON_IOT()) { FilePath new_path; if (!path.empty()) @@ -256,7 +265,7 @@ bool CefCookieManagerImpl::SetStoragePath(const CefString& path) { CefURLRequestContextGetter* getter = static_cast( _Context->browser_context()->GetRequestContext()); - getter->SetCookieStoragePath(new_path); + getter->SetCookieStoragePath(new_path, persist_session_cookies); cookie_monster_ = getter->GetURLRequestContext()->cookie_store()-> GetCookieMonster(); return true; @@ -277,7 +286,9 @@ bool CefCookieManagerImpl::SetStoragePath(const CefString& path) { file_util::CreateDirectory(new_path)) { const FilePath& cookie_path = new_path.AppendASCII("Cookies"); persistent_store = - new SQLitePersistentCookieStore(cookie_path, false, NULL); + new SQLitePersistentCookieStore(cookie_path, + persist_session_cookies, + NULL); } else { NOTREACHED() << "The cookie storage directory could not be created"; storage_path_.clear(); @@ -288,6 +299,8 @@ bool CefCookieManagerImpl::SetStoragePath(const CefString& path) { // cookie store, if any, will be automatically flushed and closed when no // longer referenced. cookie_monster_ = new net::CookieMonster(persistent_store.get(), NULL); + if (persistent_store.get() && persist_session_cookies) + cookie_monster_->SetPersistSessionCookies(true); storage_path_ = new_path; // Restore the previously supported schemes. @@ -296,7 +309,32 @@ bool CefCookieManagerImpl::SetStoragePath(const CefString& path) { // Execute on the IO thread. CEF_POST_TASK(CEF_IOT, base::Bind(base::IgnoreResult(&CefCookieManagerImpl::SetStoragePath), - this, path)); + this, path, persist_session_cookies)); + } + + return true; +} + +bool CefCookieManagerImpl::FlushStore(CefRefPtr handler) { + if (CEF_CURRENTLY_ON_IOT()) { + if (!cookie_monster_) { + if (handler.get()) + RunCompletionOnIOThread(handler); + return true; + } + + base::Closure callback; + if (handler.get()) + callback = base::Bind(RunCompletionOnIOThread, handler); + else + callback = base::Bind(&base::DoNothing); + + cookie_monster_->FlushStore(callback); + } else { + // Execute on the IO thread. + CEF_POST_TASK(CEF_IOT, + base::Bind(base::IgnoreResult(&CefCookieManagerImpl::FlushStore), + this, handler)); } return true; @@ -378,13 +416,14 @@ CefRefPtr CefCookieManager::GetGlobalManager() { } CefRefPtr manager(new CefCookieManagerImpl(true)); - manager->Initialize(CefString()); + manager->Initialize(CefString(), false); return manager.get(); } // static CefRefPtr CefCookieManager::CreateManager( - const CefString& path) { + const CefString& path, + bool persist_session_cookies) { // Verify that the context is in a valid state. if (!CONTEXT_STATE_VALID()) { NOTREACHED() << "context not valid"; @@ -392,6 +431,6 @@ CefRefPtr CefCookieManager::CreateManager( } CefRefPtr manager(new CefCookieManagerImpl(false)); - manager->Initialize(path); + manager->Initialize(path, persist_session_cookies); return manager.get(); } diff --git a/cef3/libcef/browser/cookie_manager_impl.h b/cef3/libcef/browser/cookie_manager_impl.h index c0858a9f0..b3f60f005 100644 --- a/cef3/libcef/browser/cookie_manager_impl.h +++ b/cef3/libcef/browser/cookie_manager_impl.h @@ -16,7 +16,8 @@ class CefCookieManagerImpl : public CefCookieManager { ~CefCookieManagerImpl(); // Initialize the cookie manager. - void Initialize(const CefString& path); + void Initialize(const CefString& path, + bool persist_session_cookies); // CefCookieManager methods. virtual void SetSupportedSchemes(const std::vector& schemes) @@ -28,7 +29,9 @@ class CefCookieManagerImpl : public CefCookieManager { const CefCookie& cookie) OVERRIDE; virtual bool DeleteCookies(const CefString& url, const CefString& cookie_name) OVERRIDE; - virtual bool SetStoragePath(const CefString& path) OVERRIDE; + virtual bool SetStoragePath(const CefString& path, + bool persist_session_cookies) OVERRIDE; + virtual bool FlushStore(CefRefPtr handler) OVERRIDE; net::CookieMonster* cookie_monster() { return cookie_monster_; } diff --git a/cef3/libcef/browser/url_request_context_getter.cc b/cef3/libcef/browser/url_request_context_getter.cc index 8b9af3fea..98c561938 100644 --- a/cef3/libcef/browser/url_request_context_getter.cc +++ b/cef3/libcef/browser/url_request_context_getter.cc @@ -15,6 +15,7 @@ #include "libcef/browser/url_network_delegate.h" #include "libcef/browser/url_request_context_proxy.h" #include "libcef/browser/url_request_interceptor.h" +#include "libcef/common/cef_switches.h" #include "base/command_line.h" #include "base/file_util.h" @@ -75,12 +76,16 @@ net::URLRequestContext* CefURLRequestContextGetter::GetURLRequestContext() { if (!url_request_context_.get()) { const FilePath& cache_path = _Context->cache_path(); const CommandLine& command_line = *CommandLine::ForCurrentProcess(); + const CefSettings& settings = _Context->settings(); url_request_context_.reset(new net::URLRequestContext()); storage_.reset( new net::URLRequestContextStorage(url_request_context_.get())); - SetCookieStoragePath(cache_path); + bool persist_session_cookies = + (settings.persist_session_cookies || + command_line.HasSwitch(switches::kPersistSessionCookies)); + SetCookieStoragePath(cache_path, persist_session_cookies); storage_->set_network_delegate(new CefNetworkDelegate); @@ -176,7 +181,9 @@ net::HostResolver* CefURLRequestContextGetter::host_resolver() { return url_request_context_->host_resolver(); } -void CefURLRequestContextGetter::SetCookieStoragePath(const FilePath& path) { +void CefURLRequestContextGetter::SetCookieStoragePath( + const FilePath& path, + bool persist_session_cookies) { CEF_REQUIRE_IOT(); if (url_request_context_->cookie_store() && @@ -195,7 +202,9 @@ void CefURLRequestContextGetter::SetCookieStoragePath(const FilePath& path) { file_util::CreateDirectory(path)) { const FilePath& cookie_path = path.AppendASCII("Cookies"); persistent_store = - new SQLitePersistentCookieStore(cookie_path, false, NULL); + new SQLitePersistentCookieStore(cookie_path, + persist_session_cookies, + NULL); } else { NOTREACHED() << "The cookie storage directory could not be created"; } @@ -204,8 +213,11 @@ void CefURLRequestContextGetter::SetCookieStoragePath(const FilePath& path) { // 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. - storage_->set_cookie_store( - new net::CookieMonster(persistent_store.get(), NULL)); + scoped_refptr cookie_monster = + new net::CookieMonster(persistent_store.get(), NULL); + storage_->set_cookie_store(cookie_monster); + if (persistent_store.get() && persist_session_cookies) + cookie_monster->SetPersistSessionCookies(true); cookie_store_path_ = path; // Restore the previously supported schemes. diff --git a/cef3/libcef/browser/url_request_context_getter.h b/cef3/libcef/browser/url_request_context_getter.h index 4aeb604b3..33e402801 100644 --- a/cef3/libcef/browser/url_request_context_getter.h +++ b/cef3/libcef/browser/url_request_context_getter.h @@ -85,7 +85,8 @@ class CefURLRequestContextGetter : public net::URLRequestContextGetter { net::HostResolver* host_resolver(); - void SetCookieStoragePath(const FilePath& path); + void SetCookieStoragePath(const FilePath& path, + bool persist_session_cookies); void SetCookieSupportedSchemes(const std::vector& schemes); // Manage URLRequestContext proxy objects. It's important that proxy objects diff --git a/cef3/libcef/common/cef_switches.cc b/cef3/libcef/common/cef_switches.cc index fe9be8633..422124b21 100644 --- a/cef3/libcef/common/cef_switches.cc +++ b/cef3/libcef/common/cef_switches.cc @@ -83,4 +83,7 @@ const char kDisableAuthorAndUserStyles[] = "disable-author-and-user-styles"; // Disable developer tools (WebKit Inspector). const char kDisableDeveloperTools[] = "disable-developer-tools"; +// Persist session cookies. +const char kPersistSessionCookies[] = "persist-session-cookies"; + } // namespace switches diff --git a/cef3/libcef/common/cef_switches.h b/cef3/libcef/common/cef_switches.h index d84939ff1..4bc0cb654 100644 --- a/cef3/libcef/common/cef_switches.h +++ b/cef3/libcef/common/cef_switches.h @@ -38,6 +38,7 @@ extern const char kDisableTextAreaResize[]; extern const char kDisableTabToLinks[]; extern const char kDisableAuthorAndUserStyles[]; extern const char kDisableDeveloperTools[]; +extern const char kPersistSessionCookies[]; } // namespace switches diff --git a/cef3/libcef_dll/cpptoc/completion_handler_cpptoc.cc b/cef3/libcef_dll/cpptoc/completion_handler_cpptoc.cc new file mode 100644 index 000000000..249135339 --- /dev/null +++ b/cef3/libcef_dll/cpptoc/completion_handler_cpptoc.cc @@ -0,0 +1,44 @@ +// Copyright (c) 2013 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. +// +// --------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool. If making changes by +// hand only do so within the body of existing method and function +// implementations. See the translator.README.txt file in the tools directory +// for more information. +// + +#include "libcef_dll/cpptoc/completion_handler_cpptoc.h" + + +// MEMBER FUNCTIONS - Body may be edited by hand. + +void CEF_CALLBACK completion_handler_on_complete( + struct _cef_completion_handler_t* self) { + // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING + + DCHECK(self); + if (!self) + return; + + // Execute + CefCompletionHandlerCppToC::Get(self)->OnComplete(); +} + + +// CONSTRUCTOR - Do not edit by hand. + +CefCompletionHandlerCppToC::CefCompletionHandlerCppToC( + CefCompletionHandler* cls) + : CefCppToC(cls) { + struct_.struct_.on_complete = completion_handler_on_complete; +} + +#ifndef NDEBUG +template<> long CefCppToC::DebugObjCt = 0; +#endif + diff --git a/cef3/libcef_dll/cpptoc/completion_handler_cpptoc.h b/cef3/libcef_dll/cpptoc/completion_handler_cpptoc.h new file mode 100644 index 000000000..7d0e440f8 --- /dev/null +++ b/cef3/libcef_dll/cpptoc/completion_handler_cpptoc.h @@ -0,0 +1,37 @@ +// Copyright (c) 2013 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. +// +// --------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool. If making changes by +// hand only do so within the body of existing method and function +// implementations. See the translator.README.txt file in the tools directory +// for more information. +// + +#ifndef CEF_LIBCEF_DLL_CPPTOC_COMPLETION_HANDLER_CPPTOC_H_ +#define CEF_LIBCEF_DLL_CPPTOC_COMPLETION_HANDLER_CPPTOC_H_ +#pragma once + +#ifndef USING_CEF_SHARED +#pragma message("Warning: "__FILE__" may be accessed wrapper-side only") +#else // USING_CEF_SHARED + +#include "include/cef_callback.h" +#include "include/capi/cef_callback_capi.h" +#include "libcef_dll/cpptoc/cpptoc.h" + +// Wrap a C++ class with a C structure. +// This class may be instantiated and accessed wrapper-side only. +class CefCompletionHandlerCppToC + : public CefCppToC { + public: + explicit CefCompletionHandlerCppToC(CefCompletionHandler* cls); + virtual ~CefCompletionHandlerCppToC() {} +}; + +#endif // USING_CEF_SHARED +#endif // CEF_LIBCEF_DLL_CPPTOC_COMPLETION_HANDLER_CPPTOC_H_ + diff --git a/cef3/libcef_dll/cpptoc/cookie_manager_cpptoc.cc b/cef3/libcef_dll/cpptoc/cookie_manager_cpptoc.cc index 042bdd47a..acedc3f1d 100644 --- a/cef3/libcef_dll/cpptoc/cookie_manager_cpptoc.cc +++ b/cef3/libcef_dll/cpptoc/cookie_manager_cpptoc.cc @@ -11,6 +11,7 @@ // #include "libcef_dll/cpptoc/cookie_manager_cpptoc.h" +#include "libcef_dll/ctocpp/completion_handler_ctocpp.h" #include "libcef_dll/ctocpp/cookie_visitor_ctocpp.h" #include "libcef_dll/transfer_util.h" @@ -28,14 +29,15 @@ CEF_EXPORT cef_cookie_manager_t* cef_cookie_manager_get_global_manager() { } CEF_EXPORT cef_cookie_manager_t* cef_cookie_manager_create_manager( - const cef_string_t* path) { + const cef_string_t* path, int persist_session_cookies) { // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING // Unverified params: path // Execute CefRefPtr _retval = CefCookieManager::CreateManager( - CefString(path)); + CefString(path), + persist_session_cookies?true:false); // Return type: refptr_same return CefCookieManagerCppToC::Wrap(_retval); @@ -163,7 +165,8 @@ int CEF_CALLBACK cookie_manager_delete_cookies( } int CEF_CALLBACK cookie_manager_set_storage_path( - struct _cef_cookie_manager_t* self, const cef_string_t* path) { + struct _cef_cookie_manager_t* self, const cef_string_t* path, + int persist_session_cookies) { // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING DCHECK(self); @@ -173,7 +176,25 @@ int CEF_CALLBACK cookie_manager_set_storage_path( // Execute bool _retval = CefCookieManagerCppToC::Get(self)->SetStoragePath( - CefString(path)); + CefString(path), + persist_session_cookies?true:false); + + // Return type: bool + return _retval; +} + +int CEF_CALLBACK cookie_manager_flush_store(struct _cef_cookie_manager_t* self, + cef_completion_handler_t* handler) { + // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING + + DCHECK(self); + if (!self) + return 0; + // Unverified params: handler + + // Execute + bool _retval = CefCookieManagerCppToC::Get(self)->FlushStore( + CefCompletionHandlerCToCpp::Wrap(handler)); // Return type: bool return _retval; @@ -191,6 +212,7 @@ CefCookieManagerCppToC::CefCookieManagerCppToC(CefCookieManager* cls) struct_.struct_.set_cookie = cookie_manager_set_cookie; struct_.struct_.delete_cookies = cookie_manager_delete_cookies; struct_.struct_.set_storage_path = cookie_manager_set_storage_path; + struct_.struct_.flush_store = cookie_manager_flush_store; } #ifndef NDEBUG diff --git a/cef3/libcef_dll/ctocpp/completion_handler_ctocpp.cc b/cef3/libcef_dll/ctocpp/completion_handler_ctocpp.cc new file mode 100644 index 000000000..e5964a8d7 --- /dev/null +++ b/cef3/libcef_dll/ctocpp/completion_handler_ctocpp.cc @@ -0,0 +1,33 @@ +// Copyright (c) 2013 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. +// +// --------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool. If making changes by +// hand only do so within the body of existing method and function +// implementations. See the translator.README.txt file in the tools directory +// for more information. +// + +#include "libcef_dll/ctocpp/completion_handler_ctocpp.h" + + +// VIRTUAL METHODS - Body may be edited by hand. + +void CefCompletionHandlerCToCpp::OnComplete() { + if (CEF_MEMBER_MISSING(struct_, on_complete)) + return; + + // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING + + // Execute + struct_->on_complete(struct_); +} + + +#ifndef NDEBUG +template<> long CefCToCpp::DebugObjCt = 0; +#endif + diff --git a/cef3/libcef_dll/ctocpp/completion_handler_ctocpp.h b/cef3/libcef_dll/ctocpp/completion_handler_ctocpp.h new file mode 100644 index 000000000..b744ce6c9 --- /dev/null +++ b/cef3/libcef_dll/ctocpp/completion_handler_ctocpp.h @@ -0,0 +1,42 @@ +// Copyright (c) 2013 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. +// +// --------------------------------------------------------------------------- +// +// This file was generated by the CEF translator tool. If making changes by +// hand only do so within the body of existing method and function +// implementations. See the translator.README.txt file in the tools directory +// for more information. +// + +#ifndef CEF_LIBCEF_DLL_CTOCPP_COMPLETION_HANDLER_CTOCPP_H_ +#define CEF_LIBCEF_DLL_CTOCPP_COMPLETION_HANDLER_CTOCPP_H_ +#pragma once + +#ifndef BUILDING_CEF_SHARED +#pragma message("Warning: "__FILE__" may be accessed DLL-side only") +#else // BUILDING_CEF_SHARED + +#include "include/cef_callback.h" +#include "include/capi/cef_callback_capi.h" +#include "libcef_dll/ctocpp/ctocpp.h" + +// Wrap a C structure with a C++ class. +// This class may be instantiated and accessed DLL-side only. +class CefCompletionHandlerCToCpp + : public CefCToCpp { + public: + explicit CefCompletionHandlerCToCpp(cef_completion_handler_t* str) + : CefCToCpp(str) {} + virtual ~CefCompletionHandlerCToCpp() {} + + // CefCompletionHandler methods + virtual void OnComplete() OVERRIDE; +}; + +#endif // BUILDING_CEF_SHARED +#endif // CEF_LIBCEF_DLL_CTOCPP_COMPLETION_HANDLER_CTOCPP_H_ + diff --git a/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.cc b/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.cc index 4db768d76..a12a459b1 100644 --- a/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.cc +++ b/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.cc @@ -10,6 +10,7 @@ // for more information. // +#include "libcef_dll/cpptoc/completion_handler_cpptoc.h" #include "libcef_dll/cpptoc/cookie_visitor_cpptoc.h" #include "libcef_dll/ctocpp/cookie_manager_ctocpp.h" #include "libcef_dll/transfer_util.h" @@ -28,14 +29,15 @@ CefRefPtr CefCookieManager::GetGlobalManager() { } CefRefPtr CefCookieManager::CreateManager( - const CefString& path) { + const CefString& path, bool persist_session_cookies) { // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING // Unverified params: path // Execute cef_cookie_manager_t* _retval = cef_cookie_manager_create_manager( - path.GetStruct()); + path.GetStruct(), + persist_session_cookies); // Return type: refptr_same return CefCookieManagerCToCpp::Wrap(_retval); @@ -151,7 +153,8 @@ bool CefCookieManagerCToCpp::DeleteCookies(const CefString& url, return _retval?true:false; } -bool CefCookieManagerCToCpp::SetStoragePath(const CefString& path) { +bool CefCookieManagerCToCpp::SetStoragePath(const CefString& path, + bool persist_session_cookies) { if (CEF_MEMBER_MISSING(struct_, set_storage_path)) return false; @@ -161,7 +164,25 @@ bool CefCookieManagerCToCpp::SetStoragePath(const CefString& path) { // Execute int _retval = struct_->set_storage_path(struct_, - path.GetStruct()); + path.GetStruct(), + persist_session_cookies); + + // Return type: bool + return _retval?true:false; +} + +bool CefCookieManagerCToCpp::FlushStore( + CefRefPtr handler) { + if (CEF_MEMBER_MISSING(struct_, flush_store)) + return false; + + // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING + + // Unverified params: handler + + // Execute + int _retval = struct_->flush_store(struct_, + CefCompletionHandlerCppToC::Wrap(handler)); // Return type: bool return _retval?true:false; diff --git a/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.h b/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.h index 760021ba0..cadd19f4c 100644 --- a/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.h +++ b/cef3/libcef_dll/ctocpp/cookie_manager_ctocpp.h @@ -44,7 +44,9 @@ class CefCookieManagerCToCpp const CefCookie& cookie) OVERRIDE; virtual bool DeleteCookies(const CefString& url, const CefString& cookie_name) OVERRIDE; - virtual bool SetStoragePath(const CefString& path) OVERRIDE; + virtual bool SetStoragePath(const CefString& path, + bool persist_session_cookies) OVERRIDE; + virtual bool FlushStore(CefRefPtr handler) OVERRIDE; }; #endif // USING_CEF_SHARED diff --git a/cef3/libcef_dll/libcef_dll.cc b/cef3/libcef_dll/libcef_dll.cc index 23316f080..f84b73100 100644 --- a/cef3/libcef_dll/libcef_dll.cc +++ b/cef3/libcef_dll/libcef_dll.cc @@ -70,6 +70,7 @@ #include "libcef_dll/cpptoc/zip_reader_cpptoc.h" #include "libcef_dll/ctocpp/app_ctocpp.h" #include "libcef_dll/ctocpp/browser_process_handler_ctocpp.h" +#include "libcef_dll/ctocpp/completion_handler_ctocpp.h" #include "libcef_dll/ctocpp/context_menu_handler_ctocpp.h" #include "libcef_dll/ctocpp/cookie_visitor_ctocpp.h" #include "libcef_dll/ctocpp/domevent_listener_ctocpp.h" @@ -177,6 +178,7 @@ CEF_EXPORT void cef_shutdown() { DCHECK_EQ(CefBrowserHostCppToC::DebugObjCt, 0); DCHECK_EQ(CefBrowserProcessHandlerCToCpp::DebugObjCt, 0); DCHECK_EQ(CefCallbackCppToC::DebugObjCt, 0); + DCHECK_EQ(CefCompletionHandlerCToCpp::DebugObjCt, 0); DCHECK_EQ(CefContextMenuHandlerCToCpp::DebugObjCt, 0); DCHECK_EQ(CefContextMenuParamsCppToC::DebugObjCt, 0); DCHECK_EQ(CefCookieManagerCppToC::DebugObjCt, 0); diff --git a/cef3/libcef_dll/wrapper/libcef_dll_wrapper.cc b/cef3/libcef_dll/wrapper/libcef_dll_wrapper.cc index a651315d6..9c6f37d0e 100644 --- a/cef3/libcef_dll/wrapper/libcef_dll_wrapper.cc +++ b/cef3/libcef_dll/wrapper/libcef_dll_wrapper.cc @@ -35,6 +35,7 @@ #include "include/cef_version.h" #include "libcef_dll/cpptoc/app_cpptoc.h" #include "libcef_dll/cpptoc/browser_process_handler_cpptoc.h" +#include "libcef_dll/cpptoc/completion_handler_cpptoc.h" #include "libcef_dll/cpptoc/context_menu_handler_cpptoc.h" #include "libcef_dll/cpptoc/cookie_visitor_cpptoc.h" #include "libcef_dll/cpptoc/domevent_listener_cpptoc.h" @@ -169,6 +170,7 @@ CEF_GLOBAL void CefShutdown() { DCHECK_EQ(CefBrowserHostCToCpp::DebugObjCt, 0); DCHECK_EQ(CefBrowserProcessHandlerCppToC::DebugObjCt, 0); DCHECK_EQ(CefCallbackCToCpp::DebugObjCt, 0); + DCHECK_EQ(CefCompletionHandlerCppToC::DebugObjCt, 0); DCHECK_EQ(CefContextMenuHandlerCppToC::DebugObjCt, 0); DCHECK_EQ(CefContextMenuParamsCToCpp::DebugObjCt, 0); DCHECK_EQ(CefCookieManagerCToCpp::DebugObjCt, 0); diff --git a/cef3/tests/unittests/cookie_unittest.cc b/cef3/tests/unittests/cookie_unittest.cc index 62a2b4ff0..23da601ed 100644 --- a/cef3/tests/unittests/cookie_unittest.cc +++ b/cef3/tests/unittests/cookie_unittest.cc @@ -85,17 +85,20 @@ void DeleteCookies(CefRefPtr manager, // created, otherwise a host cookie will be created. void CreateCookie(CefRefPtr manager, CefCookie& cookie, bool withDomain, + bool sessionCookie, base::WaitableEvent& event) { CefString(&cookie.name).FromASCII("my_cookie"); CefString(&cookie.value).FromASCII("My Value"); if (withDomain) CefString(&cookie.domain).FromASCII(kTestDomain); CefString(&cookie.path).FromASCII(kTestPath); - cookie.has_expires = true; - cookie.expires.year = 2200; - cookie.expires.month = 4; - cookie.expires.day_of_week = 5; - cookie.expires.day_of_month = 11; + if (!sessionCookie) { + cookie.has_expires = true; + cookie.expires.year = 2200; + cookie.expires.month = 4; + cookie.expires.day_of_week = 5; + cookie.expires.day_of_month = 11; + } CookieVector cookies; cookies.push_back(cookie); @@ -126,7 +129,7 @@ void GetCookie(CefRefPtr manager, else EXPECT_EQ(CefString(&cookie_read.domain), kTestDomain); EXPECT_EQ(CefString(&cookie_read.path), kTestPath); - EXPECT_TRUE(cookie_read.has_expires); + EXPECT_EQ(cookie.has_expires, cookie_read.has_expires); EXPECT_EQ(cookie.expires.year, cookie_read.expires.year); EXPECT_EQ(cookie.expires.month, cookie_read.expires.month); EXPECT_EQ(cookie.expires.day_of_week, cookie_read.expires.day_of_week); @@ -191,7 +194,7 @@ void TestDomainCookie(CefRefPtr manager) { CefCookie cookie; // Create a domain cookie. - CreateCookie(manager, cookie, true, event); + CreateCookie(manager, cookie, true, false, event); // Retrieve, verify and delete the domain cookie. GetCookie(manager, cookie, true, event, true); @@ -205,7 +208,7 @@ void TestHostCookie(CefRefPtr manager) { CefCookie cookie; // Create a host cookie. - CreateCookie(manager, cookie, false, event); + CreateCookie(manager, cookie, false, false, event); // Retrieve, verify and delete the host cookie. GetCookie(manager, cookie, false, event, true); @@ -388,7 +391,7 @@ void TestChangeDirectory(CefRefPtr manager, DeleteAllCookies(manager, event); // Set the new temporary directory as the storage location. - EXPECT_TRUE(manager->SetStoragePath(temp_dir.path().value())); + EXPECT_TRUE(manager->SetStoragePath(temp_dir.path().value(), false)); // Wait for the storage location change to complete on the IO thread. WaitForIOThread(); @@ -397,13 +400,13 @@ void TestChangeDirectory(CefRefPtr manager, VerifyNoCookies(manager, event, true); // Create a domain cookie. - CreateCookie(manager, cookie, true, event); + CreateCookie(manager, cookie, true, false, event); // Retrieve and verify the domain cookie. GetCookie(manager, cookie, true, event, false); // Restore the original storage location. - EXPECT_TRUE(manager->SetStoragePath(original_dir)); + EXPECT_TRUE(manager->SetStoragePath(original_dir, false)); // Wait for the storage location change to complete on the IO thread. WaitForIOThread(); @@ -412,7 +415,7 @@ void TestChangeDirectory(CefRefPtr manager, VerifyNoCookies(manager, event, true); // Set the new temporary directory as the storage location. - EXPECT_TRUE(manager->SetStoragePath(temp_dir.path().value())); + EXPECT_TRUE(manager->SetStoragePath(temp_dir.path().value(), false)); // Wait for the storage location change to complete on the IO thread. WaitForIOThread(); @@ -421,7 +424,7 @@ void TestChangeDirectory(CefRefPtr manager, GetCookie(manager, cookie, true, event, false); // Restore the original storage location. - EXPECT_TRUE(manager->SetStoragePath(original_dir)); + EXPECT_TRUE(manager->SetStoragePath(original_dir, false)); // Wait for the storage location change to complete on the IO thread. WaitForIOThread(); @@ -440,7 +443,7 @@ TEST(CookieTest, DomainCookieGlobal) { // Test creation of a domain cookie. TEST(CookieTest, DomainCookieInMemory) { CefRefPtr manager = - CefCookieManager::CreateManager(CefString()); + CefCookieManager::CreateManager(CefString(), false); EXPECT_TRUE(manager.get()); TestDomainCookie(manager); @@ -454,7 +457,7 @@ TEST(CookieTest, DomainCookieOnDisk) { EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); CefRefPtr manager = - CefCookieManager::CreateManager(temp_dir.path().value()); + CefCookieManager::CreateManager(temp_dir.path().value(), false); EXPECT_TRUE(manager.get()); TestDomainCookie(manager); @@ -471,7 +474,7 @@ TEST(CookieTest, HostCookieGlobal) { // Test creation of a host cookie. TEST(CookieTest, HostCookieInMemory) { CefRefPtr manager = - CefCookieManager::CreateManager(CefString()); + CefCookieManager::CreateManager(CefString(), false); EXPECT_TRUE(manager.get()); TestHostCookie(manager); @@ -485,7 +488,7 @@ TEST(CookieTest, HostCookieOnDisk) { EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); CefRefPtr manager = - CefCookieManager::CreateManager(temp_dir.path().value()); + CefCookieManager::CreateManager(temp_dir.path().value(), false); EXPECT_TRUE(manager.get()); TestHostCookie(manager); @@ -502,7 +505,7 @@ TEST(CookieTest, MultipleCookiesGlobal) { // Test creation of multiple cookies. TEST(CookieTest, MultipleCookiesInMemory) { CefRefPtr manager = - CefCookieManager::CreateManager(CefString()); + CefCookieManager::CreateManager(CefString(), false); EXPECT_TRUE(manager.get()); TestMultipleCookies(manager); @@ -516,7 +519,7 @@ TEST(CookieTest, MultipleCookiesOnDisk) { EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); CefRefPtr manager = - CefCookieManager::CreateManager(temp_dir.path().value()); + CefCookieManager::CreateManager(temp_dir.path().value(), false); EXPECT_TRUE(manager.get()); TestMultipleCookies(manager); @@ -531,7 +534,7 @@ TEST(CookieTest, AllCookiesGlobal) { TEST(CookieTest, AllCookiesInMemory) { CefRefPtr manager = - CefCookieManager::CreateManager(CefString()); + CefCookieManager::CreateManager(CefString(), false); EXPECT_TRUE(manager.get()); TestAllCookies(manager); @@ -544,7 +547,7 @@ TEST(CookieTest, AllCookiesOnDisk) { EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); CefRefPtr manager = - CefCookieManager::CreateManager(temp_dir.path().value()); + CefCookieManager::CreateManager(temp_dir.path().value(), false); EXPECT_TRUE(manager.get()); TestAllCookies(manager); @@ -562,13 +565,93 @@ TEST(CookieTest, ChangeDirectoryGlobal) { TEST(CookieTest, ChangeDirectoryCreated) { CefRefPtr manager = - CefCookieManager::CreateManager(CefString()); + CefCookieManager::CreateManager(CefString(), false); EXPECT_TRUE(manager.get()); TestChangeDirectory(manager, CefString()); } +namespace { + +class TestCompletionHandler : public CefCompletionHandler { + public: + explicit TestCompletionHandler(base::WaitableEvent* event) + : event_(event) {} + + virtual void OnComplete() OVERRIDE { + event_->Signal(); + } + + private: + base::WaitableEvent* event_; + + IMPLEMENT_REFCOUNTING(TestCompletionHandler); +}; + +} // namespace + +TEST(CookieTest, SessionCookieNoPersist) { + base::ScopedTempDir temp_dir; + base::WaitableEvent event(false, false); + CefCookie cookie; + + // Create a new temporary directory. + EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); + + CefRefPtr manager = + CefCookieManager::CreateManager(temp_dir.path().value(), false); + EXPECT_TRUE(manager.get()); + + // Create a session cookie. + CreateCookie(manager, cookie, true, true, event); + + // Retrieve and verify the cookie. + GetCookie(manager, cookie, true, event, false); + + // Flush the cookie store to disk. + manager->FlushStore(new TestCompletionHandler(&event)); + event.Wait(); + + // Create a new manager to read the same cookie store. + manager = + CefCookieManager::CreateManager(temp_dir.path().value(), false); + + // Verify that the cookie doesn't exist. + VerifyNoCookies(manager, event, true); +} + +TEST(CookieTest, SessionCookieWillPersist) { + base::ScopedTempDir temp_dir; + base::WaitableEvent event(false, false); + CefCookie cookie; + + // Create a new temporary directory. + EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); + + CefRefPtr manager = + CefCookieManager::CreateManager(temp_dir.path().value(), true); + EXPECT_TRUE(manager.get()); + + // Create a session cookie. + CreateCookie(manager, cookie, true, true, event); + + // Retrieve and verify the cookie. + GetCookie(manager, cookie, true, event, false); + + // Flush the cookie store to disk. + manager->FlushStore(new TestCompletionHandler(&event)); + event.Wait(); + + // Create a new manager to read the same cookie store. + manager = + CefCookieManager::CreateManager(temp_dir.path().value(), true); + + // Verify that the cookie exists. + GetCookie(manager, cookie, true, event, false); +} + + namespace { const char* kCookieJSUrl1 = "http://tests/cookie1.html"; @@ -580,8 +663,8 @@ class CookieTestJSHandler : public TestHandler { virtual void RunTest() OVERRIDE { // Create =new in-memory managers. - manager1_ = CefCookieManager::CreateManager(CefString()); - manager2_ = CefCookieManager::CreateManager(CefString()); + manager1_ = CefCookieManager::CreateManager(CefString(), false); + manager2_ = CefCookieManager::CreateManager(CefString(), false); std::string page = "" @@ -805,8 +888,8 @@ class CookieTestSchemeHandler : public TestHandler { virtual void RunTest() OVERRIDE { // Create new in-memory managers. - manager1_ = CefCookieManager::CreateManager(CefString()); - manager2_ = CefCookieManager::CreateManager(CefString()); + manager1_ = CefCookieManager::CreateManager(CefString(), false); + manager2_ = CefCookieManager::CreateManager(CefString(), false); if (scheme_ != "http") { std::vector schemes;