forked from wendigo/chrome-protocol-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
70 lines (57 loc) · 1.49 KB
/
protocol.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
package main
import "fmt"
type protocolMessage struct {
ID uint64 `json:"id"`
Result map[string]interface{} `json:"result"`
Error struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
} `json:"error"`
Method string `json:"method"`
Params map[string]interface{} `json:"params"`
SessionId string `json:"sessionId"`
}
func (p *protocolMessage) String() string {
return fmt.Sprintf(
"protocolMessage{id=%d, method=%s, sessionId=%s, result=%+v, error=%+v, params=%+v}",
p.ID,
p.Method,
p.SessionId,
p.Result,
p.Error,
p.Params,
)
}
func (p *protocolMessage) IsError() bool {
return p.Error.Code != 0
}
func (p *protocolMessage) IsResponse() bool {
return p.Method == "" && p.ID > 0
}
func (p *protocolMessage) IsRequest() bool {
return p.Method != "" && p.ID > 0
}
func (p *protocolMessage) IsEvent() bool {
return !(p.IsRequest() || p.IsResponse())
}
func (p *protocolMessage) FromTargetDomain() bool {
return p.Method == "Target.sendMessageToTarget" || p.Method == "Target.receivedMessageFromTarget"
}
func (p *protocolMessage) HasSessionId() bool {
return p.FromTargetDomain() || p.IsFlatten()
}
func (p *protocolMessage) IsFlatten() bool {
return p.SessionId != ""
}
func (p *protocolMessage) TargetID() string {
if p.SessionId != "" {
return p.SessionId
}
if p.FromTargetDomain() {
if val, ok := p.Params["sessionId"]; ok {
return val.(string)
}
}
return ""
}