Skip to content

Commit 538f692

Browse files
author
lichangze
committed
feat: lastore模块逻辑修改
原有逻辑: 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
1 parent e1d25fa commit 538f692

File tree

8 files changed

+310
-671
lines changed

8 files changed

+310
-671
lines changed

lastore/agent.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package lastore
2+
3+
import (
4+
"github.com/godbus/dbus"
5+
"sync"
6+
7+
lastore "github.com/linuxdeepin/go-dbus-factory/com.deepin.lastore"
8+
"github.com/linuxdeepin/go-lib/dbusutil"
9+
)
10+
11+
// lastore agent的interface name
12+
const (
13+
sessionAgentInterface = "com.deepin.lastore.Agent"
14+
)
15+
16+
// 对应com.deepin.daemon.Network.GetProxy方法的key值
17+
const (
18+
proxyTypeHttp = "http"
19+
proxyTypeHttps = "https"
20+
proxyTypeFtp = "ftp"
21+
proxyTypeSocks = "socks"
22+
)
23+
24+
// 对应系统代理环境变量
25+
const (
26+
envHttpProxy = "http_proxy"
27+
envHttpsProxy = "https_proxy"
28+
envFtpProxy = "ftp_proxy"
29+
envAllProxy = "all_proxy"
30+
)
31+
32+
// lastoreNotifyType 通知类型
33+
type lastoreNotifyType string
34+
35+
const (
36+
AutoCleanDone lastoreNotifyType = "AutoCleanDone"
37+
AppRemove lastoreNotifyType = "AppRemove"
38+
CanUpdate lastoreNotifyType = "CanUpdate"
39+
AutoDownloading lastoreNotifyType = "AutoDownloading"
40+
CanUpgrade lastoreNotifyType = "CanUpgrade"
41+
UpgradeFailed lastoreNotifyType = "UpgradeFailed"
42+
)
43+
44+
// Status com.deepin.lastore.Job中的Status属性
45+
type Status string
46+
47+
const (
48+
ReadyStatus Status = "ready"
49+
RunningStatus Status = "running"
50+
FailedStatus Status = "failed"
51+
SucceedStatus Status = "succeed"
52+
PausedStatus Status = "paused"
53+
EndStatus Status = "end"
54+
)
55+
56+
// JobInfo com.deepin.lastore.Job中需要的属性
57+
type JobInfo struct {
58+
Id string
59+
Status Status
60+
Name string
61+
Progress float64
62+
Type string
63+
}
64+
65+
// Agent 需要实现GetManualProxy和SendNotify两个接口
66+
type Agent struct {
67+
sysService *dbusutil.Service
68+
lastoreObj *Lastore
69+
sysLastore lastore.Lastore
70+
mu sync.Mutex
71+
}
72+
73+
func newAgent(l *Lastore) (*Agent, error) {
74+
sysBus, err := dbusutil.NewSystemService()
75+
if err != nil {
76+
logger.Warning(err)
77+
return nil, err
78+
}
79+
a := &Agent{
80+
sysService: sysBus,
81+
}
82+
a.lastoreObj = l
83+
a.sysLastore = lastore.NewLastore(a.sysService.Conn())
84+
return a, nil
85+
}
86+
87+
func (a *Agent) init() {
88+
err := a.sysService.Export("/com/deepin/lastore/agent", a)
89+
if err != nil {
90+
logger.Warning(err)
91+
return
92+
}
93+
err = a.sysLastore.Manager().RegisterAgent(0, "/com/deepin/lastore/agent")
94+
if err != nil {
95+
logger.Warning(err)
96+
}
97+
}
98+
99+
func (a *Agent) destroy() {
100+
err := a.sysLastore.Manager().UnRegisterAgent(0, "/com/deepin/lastore/agent")
101+
if err != nil {
102+
logger.Warning(err)
103+
}
104+
}
105+
106+
func (a *Agent) checkCallerAuth(sender dbus.Sender) bool {
107+
return false
108+
}

