Skip to content

Commit 5c15c72

Browse files
committed
Fix code inspection in PyCharm (cztomczak#456), ehhance tests and add new APIs.
Add Browser.GetSetting. Add Browser.HasDevTools. Add cef.GetBrowserByIdentifier. Add BrowserSettings.inherit_client_handlers_for_popups.
1 parent f5d4696 commit 5c15c72

17 files changed

+282
-78
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ Additional information for v31.2 release:
381381
* [GetNSTextInputContext](api/Browser.md#getnstextinputcontext)
382382
* [GetOpenerWindowHandle](api/Browser.md#getopenerwindowhandle)
383383
* [GetOuterWindowHandle](api/Browser.md#getouterwindowhandle)
384+
* [GetSetting](api/Browser.md#getsetting)
384385
* [GetUrl](api/Browser.md#geturl)
385386
* [GetUserData](api/Browser.md#getuserdata)
386387
* [GetWindowHandle](api/Browser.md#getwindowhandle)
@@ -390,6 +391,7 @@ Additional information for v31.2 release:
390391
* [GoForward](api/Browser.md#goforward)
391392
* [HandleKeyEventAfterTextInputClient](api/Browser.md#handlekeyeventaftertextinputclient)
392393
* [HandleKeyEventBeforeTextInputClient](api/Browser.md#handlekeyeventbeforetextinputclient)
394+
* [HasDevTools](api/Browser.md#hasdevtools)
393395
* [HasDocument](api/Browser.md#hasdocument)
394396
* [Invalidate](api/Browser.md#invalidate)
395397
* [IsFullscreen](api/Browser.md#isfullscreen)
@@ -439,6 +441,7 @@ Additional information for v31.2 release:
439441
* [default_encoding](api/BrowserSettings.md#default_encoding)
440442
* [dom_paste_disabled](api/BrowserSettings.md#dom_paste_disabled)
441443
* [file_access_from_file_urls_allowed](api/BrowserSettings.md#file_access_from_file_urls_allowed)
444+
* [inherit_client_handlers_for_popups](api/BrowserSettings.md#inherit_client_handlers_for_popups)
442445
* [image_load_disabled](api/BrowserSettings.md#image_load_disabled)
443446
* [javascript_disabled](api/BrowserSettings.md#javascript_disabled)
444447
* [javascript_close_windows_disallowed](api/BrowserSettings.md#javascript_close_windows_disallowed)
@@ -463,6 +466,7 @@ Additional information for v31.2 release:
463466
* [ExceptHook](api/cefpython.md#excepthook)
464467
* [GetAppSetting](api/cefpython.md#getappsetting)
465468
* [GetAppPath](api/cefpython.md#getapppath)
469+
* [GetBrowserByIdentifier](api/cefpython.md#getbrowserbyidentifier)
466470
* [GetBrowserByWindowHandle](api/cefpython.md#getbrowserbywindowhandle)
467471
* [GetCommandLineSwitch](api/cefpython.md#getcommandlineswitch)
468472
* [GetGlobalClientCallback](api/cefpython.md#getglobalclientcallback)

api/API-index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
* [GetNSTextInputContext](Browser.md#getnstextinputcontext)
7070
* [GetOpenerWindowHandle](Browser.md#getopenerwindowhandle)
7171
* [GetOuterWindowHandle](Browser.md#getouterwindowhandle)
72+
* [GetSetting](Browser.md#getsetting)
7273
* [GetUrl](Browser.md#geturl)
7374
* [GetUserData](Browser.md#getuserdata)
7475
* [GetWindowHandle](Browser.md#getwindowhandle)
@@ -78,6 +79,7 @@
7879
* [GoForward](Browser.md#goforward)
7980
* [HandleKeyEventAfterTextInputClient](Browser.md#handlekeyeventaftertextinputclient)
8081
* [HandleKeyEventBeforeTextInputClient](Browser.md#handlekeyeventbeforetextinputclient)
82+
* [HasDevTools](Browser.md#hasdevtools)
8183
* [HasDocument](Browser.md#hasdocument)
8284
* [Invalidate](Browser.md#invalidate)
8385
* [IsFullscreen](Browser.md#isfullscreen)
@@ -127,6 +129,7 @@
127129
* [default_encoding](BrowserSettings.md#default_encoding)
128130
* [dom_paste_disabled](BrowserSettings.md#dom_paste_disabled)
129131
* [file_access_from_file_urls_allowed](BrowserSettings.md#file_access_from_file_urls_allowed)
132+
* [inherit_client_handlers_for_popups](BrowserSettings.md#inherit_client_handlers_for_popups)
130133
* [image_load_disabled](BrowserSettings.md#image_load_disabled)
131134
* [javascript_disabled](BrowserSettings.md#javascript_disabled)
132135
* [javascript_close_windows_disallowed](BrowserSettings.md#javascript_close_windows_disallowed)
@@ -151,6 +154,7 @@
151154
* [ExceptHook](cefpython.md#excepthook)
152155
* [GetAppSetting](cefpython.md#getappsetting)
153156
* [GetAppPath](cefpython.md#getapppath)
157+
* [GetBrowserByIdentifier](cefpython.md#getbrowserbyidentifier)
154158
* [GetBrowserByWindowHandle](cefpython.md#getbrowserbywindowhandle)
155159
* [GetCommandLineSwitch](cefpython.md#getcommandlineswitch)
156160
* [GetGlobalClientCallback](cefpython.md#getglobalclientcallback)

api/Browser.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Table of contents:
4545
* [GetNSTextInputContext](#getnstextinputcontext)
4646
* [GetOpenerWindowHandle](#getopenerwindowhandle)
4747
* [GetOuterWindowHandle](#getouterwindowhandle)
48+
* [GetSetting](#getsetting)
4849
* [GetUrl](#geturl)
4950
* [GetUserData](#getuserdata)
5051
* [GetWindowHandle](#getwindowhandle)
@@ -54,6 +55,7 @@ Table of contents:
5455
* [GoForward](#goforward)
5556
* [HandleKeyEventAfterTextInputClient](#handlekeyeventaftertextinputclient)
5657
* [HandleKeyEventBeforeTextInputClient](#handlekeyeventbeforetextinputclient)
58+
* [HasDevTools](#hasdevtools)
5759
* [HasDocument](#hasdocument)
5860
* [Invalidate](#invalidate)
5961
* [IsFullscreen](#isfullscreen)
@@ -495,6 +497,17 @@ Retrieve the CEF-internal (inner or outer) window handle of the browser that ope
495497
Get the most outer window handle.
496498

497499

500+
### GetSetting
501+
502+
| Parameter | Type |
503+
| --- | --- |
504+
| key | str |
505+
| __Return__ | mixed |
506+
507+
Get a browser setting. You can set browser settings by passing
508+
`settings` parameter to `cef.CreateBrowserSync`.
509+
510+
498511
### GetUrl
499512

500513
| | |
@@ -584,6 +597,17 @@ Handles a keyDown event prior to passing it through the NSTextInputClient
584597
machinery.
585598

586599

600+
### HasDevTools
601+
602+
| | |
603+
| --- | --- |
604+
| __Return__ | bool |
605+
606+
Description from upstream CEF:
607+
> Returns true if this browser currently has an associated DevTools browser.
608+
> Must be called on the browser process UI thread.
609+
610+
587611
### HasDocument
588612

589613
| | |

api/BrowserSettings.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Table of contents:
1515
* [default_encoding](#default_encoding)
1616
* [dom_paste_disabled](#dom_paste_disabled)
1717
* [file_access_from_file_urls_allowed](#file_access_from_file_urls_allowed)
18+
* [inherit_client_handlers_for_popups](#inherit_client_handlers_for_popups)
1819
* [image_load_disabled](#image_load_disabled)
1920
* [javascript_disabled](#javascript_disabled)
2021
* [javascript_close_windows_disallowed](#javascript_close_windows_disallowed)
@@ -110,6 +111,21 @@ in a known order. Equivalent to the `SkColor` type in Chromium.
110111
(bool) Controls whether file URLs will have access to other file URLs. Also configurable using the --allow-access-from-files switch. Other similar switches are: --allow-file-access and --allow-file-access-from-files.
111112

112113

114+
### inherit_client_handlers_for_popups
115+
116+
117+
(bool) Default: True.
118+
119+
Whether to inherit client handlers and callbacks for popup windows
120+
opened with "window.open". For example when you set a handler using
121+
`browser.SetClientHandler` then this handler will also be resued
122+
for popup windows that are created implicitilly via "window.open"
123+
call in javascript or similar. If you implement
124+
`LifespanHandler.OnBeforePopup` then you can control explicitilly
125+
what handlers are set for the popup browser. To disable the default
126+
behavior set this option to False.
127+
128+
113129
### image_load_disabled
114130

115131
(bool) Controls whether image URLs will be loaded from the network. A cached image will still be rendered if requested. Also configurable using the --disable-image-loading switch.

api/cefpython.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Table of contents:
1313
* [ExceptHook](#excepthook)
1414
* [GetAppSetting](#getappsetting)
1515
* [GetAppPath](#getapppath)
16+
* [GetBrowserByIdentifier](#getbrowserbyidentifier)
1617
* [GetBrowserByWindowHandle](#getbrowserbywindowhandle)
1718
* [GetCommandLineSwitch](#getcommandlineswitch)
1819
* [GetGlobalClientCallback](#getglobalclientcallback)
@@ -107,6 +108,17 @@ to Initialize(). Returns None if key is not found.
107108
Get path to where application resides.
108109

109110

111+
### GetBrowserByIdentifier
112+
113+
| Parameter | Type |
114+
| --- | --- |
115+
| identifier | int |
116+
| __Return__ | void |
117+
118+
Get browser by identifier. Browser identifier can be obtained by
119+
calling `Browser.GetIdentifier`.
120+
121+
110122
### GetBrowserByWindowHandle
111123

112124
| Parameter | Type |

examples/wxpython.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
import sys
1414
import os
1515

16-
# Fix for PyCharm hints warnings when using static methods
17-
WindowUtils = cef.WindowUtils()
18-
1916
# Platforms
2017
WINDOWS = (platform.system() == "Windows")
2118
LINUX = (platform.system() == "Linux")
@@ -97,7 +94,7 @@ def __init__(self):
9794
# installing X11 error handlers. This must be done after
9895
# wx was intialized.
9996
if LINUX:
100-
WindowUtils.InstallX11ErrorHandlers()
97+
cef.WindowUtils.InstallX11ErrorHandlers()
10198

10299
global g_count_windows
103100
g_count_windows += 1
@@ -185,16 +182,16 @@ def OnSetFocus(self, _):
185182
if not self.browser:
186183
return
187184
if WINDOWS:
188-
WindowUtils.OnSetFocus(self.browser_panel.GetHandle(),
189-
0, 0, 0)
185+
cef.WindowUtils.OnSetFocus(self.browser_panel.GetHandle(),
186+
0, 0, 0)
190187
self.browser.SetFocus(True)
191188

192189
def OnSize(self, _):
193190
if not self.browser:
194191
return
195192
if WINDOWS:
196-
WindowUtils.OnSize(self.browser_panel.GetHandle(),
197-
0, 0, 0)
193+
cef.WindowUtils.OnSize(self.browser_panel.GetHandle(),
194+
0, 0, 0)
198195
elif LINUX:
199196
(x, y) = (0, 0)
200197
(width, height) = self.browser_panel.GetSize().Get()

src/browser.pyx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ MOUSEBUTTON_RIGHT = cef_types.MBT_RIGHT
1919

2020
cdef dict g_pyBrowsers = {}
2121

22+
# See also g_browser_settings defined in cefpython.pyx
23+
2224
# Unreferenced browsers are added to this list in OnBeforeClose().
2325
# Must keep a list of unreferenced browsers so that a new reference
2426
# is not created in GetPyBrowser() when browser was closed.
@@ -122,9 +124,15 @@ cdef PyBrowser GetPyBrowser(CefRefPtr[CefBrowser] cefBrowser,
122124
openerHandle = pyBrowser.GetOpenerWindowHandle()
123125
for identifier, tempPyBrowser in g_pyBrowsers.items():
124126
if tempPyBrowser.GetWindowHandle() == openerHandle:
125-
clientCallbacks = tempPyBrowser.GetClientCallbacksDict()
126-
if clientCallbacks:
127-
pyBrowser.SetClientCallbacksDict(clientCallbacks)
127+
# tempPyBrowser is a parent browser
128+
if tempPyBrowser.GetSetting("inherit_client_handlers_for_popups"):
129+
if pyBrowser.GetIdentifier() not in g_browser_settings:
130+
g_browser_settings[pyBrowser.GetIdentifier()] = {}
131+
g_browser_settings[pyBrowser.GetIdentifier()]["inherit_client_handlers_for_popups"] =\
132+
tempPyBrowser.GetSetting("inherit_client_handlers_for_popups")
133+
clientCallbacks = tempPyBrowser.GetClientCallbacksDict()
134+
if clientCallbacks:
135+
pyBrowser.SetClientCallbacksDict(clientCallbacks)
128136
javascriptBindings = tempPyBrowser.GetJavascriptBindings()
129137
if javascriptBindings:
130138
if javascriptBindings.GetBindToPopups():
@@ -158,6 +166,15 @@ cpdef PyBrowser GetBrowserByWindowHandle(WindowHandle windowHandle):
158166
return pyBrowser
159167
return None
160168

169+
170+
cpdef PyBrowser GetBrowserByIdentifier(int identifier):
171+
cdef PyBrowser pyBrowser
172+
for browserId in g_pyBrowsers:
173+
pyBrowser = g_pyBrowsers[browserId]
174+
if pyBrowser.GetIdentifier() == identifier:
175+
return pyBrowser
176+
return None
177+
161178
cdef public void PyBrowser_ShowDevTools(CefRefPtr[CefBrowser] cefBrowser
162179
) except * with gil:
163180
# Called from ClientHandler::OnContextMenuCommand
@@ -324,6 +341,13 @@ cdef class PyBrowser:
324341
NonCriticalError("GetImage not implemented on this platform")
325342
return None
326343

344+
cpdef object GetSetting(self, py_string key):
345+
cdef int browser_id = self.GetIdentifier()
346+
if browser_id in g_browser_settings:
347+
if key in g_browser_settings[browser_id]:
348+
return g_browser_settings[browser_id][key]
349+
return None
350+
327351
# --------------
328352
# CEF API.
329353
# --------------
@@ -474,6 +498,9 @@ cdef class PyBrowser:
474498
cpdef py_void GoForward(self):
475499
self.GetCefBrowser().get().GoForward()
476500

501+
cpdef py_bool HasDevTools(self):
502+
return self.GetCefBrowserHost().get().HasDevTools()
503+
477504
cpdef py_bool HasDocument(self):
478505
return self.GetCefBrowser().get().HasDocument()
479506

src/cef_v59..v66_changes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ NEW FEATURES
7474
+ cefpython.SetGlobalClientHandler
7575
+ Browser.Invalidate
7676
+ CookieManager.FlushStore
77+
+ BrowserSettings.inherit_client_handlers_for_popups
78+
+ Browser.GetSetting
79+
+ cefpython.GetBrowserByIdentifier
80+
+ Browser.HasDevTools
7781

7882
internal/cef_types.h
7983
+ cef_log_severity_t: new key LOGSEVERITY_DEBUG (no need to expose,

src/cefpython.pyx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ g_debug = False
312312
# The string_encoding key must be set early here and also in Initialize.
313313
g_applicationSettings = {"string_encoding": "utf-8"}
314314
g_commandLineSwitches = {}
315+
g_browser_settings = {}
315316

316317
# If ApplicationSettings.unique_request_context_per_browser is False
317318
# then a shared request context is used for all browsers. Otherwise
@@ -721,9 +722,12 @@ def CreateBrowserSync(windowInfo=None,
721722
if window_title and windowInfo.parentWindowHandle == 0:
722723
windowInfo.windowName = window_title
723724

725+
# Browser settings
724726
if not browserSettings:
725727
browserSettings = {}
726-
728+
# CEF Python only settings
729+
if "inherit_client_handlers_for_popups" not in browserSettings:
730+
browserSettings["inherit_client_handlers_for_popups"] = True
727731
cdef CefBrowserSettings cefBrowserSettings
728732
SetBrowserSettings(browserSettings, &cefBrowserSettings)
729733

@@ -774,6 +778,15 @@ def CreateBrowserSync(windowInfo=None,
774778
Debug("CefBrowser window handle = "
775779
+str(<uintptr_t>cefBrowser.get().GetHost().get().GetWindowHandle()))
776780

781+
# Make a copy as browserSettings is a reference only that might
782+
# get destroyed later.
783+
global g_browser_settings
784+
cdef int browser_id = cefBrowser.get().GetIdentifier()
785+
g_browser_settings[browser_id] = {}
786+
for key in browserSettings:
787+
g_browser_settings[browser_id][key] =\
788+
copy.deepcopy(browserSettings[key])
789+
777790
# Request context - part 2/2.
778791
if g_applicationSettings["unique_request_context_per_browser"]:
779792
requestContextHandler.get().SetBrowser(cefBrowser)

src/dpi_aware_win.pyx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ include "cefpython.pyx"
66

77
class DpiAware:
88

9-
@staticmethod
10-
def GetSystemDpi():
9+
@classmethod
10+
def GetSystemDpi(cls):
1111
"""Returns Windows DPI settings ("Custom scaling" on Win10).
1212
1313
Win7 DPI (Control Panel > Appearance and Personalization > Display):
@@ -25,15 +25,15 @@ class DpiAware:
2525
GetSystemDpi(&dpix, &dpiy)
2626
return dpix, dpiy
2727

28-
@staticmethod
29-
def CalculateWindowSize(int width, int height):
28+
@classmethod
29+
def CalculateWindowSize(cls, int width, int height):
3030
"""@DEPRECATED. Use Scale() method instead."""
3131
# Calculation for DPI < 96 is not yet supported.
3232
GetDpiAwareWindowSize(&width, &height)
3333
return width, height
3434

35-
@staticmethod
36-
def Scale(arg):
35+
@classmethod
36+
def Scale(cls, arg):
3737
"""Scale units for high DPI devices. Argument can be an int,
3838
tuple or list."""
3939
(dpix, dpiy) = DpiAware.GetSystemDpi()
@@ -54,17 +54,17 @@ class DpiAware:
5454
return tuple(ret)
5555
return ret
5656

57-
@staticmethod
58-
def IsProcessDpiAware():
57+
@classmethod
58+
def IsProcessDpiAware(cls):
5959
return IsProcessDpiAware()
6060

61-
@staticmethod
62-
def SetProcessDpiAware():
61+
@classmethod
62+
def SetProcessDpiAware(cls):
6363
"""Deprecated."""
6464
DpiAware.EnableHighDpiSupport()
6565

66-
@staticmethod
67-
def EnableHighDpiSupport():
66+
@classmethod
67+
def EnableHighDpiSupport(cls):
6868
# This CEF function sets process to be DPI aware. This
6969
# CEF func is also called in subprocesses.
7070
CefEnableHighDPISupport()

0 commit comments

Comments
 (0)