-
Notifications
You must be signed in to change notification settings - Fork 616
Implement new cold app start detection heuristic #6950
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
97 changes: 97 additions & 0 deletions
97
firebase-sessions/src/main/kotlin/com/google/firebase/sessions/ProcessDataManager.kt
This file contains hidden or 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,97 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* 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.google.firebase.sessions | ||
|
||
import android.content.Context | ||
import android.os.Process | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
/** Manage process data, used for detecting cold app starts. */ | ||
internal interface ProcessDataManager { | ||
/** An in-memory uuid to uniquely identify this instance of this process. */ | ||
val myUuid: String | ||
|
||
/** Checks if this is a cold app start, meaning all processes in the mapping table are stale. */ | ||
fun isColdStart(processDataMap: Map<String, ProcessData>): Boolean | ||
|
||
/** Call to notify the process data manager that a session has been generated. */ | ||
fun onSessionGenerated() | ||
|
||
/** Update the mapping of the current processes with data about this process. */ | ||
fun updateProcessDataMap(processDataMap: Map<String, ProcessData>?): Map<String, ProcessData> | ||
|
||
/** Generate a new mapping of process data with the current process only. */ | ||
fun generateProcessDataMap() = updateProcessDataMap(mapOf()) | ||
} | ||
|
||
/** Manage process data, used for detecting cold app starts. */ | ||
@Singleton | ||
internal class ProcessDataManagerImpl | ||
@Inject | ||
constructor(private val appContext: Context, private val uuidGenerator: UuidGenerator) : | ||
ProcessDataManager { | ||
override val myUuid: String by lazy { uuidGenerator.next().toString() } | ||
|
||
private val myProcessName: String by lazy { | ||
ProcessDetailsProvider.getCurrentProcessDetails(appContext).processName | ||
} | ||
|
||
private var hasGeneratedSession: Boolean = false | ||
|
||
override fun isColdStart(processDataMap: Map<String, ProcessData>): Boolean { | ||
if (hasGeneratedSession) { | ||
// This process has been notified that a session was generated, so cannot be a cold start | ||
return false | ||
} | ||
|
||
return ProcessDetailsProvider.getAppProcessDetails(appContext) | ||
.mapNotNull { processDetails -> | ||
processDataMap[processDetails.processName]?.let { processData -> | ||
Pair(processDetails, processData) | ||
} | ||
} | ||
.all { (processDetails, processData) -> isProcessStale(processDetails, processData) } | ||
} | ||
|
||
override fun onSessionGenerated() { | ||
hasGeneratedSession = true | ||
} | ||
|
||
override fun updateProcessDataMap( | ||
processDataMap: Map<String, ProcessData>? | ||
): Map<String, ProcessData> = | ||
processDataMap | ||
?.toMutableMap() | ||
?.apply { this[myProcessName] = ProcessData(Process.myPid(), myUuid) } | ||
?.toMap() | ||
?: mapOf(myProcessName to ProcessData(Process.myPid(), myUuid)) | ||
|
||
/** | ||
* Returns true if the process is stale, meaning the persisted process data does not match the | ||
* running process details. | ||
*/ | ||
private fun isProcessStale( | ||
runningProcessDetails: ProcessDetails, | ||
persistedProcessData: ProcessData, | ||
): Boolean = | ||
if (myProcessName == runningProcessDetails.processName) { | ||
runningProcessDetails.pid != persistedProcessData.pid || myUuid != persistedProcessData.uuid | ||
} else { | ||
runningProcessDetails.pid != persistedProcessData.pid | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
120 changes: 120 additions & 0 deletions
120
firebase-sessions/src/test/kotlin/com/google/firebase/sessions/ProcessDataManagerTest.kt
This file contains hidden or 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,120 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* 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.google.firebase.sessions | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import com.google.firebase.FirebaseApp | ||
import com.google.firebase.sessions.testing.FakeFirebaseApp | ||
import com.google.firebase.sessions.testing.FakeRunningAppProcessInfo | ||
import com.google.firebase.sessions.testing.FakeUuidGenerator | ||
import com.google.firebase.sessions.testing.FakeUuidGenerator.Companion.UUID_1 as MY_UUID | ||
import com.google.firebase.sessions.testing.FakeUuidGenerator.Companion.UUID_2 as OTHER_UUID | ||
import org.junit.After | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
internal class ProcessDataManagerTest { | ||
@Test | ||
fun isColdStart_myProcess() { | ||
val appContext = FakeFirebaseApp().firebaseApp.applicationContext | ||
val processDataManager = ProcessDataManagerImpl(appContext, FakeUuidGenerator(MY_UUID)) | ||
|
||
val coldStart = | ||
processDataManager.isColdStart(mapOf(MY_PROCESS_NAME to ProcessData(MY_PID, MY_UUID))) | ||
|
||
assertThat(coldStart).isFalse() | ||
} | ||
|
||
fun isColdStart_myProcessCurrent_otherProcessCurrent() { | ||
val appContext = | ||
FakeFirebaseApp(processes = listOf(myProcessInfo, otherProcessInfo)) | ||
.firebaseApp | ||
.applicationContext | ||
val processDataManager = ProcessDataManagerImpl(appContext, FakeUuidGenerator(MY_UUID)) | ||
|
||
val coldStart = | ||
processDataManager.isColdStart( | ||
mapOf( | ||
MY_PROCESS_NAME to ProcessData(MY_PID, MY_UUID), | ||
OTHER_PROCESS_NAME to ProcessData(OTHER_PID, OTHER_UUID), | ||
) | ||
) | ||
|
||
assertThat(coldStart).isFalse() | ||
} | ||
|
||
@Test | ||
fun isColdStart_staleProcessPid() { | ||
val appContext = FakeFirebaseApp().firebaseApp.applicationContext | ||
val processDataManager = ProcessDataManagerImpl(appContext, FakeUuidGenerator(MY_UUID)) | ||
|
||
val coldStart = | ||
processDataManager.isColdStart(mapOf(MY_PROCESS_NAME to ProcessData(OTHER_PID, MY_UUID))) | ||
|
||
assertThat(coldStart).isTrue() | ||
} | ||
|
||
@Test | ||
fun isColdStart_staleProcessUuid() { | ||
val appContext = FakeFirebaseApp().firebaseApp.applicationContext | ||
val processDataManager = ProcessDataManagerImpl(appContext, FakeUuidGenerator(MY_UUID)) | ||
|
||
val coldStart = | ||
processDataManager.isColdStart(mapOf(MY_PROCESS_NAME to ProcessData(MY_PID, OTHER_UUID))) | ||
|
||
assertThat(coldStart).isTrue() | ||
} | ||
|
||
@Test | ||
fun isColdStart_myProcessStale_otherProcessCurrent() { | ||
val appContext = | ||
FakeFirebaseApp(processes = listOf(myProcessInfo, otherProcessInfo)) | ||
.firebaseApp | ||
.applicationContext | ||
val processDataManager = ProcessDataManagerImpl(appContext, FakeUuidGenerator(MY_UUID)) | ||
mrober marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
val coldStart = | ||
processDataManager.isColdStart( | ||
mapOf( | ||
MY_PROCESS_NAME to ProcessData(OTHER_PID, MY_UUID), | ||
OTHER_PROCESS_NAME to ProcessData(OTHER_PID, OTHER_UUID), | ||
) | ||
) | ||
|
||
assertThat(coldStart).isFalse() | ||
} | ||
|
||
@After | ||
fun cleanUp() { | ||
FirebaseApp.clearInstancesForTest() | ||
} | ||
|
||
private companion object { | ||
const val MY_PROCESS_NAME = "com.google.firebase.sessions.test" | ||
const val OTHER_PROCESS_NAME = "not.my.process" | ||
|
||
const val MY_PID = 0 | ||
const val OTHER_PID = 4 | ||
|
||
val myProcessInfo = FakeRunningAppProcessInfo(pid = MY_PID, processName = MY_PROCESS_NAME) | ||
|
||
val otherProcessInfo = | ||
FakeRunningAppProcessInfo(pid = OTHER_PID, processName = OTHER_PROCESS_NAME) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.