FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

GitHub Viewer

# Copyright (c) 2012 CEF Python, see the Authors file. # All rights reserved. Licensed under BSD 3-clause license. # Project website: https://github.com/cztomczak/cefpython # IMPORTANT notes: # # - cdef/cpdef functions returning something other than a Python object # should have in its declaration "except *", otherwise exceptions are # ignored. Those cdef/cpdef that return "object" have "except *" by # default. The setup/compile.py script will check for functions missing # "except *" and will display an error message about that, but it's # not perfect and won't detect all cases. # # - TODO: add checking for "except * with gil" in functions with the # "public" keyword # # - about acquiring/releasing GIL lock, see discussion here: # https://groups.google.com/forum/?fromgroups=#!topic/cython-users/jcvjpSOZPp0 # # - new ClientHandler() # means to throw an error if the cast is not allowed # # - in client handler callbacks (or others that are called from C++ and # use "except * with gil") must embrace all code in try..except otherwise # the error will be ignored, only printed to the output console, this is the # default behavior of Cython, to remedy this you are supposed to add "except *" # in function declaration, unfortunately it does not work, some conflict with # CEF threading, see topic at cython-users for more details: # https://groups.google.com/d/msg/cython-users/CRxWoX57dnM/aufW3gXMhOUJ. # # - 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. # # - In regards to GIL locks see Issue #102 "Remove GIL to avoid deadlocks when # calling CEF functions". # # - CTags requires all functions/methods imported in .pxd files to be preceded with "cdef", # otherwise they are not indexed. # # - __del__ method does not exist in Extension Types (cdef class), # you have to use __dealloc__ instead, try to remember that as # defining __del__ will not raise any warning and could lead to # memory leaks. # # - CefString.c_str() is safe to use only on Windows, on Ubuntu 64bit # for a "Pers" string it returns: "P\x00e\x00r\x00s\x00", which is # most probably not what you expected. # # - You can rename methods when importing in pxd files: # | cdef cppclass _Object "Object": # # - Supporting operators that are not yet supported: # | RetValue& Assign "operator="(T* p) # | object.Assign(T*) # In the same way you can import function with a different name, this one # imports a static method Create() while adding a prefix "CefSome_": # | cdef extern from "..": # | static CefRefPtr[CefSome] CefSome_Create "CefSome::Create"() # # - Declaring C++ classes in Cython. Storing python callbacks # in a C++ class using Py_INCREF, Py_DECREF. Calling from # C++ using PyObject_CallMethod. # | http://stackoverflow.com/a/17070382/623622 # Disadvantage: when calling python callback from the C++ class # declared in Cython there is no easy way to propagate the python # exceptions when they occur during execution of the callback. # # - | cdef char* other_c_string = py_string # This is a very fast operation after which other_c_string points # to the byte string buffer of the Python string itself. It is # tied to the life time of the Python string. When the Python # string is garbage collected, the pointer becomes invalid. # # - When defining cpdef functions returning "cpp_bool": # | cpdef cpp_bool myfunc() except *: # Always do an additional cast when returning value, even when # variable is defined as py_bool: # | cdef py_bool returnValue # | return bool(returnValue) # Otherwise compiler warnings appear: # | cefpython.cpp(26533) : warning C4800: 'int' : forcing value # | to bool 'true' or 'false' (performance warning) # Lots of these warnings results in ignoring them, but sometimes # they are shown for a good reason. For example when you forget # to return a value in a function. # # - Always import bool from libcpp as cpp_bool, if you import it as # "bool" in a pxd file, then Cython will complain about bool casts # like "bool(1)" being invalid, in pyx files. # # - malloc example code: # from libc.stdlib cimport malloc, free # cdef RECT* rect = malloc(sizeof(RECT)) # free(rect) # # All .pyx files need to be included in this file. # Includes being made in other .pyx files are allowed to help # IDE completion, but will be removed during cython compilation. include "compile_time_constants.pxi" # ----------------------------------------------------------------------------- # IMPORTS # noinspection PyUnresolvedReferences import os import sys # noinspection PyUnresolvedReferences import cython # noinspection PyUnresolvedReferences import platform # noinspection PyUnresolvedReferences import traceback # noinspection PyUnresolvedReferences import time # noinspection PyUnresolvedReferences import types # noinspection PyUnresolvedReferences import re # noinspection PyUnresolvedReferences import copy # noinspection PyUnresolvedReferences import inspect # used by JavascriptBindings.__SetObjectMethods() # noinspection PyUnresolvedReferences import urllib # noinspection PyUnresolvedReferences import json # noinspection PyUnresolvedReferences import datetime # noinspection PyUnresolvedReferences import random # noinspection PyUnresolvedReferences import struct # noinspection PyUnresolvedReferences import base64 # 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 # noinspection PyUnresolvedReferences from urllib import pathname2url as urllib_pathname2url # noinspection PyUnresolvedReferences from urllib import urlencode as urllib_urlencode from urllib import quote as urlparse_quote ELSE: # noinspection PyUnresolvedReferences from urllib import parse as urlparse from urllib.parse import quote as urlparse_quote # noinspection PyUnresolvedReferences from urllib.request import pathname2url as urllib_pathname2url # noinspection PyUnresolvedReferences from urllib.parse import urlencode as urllib_urlencode # noinspection PyUnresolvedReferences from cpython.version cimport PY_MAJOR_VERSION # noinspection PyUnresolvedReferences import weakref # We should allow multiple string types: str, unicode, bytes. # PyToCefString() can handle them all. # Important: # If you set it to basestring, Cython will accept exactly(!) # str/unicode in Py2 and str in Py3. This won't work in Py3 # as we might want to pass bytes as well. Also it will # reject string subtypes, so using it in publi API functions # would be a bad idea. ctypedef object py_string # You can't use "void" along with cpdef function returning None, it is # planned to be added to Cython in the future, creating this virtual # type temporarily. If you change it later to "void" then don't forget # to add "except *". ctypedef object py_void # noinspection PyUnresolvedReferences from cpython cimport PyLong_FromVoidPtr # noinspection PyUnresolvedReferences from cpython cimport bool as py_bool # noinspection PyUnresolvedReferences from libcpp cimport bool as cpp_bool # noinspection PyUnresolvedReferences from libcpp.map cimport map as cpp_map # noinspection PyUnresolvedReferences from multimap cimport multimap as cpp_multimap # noinspection PyUnresolvedReferences from libcpp.pair cimport pair as cpp_pair # noinspection PyUnresolvedReferences from libcpp.vector cimport vector as cpp_vector # noinspection PyUnresolvedReferences from libcpp.string cimport string as cpp_string # noinspection PyUnresolvedReferences from libcpp.memory cimport unique_ptr as cpp_unique_ptr # noinspection PyUnresolvedReferences from libcpp cimport nullptr # noinspection PyUnresolvedReferences from wstring cimport wstring as cpp_wstring # noinspection PyUnresolvedReferences from libc.string cimport strlen # noinspection PyUnresolvedReferences from libc.string cimport memcpy # preincrement and dereference must be "as" otherwise not seen. # noinspection PyUnresolvedReferences from cython.operator cimport preincrement as preinc, dereference as deref # noinspection PyUnresolvedReferences # from cython.operator cimport address as addr # Address of an c++ object? # noinspection PyUnresolvedReferences from libc.stdlib cimport calloc, malloc, free # noinspection PyUnresolvedReferences from libc.stdlib cimport atoi # When pyx file cimports * from a pxd file and that pxd cimports * from another pxd # then these names will be visible in pyx file. # Circular imports are allowed in form "cimport ...", but won't work if you do # "from ... cimport *", this is important to know in pxd files. # noinspection PyUnresolvedReferences from libc.stdint cimport int64_t, uint32_t, uint64_t # noinspection PyUnresolvedReferences from libc.stdint cimport uintptr_t cdef extern from *: """ #include using int64 = int64_t; using uint32 = uint32_t; using char16 = char16_t; """ # noinspection PyUnresolvedReferences ctypedef uintptr_t WindowHandle # noinspection PyUnresolvedReferences cimport ctime IF UNAME_SYSNAME == "Windows": from windows cimport * from dpi_aware_win cimport * ELIF UNAME_SYSNAME == "Linux": from linux cimport * ELIF UNAME_SYSNAME == "Darwin": from mac cimport * from cpp_utils cimport * from task cimport * IF UNAME_SYSNAME == "Linux": cimport x11 from cef_string cimport * cdef extern from *: # noinspection PyUnresolvedReferences ctypedef CefString ConstCefString "const CefString" # cannot cimport *, that would cause name conflicts with constants # noinspection PyUnresolvedReferences from cef_types cimport ( CefSettings, CefBrowserSettings, CefRect, CefSize, CefPoint, CefKeyEvent, CefMouseEvent, CefScreenInfo, PathKey, PK_DIR_EXE, PK_DIR_MODULE, int32, uint32, int64, uint64, cef_log_severity_t, ) # noinspection PyUnresolvedReferences from cef_ptr cimport CefRefPtr from cef_task cimport * from cef_platform cimport * from cef_app cimport * from cef_browser cimport * # noinspection PyUnresolvedReferences cimport cef_browser_static from cef_client cimport * from client_handler cimport * from cef_frame cimport * from cef_time cimport * from cef_values cimport * from cefpython_app cimport * from cef_process_message cimport * from cef_request_handler cimport * from cef_request cimport * from cef_cookie cimport * from cef_string_visitor cimport * # noinspection PyUnresolvedReferences cimport cef_cookie_manager_namespace from cookie_visitor cimport * from string_visitor cimport * from cef_callback cimport * from cef_response cimport * from cef_resource_handler cimport * from resource_handler cimport * from cef_urlrequest cimport * from web_request_client cimport * from cef_command_line cimport * from cef_request_context cimport * from cef_request_context_handler cimport * from request_context_handler cimport * from cef_jsdialog_handler cimport * from cef_permission_handler cimport * from cef_path_util cimport * from cef_drag_data cimport * from cef_image cimport * from main_message_loop cimport * # noinspection PyUnresolvedReferences from cef_views cimport * from cef_log cimport * from cef_file_util cimport * from print_callback cimport * from cef_download_handler cimport * from cef_download_item cimport * # ----------------------------------------------------------------------------- # GLOBAL VARIABLES g_debug = False # When put None here and assigned a local dictionary in Initialize(), later # while running app this global variable was garbage collected, see topic: # https://groups.google.com/d/topic/cython-users/0dw3UASh7HY/discussion # 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 # a unique one is created for each call to CreateBrowserSync. # noinspection PyUnresolvedReferences cdef CefRefPtr[CefRequestContext] g_shared_request_context cdef unique_ptr[MainMessageLoopExternalPump] g_external_message_pump cdef py_bool g_MessageLoop_called = False cdef py_bool g_MessageLoopWork_called = False cdef py_bool g_cef_initialized = False cdef dict g_globalClientCallbacks = {} # ----------------------------------------------------------------------------- include "cef_types.pyx" include "utils.pyx" include "string_utils.pyx" IF UNAME_SYSNAME == "Windows": include "string_utils_win.pyx" include "time_utils.pyx" include "browser.pyx" include "frame.pyx" include "settings.pyx" IF UNAME_SYSNAME == "Windows": include "window_utils_win.pyx" include "dpi_aware_win.pyx" ELIF UNAME_SYSNAME == "Linux": include "window_utils_linux.pyx" ELIF UNAME_SYSNAME == "Darwin": include "window_utils_mac.pyx" include "task.pyx" include "javascript_bindings.pyx" include "virtual_keys.pyx" include "window_info.pyx" include "process_message_utils.pyx" include "javascript_callback.pyx" include "python_callback.pyx" include "request.pyx" include "cookie.pyx" include "string_visitor.pyx" include "network_error.pyx" include "paint_buffer.pyx" include "callback.pyx" include "response.pyx" include "web_request.pyx" include "command_line.pyx" include "app.pyx" include "drag_data.pyx" include "helpers.pyx" include "image.pyx" include "pdf_print_handler.pyx" include "download_item.pyx" # Handlers include "handlers/accessibility_handler.pyx" include "handlers/browser_process_handler.pyx" include "handlers/display_handler.pyx" include "handlers/download_handler.pyx" include "handlers/focus_handler.pyx" include "handlers/javascript_dialog_handler.pyx" include "handlers/permission_handler.pyx" include "handlers/keyboard_handler.pyx" include "handlers/lifespan_handler.pyx" include "handlers/load_handler.pyx" include "handlers/render_handler.pyx" include "handlers/resource_handler.pyx" include "handlers/request_handler.pyx" include "handlers/v8context_handler.pyx" include "handlers/v8function_handler.pyx" # ----------------------------------------------------------------------------- # Utility functions to provide settings to the C++ browser process code. cdef public void cefpython_GetDebugOptions( cpp_bool* debug ) except * with gil: # Called from subprocess/cefpython_app.cpp -> CefPythonApp constructor. try: debug[0] = bool(g_debug) except: (exc_type, exc_value, exc_trace) = sys.exc_info() sys.excepthook(exc_type, exc_value, exc_trace) cdef public cpp_bool ApplicationSettings_GetBool(const char* key ) except * with gil: # Called from client_handler/client_handler.cpp for example cdef py_string pyKey = CharToPyString(key) if pyKey in g_applicationSettings: return bool(g_applicationSettings[pyKey]) return False cdef public cpp_bool ApplicationSettings_GetBoolFromDict(const char* key1, const char* key2) except * with gil: cdef py_string pyKey1 = CharToPyString(key1) cdef py_string pyKey2 = CharToPyString(key2) cdef object dictValue # Yet to be checked whether it is `dict` if pyKey1 in g_applicationSettings: dictValue = g_applicationSettings[pyKey1] if type(dictValue) != dict: return False if pyKey2 in dictValue: return bool(dictValue[pyKey2]) return False cdef public cpp_string ApplicationSettings_GetString(const char* key ) except * with gil: cdef py_string pyKey = CharToPyString(key) cdef cpp_string cppString if pyKey in g_applicationSettings: cppString = PyStringToChar(AnyToPyString(g_applicationSettings[pyKey])) return cppString cdef public int CommandLineSwitches_GetInt(const char* key) except * with gil: cdef py_string pyKey = CharToPyString(key) if pyKey in g_commandLineSwitches: return int(g_commandLineSwitches[pyKey]) return 0 def GetCommandLineSwitch(key): """Return a configured Chromium command-line switch, or None.""" return g_commandLineSwitches.get(key) # ----------------------------------------------------------------------------- # If you've built custom binaries with tcmalloc hook enabled on # Linux, then do not to run any of the CEF code until Initialize() # is called. See Issue #73 in the CEF Python Issue Tracker. def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs): # applicationSettings and commandLineSwitches argument # names are kept for backward compatibility. application_settings = applicationSettings command_line_switches = commandLineSwitches # Alternative names for existing parameters if "settings" in kwargs: assert not applicationSettings, "Bad arguments" application_settings = kwargs["settings"] del kwargs["settings"] if "switches" in kwargs: assert not command_line_switches, "Bad arguments" command_line_switches = kwargs["switches"] del kwargs["switches"] for kwarg in kwargs: raise Exception("Invalid argument: "+kwarg) if command_line_switches: # Make a copy as commandLineSwitches is a reference only # that might get destroyed later. global g_commandLineSwitches for key in command_line_switches: g_commandLineSwitches[key] = copy.deepcopy( command_line_switches[key]) # Chromium 138+ no longer enables automatic SwiftShader fallback by # default. Allow it without forcing software rendering, so Chromium still # prefers an available hardware GPU. Respect the established Chromium # switch for applications that explicitly disable software rasterization. if "disable-software-rasterizer" not in g_commandLineSwitches and \ "enable-unsafe-swiftshader" not in g_commandLineSwitches: g_commandLineSwitches["enable-unsafe-swiftshader"] = "" # Use g_commandLineSwitches if you need to modify or access # command line switches inside this function. del command_line_switches del commandLineSwitches IF UNAME_SYSNAME == "Linux": # Fix Issue #231 - Discovery of the "icudtl.dat" file fails on Linux. cdef str py_module_dir = GetModuleDirectory() cdef CefString cef_module_dir PyToCefString(py_module_dir, cef_module_dir) # END IF UNAME_SYSNAME == "Linux": if not application_settings: application_settings = {} cdef str module_dir = GetModuleDirectory() cdef str mac_frameworks_dir = "" cdef str mac_main_bundle_dir = "" cdef str configured_frameworks_dir = "" cdef str configured_contents_dir = "" cdef str configured_bundle_dir = "" cdef bint mac_sandbox_supported = False # CEF 150 uses the bootstrap loader on macOS. Load its function table # before creating CefString values or invoking any other CEF API. IF UNAME_SYSNAME == "Darwin": mac_main_bundle_dir = os.path.join(module_dir, "cefpython3.app") mac_frameworks_dir = os.path.join( mac_main_bundle_dir, "Contents", "Frameworks") # Chromium's macOS sandbox requires the browser executable, CEF # framework and helper apps to share one real top-level app bundle. # An ordinary Python interpreter cannot gain that relationship by # pointing at a synthetic bundle, so use the sandbox only for a # correctly packaged/frozen application. cdef str executable_macos_dir = os.path.dirname( os.path.abspath(sys.executable)) cdef str executable_contents_dir = os.path.dirname( executable_macos_dir) cdef str executable_bundle_dir = os.path.dirname( executable_contents_dir) cdef str executable_frameworks_dir = os.path.join( executable_contents_dir, "Frameworks") if os.path.basename(executable_macos_dir) == "MacOS" and \ os.path.basename(executable_contents_dir) == "Contents" and \ executable_bundle_dir.endswith(".app") and \ os.path.isdir(os.path.join( executable_frameworks_dir, "Chromium Embedded Framework.framework")) and \ os.path.isdir(os.path.join( executable_frameworks_dir, "cefpython3 Helper.app")): mac_main_bundle_dir = executable_bundle_dir mac_frameworks_dir = executable_frameworks_dir mac_sandbox_supported = True cdef str cef_framework_dir = application_settings.get( "framework_dir_path", os.path.join(mac_frameworks_dir, "Chromium Embedded Framework.framework")) if not mac_sandbox_supported: # Frozen Python launchers can report their executable directory as # the module directory even when cefpython3 is stored elsewhere. # Derive the synthetic main bundle from an explicitly configured # framework bundle instead of constructing a nonexistent sibling # of the launcher. configured_frameworks_dir = os.path.dirname( cef_framework_dir) configured_contents_dir = os.path.dirname( configured_frameworks_dir) configured_bundle_dir = os.path.dirname( configured_contents_dir) if os.path.basename(configured_frameworks_dir) == "Frameworks" \ and os.path.basename(configured_contents_dir) == \ "Contents" \ and configured_bundle_dir.endswith(".app") \ and os.path.isdir(configured_bundle_dir): mac_main_bundle_dir = configured_bundle_dir mac_frameworks_dir = configured_frameworks_dir cdef bytes cef_framework_binary = os.fsencode(os.path.join( cef_framework_dir, "Chromium Embedded Framework")) if not cef_load_library(cef_framework_binary): raise RuntimeError("Failed to load the CEF framework: " + os.fsdecode(cef_framework_binary)) # Debug settings need to be set before Debug() is called # and before the CefPythonApp class is instantiated. global g_debug if "--debug" in sys.argv: application_settings["debug"] = True application_settings["log_file"] = os.path.join(os.getcwd(), "debug.log") application_settings["log_severity"] = LOGSEVERITY_VERBOSE sys.argv.remove("--debug") if "debug" in application_settings: g_debug = bool(application_settings["debug"]) if "log_severity" in application_settings: if application_settings["log_severity"]

Back | FazBrowse Home | New Git URL