// third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom.MojoProfile.swift is auto generated by mojom_bindings_generator.py, do not edit

// Copyright 2026 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file is autogenerated by:
//     mojo/public/tools/bindings/generators/mojom_fuzzilli_generator.py
// For:
//     third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom
//

private let mojoPrefix = "blink.mojom."

///
/// Interfaces
///
extension ILType {
    // SpeculationHost
    fileprivate static let jsBlinkMojomSpeculationHostRemote: ILType = .object(
        ofGroup: "blink.mojom.SpeculationHostRemote",
        withProperties: ["$"],
        withMethods: [
            "updateSpeculationCandidates",
            "onLCPPredicted",
            "enactCandidate",
        ])
    fileprivate static let jsBlinkMojomSpeculationHostRemoteWrapper: ILType = .object(
        ofGroup: "blink.mojom.SpeculationHostRemoteWrapper",
        withMethods: ["close", "isBound", "associateAndPassReceiver"])
    fileprivate static let jsBlinkMojomSpeculationHostPendingReceiver: ILType = .object(
        ofGroup: "blink.mojom.SpeculationHostPendingReceiver")
}

extension ObjectGroup {
    // SpeculationHost
    fileprivate static let blinkMojomSpeculationHostRemote = ObjectGroup(
        name: "blink.mojom.SpeculationHostRemote",
        instanceType: .jsBlinkMojomSpeculationHostRemote,
        properties: ["$": .jsBlinkMojomSpeculationHostRemoteWrapper],
        methods: [
            "updateSpeculationCandidates": [
                .plain(.jsBlinkMojomSpeculationCandidateArray),
                .plain(.boolean),
            ] => .undefined,
            "onLCPPredicted": [
            ] => .undefined,
            "enactCandidate": [
                .plain(.jsBlinkMojomSpeculationCandidate),
                .plain(.jsBlinkMojomSpeculationHeuristic),
            ] => .undefined,
        ]
    )

    fileprivate static let blinkMojomSpeculationHostRemoteWrapper = ObjectGroup(
        name: "blink.mojom.SpeculationHostRemoteWrapper",
        instanceType: .jsBlinkMojomSpeculationHostRemoteWrapper,
        properties: [:],
        methods: [
            "close": [] => .undefined,
            "isBound": [] => .boolean,
            "associateAndPassReceiver": [] => .jsBlinkMojomSpeculationHostPendingReceiver,
        ]
    )

    fileprivate static let blinkMojomSpeculationHostPendingReceiver = ObjectGroup(
        name: "blink.mojom.SpeculationHostPendingReceiver",
        instanceType: .jsBlinkMojomSpeculationHostPendingReceiver,
        properties: [:],
        methods: [:]
    )
}

///
/// Structs
///
extension ILType {
    fileprivate static let jsBlinkMojomSpeculationCandidate: ILType = .object(
        ofGroup: "blink.mojom.SpeculationCandidate",
        withProperties: [
            "url",
            "action",
            "referrer",
            "requiresAnonymousClientIpWhenCrossOrigin",
            "targetBrowsingContextNameHint",
            "eagerness",
            "noVarySearchHint",
            "injectionType",
            "tags",
            "formSubmission",
        ])
    fileprivate static let jsBlinkMojomSpeculationCandidateConstructor: ILType = .constructor([
        .plain(.jsUrlMojomUrl),
        .plain(.jsBlinkMojomSpeculationAction),
        .plain(.jsBlinkMojomReferrer),
        .plain(.boolean),
        .plain(.jsBlinkMojomSpeculationTargetHint),
        .plain(.jsBlinkMojomSpeculationEagerness),
        .either(.jsNetworkMojomNoVarySearch, .undefined),
        .plain(.jsBlinkMojomSpeculationInjectionType),
        .plain(.jsStringArray),
        .plain(.boolean),
    ] => .jsBlinkMojomSpeculationCandidate)
    fileprivate static let jsBlinkMojomReferrer: ILType = .object(
        ofGroup: "blink.mojom.Referrer",
        withProperties: [
            "url",
            "policy",
        ])
    fileprivate static let jsBlinkMojomReferrerConstructor: ILType = .constructor([
        .plain(.jsUrlMojomUrl),
        .plain(.jsNetworkMojomReferrerPolicy),
    ] => .jsBlinkMojomReferrer)
    fileprivate static let jsNetworkMojomNoVarySearch: ILType = .object(
        ofGroup: "network.mojom.NoVarySearch",
        withProperties: [
            "searchVariance",
            "varyOnKeyOrder",
        ])
    fileprivate static let jsNetworkMojomNoVarySearchConstructor: ILType = .constructor([
        .plain(.jsNetworkMojomSearchParamsVariance),
        .plain(.boolean),
    ] => .jsNetworkMojomNoVarySearch)
}

