forked from deanishe/alfred-firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_server.go
229 lines (197 loc) · 5.4 KB
/
rpc_server.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) 2020 Dean Jackson <[email protected]>
// MIT Licence applies https://opensource.org/licenses/MIT
package main
import (
"fmt"
"log"
"net"
"net/rpc"
"time"
"github.com/deanishe/awgo/util"
)
// rpcServer provides the RPC API. It passes commands and responses between
// RPC clients and the Firefox extension.
type rpcServer struct {
ff *firefox // native application client run by FF extension
sock string // path to UNIX socket for RPC
listener net.Listener
server *rpc.Server
}
// create new RPC server on socket specified by filepath addr
func newRPCService(addr string, client *firefox) (*rpcServer, error) {
var err error
s := &rpcServer{
ff: client,
sock: addr,
server: rpc.NewServer(),
}
if err = s.server.RegisterName("Firefox", s); err != nil {
return nil, err
}
if s.listener, err = net.Listen("unix", s.sock); err != nil {
return nil, err
}
return s, nil
}
// Ping checks connection to Firefox extension. Extension responds with "pong".
func (s *rpcServer) Ping(_ string, result *string) error {
defer util.Timed(time.Now(), "ping")
var r responseString
if err := s.ff.call("ping", nil, &r); err != nil {
return err
}
*result = r.String
return nil
}
// func (s *rpcServer) Windows(_ string, windows *[]Window) error {
// defer util.Timed(time.Now(), "get windows")
// var r responseWindows
// if err := s.ff.call("all-windows", nil, &r); err != nil {
// return err
// }
// *windows = r.Windows
// return nil
// }
// Tabs returns all Firefox tabs.
func (s *rpcServer) Tabs(_ string, tabs *[]Tab) error {
defer util.Timed(time.Now(), "get tabs")
var r responseTabs
if err := s.ff.call("all-tabs", nil, &r); err != nil {
return err
}
*tabs = r.Tabs
return nil
}
// ActivateTab brings the specified tab to the front.
func (s *rpcServer) ActivateTab(tabID int, _ *struct{}) error {
defer util.Timed(time.Now(), "activate tab")
var r responseNone
if err := s.ff.call("activate-tab", tabID, &r); err != nil {
return err
}
return nil
}
// CurrentTab returns the currently-active tab.
func (s *rpcServer) CurrentTab(_ string, tab *Tab) error {
defer util.Timed(time.Now(), "get current tab")
var r responseTab
if err := s.ff.call("current-tab", nil, &r); err != nil {
return err
}
*tab = r.Tab
return nil
}
// CloseTabsLeft closes tabs to the left of specified tab.
func (s *rpcServer) CloseTabsLeft(tabID int, _ *struct{}) error {
defer util.Timed(time.Now(), "close tabs to left")
var r responseNone
if err := s.ff.call("close-tabs-left", tabID, &r); err != nil {
return err
}
return nil
}
// CloseTabsRight closes tabs to the right of specified tab.
func (s *rpcServer) CloseTabsRight(tabID int, _ *struct{}) error {
defer util.Timed(time.Now(), "close tabs to right")
var r responseNone
if err := s.ff.call("close-tabs-right", tabID, &r); err != nil {
return err
}
return nil
}
// CloseTabsOther closes other tabs in same window as the specified one.
func (s *rpcServer) CloseTabsOther(tabID int, _ *struct{}) error {
defer util.Timed(time.Now(), "close other tabs")
var r responseNone
if err := s.ff.call("close-tabs-other", tabID, &r); err != nil {
return err
}
return nil
}
// Bookmarks returns all Firefox bookmarks matching query.
func (s *rpcServer) Bookmarks(query string, bookmarks *[]Bookmark) error {
defer util.Timed(time.Now(), fmt.Sprintf("search bookmarks for %q", query))
var (
r responseBookmarks
err error
)
if query == "" {
err = s.ff.call("all-bookmarks", nil, &r)
} else {
err = s.ff.call("search-bookmarks", query, &r)
}
if err != nil {
return err
}
*bookmarks = r.Bookmarks
return nil
}
// History searches Firefox browsing history.
func (s *rpcServer) History(query string, history *[]History) error {
defer util.Timed(time.Now(), fmt.Sprintf("search history for %q", query))
var (
r responseHistory
err error
)
err = s.ff.call("search-history", query, &r)
if err != nil {
return err
}
*history = r.Entries
return nil
}
// func (s *rpcServer) RunJS(script string, _ *struct{}) error {
// defer util.Timed(time.Now(), "execute JS")
// var r responseNone
// if err := s.ff.call("execute-js", script, &r); err != nil {
// return err
// }
// return nil
// }
// arguments required for RunBookmarklet call. TabID may be 0, in which
// case the bookmarklet is executed in the active tab.
type RunBookmarkletArg struct {
TabID int `json:"tabId"`
BookmarkID string `json:"bookmarkId"`
}
// RunBookmarklet executes a given bookmarklet in a given tab.
func (s *rpcServer) RunBookmarklet(arg RunBookmarkletArg, _ *struct{}) error {
defer util.Timed(time.Now(), "run bookmarklet")
var r responseNone
if err := s.ff.call("run-bookmarklet", arg, &r); err != nil {
return err
}
return nil
}
func (s *rpcServer) run() {
log.Printf("serving RPC on %q ...", s.sock)
s.server.Accept(s.listener)
}
func (s *rpcServer) stop() error {
return s.listener.Close()
}
type responseString struct {
String string `json:"payload"`
}
// type responseWindows struct {
// Windows []Window `json:"payload"`
// }
type responseTabs struct {
Tabs []Tab `json:"payload"`
}
type responseTab struct {
Tab Tab `json:"payload"`
}
type responseHistory struct {
Entries []History `json:"payload"`
}
type responseTabCurrent struct {
Tab Tab `json:"payload"`
}
type responseBookmarks struct {
Bookmarks []Bookmark `json:"payload"`
}
type responseBool struct {
OK bool `json:"payload"`
}
type responseNone struct{}