|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 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.autofill.impl.service |
| 18 | + |
| 19 | +import android.annotation.SuppressLint |
| 20 | +import android.app.assist.AssistStructure |
| 21 | +import android.app.assist.AssistStructure.ViewNode |
| 22 | +import android.view.autofill.AutofillId |
| 23 | +import com.duckduckgo.appbuildconfig.api.AppBuildConfig |
| 24 | +import com.duckduckgo.autofill.impl.service.AutofillFieldType.UNKNOWN |
| 25 | +import com.duckduckgo.di.scopes.AppScope |
| 26 | +import com.squareup.anvil.annotations.ContributesBinding |
| 27 | +import dagger.SingleInstanceIn |
| 28 | +import javax.inject.Inject |
| 29 | +import timber.log.Timber |
| 30 | + |
| 31 | +interface AutofillParser { |
| 32 | + // Parses structure, detects autofill fields, and returns a list of root nodes. |
| 33 | + // Each root node contains a list of parsed autofill fields. |
| 34 | + // We intend that each root node has packageId and/or website, based on child values, but it's not guaranteed. |
| 35 | + fun parseStructure(structure: AssistStructure): MutableList<AutofillRootNode> |
| 36 | +} |
| 37 | + |
| 38 | +// Parsed root node of the autofill structure |
| 39 | +data class AutofillRootNode( |
| 40 | + val packageId: String?, |
| 41 | + val website: String?, |
| 42 | + val parsedAutofillFields: List<ParsedAutofillField>, // Parsed fields in the structure |
| 43 | +) |
| 44 | + |
| 45 | +// Parsed autofill field |
| 46 | +data class ParsedAutofillField( |
| 47 | + val autofillId: AutofillId, |
| 48 | + val packageId: String?, |
| 49 | + val website: String?, |
| 50 | + val value: String, |
| 51 | + val type: AutofillFieldType = AutofillFieldType.UNKNOWN, |
| 52 | + val originalNode: ViewNode, |
| 53 | +) |
| 54 | + |
| 55 | +enum class AutofillFieldType { |
| 56 | + USERNAME, |
| 57 | + PASSWORD, |
| 58 | + UNKNOWN, |
| 59 | +} |
| 60 | + |
| 61 | +@SingleInstanceIn(AppScope::class) |
| 62 | +@ContributesBinding(AppScope::class) |
| 63 | +class RealAutofillParser @Inject constructor( |
| 64 | + private val appBuildConfig: AppBuildConfig, |
| 65 | + private val viewNodeClassifier: ViewNodeClassifier, |
| 66 | +) : AutofillParser { |
| 67 | + |
| 68 | + override fun parseStructure(structure: AssistStructure): MutableList<AutofillRootNode> { |
| 69 | + val autofillRootNodes = mutableListOf<AutofillRootNode>() |
| 70 | + val windowNodeCount = structure.windowNodeCount |
| 71 | + for (i in 0 until windowNodeCount) { |
| 72 | + val windowNode = structure.getWindowNodeAt(i) |
| 73 | + windowNode.rootViewNode?.let { viewNode -> |
| 74 | + autofillRootNodes.add( |
| 75 | + traverseViewNode(viewNode).convertIntoAutofillNode(), |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | + return autofillRootNodes |
| 80 | + } |
| 81 | + |
| 82 | + private fun traverseViewNode( |
| 83 | + viewNode: ViewNode, |
| 84 | + ): MutableList<ParsedAutofillField> { |
| 85 | + val autofillId = viewNode.autofillId ?: return mutableListOf() |
| 86 | + Timber.v("DDGAutofillService Parsing NODE: $autofillId") |
| 87 | + val traversalDataList = mutableListOf<ParsedAutofillField>() |
| 88 | + val packageId = viewNode.validPackageId() |
| 89 | + val website = viewNode.website() |
| 90 | + val autofillType = viewNodeClassifier.classify(viewNode) |
| 91 | + val value = kotlin.runCatching { viewNode.autofillValue?.textValue?.toString() ?: "" }.getOrDefault("") |
| 92 | + val parsedAutofillField = ParsedAutofillField( |
| 93 | + autofillId = autofillId, |
| 94 | + packageId = packageId, |
| 95 | + website = website, |
| 96 | + value = value, |
| 97 | + type = autofillType, |
| 98 | + originalNode = viewNode, |
| 99 | + ) |
| 100 | + Timber.v("DDGAutofillService Parsed as: $parsedAutofillField") |
| 101 | + traversalDataList.add(parsedAutofillField) |
| 102 | + |
| 103 | + for (i in 0 until viewNode.childCount) { |
| 104 | + val childNode = viewNode.getChildAt(i) |
| 105 | + traversalDataList.addAll(traverseViewNode(childNode)) |
| 106 | + } |
| 107 | + |
| 108 | + return traversalDataList |
| 109 | + } |
| 110 | + |
| 111 | + private fun List<ParsedAutofillField>.convertIntoAutofillNode(): AutofillRootNode { |
| 112 | + return AutofillRootNode( |
| 113 | + packageId = this.firstOrNull { it.packageId != null }?.packageId, |
| 114 | + website = this.firstOrNull { it.website != null }?.website, |
| 115 | + parsedAutofillFields = this, |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + private fun ViewNode.validPackageId(): String? { |
| 120 | + return this.idPackage |
| 121 | + .takeUnless { it.isNullOrBlank() } |
| 122 | + ?.takeUnless { it in INVALID_PACKAGE_ID } |
| 123 | + } |
| 124 | + |
| 125 | + @SuppressLint("NewApi") |
| 126 | + private fun ViewNode.website(): String? { |
| 127 | + return this.webDomain?.takeUnless { it.isBlank() } |
| 128 | + ?.let { nonEmptyDomain -> |
| 129 | + val scheme = if (appBuildConfig.sdkInt >= 28) { |
| 130 | + this.webScheme.takeUnless { it.isNullOrBlank() } ?: "http" |
| 131 | + } else { |
| 132 | + "http" |
| 133 | + } |
| 134 | + "$scheme://$nonEmptyDomain" |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + companion object { |
| 139 | + private val INVALID_PACKAGE_ID = listOf("android") |
| 140 | + } |
| 141 | +} |
0 commit comments