Skip to content

Commit

Permalink
feat: lastore模块逻辑修改
Browse files Browse the repository at this point in the history
原有逻辑: lastore的通知都是dde-session-daemon去监听com.deepin.lastore.Job的状态,持续监听占用资源,而且交互不方便.
逻辑优化: lastore-daemon定义一套agent接口,由dde-daemon的lastore模块实现相关接口并导出,需要发通知和需要获取系统代理时,lastore-daemon调用agent接口完成.

Log: lastore模块逻辑修改
Task: https://pms.uniontech.com/task-view-214983.html
Influence: 通知和apt代理获取
Change-Id: I27df48576528a5932e7263f3945e156dda53dc3b
  • Loading branch information
lichangze committed Nov 29, 2022
1 parent e1d25fa commit 1953d35
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 671 deletions.
117 changes: 117 additions & 0 deletions lastore/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package lastore

import (
"sync"

"github.com/godbus/dbus"
lastore "github.com/linuxdeepin/go-dbus-factory/com.deepin.lastore"
"github.com/linuxdeepin/go-lib/dbusutil"
)

// lastore agent的interface name
const (
sessionAgentInterface = "com.deepin.lastore.Agent"
)

// 对应com.deepin.daemon.Network.GetProxy方法的key值
const (
proxyTypeHttp = "http"
proxyTypeHttps = "https"
proxyTypeFtp = "ftp"
proxyTypeSocks = "socks"
)

// 对应系统代理环境变量
const (
envHttpProxy = "http_proxy"
envHttpsProxy = "https_proxy"
envFtpProxy = "ftp_proxy"
envAllProxy = "all_proxy"
)

// lastoreNotifyType 通知类型
type lastoreNotifyType string

const (
AutoCleanDone lastoreNotifyType = "AutoCleanDone"
AppRemove lastoreNotifyType = "AppRemove"
CanUpdate lastoreNotifyType = "CanUpdate"
AutoDownloading lastoreNotifyType = "AutoDownloading"
CanUpgrade lastoreNotifyType = "CanUpgrade"
UpgradeFailed lastoreNotifyType = "UpgradeFailed"
)

// Status com.deepin.lastore.Job中的Status属性
type Status string

const (
ReadyStatus Status = "ready"
RunningStatus Status = "running"
FailedStatus Status = "failed"
SucceedStatus Status = "succeed"
PausedStatus Status = "paused"
EndStatus Status = "end"
)

// JobInfo com.deepin.lastore.Job中需要的属性
type JobInfo struct {
Id string
Status Status
Name string
Progress float64
Type string
}

// Agent 需要实现GetManualProxy和SendNotify两个接口
type Agent struct {
sysService *dbusutil.Service
lastoreObj *Lastore
sysLastore lastore.Lastore
mu sync.Mutex
}

func newAgent(l *Lastore) (*Agent, error) {
sysBus, err := dbusutil.NewSystemService()
if err != nil {
logger.Warning(err)
return nil, err
}
a := &Agent{
sysService: sysBus,
}
a.lastoreObj = l
a.sysLastore = lastore.NewLastore(a.sysService.Conn())
return a, nil
}

func (a *Agent) init() {
err := a.sysService.Export("/com/deepin/lastore/agent", a)
if err != nil {
logger.Warning(err)
return
}
err = a.sysLastore.Manager().RegisterAgent(0, "/com/deepin/lastore/agent")
if err != nil {
logger.Warning(err)
}
}

func (a *Agent) destroy() {
err := a.sysLastore.Manager().UnRegisterAgent(0, "/com/deepin/lastore/agent")
if err != nil {
logger.Warning(err)
}
}

func (a *Agent) checkCallerAuth(sender dbus.Sender) bool {
const rootUid = 0
uid, err := a.sysService.GetConnUID(string(sender))
if err != nil {
return false
}
if uid != rootUid {
logger.Warningf("not allow %v call this method", sender)
return false
}
return true
}
153 changes: 153 additions & 0 deletions lastore/agent_ifc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package lastore

import (
"errors"
"fmt"
"strings"

"github.com/godbus/dbus"
lastore "github.com/linuxdeepin/go-dbus-factory/com.deepin.lastore"
"github.com/linuxdeepin/go-lib/dbusutil"
"github.com/linuxdeepin/go-lib/gettext"
)

