Skip to content

Commit

Permalink
Use secure window for incognito mode
Browse files Browse the repository at this point in the history
Close #371.
  • Loading branch information
Slion committed Sep 30, 2023
1 parent d9bd571 commit f33616f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 67 deletions.
23 changes: 16 additions & 7 deletions app/src/main/java/acr/browser/lightning/IncognitoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import acr.browser.lightning.browser.activity.BrowserActivity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import android.webkit.CookieManager
import android.webkit.CookieSyncManager
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.Completable
import javax.inject.Inject
Expand All @@ -18,25 +18,34 @@ class IncognitoActivity @Inject constructor(): BrowserActivity() {

override fun provideAccentThemeOverride(): AccentTheme = AccentTheme.PINK

@Suppress("DEPRECATION")
/**
*
*/
public override fun updateCookiePreference(): Completable = Completable.fromAction {
val cookieManager = CookieManager.getInstance()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
CookieSyncManager.createInstance(this@IncognitoActivity)
}
if (Capabilities.FULL_INCOGNITO.isSupported) {
cookieManager.setAcceptCookie(userPreferences.cookiesEnabled)
} else {
cookieManager.setAcceptCookie(userPreferences.incognitoCookiesEnabled)
}
}

@Suppress("RedundantOverride")
/**
*
*/
override fun onNewIntent(intent: Intent) {
handleNewIntent(intent)
super.onNewIntent(intent)
}

/**
*
*/
override fun onCreate(savedInstanceState: Bundle?) {
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
super.onCreate(savedInstanceState)
}

@Suppress("RedundantOverride")
override fun onPause() = super.onPause() // saveOpenTabs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ abstract class BrowserActivity : ThemedBrowserActivity(), BrowserView, UIControl
protected abstract fun updateCookiePreference(): Completable

override fun onCreate(savedInstanceState: Bundle?) {
Timber.v("onCreate")
// Need to go first to inject our components
super.onCreate(savedInstanceState)

Expand Down
23 changes: 0 additions & 23 deletions app/src/main/java/acr/browser/lightning/utils/MemoryLeakUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,8 @@ public static void clearNextServedView(@NonNull Activity activity, @NonNull Appl
Log.d(TAG, "Unable to invoke method in clearNextServedView", e);
}
}

}

public static abstract class LifecycleAdapter implements Application.ActivityLifecycleCallbacks {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

@Override
public void onActivityStarted(Activity activity) {}

@Override
public void onActivityResumed(Activity activity) {}

@Override
public void onActivityPaused(Activity activity) {}

@Override
public void onActivityStopped(Activity activity) {}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {}

@Override
public void onActivityDestroyed(Activity activity) {}
}


}
102 changes: 65 additions & 37 deletions app/src/main/java/fulguris/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package fulguris

import acr.browser.lightning.BuildConfig
import acr.browser.lightning.IncognitoActivity
import acr.browser.lightning.R
import acr.browser.lightning.database.bookmark.BookmarkExporter
import acr.browser.lightning.database.bookmark.BookmarkRepository
Expand All @@ -43,7 +44,6 @@ import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import android.webkit.WebView
import androidx.appcompat.app.AppCompatDelegate
import com.jakewharton.threetenabp.AndroidThreeTen
import dagger.hilt.android.HiltAndroidApp
import io.reactivex.Scheduler
Expand All @@ -52,11 +52,13 @@ import timber.log.Timber
import javax.inject.Inject
import kotlin.system.exitProcess


@SuppressLint("StaticFieldLeak")
lateinit var app: App

@HiltAndroidApp
class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener,
Application.ActivityLifecycleCallbacks {

@Inject internal lateinit var developerPreferences: DeveloperPreferences
@Inject internal lateinit var userPreferences: UserPreferences
Expand All @@ -71,6 +73,19 @@ class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
//Ugly way to pass our domain around for settings
var domain: String = ""

/**
* Our app can runs in a different process when using the incognito activity.
* This tells us which process it is.
* However this is initialized after the activity creation when running on versions before Android 9.
*/
var incognito = false
private set(value) {
if (value) {
Timber.d("Incognito app process")
}
field = value
}

override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
// We only need to install that multi DEX library when not doing minify code optimization, typically in debug, and on devices below API level 21.
Expand Down Expand Up @@ -108,15 +123,58 @@ class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
}


override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Timber.v("onActivityCreated")
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
if (activity is IncognitoActivity) {
// Needed as the process check we use below does not work before Android 9
incognito = true
}
}
}

override fun onActivityStarted(activity: Activity) {
Timber.v("onActivityStarted")
}

override fun onActivityResumed(activity: Activity) {
Timber.v("onActivityResumed")
resumedActivity = activity
}

override fun onActivityPaused(activity: Activity) {
Timber.v("onActivityPaused")
resumedActivity = null
}

override fun onActivityStopped(activity: Activity) {
Timber.v("onActivityStopped")
}

override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
Timber.v("onActivitySaveInstanceState")
}

override fun onActivityDestroyed(activity: Activity) {
Timber.v("onActivityDestroyed")
MemoryLeakUtils.clearNextServedView(activity, this@App)
}


/**
*
*/
override fun onCreate() {
app = this
registerActivityLifecycleCallbacks(this)
// SL: Use this to debug when launched from another app for instance
//Debug.waitForDebugger()
super.onCreate()
// No need to unregister I suppose cause this is for the life time of the application anyway
userPreferences.preferences.registerOnSharedPreferenceChangeListener(this)

plantTimberLogs()
Timber.v("onCreate")

AndroidThreeTen.init(this);

Expand All @@ -133,8 +191,9 @@ class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
*/
}

if (Build.VERSION.SDK_INT >= 28) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (getProcessName() == "$packageName:incognito") {
incognito = true
WebView.setDataDirectorySuffix("incognito")
}
}
Expand Down Expand Up @@ -177,28 +236,6 @@ class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
WebView.setWebContentsDebuggingEnabled(true)
}

registerActivityLifecycleCallbacks(object : MemoryLeakUtils.LifecycleAdapter() {
override fun onActivityDestroyed(activity: Activity) {
Timber.d("onActivityDestroyed")
MemoryLeakUtils.clearNextServedView(activity, this@App)
}

override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Timber.d("onActivityCreated")
}

// Track current activity
override fun onActivityResumed(activity: Activity) {
Timber.d("onActivityResumed")
resumedActivity = activity
}

// Track current activity
override fun onActivityPaused(activity: Activity) {
Timber.d("onActivityPaused")
resumedActivity = null
}
})
}


Expand All @@ -208,20 +245,14 @@ class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
// Apparently we take care of not leaking it above
@SuppressLint("StaticFieldLeak")
var resumedActivity: Activity? = null
private set

/**
* Used to get current activity context in order to access current theme.
*/
fun currentContext() : Context {
val act = resumedActivity
if (act!=null)
{
return act
}
else
{
return app
}
return resumedActivity
?: app
}

/**
Expand All @@ -232,9 +263,6 @@ class App : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
LocaleUtils.updateLocale(app, requestLocale)
}

init {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)
}
}

/**
Expand Down

0 comments on commit f33616f

Please sign in to comment.