From f5d4696024e680b26770fbda63a0e342cb95eee8 Mon Sep 17 00:00:00 2001 From: cztomczak Date: Sun, 19 Aug 2018 10:34:08 +0200 Subject: [PATCH] Add CookieManager.GetBlockingManager (#403) --- README.md | 1 + api/API-index.md | 1 + api/CookieManager.md | 36 +++++++++++++++++++++++++++++------ examples/wxpython.py | 4 ++-- src/cef_v59..v66_changes.txt | 3 ++- src/cookie.pyx | 32 +++++++++++++++++++++++++------ src/extern/cef/cef_cookie.pxd | 6 ++++++ unittests/main_test.py | 9 +++++++++ 8 files changed, 77 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index dafbf9ff..04897140 100644 --- a/README.md +++ b/README.md @@ -509,6 +509,7 @@ Additional information for v31.2 release: * [GetExpires](api/Cookie.md#getexpires) * [CookieManager (class)](api/CookieManager.md#cookiemanager-class) * [GetGlobalManager](api/CookieManager.md#getglobalmanager) + * [GetBlockingManager](api/CookieManager.md#getblockingmanager) * [CreateManager](api/CookieManager.md#createmanager) * [SetSupportedSchemes](api/CookieManager.md#setsupportedschemes) * [VisitAllCookies](api/CookieManager.md#visitallcookies) diff --git a/api/API-index.md b/api/API-index.md index 9c5374da..d236471d 100644 --- a/api/API-index.md +++ b/api/API-index.md @@ -197,6 +197,7 @@ * [GetExpires](Cookie.md#getexpires) * [CookieManager (class)](CookieManager.md#cookiemanager-class) * [GetGlobalManager](CookieManager.md#getglobalmanager) + * [GetBlockingManager](CookieManager.md#getblockingmanager) * [CreateManager](CookieManager.md#createmanager) * [SetSupportedSchemes](CookieManager.md#setsupportedschemes) * [VisitAllCookies](CookieManager.md#visitallcookies) diff --git a/api/CookieManager.md b/api/CookieManager.md index 6c70b153..b0d06a91 100644 --- a/api/CookieManager.md +++ b/api/CookieManager.md @@ -3,10 +3,11 @@ # CookieManager (class) -This class cannot be instantiated directly, use the CreateManager() -static method for this purpose. +Class used for managing cookies. The methods of this class may be called on +any thread unless otherwise indicated. -The cookie tests can be found in the wxpython.py script. +Use the `CookieManager.CreateManager` static method to instantiate +this class. TODO: in upstream CEF some methods here have a callback parameter that when non-NULL will execute asynchronously on the IO thread @@ -17,6 +18,7 @@ also have an OnComplete callback. Table of contents: * [Methods](#methods) * [GetGlobalManager](#getglobalmanager) + * [GetBlockingManager](#getblockingmanager) * [CreateManager](#createmanager) * [SetSupportedSchemes](#setsupportedschemes) * [VisitAllCookies](#visitallcookies) @@ -34,11 +36,33 @@ Table of contents: | | | | --- | --- | -| __Return__ | static [CookieManager](CookieManager.md) | +| __Return__ | [CookieManager](CookieManager.md) | Returns the global cookie manager. By default data will be stored at [ApplicationSettings](ApplicationSettings.md).cache_path if specified or in memory otherwise. +Description from upstream CEF: +> Returns the global cookie manager. By default data will be stored at +> CefSettings.cache_path if specified or in memory otherwise. If |callback| +> is non-NULL it will be executed asnychronously on the IO thread after the +> manager's storage has been initialized. Using this method is equivalent to +> calling CefRequestContext::GetGlobalContext()->GetDefaultCookieManager() + + +### GetBlockingManager + +| | | +| --- | --- | +| __Return__ | [CookieManager](CookieManager.md) | + +Description from upstream CEF: +> Returns a cookie manager that neither stores nor retrieves cookies. All +> usage of cookies will be blocked including cookies accessed via the network +> (request/response headers), via JavaScript (document.cookie), and via +> CefCookieManager methods. No cookies will be displayed in DevTools. If you +> wish to only block cookies sent via the network use the CefRequestHandler +> CanGetCookies and CanSetCookie methods instead. + ### CreateManager @@ -46,7 +70,7 @@ Returns the global cookie manager. By default data will be stored at | --- | --- | | path | string | | persistSessionCookies=False | bool | -| __Return__ | static [CookieManager](CookieManager.md) | +| __Return__ | [CookieManager](CookieManager.md) | Creates a new cookie manager. Otherwise, data will be stored at the specified |path|. To persist session cookies (cookies without an expiry @@ -181,4 +205,4 @@ 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. +The callback arg is not implemented yet. diff --git a/examples/wxpython.py b/examples/wxpython.py index 915f7dff..6f3892c7 100644 --- a/examples/wxpython.py +++ b/examples/wxpython.py @@ -79,7 +79,7 @@ def scale_window_size_for_high_dpi(width, height): if not WINDOWS: return width, height (_, _, max_width, max_height) = wx.GetClientDisplayRect().Get() - # noinspection PyUnresolvedReferences, PyArgumentList + # noinspection PyUnresolvedReferences (width, height) = cef.DpiAware.Scale((width, height)) if width > max_width: width = max_width @@ -174,7 +174,7 @@ def create_menu(self): def embed_browser(self): window_info = cef.WindowInfo() (width, height) = self.browser_panel.GetClientSize().Get() - assert self.browser_panel.GetHandle(), "Window handle not available yet" + assert self.browser_panel.GetHandle(), "Window handle not available" window_info.SetAsChild(self.browser_panel.GetHandle(), [0, 0, width, height]) self.browser = cef.CreateBrowserSync(window_info, diff --git a/src/cef_v59..v66_changes.txt b/src/cef_v59..v66_changes.txt index 27260280..64f45a31 100644 --- a/src/cef_v59..v66_changes.txt +++ b/src/cef_v59..v66_changes.txt @@ -73,6 +73,7 @@ NEW FEATURES + unittests/osr_test.py - new test for off-screen rendering mode + cefpython.SetGlobalClientHandler + Browser.Invalidate ++ CookieManager.FlushStore internal/cef_types.h + cef_log_severity_t: new key LOGSEVERITY_DEBUG (no need to expose, @@ -98,7 +99,7 @@ cef_browser.h - IsBackgroundHost cef_cookie.h -- GetBlockingManager ++ GetBlockingManager cef_display_handler.h + OnAutoResize diff --git a/src/cookie.pyx b/src/cookie.pyx index 4ccf8844..ce078c10 100644 --- a/src/cookie.pyx +++ b/src/cookie.pyx @@ -181,9 +181,12 @@ cdef class Cookie: # CookieManager # ------------------------------------------------------------------------------ -class CookieManager: - @staticmethod - def GetGlobalManager(): +class CookieManager(object): + """Class used for managing cookies. To instantiate this class + call CreateManager() static method.""" + + @classmethod + def GetGlobalManager(cls): global g_globalCookieManager cdef CefRefPtr[CefCookieManager] cefCookieManager if not g_globalCookieManager: @@ -192,11 +195,28 @@ class CookieManager: g_globalCookieManager = CreatePyCookieManager(cefCookieManager) return g_globalCookieManager - @staticmethod - def CreateManager(py_string path, py_bool persistSessionCookies=False): + @classmethod + def GetBlockingManager(cls): + return CreatePyCookieManager(CefCookieManager_GetBlockingManager()) + + @classmethod + def CreateManager(cls, py_string path, + py_bool persist_session_cookies=False): + """ + Create a new cookie manager. + :param path: + :type path: str + :param persist_session_cookies: + :type path: bool + :return: CookieManager object + :rtype: CookieManager + """ + # When PyCharm generates a stub for the cefpython module + # it doesn't use the above docstring for code inspections. + # No idea why. cdef CefRefPtr[CefCookieManager] cefCookieManager cefCookieManager = CefCookieManager_CreateManager( - PyToCefStringValue(path), bool(persistSessionCookies), + PyToCefStringValue(path), bool(persist_session_cookies), NULL) if cefCookieManager != NULL and cefCookieManager.get(): return CreatePyCookieManager(cefCookieManager) diff --git a/src/extern/cef/cef_cookie.pxd b/src/extern/cef/cef_cookie.pxd index e1778786..c113aea9 100644 --- a/src/extern/cef/cef_cookie.pxd +++ b/src/extern/cef/cef_cookie.pxd @@ -27,11 +27,17 @@ cdef extern from "include/cef_cookie.h": cdef CefRefPtr[CefCookieManager] CefCookieManager_GetGlobalManager \ "CefCookieManager::GetGlobalManager"( CefRefPtr[CefCompletionCallback] callback) + + cdef CefRefPtr[CefCookieManager] CefCookieManager_GetBlockingManager \ + "CefCookieManager::GetBlockingManager"() + + cdef CefRefPtr[CefCookieManager] CefCookieManager_CreateManager \ "CefCookieManager::CreateManager"( const CefString& path, cpp_bool persist_session_cookies, CefRefPtr[CefCompletionCallback] callback) + cdef cppclass CefCookieManager: void SetSupportedSchemes(const cpp_vector[CefString]& schemes, CefRefPtr[CefCompletionCallback] callback) diff --git a/unittests/main_test.py b/unittests/main_test.py index 4ecc2e2a..0b141a67 100644 --- a/unittests/main_test.py +++ b/unittests/main_test.py @@ -197,6 +197,15 @@ def test_main(self): self.assertEqual(req_data, req.GetPostData()) subtest_message("cef.Request.SetPostData(dict) ok") + # Cookie manager + self.assertIsInstance(cef.CookieManager.CreateManager(path=""), + cef.PyCookieManager) + self.assertIsInstance(cef.CookieManager.GetGlobalManager(), + cef.PyCookieManager) + self.assertIsInstance(cef.CookieManager.GetBlockingManager(), + cef.PyCookieManager) + subtest_message("cef.CookieManager ok") + # Run message loop run_message_loop()