Class: BiDiGenerate::Schema Private

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/bidi/support/bidi_generate.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Vendor content (schema vendor.<namespace>) is folded in beside the spec's: a vendor module's commands and events become a domain of their own, and its types resolve like spec types and are emitted in that module (see owned_types).



570
571
572
573
574
575
576
577
578
579
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 570

def initialize(schema)
  @vendor = schema['vendor'] || {}
  @vendor_types = @vendor.values.map { |section| section['types'] || {} }.reduce({}, :merge)
  @types = schema['types'].merge(@vendor_types)
  @commands = schema['commands'] + vendor_messages('commands').select { |c| vendor_domain?(c['domain']) }
  @events = schema['events'] + vendor_events
  @domains = schema['domains'] || {}
  promote_command_params_records!
  @vendor_params = fold_vendor_extensions!
end

Instance Method Details

#build_added_command(cmd, namespace, domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The vendor variant already scopes the command, so its name drops the namespace (moz:listExtensions → list_extensions); a name that would silently override a spec command fails generation instead.



746
747
748
749
750
751
752
753
754
755
756
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 746

def build_added_command(cmd, namespace, domain)
  command = BiDiGenerate.build_command(self, cmd.merge('name' => cmd['name'].delete_prefix("#{namespace}:")))
  spec_names = commands_for(domain).map do |c|
    BiDiGenerate.safe_method_name(BiDiGenerate.camel_to_snake(c['name']))
  end
  if spec_names.include?(command.method_name)
    raise "vendor command #{cmd['method']} would override spec command #{domain}.#{command.method_name}"
  end

  command
end

#build_vendor_override(cmd, type_name, fields, namespace) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The vendor variant's params record and the command that builds it, as [record, command].



759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 759

def build_vendor_override(cmd, type_name, fields, namespace)
  shared = @types[type_name]['fields']
  taken = shared.map { |field| field_name(field, false) }
  type = @types[type_name].merge('fields' => shared + fields.map { |f| vendor_field(f, namespace, taken) })
  record = record_class(type_name, type)
  command = Command.new(wire_name: cmd['method'],
                        method_name: BiDiGenerate.safe_method_name(BiDiGenerate.camel_to_snake(cmd['name'])),
                        params: record_params(type['fields'], false),
                        result_ref: cmd['result'] && structured_ref(cmd['result']['ref']),
                        params_class: record.ruby_name, union_params: false, spec_href: cmd['specHref'])
  [record, command]
end

#command_wrapper_refs(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The domain's command param/result wrapper type names — the classes a command constructs (params) or parses its result into. They are reachable (so tagged outbound/inbound) but are the message wrappers a command method already builds, not data a caller composes, so they are excluded from the type accessors.



701
702
703
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 701

def command_wrapper_refs(domain)
  commands_for(domain).flat_map { |c| [c.dig('params', 'ref'), c.dig('result', 'ref')] }.compact.to_set
end

#commands_for(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



693
694
695
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 693

def commands_for(domain)
  @commands.select { |c| c['domain'] == domain }
end

#domain_href(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The domain's #module-<domain> spec link, or nil when the schema has none.



657
658
659
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 657

def domain_href(domain)
  @domains.dig(domain, 'specHref')
end

#domain_relative_path(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The class path to a type relative to its domain class (an accessor body resolves in the Domain subclass scope): "ExtensionData", or "AccessibilityLocator::Value" for a synthetic nested under its owner.



719
720
721
722
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 719

