// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote.session;

import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;

public class FirefoxFilter implements CapabilitiesFilter {
  // Note: we don't take a dependency on the FirefoxDriver jar as it might not be on the classpath

  @Override
  public Map<String, Object> apply(Map<String, Object> unmodifiedCaps) {
    Map<String, Object> caps = unmodifiedCaps.entrySet().parallelStream()
        .filter(entry ->
                    ("browserName".equals(entry.getKey()) && "firefox".equals(entry.getValue())) ||
                    entry.getKey().startsWith("firefox_") ||
                    entry.getKey().startsWith("moz:"))
        .filter(entry -> Objects.nonNull(entry.getValue()))
        .collect(Collectors.toMap(
            Map.Entry::getKey,
            Map.Entry::getValue,
            (l, r) -> l,
            TreeMap::new));

    // If we only have marionette in the caps, the user is asking for firefox. Make sure we inject
    // the browser name to be sure.
    if (unmodifiedCaps.containsKey("marionette") && !caps.containsKey("browserName")) {
      caps.put("browserName", "firefox");
    }

    return caps.isEmpty() ? null : caps;
  }
}

@@ -1997,16 +1997,844 @@
 %0A    %7D%0A%0A
+    // People might have just put the binary and profile in the OSS payload, and not in firefox%0A    // options.%0A    @SuppressWarnings(%22unchecked%22)%0A    Map%3CString, Object%3E options = (Map%3CString, Object%3E) unmodifiedCaps.getOrDefault(%0A        %22moz:firefoxOptions%22,%0A        new TreeMap%3C%3E());%0A    if (unmodifiedCaps.containsKey(%22firefox_binary%22) && !options.containsKey(%22binary%22)) %7B%0A      // Here's hoping that the binary is just a string. It should be as FirefoxBinary.toJson just%0A      // encodes the path.%0A      options.put(%22binary%22, unmodifiedCaps.get(%22firefox_binary%22));%0A    %7D%0A    if (unmodifiedCaps.containsKey(%22firefox_profile%22) && !options.containsKey(%22profile%22)) %7B%0A      options.put(%22profile%22, unmodifiedCaps.get(%22firefox_profile%22));%0A    %7D%0A    if (!options.isEmpty()) %7B%0A      caps.put(%22moz:firefoxOptions%22, options);%0A    %7D%0A%0A
     retu

Copy firefox_profile and firefox_binary into w3c payload