wails icon indicating copy to clipboard operation
wails copied to clipboard

[V3] Add origin to raw message handler

Open APshenkin opened this issue 3 weeks ago • 7 comments

Description

This PR ports https://github.com/wailsapp/wails/pull/4480 to V3. RawMessageHandler will provide available origin info to handler, so that application can decide what to do with a message.

On Mac: Origin and IsMainFrame provided On Windows: Origin and TopOrigin provided On Linux: Origin only provided

Fixes # (issue) related to https://github.com/wailsapp/wails/issues/4686

Type of change

Please select the option that is relevant.

  • [ ] Bug fix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [x] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • [x] Windows
  • [x] macOS
  • [x] Linux

If you checked Linux, please specify the distro and version.

Test Configuration

# System

┌─────────────────────────────────────────────────────────────────────────────────────────────┐
| Name              | Windows 10 Pro                                                          |
| Version           | 2009 (Build: 26220)                                                     |
| ID                | 25H2                                                                    |
| Branding          | Windows 11 Pro                                                          |
| Platform          | windows                                                                 |
| Architecture      | arm64                                                                   |
| Go WebView2Loader | true                                                                    |
| WebView2 Version  | 143.0.3650.7                                                            |
| CPU               | virt-9.1                                                                |
| GPU 1             | Red Hat VirtIO GPU DOD controller (Red Hat, Inc.) - Driver: 22.7.38.43  |
| Memory            | 32GB                                                                    |
└─────────────────────────────────────────────────────────────────────────────────────────────┘

# Build Environment

┌─────────────────────────────────────────────────────────┐
| Wails CLI    | v3.0.0-dev                               |
| Go Version   | go1.24.1                                 |
| Revision     | 97b00ed52f6a5d85eed15f60f10552bbb32c9e03 |
| Modified     | false                                    |
| -buildmode   | exe                                      |
| -compiler    | gc                                       |
| CGO_CFLAGS   |                                          |
| CGO_CPPFLAGS |                                          |
| CGO_CXXFLAGS |                                          |
| CGO_ENABLED  | 1                                        |
| CGO_LDFLAGS  |                                          |
| GOARCH       | arm64                                    |
| GOARM64      | v8.0                                     |
| GOOS         | windows                                  |
| vcs          | git                                      |
| vcs.modified | false                                    |
| vcs.revision | 97b00ed52f6a5d85eed15f60f10552bbb32c9e03 |
| vcs.time     | 2025-11-11T23:15:00Z                     |
└─────────────────────────────────────────────────────────┘

# Dependencies

┌───────────────────────────────────────────┐
| npm                        | 10.9.2       |
| NSIS                       | v3.10        |
| MakeAppx.exe (Windows SDK) | Installed    |
| MSIX Packaging Tool        | 1.2024.405.0 |
| SignTool.exe (Windows SDK) | Installed    |
|                                           |
└───────── * - Optional Dependency ─────────┘
# System

┌──────────────────────────────────────────────────┐
| Name          | MacOS                            |
| Version       | 26.1                             |
| ID            | 25B78                            |
| Branding      | MacOS 26.1                       |
| Platform      | darwin                           |
| Architecture  | arm64                            |
| Apple Silicon | true                             |
| CPU           | Apple M4 Max                     |
| CPU 1         | Apple M4 Max                     |
| CPU 2         | Apple M4 Max                     |
| GPU           | 40 cores, Metal Support: Metal 4 |
| Memory        | 128 GB                           |
└──────────────────────────────────────────────────┘

# Build Environment

┌─────────────────────────────────────────────────────────┐
| Wails CLI    | v3.0.0-dev                               |
| Go Version   | go1.24.0                                 |
| Revision     | 4a445ce218b58db4a16ee8fa84eac0d7c9a69932 |
| Modified     | false                                    |
| -buildmode   | exe                                      |
| -compiler    | gc                                       |
| CGO_CFLAGS   |                                          |
| CGO_CPPFLAGS |                                          |
| CGO_CXXFLAGS |                                          |
| CGO_ENABLED  | 1                                        |
| CGO_LDFLAGS  |                                          |
| GOARCH       | arm64                                    |
| GOARM64      | v8.0                                     |
| GOOS         | darwin                                   |
| vcs          | git                                      |
| vcs.modified | false                                    |
| vcs.revision | 4a445ce218b58db4a16ee8fa84eac0d7c9a69932 |
| vcs.time     | 2025-08-04T22:42:25Z                     |
└─────────────────────────────────────────────────────────┘

