This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8320: Issue #8255: GeckoEngine: Drop speculative EngineSession if it crashed or process was killed r=pocmo a=csadilek Co-authored-by: Sebastian Kaspari <[email protected]>
- Loading branch information
Showing
12 changed files
with
965 additions
and
145 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
156 changes: 156 additions & 0 deletions
156
...a/src/main/java/mozilla/components/browser/engine/gecko/util/SpeculativeSessionFactory.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,156 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.browser.engine.gecko.util | ||
|
||
import androidx.annotation.VisibleForTesting | ||
import mozilla.components.browser.engine.gecko.GeckoEngineSession | ||
import mozilla.components.concept.engine.EngineSession | ||
import mozilla.components.concept.engine.Settings | ||
import mozilla.components.support.utils.ThreadUtils | ||
import org.mozilla.geckoview.GeckoRuntime | ||
|
||
/** | ||
* Helper factory for creating and maintaining a speculative [EngineSession]. | ||
*/ | ||
internal class SpeculativeSessionFactory { | ||
@VisibleForTesting internal var speculativeEngineSession: SpeculativeEngineSession? = null | ||
|
||
/** | ||
* Creates a speculative [EngineSession] using the provided [contextId] and [defaultSettings]. | ||
* Creates a private session if [private] is set to true. | ||
* | ||
* The speculative [EngineSession] is kept internally until explicitly needed and access via [get]. | ||
*/ | ||
@Synchronized | ||
fun create( | ||
runtime: GeckoRuntime, | ||
private: Boolean, | ||
contextId: String?, | ||
defaultSettings: Settings? | ||
) { | ||
ThreadUtils.assertOnUiThread() | ||
|
||
if (speculativeEngineSession?.matches(private, contextId) == true) { | ||
// We already have a speculative engine session for this configuration. Nothing to do here. | ||
return | ||
} | ||
|
||
// Clear any potentially non-matching engine session | ||
clear() | ||
|
||
speculativeEngineSession = SpeculativeEngineSession.create( | ||
this, | ||
runtime, | ||
private, | ||
contextId, | ||
defaultSettings | ||
) | ||
} | ||
|
||
/** | ||
* Clears the internal speculative [EngineSession]. | ||
*/ | ||
@Synchronized | ||
fun clear() { | ||
speculativeEngineSession?.cleanUp() | ||
speculativeEngineSession = null | ||
} | ||
|
||
/** | ||
* Returns a previously created ([private] speculative [EngineSession] if it uses the same | ||
* [contextId]. Returns `null` if no speculative [EngineSession] for that configuration is | ||
* available. | ||
*/ | ||
@Synchronized | ||
fun get( | ||
private: Boolean, | ||
contextId: String? | ||
): GeckoEngineSession? { | ||
val speculativeEngineSession = speculativeEngineSession ?: return null | ||
|
||
return if (speculativeEngineSession.matches(private, contextId)) { | ||
this.speculativeEngineSession = null | ||
speculativeEngineSession.unwrap() | ||
} else { | ||
clear() | ||
null | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun hasSpeculativeSession(): Boolean { | ||
return speculativeEngineSession != null | ||
} | ||
} | ||
|
||
/** | ||
* Internal wrapper for [GeckoEngineSession] that takes care of registering and unregistering an | ||
* observer for handling content process crashes/kills. | ||
*/ | ||
internal class SpeculativeEngineSession constructor( | ||
@VisibleForTesting internal val engineSession: GeckoEngineSession, | ||
@VisibleForTesting internal val observer: SpeculativeSessionObserver | ||
) { | ||
/** | ||
* Checks whether the [SpeculativeEngineSession] matches the given configuration. | ||
*/ | ||
fun matches(private: Boolean, contextId: String?): Boolean { | ||
return engineSession.geckoSession.settings.usePrivateMode == private && | ||
engineSession.geckoSession.settings.contextId == contextId | ||
} | ||
|
||
/** | ||
* Unwraps the internal [GeckoEngineSession]. | ||
* | ||
* After calling [unwrap] the wrapper will no longer observe the [GeckoEngineSession] and fruther | ||
* crash handling is left to the application. | ||
*/ | ||
fun unwrap(): GeckoEngineSession { | ||
engineSession.unregister(observer) | ||
return engineSession | ||
} | ||
|
||
/** | ||
* Cleans up the internal state of this [SpeculativeEngineSession]. After calling this method | ||
* his [SpeculativeEngineSession] cannot be used anynmore. | ||
*/ | ||
fun cleanUp() { | ||
engineSession.unregister(observer) | ||
engineSession.close() | ||
} | ||
|
||
companion object { | ||
fun create( | ||
factory: SpeculativeSessionFactory, | ||
runtime: GeckoRuntime, | ||
private: Boolean, | ||
contextId: String?, | ||
defaultSettings: Settings? | ||
): SpeculativeEngineSession { | ||
val engineSession = GeckoEngineSession(runtime, private, defaultSettings, contextId) | ||
val observer = SpeculativeSessionObserver(factory) | ||
engineSession.register(observer) | ||
|
||
return SpeculativeEngineSession(engineSession, observer) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* [EngineSession.Observer] implementation that will notify the [SpeculativeSessionFactory] if an | ||
* [GeckoEngineSession] can no longer be used after a crash. | ||
*/ | ||
internal class SpeculativeSessionObserver( | ||
private val factory: SpeculativeSessionFactory | ||
|
||
) : EngineSession.Observer { | ||
override fun onCrash() { | ||
factory.clear() | ||
} | ||
|
||
override fun onProcessKilled() { | ||
factory.clear() | ||
} | ||
} |
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
Oops, something went wrong.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.
3f00d96
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh! Looks like an error! Details
Failed to get your artifact.