Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.duckduckgo.app.browser.di.BrowserModule
import com.duckduckgo.app.browser.favicon.FaviconModule
import com.duckduckgo.app.browser.rating.di.RatingModule
import com.duckduckgo.app.global.exception.UncaughtExceptionModule
import com.duckduckgo.app.global.plugins.worker.WorkerPluginsModule
import com.duckduckgo.app.httpsupgrade.di.HttpsUpgraderModule
import com.duckduckgo.app.onboarding.di.OnboardingModule
import com.duckduckgo.app.onboarding.di.WelcomePageModule
Expand All @@ -48,6 +49,7 @@ import javax.inject.Singleton
StubStatisticsModule::class,

/* real modules */
WorkerPluginsModule::class,
ApplicationModule::class,
WorkerModule::class,
AndroidBindingModule::class,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/duckduckgo/app/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.duckduckgo.app.browser.favicon.FaviconModule
import com.duckduckgo.app.browser.rating.di.RatingModule
import com.duckduckgo.app.global.DuckDuckGoApplication
import com.duckduckgo.app.global.exception.UncaughtExceptionModule
import com.duckduckgo.app.global.plugins.worker.WorkerPluginsModule
import com.duckduckgo.app.httpsupgrade.di.HttpsUpgraderModule
import com.duckduckgo.app.onboarding.di.OnboardingModule
import com.duckduckgo.app.onboarding.di.WelcomePageModule
Expand All @@ -41,6 +42,7 @@ import javax.inject.Singleton
@Singleton
@Component(
modules = [
WorkerPluginsModule::class,
ApplicationModule::class,
JobsModule::class,
WorkerModule::class,
Expand Down
69 changes: 8 additions & 61 deletions app/src/main/java/com/duckduckgo/app/di/DaggerWorkerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,14 @@
package com.duckduckgo.app.di

import android.content.Context
import androidx.core.app.NotificationManagerCompat
import androidx.work.ListenableWorker
import androidx.work.WorkerFactory
import androidx.work.WorkerParameters
import com.duckduckgo.app.fire.DataClearingWorker
import com.duckduckgo.app.global.job.AppConfigurationWorker
import com.duckduckgo.app.global.view.ClearDataAction
import com.duckduckgo.app.job.ConfigurationDownloader
import com.duckduckgo.app.notification.NotificationFactory
import com.duckduckgo.app.notification.NotificationScheduler.ClearDataNotificationWorker
import com.duckduckgo.app.notification.NotificationScheduler.PrivacyNotificationWorker
import com.duckduckgo.app.notification.db.NotificationDao
import com.duckduckgo.app.notification.model.ClearDataNotification
import com.duckduckgo.app.notification.model.PrivacyProtectionNotification
import com.duckduckgo.app.settings.db.SettingsDataStore
import com.duckduckgo.app.statistics.api.OfflinePixelScheduler
import com.duckduckgo.app.statistics.api.OfflinePixelSender
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.global.plugins.worker.WorkerInjectorPluginPoint
import timber.log.Timber

class DaggerWorkerFactory(
private val offlinePixelSender: OfflinePixelSender,
private val settingsDataStore: SettingsDataStore,
private val clearDataAction: ClearDataAction,
private val notificationManager: NotificationManagerCompat,
private val notificationDao: NotificationDao,
private val notificationFactory: NotificationFactory,
private val clearDataNotification: ClearDataNotification,
private val privacyProtectionNotification: PrivacyProtectionNotification,
private val configurationDownloader: ConfigurationDownloader,
private val pixel: Pixel
private val workerInjectorPluginPoint: WorkerInjectorPluginPoint,
) : WorkerFactory() {

override fun createWorker(appContext: Context, workerClassName: String, workerParameters: WorkerParameters): ListenableWorker? {
Expand All @@ -57,13 +34,12 @@ class DaggerWorkerFactory(
val constructor = workerClass.getDeclaredConstructor(Context::class.java, WorkerParameters::class.java)
val instance = constructor.newInstance(appContext, workerParameters)

when (instance) {
is OfflinePixelScheduler.OfflinePixelWorker -> injectOfflinePixelWorker(instance)
is DataClearingWorker -> injectDataClearWorker(instance)
is ClearDataNotificationWorker -> injectClearDataNotificationWorker(instance)
is PrivacyNotificationWorker -> injectPrivacyNotificationWorker(instance)
is AppConfigurationWorker -> injectAppConfigurationWorker(instance)
else -> Timber.i("No injection required for worker $workerClassName")
workerInjectorPluginPoint.getPlugins().forEach { plugin ->
if (plugin.inject(instance)) {
Timber.i("Injected using plugin $workerClassName")
return@forEach
}
Timber.i("No injection required for worker $workerClassName")
}

return instance
Expand All @@ -73,33 +49,4 @@ class DaggerWorkerFactory(
}

}

private fun injectAppConfigurationWorker(worker: AppConfigurationWorker) {
worker.appConfigurationDownloader = configurationDownloader
}

private fun injectOfflinePixelWorker(worker: OfflinePixelScheduler.OfflinePixelWorker) {
worker.offlinePixelSender = offlinePixelSender
}

private fun injectDataClearWorker(worker: DataClearingWorker) {
worker.settingsDataStore = settingsDataStore
worker.clearDataAction = clearDataAction
}

private fun injectClearDataNotificationWorker(worker: ClearDataNotificationWorker) {
worker.manager = notificationManager
worker.notificationDao = notificationDao
worker.factory = notificationFactory
worker.pixel = pixel
worker.notification = clearDataNotification
}

private fun injectPrivacyNotificationWorker(worker: PrivacyNotificationWorker) {
worker.manager = notificationManager
worker.notificationDao = notificationDao
worker.factory = notificationFactory
worker.pixel = pixel
worker.notification = privacyProtectionNotification
}
}
35 changes: 3 additions & 32 deletions app/src/main/java/com/duckduckgo/app/di/WorkerModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,10 @@
package com.duckduckgo.app.di

import android.content.Context
import androidx.core.app.NotificationManagerCompat
import androidx.work.Configuration
import androidx.work.WorkManager
import androidx.work.WorkerFactory
import com.duckduckgo.app.global.view.ClearDataAction
import com.duckduckgo.app.job.ConfigurationDownloader
import com.duckduckgo.app.notification.NotificationFactory
import com.duckduckgo.app.notification.db.NotificationDao
import com.duckduckgo.app.notification.model.ClearDataNotification
import com.duckduckgo.app.notification.model.PrivacyProtectionNotification
import com.duckduckgo.app.settings.db.SettingsDataStore
import com.duckduckgo.app.statistics.api.OfflinePixelSender
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.global.plugins.worker.WorkerInjectorPluginPoint
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
Expand All @@ -50,28 +41,8 @@ class WorkerModule {
@Provides
@Singleton
fun workerFactory(
offlinePixelSender: OfflinePixelSender,
settingsDataStore: SettingsDataStore,
clearDataAction: ClearDataAction,
notificationManager: NotificationManagerCompat,
notificationDao: NotificationDao,
notificationFactory: NotificationFactory,
clearDataNotification: ClearDataNotification,
privacyProtectionNotification: PrivacyProtectionNotification,
configurationDownloader: ConfigurationDownloader,
pixel: Pixel
workerInjectorPluginPoint: WorkerInjectorPluginPoint,
): WorkerFactory {
return DaggerWorkerFactory(
offlinePixelSender,
settingsDataStore,
clearDataAction,
notificationManager,
notificationDao,
notificationFactory,
clearDataNotification,
privacyProtectionNotification,
configurationDownloader,
pixel
)
return DaggerWorkerFactory(workerInjectorPluginPoint)
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/duckduckgo/app/fire/DataClearingWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package com.duckduckgo.app.fire
import android.content.Context
import androidx.annotation.WorkerThread
import androidx.work.CoroutineWorker
import androidx.work.ListenableWorker
import androidx.work.ListenableWorker.Result.success
import androidx.work.WorkerParameters
import com.duckduckgo.app.global.plugins.worker.WorkerInjectorPlugin
import com.duckduckgo.app.global.view.ClearDataAction
import com.duckduckgo.app.settings.clear.ClearWhatOption
import com.duckduckgo.app.settings.db.SettingsDataStore
Expand Down Expand Up @@ -87,3 +89,18 @@ class DataClearingWorker(context: Context, workerParams: WorkerParameters) : Cor
const val WORK_REQUEST_TAG = "background-clear-data"
}
}

class DataClearingWorkerInjectorPlugin(
private val settingsDataStore: SettingsDataStore,
private val clearDataAction: ClearDataAction
) : WorkerInjectorPlugin {

override fun inject(worker: ListenableWorker): Boolean {
if (worker is DataClearingWorker) {
worker.settingsDataStore = settingsDataStore
worker.clearDataAction = clearDataAction
return true
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.app.global.job

import android.content.Context
import androidx.work.*
import com.duckduckgo.app.global.plugins.worker.WorkerInjectorPlugin
import com.duckduckgo.app.job.ConfigurationDownloader
import io.reactivex.Single
import timber.log.Timber
Expand Down Expand Up @@ -59,3 +60,16 @@ class AppConfigurationWorker(context: Context, workerParams: WorkerParameters) :
}
}
}

class AppConfigurationWorkerInjectorPlugin(
private val configurationDownloader: ConfigurationDownloader
) : WorkerInjectorPlugin {

override fun inject(worker: ListenableWorker): Boolean {
if (worker is AppConfigurationWorker) {
worker.appConfigurationDownloader = configurationDownloader
return true
}
return false
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/duckduckgo/app/global/plugins/PluginPoint.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2018 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.global.plugins

/**
* A PluginPoint provides a list of plugins of a particular type T
*/
interface PluginPoint<T> {
/**
* @return the list of plugins of type <T>
*/
fun getPlugins(): List<T>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.global.plugins.worker

import androidx.work.ListenableWorker
import com.duckduckgo.app.global.plugins.PluginPoint
import javax.inject.Inject
import javax.inject.Singleton

interface WorkerInjectorPlugin {
/**
* @return whether the worker has been injected
*/
fun inject(worker: ListenableWorker): Boolean
}

@Singleton
class WorkerInjectorPluginPoint @Inject constructor(
private val injectorPlugins: Set<@JvmSuppressWildcards WorkerInjectorPlugin>
) : PluginPoint<WorkerInjectorPlugin> {
override fun getPlugins(): List<WorkerInjectorPlugin> {
return injectorPlugins.toList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2020 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.global.plugins.worker

import androidx.core.app.NotificationManagerCompat
import com.duckduckgo.app.fire.DataClearingWorkerInjectorPlugin
import com.duckduckgo.app.global.job.AppConfigurationWorkerInjectorPlugin
import com.duckduckgo.app.global.view.ClearDataAction
import com.duckduckgo.app.job.ConfigurationDownloader
import com.duckduckgo.app.notification.ClearDataNotificationWorkerInjectorPlugin
import com.duckduckgo.app.notification.NotificationFactory
import com.duckduckgo.app.notification.PrivacyNotificationWorkerInjectorPlugin
import com.duckduckgo.app.notification.db.NotificationDao
import com.duckduckgo.app.notification.model.ClearDataNotification
import com.duckduckgo.app.notification.model.PrivacyProtectionNotification
import com.duckduckgo.app.settings.db.SettingsDataStore
import com.duckduckgo.app.statistics.api.OfflinePixelSender
import com.duckduckgo.app.statistics.api.OfflinePixelWorkerInjectorPlugin
import com.duckduckgo.app.statistics.pixels.Pixel
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoSet

@Module
class WorkerPluginsModule {

@Provides
@IntoSet
fun dataClearingWorkerInjectorPlugin(
settingsDataStore: SettingsDataStore,
clearDataAction: ClearDataAction
): WorkerInjectorPlugin = DataClearingWorkerInjectorPlugin(settingsDataStore, clearDataAction)

@Provides
@IntoSet
fun clearDataNotificationWorkerInjectorPlugin(
notificationManagerCompat: NotificationManagerCompat,
notificationDao: NotificationDao,
notificationFactory: NotificationFactory,
pixel: Pixel,
clearDataNotification: ClearDataNotification
): WorkerInjectorPlugin = ClearDataNotificationWorkerInjectorPlugin(
notificationManagerCompat,
notificationDao,
notificationFactory,
pixel,
clearDataNotification
)

@Provides
@IntoSet
fun privacyNotificationWorkerInjectorPlugin(
notificationManagerCompat: NotificationManagerCompat,
notificationDao: NotificationDao,
notificationFactory: NotificationFactory,
pixel: Pixel,
privacyProtectionNotification: PrivacyProtectionNotification
): WorkerInjectorPlugin = PrivacyNotificationWorkerInjectorPlugin(
notificationManagerCompat,
notificationDao,
notificationFactory,
pixel,
privacyProtectionNotification
)

@Provides
@IntoSet
fun appConfigurationWorkerInjectorPlugin(
configurationDownloader: ConfigurationDownloader
): WorkerInjectorPlugin = AppConfigurationWorkerInjectorPlugin(configurationDownloader)

@Provides
@IntoSet
fun offlinePixelWorkerInjectorPlugin(
offlinePixelSender: OfflinePixelSender
): WorkerInjectorPlugin = OfflinePixelWorkerInjectorPlugin(offlinePixelSender)
}
Loading