-
Notifications
You must be signed in to change notification settings - Fork 929
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add navigatorInterface + messageBridge support (#5461)
Task/Issue URL: https://app.asana.com/0/488551667048375/1208697679300718/f ### Description - Adds a new package to the `content-scope-scripts-impl` module to inject the `navigatorInterface` JSON from the privacy config. - Also adds the related `messageBridge` C-S-S feature which is required for SERP<->native communication. ### Steps to test this PR _navigatorInterface_ - [ ] Check out this branch - [ ] Apply the patch linked in the task - [ ] Go to https://privacy-test-pages.site/features/navigator-interface.html - [ ] Verify the page looks like this: <img width="348" alt="Screenshot 2025-01-16 at 00 51 01" src="https://github.com/user-attachments/assets/6bd33830-9ba5-43da-ab32-44e8c9fec690" /> _messageBridge_ - [ ] Go to duckduckgo.com - [ ] Open Chrome and go to `chrome://inspect` - [ ] In the console, type: `navigator.duckduckgo.createMessageBridge(“aiChat").notify("helloWorld”)` - [ ] Verify that you see the following: <img width="1048" alt="Screenshot 2025-01-16 at 14 59 51" src="https://github.com/user-attachments/assets/1f482055-727b-4f4b-a002-f8f8a7773e3d" />
- Loading branch information
Showing
25 changed files
with
1,118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
.../contentscopescripts/impl/features/messagebridge/MessageBridgeContentScopeConfigPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge | ||
|
||
import com.duckduckgo.contentscopescripts.api.ContentScopeConfigPlugin | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.MessageBridgeFeatureName.MessageBridge | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.MessageBridgeRepository | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import javax.inject.Inject | ||
|
||
@ContributesMultibinding(AppScope::class) | ||
class MessageBridgeContentScopeConfigPlugin @Inject constructor( | ||
private val messageBridgeRepository: MessageBridgeRepository, | ||
) : ContentScopeConfigPlugin { | ||
|
||
override fun config(): String { | ||
val featureName = MessageBridge.value | ||
val config = messageBridgeRepository.messageBridgeEntity.json | ||
return "\"$featureName\":$config" | ||
} | ||
|
||
override fun preferences(): String? { | ||
return null | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...om/duckduckgo/contentscopescripts/impl/features/messagebridge/MessageBridgeFeatureName.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge | ||
|
||
enum class MessageBridgeFeatureName(val value: String) { | ||
MessageBridge("messageBridge"), | ||
} |
24 changes: 24 additions & 0 deletions
24
...uckduckgo/contentscopescripts/impl/features/messagebridge/MessageBridgeFeatureNameUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge | ||
|
||
/** | ||
* Convenience method to get the [MessageBridgeFeatureName] from its [String] value | ||
*/ | ||
fun messageBridgeFeatureValueOf(value: String): MessageBridgeFeatureName? { | ||
return MessageBridgeFeatureName.entries.find { it.value == value } | ||
} |
43 changes: 43 additions & 0 deletions
43
.../duckduckgo/contentscopescripts/impl/features/messagebridge/MessageBridgeFeaturePlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge | ||
|
||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.MessageBridgeFeatureName.MessageBridge | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.MessageBridgeEntity | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.MessageBridgeRepository | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.privacy.config.api.PrivacyFeaturePlugin | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import javax.inject.Inject | ||
|
||
@ContributesMultibinding(AppScope::class) | ||
class MessageBridgeFeaturePlugin @Inject constructor( | ||
private val messageBridgeRepository: MessageBridgeRepository, | ||
) : PrivacyFeaturePlugin { | ||
|
||
override fun store(featureName: String, jsonString: String): Boolean { | ||
val messageBridgeFeatureName = messageBridgeFeatureValueOf(featureName) ?: return false | ||
if (messageBridgeFeatureName.value == this.featureName) { | ||
val entity = MessageBridgeEntity(json = jsonString) | ||
messageBridgeRepository.updateAll(messageBridgeEntity = entity) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
override val featureName: String = MessageBridge.value | ||
} |
57 changes: 57 additions & 0 deletions
57
.../com/duckduckgo/contentscopescripts/impl/features/messagebridge/di/MessageBridgeModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge.di | ||
|
||
import android.content.Context | ||
import androidx.room.Room | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.ALL_MIGRATIONS | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.MessageBridgeDatabase | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.MessageBridgeRepository | ||
import com.duckduckgo.contentscopescripts.impl.features.messagebridge.store.RealMessageBridgeRepository | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesTo | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.SingleInstanceIn | ||
import kotlinx.coroutines.CoroutineScope | ||
|
||
@Module | ||
@ContributesTo(AppScope::class) | ||
object MessageBridgeModule { | ||
|
||
@SingleInstanceIn(AppScope::class) | ||
@Provides | ||
fun provideMessageBridgeDatabase(context: Context): MessageBridgeDatabase { | ||
return Room.databaseBuilder(context, MessageBridgeDatabase::class.java, "message_bridge.db") | ||
.enableMultiInstanceInvalidation() | ||
.fallbackToDestructiveMigration() | ||
.addMigrations(*ALL_MIGRATIONS) | ||
.build() | ||
} | ||
|
||
@SingleInstanceIn(AppScope::class) | ||
@Provides | ||
fun provideMessageBridgeRepository( | ||
database: MessageBridgeDatabase, | ||
@AppCoroutineScope coroutineScope: CoroutineScope, | ||
dispatcherProvider: DispatcherProvider, | ||
): MessageBridgeRepository { | ||
return RealMessageBridgeRepository(database, coroutineScope, dispatcherProvider) | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../com/duckduckgo/contentscopescripts/impl/features/messagebridge/store/MessageBridgeDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge.store | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
|
||
@Dao | ||
abstract class MessageBridgeDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
abstract fun insert(messageBridgeEntity: MessageBridgeEntity) | ||
|
||
@Transaction | ||
open fun updateAll( | ||
messageBridgeEntity: MessageBridgeEntity, | ||
) { | ||
delete() | ||
insert(messageBridgeEntity) | ||
} | ||
|
||
@Query("select * from message_bridge") | ||
abstract fun get(): MessageBridgeEntity? | ||
|
||
@Query("delete from message_bridge") | ||
abstract fun delete() | ||
} |
34 changes: 34 additions & 0 deletions
34
...duckduckgo/contentscopescripts/impl/features/messagebridge/store/MessageBridgeDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge.store | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import androidx.room.migration.Migration | ||
|
||
@Database( | ||
exportSchema = true, | ||
version = 1, | ||
entities = [ | ||
MessageBridgeEntity::class, | ||
], | ||
) | ||
abstract class MessageBridgeDatabase : RoomDatabase() { | ||
abstract fun messageBridgeDao(): MessageBridgeDao | ||
} | ||
|
||
val ALL_MIGRATIONS = emptyArray<Migration>() |
26 changes: 26 additions & 0 deletions
26
...m/duckduckgo/contentscopescripts/impl/features/messagebridge/store/MessageBridgeEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge.store | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "message_bridge") | ||
data class MessageBridgeEntity( | ||
@PrimaryKey val id: Int = 1, | ||
val json: String, | ||
) |
58 changes: 58 additions & 0 deletions
58
...ckduckgo/contentscopescripts/impl/features/messagebridge/store/MessageBridgeRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2025 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.contentscopescripts.impl.features.messagebridge.store | ||
|
||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
interface MessageBridgeRepository { | ||
fun updateAll( | ||
messageBridgeEntity: MessageBridgeEntity, | ||
) | ||
var messageBridgeEntity: MessageBridgeEntity | ||
} | ||
|
||
class RealMessageBridgeRepository( | ||
val database: MessageBridgeDatabase, | ||
val coroutineScope: CoroutineScope, | ||
val dispatcherProvider: DispatcherProvider, | ||
) : MessageBridgeRepository { | ||
|
||
private val messageBridgeDao: MessageBridgeDao = database.messageBridgeDao() | ||
override var messageBridgeEntity = MessageBridgeEntity(json = EMPTY_JSON) | ||
|
||
init { | ||
coroutineScope.launch(dispatcherProvider.io()) { | ||
loadToMemory() | ||
} | ||
} | ||
|
||
override fun updateAll(messageBridgeEntity: MessageBridgeEntity) { | ||
messageBridgeDao.updateAll(messageBridgeEntity) | ||
loadToMemory() | ||
} | ||
|
||
private fun loadToMemory() { | ||
messageBridgeEntity = | ||
messageBridgeDao.get() ?: MessageBridgeEntity(json = EMPTY_JSON) | ||
} | ||
|
||
companion object { | ||
const val EMPTY_JSON = "{}" | ||
} | ||
} |
Oops, something went wrong.