Commit Graph

13 Commits

Author SHA1 Message Date
Daniel Martí
b481eeac51 support fetching errors from cancellation
The API isn't very shiny, but it works. It doesn't matter that much, as
most users won't care about these errors.

Fixes #295.
2019-04-08 18:52:14 +02:00
Daniel Martí
b8efcf0691 support using BrowserOption in NewContext
While at it, remove the error return from BrowserOption, to make it
consistent with all the other option func types.

We don't have a good test for this feature yet, but at least we check
that it doesn't crash or error via an example.

Fixes #292.
2019-04-08 13:03:55 +02:00
Daniel Martí
d0484ed1c5 simplify the allocator API
Exposing NewAllocator and AllocatorOption was unnecessary, and it made
the API more complex to use and understand.

Instead, have users call NewExecAllocator directly. This removes some
code, and simplifies the examples and tests.
2019-04-08 12:10:59 +02:00
Daniel Martí
687cf6d766 wait for cleanup when cancelling the first context
We were doing this for extra open tabs, since NewContext was taking care
of detaching and closing the respective target.

And cancelling an entire allocator also properly waited for all the
resources, such as processes and temporary directories, to be cleaned
up.

However, this didn't work for a browser's first context; cancelling it
should wait for that one browser's resources to be cleaned up, but that
wasn't implemented. Do that, fixing the TODO in TestExecAllocator.
2019-04-07 23:40:29 +02:00
Daniel Martí
939d377090 avoid hanging when Chrome is closed separately
It's Run that actually starts a Browser, not NewContext. If the browser
is closed or crashes, the browser handler will fail to read from the
websocket, and its goroutines will stop.

However, the target handler's goroutines may not stop. The browser
handler uses a separate cancel function to stop itself when encountering
a websocket error, so that doesn't propagate to the original context
children, like the target handler.

To fix this, make it so that NewContext can keep the cancel function
around, for Run to use it in this scenario. And add a test case that
tests this very edge case, which used to time out before the fix.

Fixes #289.
2019-04-07 19:28:41 +02:00
Daniel Martí
b647c708b4 don't create an extra tab when starting a browser
Chrome already starts with a blank page, so use that for the first
target context instead of creating a new tab.

Add the first version of the Targets API, which is useful to test this
feature.

Fixes #291.
2019-04-07 01:18:15 +02:00
Daniel Martí
97e80a00d5 make NewAllocator's cancel func block on Wait
This way, the simple examples and tests don't need to do that
separately. Practically all users will want this cleanup work to be
synchronous, and practically all Go APIs are synchronous by default, so
this makes chromedp easier to use.
2019-04-06 22:13:40 +02:00
Kenneth Shaw
65a198c84e Generic code cleanup
Adding some comments, removing unused items, and renaming handler.go to
target.go to reflect the internal type name changes.
2019-04-03 09:03:41 +02:00
Daniel Martí
32d4bae280 clean up various pieces of the API
First, collapse Browser.Start with NewBrowser. There's no reason to
split them up.

Second, unexport Browser.userDataDir, since it's only needed for a test.
It's also a bad precedent, as only the ExecAllocator will control the
user data directory.

Third, export Context.Browser, since we were already exporting
Context.Allocator.

Finally, remove the Executor interface, a duplicate of cdp.Executor.
2019-04-01 12:18:16 +01:00
Daniel Martí
a93c63124f add some missing godocs on allocators and Run 2019-04-01 12:18:16 +01:00
Daniel Martí
2ca3ea3591 avoid a second map in ExecAllocator
Instead, write the args list directly.
2019-04-01 12:18:16 +01:00
Daniel Martí
81a48280ef route all communication via the browser
Use a single websocket connection per browser, removing the need for an
extra websocket connection per target.

This is thanks to the Target.sendMessageToTarget command to send
messages to each target, and the Target.receivedMessageFromTarget event
to receive messages back.

The browser handles activity via a single worker goroutine, and the same
technique is used for each target. This means that commands and events
are dealt with in order, and we can do away with some complexity like
mutexes and extra go statements.
2019-04-01 12:18:16 +01:00
Daniel Martí
3d3bf22ccc start the chromedp v2 refactor
First, we want all of the functionality in a single package; this means
collapsing whatever is useful into the root chromedp package.

The runner package is being replaced by the Allocator interface, with a
default implementation which starts browser processes.

The client package doesn't really have a place in the new design. The
context, allocator, and browser types will handle the connection with
each browser.

Finally, the new API is context-based, hence the addition of context.go.
The tests have been modified to build and run against the new API.
2019-04-01 12:17:28 +01:00