/
screenlock_service.go
85 lines (70 loc) · 1.98 KB
/
screenlock_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2021 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package ui
import (
"context"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc"
"chromiumos/tast/errors"
"chromiumos/tast/local/chrome"
"chromiumos/tast/local/chrome/uiauto/lockscreen"
"chromiumos/tast/services/cros/ui"
"chromiumos/tast/testing"
)
func init() {
testing.AddService(&testing.Service{
Register: func(srv *grpc.Server, s *testing.ServiceState) {
ui.RegisterScreenLockServiceServer(srv, &ScreenLockService{s: s})
},
})
}
// ScreenLockService implements tast.cros.ui.ScreenLockService.
type ScreenLockService struct {
s *testing.ServiceState
cr *chrome.Chrome
}
func (p *ScreenLockService) ReuseChrome(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
if p.cr != nil {
return nil, errors.New("Chrome already available")
}
cr, err := chrome.New(ctx, chrome.TryReuseSession())
if err != nil {
return nil, err
}
p.cr = cr
return &empty.Empty{}, nil
}
func (p *ScreenLockService) NewChrome(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
if p.cr != nil {
return nil, errors.New("Chrome already available")
}
cr, err := chrome.New(ctx)
if err != nil {
return nil, err
}
p.cr = cr
return &empty.Empty{}, nil
}
func (p *ScreenLockService) CloseChrome(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
if p.cr == nil {
return nil, errors.New("Chrome not available")
}
err := p.cr.Close(ctx)
p.cr = nil
return &empty.Empty{}, err
}
func (p *ScreenLockService) Lock(ctx context.Context, req *empty.Empty) (*empty.Empty, error) {
if p.cr == nil {
return nil, errors.New("Chrome not available")
}
tconn, err := p.cr.TestAPIConn(ctx)
if err != nil {
return nil, err
}
// Lock the screen.
if err := lockscreen.Lock(ctx, tconn); err != nil {
return nil, errors.Wrap(err, "failed to lock the screen")
}
return &empty.Empty{}, nil
}