π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile] Defer background pre-emption and trim native diff
laurenspriem17f808302eb2
diff --git a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteApplication.kt b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteApplication.ktindex 9ef87aa840..363a4d90de 100644--- a/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteApplication.kt+++ b/mobile/apps/photos/android/app/src/main/kotlin/io/ente/photos/EnteApplication.kt@@ -12,8 +12,8 @@ import dev.fluttercommunity.workmanager.TaskResultimport dev.fluttercommunity.workmanager.WorkmanagerDebugimport dev.fluttercommunity.workmanager.pigeon.TaskStatusimport io.ente.background.BackgroundManagerPlugin-import kotlin.random.Randomimport org.json.JSONObject+import kotlin.random.Randomclass EnteApplication : Application() {override fun onCreate() {@@ -34,7 +34,8 @@ class EnteApplication : Application() {const val FLUTTER_SHARED_PREFERENCES = "FlutterSharedPreferences"const val REMOTE_FLAGS_KEY = "flutter.remote_flags"const val INTERNAL_USER_DISABLED_KEY = "flutter.ls.internal_user_disabled"- const val BG_DEBUG_NOTIFICATIONS_ENABLED_KEY = "flutter.ls.bg_debug_notifications_enabled"+ const val BG_DEBUG_NOTIFICATIONS_ENABLED_KEY =+ "flutter.ls.bg_debug_notifications_enabled"const val TAG = "EnteApplication"}}@@ -98,15 +99,14 @@ private class EnteWorkmanagerDebugHandler : WorkmanagerDebug() {return runCatching {JSONObject(remoteFlags).optBoolean("internalUser", false)+ }.getOrElse {+ Log.w(+ EnteApplication.TAG,+ "Failed to parse remote flags for Workmanager debug handler",+ it,+ )+ false}- .getOrElse {- Log.w(- EnteApplication.TAG,- "Failed to parse remote flags for Workmanager debug handler",- it,- )- false- }}private fun formatNotification(@@ -161,16 +161,15 @@ private class EnteWorkmanagerDebugHandler : WorkmanagerDebug() {private fun ensureNotificationChannel(notificationManager: NotificationManager) {val channel =NotificationChannel(- CHANNEL_ID,- CHANNEL_NAME,- NotificationManager.IMPORTANCE_MIN,- )- .apply {- setSound(null, null)- enableVibration(false)- vibrationPattern = longArrayOf(0L)- setShowBadge(false)- }+ CHANNEL_ID,+ CHANNEL_NAME,+ NotificationManager.IMPORTANCE_MIN,+ ).apply {+ setSound(null, null)+ enableVibration(false)+ vibrationPattern = longArrayOf(0L)+ setShowBadge(false)+ }notificationManager.createNotificationChannel(channel)}diff --git a/mobile/apps/photos/ios/Runner/AppDelegate.swift b/mobile/apps/photos/ios/Runner/AppDelegate.swiftindex 1a184813d8..77d5e4fc60 100644--- a/mobile/apps/photos/ios/Runner/AppDelegate.swift+++ b/mobile/apps/photos/ios/Runner/AppDelegate.swift@@ -91,15 +91,14 @@ import workmanager_appleif defaults.bool(forKey: "flutter.ls.internal_user_disabled") {return false}- if !defaults.bool(forKey: "flutter.ls.bg_debug_notifications_enabled")- && defaults.object(forKey: "flutter.ls.bg_debug_notifications_enabled") != nil- {+ if !defaults.bool(forKey: "flutter.ls.bg_debug_notifications_enabled") &&+ defaults.object(forKey: "flutter.ls.bg_debug_notifications_enabled") != nil {return false}guard let remoteFlags = defaults.string(forKey: "flutter.remote_flags"),- let data = remoteFlags.data(using: .utf8),- let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any]+ let data = remoteFlags.data(using: .utf8),+ let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any]else {return false}diff --git a/mobile/apps/photos/lib/main.dart b/mobile/apps/photos/lib/main.dartindex 2acd7b5e5b..c891675b12 100644--- a/mobile/apps/photos/lib/main.dart+++ b/mobile/apps/photos/lib/main.dart@@ -246,7 +246,6 @@ Future<void> runBackgroundTask(Duration? mlSelfStop,Duration? mlLockWait,MlRunControl? control,- bool Function()? shouldYield,}) async {// Created at task start so a stop that fires before ML begins stays// latched for the whole task.@@ -286,36 +285,19 @@ Future<void> runBackgroundTask("[BG TASK] No recent foreground activity, proceeding with background work",);- await _runMinimally(- taskId,- tlog,- mlRunControl,- mlLockWait,- shouldYield ?? () => false,- );+ await _runMinimally(taskId, tlog, mlRunControl, mlLockWait);} finally {mlSelfStopTimer?.cancel();mlForegroundWatchTimer.cancel();}}-void stopBackgroundSync() {- if (_isSyncInitialized) SyncService.instance.stopSync();-}-Future<void> _runMinimally(String taskId,TimeLogger tlog,MlRunControl mlRunControl,Duration? mlLockWait,- bool Function() shouldYield,) async {- bool isYieldingBefore(String stage) {- if (!shouldYield()) return false;- _logger.info("[BG TASK] $taskId yielding before $stage");- return true;- }-try {final PackageInfo packageInfo = await PackageInfo.fromPlatform();final SharedPreferences prefs = await SharedPreferences.getInstance();@@ -372,11 +354,9 @@ Future<void> _runMinimally(_logger.info("[BG TASK] update notification");updateService.showUpdateNotification().ignore();- if (isYieldingBefore("sync")) return;_logger.info("[BG TASK] sync starting");await _sync('bgTaskActiveProcess');_logger.info("[BG TASK] sync completed");- if (isYieldingBefore("post-sync work")) return;_logger.info("[BG TASK] locale fetch");final locale = await getLocale();@@ -423,7 +403,6 @@ Future<void> _runMinimally(}}}- if (isYieldingBefore("smart albums sync")) return;_logger.info("[BG TASK] smart albums sync");await smartAlbumsService.syncSmartAlbums();diff --git a/mobile/apps/photos/lib/utils/background_tasks.dart b/mobile/apps/photos/lib/utils/background_tasks.dartindex 40901d27cd..5055ad99c0 100644--- a/mobile/apps/photos/lib/utils/background_tasks.dart+++ b/mobile/apps/photos/lib/utils/background_tasks.dart@@ -29,7 +29,6 @@ class BackgroundTasks {static final _logger = Logger("BackgroundTasks");static const refresh = "io.ente.photos.nativeBackgroundRefresh";static const processing = "io.ente.photos.nativeBackgroundProcessing";- static const _pipelineHandoffWait = Duration(seconds: 5);static final _cooperativeStopReasons = {for (final reason in BackgroundStopReason.values)if (reason != BackgroundStopReason.system) reason.name,@@ -217,24 +216,6 @@ class BackgroundTasks {return _isRemoteInternalUser(prefs);}- static Future<bool> _acquirePipeline(BackgroundTask task) async {- final wait = task.identifier == processing- ? _pipelineHandoffWait- : Duration.zero;- final waited = Stopwatch()..start();- while (true) {- if (await ProcessLockClient.instance.tryAcquire(- name: "background_process",- origin: "bg",- operation: task.identifier,- )) {- return true;- }- if (task.isStopping || waited.elapsed >= wait) return false;- await Future<void>.delayed(const Duration(milliseconds: 500));- }- }-static Future<void> retireLegacySchedules() async {BgTaskUtils.resetProcessingSchedule();final identifiers = Platform.isIOS@@ -267,7 +248,12 @@ class BackgroundTasks {}await _debugNotify(prefs, "Started", [task.identifier]);task.throwIfStopping();- if (!await _acquirePipeline(task)) {+ final acquired = await ProcessLockClient.instance.tryAcquire(+ name: "background_process",+ origin: "bg",+ operation: task.identifier,+ );+ if (!acquired) {_logger.info("${task.identifier}: skipped, background pipeline busy",);@@ -291,9 +277,6 @@ class BackgroundTasks {? MlStopReason.foregroundActive: MlStopReason.backgroundDeadline,);- if (reason == BackgroundStopReason.preempted) {- stopBackgroundSync();- }}),);try {@@ -303,8 +286,6 @@ class BackgroundTasks {taskName,TimeLogger(),control: control,- shouldYield: () =>- task.stopReason == BackgroundStopReason.preempted,mlSelfStop: BgTaskUtils.mlSelfStopFor(taskName) - task.elapsed,mlLockWait: BgTaskUtils.mlLockWaitFor(taskName),).timeout(diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.ktindex b6b6b1e881..6522e1e450 100644--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/main/kotlin/io/ente/background/BackgroundRuntime.kt@@ -35,7 +35,6 @@ internal object BackgroundRuntime {private const val TASK_DATA = "task"private const val WORK_TAG = "io.ente.background"private const val IDENTIFIER_TAG_PREFIX = "io.ente.background.task:"- private const val PREEMPTION_TIMEOUT_MS = 30_000Lprivate val main = Handler(Looper.getMainLooper())private val scheduler = Executors.newSingleThreadExecutor()private val observers = LinkedHashSet<BackgroundManagerPlugin>()@@ -45,7 +44,6 @@ internal object BackgroundRuntime {private var isAllowed: () -> Boolean = { false }private var isForeground = falseprivate var active: Run? = null- private var pending: Pending? = nullvar createEngine: (Context) -> FlutterEngine = { FlutterEngine(it) }var findCallback: (Long) -> FlutterCallbackInformation? =FlutterCallbackInformation::lookupCallbackInformation@@ -68,15 +66,6 @@ internal object BackgroundRuntime {val stopResults = ArrayList<MethodChannel.Result>()}- private class Pending(- val worker: BackgroundWorker,- val configuration: TaskConfiguration,- val startedAt: Long,- val completion: (Boolean) -> Unit,- ) {- var timer: Runnable? = null- }-fun install(app: Application, allowed: () -> Boolean) {isAllowed = allowedif (application != null) return@@ -132,12 +121,10 @@ internal object BackgroundRuntime {fun foreground() {isForeground = true- skipPending("foreground")active?.let { stop(it, "foreground") }}fun requestStop(result: MethodChannel.Result? = null) {- skipPending("requested")val run = activeif (run == null) {result?.success(null)@@ -337,12 +324,10 @@ internal object BackgroundRuntime {completion(false)return@onMain}- val occupant = active- val preempts = occupant != null && canPreempt(occupant, configuration)val skip =when {!enabled -> "disabled"- occupant != null && !preempts -> "busy"+ active != null -> "busy"isForeground -> "foreground"else -> null}@@ -354,10 +339,6 @@ internal object BackgroundRuntime {completion(true)return@onMain}- if (occupant != null) {- preempt(occupant, Pending(worker, configuration, startedAt, completion))- return@onMain- }val run = Run(worker, configuration, startedAt, completion)active = runconfiguration.runBudgetMs?.let { budget ->@@ -477,48 +458,8 @@ internal object BackgroundRuntime {if (Looper.myLooper() == main.looper) action() else main.post { action() }}- private fun canPreempt(occupant: Run, configuration: TaskConfiguration): Boolean =- pending == null &&- !isForeground &&- occupant.teardownFailure == null &&- occupant.configuration.kind == "refresh" &&- configuration.kind == "processing"-- private fun preempt(occupant: Run, waiting: Pending) {- pending = waiting- waiting.timer =- Runnable { if (pending === waiting) skipPending("busy") }- .also { main.postDelayed(it, PREEMPTION_TIMEOUT_MS) }- stop(occupant, "preempted")- }-- private fun takePending(): Pending? {- val waiting = pending ?: return null- pending = null- waiting.timer?.let(main::removeCallbacks)- return waiting- }-- private fun skipPending(reason: String) {- val waiting = takePending() ?: return- report(waiting.configuration.identifier, "skipped", reason)- waiting.completion(true)- }-- private fun resumePending() {- val waiting = takePending() ?: return- start(waiting.worker, waiting.startedAt, waiting.completion)- }-fun nativeStop(worker: BackgroundWorker) {main.post {- pending- ?.takeIf { it.worker === worker }- ?.let {- takePending()- report(it.configuration.identifier, "stopped", "system")- it.completion(true)- }active?.takeIf { it.worker === worker }?.let {@@ -586,14 +527,12 @@ internal object BackgroundRuntime {run.stopResults.forEach { it.error("teardown", failure.message, null) }run.stopResults.clear()run.completion(false)- resumePending()return}if (terminal != "completed") report(run.configuration.identifier, terminal, reason, error)run.completion(terminal != "failed" && terminal != "forcedTeardown")run.stopResults.forEach { it.success(null) }run.stopResults.clear()- resumePending()}private fun retireUnselectedSchedule(identifier: String) {diff --git a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.ktindex 008e9aece1..6cac31a78c 100644--- a/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt+++ b/mobile/apps/photos/plugins/ente_background_manager/android/src/test/kotlin/io/ente/background/BackgroundRuntimeTest.kt@@ -69,26 +69,19 @@ class BackgroundRuntimeTest {awaitReply(beginConfigure(policy, enabled))}- private fun configure(policies: List<TaskConfiguration>) {- awaitReply(beginConfigure(policies, enabled = true))- }-- private fun beginConfigure(policy: TaskConfiguration, enabled: Boolean = true): Reply =- beginConfigure(listOf(policy), enabled)-- private fun beginConfigure(policies: List<TaskConfiguration>, enabled: Boolean): Reply {+ private fun beginConfigure(policy: TaskConfiguration, enabled: Boolean = true): Reply {+ val data = org.json.JSONObject(policy.encode())val result = Reply()BackgroundRuntime.configure(mapOf("enabled" to enabled,"callbackHandle" to 1L,"tasks" to- policies.map { policy ->- val data = org.json.JSONObject(policy.encode())+ listOf(data.keys().asSequence().associateWith { key ->data.get(key).takeUnless { it === org.json.JSONObject.NULL }}- },+ ),),result,)@@ -417,66 +410,6 @@ class BackgroundRuntimeTest {reconfigureWhileStarting(cooperative, enabled = true)reconfigureWhileStarting(cooperative, enabled = false)-- val processing = cooperative.copy(identifier = "test.processing", kind = "processing")- configure(listOf(cooperative, processing))- val yielding = worker(cooperative).startWork()- val yieldingChannel = bootstrap()- val yieldingReady = yieldingChannel.call("ready") as Map<*, *>- val enginesBeforePreemption = engines.size- val waiting = worker(processing).startWork()- assertFalse(waiting.isDone)- assertTrue(startups.isEmpty())- assertEquals(enginesBeforePreemption, engines.size)- assertEquals("preempted", (yieldingChannel.sent.single().arguments as Map<*, *>)["reason"])- assertEquals(ListenableWorker.Result.success(), worker(processing).startWork().get())- assertFalse(waiting.isDone)- complete(yieldingChannel, yieldingReady)- assertEquals(ListenableWorker.Result.success(), yielding.get())- val resumedChannel = bootstrap()- val resumedReady = resumedChannel.call("ready") as Map<*, *>- assertEquals(processing.identifier, resumedReady["identifier"])- assertNull(resumedReady["stopReason"])- assertEquals(ListenableWorker.Result.success(), worker(cooperative).startWork().get())- assertTrue(resumedChannel.sent.isEmpty())- complete(resumedChannel, resumedReady)- assertEquals(ListenableWorker.Result.success(), waiting.get())-- val unyielding = worker(cooperative).startWork()- val unyieldingChannel = bootstrap()- val unyieldingReady = unyieldingChannel.call("ready") as Map<*, *>- val timedOut = worker(processing).startWork()- main.idleFor(Duration.ofSeconds(29))- assertFalse(timedOut.isDone)- main.idleFor(Duration.ofSeconds(2))- assertEquals(ListenableWorker.Result.success(), timedOut.get())- complete(unyieldingChannel, unyieldingReady)- assertTrue(unyielding.isDone)- assertTrue(startups.isEmpty())-- val interruptedRefresh = worker(cooperative).startWork()- val interruptedChannel = bootstrap()- val interruptedReady = interruptedChannel.call("ready") as Map<*, *>- val abandoned = worker(processing).startWork()- assertFalse(abandoned.isDone)- val arriving = Robolectric.buildActivity(Activity::class.java).create().start().resume()- assertEquals(ListenableWorker.Result.success(), abandoned.get())- complete(interruptedChannel, interruptedReady)- assertTrue(interruptedRefresh.isDone)- assertTrue(startups.isEmpty())- arriving.pause().stop().destroy()-- val stoppedRefresh = worker(cooperative).startWork()- val stoppedRefreshChannel = bootstrap()- val stoppedRefreshReady = stoppedRefreshChannel.call("ready") as Map<*, *>- val stoppedWhileWaitingWorker = worker(processing)- val stoppedWhileWaiting = stoppedWhileWaitingWorker.startWork()- stoppedWhileWaitingWorker.onStopped()- main.idle()- complete(stoppedRefreshChannel, stoppedRefreshReady)- assertTrue(stoppedRefresh.isDone)- assertTrue(startups.isEmpty())- assertEquals(ListenableWorker.Result.success(), stoppedWhileWaiting.get())configure(cooperative)val teardownWorker = worker(cooperative)diff --git a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swiftindex f11fd711b2..d17fdff380 100644--- a/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift+++ b/mobile/apps/photos/plugins/ente_background_manager/ios/Classes/BackgroundRuntime.swift@@ -8,14 +8,12 @@ import osfinal class BackgroundRuntime: NSObject {static let shared = BackgroundRuntime()private static let logger = Logger(subsystem: "io.ente.background", category: "BackgroundManager")- private static let preemptionTimeout: TimeInterval = 30private var configuration = StoredConfiguration.load()private var isAllowed: () -> Bool = { false }private var registrant: ((FlutterPluginRegistry) -> Void)?private let observers = NSHashTable<BackgroundManagerPlugin>.weakObjects()private var registrations: [String: Bool] = [:]private var active: Run?- private var pending: Pending?private var isForeground = falseprivate var installed = falseprivate var reconciling = false@@ -43,19 +41,6 @@ final class BackgroundRuntime: NSObject {}}- private final class Pending {- let task: BGTask- let configuration: TaskConfiguration- let startedAt: TimeInterval- var timer: DispatchWorkItem?-- init(task: BGTask, configuration: TaskConfiguration, startedAt: TimeInterval) {- self.task = task- self.configuration = configuration- self.startedAt = startedAt- }- }-func install(isEnabled: @escaping () -> Bool,registrant: @escaping (FlutterPluginRegistry) -> Void@@ -105,7 +90,6 @@ final class BackgroundRuntime: NSObject {@objc private func foreground() {isForeground = true- finishPending(outcome: "skipped", reason: "foreground", success: true)if let run = active { stop(run, reason: "foreground") }}@@ -114,7 +98,6 @@ final class BackgroundRuntime: NSObject {}func requestStop(result: FlutterResult? = nil) {- finishPending(outcome: "skipped", reason: "requested", success: true)guard let run = active else {result?(nil)return@@ -277,12 +260,10 @@ final class BackgroundRuntime: NSObject {identifier: task.identifier, outcome: "failed", reason: "schedule",error: String(describing: error))}- let occupant = active- let preempts = occupant.map { canPreempt($0, with: policy) } ?? falselet skip: String?if !isAllowed() {skip = "disabled"- } else if occupant != nil, !preempts {+ } else if active != nil {skip = "busy"} else if isForeground || UIApplication.shared.applicationState != .background {skip = "foreground"@@ -294,66 +275,15 @@ final class BackgroundRuntime: NSObject {task.setTaskCompleted(success: true)return}- task.expirationHandler = { [weak task] in+ let run = Run(task: task, configuration: policy, startedAt: startedAt)+ active = run+ task.expirationHandler = { [weak run] inDispatchQueue.main.async {- guard let task else { return }- self.expire(task)+ guard let run, self.active === run else { return }+ self.stop(run, reason: "system")+ self.retire(run, outcome: "stopped", reason: "expired", success: false)}}- if let occupant {- preempt(occupant, for: Pending(task: task, configuration: policy, startedAt: startedAt))- return- }- admit(task, policy: policy, startedAt: startedAt)- }-- private func expire(_ task: BGTask) {- if pending?.task === task {- finishPending(outcome: "stopped", reason: "expired", success: false)- } else if let run = active, run.task === task {- stop(run, reason: "system")- retire(run, outcome: "stopped", reason: "expired", success: false)- }- }-- private func canPreempt(_ occupant: Run, with policy: TaskConfiguration) -> Bool {- pending == nil && !isForeground && UIApplication.shared.applicationState == .background- && occupant.configuration.kind == "refresh" && policy.kind == "processing"- }-- private func preempt(_ occupant: Run, for waiting: Pending) {- pending = waiting- let timer = DispatchWorkItem { [weak waiting] in- guard let waiting, self.pending === waiting else { return }- self.finishPending(outcome: "skipped", reason: "busy", success: true)- }- waiting.timer = timer- DispatchQueue.main.asyncAfter(deadline: .now() + Self.preemptionTimeout, execute: timer)- stop(occupant, reason: "preempted")- }-- private func takePending() -> Pending? {- guard let waiting = pending else { return nil }- pending = nil- waiting.timer?.cancel()- return waiting- }-- private func finishPending(outcome: String, reason: String, success: Bool) {- guard let waiting = takePending() else { return }- report(identifier: waiting.configuration.identifier, outcome: outcome, reason: reason)- waiting.task.expirationHandler = nil- waiting.task.setTaskCompleted(success: success)- }-- private func resumePending() {- guard let waiting = takePending() else { return }- admit(waiting.task, policy: waiting.configuration, startedAt: waiting.startedAt)- }-- private func admit(_ task: BGTask, policy: TaskConfiguration, startedAt: TimeInterval) {- let run = Run(task: task, configuration: policy, startedAt: startedAt)- active = runif let budget = policy.runBudgetMs {let timer = DispatchWorkItem { [weak run] inguard let run, self.active === run else { return }@@ -470,7 +400,6 @@ final class BackgroundRuntime: NSObject {}for result in run.stopResults { result(nil) }run.stopResults.removeAll()- resumePending()}private func report(diff --git a/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart b/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dartindex b7c247854a..73d4fc5b70 100644--- a/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart+++ b/mobile/apps/photos/plugins/ente_background_manager/lib/ente_background_manager.dart@@ -6,7 +6,7 @@ import "package:flutter/widgets.dart";enum BackgroundTaskKind { refresh, processing }-enum BackgroundStopReason { foreground, budget, requested, preempted, system }+enum BackgroundStopReason { foreground, budget, requested, system }enum BackgroundTaskResult { completed, skipped, stopped, failed }