Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions v3/examples/raw-message/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"embed"
_ "embed"
"github.com/wailsapp/wails/v3/pkg/application"
"fmt"
"log"

"github.com/wailsapp/wails/v3/pkg/application"
)

//go:embed assets
Expand All @@ -21,8 +23,8 @@ func main() {
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
RawMessageHandler: func(window application.Window, message string) {
println("Raw message received from Window '" + window.Name() + "' with message: " + message)
RawMessageHandler: func(window application.Window, message string, originInfo *application.OriginInfo) {
println(fmt.Sprintf("Raw message received from Window %s with message: %s, origin %s, topOrigin %s, isMainFrame %t", window.Name(), message, originInfo.Origin, originInfo.TopOrigin, originInfo.IsMainFrame))
},
})

Expand Down
13 changes: 10 additions & 3 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,15 @@ type (

// Messages sent from javascript get routed here
type windowMessage struct {
windowId uint
message string
windowId uint
message string
originInfo *OriginInfo
}

type OriginInfo struct {
Origin string
TopOrigin string
IsMainFrame bool
}

var windowMessageBuffer = make(chan *windowMessage, 5)
Expand Down Expand Up @@ -708,7 +715,7 @@ func (a *App) handleWindowMessage(event *windowMessage) {
window.HandleMessage(event.message)
} else {
if a.options.RawMessageHandler != nil {
a.options.RawMessageHandler(window, event.message)
a.options.RawMessageHandler(window, event.message, event.originInfo)
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion v3/pkg/application/application_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,18 @@ func processWindowEvent(windowID C.uint, eventID C.uint) {
}

//export processMessage
func processMessage(windowID C.uint, message *C.char) {
func processMessage(windowID C.uint, message *C.char, origin *C.char, isMainFrame bool) {
o := ""
if origin != nil {
o = C.GoString(origin)
}
windowMessageBuffer <- &windowMessage{
windowId: uint(windowID),
message: C.GoString(message),
originInfo: &OriginInfo{
Origin: o,
IsMainFrame: isMainFrame,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion v3/pkg/application/application_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type Options struct {

// RawMessageHandler is called when the frontend sends a raw message.
// This is useful for implementing custom frontend-to-backend communication.
RawMessageHandler func(window Window, message string)
RawMessageHandler func(window Window, message string, originInfo *OriginInfo)

// WarningHandler is called when a warning occurs
WarningHandler func(string)
Expand Down
26 changes: 26 additions & 0 deletions v3/pkg/application/linux_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ static void save_window_id(void *object, uint value)
g_object_set_data((GObject *)object, "windowid", GUINT_TO_POINTER((guint)value));
}
static void save_webview_to_content_manager(void *contentManager, void *webview)
{
g_object_set_data(G_OBJECT((WebKitUserContentManager *)contentManager), "webview", webview);
}
static WebKitWebView* get_webview_from_content_manager(void *contentManager)
{
return WEBKIT_WEB_VIEW(g_object_get_data(G_OBJECT(contentManager), "webview"));
}
static guint get_window_id(void *object)
{
return GPOINTER_TO_UINT(g_object_get_data((GObject *)object, "windowid"));
Expand Down Expand Up @@ -1109,6 +1119,8 @@ func windowNewWebview(parentId uint, gpuPolicy WebviewGpuPolicy) pointer {
C.webkit_user_content_manager_register_script_message_handler(manager, c.String("external"))
webView := C.webkit_web_view_new_with_user_content_manager(manager)

C.save_webview_to_content_manager(unsafe.Pointer(manager), unsafe.Pointer(webView))

// attach window id to both the webview and contentmanager
C.save_window_id(unsafe.Pointer(webView), C.uint(parentId))
C.save_window_id(unsafe.Pointer(manager), C.uint(parentId))
Expand Down Expand Up @@ -1646,6 +1658,17 @@ func sendMessageToBackend(contentManager *C.WebKitUserContentManager, result *C.
// Get the windowID from the contentManager
thisWindowID := uint(C.get_window_id(unsafe.Pointer(contentManager)))

webView := C.get_webview_from_content_manager(unsafe.Pointer(contentManager))
var origin string
if webView != nil {
currentUri := C.webkit_web_view_get_uri(webView)
if currentUri != nil {
uri := C.g_strdup(currentUri)
defer C.g_free(C.gpointer(uri))
origin = C.GoString(uri)
}
}

var msg string
value := C.webkit_javascript_result_get_js_value(result)
message := C.jsc_value_to_string(value)
Expand All @@ -1654,6 +1677,9 @@ func sendMessageToBackend(contentManager *C.WebKitUserContentManager, result *C.
windowMessageBuffer <- &windowMessage{
windowId: thisWindowID,
message: msg,
originInfo: &OriginInfo{
Origin: origin,
},
}
}

Expand Down
Loading
Loading