From 43afb4fa8ed28f620487f763c9923c20a4e66051 Mon Sep 17 00:00:00 2001 From: Michal Smaga Date: Fri, 1 Nov 2024 01:13:14 +0100 Subject: [PATCH] Update the logic to support feature flag --- Core/PixelEvent.swift | 8 +-- DuckDuckGo/AppDelegate.swift | 54 +++++++++++++++---- ...riptionCookieManageEventPixelMapping.swift | 10 ++-- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/Core/PixelEvent.swift b/Core/PixelEvent.swift index 0a021242ea..4849cb9921 100644 --- a/Core/PixelEvent.swift +++ b/Core/PixelEvent.swift @@ -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 @@ -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 diff --git a/DuckDuckGo/AppDelegate.swift b/DuckDuckGo/AppDelegate.swift index d5f34b1a3c..390e7a03da 100644 --- a/DuckDuckGo/AppDelegate.swift +++ b/DuckDuckGo/AppDelegate.swift @@ -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 @@ -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, @@ -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 diff --git a/DuckDuckGo/Subscription/SubscriptionCookieManageEventPixelMapping.swift b/DuckDuckGo/Subscription/SubscriptionCookieManageEventPixelMapping.swift index 19efdba9dd..0ec0ee2ef9 100644 --- a/DuckDuckGo/Subscription/SubscriptionCookieManageEventPixelMapping.swift +++ b/DuckDuckGo/Subscription/SubscriptionCookieManageEventPixelMapping.swift @@ -30,12 +30,10 @@ public final class SubscriptionCookieManageEventPixelMapping: EventMapping