lastore/agent_ifc.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package lastore
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/godbus/dbus"
9+
lastore "github.com/linuxdeepin/go-dbus-factory/com.deepin.lastore"
10+
"github.com/linuxdeepin/go-lib/dbusutil"
11+
"github.com/linuxdeepin/go-lib/gettext"
12+
)
13+
14+
func (*Agent) GetInterfaceName() string {
15+
return sessionAgentInterface
16+
}
17+
18+
func (a *Agent) GetManualProxy(sender dbus.Sender) (map[string]string, *dbus.Error) {
19+
// TODO 获取代理认证信息
20+
a.checkCallerAuth(sender)
21+
method, err := a.lastoreObj.network.GetProxyMethod(0)
22+
if err != nil {
23+
logger.Warning(err)
24+
return nil, dbusutil.ToError(err)
25+
}
26+
if method != "manual" {
27+
return nil, dbusutil.ToError(errors.New("only support manual proxy"))
28+
}
29+
proxyTypeList := []string{
30+
proxyTypeHttp, proxyTypeHttps, proxyTypeFtp,
31+
}
32+
proxyEnvMap := make(map[string]string)
33+
for _, typ := range proxyTypeList {
34+
host, port, err := a.lastoreObj.network.GetProxy(0, typ)
35+
if err != nil {
36+
logger.Warning(err)
37+
continue
38+
}
39+
proxyEnvMap[fmt.Sprintf("%s_proxy", typ)] = fmt.Sprintf("%s://%s:%s", proxyTypeHttp, host, port)
40+
}
41+
host, port, err := a.lastoreObj.network.GetProxy(0, proxyTypeSocks)
42+
if err != nil {
43+
logger.Warning(err)
44+
return proxyEnvMap, dbusutil.ToError(err)
45+
}
46+
proxyEnvMap[envAllProxy] = fmt.Sprintf("%s://%s:%s", proxyTypeSocks, host, port)
47+
return proxyEnvMap, nil
48+
}
49+
50+
func (a *Agent) SendNotify(sender dbus.Sender, typ lastoreNotifyType, jobPath dbus.ObjectPath, args []string) *dbus.Error {
51+
a.checkCallerAuth(sender)
52+
logger.Info("receive notify from lastore")
53+
var jobInfo JobInfo
54+
if jobPath != "" {
55+
notifyJob, err := lastore.NewJob(a.sysService.Conn(), jobPath)
56+
if err != nil {
57+
logger.Warning(err)
58+
return dbusutil.ToError(err)
59+
}
60+
61+
jobInfo.Id, _ = notifyJob.Id().Get(dbus.FlagNoAutoStart)
62+
jobInfo.Type, _ = notifyJob.Type().Get(dbus.FlagNoAutoStart)
63+
status, _ := notifyJob.Status().Get(dbus.FlagNoAutoStart)
64+
jobInfo.Status = Status(status)
65+
jobInfo.Name, _ = notifyJob.Name().Get(dbus.FlagNoAutoStart)
66+
}
67+
switch typ {
68+
case AutoCleanDone:
69+
a.lastoreObj.notifyAutoClean()
70+
case AppRemove:
71+
switch jobInfo.Status {
72+
case FailedStatus:
73+
a.lastoreObj.notifyRemove("", false, a.lastoreObj.createJobFailedActions(jobInfo.Id))
74+
case SucceedStatus:
75+
a.lastoreObj.notifyRemove("", true, nil)
76+
}
77+
case CanUpdate:
78+
if strings.Contains(jobInfo.Name, "+notify") {
79+
a.lastoreObj.notifyUpdateSource(a.lastoreObj.createUpdateActions())
80+
}
81+
case AutoDownloading:
82+
msg := gettext.Tr("Auto Downloading")
83+
a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createUpdateActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
84+
case CanUpgrade:
85+
msg := gettext.Tr("Upgrade Available")
86+
a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createCanUpgradeActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
87+
case UpgradeFailed:
88+
// TODO 还需要失败的reason
89+
msg := gettext.Tr("Upgrade Failed")
90+
a.lastoreObj.sendNotify("preferences-system", "", msg, a.lastoreObj.createUpgradeFailedActions(), nil, notifyExpireTimeoutDefault, "dde-control-center")
91+
// TODO 电量低的通知
92+
// TODO 下载完成通知用户去更新
93+
}
94+
95+
return nil
96+
}

lastore/daemon.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
package lastore
66

77
import (
8-
"github.com/linuxdeepin/go-lib/log"
98
"github.com/linuxdeepin/dde-daemon/loader"
9+
"github.com/linuxdeepin/go-lib/log"
1010
)
1111

1212
const (
@@ -22,6 +22,7 @@ func init() {
2222

2323
type Daemon struct {
2424
lastore *Lastore
25+
agent *Agent
2526
*loader.ModuleBase
2627
}
2728

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

41-
lastore, err := newLastore(service)
42+
lastoreObj, err := newLastore(service)
4243
if err != nil {
4344
return err
4445
}
45-
d.lastore = lastore
46-
47-
err = service.Export(dbusPath, lastore, lastore.syncConfig)
46+
d.lastore = lastoreObj
47+
err = service.Export(dbusPath, lastoreObj, lastoreObj.syncConfig)
4848
if err != nil {
4949
return err
5050
}
@@ -53,32 +53,44 @@ func (d *Daemon) Start() error {
5353
if err != nil {
5454
return err
5555
}
56-
57-
err = lastore.syncConfig.Register()
56+
err = lastoreObj.syncConfig.Register()
5857
if err != nil {
5958
logger.Warning("Failed to register sync service:", err)
6059
}
60+
agent, err := newAgent(lastoreObj)
61+
if err != nil {
62+
logger.Warning(err)
63+
return err
64+
}
65+
d.agent = agent
66+
agent.init()
6167
return nil
6268
}
6369

6470
func (d *Daemon) Stop() error {
65-
if d.lastore == nil {
66-
return nil
71+
if d.lastore != nil {
72+
service := loader.GetService()
73+
err := service.ReleaseName(dbusServiceName)
74+
if err != nil {
75+
logger.Warning(err)
76+
}
77+
d.lastore.destroy()
78+
err = service.StopExport(d.lastore)
79+
if err != nil {
80+
logger.Warning(err)
81+
}
82+
83+
d.lastore = nil
6784
}
6885

69-
service := loader.GetService()
70-
err := service.ReleaseName(dbusServiceName)
71-
if err != nil {
72-
logger.Warning(err)
73-
}
74-
75-
d.lastore.destroy()
76-
77-
err = service.StopExport(d.lastore)
78-
if err != nil {
79-
logger.Warning(err)
86+
if d.agent != nil {
87+
service := loader.GetService()
88+
d.agent.destroy()
89+
err := service.StopExport(d.agent)
90+
if err != nil {
91+
logger.Warning(err)
92+
}
93+
d.agent = nil
8094
}
81-
82-
d.lastore = nil
8395
return nil
8496
}

lastore/exported_methods_auto.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)