<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 Shared
import Storage
import XCGLogger

private let log = Logger.browserLogger

private let URLBeforePathRegex = try! NSRegularExpression(pattern: "^https?://([^/]+)/", options: [])

// TODO: Swift currently requires that classes extending generic classes must also be generic.
// This is a workaround until that requirement is fixed.
typealias SearchLoader = _SearchLoader<AnyObject, AnyObject>

/**
 * Shared data source for the SearchViewController and the URLBar domain completion.
 * Since both of these use the same SQL query, we can perform the query once and dispatch the results.
 */
class _SearchLoader<UnusedA, UnusedB>: Loader<Cursor<Site>, CliqzSearchViewController> {
    private let profile: Profile
    private let urlBar: URLBarView
    private var inProgress: Cancellable? = nil

    init(profile: Profile, urlBar: URLBarView) {
        self.profile = profile
        self.urlBar = urlBar
        super.init()
    }

    private lazy var topDomains: [String] = {
        let filePath = NSBundle.mainBundle().pathForResource("topdomains", ofType: "txt")
        return try! String(contentsOfFile: filePath!).componentsSeparatedByString("\n")
    }()

    var query: String = "" {
        didSet {
            if query.isEmpty {
                self.load(Cursor(status: .Success, msg: "Empty query"))
                return
            }

            if let inProgress = inProgress {
                inProgress.cancel()
                self.inProgress = nil
            }
            // Cliqz: disable auto complete from both history and top domains
            /*
            let deferred = self.profile.history.getSitesByFrecencyWithHistoryLimit(100, bookmarksLimit: 5, whereURLContains: query)
            inProgress = deferred as? Cancellable

            deferred.uponQueue(dispatch_get_main_queue()) { result in
                self.inProgress = nil
             
                // Failed cursors are excluded in .get().
                if let cursor = result.successValue {
                    // First, see if the query matches any URLs from the user's search history.
                    self.load(cursor)
                    for site in cursor {
                        if let url = site?.url,
                               completion = self.completionForURL(url) {
                            self.urlBar.setAutocompleteSuggestion(completion)
                            return
                        }
                    }

                    // If there are no search history matches, try matching one of the Alexa top domains.
                    for domain in self.topDomains {
                        if let completion = self.completionForDomain(domain) {
                            self.urlBar.setAutocompleteSuggestion(completion)
                            return
                        }
                    }
                }
            }
            */
        }
    }<commit_msg>[IB-824] Fixed the issue of not showing history results<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 Shared
import Storage
import XCGLogger

private let log = Logger.browserLogger

private let URLBeforePathRegex = try! NSRegularExpression(pattern: "^https?://([^/]+)/", options: [])

// TODO: Swift currently requires that classes extending generic classes must also be generic.
// This is a workaround until that requirement is fixed.
typealias SearchLoader = _SearchLoader<AnyObject, AnyObject>

/**
 * Shared data source for the SearchViewController and the URLBar domain completion.
 * Since both of these use the same SQL query, we can perform the query once and dispatch the results.
 */
class _SearchLoader<UnusedA, UnusedB>: Loader<Cursor<Site>, CliqzSearchViewController> {
    private let profile: Profile
    private let urlBar: URLBarView
    private var inProgress: Cancellable? = nil

    init(profile: Profile, urlBar: URLBarView) {
        self.profile = profile
        self.urlBar = urlBar
        super.init()
    }

    private lazy var topDomains: [String] = {
        let filePath = NSBundle.mainBundle().pathForResource("topdomains", ofType: "txt")
        return try! String(contentsOfFile: filePath!).componentsSeparatedByString("\n")
    }()

    var query: String = "" {
        didSet {
            if query.isEmpty {
                self.load(Cursor(status: .Success, msg: "Empty query"))
                return
            }

            if let inProgress = inProgress {
                inProgress.cancel()
                self.inProgress = nil
            }
            
            let deferred = self.profile.history.getSitesByFrecencyWithHistoryLimit(100, bookmarksLimit: 5, whereURLContains: query)
            inProgress = deferred as? Cancellable

            deferred.uponQueue(dispatch_get_main_queue()) { result in
                self.inProgress = nil
             
                // Failed cursors are excluded in .get().
                if let cursor = result.successValue {
                    // First, see if the query matches any URLs from the user's search history.
                    self.load(cursor)
                    // Cliqz: disable auto complete from both history and top domains
                    /*
                    for site in cursor {
                        if let url = site?.url,
                               completion = self.completionForURL(url) {
                            self.urlBar.setAutocompleteSuggestion(completion)
                            return
                        }
                    }
                    // Cliqz: disable auto complete from both history and top domains
                    // If there are no search history matches, try matching one of the Alexa top domains.
                    for domain in self.topDomains {
                        if let completion = self.completionForDomain(domain) {
                            self.urlBar.setAutocompleteSuggestion(completion)
                            return
                        }
                    }
                    */
                }
            }
        }
    }
{'fraction_non_alphanumeric': 0.07463144963144963, 'fraction_numerical': 0.0029176904176904175, 'mean_word_length': 1.746942218473218, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5383923', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1511, 'n_words': 532}