Files

174 lines
4.7 KiB
Go
Raw Permalink Normal View History

package application
import (
"context"
2024-01-19 21:34:49 +11:00
"encoding/json"
"fmt"
2023-08-18 09:35:03 +10:00
"log/slog"
2023-02-06 20:50:11 +11:00
"net/http"
"strconv"
"sync"
)
// TODO maybe we could use a new struct that has the targetWindow as an attribute so we could get rid of passing the targetWindow
// as parameter through every function call.
2023-08-24 20:24:08 +10:00
const (
callRequest = 0
clipboardRequest = 1
applicationRequest = 2
eventsRequest = 3
contextMenuRequest = 4
dialogRequest = 5
windowRequest = 6
screensRequest = 7
systemRequest = 8
browserRequest = 9
cancelCallRequesst = 10
2023-08-24 20:24:08 +10:00
)
2023-03-18 08:11:36 +11:00
type MessageProcessor struct {
pluginManager *PluginManager
2023-08-18 09:35:03 +10:00
logger *slog.Logger
runningCalls map[string]context.CancelFunc
l sync.Mutex
2023-03-18 08:11:36 +11:00
}
2023-08-18 09:35:03 +10:00
func NewMessageProcessor(logger *slog.Logger) *MessageProcessor {
return &MessageProcessor{
logger: logger,
runningCalls: map[string]context.CancelFunc{},
2023-08-18 09:35:03 +10:00
}
}
2023-02-06 20:50:11 +11:00
func (m *MessageProcessor) httpError(rw http.ResponseWriter, message string, args ...any) {
m.Error(message, args...)
rw.WriteHeader(http.StatusBadRequest)
2024-01-19 21:34:49 +11:00
_, err := rw.Write([]byte(fmt.Sprintf(message, args...)))
if err != nil {
m.Error("Unable to write error message: %s", err)
}
2023-02-06 20:50:11 +11:00
}
func (m *MessageProcessor) getTargetWindow(r *http.Request) (Window, string) {
2023-04-07 19:47:01 +10:00
windowName := r.Header.Get(webViewRequestHeaderWindowName)
if windowName != "" {
return globalApplication.GetWindowByName(windowName), windowName
2023-04-07 19:47:01 +10:00
}
windowID := r.Header.Get(webViewRequestHeaderWindowId)
if windowID == "" {
return nil, windowID
2023-04-07 19:47:01 +10:00
}
wID, err := strconv.ParseUint(windowID, 10, 64)
if err != nil {
m.Error("Window ID '%s' not parsable: %s", windowID, err)
return nil, windowID
2023-04-07 19:47:01 +10:00
}
targetWindow := globalApplication.getWindowForID(uint(wID))
if targetWindow == nil {
m.Error("Window ID %d not found", wID)
return nil, windowID
2023-04-07 19:47:01 +10:00
}
return targetWindow, windowID
2023-04-07 19:47:01 +10:00
}
2024-01-19 21:34:49 +11:00
func (m *MessageProcessor) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
2023-08-24 20:24:08 +10:00
object := r.URL.Query().Get("object")
if object == "" {
m.httpError(rw, "Invalid runtime call")
2023-08-24 20:24:08 +10:00
return
}
2023-02-06 20:50:11 +11:00
m.HandleRuntimeCallWithIDs(rw, r)
2023-08-24 20:24:08 +10:00
}
func (m *MessageProcessor) HandleRuntimeCallWithIDs(rw http.ResponseWriter, r *http.Request) {
object, err := strconv.Atoi(r.URL.Query().Get("object"))
if err != nil {
m.httpError(rw, "Error decoding object value: "+err.Error())
return
}
method, err := strconv.Atoi(r.URL.Query().Get("method"))
if err != nil {
m.httpError(rw, "Error decoding method value: "+err.Error())
return
}
params := QueryParams(r.URL.Query())
targetWindow, nameOrID := m.getTargetWindow(r)
2023-08-24 20:24:08 +10:00
if targetWindow == nil {
m.httpError(rw, fmt.Sprintf("Window '%s' not found", nameOrID))
2023-08-24 20:24:08 +10:00
return
}
switch object {
case windowRequest:
m.processWindowMethod(method, rw, r, targetWindow, params)
case clipboardRequest:
m.processClipboardMethod(method, rw, r, targetWindow, params)
case dialogRequest:
m.processDialogMethod(method, rw, r, targetWindow, params)
case eventsRequest:
m.processEventsMethod(method, rw, r, targetWindow, params)
case applicationRequest:
m.processApplicationMethod(method, rw, r, targetWindow, params)
case contextMenuRequest:
m.processContextMenuMethod(method, rw, r, targetWindow, params)
case screensRequest:
m.processScreensMethod(method, rw, r, targetWindow, params)
2023-08-27 16:52:07 +10:00
case callRequest:
m.processCallMethod(method, rw, r, targetWindow, params)
2023-08-24 20:24:08 +10:00
case systemRequest:
m.processSystemMethod(method, rw, r, targetWindow, params)
case browserRequest:
m.processBrowserMethod(method, rw, r, targetWindow, params)
case cancelCallRequesst:
m.processCallCancelMethod(method, rw, r, targetWindow, params)
default:
2023-09-08 12:03:55 +10:00
m.httpError(rw, "Unknown runtime call: %d", object)
}
2023-02-06 20:50:11 +11:00
}
func (m *MessageProcessor) Error(message string, args ...any) {
2023-08-18 09:35:03 +10:00
m.logger.Error(message, args...)
}
func (m *MessageProcessor) Info(message string, args ...any) {
2023-08-18 09:35:03 +10:00
m.logger.Info(message, args...)
}
2023-02-06 20:50:11 +11:00
func (m *MessageProcessor) json(rw http.ResponseWriter, data any) {
2023-02-15 18:56:41 +11:00
rw.Header().Set("Content-Type", "application/json")
2023-02-06 20:50:11 +11:00
// convert data to json
var jsonPayload = []byte("{}")
var err error
if data != nil {
2024-01-19 21:34:49 +11:00
jsonPayload, err = json.Marshal(data)
2023-02-06 20:50:11 +11:00
if err != nil {
m.Error("Unable to convert data to JSON. Please report this to the Wails team! Error: %s", err)
return
}
}
_, err = rw.Write(jsonPayload)
if err != nil {
m.Error("Unable to write json payload. Please report this to the Wails team! Error: %s", err)
return
}
m.ok(rw)
}
func (m *MessageProcessor) text(rw http.ResponseWriter, data string) {
_, err := rw.Write([]byte(data))
if err != nil {
m.Error("Unable to write json payload. Please report this to the Wails team! Error: %s", err)
return
}
rw.Header().Set("Content-Type", "text/plain")
2023-02-15 18:56:41 +11:00
rw.WriteHeader(http.StatusOK)
2023-02-06 20:50:11 +11:00
}
func (m *MessageProcessor) ok(rw http.ResponseWriter) {
rw.WriteHeader(http.StatusOK)
}