forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.go
137 lines (124 loc) · 3.54 KB
/
browser.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
package playwright
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type browserImpl struct {
channelOwner
isConnected bool
isClosedOrClosing bool
isConnectedOverWebSocket bool
contexts []BrowserContext
}
func (b *browserImpl) IsConnected() bool {
b.RLock()
defer b.RUnlock()
return b.isConnected
}
func (b *browserImpl) NewContext(options ...BrowserNewContextOptions) (BrowserContext, error) {
overrides := map[string]interface{}{}
if len(options) == 1 {
if options[0].ExtraHttpHeaders != nil {
overrides["extraHTTPHeaders"] = serializeMapToNameAndValue(options[0].ExtraHttpHeaders)
options[0].ExtraHttpHeaders = nil
}
if options[0].StorageStatePath != nil {
var storageState *BrowserNewContextOptionsStorageState
storageString, err := ioutil.ReadFile(*options[0].StorageStatePath)
if err != nil {
return nil, fmt.Errorf("could not read storage state file: %w", err)
}
err = json.Unmarshal(storageString, &storageState)
if err != nil {
return nil, fmt.Errorf("could not parse storage state file: %w", err)
}
options[0].StorageState = storageState
options[0].StorageStatePath = nil
}
if options[0].NoViewport != nil && *options[0].NoViewport {
overrides["noDefaultViewport"] = true
options[0].NoViewport = nil
}
if options[0].RecordHarPath != nil {
recordHar := map[string]interface{}{}
recordHar["path"] = *options[0].RecordHarPath
if options[0].RecordHarOmitContent != nil {
recordHar["omitContent"] = true
}
overrides["recordHar"] = recordHar
} else if options[0].RecordHarOmitContent != nil {
return nil, fmt.Errorf("recordHarOmitContent is set but recordHarPath is nil")
}
}
channel, err := b.channel.Send("newContext", overrides, options)
if err != nil {
return nil, fmt.Errorf("could not send message: %w", err)
}
context := fromChannel(channel).(*browserContextImpl)
if len(options) == 1 {
context.options = &options[0]
}
context.browser = b
b.Lock()
b.contexts = append(b.contexts, context)
b.Unlock()
return context, nil
}
func (b *browserImpl) NewPage(options ...BrowserNewContextOptions) (Page, error) {
context, err := b.NewContext(options...)
if err != nil {
return nil, err
}
page, err := context.NewPage()
if err != nil {
return nil, err
}
page.(*pageImpl).ownedContext = context
context.(*browserContextImpl).ownedPage = page
return page, nil
}
func (b *browserImpl) NewBrowserCDPSession() (CDPSession, error) {
channel, err := b.channel.Send("newBrowserCDPSession")
if err != nil {
return nil, fmt.Errorf("could not send message: %w", err)
}
cdpSession := fromChannel(channel).(*cdpSessionImpl)
return cdpSession, nil
}
func (b *browserImpl) Contexts() []BrowserContext {
b.Lock()
defer b.Unlock()
return b.contexts
}
func (b *browserImpl) Close() error {
_, err := b.channel.Send("close")
if err != nil {
return fmt.Errorf("could not send message: %w", err)
}
if b.isConnectedOverWebSocket {
return b.connection.Stop()
}
return nil
}
func (b *browserImpl) Version() string {
return b.initializer["version"].(string)
}
func (b *browserImpl) onClose() {
b.Lock()
if !b.isClosedOrClosing {
b.isConnected = false
b.isClosedOrClosing = true
b.Emit("disconnected")
}
b.Unlock()
}
func newBrowser(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *browserImpl {
bt := &browserImpl{
isConnected: true,
contexts: make([]BrowserContext, 0),
}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
bt.channel.On("close", bt.onClose)
return bt
}