# Dependencies

┌────────────────────────────────────────────────────────────────────────┐
| Xcode cli tools | 2416                                                 |
| npm             | 10.9.2                                               |
| *NSIS           | Not Installed. Install with `brew install makensis`. |
|                                                                        |
└─────────────────────── * - Optional Dependency ────────────────────────┘
# System 

┌─────────────────────────────────────────────────────────────────────┐
| Name         | Fedora Linux                                         |
| Version      | 42                                                   |
| ID           | fedora                                               |
| Branding     | 42 (Workstation Edition)                             |
| Platform     | linux                                                |
| Architecture | arm64                                                |
| CPU          |                                                      |
| GPU 1        | Virtio 1.0 GPU (Red Hat, Inc.) - Driver: virtio-pci  |
| Memory       | 16GB                                                 |
└─────────────────────────────────────────────────────────────────────┘

# Build Environment 

┌─────────────────────────────────────────────────────────┐
| Wails CLI    | v3.0.0-dev                               |
| Go Version   | go1.24.5                                 |
| Revision     | 97b00ed52f6a5d85eed15f60f10552bbb32c9e03 |
| Modified     | false                                    |
| -buildmode   | exe                                      |
| -compiler    | gc                                       |
| CGO_CFLAGS   |                                          |
| CGO_CPPFLAGS |                                          |
| CGO_CXXFLAGS |                                          |
| CGO_ENABLED  | 1                                        |
| CGO_LDFLAGS  |                                          |
| GOARCH       | arm64                                    |
| GOARM64      | v8.0                                     |
| GOOS         | linux                                    |
| vcs          | git                                      |
| vcs.modified | false                                    |
| vcs.revision | 97b00ed52f6a5d85eed15f60f10552bbb32c9e03 |
| vcs.time     | 2025-11-11T23:15:00Z                     |
└─────────────────────────────────────────────────────────┘

# Dependencies 

┌───────────────────────────┐
| pkg-config | 2.3.0        |
| webkit2gtk | 2.50.1       |
| gcc        | 15.2.1       |
| gtk3       | 3.24.49      |
| npm        | 10.9.2       |
|                           |
└─ * - Optional Dependency ─┘

Checklist:

  • [ ] I have updated website/src/pages/changelog.mdx with details of this PR
  • [x] My code follows the general coding style of this project
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [x] My changes generate no new warnings
  • [ ] I have added tests that prove my fix is effective or that my feature works
  • [x] New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • New Features

    • Raw message handlers now receive origin and frame context (origin, top-origin, main-frame flag) so handlers can determine message source.
  • Chores / API

    • Public callback signature changed — update any custom raw message handlers to accept the additional origin information parameter.

✏️ Tip: You can customize this high-level summary in your review settings.

APshenkin avatar Nov 14 '25 18:11 APshenkin

[!CAUTION]

Review failed

The pull request is closed.

Walkthrough

Adds an OriginInfo struct and propagates origin/top-origin/isMainFrame metadata from platform WebView layers into window messages; updates the RawMessageHandler callback to accept originInfo *application.OriginInfo; adapts platform bridges (macOS, Windows, Linux) and example usage accordingly.

Changes

Cohort / File(s) Summary
Core API / Options
v3/pkg/application/application.go, v3/pkg/application/application_options.go
Add public OriginInfo type (Origin, TopOrigin, IsMainFrame); change Options.RawMessageHandler signature from func(Window, string) to func(Window, string, *OriginInfo); extend internal windowMessage to carry originInfo.
Example
v3/examples/raw-message/main.go
Update example to import fmt and adapt RawMessageHandler to new signature; log Origin, TopOrigin, and IsMainFrame.
macOS (Darwin)
v3/pkg/application/application_darwin.go, v3/pkg/application/webview_window_darwin.m
Expand processMessage bridge to accept origin and isMainFrame; extract origin/top-frame info in Objective‑C and pass them to processMessage; construct originInfo in Go message.
Linux (CGO / WebKit)
v3/pkg/application/linux_cgo.go
Add C helpers to associate WebView with content manager; retrieve WebView URI in sendMessageToBackend to initialize OriginInfo and include it in outgoing window messages.
Windows (WebView2)
v3/pkg/application/webview_window_windows.go
Retrieve sender and args source via WebView2 GetSource() (with error handling); build OriginInfo (Origin, TopOrigin) and attach it to windowMessage before enqueueing.
Changelog / Docs
v3/UNRELEASED_CHANGELOG.md
Add changelog entry documenting addition of origin to raw message handler.

