|
| 1 | +/** |
| 2 | + * Provides classes and predicates for working with Android manifest files. |
| 3 | + */ |
| 4 | + |
| 5 | +import XML |
| 6 | + |
| 7 | +/** |
| 8 | + * An Android manifest file, named `AndroidManifest.xml`. |
| 9 | + */ |
| 10 | +class AndroidManifestXmlFile extends XMLFile { |
| 11 | + AndroidManifestXmlFile() { |
| 12 | + this.getBaseName() = "AndroidManifest.xml" and |
| 13 | + count(XMLElement e | e = this.getAChild()) = 1 and |
| 14 | + this.getAChild().getName() = "manifest" |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Gets the top-level `<manifest>` element in this Android manifest file. |
| 19 | + */ |
| 20 | + AndroidManifestXmlElement getManifestElement() { result = this.getAChild() } |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * A `<manifest>` element in an Android manifest file. |
| 25 | + */ |
| 26 | +class AndroidManifestXmlElement extends XMLElement { |
| 27 | + AndroidManifestXmlElement() { |
| 28 | + this.getParent() instanceof AndroidManifestXmlFile and this.getName() = "manifest" |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Gets the `<application>` child element of this `<manifest>` element. |
| 33 | + */ |
| 34 | + AndroidApplicationXmlElement getApplicationElement() { result = this.getAChild() } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets the value of the `package` attribute of this `<manifest>` element. |
| 38 | + */ |
| 39 | + string getPackageAttributeValue() { result = getAttributeValue("package") } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * An `<application>` element in an Android manifest file. |
| 44 | + */ |
| 45 | +class AndroidApplicationXmlElement extends XMLElement { |
| 46 | + AndroidApplicationXmlElement() { |
| 47 | + this.getParent() instanceof AndroidManifestXmlElement and this.getName() = "application" |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets a component child element of this `<application>` element. |
| 52 | + */ |
| 53 | + AndroidComponentXmlElement getAComponentElement() { result = this.getAChild() } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * An `<activity>` element in an Android manifest file. |
| 58 | + */ |
| 59 | +class AndroidActivityXmlElement extends AndroidComponentXmlElement { |
| 60 | + AndroidActivityXmlElement() { this.getName() = "activity" } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * A `<service>` element in an Android manifest file. |
| 65 | + */ |
| 66 | +class AndroidServiceXmlElement extends AndroidComponentXmlElement { |
| 67 | + AndroidServiceXmlElement() { this.getName() = "service" } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * A `<receiver>` element in an Android manifest file. |
| 72 | + */ |
| 73 | +class AndroidReceiverXmlElement extends AndroidComponentXmlElement { |
| 74 | + AndroidReceiverXmlElement() { this.getName() = "receiver" } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * A `<provider>` element in an Android manifest file. |
| 79 | + */ |
| 80 | +class AndroidProviderXmlElement extends AndroidComponentXmlElement { |
| 81 | + AndroidProviderXmlElement() { this.getName() = "provider" } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * An Android component element in an Android manifest file. |
| 86 | + */ |
| 87 | +class AndroidComponentXmlElement extends XMLElement { |
| 88 | + AndroidComponentXmlElement() { |
| 89 | + this.getParent() instanceof AndroidApplicationXmlElement and |
| 90 | + this.getName().regexpMatch("(activity|service|receiver|provider)") |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Gets an `<intent-filter>` child element of this component element. |
| 95 | + */ |
| 96 | + AndroidIntentFilterXmlElement getAnIntentFilterElement() { result = this.getAChild() } |
| 97 | + |
| 98 | + /** |
| 99 | + * Gets the value of the `android:name` attribute of this component element. |
| 100 | + */ |
| 101 | + string getComponentName() { |
| 102 | + exists(XMLAttribute attr | |
| 103 | + attr = getAnAttribute() and |
| 104 | + attr.getNamespace().getPrefix() = "android" and |
| 105 | + attr.getName() = "name" |
| 106 | + | |
| 107 | + result = attr.getValue() |
| 108 | + ) |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets the resolved value of the `android:name` attribute of this component element. |
| 113 | + */ |
| 114 | + string getResolvedComponentName() { |
| 115 | + if getComponentName().matches(".%") |
| 116 | + then |
| 117 | + result = getParent() |
| 118 | + .(XMLElement) |
| 119 | + .getParent() |
| 120 | + .(AndroidManifestXmlElement) |
| 121 | + .getPackageAttributeValue() + getComponentName() |
| 122 | + else result = getComponentName() |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Gets the value of the `android:exported` attribute of this component element. |
| 127 | + */ |
| 128 | + string getExportedAttributeValue() { |
| 129 | + exists(XMLAttribute attr | |
| 130 | + attr = getAnAttribute() and |
| 131 | + attr.getNamespace().getPrefix() = "android" and |
| 132 | + attr.getName() = "exported" |
| 133 | + | |
| 134 | + result = attr.getValue() |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Holds if the `android:exported` attribute of this component element is `true`. |
| 140 | + */ |
| 141 | + predicate isExported() { getExportedAttributeValue() = "true" } |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * An `<intent-filter>` element in an Android manifest file. |
| 146 | + */ |
| 147 | +class AndroidIntentFilterXmlElement extends XMLElement { |
| 148 | + AndroidIntentFilterXmlElement() { |
| 149 | + this.getFile() instanceof AndroidManifestXmlFile and this.getName() = "intent-filter" |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Gets an `<action>` child element of this `<intent-filter>` element. |
| 154 | + */ |
| 155 | + AndroidActionXmlElement getAnActionElement() { result = this.getAChild() } |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * An `<action>` element in an Android manifest file. |
| 160 | + */ |
| 161 | +class AndroidActionXmlElement extends XMLElement { |
| 162 | + AndroidActionXmlElement() { |
| 163 | + this.getFile() instanceof AndroidManifestXmlFile and this.getName() = "action" |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Gets the name of this action. |
| 168 | + */ |
| 169 | + string getActionName() { |
| 170 | + exists(XMLAttribute attr | |
| 171 | + attr = getAnAttribute() and |
| 172 | + attr.getNamespace().getPrefix() = "android" and |
| 173 | + attr.getName() = "name" |
| 174 | + | |
| 175 | + result = attr.getValue() |
| 176 | + ) |
| 177 | + } |
| 178 | +} |
0 commit comments