github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/proxy/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package proxy 5 6 import ( 7 "github.com/primecitizens/pcz/std/core/abi" 8 "github.com/primecitizens/pcz/std/core/mark" 9 "github.com/primecitizens/pcz/std/ffi/js" 10 "github.com/primecitizens/pcz/std/plat/js/webext/proxy/bindings" 11 "github.com/primecitizens/pcz/std/plat/js/webext/types" 12 ) 13 14 type Mode uint32 15 16 const ( 17 _ Mode = iota 18 19 Mode_DIRECT 20 Mode_AUTO_DETECT 21 Mode_PAC_SCRIPT 22 Mode_FIXED_SERVERS 23 Mode_SYSTEM 24 ) 25 26 func (Mode) FromRef(str js.Ref) Mode { 27 return Mode(bindings.ConstOfMode(str)) 28 } 29 30 func (x Mode) String() (string, bool) { 31 switch x { 32 case Mode_DIRECT: 33 return "direct", true 34 case Mode_AUTO_DETECT: 35 return "auto_detect", true 36 case Mode_PAC_SCRIPT: 37 return "pac_script", true 38 case Mode_FIXED_SERVERS: 39 return "fixed_servers", true 40 case Mode_SYSTEM: 41 return "system", true 42 default: 43 return "", false 44 } 45 } 46 47 type OnProxyErrorArgDetails struct { 48 // Details is "OnProxyErrorArgDetails.details" 49 // 50 // Required 51 Details js.String 52 // Error is "OnProxyErrorArgDetails.error" 53 // 54 // Required 55 Error js.String 56 // Fatal is "OnProxyErrorArgDetails.fatal" 57 // 58 // Required 59 Fatal bool 60 61 FFI_USE bool 62 } 63 64 // FromRef calls UpdateFrom and returns a OnProxyErrorArgDetails with all fields set. 65 func (p OnProxyErrorArgDetails) FromRef(ref js.Ref) OnProxyErrorArgDetails { 66 p.UpdateFrom(ref) 67 return p 68 } 69 70 // New creates a new OnProxyErrorArgDetails in the application heap. 71 func (p OnProxyErrorArgDetails) New() js.Ref { 72 return bindings.OnProxyErrorArgDetailsJSLoad( 73 js.Pointer(&p), js.True, 0, 74 ) 75 } 76 77 // UpdateFrom copies value of all fields of the heap object to p. 78 func (p *OnProxyErrorArgDetails) UpdateFrom(ref js.Ref) { 79 bindings.OnProxyErrorArgDetailsJSStore( 80 js.Pointer(p), ref, 81 ) 82 } 83 84 // Update writes all fields of the p to the heap object referenced by ref. 85 func (p *OnProxyErrorArgDetails) Update(ref js.Ref) { 86 bindings.OnProxyErrorArgDetailsJSLoad( 87 js.Pointer(p), js.False, ref, 88 ) 89 } 90 91 // FreeMembers frees fields with heap reference, if recursive is true 92 // free all heap references reachable from p. 93 func (p *OnProxyErrorArgDetails) FreeMembers(recursive bool) { 94 js.Free( 95 p.Details.Ref(), 96 p.Error.Ref(), 97 ) 98 p.Details = p.Details.FromRef(js.Undefined) 99 p.Error = p.Error.FromRef(js.Undefined) 100 } 101 102 type PacScript struct { 103 // Data is "PacScript.data" 104 // 105 // Optional 106 Data js.String 107 // Mandatory is "PacScript.mandatory" 108 // 109 // Optional 110 // 111 // NOTE: FFI_USE_Mandatory MUST be set to true to make this field effective. 112 Mandatory bool 113 // Url is "PacScript.url" 114 // 115 // Optional 116 Url js.String 117 118 FFI_USE_Mandatory bool // for Mandatory. 119 120 FFI_USE bool 121 } 122 123 // FromRef calls UpdateFrom and returns a PacScript with all fields set. 124 func (p PacScript) FromRef(ref js.Ref) PacScript { 125 p.UpdateFrom(ref) 126 return p 127 } 128 129 // New creates a new PacScript in the application heap. 130 func (p PacScript) New() js.Ref { 131 return bindings.PacScriptJSLoad( 132 js.Pointer(&p), js.True, 0, 133 ) 134 } 135 136 // UpdateFrom copies value of all fields of the heap object to p. 137 func (p *PacScript) UpdateFrom(ref js.Ref) { 138 bindings.PacScriptJSStore( 139 js.Pointer(p), ref, 140 ) 141 } 142 143 // Update writes all fields of the p to the heap object referenced by ref. 144 func (p *PacScript) Update(ref js.Ref) { 145 bindings.PacScriptJSLoad( 146 js.Pointer(p), js.False, ref, 147 ) 148 } 149 150 // FreeMembers frees fields with heap reference, if recursive is true 151 // free all heap references reachable from p. 152 func (p *PacScript) FreeMembers(recursive bool) { 153 js.Free( 154 p.Data.Ref(), 155 p.Url.Ref(), 156 ) 157 p.Data = p.Data.FromRef(js.Undefined) 158 p.Url = p.Url.FromRef(js.Undefined) 159 } 160 161 type Scheme uint32 162 163 const ( 164 _ Scheme = iota 165 166 Scheme_HTTP 167 Scheme_HTTPS 168 Scheme_QUIC 169 Scheme_SOCKS4 170 Scheme_SOCKS5 171 ) 172 173 func (Scheme) FromRef(str js.Ref) Scheme { 174 return Scheme(bindings.ConstOfScheme(str)) 175 } 176 177 func (x Scheme) String() (string, bool) { 178 switch x { 179 case Scheme_HTTP: 180 return "http", true 181 case Scheme_HTTPS: 182 return "https", true 183 case Scheme_QUIC: 184 return "quic", true 185 case Scheme_SOCKS4: 186 return "socks4", true 187 case Scheme_SOCKS5: 188 return "socks5", true 189 default: 190 return "", false 191 } 192 } 193 194 type ProxyServer struct { 195 // Host is "ProxyServer.host" 196 // 197 // Required 198 Host js.String 199 // Port is "ProxyServer.port" 200 // 201 // Optional 202 // 203 // NOTE: FFI_USE_Port MUST be set to true to make this field effective. 204 Port int64 205 // Scheme is "ProxyServer.scheme" 206 // 207 // Optional 208 Scheme Scheme 209 210 FFI_USE_Port bool // for Port. 211 212 FFI_USE bool 213 } 214 215 // FromRef calls UpdateFrom and returns a ProxyServer with all fields set. 216 func (p ProxyServer) FromRef(ref js.Ref) ProxyServer { 217 p.UpdateFrom(ref) 218 return p 219 } 220 221 // New creates a new ProxyServer in the application heap. 222 func (p ProxyServer) New() js.Ref { 223 return bindings.ProxyServerJSLoad( 224 js.Pointer(&p), js.True, 0, 225 ) 226 } 227 228 // UpdateFrom copies value of all fields of the heap object to p. 229 func (p *ProxyServer) UpdateFrom(ref js.Ref) { 230 bindings.ProxyServerJSStore( 231 js.Pointer(p), ref, 232 ) 233 } 234 235 // Update writes all fields of the p to the heap object referenced by ref. 236 func (p *ProxyServer) Update(ref js.Ref) { 237 bindings.ProxyServerJSLoad( 238 js.Pointer(p), js.False, ref, 239 ) 240 } 241 242 // FreeMembers frees fields with heap reference, if recursive is true 243 // free all heap references reachable from p. 244 func (p *ProxyServer) FreeMembers(recursive bool) { 245 js.Free( 246 p.Host.Ref(), 247 ) 248 p.Host = p.Host.FromRef(js.Undefined) 249 } 250 251 type ProxyRules struct { 252 // BypassList is "ProxyRules.bypassList" 253 // 254 // Optional 255 BypassList js.Array[js.String] 256 // FallbackProxy is "ProxyRules.fallbackProxy" 257 // 258 // Optional 259 // 260 // NOTE: FallbackProxy.FFI_USE MUST be set to true to get FallbackProxy used. 261 FallbackProxy ProxyServer 262 // ProxyForFtp is "ProxyRules.proxyForFtp" 263 // 264 // Optional 265 // 266 // NOTE: ProxyForFtp.FFI_USE MUST be set to true to get ProxyForFtp used. 267 ProxyForFtp ProxyServer 268 // ProxyForHttp is "ProxyRules.proxyForHttp" 269 // 270 // Optional 271 // 272 // NOTE: ProxyForHttp.FFI_USE MUST be set to true to get ProxyForHttp used. 273 ProxyForHttp ProxyServer 274 // ProxyForHttps is "ProxyRules.proxyForHttps" 275 // 276 // Optional 277 // 278 // NOTE: ProxyForHttps.FFI_USE MUST be set to true to get ProxyForHttps used. 279 ProxyForHttps ProxyServer 280 // SingleProxy is "ProxyRules.singleProxy" 281 // 282 // Optional 283 // 284 // NOTE: SingleProxy.FFI_USE MUST be set to true to get SingleProxy used. 285 SingleProxy ProxyServer 286 287 FFI_USE bool 288 } 289 290 // FromRef calls UpdateFrom and returns a ProxyRules with all fields set. 291 func (p ProxyRules) FromRef(ref js.Ref) ProxyRules { 292 p.UpdateFrom(ref) 293 return p 294 } 295 296 // New creates a new ProxyRules in the application heap. 297 func (p ProxyRules) New() js.Ref { 298 return bindings.ProxyRulesJSLoad( 299 js.Pointer(&p), js.True, 0, 300 ) 301 } 302 303 // UpdateFrom copies value of all fields of the heap object to p. 304 func (p *ProxyRules) UpdateFrom(ref js.Ref) { 305 bindings.ProxyRulesJSStore( 306 js.Pointer(p), ref, 307 ) 308 } 309 310 // Update writes all fields of the p to the heap object referenced by ref. 311 func (p *ProxyRules) Update(ref js.Ref) { 312 bindings.ProxyRulesJSLoad( 313 js.Pointer(p), js.False, ref, 314 ) 315 } 316 317 // FreeMembers frees fields with heap reference, if recursive is true 318 // free all heap references reachable from p. 319 func (p *ProxyRules) FreeMembers(recursive bool) { 320 js.Free( 321 p.BypassList.Ref(), 322 ) 323 p.BypassList = p.BypassList.FromRef(js.Undefined) 324 if recursive { 325 p.FallbackProxy.FreeMembers(true) 326 p.ProxyForFtp.FreeMembers(true) 327 p.ProxyForHttp.FreeMembers(true) 328 p.ProxyForHttps.FreeMembers(true) 329 p.SingleProxy.FreeMembers(true) 330 } 331 } 332 333 type ProxyConfig struct { 334 // Mode is "ProxyConfig.mode" 335 // 336 // Required 337 Mode Mode 338 // PacScript is "ProxyConfig.pacScript" 339 // 340 // Optional 341 // 342 // NOTE: PacScript.FFI_USE MUST be set to true to get PacScript used. 343 PacScript PacScript 344 // Rules is "ProxyConfig.rules" 345 // 346 // Optional 347 // 348 // NOTE: Rules.FFI_USE MUST be set to true to get Rules used. 349 Rules ProxyRules 350 351 FFI_USE bool 352 } 353 354 // FromRef calls UpdateFrom and returns a ProxyConfig with all fields set. 355 func (p ProxyConfig) FromRef(ref js.Ref) ProxyConfig { 356 p.UpdateFrom(ref) 357 return p 358 } 359 360 // New creates a new ProxyConfig in the application heap. 361 func (p ProxyConfig) New() js.Ref { 362 return bindings.ProxyConfigJSLoad( 363 js.Pointer(&p), js.True, 0, 364 ) 365 } 366 367 // UpdateFrom copies value of all fields of the heap object to p. 368 func (p *ProxyConfig) UpdateFrom(ref js.Ref) { 369 bindings.ProxyConfigJSStore( 370 js.Pointer(p), ref, 371 ) 372 } 373 374 // Update writes all fields of the p to the heap object referenced by ref. 375 func (p *ProxyConfig) Update(ref js.Ref) { 376 bindings.ProxyConfigJSLoad( 377 js.Pointer(p), js.False, ref, 378 ) 379 } 380 381 // FreeMembers frees fields with heap reference, if recursive is true 382 // free all heap references reachable from p. 383 func (p *ProxyConfig) FreeMembers(recursive bool) { 384 if recursive { 385 p.PacScript.FreeMembers(true) 386 p.Rules.FreeMembers(true) 387 } 388 } 389 390 type OnProxyErrorEventCallbackFunc func(this js.Ref, details *OnProxyErrorArgDetails) js.Ref 391 392 func (fn OnProxyErrorEventCallbackFunc) Register() js.Func[func(details *OnProxyErrorArgDetails)] { 393 return js.RegisterCallback[func(details *OnProxyErrorArgDetails)]( 394 fn, abi.FuncPCABIInternal(fn), 395 ) 396 } 397 398 func (fn OnProxyErrorEventCallbackFunc) DispatchCallback( 399 targetPC uintptr, ctx *js.CallbackContext, 400 ) { 401 args := ctx.Args() 402 if len(args) != 1+1 /* js this */ || 403 targetPC != uintptr(abi.FuncPCABIInternal(fn)) { 404 js.ThrowInvalidCallbackInvocation() 405 } 406 var arg0 OnProxyErrorArgDetails 407 arg0.UpdateFrom(args[0+1]) 408 defer arg0.FreeMembers(true) 409 410 if ctx.Return(fn( 411 args[0], 412 413 mark.NoEscape(&arg0), 414 )) { 415 return 416 } 417 418 js.ThrowCallbackValueNotReturned() 419 } 420 421 type OnProxyErrorEventCallback[T any] struct { 422 Fn func(arg T, this js.Ref, details *OnProxyErrorArgDetails) js.Ref 423 Arg T 424 } 425 426 func (cb *OnProxyErrorEventCallback[T]) Register() js.Func[func(details *OnProxyErrorArgDetails)] { 427 return js.RegisterCallback[func(details *OnProxyErrorArgDetails)]( 428 cb, abi.FuncPCABIInternal(cb.Fn), 429 ) 430 } 431 432 func (cb *OnProxyErrorEventCallback[T]) DispatchCallback( 433 targetPC uintptr, ctx *js.CallbackContext, 434 ) { 435 args := ctx.Args() 436 if len(args) != 1+1 /* js this */ || 437 targetPC != uintptr(abi.FuncPCABIInternal(cb.Fn)) { 438 js.ThrowInvalidCallbackInvocation() 439 } 440 var arg0 OnProxyErrorArgDetails 441 arg0.UpdateFrom(args[0+1]) 442 defer arg0.FreeMembers(true) 443 444 if ctx.Return(cb.Fn( 445 cb.Arg, 446 args[0], 447 448 mark.NoEscape(&arg0), 449 )) { 450 return 451 } 452 453 js.ThrowCallbackValueNotReturned() 454 } 455 456 // HasFuncOnProxyError returns true if the function "WEBEXT.proxy.onProxyError.addListener" exists. 457 func HasFuncOnProxyError() bool { 458 return js.True == bindings.HasFuncOnProxyError() 459 } 460 461 // FuncOnProxyError returns the function "WEBEXT.proxy.onProxyError.addListener". 462 func FuncOnProxyError() (fn js.Func[func(callback js.Func[func(details *OnProxyErrorArgDetails)])]) { 463 bindings.FuncOnProxyError( 464 js.Pointer(&fn), 465 ) 466 return 467 } 468 469 // OnProxyError calls the function "WEBEXT.proxy.onProxyError.addListener" directly. 470 func OnProxyError(callback js.Func[func(details *OnProxyErrorArgDetails)]) (ret js.Void) { 471 bindings.CallOnProxyError( 472 js.Pointer(&ret), 473 callback.Ref(), 474 ) 475 476 return 477 } 478 479 // TryOnProxyError calls the function "WEBEXT.proxy.onProxyError.addListener" 480 // in a try/catch block and returns (_, err, ok = false) when it went through 481 // the catch clause. 482 func TryOnProxyError(callback js.Func[func(details *OnProxyErrorArgDetails)]) (ret js.Void, exception js.Any, ok bool) { 483 ok = js.True == bindings.TryOnProxyError( 484 js.Pointer(&ret), js.Pointer(&exception), 485 callback.Ref(), 486 ) 487 488 return 489 } 490 491 // HasFuncOffProxyError returns true if the function "WEBEXT.proxy.onProxyError.removeListener" exists. 492 func HasFuncOffProxyError() bool { 493 return js.True == bindings.HasFuncOffProxyError() 494 } 495 496 // FuncOffProxyError returns the function "WEBEXT.proxy.onProxyError.removeListener". 497 func FuncOffProxyError() (fn js.Func[func(callback js.Func[func(details *OnProxyErrorArgDetails)])]) { 498 bindings.FuncOffProxyError( 499 js.Pointer(&fn), 500 ) 501 return 502 } 503 504 // OffProxyError calls the function "WEBEXT.proxy.onProxyError.removeListener" directly. 505 func OffProxyError(callback js.Func[func(details *OnProxyErrorArgDetails)]) (ret js.Void) { 506 bindings.CallOffProxyError( 507 js.Pointer(&ret), 508 callback.Ref(), 509 ) 510 511 return 512 } 513 514 // TryOffProxyError calls the function "WEBEXT.proxy.onProxyError.removeListener" 515 // in a try/catch block and returns (_, err, ok = false) when it went through 516 // the catch clause. 517 func TryOffProxyError(callback js.Func[func(details *OnProxyErrorArgDetails)]) (ret js.Void, exception js.Any, ok bool) { 518 ok = js.True == bindings.TryOffProxyError( 519 js.Pointer(&ret), js.Pointer(&exception), 520 callback.Ref(), 521 ) 522 523 return 524 } 525 526 // HasFuncHasOnProxyError returns true if the function "WEBEXT.proxy.onProxyError.hasListener" exists. 527 func HasFuncHasOnProxyError() bool { 528 return js.True == bindings.HasFuncHasOnProxyError() 529 } 530 531 // FuncHasOnProxyError returns the function "WEBEXT.proxy.onProxyError.hasListener". 532 func FuncHasOnProxyError() (fn js.Func[func(callback js.Func[func(details *OnProxyErrorArgDetails)]) bool]) { 533 bindings.FuncHasOnProxyError( 534 js.Pointer(&fn), 535 ) 536 return 537 } 538 539 // HasOnProxyError calls the function "WEBEXT.proxy.onProxyError.hasListener" directly. 540 func HasOnProxyError(callback js.Func[func(details *OnProxyErrorArgDetails)]) (ret bool) { 541 bindings.CallHasOnProxyError( 542 js.Pointer(&ret), 543 callback.Ref(), 544 ) 545 546 return 547 } 548 549 // TryHasOnProxyError calls the function "WEBEXT.proxy.onProxyError.hasListener" 550 // in a try/catch block and returns (_, err, ok = false) when it went through 551 // the catch clause. 552 func TryHasOnProxyError(callback js.Func[func(details *OnProxyErrorArgDetails)]) (ret bool, exception js.Any, ok bool) { 553 ok = js.True == bindings.TryHasOnProxyError( 554 js.Pointer(&ret), js.Pointer(&exception), 555 callback.Ref(), 556 ) 557 558 return 559 } 560 561 // Settings returns the value of property "WEBEXT.proxy.settings". 562 // 563 // The returned bool will be false if there is no such property. 564 func Settings() (ret types.ChromeSetting, ok bool) { 565 ok = js.True == bindings.GetSettings( 566 js.Pointer(&ret), 567 ) 568 569 return 570 } 571 572 // SetSettings sets the value of property "WEBEXT.proxy.settings" to val. 573 // 574 // It returns false if the property cannot be set. 575 func SetSettings(val types.ChromeSetting) bool { 576 return js.True == bindings.SetSettings( 577 val.Ref()) 578 }