<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation
import WebKit
import Storage
import Shared

protocol BrowserHelper {
    static func name() -> String
    func scriptMessageHandlerName() -> String?
    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
}

@objc
protocol BrowserDelegate {
    func browser(browser: Browser, didAddSnackbar bar: SnackBar)
    func browser(browser: Browser, didRemoveSnackbar bar: SnackBar)
    optional func browser(browser: Browser, didCreateWebView webView: WKWebView)
    optional func browser(browser: Browser, willDeleteWebView webView: WKWebView)
}

class Browser: NSObject {
    var webView: WKWebView? = nil

    var browserDelegate: BrowserDelegate? = nil
    var bars = [SnackBar]()
    var favicons = [Favicon]()

    var screenshot: UIImage?
    private var helperManager: HelperManager? = nil
    var lastRequest: NSURLRequest? = nil
    private var configuration: WKWebViewConfiguration? = nil

    init(configuration: WKWebViewConfiguration) {
        self.configuration = configuration
    }

    class func toTab(browser: Browser) -> RemoteTab? {
        if let displayURL = browser.displayURL {
            return RemoteTab(clientGUID: nil,
                URL: displayURL,
                title: browser.displayTitle,
                history: browser.historyList,
                lastUsed: Timestamp(),
                icon: nil)
        } else {
            return nil
        }
    }

    weak var navigationDelegate: WKNavigationDelegate? {
        didSet {
            if let webView = webView {
                webView.navigationDelegate = navigationDelegate
            }
        }
    }

    func createWebview() {
        if webView == nil {
            assert(configuration != nil, "Create webview can only be called once")
            configuration!.userContentController = WKUserContentController()
            let webView = WKWebView(frame: CGRectZero, configuration: configuration!)
            configuration = nil

            webView.accessibilityLabel = NSLocalizedString("Web content", comment: "Accessibility label for the main web content view")
            webView.allowsBackForwardNavigationGestures = true
            webView.backgroundColor = UIColor.lightGrayColor()
            webView.scrollView.layer.masksToBounds = false

            // Turning off masking allows the web content to flow outside of the scrollView's frame
            // which allows the content appear beneath the toolbars in the BrowserViewController
            webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
            webView.scrollView.layer.masksToBounds = false
            webView.navigationDelegate = navigationDelegate
            helperManager = HelperManager(webView: webView)

            if let request = lastRequest {
                webView.loadRequest(request)
            }
<commit_msg>Bug 1168832 - Revert Deceleration Rate of WKWebView<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import Foundation
import WebKit
import Storage
import Shared

protocol BrowserHelper {
    static func name() -> String
    func scriptMessageHandlerName() -> String?
    func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
}

@objc
protocol BrowserDelegate {
    func browser(browser: Browser, didAddSnackbar bar: SnackBar)
    func browser(browser: Browser, didRemoveSnackbar bar: SnackBar)
    optional func browser(browser: Browser, didCreateWebView webView: WKWebView)
    optional func browser(browser: Browser, willDeleteWebView webView: WKWebView)
}

class Browser: NSObject {
    var webView: WKWebView? = nil

    var browserDelegate: BrowserDelegate? = nil
    var bars = [SnackBar]()
    var favicons = [Favicon]()

    var screenshot: UIImage?
    private var helperManager: HelperManager? = nil
    var lastRequest: NSURLRequest? = nil
    private var configuration: WKWebViewConfiguration? = nil

    init(configuration: WKWebViewConfiguration) {
        self.configuration = configuration
    }

    class func toTab(browser: Browser) -> RemoteTab? {
        if let displayURL = browser.displayURL {
            return RemoteTab(clientGUID: nil,
                URL: displayURL,
                title: browser.displayTitle,
                history: browser.historyList,
                lastUsed: Timestamp(),
                icon: nil)
        } else {
            return nil
        }
    }

    weak var navigationDelegate: WKNavigationDelegate? {
        didSet {
            if let webView = webView {
                webView.navigationDelegate = navigationDelegate
            }
        }
    }

    func createWebview() {
        if webView == nil {
            assert(configuration != nil, "Create webview can only be called once")
            configuration!.userContentController = WKUserContentController()
            let webView = WKWebView(frame: CGRectZero, configuration: configuration!)
            configuration = nil

            webView.accessibilityLabel = NSLocalizedString("Web content", comment: "Accessibility label for the main web content view")
            webView.allowsBackForwardNavigationGestures = true
            webView.backgroundColor = UIColor.lightGrayColor()
            webView.scrollView.layer.masksToBounds = false

            // Turning off masking allows the web content to flow outside of the scrollView's frame
            // which allows the content appear beneath the toolbars in the BrowserViewController
            webView.scrollView.layer.masksToBounds = false
            webView.navigationDelegate = navigationDelegate
            helperManager = HelperManager(webView: webView)

            if let request = lastRequest {
                webView.loadRequest(request)
            }

{'fraction_non_alphanumeric': 0.06734006734006734, 'fraction_numerical': 0.002405002405002405, 'mean_word_length': 2.6911242603550294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6871411', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1527, 'n_words': 488}