// 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.safari;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.FileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.service.DriverCommandExecutor;

import java.io.IOException;

/**
 * A WebDriver implementation that controls Safari using a browser extension
 * (consequently, only Safari 5.1+ is supported).
 *
 * This driver can be configured using the {@link SafariOptions} class.
 */
public class SafariDriver extends RemoteWebDriver {

  /**
   * Capability to force usage of the deprecated SafariDriver extension while running
   * on macOS Sierra.
   *
   * <pre>
   *   DesiredCapabilities safariCap = DesiredCapabilities.Safari();
   *   safariCap.setCapability(SafariDriver.USE_LEGACY_DRIVER_CAPABILITY, true);
   *   WebDriver driver = new SafariDriver(safariCap);
   * </pre>
   */
  public static final String USE_LEGACY_DRIVER_CAPABILITY = "useLegacyDriver";

  private SafariDriverService service;

  /**
   * Initializes a new SafariDriver} class with default {@link SafariOptions}.
   */
  public SafariDriver() {
    this(new SafariOptions());
  }

  /**
   * Converts the specified {@link DesiredCapabilities} to a {@link SafariOptions}
   * instance and initializes a new SafariDriver using these options.
   * @see SafariOptions#fromCapabilities(org.openqa.selenium.Capabilities)
   *
   * @param desiredCapabilities capabilities requested of the driver
   */
  public SafariDriver(Capabilities desiredCapabilities) {
    this(SafariOptions.fromCapabilities(desiredCapabilities));
  }

  /**
   * Initializes a new SafariDriver using the specified {@link SafariOptions}.
   *
   * @param safariOptions safari specific options / capabilities for the driver
   */
  public SafariDriver(SafariOptions safariOptions) {
    super(getExecutor(safariOptions), safariOptions.toCapabilities());
  }

  private static CommandExecutor getExecutor(SafariOptions options) {
    Object useLegacy = options.toCapabilities().getCapability(USE_LEGACY_DRIVER_CAPABILITY);
    SafariDriverService service = SafariDriverService.createDefaultService(options);
    if ((useLegacy == null || !(Boolean)useLegacy) && service != null) {
      return new DriverCommandExecutor(service);
    }
    return new SafariDriverCommandExecutor(options);
  }

  @Override
  public void setFileDetector(FileDetector detector) {
    throw new WebDriverException(
        "Setting the file detector only works on remote webdriver instances obtained " +
        "via RemoteWebDriver");
  }

  @Override
  protected void startClient() {
    SafariDriverCommandExecutor executor = (SafariDriverCommandExecutor) this.getCommandExecutor();
    try {
      executor.start();
    } catch (IOException e) {
      throw new WebDriverException(e);
    }
  }

  @Override
  protected void stopClient() {
    SafariDriverCommandExecutor executor = (SafariDriverCommandExecutor) this.getCommandExecutor();
    executor.stop();
  }

  @Override
  public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
    // Get the screenshot as base64.
    String base64 = (String) execute(DriverCommand.SCREENSHOT).getValue();
    // ... and convert it.
    return target.convertFromBase64Png(base64);
  }
}

@@ -3650,36 +3650,24 @@
 ent() %7B%0A    
-SafariDriver
 CommandExecu
@@ -3662,33 +3662,40 @@
 CommandExecutor 
-e
+commandE
 xecutor = (Safar
@@ -3684,37 +3684,32 @@
 dExecutor = 
-(SafariDriver
+this.get
 CommandExecu
@@ -3707,34 +3707,75 @@
 mandExecutor
-) this.get
+();%0A    if (commandExecutor instanceof SafariDriver
 CommandExecu
@@ -3777,24 +3777,26 @@
 Executor
-();%0A
+) %7B%0A 
     
+ 
 try %7B%0A  
@@ -3799,24 +3799,64 @@
 %7B%0A      
-executor
+  ((SafariDriverCommandExecutor)commandExecutor)
 .start()
@@ -3853,24 +3853,26 @@
 r).start();%0A
+  
     %7D catch 
@@ -3891,24 +3891,26 @@
  e) %7B%0A      
+  
 throw new We
@@ -3926,24 +3926,72 @@
 ception(e);%0A
+      %7D%0A    %7D else %7B%0A      super.startClient();%0A
     %7D%0A  %7D%0A%0A 
@@ -4041,86 +4041,172 @@
     
-SafariDriverCommandExecutor executor = (SafariDriverCommandExecutor) this.getC
+CommandExecutor commandExecutor = this.getCommandExecutor();%0A    if (commandExecutor instanceof SafariDriverCommandExecutor) %7B%0A      ((SafariDriverCommandExecutor)c
 omma
@@ -4215,24 +4215,30 @@
 Executor
+).stop
 ();%0A    
 executor
@@ -4233,24 +4233,48 @@
     
-executor.stop();
+%7D else %7B%0A      super.stopClient();%0A    %7D
 %0A  %7D

fix cast exception for safaridriver on startup