<commit_before>/*
 Copyright 2015 XWebView

 Licensed 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.
*/

import Foundation
import WebKit

public class XWVChannel : NSObject, WKScriptMessageHandler {
    public let name: String
    public let thread: NSThread!
    public let queue: dispatch_queue_t!
    private(set) public weak var webView: WKWebView?
    var typeInfo: XWVReflection!

    private var instances = [Int: XWVScriptPlugin]()
    private var userScript: WKUserScript?

    private class var sequenceNumber: UInt {
        struct sequence{
            static var number: UInt = 0
        }
        return ++sequence.number
    }

    public convenience init(name: String?, webView: WKWebView) {
        let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL)
        self.init(name: name, webView:webView, queue: queue)
    }
    public init(name: String?, webView: WKWebView, queue: dispatch_queue_t) {
        self.name = name ?? "\(XWVChannel.sequenceNumber)"
        self.webView = webView
        self.queue = queue
        thread = nil
        super.init()
        setup()
    }
    public init(identifier: String?, webView: WKWebView, thread: NSThread) {
        self.name = identifier ?? "\(XWVChannel.sequenceNumber)"
        self.webView = webView
        self.thread = thread
        queue = nil
        super.init()
        setup()
    }
    private func setup() {
        webView!.prepareForPlugin()
        webView!.configuration.userContentController.addScriptMessageHandler(self, name: "\(self.name)")
    }
    deinit {
        webView?.configuration.userContentController.removeScriptMessageHandlerForName("\(name)")
    }

    public func bindPlugin(object: NSObject, toNamespace namespace: String) -> XWVScriptObject? {
        assert(typeInfo == nil)
        typeInfo = XWVReflection(plugin: object.dynamicType)
        let plugin = XWVScriptPlugin(namespace: namespace, channel: self, object: object)
        let stub = XWVStubGenerator(channel: self).generateForNamespace(namespace, object: plugin)
        userScript = webView?.injectScript((object as? XWVScripting)?.javascriptStub?(stub) ?? stub)
        instances[0] = plugin
        return plugin as XWVScriptObject
    }

    public func unbind() {
        assert(typeInfo != nil)
        if webView?.URL != nil {
            webView!.evaluateJavaScript("delete \(instances[0]!.namespace);", completionHandler:nil)
        }
        if userScript != nil {
            webView?.configuration.userContentController.removeUserScript(userScript!)
        }
        instances.removeAll(keepCapacity: false)
    }

    public func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        if let body = message.body as? [String: AnyObject], let opcode = body["$opcode"] as? String {
            let target = (body["$target"] as? NSNumber)?.integerValue ?? 0
            if let object = instances[target] {
                if opcode == "-" {
                    if target == 0 {
                        // Destroy plugin
                        unbind()
                    } else {
                        // Destroy instance
                        let object = instances.removeValueForKey(target)
                        assert(object != nil)
                    }
                } else if typeInfo.hasProperty(opcode) {
                    // Update property
                    object.updateNativeProperty(opcode, withValue: body["$operand"])
                } else if typeInfo.hasMethod(opcode) {
                    // Invoke method<commit_msg>Fixed a bug of channel's lifecycle.<commit_after>/*
 Copyright 2015 XWebView

 Licensed 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.
*/

import Foundation
import WebKit

public class XWVChannel : NSObject, WKScriptMessageHandler {
    public let name: String
    public let thread: NSThread!
    public let queue: dispatch_queue_t!
    private(set) public weak var webView: WKWebView?
    var typeInfo: XWVReflection!

    private var instances = [Int: XWVScriptPlugin]()
    private var userScript: WKUserScript?

    private class var sequenceNumber: UInt {
        struct sequence{
            static var number: UInt = 0
        }
        return ++sequence.number
    }

    public convenience init(name: String?, webView: WKWebView) {
        let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL)
        self.init(name: name, webView:webView, queue: queue)
    }
    public init(name: String?, webView: WKWebView, queue: dispatch_queue_t) {
        self.name = name ?? "\(XWVChannel.sequenceNumber)"
        self.webView = webView
        self.queue = queue
        thread = nil
        webView.prepareForPlugin()
    }
    public init(name: String?, webView: WKWebView, thread: NSThread) {
        self.name = name ?? "\(XWVChannel.sequenceNumber)"
        self.webView = webView
        self.thread = thread
        queue = nil
        webView.prepareForPlugin()
    }

    public func bindPlugin(object: NSObject, toNamespace namespace: String) -> XWVScriptObject? {
        assert(typeInfo == nil)
        webView?.configuration.userContentController.addScriptMessageHandler(self, name: name)
        typeInfo = XWVReflection(plugin: object.dynamicType)
        let plugin = XWVScriptPlugin(namespace: namespace, channel: self, object: object)
        let stub = XWVStubGenerator(channel: self).generateForNamespace(namespace, object: plugin)
        userScript = webView?.injectScript((object as? XWVScripting)?.javascriptStub?(stub) ?? stub)
        instances[0] = plugin
        return plugin as XWVScriptObject
    }

    public func unbind() {
        assert(typeInfo != nil)
        if webView?.URL != nil {
            webView!.evaluateJavaScript("delete \(instances[0]!.namespace);", completionHandler:nil)
        }
        if userScript != nil {
            webView?.configuration.userContentController.removeUserScript(userScript!)
        }
        instances.removeAll(keepCapacity: false)
        webView?.configuration.userContentController.removeScriptMessageHandlerForName(name)
    }

    public func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
        if let body = message.body as? [String: AnyObject], let opcode = body["$opcode"] as? String {
            let target = (body["$target"] as? NSNumber)?.integerValue ?? 0
            if let object = instances[target] {
                if opcode == "-" {
                    if target == 0 {
                        // Destroy plugin
                        unbind()
                    } else {
                        // Destroy instance
                        let object = instances.removeValueForKey(target)
                        assert(object != nil)
                    }
                } else if typeInfo.hasProperty(opcode) {
                    // Update property
                    object.updateNativeProperty(opcode, withValue: body["$operand"])
                } else if typeInfo.hasMethod(opcode) {
                    // Invoke method
{'fraction_non_alphanumeric': 0.08753249969047913, 'fraction_numerical': 0.0032190169617432214, 'mean_word_length': 2.481896551724138, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7215787', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2041, 'n_words': 613}