github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/webrequestinternal/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package webrequestinternal 5 6 import ( 7 "github.com/primecitizens/pcz/std/core/abi" 8 "github.com/primecitizens/pcz/std/ffi/js" 9 "github.com/primecitizens/pcz/std/plat/js/webext/webrequest" 10 "github.com/primecitizens/pcz/std/plat/js/webext/webrequestinternal/bindings" 11 ) 12 13 type AddEventListenerArgCallbackFunc func(this js.Ref) js.Ref 14 15 func (fn AddEventListenerArgCallbackFunc) Register() js.Func[func()] { 16 return js.RegisterCallback[func()]( 17 fn, abi.FuncPCABIInternal(fn), 18 ) 19 } 20 21 func (fn AddEventListenerArgCallbackFunc) DispatchCallback( 22 targetPC uintptr, ctx *js.CallbackContext, 23 ) { 24 args := ctx.Args() 25 if len(args) != 0+1 /* js this */ || 26 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 27 js.ThrowInvalidCallbackInvocation() 28 } 29 30 if ctx.Return(fn( 31 args[0], 32 )) { 33 return 34 } 35 36 js.ThrowCallbackValueNotReturned() 37 } 38 39 type AddEventListenerArgCallback[T any] struct { 40 Fn func(arg T, this js.Ref) js.Ref 41 Arg T 42 } 43 44 func (cb *AddEventListenerArgCallback[T]) Register() js.Func[func()] { 45 return js.RegisterCallback[func()]( 46 cb, abi.FuncPCABIInternal(cb.Fn), 47 ) 48 } 49 50 func (cb *AddEventListenerArgCallback[T]) DispatchCallback( 51 targetPC uintptr, ctx *js.CallbackContext, 52 ) { 53 args := ctx.Args() 54 if len(args) != 0+1 /* js this */ || 55 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 56 js.ThrowInvalidCallbackInvocation() 57 } 58 59 if ctx.Return(cb.Fn( 60 cb.Arg, 61 args[0], 62 )) { 63 return 64 } 65 66 js.ThrowCallbackValueNotReturned() 67 } 68 69 type AddEventListenerOptions uint32 70 71 const ( 72 _ AddEventListenerOptions = iota 73 74 AddEventListenerOptions_REQUEST_HEADERS 75 AddEventListenerOptions_RESPONSE_HEADERS 76 AddEventListenerOptions_BLOCKING 77 AddEventListenerOptions_ASYNC_BLOCKING 78 AddEventListenerOptions_REQUEST_BODY 79 AddEventListenerOptions_EXTRA_HEADERS 80 ) 81 82 func (AddEventListenerOptions) FromRef(str js.Ref) AddEventListenerOptions { 83 return AddEventListenerOptions(bindings.ConstOfAddEventListenerOptions(str)) 84 } 85 86 func (x AddEventListenerOptions) String() (string, bool) { 87 switch x { 88 case AddEventListenerOptions_REQUEST_HEADERS: 89 return "requestHeaders", true 90 case AddEventListenerOptions_RESPONSE_HEADERS: 91 return "responseHeaders", true 92 case AddEventListenerOptions_BLOCKING: 93 return "blocking", true 94 case AddEventListenerOptions_ASYNC_BLOCKING: 95 return "asyncBlocking", true 96 case AddEventListenerOptions_REQUEST_BODY: 97 return "requestBody", true 98 case AddEventListenerOptions_EXTRA_HEADERS: 99 return "extraHeaders", true 100 default: 101 return "", false 102 } 103 } 104 105 // HasFuncAddEventListener returns true if the function "WEBEXT.webRequestInternal.addEventListener" exists. 106 func HasFuncAddEventListener() bool { 107 return js.True == bindings.HasFuncAddEventListener() 108 } 109 110 // FuncAddEventListener returns the function "WEBEXT.webRequestInternal.addEventListener". 111 func FuncAddEventListener() (fn js.Func[func(callback js.Func[func()], filter webrequest.RequestFilter, extraInfoSpec js.Array[AddEventListenerOptions], eventName js.String, subEventName js.String, webViewInstanceId int64)]) { 112 bindings.FuncAddEventListener( 113 js.Pointer(&fn), 114 ) 115 return 116 } 117 118 // AddEventListener calls the function "WEBEXT.webRequestInternal.addEventListener" directly. 119 func AddEventListener(callback js.Func[func()], filter webrequest.RequestFilter, extraInfoSpec js.Array[AddEventListenerOptions], eventName js.String, subEventName js.String, webViewInstanceId int64) (ret js.Void) { 120 bindings.CallAddEventListener( 121 js.Pointer(&ret), 122 callback.Ref(), 123 js.Pointer(&filter), 124 extraInfoSpec.Ref(), 125 eventName.Ref(), 126 subEventName.Ref(), 127 float64(webViewInstanceId), 128 ) 129 130 return 131 } 132 133 // TryAddEventListener calls the function "WEBEXT.webRequestInternal.addEventListener" 134 // in a try/catch block and returns (_, err, ok = false) when it went through 135 // the catch clause. 136 func TryAddEventListener(callback js.Func[func()], filter webrequest.RequestFilter, extraInfoSpec js.Array[AddEventListenerOptions], eventName js.String, subEventName js.String, webViewInstanceId int64) (ret js.Void, exception js.Any, ok bool) { 137 ok = js.True == bindings.TryAddEventListener( 138 js.Pointer(&ret), js.Pointer(&exception), 139 callback.Ref(), 140 js.Pointer(&filter), 141 extraInfoSpec.Ref(), 142 eventName.Ref(), 143 subEventName.Ref(), 144 float64(webViewInstanceId), 145 ) 146 147 return 148 } 149 150 // HasFuncEventHandled returns true if the function "WEBEXT.webRequestInternal.eventHandled" exists. 151 func HasFuncEventHandled() bool { 152 return js.True == bindings.HasFuncEventHandled() 153 } 154 155 // FuncEventHandled returns the function "WEBEXT.webRequestInternal.eventHandled". 156 func FuncEventHandled() (fn js.Func[func(eventName js.String, subEventName js.String, requestId js.String, webViewInstanceId int64, response webrequest.BlockingResponse)]) { 157 bindings.FuncEventHandled( 158 js.Pointer(&fn), 159 ) 160 return 161 } 162 163 // EventHandled calls the function "WEBEXT.webRequestInternal.eventHandled" directly. 164 func EventHandled(eventName js.String, subEventName js.String, requestId js.String, webViewInstanceId int64, response webrequest.BlockingResponse) (ret js.Void) { 165 bindings.CallEventHandled( 166 js.Pointer(&ret), 167 eventName.Ref(), 168 subEventName.Ref(), 169 requestId.Ref(), 170 float64(webViewInstanceId), 171 js.Pointer(&response), 172 ) 173 174 return 175 } 176 177 // TryEventHandled calls the function "WEBEXT.webRequestInternal.eventHandled" 178 // in a try/catch block and returns (_, err, ok = false) when it went through 179 // the catch clause. 180 func TryEventHandled(eventName js.String, subEventName js.String, requestId js.String, webViewInstanceId int64, response webrequest.BlockingResponse) (ret js.Void, exception js.Any, ok bool) { 181 ok = js.True == bindings.TryEventHandled( 182 js.Pointer(&ret), js.Pointer(&exception), 183 eventName.Ref(), 184 subEventName.Ref(), 185 requestId.Ref(), 186 float64(webViewInstanceId), 187 js.Pointer(&response), 188 ) 189 190 return 191 }