func (*Agent) GetInterfaceName() string {
return sessionAgentInterface
}

func (a *Agent) GetManualProxy(sender dbus.Sender) (map[string]string, *dbus.Error) {
// TODO 获取代理认证信息
if !a.checkCallerAuth(sender) {
return nil, dbusutil.ToError(fmt.Errorf("not allow %v call this method", sender))
}
method, err := a.lastoreObj.network.GetProxyMethod(0)
if err != nil {
logger.Warning(err)
return nil, dbusutil.ToError(err)
}
if method != "manual" {
return nil, dbusutil.ToError(errors.New("only support manual proxy"))
}
proxyTypeList := []string{
proxyTypeHttp, proxyTypeHttps, proxyTypeFtp,
}
proxyEnvMap := make(map[string]string)
for _, typ := range proxyTypeList {
host, port, err := a.lastoreObj.network.GetProxy(0, typ)
if err != nil {
logger.Warning(err)
continue
}
proxyEnvMap[fmt.Sprintf("%s_proxy", typ)] = fmt.Sprintf("%s://%s:%s", proxyTypeHttp, host, port)
}
host, port, err := a.lastoreObj.network.GetProxy(0, proxyTypeSocks)
if err != nil {
logger.Warning(err)
return proxyEnvMap, dbusutil.ToError(err)
}
proxyEnvMap[envAllProxy] = fmt.Sprintf("%s://%s:%s", proxyTypeSocks, host, port)
return proxyEnvMap, nil
}

func (a *Agent) SendNotify(sender dbus.Sender, appName string, replacesId uint32, appIcon string, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) (uint32, *dbus.Error) {
if !a.checkCallerAuth(sender) {
return 0, dbusutil.ToError(fmt.Errorf("not allow %v call this method", sender))
}
logger.Info("receive notify from lastore daemon")
id, err := a.lastoreObj.notifications.Notify(0, appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout)
return id, dbusutil.ToError(err)
//var jobInfo JobInfo
//if jobPath != "" {
// notifyJob, err := lastore.NewJob(a.sysService.Conn(), jobPath)
// if err != nil {
// logger.Warning(err)
// return dbusutil.ToError(err)
// }
//
// jobInfo.Id, _ = notifyJob.Id().Get(dbus.FlagNoAutoStart)
// jobInfo.Type, _ = notifyJob.Type().Get(dbus.FlagNoAutoStart)
// status, _ := notifyJob.Status().Get(dbus.FlagNoAutoStart)
// jobInfo.Status = Status(status)
// jobInfo.Name, _ = notifyJob.Name().Get(dbus.FlagNoAutoStart)
//}
//switch typ {
//case AutoCleanDone:
// a.lastoreObj.notifyAutoClean()
//case AppRemove:
// switch jobInfo.Status {
// case FailedStatus:
// a.lastoreObj.notifyRemove("", false, a.lastoreObj.createJobFailedActions(jobInfo.Id))
// case SucceedStatus:
// a.lastoreObj.notifyRemove("", true, nil)
// }
//case CanUpdate:
// if strings.Contains(jobInfo.Name, "+notify") {
// a.lastoreObj.notifyUpdateSource(a.lastoreObj.createUpdateActions())
// }
//case AutoDownloading:
// msg := gettext.Tr("Auto Downloading")
// a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createUpdateActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
//case CanUpgrade:
// msg := gettext.Tr("Upgrade Available")
// a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createCanUpgradeActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
//case UpgradeFailed:
// // TODO 还需要失败的reason
// msg := gettext.Tr("Upgrade Failed")
// a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createUpgradeFailedActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
// // TODO 电量低的通知
// // TODO 下载完成通知用户去更新
//}
}

func (a *Agent) CloseNotification(sender dbus.Sender, id uint32) *dbus.Error {
if !a.checkCallerAuth(sender) {
return dbusutil.ToError(fmt.Errorf("not allow %v call this method", sender))
}
logger.Info("receive close notify from lastore daemon")
return dbusutil.ToError(a.lastoreObj.notifications.CloseNotification(0, id))
}

