diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index dcb75da2e..d2f0143fe 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,7 +1,2 @@ -Please do not create Issues. Issues must first be discussed -and confirmed on the Forum. Report issues on the Forum here: - -https://goo.gl/xz4cEF - -Please do not ask questions in existing issues. Ask questions -on the Forum, link above. +Questions should be asked on the forum: +https://groups.google.com/group/cefpython diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..c5663b055 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,12 @@ +# These are supported funding model platforms + +github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: # Replace with a single Patreon username +open_collective: # Replace with a single Open Collective username +ko_fi: # Replace with a single Ko-fi username +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +custom: ['https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=V7LU7PD4N4GGG'] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 2bf4f13a8..d2f0143fe 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,8 +1,2 @@ -Please do not create Issues using this form. Issues must -first be discussed and confirmed on the Forum. Report issues -on the Forum here: - -https://goo.gl/xz4cEF - -Please do not ask questions in existing issues. Ask questions -on the Forum, link above. +Questions should be asked on the forum: +https://groups.google.com/group/cefpython diff --git a/.gitignore b/.gitignore index ed3648c14..b786188bc 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ examples/webrtc_event_logs/ unittests/GPUCache/ unittests/blob_storage/ unittests/webrtc_event_logs/ +.DS_Store diff --git a/Authors b/Authors index 827d05cc5..b44b24e68 100644 --- a/Authors +++ b/Authors @@ -1,16 +1,22 @@ Core developers: -Czarek Tomczak + Czarek Tomczak Contributors: -老农 cjjer -Richard Rodriguez -Roman Plášil -Rokas Stupuras -Greg Kacy -Thomas Dähling -Dominique Burnand -Greg Farrell -Finn Hughes -Marcelo Fernandez -Simon Hatt <9hatt2@@gmail.com> -Neil Munday + 老农 cjjer + Richard Rodriguez + Roman Plášil + Rokas Stupuras + Greg Kacy + Thomas Dähling + Dominique Burnand + Greg Farrell + Finn Hughes + Marcelo Fernandez + Simon Hatt <9hatt2@@gmail.com> + Neil Munday + Joseph Kogut + Paweł Kierski + Dónal McMullan + nobodyguy + Elliot Woods + Bryan Koroleski diff --git a/README.md b/README.md index ddb393810..b85f34c6b 100644 --- a/README.md +++ b/README.md @@ -2,47 +2,124 @@ Table of contents: * [Introduction](#introduction) -* [Sponsors](#sponsors) -* [Funding for the v66 release](#funding-for-the-v66-release) * [Install](#install) -* [Tutorial](#tutorial) * [Examples](#examples) * [Support](#support) * [Support development](#support-development) - * [Thanks](#thanks) -* [Releases](#releases) -* [Other READMEs](#other-readmes) -* [Quick links](#quick-links) +* [Seeking sponsors](#seeking-sponsors) +* [API](#api) ## Introduction -CEF Python is a BSD-licensed open source project founded by Czarek Tomczak (hire me! download my [resume](https://drive.google.com/file/d/17xmoT5Z_zTHkVclqPzrs2aAV64Uiu7fh/view)) in 2012 and is based on Google Chromium and the -[CEF Framework](https://bitbucket.org/chromiumembedded/cef) -projects. The Chromium project focuses mainly on Google Chrome application -development, while CEF focuses on facilitating embedded browser use cases +CEF Python is an open source project founded by +[Czarek Tomczak](https://www.linkedin.com/in/czarektomczak/) +in 2012 to provide Python bindings for the +[Chromium Embedded Framework](https://bitbucket.org/chromiumembedded/cef) (CEF). +The Chromium project focuses mainly on Google Chrome application +development while CEF focuses on facilitating embedded browser use cases in third-party applications. Lots of applications use CEF control, there are more than [100 million CEF instances](http://en.wikipedia.org/wiki/Chromium_Embedded_Framework#Applications_using_CEF) -installed around the world. [Examples of embedding](examples/README-examples.md) -Chrome browser are available for many popular GUI toolkits including: -wxPython, PyGTK, PyQt, PySide, Kivy, Panda3D and PyGame/PyOpenGL. +installed around the world. There are numerous use cases for CEF: -There are many use cases for CEF. You can embed a web browser control -based on Chromium with great HTML 5 support. You can use it to create -a HTML 5 based GUI in an application, this can act as a replacement for -standard GUI toolkits such as wxWidgets, Qt or GTK. In such case to -communicate between Python<>Javascript use [javascript bindings](docs/Tutorial.md#javascript-integration) -or embed an internal web server and talk using http requests. You -can render web content off-screen in applications that use custom -drawing frameworks. You can use it for automated testing of existing -applications. You can use it for web scraping or as a web crawler, -or other kind of internet bots. +1. Use it as a modern HTML5 based rendering engine that can act as + a replacement for classic desktop GUI frameworks. Think of it as Electron + for Python. +2. Embed a web browser widget in a classic Qt / GTK / wxPython desktop + application +3. Render web content off-screen in applications that use custom drawing + frameworks +4. Use it for automated testing of web applications with more advanced + capabilities than Selenium web browser automation due to CEF low level + programming APIs +5. Use it for web scraping, as a web crawler or other kind of internet bots +CEF Python also provides examples of embedding CEF for many Python GUI +frameworks such as PyQt, wxPython, PyGTK, PyGObject, Tkinter, Kivy, Panda3D, +PyGame, PyOpenGL, PyWin32, PySide and PySDL2. -## Sponsors + +## Install + +Command to install with pip: + +``` +pip install cefpython3==66.1 +``` + +Hosted at [pypi/cefpython3](https://pypi.python.org/pypi/cefpython3). On Linux pip 8.1+ is required. + +You can also download packages for offline installation available on the [GitHub Releases](../../releases) pages. + +Below is a table with supported platforms, python versions and architectures. + +OS | Py2 | Py3 | 32bit | 64bit | Requirements +--- | --- | --- | --- | --- | --- +Windows | 2.7 | 3.4 / 3.5 / 3.6 / 3.7 / 3.8 / 3.9 | Yes | Yes | Windows 7+ (Note that Python 3.9 supports Windows 8.1+) +Linux | 2.7 | 3.4 / 3.5 / 3.6 / 3.7 | Yes | Yes | Debian 8+, Ubuntu 14.04+,
Fedora 24+, openSUSE 13.3+ +Mac | 2.7 | 3.4 / 3.5 / 3.6 / 3.7 | No | Yes | MacOS 10.9+ + + +## Examples + +- [Tutorial](docs/Tutorial.md) +- [All examples](examples/README-examples.md) +- [Snippets](examples/snippets/README-snippets.md) +- [PyInstaller packager](examples/pyinstaller/README-pyinstaller.md) + + +## Support + +- Ask questions and report problems on the + [Forum](https://groups.google.com/group/cefpython) +- Supported examples are listed in the + [README-examples.md](examples/README-examples.md) file +- Documentation is in the [docs/](docs) directory: + - [Build instructions](docs/Build-instructions.md) + - [Contributing code](docs/Contributing-code.md) + - [Knowledge Base](docs/Knowledge-Base.md) + - [Migration guide](docs/Migration-guide.md) + - [Tutorial](docs/Tutorial.md) +- API reference is in the [api/](api) directory: + - [API categories](api/API-categories.md#api-categories) + - [API index](api/API-index.md#api-index) +- Additional documentation is available in + [Issues labelled Knowledge Base](../../issues?q=is%3Aissue+is%3Aopen+label%3A%22Knowledge+Base%22) +- To search documentation use GitHub "This repository" search + at the top. To narrow results to documentation only select + "Markdown" in the side pane + +## Support development + +To support general CEF Python development efforts you can make a donation using PayPal button below: + + + +
+ + +## Seeking sponsors + +CEF Python is seeking companies to sponsor development of this project. Most important +thing would be to have continuous monthly releases with updates to latest Chromium. There is +also lots of cool features and new settings that would be nice to implement. We have not yet +exposed all of upstream CEF APIs. If your company would like to sponsor CEF Python development efforts +then please contact [Czarek](https://www.linkedin.com/in/czarektomczak/). There are no active sponsors +at this moment. + + +### Previous sponsors + + + - -
+ +
-See the [Support development](#support-development) section for a list of -all the individuals and companies supporting CEF Python. - - -## Funding for the v66 release - -I am happy to announce that Lampix is sponsoring the v66 release of cefpython. -Lampix is the first hardware and software solution that turns any surface -into a smart, augmented reality or interactive surface. Please visit its -website here: https://lampix.com/ . See the [v66 milestone](../../milestone/13) -to see progress on the new release. - -[![lampix600](https://raw.githubusercontent.com/wiki/cztomczak/cefpython/images/lampix600.png)](https://lampix.com/) - -## Install - -You can install [pypi/cefpython3](https://pypi.python.org/pypi/cefpython3) -package using pip tool. On Linux pip 8.1+ is required. You can -also download packages for offline installation available on the -[GitHub Releases](../../releases) pages. Command to install with pip: - -``` -pip install cefpython3==57.0 -``` - -If you get an error when importing the cefpython3 package on -Windows then see this section in the Knowledge Base document: -[ImportError: DLL load failed (Windows)](docs/Knowledge-Base.md#importerror-dll-load-failed-windows). - -## Tutorial -See the [Tutorial.md](docs/Tutorial.md) file. - - -## Examples - -See the [README-examples.md](examples/README-examples.md) file. - - -## Support - -- Ask questions, report problems and issues on the [Forum](https://groups.google.com/group/cefpython) -- Supported examples are listed in the [README-examples.md](examples/README-examples.md) file -- Documentation is in the [docs/](docs) directory: - - [Build instructions](docs/Build-instructions.md) - - [Contributing code](docs/Contributing-code.md) - - [Knowledge Base](docs/Knowledge-Base.md) - - [Migration guide](docs/Migration-guide.md) - - [Tutorial](docs/Tutorial.md) -- API reference is in the [api/](api) directory: - - [API categories](api/API-categories.md#api-categories) - - [API index](api/API-index.md#api-index) -- Additional documentation is available in [Issues labelled Knowledge Base](../../issues?q=is%3Aissue+is%3Aopen+label%3A%22Knowledge+Base%22) -- To search documentation use GitHub "This repository" search - at the top. To narrow results to documentation only select - "Markdown" in the right pane. -- You can vote on issues in the tracker to let us know which issues are important to you. - To do that add a +1 thumb up reaction to the first post in the issue. See - [Most popular issues](../../issues?q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc) - sorted by reactions. -- Wiki pages are deprecated and for v31 only - - -## Support development - -If you would like to support general CEF Python development efforts -by making a donation please click the Paypal Donate button: - - - -

- -If you would like to see some feature implemented you can make -a comment about that when making a donation. It will give it -a higher priority. - -If you are interested in sponsorship opportunities please contact Czarek -directly - contact details are available in his [Resume](https://drive.google.com/file/d/17xmoT5Z_zTHkVclqPzrs2aAV64Uiu7fh/view). - -If you would like to sponsor CEF Python development efforts and see your company -logo with a link in README, please contact Czarek. -This README page alone has 1,070 visitors and 2,402 views every two weeks, -so your company logo and a website link will be visible to many Python developers. -The statistics are from Mar 9, 2018. Complete statistics are available in this topic -on the Forum: [[ANN] Call for sponsors](https://groups.google.com/d/topic/cefpython/CjkZF2P4rMk/discussion). - -### Thanks - -* [2018] Many thanks to [Lampix](https://lampix.com/) for sponsoring the v66 - release for all platforms -* [2017] Many thanks to [ClearChat Inc.](https://clearchat.com/) for sponsoring - the v55/v56 releases for all platforms -* [2016-2018] Thanks to JetBrains for providing an Open Source license for - [PyCharm](https://www.jetbrains.com/pycharm/) -* [2014] Thanks to Adam Duston for donating a Macbook to aid the development - of Mac port -* [2013-2015] Lots of thanks goes to [Cyan Inc.](http://www.blueplanet.com/) - for sponsoring this project for a long time, making CEF Python 3 mature -* [2013] Thanks to [Rentouch GmbH](http://www.rentouch.ch/) for sponsoring the - development of the off-screen rendering support -* [2013] Thanks to Thomas Wusatiuk for sponsoring the development of the web - response reading features -* [2012-2018] Thanks to those who have made a Paypal donation: - [Rentouch GmbH](http://www.rentouch.ch/), Walter Purvis, Rokas Stupuras, - Alex Rattray, Greg Kacy, Paul Korzhyk, Tomasz Tomanek. -* [2012-2017] Thanks to those who have donated their time through code contributions, - they are listed in the [Authors](Authors) file - - -## Releases - -Information on planned new and current releases, supported platforms, -python versions, architectures and requirements. If you want to -support old operating systems then choose the v31 release. - -**Next release** - -- To see planned new features or bugs to be fixed in the - next release, see the - [next release](../../issues?q=is%3Aissue+is%3Aopen+label%3A%22next+release%22) - label in the tracker -- To see planned new features or bugs to be fixed in the - in one of next releases, see the - [next release 2](../../issues?q=is%3Aissue+is%3Aopen+label%3A%22next+release+2%22) - label in the tracker - -**Latest release** - -OS | Py2 | Py3 | 32bit | 64bit | Requirements ---- | --- | --- | --- | --- | --- -Windows | 2.7 | 3.4 / 3.5 / 3.6 | Yes | Yes | Windows 7+ -Linux | 2.7 | 3.4 / 3.5 / 3.6 | Yes | Yes | Debian 7+ / Ubuntu 12.04+ -Mac | 2.7 | 3.4 / 3.5 / 3.6 | No | Yes | MacOS 10.9+ - -These platforms are not supported yet: -- ARM - see [Issue #267](../../issues/267) -- Android - see [Issue #307](../../issues/307) - -**v31 release** - -OS | Py2 | Py3 | 32bit | 64bit | Requirements ---- | --- | --- | --- | --- | --- -Windows | 2.7 | No | Yes | Yes | Windows XP+ -Linux | 2.7 | No | Yes | Yes | Debian 7+ / Ubuntu 12.04+ -Mac | 2.7 | No | Yes | Yes | MacOS 10.7+ - -Additional information for v31.2 release: -- On Windows/Mac you can install with command: `pip install cefpython3==31.2` -- Downloads are available on [wiki pages](../../wiki#downloads) - and on GitHub Releases tagged [v31.2](../../releases/tag/v31.2). -- Documentation is on [wiki pages](../../wiki) -- API reference is available in revision [169a1b2](../../tree/169a1b20d3cd09879070d41aab28cfa195d2a7d5/docs/api) - - -## Other READMEs - -- [PyInstaller packager](examples/pyinstaller/README-pyinstaller.md) - - - -## Quick links - -### Docs - -- [Build instructions](docs/Build-instructions.md) -- [Knowledge Base](docs/Knowledge-Base.md) -- [Migration guide](docs/Migration-guide.md) -- [Tutorial](docs/Tutorial.md) - - -### API categories +## API #### Modules @@ -295,6 +203,7 @@ Additional information for v31.2 release: #### Client handlers (interfaces) + * [AccessibilityHandler](api/AccessibilityHandler.md#accessibilityhandler-interface) * [DisplayHandler](api/DisplayHandler.md#displayhandler-interface) * [DownloadHandler](api/DownloadHandler.md#downloadhandler) * [FocusHandler](api/FocusHandler.md#focushandler-interface) @@ -317,6 +226,9 @@ Additional information for v31.2 release: ### API index +* [AccessibilityHandler (interface)](api/AccessibilityHandler.md#accessibilityhandler-interface) + * [_OnAccessibilityTreeChange](api/AccessibilityHandler.md#_onaccessibilitytreechange) + * [_OnAccessibilityLocationChange](api/AccessibilityHandler.md#_onaccessibilitylocationchange) * [Application settings](api/ApplicationSettings.md#application-settings) * [accept_language_list](api/ApplicationSettings.md#accept_language_list) * [app_user_model_id](api/ApplicationSettings.md#app_user_model_id) @@ -381,6 +293,7 @@ Additional information for v31.2 release: * [GetNSTextInputContext](api/Browser.md#getnstextinputcontext) * [GetOpenerWindowHandle](api/Browser.md#getopenerwindowhandle) * [GetOuterWindowHandle](api/Browser.md#getouterwindowhandle) + * [GetSetting](api/Browser.md#getsetting) * [GetUrl](api/Browser.md#geturl) * [GetUserData](api/Browser.md#getuserdata) * [GetWindowHandle](api/Browser.md#getwindowhandle) @@ -390,7 +303,9 @@ Additional information for v31.2 release: * [GoForward](api/Browser.md#goforward) * [HandleKeyEventAfterTextInputClient](api/Browser.md#handlekeyeventaftertextinputclient) * [HandleKeyEventBeforeTextInputClient](api/Browser.md#handlekeyeventbeforetextinputclient) + * [HasDevTools](api/Browser.md#hasdevtools) * [HasDocument](api/Browser.md#hasdocument) + * [Invalidate](api/Browser.md#invalidate) * [IsFullscreen](api/Browser.md#isfullscreen) * [IsLoading](api/Browser.md#isloading) * [IsMouseCursorChangeDisabled](api/Browser.md#ismousecursorchangedisabled) @@ -405,6 +320,7 @@ Additional information for v31.2 release: * [Reload](api/Browser.md#reload) * [ReloadIgnoreCache](api/Browser.md#reloadignorecache) * [ReplaceMisspelling](api/Browser.md#replacemisspelling) + * [SetAutoResizeEnabled](api/Browser.md#setautoresizeenabled) * [SetBounds](api/Browser.md#setbounds) * [SendKeyEvent](api/Browser.md#sendkeyevent) * [SendMouseClickEvent](api/Browser.md#sendmouseclickevent) @@ -412,6 +328,7 @@ Additional information for v31.2 release: * [SendMouseWheelEvent](api/Browser.md#sendmousewheelevent) * [SendFocusEvent](api/Browser.md#sendfocusevent) * [SendCaptureLostEvent](api/Browser.md#sendcapturelostevent) + * [SetAccessibilityState](api/Browser.md#setaccessibilitystate) * [SetClientCallback](api/Browser.md#setclientcallback) * [SetClientHandler](api/Browser.md#setclienthandler) * [SetFocus](api/Browser.md#setfocus) @@ -436,6 +353,7 @@ Additional information for v31.2 release: * [default_encoding](api/BrowserSettings.md#default_encoding) * [dom_paste_disabled](api/BrowserSettings.md#dom_paste_disabled) * [file_access_from_file_urls_allowed](api/BrowserSettings.md#file_access_from_file_urls_allowed) + * [inherit_client_handlers_for_popups](api/BrowserSettings.md#inherit_client_handlers_for_popups) * [image_load_disabled](api/BrowserSettings.md#image_load_disabled) * [javascript_disabled](api/BrowserSettings.md#javascript_disabled) * [javascript_close_windows_disallowed](api/BrowserSettings.md#javascript_close_windows_disallowed) @@ -460,19 +378,23 @@ Additional information for v31.2 release: * [ExceptHook](api/cefpython.md#excepthook) * [GetAppSetting](api/cefpython.md#getappsetting) * [GetAppPath](api/cefpython.md#getapppath) + * [GetBrowserByIdentifier](api/cefpython.md#getbrowserbyidentifier) * [GetBrowserByWindowHandle](api/cefpython.md#getbrowserbywindowhandle) * [GetCommandLineSwitch](api/cefpython.md#getcommandlineswitch) + * [GetDataUrl](api/cefpython.md#getdataurl) * [GetGlobalClientCallback](api/cefpython.md#getglobalclientcallback) * [GetModuleDirectory](api/cefpython.md#getmoduledirectory) * [GetVersion](api/cefpython.md#getversion) * [Initialize](api/cefpython.md#initialize) * [IsThread](api/cefpython.md#isthread) + * [LoadCrlSetsFile](api/cefpython.md#loadcrlsetsfile) * [MessageLoop](api/cefpython.md#messageloop) * [MessageLoopWork](api/cefpython.md#messageloopwork) * [PostTask](api/cefpython.md#posttask) * [PostDelayedTask](api/cefpython.md#postdelayedtask) * [QuitMessageLoop](api/cefpython.md#quitmessageloop) * [SetGlobalClientCallback](api/cefpython.md#setglobalclientcallback) + * [SetGlobalClientHandler](api/cefpython.md#setglobalclienthandler) * [SetOsModalLoop](api/cefpython.md#setosmodalloop) * [Shutdown](api/cefpython.md#shutdown) * [Command line switches](api/CommandLineSwitches.md#command-line-switches) @@ -505,6 +427,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) @@ -517,10 +440,12 @@ Additional information for v31.2 release: * [Visit](api/CookieVisitor.md#visit) * [DisplayHandler (interface)](api/DisplayHandler.md#displayhandler-interface) * [OnAddressChange](api/DisplayHandler.md#onaddresschange) + * [OnAutoResize](api/DisplayHandler.md#onautoresize) + * [OnConsoleMessage](api/DisplayHandler.md#onconsolemessage) + * [OnLoadingProgressChange](api/DisplayHandler.md#onloadingprogresschange) + * [OnStatusMessage](api/DisplayHandler.md#onstatusmessage) * [OnTitleChange](api/DisplayHandler.md#ontitlechange) * [OnTooltip](api/DisplayHandler.md#ontooltip) - * [OnStatusMessage](api/DisplayHandler.md#onstatusmessage) - * [OnConsoleMessage](api/DisplayHandler.md#onconsolemessage) * [DownloadHandler](api/DownloadHandler.md#downloadhandler) * [DpiAware (class)](api/DpiAware.md#dpiaware-class) * [CalculateWindowSize](api/DpiAware.md#calculatewindowsize) @@ -528,6 +453,7 @@ Additional information for v31.2 release: * [GetSystemDpi](api/DpiAware.md#getsystemdpi) * [IsProcessDpiAware](api/DpiAware.md#isprocessdpiaware) * [SetProcessDpiAware](api/DpiAware.md#setprocessdpiaware) + * [Scale](api/DpiAware.md#scale) * [DragData (object)](api/DragData.md#dragdata-object) * [IsLink](api/DragData.md#islink) * [IsFragment](api/DragData.md#isfragment) @@ -594,10 +520,10 @@ Additional information for v31.2 release: * [OnPreKeyEvent](api/KeyboardHandler.md#onprekeyevent) * [OnKeyEvent](api/KeyboardHandler.md#onkeyevent) * [LifespanHandler (interface)](api/LifespanHandler.md#lifespanhandler-interface) - * [OnBeforePopup](api/LifespanHandler.md#onbeforepopup) - * [_OnAfterCreated](api/LifespanHandler.md#_onaftercreated) * [DoClose](api/LifespanHandler.md#doclose) + * [_OnAfterCreated](api/LifespanHandler.md#_onaftercreated) * [OnBeforeClose](api/LifespanHandler.md#onbeforeclose) + * [OnBeforePopup](api/LifespanHandler.md#onbeforepopup) * [LoadHandler (interface)](api/LoadHandler.md#loadhandler-interface) * [OnLoadingStateChange](api/LoadHandler.md#onloadingstatechange) * [OnLoadStart](api/LoadHandler.md#onloadstart) @@ -656,7 +582,7 @@ Additional information for v31.2 release: * [ERR_UNSAFE_REDIRECT](api/NetworkError.md#err_unsafe_redirect) * [PaintBuffer (object)](api/PaintBuffer.md#paintbuffer-object) * [GetIntPointer](api/PaintBuffer.md#getintpointer) - * [GetString](api/PaintBuffer.md#getstring) + * [GetBytes](api/PaintBuffer.md#getbytes) * [RenderHandler (interface)](api/RenderHandler.md#renderhandler-interface) * [GetRootScreenRect](api/RenderHandler.md#getrootscreenrect) * [GetViewRect](api/RenderHandler.md#getviewrect) @@ -667,6 +593,7 @@ Additional information for v31.2 release: * [OnPaint](api/RenderHandler.md#onpaint) * [OnCursorChange](api/RenderHandler.md#oncursorchange) * [OnScrollOffsetChanged](api/RenderHandler.md#onscrolloffsetchanged) + * [OnTextSelectionChanged](api/RenderHandler.md#ontextselectionchanged) * [StartDragging](api/RenderHandler.md#startdragging) * [UpdateDragCursor](api/RenderHandler.md#updatedragcursor) * [Request (class)](api/Request.md#request-class) @@ -689,19 +616,21 @@ Additional information for v31.2 release: * [GetResourceType](api/Request.md#getresourcetype) * [GetTransitionType](api/Request.md#gettransitiontype) * [RequestHandler (interface)](api/RequestHandler.md#requesthandler-interface) + * [CanGetCookies](api/RequestHandler.md#cangetcookies) + * [CanSetCookie](api/RequestHandler.md#cansetcookie) + * [GetAuthCredentials](api/RequestHandler.md#getauthcredentials) + * [GetCookieManager](api/RequestHandler.md#getcookiemanager) + * [GetResourceHandler](api/RequestHandler.md#getresourcehandler) * [OnBeforeBrowse](api/RequestHandler.md#onbeforebrowse) + * [_OnBeforePluginLoad](api/RequestHandler.md#_onbeforepluginload) * [OnBeforeResourceLoad](api/RequestHandler.md#onbeforeresourceload) - * [GetResourceHandler](api/RequestHandler.md#getresourcehandler) + * [_OnCertificateError](api/RequestHandler.md#_oncertificateerror) + * [OnQuotaRequest](api/RequestHandler.md#onquotarequest) * [OnResourceRedirect](api/RequestHandler.md#onresourceredirect) * [OnResourceResponse](api/RequestHandler.md#onresourceresponse) - * [GetAuthCredentials](api/RequestHandler.md#getauthcredentials) - * [OnQuotaRequest](api/RequestHandler.md#onquotarequest) - * [GetCookieManager](api/RequestHandler.md#getcookiemanager) + * [OnPluginCrashed](api/RequestHandler.md#onplugincrashed) * [OnProtocolExecution](api/RequestHandler.md#onprotocolexecution) - * [_OnBeforePluginLoad](api/RequestHandler.md#_onbeforepluginload) - * [_OnCertificateError](api/RequestHandler.md#_oncertificateerror) * [OnRendererProcessTerminated](api/RequestHandler.md#onrendererprocessterminated) - * [OnPluginCrashed](api/RequestHandler.md#onplugincrashed) * [ResourceHandler (interface)](api/ResourceHandler.md#resourcehandler-interface) * [ProcessRequest](api/ResourceHandler.md#processrequest) * [GetResponseHeaders](api/ResourceHandler.md#getresponseheaders) diff --git a/api/API-categories.md b/api/API-categories.md index 1f4fc9922..d1d7320e8 100644 --- a/api/API-categories.md +++ b/api/API-categories.md @@ -38,6 +38,7 @@ ### Client handlers (interfaces) + * [AccessibilityHandler](AccessibilityHandler.md#accessibilityhandler-interface) * [DisplayHandler](DisplayHandler.md#displayhandler-interface) * [DownloadHandler](DownloadHandler.md#downloadhandler) * [FocusHandler](FocusHandler.md#focushandler-interface) diff --git a/api/API-index.md b/api/API-index.md index 3382f3633..9535cb5b7 100644 --- a/api/API-index.md +++ b/api/API-index.md @@ -2,6 +2,9 @@ # API index +* [AccessibilityHandler (interface)](AccessibilityHandler.md#accessibilityhandler-interface) + * [_OnAccessibilityTreeChange](AccessibilityHandler.md#_onaccessibilitytreechange) + * [_OnAccessibilityLocationChange](AccessibilityHandler.md#_onaccessibilitylocationchange) * [Application settings](ApplicationSettings.md#application-settings) * [accept_language_list](ApplicationSettings.md#accept_language_list) * [app_user_model_id](ApplicationSettings.md#app_user_model_id) @@ -66,6 +69,7 @@ * [GetNSTextInputContext](Browser.md#getnstextinputcontext) * [GetOpenerWindowHandle](Browser.md#getopenerwindowhandle) * [GetOuterWindowHandle](Browser.md#getouterwindowhandle) + * [GetSetting](Browser.md#getsetting) * [GetUrl](Browser.md#geturl) * [GetUserData](Browser.md#getuserdata) * [GetWindowHandle](Browser.md#getwindowhandle) @@ -75,7 +79,9 @@ * [GoForward](Browser.md#goforward) * [HandleKeyEventAfterTextInputClient](Browser.md#handlekeyeventaftertextinputclient) * [HandleKeyEventBeforeTextInputClient](Browser.md#handlekeyeventbeforetextinputclient) + * [HasDevTools](Browser.md#hasdevtools) * [HasDocument](Browser.md#hasdocument) + * [Invalidate](Browser.md#invalidate) * [IsFullscreen](Browser.md#isfullscreen) * [IsLoading](Browser.md#isloading) * [IsMouseCursorChangeDisabled](Browser.md#ismousecursorchangedisabled) @@ -90,6 +96,7 @@ * [Reload](Browser.md#reload) * [ReloadIgnoreCache](Browser.md#reloadignorecache) * [ReplaceMisspelling](Browser.md#replacemisspelling) + * [SetAutoResizeEnabled](Browser.md#setautoresizeenabled) * [SetBounds](Browser.md#setbounds) * [SendKeyEvent](Browser.md#sendkeyevent) * [SendMouseClickEvent](Browser.md#sendmouseclickevent) @@ -97,6 +104,7 @@ * [SendMouseWheelEvent](Browser.md#sendmousewheelevent) * [SendFocusEvent](Browser.md#sendfocusevent) * [SendCaptureLostEvent](Browser.md#sendcapturelostevent) + * [SetAccessibilityState](Browser.md#setaccessibilitystate) * [SetClientCallback](Browser.md#setclientcallback) * [SetClientHandler](Browser.md#setclienthandler) * [SetFocus](Browser.md#setfocus) @@ -121,6 +129,7 @@ * [default_encoding](BrowserSettings.md#default_encoding) * [dom_paste_disabled](BrowserSettings.md#dom_paste_disabled) * [file_access_from_file_urls_allowed](BrowserSettings.md#file_access_from_file_urls_allowed) + * [inherit_client_handlers_for_popups](BrowserSettings.md#inherit_client_handlers_for_popups) * [image_load_disabled](BrowserSettings.md#image_load_disabled) * [javascript_disabled](BrowserSettings.md#javascript_disabled) * [javascript_close_windows_disallowed](BrowserSettings.md#javascript_close_windows_disallowed) @@ -145,19 +154,23 @@ * [ExceptHook](cefpython.md#excepthook) * [GetAppSetting](cefpython.md#getappsetting) * [GetAppPath](cefpython.md#getapppath) + * [GetBrowserByIdentifier](cefpython.md#getbrowserbyidentifier) * [GetBrowserByWindowHandle](cefpython.md#getbrowserbywindowhandle) * [GetCommandLineSwitch](cefpython.md#getcommandlineswitch) + * [GetDataUrl](cefpython.md#getdataurl) * [GetGlobalClientCallback](cefpython.md#getglobalclientcallback) * [GetModuleDirectory](cefpython.md#getmoduledirectory) * [GetVersion](cefpython.md#getversion) * [Initialize](cefpython.md#initialize) * [IsThread](cefpython.md#isthread) + * [LoadCrlSetsFile](cefpython.md#loadcrlsetsfile) * [MessageLoop](cefpython.md#messageloop) * [MessageLoopWork](cefpython.md#messageloopwork) * [PostTask](cefpython.md#posttask) * [PostDelayedTask](cefpython.md#postdelayedtask) * [QuitMessageLoop](cefpython.md#quitmessageloop) * [SetGlobalClientCallback](cefpython.md#setglobalclientcallback) + * [SetGlobalClientHandler](cefpython.md#setglobalclienthandler) * [SetOsModalLoop](cefpython.md#setosmodalloop) * [Shutdown](cefpython.md#shutdown) * [Command line switches](CommandLineSwitches.md#command-line-switches) @@ -190,6 +203,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) @@ -202,10 +216,12 @@ * [Visit](CookieVisitor.md#visit) * [DisplayHandler (interface)](DisplayHandler.md#displayhandler-interface) * [OnAddressChange](DisplayHandler.md#onaddresschange) + * [OnAutoResize](DisplayHandler.md#onautoresize) + * [OnConsoleMessage](DisplayHandler.md#onconsolemessage) + * [OnLoadingProgressChange](DisplayHandler.md#onloadingprogresschange) + * [OnStatusMessage](DisplayHandler.md#onstatusmessage) * [OnTitleChange](DisplayHandler.md#ontitlechange) * [OnTooltip](DisplayHandler.md#ontooltip) - * [OnStatusMessage](DisplayHandler.md#onstatusmessage) - * [OnConsoleMessage](DisplayHandler.md#onconsolemessage) * [DownloadHandler](DownloadHandler.md#downloadhandler) * [DpiAware (class)](DpiAware.md#dpiaware-class) * [CalculateWindowSize](DpiAware.md#calculatewindowsize) @@ -213,6 +229,7 @@ * [GetSystemDpi](DpiAware.md#getsystemdpi) * [IsProcessDpiAware](DpiAware.md#isprocessdpiaware) * [SetProcessDpiAware](DpiAware.md#setprocessdpiaware) + * [Scale](DpiAware.md#scale) * [DragData (object)](DragData.md#dragdata-object) * [IsLink](DragData.md#islink) * [IsFragment](DragData.md#isfragment) @@ -279,10 +296,10 @@ * [OnPreKeyEvent](KeyboardHandler.md#onprekeyevent) * [OnKeyEvent](KeyboardHandler.md#onkeyevent) * [LifespanHandler (interface)](LifespanHandler.md#lifespanhandler-interface) - * [OnBeforePopup](LifespanHandler.md#onbeforepopup) - * [_OnAfterCreated](LifespanHandler.md#_onaftercreated) * [DoClose](LifespanHandler.md#doclose) + * [_OnAfterCreated](LifespanHandler.md#_onaftercreated) * [OnBeforeClose](LifespanHandler.md#onbeforeclose) + * [OnBeforePopup](LifespanHandler.md#onbeforepopup) * [LoadHandler (interface)](LoadHandler.md#loadhandler-interface) * [OnLoadingStateChange](LoadHandler.md#onloadingstatechange) * [OnLoadStart](LoadHandler.md#onloadstart) @@ -341,7 +358,7 @@ * [ERR_UNSAFE_REDIRECT](NetworkError.md#err_unsafe_redirect) * [PaintBuffer (object)](PaintBuffer.md#paintbuffer-object) * [GetIntPointer](PaintBuffer.md#getintpointer) - * [GetString](PaintBuffer.md#getstring) + * [GetBytes](PaintBuffer.md#getbytes) * [RenderHandler (interface)](RenderHandler.md#renderhandler-interface) * [GetRootScreenRect](RenderHandler.md#getrootscreenrect) * [GetViewRect](RenderHandler.md#getviewrect) @@ -352,6 +369,7 @@ * [OnPaint](RenderHandler.md#onpaint) * [OnCursorChange](RenderHandler.md#oncursorchange) * [OnScrollOffsetChanged](RenderHandler.md#onscrolloffsetchanged) + * [OnTextSelectionChanged](RenderHandler.md#ontextselectionchanged) * [StartDragging](RenderHandler.md#startdragging) * [UpdateDragCursor](RenderHandler.md#updatedragcursor) * [Request (class)](Request.md#request-class) @@ -374,19 +392,21 @@ * [GetResourceType](Request.md#getresourcetype) * [GetTransitionType](Request.md#gettransitiontype) * [RequestHandler (interface)](RequestHandler.md#requesthandler-interface) + * [CanGetCookies](RequestHandler.md#cangetcookies) + * [CanSetCookie](RequestHandler.md#cansetcookie) + * [GetAuthCredentials](RequestHandler.md#getauthcredentials) + * [GetCookieManager](RequestHandler.md#getcookiemanager) + * [GetResourceHandler](RequestHandler.md#getresourcehandler) * [OnBeforeBrowse](RequestHandler.md#onbeforebrowse) + * [_OnBeforePluginLoad](RequestHandler.md#_onbeforepluginload) * [OnBeforeResourceLoad](RequestHandler.md#onbeforeresourceload) - * [GetResourceHandler](RequestHandler.md#getresourcehandler) + * [_OnCertificateError](RequestHandler.md#_oncertificateerror) + * [OnQuotaRequest](RequestHandler.md#onquotarequest) * [OnResourceRedirect](RequestHandler.md#onresourceredirect) * [OnResourceResponse](RequestHandler.md#onresourceresponse) - * [GetAuthCredentials](RequestHandler.md#getauthcredentials) - * [OnQuotaRequest](RequestHandler.md#onquotarequest) - * [GetCookieManager](RequestHandler.md#getcookiemanager) + * [OnPluginCrashed](RequestHandler.md#onplugincrashed) * [OnProtocolExecution](RequestHandler.md#onprotocolexecution) - * [_OnBeforePluginLoad](RequestHandler.md#_onbeforepluginload) - * [_OnCertificateError](RequestHandler.md#_oncertificateerror) * [OnRendererProcessTerminated](RequestHandler.md#onrendererprocessterminated) - * [OnPluginCrashed](RequestHandler.md#onplugincrashed) * [ResourceHandler (interface)](ResourceHandler.md#resourcehandler-interface) * [ProcessRequest](ResourceHandler.md#processrequest) * [GetResponseHeaders](ResourceHandler.md#getresponseheaders) diff --git a/api/AccessibilityHandler.md b/api/AccessibilityHandler.md new file mode 100644 index 000000000..3433e0039 --- /dev/null +++ b/api/AccessibilityHandler.md @@ -0,0 +1,50 @@ +[API categories](API-categories.md) | [API index](API-index.md) + + +# AccessibilityHandler (interface) + +This handler is for use only with off-screen rendering enabled. +See [RenderHandler](RenderHandler.md) for details. + +Implement this interface to receive accessibility notification when +accessibility events have been registered. The methods of this class will +be called on the UI thread. + +Callbacks in this interface are not associated with any specific browser, +thus you must call +`cefpython`.[SetGlobalClientHandler](cefpython.md#setglobalclienthandler) +or [SetGlobalClientCallback](cefpython.md#setglobalclientcallback) +to use them. The callbacks names were prefixed +with "`_`" to distinguish this special behavior. + + +Table of contents: +* [Callbacks](#callbacks) + * [_OnAccessibilityTreeChange](#_onaccessibilitytreechange) + * [_OnAccessibilityLocationChange](#_onaccessibilitylocationchange) + + +## Callbacks + + +### _OnAccessibilityTreeChange + +| Parameter | Type | +| --- | --- | +| value | list | +| __Return__ | void | + +Called after renderer process sends accessibility tree changes to the +browser process. + + +### _OnAccessibilityLocationChange + +| Parameter | Type | +| --- | --- | +| value | list | +| __Return__ | void | + +Called after renderer process sends accessibility location changes to the +browser process. + diff --git a/api/ApplicationSettings.md b/api/ApplicationSettings.md index b5a819009..a9ad20f1b 100644 --- a/api/ApplicationSettings.md +++ b/api/ApplicationSettings.md @@ -185,17 +185,30 @@ Downloads are handled automatically. A default `SaveAs` file dialog provided by (bool) Default: False -EXPERIMENTAL: So far this was tested only on Linux and actually made app - significantly slower. Windows and Mac platforms were not - tested yet. Reported issue in upstream, see [Issue #246] - (https://github.com/cztomczak/cefpython/issues/246) for details. +This option is for use on Mac and Linux only. On Windows for best +performance you should use a multi-threaded message loop instead +of calling CefDoMessageLoopWork in a timer. + +EXPERIMENTAL (Linux): There are issues with this option on Linux. See + [Issue #246](https://github.com/cztomczak/cefpython/issues/246) + for details. It is recommended to use this option as a replacement for calls to cefpython.MessageLoopWork(). CEF Python will do these calls automatically using CEF's OnScheduleMessagePumpWork. This results in improved performance on Windows and Mac and resolves some bugs with missing keyboard events -on these platforms. See [Issue #246] -(https://github.com/cztomczak/cefpython/issues/246) for more details. +on these platforms. See [Issue #246](https://github.com/cztomczak/cefpython/issues/246) +for more details. + +IMPORTANT: Currently there are issues on Mac with both message loop work + and external message pump. In Qt apps calling message loop + work in a timer doesn't work anymore, you have to use external + message pump. In wxPython apps it is required to call a message + loop work in a timer and enable external message pump + both at the same time (an incorrect approach, but it works). + This is just a temporary solution and how this affects + performance was not tested. See [Issue #442](../../../issues/442) + for more details. Description from upstream CEF: > Set to true (1) to control browser process main (UI) thread message pump diff --git a/api/Browser.md b/api/Browser.md index 47b3752b8..b34b90e2f 100644 --- a/api/Browser.md +++ b/api/Browser.md @@ -3,6 +3,9 @@ # Browser (object) +The methods of this object may be called on any thread unless otherwise +indicated in the comments. + Remember to free all browser references when closing app for the browser to shut down cleanly. Otherwise data such as cookies or other storage might not be flushed to disk @@ -45,6 +48,7 @@ Table of contents: * [GetNSTextInputContext](#getnstextinputcontext) * [GetOpenerWindowHandle](#getopenerwindowhandle) * [GetOuterWindowHandle](#getouterwindowhandle) + * [GetSetting](#getsetting) * [GetUrl](#geturl) * [GetUserData](#getuserdata) * [GetWindowHandle](#getwindowhandle) @@ -54,7 +58,9 @@ Table of contents: * [GoForward](#goforward) * [HandleKeyEventAfterTextInputClient](#handlekeyeventaftertextinputclient) * [HandleKeyEventBeforeTextInputClient](#handlekeyeventbeforetextinputclient) + * [HasDevTools](#hasdevtools) * [HasDocument](#hasdocument) + * [Invalidate](#invalidate) * [IsFullscreen](#isfullscreen) * [IsLoading](#isloading) * [IsMouseCursorChangeDisabled](#ismousecursorchangedisabled) @@ -69,6 +75,7 @@ Table of contents: * [Reload](#reload) * [ReloadIgnoreCache](#reloadignorecache) * [ReplaceMisspelling](#replacemisspelling) + * [SetAutoResizeEnabled](#setautoresizeenabled) * [SetBounds](#setbounds) * [SendKeyEvent](#sendkeyevent) * [SendMouseClickEvent](#sendmouseclickevent) @@ -76,6 +83,7 @@ Table of contents: * [SendMouseWheelEvent](#sendmousewheelevent) * [SendFocusEvent](#sendfocusevent) * [SendCaptureLostEvent](#sendcapturelostevent) + * [SetAccessibilityState](#setaccessibilitystate) * [SetClientCallback](#setclientcallback) * [SetClientHandler](#setclienthandler) * [SetFocus](#setfocus) @@ -303,7 +311,7 @@ Passing a python function here is not allowed, it is only possible through [Java | startLine=1 | int | | __Return__ | void | -Execute a string of JavaScript code in this frame. The `sciptURL` parameter is the URL where the script in question can be found, if any. The renderer may request this URL to show the developer the source of the error. The `startLine` parameter is the base line number to use for error reporting. +Execute a string of JavaScript code in this frame. The `scriptURL` parameter is the URL where the script in question can be found, if any. The renderer may request this URL to show the developer the source of the error. The `startLine` parameter is the base line number to use for error reporting. This function executes asynchronously so there is no way to get the returned value. @@ -368,7 +376,7 @@ Returns the focused [Frame](Frame.md) for the browser window. | name | string | | __Return__ | Frame | -Returns the [Frame](Frame.md) with the specified name, or NULL if not found. +Returns the [Frame](Frame.md) with the specified name, or NULL if not found. ### GetFrameByIdentifier @@ -427,7 +435,7 @@ Returns the names of all existing frames. This list does not include the main fr | --- | --- | | __Return__ | tuple(bytes buffer, int width, int height) | -Currently works only on Linux (Issue [#427](../../../issues/427)). +Currently available only on Linux (Issue [#427](../../../issues/427)). Get browser contents as image. Only screen visible contents are returned. @@ -492,6 +500,17 @@ Retrieve the CEF-internal (inner or outer) window handle of the browser that ope Get the most outer window handle. +### GetSetting + +| Parameter | Type | +| --- | --- | +| key | str | +| __Return__ | mixed | + +Get a browser setting. You can set browser settings by passing +`settings` parameter to `cef.CreateBrowserSync`. + + ### GetUrl | | | @@ -581,6 +600,17 @@ Handles a keyDown event prior to passing it through the NSTextInputClient machinery. +### HasDevTools + +| | | +| --- | --- | +| __Return__ | bool | + +Description from upstream CEF: +> Returns true if this browser currently has an associated DevTools browser. +> Must be called on the browser process UI thread. + + ### HasDocument | | | @@ -590,6 +620,23 @@ machinery. Returns true if a document has been loaded in the browser. +### Invalidate + +| | | +| --- | --- | +| element_type | PaintElementType | +| __Return__ | void | + +Description from upstream CEF: +> Invalidate the view. The browser will call CefRenderHandler::OnPaint +> asynchronously. This method is only used when window rendering is +> disabled. + +`PaintElementType` enum values defined in cefpython module: +* PET_VIEW +* PET_POPUP + + ### IsFullscreen | | | @@ -650,6 +697,13 @@ Returns true if window rendering is disabled. Load url in the main frame. +If the url is a local path it needs to start with the `file://` prefix. +If the url contains special characters it may need proper handling. +Starting with v66.1+ it is required for the app code to encode the url +properly. You can use the `pathlib.PurePath.as_uri` in Python 3 +or `urllib.pathname2url` in Python 2 (`urllib.request.pathname2url` +in Python 3) depending on your case. + ### Navigate @@ -735,6 +789,21 @@ If a misspelled word is currently selected in an editable node calling this method will replace it with the specified |word|. +### SetAutoResizeEnabled + +| Parameter | Type | +| --- | --- | +| enabled | bool | +| min_size | list[width, height] | +| max_size | list[width, heifght] | +| __Return__ | void | + +Description from upstream CEF: +> Enable notifications of auto resize via CefDisplayHandler::OnAutoResize. +> Notifications are disabled by default. |min_size| and |max_size| define the +> range of allowed sizes. + + ### SetBounds | Parameter | Type | @@ -844,6 +913,44 @@ Send a focus event to the browser. Send a capture lost event to the browser. +### SetAccessibilityState + +| | | +| --- | --- | +| state | cef_state_t | +| __Return__ | void | + +cef_state_t enum values defined in cefpython module: +- STATE_DEFAULT +- STATE_ENABLED +- STATE_DISABLED + +Description from upstream CEF: +> Set accessibility state for all frames. |accessibility_state| may be +> default, enabled or disabled. If |accessibility_state| is STATE_DEFAULT +> then accessibility will be disabled by default and the state may be further +> controlled with the "force-renderer-accessibility" and +> "disable-renderer-accessibility" command-line switches. If +> |accessibility_state| is STATE_ENABLED then accessibility will be enabled. +> If |accessibility_state| is STATE_DISABLED then accessibility will be +> completely disabled. +> +> For windowed browsers accessibility will be enabled in Complete mode (which +> corresponds to kAccessibilityModeComplete in Chromium). In this mode all +> platform accessibility objects will be created and managed by Chromium's +> internal implementation. The client needs only to detect the screen reader +> and call this method appropriately. For example, on macOS the client can +> handle the @"AXEnhancedUserInterface" accessibility attribute to detect +> VoiceOver state changes and on Windows the client can handle WM_GETOBJECT +> with OBJID_CLIENT to detect accessibility readers. +> +> For windowless browsers accessibility will be enabled in TreeOnly mode +> (which corresponds to kAccessibilityModeWebContentsOnly in Chromium). In +> this mode renderer accessibility is enabled, the full tree is computed, and +> events are passed to CefAccessibiltyHandler, but platform accessibility +> objects are not created. The client may implement platform accessibility +> objects using CefAccessibiltyHandler callbacks if desired. + ### SetClientCallback | Parameter | Type | diff --git a/api/BrowserSettings.md b/api/BrowserSettings.md index 4b1b13af0..ed2b2e50d 100644 --- a/api/BrowserSettings.md +++ b/api/BrowserSettings.md @@ -15,6 +15,7 @@ Table of contents: * [default_encoding](#default_encoding) * [dom_paste_disabled](#dom_paste_disabled) * [file_access_from_file_urls_allowed](#file_access_from_file_urls_allowed) + * [inherit_client_handlers_for_popups](#inherit_client_handlers_for_popups) * [image_load_disabled](#image_load_disabled) * [javascript_disabled](#javascript_disabled) * [javascript_close_windows_disallowed](#javascript_close_windows_disallowed) @@ -110,6 +111,21 @@ in a known order. Equivalent to the `SkColor` type in Chromium. (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. +### inherit_client_handlers_for_popups + + +(bool) Default: True. + +Whether to inherit client handlers and callbacks for popup windows +opened with "window.open". For example when you set a handler using +`browser.SetClientHandler` then this handler will also be resued +for popup windows that are created implicitilly via "window.open" +call in javascript or similar. If you implement +`LifespanHandler.OnBeforePopup` then you can control explicitilly +what handlers are set for the popup browser. To disable the default +behavior set this option to False. + + ### image_load_disabled (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. diff --git a/api/CookieManager.md b/api/CookieManager.md index db53c9e06..b0d06a911 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 @@ -174,11 +198,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 yet. diff --git a/api/DisplayHandler.md b/api/DisplayHandler.md index 4e45efaf1..2c32ec1f4 100644 --- a/api/DisplayHandler.md +++ b/api/DisplayHandler.md @@ -3,18 +3,19 @@ # DisplayHandler (interface) -Implement this interface to handle events related to browser display state. The methods of this class will be called on the UI thread. - -For an example of how to implement handler see [cefpython](cefpython.md).CreateBrowser(). For a list of all handler interfaces see [API > Client handlers](API#Client_handlers). +Implement this interface to handle events related to browser display +state. The methods of this class will be called on the UI thread. Table of contents: * [Callbacks](#callbacks) * [OnAddressChange](#onaddresschange) + * [OnAutoResize](#onautoresize) + * [OnConsoleMessage](#onconsolemessage) + * [OnLoadingProgressChange](#onloadingprogresschange) + * [OnStatusMessage](#onstatusmessage) * [OnTitleChange](#ontitlechange) * [OnTooltip](#ontooltip) - * [OnStatusMessage](#onstatusmessage) - * [OnConsoleMessage](#onconsolemessage) ## Callbacks @@ -32,31 +33,50 @@ Table of contents: Called when a frame's address has changed. -### OnTitleChange + +### OnAutoResize | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| title | string | -| __Return__ | void | +| new_size | list[width, height] | +| __Return__ | bool | -Called when the page title changes. +Description from upstream CEF: +> Called when auto-resize is enabled via CefBrowserHost::SetAutoResizeEnabled +> and the contents have auto-resized. |new_size| will be the desired size in +> view coordinates. Return true if the resize was handled or false for +> default handling. -### OnTooltip +### OnConsoleMessage | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| text_out | list | +| level | int | +| message | string | +| source | string | +| line | int | | __Return__ | bool | -Called when the browser is about to display a tooltip. `text_out[0]` contains the -text that will be displayed in the tooltip. To handle the display of the -tooltip yourself return true. Otherwise, you can optionally modify `text_out[0]` -and then return false to allow the browser to display the tooltip. -When window rendering is disabled the application is responsible for -drawing tooltips and the return value is ignored. +Called to display a console message. Return true to stop the message from +being output to the console. + +|level| can be one of the same values as in ApplicationSettings.[log_severity](ApplicationSettings.md#log_severity). + + +### OnLoadingProgressChange + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| progress | double | +| __Return__ | void | + +Description from upstream CEF: +> Called when the overall page loading progress has changed. |progress| +> ranges from 0.0 to 1.0. ### OnStatusMessage @@ -70,19 +90,28 @@ drawing tooltips and the return value is ignored. Called when the browser receives a status message. -### OnConsoleMessage +### OnTitleChange | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| level | int | -| message | string | -| source | string | -| line | int | -| __Return__ | bool | +| title | string | +| __Return__ | void | -Called to display a console message. Return true to stop the message from -being output to the console. +Called when the page title changes. -|level| can be one of the same values as in ApplicationSettings.[log_severity](ApplicationSettings.md#log_severity). +### OnTooltip + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| text_out | list | +| __Return__ | bool | + +Called when the browser is about to display a tooltip. `text_out[0]` contains the +text that will be displayed in the tooltip. To handle the display of the +tooltip yourself return true. Otherwise, you can optionally modify `text_out[0]` +and then return false to allow the browser to display the tooltip. +When window rendering is disabled the application is responsible for +drawing tooltips and the return value is ignored. diff --git a/api/DpiAware.md b/api/DpiAware.md index a86d85d37..5a335b216 100644 --- a/api/DpiAware.md +++ b/api/DpiAware.md @@ -16,6 +16,7 @@ Table of contents: * [GetSystemDpi](#getsystemdpi) * [IsProcessDpiAware](#isprocessdpiaware) * [SetProcessDpiAware](#setprocessdpiaware) + * [Scale](#scale) @@ -37,13 +38,13 @@ Enabling High DPI support in app can be done by embedding a DPI awareness xml ma | height | int | | __Return__ | tuple | +Deprecated. Use `Scale()` method instead which can handle +non standard DPI settings such as '132%' on Windows 10. + This utility function will adjust width/height using OS DPI settings. For 800/600 with Win7 DPI settings being set to "Larger 150%" will return 1200/900. -Calculation for DPI < 96 is not yet supported. Use -the `GetSystemDpi` method for that. - ### EnableHighDpiSupport @@ -68,20 +69,29 @@ Description from upstream CEF: Returns tuple(int dpix, int dpiy). +Returns Windows DPI settings ("Custom scaling" on Win10). + Win7 DPI (Control Panel > Appearance and Personalization > Display): * text size Larger 150% => dpix/dpiy 144 * text size Medium 125% => dpix/dpiy 120 * text size Smaller 100% => dpix/dpiy 96 -Example zoom levels based on DPI. For use with the ApplicationSettings.`auto_zooming` option. +Example zoom levels based on DPI. For use with the +ApplicationSettings.`auto_zooming` option. * dpix=96 zoomlevel=0.0 * dpix=120 zoomlevel=1.0 * dpix=144 zoomlevel=2.0 * dpix=72 zoomlevel=-1.0 -If DPI awareness wasn't yet enabled, then `GetSystemDpi` will always return a default 96 DPI. +If DPI awareness wasn't yet enabled, then `GetSystemDpi` will always +return a default 96 DPI. + +DPI settings should not be cached. When `SetProcessDpiAware` +is not yet called, then OS returns 96 DPI, even though it +is set to 144 DPI. After DPI Awareness is enabled for the +running process it will return the correct 144 DPI. ### IsProcessDpiAware @@ -106,3 +116,13 @@ See [Issue #358](../../../issues/358) for how the behavior changed in latest CEF. This method now internally calls `EnableHighDpiSupport()`. Enables DPI awareness for the running process. Embedding a DPI manifest in .exe is the prefered way, as it gives more reliable results, otherwise some display bugs may appear (discussed in the "Introduction" section on this page). + + +### Scale + +| Parameter | Type | +| --- | --- | +| size | int/tuple/list | +| __Return__ | tuple | + +Scale units for high DPI devices. diff --git a/api/Frame.md b/api/Frame.md index 87dbc89cd..057129122 100644 --- a/api/Frame.md +++ b/api/Frame.md @@ -228,9 +228,16 @@ Take also a look at a [custom resource handler](ResourceHandler.md). Load the contents of |value| with the specified dummy |url|. |url| should have a standard scheme (for example, http scheme) or behaviors like -link clicks and web security restrictions may not behave as expected. +link clicks and web security restrictions may not behave as expected. LoadString() can be called only after the Renderer process has been created. +If the url is a local path it needs to start with the `file://` prefix. +If the url contains special characters it may need proper handling. +Starting with v66.1+ it is required for the app code to encode the url +properly. You can use the `pathlib.PurePath.as_uri` in Python 3 +or `urllib.pathname2url` in Python 2 (`urllib.request.pathname2url` +in Python 3) depending on your case. + ### LoadUrl diff --git a/api/KeyboardHandler.md b/api/KeyboardHandler.md index f80828054..4735a88a7 100644 --- a/api/KeyboardHandler.md +++ b/api/KeyboardHandler.md @@ -38,7 +38,7 @@ KeyEvent is a dictionary with the following keys: | type | KeyEventType | The type of keyboard event | | modifiers | KeyEventFlags | Bit flags describing any pressed modifier keys | | windows_key_code | int | The Windows key code for the key event. This value is used by the DOM specification. Sometimes it comes directly from the event (i.e. on Windows) and sometimes it's determined using a mapping function. See "chromium/KeyboardCodes.h" for a list of values. | -| native_key_code | int | The actual key code genenerated by the platform | +| native_key_code | int | The actual key code generated by the platform | | is_system_key | bool | Indicates whether the event is considered a "system key" event. For Windows see [WM_SYSKEYDOWN](http://msdn.microsoft.com/en-us/library/ms646286.aspx). This value will always be false on non-Windows platforms. | | character | wchar_t or unsigned short | The character generated by the keystroke | | unmodified_character | wchar_t or unsigned short | Same as 'character' but unmodified by any concurrently-held modifiers (except shift). This is useful for working out shortcut keys. | diff --git a/api/LifespanHandler.md b/api/LifespanHandler.md index d3bad5c36..1794d62d7 100644 --- a/api/LifespanHandler.md +++ b/api/LifespanHandler.md @@ -3,22 +3,71 @@ # LifespanHandler (interface) -Implement this interface to handle events related to browser life span. The methods of this class will be called on the UI thread. +Implement this interface to handle events related to browser life span. +The methods of this class will be called on the UI thread. -For an example of how to implement a handler see [cefpython](cefpython.md).CreateBrowser(). For a list of all handler interfaces see [API > Client handlers](API#Client_handlers). +Related code snippets: +- [onbeforeclose.py](../examples/snippets/onbeforeclose.py) Table of contents: * [Callbacks](#callbacks) - * [OnBeforePopup](#onbeforepopup) - * [_OnAfterCreated](#_onaftercreated) * [DoClose](#doclose) + * [_OnAfterCreated](#_onaftercreated) * [OnBeforeClose](#onbeforeclose) + * [OnBeforePopup](#onbeforepopup) ## Callbacks +### DoClose + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| __Return__ | bool | + +Called when a browser has recieved a request to close. This may result +directly from a call to [Browser](Browser.md).`CloseBrowser` or indirectly +if the +browser is a top-level OS window created by CEF and the user attempts to +close the window. This method will be called after the Javascript +'onunload' event has been fired. It will not be called for browsers after +the associated OS window has been destroyed (for those browsers it is no +longer possible to cancel the close). + +See complete description of this callback in [cef_life_span_handler.h] +(..|src|include|cef_life_span_handler.h). + + +### _OnAfterCreated + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| __Return__ | void | + +Called after a new browser is created. This callback will be the first +notification that references |browser|. + +This callback will be executed during browser creation, thus you must call [cefpython](cefpython.md).SetGlobalClientCallback() to use it. The callback name was prefixed with "`_`" to distinguish this special behavior. + + +### OnBeforeClose + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| __Return__ | void | + +Called just before a browser is destroyed. Release all references to the +browser object and do not attempt to execute any methods on the browser +object after this callback returns. This callback will be the last +notification that references |browser|. See DoClose() documentation for +additional usage information. + + ### OnBeforePopup | Parameter | Type | @@ -58,10 +107,6 @@ Description from upstream CEF: > browser is destroyed before the popup browser creation completes (indicated > by a call to OnAfterCreated for the popup browser). -Note that if you return True and create the popup window yourself, then -the popup window and parent window will not be able to script each other. -There will be no "window.opener" property available in the popup window. - `WindowOpenDisposition` constants in the cefpython module: * WOD_UNKNOWN, * WOD_CURRENT_TAB, @@ -74,49 +119,10 @@ There will be no "window.opener" property available in the popup window. * WOD_OFF_THE_RECORD, * WOD_IGNORE_ACTION - -### _OnAfterCreated - -| Parameter | Type | -| --- | --- | -| browser | [Browser](Browser.md) | -| __Return__ | void | - -Called after a new browser is created. This callback will be the first -notification that references |browser|. - -This callback will be executed during browser creation, thus you must call [cefpython](cefpython.md).SetGlobalClientCallback() to use it. The callback name was prefixed with "`_`" to distinguish this special behavior. - - -### DoClose - -| Parameter | Type | -| --- | --- | -| browser | [Browser](Browser.md) | -| __Return__ | bool | - -Called when a browser has recieved a request to close. This may result -directly from a call to [Browser](Browser.md).`CloseBrowser` or indirectly -if the -browser is a top-level OS window created by CEF and the user attempts to -close the window. This method will be called after the Javascript -'onunload' event has been fired. It will not be called for browsers after -the associated OS window has been destroyed (for those browsers it is no -longer possible to cancel the close). - -See complete description of this callback in [cef_life_span_handler.h] -(..|src|include|cef_life_span_handler.h). - - -### OnBeforeClose - -| Parameter | Type | -| --- | --- | -| browser | [Browser](Browser.md) | -| __Return__ | void | - -Called just before a browser is destroyed. Release all references to the -browser object and do not attempt to execute any methods on the browser -object after this callback returns. This callback will be the last -notification that references |browser|. See DoClose() documentation for -additional usage information. +Note that if you return True and create the popup window yourself, then +the popup window and parent window will not be able to script each other. +There will be no "window.opener" property available in the popup window. +To avoid this issue create a hidden window when your application starts. +Parent the new popup browser to the hidden window in OnBeforePopup. After +the browser exists (OnAfterCreated) create the desired target window +and re-parent the browser to that target window. diff --git a/api/PaintBuffer.md b/api/PaintBuffer.md index edd9376d3..c6509db21 100644 --- a/api/PaintBuffer.md +++ b/api/PaintBuffer.md @@ -9,7 +9,7 @@ This object used in: [RenderHandler](RenderHandler.md).OnPaint(). Table of contents: * [Methods](#methods) * [GetIntPointer](#getintpointer) - * [GetString](#getstring) + * [GetBytes](#getbytes) ## Methods @@ -28,7 +28,7 @@ Description from upstream CEF: > image with an upper-left origin. -### GetString +### GetBytes | Parameter | Type | | --- | --- | diff --git a/api/RenderHandler.md b/api/RenderHandler.md index bb25ac01d..a33a2154f 100644 --- a/api/RenderHandler.md +++ b/api/RenderHandler.md @@ -22,6 +22,7 @@ Table of contents: * [OnPaint](#onpaint) * [OnCursorChange](#oncursorchange) * [OnScrollOffsetChanged](#onscrolloffsetchanged) + * [OnTextSelectionChanged](#ontextselectionchanged) * [StartDragging](#startdragging) * [UpdateDragCursor](#updatedragcursor) @@ -177,6 +178,25 @@ Called when the browser's cursor has changed. If |type| is CT_CUSTOM then Called when the scroll offset has changed. +### OnTextSelectionChanged + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| selected_text | str | +| selected_range | list[x, y] | +| __Return__ | void | + +Description from upstream CEF: +> Called when text selection has changed for the specified |browser|. +> |selected_text| is the currently selected text and |selected_range| is +> the character range. + +NOTE: this callback seems to be called only when selecting text +with a mouse. When selecting text programmatically using javascript +code it doesn't get called. + + ### StartDragging | Parameter | Type | diff --git a/api/Request.md b/api/Request.md index 3fcf50320..d9d0ece38 100644 --- a/api/Request.md +++ b/api/Request.md @@ -95,9 +95,10 @@ Set the request method type. | --- | --- | | __Return__ | list/dict | -Get the post data. If the form content type is "multipart/form-data" -then the post data will be returned as a list. If the form content -type is "application/x-www-form-urlencoded" then the post data will +Get the post data. All strings are byte strings. If the form content +type is "multipart/form-data" then the post data will be returned +as a list. If the form content type is +"application/x-www-form-urlencoded" then the post data will be returned as a dict. @@ -108,8 +109,8 @@ be returned as a dict. | postData | list/dict | | __Return__ | void | -Set the post data. See GetPostData() for an explanation of the -postData type. +Set the post data. All strings are expected to be byte strings. +See GetPostData() for an explanation of the postData type. ### GetHeaderMap diff --git a/api/RequestHandler.md b/api/RequestHandler.md index 7d7423cfb..f1ef8bf72 100644 --- a/api/RequestHandler.md +++ b/api/RequestHandler.md @@ -4,134 +4,69 @@ # RequestHandler (interface) Implement this interface to handle events related to browser requests. +The methods of this class will be called on the thread indicated. -For an example of how to implement handler see -[cefpython](cefpython.md).CreateBrowserSync(). For a list of all handler -interfaces see [API > Client handlers](API#Client_handlers). - -The `RequestHandler` tests can be found in the old wxpython.py script (v31). - -Not yet exposed in CEF Python: -* OnOpenURLFromTab -* OnSelectClientCertificate +Related code snippets: +- [network_cookies.py](../examples/snippets/network_cookies.py) +Available in upstream CEF, but not yet exposed to CEF Python: +- OnOpenURLFromTab +- OnSelectClientCertificate Table of contents: * [Callbacks](#callbacks) + * [CanGetCookies](#cangetcookies) + * [CanSetCookie](#cansetcookie) + * [GetAuthCredentials](#getauthcredentials) + * [GetCookieManager](#getcookiemanager) + * [GetResourceHandler](#getresourcehandler) * [OnBeforeBrowse](#onbeforebrowse) + * [_OnBeforePluginLoad](#_onbeforepluginload) * [OnBeforeResourceLoad](#onbeforeresourceload) - * [GetResourceHandler](#getresourcehandler) + * [_OnCertificateError](#_oncertificateerror) + * [OnQuotaRequest](#onquotarequest) * [OnResourceRedirect](#onresourceredirect) * [OnResourceResponse](#onresourceresponse) - * [GetAuthCredentials](#getauthcredentials) - * [OnQuotaRequest](#onquotarequest) - * [GetCookieManager](#getcookiemanager) + * [OnPluginCrashed](#onplugincrashed) * [OnProtocolExecution](#onprotocolexecution) - * [_OnBeforePluginLoad](#_onbeforepluginload) - * [_OnCertificateError](#_oncertificateerror) * [OnRendererProcessTerminated](#onrendererprocessterminated) - * [OnPluginCrashed](#onplugincrashed) ## Callbacks -### OnBeforeBrowse +### CanGetCookies | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | | frame | [Frame](Frame.md) | | request | [Request](Request.md) | -| user_gesture | bool | -| is_redirect | bool | | __Return__ | bool | Description from upstream CEF: -> Called on the UI thread before browser navigation. Return true to cancel -> the navigation or false to allow the navigation to proceed. The |request| -> object cannot be modified in this callback. -> CefLoadHandler::OnLoadingStateChange will be called twice in all cases. -> If the navigation is allowed CefLoadHandler::OnLoadStart and -> CefLoadHandler::OnLoadEnd will be called. If the navigation is canceled -> CefLoadHandler::OnLoadError will be called with an |errorCode| value of -> ERR_ABORTED. The |user_gesture| value will be true if the browser -> navigated via explicit user gesture (e.g. clicking a link) or false if it -> navigated automatically (e.g. via the DomContentLoaded event). +> Called on the IO thread before sending a network request with a "Cookie" +> request header. Return true to allow cookies to be included in the network +> request or false to block cookies. The |request| object should not be +> modified in this callback. -### OnBeforeResourceLoad +### CanSetCookie | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | | frame | [Frame](Frame.md) | | request | [Request](Request.md) | +| cookie | [Cookie](Cookie.md) | | __Return__ | bool | -Called on the IO thread before a resource request is loaded. The |request| -object may be modified. To cancel the request return true otherwise return -false. - - -### GetResourceHandler - -| Parameter | Type | -| --- | --- | -| browser | [Browser](Browser.md) | -| frame | [Frame](Frame.md) | -| request | [Request](Request.md) | -| __Return__ | [ResourceHandler](ResourceHandler.md) | - -Called on the IO thread before a resource is loaded. To allow the resource -to load normally return None. To specify a handler for the resource return -a [ResourceHandler](ResourceHandler.md) object. The |request| object should -not be modified in this callback. - -The [ResourceHandler](ResourceHandler.md) object is a python class that -implements the `ResourceHandler` callbacks. Remember to keep a strong -reference to this object while resource is being loaded. - -The `GetResourceHandler` example can be found in the old v31 -"wxpython-response.py" script on Linux. - - -### OnResourceRedirect - -| Parameter | Type | -| --- | --- | -| browser | [Browser](Browser.md) | -| frame | [Frame](Frame.md) | -| old_url | string | -| new_url_out | list[string] | -| request | [Request](Request.md) | -| response | [Response](Response.md) | -| __Return__ | void | - Description from upstream CEF: -> Called on the IO thread when a resource load is redirected. The |request| -> parameter will contain the old URL and other request-related information. -> The |response| parameter will contain the response that resulted in the -> redirect. The |new_url| parameter will contain the new URL and can be -> changed if desired. The |request| object cannot be modified in this -> callback. - - -### OnResourceResponse - -| | | -| --- | --- | -| __Return__ | void | - -Available in upstream CEF, but not yet exposed to CEF Python. -See Issue #229. - -You can implement this functionality by using -[ResourceHandler](ResourceHandler.md) and [WebRequest](WebRequest.md) -/ [WebRequestClient](WebRequestClient.md). For an example see the -_OnResourceResponse() method in the old v31 [wxpython-response.py] -example. +> Called on the IO thread when receiving a network request with a +> "Set-Cookie" response header value represented by |cookie|. Return true to +> allow the cookie to be stored or false to block the cookie. The |request| +> object should not be modified in this callback. ### GetAuthCredentials @@ -171,28 +106,6 @@ Example implementations: [[3]](https://github.com/cztomczak/cefpython/blob/cefpython31/cefpython/http_authentication_win.pyx). -### OnQuotaRequest - -| Parameter | Type | -| --- | --- | -| browser | [Browser](Browser.md) | -| origin_url | string | -| new_size | long | -| callback | RequestCallback | -| __Return__ | bool | - -Called on the IO thread when javascript requests a specific storage quota -size via the `webkitStorageInfo.requestQuota` function. |origin_url| is the -origin of the page making the request. |new_size| is the requested quota -size in bytes. Return true to continue the request and call -CefRequestCallback::Continue() either in this method or at a later time to -grant or deny the request. Return false to cancel the request immediately. - -The `RequestCallback` object methods: -* void Continue(bool allow) -* void Cancel() - - ### GetCookieManager | Parameter | Type | @@ -215,6 +128,12 @@ to True. Otherwise the browser param passed to this callback will always be the same first browser that was created using [cefpython](cefpython.md).`CreateBrowserSync`. +**NOTE**: If implementing custom cookie managers you will encounter +problems similar to [Issue #365](../../../issues/365) ("Cookies not +flushed to disk when closing app immediately"). To resolve +it you have to call CookieManager.[FlushStore](CookieManager.md#flushstore) +method when closing associated browser. + Popup browsers created javascript's window.open share the same renderer process and request context. If you want to have separate cookie managers for popups created using window.open then you have @@ -225,27 +144,50 @@ The `CreateAnotherBrowser` function from the old v31 wxpython example does that. - -### OnProtocolExecution +### GetResourceHandler | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| url | string | -| allow_execution_out | list[bool] | -| __Return__ | void | +| frame | [Frame](Frame.md) | +| request | [Request](Request.md) | +| __Return__ | [ResourceHandler](ResourceHandler.md) | -Called on the UI thread to handle requests for URLs with an unknown -protocol component. Set |allow_execution_out[0]| to True to attempt -execution via the registered OS protocol handler, if any. +Called on the IO thread before a resource is loaded. To allow the resource +to load normally return None. To specify a handler for the resource return +a [ResourceHandler](ResourceHandler.md) object. The |request| object should +not be modified in this callback. -__SECURITY NOTE__: You should use this callback to enforce restrictions -based on scheme, host or other url analysis before allowing OS execution. +The [ResourceHandler](ResourceHandler.md) object is a python class that +implements the `ResourceHandler` callbacks. Remember to keep a strong +reference to this object while resource is being loaded. + +The `GetResourceHandler` example can be found in the old v31 +"wxpython-response.py" script on Linux. -There's no default implementation for OnProtocolExecution on Linux, -you have to make OS system call on your own. You probably also need -to use [LoadHandler](LoadHandler.md)::OnLoadError() when implementing -this on Linux. + +### OnBeforeBrowse + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| frame | [Frame](Frame.md) | +| request | [Request](Request.md) | +| user_gesture | bool | +| is_redirect | bool | +| __Return__ | bool | + +Description from upstream CEF: +> Called on the UI thread before browser navigation. Return true to cancel +> the navigation or false to allow the navigation to proceed. The |request| +> object cannot be modified in this callback. +> CefLoadHandler::OnLoadingStateChange will be called twice in all cases. +> If the navigation is allowed CefLoadHandler::OnLoadStart and +> CefLoadHandler::OnLoadEnd will be called. If the navigation is canceled +> CefLoadHandler::OnLoadError will be called with an |errorCode| value of +> ERR_ABORTED. The |user_gesture| value will be true if the browser +> navigated via explicit user gesture (e.g. clicking a link) or false if it +> navigated automatically (e.g. via the DomContentLoaded event). ### _OnBeforePluginLoad @@ -278,7 +220,6 @@ Description from upstream CEF: > purge the plugin list cache and potentially trigger new calls to this > method call CefRequestContext::PurgePluginListCache. - Return True to block loading of the plugin. This callback will be executed during browser creation, thus you must @@ -291,6 +232,20 @@ This callback is called every time the page tries to load a plugin (perhaps even multiple times per plugin). +### OnBeforeResourceLoad + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| frame | [Frame](Frame.md) | +| request | [Request](Request.md) | +| __Return__ | bool | + +Called on the IO thread before a resource request is loaded. The |request| +object may be modified. To cancel the request return true otherwise return +false. + + ### _OnCertificateError | Parameter | Type | @@ -317,21 +272,63 @@ The `RequestCallback` object methods: * void Cancel() -### OnRendererProcessTerminated +### OnQuotaRequest | Parameter | Type | | --- | --- | | browser | [Browser](Browser.md) | -| status | TerminationStatus | +| origin_url | string | +| new_size | long | +| callback | RequestCallback | +| __Return__ | bool | + +Called on the IO thread when javascript requests a specific storage quota +size via the `webkitStorageInfo.requestQuota` function. |origin_url| is the +origin of the page making the request. |new_size| is the requested quota +size in bytes. Return true to continue the request and call +CefRequestCallback::Continue() either in this method or at a later time to +grant or deny the request. Return false to cancel the request immediately. + +The `RequestCallback` object methods: +* void Continue(bool allow) +* void Cancel() + + +### OnResourceRedirect + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| frame | [Frame](Frame.md) | +| old_url | string | +| new_url_out | list[string] | +| request | [Request](Request.md) | +| response | [Response](Response.md) | | __Return__ | void | -Called when the render process terminates unexpectedly. |status| indicates -how the process terminated. +Description from upstream CEF: +> Called on the IO thread when a resource load is redirected. The |request| +> parameter will contain the old URL and other request-related information. +> The |response| parameter will contain the response that resulted in the +> redirect. The |new_url| parameter will contain the new URL and can be +> changed if desired. The |request| object cannot be modified in this +> callback. -`TerminationStatus` constants in the cefpython module: - * TS_ABNORMAL_TERMINATION - Non-zero exit status. - * TS_PROCESS_WAS_KILLED - SIGKILL or task manager kill. - * TS_PROCESS_CRASHED - Segmentation fault. + +### OnResourceResponse + +| | | +| --- | --- | +| __Return__ | void | + +Available in upstream CEF, but not yet exposed to CEF Python. +See Issue #229. + +You can implement this functionality by using +[ResourceHandler](ResourceHandler.md) and [WebRequest](WebRequest.md) +/ [WebRequestClient](WebRequestClient.md). For an example see the +_OnResourceResponse() method in the old v31 [wxpython-response.py] +example. ### OnPluginCrashed @@ -344,3 +341,42 @@ how the process terminated. Called when a plugin has crashed. |plugin_path| is the path of the plugin that crashed. + + +### OnProtocolExecution + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| url | string | +| allow_execution_out | list[bool] | +| __Return__ | void | + +Called on the UI thread to handle requests for URLs with an unknown +protocol component. Set |allow_execution_out[0]| to True to attempt +execution via the registered OS protocol handler, if any. + +__SECURITY NOTE__: You should use this callback to enforce restrictions +based on scheme, host or other url analysis before allowing OS execution. + +There's no default implementation for OnProtocolExecution on Linux, +you have to make OS system call on your own. You probably also need +to use [LoadHandler](LoadHandler.md)::OnLoadError() when implementing +this on Linux. + + +### OnRendererProcessTerminated + +| Parameter | Type | +| --- | --- | +| browser | [Browser](Browser.md) | +| status | TerminationStatus | +| __Return__ | void | + +Called when the render process terminates unexpectedly. |status| indicates +how the process terminated. + +`TerminationStatus` constants in the cefpython module: + * TS_ABNORMAL_TERMINATION - Non-zero exit status. + * TS_PROCESS_WAS_KILLED - SIGKILL or task manager kill. + * TS_PROCESS_CRASHED - Segmentation fault. diff --git a/api/V8ContextHandler.md b/api/V8ContextHandler.md index 9c6f6fbc3..b88a86418 100644 --- a/api/V8ContextHandler.md +++ b/api/V8ContextHandler.md @@ -3,7 +3,9 @@ # V8ContextHandler (interface) -Implement this interface to handle javascript exceptions globally. +Implement this interface to handle render process callbacks. +Through inter-process messaging you are notified about these events +in the browser process. Table of contents: diff --git a/api/WebRequestClient.md b/api/WebRequestClient.md index d8431cecc..0c75a668d 100644 --- a/api/WebRequestClient.md +++ b/api/WebRequestClient.md @@ -65,7 +65,7 @@ response (or -1 if not determined). | Parameter | Type | | --- | --- | | web_request | [WebRequest](WebRequest.md) | -| data | string | +| data | bytes | | __Return__ | void | Called when some part of the response is read. |data| contains the current diff --git a/api/cefpython.md b/api/cefpython.md index e68d02313..ae7d255c3 100644 --- a/api/cefpython.md +++ b/api/cefpython.md @@ -13,19 +13,23 @@ Table of contents: * [ExceptHook](#excepthook) * [GetAppSetting](#getappsetting) * [GetAppPath](#getapppath) + * [GetBrowserByIdentifier](#getbrowserbyidentifier) * [GetBrowserByWindowHandle](#getbrowserbywindowhandle) * [GetCommandLineSwitch](#getcommandlineswitch) + * [GetDataUrl](#getdataurl) * [GetGlobalClientCallback](#getglobalclientcallback) * [GetModuleDirectory](#getmoduledirectory) * [GetVersion](#getversion) * [Initialize](#initialize) * [IsThread](#isthread) + * [LoadCrlSetsFile](#loadcrlsetsfile) * [MessageLoop](#messageloop) * [MessageLoopWork](#messageloopwork) * [PostTask](#posttask) * [PostDelayedTask](#postdelayedtask) * [QuitMessageLoop](#quitmessageloop) * [SetGlobalClientCallback](#setglobalclientcallback) + * [SetGlobalClientHandler](#setglobalclienthandler) * [SetOsModalLoop](#setosmodalloop) * [Shutdown](#shutdown) @@ -55,6 +59,13 @@ All parameters are optional. This function can only be called on the UI thread. +If the url is a local path it needs to start with the `file://` prefix. +If the url contains special characters it may need proper handling. +Starting with v66.1+ it is required for the app code to encode the url +properly. You can use the `pathlib.PurePath.as_uri` in Python 3 +or `urllib.pathname2url` in Python 2 (`urllib.request.pathname2url` +in Python 3) depending on your case. + The "window_title" parameter will be used only when parent window provided in window_info was set to 0. This is for use with hello_world.py and tutorial.py examples which don't use @@ -62,7 +73,8 @@ any third party GUI framework for creation of top-level window. After the call to CreateBrowserSync() the page is not yet loaded, if you want your next lines of code to do some stuff on the -webpage you will have to implement LoadHandler.[OnLoadingStateChange]((LoadHandler.md#onloadingstatechange)) +webpage you will have to implement +LoadHandler.[OnLoadingStateChange](LoadHandler.md#onloadingstatechange) callback. @@ -106,6 +118,17 @@ to Initialize(). Returns None if key is not found. Get path to where application resides. +### GetBrowserByIdentifier + +| Parameter | Type | +| --- | --- | +| identifier | int | +| __Return__ | void | + +Get browser by identifier. Browser identifier can be obtained by +calling `Browser.GetIdentifier`. + + ### GetBrowserByWindowHandle | Parameter | Type | @@ -126,6 +149,20 @@ Get browser by outer or inner window handle. An outer window handle is the one t Returns the [CommandLineSwitches](CommandLineSwitches.md) switch that was passed to Initialize(). Returns None if key is not found. +### GetDataUrl + +| Parameter | Type | +| --- | --- | +| data | string | +| mediatype="html" (optional) | string | +| __Return__ | object | + +Convert data to a Data URL. Only "html" media type is currently supported. + +See: +https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs + + ### GetGlobalClientCallback | Parameter | Type | @@ -133,7 +170,8 @@ Returns the [CommandLineSwitches](CommandLineSwitches.md) switch that was passed | name | string | | __Return__ | object | -Returns a global client callback that was set using SetGlobalClientCallback(). Returns None if callback was not set. +Returns a global client callback that was set using SetGlobalClientCallback() +or SetGlobalClientHandler. Returns None if callback was not set. ### GetModuleDirectory @@ -186,6 +224,22 @@ CEF maintains multiple internal threads that are used for handling different typ See PostTask() for a list of threads. +### LoadCrlSetsFile + +| Parameter | Type | +| --- | --- | +| path | bytes | +| __Return__ | bool | + +Description from upstream CEF: +> Loads the existing "Certificate Revocation Lists" file that is managed by +> Google Chrome. This file can generally be found in Chrome's User Data +> directory (e.g. "C:\Users\[User]\AppData\Local\Google\Chrome\User Data\" on +> Windows) and is updated periodically by Chrome's component updater service. +> Must be called in the browser process after the context has been initialized. +> See https://dev.chromium.org/Home/chromium-security/crlsets for background. + + ### MessageLoop | | | @@ -193,7 +247,7 @@ See PostTask() for a list of threads. | __Return__ | void | Run the CEF message loop. Use this function instead of an application- -provided message loop to get the best balance between performance and +provided message loop to get the best balance between performance and CPU usage. This function should only be called on the main application thread (UI thread) and only if cefpython.Initialize() is called with a [ApplicationSettings](ApplicationSettings.md).multi_threaded_message_loop @@ -290,6 +344,23 @@ Some client callbacks are not associated with any browser. In such case use this Example of using SetGlobalClientCallback() is provided in the wxpython.py example. +### SetGlobalClientHandler + +| Parameter | Type | +| --- | --- | +| handler | object | +| __Return__ | void | + +Set client handler object (class instance). Its members will be inspected. +Private methods that are not meant to be callbacks should have their names +prepended with two underscores. Methods with single underscore or no +underscore are treated the same as client callbacks. + +You can call this method multiple times to set many handlers. For +example you can create in your code several objects named AccessibilityHandler, +RequestHandler etc. + + ### SetOsModalLoop | Parameter | Type | diff --git a/docs/Build-instructions.md b/docs/Build-instructions.md index 5e698af4e..9ca4d6e99 100644 --- a/docs/Build-instructions.md +++ b/docs/Build-instructions.md @@ -66,8 +66,9 @@ are named "cefpythonXX" where XX is Chromium version number. from [here](https://www.microsoft.com/en-us/download/details.aspx?id=44266) 5) For Python 2.7 and when using using "Visual C++ compiler for Python 2.7" - you have to install "Visual C++ 2008 Redistributable Package (x64)" - from [here](https://www.microsoft.com/en-us/download/details.aspx?id=15336) + you have to install "Visual C++ 2008 Redistributable Package" + from [here](https://www.microsoft.com/en-us/download/details.aspx?id=29) + and [here](https://www.microsoft.com/en-us/download/details.aspx?id=15336) 6) Clone cefpython, checkout for example "cefpython57" branch that includes Chromium v57, then create a build/ directory and enter it: @@ -164,7 +165,8 @@ requirements common for all platforms. * For Python 2.7 install "Microsoft Visual C++ Compiler for Python 2.7" from [here](https://www.microsoft.com/en-us/download/details.aspx?id=44266) * When using "Visual C++ compiler for Python 2.7" you have to install - "Microsoft Visual C++ 2008 Redistributable Package (x64)" from + "Microsoft Visual C++ 2008 Redistributable Package" from + [here](https://www.microsoft.com/en-us/download/details.aspx?id=29) and [here](https://www.microsoft.com/en-us/download/details.aspx?id=15336) * For Python 2.7 copy "cefpython/src/windows/py27/stdint.h" to "%LocalAppData%\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include\" @@ -193,7 +195,8 @@ requirements common for all platforms. * Install packages: `sudo apt-get install cmake g++ libgtk2.0-dev libgtkglext1-dev` * If building CEF from sources: - * Official binaries are built on Ubuntu 14.04 (cmake 2.8.12, g++ 4.8.4) + * Official binaries are built on Ubuntu 14.04 (cmake 2.8.12, g++ 4.8.4) and these instructions apply to that OS + * For Fedora build dependencies see [Issue #466](https://github.com/cztomczak/cefpython/issues/466#issuecomment-419794341) * Download [ninja](https://github.com/ninja-build/ninja/releases) 1.7.1 or later and copy it to /usr/bin and chmod 755. * Install/upgrade required packages using one of the four methods below diff --git a/docs/Contributing-code.md b/docs/Contributing-code.md index bbcefc260..fb56e115f 100644 --- a/docs/Contributing-code.md +++ b/docs/Contributing-code.md @@ -12,6 +12,7 @@ Table of contents: * [API docs](#api-docs) * [Unit tests](#unit-tests) * [Platforms](#platforms) +* [GIL](#gil) * [Authors](#authors) * [Updating CEF version](#updating-cef-version) @@ -123,6 +124,24 @@ In most cases new code should run fine on all platforms, but in some cases it might be required to test on all platforms before PR is merged. +## GIL + +In the pxd file, functions should be defined as "nogil" to avoid +deadlocks when calling CEF functions. In the pyx file the call must +use the "with nogil" statement. + +From [Cython's documentation](http://docs.cython.org/src/userguide/external_C_code.html#acquiring-and-releasing-the-gil): + +> Note that acquiring the GIL is a blocking thread-synchronising operation, +> and therefore potentially costly. It might not be worth releasing the GIL +> for minor calculations. Usually, I/O operations and substantial computations +> in parallel code will benefit from it. + +Revision [ec1ce78](https://github.com/cztomczak/cefpython/commit/ec1ce788373bb9e0fd2cedd71e900c3877e9185a) removes the GIL lock from the +following calls: Initialize(), Shutdown(), CreateBrowserSync(), +SetOsModalLoop(), QuitMessageLoop(), CefExecuteProcess(). There still +might be some more functions from which the GIL lock should be removed. + ## Authors In your pull request modify also the [Authors](../Authors) file diff --git a/docs/Knowledge-Base.md b/docs/Knowledge-Base.md index cf1deba52..c68f4cc06 100644 --- a/docs/Knowledge-Base.md +++ b/docs/Knowledge-Base.md @@ -1,11 +1,11 @@ # Knowledge Base Table of contents: -* [ImportError: DLL load failed (Windows)](#importerror-dll-load-failed-windows) * [Notifications about new releases / commits](#notifications-about-new-releases--commits) * [Changes in API after CEF updates](#changes-in-api-after-cef-updates) * [Differences between Python 2 and Python 3](#differences-between-python-2-and-python-3) * [How to enable debug information in examples?](#how-to-enable-debug-information-in-examples) +* [Remote debugging with Google Chrome instance](#remote-debugging-with-google-chrome-instance) * [Debugging using various chrome:// protocol uris](#debugging-using-various-chrome-protocol-uris) * [A blank window on Mac/Linux](#a-blank-window-on-maclinux) * [Location of CEF framework in Mac apps](#location-of-cef-framework-in-mac-apps) @@ -20,40 +20,6 @@ Table of contents: * [Security](#security) -## ImportError: DLL load failed (Windows) - -If you get such an error on Windows: -```Text -import cefpython3 -Traceback (most recent call last): - File "", line 1, in - File "C:\Python3\lib\site-packages\cefpython3\__init__.py", line 59, in - from . import cefpython_py36 as cefpython -ImportError: DLL load failed: The specified module could not be found. -``` - -Then most probably this is caused, because you are missing -"msvcp140.dll" dependency (for Python 3.5/3.6 for example). This -is a dependency for Python C++ extensions (eg. cefpython_py36.pyd -depends on it). For Python 3.5/3.6 to fix this download -[Visual C++ Redistributable for VS2015](https://www.microsoft.com/en-us/download/details.aspx?id=52685) -(13 MB) - this is just a VC++ redistributable, not a Visual Studio -package. For 32-bit download "vc_redist.x86.exe" file and for -64-bit download "vc_redist.x64.exe" file. - -Explanation: msvcp140.dll is the DLL for the C++ runtime library. -This dependency is added by Cython when building CEF Python -module. It seems that Python 3.6 only ships "vcruntime140.dll" -which is the DLL for the C runtime library. - -On a side note, when using pyinstaller/py2exe tools for -freezing application into exe then these tools should -automatically detect the msvcp140.dll dependency and ship it -with your application. - -Created [Issue #359](../../../issues/359) to track this problem. - - ## Notifications about new releases / commits To be notified of new releases subscribe to this [RSS/Atom feed](../../../releases.atom). @@ -69,15 +35,14 @@ To be notified on new commits subscribe to this [RSS/Atom feed](../../../commits CEF Python depends on CEF and API breaks are inevitable when updating to latest CEF. The [Migration Guide](Migration-guide.md) document -lists most notable breaking changes for each release. Other changes -that do not backwards compatibility can be seen on [GitHub Releases](../../../releases) -pages. +lists most notable breaking changes for each release. Full chanelogs +can be found on [GitHub Releases](../../../releases) pages. -Due to unavoidable changes in API it is recommended for your setup -scripts that use for example PIP to install the cefpython3 package, +Due to unavoidable changes in upstream API it is recommended for your setup +scripts, that for example use PIP to install the cefpython3 package, to hardcode the cefpython version string. If for example using PIP's -requirements.txt file then list the cefpython3 package in the -following format if using cefpython v57.0: `cefpython3 == 57.0`. +`requirements.txt` file then include the cefpython3 package in the +following format if using e.g. cefpython v57.0: `cefpython3 == 57.0`. ## Differences between Python 2 and Python 3 @@ -112,6 +77,46 @@ Now you should see debug information displayed in console like this: ``` +## Remote debugging with Google Chrome instance + +Remote debugging is enabled by default and is configurable using +the ApplicationSettings.[remote_debugging_port](../api/ApplicationSettings.md#remote_debugging_port) option. +When launching app you can see in console log the random port that +was generated: + +``` +DevTools listening on ws://127.0.0.1:63967/devtools/browser/c52ad9ad-bf40-47d1-b2d1-be392d536a2b +``` + +You can debug remotely in two ways: + +1. Debug with CEF devtools. Open the `http://127.0.0.1:port` url + (replace port with e.g. 63967 in our case) in a Google Chrome + browser. You will see a list of CEF browser instances running + which you can debug with DevTools. + This way of debugging has the same sets of features as opening DevTools + popup via `Browser.ShowDevTools` method or using the "Show DevTools" + option from mouse context menu in a CEF app. CEF DevTools has some + limits, not all features of Google Chrome DevTools do work. There + is another way to remotely debug that can workaround these limits, + see the point 2 below. + +2. If some features don't work when debugging with CEF devtools you can + use dedicated DevTools for Node in Google Chrome browser. For example + as of CEF v70 the devtools feature "Save as HAR file" doesn't work, + however it works with dedicated DevTools for Node. Follow these steps + to use dedicated DevTools for Node with CEF: + + 1. In Google Chrome browser open `chrome://inspect` url and click + "Open dedicated DevTools for Node" + 2. Add `localhost:1234` connection and close the popup window + 3. Set `ApplicationSettings.remote_debugging_port` to `1234` and + run your app + 4. Refresh the `chrome://inspect` page in Google Chrome browser + 5. You should see a new target on the Remote Target list. Click + "inspect" link for this target. + + ## Debugging using various chrome:// protocol uris The `chrome://` protocol uris give you access to various debugging @@ -140,7 +145,7 @@ Here is a list of supported `chrome://` protocol uris as of v55.2: - chrome://view-http-cache - chrome://webrtc-internals - chrome://webui-hosts - + ## A blank window on Mac/Linux diff --git a/docs/Migration-guide.md b/docs/Migration-guide.md index 3e419176d..5ad3d4384 100644 --- a/docs/Migration-guide.md +++ b/docs/Migration-guide.md @@ -15,25 +15,29 @@ in your application. Table of contents: -* [v50+ Distribution packages](#v50-distribution-packages) +* [v49+ Distribution packages](#v49-distribution-packages) +* [v49+ cefbuilds.com is deprected, use Spotify Automated CEF Builds](#v49-cefbuildscom-is-deprected-use-spotify-automated-cef-builds) +* [v49+ Build instructions and build tools](#v49-build-instructions-and-build-tools) +* [v49: GPU acceleration should be disabled on Windows XP](#v49-gpu-acceleration-should-be-disabled-on-windows-xp) +* [v49 (Win) Handlers' callbacks and other interfaces](#v49-win-handlers-callbacks-and-other-interfaces) +* [v49+ High DPI support on Windows](#v49-high-dpi-support-on-windows) +* [v49 (Win) Do not call the 'WindowUtils.OnSize' function](#v49-win-do-not-call-the-windowutilsonsize-function) +* [v49+ Notify CEF on move or resize events](#v49-notify-cef-on-move-or-resize-events) +* [v49+ Flash support](#v49-flash-support) +* [v49+ Off-screen-rendering: new option "windowless_rendering_enabled"](#v49-off-screen-rendering-new-option-windowless_rendering_enabled) +* [v49+ BrowserSettings options removed](#v49-browsersettings-options-removed) +* [v49+ cef.Request.Flags changed](#v49-cefrequestflags-changed) +* [v49+ Request.GetHeaderMap and SetHeaderMap change](#v49-requestgetheadermap-and-setheadermap-change) +* [v49+ (Win) HTTPS cache problems on pages with certificate errors](#v49-win-https-cache-problems-on-pages-with-certificate-errors) * [v50+ Importing the cefpython3 package on Linux](#v50-importing-the-cefpython3-package-on-linux) * [v50+ Install X11 error handlers on Linux](#v50-install-x11-error-handlers-on-linux) * [v50+ Set window bounds on Linux](#v50-set-window-bounds-on-linux) -* [v50+ Notify CEF on move or resize events](#v50-notify-cef-on-move-or-resize-events) * [v50+ Keyboard focus issues on Linux](#v50-keyboard-focus-issues-on-linux) * [v50+ Windows XP and Vista are no more supported](#v50-windows-xp-and-vista-are-no-more-supported) * [v50+ Mac 32-bit is no more supported](#v50-mac-32-bit-is-no-more-supported) -* [v50+ cefbuilds.com is deprected, use Spotify Automated CEF Builds](#v50-cefbuildscom-is-deprected-use-spotify-automated-cef-builds) -* [v50+ Build instructions and build tools](#v50-build-instructions-and-build-tools) -* [v51+ Off-screen-rendering: new option "windowless_rendering_enabled"](#v51-off-screen-rendering-new-option-windowless_rendering_enabled) * [v51+ Remove LifespanHandler.RunModal](#v51-remove-lifespanhandlerrunmodal) -* [v51+ BrowserSettings options removed](#v51-browsersettings-options-removed) -* [v51+ cef.Request.Flags changed](#v51-cefrequestflags-changed) -* [v51+ Request.GetHeaderMap and SetHeaderMap change](#v51-requestgetheadermap-and-setheadermap-change) -* [v54+ GTK 3 example doesn't work anymore on Linux](#v54-gtk-3-example-doesnt-work-anymore-on-linux) * [v54+ libcef.so library is stripped from symbols on Linux](#v54-libcefso-library-is-stripped-from-symbols-on-linux) -* [v55+ HTTPS cache problems on pages with certificate errors](#v55-https-cache-problems-on-pages-with-certificate-errors) -* [v55.3+ Handlers' callbacks and other interfaces are now called using keyword args](#v553-handlers-callbacks-and-other-interfaces-are-now-called-using-keyword-args) +* [v55.3+ Handlers' callbacks and other interfaces](#v553-handlers-callbacks-and-other-interfaces) * [v56+ MacOS 10.9+ required to run](#v56-macos-109-required-to-run) * [v57.1+ High DPI support on Windows](#v571-high-dpi-support-on-windows) * [v66+ Linux patch that fixed HTTPS cache problems on pages with certificate errors was disabled](#v66-linux-patch-that-fixed-https-cache-problems-on-pages-with-certificate-errors-was-disabled) @@ -44,11 +48,13 @@ Table of contents: * [v66+ BrowserSettings.javascript_open_windows_disallowed option was removed](#v66-browsersettingsjavascript_open_windows_disallowed-option-was-removed) * [v66+ Threads removed: TID_DB, TID_PROCESS_LAUNCHER, TID_CACHE](#v66-threads-removed-tid_db-tid_process_launcher-tid_cache) * [v66+ cef.Request.Flags changed](#v66-cefrequestflags-changed) -* [v66+ RequestHandler.GetCookieManager 'browser' param may be None](#v66-requesthandlergetcookiemanager-browser-param-may-be-none) +* [v66+ RequestHandler.GetCookieManager not getting called in some cases](#v66-requesthandlergetcookiemanager-not-getting-called-in-some-cases) +* [v66+ Changes to Mac apps that integrate into existing message loop (Qt, wxPython)](#v66-changes-to-mac-apps-that-integrate-into-existing-message-loop-qt-wxpython) +* [v66.1+ Navigation urls passed to CreateBrowserSync or LoadUrl methods need to be encoded by app code](#v661-navigation-urls-passed-to-createbrowsersync-or-loadurl-methods-need-to-be-encoded-by-app-code) +* [v67+ Do not call the 'WindowUtils.OnSize' function](#v67-do-not-call-the-windowutilsonsize-function) - -## v50+ Distribution packages +## v49+ Distribution packages In latest CEF Python there is only one distribution package available: a wheel package. Wheel packages are distributed on @@ -76,120 +82,135 @@ in an automated manner, it might be reconsidered in the future to provide debian packages again. -## v50+ Importing the cefpython3 package on Linux +## v49+ cefbuilds.com is deprected, use Spotify Automated CEF Builds -In the past on Linux it was required for the cefpython3 package -to be imported before any other packages due to tcmalloc global -hook being loaded. This is not required anymore, tcmalloc is -disabled by default. +The cefbuilds.com site with CEF prebuilt binaries is now deprecated. +From now on download prebuilt CEF binaries from the Spotify Automated +CEF Builds: +http://opensource.spotify.com/cefbuilds/index.html -## v50+ Install X11 error handlers on Linux -It is required to install X11 error handlers on Linux, otherwise -you will see 'BadWindow' errors happening - sometimes randomly - -which will cause application to terminate. Since v56+ x11 error -handlers are installed automatically by default during the call -to cef.Initialize(). However sometimes that is not enough like -for example in the wxpython.py example which requires the x11 -error handlers to be installed manually after wx was initialized, -and that is because wx initialization had reset x11 error handlers -that were installed earlier during cef initialization (Issue [#334](../../../issues/334)). +## v49+ Build instructions and build tools -You can install X11 error handlers by calling: -``` -WindowUtils = cef.WindowUtils() -WindowUtils.InstallX11ErrorHandlers() -``` +There were many changes in regards to building CEF and CEF Python. +There are now new tools in the tools/ root directory that fully +automate building CEF and CEF Python. CEF Python now provides +upstream CEF prebuilt binaries and libraries on GitHub Releases +tagged eg. "v49-upstream". With these binaries you can build +CEF Python from sources in less than 10 minutes. See the new +[Build instructions](Build-instructions.md) document. -API ref: WindowUtils.[InstallX11ErrorHandlers()](../api/WindowUtils.md#installx11errorhandlers-linux) +## v49: GPU acceleration should be disabled on Windows XP -## v50+ Set window bounds on Linux +On XP you should disable GPU acceleration by setting the `--disable-gpu` +and `--disable-gpu-compositing` switches. These switches can +be passed programmatically to `cef.Initialize`, see +[api/Command Line Switches](../api/CommandLineSwitches.md). -It is now required to set window bounds during window "resize", -"move" and "configure" events on Linux. You can do so by calling: -``` -browser.SetBounds(x, y, width, height) -``` +## v49 (Win) Handlers' callbacks and other interfaces -API ref: Browser.[SetBounds()](../api/Browser.md#setbounds) +In v49.0 release for Windows all handlers' +callbacks and other interfaces such as +CookieVisitor, StringVisitor and WebRequestClient, are now called +using keyword arguments (Issue [#291](../../../issues/291)). +This will cause many of existing code to break. This is how you +should declare callbacks using the new style: +``` +def OnLoadStart(self, browser, **_): + pass + +def OnLoadStart(self, **kwargs): + browser = kwargs["browser"] +``` -## v50+ Notify CEF on move or resize events +In the first declaration you see that only one argument is +declared, the browser, the others unused will be in the "_" +variable (the name of the variable is so that PyCharm doesn't +warn about unused variable). -It is required to notify the browser on move or resize events -so that popup widgets (e.g. \) are displayed in the correct -location and dismissed when the window moves. Also so that -drag & drop areas are updated accordingly. +Even if you specify and use all arguments, always add the +unused kwargs (`**_`) at the end: ``` -browser.NotifyMoveOrResizeStarted() +def OnLoadStart(self, browser, frame, **_): + pass ``` -API ref: Browser.[NotifyMoveOrResizeStarted()](../api/Browser.md#notifymoveorresizestarted) - +This will be handy in the future, in a case when upstream CEF +adds a new argument to the API, your code won't break. When +an argument is removed in upstream CEF API, if it's possible +CEF Python will try to keep backward compatibility by +emulating behavior of the old argument. -## v50+ Keyboard focus issues on Linux +In case of OnLoadStart, when you've used "browser" and "frame" +names for the arguments, your code won't break. However in +many other callbacks, where you've used argument names that +differed from how they were named in API docs, your code will +break. Also argument names were changed from camelCase +to underscores. For example the OnLoadEnd callback has renamed +the `httpStatusCode` argument to `http_code`. So in this case +your code will definitely break, unless you've also used +"http_code" for argument name. -There several keyboard focus issues on Linux since CEF library -replaced GTK library with X11 library. Most of these issues are -fixed in examples by calling SetFocus in LoadHandler.OnLoadStart -during initial app loading and/or by calling SetFocus in -FocusHandler.OnGotFocus. This keyboard focus issues need to be -fixed in usptream CEF. For more details see Issue [#284](../../../issues/284). +## v49+ High DPI support on Windows -## v50+ Windows XP and Vista are no more supported +It is recommended to embed a DPI awareness manifest in both the main +process and the subprocesses (the subprocess.exe executable) instead +of calling `DpiAware`.[SetProcessDpiAware](../api/DpiAware.md#setprocessdpiaware) +which sets DPI awareness only for the main process. -CEF Python v31.2 was the last version to support Windows XP. -This is due to Chromium/CEF dropping XP support, last version -that supported XP was CEF v49. +The `ApplicationSettings`.[auto_zooming](../api/ApplicationSettings.md#auto_zooming) +option has a default value of an empty string now. Previously the +default was "system_dpi". When enabling High DPI support you should +set it to "system_dpi" explicitilly. +Note that `DpiAware`.[CalculateWindowSize](../api/DpiAware.md#calculatewindowsize) +does not handle all DPI settings (e.g. 132% on Windows 10). +In newer CEF Python there is available `DpiAware`[Scale](../api/DpiAware.md#scale) +which is more reliable and can handle all DPI resolutions. You can copy see +its implementation in `src/dpi_aware_win.pyx`. -## v50+ Mac 32-bit is no more supported -CEF Python v31.2 was the last version to support Mac 32-bit. -This is due to CEF/Chromium dropping 32-bit support, last version -that supported 32-bit was CEF v38. +## v49 (Win) Do not call the 'WindowUtils.OnSize' function +This function can sometimes cause app hanging during window resize. +Call instead the new `WindowUtils`.[UpdateBrowserSize](../api/WindowUtils.md#updatebrowsersize) +function. Except when you use the `pywin32.py` example, in such case +`WindowUtils.OnSize` must be called. +See [Issue #464](../../../issues/464) for more details. -## v50+ cefbuilds.com is deprected, use Spotify Automated CEF Builds -The cefbuilds.com site with CEF prebuilt binaries is now deprecated. -From now on download prebuilt CEF binaries from the Spotify Automated -CEF Builds: +## v49+ Notify CEF on move or resize events -http://opensource.spotify.com/cefbuilds/index.html +It is required to notify the browser on move or resize events +so that popup widgets (e.g. \) are displayed in the correct +location and dismissed when the window moves. Also so that +drag & drop areas are updated accordingly. Call +Browser.[NotifyMoveOrResizeStarted()](../api/Browser.md#notifymoveorresizestarted) +during a move or resize event in your app window. -## v50+ Build instructions and build tools +## v49+ Flash support -There were many changes in regards to building CEF and CEF Python. -There are now new tools in the tools/ root directory that fully -automate building CEF and CEF Python. CEF Python now provides -upstream CEF prebuilt binaries and libraries on GitHub Releases -tagged eg. "v56-upstream". With these binaries you can build -CEF Python from sources in less than 10 minutes. See the new -[Build instructions](Build-instructions.md) document. +See [Issue #235](../../../issues/235) ("Flash support in CEF v49+") +for instructions on how to enable Flash. -## v51+ Off-screen-rendering: new option "windowless_rendering_enabled" +## v49+ Off-screen-rendering: new option "windowless_rendering_enabled" When using off-screen-rendering you must set the ApplicationSettings "windowless_rendering_enabled" option to True. This applies to -examples such as: Kivy, Panda3D and screenshot example. +examples such as: Kivy, Panda3D, PySDL2 and screenshot example. API ref: ApplicationSettings.[windowless_rendering_enabled](../api/ApplicationSettings.md#windowless_rendering_enabled) -## v51+ Remove LifespanHandler.RunModal - -LifespanHandler.RunModal callback is no more available. - - -## v51+ BrowserSettings options removed +## v49+ BrowserSettings options removed The following options were removed from BrowserSettings: - user_style_sheet_location @@ -198,7 +219,7 @@ The following options were removed from BrowserSettings: - author_and_user_styles_disabled -## v51+ cef.Request.Flags changed +## v49+ cef.Request.Flags changed The following flags were removed from cef.Request.Flags: - AllowCookies @@ -208,7 +229,7 @@ The following flags were removed from cef.Request.Flags: API ref: Request.[GetFlags](../api/Request.md#getflags) -## v51+ Request.GetHeaderMap and SetHeaderMap change +## v49+ Request.GetHeaderMap and SetHeaderMap change GetHeaderMap() will not include the Referer value if any and SetHeaderMap() will ignore the Referer value. @@ -216,35 +237,93 @@ and SetHeaderMap() will ignore the Referer value. API ref: Request.[GetHeaderMap](../api/Request.md#getheadermap) -## v54+ GTK 3 example doesn't work anymore on Linux +## v49+ (Win) HTTPS cache problems on pages with certificate errors -Update: GTK 3 example is back working in v57+. +The fix for HTTPS cache problems on pages with certificate errors +(and that includes self-signed certificates) is no more applied +on Windows. -Currently GTK 3 example is broken on Linux. You can either -downgrade to an old cefpython v53 (available on GitHub release -page) or use GTK 2 example. For more details on the problem see -Issue [#261](../../../issues/261). +See Issue [#125](../../../issues/125) for more details. -## v54+ libcef.so library is stripped from symbols on Linux +## v50+ Importing the cefpython3 package on Linux -Symbols useful for debugging are no more available in libcef.so -shipped with distribution packages on Linux. This is explained -in details in Issue [#262](../../../issues/262). +In the past on Linux it was required for the cefpython3 package +to be imported before any other packages due to tcmalloc global +hook being loaded. This is not required anymore, tcmalloc is +disabled by default. -## v55+ HTTPS cache problems on pages with certificate errors +## v50+ Install X11 error handlers on Linux -The fix for HTTPS cache problems on pages with certificate errors -is no more applied on Windows. +It is required to install X11 error handlers on Linux, otherwise +you will see 'BadWindow' errors happening - sometimes randomly - +which will cause application to terminate. Since v56+ x11 error +handlers are installed automatically by default during the call +to cef.Initialize(). However sometimes that is not enough like +for example in the wxpython.py example which requires the x11 +error handlers to be installed manually after wx was initialized, +and that is because wx initialization had reset x11 error handlers +that were installed earlier during cef initialization (Issue [#334](../../../issues/334)). + +You can install X11 error handlers by calling: +``` +WindowUtils = cef.WindowUtils() +WindowUtils.InstallX11ErrorHandlers() +``` -Soon this will fix also won't be applied on Linux anymore when -cefpython starts using CEF prebuilt binaries from Spotify. +API ref: WindowUtils.[InstallX11ErrorHandlers()](../api/WindowUtils.md#installx11errorhandlers-linux) -See Issue [#125](../../../issues/125) for more details. +## v50+ Set window bounds on Linux + +It is now required to set window bounds during window "resize", +"move" and "configure" events on Linux. You can do so by calling: + +``` +browser.SetBounds(x, y, width, height) +``` + +API ref: Browser.[SetBounds()](../api/Browser.md#setbounds) + + +## v50+ Keyboard focus issues on Linux -## v55.3+ Handlers' callbacks and other interfaces are now called using keyword args +There several keyboard focus issues on Linux since CEF library +replaced GTK library with X11 library. Most of these issues are +fixed in examples by calling SetFocus in LoadHandler.OnLoadStart +during initial app loading and/or by calling SetFocus in +FocusHandler.OnGotFocus. This keyboard focus issues need to be +fixed in usptream CEF. For more details see Issue [#284](../../../issues/284). + + +## v50+ Windows XP and Vista are no more supported + +CEF Python v49.0 was the last version to support Windows XP. +This is due to Chromium/CEF dropping XP support, last version +that supported XP was CEF v49. + + +## v50+ Mac 32-bit is no more supported + +CEF Python v31.2 was the last version to support Mac 32-bit. +This is due to CEF/Chromium dropping 32-bit support, last version +that supported 32-bit was CEF v38. + + +## v51+ Remove LifespanHandler.RunModal + +LifespanHandler.RunModal callback is no more available. + + +## v54+ libcef.so library is stripped from symbols on Linux + +Symbols useful for debugging are no more available in libcef.so +shipped with distribution packages on Linux. This is explained +in details in Issue [#262](../../../issues/262). + + +## v55.3+ Handlers' callbacks and other interfaces Since v55.3 all handlers' callbacks and other interfaces such as CookieVisitor, StringVisitor and WebRequestClient, are now called @@ -384,3 +463,46 @@ In some cases the RequestHandler.[GetCookieManager](../api/RequestHandler.md#get callback is not getting called due to a race condition. This bug is to be fixed in Issue [#429](../../../issues/429). + +## v66+ Changes to Mac apps that integrate into existing message loop (Qt, wxPython) + +These changes are required only on Mac platform. + +In Qt apps calling message loop work in a timer doesn't work anymore. +You have to enable external message pump by setting +ApplicationSettings.[external_message_pump](../api/ApplicationSettings.md#external_message_pump) +to `True`. The `qt.py` example was updated to disable calling +message loop work in a timer. External message pump +is a recommended way over calling message loop work in a timer on Mac, +so this should make Qt apps work smoothly. + +In wxPython apps you have to implement both approaches for +integrating with existing message loop at the same time: +1. Call `cef.MessageLoopWork` in a 10ms timer +2. Set `ApplicationSettings.external_message_pump` to True + +This is not a correct approach and is only a temporary fix for wxPython +apps. More testing is required to check if that resolves all the issues +with message loop freezing. Only basic testing was performed. It was not +tested of how this change affects performance. + +See Issue [#442](../../../issues/442) for more details on the issues. + +## v66.1+ Navigation urls passed to CreateBrowserSync or LoadUrl methods need to be encoded by app code + +[Issue #384](../../../issues/384) fixes problems with browser failing to load +urls containing certain characters by not encoding the url anymore. From now +on it is required for the app code to encode the url properly. You can use +the `pathlib.PurePath.as_uri` in Python 3 or `urllib.pathname2url` in +Python 2 (`urllib.request.pathname2url` in Python 3) depending on your case. + +The `cef.GetNavigateUrl` function was removed from the cefpython3 module. + + +## v67+ Do not call the 'WindowUtils.OnSize' function + +This function can sometimes cause app hanging during window resize. +Call instead the new `WindowUtils`.[UpdateBrowserSize](../api/WindowUtils.md#updatebrowsersize) +function. Except when you use the `pywin32.py` example, in such case +`WindowUtils.OnSize` must be called. +See [Issue #464](../../../issues/464) for more details. \ No newline at end of file diff --git a/docs/Tutorial.md b/docs/Tutorial.md index e79815a45..11cf5db71 100644 --- a/docs/Tutorial.md +++ b/docs/Tutorial.md @@ -9,7 +9,8 @@ basics. This tutorial will discuss the three featured examples: [hello_world.py](../examples/hello_world.py), [tutorial.py](../examples/tutorial.py) and [screenshot.py](../examples/screenshot.py). There are many -more examples that you can find in the [README-examples.md](../examples/README-examples.md) +more examples that you can find in the +[README-examples.md](../examples/README-examples.md) file, but these examples are out of scope for this tutorial. @@ -39,7 +40,7 @@ Run the commands below to install the cefpython3 package, clone the repository and run the Hello World example: ```commandline -pip install cefpython3==57.0 +pip install cefpython3==66.0 git clone https://github.com/cztomczak/cefpython.git cd cefpython/examples/ python hello_world.py @@ -68,10 +69,10 @@ Google website. Let's analyze the code from that example: 1. `from cefpython3 import cefpython as cef` - Import the cefpython module and make a short "cef" alias 2. `sys.excepthook = cef.ExceptHook` - Overwrite Python's default - exception handler so that all CEF sub-processes are terminated - when Python exception occurs. To understand this better read the - "Architecture" and "Handling Python exceptions" sections - further down in this Tutorial. + exception handler so that all CEF sub-processes are reliably + terminated when Python exception occurs. To understand this + better read the "Architecture" and "Handling Python exceptions" + sections further down in this Tutorial. 3. `cef.Initialize()` - Initialize CEF. This function must be called somewhere in the beginning of your code. It must be called before any application window is created. It must be called only once @@ -139,9 +140,9 @@ special handling. When Python exception occurs then main process is terminated. For CEF this means that the Browser process is terminated, however there may still be running CEF sub-processes like Renderer process, GPU process, etc. To terminate these -sub-processes cef.[Shutdown](../api/cefpython.md#shutdown) +sub-processes cleanly cef.[Shutdown](../api/cefpython.md#shutdown) must be called and if running CEF message loop then it must be -stopped first. In all CEF Python examples you can find such +stopped first. In most of CEF Python examples you can find such a line that overwrites the default exception handler in Python: ```python @@ -159,7 +160,8 @@ The cef.ExceptHook helper function does the following: which exits the process with status 1, without calling cleanup handlers, flushing stdio buffers, etc. -See CEF Python's ExceptHook source code in src/[helpers.pyx](../src/helpers.pyx). +If you would like to modify `ExceptHook` behavior, see its source code +in src/[helpers.pyx](../src/helpers.pyx) file. ## Settings @@ -260,7 +262,7 @@ MyProduct/10.00 Safari/537.36 ``` To change the whole user agent string use the "user_agent" -option. For example set it to "MyApp/40.00 MyProduct/10.00" +option. For example set it to "MyAgent/20.00 MyProduct/10.00" and both User-Agent HTTP header and js navigator.userAgent will be: ```text @@ -409,7 +411,8 @@ html_to_data_uri("test", js_callback_1); Python and Javascript can also communicate using http requests by running an internal web-server. See for example [SimpleHTTPServer](https://docs.python.org/2/library/simplehttpserver.html) -in Python docs. +in Python docs. In upstream CEF there is available a fast built-in +web server and [Issue #445](../../../issues/445) is to expose its API. With http requests it is possible for synchronous communication from Javascript to Python by performing @@ -431,18 +434,15 @@ however these APIs were not yet exposed to CEF Python. ## Javascript exceptions and Python exceptions -There are cases when executing Javascript code may end up with -Python exception being thrown: +When a Python function is invoked from Javascript and it fails, +a Python exception will be thrown. When Python executes a Javascript +callback and it fails, a Javascript exception will be thrown. -1. When a Python function is invoked from Javascript and it fails, - a Python exception will be thrown -2. When Python executes a Javascript callback and it fails, - a Python exception will be thrown - -In other cases to see Javascript exceptions open Developer Tools +To see Javascript exceptions open Developer Tools window using mouse context menu and switch to Console tab. -There are multiple ways to catch/intercept javascript exceptions: +There are multiple ways to intercept javascript exceptions programmaticaly +in CEF: 1. In Javascript you can register "window.onerror" event to catch all Javascript exceptions diff --git a/examples/README-examples.md b/examples/README-examples.md index 8517e4eff..ec2f9bcd3 100644 --- a/examples/README-examples.md +++ b/examples/README-examples.md @@ -3,6 +3,11 @@ Table of contents: * [Hello World!](#hello-world) * [Supported examples](#supported-examples) + * [Featured](#featured) + * [Snippets](#snippets) + * [GUI frameworks](#gui-frameworks) + * [Build executable with PyInstaller](#build-executable-with-pyinstaller) + * [Unit tests](#unit-tests) * [Other examples](#other-examples) * [More examples to come](#more-examples-to-come) @@ -12,7 +17,7 @@ Instructions to install the cefpython3 package, clone the repository and run the hello_world.py example: ``` -pip install cefpython3==57.0 +pip install cefpython3==66.0 git clone https://github.com/cztomczak/cefpython.git cd cefpython/examples/ python hello_world.py @@ -27,7 +32,7 @@ in sources to see whether this is a known issue with available workarounds. -**Featured** +### Featured - [hello_world.py](hello_world.py) - Basic example, doesn't require any third party GUI framework to run @@ -38,7 +43,16 @@ workarounds. section. -**Embedding using various GUI frameworks** +### Snippets + +For small code snippets that show various CEF features and are easy to +understand see the [README-snippets.md](snippets/README-snippets.md) +document. + + +### GUI frameworks + +Examples of embedding CEF browser using various GUI frameworks: - [gtk2.py](gtk2.py): example for [PyGTK](http://www.pygtk.org/) library (GTK 2) @@ -51,24 +65,28 @@ workarounds. library - [qt.py](qt.py): example for [PyQt4](https://wiki.python.org/moin/PyQt4), [PyQt5](https://pypi.python.org/pypi/PyQt5) - and [PySide](https://wiki.qt.io/PySide) libraries + and [PySide](https://wiki.qt.io/PySide) libraries. + PyQt4 and PySide examples are currently broken on Linux, see + [Issue #452](../../../issues/452). - [tkinter_.py](tkinter_.py): example for [Tkinter](https://wiki.python.org/moin/TkInter). Currently broken on Mac ([#309](../../../issues/309)). - [wxpython.py](wxpython.py): example for [wxPython](https://wxpython.org/) - toolkit + toolkit. This example implements High DPI support on Windows. -**Build executable using Python packagers** +### Build executable with PyInstaller - [PyInstaller example](pyinstaller/README-pyinstaller.md): example of packaging app using [PyInstaller](http://www.pyinstaller.org/) - packager + packager. Currently this example supports only Windows platform. -**Unit tests** +### Unit tests There are also available unit tests and its usage of the API can -be of some use. See [main_test.py](../unittests/main_test.py). +be of some use. See: +- [main_test.py](../unittests/main_test.py) - windowed rendering general tests +- [osr_test.py](../unittests/osr_test.py) - off-screen rendering tests ## Other examples @@ -89,7 +107,8 @@ yet ported to latest CEF. Some of them are externally maintained. for reading/modifying web requests: see the [wxpython-response.py](https://github.com/cztomczak/cefpython/blob/cefpython31/cefpython/cef3/linux/binaries_64bit/wxpython-response.py) example in the cefpython31 branch. - Example of using Python network library (urllib3/openssl) instead of Chromium's - network library - see [gist by Massimiliano Dal Cero](https://gist.github.com/yattamax/0252a3c5dc54a2f81650d5c0eafabf99) + network library - see [gist by Massimiliano Dal Cero](https://gist.github.com/cztomczak/83b77cbdda03ccef81e22e8bd36a51f6) +- Example of passing exceptions from Python to Javascript and using await syntax to receive values from python return values - see [Managed python calls example by Elliot Woods](https://github.com/elliotwoods/cefpython-tests/tree/0180b22eac10a1bde08820ca192fdc30eb93f00d/6.%20Managed%20python%20calls) ## More examples to come diff --git a/examples/hello_world.py b/examples/hello_world.py index 478c70604..789f4666e 100644 --- a/examples/hello_world.py +++ b/examples/hello_world.py @@ -1,5 +1,12 @@ # Hello world example. Doesn't depend on any third party GUI framework. # Tested with CEF Python v57.0+. +# +# ==== High DPI support on Windows ==== +# To enable DPI awareness on Windows you have to either embed DPI aware manifest +# in your executable created with pyinstaller or change python.exe properties manually: +# Compatibility > High DPI scaling override > Application. +# Setting DPI awareness programmatically via a call to cef.DpiAware.EnableHighDpiSupport +# is problematic in Python, may not work and can cause display glitches. from cefpython3 import cefpython as cef import platform diff --git a/examples/pyinstaller/README-pyinstaller.md b/examples/pyinstaller/README-pyinstaller.md index caf3ff81e..f148bbceb 100644 --- a/examples/pyinstaller/README-pyinstaller.md +++ b/examples/pyinstaller/README-pyinstaller.md @@ -12,10 +12,8 @@ Table of contents: This is an example of using [PyInstaller](http://www.pyinstaller.org/) packager to build executable from one of CEF Python's examples -(wxpython.py). Although this pyinstaller example supports only packaging -on Windows, CEF can be packaged on all platforms without problems, but -this specific packaging example just wasn't tested on other platforms. -Pull requests are welcome. +(wxpython.py). This pyinstaller example supports packaging +on Windows, Linux and Darwin but was mainly tested on Windows platform. To install required packages type: ``` diff --git a/examples/pyinstaller/hook-cefpython3.py b/examples/pyinstaller/hook-cefpython3.py index e36c8ed30..9160f67f9 100644 --- a/examples/pyinstaller/hook-cefpython3.py +++ b/examples/pyinstaller/hook-cefpython3.py @@ -1,6 +1,6 @@ """ This is PyInstaller hook file for CEF Python. This file -helps pyinstaller find CEF Python dependencies that are +helps PyInstaller find CEF Python dependencies that are required to run final executable. See PyInstaller docs for hooks: @@ -13,8 +13,14 @@ import re import sys import PyInstaller -from PyInstaller.utils.hooks import is_module_satisfies +from PyInstaller.utils.hooks import is_module_satisfies, get_package_paths +from PyInstaller.compat import is_win, is_darwin, is_linux from PyInstaller import log as logging +try: + # PyInstaller >= 4.0 doesn't support Python 2.7 + from PyInstaller.compat import is_py2 +except ImportError: + is_py2 = None # Constants CEFPYTHON_MIN_VERSION = "57.0" @@ -24,14 +30,10 @@ # TODO: use this code to work cross-platform: # > from PyInstaller.utils.hooks import get_package_paths # > get_package_paths("cefpython3") -CEFPYTHON3_DIR = os.path.join( - os.path.dirname(sys.executable), - 'Lib', 'site-packages', 'cefpython3') -if platform.system() == "Windows": - CYTHON_MODULE_EXT = ".pyd" -else: - CYTHON_MODULE_EXT = ".so" +CEFPYTHON3_DIR = get_package_paths("cefpython3")[1] + +CYTHON_MODULE_EXT = ".pyd" if is_win else ".so" # Globals logger = logging.getLogger(__name__) @@ -39,9 +41,9 @@ # Functions def check_platforms(): - if platform.system() != "Windows": - raise SystemExit("Error: Currently only Windows platform is " - " supported, see Issue #135.") + if not is_win and not is_darwin and not is_linux: + raise SystemExit("Error: Currently only Windows, Linux and Darwin " + "platforms are supported, see Issue #135.") def check_pyinstaller_version(): @@ -51,7 +53,7 @@ def check_pyinstaller_version(): # Example version string for dev version of pyinstaller: # > 3.3.dev0+g5dc9557c version = PyInstaller.__version__ - match = re.search(r"^\d+\.\d+", version) + match = re.search(r"^\d+\.\d+(\.\d+)?", version) if not (match.group(0) >= PYINSTALLER_MIN_VERSION): raise SystemExit("Error: pyinstaller %s or higher is required" % PYINSTALLER_MIN_VERSION) @@ -97,8 +99,8 @@ def get_excluded_cefpython_modules(): def get_cefpython3_datas(): - """Returning all cefpython binaries as DATAS, because - pyinstaller does strange things and fails if these are + """Returning almost all of cefpython binaries as DATAS (see exception + below), because pyinstaller does strange things and fails if these are returned as BINARIES. It first updates manifest in .dll files: >> Updating manifest in chrome_elf.dll @@ -114,32 +116,70 @@ def get_cefpython3_datas(): as pyinstaller would fail to find binary depdendencies on these files. + One exception is subprocess (subprocess.exe on Windows) executable + file, which is passed to pyinstaller as BINARIES in order to collect + its dependecies. + DATAS are in format: tuple(full_path, dest_subdir). """ ret = list() + if is_win: + cefdatadir = "." + elif is_darwin or is_linux: + cefdatadir = "." + else: + assert False, "Unsupported system {}".format(platform.system()) + # Binaries, licenses and readmes in the cefpython3/ directory for filename in os.listdir(CEFPYTHON3_DIR): # Ignore Cython modules which are already handled by # pyinstaller automatically. - if filename[:-4] in get_cefpython_modules(): + if filename[:-len(CYTHON_MODULE_EXT)] in get_cefpython_modules(): continue + # CEF binaries and datas - if filename[-4:] in [".exe", ".dll", ".so", ".pak", ".dat", ".bin", - ".txt"]\ - or filename in ["License", "subprocess"]: - logger.info("Include cefpython3 data: %s" % filename) - ret.append((os.path.join(CEFPYTHON3_DIR, filename), - "")) - - # The .pak files in cefpython3/locales/ directory - locales_dir = os.path.join(CEFPYTHON3_DIR, "locales") - assert os.path.exists(locales_dir), "locales/ dir not found in cefpython3" - for filename in os.listdir(locales_dir): - logger.info("Include cefpython3 data: %s/%s" % ( - os.path.basename(locales_dir), filename)) - ret.append((os.path.join(locales_dir, filename), - "locales")) + extension = os.path.splitext(filename)[1] + if extension in \ + [".exe", ".dll", ".pak", ".dat", ".bin", ".txt", ".so", ".plist"] \ + or filename.lower().startswith("license"): + logger.info("Include cefpython3 data: {}".format(filename)) + ret.append((os.path.join(CEFPYTHON3_DIR, filename), cefdatadir)) + + if is_darwin: + # "Chromium Embedded Framework.framework/Resources" with subdirectories + # is required. Contain .pak files and locales (each locale in separate + # subdirectory). + resources_subdir = \ + os.path.join("Chromium Embedded Framework.framework", "Resources") + base_path = os.path.join(CEFPYTHON3_DIR, resources_subdir) + assert os.path.exists(base_path), \ + "{} dir not found in cefpython3".format(resources_subdir) + for path, dirs, files in os.walk(base_path): + for file in files: + absolute_file_path = os.path.join(path, file) + dest_path = os.path.relpath(path, CEFPYTHON3_DIR) + ret.append((absolute_file_path, dest_path)) + logger.info("Include cefpython3 data: {}".format(dest_path)) + elif is_win or is_linux: + # The .pak files in cefpython3/locales/ directory + locales_dir = os.path.join(CEFPYTHON3_DIR, "locales") + assert os.path.exists(locales_dir), \ + "locales/ dir not found in cefpython3" + for filename in os.listdir(locales_dir): + logger.info("Include cefpython3 data: {}/{}".format( + os.path.basename(locales_dir), filename)) + ret.append((os.path.join(locales_dir, filename), + os.path.join(cefdatadir, "locales"))) + + # Optional .so/.dll files in cefpython3/swiftshader/ directory + swiftshader_dir = os.path.join(CEFPYTHON3_DIR, "swiftshader") + if os.path.isdir(swiftshader_dir): + for filename in os.listdir(swiftshader_dir): + logger.info("Include cefpython3 data: {}/{}".format( + os.path.basename(swiftshader_dir), filename)) + ret.append((os.path.join(swiftshader_dir, filename), + os.path.join(cefdatadir, "swiftshader"))) return ret @@ -178,7 +218,7 @@ def get_cefpython3_datas(): "urllib", "weakref", ] -if sys.version_info.major == 2: +if is_py2: hiddenimports += [ "urlparse", ] @@ -186,8 +226,13 @@ def get_cefpython3_datas(): # Excluded modules excludedimports = get_excluded_cefpython_modules() -# Include binaries -binaries = [] +# Include binaries requiring to collect its dependencies +if is_darwin or is_linux: + binaries = [(os.path.join(CEFPYTHON3_DIR, "subprocess"), ".")] +elif is_win: + binaries = [(os.path.join(CEFPYTHON3_DIR, "subprocess.exe"), ".")] +else: + binaries = [] # Include datas datas = get_cefpython3_datas() diff --git a/examples/pyinstaller/pyinstaller.py b/examples/pyinstaller/pyinstaller.py index aedc3669e..67bb8934a 100644 --- a/examples/pyinstaller/pyinstaller.py +++ b/examples/pyinstaller/pyinstaller.py @@ -25,13 +25,15 @@ EXE_EXT = ".exe" elif platform.system() == "Darwin": EXE_EXT = ".app" +elif platform.system() == "Linux": + EXE_EXT = "" def main(): # Platforms supported - if platform.system() != "Windows": - raise SystemExit("Error: Only Windows platform is currently " - "supported. See Issue #135 for details.") + if platform.system() not in ["Windows", "Darwin", "Linux"]: + raise SystemExit("Error: Only Windows, Linux and Darwin platforms are " + "currently supported. See Issue #135 for details.") # Make sure nothing is cached from previous build. # Delete the build/ and dist/ directories. diff --git a/examples/pysdl2.py b/examples/pysdl2.py index 74bc54236..97f9293dc 100644 --- a/examples/pysdl2.py +++ b/examples/pysdl2.py @@ -38,7 +38,6 @@ - Performance is still not perfect, see Issue #324 for further details - Keyboard modifiers that are not yet handled in this example: ctrl, marking text inputs with the shift key. -- Backspace key doesn't work on Mac - Dragging with mouse not implemented - Window size is fixed, cannot be resized @@ -96,7 +95,19 @@ def die(msg): " To install type: pip install Pillow") +if sys.platform == 'darwin': + try: + import AppKit + except ImportError: + die("ERROR: pyobjc package not found\n" + " To install type: pip install pyobjc") + + def main(): + """ + Parses input, initializes everything and then runs the main loop of the + program, which handles input and draws the scene. + """ parser = argparse.ArgumentParser( description='PySDL2 / cefpython example', add_help=True @@ -159,6 +170,14 @@ def main(): } cef.Initialize(settings={"windowless_rendering_enabled": True}, switches=switches) + + if sys.platform == 'darwin': + # On MacOS, the NSApplication created in the cefpython initialization + # will be hidden if windowless is specified. In order for SDL to receive + # propper input events and for the application to show up in the + # command-tab list, the application must be made "regular". + AppKit.NSApplication.sharedApplication().setActivationPolicy_( + AppKit.NSApplicationActivationPolicyRegular) logging.debug("cef initialised") window_info = cef.WindowInfo() window_info.SetAsOffscreen(0) @@ -169,7 +188,7 @@ def main(): logging.debug("SDL2 initialised") # Create the window window = sdl2.video.SDL_CreateWindow( - 'cefpython3 SDL2 Demo', + b'cefpython3 SDL2 Demo', sdl2.video.SDL_WINDOWPOS_UNDEFINED, sdl2.video.SDL_WINDOWPOS_UNDEFINED, width, @@ -207,6 +226,7 @@ def main(): # viewport size is available and that OnPaint may be called. browser.SendFocusEvent(True) browser.WasResized() + # Begin the main rendering loop running = True # FPS debug variables @@ -324,8 +344,11 @@ def main(): key_event = { "type": cef.KEYEVENT_RAWKEYDOWN, "windows_key_code": keycode, - "character": keycode, - "unmodified_character": keycode, + "native_key_code": get_native_key(keycode), + # For raw key events, the character and unmodified + # character codes should be 0. + "character": 0, + "unmodified_character": 0, "modifiers": cef.EVENTFLAG_NONE } browser.SendKeyEvent(key_event) @@ -348,6 +371,11 @@ def main(): key_event = { "type": cef.KEYEVENT_KEYUP, "windows_key_code": keycode, + "native_key_code": get_native_key(keycode), + # On raw key up events, the character and unmodified + # character need to be defined, otherwise pressing + # one of the above-listed keys will eat the next + # normal keypress. "character": keycode, "unmodified_character": keycode, "modifiers": cef.EVENTFLAG_NONE @@ -382,7 +410,7 @@ def main(): # regulate frame rate if sdl2.timer.SDL_GetTicks() - startTime < 1000.0 / frameRate: sdl2.timer.SDL_Delay( - (1000 / frameRate) - (sdl2.timer.SDL_GetTicks() - startTime) + (1000 // frameRate) - (sdl2.timer.SDL_GetTicks() - startTime) ) # User exited exit_app() @@ -413,6 +441,34 @@ def get_key_code(key): return None +# The key events on MacOS have different native keycode than on other operating +# systems. This table is a translation from Windows-keycodes to MacOS ones. +MACOS_TRANSLATION_TABLE = { + # Backspace + 0x08: 0x33, + + # Left arrow + 0x25: 0x7B, + # Up arrow + 0x26: 0x7E, + # Right arrow + 0x27: 0x7C, + # Down arrow + 0x28: 0x7D, +} + + +def get_native_key(key): + """ + Helper function for returning the correct native key map for the operating + system. + """ + if sys.platform == 'darwin': + return MACOS_TRANSLATION_TABLE.get(key, key) + + return key + + class LoadHandler(object): """Simple handler for loading URLs.""" diff --git a/examples/pywin32.py b/examples/pywin32.py index d18aa54ed..bf8d2b9be 100644 --- a/examples/pywin32.py +++ b/examples/pywin32.py @@ -108,7 +108,7 @@ def check_versions(): pywin32_version = fp.read().strip() print("[pywin32.py] pywin32 {ver}".format(ver=pywin32_version)) - assert cef.__version__ >= "57.0", "CEF Python v57.0 required to run this" + assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this" def create_browser(window_info, settings, url): diff --git a/examples/qt.py b/examples/qt.py index 731ac2e0b..efb1b381f 100644 --- a/examples/qt.py +++ b/examples/qt.py @@ -6,6 +6,7 @@ # - PyQt 5.8.2 (qt 5.8.0) on Windows/Linux/Mac # - PyQt 4.10.4 / 4.11.4 (qt 4.8.6 / 4.8.7) on Windows/Linux # - PySide 1.2.1 (qt 4.8.6) on Windows/Linux/Mac +# - PySide2 5.6.0, 5.11.2 (qt 5.6.2, 5.11.2) on Windows/Linux/Mac # - CEF Python v55.4+ # # Issues with PySide 1.2: @@ -22,6 +23,7 @@ PYQT4 = False PYQT5 = False PYSIDE = False +PYSIDE2 = False if "pyqt4" in sys.argv: PYQT4 = True @@ -47,11 +49,24 @@ from PySide.QtGui import * # noinspection PyUnresolvedReferences from PySide.QtCore import * +elif "pyside2" in sys.argv: + PYSIDE2 = True + # noinspection PyUnresolvedReferences + import PySide2 + # noinspection PyUnresolvedReferences + from PySide2 import QtCore + # noinspection PyUnresolvedReferences + from PySide2.QtGui import * + # noinspection PyUnresolvedReferences + from PySide2.QtCore import * + # noinspection PyUnresolvedReferences + from PySide2.QtWidgets import * else: print("USAGE:") print(" qt.py pyqt4") print(" qt.py pyqt5") print(" qt.py pyside") + print(" qt.py pyside2") sys.exit(1) # Fix for PyCharm hints warnings when using static methods @@ -76,14 +91,22 @@ def main(): check_versions() sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error - cef.Initialize() + settings = {} + if MAC: + # Issue #442 requires enabling message pump on Mac + # in Qt example. Calling cef.DoMessageLoopWork in a timer + # doesn't work anymore. + settings["external_message_pump"] = True + + cef.Initialize(settings) app = CefApplication(sys.argv) main_window = MainWindow() main_window.show() main_window.activateWindow() main_window.raise_() app.exec_() - app.stopTimer() + if not cef.GetAppSetting("external_message_pump"): + app.stopTimer() del main_window # Just to be safe, similarly to "del app" del app # Must destroy app object before calling Shutdown cef.Shutdown() @@ -99,6 +122,9 @@ def check_versions(): elif PYSIDE: print("[qt.py] PySide {v1} (qt {v2})".format( v1=PySide.__version__, v2=QtCore.__version__)) + elif PYSIDE2: + print("[qt.py] PySide2 {v1} (qt {v2})".format( + v1=PySide2.__version__, v2=QtCore.__version__)) # CEF Python version requirement assert cef.__version__ >= "55.4", "CEF Python v55.4+ required to run this" @@ -118,6 +144,8 @@ def __init__(self): self.setWindowTitle("PyQt5 example") elif PYSIDE: self.setWindowTitle("PySide example") + elif PYSIDE2: + self.setWindowTitle("PySide2 example") self.setFocusPolicy(Qt.StrongFocus) self.setupLayout() @@ -139,7 +167,7 @@ def setupLayout(self): frame.setLayout(layout) self.setCentralWidget(frame) - if PYQT5 and WINDOWS: + if (PYSIDE2 or PYQT5) and WINDOWS: # On Windows with PyQt5 main window must be shown first # before CEF browser is embedded, otherwise window is # not resized and application hangs during resize. @@ -148,7 +176,7 @@ def setupLayout(self): # Browser can be embedded only after layout was set up self.cef_widget.embedBrowser() - if PYQT5 and LINUX: + if (PYSIDE2 or PYQT5) and LINUX: # On Linux with PyQt5 the QX11EmbedContainer widget is # no more available. An equivalent in Qt5 is to create # a hidden window, embed CEF browser in it and then @@ -184,6 +212,8 @@ def __init__(self, parent=None): def focusInEvent(self, event): # This event seems to never get called on Linux, as CEF is # stealing all focus due to Issue #284. + if cef.GetAppSetting("debug"): + print("[qt.py] CefWidget.focusInEvent") if self.browser: if WINDOWS: WindowUtils.OnSetFocus(self.getHandle(), 0, 0, 0) @@ -192,11 +222,13 @@ def focusInEvent(self, event): def focusOutEvent(self, event): # This event seems to never get called on Linux, as CEF is # stealing all focus due to Issue #284. + if cef.GetAppSetting("debug"): + print("[qt.py] CefWidget.focusOutEvent") if self.browser: self.browser.SetFocus(False) def embedBrowser(self): - if PYQT5 and LINUX: + if (PYSIDE2 or PYQT5) and LINUX: # noinspection PyUnresolvedReferences self.hidden_window = QWindow() window_info = cef.WindowInfo() @@ -259,7 +291,8 @@ def resizeEvent(self, event): class CefApplication(QApplication): def __init__(self, args): super(CefApplication, self).__init__(args) - self.timer = self.createTimer() + if not cef.GetAppSetting("external_message_pump"): + self.timer = self.createTimer() self.setupIcon() def createTimer(self): @@ -307,14 +340,20 @@ class FocusHandler(object): def __init__(self, cef_widget): self.cef_widget = cef_widget + def OnTakeFocus(self, **_): + if cef.GetAppSetting("debug"): + print("[qt.py] FocusHandler.OnTakeFocus") + def OnSetFocus(self, **_): - pass + if cef.GetAppSetting("debug"): + print("[qt.py] FocusHandler.OnSetFocus") def OnGotFocus(self, browser, **_): + if cef.GetAppSetting("debug"): + print("[qt.py] FocusHandler.OnGotFocus") + self.cef_widget.setFocus() # Temporary fix no. 1 for focus issues on Linux (Issue #284) if LINUX: - print("[qt.py] FocusHandler.OnGotFocus:" - " keyboard focus fix no. 1 (Issue #284)") browser.SetFocus(True) diff --git a/examples/screenshot.py b/examples/screenshot.py index e99c2fe67..5ca8d4913 100644 --- a/examples/screenshot.py +++ b/examples/screenshot.py @@ -24,6 +24,15 @@ Tested configurations: - CEF Python v57.0+ - Pillow 2.3.0 / 4.1.0 + +NOTE: There are limits in Chromium on viewport size. For some + websites with huge viewport size it won't work. In such + case it is required to reduce viewport size to an usual + size of a window and perform scrolling programmatically + using javascript while making a screenshot for each of + the scrolled region. Then at the end combine all the + screenshots into one. To force a paint event in OSR + mode call cef.Invalidate(). """ from cefpython3 import cefpython as cef @@ -33,8 +42,8 @@ import sys try: - from PIL import Image, PILLOW_VERSION -except: + from PIL import Image, __version__ as PILLOW_VERSION +except ImportError: print("[screenshot.py] Error: PIL module not available. To install" " type: pip install Pillow") sys.exit(1) @@ -55,9 +64,26 @@ def main(): os.remove(SCREENSHOT_PATH) command_line_arguments() # Off-screen-rendering requires setting "windowless_rendering_enabled" - # option, so that RenderHandler callbacks are called. - cef.Initialize(settings={"windowless_rendering_enabled": True}) - create_browser() + # option. + settings = { + "windowless_rendering_enabled": True, + } + switches = { + # GPU acceleration is not supported in OSR mode, so must disable + # it using these Chromium switches (Issue #240 and #463) + "disable-gpu": "", + "disable-gpu-compositing": "", + # Tweaking OSR performance by setting the same Chromium flags + # as in upstream cefclient (Issue #240). + "enable-begin-frame-scheduling": "", + "disable-surfaces": "", # This is required for PDF ext to work + } + browser_settings = { + # Tweaking OSR performance (Issue #240) + "windowless_frame_rate": 30, # Default frame rate in CEF is 30 + } + cef.Initialize(settings=settings, switches=switches) + create_browser(browser_settings) cef.MessageLoop() cef.Shutdown() print("[screenshot.py] Opening screenshot with default application") @@ -99,7 +125,7 @@ def command_line_arguments(): sys.exit(1) -def create_browser(): +def create_browser(settings): # Create browser in off-screen-rendering mode (windowless mode) # by calling SetAsOffscreen method. In such mode parent window # handle can be NULL (0). @@ -111,6 +137,7 @@ def create_browser(): print("[screenshot.py] Loading url: {url}" .format(url=URL)) browser = cef.CreateBrowserSync(window_info=window_info, + settings=settings, url=URL) browser.SetClientHandler(LoadHandler()) browser.SetClientHandler(RenderHandler()) @@ -131,6 +158,8 @@ def save_screenshot(browser): "raw", "RGBA", 0, 1) image.save(SCREENSHOT_PATH, "PNG") print("[screenshot.py] Saved image: {path}".format(path=SCREENSHOT_PATH)) + # See comments in exit_app() why PostTask must be used + cef.PostTask(cef.TID_UI, exit_app, browser) def open_with_default_application(path): @@ -161,9 +190,10 @@ def OnLoadingStateChange(self, browser, is_loading, **_): # Loading is complete sys.stdout.write(os.linesep) print("[screenshot.py] Web page loading is complete") - save_screenshot(browser) - # See comments in exit_app() why PostTask must be used - cef.PostTask(cef.TID_UI, exit_app, browser) + print("[screenshot.py] Will save screenshot in 2 seconds") + # Give up to 2 seconds for the OnPaint call. Most of the time + # it is already called, but sometimes it may be called later. + cef.PostDelayedTask(cef.TID_UI, 2000, save_screenshot, browser) def OnLoadError(self, browser, frame, error_code, failed_url, **_): """Called when the resource load for a navigation fails @@ -205,8 +235,8 @@ def OnPaint(self, browser, element_type, paint_buffer, **_): # reasons it would be better not to copy this string. # I think that Python makes a copy of that string when # passing it to SetUserData. - buffer_string = paint_buffer.GetString(mode="rgba", - origin="top-left") + buffer_string = paint_buffer.GetBytes(mode="rgba", + origin="top-left") # Browser object provides GetUserData/SetUserData methods # for storing custom data associated with browser. browser.SetUserData("OnPaint.buffer_string", buffer_string) diff --git a/examples/snippets/README-snippets.md b/examples/snippets/README-snippets.md new file mode 100644 index 000000000..0f41e4f3f --- /dev/null +++ b/examples/snippets/README-snippets.md @@ -0,0 +1,48 @@ +# Snippets README + +Table of contents: +* [Hello World!](#hello-world) +* [Snippets](#snippets) + + +## Hello World! + +Instructions to install the cefpython3 package, clone +the repository and run the `javascript_bindings.py` snippet: + +``` +pip install cefpython3==66.0 +git clone https://github.com/cztomczak/cefpython.git +cd cefpython/examples/snippets/ +python javascript_bindings.py +``` + + +## Snippets + +Below are small code snippets that show various CEF features and +are easy to understand. These are available in the [examples/snippets/](./) +directory. If looking for non-trivial examples then see the +[README-examples.md](../README-examples.md) document. + + +- [cookies.py](cookies.py) - Shows how to fetch all cookies, + all cookies for a given url and how to delete a specific cookie. +- [javascript_bindings.py](javascript_bindings.py) - Communicate + between Python and Javascript asynchronously using + inter-process messaging with the use of Javascript Bindings. +- [javascript_errors.py](javascript_errors.py) - Two ways for + intercepting Javascript errors. +- [mouse_clicks.py](mouse_clicks.py) - Perform mouse clicks + and mouse movements programmatically. +- [network_cookies.py](network_cookies.py) - Implement + interfaces to block or allow cookies over network requests. +- [onbeforeclose.py](onbeforeclose.py) - Implement interface + to execute custom code before browser window closes. +- [ondomready.py](ondomready.py) - Execute custom Python code + on a web page as soon as DOM is ready. +- [onpagecomplete.py](onpagecomplete.py) - Execute custom + Python code on a web page when page loading is complete. +- [setcookie.py](setcookie.py) - Shows how to set a cookie +- [window_size.py](window_size.py) - Set initial window size + without use of any third party GUI framework. diff --git a/examples/snippets/cookies.py b/examples/snippets/cookies.py new file mode 100644 index 000000000..bd732a78a --- /dev/null +++ b/examples/snippets/cookies.py @@ -0,0 +1,62 @@ +"""Shows how to fetch all cookies, all cookies for +a given url and how to delete a specific cookie. For +an example on how to set a cookie see the 'setcookie.py' +snippet.""" + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync( + url="http://www.html-kit.com/tools/cookietester/", + window_title="Cookies") + browser.SetClientHandler(LoadHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +class LoadHandler(object): + def OnLoadingStateChange(self, browser, is_loading, **_): + if is_loading: + print("Page loading complete - start visiting cookies") + manager = cef.CookieManager.GetGlobalManager() + # Must keep a strong reference to the CookieVisitor object + # while cookies are being visited. + self.cookie_visitor = CookieVisitor() + # Visit all cookies + result = manager.VisitAllCookies(self.cookie_visitor) + if not result: + print("Error: could not access cookies") + # To visit cookies only for a given url uncomment the + # code below. + """ + url = "http://www.html-kit.com/tools/cookietester/" + http_only_cookies = False + result = manager.VisitUrlCookies(url, http_only_cookies, + self.cookie_visitor) + if not result: + print("Error: could not access cookies") + """ + + +class CookieVisitor(object): + def Visit(self, cookie, count, total, delete_cookie_out): + """This callback is called on the IO thread.""" + print("Cookie {count}/{total}: '{name}', '{value}'" + .format(count=count+1, total=total, name=cookie.GetName(), + value=cookie.GetValue())) + # Set a cookie named "delete_me" and it will be deleted. + # You have to refresh page to see whether it succeeded. + if cookie.GetName() == "delete_me": + # 'delete_cookie_out' arg is a list passed by reference. + # Set its '0' index to True to delete the cookie. + delete_cookie_out[0] = True + print("Deleted cookie: {name}".format(name=cookie.GetName())) + # Return True to continue visiting more cookies + return True + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/javascript_bindings.py b/examples/snippets/javascript_bindings.py new file mode 100644 index 000000000..41ca1d762 --- /dev/null +++ b/examples/snippets/javascript_bindings.py @@ -0,0 +1,69 @@ +""" +Communicate between Python and Javascript asynchronously using +inter-process messaging with the use of Javascript Bindings. +""" + +from cefpython3 import cefpython as cef + +g_htmlcode = """ + + + + + + + +

Javascript Bindings

+
+ + +""" + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url=cef.GetDataUrl(g_htmlcode), + window_title="Javascript Bindings") + browser.SetClientHandler(LoadHandler()) + bindings = cef.JavascriptBindings() + bindings.SetFunction("py_function", py_function) + bindings.SetFunction("py_callback", py_callback) + browser.SetJavascriptBindings(bindings) + cef.MessageLoop() + del browser + cef.Shutdown() + + +def py_function(value, js_callback): + print("Value sent from Javascript: "+value) + js_callback.Call("I am a Python string #2", py_callback) + + +def py_callback(value): + print("Value sent from Javascript: "+value) + + +class LoadHandler(object): + def OnLoadEnd(self, browser, **_): + browser.ExecuteFunction("js_function", "I am a Python string #1") + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/javascript_errors.py b/examples/snippets/javascript_errors.py new file mode 100644 index 000000000..c33ef8d73 --- /dev/null +++ b/examples/snippets/javascript_errors.py @@ -0,0 +1,61 @@ +""" +Two ways for intercepting Javascript errors: +1. window.onerror event in Javascript +2. DisplayHandler.OnConsoleMessage in Python +""" + +from cefpython3 import cefpython as cef + +g_htmlcode = """ + + + + + + + +

Javascript Errors

+
+ + +""" + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url=cef.GetDataUrl(g_htmlcode), + window_title="Javascript Errors") + browser.SetClientHandler(DisplayHandler()) + cef.MessageLoop() + cef.Shutdown() + + +class DisplayHandler(object): + def OnConsoleMessage(self, browser, message, line, **_): + if "error" in message.lower() or "uncaught" in message.lower(): + logmsg = "[Py:OnConsoleMessage] {message} (line {line})" \ + .format(message=message, line=line) + print(logmsg) + browser.ExecuteFunction("print", logmsg) + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/keyboard_handler.py b/examples/snippets/keyboard_handler.py new file mode 100644 index 000000000..9b8e40c8a --- /dev/null +++ b/examples/snippets/keyboard_handler.py @@ -0,0 +1,19 @@ +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url="https://www.google.com/", + window_title="Keyboard Handler") + browser.SetClientHandler(KeyboardHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +class KeyboardHandler(object): + def OnKeyEvent(self, browser, event, event_handle, **_): + print("OnKeyEvent: "+str(event)) + +if __name__ == '__main__': + main() diff --git a/examples/snippets/mouse_clicks.py b/examples/snippets/mouse_clicks.py new file mode 100644 index 000000000..e8ace36a4 --- /dev/null +++ b/examples/snippets/mouse_clicks.py @@ -0,0 +1,51 @@ +# Perform mouse clicks and mouse movements programmatically. + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync( + url="data:text/html,

Mouse clicks snippet

" + "This text will be selected after one second.
" + "This text will be selected after two seconds.", + window_title="Mouse clicks") + browser.SetClientHandler(LifespanHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +def click_after_1_second(browser): + print("Click after 1 second") + # Mouse move to the top-left corner of the text + browser.SendMouseMoveEvent(0, 70, False, 0) + # Left mouse button click in the top-left corner of the text + browser.SendMouseClickEvent(0, 70, cef.MOUSEBUTTON_LEFT, False, 1) + # Mouse move to the bottom-right corner of the text, + # while holding left mouse button. + browser.SendMouseMoveEvent(400, 80, False, cef.EVENTFLAG_LEFT_MOUSE_BUTTON) + # Release left mouse button + browser.SendMouseClickEvent(400, 80, cef.MOUSEBUTTON_LEFT, True, 1) + cef.PostDelayedTask(cef.TID_UI, 1000, click_after_2_seconds, browser) + + +def click_after_2_seconds(browser): + print("Click after 2 seconds") + browser.SendMouseMoveEvent(0, 90, False, 0) + browser.SendMouseClickEvent(0, 90, cef.MOUSEBUTTON_LEFT, False, 1) + browser.SendMouseMoveEvent(400, 99, False, cef.EVENTFLAG_LEFT_MOUSE_BUTTON) + browser.SendMouseClickEvent(400, 99, cef.MOUSEBUTTON_LEFT, True, 1) + cef.PostDelayedTask(cef.TID_UI, 1000, click_after_1_second, browser) + + +class LifespanHandler(object): + def OnLoadEnd(self, browser, **_): + # Execute function with a delay of 1 second after page + # has completed loading. + print("Page loading is complete") + cef.PostDelayedTask(cef.TID_UI, 1000, click_after_1_second, browser) + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/network_cookies.py b/examples/snippets/network_cookies.py new file mode 100644 index 000000000..c3631cff5 --- /dev/null +++ b/examples/snippets/network_cookies.py @@ -0,0 +1,51 @@ +""" +Implement RequestHandler.CanGetCookies and CanSetCookie +to block or allow cookies over network requests. +""" + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync( + url="http://www.html-kit.com/tools/cookietester/", + window_title="Network cookies") + browser.SetClientHandler(RequestHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +class RequestHandler(object): + def __init__(self): + self.getcount = 0 + self.setcount = 0 + + def CanGetCookies(self, frame, request, **_): + # There are multiple iframes on that website, let's log + # cookies only for the main frame. + if frame.IsMain(): + self.getcount += 1 + print("-- CanGetCookies #"+str(self.getcount)) + print("url="+request.GetUrl()[0:80]) + print("") + # Return True to allow reading cookies or False to block + return True + + def CanSetCookie(self, frame, request, cookie, **_): + # There are multiple iframes on that website, let's log + # cookies only for the main frame. + if frame.IsMain(): + self.setcount += 1 + print("-- CanSetCookie @"+str(self.setcount)) + print("url="+request.GetUrl()[0:80]) + print("Name="+cookie.GetName()) + print("Value="+cookie.GetValue()) + print("") + # Return True to allow setting cookie or False to block + return True + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/onbeforeclose.py b/examples/snippets/onbeforeclose.py new file mode 100644 index 000000000..9b072e964 --- /dev/null +++ b/examples/snippets/onbeforeclose.py @@ -0,0 +1,26 @@ +""" +Implement LifespanHandler.OnBeforeClose to execute custom +code before browser window closes. +""" + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url="https://www.google.com/", + window_title="OnBeforeClose") + browser.SetClientHandler(LifespanHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +class LifespanHandler(object): + def OnBeforeClose(self, browser): + print("Browser ID: {}".format(browser.GetIdentifier())) + print("Browser will close and app will exit") + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/ondomready.py b/examples/snippets/ondomready.py new file mode 100644 index 000000000..8775129c7 --- /dev/null +++ b/examples/snippets/ondomready.py @@ -0,0 +1,50 @@ +""" +Execute custom Python code on a web page as soon as DOM is ready. +Implements a custom "_OnDomReady" event in the LoadHandler object. +""" + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url="https://www.google.com/", + window_title="_OnDomReady event") + load_handler = LoadHandler(browser) + browser.SetClientHandler(load_handler) + bindings = cef.JavascriptBindings() + bindings.SetFunction("LoadHandler_OnDomReady", + load_handler["_OnDomReady"]) + browser.SetJavascriptBindings(bindings) + cef.MessageLoop() + del load_handler + del browser + cef.Shutdown() + + +class LoadHandler(object): + def __init__(self, browser): + self.browser = browser + + def __getitem__(self, key): + return getattr(self, key) + + def OnLoadStart(self, browser, **_): + browser.ExecuteJavascript(""" + if (document.readyState === "complete") { + LoadHandler_OnDomReady(); + } else { + document.addEventListener("DOMContentLoaded", function() { + LoadHandler_OnDomReady(); + }); + } + """) + + def _OnDomReady(self): + print("DOM is ready!") + self.browser.ExecuteFunction("alert", + "Message from Python: DOM is ready!") + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/onpagecomplete.py b/examples/snippets/onpagecomplete.py new file mode 100644 index 000000000..e118e8e7c --- /dev/null +++ b/examples/snippets/onpagecomplete.py @@ -0,0 +1,35 @@ +""" +Execute custom Python code on a web page when page loading is complete. +Implements a custom "_OnPageComplete" event in the LoadHandler object. +""" + +from cefpython3 import cefpython as cef + + +def main(): + cef.Initialize() + browser = cef.CreateBrowserSync(url="https://www.google.com/", + window_title="_OnPageComplete event") + browser.SetClientHandler(LoadHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +class LoadHandler(object): + def OnLoadingStateChange(self, browser, is_loading, **_): + """For detecting if page loading has ended it is recommended + to use OnLoadingStateChange which is most reliable. The OnLoadEnd + callback also available in LoadHandler can sometimes fail in + some cases e.g. when image loading hangs.""" + if not is_loading: + self._OnPageComplete(browser) + + def _OnPageComplete(self, browser): + print("Page loading is complete!") + browser.ExecuteFunction("alert", "Message from Python: Page loading" + " is complete!") + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/setcookie.py b/examples/snippets/setcookie.py new file mode 100644 index 000000000..04a99508e --- /dev/null +++ b/examples/snippets/setcookie.py @@ -0,0 +1,36 @@ +""" +Shows how to set a cookie. +""" + +from cefpython3 import cefpython as cef +import datetime + + +def main(): + cef.Initialize() + cef.CreateBrowserSync( + url="http://www.html-kit.com/tools/cookietester/", + window_title="Set a cookie") + manager = cef.CookieManager.GetGlobalManager() + cookie = cef.Cookie() + cookie.Set({ + "name": "my_cookie", + "value": "my_value", + # Make sure domain is a valid value otherwise it crashes + # app (Issue #459) + "domain": "www.html-kit.com", + "path": "/", + "secure": False, + "httpOnly": False, + "creation": datetime.datetime(2018, 8, 22), + "lastAccess": datetime.datetime(2018, 8, 22), + "hasExpires": True, + "expires": datetime.datetime(2028, 12, 31, 23, 59, 59), + }) + manager.SetCookie("http://www.html-kit.com/", cookie) + cef.MessageLoop() + cef.Shutdown() + + +if __name__ == '__main__': + main() diff --git a/examples/snippets/window_size.py b/examples/snippets/window_size.py new file mode 100644 index 000000000..5a7b04d0f --- /dev/null +++ b/examples/snippets/window_size.py @@ -0,0 +1,38 @@ +""" +Set initial window size to 900/640px without use of +any third party GUI framework. On Linux/Mac you can set +window size by calling WindowInfo.SetAsChild. On Windows +you can accomplish this by calling Windows native functions +using the ctypes module. +""" + +from cefpython3 import cefpython as cef +import ctypes +import platform + + +def main(): + cef.Initialize() + window_info = cef.WindowInfo() + parent_handle = 0 + # This call has effect only on Mac and Linux. + # All rect coordinates are applied including X and Y parameters. + window_info.SetAsChild(parent_handle, [0, 0, 900, 640]) + browser = cef.CreateBrowserSync(url="https://www.google.com/", + window_info=window_info, + window_title="Window size") + if platform.system() == "Windows": + window_handle = browser.GetOuterWindowHandle() + insert_after_handle = 0 + # X and Y parameters are ignored by setting the SWP_NOMOVE flag + SWP_NOMOVE = 0x0002 + # noinspection PyUnresolvedReferences + ctypes.windll.user32.SetWindowPos(window_handle, insert_after_handle, + 0, 0, 900, 640, SWP_NOMOVE) + cef.MessageLoop() + del browser + cef.Shutdown() + + +if __name__ == '__main__': + main() diff --git a/examples/tkinter_.py b/examples/tkinter_.py index 024e0feb2..327f171fb 100644 --- a/examples/tkinter_.py +++ b/examples/tkinter_.py @@ -13,8 +13,11 @@ # Known issue on Linux: When typing url, mouse must be over url # entry widget otherwise keyboard focus is lost (Issue #255 # and Issue #284). +# Other focus issues discussed in Issue #535. + from cefpython3 import cefpython as cef +import ctypes try: import tkinter as tk except ImportError: @@ -41,7 +44,7 @@ def main(): - logger.setLevel(_logging.INFO) + logger.setLevel(_logging.DEBUG) stream_handler = _logging.StreamHandler() formatter = _logging.Formatter("[%(filename)s] %(message)s") stream_handler.setFormatter(formatter) @@ -52,22 +55,26 @@ def main(): logger.info("Tk {ver}".format(ver=tk.Tcl().eval('info patchlevel'))) assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this" sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error + # Tk must be initialized before CEF otherwise fatal error (Issue #306) root = tk.Tk() app = MainFrame(root) - # Tk must be initialized before CEF otherwise fatal error (Issue #306) - cef.Initialize() + settings = {} + if MAC: + settings["external_message_pump"] = True + cef.Initialize(settings=settings) app.mainloop() + logger.debug("Main loop exited") cef.Shutdown() - class MainFrame(tk.Frame): def __init__(self, root): self.browser_frame = None self.navigation_bar = None + self.root = root # Root - root.geometry("800x600") + root.geometry("900x640") tk.Grid.rowconfigure(root, 0, weight=1) tk.Grid.columnconfigure(root, 0, weight=1) @@ -121,7 +128,9 @@ def on_focus_out(self, _): def on_close(self): if self.browser_frame: self.browser_frame.on_root_close() - self.master.destroy() + self.browser_frame = None + else: + self.master.destroy() def get_browser(self): if self.browser_frame: @@ -144,14 +153,16 @@ def setup_icon(self): class BrowserFrame(tk.Frame): - def __init__(self, master, navigation_bar=None): + def __init__(self, mainframe, navigation_bar=None): self.navigation_bar = navigation_bar self.closing = False self.browser = None - tk.Frame.__init__(self, master) + tk.Frame.__init__(self, mainframe) + self.mainframe = mainframe self.bind("", self.on_focus_in) self.bind("", self.on_focus_out) self.bind("", self.on_configure) + """For focus problems see Issue #255 and Issue #535. """ self.focus_set() def embed_browser(self): @@ -161,27 +172,42 @@ def embed_browser(self): self.browser = cef.CreateBrowserSync(window_info, url="https://www.google.com/") assert self.browser + self.browser.SetClientHandler(LifespanHandler(self)) self.browser.SetClientHandler(LoadHandler(self)) self.browser.SetClientHandler(FocusHandler(self)) self.message_loop_work() def get_window_handle(self): - if self.winfo_id() > 0: - return self.winfo_id() - elif MAC: - # On Mac window id is an invalid negative value (Issue #308). - # This is kind of a dirty hack to get window handle using - # PyObjC package. If you change structure of windows then you + if MAC: + # Do not use self.winfo_id() on Mac, because of these issues: + # 1. Window id sometimes has an invalid negative value (Issue #308). + # 2. Even with valid window id it crashes during the call to NSView.setAutoresizingMask: + # https://github.com/cztomczak/cefpython/issues/309#issuecomment-661094466 + # + # To fix it using PyObjC package to obtain window handle. If you change structure of windows then you # need to do modifications here as well. + # + # There is still one issue with this solution. Sometimes there is more than one window, for example when application + # didn't close cleanly last time Python displays an NSAlert window asking whether to Reopen that window. In such + # case app will crash and you will see in console: + # > Fatal Python error: PyEval_RestoreThread: NULL tstate + # > zsh: abort python tkinter_.py + # Error messages related to this: https://github.com/cztomczak/cefpython/issues/441 + # + # There is yet another issue that might be related as well: + # https://github.com/cztomczak/cefpython/issues/583 + # noinspection PyUnresolvedReferences from AppKit import NSApp # noinspection PyUnresolvedReferences import objc - # Sometimes there is more than one window, when application - # didn't close cleanly last time Python displays an NSAlert - # window asking whether to Reopen that window. + logger.info("winfo_id={}".format(self.winfo_id())) # noinspection PyUnresolvedReferences - return objc.pyobjc_id(NSApp.windows()[-1].contentView()) + content_view = objc.pyobjc_id(NSApp.windows()[-1].contentView()) + logger.info("content_view={}".format(content_view)) + return content_view + elif self.winfo_id() > 0: + return self.winfo_id() else: raise Exception("Couldn't obtain window handle") @@ -201,7 +227,9 @@ def on_root_configure(self): def on_mainframe_configure(self, width, height): if self.browser: if WINDOWS: - WindowUtils.OnSize(self.get_window_handle(), 0, 0, 0) + ctypes.windll.user32.SetWindowPos( + self.browser.GetWindowHandle(), 0, + 0, 0, width, height, 0x0002) elif LINUX: self.browser.SetBounds(0, 0, width, height) self.browser.NotifyMoveOrResizeStarted() @@ -213,14 +241,20 @@ def on_focus_in(self, _): def on_focus_out(self, _): logger.debug("BrowserFrame.on_focus_out") - if self.browser: + """For focus problems see Issue #255 and Issue #535. """ + if LINUX and self.browser: self.browser.SetFocus(False) def on_root_close(self): + logger.info("BrowserFrame.on_root_close") if self.browser: + logger.debug("CloseBrowser") self.browser.CloseBrowser(True) self.clear_browser_references() - self.destroy() + else: + logger.debug("tk.Frame.destroy") + self.destroy() + def clear_browser_references(self): # Clear browser references that you keep anywhere in your @@ -228,6 +262,16 @@ def clear_browser_references(self): self.browser = None +class LifespanHandler(object): + + def __init__(self, tkFrame): + self.tkFrame = tkFrame + + def OnBeforeClose(self, browser, **_): + logger.debug("LifespanHandler.OnBeforeClose") + self.tkFrame.quit() + + class LoadHandler(object): def __init__(self, browser_frame): @@ -239,6 +283,7 @@ def OnLoadStart(self, browser, **_): class FocusHandler(object): + """For focus problems see Issue #255 and Issue #535. """ def __init__(self, browser_frame): self.browser_frame = browser_frame @@ -250,13 +295,15 @@ def OnTakeFocus(self, next_component, **_): def OnSetFocus(self, source, **_): logger.debug("FocusHandler.OnSetFocus, source={source}" .format(source=source)) - return False + if LINUX: + return False + else: + return True def OnGotFocus(self, **_): - """Fix CEF focus issues (#255). Call browser frame's focus_set - to get rid of type cursor in url entry widget.""" logger.debug("FocusHandler.OnGotFocus") - self.browser_frame.focus_set() + if LINUX: + self.browser_frame.focus_set() class NavigationBar(tk.Frame): @@ -337,7 +384,7 @@ def on_load_url(self, _): self.master.get_browser().LoadUrl(self.url_entry.get()) def on_button1(self, _): - """Fix CEF focus issues (#255). See also FocusHandler.OnGotFocus.""" + """For focus problems see Issue #255 and Issue #535. """ logger.debug("NavigationBar.on_button1") self.master.master.focus_force() diff --git a/examples/wxpython.py b/examples/wxpython.py index 6adfa9c4f..92dd7d834 100644 --- a/examples/wxpython.py +++ b/examples/wxpython.py @@ -5,7 +5,7 @@ # - wxPython 4.0 on Windows/Mac/Linux # - wxPython 3.0 on Windows/Mac # - wxPython 2.8 on Linux -# - CEF Python v55.4+ +# - CEF Python v66.0+ import wx from cefpython3 import cefpython as cef @@ -13,17 +13,25 @@ import sys import os -# Fix for PyCharm hints warnings when using static methods -WindowUtils = cef.WindowUtils() - # Platforms WINDOWS = (platform.system() == "Windows") LINUX = (platform.system() == "Linux") MAC = (platform.system() == "Darwin") +if MAC: + try: + # noinspection PyUnresolvedReferences + from AppKit import NSApp + except ImportError: + print("[wxpython.py] Error: PyObjC package is missing, " + "cannot fix Issue #371") + print("[wxpython.py] To install PyObjC type: " + "pip install -U pyobjc") + sys.exit(1) + # Configuration -WIDTH = 800 -HEIGHT = 600 +WIDTH = 900 +HEIGHT = 640 # Globals g_count_windows = 0 @@ -33,6 +41,12 @@ def main(): check_versions() sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error settings = {} + if MAC: + # Issue #442 requires enabling message pump on Mac + # and calling message loop work in a timer both at + # the same time. This is an incorrect approach + # and only a temporary fix. + settings["external_message_pump"] = True if WINDOWS: # noinspection PyUnresolvedReferences, PyArgumentList cef.DpiAware.EnableHighDpiSupport() @@ -51,25 +65,59 @@ def check_versions(): ver=platform.python_version(), arch=platform.architecture()[0])) print("[wxpython.py] wxPython {ver}".format(ver=wx.version())) # CEF Python version requirement - assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this" + assert cef.__version__ >= "66.0", "CEF Python v66.0+ required to run this" + + +def scale_window_size_for_high_dpi(width, height): + """Scale window size for high DPI devices. This func can be + called on all operating systems, but scales only for Windows. + If scaled value is bigger than the work area on the display + then it will be reduced.""" + if not WINDOWS: + return width, height + (_, _, max_width, max_height) = wx.GetClientDisplayRect().Get() + # noinspection PyUnresolvedReferences + (width, height) = cef.DpiAware.Scale((width, height)) + if width > max_width: + width = max_width + if height > max_height: + height = max_height + return width, height class MainFrame(wx.Frame): def __init__(self): - wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, - title='wxPython example', size=(WIDTH, HEIGHT)) self.browser = None # Must ignore X11 errors like 'BadWindow' and others by # installing X11 error handlers. This must be done after # wx was intialized. if LINUX: - WindowUtils.InstallX11ErrorHandlers() + cef.WindowUtils.InstallX11ErrorHandlers() global g_count_windows g_count_windows += 1 + if WINDOWS: + # noinspection PyUnresolvedReferences, PyArgumentList + print("[wxpython.py] System DPI settings: %s" + % str(cef.DpiAware.GetSystemDpi())) + if hasattr(wx, "GetDisplayPPI"): + print("[wxpython.py] wx.GetDisplayPPI = %s" % wx.GetDisplayPPI()) + print("[wxpython.py] wx.GetDisplaySize = %s" % wx.GetDisplaySize()) + + print("[wxpython.py] MainFrame declared size: %s" + % str((WIDTH, HEIGHT))) + size = scale_window_size_for_high_dpi(WIDTH, HEIGHT) + print("[wxpython.py] MainFrame DPI scaled size: %s" % str(size)) + + wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, + title='wxPython example', size=size) + # wxPython will set a smaller size when it is bigger + # than desktop size. + print("[wxpython.py] MainFrame actual size: %s" % self.GetSize()) + self.setup_icon() self.create_menu() self.Bind(wx.EVT_CLOSE, self.OnClose) @@ -81,20 +129,12 @@ def __init__(self): self.browser_panel.Bind(wx.EVT_SIZE, self.OnSize) if MAC: - try: - # noinspection PyUnresolvedReferences - from AppKit import NSApp - # Make the content view for the window have a layer. - # This will make all sub-views have layers. This is - # necessary to ensure correct layer ordering of all - # child views and their layers. This fixes Window - # glitchiness during initial loading on Mac (Issue #371). - NSApp.windows()[0].contentView().setWantsLayer_(True) - except ImportError: - print("[wxpython.py] Warning: PyObjC package is missing, " - "cannot fix Issue #371") - print("[wxpython.py] To install PyObjC type: " - "pip install -U pyobjc") + # Make the content view for the window have a layer. + # This will make all sub-views have layers. This is + # necessary to ensure correct layer ordering of all + # child views and their layers. This fixes Window + # glitchiness during initial loading on Mac (Issue #371). + NSApp.windows()[0].contentView().setWantsLayer_(True) if LINUX: # On Linux must show before embedding browser, so that handle @@ -131,7 +171,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, @@ -142,16 +182,16 @@ def OnSetFocus(self, _): if not self.browser: return if WINDOWS: - WindowUtils.OnSetFocus(self.browser_panel.GetHandle(), - 0, 0, 0) + cef.WindowUtils.OnSetFocus(self.browser_panel.GetHandle(), + 0, 0, 0) self.browser.SetFocus(True) def OnSize(self, _): if not self.browser: return if WINDOWS: - WindowUtils.OnSize(self.browser_panel.GetHandle(), - 0, 0, 0) + cef.WindowUtils.OnSize(self.browser_panel.GetHandle(), + 0, 0, 0) elif LINUX: (x, y) = (0, 0) (width, height) = self.browser_panel.GetSize().Get() diff --git a/src/browser.pyx b/src/browser.pyx index 03e43e398..c8cba03ad 100644 --- a/src/browser.pyx +++ b/src/browser.pyx @@ -5,6 +5,7 @@ include "cefpython.pyx" cimport cef_types +from cef_types cimport cef_state_t IF UNAME_SYSNAME == "Linux": cimport x11 @@ -18,6 +19,8 @@ MOUSEBUTTON_RIGHT = cef_types.MBT_RIGHT cdef dict g_pyBrowsers = {} +# See also g_browser_settings defined in cefpython.pyx + # Unreferenced browsers are added to this list in OnBeforeClose(). # Must keep a list of unreferenced browsers so that a new reference # is not created in GetPyBrowser() when browser was closed. @@ -33,6 +36,20 @@ cdef PyBrowser GetPyBrowserById(int browserId): return g_pyBrowsers[browserId] return None +cdef py_bool IsBrowserClosed(CefRefPtr[CefBrowser] cefBrowser): + """Possibly fix Issue #455 by using this helper function to detect + if browser is closing/closed.""" + # CefBrowser may sometimes be NULL e.g. Issue #429, Issue #454. + if not cefBrowser.get(): + return True + if not cefBrowser.get().GetHost().get(): + return True + cdef int browserId = cefBrowser.get().GetIdentifier() + if browserId in g_unreferenced_browsers \ + or browserId in g_closed_browsers: + return True + return False + cdef PyBrowser GetPyBrowser(CefRefPtr[CefBrowser] cefBrowser, callerIdStr="GetPyBrowser"): """The second argument 'callerIdStr' is so that a debug @@ -74,7 +91,8 @@ cdef PyBrowser GetPyBrowser(CefRefPtr[CefBrowser] cefBrowser, cdef JavascriptBindings javascriptBindings cdef PyBrowser tempPyBrowser - if browserId in g_unreferenced_browsers: + if browserId in g_unreferenced_browsers \ + or browserId in g_closed_browsers: # This browser was already unreferenced due to OnBeforeClose # was already called. An incomplete new instance of Browser # object is created. This instance doesn't have the client @@ -106,9 +124,15 @@ cdef PyBrowser GetPyBrowser(CefRefPtr[CefBrowser] cefBrowser, openerHandle = pyBrowser.GetOpenerWindowHandle() for identifier, tempPyBrowser in g_pyBrowsers.items(): if tempPyBrowser.GetWindowHandle() == openerHandle: - clientCallbacks = tempPyBrowser.GetClientCallbacksDict() - if clientCallbacks: - pyBrowser.SetClientCallbacksDict(clientCallbacks) + # tempPyBrowser is a parent browser + if tempPyBrowser.GetSetting("inherit_client_handlers_for_popups"): + if pyBrowser.GetIdentifier() not in g_browser_settings: + g_browser_settings[pyBrowser.GetIdentifier()] = {} + g_browser_settings[pyBrowser.GetIdentifier()]["inherit_client_handlers_for_popups"] =\ + tempPyBrowser.GetSetting("inherit_client_handlers_for_popups") + clientCallbacks = tempPyBrowser.GetClientCallbacksDict() + if clientCallbacks: + pyBrowser.SetClientCallbacksDict(clientCallbacks) javascriptBindings = tempPyBrowser.GetJavascriptBindings() if javascriptBindings: if javascriptBindings.GetBindToPopups(): @@ -119,9 +143,13 @@ cdef PyBrowser GetPyBrowser(CefRefPtr[CefBrowser] cefBrowser, cdef void RemovePyBrowser(int browserId) except *: # Called from LifespanHandler_OnBeforeClose(). global g_pyBrowsers, g_unreferenced_browsers + cdef PyBrowser pyBrowser if browserId in g_pyBrowsers: # noinspection PyUnresolvedReferences Debug("del g_pyBrowsers[%s]" % browserId) + pyBrowser = g_pyBrowsers[browserId] + pyBrowser.cefBrowser.Assign(NULL) + del pyBrowser del g_pyBrowsers[browserId] g_unreferenced_browsers.append(browserId) else: @@ -138,6 +166,15 @@ cpdef PyBrowser GetBrowserByWindowHandle(WindowHandle windowHandle): return pyBrowser return None + +cpdef PyBrowser GetBrowserByIdentifier(int identifier): + cdef PyBrowser pyBrowser + for browserId in g_pyBrowsers: + pyBrowser = g_pyBrowsers[browserId] + if pyBrowser.GetIdentifier() == identifier: + return pyBrowser + return None + cdef public void PyBrowser_ShowDevTools(CefRefPtr[CefBrowser] cefBrowser ) except * with gil: # Called from ClientHandler::OnContextMenuCommand @@ -198,7 +235,8 @@ cdef class PyBrowser: # DisplayHandler self.allowedClientCallbacks += [ "OnAddressChange", "OnTitleChange", "OnTooltip", - "OnStatusMessage", "OnConsoleMessage"] + "OnStatusMessage", "OnConsoleMessage", "OnAutoResize", + "OnLoadingProgressChange"] # KeyboardHandler self.allowedClientCallbacks += ["OnPreKeyEvent", "OnKeyEvent"] # RequestHandler @@ -210,7 +248,7 @@ cdef class PyBrowser: "OnQuotaRequest", "OnProtocolExecution", "GetResourceHandler", "OnBeforeBrowse", "OnRendererProcessTerminated", - "OnPluginCrashed"] + "OnPluginCrashed", "CanGetCookies", "CanSetCookie"] # RequestContextHandler self.allowedClientCallbacks += ["GetCookieManager"] # LoadHandler @@ -227,7 +265,11 @@ cdef class PyBrowser: "GetScreenRect", "OnPopupShow", "OnPopupSize", "OnPaint", "OnCursorChange", "OnScrollOffsetChanged", - "StartDragging", "UpdateDragCursor"] + "StartDragging", "UpdateDragCursor", + "OnTextSelectionChanged"] + # V8ContextHandler + self.allowedClientCallbacks += ["OnContextCreated", + "OnContextReleased"] # JavascriptDialogHandler self.allowedClientCallbacks += ["OnJavascriptDialog", "OnBeforeUnloadJavascriptDialog", @@ -303,6 +345,13 @@ cdef class PyBrowser: NonCriticalError("GetImage not implemented on this platform") return None + cpdef object GetSetting(self, py_string key): + cdef int browser_id = self.GetIdentifier() + if browser_id in g_browser_settings: + if key in g_browser_settings[browser_id]: + return g_browser_settings[browser_id][key] + return None + # -------------- # CEF API. # -------------- @@ -323,7 +372,32 @@ 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) + + # Fix Issue #454 "Crash on exit when closing browser + # immediately during initial loading". + if not self.cefBrowser.get(): + Debug("cefBrowser.get() failed in CloseBrowser") + return + # From testing it seems that only cefBrowser.get() can fail, + # however let's check the host as well just to be safe. + if not self.cefBrowser.get().GetHost().get(): + Debug("cefBrowser.get().GetHost() failed in CloseBrowser") + return + + # 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( + NULL) \ + .get().FlushStore(NULL) + cdef int browserId = self.GetCefBrowser().get().GetIdentifier() self.GetCefBrowserHost().get().CloseBrowser(bool(forceClose)) global g_closed_browsers @@ -428,9 +502,16 @@ cdef class PyBrowser: cpdef py_void GoForward(self): self.GetCefBrowser().get().GoForward() + cpdef py_bool HasDevTools(self): + return self.GetCefBrowserHost().get().HasDevTools() + cpdef py_bool HasDocument(self): return self.GetCefBrowser().get().HasDocument() + cpdef py_void Invalidate(self, + cef_types.cef_paint_element_type_t element_type): + return self.GetCefBrowserHost().get().Invalidate(element_type) + cpdef py_bool IsFullscreen(self): return bool(self.isFullscreen) @@ -463,12 +544,26 @@ cdef class PyBrowser: PyToCefString(word, cef_word) self.GetCefBrowserHost().get().ReplaceMisspelling(cef_word) + cpdef py_void SetAutoResizeEnabled(self, + py_bool enabled, + list min_size, + list max_size): + self.GetCefBrowserHost().get().SetAutoResizeEnabled( + bool(enabled), + CefSize(min_size[0], min_size[1]), + CefSize(max_size[0], max_size[1]) + ) + + cpdef py_void SetBounds(self, int x, int y, int width, int height): IF UNAME_SYSNAME == "Linux": x11.SetX11WindowBounds(self.GetCefBrowser(), x, y, width, height) ELSE: NonCriticalError("SetBounds() not implemented on this platform") + cpdef py_void SetAccessibilityState(self, cef_state_t state): + self.GetCefBrowserHost().get().SetAccessibilityState(state) + cpdef py_void SetFocus(self, enable): self.GetCefBrowserHost().get().SetFocus(bool(enable)) @@ -481,11 +576,8 @@ cdef class PyBrowser: cpdef py_void ShowDevTools(self): cdef CefWindowInfo window_info IF UNAME_SYSNAME == "Windows": - # On Windows with empty window_info structure the devtools - # window doesn't appear. - window_info.SetAsPopup( - self.GetOpenerWindowHandle(), - PyToCefStringValue("DevTools")) + window_info.SetAsPopup(self.GetWindowHandle(), + PyToCefStringValue("DevTools")) cdef CefBrowserSettings settings cdef CefPoint inspect_element_at self.GetCefBrowserHost().get().ShowDevTools( diff --git a/src/cef_types.pyx b/src/cef_types.pyx new file mode 100644 index 000000000..6d9ad5fe5 --- /dev/null +++ b/src/cef_types.pyx @@ -0,0 +1,9 @@ +# Copyright (c) 2014 CEF Python, see the Authors file. +# All rights reserved. Licensed under BSD 3-clause license. +# Project website: https://github.com/cztomczak/cefpython + +cimport cef_types + +STATE_DEFAULT = cef_types.STATE_DEFAULT +STATE_ENABLED = cef_types.STATE_ENABLED +STATE_DISABLED = cef_types.STATE_DISABLED diff --git a/src/cef_v59..v66_changes.txt b/src/cef_v59..v66_changes.txt index 1feb6f1c1..fab7e5e75 100644 --- a/src/cef_v59..v66_changes.txt +++ b/src/cef_v59..v66_changes.txt @@ -70,47 +70,58 @@ MISC NEW FEATURES ------------ ++ unittests/osr_test.py - new test for off-screen rendering mode ++ cefpython.SetGlobalClientHandler ++ Browser.Invalidate ++ CookieManager.FlushStore ++ BrowserSettings.inherit_client_handlers_for_popups ++ Browser.GetSetting ++ cefpython.GetBrowserByIdentifier ++ Browser.HasDevTools ++ examples/snippets/ + + onbeforeclose.py + + network_cookies.py + + mouse_clicks.py + + javascript_bindings.py + + javascript_errors.py + + ondomready.py + + onpagecomplete ++ cef.GetDataUrl + internal/cef_types.h + cef_log_severity_t: new key LOGSEVERITY_DEBUG (no need to expose, same as LOGSEVERITY_VERBOSE, see code comments in setting.pyx + cef_settings_t: + background_color: OSR windows can set an ARGB background color -- cef_path_key_t (informational only): - - PK_DIR_RESOURCES new key -- cef_urlrequest_flags_t (expose in cefpython.Request.Flags): - - UR_FLAG_ONLY_FROM_CACHE new flag - - UR_FLAG_STOP_ON_REDIRECT new flag - - enum values have changed due to new key -- cef_thread_id_t: - - TID_FILE_BACKGROUND (deprecated TID_FILE) - - TID_FILE_USER_VISIBLE -- cef_popup_features_t: some keys removed (not exposed, informational only) -- cef_referrer_policy_t changes (not exposed) ++ cef_path_key_t (informational only): + + PK_DIR_RESOURCES new key ++ cef_popup_features_t: some keys removed (not exposed, informational only) ++ cef_referrer_policy_t changes (not exposed, info only) cef_accessibility_handler.h -- CefAccessibilityHandler -- CefRenderHandler::GetAccessibilityHandler ++ CefAccessibilityHandler ++ CefRenderHandler::GetAccessibilityHandler cef_render_handler.h -- OnTextSelectionChanged ++ OnTextSelectionChanged cef_browser.h -- SetAccessibilityState -- SetAutoResizeEnabled ++ SetAccessibilityState ++ SetAutoResizeEnabled - GetExtension - IsBackgroundHost cef_cookie.h -- GetBlockingManager ++ GetBlockingManager cef_display_handler.h -- OnAutoResize -- OnLoadingProgressChange ++ OnAutoResize ++ OnLoadingProgressChange cef_drag_data.h -- GetImage (cross-platform) -- GetImageHotspot (cross-platform) -- HasImage (cross-platform) ++ GetImage (cross-platform) ++ GetImageHotspot (cross-platform) ++ HasImage (cross-platform) cef_extension.h - CefExtension @@ -132,11 +143,11 @@ cef_extension_handler.h - CefExtensionHandler cef_file_util.h -- CefLoadCRLSetsFile ++ CefLoadCRLSetsFile cef_request_handler.h -- CanGetCookies -- CanSetCookie ++ CanGetCookies ++ CanSetCookie cef_response.h - GetURL @@ -154,4 +165,7 @@ cef_v8.h - CreateArrayBuffer (does not own the buffer) - IsArrayBuffer - GetArrayBufferReleaseCallback - - NeuterArrayBuffer \ No newline at end of file + - NeuterArrayBuffer + +cef_urlrequest.h +- ResponseWasCached \ No newline at end of file diff --git a/src/cefpython.pyx b/src/cefpython.pyx index dd3f9f490..c3b1e1a13 100644 --- a/src/cefpython.pyx +++ b/src/cefpython.pyx @@ -134,20 +134,24 @@ import datetime import random # noinspection PyUnresolvedReferences import struct +# noinspection PyUnresolvedReferences +import base64 -if sys.version_info.major == 2: +# Must use compile-time condition instead of checking sys.version_info.major +# otherwise results in "ImportError: cannot import name urlencode" strange +# error in Python 3.6. +IF PY_MAJOR_VERSION == 2: # noinspection PyUnresolvedReferences import urlparse -else: # noinspection PyUnresolvedReferences - from urllib import parse as urlparse - -if sys.version_info.major == 2: + from urllib import urlencode as urllib_urlencode + from urllib import quote as urlparse_quote +ELSE: # noinspection PyUnresolvedReferences - from urllib import pathname2url as urllib_pathname2url -else: + from urllib import parse as urlparse + from urllib.parse import quote as urlparse_quote # noinspection PyUnresolvedReferences - from urllib.request import pathname2url as urllib_pathname2url + from urllib.parse import urlencode as urllib_urlencode # noinspection PyUnresolvedReferences from cpython.version cimport PY_MAJOR_VERSION @@ -296,6 +300,7 @@ from main_message_loop cimport * # noinspection PyUnresolvedReferences from cef_views cimport * from cef_log cimport * +from cef_file_util cimport * # ----------------------------------------------------------------------------- # GLOBAL VARIABLES @@ -308,6 +313,7 @@ g_debug = False # The string_encoding key must be set early here and also in Initialize. g_applicationSettings = {"string_encoding": "utf-8"} g_commandLineSwitches = {} +g_browser_settings = {} # If ApplicationSettings.unique_request_context_per_browser is False # then a shared request context is used for all browsers. Otherwise @@ -325,6 +331,7 @@ cdef dict g_globalClientCallbacks = {} # ----------------------------------------------------------------------------- +include "cef_types.pyx" include "utils.pyx" include "string_utils.pyx" IF UNAME_SYSNAME == "Windows": @@ -364,13 +371,10 @@ include "command_line.pyx" include "app.pyx" include "drag_data.pyx" include "helpers.pyx" - -# Currently used only on Linux via DragData. Do not include on other -# platforms otherwise warning about unused function appears. -IF UNAME_SYSNAME == "Linux": - include "image.pyx" +include "image.pyx" # Handlers +include "handlers/accessibility_handler.pyx" include "handlers/browser_process_handler.pyx" include "handlers/display_handler.pyx" include "handlers/focus_handler.pyx" @@ -499,9 +503,7 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs): Debug("Initialize() called") - # Mac initialization. Need to call NSApplication.sharedApplication() - # and do NSApplication methods swizzling to implement - # CrAppControlProtocol. See Issue 156. + # Additional initialization on Mac, see util_mac.mm. IF UNAME_SYSNAME == "Darwin": MacInitialize() @@ -583,6 +585,15 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs): application_settings["single_process"] = False # ------------------------------------------------------------------------ + # ------------------------------------------------------------------------ + # Fix GPUCache/ folder creation when using in-memory cache (Issue #419) + # ------------------------------------------------------------------------ + if not "cache_path" in application_settings: + application_settings["cache_path"] = "" + if not application_settings["cache_path"]: + g_commandLineSwitches["disable-gpu-shader-disk-cache"] = "" + + cdef CefRefPtr[CefApp] cefApp = new CefPythonApp() IF UNAME_SYSNAME == "Windows": @@ -613,8 +624,11 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs): # External message pump if GetAppSetting("external_message_pump")\ and not g_external_message_pump.get(): - g_external_message_pump.reset( - MainMessageLoopExternalPump.Create().get()) + Debug("Create external message pump") + # Using .reset() here to assign new instance was causing + # MainMessageLoopExternalPump destructor to be called. Strange. + g_external_message_pump.Assign( + MainMessageLoopExternalPump.Create()) Debug("CefInitialize()") cdef cpp_bool ret @@ -709,17 +723,18 @@ def CreateBrowserSync(windowInfo=None, if window_title and windowInfo.parentWindowHandle == 0: windowInfo.windowName = window_title + # Browser settings if not browserSettings: browserSettings = {} - + # CEF Python only settings + if "inherit_client_handlers_for_popups" not in browserSettings: + browserSettings["inherit_client_handlers_for_popups"] = True cdef CefBrowserSettings cefBrowserSettings SetBrowserSettings(browserSettings, &cefBrowserSettings) cdef CefWindowInfo cefWindowInfo SetCefWindowInfo(cefWindowInfo, windowInfo) - navigateUrl = GetNavigateUrl(navigateUrl) - Debug("navigateUrl: %s" % navigateUrl) cdef CefString cefNavigateUrl PyToCefString(navigateUrl, cefNavigateUrl) @@ -762,6 +777,15 @@ def CreateBrowserSync(windowInfo=None, Debug("CefBrowser window handle = " +str(cefBrowser.get().GetHost().get().GetWindowHandle())) + # Make a copy as browserSettings is a reference only that might + # get destroyed later. + global g_browser_settings + cdef int browser_id = cefBrowser.get().GetIdentifier() + g_browser_settings[browser_id] = {} + for key in browserSettings: + g_browser_settings[browser_id][key] =\ + copy.deepcopy(browserSettings[key]) + # Request context - part 2/2. if g_applicationSettings["unique_request_context_per_browser"]: requestContextHandler.get().SetBrowser(cefBrowser) @@ -834,11 +858,6 @@ def QuitMessageLoop(): def Shutdown(): Debug("Shutdown()") - # Release shared request context. This is sometimes causing - # segmentation fault, so disabling it for now. See Issue #333: - # https://github.com/cztomczak/cefpython/issues/333 - # OFF: g_shared_request_context.Assign(NULL) - # Run some message loop work, force closing browsers and then run # some message loop work again for the browsers to close cleanly. # @@ -920,15 +939,26 @@ def Shutdown(): NonCriticalError("Shutdown called, but there are still browser" " references alive") - Debug("CefShutdown()") - with nogil: - CefShutdown() + # Release shared request context. In the past this was sometimes + # causing segmentation fault. See Issue #333: + # https://github.com/cztomczak/cefpython/issues/333 + # Debug("Free g_shared_request_context") + # g_shared_request_context.Assign(NULL) - # Release external message pump, as in cefclient after Shutdown + # Release external message pump before CefShutdown, so that + # message pump timer is killed. if g_external_message_pump.get(): + Debug("Reset external message pump") # Reset will set it to NULL g_external_message_pump.reset() + Debug("CefShutdown()") + with nogil: + CefShutdown() + + # Additional cleanup on Mac, see util_mac.mm. + IF UNAME_SYSNAME == "Darwin": + MacShutdown() def SetOsModalLoop(py_bool modalLoop): cdef cpp_bool cefModalLoop = bool(modalLoop) @@ -937,12 +967,32 @@ def SetOsModalLoop(py_bool modalLoop): cpdef py_void SetGlobalClientCallback(py_string name, object callback): global g_globalClientCallbacks - if name in ["OnCertificateError", "OnBeforePluginLoad", "OnAfterCreated"]: + # Global callbacks are prefixed with "_" in documentation. + # Accept both with and without a prefix. + if name.startswith("_"): + name = name[1:] + if name in ["OnCertificateError", "OnBeforePluginLoad", "OnAfterCreated", + "OnAccessibilityTreeChange", "OnAccessibilityLocationChange"]: g_globalClientCallbacks[name] = callback else: raise Exception("SetGlobalClientCallback() failed: "\ "invalid callback name = %s" % name) +cpdef py_void SetGlobalClientHandler(object clientHandler): + if not hasattr(clientHandler, "__class__"): + raise Exception("SetGlobalClientHandler() failed: __class__ " + "attribute missing") + cdef dict methods = {} + cdef py_string key + cdef object method + cdef tuple value + for value in inspect.getmembers(clientHandler, + predicate=inspect.ismethod): + key = value[0] + method = value[1] + if key and key[0:2] != '__': + SetGlobalClientCallback(key, method) + cpdef object GetGlobalClientCallback(py_string name): global g_globalClientCallbacks if name in g_globalClientCallbacks: @@ -968,3 +1018,13 @@ cpdef dict GetVersion(): cef_commit_hash=__cef_commit_hash__, cef_commit_number=__cef_commit_number__, ) + +cpdef LoadCrlSetsFile(py_string path): + CefLoadCRLSetsFile(PyToCefStringValue(path)) + +cpdef GetDataUrl(data, mediatype="html"): + if PY_MAJOR_VERSION >= 3: + data = data.encode("utf-8", "replace") + b64 = base64.b64encode(data).decode("utf-8", "replace") + ret = "data:text/html;base64,{data}".format(data=b64) + return ret diff --git a/src/client_handler/Makefile b/src/client_handler/Makefile index bdb2ee728..25e645ff7 100644 --- a/src/client_handler/Makefile +++ b/src/client_handler/Makefile @@ -23,7 +23,7 @@ SRC = client_handler.cpp cookie_visitor.cpp resource_handler.cpp \ download_handler.cpp focus_handler.cpp js_dialog_handler.cpp \ keyboard_handler.cpp lifespan_handler.cpp load_handler.cpp \ render_handler.cpp request_handler.cpp dialog_handler.cpp \ - cef_log.cpp \ + cef_log.cpp accessibility_handler.cpp \ $(SRC_MORE) OBJ = $(filter %.o, $(SRC:.cpp=.o) $(SRC:.mm=.o)) @@ -37,6 +37,7 @@ INC = -I./../ -I./../common/ -I$(PYTHON_INCLUDE) \ -I/usr/include/glib-2.0 \ -I/usr/include/cairo \ -I/usr/include/pango-1.0 \ + -I/usr/include/harfbuzz \ -I/usr/include/gdk-pixbuf-2.0 \ -I/usr/include/atk-1.0 \ -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include \ diff --git a/src/client_handler/accessibility_handler.cpp b/src/client_handler/accessibility_handler.cpp new file mode 100644 index 000000000..a75ab2f69 --- /dev/null +++ b/src/client_handler/accessibility_handler.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2018 CEF Python, see the Authors file. +// All rights reserved. Licensed under BSD 3-clause license. +// Project website: https://github.com/cztomczak/cefpython + +#include "accessibility_handler.h" +#include "common/cefpython_public_api.h" + + +void AccessibilityHandler::OnAccessibilityTreeChange( + CefRefPtr value) { + REQUIRE_UI_THREAD(); + AccessibilityHandler_OnAccessibilityTreeChange(value); +} + +void AccessibilityHandler::OnAccessibilityLocationChange( + CefRefPtr value) { + REQUIRE_UI_THREAD(); + AccessibilityHandler_OnAccessibilityLocationChange(value); +} diff --git a/src/client_handler/accessibility_handler.h b/src/client_handler/accessibility_handler.h new file mode 100644 index 000000000..ba18171c5 --- /dev/null +++ b/src/client_handler/accessibility_handler.h @@ -0,0 +1,20 @@ +// Copyright (c) 2018 CEF Python, see the Authors file. +// All rights reserved. Licensed under BSD 3-clause license. +// Project website: https://github.com/cztomczak/cefpython + +#include "common/cefpython_public_api.h" +#include "include/cef_accessibility_handler.h" + + +class AccessibilityHandler : public CefAccessibilityHandler +{ +public: + AccessibilityHandler(){} + virtual ~AccessibilityHandler(){} + + virtual void OnAccessibilityTreeChange(CefRefPtr value) override; + virtual void OnAccessibilityLocationChange(CefRefPtr value) override; + +private: + IMPLEMENT_REFCOUNTING(AccessibilityHandler); +}; diff --git a/src/client_handler/client_handler.cpp b/src/client_handler/client_handler.cpp index f55da8123..daf9a4e59 100644 --- a/src/client_handler/client_handler.cpp +++ b/src/client_handler/client_handler.cpp @@ -30,6 +30,7 @@ bool ClientHandler::OnProcessMessageReceived( CefProcessId source_process, CefRefPtr message) { + // Return true if message was handled. if (source_process != PID_RENDERER) { return false; } @@ -42,6 +43,12 @@ bool ClientHandler::OnProcessMessageReceived( if (arguments->GetSize() == 1 && arguments->GetType(0) == VTYPE_INT) { int64 frameId = arguments->GetInt(0); CefRefPtr frame = browser->GetFrame(frameId); + if (!frame.get()) { + // Frame was already destroyed while IPC messaging was + // executing. Issue #431. User callback will not be + // executed in such case. + return true; + } V8ContextHandler_OnContextCreated(browser, frame); return true; } else { @@ -56,6 +63,11 @@ bool ClientHandler::OnProcessMessageReceived( && arguments->GetType(1) == VTYPE_INT) { int browserId = arguments->GetInt(0); int64 frameId = arguments->GetInt(1); + // Even if frame was alrady destroyed (Issue #431) you still + // want to call V8ContextHandler_OnContextReleased as it releases + // some resources. Thus passing IDs instead of actual + // objects. Cython code in V8ContextHandler_OnContextReleased + // will handle a case when frame is already destroyed. V8ContextHandler_OnContextReleased(browserId, frameId); return true; } else { @@ -75,8 +87,13 @@ bool ClientHandler::OnProcessMessageReceived( int64 frameId = arguments->GetInt(0); CefString functionName = arguments->GetString(1); CefRefPtr functionArguments = arguments->GetList(2); - CefRefPtr frame = browser->GetFrame(frameId); - V8FunctionHandler_Execute(browser, frame, functionName, + // Even if frame was already destroyed (Issue #431) you still + // want to call V8FunctionHandler_Execute, as it can run + // Python code without issues and doesn't require an actual + // frame. Thus passing IDs instead of actual objects. Cython + // code in V8FunctionHandler_Execute will handle a case when + // frame is already destroyed. + V8FunctionHandler_Execute(browser, frameId, functionName, functionArguments); return true; } else { @@ -100,18 +117,6 @@ bool ClientHandler::OnProcessMessageReceived( " messageName=ExecutePythonCallback"; return false; } - } else if (messageName == "RemovePythonCallbacksForFrame") { - CefRefPtr arguments = message->GetArgumentList(); - if (arguments->GetSize() == 1 && arguments->GetType(0) == VTYPE_INT) { - int frameId = arguments->GetInt(0); - RemovePythonCallbacksForFrame(frameId); - return true; - } else { - LOG(ERROR) << "[Browser process] OnProcessMessageReceived():" - " invalid arguments," - " messageName=ExecutePythonCallback"; - return false; - } } return false; } diff --git a/src/client_handler/display_handler.cpp b/src/client_handler/display_handler.cpp index 8e770510c..b00bf6a51 100644 --- a/src/client_handler/display_handler.cpp +++ b/src/client_handler/display_handler.cpp @@ -48,3 +48,15 @@ bool DisplayHandler::OnConsoleMessage(CefRefPtr browser, return DisplayHandler_OnConsoleMessage(browser, level, message, source, line); } + +bool DisplayHandler::OnAutoResize(CefRefPtr browser, + const CefSize& new_size) { + REQUIRE_UI_THREAD(); + return DisplayHandler_OnAutoResize(browser, new_size); +} + +void DisplayHandler::OnLoadingProgressChange(CefRefPtr browser, + double progress) { + REQUIRE_UI_THREAD(); + return DisplayHandler_OnLoadingProgressChange(browser, progress); +} \ No newline at end of file diff --git a/src/client_handler/display_handler.h b/src/client_handler/display_handler.h index f6f4c8658..a281713d9 100644 --- a/src/client_handler/display_handler.h +++ b/src/client_handler/display_handler.h @@ -33,6 +33,12 @@ class DisplayHandler : public CefDisplayHandler const CefString& source, int line) override; + bool OnAutoResize(CefRefPtr browser, + const CefSize& new_size) override; + + void OnLoadingProgressChange(CefRefPtr browser, + double progress) override; + private: IMPLEMENT_REFCOUNTING(DisplayHandler); }; diff --git a/src/client_handler/render_handler.cpp b/src/client_handler/render_handler.cpp index f6b4d5bec..cf45d15ee 100644 --- a/src/client_handler/render_handler.cpp +++ b/src/client_handler/render_handler.cpp @@ -106,3 +106,11 @@ void RenderHandler::UpdateDragCursor(CefRefPtr browser, REQUIRE_UI_THREAD(); RenderHandler_UpdateDragCursor(browser, operation); } + +void RenderHandler::OnTextSelectionChanged(CefRefPtr browser, + const CefString& selected_text, + const CefRange& selected_range) { + REQUIRE_UI_THREAD(); + RenderHandler_OnTextSelectionChanged(browser, selected_text, + selected_range); +} diff --git a/src/client_handler/render_handler.h b/src/client_handler/render_handler.h index 5baae3202..75eee86c5 100644 --- a/src/client_handler/render_handler.h +++ b/src/client_handler/render_handler.h @@ -4,14 +4,21 @@ #include "common/cefpython_public_api.h" #include "include/cef_render_handler.h" +#include "accessibility_handler.h" -class RenderHandler : public CefRenderHandler +class RenderHandler : public CefRenderHandler, + public AccessibilityHandler { public: RenderHandler(){} virtual ~RenderHandler(){} + CefRefPtr GetAccessibilityHandler() + override { + return this; + } + bool GetRootScreenRect(CefRefPtr browser, CefRect& rect) override; @@ -57,6 +64,10 @@ class RenderHandler : public CefRenderHandler void UpdateDragCursor(CefRefPtr browser, cef_drag_operations_mask_t operation) override; + void OnTextSelectionChanged(CefRefPtr browser, + const CefString& selected_text, + const CefRange& selected_range) override; + private: IMPLEMENT_REFCOUNTING(RenderHandler); }; diff --git a/src/client_handler/request_handler.cpp b/src/client_handler/request_handler.cpp index ea6815592..5227b8921 100644 --- a/src/client_handler/request_handler.cpp +++ b/src/client_handler/request_handler.cpp @@ -117,3 +117,18 @@ void RequestHandler::OnPluginCrashed(CefRefPtr browser, REQUIRE_UI_THREAD(); RequestHandler_OnPluginCrashed(browser, plugin_path); } + +bool RequestHandler::CanGetCookies(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr request) { + REQUIRE_IO_THREAD(); + return RequestHandler_CanGetCookies(browser, frame, request); +} + +bool RequestHandler::CanSetCookie(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr request, + const CefCookie& cookie) { + REQUIRE_IO_THREAD(); + return RequestHandler_CanSetCookie(browser, frame, request, cookie); +} diff --git a/src/client_handler/request_handler.h b/src/client_handler/request_handler.h index a56760b29..7e5e0e6c3 100644 --- a/src/client_handler/request_handler.h +++ b/src/client_handler/request_handler.h @@ -67,6 +67,15 @@ class RequestHandler : public CefRequestHandler void OnPluginCrashed(CefRefPtr browser, const CefString& plugin_path) override; + bool CanGetCookies(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr request) override; + + bool CanSetCookie(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr request, + const CefCookie& cookie) override; + private: IMPLEMENT_REFCOUNTING(RequestHandler); }; diff --git a/src/client_handler/util_mac.h b/src/client_handler/util_mac.h index 37ec7266b..63ee1dff7 100644 --- a/src/client_handler/util_mac.h +++ b/src/client_handler/util_mac.h @@ -11,6 +11,7 @@ #include "include/cef_browser.h" void MacInitialize(); +void MacShutdown(); void MacSetWindowTitle(CefRefPtr browser, char* title); #endif // CEFPYTHON_UTIL_MAC_H_ diff --git a/src/client_handler/util_mac.mm b/src/client_handler/util_mac.mm index f2e14cd1b..4916b253a 100644 --- a/src/client_handler/util_mac.mm +++ b/src/client_handler/util_mac.mm @@ -2,6 +2,16 @@ // reserved. Use of this source code is governed by a BSD-style license that // can be found in the LICENSE file. +// Copyright (c) 2015 CEF Python, see the Authors file. +// All rights reserved. Licensed under BSD 3-clause license. +// Project website: https://github.com/cztomczak/cefpython + +// Some code was copied from here: +// java-cef: src/master/native/util_mac.mm +// upstream cef: src/tests/ceftests/run_all_unittests_mac.mm +// upstream cef: src/tests/cefclient/cefclient_mac.mm +// upstream cef: src/tests/cefsimple/cefsimple_mac.mm + #import "util_mac.h" #import #include @@ -11,13 +21,14 @@ namespace { +// static NSAutoreleasePool* g_autopool = nil; BOOL g_handling_send_event = false; } // namespace -// Add the necessary CrAppControlProtocol -// functionality to NSApplication using categories and swizzling. -@interface NSApplication (CEFPythonApplication) +// Add the necessary CefAppProtocol functionality to NSApplication +// using categories and swizzling (Issue #442, Issue #156). +@interface NSApplication (CEFPythonApplication) - (BOOL)isHandlingSendEvent; - (void)setHandlingSendEvent:(BOOL)handlingSendEvent; @@ -63,9 +74,16 @@ - (void)_swizzled_terminate:(id)sender { @end void MacInitialize() { + // OFF: it's causing a crash during shutdown release + // g_autopool = [[NSAutoreleasePool alloc] init]; [NSApplication sharedApplication]; } +void MacShutdown() { + // OFF: it's causing a crash during shutdown release + // [g_autopool release]; +} + void MacSetWindowTitle(CefRefPtr browser, char* title) { NSView* view = browser->GetHost()->GetWindowHandle(); NSString* nstitle = [NSString stringWithFormat:@"%s" , title]; diff --git a/src/common/cefpython_public_api.h b/src/common/cefpython_public_api.h index 0fbd60a56..c796388e6 100644 --- a/src/common/cefpython_public_api.h +++ b/src/common/cefpython_public_api.h @@ -44,6 +44,12 @@ #include "../../build/build_cefpython/cefpython_py35_fixed.h" #elif PY_MINOR_VERSION == 6 #include "../../build/build_cefpython/cefpython_py36_fixed.h" +#elif PY_MINOR_VERSION == 7 +#include "../../build/build_cefpython/cefpython_py37_fixed.h" +#elif PY_MINOR_VERSION == 8 +#include "../../build/build_cefpython/cefpython_py38_fixed.h" +#elif PY_MINOR_VERSION == 9 +#include "../../build/build_cefpython/cefpython_py39_fixed.h" #endif // PY_MINOR_VERSION #endif // PY_MAJOR_VERSION diff --git a/src/compile_time_constants.pxi b/src/compile_time_constants.pxi index 632aab59c..bf130d6ed 100644 --- a/src/compile_time_constants.pxi +++ b/src/compile_time_constants.pxi @@ -1,3 +1,10 @@ # This file was generated by setup.py + +# Type this command to ignore changes to this file: +# git update-index --assume-unchanged src/compile_time_constants.pxi + DEF UNAME_SYSNAME = "Windows" DEF PY_MAJOR_VERSION = 3 +cdef extern from "limits.h": + cdef int INT_MIN + cdef int INT_MAX diff --git a/src/cookie.pyx b/src/cookie.pyx index 45b692ccb..6d90144b0 100644 --- a/src/cookie.pyx +++ b/src/cookie.pyx @@ -119,6 +119,19 @@ cdef class Cookie: return CefToPyString(cefString) cpdef py_void SetDomain(self, py_string domain): + pattern = re.compile(r"^(?:[a-z0-9](?:[a-z0-9-_]{0,61}[a-z0-9])?\.)" + r"+[a-z0-9][a-z0-9-_]{0,61}[a-z]$") + if PY_MAJOR_VERSION == 2: + assert isinstance(domain, bytes), "domain type is not bytes" + domain = domain.decode(g_applicationSettings["string_encoding"], + errors=BYTES_DECODE_ERRORS) + try: + if not pattern.match(domain.encode("idna").decode("ascii")): + raise Exception("Cookie.SetDomain() failed, invalid domain: {0}" + .format(domain)) + except UnicodeError: + raise Exception("Cookie.SetDomain() failed, invalid domain: {0}" + .format(domain)) cdef CefString cefString cefString.Attach(&self.cefCookie.domain, False) PyToCefString(domain, cefString) @@ -181,9 +194,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 +208,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) @@ -268,6 +301,11 @@ cdef class PyCookieManager: PyToCefStringValue(path), bool(persistSessionCookies), NULL) + cpdef py_bool FlushStore(self, callback=None): + return self.cefCookieManager.get().FlushStore( + NULL) + + # ------------------------------------------------------------------------------ # PyCookieVisitor # ------------------------------------------------------------------------------ diff --git a/src/cpp_utils/Makefile b/src/cpp_utils/Makefile index 338fd960b..e6a6a2fe5 100644 --- a/src/cpp_utils/Makefile +++ b/src/cpp_utils/Makefile @@ -7,7 +7,7 @@ OUT = libcpp_utils.a INC = -I./../ -I/usr/include/gtk-2.0 \ -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/gtk-2.0/include \ -I/usr/lib/i386-linux-gnu/glib-2.0/include -I/usr/include/cairo \ - -I/usr/include/pango-1.0 -I/usr/include/gdk-pixbuf-2.0 \ + -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/gdk-pixbuf-2.0 \ -I/usr/include/atk-1.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \ -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include \ -I/usr/lib64/glib-2.0/include -I/usr/lib64/gtk-2.0/include \ diff --git a/src/dpi_aware_win.pyx b/src/dpi_aware_win.pyx index 4a02184c7..a7fc345fa 100644 --- a/src/dpi_aware_win.pyx +++ b/src/dpi_aware_win.pyx @@ -6,42 +6,65 @@ include "cefpython.pyx" class DpiAware: - @staticmethod - def GetSystemDpi(): - # Win7 DPI (Control Panel > Appearance and Personalization > Display): - # text size Larger 150% => dpix/dpiy 144 - # text size Medium 125% => dpix/dpiy 120 - # text size Smaller 100% => dpix/dpiy 96 - # - # dpix=96 zoomlevel=0.0 - # dpix=120 zoomlevel=1.0 - # dpix=144 zoomlevel=2.0 - # dpix=72 zoomlevel=-1.0 - # - # If DPI awareness wasn't yet enabled, then GetSystemDpi - # will always return a default 96 DPI. + @classmethod + def GetSystemDpi(cls): + """Returns Windows DPI settings ("Custom scaling" on Win10). + + Win7 DPI (Control Panel > Appearance and Personalization > Display): + text size Larger 150% => dpix/dpiy 144 + text size Medium 125% => dpix/dpiy 120 + text size Smaller 100% => dpix/dpiy 96 + + DPI settings should not be cached. When SetProcessDpiAware + is not yet called, then OS returns 96 DPI, even though it + is set to 144 DPI. After DPI Awareness is enabled for the + running process it will return the correct 144 DPI. + """ cdef int dpix = 0 cdef int dpiy = 0 GetSystemDpi(&dpix, &dpiy) - return tuple(dpix, dpiy) + return dpix, dpiy - @staticmethod - def CalculateWindowSize(int width, int height): + @classmethod + def CalculateWindowSize(cls, int width, int height): + """@DEPRECATED. Use Scale() method instead.""" # Calculation for DPI < 96 is not yet supported. GetDpiAwareWindowSize(&width, &height) - return tuple(width, height) + return width, height + + @classmethod + def Scale(cls, arg): + """Scale units for high DPI devices. Argument can be an int, + tuple or list.""" + (dpix, dpiy) = DpiAware.GetSystemDpi() + # - Using only "dpix" value to calculate zoom level since all + # modern displays have equal horizontal and vertical resolution. + default_dpix = 96 + scale = MulDiv(dpix, 100, default_dpix) + if isinstance(arg, (int, long)): + v = arg + new_value = MulDiv(v, scale, 100) + return new_value + ret_tuple = isinstance(arg, tuple) + ret = list() + for i,v in enumerate(arg): + v = int(v) + ret.append(MulDiv(v, scale, 100)) + if ret_tuple: + return tuple(ret) + return ret - @staticmethod - def IsProcessDpiAware(): + @classmethod + def IsProcessDpiAware(cls): return IsProcessDpiAware() - @staticmethod - def SetProcessDpiAware(): + @classmethod + def SetProcessDpiAware(cls): """Deprecated.""" DpiAware.EnableHighDpiSupport() - @staticmethod - def EnableHighDpiSupport(): + @classmethod + def EnableHighDpiSupport(cls): # This CEF function sets process to be DPI aware. This # CEF func is also called in subprocesses. CefEnableHighDPISupport() diff --git a/src/drag_data.pyx b/src/drag_data.pyx index ddb7d9f98..5da99a6f2 100644 --- a/src/drag_data.pyx +++ b/src/drag_data.pyx @@ -36,20 +36,16 @@ cdef class DragData: cpdef py_string GetFragmentHtml(self): return CefToPyString(self.cef_drag_data.get().GetFragmentHtml()) - IF UNAME_SYSNAME == "Linux": - - cpdef PyImage GetImage(self): - cdef CefRefPtr[CefImage] cef_image =\ - self.cef_drag_data.get().GetImage() - if not cef_image.get(): - raise Exception("Image is not available") - return PyImage_Init(cef_image) - - cpdef tuple GetImageHotspot(self): - cdef CefPoint point = self.cef_drag_data.get().GetImageHotspot() - return (point.x, point.y) - - cpdef py_bool HasImage(self): - return self.cef_drag_data.get().HasImage() - - # END IF UNAME_SYSNAME == "Linux": + cpdef PyImage GetImage(self): + cdef CefRefPtr[CefImage] cef_image =\ + self.cef_drag_data.get().GetImage() + if not cef_image.get(): + raise Exception("Image is not available") + return PyImage_Init(cef_image) + + cpdef tuple GetImageHotspot(self): + cdef CefPoint point = self.cef_drag_data.get().GetImageHotspot() + return point.x, point.y + + cpdef py_bool HasImage(self): + return self.cef_drag_data.get().HasImage() diff --git a/src/extern/cef/cef_browser.pxd b/src/extern/cef/cef_browser.pxd index 50a70393a..da209d5c3 100644 --- a/src/extern/cef/cef_browser.pxd +++ b/src/extern/cef/cef_browser.pxd @@ -11,10 +11,11 @@ from libcpp cimport bool as cpp_bool from libcpp.vector cimport vector as cpp_vector from cef_frame cimport CefFrame cimport cef_types -from cef_types cimport int64 +from cef_types cimport int64, cef_state_t, CefSize 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 @@ -61,6 +62,9 @@ cdef extern from "include/cef_browser.h": const CefBrowserSettings& settings, const CefPoint& inspect_element_at) void CloseDevTools() + cpp_bool HasDevTools() + + CefRefPtr[CefRequestContext] GetRequestContext() void Find(int identifier, const CefString& searchText, cpp_bool forward, cpp_bool matchCase, cpp_bool findNext) @@ -83,6 +87,12 @@ cdef extern from "include/cef_browser.h": void ReplaceMisspelling(const CefString& word) void AddWordToDictionary(const CefString& word) + void SetAccessibilityState(cef_state_t accessibility_state) + void Invalidate(cef_types.cef_paint_element_type_t element_type) + void SetAutoResizeEnabled(cpp_bool enabled, + const CefSize& min_size, + const CefSize& max_size) + cdef cppclass CefBrowser: diff --git a/src/extern/cef/cef_cookie.pxd b/src/extern/cef/cef_cookie.pxd index 94263a6b4..c113aea9a 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) @@ -47,7 +53,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 diff --git a/src/extern/cef/cef_cookie_manager_namespace.pxd b/src/extern/cef/cef_cookie_manager_namespace.pxd index bcc8a1b33..daf1a0f38 100644 --- a/src/extern/cef/cef_cookie_manager_namespace.pxd +++ b/src/extern/cef/cef_cookie_manager_namespace.pxd @@ -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: @@ -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) diff --git a/src/extern/cef/cef_file_util.pxd b/src/extern/cef/cef_file_util.pxd new file mode 100644 index 000000000..52e62580d --- /dev/null +++ b/src/extern/cef/cef_file_util.pxd @@ -0,0 +1,8 @@ +# Copyright (c) 2018 CEF Python, see the Authors file. +# All rights reserved. Licensed under BSD 3-clause license. +# Project website: https://github.com/cztomczak/cefpython + +from cef_string cimport CefString + +cdef extern from "include/cef_file_util.h": + void CefLoadCRLSetsFile(const CefString& path) diff --git a/src/extern/cef/cef_request_context.pxd b/src/extern/cef/cef_request_context.pxd index 811cf72f5..116741749 100644 --- a/src/extern/cef/cef_request_context.pxd +++ b/src/extern/cef/cef_request_context.pxd @@ -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: @@ -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) diff --git a/src/extern/cef/cef_scoped_ptr.pxd b/src/extern/cef/cef_scoped_ptr.pxd index 4498360cd..2e770a5b9 100644 --- a/src/extern/cef/cef_scoped_ptr.pxd +++ b/src/extern/cef/cef_scoped_ptr.pxd @@ -13,3 +13,5 @@ cdef extern from "include/base/cef_scoped_ptr.h": void reset(T* p) # noinspection PyUnresolvedReferences T* get() + # noinspection PyUnresolvedReferences + scoped_ptr[T]& Assign "operator="(scoped_ptr[T] p) diff --git a/src/extern/cef/cef_string.pxd b/src/extern/cef/cef_string.pxd index 74f2e342b..0986d7a6c 100644 --- a/src/extern/cef/cef_string.pxd +++ b/src/extern/cef/cef_string.pxd @@ -21,5 +21,5 @@ cdef extern from "include/internal/cef_string.h": cpp_bool FromString(cpp_string& str) cpp_string ToString() cpp_wstring ToWString() - char* c_str() + const char* c_str() size_t length() diff --git a/src/extern/cef/cef_task.pxd b/src/extern/cef/cef_task.pxd index 806cbe897..fbc0181e0 100644 --- a/src/extern/cef/cef_task.pxd +++ b/src/extern/cef/cef_task.pxd @@ -9,7 +9,6 @@ from cef_types cimport int64 from cef_ptr cimport CefRefPtr cdef extern from "include/cef_task.h": - ctypedef int CefThreadId ctypedef cef_types.cef_thread_id_t CefThreadId cdef cpp_bool CefCurrentlyOn(CefThreadId) diff --git a/src/extern/cef/cef_types.pxd b/src/extern/cef/cef_types.pxd index 70263780f..6bd21ba46 100644 --- a/src/extern/cef/cef_types.pxd +++ b/src/extern/cef/cef_types.pxd @@ -377,3 +377,7 @@ cdef extern from "include/internal/cef_types.h": ctypedef enum cef_focus_source_t: FOCUS_SOURCE_NAVIGATION, FOCUS_SOURCE_SYSTEM, + + cdef cppclass CefRange: + int from_val "from" + int to_val "to" diff --git a/src/extern/cef/cef_values.pxd b/src/extern/cef/cef_values.pxd index 201bb82be..3210432b4 100644 --- a/src/extern/cef/cef_values.pxd +++ b/src/extern/cef/cef_values.pxd @@ -12,6 +12,19 @@ cdef extern from "include/cef_values.h": cdef CefRefPtr[CefBinaryValue] CefBinaryValue_Create \ "CefBinaryValue::Create"(const void* data, size_t data_size) + cdef cppclass CefValue: + cpp_bool IsValid() + cpp_bool IsOwned() + cpp_bool IsReadOnly() + cef_value_type_t GetType() + cpp_bool GetBool() + int GetInt() + double GetDouble() + CefString GetString() + CefRefPtr[CefBinaryValue] GetBinary() + CefRefPtr[CefDictionaryValue] GetDictionary() + CefRefPtr[CefListValue] GetList() + cdef cppclass CefBinaryValue: cpp_bool IsValid() cpp_bool IsOwned() @@ -59,20 +72,20 @@ cdef extern from "include/cef_values.h": cpp_bool SetSize(size_t size) size_t GetSize() cpp_bool Clear() - cpp_bool Remove(int index) - cef_value_type_t GetType(int index) - cpp_bool GetBool(int index) - int GetInt(int index) - double GetDouble(int index) - CefString GetString(int index) - CefRefPtr[CefBinaryValue] GetBinary(int index) - CefRefPtr[CefDictionaryValue] GetDictionary(int index) - CefRefPtr[CefListValue] GetList(int index) - cpp_bool SetNull(int index) - cpp_bool SetBool(int index, cpp_bool value) - cpp_bool SetInt(int index, int value) - cpp_bool SetDouble(int index, double value) - cpp_bool SetString(int index, const CefString& value) - cpp_bool SetBinary(int index, CefRefPtr[CefBinaryValue] value) - cpp_bool SetDictionary(int index, CefRefPtr[CefDictionaryValue] value) - cpp_bool SetList(int index, CefRefPtr[CefListValue] value) + cpp_bool Remove(size_t index) + cef_value_type_t GetType(size_t index) + cpp_bool GetBool(size_t index) + int GetInt(size_t index) + double GetDouble(size_t index) + CefString GetString(size_t index) + CefRefPtr[CefBinaryValue] GetBinary(size_t index) + CefRefPtr[CefDictionaryValue] GetDictionary(size_t index) + CefRefPtr[CefListValue] GetList(size_t index) + cpp_bool SetNull(size_t index) + cpp_bool SetBool(size_t index, cpp_bool value) + cpp_bool SetInt(size_t index, int value) + cpp_bool SetDouble(size_t index, double value) + cpp_bool SetString(size_t index, const CefString& value) + cpp_bool SetBinary(size_t index, CefRefPtr[CefBinaryValue] value) + cpp_bool SetDictionary(size_t index, CefRefPtr[CefDictionaryValue] value) + cpp_bool SetList(size_t index, CefRefPtr[CefListValue] value) diff --git a/src/extern/dpi_aware_win.pxd b/src/extern/dpi_aware_win.pxd index 3842062a2..fd9a3cbe1 100644 --- a/src/extern/dpi_aware_win.pxd +++ b/src/extern/dpi_aware_win.pxd @@ -9,4 +9,3 @@ cdef extern from "client_handler/dpi_aware.h": cdef void GetDpiAwareWindowSize(int* width, int* height) cdef void SetProcessDpiAware() cdef cpp_bool IsProcessDpiAware() - diff --git a/src/extern/mac.pxd b/src/extern/mac.pxd index cca038b98..e23b4b980 100644 --- a/src/extern/mac.pxd +++ b/src/extern/mac.pxd @@ -7,4 +7,5 @@ from cef_browser cimport CefBrowser cdef extern from "client_handler/util_mac.h": void MacInitialize() + void MacShutdown() void MacSetWindowTitle(CefRefPtr[CefBrowser] browser, char* title) diff --git a/src/extern/windows.pxd b/src/extern/windows.pxd index 82355a60e..5e329b2a3 100644 --- a/src/extern/windows.pxd +++ b/src/extern/windows.pxd @@ -23,6 +23,8 @@ cdef extern from "Windows.h" nogil: ctypedef HICON HCURSOR ctypedef unsigned int UINT + ctypedef unsigned int UINT_PTR + # noinspection PyUnresolvedReferences ctypedef wchar_t* LPCTSTR # noinspection PyUnresolvedReferences @@ -49,7 +51,11 @@ cdef extern from "Windows.h" nogil: cdef size_t mbstowcs(wchar_t *wcstr, const_char *mbstr, size_t count) ctypedef void* HDWP + cdef int SWP_NOZORDER + cdef int SWP_NOACTIVATE + cdef int SWP_FRAMECHANGED + cdef HDWP BeginDeferWindowPos(int nNumWindows) cdef HDWP DeferWindowPos( HDWP hWinPosInfo, HWND hWnd, HWND hWndInsertAfter, @@ -64,8 +70,6 @@ cdef extern from "Windows.h" nogil: cdef BOOL PostMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) - ctypedef unsigned int UINT_PTR - ctypedef unsigned int UINT ctypedef struct TIMERPROC: pass cdef UINT_PTR SetTimer( @@ -108,9 +112,6 @@ cdef extern from "Windows.h" nogil: cdef BOOL SetWindowPos( HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags) - cdef int SWP_NOZORDER - cdef int SWP_NOACTIVATE - cdef int SWP_FRAMECHANGED cdef DWORD GetLastError() cdef BOOL IsWindow(HWND hWnd) @@ -127,3 +128,5 @@ cdef extern from "Windows.h" nogil: cdef int ICON_SMALL cdef HWND GetParent(HWND hwnd) + cdef int MulDiv(int number, int numerator, int denominator) + diff --git a/src/frame.pyx b/src/frame.pyx index 6535791dc..812e4b3c9 100644 --- a/src/frame.pyx +++ b/src/frame.pyx @@ -7,6 +7,10 @@ include "browser.pyx" cdef dict g_pyFrames = {} +# If a frame was unreferenced (browser closed or OnContextReleased) +# it shouldn't be kept global anymore. +cdef list g_unreferenced_frames = [] # [str unique identifier, ..] + cdef object GetUniqueFrameId(int browserId, object frameId): return str(browserId) +"#"+ str(frameId) @@ -28,8 +32,17 @@ cdef PyFrame GetPyFrame(CefRefPtr[CefFrame] cefFrame): assert (frameId and browserId), "frameId or browserId empty" cdef object uniqueFrameId = GetUniqueFrameId(browserId, frameId) - if uniqueFrameId in g_pyFrames: - return g_pyFrames[uniqueFrameId] + if frameId < 0: + # Underlying frame does not yet exist. In such case PyFrame + # is not stored in g_pyFrames since frameId is invalid. + # However even though frame is not supposed to exist, you + # can still call CefFrame.ExecuteFunction and it works fine + # in tutorial.py example. + Debug("GetPyFrame(): underlying frame does not yet exist:" + " browserId = {0}, frameId = {1}".format(browserId, frameId)) + else: + if uniqueFrameId in g_pyFrames: + return g_pyFrames[uniqueFrameId] # This code probably ain't needed. # ---- @@ -46,9 +59,13 @@ cdef PyFrame GetPyFrame(CefRefPtr[CefFrame] cefFrame): pyFrame = PyFrame(browserId, frameId) pyFrame.cefFrame = cefFrame - if browserId in g_unreferenced_browsers: + if uniqueFrameId in g_unreferenced_frames \ + or frameId < 0 \ + or browserId in g_unreferenced_browsers \ + or browserId in g_closed_browsers: # Browser was already globally unreferenced in OnBeforeClose, - # thus all frames are globally unreferenced too. Create a new + # thus all frames are globally unreferenced too, or frame + # was unreferenced in OnContextReleased. Create a new # incomplete instance of PyFrame object. Read comments in # browser.pyx > GetPyBrowser and in Browser.md for what # "incomplete" means. @@ -60,17 +77,23 @@ cdef PyFrame GetPyFrame(CefRefPtr[CefFrame] cefFrame): # SIDE EFFECT: two calls to GetPyFrame for the same frame object # may return two different PyFrame objects. Compare # frame objects always using GetIdentifier(). - # Debug("GetPyFrame(): create new PyFrame, frameId=%s" % frameId) + Debug("GetPyFrame(): create new PyFrame, frameId=%s" % frameId) g_pyFrames[uniqueFrameId] = pyFrame return pyFrame cdef void RemovePyFrame(int browserId, object frameId) except *: # Called from V8ContextHandler_OnContextReleased(). global g_pyFrames + cdef PyFrame pyFrame cdef object uniqueFrameId = GetUniqueFrameId(browserId, frameId) if uniqueFrameId in g_pyFrames: Debug("del g_pyFrames[%s]" % uniqueFrameId) + pyFrame = g_pyFrames[uniqueFrameId] + pyFrame.cefFrame.Assign(NULL) + del pyFrame del g_pyFrames[uniqueFrameId] + g_unreferenced_frames.append(uniqueFrameId) + RemovePythonCallbacksForFrame(frameId) else: Debug("RemovePyFrame() FAILED: uniqueFrameId = %s" % uniqueFrameId) @@ -85,7 +108,13 @@ cdef void RemovePyFramesForBrowser(int browserId) except *: toRemove.append(uniqueFrameId) for uniqueFrameId in toRemove: Debug("del g_pyFrames[%s]" % uniqueFrameId) + pyFrame = g_pyFrames[uniqueFrameId] + pyFrame.cefFrame.Assign(NULL) + del pyFrame del g_pyFrames[uniqueFrameId] + g_unreferenced_frames.append(uniqueFrameId) + # RemovePythonCallbacksForBrowser already called + # in LifespanHandler_OnBeforeClose. cdef class PyFrame: cdef CefRefPtr[CefFrame] cefFrame @@ -182,7 +211,6 @@ cdef class PyFrame: self.GetCefFrame().get().LoadString(cefValue, cefUrl) cpdef py_void LoadUrl(self, py_string url): - url = GetNavigateUrl(url) cdef CefString cefUrl PyToCefString(url, cefUrl) self.GetCefFrame().get().LoadURL(cefUrl) diff --git a/src/handlers/accessibility_handler.pyx b/src/handlers/accessibility_handler.pyx new file mode 100644 index 000000000..055fb61cf --- /dev/null +++ b/src/handlers/accessibility_handler.pyx @@ -0,0 +1,34 @@ +# Copyright (c) 2018 CEF Python, see the Authors file. +# All rights reserved. Licensed under BSD 3-clause license. +# Project website: https://github.com/cztomczak/cefpython + +include "../cefpython.pyx" +include "../browser.pyx" +include "../frame.pyx" +include "../process_message_utils.pyx" + +cdef public void AccessibilityHandler_OnAccessibilityTreeChange( + CefRefPtr[CefValue] cefValue + ) except * with gil: + cdef object value = CefValueToPyValue(cefValue) + cdef object callback + try: + callback = GetGlobalClientCallback("OnAccessibilityTreeChange") + if callback: + callback(value=value) + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) + +cdef public void AccessibilityHandler_OnAccessibilityLocationChange( + CefRefPtr[CefValue] cefValue + ) except * with gil: + cdef object value = CefValueToPyValue(cefValue) + cdef object callback + try: + callback = GetGlobalClientCallback("OnAccessibilityLocationChange") + if callback: + callback(value=value) + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/handlers/display_handler.pyx b/src/handlers/display_handler.pyx index 3ad7a7f6b..7d66d00f7 100644 --- a/src/handlers/display_handler.pyx +++ b/src/handlers/display_handler.pyx @@ -25,6 +25,25 @@ cdef public void DisplayHandler_OnAddressChange( (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) + +cdef public cpp_bool DisplayHandler_OnAutoResize( + CefRefPtr[CefBrowser] cef_browser, + const CefSize& new_size + ) except * with gil: + cdef PyBrowser browser + cdef object callback + try: + browser = GetPyBrowser(cef_browser, "OnAutoResize") + callback = browser.GetClientCallback("OnAutoResize") + if callback: + return bool(callback(browser=browser, new_size=[new_size.width, + new_size.height])) + return False + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) + + cdef public void DisplayHandler_OnTitleChange( CefRefPtr[CefBrowser] cefBrowser, const CefString& cefTitle @@ -109,3 +128,18 @@ cdef public cpp_bool DisplayHandler_OnConsoleMessage( except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) + +cdef public void DisplayHandler_OnLoadingProgressChange( + CefRefPtr[CefBrowser] cefBrowser, + double progress + ) except * with gil: + cdef PyBrowser pyBrowser + cdef object callback + try: + pyBrowser = GetPyBrowser(cefBrowser, "OnLoadingProgressChange") + callback = pyBrowser.GetClientCallback("OnLoadingProgressChange") + if callback: + callback(browser=pyBrowser, progress=progress) + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/handlers/keyboard_handler.pyx b/src/handlers/keyboard_handler.pyx index c895b9b5e..30c04e2f5 100644 --- a/src/handlers/keyboard_handler.pyx +++ b/src/handlers/keyboard_handler.pyx @@ -3,6 +3,7 @@ # Project website: https://github.com/cztomczak/cefpython include "../cefpython.pyx" +include "../browser.pyx" # noinspection PyUnresolvedReferences cimport cef_types @@ -72,39 +73,67 @@ cdef public cpp_bool KeyboardHandler_OnPreKeyEvent( (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) + +cdef py_bool HandleKeyboardShortcuts(PyBrowser browser, dict event): + """Default implementation for keyboard shortcuts on Mac (Issue #161).""" + if platform.system() == "Darwin": + if event["modifiers"] == 128 \ + and event["type"] != KEYEVENT_RAWKEYDOWN: + # Copy and paste was handled in RAWKEYDOWN, return True + if event["native_key_code"] in [8, 9]: + return True + if event["modifiers"] == 128 \ + and event["type"] == KEYEVENT_RAWKEYDOWN: + # Select all: command + a + if event["native_key_code"] == 0: + browser.GetMainFrame().SelectAll() + return True + # Copy: command + c + elif event["native_key_code"] == 8: + browser.GetMainFrame().Copy() + return True + # Paste: command + v + elif event["native_key_code"] == 9: + browser.GetMainFrame().Paste() + return True + # Cut: command + x + elif event["native_key_code"] == 7: + browser.GetMainFrame().Cut() + return True + # Undo: command + z + elif event["native_key_code"] == 6: + browser.GetMainFrame().Undo() + return True + elif event["modifiers"] == 130 \ + and event["type"] == KEYEVENT_RAWKEYDOWN: + # Redo: command + shift + z + if event["native_key_code"] == 6: + browser.GetMainFrame().Redo() + return True + return False + cdef public cpp_bool KeyboardHandler_OnKeyEvent( CefRefPtr[CefBrowser] cefBrowser, const cef_types.CefKeyEvent& cefEvent, cef_types.CefEventHandle cefEventHandle ) except * with gil: - cdef PyBrowser pyBrowser - cdef dict pyEvent + cdef PyBrowser browser + cdef dict event cdef py_bool returnValue cdef object callback try: - pyBrowser = GetPyBrowser(cefBrowser, "OnKeyEvent") - pyEvent = CefToPyKeyEvent(cefEvent) - callback = pyBrowser.GetClientCallback("OnKeyEvent") + browser = GetPyBrowser(cefBrowser, "OnKeyEvent") + event = CefToPyKeyEvent(cefEvent) + callback = browser.GetClientCallback("OnKeyEvent") if callback: returnValue = callback( - browser=pyBrowser, - event=pyEvent, + browser=browser, + event=event, event_handle=PyLong_FromVoidPtr(cefEventHandle)) # If returnValue is False then handle copy/paste on Mac if returnValue: return bool(returnValue) - if platform.system() == "Darwin": - # Handle copy: command + c - if pyEvent["modifiers"] == 128 \ - and pyEvent["native_key_code"] == 8: - pyBrowser.GetFocusedFrame().Copy() - return True - # Handle paste: command + v - elif pyEvent["modifiers"] == 128 \ - and pyEvent["native_key_code"] == 9: - pyBrowser.GetFocusedFrame().Paste() - return True - return False + return bool(HandleKeyboardShortcuts(browser, event)) except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/handlers/lifespan_handler.pyx b/src/handlers/lifespan_handler.pyx index 32a3f0c46..bfe22bd11 100644 --- a/src/handlers/lifespan_handler.pyx +++ b/src/handlers/lifespan_handler.pyx @@ -110,6 +110,7 @@ cdef public void LifespanHandler_OnBeforeClose( CefRefPtr[CefBrowser] cefBrowser ) except * with gil: cdef PyBrowser pyBrowser + cdef int browserId cdef object callback try: Debug("LifespanHandler_OnBeforeClose") @@ -126,14 +127,29 @@ cdef public void LifespanHandler_OnBeforeClose( callback = pyBrowser.GetClientCallback("OnBeforeClose") if callback: callback(browser=pyBrowser) - RemovePythonCallbacksForBrowser(pyBrowser.GetIdentifier()) - RemovePyFramesForBrowser(pyBrowser.GetIdentifier()) - RemovePyBrowser(pyBrowser.GetIdentifier()) + + # 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( + NULL) \ + .get().FlushStore(NULL) + + browserId = pyBrowser.GetIdentifier() + pyBrowser.cefBrowser.Assign(NULL) + cefBrowser.Assign(NULL) + del pyBrowser + + RemovePythonCallbacksForBrowser(browserId) + RemovePyFramesForBrowser(browserId) + RemovePyBrowser(browserId) + if g_MessageLoop_called and not len(g_pyBrowsers): # Automatically quit message loop when last browser was closed. # This is required for hello_world.py example to work. - QuitMessageLoop() - + PostTask(TID_UI, QuitMessageLoop) except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/handlers/render_handler.pyx b/src/handlers/render_handler.pyx index 1594d6bd6..a8ad49124 100644 --- a/src/handlers/render_handler.pyx +++ b/src/handlers/render_handler.pyx @@ -4,8 +4,10 @@ include "../cefpython.pyx" include "../browser.pyx" +include "../string_utils.pyx" cimport cef_types +from cef_types cimport CefRange # cef_paint_element_type_t, PaintElementType PET_VIEW = cef_types.PET_VIEW @@ -288,3 +290,20 @@ cdef public void RenderHandler_UpdateDragCursor( (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) +cdef public void RenderHandler_OnTextSelectionChanged( + CefRefPtr[CefBrowser] cef_browser, + const CefString& selected_text, + const CefRange& selected_range + ) except * with gil: + cdef PyBrowser browser + try: + browser = GetPyBrowser(cef_browser, "OnTextSelectionChanged") + callback = browser.GetClientCallback("OnTextSelectionChanged") + if callback: + callback(browser=browser, + selected_text=CefToPyString(selected_text), + selected_range=[selected_range.from_val, + selected_range.to_val]) + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/handlers/request_handler.pyx b/src/handlers/request_handler.pyx index 02bbc15d8..866942bfd 100644 --- a/src/handlers/request_handler.pyx +++ b/src/handlers/request_handler.pyx @@ -4,6 +4,7 @@ include "../cefpython.pyx" include "../browser.pyx" +include "../cookie.pyx" # cef_termination_status_t cimport cef_types @@ -68,6 +69,11 @@ cdef public cpp_bool RequestHandler_OnBeforeBrowse( cdef object clientCallback cdef py_bool returnValue try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return False + pyBrowser = GetPyBrowser(cefBrowser, "OnBeforeBrowse") pyFrame = GetPyFrame(cefFrame) pyRequest = CreatePyRequest(cefRequest) @@ -98,6 +104,11 @@ cdef public cpp_bool RequestHandler_OnBeforeResourceLoad( cdef object clientCallback cdef py_bool returnValue try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return False + pyBrowser = GetPyBrowser(cefBrowser, "OnBeforeResourceLoad") pyFrame = GetPyFrame(cefFrame) pyRequest = CreatePyRequest(cefRequest) @@ -126,6 +137,11 @@ cdef public CefRefPtr[CefResourceHandler] RequestHandler_GetResourceHandler( cdef object clientCallback cdef object returnValue try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return NULL + pyBrowser = GetPyBrowser(cefBrowser, "GetResourceHandler") pyFrame = GetPyFrame(cefFrame) pyRequest = CreatePyRequest(cefRequest) @@ -162,6 +178,11 @@ cdef public void RequestHandler_OnResourceRedirect( cdef PyResponse pyResponse cdef object clientCallback try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return + pyBrowser = GetPyBrowser(cefBrowser, "OnResourceRedirect") pyFrame = GetPyFrame(cefFrame) pyOldUrl = CefToPyString(cefOldUrl) @@ -207,6 +228,11 @@ cdef public cpp_bool RequestHandler_GetAuthCredentials( cdef list pyPasswordOut cdef object clientCallback try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return False + pyBrowser = GetPyBrowser(cefBrowser, "GetAuthCredentials") pyFrame = GetPyFrame(cefFrame) pyIsProxy = bool(cefIsProxy) @@ -261,6 +287,11 @@ cdef public cpp_bool RequestHandler_OnQuotaRequest( cdef py_bool returnValue cdef object clientCallback try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return False + pyBrowser = GetPyBrowser(cefBrowser, "OnQuotaRequest") pyOriginUrl = CefToPyString(cefOriginUrl) clientCallback = pyBrowser.GetClientCallback("OnQuotaRequest") @@ -291,10 +322,16 @@ cdef public CefRefPtr[CefCookieManager] RequestHandler_GetCookieManager( cdef object clientCallback cdef PyCookieManager returnValue try: + # Issue #429: in some cases due to a race condition the browser + # may be NULL. if not cefBrowser.get(): - # Bug: In some cases due to a race condition the browser - # may be NULL. Issue #429. return NULL + + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return NULL + pyBrowser = GetPyBrowser(cefBrowser, "GetCookieManager") pyMainUrl = CefToPyString(cefMainUrl) clientCallback = pyBrowser.GetClientCallback("GetCookieManager") @@ -325,6 +362,11 @@ cdef public void RequestHandler_OnProtocolExecution( cdef list pyAllowOSExecutionOut cdef object clientCallback try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return + pyBrowser = GetPyBrowser(cefBrowser, "OnProtocolExecution") pyUrl = CefToPyString(cefUrl) pyAllowOSExecutionOut = [bool(cefAllowOSExecution)] @@ -369,6 +411,12 @@ cdef public cpp_bool RequestHandler_OnBeforePluginLoad( Debug("WARNING: RequestHandler_OnBeforePluginLoad() failed," " Browser object is not available") return False + + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(browser): + return False + py_browser = GetPyBrowser(browser, "OnBeforePluginLoad") py_plugin_info = CreatePyWebPluginInfo(plugin_info) clientCallback = GetGlobalClientCallback("OnBeforePluginLoad") @@ -424,6 +472,11 @@ cdef public void RequestHandler_OnRendererProcessTerminated( cdef PyBrowser pyBrowser cdef object clientCallback try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return + pyBrowser = GetPyBrowser(cefBrowser, "OnRendererProcessTerminated") clientCallback = pyBrowser.GetClientCallback( "OnRendererProcessTerminated") @@ -439,13 +492,18 @@ cdef public void RequestHandler_OnPluginCrashed( const CefString& cefPluginPath ) except * with gil: # TODO: plugin may crash during browser creation. Let this callback - # to be set either through cefpython.SetGlobalClientCallback() + # to be set either through cefpython.SetGlobalClientCallback() # or PyBrowser.SetClientCallback(). Modify the # PyBrowser.GetClientCallback() implementation to return a global # callback first if set. cdef PyBrowser pyBrowser cdef object clientCallback try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cefBrowser): + return + pyBrowser = GetPyBrowser(cefBrowser, "OnPluginCrashed") clientCallback = pyBrowser.GetClientCallback("OnPluginCrashed") if clientCallback: @@ -455,3 +513,75 @@ cdef public void RequestHandler_OnPluginCrashed( except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) + + +cdef public cpp_bool RequestHandler_CanGetCookies( + CefRefPtr[CefBrowser] cef_browser, + CefRefPtr[CefFrame] cef_frame, + CefRefPtr[CefRequest] cef_request + ) except * with gil: + cdef PyBrowser browser + cdef PyFrame frame + cdef PyRequest request + cdef object callback + cdef py_bool retval + try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cef_browser): + return False + + browser = GetPyBrowser(cef_browser, "CanGetCookies") + frame = GetPyFrame(cef_frame) + request = CreatePyRequest(cef_request) + callback = browser.GetClientCallback("CanGetCookies") + if callback: + retval = callback( + browser=browser, + frame=frame, + request=request) + return bool(retval) + else: + # Return True by default + return True + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) + + +cdef public cpp_bool RequestHandler_CanSetCookie( + CefRefPtr[CefBrowser] cef_browser, + CefRefPtr[CefFrame] cef_frame, + CefRefPtr[CefRequest] cef_request, + const CefCookie& cef_cookie + ) except * with gil: + cdef PyBrowser browser + cdef PyFrame frame + cdef PyRequest request + cdef PyCookie cookie + cdef object callback + cdef py_bool retval + try: + # Issue #455: CefRequestHandler callbacks still executed after + # browser was closed. + if IsBrowserClosed(cef_browser): + return False + + browser = GetPyBrowser(cef_browser, "CanSetCookie") + frame = GetPyFrame(cef_frame) + request = CreatePyRequest(cef_request) + cookie = CreatePyCookie(cef_cookie) + callback = browser.GetClientCallback("CanSetCookie") + if callback: + retval = callback( + browser=browser, + frame=frame, + request=request, + cookie=cookie) + return bool(retval) + else: + # Return True by default + return True + except: + (exc_type, exc_value, exc_trace) = sys.exc_info() + sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/handlers/v8context_handler.pyx b/src/handlers/v8context_handler.pyx index 704f6ad92..fccdad562 100644 --- a/src/handlers/v8context_handler.pyx +++ b/src/handlers/v8context_handler.pyx @@ -10,6 +10,7 @@ include "../cefpython.pyx" include "../browser.pyx" +include "../frame.pyx" cdef public void V8ContextHandler_OnContextCreated( CefRefPtr[CefBrowser] cefBrowser, @@ -23,7 +24,7 @@ cdef public void V8ContextHandler_OnContextCreated( pyBrowser = GetPyBrowser(cefBrowser, "OnContextCreated") pyBrowser.SetUserData("__v8ContextCreated", True) pyFrame = GetPyFrame(cefFrame) - # User defined callback. + # User defined callback clientCallback = pyBrowser.GetClientCallback("OnContextCreated") if clientCallback: clientCallback(browser=pyBrowser, frame=pyFrame) @@ -52,19 +53,15 @@ cdef public void V8ContextHandler_OnContextReleased( if not pyBrowser: Debug("OnContextReleased: Browser doesn't exist anymore, id={id}" .format(id=str(browserId))) + RemovePyFrame(browserId, frameId) return pyFrame = GetPyFrameById(browserId, frameId) - if pyBrowser and pyFrame: + # Frame may already be destroyed while IPC messaging was executing + # (Issue #431). + if pyFrame: clientCallback = pyBrowser.GetClientCallback("OnContextReleased") if clientCallback: clientCallback(browser=pyBrowser, frame=pyFrame) - else: - if not pyBrowser: - Debug("V8ContextHandler_OnContextReleased() WARNING: " - "PyBrowser not found") - if not pyFrame: - Debug("V8ContextHandler_OnContextReleased() WARNING: " - "PyFrame not found") RemovePyFrame(browserId, frameId) except: (exc_type, exc_value, exc_trace) = sys.exc_info() diff --git a/src/handlers/v8function_handler.pyx b/src/handlers/v8function_handler.pyx index 2c9b3f1cf..ee73619f8 100644 --- a/src/handlers/v8function_handler.pyx +++ b/src/handlers/v8function_handler.pyx @@ -4,46 +4,48 @@ include "../cefpython.pyx" include "../browser.pyx" +include "../frame.pyx" cdef public void V8FunctionHandler_Execute( CefRefPtr[CefBrowser] cefBrowser, - CefRefPtr[CefFrame] cefFrame, - CefString& cefFunctionName, - CefRefPtr[CefListValue] cefFunctionArguments + int64 frameId, + CefString& cefFuncName, + CefRefPtr[CefListValue] cefFuncArgs ) except * with gil: cdef PyBrowser pyBrowser - cdef PyFrame pyFrame - cdef py_string functionName - cdef object function - cdef list functionArguments + cdef CefRefPtr[CefFrame] cefFrame + cdef PyFrame pyFrame # may be None + cdef py_string funcName + cdef object func + cdef list funcArgs cdef object returnValue - cdef py_string jsErrorMessage + cdef py_string errorMessage try: pyBrowser = GetPyBrowser(cefBrowser, "V8FunctionHandler_Execute") - pyFrame = GetPyFrame(cefFrame) - functionName = CefToPyString(cefFunctionName) - Debug("V8FunctionHandler_Execute(): functionName=%s" % functionName) + cefFrame = cefBrowser.get().GetFrame(frameId) + if cefFrame.get(): + pyFrame = GetPyFrame(cefFrame) + else: + pyFrame = None + funcName = CefToPyString(cefFuncName) + Debug("V8FunctionHandler_Execute(): funcName=%s" % funcName) jsBindings = pyBrowser.GetJavascriptBindings() - function = jsBindings.GetFunctionOrMethod(functionName) - if not function: + func = jsBindings.GetFunctionOrMethod(funcName) + if not func: # The Renderer process already checks whether function # name is valid before calling V8FunctionHandler_Execute(), # but it is possible for the javascript bindings to change # during execution, so it's possible for the Browser/Renderer # bindings to be out of sync due to delay in process messaging. - jsErrorMessage = "V8FunctionHandler_Execute() FAILED: " \ - "python function not found: %s" % functionName - Debug(jsErrorMessage) - # Raise a javascript exception in that frame. - pyFrame.ExecuteJavascript("throw '%s';" % jsErrorMessage) + errorMessage = "V8FunctionHandler_Execute() FAILED: " \ + "python function not found: %s" % funcName + NonCriticalError(errorMessage) + # Raise a javascript exception in that frame if it still exists + if pyFrame: + pyFrame.ExecuteJavascript("throw '%s';" % errorMessage) return - functionArguments = CefListValueToPyList(cefBrowser, - cefFunctionArguments) - returnValue = function(*functionArguments) - if returnValue is not None: - Debug("V8FunctionHandler_Execute() WARNING: function returned" \ - "value, but returning values to javascript is not " \ - "supported, functionName=%s" % functionName) + funcArgs = CefListValueToPyList(cefBrowser, cefFuncArgs) + func(*funcArgs) except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/javascript_bindings.pyx b/src/javascript_bindings.pyx index 9043639e3..31620a6dc 100644 --- a/src/javascript_bindings.pyx +++ b/src/javascript_bindings.pyx @@ -140,6 +140,8 @@ cdef class JavascriptBindings: return True elif valueType == int: return True + elif valueType == long: + return True elif valueType == type(None): return True elif IsFunctionOrMethod(valueType): diff --git a/src/linux/binaries_64bit/kivy_.py b/src/linux/binaries_64bit/kivy_.py index bdfb6d2c4..a9a1590b0 100644 --- a/src/linux/binaries_64bit/kivy_.py +++ b/src/linux/binaries_64bit/kivy_.py @@ -1,18 +1,25 @@ -# An example of embedding CEF browser in the Kivy framework. -# The browser is embedded using off-screen rendering mode. - -# Tested using Kivy 1.7.2 stable, only on Linux. +# An example of embedding CEF browser with the Kivy framework +# by using off-screen rendering mode. # In this example kivy-lang is used to declare the layout which # contains two buttons (back, forward) and the browser view. from cefpython3 import cefpython as cef -import pygtk -import gtk import sys import os import time +if sys.platform == 'linux': + import pygtk + import gtk + pygtk.require('2.0') +elif sys.platform == 'darwin': + import gi + gi.require_version("Gtk", "3.0") + from gi.repository import Gtk +elif sys.platform == 'win32': + # no gtk needed on Windows + pass from kivy.app import App from kivy.uix.button import Button @@ -28,9 +35,6 @@ # Global variables g_switches = None -# PyGTK required -pygtk.require('2.0') - class BrowserLayout(BoxLayout): @@ -38,7 +42,7 @@ def __init__(self, **kwargs): super(BrowserLayout, self).__init__(**kwargs) self.orientation = "vertical" - self.browser_widget = CefBrowser(id="browser") + self.browser_widget = CefBrowser() layout = BoxLayout() layout.size_hint_y = None @@ -150,9 +154,6 @@ def start_cef(self): # Configure CEF settings = { - # This directories must be set on Linux - "locales_dir_path": cef.GetModuleDirectory()+"/locales", - "resources_dir_path": cef.GetModuleDirectory(), "browser_subprocess_path": "%s/%s" % ( cef.GetModuleDirectory(), "subprocess"), "windowless_rendering_enabled": True, @@ -161,7 +162,15 @@ def start_cef(self): "enabled": False, }, "external_message_pump": False, # See Issue #246 + "multi_threaded_message_loop": False, } + if sys.platform == 'linux': + # This directories must be set on Linux + settings["locales_dir_path"] = cef.GetModuleDirectory() + "/locales" + settings["resources_dir_path"] = cef.GetModuleDirectory() + if sys.platform == 'darwin': + settings["external_message_pump"] = True # Temporary fix for Issue #246 + switches = { # Tweaking OSR performance by setting the same Chromium flags # as in upstream cefclient (# Issue #240). @@ -190,18 +199,21 @@ def start_cef(self): # Start idle - CEF message loop work. Clock.schedule_once(self._message_loop_work, 0) + windowInfo = cef.WindowInfo() + # TODO: For printing to work in off-screen-rendering mode # it is enough to call gtk_init(). It is not required # to provide window handle when calling SetAsOffscreen(). # However it still needs to be tested whether providing # window handle is required for mouse context menu and # popup widgets to work. - gtkwin = gtk.Window() - gtkwin.realize() - # WindowInfo offscreen flag - windowInfo = cef.WindowInfo() - windowInfo.SetAsOffscreen(gtkwin.window.xid) + if sys.platform == 'linux': + gtkwin = gtk.Window() + gtkwin.realize() + windowInfo.SetAsOffscreen(gtkwin.window.xid) + elif sys.platform == 'darwin' or sys.platform == 'win32': + windowInfo.SetAsOffscreen(0) # Create Broswer and naviagte to empty page <= OnPaint won't get # called yet @@ -519,12 +531,12 @@ def get_windows_key_code(self, kivycode): def go_forward(self, *_): """Going to forward in browser history.""" - print "go forward" + print("go forward") self.browser.GoForward() def go_back(self, *_): """Going back in browser history.""" - print "go back" + print("go back") self.browser.GoBack() def reload(self, *_): @@ -864,7 +876,7 @@ def OnLoadingStateChange(self, is_loading, **_): def OnPaint(self, element_type, paint_buffer, **_): # print "OnPaint()" if element_type != cef.PET_VIEW: - print "Popups aren't implemented yet" + print("Popups aren't implemented yet") return # FPS meter ("fps" arg) diff --git a/src/paint_buffer.pyx b/src/paint_buffer.pyx index 8e7be4300..d99f19d02 100644 --- a/src/paint_buffer.pyx +++ b/src/paint_buffer.pyx @@ -14,9 +14,9 @@ cdef PaintBuffer CreatePaintBuffer(const void* buffer_, int width, int height): cdef class PaintBuffer: cdef const void* buffer - cdef int width - cdef int height - cdef Py_ssize_t length + cdef public int width + cdef public int height + cdef public Py_ssize_t length cpdef uintptr_t GetPointer(self) except *: # BEFORE MODIFYING CODE: diff --git a/src/process_message_utils.pyx b/src/process_message_utils.pyx index a9c10f225..e2d55dabd 100644 --- a/src/process_message_utils.pyx +++ b/src/process_message_utils.pyx @@ -10,6 +10,7 @@ # enumerate(), to an int. include "cefpython.pyx" +include "utils.pyx" # ----------------------------------------------------------------------------- # CEF values to Python values @@ -36,6 +37,53 @@ cdef object CheckForCefPythonMessageHash(CefRefPtr[CefBrowser] cefBrowser, return jsCallback return pyString +cdef object CefValueToPyValue(CefRefPtr[CefValue] cefValue): + assert cefValue.get().IsValid(), "cefValue is invalid" + cdef cef_types.cef_value_type_t valueType = cefValue.get().GetType() + cdef CefRefPtr[CefBinaryValue] binaryValue + cdef uint32 uint32_value = 0 + cdef int64 int64_value = 0 + + if valueType == cef_types.VTYPE_NULL: + return None + elif valueType == cef_types.VTYPE_BOOL: + return bool(cefValue.get().GetBool()) + elif valueType == cef_types.VTYPE_INT: + return cefValue.get().GetInt() + elif valueType == cef_types.VTYPE_DOUBLE: + return cefValue.get().GetDouble() + elif valueType == cef_types.VTYPE_STRING: + return CefToPyString(cefValue.get().GetString()) + elif valueType == cef_types.VTYPE_DICTIONARY: + return CefDictionaryValueToPyDict( + NULL, + cefValue.get().GetDictionary(), + 1) + elif valueType == cef_types.VTYPE_LIST: + return CefListValueToPyList( + NULL, + cefValue.get().GetList(), + 1) + elif valueType == cef_types.VTYPE_BINARY: + binaryValue = cefValue.get().GetBinary() + if binaryValue.get().GetSize() == sizeof(uint32_value): + binaryValue.get().GetData( + &uint32_value, sizeof(uint32_value), 0) + return uint32_value + elif binaryValue.get().GetSize() == sizeof(int64_value): + binaryValue.get().GetData( + &int64_value, sizeof(int64_value), 0) + return int64_value + else: + NonCriticalError("Unknown binary value, size=%s" % \ + binaryValue.get().GetSize()) + return None + else: + raise Exception("Unknown CefValue type=%s" % valueType) + +# TODO: Use CefListValue.GetValue to get CefValue and use CefValueToPyValue +# for dictionary and lists? + cdef list CefListValueToPyList( CefRefPtr[CefBrowser] cefBrowser, CefRefPtr[CefListValue] cefListValue, @@ -44,13 +92,13 @@ cdef list CefListValueToPyList( if nestingLevel > 8: raise Exception("CefListValueToPyList(): max nesting level (8)" " exceeded") - cdef int index - cdef int size = int(cefListValue.get().GetSize()) + cdef size_t index + cdef size_t size = cefListValue.get().GetSize() cdef cef_types.cef_value_type_t valueType cdef list ret = [] cdef CefRefPtr[CefBinaryValue] binaryValue - cdef uint32 uint32_value - cdef int64 int64_value + cdef uint32 uint32_value = 0 + cdef int64 int64_value = 0 cdef object originallyString for index in range(0, size): valueType = cefListValue.get().GetType(index) @@ -65,8 +113,9 @@ cdef list CefListValueToPyList( elif valueType == cef_types.VTYPE_STRING: originallyString = CefToPyString( cefListValue.get().GetString(index)) - originallyString = CheckForCefPythonMessageHash(cefBrowser, - originallyString) + if cefBrowser.get(): + originallyString = CheckForCefPythonMessageHash(cefBrowser, + originallyString) ret.append(originallyString) elif valueType == cef_types.VTYPE_DICTIONARY: ret.append(CefDictionaryValueToPyDict( @@ -89,10 +138,11 @@ cdef list CefListValueToPyList( &int64_value, sizeof(int64_value), 0) ret.append(int64_value) else: - raise Exception("Unknown binary value, size=%s" % \ - binaryValue.get().GetSize()) + NonCriticalError("Unknown binary value, size=%s" % \ + binaryValue.get().GetSize()) + ret.append(None) else: - raise Exception("Unknown value type=%s" % valueType) + raise Exception("Unknown CefValue type=%s" % valueType) return ret cdef dict CefDictionaryValueToPyDict( @@ -107,16 +157,19 @@ cdef dict CefDictionaryValueToPyDict( cefDictionaryValue.get().GetKeys(keyList) cdef cef_types.cef_value_type_t valueType cdef dict ret = {} + # noinspection PyUnresolvedReferences cdef cpp_vector[CefString].iterator iterator = keyList.begin() cdef CefString cefKey cdef py_string pyKey cdef CefRefPtr[CefBinaryValue] binaryValue - cdef uint32 uint32_value - cdef int64 int64_value + cdef uint32 uint32_value = 0 + cdef int64 int64_value = 0 cdef object originallyString while iterator != keyList.end(): + # noinspection PyUnresolvedReferences cefKey = deref(iterator) pyKey = CefToPyString(cefKey) + # noinspection PyUnresolvedReferences preinc(iterator) valueType = cefDictionaryValue.get().GetType(cefKey) if valueType == cef_types.VTYPE_NULL: @@ -130,8 +183,9 @@ cdef dict CefDictionaryValueToPyDict( elif valueType == cef_types.VTYPE_STRING: originallyString = CefToPyString( cefDictionaryValue.get().GetString(cefKey)) - originallyString = CheckForCefPythonMessageHash(cefBrowser, - originallyString) + if cefBrowser.get(): + originallyString = CheckForCefPythonMessageHash(cefBrowser, + originallyString) ret[pyKey] = originallyString elif valueType == cef_types.VTYPE_DICTIONARY: ret[pyKey] = CefDictionaryValueToPyDict( @@ -154,10 +208,11 @@ cdef dict CefDictionaryValueToPyDict( &int64_value, sizeof(int64_value), 0) ret[pyKey] = int64_value else: - raise Exception("Unknown binary value, size=%s" % \ - binaryValue.get().GetSize()) + NonCriticalError("Unknown binary value, size=%s" % \ + binaryValue.get().GetSize()) + ret[pyKey] = None else: - raise Exception("Unknown value type = %s" % valueType) + raise Exception("Unknown CefValue type = %s" % valueType) return ret # ----------------------------------------------------------------------------- @@ -175,7 +230,7 @@ cdef CefRefPtr[CefListValue] PyListToCefListValue( cdef type valueType cdef CefRefPtr[CefListValue] ret = CefListValue_Create() cdef CefRefPtr[CefBinaryValue] binaryValue - cdef int index + cdef size_t index for index_size_t, value in enumerate(pyList): index = int(index_size_t) valueType = type(value) @@ -183,14 +238,9 @@ cdef CefRefPtr[CefListValue] PyListToCefListValue( ret.get().SetNull(index) elif valueType == bool: ret.get().SetBool(index, bool(value)) - elif valueType == int: - ret.get().SetInt(index, int(value)) - elif valueType == long: - # Int32 range is -2147483648..2147483647, we've increased the - # minimum size by one as Cython was throwing a warning: - # "unary minus operator applied to unsigned type, result still - # unsigned". - if -2147483647 <= value <= 2147483647: + elif valueType == int or valueType == long: # In Py3 int and long types are the same type. + # Int32 range is -2147483648..2147483647 + if INT_MIN <= value <= INT_MAX: ret.get().SetInt(index, int(value)) else: # Long values become strings. @@ -234,7 +284,7 @@ cdef void PyListToExistingCefListValue( " exceeded") cdef type valueType cdef CefRefPtr[CefListValue] newCefListValue - cdef int index + cdef size_t index for index_size_t, value in enumerate(pyList): index = int(index_size_t) valueType = type(value) @@ -242,14 +292,9 @@ cdef void PyListToExistingCefListValue( cefListValue.get().SetNull(index) elif valueType == bool: cefListValue.get().SetBool(index, bool(value)) - elif valueType == int: - cefListValue.get().SetInt(index, int(value)) - elif valueType == long: - # Int32 range is -2147483648..2147483647, we've increased the - # minimum size by one as Cython was throwing a warning: - # "unary minus operator applied to unsigned type, result still - # unsigned". - if -2147483647 <= value <= 2147483647: + elif valueType == int or valueType == long: # In Py3 int and long types are the same type. + # Int32 range is -2147483648..2147483647 + if INT_MIN <= value <= INT_MAX: cefListValue.get().SetInt(index, int(value)) else: # Long values become strings. @@ -302,14 +347,9 @@ cdef CefRefPtr[CefDictionaryValue] PyDictToCefDictionaryValue( ret.get().SetNull(cefKey) elif valueType == bool: ret.get().SetBool(cefKey, bool(value)) - elif valueType == int: - ret.get().SetInt(cefKey, int(value)) - elif valueType == long: - # Int32 range is -2147483648..2147483647, we've increased the - # minimum size by one as Cython was throwing a warning: - # "unary minus operator applied to unsigned type, result still - # unsigned". - if -2147483647 <= value <= 2147483647: + elif valueType == int or valueType == long: # In Py3 int and long types are the same type. + # Int32 range is -2147483648..2147483647 + if INT_MIN <= value <= INT_MAX: ret.get().SetInt(cefKey, int(value)) else: # Long values become strings. diff --git a/src/python_callback.pyx b/src/python_callback.pyx index 705e0f54c..c76f7ce39 100644 --- a/src/python_callback.pyx +++ b/src/python_callback.pyx @@ -5,10 +5,12 @@ include "cefpython.pyx" cdef int g_pythonCallbackMaxId = 0 +# [callbackId] = (browserId, frameId, func) cdef dict g_pythonCallbacks = {} # TODO: send callbackId using CefBinaryNamedValue, see: -# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10881 +# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10881 + cdef struct PythonCallback: int callbackId char uniqueCefBinaryValueSize[16] @@ -16,7 +18,7 @@ cdef struct PythonCallback: cdef CefRefPtr[CefBinaryValue] PutPythonCallback( object browserId, object frameId, - object function + object func ) except *: global g_pythonCallbacks global g_pythonCallbackMaxId @@ -29,12 +31,11 @@ cdef CefRefPtr[CefBinaryValue] PutPythonCallback( pyCallback.callbackId = g_pythonCallbackMaxId cdef CefRefPtr[CefBinaryValue] binaryValue = CefBinaryValue_Create( &pyCallback, sizeof(pyCallback)) - # [0] browserId, [1] frameId, [2] function. - g_pythonCallbacks[g_pythonCallbackMaxId] = (browserId, frameId, function) + g_pythonCallbacks[g_pythonCallbackMaxId] = (browserId, frameId, func) return binaryValue cdef public void RemovePythonCallbacksForFrame( - int frameId + object frameId ) except * with gil: # Cannot remove elements from g_pythonCallbacks (dict) while iterating. cdef list toRemove = [] @@ -46,8 +47,8 @@ cdef public void RemovePythonCallbacksForFrame( for callbackId in toRemove: del g_pythonCallbacks[callbackId] Debug("RemovePythonCallbacksForFrame(): " \ - "removed python callback, callbackId = %s" \ - % callbackId) + "removed python callback, callbackId = %s" \ + % callbackId) except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) @@ -62,33 +63,28 @@ cdef void RemovePythonCallbacksForBrowser( for callbackId in toRemove: del g_pythonCallbacks[callbackId] Debug("RemovePythonCallbacksForBrowser(): " \ - "removed python callback, callbackId = %s" \ - % callbackId) + "removed python callback, callbackId = %s" \ + % callbackId) cdef public cpp_bool ExecutePythonCallback( CefRefPtr[CefBrowser] cefBrowser, int callbackId, - CefRefPtr[CefListValue] cefFunctionArguments, + CefRefPtr[CefListValue] cefFuncArgs, ) except * with gil: - cdef object function - cdef list functionArguments + cdef object func + cdef list funcArgs cdef object returnValue try: global g_pythonCallbacks if callbackId in g_pythonCallbacks: - # [0] browserId, [1] frameId, [2] function. - function = g_pythonCallbacks[callbackId][2] - functionArguments = CefListValueToPyList( - cefBrowser, cefFunctionArguments) - returnValue = function(*functionArguments) - if returnValue is not None: - Debug("ExecutePythonCallback() WARNING: function returned" \ - "value, but returning values to javascript is not " \ - "supported, function name = %s" % function.__name__) + func = g_pythonCallbacks[callbackId][2] + funcArgs = CefListValueToPyList( + cefBrowser, cefFuncArgs) + func(*funcArgs) return True else: Debug("ExecutePythonCallback() FAILED: callback not found, " \ - "callbackId = %s" % callbackId) + "callbackId = %s" % callbackId) return False except: (exc_type, exc_value, exc_trace) = sys.exc_info() diff --git a/src/request.pyx b/src/request.pyx index 0f770b204..c0d2793c0 100644 --- a/src/request.pyx +++ b/src/request.pyx @@ -99,10 +99,10 @@ cdef class PyRequest: # pyData is really of type "str", but Cython will throw # an error if we use that type: "Cannot convert 'bytes' # object to str implicitly. This is not portable to Py3." - cdef object pyData + cdef bytes pyData cdef size_t bytesCount cdef void* voidData - cdef str pyFile + cdef bytes pyFile while iterator != elementVector.end(): postDataElement = deref(iterator) if postDataElement.get().GetType() == cef_types.PDE_TYPE_EMPTY: @@ -112,18 +112,26 @@ cdef class PyRequest: bytesCount = postDataElement.get().GetBytesCount() voidData = malloc(bytesCount) postDataElement.get().GetBytes(bytesCount, voidData) - pyData = VoidPtrToString(voidData, bytesCount) + pyData = VoidPtrToBytes(voidData, bytesCount) free(voidData) - if pyData.startswith('--') or retMultipart: + if pyData.startswith(b'--') or retMultipart: # Content-Type: multipart/form-data retMultipart.append(pyData) else: # Content-Type: application/x-www-form-urlencoded - retUrlEncoded.update(urlparse.parse_qsl(qs=pyData, + quoted = urlparse_quote(pyData, safe="=") + retUrlEncoded.update(urlparse.parse_qsl(qs=quoted, keep_blank_values=True)) + if PY_MAJOR_VERSION >= 3: + retUrlEncoded_copy = copy.deepcopy(retUrlEncoded) + retUrlEncoded = dict() + for key in retUrlEncoded_copy: + retUrlEncoded[key.encode("utf-8", "replace")] =\ + retUrlEncoded_copy[key].encode( + "utf-8", "replace") elif postDataElement.get().GetType() == cef_types.PDE_TYPE_FILE: - pyFile = CefToPyString(postDataElement.get().GetFile()) - retMultipart.append("@"+pyFile) + pyFile = CefToPyBytes(postDataElement.get().GetFile()) + retMultipart.append(b"@"+pyFile) else: raise Exception("Invalid type of CefPostDataElement") preinc(iterator) @@ -135,15 +143,15 @@ cdef class PyRequest: cpdef py_void SetPostData(self, object pyPostData): cdef CefRefPtr[CefPostData] postData = CefPostData_Create() cdef CefRefPtr[CefPostDataElement] postDataElement - cdef py_string pyElement + cdef bytes pyElement cdef CefString sfile if type(pyPostData) == list: for pyElement in pyPostData: - if pyElement.startswith('--'): + if pyElement.startswith(b'--'): postDataElement = CefPostDataElement_Create() postDataElement.get().SetToBytes(len(pyElement), pyElement) - elif pyElement.startswith('@'): + elif pyElement.startswith(b'@'): postDataElement = CefPostDataElement_Create() PyToCefString(pyElement[1:], sfile) postDataElement.get().SetToFile(sfile) @@ -156,8 +164,7 @@ cdef class PyRequest: postData.get().AddElement(postDataElement) self.GetCefRequest().get().SetPostData(postData) elif type(pyPostData) == dict: - pyElement = urllib.urlencode(pyPostData) - pyElement = str(pyElement) + pyElement = urllib_urlencode(pyPostData).encode("utf-8", "replace") postDataElement = CefPostDataElement_Create() postDataElement.get().SetToBytes(len(pyElement), pyElement) postData.get().AddElement(postDataElement) diff --git a/src/settings.pyx b/src/settings.pyx index 082044916..d7052eae4 100644 --- a/src/settings.pyx +++ b/src/settings.pyx @@ -139,7 +139,10 @@ cdef void SetBrowserSettings( cdef CefString* cefString for key in browserSettings: - if key == "accept_language_list": + if key == "inherit_client_handlers_for_popups": + # CEF Python only options. These are not to be found in CEF. + continue + elif key == "accept_language_list": cefString = new CefString(&cefBrowserSettings.accept_language_list) PyToCefStringPointer(browserSettings[key], cefString) del cefString diff --git a/src/string_utils.pyx b/src/string_utils.pyx index 3129b1a38..7b1c9a06f 100644 --- a/src/string_utils.pyx +++ b/src/string_utils.pyx @@ -94,16 +94,26 @@ cdef py_string CefToPyString( g_applicationSettings["string_encoding"], errors=BYTES_DECODE_ERRORS)) +cdef bytes CefToPyBytes( + ConstCefString& cefString): + return cefString.ToString() + cdef void PyToCefString( py_string pyString, CefString& cefString ) except *: if PY_MAJOR_VERSION < 3: + # Handle objects that may be converted to string e.g. QString + if not isinstance(pyString, str) and not isinstance(pyString, unicode): + pyString = str(pyString) if type(pyString) == unicode: pyString = (pyString.encode( g_applicationSettings["string_encoding"], errors=UNICODE_ENCODE_ERRORS)) else: + # Handle objects that may be converted to string e.g. QString + if not isinstance(pyString, str) and not isinstance(pyString, bytes): + pyString = str(pyString) # The unicode type is not defined in Python 3. if type(pyString) == str: pyString = (pyString.encode( @@ -148,3 +158,7 @@ cdef py_string VoidPtrToString(const void* data, size_t dataLength): return (((data)[:dataLength]).decode( g_applicationSettings["string_encoding"], errors=BYTES_DECODE_ERRORS)) + +cdef bytes VoidPtrToBytes(const void* data, size_t dataLength): + return ((data)[:dataLength]) + diff --git a/src/subprocess/Makefile b/src/subprocess/Makefile index f52b72df7..adc34fca4 100644 --- a/src/subprocess/Makefile +++ b/src/subprocess/Makefile @@ -11,6 +11,7 @@ INC = -I./../ -I./../common/ -I$(PYTHON_INCLUDE) \ -I/usr/include/glib-2.0 \ -I/usr/include/cairo \ -I/usr/include/pango-1.0 \ + -I/usr/include/harfbuzz \ -I/usr/include/gdk-pixbuf-2.0 \ -I/usr/include/atk-1.0 \ -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include \ diff --git a/src/subprocess/Makefile-libcefpythonapp b/src/subprocess/Makefile-libcefpythonapp index 57f3dccea..e8a6b852b 100644 --- a/src/subprocess/Makefile-libcefpythonapp +++ b/src/subprocess/Makefile-libcefpythonapp @@ -15,7 +15,7 @@ ifeq ($(UNAME_S), Linux) SRC_MORE = print_handler_gtk.cpp ../client_handler/x11.cpp \ main_message_loop/main_message_loop_external_pump_linux.cpp else ifeq ($(UNAME_S), Darwin) - SRC_MORE = main_message_loop/main_message_loop_external_pump_mac.mm + SRC_MORE = main_message_loop/main_message_loop_external_pump_mac.mm endif SRC = cefpython_app.cpp v8function_handler.cpp v8utils.cpp \ @@ -36,6 +36,7 @@ INC = -I./../ -I./../common/ -I$(PYTHON_INCLUDE) \ -I/usr/include/glib-2.0 \ -I/usr/include/cairo \ -I/usr/include/pango-1.0 \ + -I/usr/include/harfbuzz \ -I/usr/include/gdk-pixbuf-2.0 \ -I/usr/include/atk-1.0 \ -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include \ diff --git a/src/subprocess/cefpython_app.cpp b/src/subprocess/cefpython_app.cpp index a02a7cd51..786a919d5 100644 --- a/src/subprocess/cefpython_app.cpp +++ b/src/subprocess/cefpython_app.cpp @@ -337,15 +337,10 @@ void CefPythonApp::OnContextReleased(CefRefPtr browser, // ------------------------------------------------------------------------ // 2. Remove python callbacks for a frame. // ------------------------------------------------------------------------ - // If this is the main frame then the message won't arrive - // to the browser process, as browser is being destroyed, - // but it doesn't matter because in LifespanHandler_BeforeClose() + // This is already done via RemovePyFrame called from + // V8ContextHandler_OnContextReleased. + // If this is the main frame then in LifespanHandler_BeforeClose() // we're calling RemovePythonCallbacksForBrowser(). - message = CefProcessMessage::Create("RemovePythonCallbacksForFrame"); - arguments = message->GetArgumentList(); - // TODO: int64 precision lost - arguments->SetInt(0, (int)(frame->GetIdentifier())); - browser->SendProcessMessage(PID_BROWSER, message); // ------------------------------------------------------------------------ // 3. Clear javascript callbacks. // ------------------------------------------------------------------------ diff --git a/src/subprocess/main_message_loop/main_message_loop_external_pump.cpp b/src/subprocess/main_message_loop/main_message_loop_external_pump.cpp index abd81a84a..18b142898 100644 --- a/src/subprocess/main_message_loop/main_message_loop_external_pump.cpp +++ b/src/subprocess/main_message_loop/main_message_loop_external_pump.cpp @@ -22,7 +22,7 @@ const int32 kTimerDelayPlaceholder = INT_MAX; // DoWork(). const int64 kMaxTimerDelay = 1000 / 30; // 30fps -MainMessageLoopExternalPump* g_external_message_pump = NULL; +::MainMessageLoopExternalPump* g_external_message_pump = NULL; } // namespace @@ -43,6 +43,8 @@ MainMessageLoopExternalPump* MainMessageLoopExternalPump::Get() { void MainMessageLoopExternalPump::OnScheduleWork(int64 delay_ms) { REQUIRE_MAIN_THREAD(); + // LOG(INFO) << "MainMessageLoopExternalPump::OnScheduleWork"; + // LOG(INFO) << delay_ms << " ms"; if (delay_ms == kTimerDelayPlaceholder && IsTimerPending()) { // Don't set the maximum timer requested from DoWork() if a timer event is diff --git a/src/subprocess/main_message_loop/main_message_loop_external_pump_mac.mm b/src/subprocess/main_message_loop/main_message_loop_external_pump_mac.mm index f77c4d27c..c446b59b4 100644 --- a/src/subprocess/main_message_loop/main_message_loop_external_pump_mac.mm +++ b/src/subprocess/main_message_loop/main_message_loop_external_pump_mac.mm @@ -92,6 +92,7 @@ - (void)timerTimeout:(id)obj { } int MainMessageLoopExternalPumpMac::Run() { + LOG(INFO) << "MainMessageLoopExternalPumpMac::Run"; // Run the message loop. [NSApp run]; diff --git a/src/subprocess/main_message_loop/util_win.cpp b/src/subprocess/main_message_loop/util_win.cpp index bc1f0965c..834850e32 100644 --- a/src/subprocess/main_message_loop/util_win.cpp +++ b/src/subprocess/main_message_loop/util_win.cpp @@ -136,8 +136,8 @@ int GetCefKeyboardModifiers(WPARAM wparam, LPARAM lparam) { return modifiers; } -bool IsKeyDown(WPARAM wparam) { - return (GetKeyState(wparam) & 0x8000) != 0; +bool IsKeyDown(int keycode) { + return (GetKeyState(keycode) & 0x8000) != 0; } float GetDeviceScaleFactor() { diff --git a/src/subprocess/main_message_loop/util_win.h b/src/subprocess/main_message_loop/util_win.h index 39870204b..a716d9380 100644 --- a/src/subprocess/main_message_loop/util_win.h +++ b/src/subprocess/main_message_loop/util_win.h @@ -31,7 +31,7 @@ WNDPROC SetWndProcPtr(HWND hWnd, WNDPROC wndProc); int GetCefMouseModifiers(WPARAM wparam); int GetCefKeyboardModifiers(WPARAM wparam, LPARAM lparam); -bool IsKeyDown(WPARAM wparam); +bool IsKeyDown(int keycode); // Returns the device scale factor. For example, 200% display scaling will // return 2.0. diff --git a/src/utils.pyx b/src/utils.pyx index 03bfd1e3d..7d8553613 100644 --- a/src/utils.pyx +++ b/src/utils.pyx @@ -3,6 +3,7 @@ # Project website: https://github.com/cztomczak/cefpython include "cefpython.pyx" +include "string_utils.pyx" # noinspection PyUnresolvedReferences cimport cef_types @@ -71,56 +72,11 @@ cpdef str GetSystemError(): ELSE: return "" -cpdef str GetNavigateUrl(py_string url): - # Encode local file paths so that CEF can load them correctly: - # | some.html, some/some.html, D:\, /var, file:// - if re.search(r"^file:", url, re.I) or \ - re.search(r"^[a-zA-Z]:", url) or \ - not re.search(r"^[\w-]+:", url): - - # Function pathname2url will complain if url starts with "file://". - # CEF may also change local urls to "file:///C:/" - three slashes. - is_file_protocol = False - file_prefix = "" - file_prefixes = ["file:///", "file://"] - for file_prefix in file_prefixes: - if url.startswith(file_prefix): - is_file_protocol = True - # Remove the file:// prefix - url = url[len(file_prefix):] - break - - # Need to encode chinese characters in local file paths, - # otherwise CEF will try to encode them by itself. But it - # will fail in doing so. CEF will return the following string: - # >> %EF%BF%97%EF%BF%80%EF%BF%83%EF%BF%A6 - # But it should be: - # >> %E6%A1%8C%E9%9D%A2 - url = urllib_pathname2url(url) - - if is_file_protocol: - url = "%s%s" % (file_prefix, url) - - # If it is C:\ then colon was encoded. Decode it back. - url = re.sub(r"^([a-zA-Z])%3A", r"\1:", url) - - # Allow hash when loading urls. The pathname2url function - # replaced hashes with "%23" (Issue #114). - url = url.replace("%23", "#") - - # Allow more special characters when loading urls. The pathname2url - # function encoded them and need to decode them back here - # Characters: ? & = (Issue #273). - url = url.replace("%3F", "?") - url = url.replace("%26", "&") - url = url.replace("%3D", "=") - - return str(url) - cpdef py_bool IsFunctionOrMethod(object valueType): if (valueType == types.FunctionType or valueType == types.MethodType or valueType == types.BuiltinFunctionType - or valueType == types.BuiltinMethodType): + or valueType == types.BuiltinMethodType + or valueType.__name__ == "cython_function_or_method"): return True return False diff --git a/src/version/cef_version_mac.h b/src/version/cef_version_mac.h index 11454273f..50e9b4efd 100644 --- a/src/version/cef_version_mac.h +++ b/src/version/cef_version_mac.h @@ -1,4 +1,4 @@ -// Copyright (c) 2017 Marshall A. Greenblatt. All rights reserved. +// Copyright (c) 2018 Marshall A. Greenblatt. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are @@ -35,16 +35,16 @@ #ifndef CEF_INCLUDE_CEF_VERSION_H_ #define CEF_INCLUDE_CEF_VERSION_H_ -#define CEF_VERSION "3.2987.1601.gf035232" +#define CEF_VERSION "3.3359.1774.gd49d25f" #define CEF_VERSION_MAJOR 3 -#define CEF_COMMIT_NUMBER 1601 -#define CEF_COMMIT_HASH "f035232c082f837d2b85bd7821a93a54fc742775" -#define COPYRIGHT_YEAR 2017 +#define CEF_COMMIT_NUMBER 1774 +#define CEF_COMMIT_HASH "d49d25f881b68f418e243e12801cbbb7caebb563" +#define COPYRIGHT_YEAR 2018 -#define CHROME_VERSION_MAJOR 57 +#define CHROME_VERSION_MAJOR 66 #define CHROME_VERSION_MINOR 0 -#define CHROME_VERSION_BUILD 2987 -#define CHROME_VERSION_PATCH 133 +#define CHROME_VERSION_BUILD 3359 +#define CHROME_VERSION_PATCH 181 #define DO_MAKE_STRING(p) #p #define MAKE_STRING(p) DO_MAKE_STRING(p) @@ -63,13 +63,13 @@ extern "C" { // universal hash value will change if any platform is affected whereas the // platform hash values will change only if that particular platform is // affected. -#define CEF_API_HASH_UNIVERSAL "b0a24e3e202f3d8b72f2fbc1ebc5864f96ec16ae" +#define CEF_API_HASH_UNIVERSAL "84263345b0b1143139aba560e5e5ed16fb6a9628" #if defined(OS_WIN) -#define CEF_API_HASH_PLATFORM "1c6a27f840ac87c8c971350c907edbe2c5fa0387" +#define CEF_API_HASH_PLATFORM "cf092ef692a2ff18b0fc732b58bde9b8b8655fcc" #elif defined(OS_MACOSX) -#define CEF_API_HASH_PLATFORM "1567db600ee83cc2a59bb8c17ca416d11a7c9b8a" +#define CEF_API_HASH_PLATFORM "34f636bde2f02cb43c10061f384af4fc51c26e1f" #elif defined(OS_LINUX) -#define CEF_API_HASH_PLATFORM "1f9f9e15bf7cf13de2557ddd411dfc9f694503b0" +#define CEF_API_HASH_PLATFORM "6b57a640612f8d459042917ad2568b1526a70af4" #endif // Returns CEF version information for the libcef library. The |entry| diff --git a/src/web_request.pyx b/src/web_request.pyx index 9891dade8..8e51fca12 100644 --- a/src/web_request.pyx +++ b/src/web_request.pyx @@ -176,7 +176,7 @@ cdef public void WebRequestClient_OnDownloadData( if userCallback: userCallback( web_request=webRequest, - data=VoidPtrToString(data, dataLength)) + data=VoidPtrToBytes(data, dataLength)) except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) diff --git a/src/window_utils_linux.pyx b/src/window_utils_linux.pyx index 63dda9c86..51691a428 100644 --- a/src/window_utils_linux.pyx +++ b/src/window_utils_linux.pyx @@ -8,46 +8,47 @@ class WindowUtils: # You have to overwrite this class and provide implementations # for these methods. - @staticmethod - def OnSetFocus(WindowHandle windowHandle, long msg, long wparam, + @classmethod + def OnSetFocus(cls, WindowHandle windowHandle, long msg, long wparam, long lparam): # Available only on Windows, but have it available on other # platforms so that PyCharm doesn't warn about unresolved reference. pass - @staticmethod - def OnSize(WindowHandle windowHandle, long msg, long wparam, long lparam): + @classmethod + def OnSize(cls, WindowHandle windowHandle, long msg, long wparam, + long lparam): # Available only on Windows, but have it available on other # platforms so that PyCharm doesn't warn about unresolved reference. pass - @staticmethod - def OnEraseBackground(WindowHandle windowHandle, long msg, long wparam, - long lparam): + @classmethod + def OnEraseBackground(cls, WindowHandle windowHandle, long msg, + long wparam, long lparam): # Available only on Windows, but have it available on other # platforms so that PyCharm doesn't warn about unresolved reference. pass - @staticmethod - def GetParentHandle(WindowHandle windowHandle): + @classmethod + def GetParentHandle(cls, WindowHandle windowHandle): Debug("WindowUtils::GetParentHandle() not implemented (returns 0)") return 0 - @staticmethod - def IsWindowHandle(WindowHandle windowHandle): + @classmethod + def IsWindowHandle(cls, WindowHandle windowHandle): Debug("WindowUtils::IsWindowHandle() not implemented (always True)") return True - @staticmethod - def gtk_plug_new(WindowHandle gdkNativeWindow): + @classmethod + def gtk_plug_new(cls, WindowHandle gdkNativeWindow): return gtk_plug_new(gdkNativeWindow) - @staticmethod - def gtk_widget_show(WindowHandle gtkWidgetPtr): + @classmethod + def gtk_widget_show(cls, WindowHandle gtkWidgetPtr): with nogil: gtk_widget_show(gtkWidgetPtr) - @staticmethod - def InstallX11ErrorHandlers(): + @classmethod + def InstallX11ErrorHandlers(cls): with nogil: x11.InstallX11ErrorHandlers() diff --git a/src/window_utils_mac.pyx b/src/window_utils_mac.pyx index d683daf81..31a90095a 100644 --- a/src/window_utils_mac.pyx +++ b/src/window_utils_mac.pyx @@ -8,36 +8,37 @@ class WindowUtils: # You have to overwrite this class and provide implementations # for these methods. - @staticmethod - def OnSetFocus(WindowHandle windowHandle, long msg, long wparam, + @classmethod + def OnSetFocus(cls, WindowHandle windowHandle, long msg, long wparam, long lparam): # Available only on Windows, but have it available on other # platforms so that PyCharm doesn't warn about unresolved reference. pass - @staticmethod - def OnSize(WindowHandle windowHandle, long msg, long wparam, long lparam): + @classmethod + def OnSize(cls, WindowHandle windowHandle, long msg, long wparam, + long lparam): # Available only on Windows, but have it available on other # platforms so that PyCharm doesn't warn about unresolved reference. pass - @staticmethod - def OnEraseBackground(WindowHandle windowHandle, long msg, long wparam, - long lparam): + @classmethod + def OnEraseBackground(cls, WindowHandle windowHandle, long msg, + long wparam, long lparam): # Available only on Windows, but have it available on other # platforms so that PyCharm doesn't warn about unresolved reference. pass - @staticmethod - def GetParentHandle(WindowHandle windowHandle): + @classmethod + def GetParentHandle(cls, WindowHandle windowHandle): Debug("WindowUtils::GetParentHandle() not implemented (returns 0)") return 0 - @staticmethod - def IsWindowHandle(WindowHandle windowHandle): + @classmethod + def IsWindowHandle(cls, WindowHandle windowHandle): Debug("WindowUtils::IsWindowHandle() not implemented (always True)") return True - @staticmethod - def InstallX11ErrorHandlers(): + @classmethod + def InstallX11ErrorHandlers(cls): pass diff --git a/src/window_utils_win.pyx b/src/window_utils_win.pyx index 0dd9ee70b..2bd3eb776 100644 --- a/src/window_utils_win.pyx +++ b/src/window_utils_win.pyx @@ -6,16 +6,18 @@ include "cefpython.pyx" class WindowUtils(object): - @staticmethod - def OnSetFocus(WindowHandle windowHandle, long msg, long wparam, long lparam): + @classmethod + def OnSetFocus(cls, WindowHandle windowHandle, long msg, long wparam, + long lparam): cdef PyBrowser pyBrowser = GetBrowserByWindowHandle(windowHandle) if not pyBrowser: return 0 pyBrowser.SetFocus(True) return 0 - @staticmethod - def OnSize(WindowHandle windowHandle, long msg, long wparam, long lparam): + @classmethod + def OnSize(cls, WindowHandle windowHandle, long msg, long wparam, + long lparam): cdef PyBrowser pyBrowser = GetBrowserByWindowHandle(windowHandle) if not pyBrowser: return DefWindowProc(windowHandle, msg, wparam, lparam) @@ -34,9 +36,9 @@ class WindowUtils(object): return DefWindowProc(windowHandle, msg, wparam, lparam) - @staticmethod - def OnEraseBackground(WindowHandle windowHandle, long msg, long wparam, - long lparam): + @classmethod + def OnEraseBackground(cls, WindowHandle windowHandle, long msg, + long wparam, long lparam): cdef PyBrowser pyBrowser = GetBrowserByWindowHandle(windowHandle) if not pyBrowser: return DefWindowProc(windowHandle, msg, wparam, lparam) @@ -48,8 +50,8 @@ class WindowUtils(object): return DefWindowProc(windowHandle, msg, wparam, lparam) - @staticmethod - def SetTitle(PyBrowser pyBrowser, str pyTitle): + @classmethod + def SetTitle(cls, PyBrowser pyBrowser, str pyTitle): # Each browser window should have a title (Issue 3). # When popup is created, the window that sits in taskbar # has no title. @@ -86,8 +88,8 @@ class WindowUtils(object): # is displayed currently. SetWindowTextW(windowHandle, cefTitle.ToWString().c_str()) - @staticmethod - def SetIcon(PyBrowser pyBrowser, py_string icon="inherit"): + @classmethod + def SetIcon(cls, PyBrowser pyBrowser, py_string icon="inherit"): # `icon` parameter is not implemented. # Popup window inherits icon from the main window. @@ -135,17 +137,17 @@ class WindowUtils(object): SendMessage(windowHandle, WM_SETICON, ICON_SMALL, parentIconSmall) - @staticmethod - def GetParentHandle(WindowHandle windowHandle): + @classmethod + def GetParentHandle(cls, WindowHandle windowHandle): return GetParent(windowHandle) - @staticmethod - def IsWindowHandle(WindowHandle windowHandle): + @classmethod + def IsWindowHandle(cls, WindowHandle windowHandle): IF UNAME_SYSNAME == "Windows": return bool(IsWindow(windowHandle)) ELSE: return False - @staticmethod - def InstallX11ErrorHandlers(): + @classmethod + def InstallX11ErrorHandlers(cls): pass diff --git a/tools/apidocs.py b/tools/apidocs.py index 8bd8da318..230b28f18 100644 --- a/tools/apidocs.py +++ b/tools/apidocs.py @@ -17,10 +17,22 @@ import glob import os import re +import subprocess def main(): """Main entry point.""" + # Call toc.py for docs/, api/, examples/ and examples/snippets/ + # directories. + toc_dirs = [API_DIR, DOCS_DIR, EXAMPLES_DIR, SNIPPETS_DIR] + for toc_dir in toc_dirs: + print("Running toc.py in {}/ dir".format(os.path.basename(toc_dir))) + retcode = subprocess.call([sys.executable, + os.path.join(TOOLS_DIR, "toc.py"), + toc_dir]) + assert retcode == 0, "Executing toc.py failed" + + # Generate API reference in api/ dir and in root/README.md api_links = generate_api_links() update_api_index_file(api_links) update_readme_file(api_links) @@ -69,8 +81,8 @@ def update_readme_file(api_links): assert re.search(re_find, readme_contents), ("API categories not found" " in README") contents = re.sub(re_find, - ("### API categories\r\n\r\n{categories_contents}" - "\r\n### API index" + (u"### API categories\r\n\r\n{categories_contents}" + u"\r\n### API index" .format(categories_contents=categories_contents)), contents) @@ -80,7 +92,7 @@ def update_readme_file(api_links): assert re.search(re_find, readme_contents), ("API index not found" " in README") contents = re.sub(re_find, - ("### API index\r\n\r\n{api_links}" + (u"### API index\r\n\r\n{api_links}" .format(api_links=api_links)), contents) @@ -103,7 +115,7 @@ def generate_api_links(): continue with open(file_, "rb") as fo: md_contents = fo.read().decode("utf-8") - md_contents = re.sub(r"```[\s\S]+?```", "", md_contents) + md_contents = re.sub(u"```[\\s\\S]+?```", u"", md_contents) matches = re.findall(r"^(#|###)\s+(.*)", md_contents, re.MULTILINE) for match in matches: diff --git a/tools/automate.py b/tools/automate.py index a618cee51..2ba882785 100644 --- a/tools/automate.py +++ b/tools/automate.py @@ -29,11 +29,14 @@ [--fast-build FAST_BUILD] [--force-chromium-update FORCE_CHROMIUM_UPDATE] [--no-cef-update NO_CEF_UPDATE] - [--cef-branch BRANCH] [--cef-commit COMMIT] + [--cef-git-url URL] [--cef-branch BRANCH] [--cef-commit COMMIT] [--build-dir BUILD_DIR] [--cef-build-dir CEF_BUILD_DIR] [--ninja-jobs JOBS] [--gyp-generators GENERATORS] [--gyp-msvs-version MSVS] [--use-system-freetype USE_SYSTEM_FREETYPE] + [--use-gtk3 USE_GTK3] + [--use-ccache USE_CCACHE] + [--proprietary-codecs PROPRIETARY_CODECS] [--no-depot-tools-update NO_DEPOT_TOOLS_UPDATE] automate.py (-h | --help) [type -h to show full description for options] @@ -51,6 +54,7 @@ --force-chromium-update Force Chromium update (gclient sync etc). --no-cef-update Do not update CEF sources (by default both cef/ directories are deleted on every run). + --cef-git-url= Git URL to clone CEF from, defaults to upstream --cef-branch= CEF branch. Defaults to CHROME_VERSION_BUILD from "src/version/cef_version_{platform}.h". --cef-commit= CEF revision. Defaults to CEF_COMMIT_HASH from @@ -58,12 +62,18 @@ --build-dir= Build directory. --cef-build-dir= CEF build directory. By default same as --build-dir. - --ninja-jobs= How many CEF jobs to run in parallel. To speed up - building set it to number of cores in your CPU. - By default set to cpu_count / 2. + --ninja-jobs= How many CEF jobs to run in parallel. By default + sets to CPU threads * 2. If you need to perform + other tasks on computer and it is slowed down + by the build then decrease the number of ninja + jobs. --gyp-generators= Set GYP_GENERATORS [default: ninja]. --gyp-msvs-version= Set GYP_MSVS_VERSION. --use-system-freetype Use system Freetype library on Linux (Issue #402) + --use-gtk3 Link CEF with GTK 3 libraries (Issue #446) + --use-ccache Use ccache for faster (re)builds + --proprietary-codecs Enable proprietary codecs such as H264 and AAC, + licensing restrictions may apply. --no-depot-tools-update Do not update depot_tools/ directory. When building old unsupported versions of Chromium you want to manually checkout an old version @@ -86,7 +96,7 @@ from setuptools.msvc import msvc9_query_vcvarsall # Constants -CEF_GIT_URL = "https://bitbucket.org/chromiumembedded/cef.git" +CEF_UPSTREAM_GIT_URL = "https://bitbucket.org/chromiumembedded/cef.git" RUNTIME_MT = "MT" RUNTIME_MD = "MD" @@ -102,6 +112,7 @@ class Options(object): fast_build = False force_chromium_update = False no_cef_update = False + cef_git_url = "" cef_branch = "" cef_commit = "" cef_version = "" @@ -111,6 +122,9 @@ class Options(object): gyp_generators = "ninja" # Even though CEF uses now GN, still some GYP gyp_msvs_version = "" # env variables are being used. use_system_freetype = False + use_gtk3 = False + use_ccache = False + proprietary_codecs = False no_depot_tools_update = False # Internal options @@ -158,6 +172,9 @@ def setup_options(docopt_args): Options.tools_dir = os.path.dirname(os.path.realpath(__file__)) Options.cefpython_dir = os.path.dirname(Options.tools_dir) + if not Options.cef_git_url: + Options.cef_git_url = CEF_UPSTREAM_GIT_URL + # If --cef-branch is specified will use latest CEF commit from that # branch. Otherwise get cef branch/commit from src/version/. if not Options.cef_branch: @@ -210,12 +227,11 @@ def setup_options(docopt_args): # ninja_jobs # cpu_count() returns number of CPU threads, not CPU cores. - # On i5 with 2 cores and 4 cpu threads the default of 4 ninja - # jobs slows down computer significantly. + # On i5 with 2 cores there are 4 cpu threads and will enable + # 8 ninja jobs by default. if not Options.ninja_jobs: - Options.ninja_jobs = int(multiprocessing.cpu_count() / 2) - if Options.ninja_jobs < 1: - Options.ninja_jobs = 1 + Options.ninja_jobs = int(multiprocessing.cpu_count() * 2) + assert Options.ninja_jobs > 0 Options.ninja_jobs = str(Options.ninja_jobs) @@ -296,7 +312,7 @@ def create_cef_directories(): # Clone cef repo and checkout branch if os.path.exists(cef_dir): rmdir(cef_dir) - run_git("clone -b %s %s cef" % (Options.cef_branch, CEF_GIT_URL), + run_git("clone -b %s %s cef" % (Options.cef_branch, Options.cef_git_url), Options.cef_build_dir) if Options.cef_commit: run_git("checkout %s" % Options.cef_commit, cef_dir) @@ -574,15 +590,15 @@ def fix_cmake_variables_for_MD_library(undo=False, try_undo=False): # This replacements must be unique for the undo operation # to be reliable. - mt_find = r'CEF_RUNTIME_LIBRARY_FLAG "/MT"' - mt_replace = r'CEF_RUNTIME_LIBRARY_FLAG "/MD"' + mt_find = u'CEF_RUNTIME_LIBRARY_FLAG "/MT"' + mt_replace = u'CEF_RUNTIME_LIBRARY_FLAG "/MD"' - wd4275_find = ('/wd4244 ' - '# Ignore "conversion possible loss of data" warning') - wd4275_replace = ('/wd4244 ' - '# Ignore "conversion possible loss of data" warning' - '\r\n' - ' /wd4275 # Ignore "non dll-interface class"') + wd4275_find = (u'/wd4244 ' + u'# Ignore "conversion possible loss of data" warning') + wd4275_replace = (u'/wd4244 ' + u'# Ignore "conversion possible loss of data" warning' + u'\r\n' + u' /wd4275 # Ignore "non dll-interface class"') cmake_variables = os.path.join(Options.cef_binary, "cmake", "cef_variables.cmake") @@ -744,7 +760,9 @@ def create_prebuilt_binaries(): "build_cefclient", "tests", "cefclient", Options.build_type, "cefclient" + APP_EXT) - if LINUX and os.path.exists(cefclient): + if not MAC: + assert os.path.exists(cefclient) + if LINUX: # On Windows resources/*.html files are embedded inside exe cefclient_files = os.path.join( src, @@ -759,6 +777,8 @@ def create_prebuilt_binaries(): "build_cefclient", "tests", "cefsimple", Options.build_type, "cefsimple" + APP_EXT) + if not MAC: + assert os.path.exists(cefsimple) # ceftests ceftests = os.path.join( @@ -766,7 +786,9 @@ def create_prebuilt_binaries(): "build_cefclient", "tests", "ceftests", Options.build_type, "ceftests" + APP_EXT) - if LINUX and os.path.exists(ceftests): + if not MAC: + assert os.path.exists(ceftests) + if LINUX: # On Windows resources/*.html files are embedded inside exe ceftests_files = os.path.join( src, @@ -776,8 +798,7 @@ def create_prebuilt_binaries(): cpdir(ceftests_files, os.path.join(bindir, "ceftests_files")) def copy_app(app): - if os.path.exists(app): - if os.path.isdir(app): + if MAC: # On Mac app is a directory shutil.copytree(app, os.path.join(bindir, @@ -786,7 +807,8 @@ def copy_app(app): shutil.copy(app, bindir) if not MAC: - # Currently do not copy apps on Mac + # Currently do not copy apps on Mac, as they take lots of + # additional space (cefsimple is 157 MB). copy_app(cefclient) copy_app(cefsimple) copy_app(ceftests) @@ -891,14 +913,26 @@ def getenv(): env["CEF_USE_GN"] = "1" # Issue #73 patch applied here with "use_allocator=none" env["GN_DEFINES"] = "use_sysroot=true use_allocator=none symbol_level=1" - # env["GN_DEFINES"] += " use_gtk3=false" + + # Link with GTK 3 (Issue #446) + if Options.use_gtk3: + env["GN_DEFINES"] += " use_gtk3=true" + + # Use ccache for faster (re)builds + if Options.use_ccache: + env["GN_DEFINES"] += " cc_wrapper=ccache" + + # Enable proprietary codecs + if Options.proprietary_codecs: + env["GN_DEFINES"] += " proprietary_codecs=true ffmpeg_branding=Chrome" + # To perform an official build set GYP_DEFINES=buildtype=Official. # This will disable debugging code and enable additional link-time # optimizations in Release builds. if Options.release_build and not Options.fast_build: env["GN_DEFINES"] += " is_official_build=true" - # Isssue #402 - Blurry font rendering on Linux + # Blurry font rendering on Linux (Isssue #402) if Options.use_system_freetype: env["GN_DEFINES"] += " use_system_freetype=true" @@ -983,13 +1017,10 @@ def run_automate_git(): # later in cef_binary/ with cmake/ninja do works fine. args.append("--build-target=cefsimple") - # On Windows automate-git.py must be run using Python 2.7 - # from depot_tools. depot_tools should already be added to PATH. - python = "python" # *do not* replace with sys.executable! args = " ".join(args) command = script + " " + args working_dir = Options.cef_build_dir - return run_command("%s %s" % (python, command), working_dir) + return run_command("%s %s" % (sys.executable, command), working_dir) def run_make_distrib(): diff --git a/tools/build.py b/tools/build.py index 1ae47b737..f3960283e 100644 --- a/tools/build.py +++ b/tools/build.py @@ -18,16 +18,21 @@ from sources or use ready binaries from Spotify Automated Builds. Usage: - build.py VERSION [--rebuild-cpp] [--fast] [--clean] [--kivy] - [--hello-world] + build.py VERSION [--rebuild-cpp] [--unittests] [--fast] [--clean] [--kivy] + [--hello-world] [--enable-profiling] + [--enable-line-tracing] Options: - VERSION Version number eg. 50.0 - --no-run-examples Do not run examples after build, only unit tests - --fast Fast mode - --clean Clean C++ projects build files on Linux/Mac - --kivy Run only Kivy example - --hello-world Run only Hello World example + VERSION Version number eg. 50.0 + --unittests Run only unit tests. Do not run examples while + building cefpython modules. Examples require + interaction such as closing window before proceeding. + --fast Fast mode + --clean Clean C++ projects build files on Linux/Mac + --kivy Run only Kivy example + --hello-world Run only Hello World example + --enable-profiling Enable cProfile profiling + --enable-line-tracing Enable cProfile line tracing """ # --rebuild-cpp Force rebuild of .vcproj C++ projects (DISABLED) @@ -58,6 +63,7 @@ # 6. More commands: http://docs.cython.org/src/userguide/debugging.html from common import * +import copy import sys import os import glob @@ -74,14 +80,17 @@ pass # Command line args variables +SYS_ARGV_ORIGINAL = None VERSION = "" -NO_RUN_EXAMPLES = False +UNITTESTS = False DEBUG_FLAG = False FAST_FLAG = False CLEAN_FLAG = False KIVY_FLAG = False HELLO_WORLD_FLAG = False REBUILD_CPP = False +ENABLE_PROFILING = False +ENABLE_LINE_TRACING = False # First run FIRST_RUN = False @@ -119,51 +128,78 @@ def main(): def command_line_args(): global DEBUG_FLAG, FAST_FLAG, CLEAN_FLAG, KIVY_FLAG, HELLO_WORLD_FLAG, \ - REBUILD_CPP, VERSION, NO_RUN_EXAMPLES + REBUILD_CPP, VERSION, UNITTESTS VERSION = get_version_from_command_line_args(__file__) + # Other scripts called by this script expect that version number + # is available in sys.argv, so don't remove it like it's done + # for all other args starting with "--". if not VERSION: print(__doc__) sys.exit(1) print("[build.py] Parse command line arguments") - # --no-run-examples - if "--no-run-examples" in sys.argv: - NO_RUN_EXAMPLES = True - print("[build.py] Running examples disabled (--no-run-examples)") + global SYS_ARGV_ORIGINAL + SYS_ARGV_ORIGINAL = copy.copy(sys.argv) + + if "--unittests" in sys.argv: + UNITTESTS = True + print("[build.py] Running examples disabled (--unittests)") + sys.argv.remove("--unittests") - # -- debug if "--debug" in sys.argv: DEBUG_FLAG = True print("[build.py] DEBUG mode On") + sys.argv.remove("--debug") - # --fast if "--fast" in sys.argv: # Fast mode doesn't delete C++ .o .a files. # Fast mode also disables optimization flags in setup/setup.py . FAST_FLAG = True print("[build.py] FAST mode On") + sys.argv.remove("--fast") - # --clean if "--clean" in sys.argv: CLEAN_FLAG = True + sys.argv.remove("--clean") - # --kivy if "--kivy" in sys.argv: KIVY_FLAG = True print("[build.py] KIVY example") + sys.argv.remove("--kivy") - # --kivy if "--hello-world" in sys.argv: HELLO_WORLD_FLAG = True print("[build.py] HELLO WORLD example") + sys.argv.remove("--hello-world") - # --rebuild-cpp # Rebuild c++ projects if "--rebuild-cpp" in sys.argv: REBUILD_CPP = True print("[build.py] REBUILD_CPP mode enabled") + sys.argv.remove("--rebuild-cpp") + + global ENABLE_PROFILING + if "--enable-profiling" in sys.argv: + print("[build.py] cProfile profiling enabled") + ENABLE_PROFILING = True + sys.argv.remove("--enable-profiling") + + global ENABLE_LINE_TRACING + if "--enable-line-tracing" in sys.argv: + print("[build.py] cProfile line tracing enabled") + ENABLE_LINE_TRACING = True + sys.argv.remove("--enable-line-tracing") + + for arg in sys.argv: + if arg.startswith("--"): + print("ERROR: invalid arg {0}".format(arg)) + sys.exit(1) + + if len(sys.argv) <= 1: + print(__doc__) + sys.exit(1) print("[build.py] VERSION=%s" % VERSION) @@ -205,7 +241,7 @@ def check_directories(): prebuilt_name = get_cef_binaries_libraries_basename(OS_POSTFIX2) print("[build.py] ERROR: Couldn't find CEF prebuilt binaries and" " libraries: 'build/{prebuilt_dir}/'. Download it" - " from GitHub released tagged eg. 'v50-upstream` or download" + " from GitHub released tagged eg. 'vXX-upstream` or download" " CEF binaries from Spotify Automated Builds and then run" "`automate.py --prebuilt-cef`." .format(prebuilt_dir=prebuilt_name)) @@ -257,7 +293,7 @@ def setup_environ(): print("[build.py] PYTHON_INCLUDE: {python_include}" .format(python_include=os.environ["PYTHON_INCLUDE"])) - os.environ["CEF_CCFLAGS"] = "-std=gnu++11 -DNDEBUG -Wall -Werror" + os.environ["CEF_CCFLAGS"] = "-std=gnu++11 -DNDEBUG -Wall -Werror -Wno-deprecated-declarations" if FAST_FLAG: os.environ["CEF_CCFLAGS"] += " -O0" else: @@ -441,6 +477,12 @@ def build_vcproj_DEPRECATED(vcproj): # msbuild /p:BuildProjectReferences=false project.proj # MSBuild.exe MyProject.proj /t:build + VS2008_BUILD = ("%LocalAppData%\\Programs\\Common\\" + "Microsoft\\Visual C++ for Python\\9.0\\" + "VC\\bin\\amd64\\vcbuild.exe") + VS2008_BUILD = VS2008_BUILD.replace("%LocalAppData%", + os.environ["LOCALAPPDATA"]) + if PYVERSION == "27": args = list() args.append(VS2008_VCVARS) @@ -618,8 +660,8 @@ def copy_and_fix_pyx_files(): shutil.copy("../../src/%s" % mainfile_original, "./%s" % mainfile_newname) with open("./%s" % mainfile_newname, "rb") as fo: content = fo.read().decode("utf-8") - (content, subs) = re.subn(r"^include \"handlers/", - "include \"", + (content, subs) = re.subn(u"^include \"handlers/", + u"include \"", content, flags=re.MULTILINE) # Add __version__ variable in cefpython.pyx @@ -647,8 +689,8 @@ def copy_and_fix_pyx_files(): # Do not remove the newline - so that line numbers # are exact with originals. (content, subs) = re.subn( - r"^include[\t ]+[\"'][^\"'\n\r]+[\"'][\t ]*", - "", + u"^include[\\t ]+[\"'][^\"'\\n\\r]+[\"'][\\t ]*", + u"", content, flags=re.MULTILINE) if subs: @@ -726,8 +768,18 @@ def build_cefpython_module(): os.chdir(BUILD_CEFPYTHON) + enable_profiling = "" + if ENABLE_PROFILING: + enable_profiling = "--enable-profiling" + enable_line_tracing = "" + if ENABLE_LINE_TRACING: + enable_line_tracing = "--enable-line-tracing" + command = ("\"{python}\" {tools_dir}/cython_setup.py build_ext" - .format(python=sys.executable, tools_dir=TOOLS_DIR)) + " {enable_profiling} {enable_line_tracing}" + .format(python=sys.executable, tools_dir=TOOLS_DIR, + enable_profiling=enable_profiling, + enable_line_tracing=enable_line_tracing)) if FAST_FLAG: command += " --fast" ret = subprocess.call(command, shell=True) @@ -757,9 +809,10 @@ def build_cefpython_module(): args = list() args.append("\"{python}\"".format(python=sys.executable)) args.append(os.path.join(TOOLS_DIR, os.path.basename(__file__))) - assert __file__ in sys.argv[0] - args.extend(sys.argv[1:]) + assert os.path.basename(__file__) in sys.argv[0] + args.extend(SYS_ARGV_ORIGINAL[1:]) command = " ".join(args) + print("[build.py] Running command: %s" % command) ret = subprocess.call(command, shell=True) # Always pass fixed value to sys.exit, read note at # the top of the script about os.system and sys.exit @@ -863,7 +916,7 @@ def install_and_run(): sys.exit(1) # Run examples - if not NO_RUN_EXAMPLES: + if not UNITTESTS: print("[build.py] Run examples") os.chdir(EXAMPLES_DIR) flags = "" diff --git a/tools/build_distrib.py b/tools/build_distrib.py index 3c2abef8c..051914f57 100644 --- a/tools/build_distrib.py +++ b/tools/build_distrib.py @@ -7,19 +7,23 @@ python versions. Usage: - build_distrib.py VERSION [--no-run-examples] [--no-rebuild] + build_distrib.py VERSION [--unittests] [--no-rebuild] [--no-automate] + [--allow-partial] Options: VERSION Version number eg. 50.0 - --no-run-examples Do not run examples while building cefpython modules. - Examples require interaction, closing window before - proceeding. Only unit tests will be run in such case. + --unittests Run only unit tests. Do not run examples while building + cefpython modules. Examples require interaction such as + closing window before proceeding. --no-rebuild Do not rebuild cefpython modules. For internal use so that changes to packaging can be quickly tested. --no-automate Do not run automate.py --prebuilt-cef. This flag allows to use CEF prebuilt binaries and libraries downloaded from CEF Python's Github releases to build distribution pacakges. + --allow-partial Do not require all supported Python versions to + be installed. If some are missing they just won't + be included in distribution. This script does the following: @@ -70,18 +74,20 @@ # Command line args VERSION = "" -NO_RUN_EXAMPLES = False +UNITTESTS = False NO_REBUILD = False NO_AUTOMATE = False +ALLOW_PARTIAL = False # Python versions -SUPPORTED_PYTHON_VERSIONS = [(2, 7), (3, 4), (3, 5), (3, 6)] +SUPPORTED_PYTHON_VERSIONS = [(2, 7), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9)] # Python search paths. It will use first Python found for specific version. # Supports replacement of one environment variable in path eg.: %ENV_KEY%. PYTHON_SEARCH_PATHS = dict( WINDOWS=[ - "C:\\Python*\\", + "C:\\Python??*\\", + "C:\\Pythons\\Python*\\", "%LOCALAPPDATA%\\Programs\\Python\\Python*\\", "C:\\Program Files\\Python*\\", "C:\\Program Files (x86)\\Python*\\", @@ -127,42 +133,45 @@ def main(): run_automate_prebuilt_cef(pythons_32bit[0]) pack_prebuilt_cef("32bit") if LINUX: - reduce_package_size_issue_262("32bit") + reduce_package_size_issue262("32bit") remove_unnecessary_package_files("32bit") if pythons_64bit: if not NO_AUTOMATE: run_automate_prebuilt_cef(pythons_64bit[0]) pack_prebuilt_cef("64bit") if LINUX: - reduce_package_size_issue_262("64bit") + reduce_package_size_issue262("64bit") remove_unnecessary_package_files("64bit") if not NO_REBUILD: build_cefpython_modules(pythons_32bit, "32bit") build_cefpython_modules(pythons_64bit, "64bit") if pythons_32bit: - make_packages(pythons_32bit[0], "32bit") + make_packages(pythons_32bit[0], "32bit", pythons_32bit) if pythons_64bit: - make_packages(pythons_64bit[0], "64bit") + make_packages(pythons_64bit[0], "64bit", pythons_64bit) test_wheel_packages(pythons_32bit + pythons_64bit) show_summary(pythons_32bit, pythons_64bit) def command_line_args(): - global VERSION, NO_RUN_EXAMPLES, NO_REBUILD, NO_AUTOMATE + global VERSION, UNITTESTS, NO_REBUILD, NO_AUTOMATE, ALLOW_PARTIAL version = get_version_from_command_line_args(__file__) if not version or "--help" in sys.argv: print(__doc__) sys.exit(1) VERSION = version - if "--no-run-examples" in sys.argv: - NO_RUN_EXAMPLES = True - sys.argv.remove("--no-run-examples") + if "--unittests" in sys.argv: + UNITTESTS = True + sys.argv.remove("--unittests") if "--no-rebuild" in sys.argv: NO_REBUILD = True sys.argv.remove("--no-rebuild") if "--no-automate" in sys.argv: NO_AUTOMATE = True sys.argv.remove("--no-automate") + if "--allow-partial" in sys.argv: + ALLOW_PARTIAL = True + sys.argv.remove("--allow-partial") args = sys.argv[1:] for arg in args: if arg == version: @@ -192,6 +201,28 @@ def clean_build_directories(): # Delete cef binaries and libraries dirs if not NO_AUTOMATE: + # Delete cef binlib dir only if cef_binary dir exists, + # otherwise you will end up with cef binlib directory + # deleted and script failing further when calling + # automate.py --prebuilt-cef. + version = get_cefpython_version() + # 32-bit + if not MAC: + postfix2 = get_cef_postfix2_for_arch("32bit") + cef_binary_dir = "cef_binary_{cef_version}_{postfix2}"\ + .format(cef_version=version["CEF_VERSION"], + postfix2=postfix2) + if len(glob.glob(cef_binary_dir)) != 1: + raise Exception("Directory not found: "+cef_binary_dir) + # 64-bit + postfix2 = get_cef_postfix2_for_arch("64bit") + cef_binary_dir = "cef_binary_{cef_version}_windows64"\ + .format(cef_version=version["CEF_VERSION"], + postfix2=postfix2) + if len(glob.glob(cef_binary_dir)) != 1: + raise Exception("Directory not found: "+cef_binary_dir) + + # Delete delete_cef_binaries_libraries_dir("32bit") delete_cef_binaries_libraries_dir("64bit") @@ -246,6 +277,8 @@ def search_for_pythons(search_arch): version_str = subprocess.check_output([python, "-c", version_code]) version_str = version_str.strip() + if sys.version_info >= (3, 0): + version_str = version_str.decode("utf-8") match = re.search("^\((\d+), (\d+), (\d+)\)$", version_str) assert match, version_str major = match.group(1) @@ -257,6 +290,8 @@ def search_for_pythons(search_arch): "print(str(platform.architecture()[0]));") arch = subprocess.check_output([python, "-c", arch_code]) arch = arch.strip() + if sys.version_info >= (3, 0): + arch = arch.decode("utf-8") if version_tuple2 in SUPPORTED_PYTHON_VERSIONS \ and arch == search_arch: name = ("Python {major}.{minor}.{micro} {arch}" @@ -295,7 +330,8 @@ def check_pythons(pythons_32bit, pythons_64bit): if pythons_32bit: print("[build_distrib.py] Pythons 32-bit found:") pp.pprint(pythons_32bit) - if check_32bit and len(pythons_32bit) != len(SUPPORTED_PYTHON_VERSIONS): + if check_32bit and len(pythons_32bit) != len(SUPPORTED_PYTHON_VERSIONS) \ + and not ALLOW_PARTIAL: print("[build_distrib.py] ERROR: Couldn't find all supported" " python 32-bit installations. Found: {found}." .format(found=len(pythons_32bit))) @@ -303,7 +339,8 @@ def check_pythons(pythons_32bit, pythons_64bit): if pythons_64bit: print("[build_distrib.py] Pythons 64-bit found:") pp.pprint(pythons_64bit) - if check_64bit and len(pythons_64bit) != len(SUPPORTED_PYTHON_VERSIONS): + if check_64bit and len(pythons_64bit) != len(SUPPORTED_PYTHON_VERSIONS) \ + and not ALLOW_PARTIAL: print("[build_distrib.py] ERROR: Couldn't find all supported" " python 64-bit installations. Found: {found}." .format(found=len(pythons_64bit))) @@ -316,8 +353,14 @@ def install_upgrade_requirements(pythons): " for: {name}".format(name=python["name"])) # Upgrade pip - command = ("\"{python}\" -m pip install --upgrade pip" - .format(python=python["executable"])) + pip_version = "pip" + # Old Python versions require specific versions of pip, latest versions are broken with these. + if python["version2"] == (2, 7): + pip_version = "pip==20.3.4" + elif python["version2"] == (3, 4): + pip_version = "pip==19.1.1" + command = ("\"{python}\" -m pip install --upgrade {pip_version}" + .format(python=python["executable"], pip_version=pip_version)) command = sudo_command(command, python=python["executable"]) pcode = subprocess.call(command, shell=True) if pcode != 0: @@ -346,7 +389,7 @@ def uninstall_cefpython3_packages(pythons): .format(python=python["executable"])) try: output = subprocess.check_output(command, shell=True) - except subprocess.CalledProcessError, exc: + except subprocess.CalledProcessError as exc: # pip show returns error code when package is not installed output = exc.output if not len(output.strip()): @@ -423,7 +466,7 @@ def zip_directory(path, base_path, archive): os.chdir(original_dir) -def reduce_package_size_issue_262(arch): +def reduce_package_size_issue262(arch): """Linux only: libcef.so is huge (500 MB) in Chrome v54+. Issue #262.""" print("[build_distrib.py] Reduce package size for {arch} (Issue #262)" .format(arch=arch)) @@ -457,8 +500,8 @@ def build_cefpython_modules(pythons, arch): print("[build_distrib.py] Build cefpython module for {python_name}" .format(python_name=python["name"])) flags = "" - if NO_RUN_EXAMPLES: - flags += " --no-run-examples" + if UNITTESTS: + flags += " --unittests" # On Linux/Mac Makefiles are used and must pass --clean flag command = ("\"{python}\" {build_py} {version} --clean {flags}" .format(python=python["executable"], @@ -485,46 +528,44 @@ def build_cefpython_modules(pythons, arch): def backup_subprocess_executable_issue342(python): - """Use subprocess executable build by Python 2.7 to avoid - false-positives by AVs when building subprocess with Python 3. - Windows-only issue.""" + """Use subprocess executable built by Python 3.4 to have the least amount of + false-positives by AVs. Windows-only issue.""" if not WINDOWS: return if python["version2"] == (2, 7): print("[build_distrib.py] Backup subprocess executable built" - " with Python 2.7 (Issue #342)") + " with Python 3.4 (Issue #342)") cefpython_binary_basename = get_cefpython_binary_basename( get_os_postfix2_for_arch(python["arch"])) cefpython_binary = os.path.join(BUILD_DIR, cefpython_binary_basename) assert os.path.isdir(cefpython_binary) src = os.path.join(cefpython_binary, "subprocess.exe") dst = os.path.join(BUILD_CEFPYTHON, - "subprocess_py27_{arch}_issue342.exe" + "subprocess_py34_{arch}_issue342.exe" .format(arch=python["arch"])) shutil.copy(src, dst) def restore_subprocess_executable_issue342(arch): - """Use subprocess executable build by Python 2.7 to avoid - false-positives by AVs when building subprocess with Python 3. - Windows-only issue.""" + """Use subprocess executable built by Python 3.4 to have the least amount of + false-positives by AVs. Windows-only issue.""" if not WINDOWS: return print("[build_distrib.py] Restore subprocess executable built" - " with Python 2.7 (Issue #342)") + " with Python 3.4 (Issue #342)") cefpython_binary_basename = get_cefpython_binary_basename( get_os_postfix2_for_arch(arch)) cefpython_binary = os.path.join(BUILD_DIR, cefpython_binary_basename) assert os.path.isdir(cefpython_binary) src = os.path.join(BUILD_CEFPYTHON, - "subprocess_py27_{arch}_issue342.exe" + "subprocess_py34_{arch}_issue342.exe" .format(arch=arch)) assert os.path.isfile(src) dst = os.path.join(cefpython_binary, "subprocess.exe") shutil.copy(src, dst) -def make_packages(python, arch): +def make_packages(python, arch, all_pythons): """Make setup and wheel packages.""" print("[build_distrib.py] Make setup package for {arch}..." .format(arch=arch)) @@ -547,6 +588,7 @@ def make_packages(python, arch): setup_basename = get_setup_installer_basename( VERSION, get_os_postfix2_for_arch(arch)) setup_dir = os.path.join(BUILD_DIR, setup_basename) + check_cpp_extension_dependencies_issue359(setup_dir, all_pythons) archive = pack_directory(setup_dir, BUILD_DIR) shutil.move(archive, DISTRIB_DIR) @@ -574,6 +616,31 @@ def make_packages(python, arch): shutil.rmtree(setup_dir) +def check_cpp_extension_dependencies_issue359(setup_dir, all_pythons): + """Windows only: check if msvcpXX.dll exist for all Python versions. + Issue #359.""" + if not WINDOWS: + return + checked_any = False + for python in all_pythons: + if python["version2"] in ((3, 5), (3, 6), (3, 7), (3, 8), (3, 9)): + checked_any = True + if not os.path.exists(os.path.join(setup_dir, "cefpython3", + "msvcp140.dll")): + raise Exception("C++ ext dependency missing: msvcp140.dll") + elif python["version2"] == (3, 4): + checked_any = True + if not os.path.exists(os.path.join(setup_dir, "cefpython3", + "msvcp100.dll")): + raise Exception("C++ ext dependency missing: msvcp100.dll") + elif python["version2"] == (2, 7): + if not os.path.exists(os.path.join(setup_dir, "cefpython3", + "msvcp90.dll")): + raise Exception("C++ ext dependency missing: msvcp90.dll") + checked_any = True + assert checked_any + + def test_wheel_packages(pythons): """Test wheel packages installation and run unit tests.""" uninstall_cefpython3_packages(pythons) @@ -602,7 +669,7 @@ def test_wheel_packages(pythons): command = ("\"{python}\" {unittests}" .format(python=python["executable"], unittests=os.path.join(UNITTESTS_DIR, - "main_test.py"))) + "_test_runner.py"))) pcode = subprocess.call(command, cwd=DISTRIB_DIR, shell=True) if pcode != 0: print("[build_distrib.py] ERROR: Unit tests failed for" diff --git a/tools/common.py b/tools/common.py index 4ca34d73e..303ccb856 100644 --- a/tools/common.py +++ b/tools/common.py @@ -4,6 +4,7 @@ # Common stuff for tools such as automate.py, build.py, etc. +import atexit import glob import os import platform @@ -11,11 +12,12 @@ import shutil import struct import sys +import tempfile # These sample apps will be deleted when creating setup/wheel packages CEF_SAMPLE_APPS = ["cefclient", "cefsimple", "ceftests", "chrome-sandbox"] -# Architecture and OS postfixes +# Python architecture and OS postfixes ARCH32 = (8 * struct.calcsize('P') == 32) ARCH64 = (8 * struct.calcsize('P') == 64) # Make sure platform.architecture()[0] shows correctly 32bit when @@ -26,6 +28,10 @@ assert platform.architecture()[0] == "64bit" ARCH_STR = platform.architecture()[0] +# Operating system architecture +SYSTEM64 = platform.machine().endswith('64') +SYSTEM32 = not SYSTEM64 + # OS_POSTFIX is for directories/files names in cefpython sources # and doesn't include architecture type, just OS name. @@ -119,8 +125,9 @@ assert __file__ ROOT_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) -# API reference +# API reference and docs API_DIR = os.path.join(ROOT_DIR, "api") +DOCS_DIR = os.path.join(ROOT_DIR, "docs") # Build directories BUILD_DIR = os.path.join(ROOT_DIR, "build") @@ -151,6 +158,7 @@ # -- end build directories EXAMPLES_DIR = os.path.join(ROOT_DIR, "examples") +SNIPPETS_DIR = os.path.join(EXAMPLES_DIR, "snippets") SRC_DIR = os.path.join(ROOT_DIR, "src") # Subdirectories in src/ @@ -167,6 +175,25 @@ UNITTESTS_DIR = os.path.abspath(os.path.join(ROOT_DIR, "unittests")) # ---------------------------------------------------------------------------- + +# Auto cleanup in the examples/ directory, so that build scripts +# do not include trash directories. See Issue #432. + +shutil.rmtree(os.path.join(EXAMPLES_DIR, "blob_storage"), + ignore_errors=True) +shutil.rmtree(os.path.join(EXAMPLES_DIR, "webrtc_event_logs"), + ignore_errors=True) +shutil.rmtree(os.path.join(EXAMPLES_DIR, "webcache"), + ignore_errors=True) + +shutil.rmtree(os.path.join(SNIPPETS_DIR, "blob_storage"), + ignore_errors=True) +shutil.rmtree(os.path.join(SNIPPETS_DIR, "webrtc_event_logs"), + ignore_errors=True) +shutil.rmtree(os.path.join(SNIPPETS_DIR, "webcache"), + ignore_errors=True) + + # cefpython API header file and a fixed copy of it CEFPYTHON_API_HFILE = os.path.join(BUILD_CEFPYTHON, "cefpython_py{pyver}.h" @@ -185,30 +212,32 @@ SUBPROCESS_EXE = os.path.join(BUILD_SUBPROCESS, "subprocess" + EXECUTABLE_EXT) -# Visual Studio constants +# These Visual Studio constants are used by automate.py tool +# to build upstream C++ projects. CEF Python C++ code is built +# with setuptools/distutils in the build_cpp_projects.py tool. +# ----------------------------------------------------------------------------- + VS_PLATFORM_ARG = "x86" if ARCH32 else "amd64" +# Python 3.5 / 3.6 / 3.7 / 3.8 / 3.9 VS2015_VCVARS = ("C:\\Program Files (x86)\\Microsoft Visual Studio 14.0" "\\VC\\vcvarsall.bat") -# For CEF build +# Required for building old CEF branches < 2704 VS2013_VCVARS = ("C:\\Program Files (x86)\\Microsoft Visual Studio 12.0" "\\VC\\vcvarsall.bat") -# VS2010 vcvarsall not used, using detection with setuptools instead +# Python 3.4 VS2010_VCVARS = ("C:\\Program Files (x86)\\Microsoft Visual Studio 10.0" "\\VC\\vcvarsall.bat") -VS2008_VCVARS = ("%LocalAppData%\\Programs\\Common\\Microsoft" - "\\Visual C++ for Python\\9.0\\vcvarsall.bat") -VS2008_BUILD = ("%LocalAppData%\\Programs\\Common\\" - "Microsoft\\Visual C++ for Python\\9.0\\" - "VC\\bin\\amd64\\vcbuild.exe") -if "LOCALAPPDATA" in os.environ: - VS2008_VCVARS = VS2008_VCVARS.replace("%LocalAppData%", - os.environ["LOCALAPPDATA"]) - VS2008_BUILD = VS2008_BUILD.replace("%LocalAppData%", - os.environ["LOCALAPPDATA"]) +# Python 2.7 +VS2008_VCVARS = ("C:\\Program Files (x86)\\Microsoft Visual Studio 9.0" + "\\VC\\vcvarsall.bat") + +if WINDOWS and not os.path.exists(VS2008_VCVARS): + VS2008_VCVARS = (os.environ["LOCALAPPDATA"]+"\\Programs\\Common\\Microsoft" + "\\Visual C++ for Python\\9.0\\vcvarsall.bat") # ----------------------------------------------------------------------------- @@ -265,8 +294,12 @@ def get_python_include_path(): return ".\\" if WINDOWS else "./" +g_deleted_sample_apps = [] + + def delete_cef_sample_apps(caller_script, bin_dir): """Delete CEF sample apps to reduce package size.""" + atexit.register(restore_cef_sample_apps, caller_script) for sample_app_name in CEF_SAMPLE_APPS: sample_app = os.path.join(bin_dir, sample_app_name + APP_EXT) # Not on all platforms sample apps may be available @@ -274,16 +307,34 @@ def delete_cef_sample_apps(caller_script, bin_dir): print("[{script}] Delete {sample_app}" .format(script=os.path.basename(caller_script), sample_app=os.path.basename(sample_app))) - if os.path.isdir(sample_app): - shutil.rmtree(sample_app) - else: - os.remove(sample_app) + tmpdir = tempfile.mkdtemp() + g_deleted_sample_apps.append((bin_dir, + os.path.basename(sample_app), + tmpdir)) + shutil.move(sample_app, tmpdir) # Also delete subdirs eg. cefclient_files/, ceftests_files/ files_subdir = os.path.join(bin_dir, sample_app_name + "_files") if os.path.isdir(files_subdir): - print("[build_distrib.py] Delete directory: {dir}/" - .format(dir=os.path.basename(files_subdir))) - shutil.rmtree(files_subdir) + print("[{script}] Delete directory: {dir}/" + .format(script=os.path.basename(caller_script), + dir=os.path.basename(files_subdir))) + tmpdir = tempfile.mkdtemp() + g_deleted_sample_apps.append((bin_dir, + os.path.basename(files_subdir), + tmpdir)) + shutil.move(files_subdir, tmpdir) + + +def restore_cef_sample_apps(caller_script): + for deleted in g_deleted_sample_apps: + bin_dir = deleted[0] + tmp = os.path.join(deleted[2], deleted[1]) + print("[{script}] Restore: {path}" + .format(script=os.path.basename(caller_script), + path=os.path.join(bin_dir, deleted[1]))) + shutil.move(tmp, bin_dir) + shutil.rmtree(deleted[2]) + del g_deleted_sample_apps[0:len(g_deleted_sample_apps)] def _detect_cef_binaries_libraries_dir(): @@ -424,6 +475,12 @@ def get_msvs_for_python(vs_prefix=False): return "VS2015" if vs_prefix else "2015" elif sys.version_info[:2] == (3, 6): return "VS2015" if vs_prefix else "2015" + elif sys.version_info[:2] == (3, 7): + return "VS2015" if vs_prefix else "2015" + elif sys.version_info[:2] == (3, 8): + return "VS2015" if vs_prefix else "2015" + elif sys.version_info[:2] == (3, 9): + return "VS2015" if vs_prefix else "2015" else: print("ERROR: This version of Python is not supported") sys.exit(1) diff --git a/tools/cython_setup.py b/tools/cython_setup.py index ea9443e5a..436bcb2ce 100644 --- a/tools/cython_setup.py +++ b/tools/cython_setup.py @@ -25,6 +25,7 @@ import sys import platform import Cython +import copy import os # Must monkey patch Cython's ModuleNode to inject custom C++ code @@ -65,8 +66,31 @@ def generate_extern_c_macro_definition(self, code): generate_extern_c_macro_definition) -# Constants +# Issue #554: Shared libraries in manylinux1 wheel should not +# be linked against libpythonX.Y.so.1.0. +if LINUX: + get_libraries_old = (build_ext.get_libraries) + def get_libraries_new(self, ext): + libraries = get_libraries_old(self, ext) + libpython = ('python' + str(sys.version_info.major) + '.' + + str(sys.version_info.minor)) + for lib in copy.copy(libraries): + # Library name for Python versions before 3.8 may have + # an 'm' at the end. + if lib.startswith(libpython): + print("[cython_setup.py] Do not link against -l%s (Issue #554)" + % lib) + libraries.remove(lib) + return libraries + build_ext.get_libraries = ( + get_libraries_new + ) + + +# Command line args FAST_FLAG = False +ENABLE_PROFILING = False +ENABLE_LINE_TRACING = False # Cython options. Stop on first error, otherwise hundreds # of errors appear in the console. @@ -80,12 +104,24 @@ def main(): print("[cython_setup.py] Cython version: %s" % Cython.__version__) global FAST_FLAG - if len(sys.argv) > 1 and "--fast" in sys.argv: + if "--fast" in sys.argv: # Fast mode disables optimization flags print("[cython_setup.py] FAST mode enabled") FAST_FLAG = True sys.argv.remove("--fast") + global ENABLE_PROFILING + if "--enable-profiling" in sys.argv: + print("[cython_setup.py] cProfile profiling enabled") + ENABLE_PROFILING = True + sys.argv.remove("--enable-profiling") + + global ENABLE_LINE_TRACING + if "--enable-line-tracing" in sys.argv: + print("[cython_setup.py] cProfile line tracing enabled") + ENABLE_LINE_TRACING = True + sys.argv.remove("--enable-line-tracing") + if len(sys.argv) <= 1: print(__doc__) sys.exit(1) @@ -111,11 +147,13 @@ def get_winsdk_lib(): if WINDOWS: if ARCH32: winsdk_libs = [ + # Windows 7 SDKs. r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Lib", r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Lib", ] elif ARCH64: winsdk_libs = [ + # Windows 7 SDKs. r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Lib\\x64", r"C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Lib\\x64", ] @@ -151,9 +189,18 @@ def set_compiler_options(options): # function "public: virtual bool __thiscall # ClientHandler::OnProcessMessageReceived # + # /wd4305 - disable warnnings such as this: + # + # warning C4305: '=' : truncation from 'int' to 'bool' + # Code: > cdef public cpp_bool ExecutePythonCallback(...) except *: + # > return True + # Discussed on cython group error 4800 which is similar to this, + # but there is no other way to fix this warning: + # https://groups.google.com/d/topic/cython-users/X_X0lfIBCqo/discussion + # # The above warning LNK4217 is caused by the warning below which occurs # when building the client_handler.lib static library: - extra_compile_args.extend(["/EHsc"]) + extra_compile_args.extend(["/EHsc", "/wd4305"]) extra_link_args.extend(["/ignore:4217"]) if LINUX or MAC: @@ -269,6 +316,7 @@ def get_include_dirs(): '/usr/include/gtk-unix-print-2.0', '/usr/include/cairo', '/usr/include/pango-1.0', + '/usr/include/harfbuzz', '/usr/include/gdk-pixbuf-2.0', '/usr/include/atk-1.0', # Fedora @@ -288,6 +336,7 @@ def get_include_dirs(): '/usr/include/gtk-unix-print-2.0', '/usr/include/cairo', '/usr/include/pango-1.0', + '/usr/include/harfbuzz', '/usr/include/gdk-pixbuf-2.0', '/usr/include/atk-1.0', # Ubuntu @@ -382,8 +431,11 @@ def get_ext_modules(options): # > Unknown Extension options: 'cython_directives' warnings.warn(msg) cython_directives={ # Any conversion to unicode must be explicit using .decode(). + "language_level": 2, # Yes, Py2 for all python versions. "c_string_type": "bytes", "c_string_encoding": "utf-8", + "profile": ENABLE_PROFILING, + "linetrace": ENABLE_LINE_TRACING, }, language="c++", @@ -417,6 +469,9 @@ def compile_time_constants(): # A way around Python 3.2 bug: UNAME_SYSNAME is not set contents += 'DEF UNAME_SYSNAME = "%s"\n' % platform.uname()[0] contents += 'DEF PY_MAJOR_VERSION = %s\n' % sys.version_info.major + contents += 'cdef extern from "limits.h":\n' + contents += ' cdef int INT_MIN\n' + contents += ' cdef int INT_MAX\n' fo.write(contents.encode("utf-8")) diff --git a/tools/installer/cefpython3.__init__.py b/tools/installer/cefpython3.__init__.py index d7463248a..624a26e7b 100644 --- a/tools/installer/cefpython3.__init__.py +++ b/tools/installer/cefpython3.__init__.py @@ -26,9 +26,13 @@ package_dir = os.path.dirname(os.path.abspath(__file__)) -# This loads the libcef.so library for the subprocess executable. -# On Mac it works without setting library paths. -os.environ["LD_LIBRARY_PATH"] = package_dir +# This loads the libcef.so library for the subprocess executable on Linux. +# TODO: Use -Wl,-rpath=\$$ORIGIN in Makefile. +ld_library_path = os.environ.get("LD_LIBRARY_PATH") +if ld_library_path and ld_library_path.strip(): + os.environ["LD_LIBRARY_PATH"] = package_dir + os.pathsep + ld_library_path +else: + os.environ["LD_LIBRARY_PATH"] = package_dir # This env variable will be returned by cefpython.GetModuleDirectory(). os.environ["CEFPYTHON3_PATH"] = package_dir @@ -44,7 +48,7 @@ libcef = os.path.join(package_dir, "libcef.so") ctypes.CDLL(libcef, ctypes.RTLD_GLOBAL) -# Load the cefpython module for proper Python version +# Load the cefpython module for given Python version if sys.version_info[:2] == (2, 7): # noinspection PyUnresolvedReferences from . import cefpython_py27 as cefpython @@ -57,5 +61,14 @@ elif sys.version_info[:2] == (3, 6): # noinspection PyUnresolvedReferences from . import cefpython_py36 as cefpython +elif sys.version_info[:2] == (3, 7): + # noinspection PyUnresolvedReferences + from . import cefpython_py37 as cefpython +elif sys.version_info[:2] == (3, 8): + # noinspection PyUnresolvedReferences + from . import cefpython_py38 as cefpython +elif sys.version_info[:2] == (3, 9): + # noinspection PyUnresolvedReferences + from . import cefpython_py39 as cefpython else: raise Exception("Python version not supported: " + sys.version) diff --git a/tools/installer/cefpython3.setup.py b/tools/installer/cefpython3.setup.py index 08065ebd5..d14ee2ce5 100644 --- a/tools/installer/cefpython3.setup.py +++ b/tools/installer/cefpython3.setup.py @@ -129,7 +129,7 @@ def main(): "https://github.com/cztomczak/cefpython", license="BSD 3-clause", author="Czarek Tomczak", - author_email="czarek.tomczak@@gmail.com", + author_email="czarek.tomczak@gmail.com", url="https://github.com/cztomczak/cefpython", download_url="https://github.com/cztomczak/cefpython/releases", platforms=["{{SYSCONFIG_PLATFORM}}"], @@ -147,6 +147,9 @@ def main(): "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", "Topic :: Desktop Environment", "Topic :: Internet", "Topic :: Internet :: WWW/HTTP", diff --git a/tools/make_installer.py b/tools/make_installer.py index 4220710f4..012e66575 100644 --- a/tools/make_installer.py +++ b/tools/make_installer.py @@ -102,6 +102,8 @@ def main(): create_empty_log_file(os.path.join(PKG_DIR, "debug.log")) create_empty_log_file(os.path.join(PKG_DIR, "examples/debug.log")) + copy_cpp_extension_dependencies_issue359(PKG_DIR) + print("[make_installer.py] Done. Installer package created: {setup_dir}" .format(setup_dir=SETUP_DIR)) @@ -184,7 +186,7 @@ def replace_template_vars(string, dictionary): string = string.replace("{{"+key+"}}", value) if string == orig_string: raise Exception("Nothing to format") - if re.search(r"\{\{[a-zA-Z0-9_]+\}\}", string): + if re.search(r"{{[a-zA-Z0-9_]+}}", string): raise Exception("Not all strings were formatted") return string @@ -334,6 +336,86 @@ def create_empty_log_file(log_file): subprocess.check_call(command, shell=True) +def copy_cpp_extension_dependencies_issue359(pkg_dir): + """CEF Python module is written in Cython and is a Python C++ + extension and depends on msvcpXX.dll. For Python 3.5 / 3.6 / 3.7 / 3.8 / 3.9 + msvcp140.dll is required. See Issue #359. For Python 2.7 + msvcp90.dll is required. Etc. These dependencies are not included + with Python binaries from Python.org.""" + if not WINDOWS: + return + + windows_dir = os.environ["SYSTEMROOT"] + if SYSTEM64: + system32 = os.path.join(windows_dir, "SysWOW64") + system64 = os.path.join(windows_dir, "System32") + else: + system32 = os.path.join(windows_dir, "") + system64 = None + if ARCH64: + system = system64 + else: + system = system32 + + root_search_paths = [] + + # Need to check for .pyd files for all Python version, because + # the builder/installer work in a way that previous cefpython + # module builds for other Python versions are also included + # in the package. Thus if included, msvcpxx.dll dependency is + # required as well. + + # Python 3.5 / 3.6 / 3.7 / 3.8 / 3.9 + if os.path.exists(os.path.join(pkg_dir, "cefpython_py35.pyd")) \ + or os.path.exists(os.path.join(pkg_dir, "cefpython_py36.pyd")) \ + or os.path.exists(os.path.join(pkg_dir, "cefpython_py37.pyd")) \ + or os.path.exists(os.path.join(pkg_dir, "cefpython_py38.pyd")) \ + or os.path.exists(os.path.join(pkg_dir, "cefpython_py39.pyd")): + search_paths = [ + # This is where Microsoft Visual C++ 2015 Update 3 installs + # (14.00.24212). + os.path.join(system, "msvcp140.dll"), + ] + root_search_paths.append(search_paths) + + # Python 3.4 + if os.path.exists(os.path.join(pkg_dir, "cefpython_py34.pyd")): + search_paths = [ + # 10.00.40219.325 installs here on my system. + os.path.join(system, "msvcp100.dll"), + ] + root_search_paths.append(search_paths) + + # Python 2.7 + if os.path.exists(os.path.join(pkg_dir, "cefpython_py27.pyd")): + if ARCH32: + search_paths = [ + # This runtime version is shipped with Python 2.7.14 + r"c:\Windows\winsxs\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b" + r"_9.0.30729.1_none_e163563597edeada\msvcp90.dll", + ] + else: + search_paths = [ + # This runtime version is shipped with Python 2.7.14 + r"c:\Windows\winsxs\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b" + r"_9.0.30729.1_none_99b61f5e8371c1d4\msvcp90.dll", + ] + root_search_paths.append(search_paths) + + assert len(root_search_paths) + + for search_paths in root_search_paths: + found = False + for path in search_paths: + if os.path.exists(path): + shutil.copy(path, pkg_dir) + found = True + if not found: + raise Exception("C++ extension dll dependency not found." + " Search paths: {0}" + .format(", ".join(search_paths))) + + def short_src_path(path): # Very long: \build\cef55_3.2883.1553.g80bd606_win32\ find = os.path.basename(CEF_BINARIES_LIBRARIES) diff --git a/tools/requirements.py b/tools/requirements.py new file mode 100644 index 000000000..d52c5d4b1 --- /dev/null +++ b/tools/requirements.py @@ -0,0 +1,23 @@ +""" +Installs Python dependencies using the pip tool. +See the requirements.txt file. +python -m pip install --upgrade -r ../tools/requirements.txt +""" + +from common import * +import subprocess + + +def main(): + args = [] + if sys.executable.startswith("/usr/"): + args.append("sudo") + requirements = os.path.join(TOOLS_DIR, "requirements.txt") + args.extend([sys.executable, "-m", "pip", "install", "--upgrade", + "-r", requirements]) + retcode = subprocess.call(args) + sys.exit(retcode) + + +if __name__ == "__main__": + main() diff --git a/tools/requirements.txt b/tools/requirements.txt index e8f345b1f..4b0835794 100644 --- a/tools/requirements.txt +++ b/tools/requirements.txt @@ -1,4 +1,4 @@ -Cython == 0.25.2 +Cython == 0.29.21 docopt >= 0.6.2 setuptools wheel diff --git a/tools/run_examples.py b/tools/run_examples.py index 2948012ce..bf70f0450 100644 --- a/tools/run_examples.py +++ b/tools/run_examples.py @@ -3,7 +3,8 @@ # Project website: https://github.com/cztomczak/cefpython """ -Run all examples that can be run on current configuration. +Run all examples that can be run on current configuration +and display a summary at the end. Note on GTK 2 / GTK 3 on Windows: Installing both PyGTK and PyGI on Windows will cause errors. @@ -14,6 +15,7 @@ import importlib import os +import subprocess import sys @@ -77,7 +79,10 @@ def main(): passed.append("gtk3.py") # pyqt4 - if packages["PyQt4"]: + if LINUX: + print("[run_examples.py] PASS: qt.py pyqt4 (Issue #452)") + passed.append("qt.py pyqt4 (Issue #452)") + elif packages["PyQt4"]: examples.append("qt.py pyqt4") else: print("[run_examples.py] PASS: qt.py pyqt4 (PyQt4 not installed)") @@ -91,7 +96,10 @@ def main(): passed.append("qt.py pyqt5") # pyside - if packages["PySide"]: + if LINUX: + print("[run_examples.py] PASS: qt.py pyside (Issue #452)") + passed.append("qt.py pyside (Issue #452)") + elif packages["PySide"]: examples.append("qt.py pyside") else: print("[run_examples.py] PASS: qt.py pyside (PySide not installed)") @@ -102,6 +110,9 @@ def main(): # This example often crashes on Mac (Issue #309) print("[run_examples.py] PASS: tkinter_.py (Issue #309)") passed.append("tkinter_.py (Issue #309)") + elif WINDOWS and sys.version_info.major == 2: + print("[run_examples.py] PASS: tkinter_.py (Issue #441)") + passed.append("tkinter_.py (Issue #441)") elif packages["tkinter"] or packages["Tkinter"]: examples.append("tkinter_.py") else: @@ -178,8 +189,12 @@ def check_installed_packages(): } for package in packages: try: - importlib.import_module(package) - packages[package] = True + if package == "PyQt5": + # Strange issue on Mac, PyQt5 is an empty built-in module + from PyQt5 import QtGui + else: + importlib.import_module(package) + packages[package] = True except ImportError: packages[package] = False packages["gi"] = check_gi_installed() @@ -189,9 +204,10 @@ def check_installed_packages(): def check_gi_installed(): # Cannot import both gtk and gi in the same script, thus # need another way of checking if gi package is installed. - command = ("\"{python}\" -c \"import gi\"" - .format(python=sys.executable)) - code = os.system(command) + code = subprocess.call([sys.executable, "-c", "import gi"]) + if code != 0: + print("[run_examples.py] gi module not found (PyGI / GTK 3).") + print(" Import error above can be safely ignored.") return True if code == 0 else False diff --git a/tools/run_snippets.py b/tools/run_snippets.py new file mode 100644 index 000000000..193d95587 --- /dev/null +++ b/tools/run_snippets.py @@ -0,0 +1,55 @@ +# Copyright (c) 2018 CEF Python, see the Authors file. +# All rights reserved. Licensed under BSD 3-clause license. +# Project website: https://github.com/cztomczak/cefpython + +""" +Run all snippets from the examples/snippets/ directory +and display a summary at the end. +""" + +from common import * + +import glob +import os +import subprocess +import sys + + +def main(): + # Iterate over all snippets + snippets_iter = glob.glob(os.path.join(SNIPPETS_DIR, "*.py")) + succeeded = [] + failed = [] + for snippet in snippets_iter: + print("[run_snippets.py] Running '{snippet}'..." + .format(snippet=os.path.basename(snippet))) + retcode = subprocess.call([sys.executable, snippet]) + if retcode == 0: + succeeded.append(os.path.basename(snippet)) + else: + print("[run_snippets.py] ERROR while running snippet: {snippet}" + .format(snippet=snippet)) + failed.append(os.path.basename(snippet)) + + # Print summary + summary = "" + for snippet in succeeded: + summary += " OK {snippet}{nl}"\ + .format(snippet=snippet, nl=os.linesep) + for snippet in failed: + summary += " ERROR {snippet}{nl}"\ + .format(snippet=snippet, nl=os.linesep) + summary = summary[:-(len(os.linesep))] + print("[run_snippets.py] SUMMARY:") + print(summary.format()) + if len(failed): + print("[run_snippets.py] ERRORS ({failed}) while running snippets" + .format(failed=len(failed))) + sys.exit(1) + else: + print("[run_snippets.py] OK ({succeeded})" + .format(succeeded=len(succeeded))) + + +if __name__ == "__main__": + main() diff --git a/unittests/_common.py b/unittests/_common.py new file mode 100644 index 000000000..fcd899172 --- /dev/null +++ b/unittests/_common.py @@ -0,0 +1,247 @@ +# Copyright (c) 2018 CEF Python, see the Authors file. +# All rights reserved. Licensed under BSD 3-clause license. +# Project website: https://github.com/cztomczak/cefpython + +from cefpython3 import cefpython as cef + +import base64 +import os +import platform +import sys +import time + +# Platforms +SYSTEM = platform.system().upper() +if SYSTEM == "DARWIN": + SYSTEM = "MAC" +WINDOWS = SYSTEM if SYSTEM == "WINDOWS" else False +LINUX = SYSTEM if SYSTEM == "LINUX" else False +MAC = SYSTEM if SYSTEM == "MAC" else False + +# To show the window for an extended period of time increase this number. +MESSAGE_LOOP_RANGE = 200 # each iteration is 0.01 sec + +MAIN_BROWSER_ID = 1 +POPUP_BROWSER_ID = 2 + +g_subtests_ran = 0 +g_js_code_completed = False +g_on_load_end_callbacks = [] + + +def subtest_message(message): + global g_subtests_ran + g_subtests_ran += 1 + print(str(g_subtests_ran) + ". " + message) + sys.stdout.flush() + + +def show_test_summary(pyfile): + print("\nRan " + str(g_subtests_ran) + " sub-tests in " + + os.path.basename(pyfile)) + + +def run_message_loop(): + # Run message loop for some time. + # noinspection PyTypeChecker + for i in range(MESSAGE_LOOP_RANGE): + cef.MessageLoopWork() + time.sleep(0.01) + subtest_message("cef.MessageLoopWork() ok") + + +def do_message_loop_work(work_loops): + # noinspection PyTypeChecker + for i in range(work_loops): + cef.MessageLoopWork() + time.sleep(0.01) + + +def on_load_end(callback, *args): + global g_on_load_end_callbacks + g_on_load_end_callbacks.append([callback, args]) + + +def js_code_completed(): + """Sometimes window.onload can execute before javascript bindings + are ready if the document loads very fast. When setting javascript + bindings it can take some time, because these bindings are sent + via IPC messaging to the Renderer process.""" + global g_js_code_completed + assert not g_js_code_completed + g_js_code_completed = True + subtest_message("js_code_completed() ok") + + +def check_auto_asserts(test_case, objects): + # Check if js code completed + test_case.assertTrue(g_js_code_completed) + + # Automatic check of asserts in handlers and in external + for obj in objects: + test_for_True = False # Test whether asserts are working correctly + for key, value in obj.__dict__.items(): + if key == "test_for_True": + test_for_True = value + continue + if "_True" in key: + test_case.assertTrue(value, "Check assert: " + + obj.__class__.__name__ + "." + key) + subtest_message(obj.__class__.__name__ + "." + + key.replace("_True", "") + + " ok") + elif "_False" in key: + test_case.assertFalse(value, "Check assert: " + + obj.__class__.__name__ + "." + key) + subtest_message(obj.__class__.__name__ + "." + + key.replace("_False", "") + + " ok") + if "test_for_True" in obj.__dict__.keys(): + test_case.assertTrue(test_for_True) + + +class DisplayHandler(object): + def __init__(self, test_case): + self.test_case = test_case + + # Asserts for True/False will be checked just before shutdown + self.test_for_True = True # Test whether asserts are working correctly + self.javascript_errors_False = False + self.OnConsoleMessage_True = False + + def OnConsoleMessage(self, message, **_): + if "error" in message.lower() or "uncaught" in message.lower(): + self.javascript_errors_False = True + raise Exception("Javascript error: " + message) + else: + # Check whether messages from javascript are coming + self.OnConsoleMessage_True = True + subtest_message(message) + + +def close_popup(global_handler, browser): + browser.CloseBrowser() + global_handler.PopupClosed_True = True + + # Test developer tools popup + main_browser = cef.GetBrowserByIdentifier(MAIN_BROWSER_ID) + main_browser.ShowDevTools() + cef.PostDelayedTask(cef.TID_UI, 1500, close_devtools, global_handler) + cef.PostDelayedTask(cef.TID_UI, 500, main_browser.SetFocus, True) + + +def close_devtools(global_handler): + main_browser = cef.GetBrowserByIdentifier(MAIN_BROWSER_ID) + global_handler.HasDevTools_True = main_browser.HasDevTools() + main_browser.CloseDevTools() + subtest_message("DevTools popup ok") + + +class GlobalHandler(object): + def __init__(self, test_case): + self.test_case = test_case + + # Asserts for True/False will be checked just before shutdown + self.test_for_True = True # Test whether asserts are working correctly + self.OnAfterCreatedMain_True = False + + if "main_test" in test_case.id(): + self.OnAfterCreatedPopup_True = False + self.PopupClosed_True = False + self.HasDevTools_True = False + + def _OnAfterCreated(self, browser, **_): + # For asserts that are checked automatically before shutdown its + # values should be set first, so that when other asserts fail + # (the ones called through the test_case member) they are reported + # correctly. + if not self.OnAfterCreatedMain_True: + # First call for main browser. + # browser.GetUrl() returns empty at this moment. + self.OnAfterCreatedMain_True = True + self.test_case.assertEqual(browser.GetIdentifier(), + MAIN_BROWSER_ID) + self.test_case.assertFalse(browser.IsPopup()) + else: + # Second call for implicit popup browser opened via js. + # Should execute only for main test. + # Should not execute for DevTools window. + assert "main_test" in self.test_case.id() + assert not self.OnAfterCreatedPopup_True + self.OnAfterCreatedPopup_True = True + self.test_case.assertEqual(browser.GetIdentifier(), + POPUP_BROWSER_ID) + # browser.GetUrl() returns empty at this moment. + self.test_case.assertTrue(browser.IsPopup()) + if WINDOWS: + cef.WindowUtils.SetTitle(browser, "Popup test") + # Close the popup browser after 250 ms + cef.PostDelayedTask(cef.TID_UI, 250, close_popup, self, browser) + + +class LoadHandler(object): + def __init__(self, test_case, datauri): + self.test_case = test_case + self.datauri = datauri + self.frame_source_visitor = None + + # Asserts for True/False will be checked just before shutdown + self.test_for_True = True # Test whether asserts are working correctly + self.OnLoadStart_True = False + self.OnLoadEnd_True = False + self.FrameSourceVisitor_True = False + # self.OnLoadingStateChange_Start_True = False # FAILS + self.OnLoadingStateChange_End_True = False + + def OnLoadStart(self, browser, frame, **_): + self.test_case.assertFalse(self.OnLoadStart_True) + self.OnLoadStart_True = True + self.test_case.assertEqual(browser.GetUrl(), frame.GetUrl()) + self.test_case.assertEqual(browser.GetUrl(), self.datauri) + + def OnLoadEnd(self, browser, frame, http_code, **_): + # OnLoadEnd should be called only once + self.test_case.assertFalse(self.OnLoadEnd_True) + self.OnLoadEnd_True = True + self.test_case.assertEqual(http_code, 200) + self.frame_source_visitor = FrameSourceVisitor(self, self.test_case) + frame.GetSource(self.frame_source_visitor) + browser.ExecuteJavascript("print('LoadHandler.OnLoadEnd() ok')") + + subtest_message("Executing callbacks registered with on_load_end()") + global g_on_load_end_callbacks + for callback_data in g_on_load_end_callbacks: + callback_data[0](*callback_data[1]) + del g_on_load_end_callbacks + + def OnLoadingStateChange(self, browser, is_loading, can_go_back, + can_go_forward, **_): + if is_loading: + # TODO: this test fails, looks like OnLoadingStaetChange with + # is_loading=False is being called very fast, before + # OnLoadStart and before client handler is set by calling + # browser.SetClientHandler(). + # SOLUTION: allow to set OnLoadingStateChange through + # SetGlobalClientCallback similarly to _OnAfterCreated(). + # self.test_case.assertFalse(self.OnLoadingStateChange_Start_True) + # self.OnLoadingStateChange_Start_True = True + pass + else: + self.test_case.assertFalse(self.OnLoadingStateChange_End_True) + self.OnLoadingStateChange_End_True = True + self.test_case.assertEqual(browser.CanGoBack(), can_go_back) + self.test_case.assertEqual(browser.CanGoForward(), can_go_forward) + + +class FrameSourceVisitor(object): + """Visitor for Frame.GetSource().""" + + def __init__(self, load_handler, test_case): + self.load_handler = load_handler + self.test_case = test_case + + def Visit(self, value): + self.test_case.assertFalse(self.load_handler.FrameSourceVisitor_True) + self.load_handler.FrameSourceVisitor_True = True + self.test_case.assertIn("747ef3e6011b6a61e6b3c6e54bdd2dee", + value) diff --git a/unittests/issue517.py b/unittests/issue517.py new file mode 100644 index 000000000..60f9a04c9 --- /dev/null +++ b/unittests/issue517.py @@ -0,0 +1,43 @@ +# coding=utf8 +from cefpython3 import cefpython as cef +import sys + +html = """ + + + + + Title + + + + + +""" + + +class RequestHandler: + def GetResourceHandler(self, browser, frame, request): + print(request.GetPostData()) + return None + +def main(): + sys.excepthook = cef.ExceptHook + cef.Initialize() + browser = cef.CreateBrowserSync(url=cef.GetDataUrl(html)) + browser.SetClientHandler(RequestHandler()) + cef.MessageLoop() + del browser + cef.Shutdown() + + +if __name__ == '__main__': + main() diff --git a/unittests/main_test.py b/unittests/main_test.py index ecc0f9c31..ce71af20f 100644 --- a/unittests/main_test.py +++ b/unittests/main_test.py @@ -7,14 +7,14 @@ import unittest # noinspection PyUnresolvedReferences import _test_runner -from os.path import basename +from _common import * + from cefpython3 import cefpython as cef -import time -import base64 + +import glob +import os import sys -# To show the window for an extended period of time increase this number. -MESSAGE_LOOP_RANGE = 200 # each iteration is 0.01 sec g_datauri_data = """ @@ -25,6 +25,10 @@ font-family: Arial; font-size: 11pt; } + body { + width: 810px; + heiht: 610px; + } @@ -90,25 +113,14 @@ """ -g_datauri = "data:text/html;base64,"+base64.b64encode(g_datauri_data.encode( - "utf-8", "replace")).decode("utf-8", "replace") - -g_subtests_ran = 0 - - -def subtest_message(message): - global g_subtests_ran - g_subtests_ran += 1 - print(str(g_subtests_ran) + ". " + message) - sys.stdout.flush() +g_datauri = cef.GetDataUrl(g_datauri_data) class MainTest_IsolatedTest(unittest.TestCase): - def test_main(self): - """Main entry point.""" - # All this code must run inside one single test, otherwise strange - # things happen. + """Main entry point. All the code must run inside one + single test, otherwise strange things happen.""" + print("") print("CEF Python {ver}".format(ver=cef.__version__)) print("Python {ver}".format(ver=sys.version[:6])) @@ -119,33 +131,95 @@ def test_main(self): "log_severity": cef.LOGSEVERITY_ERROR, "log_file": "", } + if not LINUX: + # On Linux you get a lot of "X error received" messages + # from Chromium's "x11_util.cc", so do not show them. + settings["log_severity"] = cef.LOGSEVERITY_WARNING if "--debug" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_INFO + if "--debug-warning" in sys.argv: + settings["debug"] = True + settings["log_severity"] = cef.LOGSEVERITY_WARNING cef.Initialize(settings) subtest_message("cef.Initialize() ok") - # Test global handler + # CRL set file + certrevoc_dir = "" + if WINDOWS: + certrevoc_dir = r"C:\localappdata\Google\Chrome\User Data" \ + r"\CertificateRevocation" + elif LINUX: + certrevoc_dir = r"/home/*/.config/google-chrome" \ + r"/CertificateRevocation" + elif MAC: + certrevoc_dir = r"/Users/*/Library/Application Support/Google" \ + r"/Chrome/CertificateRevocation" + crlset_files = glob.iglob(os.path.join(certrevoc_dir, "*", + "crl-set")) + crlset = "" + for crlset in crlset_files: + pass + if os.path.exists(crlset): + cef.LoadCrlSetsFile(crlset) + subtest_message("cef.LoadCrlSetsFile ok") + + # High DPI on Windows. + # Setting DPI awareness from Python is usually too late and should be done + # via manifest file. Alternatively change python.exe properties > Compatibility + # > High DPI scaling override > Application. + # Using cef.DpiAware.EnableHighDpiSupport is problematic, it can cause + # display glitches. + if WINDOWS: + self.assertIsInstance(cef.DpiAware.GetSystemDpi(), tuple) + window_size = cef.DpiAware.CalculateWindowSize(800, 600) + self.assertIsInstance(window_size, tuple) + self.assertGreater(window_size[0], 0) + self.assertGreater(cef.DpiAware.Scale((800, 600))[0], 0) + + # OFF - see comments above. + # cef.DpiAware.EnableHighDpiSupport() + # self.assertTrue(cef.DpiAware.IsProcessDpiAware()) + + # Make some calls again after DPI Aware was set + self.assertIsInstance(cef.DpiAware.GetSystemDpi(), tuple) + self.assertGreater(cef.DpiAware.Scale([800, 600])[0], 0) + self.assertIsInstance(cef.DpiAware.Scale(800), int) + self.assertGreater(cef.DpiAware.Scale(800), 0) + subtest_message("cef.DpiAware ok") + + # Global handler global_handler = GlobalHandler(self) cef.SetGlobalClientCallback("OnAfterCreated", global_handler._OnAfterCreated) subtest_message("cef.SetGlobalClientCallback() ok") - # Test creation of browser - browser = cef.CreateBrowserSync(url=g_datauri) + # Create browser + browser_settings = { + "inherit_client_handlers_for_popups": False, + } + browser = cef.CreateBrowserSync(url=g_datauri, + settings=browser_settings) self.assertIsNotNone(browser, "Browser object") + browser.SetFocus(True) subtest_message("cef.CreateBrowserSync() ok") - # Test other handlers: LoadHandler, DisplayHandler etc. - client_handlers = [LoadHandler(self), DisplayHandler(self)] + # Client handlers + display_handler2 = DisplayHandler2(self) + v8context_handler = V8ContextHandler(self) + client_handlers = [LoadHandler(self, g_datauri), + DisplayHandler(self), + display_handler2, + v8context_handler] for handler in client_handlers: browser.SetClientHandler(handler) subtest_message("browser.SetClientHandler() ok") - # Test javascript bindings + # Javascript bindings external = External(self) bindings = cef.JavascriptBindings( bindToFrames=False, bindToPopups=False) + bindings.SetFunction("js_code_completed", js_code_completed) bindings.SetFunction("test_function", external.test_function) bindings.SetProperty("test_property1", external.test_property1) bindings.SetProperty("test_property2", external.test_property2) @@ -153,157 +227,154 @@ def test_main(self): # supports passing functions as callbacks when called from # javascript, and as a side effect any value and in this case # a property can also be a function. - bindings.SetProperty("test_property3_function", external.test_property3_function) + bindings.SetProperty("test_property3_function", + external.test_property3_function) bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings) subtest_message("browser.SetJavascriptBindings() ok") - # Run message loop for some time. - # noinspection PyTypeChecker - for i in range(MESSAGE_LOOP_RANGE): - cef.MessageLoopWork() - time.sleep(0.01) - subtest_message("cef.MessageLoopWork() ok") - - # Test browser closing. Remember to clean reference. + # Set auto resize. Call it after js bindings were set. + browser.SetAutoResizeEnabled(enabled=True, + min_size=[800, 600], + max_size=[1024, 768]) + subtest_message("browser.SetAutoResizeEnabled() ok") + + # Test Request.SetPostData(list) + # noinspection PyArgumentList + req = cef.Request.CreateRequest() + req_file = os.path.dirname(os.path.abspath(__file__)) + req_file = os.path.join(req_file, "main_test.py") + if sys.version_info.major > 2: + req_file = req_file.encode("utf-8") + req_data = [b"--key=value", b"@"+req_file] + req.SetMethod("POST") + req.SetPostData(req_data) + self.assertEqual(req_data, req.GetPostData()) + subtest_message("cef.Request.SetPostData(list) ok") + + # Test Request.SetPostData(dict) + # noinspection PyArgumentList + req = cef.Request.CreateRequest() + req_data = {b"key": b"value"} + req.SetMethod("POST") + req.SetPostData(req_data) + 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") + + # Window Utils + if WINDOWS: + hwnd = 1 # When using 0 getting issues with OnautoResize + self.assertFalse(cef.WindowUtils.IsWindowHandle(hwnd)) + cef.WindowUtils.OnSetFocus(hwnd, 0, 0, 0) + cef.WindowUtils.OnSize(hwnd, 0, 0, 0) + cef.WindowUtils.OnEraseBackground(hwnd, 0, 0, 0) + cef.WindowUtils.GetParentHandle(hwnd) + cef.WindowUtils.SetTitle(browser, "Main test") + subtest_message("cef.WindowUtils ok") + elif LINUX: + cef.WindowUtils.InstallX11ErrorHandlers() + subtest_message("cef.WindowUtils ok") + elif MAC: + hwnd = 0 + cef.WindowUtils.GetParentHandle(hwnd) + cef.WindowUtils.IsWindowHandle(hwnd) + subtest_message("cef.WindowUtils ok") + + # Run message loop + run_message_loop() + + # Make sure popup browser was destroyed + self.assertIsInstance(cef.GetBrowserByIdentifier(MAIN_BROWSER_ID), + cef.PyBrowser) + self.assertIsNone(cef.GetBrowserByIdentifier(POPUP_BROWSER_ID)) + subtest_message("cef.GetBrowserByIdentifier() ok") + + # Close browser and clean reference browser.CloseBrowser(True) del browser subtest_message("browser.CloseBrowser() ok") - # Give it some time to close before calling shutdown. + # Give it some time to close before checking asserts + # and calling shutdown. + do_message_loop_work(25) + + # Asserts before shutdown + self.assertEqual(display_handler2.OnLoadingProgressChange_Progress, + 1.0) # noinspection PyTypeChecker - for i in range(25): - cef.MessageLoopWork() - time.sleep(0.01) - - # Automatic check of asserts in handlers and in external - for obj in [] + client_handlers + [global_handler, external]: - test_for_True = False # Test whether asserts are working correctly - for key, value in obj.__dict__.items(): - if key == "test_for_True": - test_for_True = True - continue - if "_True" in key: - self.assertTrue(value, "Check assert: " + - obj.__class__.__name__ + "." + key) - subtest_message(obj.__class__.__name__ + "." + - key.replace("_True", "") + - " ok") - elif "_False" in key: - self.assertFalse(value, "Check assert: " + - obj.__class__.__name__ + "." + key) - subtest_message(obj.__class__.__name__ + "." + - key.replace("_False", "") + - " ok") - self.assertTrue(test_for_True) + check_auto_asserts(self, [] + client_handlers + + [global_handler, + external]) # Test shutdown of CEF cef.Shutdown() subtest_message("cef.Shutdown() ok") - # Display real number of tests there were run - print("\nRan " + str(g_subtests_ran) + " sub-tests in test_main") + # Display summary + show_test_summary(__file__) sys.stdout.flush() -class GlobalHandler(object): +class DisplayHandler2(object): def __init__(self, test_case): self.test_case = test_case - - # Asserts for True/False will be checked just before shutdown - self.test_for_True = True # Test whether asserts are working correctly - self.OnAfterCreated_True = False - - def _OnAfterCreated(self, browser, **_): - # For asserts that are checked automatically before shutdown its - # values should be set first, so that when other asserts fail - # (the ones called through the test_case member) they are reported - # correctly. - self.test_case.assertFalse(self.OnAfterCreated_True) - self.OnAfterCreated_True = True - self.test_case.assertEqual(browser.GetIdentifier(), 1) - - -class LoadHandler(object): + # Asserts for True/False will be checked just before shutdown. + # Test whether asserts are working correctly. + self.test_for_True = True + self.OnAutoResize_True = False + self.OnLoadingProgressChange_True = False + self.OnLoadingProgressChange_Progress = 0.0 + + def OnAutoResize(self, new_size, **_): + self.OnAutoResize_True = True + self.test_case.assertGreaterEqual(new_size[0], 800) + self.test_case.assertLessEqual(new_size[0], 1024) + self.test_case.assertGreaterEqual(new_size[1], 600) + self.test_case.assertLessEqual(new_size[1], 768) + + def OnLoadingProgressChange(self, progress, **_): + self.OnLoadingProgressChange_True = True + self.OnLoadingProgressChange_Progress = progress + + +class V8ContextHandler(object): def __init__(self, test_case): self.test_case = test_case - self.frame_source_visitor = None - - # Asserts for True/False will be checked just before shutdown - self.test_for_True = True # Test whether asserts are working correctly - self.OnLoadStart_True = False - self.OnLoadEnd_True = False - self.FrameSourceVisitor_True = False - # self.OnLoadingStateChange_Start_True = False # FAILS - self.OnLoadingStateChange_End_True = False - - def OnLoadStart(self, browser, frame, **_): - self.test_case.assertFalse(self.OnLoadStart_True) - self.OnLoadStart_True = True - self.test_case.assertEqual(browser.GetUrl(), frame.GetUrl()) - self.test_case.assertEqual(browser.GetUrl(), g_datauri) - - def OnLoadEnd(self, browser, frame, http_code, **_): - # OnLoadEnd should be called only once - self.test_case.assertFalse(self.OnLoadEnd_True) - self.OnLoadEnd_True = True - self.test_case.assertEqual(http_code, 200) - self.frame_source_visitor = FrameSourceVisitor(self, self.test_case) - frame.GetSource(self.frame_source_visitor) - browser.ExecuteJavascript("print('LoadHandler.OnLoadEnd() ok')") - - def OnLoadingStateChange(self, browser, is_loading, can_go_back, - can_go_forward, **_): - if is_loading: - # TODO: this test fails, looks like OnLoadingStaetChange with - # is_loading=False is being called very fast, before - # OnLoadStart and before client handler is set by calling - # browser.SetClientHandler(). - # SOLUTION: allow to set OnLoadingStateChange through - # SetGlobalClientCallback similarly to _OnAfterCreated(). - # self.test_case.assertFalse(self.OnLoadingStateChange_Start_True) - # self.OnLoadingStateChange_Start_True = True - pass + self.OnContextCreatedFirstCall_True = False + self.OnContextCreatedSecondCall_True = False + self.OnContextReleased_True = False + + def OnContextCreated(self, browser, frame): + """CEF creates one context when creating browser and this one is + released immediately. Then when it loads url another context is + created.""" + if not self.OnContextCreatedFirstCall_True: + self.OnContextCreatedFirstCall_True = True else: - self.test_case.assertFalse(self.OnLoadingStateChange_End_True) - self.OnLoadingStateChange_End_True = True - self.test_case.assertEqual(browser.CanGoBack(), can_go_back) - self.test_case.assertEqual(browser.CanGoForward(), can_go_forward) - - -class DisplayHandler(object): - def __init__(self, test_case): - self.test_case = test_case - - # Asserts for True/False will be checked just before shutdown - self.test_for_True = True # Test whether asserts are working correctly - self.javascript_errors_False = False - self.OnConsoleMessage_True = False - - def OnConsoleMessage(self, message, **_): - if "error" in message.lower() or "uncaught" in message.lower(): - self.javascript_errors_False = True - raise Exception(message) - else: - # Check whether messages from javascript are coming - self.OnConsoleMessage_True = True - subtest_message(message) - - -class FrameSourceVisitor(object): - """Visitor for Frame.GetSource().""" - - def __init__(self, load_handler, test_case): - self.load_handler = load_handler - self.test_case = test_case - - def Visit(self, **_): - self.test_case.assertFalse(self.load_handler.FrameSourceVisitor_True) - self.load_handler.FrameSourceVisitor_True = True - self.test_case.assertIn("747ef3e6011b6a61e6b3c6e54bdd2dee", - g_datauri_data) - + self.test_case.assertFalse(self.OnContextCreatedSecondCall_True) + self.OnContextCreatedSecondCall_True = True + self.test_case.assertEqual(browser.GetIdentifier(), MAIN_BROWSER_ID) + self.test_case.assertEqual(frame.GetIdentifier(), 2) + + def OnContextReleased(self, browser, frame): + """This gets called only for the initial empty context, see comment + in OnContextCreated. This should never get called for the main frame + of the main browser, because it happens during app exit and there + isn't enough time for the IPC messages to go through.""" + self.test_case.assertFalse(self.OnContextReleased_True) + self.OnContextReleased_True = True + self.test_case.assertEqual(browser.GetIdentifier(), MAIN_BROWSER_ID) + self.test_case.assertEqual(frame.GetIdentifier(), 2) class External(object): """Javascript 'window.external' object.""" @@ -312,9 +383,10 @@ def __init__(self, test_case): self.test_case = test_case # Test binding properties to the 'window' object. + # 2147483648 is out of INT_MAX limit and will be sent to JS as string value. self.test_property1 = "Test binding property to the 'window' object" self.test_property2 = {"key1": self.test_property1, - "key2": ["Inside list", 1, 2]} + "key2": ["Inside list", 2147483647, 2147483648]} # Asserts for True/False will be checked just before shutdown self.test_for_True = True # Test whether asserts are working correctly @@ -342,4 +414,4 @@ def py_callback(msg_from_js): if __name__ == "__main__": - _test_runner.main(basename(__file__)) + _test_runner.main(os.path.basename(__file__)) diff --git a/unittests/osr_test.py b/unittests/osr_test.py new file mode 100644 index 000000000..672e98fe5 --- /dev/null +++ b/unittests/osr_test.py @@ -0,0 +1,275 @@ +# Copyright (c) 2018 CEF Python, see the Authors file. +# All rights reserved. Licensed under BSD 3-clause license. +# Project website: https://github.com/cztomczak/cefpython + +"""Off-screen rendering tests.""" + +import unittest +# noinspection PyUnresolvedReferences +import _test_runner +from _common import * + +from cefpython3 import cefpython as cef + +import sys + +g_datauri_data = """ + + + + + + + + + +

Off-screen rendering test

+
+
Test selection.
+ + +""" +g_datauri = cef.GetDataUrl(g_datauri_data) + + +class OsrTest_IsolatedTest(unittest.TestCase): + def test_osr(self): + """Main entry point. All the code must run inside one + single test, otherwise strange things happen.""" + + print("") + print("CEF Python {ver}".format(ver=cef.__version__)) + print("Python {ver}".format(ver=sys.version[:6])) + + # Application settings + settings = { + "debug": False, + "log_severity": cef.LOGSEVERITY_ERROR, + "log_file": "", + "windowless_rendering_enabled": True + } + if not LINUX: + # On Linux you get a lot of "X error received" messages + # from Chromium's "x11_util.cc", so do not show them. + settings["log_severity"] = cef.LOGSEVERITY_WARNING + if "--debug" in sys.argv: + settings["debug"] = True + settings["log_severity"] = cef.LOGSEVERITY_INFO + if "--debug-warning" in sys.argv: + settings["debug"] = True + settings["log_severity"] = cef.LOGSEVERITY_WARNING + + switches = { + # GPU acceleration is not supported in OSR mode, so must disable + # it using these Chromium switches (Issue #240 and #463) + "disable-gpu": "", + "disable-gpu-compositing": "", + # Tweaking OSR performance by setting the same Chromium flags + # as in upstream cefclient (Issue #240). + "enable-begin-frame-scheduling": "", + "disable-surfaces": "", # This is required for PDF ext to work + } + browser_settings = { + # Tweaking OSR performance (Issue #240) + "windowless_frame_rate": 30, # Default frame rate in CEF is 30 + } + + # Initialize + cef.Initialize(settings=settings, switches=switches) + subtest_message("cef.Initialize() ok") + + # Accessibility handler + accessibility_handler = AccessibilityHandler(self) + cef.SetGlobalClientHandler(accessibility_handler) + subtest_message("cef.SetGlobalClientHandler() ok") + + # Global handler + global_handler = GlobalHandler(self) + cef.SetGlobalClientCallback("OnAfterCreated", + global_handler._OnAfterCreated) + subtest_message("cef.SetGlobalClientCallback() ok") + + # Create browser + window_info = cef.WindowInfo() + window_info.SetAsOffscreen(0) + browser = cef.CreateBrowserSync(window_info=window_info, + settings=browser_settings, + url=g_datauri) + + # Javascript bindings + bindings = cef.JavascriptBindings( + bindToFrames=False, bindToPopups=False) + bindings.SetFunction("js_code_completed", js_code_completed) + bindings.SetProperty("cefpython_version", cef.GetVersion()) + browser.SetJavascriptBindings(bindings) + subtest_message("browser.SetJavascriptBindings() ok") + + # Enable accessibility + browser.SetAccessibilityState(cef.STATE_ENABLED) + subtest_message("cef.SetAccessibilityState(STATE_ENABLED) ok") + + # Client handlers + client_handlers = [LoadHandler(self, g_datauri), + DisplayHandler(self), + RenderHandler(self)] + for handler in client_handlers: + browser.SetClientHandler(handler) + + # Initiate OSR rendering + browser.SendFocusEvent(True) + browser.WasResized() + + # Test selection + on_load_end(select_h1_text, browser) + + # Message loop + run_message_loop() + + # Close browser and clean reference + browser.CloseBrowser(True) + del browser + subtest_message("browser.CloseBrowser() ok") + + # Give it some time to close before checking asserts + # and calling shutdown. + do_message_loop_work(25) + + # Asserts before shutdown + # noinspection PyTypeChecker + check_auto_asserts(self, [] + client_handlers + + [global_handler, + accessibility_handler]) + + # Test shutdown of CEF + cef.Shutdown() + subtest_message("cef.Shutdown() ok") + + # Display summary + show_test_summary(__file__) + sys.stdout.flush() + + +class AccessibilityHandler(object): + def __init__(self, test_case): + self.test_case = test_case + + # Asserts for True/False will be checked just before shutdown. + # Test whether asserts are working correctly. + self.test_for_True = True + + self.javascript_errors_False = False + self._OnAccessibilityTreeChange_True = False + self._OnAccessibilityLocationChange_True = False + self.loadComplete_True = False + self.layoutComplete_True = False + + def _OnAccessibilityTreeChange(self, value): + self._OnAccessibilityTreeChange_True = True + for event in value: + if "event_type" in event: + if event["event_type"] == "loadComplete": + # LoadHandler.OnLoadEnd is called after this event + self.test_case.assertFalse(self.loadComplete_True) + self.loadComplete_True = True + elif event["event_type"] == "layoutComplete": + # layoutComplete event occurs twice, one when a blank + # page is loaded and second time when loading datauri. + if self.loadComplete_True: + self.test_case.assertFalse(self.layoutComplete_True) + self.layoutComplete_True = True + + def _OnAccessibilityLocationChange(self, **_): + self._OnAccessibilityLocationChange_True = True + + +def select_h1_text(browser): + browser.SendMouseClickEvent(0, 0, cef.MOUSEBUTTON_LEFT, + mouseUp=False, clickCount=1) + browser.SendMouseMoveEvent(400, 20, mouseLeave=False, + modifiers=cef.EVENTFLAG_LEFT_MOUSE_BUTTON) + browser.SendMouseClickEvent(400, 20, cef.MOUSEBUTTON_LEFT, + mouseUp=True, clickCount=1) + browser.Invalidate(cef.PET_VIEW) + subtest_message("select_h1_text() ok") + + +class RenderHandler(object): + def __init__(self, test_case): + self.test_case = test_case + + # Asserts for True/False will be checked just before shutdown. + # Test whether asserts are working correctly. + self.test_for_True = True + + self.GetViewRect_True = False + self.OnPaint_True = False + self.OnTextSelectionChanged_True = False + + def GetViewRect(self, rect_out, **_): + """Called to retrieve the view rectangle which is relative + to screen coordinates. Return True if the rectangle was + provided.""" + # rect_out --> [x, y, width, height] + self.GetViewRect_True = True + rect_out.extend([0, 0, 800, 600]) + return True + + def OnPaint(self, element_type, paint_buffer, **_): + """Called when an element should be painted.""" + if element_type == cef.PET_VIEW: + self.test_case.assertEqual(paint_buffer.width, 800) + self.test_case.assertEqual(paint_buffer.height, 600) + if not self.OnPaint_True: + self.OnPaint_True = True + subtest_message("RenderHandler.OnPaint: viewport ok") + else: + raise Exception("Unsupported element_type in OnPaint") + + def OnTextSelectionChanged(self, selected_text, selected_range, **_): + if not self.OnTextSelectionChanged_True: + self.OnTextSelectionChanged_True = True + # First call + self.test_case.assertEqual(selected_text, "") + self.test_case.assertEqual(selected_range, [0, 0]) + else: + # Second call. + #

tag should be selected. + self.test_case.assertEqual(selected_text, + "Off-screen rendering test") + + +if __name__ == "__main__": + _test_runner.main(os.path.basename(__file__))