Skip to content

Commit

Permalink
Ignore internal service methods when binding (#3720)
Browse files Browse the repository at this point in the history
* Ignore internal service methods when binding

* Updated changelog.md
  • Loading branch information
leaanthony committed Sep 8, 2024
1 parent cc70b98 commit ef8c886
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 15 deletions.
2 changes: 2 additions & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [linux] Fixed linux compile error introduced by IgnoreMouseEvents addition by [atterpac](https://github.com/atterpac) in [#3721](https://github.com/wailsapp/wails/pull/3721)
- [windows] Fixed syso icon file generation bug by [atterpac](https://github.com/atterpac) in [#3675](https://github.com/wailsapp/wails/pull/3675)
- [linux] Fix to run natively in wayland incorporated from [#1811](https://github.com/wailsapp/wails/pull/1811) in [#3614](https://github.com/wailsapp/wails/pull/3614) by [@stendler](https://github.com/stendler)
- Do not bind internal service methods in [#3720](https://github.com/wailsapp/wails/pull/3720)
by [leaanthony](https://github.com/leaanthony)
- [windows] Fixed system tray startup panic in [#3693](https://github.com/wailsapp/wails/issues/3693) by [@DeltaLaboratory](https://github.com/DeltaLaboratory)

## v3.0.0-alpha.6 - 2024-07-30
Expand Down
4 changes: 3 additions & 1 deletion v3/examples/services/hashes/hashes.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package hashes

import (
"context"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"github.com/wailsapp/wails/v3/pkg/application"
)

type Hashes struct {
Expand Down Expand Up @@ -35,6 +37,6 @@ func (h *Hashes) Name() string {
return "Hashes Service"
}

func (h *Hashes) OnStartup() error {
func (h *Hashes) OnStartup(_ context.Context, _ application.ServiceOptions) error {
return nil
}
9 changes: 8 additions & 1 deletion v3/examples/services/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ import (
"log/slog"
"os"
"path/filepath"
"runtime"
)

//go:embed assets/*
var assets embed.FS

func main() {

rootPath, _ := filepath.Abs("./files")
// Get the local directory of this source file
// This isn't needed when running the example with `go run .`
// but is needed when running the example from an IDE
_, thisFile, _, _ := runtime.Caller(0)
localDir := filepath.Dir(thisFile)

rootPath := filepath.Join(localDir, "files")
app := application.New(application.Options{
Name: "Services Demo",
Description: "A demo of the services API",
Expand Down
2 changes: 0 additions & 2 deletions v3/internal/assetserver/assetserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ func (a *AssetServer) serveHTTP(rw http.ResponseWriter, req *http.Request, userH
for route, handler := range a.services {
if strings.HasPrefix(reqPath, route) {
req.URL.Path = strings.TrimPrefix(reqPath, route)
// Strip leading slash
req.URL.Path = strings.TrimPrefix(req.URL.Path, "/")
handler.ServeHTTP(rw, req)
return
}
Expand Down
50 changes: 40 additions & 10 deletions v3/pkg/application/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func NewBindings(instances []Service, aliases map[uint32]uint32) (*Bindings, err

// Add the given named type pointer methods to the Bindings
func (b *Bindings) Add(namedPtr interface{}) error {
methods, err := b.getMethods(namedPtr, false)
methods, err := b.getMethods(namedPtr)
if err != nil {
return fmt.Errorf("cannot bind value to app: %s", err.Error())
}
Expand Down Expand Up @@ -153,7 +153,7 @@ func (b *BoundMethod) String() string {
return fmt.Sprintf("%s.%s.%s", b.PackagePath, b.TypeName, b.Name)
}

func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod, error) {
func (b *Bindings) getMethods(value interface{}) ([]*BoundMethod, error) {
// Create result placeholder
var result []*BoundMethod

Expand Down Expand Up @@ -188,6 +188,10 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
methodName := methodDef.Name
method := namedValue.MethodByName(methodName)

if b.internalMethod(methodDef) {
continue
}

// Create new method
boundMethod := &BoundMethod{
Name: methodName,
Expand All @@ -204,16 +208,15 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
return nil, err
}

if !isPlugin {
args := []any{"name", boundMethod, "id", boundMethod.ID}
if b.methodAliases != nil {
alias, found := lo.FindKey(b.methodAliases, boundMethod.ID)
if found {
args = append(args, "alias", alias)
}
args := []any{"name", boundMethod, "id", boundMethod.ID}
if b.methodAliases != nil {
alias, found := lo.FindKey(b.methodAliases, boundMethod.ID)
if found {
args = append(args, "alias", alias)
}
globalApplication.debug("Adding method:", args...)
}
globalApplication.debug("Adding method:", args...)

// Iterate inputs
methodType := method.Type()
inputParamCount := methodType.NumIn()
Expand Down Expand Up @@ -245,6 +248,33 @@ func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod,
return result, nil
}

func (b *Bindings) internalMethod(def reflect.Method) bool {
// Get the receiver type
receiverType := def.Type.In(0)

// Create a new instance of the receiver type
instance := reflect.New(receiverType.Elem()).Interface()

// Check if the instance implements any of our service interfaces
// and if the method matches the interface method
switch def.Name {
case "Name":
if _, ok := instance.(ServiceName); ok {
return true
}
case "OnStartup":
if _, ok := instance.(ServiceStartup); ok {
return true
}
case "OnShutdown":
if _, ok := instance.(ServiceShutdown); ok {
return true
}
}

return false
}

var errorType = reflect.TypeFor[error]()

// Call will attempt to call this bound method with the given args
Expand Down
1 change: 0 additions & 1 deletion v3/pkg/services/fileserver/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ func (s *Service) OnStartup(ctx context.Context, options application.ServiceOpti

func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Create a new file server rooted at the given path
// Strip the base path out of the request path
s.fs.ServeHTTP(w, r)
}

0 comments on commit ef8c886

Please sign in to comment.