2019-03-05 13:14:50 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// An Allocator is responsible for creating and managing a number of browsers.
|
|
|
|
//
|
|
|
|
// This interface abstracts away how the browser process is actually run. For
|
|
|
|
// example, an Allocator implementation may reuse browser processes, or connect
|
|
|
|
// to already-running browsers on remote machines.
|
2019-03-05 13:14:50 +00:00
|
|
|
type Allocator interface {
|
2019-03-21 15:21:52 +00:00
|
|
|
// Allocate creates a new browser. It can be cancelled via the provided
|
|
|
|
// context, at which point all the resources used by the browser (such
|
|
|
|
// as temporary directories) will be freed.
|
2019-03-05 13:14:50 +00:00
|
|
|
Allocate(context.Context) (*Browser, error)
|
|
|
|
|
2019-04-06 19:58:54 +00:00
|
|
|
// TODO: Wait should probably return an error, which can then be
|
|
|
|
// retrieved by the user if just calling cancel().
|
|
|
|
|
|
|
|
// Wait blocks until an allocator has freed all of its resources.
|
|
|
|
// Cancelling the context obtained via NewAllocator will already perform
|
|
|
|
// this operation, so normally there's no need to call Wait directly.
|
2019-03-05 13:14:50 +00:00
|
|
|
Wait()
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// NewAllocator creates a new allocator context, suitable for use with
|
|
|
|
// NewContext or Run.
|
2019-03-05 13:14:50 +00:00
|
|
|
func NewAllocator(parent context.Context, opts ...AllocatorOption) (context.Context, context.CancelFunc) {
|
|
|
|
ctx, cancel := context.WithCancel(parent)
|
|
|
|
c := &Context{}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.Allocator)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, contextKey{}, c)
|
2019-04-06 19:58:54 +00:00
|
|
|
cancelWait := func() {
|
|
|
|
cancel()
|
|
|
|
c.Allocator.Wait()
|
|
|
|
}
|
|
|
|
return ctx, cancelWait
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 00:05:46 +00:00
|
|
|
// AllocatorOption is a allocator option.
|
2019-03-05 13:14:50 +00:00
|
|
|
type AllocatorOption func(*Allocator)
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// WithExecAllocator returns an AllocatorOption which sets up an ExecAllocator.
|
2019-03-05 13:14:50 +00:00
|
|
|
func WithExecAllocator(opts ...ExecAllocatorOption) func(*Allocator) {
|
|
|
|
return func(p *Allocator) {
|
|
|
|
ep := &ExecAllocator{
|
|
|
|
initFlags: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(ep)
|
|
|
|
}
|
|
|
|
if ep.execPath == "" {
|
|
|
|
ep.execPath = findExecPath()
|
|
|
|
}
|
|
|
|
*p = ep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 00:05:46 +00:00
|
|
|
// ExecAllocatorOption is a exec allocator option.
|
2019-03-05 13:14:50 +00:00
|
|
|
type ExecAllocatorOption func(*ExecAllocator)
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// ExecAllocator is an Allocator which starts new browser processes on the host
|
|
|
|
// machine.
|
2019-03-05 13:14:50 +00:00
|
|
|
type ExecAllocator struct {
|
|
|
|
execPath string
|
|
|
|
initFlags map[string]interface{}
|
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2019-04-03 00:05:46 +00:00
|
|
|
// Allocate satisfies the Allocator interface.
|
2019-03-05 13:14:50 +00:00
|
|
|
func (p *ExecAllocator) Allocate(ctx context.Context) (*Browser, error) {
|
2019-03-20 17:57:49 +00:00
|
|
|
var args []string
|
2019-03-05 13:14:50 +00:00
|
|
|
for name, value := range p.initFlags {
|
2019-03-20 17:57:49 +00:00
|
|
|
switch value := value.(type) {
|
|
|
|
case string:
|
|
|
|
args = append(args, fmt.Sprintf("--%s=%s", name, value))
|
|
|
|
case bool:
|
|
|
|
if value {
|
|
|
|
args = append(args, fmt.Sprintf("--%s", name))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid exec pool flag")
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 17:57:49 +00:00
|
|
|
removeDir := false
|
|
|
|
dataDir, ok := p.initFlags["user-data-dir"].(string)
|
2019-03-05 13:14:50 +00:00
|
|
|
if !ok {
|
|
|
|
tempDir, err := ioutil.TempDir("", "chromedp-runner")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-20 17:57:49 +00:00
|
|
|
args = append(args, "--user-data-dir="+tempDir)
|
2019-03-05 13:14:50 +00:00
|
|
|
dataDir = tempDir
|
|
|
|
removeDir = true
|
|
|
|
}
|
2019-03-20 17:57:49 +00:00
|
|
|
args = append(args, "--remote-debugging-port=0")
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-03-20 17:57:49 +00:00
|
|
|
var cmd *exec.Cmd
|
2019-03-05 13:14:50 +00:00
|
|
|
p.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
// First wait for the process to be finished.
|
|
|
|
if cmd != nil {
|
|
|
|
cmd.Wait()
|
|
|
|
}
|
|
|
|
// Then delete the temporary user data directory, if needed.
|
|
|
|
if removeDir {
|
|
|
|
os.RemoveAll(dataDir)
|
|
|
|
}
|
|
|
|
p.wg.Done()
|
|
|
|
}()
|
|
|
|
|
2019-04-06 20:32:02 +00:00
|
|
|
// force the first page to be blank, instead of the welcome page
|
|
|
|
// TODO: why isn't --no-first-run enough?
|
|
|
|
args = append(args, "about:blank")
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
cmd = exec.CommandContext(ctx, p.execPath, args...)
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick up the browser's websocket URL from stderr.
|
|
|
|
wsURL := ""
|
|
|
|
scanner := bufio.NewScanner(stderr)
|
|
|
|
prefix := "DevTools listening on"
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if s := strings.TrimPrefix(line, prefix); s != line {
|
|
|
|
wsURL = strings.TrimSpace(s)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stderr.Close()
|
|
|
|
|
2019-03-15 17:17:57 +00:00
|
|
|
browser, err := NewBrowser(ctx, wsURL)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-07 17:25:03 +00:00
|
|
|
browser.process = cmd.Process
|
2019-03-21 15:44:28 +00:00
|
|
|
browser.userDataDir = dataDir
|
2019-03-05 13:14:50 +00:00
|
|
|
return browser, nil
|
|
|
|
}
|
|
|
|
|
2019-04-03 00:05:46 +00:00
|
|
|
// Wait satisfies the Allocator interface.
|
2019-03-05 13:14:50 +00:00
|
|
|
func (p *ExecAllocator) Wait() {
|
|
|
|
p.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:21:52 +00:00
|
|
|
// ExecPath returns an ExecAllocatorOption which uses the given path to execute
|
|
|
|
// browser processes. The given path can be an absolute path to a binary, or
|
|
|
|
// just the name of the program to find via exec.LookPath.
|
2019-03-05 13:14:50 +00:00
|
|
|
func ExecPath(path string) ExecAllocatorOption {
|
|
|
|
return func(p *ExecAllocator) {
|
|
|
|
if fullPath, _ := exec.LookPath(path); fullPath != "" {
|
|
|
|
// Convert to an absolute path if possible, to avoid
|
|
|
|
// repeated LookPath calls in each Allocate.
|
|
|
|
path = fullPath
|
|
|
|
}
|
|
|
|
p.execPath = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// findExecPath tries to find the Chrome browser somewhere in the current
|
|
|
|
// system. It performs a rather agressive search, which is the same in all
|
|
|
|
// systems. That may make it a bit slow, but it will only be run when creating a
|
|
|
|
// new ExecAllocator.
|
|
|
|
func findExecPath() string {
|
|
|
|
for _, path := range [...]string{
|
|
|
|
// Unix-like
|
|
|
|
"headless_shell",
|
2019-04-03 00:05:46 +00:00
|
|
|
"headless-shell",
|
2019-03-05 13:14:50 +00:00
|
|
|
"chromium",
|
|
|
|
"chromium-browser",
|
|
|
|
"google-chrome",
|
|
|
|
"google-chrome-stable",
|
|
|
|
"google-chrome-beta",
|
|
|
|
"google-chrome-unstable",
|
|
|
|
"/usr/bin/google-chrome",
|
|
|
|
|
|
|
|
// Windows
|
|
|
|
"chrome",
|
|
|
|
"chrome.exe", // in case PATHEXT is misconfigured
|
|
|
|
`C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
|
|
|
|
|
|
|
|
// Mac
|
|
|
|
`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`,
|
|
|
|
} {
|
|
|
|
found, err := exec.LookPath(path)
|
|
|
|
if err == nil {
|
|
|
|
return found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fall back to something simple and sensible, to give a useful error
|
|
|
|
// message.
|
|
|
|
return "google-chrome"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flag is a generic command line option to pass a flag to Chrome. If the value
|
|
|
|
// is a string, it will be passed as --name=value. If it's a boolean, it will be
|
|
|
|
// passed as --name if value is true.
|
|
|
|
func Flag(name string, value interface{}) ExecAllocatorOption {
|
|
|
|
return func(p *ExecAllocator) {
|
|
|
|
p.initFlags[name] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserDataDir is the command line option to set the user data dir.
|
|
|
|
//
|
|
|
|
// Note: set this option to manually set the profile directory used by Chrome.
|
|
|
|
// When this is not set, then a default path will be created in the /tmp
|
|
|
|
// directory.
|
|
|
|
func UserDataDir(dir string) ExecAllocatorOption {
|
|
|
|
return Flag("user-data-dir", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProxyServer is the command line option to set the outbound proxy server.
|
|
|
|
func ProxyServer(proxy string) ExecAllocatorOption {
|
|
|
|
return Flag("proxy-server", proxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WindowSize is the command line option to set the initial window size.
|
|
|
|
func WindowSize(width, height int) ExecAllocatorOption {
|
|
|
|
return Flag("window-size", fmt.Sprintf("%d,%d", width, height))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserAgent is the command line option to set the default User-Agent
|
|
|
|
// header.
|
|
|
|
func UserAgent(userAgent string) ExecAllocatorOption {
|
|
|
|
return Flag("user-agent", userAgent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoSandbox is the Chrome comamnd line option to disable the sandbox.
|
|
|
|
func NoSandbox(p *ExecAllocator) {
|
|
|
|
Flag("no-sandbox", true)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoFirstRun is the Chrome comamnd line option to disable the first run
|
|
|
|
// dialog.
|
|
|
|
func NoFirstRun(p *ExecAllocator) {
|
|
|
|
Flag("no-first-run", true)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoDefaultBrowserCheck is the Chrome comamnd line option to disable the
|
|
|
|
// default browser check.
|
|
|
|
func NoDefaultBrowserCheck(p *ExecAllocator) {
|
|
|
|
Flag("no-default-browser-check", true)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Headless is the command line option to run in headless mode.
|
|
|
|
func Headless(p *ExecAllocator) {
|
|
|
|
Flag("headless", true)(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableGPU is the command line option to disable the GPU process.
|
|
|
|
func DisableGPU(p *ExecAllocator) {
|
|
|
|
Flag("disable-gpu", true)(p)
|
|
|
|
}
|