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

Add support for pir in DuckDuckGoApplication #5708

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import kotlinx.coroutines.*
import timber.log.Timber

private const val VPN_PROCESS_NAME = "vpn"
private const val PIR_PROCESS_NAME = "pir"

open class DuckDuckGoApplication : HasDaggerInjector, MultiProcessApplication() {

Expand Down Expand Up @@ -110,6 +111,13 @@ open class DuckDuckGoApplication : HasDaggerInjector, MultiProcessApplication()
}
}
}

runInSecondaryProcessNamed(PIR_PROCESS_NAME) {
configureLogging()
Timber.d("Init for secondary process $shortProcessName with pid=${android.os.Process.myPid()}")
configureStrictMode()
configureDependencyInjection()
}
}

private fun setupActivityLifecycleCallbacks() {
Expand Down Expand Up @@ -171,12 +179,15 @@ open class DuckDuckGoApplication : HasDaggerInjector, MultiProcessApplication()
mode: Int,
): File {
val dir = super.getDir(name, mode)
runInSecondaryProcessNamed(VPN_PROCESS_NAME) {
if (!isMainProcess) {
if (name == "webview") {
return File("${dir.absolutePath}/vpn").apply {
Timber.d(":vpn process getDir = $absolutePath")
if (!exists()) {
mkdirs()
val processName = shortProcessName
if (processName != "UNKNOWN") {
return File("${dir.absolutePath}/$processName").apply {
Timber.d(":$processName process getDir = $absolutePath")
if (!exists()) {
mkdirs()
}
}
}
}
Expand All @@ -186,11 +197,14 @@ open class DuckDuckGoApplication : HasDaggerInjector, MultiProcessApplication()

override fun getCacheDir(): File {
val dir = super.getCacheDir()
runInSecondaryProcessNamed(VPN_PROCESS_NAME) {
return File("${dir.absolutePath}/vpn").apply {
Timber.d(":vpn process getCacheDir = $absolutePath")
if (!exists()) {
mkdirs()
if (!isMainProcess) {
val processName = shortProcessName
if (processName != "UNKNOWN") {
return File("${dir.absolutePath}/$processName").apply {
Timber.d(":$processName process getCacheDir = $absolutePath")
if (!exists()) {
mkdirs()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import android.os.Build
import android.os.Process

abstract class MultiProcessApplication : Application() {
private val shortProcessName: String by lazy {
currentProcessName?.substringAfter(delimiter = packageName, missingDelimiterValue = "UNKNOWN") ?: "UNKNOWN"
}
private val shortProcessNameCached: String by lazy { shortProcessName }
private val isMainProcessCached: Boolean by lazy { isMainProcess }

final override fun onCreate() {
super.onCreate()
if (isMainProcessCached) {
onMainProcessCreate()
} else {
onSecondaryProcessCreate(shortProcessName)
onSecondaryProcessCreate(shortProcessNameCached)
}
}

Expand All @@ -42,17 +40,27 @@ abstract class MultiProcessApplication : Application() {
open fun onSecondaryProcessCreate(shortProcessName: String) {}
}

inline val Application.shortProcessName: String
get() = currentProcessName?.substringAfter(delimiter = "$packageName:", missingDelimiterValue = "UNKNOWN") ?: "UNKNOWN"

inline val Application.isMainProcess: Boolean
get() = packageName == currentProcessName

inline fun Context.runInSecondaryProcessNamed(name: String, block: () -> Unit) {
inline fun Context.runInSecondaryProcessNamed(
name: String,
block: () -> Unit,
) {
if (currentProcessName == "$packageName:$name") {
block()
}
}

val Context.currentProcessName: String?
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { Application.getProcessName() } else { processNameFromSystemService() }
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Application.getProcessName()
} else {
processNameFromSystemService()
}

private fun Context.processNameFromSystemService(): String {
val am = this.getSystemService(Application.ACTIVITY_SERVICE) as ActivityManager?
Expand Down
Loading