forked from deanishe/alfred-firefox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_server.go
345 lines (306 loc) · 7.94 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) 2020 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
package main
import (
"errors"
"fmt"
"log"
"net"
"net/rpc"
"os"
"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
}
if err = os.Chmod(addr, 0600); err != nil {
return nil, err
}
return s, nil
}
// AppName returns name of browser running the server.
func (s *rpcServer) AppName(_ string, name *string) error {
*name = browserName
return 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
}
if r.Error != "" {
return errors.New(r.Error)
}
*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
}
if r.Error != "" {
return errors.New(r.Error)
}
*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
}
if r.Error != "" {
return errors.New(r.Error)
}
return nil
}
// Tab returns the specified tab. If tabID is 0, returns the active tab.
func (s *rpcServer) Tab(tabID int, tab *Tab) error {
defer util.Timed(time.Now(), "get tab")
var r responseTab
if err := s.ff.call("tab", tabID, &r); err != nil {
return err
}
if r.Error != "" {
return errors.New(r.Error)
}
*tab = r.Tab
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("tab", 0, &r); err != nil {
return err
}
if r.Error != "" {
return errors.New(r.Error)
}
*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
}
if r.Error != "" {
return errors.New(r.Error)
}
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
}
if r.Error != "" {
return errors.New(r.Error)
}
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
}
if r.Error != "" {
return errors.New(r.Error)
}
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
}
if r.Error != "" {
return errors.New(r.Error)
}
*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
}
if r.Error != "" {
return errors.New(r.Error)
}
*history = r.Entries
return nil
}
// Downloads searches Firefox downloads.
func (s *rpcServer) Downloads(query string, downloads *[]Download) error {
defer util.Timed(time.Now(), fmt.Sprintf("search download for %q", query))
var (
r responseDownload
err error
)
err = s.ff.call("search-downloads", query, &r)
if err != nil {
return err
}
if r.Error != "" {
return errors.New(r.Error)
}
*downloads = r.Downloads
return nil
}
// OpenIncognito opens URL in a new incognito window.
func (s *rpcServer) OpenIncognito(URL string, _ *struct{}) error {
defer util.Timed(time.Now(), "open incognito")
var r responseNone
if err := s.ff.call("open-incognito", URL, &r); err != nil {
return err
}
if r.Error != "" {
return errors.New(r.Error)
}
return nil
}
// RunJSArg is the arguments required for RunJS call. TabID may be 0, in which
// case the JavaScript is executed in the active tab.
type RunJSArg struct {
TabID int `json:"tabId"`
JS string `json:"js"`
}
// RunJS executes JavaScript in the specified tab. If tabID is 0, the
// script is executed in the current tab.
func (s *rpcServer) RunJS(arg RunJSArg, JSON *string) error {
defer util.Timed(time.Now(), "execute JS")
var r responseString
if err := s.ff.call("execute-js", arg, &r); err != nil {
return err
}
if r.Error != "" {
return errors.New(r.Error)
}
*JSON = r.String
return nil
}
// RunBookmarkletArg is the 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
}
if r.Error != "" {
return errors.New(r.Error)
}
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"`
Error string `json:"error"`
}
// type responseWindows struct {
// Windows []Window `json:"payload"`
// }
type responseTabs struct {
Tabs []Tab `json:"payload"`
Error string `json:"error"`
}
type responseTab struct {
Tab Tab `json:"payload"`
Error string `json:"error"`
}
type responseHistory struct {
Entries []History `json:"payload"`
Error string `json:"error"`
}
type responseTabCurrent struct {
Tab Tab `json:"payload"`
Error string `json:"error"`
}
type responseBookmarks struct {
Bookmarks []Bookmark `json:"payload"`
Error string `json:"error"`
}
type responseDownload struct {
Downloads []Download `json:"payload"`
Error string `json:"error"`
}
type responseBool struct {
OK bool `json:"payload"`
Error string `json:"error"`
}
type responseNone struct {
Error string `json:"error"`
}