Debian Codegraph @

webview/platforms/android/jinterface/pywebview.py

raw
1 __all__ = ('EventCallbackWrapper', 'JsApiCallbackWrapper', 'RequestInterceptor')
2
3 from jnius import PythonJavaClass, java_method
4
5
6 class EventCallbackWrapper(PythonJavaClass):
7 """
8 Represents a wrapper for event callbacks between Python and Java.
9
10 This class enables interaction between Python and Java by implementing
11 a Java interface and providing a callback mechanism. The purpose is to
12 allow Python to respond to events triggered from the Java side by
13 passing the event and related data.
14
15 Attributes:
16 callback (Callable[[str, str], None]): A callable function that handles
17 an event and its associated data.
18
19 Methods:
20 callback(event: str, data: str): Calls the provided callback function
21 with the given event and data.
22 """
23
24 __javacontext__ = 'app'
25 __javainterfaces__ = ['com/pywebview/EventCallbackWrapper']
26
27 def __init__(self, callback):
28 self.callback_wrapper = callback
29
30 @java_method('(Ljava/lang/String;Ljava/lang/String;)V')
31 def callback(self, event, data):
32 self.callback_wrapper(event, data)
33
34
35 class JsApiCallbackWrapper(PythonJavaClass):
36 """
37 Wrapper class for JavaScript API callbacks.
38
39 This class serves as a bridge between a Python application and a Java
40 environment, enabling communication through JavaScript API callback functionality.
41 It implements the `JsApiCallbackWrapper` interface and is designed specifically
42 for use with webview integration.
43
44 Attributes:
45 callback (Callable): A Python callable that gets executed when the callback
46 method is triggered. The callable should accept three arguments.
47
48 """
49
50 __javacontext__ = 'app'
51 __javainterfaces__ = ['com/pywebview/JsApiCallbackWrapper']
52
53 def __init__(self, callback):
54 self.callback_wrapper = callback
55
56 @java_method('(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V')
57 def callback(self, func, params, id):
58 self.callback_wrapper(func, params, id)
59
60
61 class RequestInterceptor(PythonJavaClass):
62 """
63 Represents a class for intercepting web view requests and responses.
64
65 This class provides a mechanism to handle events during web view
66 requests and responses by allowing custom callback functions to be
67 invoked. It acts as a bridge between Python and Java for facilitating
68 interception of web-related actions within a web view environment.
69
70 Attributes:
71 on_request (Callable[[str, str, str], str]): Callback function that gets
72 invoked when a web request is intercepted. The callback function should
73 accept the request URL, HTTP method, and headers in JSON format, and it
74 should return a string.
75 on_response (Callable[[str, int, str], None]): Callback function that gets
76 invoked when a web response is intercepted. The callback function should
77 accept the response URL, HTTP status code, and headers in JSON format.
78
79 """
80
81 __javacontext__ = 'app'
82 __javainterfaces__ = ['com/pywebview/WebViewRequestInterceptor']
83
84 def __init__(self, on_request, on_response):
85 self.on_request = on_request
86 self.on_response = on_response
87
88 @java_method('(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;')
89 def onRequest(self, url: str, method: str, headers_json: str):
90 return self.on_request(url, method, headers_json)
91
92 @java_method('(Ljava/lang/String;ILjava/lang/String;)V')
93 def onResponse(self, url: str, status_code: int, headers_json: str):
94 self.on_response(url, status_code, headers_json)