forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppURLs.swift
More file actions
261 lines (204 loc) · 10.7 KB
/
AppURLs.swift
File metadata and controls
261 lines (204 loc) · 10.7 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//
// AppURLs.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 BrowserServicesKit
import Foundation
public extension URL {
private static let base: String = ProcessInfo.processInfo.environment["BASE_URL", default: "https://duckduckgo.com"]
private static let staticBase: String = "https://staticcdn.duckduckgo.com"
static let ddg = URL(string: URL.base)!
static let autocomplete = URL(string: "\(base)/ac/")!
static let emailProtection = URL(string: "\(base)/email")!
static let emailProtectionSignUp = URL(string: "\(base)/email/start-incontext")!
static let emailProtectionQuickLink = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/email"))!
static let emailProtectionAccountLink = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/email/settings/account"))!
static let emailProtectionSupportLink = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/email/settings/support"))!
static let emailProtectionHelpPageLink = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/duckduckgo-help-pages/email-protection/what-is-duckduckgo-email-protection/"))!
static let aboutLink = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/about"))!
static let apps = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/apps?origin=funnel_app_ios"))!
static let searchSettings = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/settings"))!
static let autofillHelpPageLink = URL(string: AppDeepLinkSchemes.quickLink.appending("\(ddg.host!)/duckduckgo-help-pages/sync-and-backup/password-manager-security/"))!
static let surrogates = URL(string: "\(staticBase)/surrogates.txt")!
// The following URLs shall match the ones in update_embedded.sh.
// Danger checks that the URLs match on every PR. If the code changes, the regex that Danger uses may need an update.
static let privacyConfig = URL(string: "\(staticBase)/trackerblocking/config/v4/ios-config.json")!
static let trackerDataSet = URL(string: "\(staticBase)/trackerblocking/v5/current/ios-tds.json")!
static let bloomFilter = URL(string: "\(staticBase)/https/https-mobile-v2-bloom.bin")!
static let bloomFilterSpec = URL(string: "\(staticBase)/https/https-mobile-v2-bloom-spec.json")!
static let bloomFilterExcludedDomains = URL(string: "\(staticBase)/https/https-mobile-v2-false-positives.json")!
private static var devMode: String { isDebugBuild ? "?test=1" : "" }
static let atb = URL(string: "\(base)/atb.js\(devMode)")!
static let exti = URL(string: "\(base)/exti/\(devMode)")!
static let feedback = URL(string: "\(base)/feedback.js?type=app-feedback")!
static let appStore = URL(string: "https://apps.apple.com/app/duckduckgo-privacy-browser/id663592361")!
static let mac = URL(string: "\(base)/mac")!
static let windows = URL(string: "\(base)/windows")!
static func makeExtiURL(atb: String) -> URL { URL.exti.appendingParameter(name: Param.atb, value: atb) }
static func isDuckDuckGo(domain: String?) -> Bool {
guard let domain = domain, let url = URL(string: "https://\(domain)") else { return false }
return url.isDuckDuckGo
}
var isDuckDuckGo: Bool { isPart(ofDomain: URL.ddg.host!) }
var isDuckDuckGoStatic: Bool {
let staticPaths = ["/settings", "/params"]
guard isDuckDuckGo, staticPaths.contains(path) else { return false }
return true
}
var isDuckDuckGoSearch: Bool {
guard isDuckDuckGo, getParameter(named: Param.search) != nil else { return false }
return true
}
var isDuckDuckGoEmailProtection: Bool { absoluteString.starts(with: URL.emailProtection.absoluteString) }
var searchQuery: String? {
guard isDuckDuckGo else { return nil }
return getParameter(named: Param.search)
}
func applyingSearchHeaderParams() -> URL {
removingParameters(named: [Param.searchHeader]).appendingParameter(name: Param.searchHeader, value: ParamValue.searchHeader)
}
var hasCorrectSearchHeaderParams: Bool {
guard let header = getParameter(named: Param.searchHeader) else { return false }
return header == ParamValue.searchHeader
}
func removingInternalSearchParameters() -> URL {
guard isDuckDuckGoSearch else { return self }
return removingParameters(named: [Param.atb, Param.source, Param.searchHeader])
}
fileprivate enum Param {
static let search = "q"
static let source = "t"
static let atb = "atb"
static let setAtb = "set_atb"
static let activityType = "at"
static let partialHost = "pv1"
static let searchHeader = "ko"
static let vertical = "ia"
static let verticalRewrite = "iar"
static let verticalMaps = "iaxm"
static let email = "email"
}
fileprivate enum ParamValue {
static let source = "ddg_ios"
static let appUsage = "app_use"
static let searchHeader = "-1"
static let emailEnabled = "1"
static let emailDisabled = "0"
static let majorVerticals: Set<String> = ["images", "videos", "news"]
}
// MARK: - StatisticsDependentURLs
private static let defaultStatisticsDependentURLFactory = StatisticsDependentURLFactory()
static func makeSearchURL(text: String) -> URL? { defaultStatisticsDependentURLFactory.makeSearchURL(text: text) }
static func makeSearchURL(query: String, queryContext: URL? = nil) -> URL? {
defaultStatisticsDependentURLFactory.makeSearchURL(query: query, queryContext: queryContext)
}
func applyingStatsParams() -> URL { URL.defaultStatisticsDependentURLFactory.applyingStatsParams(to: self) }
static var searchAtb: URL? { defaultStatisticsDependentURLFactory.makeSearchAtbURL() }
static var appAtb: URL? { defaultStatisticsDependentURLFactory.makeAppAtbURL() }
var hasCorrectMobileStatsParams: Bool { URL.defaultStatisticsDependentURLFactory.hasCorrectMobileStatsParams(url: self) }
static func makePixelURL(pixelName: String, formFactor: String? = nil, includeATB: Bool = true) -> URL {
defaultStatisticsDependentURLFactory.makePixelURL(pixelName: pixelName, formFactor: formFactor, includeATB: includeATB)
}
}
public final class StatisticsDependentURLFactory {
private let statisticsStore: StatisticsStore
init(statisticsStore: StatisticsStore = StatisticsUserDefaults()) {
self.statisticsStore = statisticsStore
}
// MARK: Search
func makeSearchURL(text: String) -> URL? {
makeSearchURL(text: text, additionalParameters: [])
}
func makeSearchURL(query: String, queryContext: URL? = nil) -> URL? {
if let url = URL.webUrl(from: query) {
return url
}
var parameters = [String: String]()
if let queryContext = queryContext,
queryContext.isDuckDuckGoSearch,
queryContext.getParameter(named: URL.Param.verticalMaps) == nil,
let vertical = queryContext.getParameter(named: URL.Param.vertical),
URL.ParamValue.majorVerticals.contains(vertical) {
parameters[URL.Param.verticalRewrite] = vertical
}
return makeSearchURL(text: query, additionalParameters: parameters)
}
/**
Generates a search url with the source (t) https://duck.co/help/privacy/t
and cohort (atb) https://duck.co/help/privacy/atb
*/
private func makeSearchURL<C: Collection>(text: String, additionalParameters: C) -> URL
where C.Element == (key: String, value: String) {
let searchURL = URL.ddg
.appendingParameter(name: URL.Param.search, value: text)
.appendingParameters(additionalParameters)
return applyingStatsParams(to: searchURL)
}
func applyingStatsParams(to url: URL) -> URL {
var searchURL = url.removingParameters(named: [URL.Param.source, URL.Param.atb])
.appendingParameter(name: URL.Param.source,
value: URL.ParamValue.source)
if let atbWithVariant = statisticsStore.atbWithVariant {
searchURL = searchURL.appendingParameter(name: URL.Param.atb, value: atbWithVariant)
}
return searchURL
}
// MARK: ATB
func makeSearchAtbURL() -> URL? {
guard let atbWithVariant = statisticsStore.atbWithVariant, let setAtb = statisticsStore.searchRetentionAtb else {
return nil
}
return URL.atb.appendingParameters([
URL.Param.atb: atbWithVariant,
URL.Param.setAtb: setAtb,
URL.Param.email: EmailManager().isSignedIn ? URL.ParamValue.emailEnabled : URL.ParamValue.emailDisabled
])
}
func makeAppAtbURL() -> URL? {
guard let atbWithVariant = statisticsStore.atbWithVariant, let setAtb = statisticsStore.appRetentionAtb else {
return nil
}
return URL.atb.appendingParameters([
URL.Param.activityType: URL.ParamValue.appUsage,
URL.Param.atb: atbWithVariant,
URL.Param.setAtb: setAtb,
URL.Param.email: EmailManager().isSignedIn ? URL.ParamValue.emailEnabled : URL.ParamValue.emailDisabled
])
}
func hasCorrectMobileStatsParams(url: URL) -> Bool {
guard let source = url.getParameter(named: URL.Param.source),
source == URL.ParamValue.source
else { return false }
if let atbWithVariant = statisticsStore.atbWithVariant {
return atbWithVariant == url.getParameter(named: URL.Param.atb)
}
return true
}
// MARK: Pixel
private static let pixelBase: String = ProcessInfo.processInfo.environment["PIXEL_BASE_URL", default: "https://improving.duckduckgo.com"]
func makePixelURL(pixelName: String, formFactor: String? = nil, includeATB: Bool = true) -> URL {
var urlString = "\(Self.pixelBase)/t/\(pixelName)"
if let formFactor = formFactor {
urlString.append("_ios_\(formFactor)")
}
var url = URL(string: urlString)!
if includeATB {
url = url.appendingParameter(name: URL.Param.atb, value: statisticsStore.atbWithVariant ?? "")
}
return url
}
}