|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace ElectronNET.API |
| 7 | +{ |
| 8 | + |
| 9 | + public sealed class Protocol : ApiBase |
| 10 | + { |
| 11 | + protected override SocketTaskEventNameTypes SocketTaskEventNameType => SocketTaskEventNameTypes.DashesLowerFirst; |
| 12 | + protected override SocketTaskMessageNameTypes SocketTaskMessageNameType => SocketTaskMessageNameTypes.DashesLowerFirst; |
| 13 | + |
| 14 | + internal Protocol() |
| 15 | + { |
| 16 | + } |
| 17 | + |
| 18 | + internal static Protocol Instance |
| 19 | + { |
| 20 | + get |
| 21 | + { |
| 22 | + if (_protocol == null) |
| 23 | + { |
| 24 | + lock (_syncRoot) |
| 25 | + { |
| 26 | + _protocol ??= new Protocol(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return _protocol; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private static Protocol _protocol; |
| 35 | + |
| 36 | + private static readonly object _syncRoot = new(); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Registers the scheme as standard, secure, bypasses content security policy for resources, allows registering ServiceWorker, supports fetch API, streaming video/audio, and V8 code cache. |
| 40 | + /// Specify a privilege with the value of true to enable the capability. |
| 41 | + /// </summary> |
| 42 | + /// <param name="customSchemes">Custom schemes to be registered with options.</param> |
| 43 | + /// <remarks>This method can only be used before the <see cref="App.Ready"/> event of the app module gets emitted and can be called only once.</remarks> |
| 44 | + public Task RegisterSchemesAsPrivilegedAsync(params CustomScheme[] customSchemes) |
| 45 | + { |
| 46 | + var tsc = new TaskCompletionSource(); |
| 47 | + |
| 48 | + BridgeConnector.Socket.Once("registerSchemesAsPrivilegedComplete", tsc.SetResult); |
| 49 | + BridgeConnector.Socket.Emit("registerSchemesAsPrivileged", customSchemes); |
| 50 | + |
| 51 | + return tsc.Task; |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Register a protocol handler for scheme. Requests made to URLs with this scheme will delegate to this handler to determine what response should be sent. |
| 56 | + /// </summary> |
| 57 | + /// <param name="scheme">scheme to handle, for example https or my-app. This is the bit before the : in a URL.</param> |
| 58 | + /// <param name="handler">Either a <see cref="Response" /> or a <see cref="Task{Response}"/> can be returned.</param> |
| 59 | + public void Handle(string scheme, Func<Request, Response> handler) |
| 60 | + { |
| 61 | + if (string.IsNullOrWhiteSpace(scheme)) |
| 62 | + throw new ArgumentException("Scheme must not be null or empty.", nameof(scheme)); |
| 63 | + |
| 64 | + if (handler == null) |
| 65 | + throw new ArgumentNullException(nameof(handler)); |
| 66 | + |
| 67 | + Handle(scheme, req => Task.FromResult(handler(req))); |
| 68 | + } |
| 69 | + |
| 70 | + public void Handle(string scheme, Func<Request, Task<Response>> handler) |
| 71 | + { |
| 72 | + if (string.IsNullOrWhiteSpace(scheme)) |
| 73 | + throw new ArgumentException("Scheme must not be null or empty.", nameof(scheme)); |
| 74 | + |
| 75 | + if (handler == null) |
| 76 | + throw new ArgumentNullException(nameof(handler)); |
| 77 | + |
| 78 | + var tsc = new TaskCompletionSource(); |
| 79 | + |
| 80 | + // Tell TS to register protocol.handle for this scheme. |
| 81 | + BridgeConnector.Socket.Emit("protocol-handle-register", new |
| 82 | + { |
| 83 | + scheme |
| 84 | + }); |
| 85 | + |
| 86 | + // Listen for incoming requests from TS |
| 87 | + BridgeConnector.Socket.On<Request>("protocol-handle-request", async (request) => |
| 88 | + { |
| 89 | + try |
| 90 | + { |
| 91 | + if (request == null || !string.Equals(request.Scheme, scheme, StringComparison.OrdinalIgnoreCase)) |
| 92 | + return; // Not our scheme, ignore. |
| 93 | + |
| 94 | + var response = await handler(request).ConfigureAwait(false) ?? new Response |
| 95 | + { |
| 96 | + Status = 204 |
| 97 | + }; |
| 98 | + |
| 99 | + // Ensure headers dictionary exists |
| 100 | + response.Headers ??= new(); |
| 101 | + |
| 102 | + // Push ContentType also as header, for TS convenience |
| 103 | + if (!string.IsNullOrEmpty(response.ContentType)) |
| 104 | + { |
| 105 | + response.Headers["content-type"] = new[] { response.ContentType! }; |
| 106 | + } |
| 107 | + |
| 108 | + BridgeConnector.Socket.Emit("protocol-handle-response", new |
| 109 | + { |
| 110 | + id = request.Id, |
| 111 | + status = response.Status, |
| 112 | + headers = response.Headers, |
| 113 | + body = response.Body != null |
| 114 | + ? Convert.ToBase64String(response.Body) |
| 115 | + : null |
| 116 | + }); |
| 117 | + } |
| 118 | + catch (Exception ex) |
| 119 | + { |
| 120 | + // In case of error, send a 500 back |
| 121 | + var errorBody = Encoding.UTF8.GetBytes("Protocol handler error:\n" + ex); |
| 122 | + |
| 123 | + BridgeConnector.Socket.Emit("protocol-handle-response", new |
| 124 | + { |
| 125 | + id = request.Id, |
| 126 | + status = 500, |
| 127 | + headers = new |
| 128 | + { |
| 129 | + // minimal header set |
| 130 | + contentType = new[] { "text/plain; charset=utf-8" } |
| 131 | + }, |
| 132 | + body = Convert.ToBase64String(errorBody) |
| 133 | + }); |
| 134 | + } |
| 135 | + }); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public sealed class Request |
| 140 | + { |
| 141 | + public string Id { get; set; } = default!; |
| 142 | + public string Scheme { get; set; } = default!; |
| 143 | + public string Url { get; set; } = default!; |
| 144 | + public string Method { get; set; } = "GET"; |
| 145 | + |
| 146 | + // Case-insensitive header name, multiple values per header. |
| 147 | + public Dictionary<string, string[]> Headers { get; set; } = new(StringComparer.OrdinalIgnoreCase); |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Request body as raw bytes, or null if none. |
| 151 | + /// </summary> |
| 152 | + public byte[] Body { get; set; } |
| 153 | + } |
| 154 | + |
| 155 | + public sealed class Response |
| 156 | + { |
| 157 | + /// <summary> |
| 158 | + /// HTTP-like status code. Default 200. |
| 159 | + /// </summary> |
| 160 | + public int Status { get; set; } = 200; |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Content-Type header value; convenience property. |
| 164 | + /// </summary> |
| 165 | + public string ContentType { get; set; } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Response headers (without Content-Type). |
| 169 | + /// </summary> |
| 170 | + public Dictionary<string, string[]> Headers { get; set; } = new(StringComparer.OrdinalIgnoreCase); |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Raw response body (may be null for no body). |
| 174 | + /// </summary> |
| 175 | + public byte[] Body { get; set; } |
| 176 | + } |
| 177 | + |
| 178 | + public sealed class CustomScheme |
| 179 | + { |
| 180 | + public string Scheme { get; set; } |
| 181 | + public CustomSchemePrivileges Privileges { get; set; } |
| 182 | + |
| 183 | + } |
| 184 | + public sealed class CustomSchemePrivileges |
| 185 | + { |
| 186 | + public bool? Standard { get; set; } |
| 187 | + public bool? Secure { get; set; } |
| 188 | + public bool? BypassCSP { get; set; } |
| 189 | + public bool? AllowServiceWorkers { get; set; } |
| 190 | + public bool? SupportFetchAPI { get; set; } |
| 191 | + public bool? CorsEnabled { get; set; } |
| 192 | + public bool? Stream { get; set; } |
| 193 | + public bool? CodeCache { get; set; } |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments