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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
49adc90
commit e5e0287
Showing
37 changed files
with
1,199 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +0,0 @@ | ||
// 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 WebKit | ||
import Shared | ||
import Deferred | ||
import Data | ||
import BraveShared | ||
|
||
private let log = Logger.browserLogger | ||
|
||
class BlocklistName: Hashable, CustomStringConvertible, ContentBlocker { | ||
|
||
static let ad = BlocklistName(filename: "block-ads") | ||
static let tracker = BlocklistName(filename: "block-trackers") | ||
static let https = BlocklistName(filename: "upgrade-http") | ||
static let image = BlocklistName(filename: "block-images") | ||
static let cookie = BlocklistName(filename: "block-cookies") | ||
|
||
static var allLists: Set<BlocklistName> { return [.ad, .tracker, .https, .image] } | ||
|
||
let filename: String | ||
var rule: WKContentRuleList? | ||
|
||
init(filename: String) { | ||
self.filename = filename | ||
} | ||
|
||
var description: String { | ||
return "<\(type(of: self)): \(self.filename)>" | ||
} | ||
|
||
private static let blocklistFileVersionMap: [BlocklistName: Preferences.Option<String?>] = [ | ||
BlocklistName.ad: Preferences.BlockFileVersion.adblock, | ||
BlocklistName.https: Preferences.BlockFileVersion.httpse | ||
] | ||
|
||
lazy var fileVersionPref: Preferences.Option<String?>? = { | ||
return BlocklistName.blocklistFileVersionMap[self] | ||
}() | ||
|
||
lazy var fileVersion: String? = { | ||
guard let _ = BlocklistName.blocklistFileVersionMap[self] else { return nil } | ||
return Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String | ||
}() | ||
|
||
static func blocklists(forDomain domain: Domain) -> (on: Set<BlocklistName>, off: Set<BlocklistName>) { | ||
if domain.shield_allOff == 1 { | ||
return ([], allLists) | ||
} | ||
|
||
var onList = Set<BlocklistName>() | ||
|
||
let isPrivateBrowsing = PrivateBrowsingManager.shared.isPrivateBrowsing | ||
if domain.isShieldExpected(.AdblockAndTp, isPrivateBrowsing: isPrivateBrowsing) { | ||
onList.formUnion([.ad, .tracker]) | ||
} | ||
|
||
// For lists not implemented, always return exclude from `onList` to prevent accidental execution | ||
|
||
// TODO #159: Setup image shield | ||
|
||
if domain.isShieldExpected(.HTTPSE, isPrivateBrowsing: isPrivateBrowsing) { | ||
onList.formUnion([.https]) | ||
} | ||
|
||
return (onList, allLists.subtracting(onList)) | ||
} | ||
|
||
static func compileAll(ruleStore: WKContentRuleListStore) -> Deferred<Void> { | ||
let allCompiledDeferred = Deferred<Void>() | ||
var allOfThem = BlocklistName.allLists.map { | ||
$0.buildRule(ruleStore: ruleStore) | ||
} | ||
//Compile block-cookie additionally | ||
allOfThem.append(BlocklistName.cookie.buildRule(ruleStore: ruleStore)) | ||
all(allOfThem).upon { _ in | ||
allCompiledDeferred.fill(()) | ||
} | ||
|
||
return allCompiledDeferred | ||
} | ||
|
||
public static func == (lhs: BlocklistName, rhs: BlocklistName) -> Bool { | ||
return lhs.filename == rhs.filename | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(filename) | ||
} | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// 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 | ||
|
||
struct CachedNetworkResource { | ||
let data: Data | ||
let etag: String? | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// 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 Deferred | ||
import Shared | ||
|
||
private let log = Logger.browserLogger | ||
|
||
class NetworkManager { | ||
private let session: NetworkSession | ||
|
||
init(session: NetworkSession = URLSession.shared) { | ||
self.session = session | ||
} | ||
|
||
func dataRequest(with url: URL, completion: @escaping NetworkSessionDataResponse) { | ||
session.dataRequest(with: url) { data, response, error in | ||
completion(data, response, error) | ||
} | ||
} | ||
|
||
func dataRequest(with urlRequest: URLRequest, completion: @escaping NetworkSessionDataResponse) { | ||
session.dataRequest(with: urlRequest) { data, response, error in | ||
completion(data, response, error) | ||
} | ||
} | ||
|
||
func downloadResource(with url: URL, resourceType: NetworkResourceType, | ||
retryTimeout: TimeInterval? = 60) -> Deferred<CachedNetworkResource> { | ||
let completion = Deferred<CachedNetworkResource>() | ||
|
||
var request = URLRequest(url: url) | ||
|
||
// Makes the request conditional, returns 304 if Etag value did not change. | ||
let ifNoneMatchHeader = "If-None-Match" | ||
let fileNotModifiedStatusCode = 304 | ||
|
||
// Identifier for a specific version of a resource for a HTTP request | ||
let etagHeader = "Etag" | ||
|
||
switch resourceType { | ||
case .cached(let etag): | ||
let requestEtag = etag ?? UUID().uuidString | ||
|
||
// This cache policy is required to support `If-None-Match` header. | ||
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData | ||
request.addValue(requestEtag, forHTTPHeaderField: ifNoneMatchHeader) | ||
default: break | ||
} | ||
|
||
session.dataRequest(with: request) { data, response, error -> Void in | ||
if let err = error { | ||
log.error(err.localizedDescription) | ||
if let retryTimeout = retryTimeout { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + retryTimeout) { | ||
self.downloadResource(with: url, resourceType: resourceType, retryTimeout: retryTimeout).upon { resource in | ||
completion.fill(resource) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
guard let data = data, let response = response as? HTTPURLResponse else { | ||
log.error("Failed to unwrap http response or data") | ||
return | ||
} | ||
|
||
switch response.statusCode { | ||
case 400...499: | ||
log.error(""" | ||
Failed to download, status code: \(response.statusCode),\ | ||
URL:\(String(describing: response.url)) | ||
""") | ||
case fileNotModifiedStatusCode: | ||
log.info("File not modified") | ||
default: | ||
let responseEtag = resourceType.isCached() ? | ||
response.allHeaderFields[etagHeader] as? String : nil | ||
|
||
completion.fill(CachedNetworkResource(data: data, etag: responseEtag)) | ||
} | ||
} | ||
|
||
return completion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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 | ||
|
||
enum NetworkResourceType { | ||
case cached(etag: String?) | ||
case regular | ||
|
||
func isCached() -> Bool { | ||
switch self { | ||
case .cached(_): return true | ||
default: return false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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 | ||
|
||
typealias NetworkSessionDataResponse = (Data?, URLResponse?, Error?) -> Void | ||
|
||
protocol NetworkSession { | ||
func dataRequest(with url: URL, completion: @escaping NetworkSessionDataResponse) | ||
func dataRequest(with urlRequest: URLRequest, completion: @escaping NetworkSessionDataResponse) | ||
} | ||
|
||
extension URLSession: NetworkSession { | ||
func dataRequest(with url: URL, completion: @escaping NetworkSessionDataResponse) { | ||
let task = dataTask(with: url) { data, response, error in | ||
completion(data, response, error) | ||
} | ||
|
||
task.resume() | ||
} | ||
|
||
func dataRequest(with urlRequest: URLRequest, completion: @escaping NetworkSessionDataResponse) { | ||
let task = dataTask(with: urlRequest) { data, response, error in | ||
completion(data, response, error) | ||
} | ||
|
||
task.resume() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 | ||
|
||
class NetworkSessionMock: NetworkSession { | ||
var data: Data? | ||
var response: URLResponse? | ||
var error: Error? | ||
|
||
func dataRequest(with url: URL, completion: @escaping NetworkSessionDataResponse) { | ||
completion(data, response, error) | ||
} | ||
|
||
func dataRequest(with urlRequest: URLRequest, completion: @escaping NetworkSessionDataResponse) { | ||
completion(data, response, error) | ||
} | ||
} |
Oops, something went wrong.