@@ -1,6 +1,11 @@ 1 1 const std = @import("std"); 2 + const bridge_error = @import("bridge_error.zig"); 3 + const desktop_bridge_envelope = @import("desktop_bridge_envelope.zig"); 2 4 const desktop_window_registry = @import("desktop_window_registry.zig"); 5 + const json_utils = @import("json_utils.zig"); 6 + const request_context = @import("request_context.zig"); 3 7 const window_context = @import("window_context.zig"); 8 + const window_registry = @import("window_registry.zig"); 4 9 const window_reply_target = @import("window_reply_target.zig"); 5 10 6 11 // Linux implementation using GTK3 and WebKit2GTK 4.1. ⋯ 22 lines Show the 22 lines above this hunk @@ -29,6 +34,7 @@ pub extern "c" fn gtk_window_set_position(window: *anyopaque, x: c_int, y: c_int 29 34 pub extern "c" fn webkit_web_view_new() *anyopaque; 30 35 pub extern "c" fn webkit_web_view_load_uri(webview: *anyopaque, uri: [*:0]const u8) void; 31 36 pub extern "c" fn webkit_web_view_load_html(webview: *anyopaque, html: [*:0]const u8, base_uri: [*:0]const u8) void; 37 + pub extern "c" fn webkit_web_view_reload(webview: *anyopaque) void; 32 38 pub extern "c" fn webkit_web_view_get_settings(webview: *anyopaque) *anyopaque; 33 39 pub extern "c" fn webkit_settings_set_enable_developer_extras(settings: *anyopaque, enabled: c_int) void; 34 40 pub extern "c" fn webkit_settings_set_enable_webgl(settings: *anyopaque, enabled: c_int) void; ⋯ 44 lines Show the 44 lines above this hunk @@ -79,6 +85,9 @@ pub extern "c" fn webkit_user_script_new( 79 85 ) *anyopaque; 80 86 pub extern "c" fn webkit_user_content_manager_add_script(manager: *anyopaque, script: *anyopaque) void; 81 87 pub extern "c" fn webkit_user_content_manager_remove_all_scripts(manager: *anyopaque) void; 88 + pub extern "c" fn webkit_user_content_manager_register_script_message_handler(manager: *anyopaque, name: [*:0]const u8) c_int; 89 + pub extern "c" fn webkit_javascript_result_get_js_value(result: *anyopaque) *anyopaque; 90 + pub extern "c" fn jsc_value_to_json(value: *anyopaque, indent: c_uint) ?[*:0]u8; 82 91 83 92 pub extern "c" fn gtk_container_add(container: *anyopaque, widget: *anyopaque) void; 84 93 ⋯ 135 lines Show the 135 lines above this hunk @@ -220,7 +229,175 @@ fn registerWindow(gtk_window: *anyopaque, webview: *anyopaque) ?u32 { 220 229 /// Forget the handle at actual teardown, so neither path leaves a stale 221 230 /// webview eligible for bridge replies. 222 231 fn onWindowDestroyed(widget: *anyopaque, _: ?*anyopaque) callconv(.c) void { 223 - _ = desktop_windows.forgetWindow(@intFromPtr(widget)); 232 + const entry = desktop_windows.forgetWindow(@intFromPtr(widget)) orelse return ; 233 + window_registry.forgetOwner(entry.webview); 234 + window_registry.forget(entry.window); 235 + } 236 + 237 + /// WebKit supplies the WebView via signal user data; a page-supplied window id 238 + /// is only a name to resolve after the native sender has been authenticated. 239 + fn onScriptMessage(_: *anyopaque, result: *anyopaque, user_data: ?*anyopaque) callconv(.c) void { 240 + const sender = user_data orelse return; 241 + const entry = desktop_windows.byWebview(@intFromPtr(sender)) orelse return; 242 + const value = webkit_javascript_result_get_js_value(result); 243 + const json_z = jsc_value_to_json(value, 0) orelse return; 244 + defer g_free(@ptrCast(json_z)); 245 + 246 + var envelope = desktop_bridge_envelope.parse(std.heap.c_allocator, std.mem.span(json_z)) catch return; 247 + defer envelope.deinit(); 248 + window_context.push(entry.window, entry.webview); 249 + defer window_context.pop(); 250 + request_context.push(envelope.request_id); 251 + defer request_context.pop(); 252 + 253 + if (!std.mem.eql(u8, envelope.kind, "window")) { 254 + bridge_error.sendErrorToJS(std.heap.c_allocator, envelope.action, error.PlatformNotSupported); 255 + return; 256 + } 257 + handleWindowAction(envelope.action, envelope.data) catch |err| { 258 + bridge_error.sendErrorToJS(std.heap.c_allocator, envelope.action, bridge_error.fromHandlerError(err)); 259 + }; 260 + } 261 + 262 + fn namedWindowResult(allocator: std.mem.Allocator, name: []const u8) ![]u8 { 263 + var json: std.ArrayListUnmanaged(u8) = .empty; 264 + errdefer json.deinit(allocator); 265 + try json.appendSlice(allocator, "{\"name\":\""); 266 + try bridge_error.appendJsonEscaped(allocator, &json, name); 267 + try json.appendSlice(allocator, "\"}"); 268 + return json.toOwnedSlice(allocator); 269 + } 270 + 271 + fn openNamedWindow(action: []const u8, data: ?[]const u8) !void { 272 + const allocator = std.heap.c_allocator; 273 + const json = data orelse return error.MissingData; 274 + const explicit_name = try json_utils.getStringDecoded(allocator, json, "name"); 275 + defer if (explicit_name) |name| allocator.free(name); 276 + const fallback_id = if (explicit_name == null) 277 + try json_utils.getStringDecoded(allocator, json, "id") 278 + else 279 + null; 280 + defer if (fallback_id) |name| allocator.free(name); 281 + const name = explicit_name orelse fallback_id orelse return error.InvalidParameter; 282 + if (name.len == 0 or name.len > window_registry.max_name or 283 + std.mem.eql(u8, name, "main") or std.mem.indexOfScalar(u8, name, 0) != null) 284 + return error.InvalidParameter; 285 + 286 + const url = try json_utils.getStringDecoded(allocator, json, "url"); 287 + defer if (url) |text| allocator.free(text); 288 + const html = try json_utils.getStringDecoded(allocator, json, "html"); 289 + defer if (html) |text| allocator.free(text); 290 + if (url == null and html == null) return error.InvalidParameter; 291 + const title = try json_utils.getStringDecoded(allocator, json, "title"); 292 + defer if (title) |text| allocator.free(text); 293 + for ([_]?[]const u8{ url, html, title }) |value| { 294 + if (value) |text| if (std.mem.indexOfScalar(u8, text, 0) != null) 295 + return error.InvalidParameter; 296 + } 297 + 298 + // Allocate the response before creating native state. A failed allocation 299 + // must not leave a live window behind a rejected creation Promise. 300 + const result = try namedWindowResult(allocator, name); 301 + defer allocator.free(result); 302 + const owner = window_context.currentWebView() orelse return error.WebViewHandleNotSet; 303 + if (window_registry.byName(name)) |existing| { 304 + if (desktop_windows.byWindow(existing) == null) return error.WindowHandleNotSet; 305 + if (!window_registry.rememberNamedOwned(existing, name, owner)) return error.InvalidParameter; 306 + gtk_window_present(@ptrFromInt(existing)); 307 + bridge_error.sendResultToJS(allocator, action, result); 308 + return; 309 + } 310 + 311 + const width = json_utils.getInt(u32, json, "width") orelse 800; 312 + const height = json_utils.getInt(u32, json, "height") orelse 600; 313 + if (width == 0 or height == 0 or width > @as(u32, std.math.maxInt(c_int)) or height > @as(u32, std.math.maxInt(c_int))) 314 + return error.InvalidParameter; 315 + var created = try Window.create(.{ 316 + .title = title orelse name, 317 + .width = width, 318 + .height = height, 319 + .x = json_utils.getInt(i32, json, "x"), 320 + .y = json_utils.getInt(i32, json, "y"), 321 + .resizable = json_utils.getBool(json, "resizable") orelse true, 322 + .frameless = json_utils.getBool(json, "frameless") orelse false, 323 + .fullscreen = json_utils.getBool(json, "fullscreen") orelse false, 324 + .dev_tools = json_utils.getBool(json, "devTools") orelse false, 325 + }); 326 + errdefer created.close(); 327 + if (url) |text| try created.loadURL(text) else if (html) |text| try created.loadHTML(text); 328 + if (!window_registry.rememberNamedOwned(@intFromPtr(created.gtk_window), name, owner)) 329 + return error.TooManyWindows; 330 + created.show(); 331 + bridge_error.sendResultToJS(allocator, action, result); 332 + } 333 + 334 + fn targetWindow(data: ?[]const u8) !desktop_window_registry.Entry { 335 + if (data) |json| { 336 + const name = try json_utils.getStringDecoded(std.heap.c_allocator, json, "windowId"); 337 + defer if (name) |text| std.heap.c_allocator.free(text); 338 + if (name) |text| { 339 + if (!std.mem.eql(u8, text, "main")) { 340 + const handle = window_registry.byName(text) orelse return error.NotFound; 341 + return desktop_windows.byWindow(handle) orelse error.NotFound; 342 + } 343 + } 344 + } 345 + return desktop_windows.byWindow(window_context.current() orelse return error.WindowHandleNotSet) orelse error.WindowHandleNotSet; 346 + } 347 + 348 + fn handleWindowAction(action: []const u8, data: ?[]const u8) !void { 349 + if (std.mem.eql(u8, action, "open") or std.mem.eql(u8, action, "create")) 350 + return openNamedWindow(action, data); 351 + 352 + const entry = try targetWindow(data); 353 + const window: *anyopaque = @ptrFromInt(entry.window); 354 + const webview: *anyopaque = @ptrFromInt(entry.webview); 355 + if (std.mem.eql(u8, action, "show") or std.mem.eql(u8, action, "focus")) { 356 + gtk_window_present(window); 357 + } else if (std.mem.eql(u8, action, "hide")) { 358 + gtk_widget_hide(window); 359 + } else if (std.mem.eql(u8, action, "close") or std.mem.eql(u8, action, "destroy")) { 360 + gtk_window_close(window); 361 + } else if (std.mem.eql(u8, action, "minimize")) { 362 + gtk_window_minimize(window); 363 + } else if (std.mem.eql(u8, action, "maximize")) { 364 + gtk_window_maximize(window); 365 + } else if (std.mem.eql(u8, action, "unmaximize") or std.mem.eql(u8, action, "restore")) { 366 + gtk_window_unmaximize(window); 367 + } else if (std.mem.eql(u8, action, "reload")) { 368 + webkit_web_view_reload(webview); 369 + } else if (std.mem.eql(u8, action, "setTitle")) { 370 + const json = data orelse return error.MissingData; 371 + const title = (try json_utils.getStringDecoded(std.heap.c_allocator, json, "title")) orelse return error.InvalidParameter; 372 + defer std.heap.c_allocator.free(title); 373 + if (std.mem.indexOfScalar(u8, title, 0) != null) return error.InvalidParameter; 374 + const title_z = try @import("memory.zig").dupeZ(std.heap.c_allocator, u8, title); 375 + defer std.heap.c_allocator.free(title_z); 376 + gtk_window_set_title(window, title_z); 377 + } else if (std.mem.eql(u8, action, "setSize")) { 378 + const json = data orelse return error.MissingData; 379 + const width = json_utils.getInt(u32, json, "width") orelse return error.InvalidParameter; 380 + const height = json_utils.getInt(u32, json, "height") orelse return error.InvalidParameter; 381 + if (width == 0 or height == 0 or width > @as(u32, std.math.maxInt(c_int)) or height > @as(u32, std.math.maxInt(c_int))) 382 + return error.InvalidParameter; 383 + gtk_window_set_default_size(window, @intCast(width), @intCast(height)); 384 + } else if (std.mem.eql(u8, action, "setPosition")) { 385 + const json = data orelse return error.MissingData; 386 + const x = json_utils.getInt(i32, json, "x") orelse return error.InvalidParameter; 387 + const y = json_utils.getInt(i32, json, "y") orelse return error.InvalidParameter; 388 + gtk_window_set_position(window, x, y); 389 + } else if (std.mem.eql(u8, action, "loadURL") or std.mem.eql(u8, action, "loadHTML")) { 390 + const json = data orelse return error.MissingData; 391 + const key: []const u8 = if (std.mem.eql(u8, action, "loadURL")) "url" else "html"; 392 + const value = (try json_utils.getStringDecoded(std.heap.c_allocator, json, key)) orelse return error.InvalidParameter; 393 + defer std.heap.c_allocator.free(value); 394 + if (std.mem.indexOfScalar(u8, value, 0) != null) return error.InvalidParameter; 395 + const value_z = try @import("memory.zig").dupeZ(std.heap.c_allocator, u8, value); 396 + defer std.heap.c_allocator.free(value_z); 397 + if (std.mem.eql(u8, action, "loadURL")) webkit_web_view_load_uri(webview, value_z) else webkit_web_view_load_html(webview, value_z, ""); 398 + } else { 399 + return error.PlatformNotSupported; 400 + } 224 401 } 225 402 226 403 // Application state ⋯ 38 lines Show the 38 lines above this hunk @@ -265,9 +442,16 @@ pub const Window = struct { 265 442 } 266 443 267 444 const window = gtk_application_window_new(app_instance.?); 445 + errdefer gtk_window_close(window); 268 446 269 447 // Create WebView 270 448 const webview = webkit_web_view_new(); 449 + const manager = webkit_web_view_get_user_content_manager(webview); 450 + // Connect before registration: WebKit can emit a page message as soon 451 + // as the injected document-start API becomes available. 452 + if (g_signal_connect_data(manager, "script-message-received::craft", @ptrCast(&onScriptMessage), webview, null, 0) == 0 or 453 + webkit_user_content_manager_register_script_message_handler(manager, "craft") == 0) 454 + return error.SignalConnectionFailed; 271 455 272 456 // Configure WebView settings 273 457 const settings = webkit_web_view_get_settings(webview); ⋯ 64 lines Show the 64 lines above this hunk @@ -338,12 +522,10 @@ pub const Window = struct { 338 522 339 523 // Register window in the multi-window registry 340 524 const window_id = registerWindow(window, webview) orelse { 341 - gtk_window_close(window); 342 525 return error.TooManyWindows; 343 526 }; 344 527 if (g_signal_connect_data(window, "destroy", @ptrCast(&onWindowDestroyed), null, null, 0) == 0) { 345 528 _ = desktop_windows.forgetWindow(@intFromPtr(window)); 346 - gtk_window_close(window); 347 529 return error.SignalConnectionFailed; 348 530 } 349 531 ⋯ 7 lines Show the 7 lines above this hunk @@ -357,8 +539,6 @@ pub const Window = struct { 357 539 .x = x, 358 540 .y = y, 359 541 }; 360 - errdefer created.close(); 361 - 362 542 // Register the page API before any load. The user-content manager 363 543 // reapplies this document-start script after navigation too. 364 544 try created.injectScript(@embedFile("js/craft-bridge.js"));