Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sketch for implementing sessions with flattened protocol. #109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions cdp_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions cmd/cdpgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,27 +297,36 @@ func (g *Generator) format() []byte {
// CdpClient creates the cdp.Client type.
func (g *Generator) CdpClient(domains []proto.Domain) {
g.hasContent = true
var fields, newFields Generator
var fields, newFields, newSessionFields Generator
for _, d := range domains {
fields.Printf("\t%s %s\n", d.Name(), d.Type())
newFields.Printf("\t\t%s: %s.NewClient(conn),\n", d.Name(), strings.ToLower(d.Name()))
newSessionFields.Printf("\t\t%s: %s.NewSessionClient(c.Conn, sessionID),\n", d.Name(), strings.ToLower(d.Name()))
}
g.Printf(`
// Client represents a Chrome DevTools Protocol client that can be used to
// invoke methods or listen to events in every CDP domain. The Client consumes
// a rpcc connection, used to invoke the methods.
type Client struct {
Conn *rpcc.Conn
%s
}

// NewClient returns a new Client that uses conn
// for communication with the debugging target.
func NewClient(conn *rpcc.Conn) *Client {
return &Client{
Conn: conn,
%s
}
}
`, fields.buf.Bytes(), newFields.buf.Bytes())

func (c *Client) NewSession(sessionID string) *Client {
return &Client{
%s
}
}
`, fields.buf.Bytes(), newFields.buf.Bytes(), newSessionFields.buf.Bytes())
}

// PackageHeader writes the header for a package.
Expand Down Expand Up @@ -406,12 +415,17 @@ func (g *Generator) DomainDefinition(d proto.Domain) {
comment := fmt.Sprintf("domainClient is a client for the %s domain. ", d.Name())
g.Printf(`
// %[1]s%[3]s
type domainClient struct{ conn *rpcc.Conn }
type domainClient struct{ conn *rpcc.Conn; sessionID string }

// NewClient returns a client for the %[2]s domain with the connection set to conn.
func NewClient(conn *rpcc.Conn) *domainClient {
return &domainClient{conn: conn}
}

// NewClient returns a client for the %[2]s domain with the connection set to conn.
func NewSessionClient(conn *rpcc.Conn, sessionID string) *domainClient {
return &domainClient{conn: conn, sessionID: sessionID}
}
`, comment, d.Name(), d.Desc(0, len(comment)))

for _, c := range d.Commands {
Expand Down Expand Up @@ -439,9 +453,9 @@ func (d *domainClient) %[1]s(ctx context.Context%[3]s) %[4]s {`, c.Name(), comme
if len(c.Parameters) > 0 {
g.Printf(`
if args != nil {
err = rpcc.Invoke(ctx, %[1]q, args, %[2]s, d.conn)
err = rpcc.InvokeRPC(ctx, %[1]q, d.sessionID, args, %[2]s, d.conn)
} else {
err = rpcc.Invoke(ctx, %[1]q, nil, %[2]s, d.conn)
err = rpcc.InvokeRPC(ctx, %[1]q, d.sessionID, nil, %[2]s, d.conn)
}
if err != nil {
err = &internal.OpError{Domain: %[3]q, Op: %[4]q, Err: err}
Expand All @@ -451,7 +465,7 @@ func (d *domainClient) %[1]s(ctx context.Context%[3]s) %[4]s {`, c.Name(), comme
`, d.Domain+"."+c.NameName, invokeReply, d.Name(), c.Name())
} else {
g.Printf(`
err = rpcc.Invoke(ctx, %q, nil, %s, d.conn)
err = rpcc.InvokeRPC(ctx, %q, d.sessionID, nil, %s, d.conn)
if err != nil {
err = &internal.OpError{Domain: %q, Op: %q, Err: err}
}
Expand Down Expand Up @@ -516,7 +530,7 @@ func Test%[1]s_%[2]s(t *testing.T) {
// Implement event on domain.
g.Printf(`
func (d *domainClient) %s(ctx context.Context) (%s, error) {
s, err := rpcc.NewStream(ctx, %q, d.conn)
s, err := rpcc.NewStream(ctx, %q, d.sessionID, d.conn)
if err != nil {
return nil, err
}
Expand Down
20 changes: 14 additions & 6 deletions protocol/accessibility/domain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 29 additions & 21 deletions protocol/animation/domain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.