Skip to content

Commit 912f035

Browse files
committed
Add GetNativeSettingsHandler to process getNativeSettings messages from SERP
Placeholder for now, adding persistence and the rest in another PR
1 parent 3ff2482 commit 912f035

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.settings.impl.serpsettings.messaging
18+
19+
import com.duckduckgo.app.di.AppCoroutineScope
20+
import com.duckduckgo.common.utils.AppUrl
21+
import com.duckduckgo.common.utils.DispatcherProvider
22+
import com.duckduckgo.contentscopescripts.api.ContentScopeJsMessageHandlersPlugin
23+
import com.duckduckgo.di.scopes.AppScope
24+
import com.duckduckgo.js.messaging.api.JsMessage
25+
import com.duckduckgo.js.messaging.api.JsMessageCallback
26+
import com.duckduckgo.js.messaging.api.JsMessageHandler
27+
import com.duckduckgo.js.messaging.api.JsMessaging
28+
import com.duckduckgo.settings.api.SettingsPageFeature
29+
import com.squareup.anvil.annotations.ContributesMultibinding
30+
import kotlinx.coroutines.CoroutineScope
31+
import kotlinx.coroutines.launch
32+
import logcat.logcat
33+
import javax.inject.Inject
34+
35+
/**
36+
* Handles the getNativeSettings message from SERP to retrieve stored settings.
37+
*
38+
* If there are no stored settings, it returns an empty JSON object.
39+
*/
40+
@ContributesMultibinding(AppScope::class)
41+
class GetNativeSettingsHandler @Inject constructor(
42+
private val dispatcherProvider: DispatcherProvider,
43+
@AppCoroutineScope private val appScope: CoroutineScope,
44+
private val settingsPageFeature: SettingsPageFeature,
45+
// TODO: Inject data store when implemented
46+
) : ContentScopeJsMessageHandlersPlugin {
47+
48+
override fun getJsMessageHandler(): JsMessageHandler =
49+
object : JsMessageHandler {
50+
override fun process(
51+
jsMessage: JsMessage,
52+
jsMessaging: JsMessaging,
53+
jsMessageCallback: JsMessageCallback?,
54+
) {
55+
appScope.launch(dispatcherProvider.io()) {
56+
if (settingsPageFeature.serpSettingsSync().isEnabled()) {
57+
logcat { "SERP-SETTINGS: GetNativeSettingsHandler processing message" }
58+
59+
// TODO return settings from datastore or empty object
60+
}
61+
}
62+
}
63+
64+
override val allowedDomains: List<String> = listOf(AppUrl.Url.HOST)
65+
override val featureName: String = "serpSettings"
66+
override val methods: List<String> = listOf(GET_NATIVE_SETTINGS_METHOD_NAME)
67+
}
68+
69+
companion object {
70+
private const val GET_NATIVE_SETTINGS_METHOD_NAME = "getNativeSettings"
71+
}
72+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.settings.impl.serpsettings.messaging
18+
19+
import com.duckduckgo.common.test.CoroutineTestRule
20+
import com.duckduckgo.settings.api.SettingsPageFeature
21+
import org.junit.Assert.*
22+
import org.junit.Rule
23+
import org.junit.Test
24+
import org.mockito.kotlin.mock
25+
26+
class GetNativeSettingsHandlerTest {
27+
28+
@get:Rule
29+
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
30+
31+
private val handler = GetNativeSettingsHandler(
32+
dispatcherProvider = coroutineTestRule.testDispatcherProvider,
33+
appScope = coroutineTestRule.testScope,
34+
settingsPageFeature = mock<SettingsPageFeature>(),
35+
).getJsMessageHandler()
36+
37+
@Test
38+
fun `only allow duckduckgo dot com domains`() {
39+
val domains = handler.allowedDomains
40+
assertEquals(1, domains.size)
41+
assertEquals("duckduckgo.com", domains.first())
42+
}
43+
44+
@Test
45+
fun `feature name is serpSettings`() {
46+
assertEquals("serpSettings", handler.featureName)
47+
}
48+
49+
@Test
50+
fun `only contains getNativeSettings method`() {
51+
val methods = handler.methods
52+
assertEquals(1, methods.size)
53+
assertEquals("getNativeSettings", methods[0])
54+
}
55+
}

0 commit comments

Comments
 (0)