This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 440
/
AdblockRustEngine.swift
74 lines (62 loc) · 2.42 KB
/
AdblockRustEngine.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2022 The Brave Authors. All rights reserved.
// 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 BraveCore
extension AdblockEngine {
public enum ResourceType: String, Decodable {
case xmlhttprequest, script, image
}
/// Check the rust engine if the request should be blocked given the `sourceURL` and `resourceType`.
///
/// - Warning: You must provide a absolute URL (i.e. containing a host) fo r `requestURL` and `sourceURL`
public func shouldBlock(requestURL: URL, sourceURL: URL, resourceType: ResourceType, isAggressive: Bool) -> Bool {
guard requestURL.scheme != "data" else {
// TODO: @JS Investigate if we need to deal with data schemes and if so, how?
return false
}
guard sourceURL.absoluteString != "about:blank" else {
// TODO: @JS Investigate why sometimes `sourceURL` is `about:blank` and find out how to deal with it
return false
}
guard let requestDomain = requestURL.baseDomain, let sourceDomain = sourceURL.baseDomain else {
return false
}
guard let requestHost = requestURL.host, let sourceHost = sourceURL.host else {
return false
}
// The content blocker rule for third party is the following:
// "third-party triggers if the resource isn’t from the same domain as the main page resource"
let isThirdParty = requestDomain != sourceDomain
if !isAggressive {
// If we have standard mode for this engine,
// we don't block first party ads
guard isThirdParty else {
return false
}
}
return matches(
url: requestURL.absoluteString,
host: requestHost,
tabHost: sourceHost,
isThirdParty: isThirdParty,
resourceType: resourceType.rawValue
).didMatchRule
}
@available(*, deprecated, renamed: "deserialize(data:)")
@discardableResult
public func set(data: Data) -> Bool {
deserialize(data: data)
}
@available(*, deprecated, message: "Use AdblockEngine.addResources(_:)")
public func set(json: Data) -> Bool {
guard let string = String(data: json, encoding: .utf8) else {
return false
}
useResources(string)
return true
}
}
@available(*, deprecated, renamed: "AdblockEngine")
typealias AdblockRustEngine = AdblockEngine