Sequence Diagram(s)

sequenceDiagram
    participant WebView as Platform WebView
    participant Native as Native Bridge (ObjC/C/Win/CGo)
    participant Go as Go runtime
    participant Handler as RawMessageHandler

    WebView->>Native: emit script message (body + frame info)
    activate Native
    Note right of Native `#e8f0ff`: extract origin / top-origin / isMainFrame
    Native->>Go: processMessage(windowID, msg, origin, isMainFrame)
    deactivate Native

    activate Go
    Note right of Go `#f6fff0`: construct windowMessage with originInfo
    Go->>Handler: RawMessageHandler(window, msg, originInfo)
    deactivate Go

    activate Handler
    Handler->>Handler: consume / log / process message + originInfo
    deactivate Handler

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Review platform boundary conversions (C char ↔ Go string) and nil handling for originInfo.
  • Verify all processMessage call sites and signatures were updated consistently across macOS, Windows, and Linux.
  • Inspect Linux C helpers linking WebView ↔ content manager and Windows WebView2 GetSource() error handling.

Possibly related PRs

  • wailsapp/wails#4710 — same change set: add OriginInfo and propagate origin/topOrigin/isMainFrame through platform bridges to backend handler.
  • wailsapp/wails#4480 — related work extracting and forwarding message origin/main-frame info through the platform→Go pipeline.
  • wailsapp/wails#4645 — related modifications to Windows processMessage and WebView2 message handling.

Suggested labels

Enhancement, go, Linux, To Be Documented, size:M

Poem

🐇 I hopped from frame to byte and log,
I chased the origin through mist and fog,
From native bridge to Go's bright glade,
I carry where each message was made,
🥕 tiny hops, a header and a nod.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: adding origin information to the raw message handler callback.
Description check ✅ Passed The description covers the main objective, mentions the related PR and issue, specifies type of changes (feature and breaking change), includes comprehensive test configuration for Windows, macOS, and Linux, and addresses most checklist items. However, changelog and documentation updates are not marked as completed.

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9f34b0b4a4a84d112f805f294a0169a2cecadf2e and 3675066a2abe5bf2d48e423de7a99b6eb5d462cf.

📒 Files selected for processing (2)
  • v3/UNRELEASED_CHANGELOG.md (1 hunks)
  • v3/pkg/application/application_darwin.go (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Nov 14 '25 18:11 coderabbitai[bot]

@coderabbitai generate docstrings

leaanthony avatar Nov 18 '25 19:11 leaanthony

✅ Actions performed

Initiated docstring generation; will generate only if new commits exist.

coderabbitai[bot] avatar Nov 18 '25 19:11 coderabbitai[bot]

[!NOTE] Docstrings generation - SUCCESS Generated docstrings for this pull request at https://github.com/wailsapp/wails/pull/4717

coderabbitai[bot] avatar Nov 18 '25 19:11 coderabbitai[bot]

Thanks @APshenkin 🙏 Please could you add an entry to the changelog located at v3/UNRELEASED_CHANGELOG.md? Looks like there's some build failures on Windows too. Thanks!

leaanthony avatar Nov 22 '25 03:11 leaanthony

@leaanthony added and fixed

APshenkin avatar Nov 24 '25 23:11 APshenkin

@leaanthony I believe this one can be merged as well? 🙏

APshenkin avatar Dec 07 '25 11:12 APshenkin

@APshenkin - I've pushed some docs updates for this (I couldn't push to your branch). Feel free to update the docs as you see fit after merge :+1:

leaanthony avatar Dec 08 '25 08:12 leaanthony

Thanks for your patience @APshenkin and incredible PR as always :pray:

leaanthony avatar Dec 08 '25 09:12 leaanthony

@leaanthony added required docs here https://github.com/wailsapp/wails/pull/4758

APshenkin avatar Dec 08 '25 10:12 APshenkin