Harden theme IPC payload handling
This commit is contained in:
parent
f9c4baa720
commit
bc3b08ca5d
2 changed files with 20 additions and 6 deletions
|
|
@ -2,6 +2,7 @@ package display
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
core "dappco.re/go/core"
|
||||
coreerr "dappco.re/go/core/log"
|
||||
|
|
@ -507,22 +508,36 @@ func (s *Service) SetTheme(theme string) error {
|
|||
|
||||
func (s *Service) GetTheme() *Theme {
|
||||
r := s.Core().QUERY(environment.QueryTheme{})
|
||||
if !r.OK {
|
||||
info, ok := themeInfoFromQueryResult(s, "display.GetTheme", r)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
info, _ := r.Value.(environment.ThemeInfo)
|
||||
return &Theme{IsDark: info.IsDark}
|
||||
}
|
||||
|
||||
func (s *Service) GetSystemTheme() string {
|
||||
r := s.Core().QUERY(environment.QueryTheme{})
|
||||
if !r.OK {
|
||||
info, ok := themeInfoFromQueryResult(s, "display.GetSystemTheme", r)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
info, _ := r.Value.(environment.ThemeInfo)
|
||||
return info.Theme
|
||||
}
|
||||
|
||||
func themeInfoFromQueryResult(s *Service, method string, r core.Result) (environment.ThemeInfo, bool) {
|
||||
if !r.OK {
|
||||
return environment.ThemeInfo{}, false
|
||||
}
|
||||
info, ok := r.Value.(environment.ThemeInfo)
|
||||
if ok {
|
||||
return info, true
|
||||
}
|
||||
if s != nil && s.Core() != nil {
|
||||
s.Core().LogWarn(fmt.Errorf("query=environment.QueryTheme value_type=%T", r.Value), method, "malformed theme query payload")
|
||||
}
|
||||
return environment.ThemeInfo{}, false
|
||||
}
|
||||
|
||||
func (s *Service) sendNotification(opts notification.NotificationOptions) error {
|
||||
result := s.Core().Action("notification.send").Run(context.Background(), core.NewOptions(
|
||||
core.Option{Key: "task", Value: notification.TaskSend{Options: opts}},
|
||||
|
|
|
|||
|
|
@ -291,8 +291,7 @@ func TestDisplayAPI_GetTheme_Bad(t *testing.T) {
|
|||
})
|
||||
|
||||
theme := svc.GetTheme()
|
||||
require.NotNil(t, theme)
|
||||
assert.False(t, theme.IsDark)
|
||||
assert.Nil(t, theme)
|
||||
assert.Empty(t, svc.GetSystemTheme())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue