Skip to content

Commit

Permalink
Fix flushing cookies to disk when closing app immediately (#365).
Browse files Browse the repository at this point in the history
Expose CookieManager.FlushStore method.
  • Loading branch information
cztomczak committed Aug 2, 2018
1 parent 7aeee77 commit c78619a
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions api/CookieManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ browsers do not persist them. Returns false if cookies cannot be accessed.

| Parameter | Type |
| --- | --- |
| handler | CompletionHandler |
| callback | CompletionHandler (optional) |
| __Return__ | bool |

Not yet implemented.

Flush the backing store (if any) to disk. If |callback| is non-NULL it will
be executed asnychronously on the IO thread after the flush is complete.
Returns false if cookies cannot be accessed.

The callback arg is not implemented.
14 changes: 14 additions & 0 deletions src/browser.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,21 @@ cdef class PyBrowser:
pass

cpdef py_void CloseBrowser(self, py_bool forceClose=False):
# Browser can be closed in two ways. Either by calling
# CloseBrowser explicitilly or by destroying window
# object and in such case lifespanHandler.OnBeforeClose
# will be called.
Debug("CefBrowser::CloseBrowser(%s)" % forceClose)

# Flush cookies to disk. Temporary solution for Issue #365.
# A similar call is made in LifespanHandler_OnBeforeClose.
# If using GetCookieManager to implement custom cookie managers
# then flushing of cookies would need to be handled manually.
self.GetCefBrowserHost().get().GetRequestContext().get() \
.GetDefaultCookieManager(
<CefRefPtr[CefCompletionCallback]?>NULL) \
.get().FlushStore(<CefRefPtr[CefCompletionCallback]?>NULL)

cdef int browserId = self.GetCefBrowser().get().GetIdentifier()
self.GetCefBrowserHost().get().CloseBrowser(bool(forceClose))
global g_closed_browsers
Expand Down
5 changes: 5 additions & 0 deletions src/cookie.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ cdef class PyCookieManager:
PyToCefStringValue(path), bool(persistSessionCookies),
<CefRefPtr[CefCompletionCallback]?>NULL)

cpdef py_bool FlushStore(self, callback=None):
return self.cefCookieManager.get().FlushStore(
<CefRefPtr[CefCompletionCallback]?>NULL)


# ------------------------------------------------------------------------------
# PyCookieVisitor
# ------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions src/extern/cef/cef_browser.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ from cef_types cimport int64
from cef_types cimport CefBrowserSettings, CefPoint
from cef_drag_data cimport CefDragData
from cef_types cimport CefMouseEvent
from cef_request_context cimport CefRequestContext

from cef_process_message cimport CefProcessMessage, CefProcessId

Expand Down Expand Up @@ -62,6 +63,8 @@ cdef extern from "include/cef_browser.h":
const CefPoint& inspect_element_at)
void CloseDevTools()

CefRefPtr[CefRequestContext] GetRequestContext()

void Find(int identifier, const CefString& searchText, cpp_bool forward,
cpp_bool matchCase, cpp_bool findNext)
void StopFinding(cpp_bool clearSelection)
Expand Down
2 changes: 1 addition & 1 deletion src/extern/cef/cef_cookie.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cdef extern from "include/cef_cookie.h":
cpp_bool SetStoragePath(const CefString& path,
cpp_bool persist_session_cookies,
CefRefPtr[CefCompletionCallback] callback)
# cpp_bool FlushStore(CefRefPtr[CefCompletionCallback] handler)
cpp_bool FlushStore(CefRefPtr[CefCompletionCallback] callback)

cdef cppclass CefCookieVisitor:
pass
Expand Down
4 changes: 4 additions & 0 deletions src/extern/cef/cef_cookie_manager_namespace.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ from cef_cookie cimport CefCookie
# noinspection PyUnresolvedReferences
from cef_cookie cimport CefSetCookieCallback, CefDeleteCookiesCallback
from cef_ptr cimport CefRefPtr
# noinspection PyUnresolvedReferences
from cef_callback cimport CefCompletionCallback

# We need to pass C++ class methods by reference to a function,
# it is not possible with such syntax:
Expand All @@ -28,3 +30,5 @@ cdef extern from "include/cef_cookie.h" namespace "CefCookieManager":
cpp_bool DeleteCookies(const CefString& url,
const CefString& cookie_name,
CefRefPtr[CefDeleteCookiesCallback] callback)

cpp_bool FlushStore(CefRefPtr[CefCompletionCallback] callback)
4 changes: 4 additions & 0 deletions src/extern/cef/cef_request_context.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from cef_ptr cimport CefRefPtr
# noinspection PyUnresolvedReferences
from cef_request_context_handler cimport CefRequestContextHandler
from cef_callback cimport CefCompletionCallback
from cef_cookie cimport CefCookieManager

cdef extern from "include/cef_request_context.h":
cdef cppclass CefRequestContext:
Expand All @@ -14,3 +16,5 @@ cdef extern from "include/cef_request_context.h":
CefRefPtr[CefRequestContext] CreateContext(
CefRefPtr[CefRequestContext] other,
CefRefPtr[CefRequestContextHandler] handler)
CefRefPtr[CefCookieManager] GetDefaultCookieManager(
CefRefPtr[CefCompletionCallback] callback)
9 changes: 9 additions & 0 deletions src/handlers/lifespan_handler.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ cdef public void LifespanHandler_OnBeforeClose(
if callback:
callback(browser=pyBrowser)

# Flush cookies to disk. Temporary solution for Issue #365.
# A similar call is made in Browser.CloseBrowser. If using
# GetCookieManager to implement custom cookie managers then
# flushing of cookies would need to be handled manually.
cefBrowser.get().GetHost().get().GetRequestContext().get() \
.GetDefaultCookieManager(
<CefRefPtr[CefCompletionCallback]?>NULL) \
.get().FlushStore(<CefRefPtr[CefCompletionCallback]?>NULL)

browserId = pyBrowser.GetIdentifier()
pyBrowser.cefBrowser.Assign(NULL)
cefBrowser.Assign(NULL)
Expand Down

0 comments on commit c78619a

Please sign in to comment.