Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate autofill repo #1229

Merged
merged 2 commits into from May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -4,3 +4,6 @@
[submodule "submodules/privacy-grade"]
path = submodules/privacy-grade
url = https://github.com/duckduckgo/privacy-grade
[submodule "submodules/autofill"]
path = submodules/autofill
url = https://github.com/duckduckgo/duckduckgo-autofill
7 changes: 7 additions & 0 deletions app/build.gradle
Expand Up @@ -32,6 +32,13 @@ android {
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
androidTest.resources.srcDirs += files("$projectDir/../submodules/".toString())
main {
java {
resources {
srcDirs += files("$projectDir/../submodules/autofill/dist/".toString())
}
}
}
}
}
compileOptions {
Expand Down
Expand Up @@ -24,6 +24,7 @@ import com.duckduckgo.app.browser.R
import com.nhaarman.mockitokotlin2.*
import org.junit.Before
import org.junit.Test
import java.io.BufferedReader

class EmailInjectorJsTest {

Expand Down Expand Up @@ -121,9 +122,7 @@ class EmailInjectorJsTest {
}

private fun getJsToEvaluate(): String {
val js = InstrumentationRegistry.getInstrumentation().targetContext.resources.openRawResource(R.raw.autofill)
.bufferedReader()
.use { it.readText() }
val js = readResource().use { it?.readText() }.orEmpty()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use for the win

return "javascript:$js"
}

Expand All @@ -133,4 +132,8 @@ class EmailInjectorJsTest {
.use { it.readText() }
return "javascript:$js"
}

private fun readResource(): BufferedReader? {
return javaClass.classLoader?.getResource("autofill.js")?.openStream()?.bufferedReader()
}
}
13 changes: 10 additions & 3 deletions app/src/main/java/com/duckduckgo/app/email/EmailInjector.kt
Expand Up @@ -22,6 +22,7 @@ import androidx.annotation.UiThread
import com.duckduckgo.app.browser.DuckDuckGoUrlDetector
import com.duckduckgo.app.browser.R
import com.duckduckgo.app.email.EmailJavascriptInterface.Companion.JAVASCRIPT_INTERFACE_NAME
import java.io.BufferedReader

interface EmailInjector {
fun injectEmailAutofillJs(webView: WebView, url: String?)
Expand All @@ -42,7 +43,7 @@ class EmailInjectorJs(private val emailManager: EmailManager, private val urlDet
override fun injectEmailAutofillJs(webView: WebView, url: String?) {
if (!hasJsBeenInjected && (isDuckDuckGoUrl(url) || emailManager.isSignedIn())) {
hasJsBeenInjected = true
webView.evaluateJavascript("javascript:${javaScriptInjector.getFunctionsJS(webView.context)}", null)
webView.evaluateJavascript("javascript:${javaScriptInjector.getFunctionsJS()}", null)
}
}

Expand All @@ -61,9 +62,9 @@ class EmailInjectorJs(private val emailManager: EmailManager, private val urlDet
private lateinit var functions: String
private lateinit var aliasFunctions: String

fun getFunctionsJS(context: Context): String {
fun getFunctionsJS(): String {
if (!this::functions.isInitialized) {
functions = context.resources.openRawResource(R.raw.autofill).bufferedReader().use { it.readText() }
functions = loadJs("autofill.js")
}
return functions
}
Expand All @@ -74,5 +75,11 @@ class EmailInjectorJs(private val emailManager: EmailManager, private val urlDet
}
return aliasFunctions.replace("%s", alias.orEmpty())
}

fun loadJs(resourceName: String): String = readResource(resourceName).use { it?.readText() }.orEmpty()

private fun readResource(resourceName: String): BufferedReader? {
return javaClass.classLoader?.getResource(resourceName)?.openStream()?.bufferedReader()
}
}
}