Components.kt 5.74 KB
Newer Older
Jeff Boek's avatar
Jeff Boek committed
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
3
 * 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/. */
Jeff Boek's avatar
Jeff Boek committed
4

5
6
package org.mozilla.fenix.components

7
import android.app.Application
8
import android.content.Context
9
10
import android.content.Intent
import androidx.core.net.toUri
11
import com.google.android.play.core.review.ReviewManagerFactory
Gabriel Luong's avatar
Gabriel Luong committed
12
import mozilla.components.feature.addons.AddonManager
13
14
import mozilla.components.feature.addons.migration.DefaultSupportedAddonsChecker
import mozilla.components.feature.addons.migration.SupportedAddonsChecker
Gabriel Luong's avatar
Gabriel Luong committed
15
16
import mozilla.components.feature.addons.update.AddonUpdater
import mozilla.components.feature.addons.update.DefaultAddonUpdater
17
import mozilla.components.feature.autofill.AutofillConfiguration
18
import mozilla.components.feature.sitepermissions.SitePermissionsStorage
19
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
20
import mozilla.components.support.migration.state.MigrationStore
21
import org.mozilla.fenix.BuildConfig
22
import org.mozilla.fenix.Config
23
import org.mozilla.fenix.HomeActivity
24
25
import org.mozilla.fenix.R
import org.mozilla.fenix.autofill.AutofillUnlockActivity
26
import org.mozilla.fenix.perf.StrictModeManager
27
import org.mozilla.fenix.components.metrics.AppStartupTelemetry
28
import org.mozilla.fenix.ext.components
29
import org.mozilla.fenix.ext.settings
30
import org.mozilla.fenix.perf.lazyMonitored
Matthew Finkel's avatar
Matthew Finkel committed
31
import org.mozilla.fenix.tor.TorController
32
import org.mozilla.fenix.utils.ClipboardHandler
33
import org.mozilla.fenix.utils.Mockable
34
import org.mozilla.fenix.utils.Settings
35
import org.mozilla.fenix.wifi.WifiConnectionMonitor
Gabriel Luong's avatar
Gabriel Luong committed
36
37
import java.util.concurrent.TimeUnit

38
private const val AMO_COLLECTION_MAX_CACHE_AGE = 2 * 24 * 60L // Two days in minutes
39
40

/**
41
42
43
44
45
 * Provides access to all components. This class is an implementation of the Service Locator
 * pattern, which helps us manage the dependencies in our app.
 *
 * Note: these aren't just "components" from "android-components": they're any "component" that
 * can be considered a building block of our app.
46
 */
47
@Mockable
48
class Components(private val context: Context) {
49
    val backgroundServices by lazyMonitored {
50
51
        BackgroundServices(
            context,
52
            push,
53
            analytics.crashReporter,
54
55
            core.lazyHistoryStorage,
            core.lazyBookmarksStorage,
56
            core.lazyPasswordsStorage,
57
58
            core.lazyRemoteTabsStorage,
            strictMode
59
        )
60
    }
61
62
    val services by lazyMonitored { Services(context, backgroundServices.accountManager) }
    val core by lazyMonitored { Core(context, analytics.crashReporter, strictMode) }
63
    @Suppress("Deprecation")
64
    val useCases by lazyMonitored {
65
66
        UseCases(
            context,
67
            core.engine,
68
69
            core.sessionManager,
            core.store,
70
            core.webAppShortcutManager,
71
            core.topSitesStorage
72
        )
73
    }
74

75
    val intentProcessors by lazyMonitored {
76
77
        IntentProcessors(
            context,
78
            core.store,
79
            useCases.sessionUseCases,
80
            useCases.tabsUseCases,
81
            useCases.customTabsUseCases,
82
            useCases.searchUseCases,
Tiger Oakes's avatar
Tiger Oakes committed
83
            core.relationChecker,
84
            core.customTabsStore,
85
86
            migrationStore,
            core.webAppManifestStorage
87
        )
88
    }
Gabriel Luong's avatar
Gabriel Luong committed
89

90
    val addonCollectionProvider by lazyMonitored {
Alex Catarineu's avatar
Alex Catarineu committed
91
        TorAddonCollectionProvider(context, core.client)
Gabriel Luong's avatar
Gabriel Luong committed
92
93
    }

94
    val appStartupTelemetry by lazyMonitored { AppStartupTelemetry(analytics.metrics) }
95

96
    @Suppress("MagicNumber")
97
    val addonUpdater by lazyMonitored {
98
        DefaultAddonUpdater(context, AddonUpdater.Frequency(12, TimeUnit.HOURS))
Gabriel Luong's avatar
Gabriel Luong committed
99
100
    }

101
    @Suppress("MagicNumber")
102
    val supportedAddonsChecker by lazyMonitored {
103
        DefaultSupportedAddonsChecker(context, SupportedAddonsChecker.Frequency(12, TimeUnit.HOURS),
104
105
106
            onNotificationClickIntent = Intent(context, HomeActivity::class.java).apply {
                action = Intent.ACTION_VIEW
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
107
                data = "${BuildConfig.DEEP_LINK_SCHEME}://settings_addon_manager".toUri()
108
109
110
111
            }
        )
    }

112
    val addonManager by lazyMonitored {
Gabriel Luong's avatar
Gabriel Luong committed
113
114
115
        AddonManager(core.store, core.engine, addonCollectionProvider, addonUpdater)
    }

116
117
118
119
    val sitePermissionsStorage by lazyMonitored {
        SitePermissionsStorage(context, context.components.core.engine)
    }

120
121
122
123
124
125
126
127
    val analytics by lazyMonitored { Analytics(context) }
    val publicSuffixList by lazyMonitored { PublicSuffixList(context) }
    val clipboardHandler by lazyMonitored { ClipboardHandler(context) }
    val migrationStore by lazyMonitored { MigrationStore() }
    val performance by lazyMonitored { PerformanceComponent() }
    val push by lazyMonitored { Push(context, analytics.crashReporter) }
    val wifiConnectionMonitor by lazyMonitored { WifiConnectionMonitor(context as Application) }
    val strictMode by lazyMonitored { StrictModeManager(Config, this) }
128

129
    val settings by lazyMonitored { Settings(context) }
130

131
    val reviewPromptController by lazyMonitored {
132
        ReviewPromptController(
133
134
            manager = ReviewManagerFactory.create(context),
            reviewSettings = FenixReviewSettings(settings)
135
136
        )
    }
137
138
139
140
141
142
143
144
145
146
147

    val autofillConfiguration by lazyMonitored {
        AutofillConfiguration(
            storage = core.passwordsStorage,
            publicSuffixList = publicSuffixList,
            unlockActivity = AutofillUnlockActivity::class.java,
            confirmActivity = AutofillConfiguration::class.java,
            applicationName = context.getString(R.string.app_name),
            httpClient = core.client
        )
    }
Matthew Finkel's avatar
Matthew Finkel committed
148
149

    val torController by lazyMonitored { TorController(context) }
150
}