-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
303 additions
and
20 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
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
121 changes: 121 additions & 0 deletions
121
...ubscriptions-impl/src/main/java/com/duckduckgo/subscriptions/impl/serp_promo/SerpPromo.kt
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,121 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.duckduckgo.subscriptions.impl.serp_promo | ||
|
||
import android.webkit.CookieManager | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.subscriptions.impl.PrivacyProFeature | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import com.squareup.anvil.annotations.ContributesTo | ||
import dagger.Lazy | ||
import dagger.Module | ||
import dagger.Provides | ||
import javax.inject.Inject | ||
import javax.inject.Qualifier | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
import logcat.logcat | ||
|
||
interface SerpPromo { | ||
suspend fun injectCookie(cookieValue: String?) | ||
} | ||
|
||
private const val HTTPS_WWW_SUBSCRIPTION_DDG_COM = ".subscriptions.duckduckgo.com" | ||
private const val SERP_PPRO_PROMO_COOKIE_NAME = "privacy_pro_access_token" | ||
|
||
@ContributesBinding( | ||
scope = AppScope::class, | ||
boundType = SerpPromo::class, | ||
) | ||
@ContributesMultibinding( | ||
scope = AppScope::class, | ||
boundType = MainProcessLifecycleObserver::class, | ||
) | ||
class RealSerpPromo @Inject constructor( | ||
@InternalApi private val cookieManager: CookieManagerWrapper, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val privacyProFeature: Lazy<PrivacyProFeature>, | ||
) : SerpPromo, MainProcessLifecycleObserver { | ||
|
||
override suspend fun injectCookie(cookieValue: String?) = withContext(dispatcherProvider.io()) { | ||
if (privacyProFeature.get().serpPromoCookie().isEnabled()) { | ||
synchronized(cookieManager) { | ||
kotlin.runCatching { | ||
val cookieString = "$SERP_PPRO_PROMO_COOKIE_NAME = ${cookieValue.orEmpty()}; HttpOnly; Path=/;" | ||
cookieManager.setCookie(HTTPS_WWW_SUBSCRIPTION_DDG_COM, cookieString) | ||
} | ||
} | ||
return@withContext | ||
} | ||
} | ||
|
||
override fun onStart(owner: LifecycleOwner) { | ||
owner.lifecycleScope.launch(dispatcherProvider.io()) { | ||
if (privacyProFeature.get().serpPromoCookie().isEnabled()) { | ||
kotlin.runCatching { | ||
val cookies = cookieManager.getCookie(HTTPS_WWW_SUBSCRIPTION_DDG_COM) ?: "" | ||
val pproCookies = cookies.split(";").filter { it.contains(SERP_PPRO_PROMO_COOKIE_NAME) } | ||
if (pproCookies.isEmpty()) { | ||
injectCookie("") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// This class is basically a convenience wrapper for easier testing | ||
interface CookieManagerWrapper { | ||
/** | ||
* @return the cookie stored for the given [url] if any, null otherwise | ||
*/ | ||
fun getCookie(url: String): String? | ||
|
||
fun setCookie(url: String, cookieString: String) | ||
} | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Qualifier | ||
private annotation class InternalApi | ||
|
||
@Module | ||
@ContributesTo(AppScope::class) | ||
object CookieManagerWrapperModule { | ||
@Provides | ||
@InternalApi | ||
fun providesCookieManagerWrapper(): CookieManagerWrapper { | ||
return CookieManagerWrapperImpl() | ||
} | ||
} | ||
private class CookieManagerWrapperImpl constructor() : CookieManagerWrapper { | ||
|
||
private val cookieManager: CookieManager by lazy { CookieManager.getInstance() } | ||
override fun getCookie(url: String): String? { | ||
return cookieManager.getCookie(url) | ||
} | ||
|
||
override fun setCookie(domain: String, cookie: String) { | ||
logcat { "Setting cookie $cookie for domain $domain" } | ||
cookieManager.setCookie(domain, cookie) | ||
cookieManager.flush() | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
...riptions-impl/src/test/java/com/duckduckgo/subscriptions/impl/serp_promo/FakeSerpPromo.kt
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,26 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.duckduckgo.subscriptions.impl.serp_promo | ||
|
||
class FakeSerpPromo : SerpPromo { | ||
var cookie: String? = null | ||
private set | ||
|
||
override suspend fun injectCookie(cookieValue: String?) { | ||
this.cookie = cookieValue | ||
} | ||
} |
Oops, something went wrong.