chromedp/cmd/chromedp-gen/fixup/fixup.go

563 lines
17 KiB
Go
Raw Normal View History

2017-01-24 15:09:23 +00:00
package fixup
import (
"fmt"
"strings"
2017-01-26 07:28:34 +00:00
"github.com/knq/chromedp/cmd/chromedp-gen/internal"
2017-01-24 15:09:23 +00:00
"github.com/knq/chromedp/cmd/chromedp-gen/templates"
)
func init() {
// set the internal types
2017-01-26 07:28:34 +00:00
internal.SetCDPTypes(map[string]bool{
2017-01-24 15:09:23 +00:00
"DOM.BackendNodeId": true,
"DOM.BackendNode": true,
"DOM.NodeId": true,
"DOM.Node": true,
"DOM.NodeType": true,
"DOM.PseudoType": true,
"DOM.RGBA": true,
"DOM.ShadowRootType": true,
"Inspector.ErrorType": true,
"Inspector.MessageError": true,
"Inspector.Message": true,
"Inspector.MethodType": true,
"Network.LoaderId": true,
"Network.Timestamp": true,
"Page.FrameId": true,
"Page.Frame": true,
})
}
// if the internal type locations change above, these will also need to change:
const (
2017-01-26 07:28:34 +00:00
domNodeIDRef = "NodeID"
domNodeRef = "*Node"
2017-01-24 15:09:23 +00:00
)
// FixupDomains changes types in the domains, so that the generated code is
// more type specific, and easier to use.
//
// currently:
// - add 'Inspector.MethodType' type as a string enumeration of all the event/command names.
// - add 'Inspector.MessageError' type as a object with code (integer), and message (string).
// - add 'Inspector.Message' type as a object with id (integer), method (MethodType), params (interface{}), error (MessageError).
// - add 'Inspector.DetachReason' type and change event 'Inspector.detached''s parameter reason's type.
// - add 'Inspector.ErrorType' type.
// - change 'Runtime.Timestamp' to 'Network.Timestamp'.
// - change any object property or command/event parameter named 'timestamp'
// or has $ref to Network/Runtime.Timestamp to type 'Network.Timestamp'.
// - convert object properties and event/command parameters that are enums into independent types.
// - change '*.modifiers' parameters to type Input.Modifier.
// - add 'DOM.NodeType' type and convert "nodeType" parameters to it.
// - change Page.Frame.id/parentID to FrameID type.
// - add additional properties to 'Page.Frame' and 'DOM.Node' for use by higher level packages.
// - add special unmarshaler to NodeId, BackendNodeId, FrameId to handle values from older (v1.1) protocol versions. -- NOTE: this might need to be applied to more types, such as network.LoaderId
// - rename 'Input.GestureSourceType' -> 'Input.GestureType'.
// - rename CSS.CSS* types.
// - add Error() method to 'Runtime.ExceptionDetails' type so that it can be used as error.
2017-01-26 07:28:34 +00:00
func FixupDomains(domains []*internal.Domain) {
2017-01-24 15:09:23 +00:00
// method type
2017-01-26 07:28:34 +00:00
methodType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "MethodType",
2017-01-26 07:28:34 +00:00
Type: internal.TypeString,
2017-01-24 15:09:23 +00:00
Description: "Chrome Debugging Protocol method type (ie, event and command names).",
EnumValueNameMap: make(map[string]string),
Extra: templates.ExtraMethodTypeDomainDecoder(),
}
// message error type
2017-01-26 07:28:34 +00:00
messageErrorType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "MessageError",
2017-01-26 07:28:34 +00:00
Type: internal.TypeObject,
2017-01-24 15:09:23 +00:00
Description: "Message error type.",
2017-01-26 07:28:34 +00:00
Properties: []*internal.Type{
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "code",
2017-01-26 07:28:34 +00:00
Type: internal.TypeInteger,
2017-01-24 15:09:23 +00:00
Description: "Error code.",
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "message",
2017-01-26 07:28:34 +00:00
Type: internal.TypeString,
2017-01-24 15:09:23 +00:00
Description: "Error message.",
},
},
Extra: "// Error satisfies error interface.\nfunc (e *MessageError) Error() string {\nreturn fmt.Sprintf(\"%s (%d)\", e.Message, e.Code)\n}\n",
}
// message type
2017-01-26 07:28:34 +00:00
messageType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "Message",
2017-01-26 07:28:34 +00:00
Type: internal.TypeObject,
2017-01-24 15:09:23 +00:00
Description: "Chrome Debugging Protocol message sent to/read over websocket connection.",
2017-01-26 07:28:34 +00:00
Properties: []*internal.Type{
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "id",
2017-01-26 07:28:34 +00:00
Type: internal.TypeInteger,
2017-01-24 15:09:23 +00:00
Description: "Unique message identifier.",
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "method",
Ref: "Inspector.MethodType",
Description: "Event or command type.",
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "params",
2017-01-26 07:28:34 +00:00
Type: internal.TypeAny,
2017-01-24 15:09:23 +00:00
Description: "Event or command parameters.",
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "result",
2017-01-26 07:28:34 +00:00
Type: internal.TypeAny,
2017-01-24 15:09:23 +00:00
Description: "Command return values.",
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "error",
Ref: "MessageError",
Description: "Error message.",
},
},
}
// detach reason type
2017-01-26 07:28:34 +00:00
detachReasonType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "DetachReason",
2017-01-26 07:28:34 +00:00
Type: internal.TypeString,
2017-01-24 15:09:23 +00:00
Enum: []string{"target_closed", "canceled_by_user", "replaced_with_devtools", "Render process gone."},
Description: "Detach reason.",
}
// cdp error types
errorValues := []string{"context done", "channel closed", "invalid result", "unknown result"}
errorValueNameMap := make(map[string]string)
for _, e := range errorValues {
2017-01-26 07:28:34 +00:00
errorValueNameMap[e] = "Err" + internal.ForceCamel(e)
2017-01-24 15:09:23 +00:00
}
2017-01-26 07:28:34 +00:00
errorType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "ErrorType",
2017-01-26 07:28:34 +00:00
Type: internal.TypeString,
2017-01-24 15:09:23 +00:00
Enum: errorValues,
EnumValueNameMap: errorValueNameMap,
Description: "Error type.",
2017-01-26 07:28:34 +00:00
Extra: templates.ExtraCDPTypes(),
2017-01-24 15:09:23 +00:00
}
// modifier type
2017-01-26 07:28:34 +00:00
modifierType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "Modifier",
2017-01-26 07:28:34 +00:00
Type: internal.TypeInteger,
2017-01-24 15:09:23 +00:00
EnumBitMask: true,
Description: "Input key modifier type.",
Enum: []string{"None", "Alt", "Ctrl", "Meta", "Shift"},
Extra: "// ModifierCommand is an alias for ModifierMeta.\nconst ModifierCommand Modifier = ModifierMeta",
}
// node type type -- see: https://developer.mozilla.org/en/docs/Web/API/Node/nodeType
2017-01-26 07:28:34 +00:00
nodeTypeType := &internal.Type{
2017-01-24 15:09:23 +00:00
ID: "NodeType",
2017-01-26 07:28:34 +00:00
Type: internal.TypeInteger,
2017-01-24 15:09:23 +00:00
Description: "Node type.",
Enum: []string{
"Element", "Attribute", "Text", "CDATA", "EntityReference",
"Entity", "ProcessingInstruction", "Comment", "Document",
"DocumentType", "DocumentFragment", "Notation",
},
}
// process domains
for _, d := range domains {
switch d.Domain {
2017-01-26 07:28:34 +00:00
case internal.DomainInspector:
2017-01-24 15:09:23 +00:00
// add Inspector types
d.Types = append(d.Types, messageErrorType, messageType, methodType, detachReasonType, errorType)
// find detached event's reason parameter and change type
for _, e := range d.Events {
if e.Name == "detached" {
for _, t := range e.Parameters {
if t.Name == "reason" {
t.Ref = "DetachReason"
2017-01-26 07:28:34 +00:00
t.Type = internal.TypeEnum("")
2017-01-24 15:09:23 +00:00
break
}
}
break
}
}
2017-01-26 07:28:34 +00:00
case internal.DomainCSS:
2017-01-24 15:09:23 +00:00
for _, t := range d.Types {
if t.ID == "CSSComputedStyleProperty" {
t.ID = "ComputedProperty"
}
}
2017-01-26 07:28:34 +00:00
case internal.DomainInput:
2017-01-24 15:09:23 +00:00
// add Input types
d.Types = append(d.Types, modifierType)
for _, t := range d.Types {
if t.ID == "GestureSourceType" {
t.ID = "GestureType"
}
}
2017-01-26 07:28:34 +00:00
case internal.DomainDOM:
2017-01-24 15:09:23 +00:00
// add DOM types
d.Types = append(d.Types, nodeTypeType)
for _, t := range d.Types {
switch t.ID {
case "NodeId", "BackendNodeId":
2017-01-26 07:28:34 +00:00
t.Extra += templates.ExtraFixStringUnmarshaler(internal.ForceCamel(t.ID), "ParseInt", ", 10, 64")
2017-01-24 15:09:23 +00:00
case "Node":
t.Properties = append(t.Properties,
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "Parent",
Ref: domNodeRef,
Description: "Parent node.",
NoResolve: true,
NoExpose: true,
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "Invalidated",
Ref: "chan struct{}",
Description: "Invalidated channel.",
NoResolve: true,
NoExpose: true,
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "State",
Ref: "NodeState",
Description: "Node state.",
NoResolve: true,
NoExpose: true,
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "",
Ref: "sync.RWMutex",
Description: "Read write mutex.",
NoResolve: true,
NoExpose: true,
},
)
t.Extra += templates.ExtraNodeTemplate()
break
}
}
2017-01-26 07:28:34 +00:00
case internal.DomainPage:
2017-01-24 15:09:23 +00:00
for _, t := range d.Types {
switch t.ID {
case "FrameId":
2017-01-26 07:28:34 +00:00
t.Extra += templates.ExtraFixStringUnmarshaler(internal.ForceCamel(t.ID), "", "")
2017-01-24 15:09:23 +00:00
case "Frame":
t.Properties = append(t.Properties,
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "State",
Ref: "FrameState",
Description: "Frame state.",
NoResolve: true,
NoExpose: true,
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "Root",
Ref: domNodeRef,
Description: "Frame document root.",
NoResolve: true,
NoExpose: true,
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "Nodes",
Ref: "map[" + domNodeIDRef + "]" + domNodeRef,
Description: "Frame nodes.",
NoResolve: true,
NoExpose: true,
},
2017-01-26 07:28:34 +00:00
&internal.Type{
2017-01-24 15:09:23 +00:00
Name: "",
Ref: "sync.RWMutex",
Description: "Read write mutex.",
NoResolve: true,
NoExpose: true,
},
)
t.Extra += templates.ExtraFrameTemplate()
// convert Frame.id/parentId to $ref of FrameID
for _, p := range t.Properties {
if p.Name == "id" || p.Name == "parentId" {
p.Ref = "FrameId"
2017-01-26 07:28:34 +00:00
p.Type = internal.TypeEnum("")
2017-01-24 15:09:23 +00:00
}
}
}
}
2017-01-26 07:28:34 +00:00
case internal.DomainNetwork:
2017-01-24 15:09:23 +00:00
for _, t := range d.Types {
// change Timestamp to TypeTimestamp and add extra unmarshaling template
if t.ID == "Timestamp" {
2017-01-26 07:28:34 +00:00
t.Type = internal.TypeTimestamp
2017-01-24 15:09:23 +00:00
t.Extra += templates.ExtraTimestampTemplate(t, d)
}
}
2017-01-26 07:28:34 +00:00
case internal.DomainRuntime:
var types []*internal.Type
2017-01-24 15:09:23 +00:00
for _, t := range d.Types {
switch t.ID {
case "Timestamp":
2017-01-24 15:09:23 +00:00
continue
case "ExceptionDetails":
t.Extra += templates.ExtraExceptionDetailsTemplate()
2017-01-24 15:09:23 +00:00
}
2017-01-24 15:09:23 +00:00
types = append(types, t)
}
d.Types = types
}
for _, t := range d.Types {
// convert object properties
if t.Properties != nil {
t.Properties = convertObjectProperties(t.Properties, d, t.ID)
}
}
// process events and commands
2017-01-26 07:28:34 +00:00
processTypesWithParameters(methodType, d, d.Events, internal.EventMethodPrefix, internal.EventMethodSuffix)
processTypesWithParameters(methodType, d, d.Commands, internal.CommandMethodPrefix, internal.CommandMethodSuffix)
2017-01-24 15:09:23 +00:00
// fix input enums
2017-01-26 07:28:34 +00:00
if d.Domain == internal.DomainInput {
2017-01-24 15:09:23 +00:00
for _, t := range d.Types {
if t.Enum != nil && t.ID != "Modifier" {
t.EnumValueNameMap = make(map[string]string)
for _, v := range t.Enum {
prefix := ""
switch t.ID {
case "GestureType":
prefix = "Gesture"
case "ButtonType":
prefix = "Button"
}
2017-01-26 07:28:34 +00:00
n := prefix + internal.ForceCamel(v)
2017-01-24 15:09:23 +00:00
if t.ID == "KeyType" {
n = "Key" + strings.Replace(n, "Key", "", -1)
}
t.EnumValueNameMap[v] = strings.Replace(n, "Cancell", "Cancel", -1)
}
}
}
}
2017-01-26 07:28:34 +00:00
for _, t := range d.Types {
// fix type stuttering
if !t.NoExpose && !t.NoResolve {
id := strings.TrimPrefix(t.ID, d.Domain.String())
if id == "" {
continue
}
t.ID = id
}
}
2017-01-24 15:09:23 +00:00
}
}
// processTypesWithParameters adds the types to t's enum values, setting the
// enum value map for m. Also, converts the Parameters and Returns properties.
2017-01-26 07:28:34 +00:00
func processTypesWithParameters(m *internal.Type, d *internal.Domain, types []*internal.Type, prefix, suffix string) {
2017-01-24 15:09:23 +00:00
for _, t := range types {
n := t.ProtoName(d)
m.Enum = append(m.Enum, n)
m.EnumValueNameMap[n] = t.TypeName(prefix+d.String(), suffix)
t.Parameters = convertObjectProperties(t.Parameters, d, t.Name)
if t.Returns != nil {
t.Returns = convertObjectProperties(t.Returns, d, t.Name)
}
}
}
// convertObjectProperties converts object properties.
2017-01-26 07:28:34 +00:00
func convertObjectProperties(params []*internal.Type, d *internal.Domain, name string) []*internal.Type {
r := make([]*internal.Type, 0)
2017-01-24 15:09:23 +00:00
for _, p := range params {
switch {
case p.Items != nil:
2017-01-26 07:28:34 +00:00
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
2017-01-26 07:28:34 +00:00
Type: internal.TypeArray,
2017-01-24 15:09:23 +00:00
Description: p.Description,
Optional: p.Optional,
2017-01-26 07:28:34 +00:00
Items: convertObjectProperties([]*internal.Type{p.Items}, d, name+"."+p.Name)[0],
2017-01-24 15:09:23 +00:00
})
case p.Enum != nil:
r = append(r, fixupEnumParameter(name, p, d))
2017-01-26 07:28:34 +00:00
case (p.Name == "timestamp" || p.Ref == "Network.Timestamp" || p.Ref == "Timestamp") && d.Domain != internal.DomainInput:
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
Ref: "Network.Timestamp",
Description: p.Description,
Optional: p.Optional,
})
case p.Name == "modifiers":
2017-01-26 07:28:34 +00:00
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
Ref: "Modifier",
Description: p.Description,
Optional: p.Optional,
})
case p.Name == "nodeType":
2017-01-26 07:28:34 +00:00
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
Ref: "NodeType",
Description: p.Description,
Optional: p.Optional,
})
case p.Ref == "GestureSourceType":
2017-01-26 07:28:34 +00:00
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
Ref: "GestureType",
Description: p.Description,
Optional: p.Optional,
})
case p.Ref == "CSSComputedStyleProperty":
2017-01-26 07:28:34 +00:00
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
Ref: "ComputedProperty",
Description: p.Description,
Optional: p.Optional,
})
2017-01-26 07:28:34 +00:00
case p.Ref != "" && !p.NoExpose && !p.NoResolve:
ref := strings.SplitN(p.Ref, ".", 2)
if len(ref) == 1 {
ref[0] = strings.TrimPrefix(ref[0], d.Domain.String())
} else {
ref[1] = strings.TrimPrefix(ref[1], ref[0])
}
z := strings.Join(ref, ".")
if z == "" {
z = p.Ref
}
r = append(r, &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
2017-01-26 07:28:34 +00:00
Ref: z,
2017-01-24 15:09:23 +00:00
Description: p.Description,
Optional: p.Optional,
})
default:
r = append(r, p)
}
}
return r
}
// addEnumValues adds orig.Enum values to type named n's Enum values in domain.
2017-01-26 07:28:34 +00:00
func addEnumValues(d *internal.Domain, n string, p *internal.Type) {
2017-01-24 15:09:23 +00:00
// find type
2017-01-26 07:28:34 +00:00
var typ *internal.Type
2017-01-24 15:09:23 +00:00
for _, t := range d.Types {
if t.ID == n {
typ = t
break
}
}
if typ == nil {
2017-01-26 07:28:34 +00:00
typ = &internal.Type{
2017-01-24 15:09:23 +00:00
ID: n,
2017-01-26 07:28:34 +00:00
Type: internal.TypeString,
2017-01-24 15:09:23 +00:00
Description: p.Description,
Optional: p.Optional,
}
d.Types = append(d.Types, typ)
}
// combine typ.Enum and vals
v := make(map[string]bool)
all := append(typ.Enum, p.Enum...)
for _, z := range all {
v[z] = false
}
i := 0
typ.Enum = make([]string, len(v))
for _, z := range all {
if !v[z] {
typ.Enum[i] = z
i++
}
v[z] = true
}
}
// enumRefMap is the fully qualified parameter name to ref.
var enumRefMap = map[string]string{
2017-01-26 07:28:34 +00:00
"Animation.Animation.type": "Type",
"CSS.CSSMedia.source": "MediaSource",
2017-01-24 15:09:23 +00:00
"CSS.forcePseudoState.forcedPseudoClasses": "PseudoClass",
"Debugger.setPauseOnExceptions.state": "ExceptionsState",
"Emulation.ScreenOrientation.type": "OrientationType",
"Emulation.setTouchEmulationEnabled.configuration": "EnabledConfiguration",
"Input.dispatchKeyEvent.type": "KeyType",
"Input.dispatchMouseEvent.button": "ButtonType",
"Input.dispatchMouseEvent.type": "MouseType",
"Input.dispatchTouchEvent.type": "TouchType",
"Input.emulateTouchFromMouseEvent.button": "ButtonType",
"Input.emulateTouchFromMouseEvent.type": "MouseType",
"Input.TouchPoint.state": "TouchState",
"Log.LogEntry.level": "Level",
"Log.LogEntry.source": "Source",
"Log.ViolationSetting.name": "Violation",
"Network.Request.mixedContentType": "MixedContentType",
"Network.Request.referrerPolicy": "ReferrerPolicy",
"Page.startScreencast.format": "ScreencastFormat",
"Runtime.consoleAPICalled.type": "APIType",
"Runtime.ObjectPreview.subtype": "Subtype",
"Runtime.ObjectPreview.type": "Type",
"Runtime.PropertyPreview.subtype": "Subtype",
"Runtime.PropertyPreview.type": "Type",
"Runtime.RemoteObject.subtype": "Subtype",
"Runtime.RemoteObject.type": "Type",
"Tracing.start.transferMode": "TransferMode",
"Tracing.TraceConfig.recordMode": "RecordMode",
}
// fixupEnumParameter takes an enum parameter, adds it to the domain and
// returns a type suitable for use in place of the type.
2017-01-26 07:28:34 +00:00
func fixupEnumParameter(typ string, p *internal.Type, d *internal.Domain) *internal.Type {
2017-01-24 15:09:23 +00:00
fqname := strings.TrimSuffix(fmt.Sprintf("%s.%s.%s", d.Domain, typ, p.Name), ".")
2017-01-26 07:28:34 +00:00
ref := internal.ForceCamel(typ + "." + p.Name)
2017-01-24 15:09:23 +00:00
if n, ok := enumRefMap[fqname]; ok {
ref = n
}
// add enum values to type name
addEnumValues(d, ref, p)
2017-01-26 07:28:34 +00:00
return &internal.Type{
2017-01-24 15:09:23 +00:00
Name: p.Name,
Ref: ref,
Description: p.Description,
Optional: p.Optional,
}
}