| 1 |
/* |
| 2 |
Copyright 2013-2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
package service |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"os" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"github.com/godbus/dbus/v5" |
| 25 |
"github.com/google/uuid" |
| 26 |
|
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/accounts" |
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/emblemcounter" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/haptic" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/notifications" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/greeter" |
| 33 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus/focusinfo" |
| 34 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 35 |
"gitlab.com/ubports/development/core/lomiri-push-service/click/cnotificationsettings" |
| 36 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 37 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 38 |
"gitlab.com/ubports/development/core/lomiri-push-service/messaging" |
| 39 |
"gitlab.com/ubports/development/core/lomiri-push-service/messaging/reply" |
| 40 |
"gitlab.com/ubports/development/core/lomiri-push-service/nih" |
| 41 |
"gitlab.com/ubports/development/core/lomiri-push-service/sounds" |
| 42 |
"gitlab.com/ubports/development/core/lomiri-push-service/urldispatcher" |
| 43 |
"gitlab.com/ubports/development/core/lomiri-push-service/util" |
| 44 |
) |
| 45 |
|
| 46 |
type messageHandler func(*click.AppId, string, *launch_helper.HelperOutput) bool |
| 47 |
|
| 48 |
// a Presenter is something that knows how to present a Notification |
| 49 |
type Presenter interface { |
| 50 |
Present(*click.AppId, string, *launch_helper.Notification) bool |
| 51 |
} |
| 52 |
|
| 53 |
type notificationCentre interface { |
| 54 |
Presenter |
| 55 |
GetCh() chan *reply.MMActionReply |
| 56 |
RemoveNotification(string, bool) |
| 57 |
Tags(*click.AppId) []string |
| 58 |
Clear(*click.AppId, ...string) int |
| 59 |
} |
| 60 |
|
| 61 |
// PostalServiceSetup is a configuration object for the service |
| 62 |
type PostalServiceSetup struct { |
| 63 |
InstalledChecker click.InstalledChecker |
| 64 |
FallbackVibration *launch_helper.Vibration |
| 65 |
FallbackSound string |
| 66 |
} |
| 67 |
|
| 68 |
// PostalService is the dbus api |
| 69 |
type PostalService struct { |
| 70 |
DBusService |
| 71 |
mbox map[string]*mBox |
| 72 |
msgHandler messageHandler |
| 73 |
launchers map[string]launch_helper.HelperLauncher |
| 74 |
HelperPool launch_helper.HelperPool |
| 75 |
messagingMenu notificationCentre |
| 76 |
// the endpoints are only exposed for testing from client |
| 77 |
// XXX: uncouple some more so this isn't necessary |
| 78 |
EmblemCounterEndp bus.Endpoint |
| 79 |
AccountsEndp bus.Endpoint |
| 80 |
HapticEndp bus.Endpoint |
| 81 |
NotificationsEndp bus.Endpoint |
| 82 |
GreeterEndp bus.Endpoint |
| 83 |
FocusInfoEndp bus.Endpoint |
| 84 |
// presenters: |
| 85 |
Presenters []Presenter |
| 86 |
emblemCounter *emblemcounter.EmblemCounter |
| 87 |
accounts accounts.Accounts |
| 88 |
haptic *haptic.Haptic |
| 89 |
notifications *notifications.RawNotifications |
| 90 |
sound sounds.Sound |
| 91 |
// the url dispatcher, used for stuff. |
| 92 |
urlDispatcher urldispatcher.URLDispatcher |
| 93 |
greeter *greeter.Greeter |
| 94 |
focusInfo *focusinfo.FocusInfo |
| 95 |
// fallback values for simplified notification usage |
| 96 |
fallbackVibration *launch_helper.Vibration |
| 97 |
fallbackSound string |
| 98 |
} |
| 99 |
|
| 100 |
var ( |
| 101 |
PostalServiceBusAddress = bus.Address{ |
| 102 |
Interface: "com.lomiri.Postal", |
| 103 |
Path: "/com/lomiri/Postal", |
| 104 |
Name: "com.lomiri.Postal", |
| 105 |
} |
| 106 |
) |
| 107 |
|
| 108 |
var ( |
| 109 |
SystemUpdateUrl = "settings:///system/system-update" |
| 110 |
useTrivialHelper = os.Getenv("LOMIRI_PUSH_USE_TRIVIAL_HELPER") != "" |
| 111 |
) |
| 112 |
|
| 113 |
// NewPostalService() builds a new service and returns it. |
| 114 |
func NewPostalService(setup *PostalServiceSetup, log logger.Logger) *PostalService { |
| 115 |
var svc = &PostalService{} |
| 116 |
svc.Log = log |
| 117 |
svc.Bus = bus.SessionBus.Endpoint(PostalServiceBusAddress, log) |
| 118 |
svc.installedChecker = setup.InstalledChecker |
| 119 |
svc.fallbackVibration = setup.FallbackVibration |
| 120 |
svc.fallbackSound = setup.FallbackSound |
| 121 |
svc.NotificationsEndp = bus.SessionBus.Endpoint(notifications.BusAddress, log) |
| 122 |
svc.EmblemCounterEndp = bus.SessionBus.Endpoint(emblemcounter.BusAddress, log) |
| 123 |
svc.AccountsEndp = bus.SystemBus.Endpoint(accounts.BusAddress, log) |
| 124 |
svc.HapticEndp = bus.SystemBus.Endpoint(haptic.HfdBusAddress, log) |
| 125 |
svc.GreeterEndp = bus.SessionBus.Endpoint(greeter.BusAddress, log) |
| 126 |
svc.FocusInfoEndp = bus.SessionBus.Endpoint(focusinfo.BusAddress, log) |
| 127 |
svc.msgHandler = svc.messageHandler |
| 128 |
svc.launchers = launch_helper.DefaultLaunchers(log) |
| 129 |
return svc |
| 130 |
} |
| 131 |
|
| 132 |
// SetMessageHandler() sets the message-handling callback |
| 133 |
func (svc *PostalService) SetMessageHandler(callback messageHandler) { |
| 134 |
svc.lock.RLock() |
| 135 |
defer svc.lock.RUnlock() |
| 136 |
svc.msgHandler = callback |
| 137 |
} |
| 138 |
|
| 139 |
// GetMessageHandler() returns the (possibly nil) messaging handler callback |
| 140 |
func (svc *PostalService) GetMessageHandler() messageHandler { |
| 141 |
svc.lock.RLock() |
| 142 |
defer svc.lock.RUnlock() |
| 143 |
return svc.msgHandler |
| 144 |
} |
| 145 |
|
| 146 |
// Start() dials the bus, grab the name, and listens for method calls. |
| 147 |
func (svc *PostalService) Start() error { |
| 148 |
return svc.DBusService.Start(bus.DispatchMap{ |
| 149 |
"PopAll": svc.popAll, |
| 150 |
"Post": svc.post, |
| 151 |
"ListPersistent": svc.listPersistent, |
| 152 |
"ClearPersistent": svc.clearPersistent, |
| 153 |
"ClearPersistentList": svc.clearPersistentList, |
| 154 |
"SetCounter": svc.setCounter, |
| 155 |
}, PostalServiceBusAddress, svc.init) |
| 156 |
} |
| 157 |
|
| 158 |
func (svc *PostalService) init() error { |
| 159 |
actionsCh, err := svc.takeTheBus() |
| 160 |
if err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
svc.urlDispatcher = urldispatcher.New(svc.Log) |
| 164 |
|
| 165 |
svc.accounts = accounts.New(svc.AccountsEndp, svc.Log) |
| 166 |
err = svc.accounts.Start() |
| 167 |
if err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
|
| 171 |
svc.sound = sounds.New(svc.Log, svc.accounts, svc.fallbackSound) |
| 172 |
svc.notifications = notifications.Raw(svc.NotificationsEndp, svc.Log, svc.sound) |
| 173 |
svc.emblemCounter = emblemcounter.New(svc.EmblemCounterEndp, svc.Log) |
| 174 |
svc.haptic = haptic.New(svc.HapticEndp, svc.Log, svc.accounts, svc.fallbackVibration) |
| 175 |
svc.messagingMenu = messaging.New(svc.Log) |
| 176 |
svc.Presenters = []Presenter{ |
| 177 |
svc.notifications, |
| 178 |
svc.emblemCounter, |
| 179 |
svc.haptic, |
| 180 |
svc.messagingMenu, |
| 181 |
} |
| 182 |
if useTrivialHelper { |
| 183 |
svc.HelperPool = launch_helper.NewTrivialHelperPool(svc.Log) |
| 184 |
} else { |
| 185 |
svc.HelperPool = launch_helper.NewHelperPool(svc.launchers, svc.Log) |
| 186 |
} |
| 187 |
svc.greeter = greeter.New(svc.GreeterEndp, svc.Log) |
| 188 |
svc.focusInfo = focusinfo.New(svc.FocusInfoEndp, svc.Log) |
| 189 |
|
| 190 |
go svc.consumeHelperResults(svc.HelperPool.Start()) |
| 191 |
go svc.handleActions(actionsCh, svc.messagingMenu.GetCh()) |
| 192 |
return nil |
| 193 |
} |
| 194 |
|
| 195 |
// xxx Stop() closing channels and helper launcher |
| 196 |
|
| 197 |
// handleactions loops on the actions channels waiting for actions and handling them |
| 198 |
func (svc *PostalService) handleActions(actionsCh <-chan *notifications.RawAction, mmuActionsCh <-chan *reply.MMActionReply) { |
| 199 |
Handle: |
| 200 |
for { |
| 201 |
select { |
| 202 |
case action, ok := <-actionsCh: |
| 203 |
if !ok { |
| 204 |
break Handle |
| 205 |
} |
| 206 |
if action == nil { |
| 207 |
svc.Log.Debugf("handleActions got nil action; ignoring") |
| 208 |
} else { |
| 209 |
url := action.Action |
| 210 |
// remove the notification from the messaging menu |
| 211 |
svc.messagingMenu.RemoveNotification(action.Nid, true) |
| 212 |
// this ignores the error (it's been logged already) |
| 213 |
svc.urlDispatcher.DispatchURL(url, action.App) |
| 214 |
} |
| 215 |
case mmuAction, ok := <-mmuActionsCh: |
| 216 |
if !ok { |
| 217 |
break Handle |
| 218 |
} |
| 219 |
if mmuAction == nil { |
| 220 |
svc.Log.Debugf("handleActions (MMU) got nil action; ignoring") |
| 221 |
} else { |
| 222 |
svc.Log.Debugf("handleActions (MMU) got: %v", mmuAction) |
| 223 |
url := mmuAction.Action |
| 224 |
// remove the notification from the messagingmenu map |
| 225 |
svc.messagingMenu.RemoveNotification(mmuAction.Notification, false) |
| 226 |
// this ignores the error (it's been logged already) |
| 227 |
svc.urlDispatcher.DispatchURL(url, mmuAction.App) |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func (svc *PostalService) takeTheBus() (<-chan *notifications.RawAction, error) { |
| 235 |
endps := []struct { |
| 236 |
name string |
| 237 |
endp bus.Endpoint |
| 238 |
}{ |
| 239 |
{"notifications", svc.NotificationsEndp}, |
| 240 |
{"emblemcounter", svc.EmblemCounterEndp}, |
| 241 |
{"accounts", svc.AccountsEndp}, |
| 242 |
{"haptic", svc.HapticEndp}, |
| 243 |
{"greeter", svc.GreeterEndp}, |
| 244 |
{"focusinfo", svc.FocusInfoEndp}, |
| 245 |
} |
| 246 |
for _, endp := range endps { |
| 247 |
if endp.endp == nil { |
| 248 |
svc.Log.Errorf("endpoint for %s is nil", endp.name) |
| 249 |
return nil, ErrNotConfigured |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
var wg sync.WaitGroup |
| 254 |
wg.Add(len(endps)) |
| 255 |
for _, endp := range endps { |
| 256 |
go func(name string, endp bus.Endpoint) { |
| 257 |
util.NewAutoRedialer(endp).Redial() |
| 258 |
wg.Done() |
| 259 |
}(endp.name, endp.endp) |
| 260 |
} |
| 261 |
wg.Wait() |
| 262 |
|
| 263 |
return notifications.Raw(svc.NotificationsEndp, svc.Log, nil).WatchActions() |
| 264 |
} |
| 265 |
|
| 266 |
func (svc *PostalService) listPersistent(msg dbus.Message, id string) ([]string, *dbus.Error) { |
| 267 |
app, err := svc.grabDBusPackageAndAppId(&msg, id) |
| 268 |
if err != nil { |
| 269 |
return nil, MakeDBusNoObjectErrorWithReason( |
| 270 |
msg.Headers[dbus.FieldPath].Value().(string), err) |
| 271 |
} |
| 272 |
|
| 273 |
tagmap := svc.messagingMenu.Tags(app) |
| 274 |
return tagmap, nil |
| 275 |
} |
| 276 |
|
| 277 |
func (svc *PostalService) clearPersistent(msg dbus.Message, id string, tag string) (uint32, *dbus.Error) { |
| 278 |
app, err := svc.grabDBusPackageAndAppId(&msg, id) |
| 279 |
if err != nil { |
| 280 |
return 0, MakeDBusNoObjectErrorWithReason( |
| 281 |
msg.Headers[dbus.FieldPath].Value().(string), err) |
| 282 |
} |
| 283 |
|
| 284 |
// TODO: should we rate-limit this log? |
| 285 |
svc.Log.Infof("App '%s' calls a deprecated method 'clearPersistent'. "+ |
| 286 |
"Uses 'clearPersistentList' instead.", app) |
| 287 |
|
| 288 |
return uint32(svc.messagingMenu.Clear(app, tag)), nil |
| 289 |
} |
| 290 |
|
| 291 |
func (svc *PostalService) clearPersistentList(msg dbus.Message, id string, tags []string) (uint32, *dbus.Error) { |
| 292 |
app, err := svc.grabDBusPackageAndAppId(&msg, id) |
| 293 |
if err != nil { |
| 294 |
return 0, MakeDBusNoObjectErrorWithReason( |
| 295 |
msg.Headers[dbus.FieldPath].Value().(string), err) |
| 296 |
} |
| 297 |
return uint32(svc.messagingMenu.Clear(app, tags...)), nil |
| 298 |
} |
| 299 |
|
| 300 |
func (svc *PostalService) setCounter(msg dbus.Message, id string, count int, visible bool) *dbus.Error { |
| 301 |
app, err := svc.grabDBusPackageAndAppId(&msg, id) |
| 302 |
if err != nil { |
| 303 |
return MakeDBusNoObjectErrorWithReason( |
| 304 |
msg.Headers[dbus.FieldPath].Value().(string), err) |
| 305 |
} |
| 306 |
|
| 307 |
svc.emblemCounter.SetCounter(app, int32(count), visible) |
| 308 |
return nil |
| 309 |
} |
| 310 |
|
| 311 |
func (svc *PostalService) popAll(msg dbus.Message, id string) ([]string, *dbus.Error) { |
| 312 |
app, err := svc.grabDBusPackageAndAppId(&msg, id) |
| 313 |
if err != nil { |
| 314 |
return nil, MakeDBusNoObjectErrorWithReason( |
| 315 |
msg.Headers[dbus.FieldPath].Value().(string), err) |
| 316 |
} |
| 317 |
|
| 318 |
svc.lock.Lock() |
| 319 |
defer svc.lock.Unlock() |
| 320 |
|
| 321 |
if svc.mbox == nil { |
| 322 |
return []string{}, nil |
| 323 |
} |
| 324 |
appId := app.Original() |
| 325 |
box, ok := svc.mbox[appId] |
| 326 |
if !ok { |
| 327 |
return []string{}, nil |
| 328 |
} |
| 329 |
msgs := box.AllMessages() |
| 330 |
delete(svc.mbox, appId) |
| 331 |
|
| 332 |
return msgs, nil |
| 333 |
} |
| 334 |
|
| 335 |
var newNid = func() string { |
| 336 |
return uuid.Must(uuid.NewRandom()).String() |
| 337 |
} |
| 338 |
|
| 339 |
func (svc *PostalService) post(msg dbus.Message, id, notif string) *dbus.Error { |
| 340 |
app, err := svc.grabDBusPackageAndAppId(&msg, id) |
| 341 |
if err != nil { |
| 342 |
return MakeDBusNoObjectErrorWithReason( |
| 343 |
msg.Headers[dbus.FieldPath].Value().(string), err) |
| 344 |
} |
| 345 |
var dummy interface{} |
| 346 |
rawJSON := json.RawMessage(notif) |
| 347 |
err = json.Unmarshal(rawJSON, &dummy) |
| 348 |
if err != nil { |
| 349 |
return dbus.MakeFailedError(ErrBadJSON) |
| 350 |
} |
| 351 |
|
| 352 |
svc.Post(app, "", rawJSON) |
| 353 |
return nil |
| 354 |
} |
| 355 |
|
| 356 |
// Post() signals to an application over dbus that a notification |
| 357 |
// has arrived. If nid is "" generate one. |
| 358 |
func (svc *PostalService) Post(app *click.AppId, nid string, payload json.RawMessage) { |
| 359 |
if nid == "" { |
| 360 |
nid = newNid() |
| 361 |
} |
| 362 |
arg := launch_helper.HelperInput{ |
| 363 |
App: app, |
| 364 |
NotificationId: nid, |
| 365 |
Payload: payload, |
| 366 |
} |
| 367 |
var kind string |
| 368 |
if app.Click { |
| 369 |
kind = "click" |
| 370 |
} else { |
| 371 |
kind = "legacy" |
| 372 |
} |
| 373 |
svc.HelperPool.Run(kind, &arg) |
| 374 |
} |
| 375 |
|
| 376 |
func (svc *PostalService) consumeHelperResults(ch chan *launch_helper.HelperResult) { |
| 377 |
for res := range ch { |
| 378 |
svc.handleHelperResult(res) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func (svc *PostalService) handleHelperResult(res *launch_helper.HelperResult) { |
| 383 |
svc.lock.Lock() |
| 384 |
defer svc.lock.Unlock() |
| 385 |
if svc.mbox == nil { |
| 386 |
svc.mbox = make(map[string]*mBox) |
| 387 |
} |
| 388 |
|
| 389 |
app := res.Input.App |
| 390 |
nid := res.Input.NotificationId |
| 391 |
output := res.HelperOutput |
| 392 |
|
| 393 |
appId := app.Original() |
| 394 |
box, ok := svc.mbox[appId] |
| 395 |
if !ok { |
| 396 |
box = new(mBox) |
| 397 |
svc.mbox[appId] = box |
| 398 |
} |
| 399 |
box.Append(output.Message, nid) |
| 400 |
|
| 401 |
if svc.msgHandler != nil { |
| 402 |
b := svc.msgHandler(app, nid, &output) |
| 403 |
if !b { |
| 404 |
svc.Log.Debugf("msgHandler did not present the notification") |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
svc.Bus.Signal("Post", "/"+string(nih.Quote([]byte(app.Package))), []interface{}{appId}) |
| 409 |
} |
| 410 |
|
| 411 |
func (svc *PostalService) validateActions(app *click.AppId, notif *launch_helper.Notification) bool { |
| 412 |
if notif.Card == nil || len(notif.Card.Actions) == 0 { |
| 413 |
return true |
| 414 |
} |
| 415 |
return svc.urlDispatcher.TestURL(app, notif.Card.Actions) |
| 416 |
} |
| 417 |
|
| 418 |
var areNotificationsEnabled = cnotificationsettings.AreNotificationsEnabled |
| 419 |
|
| 420 |
func (svc *PostalService) messageHandler(app *click.AppId, nid string, output *launch_helper.HelperOutput) bool { |
| 421 |
if output == nil || output.Notification == nil { |
| 422 |
svc.Log.Debugf("skipping notification: nil.") |
| 423 |
return false |
| 424 |
} |
| 425 |
// validate actions |
| 426 |
if !svc.validateActions(app, output.Notification) { |
| 427 |
// no need to log, (it's been logged already) |
| 428 |
return false |
| 429 |
} |
| 430 |
|
| 431 |
locked := svc.greeter.IsActive() |
| 432 |
focused := svc.focusInfo.IsAppFocused(app) |
| 433 |
|
| 434 |
if !locked && focused { |
| 435 |
svc.Log.Debugf("notification skipped because app is focused.") |
| 436 |
return false |
| 437 |
} |
| 438 |
|
| 439 |
if !areNotificationsEnabled(app) { |
| 440 |
svc.Log.Debugf("notification skipped (except emblem counter) because app has notifications disabled") |
| 441 |
return svc.emblemCounter.Present(app, nid, output.Notification) |
| 442 |
} |
| 443 |
|
| 444 |
b := false |
| 445 |
for _, p := range svc.Presenters { |
| 446 |
// we don't want this to shortcut :) |
| 447 |
b = p.Present(app, nid, output.Notification) || b |
| 448 |
} |
| 449 |
return b |
| 450 |
} |