<commit_before>import Foundation

extension String {
  func regexReplacePattern(pattern:String,  with:String) -> String {
    let regex = try! NSRegularExpression(pattern:pattern, options: [])
    return regex.stringByReplacingMatchesInString(self, options: [], range: NSMakeRange(0, self.characters.count), withTemplate: with)
  }
}

extension NSURL {
  func hostWithGenericSubdomainPrefixRemoved() -> String? {
    return host?.regexReplacePattern("^(m\\.|www\\.|mobile\\.)", with:"");
  }
}

var loadedCache = [String: String]()
func loadJs(name: String) -> String {
  if let cached = loadedCache[name] {
    return cached
  }
  let path = NSBundle.mainBundle().pathForResource(name, ofType: "js")
  assert(path != nil)
  let result = try! String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
  loadedCache[name] = result
  return result
}

func loadAndRunJs(name: String, _ webView: UIWebView) {
  let fileContents = loadJs(name)
  webView.stringByEvaluatingJavaScriptFromString(fileContents)
}

func jsonParseArray(messageJSON: String?) -> [NSDictionary] {
  guard let data = messageJSON?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return [] }

  do {
    let obj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
    let result = obj as? [NSDictionary]
    return result ?? []
  } catch let error as NSError {
    print("Failed to load: \(error.localizedDescription)")
  }
  return []
}


