Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to subscription cookie #3512

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Core/PixelEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ extension Pixel {
case privacyProKeychainAccessError
case privacyProSubscriptionCookieMissingTokenOnSignIn
case privacyProSubscriptionCookieMissingCookieOnSignOut
case privacyProSubscriptionCookieRefreshedWithUpdate
case privacyProSubscriptionCookieRefreshedWithDelete
case privacyProSubscriptionCookieRefreshedWithAccessToken
case privacyProSubscriptionCookieRefreshedWithEmptyValue
case privacyProSubscriptionCookieFailedToSetSubscriptionCookie

// MARK: Pixel Experiment
Expand Down Expand Up @@ -1527,8 +1527,8 @@ extension Pixel.Event {
case .privacyProKeychainAccessError: return "m_privacy-pro_keychain_access_error"
case .privacyProSubscriptionCookieMissingTokenOnSignIn: return "m_privacy-pro_subscription-cookie-missing_token_on_sign_in"
case .privacyProSubscriptionCookieMissingCookieOnSignOut: return "m_privacy-pro_subscription-cookie-missing_cookie_on_sign_out"
case .privacyProSubscriptionCookieRefreshedWithUpdate: return "m_privacy-pro_subscription-cookie-refreshed_with_update"
case .privacyProSubscriptionCookieRefreshedWithDelete: return "m_privacy-pro_subscription-cookie-refreshed_with_delete"
case .privacyProSubscriptionCookieRefreshedWithAccessToken: return "m_privacy-pro_subscription-cookie-refreshed_with_access_token"
case .privacyProSubscriptionCookieRefreshedWithEmptyValue: return "m_privacy-pro_subscription-cookie-refreshed_with_empty_value"
case .privacyProSubscriptionCookieFailedToSetSubscriptionCookie: return "m_privacy-pro_subscription-cookie-failed_to_set_subscription_cookie"

// MARK: Pixel Experiment
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11009,8 +11009,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/DuckDuckGo/BrowserServicesKit";
requirement = {
kind = exactVersion;
version = 201.0.0;
branch = "michal/update-subs-cookie";
kind = branch;
};
};
9F8FE9472BAE50E50071E372 /* XCRemoteSwiftPackageReference "lottie-spm" */ = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/DuckDuckGo/BrowserServicesKit",
"state" : {
"revision" : "e5946eee6af859690cc1cc5e51daef3c8368981b",
"version" : "201.0.0"
"branch" : "michal/update-subs-cookie",
"revision" : "9506581ae99273681073f9993fc6d881d3edaa7f"
}
},
{
Expand Down
54 changes: 43 additions & 11 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import os.log

private(set) var subscriptionFeatureAvailability: SubscriptionFeatureAvailability!
private var subscriptionCookieManager: SubscriptionCookieManaging!
private var subscriptionCookieManagerFeatureFlagCancellable: AnyCancellable?
var privacyProDataReporter: PrivacyProDataReporting!

// MARK: - Feature specific app event handlers
Expand Down Expand Up @@ -313,17 +314,8 @@ import os.log
subscriptionFeatureAvailability = DefaultSubscriptionFeatureAvailability(
privacyConfigurationManager: ContentBlocking.shared.privacyConfigurationManager,
purchasePlatform: .appStore)

subscriptionCookieManager = SubscriptionCookieManager(subscriptionManager: AppDependencyProvider.shared.subscriptionManager,
currentCookieStore: { [weak self] in
guard self?.mainViewController?.tabManager.model.hasActiveTabs ?? false else {
// We shouldn't interact with WebKit's cookie store unless we have a WebView,
// eventually the subscription cookie will be refreshed on opening the first tab
return nil
}

return WKWebsiteDataStore.current().httpCookieStore
}, eventMapping: SubscriptionCookieManageEventPixelMapping())

subscriptionCookieManager = makeSubscriptionCookieManager()

homePageConfiguration = HomePageConfiguration(variantManager: AppDependencyProvider.shared.variantManager,
remoteMessagingClient: remoteMessagingClient,
Expand Down Expand Up @@ -416,6 +408,46 @@ import os.log
return true
}

private func makeSubscriptionCookieManager() -> SubscriptionCookieManaging {
let subscriptionCookieManager = SubscriptionCookieManager(subscriptionManager: AppDependencyProvider.shared.subscriptionManager,
currentCookieStore: { [weak self] in
guard self?.mainViewController?.tabManager.model.hasActiveTabs ?? false else {
// We shouldn't interact with WebKit's cookie store unless we have a WebView,
// eventually the subscription cookie will be refreshed on opening the first tab
return nil
}

return WKWebsiteDataStore.current().httpCookieStore
}, eventMapping: SubscriptionCookieManageEventPixelMapping())


let privacyConfigurationManager = ContentBlocking.shared.privacyConfigurationManager

// Enable subscriptionCookieManager if feature flag is present
if privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.setAccessTokenCookieForSubscriptionDomains) {
subscriptionCookieManager.enableSettingSubscriptionCookie()
}

// Keep track of feature flag changes
subscriptionCookieManagerFeatureFlagCancellable = privacyConfigurationManager.updatesPublisher
.sink { [weak self, weak privacyConfigurationManager] in
guard let self, let privacyConfigurationManager else { return }

let isEnabled = privacyConfigurationManager.privacyConfig.isSubfeatureEnabled(PrivacyProSubfeature.setAccessTokenCookieForSubscriptionDomains)

Task { [weak self] in
if isEnabled {
self?.subscriptionCookieManager.enableSettingSubscriptionCookie()
await self?.subscriptionCookieManager.refreshSubscriptionCookie()
} else {
await self?.subscriptionCookieManager.disableSettingSubscriptionCookie()
}
}
}

return subscriptionCookieManager
}

private func makeHistoryManager() -> HistoryManaging {

let provider = AppDependencyProvider.shared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ public final class SubscriptionCookieManageEventPixelMapping: EventMapping<Subsc
switch event {
case .errorHandlingAccountDidSignInTokenIsMissing:
return .privacyProSubscriptionCookieMissingTokenOnSignIn
case .errorHandlingAccountDidSignOutCookieIsMissing:
return .privacyProSubscriptionCookieMissingCookieOnSignOut
case .subscriptionCookieRefreshedWithUpdate:
return .privacyProSubscriptionCookieRefreshedWithUpdate
case .subscriptionCookieRefreshedWithDelete:
return .privacyProSubscriptionCookieRefreshedWithDelete
case .subscriptionCookieRefreshedWithAccessToken:
return .privacyProSubscriptionCookieRefreshedWithAccessToken
case .subscriptionCookieRefreshedWithEmptyValue:
return .privacyProSubscriptionCookieRefreshedWithEmptyValue
case .failedToSetSubscriptionCookie:
return .privacyProSubscriptionCookieFailedToSetSubscriptionCookie
}
Expand Down