16
16
17
17
package com.duckduckgo.pir.impl.dashboard.messaging.handlers
18
18
19
+ import com.duckduckgo.app.di.AppCoroutineScope
20
+ import com.duckduckgo.common.utils.DispatcherProvider
19
21
import com.duckduckgo.di.scopes.ActivityScope
20
22
import com.duckduckgo.js.messaging.api.JsMessage
21
23
import com.duckduckgo.js.messaging.api.JsMessageCallback
22
24
import com.duckduckgo.js.messaging.api.JsMessaging
23
25
import com.duckduckgo.pir.impl.dashboard.messaging.PirDashboardWebMessages
24
26
import com.duckduckgo.pir.impl.dashboard.messaging.model.PirWebMessageResponse
27
+ import com.duckduckgo.subscriptions.api.Subscriptions
25
28
import com.squareup.anvil.annotations.ContributesMultibinding
26
29
import javax.inject.Inject
30
+ import kotlinx.coroutines.CoroutineScope
31
+ import kotlinx.coroutines.launch
27
32
import logcat.logcat
28
33
29
34
/* *
@@ -33,7 +38,11 @@ import logcat.logcat
33
38
scope = ActivityScope ::class ,
34
39
boundType = PirWebJsMessageHandler ::class ,
35
40
)
36
- class PirWebHandshakeMessageHandler @Inject constructor() : PirWebJsMessageHandler() {
41
+ class PirWebHandshakeMessageHandler @Inject constructor(
42
+ private val subscriptions : Subscriptions ,
43
+ private val dispatcherProvider : DispatcherProvider ,
44
+ @AppCoroutineScope private val appCoroutineScope : CoroutineScope ,
45
+ ) : PirWebJsMessageHandler() {
37
46
38
47
override val message = PirDashboardWebMessages .HANDSHAKE
39
48
@@ -44,14 +53,25 @@ class PirWebHandshakeMessageHandler @Inject constructor() : PirWebJsMessageHandl
44
53
) {
45
54
logcat { " PIR-WEB: PirWebHandshakeMessageHandler: process $jsMessage " }
46
55
47
- jsMessaging.sendResponse(
48
- jsMessage = jsMessage,
49
- response = PirWebMessageResponse .HandshakeResponse (
50
- success = true ,
51
- userData = PirWebMessageResponse .HandshakeResponse .UserData (
52
- isAuthenticatedUser = true ,
56
+ appCoroutineScope.launch(dispatcherProvider.io()) {
57
+ // Web request contains the version of the API the web uses, but no version check comparison is needed
58
+ // per https://app.asana.com/1/137249556945/task/1205606257846476/comment/1211089906912712?focus=true
59
+ // (PIR is backwards and forwards compatible)
60
+
61
+ jsMessaging.sendResponse(
62
+ jsMessage = jsMessage,
63
+ response = PirWebMessageResponse .HandshakeResponse (
64
+ success = true ,
65
+ userData = getHandshakeUserData(),
53
66
),
54
- ),
67
+ )
68
+ }
69
+ }
70
+
71
+ private suspend fun getHandshakeUserData (): PirWebMessageResponse .HandshakeResponse .UserData {
72
+ return PirWebMessageResponse .HandshakeResponse .UserData (
73
+ isAuthenticatedUser = subscriptions.getAccessToken() != null ,
74
+ isUserEligibleForFreeTrial = subscriptions.isFreeTrialEligible(),
55
75
)
56
76
}
57
77
}
0 commit comments