X Tutup
Skip to content

Commit c78619a

Browse files
committed
Fix flushing cookies to disk when closing app immediately (cztomczak#365).
Expose CookieManager.FlushStore method.
1 parent 7aeee77 commit c78619a

File tree

8 files changed

+43
-4
lines changed

8 files changed

+43
-4
lines changed

api/CookieManager.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ browsers do not persist them. Returns false if cookies cannot be accessed.
174174

175175
| Parameter | Type |
176176
| --- | --- |
177-
| handler | CompletionHandler |
177+
| callback | CompletionHandler (optional) |
178178
| __Return__ | bool |
179179

180-
Not yet implemented.
181-
182180
Flush the backing store (if any) to disk. If |callback| is non-NULL it will
183181
be executed asnychronously on the IO thread after the flush is complete.
184182
Returns false if cookies cannot be accessed.
183+
184+
The callback arg is not implemented.

src/browser.pyx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,21 @@ cdef class PyBrowser:
328328
pass
329329

330330
cpdef py_void CloseBrowser(self, py_bool forceClose=False):
331+
# Browser can be closed in two ways. Either by calling
332+
# CloseBrowser explicitilly or by destroying window
333+
# object and in such case lifespanHandler.OnBeforeClose
334+
# will be called.
331335
Debug("CefBrowser::CloseBrowser(%s)" % forceClose)
336+
337+
# Flush cookies to disk. Temporary solution for Issue #365.
338+
# A similar call is made in LifespanHandler_OnBeforeClose.
339+
# If using GetCookieManager to implement custom cookie managers
340+
# then flushing of cookies would need to be handled manually.
341+
self.GetCefBrowserHost().get().GetRequestContext().get() \
342+
.GetDefaultCookieManager(
343+
<CefRefPtr[CefCompletionCallback]?>NULL) \
344+
.get().FlushStore(<CefRefPtr[CefCompletionCallback]?>NULL)
345+
332346
cdef int browserId = self.GetCefBrowser().get().GetIdentifier()
333347
self.GetCefBrowserHost().get().CloseBrowser(bool(forceClose))
334348
global g_closed_browsers

src/cookie.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ cdef class PyCookieManager:
268268
PyToCefStringValue(path), bool(persistSessionCookies),
269269
<CefRefPtr[CefCompletionCallback]?>NULL)
270270

271+
cpdef py_bool FlushStore(self, callback=None):
272+
return self.cefCookieManager.get().FlushStore(
273+
<CefRefPtr[CefCompletionCallback]?>NULL)
274+
275+
271276
# ------------------------------------------------------------------------------
272277
# PyCookieVisitor
273278
# ------------------------------------------------------------------------------

src/extern/cef/cef_browser.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from cef_types cimport int64
1515
from cef_types cimport CefBrowserSettings, CefPoint
1616
from cef_drag_data cimport CefDragData
1717
from cef_types cimport CefMouseEvent
18+
from cef_request_context cimport CefRequestContext
1819

1920
from cef_process_message cimport CefProcessMessage, CefProcessId
2021

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

66+
CefRefPtr[CefRequestContext] GetRequestContext()
67+
6568
void Find(int identifier, const CefString& searchText, cpp_bool forward,
6669
cpp_bool matchCase, cpp_bool findNext)
6770
void StopFinding(cpp_bool clearSelection)

src/extern/cef/cef_cookie.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ cdef extern from "include/cef_cookie.h":
4747
cpp_bool SetStoragePath(const CefString& path,
4848
cpp_bool persist_session_cookies,
4949
CefRefPtr[CefCompletionCallback] callback)
50-
# cpp_bool FlushStore(CefRefPtr[CefCompletionCallback] handler)
50+
cpp_bool FlushStore(CefRefPtr[CefCompletionCallback] callback)
5151

5252
cdef cppclass CefCookieVisitor:
5353
pass

src/extern/cef/cef_cookie_manager_namespace.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ from cef_cookie cimport CefCookie
1010
# noinspection PyUnresolvedReferences
1111
from cef_cookie cimport CefSetCookieCallback, CefDeleteCookiesCallback
1212
from cef_ptr cimport CefRefPtr
13+
# noinspection PyUnresolvedReferences
14+
from cef_callback cimport CefCompletionCallback
1315

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

src/extern/cef/cef_request_context.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from cef_ptr cimport CefRefPtr
66
# noinspection PyUnresolvedReferences
77
from cef_request_context_handler cimport CefRequestContextHandler
8+
from cef_callback cimport CefCompletionCallback
9+
from cef_cookie cimport CefCookieManager
810

911
cdef extern from "include/cef_request_context.h":
1012
cdef cppclass CefRequestContext:
@@ -14,3 +16,5 @@ cdef extern from "include/cef_request_context.h":
1416
CefRefPtr[CefRequestContext] CreateContext(
1517
CefRefPtr[CefRequestContext] other,
1618
CefRefPtr[CefRequestContextHandler] handler)
19+
CefRefPtr[CefCookieManager] GetDefaultCookieManager(
20+
CefRefPtr[CefCompletionCallback] callback)

src/handlers/lifespan_handler.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ cdef public void LifespanHandler_OnBeforeClose(
128128
if callback:
129129
callback(browser=pyBrowser)
130130

131+
# Flush cookies to disk. Temporary solution for Issue #365.
132+
# A similar call is made in Browser.CloseBrowser. If using
133+
# GetCookieManager to implement custom cookie managers then
134+
# flushing of cookies would need to be handled manually.
135+
cefBrowser.get().GetHost().get().GetRequestContext().get() \
136+
.GetDefaultCookieManager(
137+
<CefRefPtr[CefCompletionCallback]?>NULL) \
138+
.get().FlushStore(<CefRefPtr[CefCompletionCallback]?>NULL)
139+
131140
browserId = pyBrowser.GetIdentifier()
132141
pyBrowser.cefBrowser.Assign(NULL)
133142
cefBrowser.Assign(NULL)

0 commit comments

Comments
 (0)
X Tutup