This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1861459 - Remove Home Screen usages of BrowsingModeManager
- Loading branch information
1 parent
d5fa393
commit ea62d47
Showing
17 changed files
with
426 additions
and
116 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
56 changes: 56 additions & 0 deletions
56
fenix/app/src/main/java/org/mozilla/fenix/BrowsingModeBinding.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,56 @@ | ||
/* 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 org.mozilla.fenix | ||
|
||
import android.view.Window | ||
import android.view.WindowManager | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChangedBy | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.lib.state.helpers.AbstractBinding | ||
import org.mozilla.fenix.browser.browsingmode.BrowsingMode | ||
import org.mozilla.fenix.components.AppStore | ||
import org.mozilla.fenix.components.appstate.AppState | ||
import org.mozilla.fenix.theme.ThemeManager | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
/** | ||
* Binding to react to Private Browsing Mode changes in AppState. | ||
* | ||
* @param appStore AppStore to observe state changes from. | ||
* @param themeManager Theme will be updated based on state changes. | ||
* @param retrieveWindow Get window to update privacy flags for. | ||
* @param settings Determine user settings for privacy features. | ||
* @param ioDispatcher Dispatcher to launch disk reads. Exposed for test. | ||
*/ | ||
class BrowsingModeBinding( | ||
appStore: AppStore, | ||
private val themeManager: ThemeManager, | ||
private val retrieveWindow: () -> Window, | ||
private val settings: Settings, | ||
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO, | ||
) : AbstractBinding<AppState>(appStore) { | ||
override suspend fun onState(flow: Flow<AppState>) { | ||
flow.distinctUntilChangedBy { it.mode }.collect { | ||
themeManager.currentTheme = it.mode | ||
setWindowPrivacy(it.mode) | ||
} | ||
} | ||
|
||
private suspend fun setWindowPrivacy(mode: BrowsingMode) { | ||
if (mode == BrowsingMode.Private) { | ||
val allowScreenshots = withContext(ioDispatcher) { | ||
settings.allowScreenshotsInPrivateMode | ||
} | ||
if (!allowScreenshots) { | ||
retrieveWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE) | ||
} | ||
} else { | ||
retrieveWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE) | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
fenix/app/src/main/java/org/mozilla/fenix/BrowsingModePersistenceMiddleware.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,49 @@ | ||
/* 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 org.mozilla.fenix | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import mozilla.components.lib.state.Middleware | ||
import mozilla.components.lib.state.MiddlewareContext | ||
import org.mozilla.fenix.components.appstate.AppAction | ||
import org.mozilla.fenix.components.appstate.AppState | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
/** | ||
* Middleware for controlling side-effects relating to Private Browsing Mode. | ||
* | ||
* @param settings Used to update disk-cache related PBM data. | ||
* @param scope Scope used for disk writes and reads. Exposed for test overrides. | ||
*/ | ||
class BrowsingModePersistenceMiddleware( | ||
private val settings: Settings, | ||
private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO), | ||
) : Middleware<AppState, AppAction> { | ||
override fun invoke( | ||
context: MiddlewareContext<AppState, AppAction>, | ||
next: (AppAction) -> Unit, | ||
action: AppAction, | ||
) { | ||
val initialMode = context.state.mode | ||
next(action) | ||
val updatedMode = context.state.mode | ||
if (initialMode != context.state.mode) { | ||
scope.launch { | ||
settings.lastKnownMode = updatedMode | ||
} | ||
} | ||
when (action) { | ||
is AppAction.Init -> { | ||
scope.launch { | ||
val mode = settings.lastKnownMode | ||
context.store.dispatch(AppAction.BrowsingModeLoaded(mode)) | ||
} | ||
} | ||
else -> Unit | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.