<commit_before>//
//  OAuthSwiftURLHandlerType.swift
//  OAuthSwift
//
//  Created by phimage on 11/05/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

#if os(iOS) || os(tvOS)
    import UIKit
#elseif os(watchOS)
    import WatchKit
#elseif os(OSX)
    import AppKit
#endif

@objc public protocol OAuthSwiftURLHandlerType {
    func handle(_ url: URL)
}

// MARK: Open externally
open class OAuthSwiftOpenURLExternally: OAuthSwiftURLHandlerType {

    public static var sharedInstance: OAuthSwiftOpenURLExternally = OAuthSwiftOpenURLExternally()

    @objc open func handle(_ url: URL) {
        #if os(iOS) || os(tvOS)
            #if !OAUTH_APP_EXTENSIONS
                UIApplication.shared.openURL(url)
            #endif
        #elseif os(watchOS)
        // WATCHOS: not implemented
        #elseif os(OSX)
            NSWorkspace.shared.open(url)
        #endif
    }
}

// MARK: Open SFSafariViewController
#if os(iOS)
import SafariServices

    @available(iOS 9.0, *)
    open class SafariURLHandler: NSObject, OAuthSwiftURLHandlerType, SFSafariViewControllerDelegate {

        public typealias UITransion = (_ controller: SFSafariViewController, _ handler: SafariURLHandler) -> Void

        weak open var oauthSwift: OAuthSwift?
        open var present: UITransion
        open var dismiss: UITransion
        /// retains observers
        var observers = [String: NSObjectProtocol]()

        open var factory: (_ URL: URL) -> SFSafariViewController = {URL in
            return SFSafariViewController(url: URL)
        }

        /// delegates
        open weak var delegate: SFSafariViewControllerDelegate?

        // configure default presentation and dismissal code

        open var animated: Bool = true
        open var presentCompletion: (() -> Void)?
        open var dismissCompletion: (() -> Void)?
        open var delay: UInt32? = 1

        /// init
        public init(viewController: UIViewController, oauthSwift: OAuthSwift) {<commit_msg>Add Safari views for iOS 11+<commit_after>//
//  OAuthSwiftURLHandlerType.swift
//  OAuthSwift
//
//  Created by phimage on 11/05/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

import Foundation

#if os(iOS) || os(tvOS)
    import UIKit
#elseif os(watchOS)
    import WatchKit
#elseif os(OSX)
    import AppKit
#endif

@objc public protocol OAuthSwiftURLHandlerType {
    func handle(_ url: URL)
}

// MARK: Open externally
open class OAuthSwiftOpenURLExternally: OAuthSwiftURLHandlerType {

    public static var sharedInstance: OAuthSwiftOpenURLExternally = OAuthSwiftOpenURLExternally()

    @objc open func handle(_ url: URL) {
        #if os(iOS) || os(tvOS)
            #if !OAUTH_APP_EXTENSIONS
                UIApplication.shared.openURL(url)
            #endif
        #elseif os(watchOS)
        // WATCHOS: not implemented
        #elseif os(OSX)
            NSWorkspace.shared.open(url)
        #endif
    }
}

// MARK: Open SFSafariViewController
#if os(iOS)
import SafariServices
#if canImport(AuthenticationServices)
import AuthenticationServices
#endif

    @available(iOS 12.0, *)
    open class ASWebAuthenticationURLHandler: OAuthSwiftURLHandlerType {
        var webAuthSession: ASWebAuthenticationSession!
        let callbackUrlScheme: String

        init(callbackUrlScheme: String) {
            self.callbackUrlScheme = callbackUrlScheme
        }

        public func handle(_ url: URL) {
            webAuthSession = ASWebAuthenticationSession(url: url,
                                                        callbackURLScheme: callbackUrlScheme,
                                                        completionHandler: { callback, error in
                                                            guard error == nil, let successURL = callback else {
                                                                let msg = error?.localizedDescription.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
                                                                let urlString = "\(self.callbackUrlScheme)?error=\(msg ?? "UNKNOWN")"
                                                                let url = URL(string: urlString)!
                                                                UIApplication.shared.open(url, options: [:], completionHandler: nil)
                                                                return
                                                            }
                                                            UIApplication.shared.open(successURL, options: [:], completionHandler: nil)
            })

            _ = webAuthSession.start()
        }
    }

    @available(iOS 11.0, *)
    open class SFAuthenticationURLHandler: OAuthSwiftURLHandlerType {
        var webAuthSession: SFAuthenticationSession!
        let callbackUrlScheme: String

        init(callbackUrlScheme: String) {
            self.callbackUrlScheme = callbackUrlScheme
        }

        public func handle(_ url: URL) {
            webAuthSession = SFAuthenticationSession(url: url,
                                                     callbackURLScheme: callbackUrlScheme,
                                                     completionHandler: { callback, error in
                                                        guard error == nil, let successURL = callback else {
                                                            let msg = error?.localizedDescription.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
                                                            let urlString = "\(self.callbackUrlScheme)?error=\(msg ?? "UNKNOWN")"
                                                            let url = URL(string: urlString)!
                                                            UIApplication.shared.open(url, options: [:], completionHandler: nil)
                                                            return
                                                        }
                                                        UIApplication.shared.open(successURL, options: [:], completionHandler: nil)
            })

            _ = webAuthSession.start()
        }
    }

    @available(iOS 9.0, *)
    open class SafariURLHandler: NSObject, OAuthSwiftURLHandlerType, SFSafariViewControllerDelegate {

        public typealias UITransion = (_ controller: SFSafariViewController, _ handler: SafariURLHandler) -> Void

        weak open var oauthSwift: OAuthSwift?
        open var present: UITransion
        open var dismiss: UITransion
        /// retains observers
        var observers = [String: NSObjectProtocol]()

        open var factory: (_ URL: URL) -> SFSafariViewController = {URL in
            return SFSafariViewController(url: URL)
        }

        /// delegates
        open weak var delegate: SFSafariViewControllerDelegate?

        // configure default presentation and dismissal code

        open var animated: Bool = true
        open var presentCompletion: (() -> Void)?
        open var dismissCompletion: (() -> Void)?
        open var delay: UInt32? = 1

        /// init
        public init(viewController: UIViewController, oauthSwift: OAuthSwift) {
{'fraction_non_alphanumeric': 0.07636867636867636, 'fraction_numerical': 0.0052668052668052665, 'mean_word_length': 1.6755654430849092, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3208836', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1666, 'n_words': 454}