2019-02-15 17:31:03 +00:00
|
|
|
/* 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
|
|
|
|
|
2019-05-02 15:59:13 +00:00
|
|
|
import org.mozilla.fenix.utils.Settings
|
2019-02-15 17:31:03 +00:00
|
|
|
|
|
|
|
interface BrowsingModeManager {
|
|
|
|
enum class Mode {
|
2019-05-30 23:13:06 +00:00
|
|
|
Normal, Private;
|
|
|
|
|
|
|
|
fun isPrivate(): Boolean = this == Private
|
2019-02-15 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val isPrivate: Boolean
|
2019-05-30 22:49:58 +00:00
|
|
|
var mode: Mode
|
|
|
|
}
|
|
|
|
|
|
|
|
interface BrowserModeStorage {
|
|
|
|
fun setMode(mode: BrowsingModeManager.Mode)
|
|
|
|
fun currentMode(): BrowsingModeManager.Mode
|
|
|
|
}
|
2019-02-15 17:31:03 +00:00
|
|
|
|
2019-05-30 22:49:58 +00:00
|
|
|
fun Settings.createBrowserModeStorage(): BrowserModeStorage = object : BrowserModeStorage {
|
|
|
|
override fun currentMode(): BrowsingModeManager.Mode {
|
|
|
|
return if (this@createBrowserModeStorage.usePrivateMode) {
|
|
|
|
BrowsingModeManager.Mode.Private
|
|
|
|
} else {
|
|
|
|
BrowsingModeManager.Mode.Normal
|
2019-02-15 17:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 22:49:58 +00:00
|
|
|
override fun setMode(mode: BrowsingModeManager.Mode) {
|
|
|
|
this@createBrowserModeStorage.setPrivateMode(mode == BrowsingModeManager.Mode.Private)
|
2019-02-15 17:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 22:49:58 +00:00
|
|
|
|
2019-05-30 23:13:06 +00:00
|
|
|
class DefaultBrowsingModeManager(
|
|
|
|
private val storage: BrowserModeStorage,
|
|
|
|
private val modeDidChange: (BrowsingModeManager.Mode) -> Unit
|
|
|
|
) : BrowsingModeManager {
|
2019-05-30 22:49:58 +00:00
|
|
|
override val isPrivate: Boolean
|
|
|
|
get() = mode == BrowsingModeManager.Mode.Private
|
|
|
|
|
|
|
|
override var mode: BrowsingModeManager.Mode
|
|
|
|
get() = storage.currentMode()
|
2019-05-30 23:13:06 +00:00
|
|
|
set(value) {
|
|
|
|
storage.setMode(value)
|
|
|
|
modeDidChange(mode)
|
|
|
|
}
|
2019-05-30 22:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class CustomTabBrowsingModeManager : BrowsingModeManager {
|
|
|
|
override val isPrivate = false
|
|
|
|
override var mode: BrowsingModeManager.Mode
|
|
|
|
get() = BrowsingModeManager.Mode.Normal
|
|
|
|
set(_) { return }
|
2019-05-30 23:13:06 +00:00
|
|
|
}
|