// SendNotify2 TODO delete
func (a *Agent) SendNotify2(sender dbus.Sender, appName lastoreNotifyType, replacesId uint32, jobPath dbus.ObjectPath, summary string, body string, actions []string, hints map[string]dbus.Variant, expireTimeout int32) *dbus.Error {
var jobInfo JobInfo
if jobPath != "" {
notifyJob, err := lastore.NewJob(a.sysService.Conn(), jobPath)
if err != nil {
logger.Warning(err)
}

jobInfo.Id, _ = notifyJob.Id().Get(dbus.FlagNoAutoStart)
jobInfo.Type, _ = notifyJob.Type().Get(dbus.FlagNoAutoStart)
status, _ := notifyJob.Status().Get(dbus.FlagNoAutoStart)
jobInfo.Status = Status(status)
jobInfo.Name, _ = notifyJob.Name().Get(dbus.FlagNoAutoStart)
}
switch appName {
case AutoCleanDone:
a.lastoreObj.notifyAutoClean()
case AppRemove:
switch jobInfo.Status {
case FailedStatus:
a.lastoreObj.notifyRemove("", false, a.lastoreObj.createJobFailedActions(jobInfo.Id))
case SucceedStatus:
a.lastoreObj.notifyRemove("", true, nil)
}
case CanUpdate:
if strings.Contains(jobInfo.Name, "+notify") {
a.lastoreObj.notifyUpdateSource(a.lastoreObj.createUpdateActions())
}
case AutoDownloading:
msg := gettext.Tr("Auto Downloading")
a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createUpdateActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
case CanUpgrade:
msg := gettext.Tr("Upgrade Available")
a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createCanUpgradeActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
case UpgradeFailed:
// TODO 还需要失败的reason
msg := gettext.Tr("Upgrade Failed")
a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createUpgradeFailedActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
// TODO 电量低的通知
// TODO 下载完成通知用户去更新
}
return nil
}
56 changes: 34 additions & 22 deletions lastore/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package lastore

import (
"github.com/linuxdeepin/go-lib/log"
"github.com/linuxdeepin/dde-daemon/loader"
"github.com/linuxdeepin/go-lib/log"
)

const (
Expand All @@ -22,6 +22,7 @@ func init() {

type Daemon struct {
lastore *Lastore
agent *Agent
*loader.ModuleBase
}

Expand All @@ -38,13 +39,12 @@ func (*Daemon) GetDependencies() []string {
func (d *Daemon) Start() error {
service := loader.GetService()

lastore, err := newLastore(service)
lastoreObj, err := newLastore(service)
if err != nil {
return err
}
d.lastore = lastore

err = service.Export(dbusPath, lastore, lastore.syncConfig)
d.lastore = lastoreObj
err = service.Export(dbusPath, lastoreObj, lastoreObj.syncConfig)
if err != nil {
return err
}
Expand All @@ -53,32 +53,44 @@ func (d *Daemon) Start() error {
if err != nil {
return err
}

err = lastore.syncConfig.Register()
err = lastoreObj.syncConfig.Register()
if err != nil {
logger.Warning("Failed to register sync service:", err)
}
agent, err := newAgent(lastoreObj)
if err != nil {
logger.Warning(err)
return err
}
d.agent = agent
agent.init()
return nil
}

func (d *Daemon) Stop() error {
if d.lastore == nil {
return nil
if d.lastore != nil {
service := loader.GetService()
err := service.ReleaseName(dbusServiceName)
if err != nil {
logger.Warning(err)
}
d.lastore.destroy()
err = service.StopExport(d.lastore)
if err != nil {
logger.Warning(err)
}

d.lastore = nil
}

service := loader.GetService()
err := service.ReleaseName(dbusServiceName)
if err != nil {
logger.Warning(err)
}

d.lastore.destroy()

err = service.StopExport(d.lastore)
if err != nil {
logger.Warning(err)
if d.agent != nil {
service := loader.GetService()
d.agent.destroy()
err := service.StopExport(d.agent)
if err != nil {
logger.Warning(err)
}
d.agent = nil
}

d.lastore = nil
return nil
}
Loading

0 comments on commit 1953d35

Please sign in to comment.