extension ObjectGroup {
    fileprivate static let blinkMojomSpeculationCandidate = ObjectGroup(
        name: "blink.mojom.SpeculationCandidate",
        instanceType: .jsBlinkMojomSpeculationCandidate,
        properties: [
            "url": .jsUrlMojomUrl,
            "action": .jsBlinkMojomSpeculationAction,
            "referrer": .jsBlinkMojomReferrer,
            "requiresAnonymousClientIpWhenCrossOrigin": .boolean,
            "targetBrowsingContextNameHint": .jsBlinkMojomSpeculationTargetHint,
            "eagerness": .jsBlinkMojomSpeculationEagerness,
            "noVarySearchHint": .jsNetworkMojomNoVarySearch,
            "injectionType": .jsBlinkMojomSpeculationInjectionType,
            "tags": .jsStringArray,
            "formSubmission": .boolean,
        ],
        methods: [:]
    )
    fileprivate static let blinkMojomReferrer = ObjectGroup(
        name: "blink.mojom.Referrer",
        instanceType: .jsBlinkMojomReferrer,
        properties: [
            "url": .jsUrlMojomUrl,
            "policy": .jsNetworkMojomReferrerPolicy,
        ],
        methods: [:]
    )
    fileprivate static let networkMojomNoVarySearch = ObjectGroup(
        name: "network.mojom.NoVarySearch",
        instanceType: .jsNetworkMojomNoVarySearch,
        properties: [
            "searchVariance": .jsNetworkMojomSearchParamsVariance,
            "varyOnKeyOrder": .boolean,
        ],
        methods: [:]
    )
}

///
/// Arrays
///
extension ILType {
    fileprivate static let jsBlinkMojomSpeculationCandidateArray: ILType =
        .createJsArrayType(ofElementType: .jsBlinkMojomSpeculationCandidate)
    fileprivate static let jsStringArray: ILType =
        .createJsArrayType(ofElementType: .jsStringElement)
}

/// As Fuzzilli runs, the OperationMutator and VariadicInputReducer will modify
/// the `CreateArray` operation to increase or decrease the element count. This
/// allows arrays to be arbitrarily long.
///
/// `findOrGenerateType` is preferred over specifying `inputs` because
/// Fuzzilli only uses CodeGenerators to produce types in `inputs`, while
/// `findOrGenerateType` uses a variety of sources (e.g. `builtins`, other
/// method properties and methods). An input is specified (i.e. `.one`)
/// to prevent the CodeGenerator from being scheduled first when there are
/// no visible variables, as `findOrGenerateType` causes a crash if there
/// are no visibile variables.
private let MojoBlinkMojomSpeculationCandidateArrayGenerator = CodeGenerator(
    "MojoBlinkMojomSpeculationCandidateArrayGenerator",
    inputs: .one,
    produces: [.jsBlinkMojomSpeculationCandidateArray]
) { b, _ in
    let elem = b.findOrGenerateType(.jsBlinkMojomSpeculationCandidate)
    var elements: [Variable] = [elem]
    b.createArray(
        with: elements,
        elementGroupName:
            ObjectGroup.blinkMojomSpeculationCandidate.name)
}
private let MojoStringArrayGenerator = CodeGenerator(
    "MojoStringArrayGenerator",
    inputs: .one,
    produces: [.jsStringArray]
) { b, _ in
    let elem = b.findOrGenerateType(.string)
    var elements: [Variable] = [elem]
    b.createArray(
        with: elements,
        elementGroupName:
            ObjectGroup.stringElement.name)
}

