forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebCacheManager.swift
More file actions
174 lines (133 loc) · 5.48 KB
/
WebCacheManager.swift
File metadata and controls
174 lines (133 loc) · 5.48 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// WebCacheManager.swift
// DuckDuckGo
//
// Copyright © 2017 DuckDuckGo. All rights reserved.
//
// 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 WebKit
public protocol WebCacheManagerCookieStore {
func getAllCookies(_ completionHandler: @escaping ([HTTPCookie]) -> Void)
func setCookie(_ cookie: HTTPCookie, completionHandler: (() -> Void)?)
func delete(_ cookie: HTTPCookie, completionHandler: (() -> Void)?)
}
public protocol WebCacheManagerDataStore {
var cookieStore: WebCacheManagerCookieStore? { get }
func removeAllData(completion: @escaping () -> Void)
}
public class WebCacheManager {
private struct Constants {
static let cookieDomain = "duckduckgo.com"
}
public static var shared = WebCacheManager()
private init() { }
public func consumeCookies(cookieStorage: CookieStorage = CookieStorage(),
httpCookieStore: WebCacheManagerCookieStore? = WKWebsiteDataStore.default().cookieStore,
completion: @escaping () -> Void) {
guard let httpCookieStore = httpCookieStore else {
completion()
return
}
let cookies = cookieStorage.cookies
guard !cookies.isEmpty else {
completion()
return
}
let group = DispatchGroup()
for cookie in cookies {
group.enter()
httpCookieStore.setCookie(cookie) {
group.leave()
}
}
DispatchQueue.global(qos: .userInitiated).async {
group.wait()
DispatchQueue.main.async {
cookieStorage.clear()
completion()
}
}
}
public func removeCookies(forDomains domains: [String],
dataStore: WebCacheManagerDataStore = WKWebsiteDataStore.default(),
completion: @escaping () -> Void) {
guard let cookieStore = dataStore.cookieStore else {
completion()
return
}
cookieStore.getAllCookies { cookies in
let group = DispatchGroup()
cookies.forEach { cookie in
domains.forEach { domain in
if cookie.domain == domain || (cookie.domain.hasPrefix(".") && domain.hasSuffix(cookie.domain)) {
group.enter()
cookieStore.delete(cookie) {
group.leave()
}
// don't try to delete the cookie twice as it doesn't always work (esecially on the simulator)
return
}
}
}
DispatchQueue.global(qos: .background).async {
_ = group.wait(timeout: .now() + 5)
DispatchQueue.main.async {
completion()
}
}
}
}
public func clear(dataStore: WebCacheManagerDataStore = WKWebsiteDataStore.default(),
appCookieStorage: CookieStorage = CookieStorage(),
logins: PreserveLogins = PreserveLogins.shared,
completion: @escaping () -> Void) {
extractAllowedCookies(from: dataStore.cookieStore, cookieStorage: appCookieStorage, logins: logins) {
self.clearAllData(dataStore: dataStore, completion: completion)
}
}
private func clearAllData(dataStore: WebCacheManagerDataStore, completion: @escaping () -> Void) {
dataStore.removeAllData(completion: completion)
}
private func extractAllowedCookies(from cookieStore: WebCacheManagerCookieStore?,
cookieStorage: CookieStorage,
logins: PreserveLogins,
completion: @escaping () -> Void) {
guard let cookieStore = cookieStore else {
completion()
return
}
cookieStore.getAllCookies { cookies in
for cookie in cookies {
if cookie.domain == Constants.cookieDomain || logins.isAllowed(cookieDomain: cookie.domain) {
cookieStorage.setCookie(cookie)
}
}
completion()
}
}
}
@available(iOS 11, *)
extension WKHTTPCookieStore: WebCacheManagerCookieStore {
}
extension WKWebsiteDataStore: WebCacheManagerDataStore {
public var cookieStore: WebCacheManagerCookieStore? {
guard #available(iOS 11, *) else { return nil }
return self.httpCookieStore
}
public func removeAllData(completion: @escaping () -> Void) {
removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(),
modifiedSince: Date.distantPast,
completionHandler: completion)
}
}