def domain_relative_path(name)
  prefix = "#{BiDiGenerate.snake_to_class_name(BiDiGenerate.camel_to_snake(name.split('.', 2).first))}::"
  ruby_path(name).sub(/\A#{Regexp.escape(prefix)}/, '')
end

#domains ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Domains that carry a command or event each become one generated module.



689
690
691
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 689

def domains
  (@commands + @events).map { |entry| entry['domain'] }.uniq
end

#enums_for(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Enum types declared under "." become nested constant modules.



806
807
808
809
810
811
812
813
814
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 806

def enums_for(domain)
  owned_types(domain).filter_map do |name, type|
    next unless type['kind'] == 'enum'

    pairs = type['values'].map { |v| [BiDiGenerate.enum_key(v), v] }
    Enum.new(constant_name: BiDiGenerate.screaming_snake(name.split('.', 2).last), pairs: pairs,
             primitive: type['primitive'], spec_href: type['specHref'])
  end
end

#envelope_synthetic?(type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A synthetic record lifted out as an envelope's params (its owner is an envelope).

Returns:

  • (Boolean)


892
893
894
895
896
897
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 892

def envelope_synthetic?(type)
  return false unless type['synthetic']

  owner = @types[type['owner']]
  owner && owner['kind'] == 'record' && message_envelope?(owner)
end

#error_codes ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The protocol-root ErrorCode enum's wire values (e.g. "no such frame"), in schema order. Used to generate the BiDi-specific Error subclasses. [] when the schema has no ErrorCode.



858
859
860
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 858

def error_codes
  @types.dig('ErrorCode', 'values') || []
end

#events_for(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



785
786
787
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 785

def events_for(domain)
  @events.select { |e| e['domain'] == domain }
end

#fold_vendor_extensions! ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A vendor's fields on a spec type are declared fields (ADR 17786), so they are typed and never ride the extensions map. Fields on a spec command's params go to a record of the vendor variant's own (see vendor_modules_for), returned per namespace; fields on any other type join it as optional fields.



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 587

def fold_vendor_extensions!
  @vendor_field_refs = []
  @vendor.to_h do |namespace, section|
    params = {}
    (section['extends'] || {}).each do |type_name, entry|
      fields = vendor_extension_fields(namespace, type_name, entry)
      if params_command(type_name)
        params[type_name] = fields
      else
        optional = fields.map { |field| field.merge('required' => false) }
        @types[type_name] = @types[type_name].merge('fields' => @types[type_name]['fields'] + optional)
      end
    end
    [namespace, params]
  end
end

#hoist_vendor_enum(type_name, field) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The projector leaves a vendor field's enum inline (moz:scope), so name it after the field in the extended type's domain (browsingContext.MozScope) to emit and validate it like a spec enum.



616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 616

def hoist_vendor_enum(type_name, field)
  values = field['type']['enum']
  return field unless values

  domain = type_name.split('.', 2).first
  name = "#{domain}.#{BiDiGenerate.snake_to_class_name(BiDiGenerate.camel_to_snake(field['name']))}"
  enum = {'kind' => 'enum', 'values' => values, 'primitive' => field['type']['primitive']}
  raise "vendor enum #{name} conflicts with an existing type" if @types.key?(name) && @types[name] != enum

  @types[name] = enum
  field.merge('type' => field['type'].slice('nullable').merge('ref' => name))
end

#message_envelope?(type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A protocol message envelope is a record with a baked method discriminator ({method: <const>, params: …}) — the wire shape of a command/event message. No value type carries a const method field, so this is unambiguous.

Returns:

  • (Boolean)


887
888
889
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 887

def message_envelope?(type)
  type['fields'].any? { |f| f['wire'] == 'method' && f['type'].key?('const') }
end

#owned_types(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The types a domain's module declares. A spec domain's are the "." types the spec defines; a vendor's helper types under that prefix are not emitted until a generated command needs them. A vendor module (moz:debugging) owns the vendor types its commands and events name (Mozilla prefixes them mozDebugging.), read off their params and result refs.



820
821
822
823
824
825
826
827
828
829
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 820

def owned_types(domain)
  vendor = vendor_domain?(domain)
  prefixes = type_prefixes(domain).map { |prefix| "#{prefix}." }
  added = vendor ? Set.new : vendor_types_reached(domain)
  @types.select do |name, _|
    next false unless prefixes.any? { |p| name.start_with?(p) }

    @vendor_types.key?(name) ? vendor || added.include?(name) : !vendor
  end
end

#params_command(type_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The spec command that sends a type as its params, or nil.



630
631
632
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 630

def params_command(type_name)
  @commands.find { |c| c.dig('params', 'ref') == type_name && !vendor_domain?(c['domain']) }
end

#params_for(params_ref) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Flat params for a command: the record's fields, or — for a union of records — the merged superset of variant fields. Returns [] for commands with no params, or nil when params can't be flattened (alias, or a union whose variants aren't all records) so the caller forwards verbatim.



793
794
795
796
797
798
799
800
801
802
803
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 793

def params_for(params_ref)
  return [] unless params_ref

  type = @types[params_ref['ref']]
  return nil unless type

  case type['kind']
  when 'record' then record_params(type['fields'], extensible_record?(params_ref['ref']))
  when 'union' then union_params(type, params_ref['ref'])
  end
end

#plainly_reached_types ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Type names reached by at least one non-union-arm reference: used as a record field, list element, map value, or alias target somewhere — not solely as a named union's variant. A type reached only as a union arm is built through its union (a variant factory or the command's flattened dispatch), so a nested one needs no accessor; one reached as a plain field ref (browsingContext.AccessibilityLocator's value) does.



710
711
712
713
714
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 710

def plainly_reached_types
  @plainly_reached_types ||= @types.each_value.with_object(Set.new) do |node, reached|
    plain_refs(node).each { |ref| reached << ref }
  end
end

#promote_command_params_records! ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A command written in CDDL map form carries its params as an inline object (rather than the usual group form referencing a named params type). The projector links the command to those params, but hoists them into a synthetic record owned by the command's message envelope. That envelope is suppressed (Transport forms it), so the synthetic params record would never be emitted even though the command's params ref points straight at it. Promote it to a top-level domain record so the generator emits and references it like any other params type. Today this is exactly userAgentClientHints.setClientHintsOverride.



669
670
671
672
673
674
675
676
677
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 669

def promote_command_params_records!
  @commands.each do |cmd|
    ref = cmd.dig('params', 'ref')
    next unless ref

    type = @types[ref]
    promote_to_domain_type!(ref) if type && envelope_synthetic?(type)
  end
end

#promote_to_domain_type!(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Strip the synthetic/owner/label tags so a lifted-out type emits as a top-level domain record instead of nesting under its (suppressed) envelope.



681
682
683
684
685
686
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 681

def promote_to_domain_type!(name)
  type = @types[name]
  type&.delete('synthetic')
  type&.delete('owner')
  type&.delete('label')
end

#ruby_path_for(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Public ruby-path resolver (Owner::Label for a synthetic), matching how a variant's ref is emitted — so a caller can map a variant ref back to its emitted record.



908
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 908

def ruby_path_for(name) = ruby_path(name)

#structured_ref(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The Protocol-relative class path a command result parses into, or nil when it is non-structured (or a bare list, returned raw).



901
902
903
904
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 901

def structured_ref(name)
  resolved = resolve_named(name)
  resolved[:list] ? nil : resolved[:ref]
end

#suppressed_record?(type) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Records the generator deliberately does not emit: a message envelope, or a synthetic params record lifted out of one. Both are reachable only through the envelope, which Transport replaces — so nothing else references them.

Returns:

  • (Boolean)


880
881
882
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 880

def suppressed_record?(type)
  message_envelope?(type) || envelope_synthetic?(type)
end

#type_kind(ref) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



781
782
783
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 781

def type_kind(ref)
  @types[ref]&.fetch('kind', nil)
end

#type_prefixes(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



848
849
850
851
852
853
854
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 848

def type_prefixes(domain)
  return [domain] unless vendor_domain?(domain)

  messages = commands_for(domain) + events_for(domain)
  refs = messages.flat_map { |m| [m.dig('params', 'ref'), m.dig('result', 'ref')] }
  refs.compact.select { |ref| ref.include?('.') }.map { |ref| ref.split('.', 2).first }.uniq
end

#types_for(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Structured value classes (records + discriminated unions) declared under "." Empty records are projector artifacts with nothing to carry, so they stay opaque hashes; only non-empty records and unions become classes. Command/event message envelopes (the {method, params} wire wrapper) are skipped — Transport forms that envelope, so nothing references them.



867
868
869
870
871
872
873
874
875
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 867

def types_for(domain)
  owned_types(domain).filter_map do |name, type|
    case type['kind']
    when 'record' then record_class(name, type) unless type['fields'].empty? || suppressed_record?(type)
    when 'union' then union_class(name)
    when 'alias' then union_class(name) if type['type'].key?('union')
    end
  end
end

#vendor_added_commands(domain, namespace) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The commands a vendor defines in a spec domain (webExtension.moz:listExtensions). They are emitted on that domain's vendor variant, never on the shared class (see vendor_modules_for).



650
651
652
653
654
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 650

def vendor_added_commands(domain, namespace)
  return [] if vendor_domain?(domain)

  (@vendor.dig(namespace, 'commands') || []).select { |c| c['domain'] == domain }
end

#vendor_domain?(domain) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A vendor module's domain keeps its wire prefix (moz:debugging); a spec domain has none.

Returns:

  • (Boolean)


582
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 582

def vendor_domain?(domain) = domain.include?(':')

#vendor_events ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A vendor event in a spec domain would need the same vendor-variant routing as a command; none exists yet, so fail generation rather than leak it into the shared class.



640
641
642
643
644
645
646
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 640

def vendor_events
  events = vendor_messages('events')
  stray = events.reject { |e| vendor_domain?(e['domain']) }
  raise "vendor event #{stray.first['method']} in spec domain #{stray.first['domain']} is unsupported" if stray.any?

  events
end

#vendor_extension_fields(namespace, type_name, entry) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Also records the vendor types the fields reference, so vendor_types_reached emits them.



605
606
607
608
609
610
611
612
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 605

def vendor_extension_fields(namespace, type_name, entry)
  type = @types[type_name] || raise("vendor #{namespace} extends unknown type #{type_name}")
  raise "vendor #{namespace} extends #{type_name}, which is not a record" unless type['kind'] == 'record'

  fields = entry['fields'].map { |field| hoist_vendor_enum(type_name, field) }
  @vendor_field_refs.concat(fields.flat_map { |field| refs_in_type(field['type']) })
  fields
end

#vendor_field(field, namespace, taken) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A vendor field's ruby name drops its namespace prefix (moz:permanent → permanent): the module already scopes it, so re-encoding the namespace in every identifier is redundant. The wire key is untouched. Falls back to the prefixed name only if stripping would collide with a shared field on the same record.



776
777
778
779
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 776

def vendor_field(field, namespace, taken)
  stripped = field.merge('name' => field['name'].delete_prefix("#{namespace}:"))
  taken.include?(field_name(stripped, false)) ? field : stripped
end

#vendor_messages(kind) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



634
635
636
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 634

def vendor_messages(kind)
  @vendor.values.flat_map { |section| section[kind] || [] }
end

#vendor_modules_for(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The vendor modules a domain carries, one per namespace (moz → module Moz). Each command whose params a vendor extends is overridden there with a closed params record of the variant's own (the shared fields plus the vendor's), so the vendor method mirrors the base command's wire method and result while adding the typed vendor fields. Empty for any domain (or schema) with no vendor extensions, so non-vendor output is unaffected.



729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 729

def vendor_modules_for(domain)
  parent = BiDiGenerate.snake_to_class_name(BiDiGenerate.camel_to_snake(domain))
  @vendor_params.filter_map do |namespace, extended|
    overrides = extended.filter_map do |type_name, fields|
      cmd = params_command(type_name)
      build_vendor_override(cmd, type_name, fields, namespace) if cmd['domain'] == domain
    end
    added = vendor_added_commands(domain, namespace).map { |cmd| build_added_command(cmd, namespace, domain) }
    next if overrides.empty? && added.empty?

    VendorModule.new(name: BiDiGenerate.snake_to_class_name(namespace), namespace: namespace, parent: parent,
                     types: overrides.map(&:first), commands: overrides.map(&:last) + added)
  end
end

#vendor_types_reached(domain) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The vendor types a vendor-added command in this spec domain reaches through its params and result, or a vendor field folded onto a spec type references, so they are emitted in the domain's module beside what uses them.



834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/selenium/webdriver/bidi/support/bidi_generate.rb', line 834

def vendor_types_reached(domain)
  commands = @vendor.keys.flat_map { |namespace| vendor_added_commands(domain, namespace) }
  pending = commands.flat_map { |c| [c.dig('params', 'ref'), c.dig('result', 'ref')] }.compact + @vendor_field_refs
  reached = Set.new
  until pending.empty?
    name = pending.pop
    next if reached.include?(name) || !@vendor_types.key?(name)

    reached << name
    pending.concat(plain_refs(@types[name]) + (@types[name]['variants'] || []))
  end
  reached
end