///
/// Enumerations
///
extension ILType {
    fileprivate static let jsBlinkMojomSpeculationAction: ILType = .intEnumeration(
        ofName: "blink.mojom.SpeculationAction",
        withValues: [0, 1, 2])
    fileprivate static let jsNetworkMojomReferrerPolicy: ILType = .intEnumeration(
        ofName: "network.mojom.ReferrerPolicy",
        withValues: [0, 1, 2, 3, 4, 5, 6, 7, 8])
    fileprivate static let jsBlinkMojomSpeculationTargetHint: ILType = .intEnumeration(
        ofName: "blink.mojom.SpeculationTargetHint",
        withValues: [0, 1, 2])
    fileprivate static let jsBlinkMojomSpeculationEagerness: ILType = .intEnumeration(
        ofName: "blink.mojom.SpeculationEagerness",
        withValues: [0, 1, 2, 3])
    fileprivate static let jsBlinkMojomSpeculationInjectionType: ILType = .intEnumeration(
        ofName: "blink.mojom.SpeculationInjectionType",
        withValues: [0, 1, 2, 3])
    fileprivate static let jsBlinkMojomSpeculationHeuristic: ILType = .intEnumeration(
        ofName: "blink.mojom.SpeculationHeuristic",
        withValues: [0, 1, 2, 3])
}

///
/// Unions
///
extension ILType {
    fileprivate static let jsNetworkMojomSearchParamsVariance: ILType =
        OptionsBag.networkMojomSearchParamsVariance.group.instanceType
}

extension OptionsBag {
    fileprivate static let networkMojomSearchParamsVariance = OptionsBag(
        name: "network.mojom.SearchParamsVariance",
        properties: [
            "noVaryParams": .jsStringArray,
            "varyParams": .jsStringArray,
        ],
        selectionMode: .exactlyOne,
    )
}

///
/// Generators
///
// TODO(crbug.com/500386713) Consider using mojoBuiltins or another list instead of regex
private func isTargetObject(type: ILType) -> Bool {
    guard type.Is(.object()), let group = type.group else { return false }
    return group.starts(with: mojoPrefix)
}

/// Mojo variant of the builtin `MethodCallGenerator` that operates only on
/// "Mojo" objects instead of all the objects in the JavaScript environment,
/// most of which are unrelated to the interface. This generator emits random
/// method calls, generating any arguments that are not available.
private let MojoMethodCallGenerator = CodeGenerator("MojoMethodCallGenerator") {
    b in
    // Pick a variable among all Mojo variables with methods
    var targetVar = b.findVariable {
        let type = b.type(of: $0)
        return isTargetObject(type: type) && !type.methods.isEmpty
    }

    // If we can't find a Mojo object in scope, create the SpeculationHost remote
    guard let obj = targetVar else {
        let getRemoteFunc = b.createNamedVariable(forBuiltin: "getOrCreatePrimaryRemote")
        b.callFunction(getRemoteFunc, withArgs: [])
        return
    }

    let methodName = b.type(of: obj).randomMethod()!
    let signatures = b.methodSignatures(of: methodName, on: obj)
    let signature = chooseUniform(from: signatures)
    let arguments = b.findOrGenerateArguments(forSignature: signature)
    b.callMethod(methodName, on: obj, withArgs: arguments, guard: false)
}

/// Mojo variant of the builtin `PropertyRetrievalGenerator` that operates only
/// on "Mojo" objects instead of all the objects in the JavaScript environment,
/// most of which are unrelated to the interface. This generator emits random
/// property retrievals.
private let MojoPropertyRetrievalGenerator = CodeGenerator(
    "MojoPropertyRetrievalGenerator"
) { b in
    let targetVar = b.findVariable { isTargetObject(type: b.type(of: $0)) }
    guard let obj = targetVar else { return }

    let propertyName =
        b.type(of: obj).randomProperty() ?? b.randomCustomPropertyName()
    b.getProperty(propertyName, of: obj)
}

