HeadlessModule.kt 4.2 KB
Newer Older
1
2
package org.briarproject.briar.headless

3
import com.fasterxml.jackson.databind.ObjectMapper
4
5
import dagger.Module
import dagger.Provides
6
import org.briarproject.bramble.api.FeatureFlags
7
import org.briarproject.bramble.api.battery.BatteryManager
8
9
10
11
12
13
14
15
16
17
18
import org.briarproject.bramble.api.db.DatabaseConfig
import org.briarproject.bramble.api.event.EventBus
import org.briarproject.bramble.api.lifecycle.IoExecutor
import org.briarproject.bramble.api.network.NetworkManager
import org.briarproject.bramble.api.plugin.BackoffFactory
import org.briarproject.bramble.api.plugin.PluginConfig
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory
import org.briarproject.bramble.api.system.Clock
import org.briarproject.bramble.api.system.LocationUtils
import org.briarproject.bramble.api.system.ResourceProvider
19
import org.briarproject.bramble.battery.DefaultBatteryManagerModule
akwizgran's avatar
akwizgran committed
20
import org.briarproject.bramble.event.DefaultEventExecutorModule
21
22
23
import org.briarproject.bramble.network.JavaNetworkModule
import org.briarproject.bramble.plugin.tor.CircumventionModule
import org.briarproject.bramble.plugin.tor.CircumventionProvider
24
import org.briarproject.bramble.plugin.tor.UnixTorPluginFactory
25
import org.briarproject.bramble.socks.SocksModule
26
import org.briarproject.bramble.system.JavaSystemModule
akwizgran's avatar
akwizgran committed
27
import org.briarproject.bramble.util.OsUtils.isLinux
28
import org.briarproject.bramble.util.OsUtils.isMac
29
30
31
32
33
import org.briarproject.briar.headless.blogs.HeadlessBlogModule
import org.briarproject.briar.headless.contact.HeadlessContactModule
import org.briarproject.briar.headless.event.HeadlessEventModule
import org.briarproject.briar.headless.forums.HeadlessForumModule
import org.briarproject.briar.headless.messaging.HeadlessMessagingModule
34
35
36
37
38
39
40
41
42
43
44
import java.io.File
import java.util.Collections.emptyList
import java.util.concurrent.Executor
import javax.inject.Singleton
import javax.net.SocketFactory

@Module(
    includes = [
        JavaNetworkModule::class,
        JavaSystemModule::class,
        CircumventionModule::class,
45
        DefaultBatteryManagerModule::class,
akwizgran's avatar
akwizgran committed
46
        DefaultEventExecutorModule::class,
47
        SocksModule::class,
48
49
50
51
52
        HeadlessBlogModule::class,
        HeadlessContactModule::class,
        HeadlessEventModule::class,
        HeadlessForumModule::class,
        HeadlessMessagingModule::class
53
54
    ]
)
55
internal class HeadlessModule(private val appDir: File) {
56

57
58
59
60
    @Provides
    @Singleton
    internal fun provideBriarService(briarService: BriarServiceImpl): BriarService = briarService

61
62
63
64
65
66
67
68
69
70
71
72
    @Provides
    @Singleton
    internal fun provideDatabaseConfig(): DatabaseConfig {
        val dbDir = File(appDir, "db")
        val keyDir = File(appDir, "key")
        return HeadlessDatabaseConfig(dbDir, keyDir)
    }

    @Provides
    internal fun providePluginConfig(
        @IoExecutor ioExecutor: Executor, torSocketFactory: SocketFactory,
        backoffFactory: BackoffFactory, networkManager: NetworkManager,
73
74
        locationUtils: LocationUtils, eventBus: EventBus, resourceProvider: ResourceProvider,
        circumventionProvider: CircumventionProvider, batteryManager: BatteryManager, clock: Clock
75
    ): PluginConfig {
76
77
        val duplex: List<DuplexPluginFactory> = if (isLinux() || isMac()) {
            val torDirectory = File(appDir, "tor")
78
79
80
81
            val tor = UnixTorPluginFactory(
                ioExecutor, networkManager, locationUtils, eventBus, torSocketFactory,
                backoffFactory, resourceProvider, circumventionProvider, batteryManager, clock,
                torDirectory
82
            )
83
            listOf(tor)
84
        } else {
85
            emptyList()
86
        }
87
        return object : PluginConfig {
88
89
90
            override fun getDuplexFactories(): Collection<DuplexPluginFactory> = duplex
            override fun getSimplexFactories(): Collection<SimplexPluginFactory> = emptyList()
            override fun shouldPoll(): Boolean = true
91
92
93
        }
    }

94
95
96
97
    @Provides
    @Singleton
    internal fun provideObjectMapper() = ObjectMapper()

98
99
100
    @Provides
    internal fun provideFeatureFlags() = object : FeatureFlags {
        override fun shouldEnableImageAttachments() = false
101
        override fun shouldEnablePrivateMessageDeletion() = true
102
    }
103
}