extension LegacyWebView {
  func replaceImagesUsingTheVault(webView: UIWebView) {
    let isJsResourcesLoaded = webView.stringByEvaluatingJavaScriptFromString("typeof _brave_adInfo !== 'undefined'")

    if (isJsResourcesLoaded != "true") {
      loadAndRunJs("adInfo", webView)
      loadAndRunJs("adInfo-wrapper", webView)
    }

    guard var host = webView.request?.URL?.hostWithGenericSubdomainPrefixRemoved() else { return }
    // TODO: ffox code has a function surely to clean up URL
    host = host.regexReplacePattern(".+error\\.html\\?url=http", with: "http")
    let divSizeQuery = loadJs("adInfo-divquerytemplate").stringByReplacingOccurrencesOfString("HOST", withString: host)

    let jsonResult = webView.stringByEvaluatingJavaScriptFromString(divSizeQuery)
    if (jsonResult == nil || jsonResult?.characters.count < 1) {
      return
    }

    let divNamesAndSizes = jsonParseArray(jsonResult)
    if (divNamesAndSizes.count < 1) {
      return
    }

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let profile = appDelegate.getProfile(UIApplication.sharedApplication())
    var vaultServerHost = profile.prefs.stringForKey(VaultAddressSetting.prefKey) ?? VaultAddressSetting.defaultValue
    if !vaultServerHost.startsWith("http") {
      vaultServerHost = "http://" + vaultServerHost
    }

    for item in divNamesAndSizes {
      let w = item["width"] as? Int ?? 0
      let h = item["height"] as? Int ?? 0
      if (w == 0 || h == 0) {
        continue;
      }
      let divId = item["divId"] as! String
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        // TODO remove fake id
        let fakeId = "30c889b7-6e7f-4f7d-9410-85bf710eb0ff"
        let vault = "\(vaultServerHost)/ad?braveUserId=\(fakeId)&intentHost=\(host)&tagName=IFRAME&width=\(w)&height=\(h)"
        var vaultResponse: String
        do {
         // TODO use 'proper' async networking calls (NSURLConnection etc.)
         vaultResponse = try String(contentsOfURL: NSURL(string: vault)!, encoding: NSUTF8StringEncoding)

          dispatch_async(dispatch_get_main_queue(), {
            let js = "_brave_replaceDivWithNewContent({'divId':'\(divId)'," +
            "'width':\(w), 'height':\(h),'newContent':'\(vaultResponse)'})"
            webView.stringByEvaluatingJavaScriptFromString(js)<commit_msg>Changed vault route from "ad" to "replacement"<commit_after>import Foundation

extension String {
  func regexReplacePattern(pattern:String,  with:String) -> String {
    let regex = try! NSRegularExpression(pattern:pattern, options: [])
    return regex.stringByReplacingMatchesInString(self, options: [], range: NSMakeRange(0, self.characters.count), withTemplate: with)
  }
}

extension NSURL {
  func hostWithGenericSubdomainPrefixRemoved() -> String? {
    return host?.regexReplacePattern("^(m\\.|www\\.|mobile\\.)", with:"");
  }
}

var loadedCache = [String: String]()
func loadJs(name: String) -> String {
  if let cached = loadedCache[name] {
    return cached
  }
  let path = NSBundle.mainBundle().pathForResource(name, ofType: "js")
  assert(path != nil)
  let result = try! String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
  loadedCache[name] = result
  return result
}

func loadAndRunJs(name: String, _ webView: UIWebView) {
  let fileContents = loadJs(name)
  webView.stringByEvaluatingJavaScriptFromString(fileContents)
}

func jsonParseArray(messageJSON: String?) -> [NSDictionary] {
  guard let data = messageJSON?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) else { return [] }

  do {
    let obj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
    let result = obj as? [NSDictionary]
    return result ?? []
  } catch let error as NSError {
    print("Failed to load: \(error.localizedDescription)")
  }
  return []
}


extension LegacyWebView {
  func replaceImagesUsingTheVault(webView: UIWebView) {
    let isJsResourcesLoaded = webView.stringByEvaluatingJavaScriptFromString("typeof _brave_adInfo !== 'undefined'")

    if (isJsResourcesLoaded != "true") {
      loadAndRunJs("adInfo", webView)
      loadAndRunJs("adInfo-wrapper", webView)
    }

    guard var host = webView.request?.URL?.hostWithGenericSubdomainPrefixRemoved() else { return }
    // TODO: ffox code has a function surely to clean up URL
    host = host.regexReplacePattern(".+error\\.html\\?url=http", with: "http")
    let divSizeQuery = loadJs("adInfo-divquerytemplate").stringByReplacingOccurrencesOfString("HOST", withString: host)

    let jsonResult = webView.stringByEvaluatingJavaScriptFromString(divSizeQuery)
    if (jsonResult == nil || jsonResult?.characters.count < 1) {
      return
    }

    let divNamesAndSizes = jsonParseArray(jsonResult)
    if (divNamesAndSizes.count < 1) {
      return
    }

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let profile = appDelegate.getProfile(UIApplication.sharedApplication())
    var vaultServerHost = profile.prefs.stringForKey(VaultAddressSetting.prefKey) ?? VaultAddressSetting.defaultValue
    if !vaultServerHost.startsWith("http") {
      vaultServerHost = "http://" + vaultServerHost
    }

    for item in divNamesAndSizes {
      let w = item["width"] as? Int ?? 0
      let h = item["height"] as? Int ?? 0
      if (w == 0 || h == 0) {
        continue;
      }
      let divId = item["divId"] as! String
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        // TODO remove fake id
        let fakeId = "30c889b7-6e7f-4f7d-9410-85bf710eb0ff"
        let vault = "\(vaultServerHost)/replacement?braveUserId=\(fakeId)&intentHost=\(host)&tagName=IFRAME&width=\(w)&height=\(h)"
        var vaultResponse: String
        do {
         // TODO use 'proper' async networking calls (NSURLConnection etc.)
         vaultResponse = try String(contentsOfURL: NSURL(string: vault)!, encoding: NSUTF8StringEncoding)

          dispatch_async(dispatch_get_main_queue(), {
            let js = "_brave_replaceDivWithNewContent({'divId':'\(divId)'," +
            "'width':\(w), 'height':\(h),'newContent':'\(vaultResponse)'})"
            webView.stringByEvaluatingJavaScriptFromString(js)
{'fraction_non_alphanumeric': 0.12126673532440782, 'fraction_numerical': 0.007981462409886715, 'mean_word_length': 4.101116217990808, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '1446542', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2333, 'n_words': 499}