private let mojoBuiltins: [String: ILType] = [
    /// Profiles declare this method, a wrapper method around `getRemote` that follows
    /// the Singleton pattern, in `codePrefix`. This method ensures: (1) `getRemote`
    /// is only called once, and (2) the remote is saved to a global variable so it
    /// can be closed at the end of the program.
    "getOrCreatePrimaryRemote": .function([] => .jsBlinkMojomSpeculationHostRemote),
    "blink.mojom.SpeculationCandidate": .jsBlinkMojomSpeculationCandidateConstructor,
    "blink.mojom.Referrer": .jsBlinkMojomReferrerConstructor,
    "network.mojom.NoVarySearch": .jsNetworkMojomNoVarySearchConstructor,
]

private let keepGenerators = [
    "IntegerGenerator", "StringGenerator", "TernaryOperationGenerator",
    "PlainFunctionGenerator", "AsyncFunctionGenerator", "AwaitGenerator",
    "SubroutineReturnGenerator", "TryCatchGenerator",
]

private let mojoDisabledGenerators: [String] = CodeGenerators.map { $0.name }
    .filter {
        !keepGenerators.contains($0)
    }

let mojoSpeculationHostProfile = Profile(
    processArgs: { _ in return [] },
    processArgsReference: nil,
    processEnv: [
        "ASAN_OPTIONS": "detect_odr_violation=0:abort_on_error=1", "DISPLAY": ":20",
    ],
    maxExecsBeforeRespawn: 1000,
    timeout: Timeout.interval(11000, 11000),
    codePrefix: """
        let globalRemote = null;
        function getOrCreatePrimaryRemote() {
            if (!globalRemote) {
                globalRemote = blink.mojom.SpeculationHost.getRemote();
            }
            return globalRemote;
        }
        """,
    codeSuffix: "if (globalRemote) {globalRemote.$.close()}",
    ecmaVersion: v8Profile.ecmaVersion,
    startupTests: [
        ("fuzzilli('FUZZILLI_PRINT', 'test')", .shouldSucceed),
        (
            """
            if (typeof blink == 'undefined' \
            || blink?.mojom?.SpeculationHost === undefined) \
            throw 'SpeculationHost not found'
            """,
            .shouldSucceed
        ),
    ] + v8Profile.startupTests,
    additionalCodeGenerators: [
        (MojoMethodCallGenerator, 10000),
        (MojoPropertyRetrievalGenerator, 10000),
        (MojoBlinkMojomSpeculationCandidateArrayGenerator, 1),
        (MojoStringArrayGenerator, 1),
    ] + commonMojoCodeGenerators,
    additionalProgramTemplates: WeightedList([]),
    disabledCodeGenerators: mojoDisabledGenerators,
    disabledMutators: [
        "ExplorationMutator", "ProbingMutator", "PropertyAccessorMutator"
    ],
    additionalBuiltins: mojoBuiltins.merging(commonMojoBuiltins) { (existing, _) in existing },
    additionalObjectGroups: [
        .blinkMojomSpeculationHostRemote,
        .blinkMojomSpeculationHostRemoteWrapper,
        .blinkMojomSpeculationHostPendingReceiver,
        .blinkMojomSpeculationCandidate,
        .blinkMojomReferrer,
        .networkMojomNoVarySearch,
    ] + commonMojoObjectGroups,
    additionalEnumerations: [
        .jsBlinkMojomSpeculationAction,
        .jsNetworkMojomReferrerPolicy,
        .jsBlinkMojomSpeculationTargetHint,
        .jsBlinkMojomSpeculationEagerness,
        .jsBlinkMojomSpeculationInjectionType,
        .jsBlinkMojomSpeculationHeuristic,
    ] + commonMojoEnumerations,
    additionalOptionsBags: [
        .networkMojomSearchParamsVariance,
    ] + commonMojoOptionsBags,
    optionalPostProcessor: nil
)
