Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 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 | #include "ResourceUtils.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 18 | #include "link/ManifestFixer.h" |
| 19 | #include "util/Util.h" |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 20 | #include "xml/XmlActionExecutor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 21 | #include "xml/XmlDom.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 22 | |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame^] | 23 | #include <unordered_set> |
| 24 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 27 | /** |
| 28 | * This is how PackageManager builds class names from AndroidManifest.xml entries. |
| 29 | */ |
| 30 | static bool nameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
| 31 | SourcePathDiagnostics* diag) { |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 32 | // We allow unqualified class names (ie: .HelloActivity) |
| 33 | // Since we don't know the package name, we can just make a fake one here and |
| 34 | // the test will be identical as long as the real package name is valid too. |
| 35 | Maybe<std::u16string> fullyQualifiedClassName = |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame^] | 36 | util::getFullyQualifiedClassName(u"a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 37 | |
| 38 | StringPiece16 qualifiedClassName = fullyQualifiedClassName |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame^] | 39 | ? fullyQualifiedClassName.value() : attr->value; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 40 | if (!util::isJavaClassName(qualifiedClassName)) { |
| 41 | diag->error(DiagMessage(el->lineNumber) |
| 42 | << "attribute 'android:name' in <" |
| 43 | << el->name << "> tag must be a valid Java class name"); |
| 44 | return false; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | static bool optionalNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
| 50 | if (xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"name")) { |
| 51 | return nameIsJavaClassName(el, attr, diag); |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | static bool requiredNameIsJavaClassName(xml::Element* el, SourcePathDiagnostics* diag) { |
| 57 | if (xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"name")) { |
| 58 | return nameIsJavaClassName(el, attr, diag); |
| 59 | } |
| 60 | diag->error(DiagMessage(el->lineNumber) |
| 61 | << "<" << el->name << "> is missing attribute 'android:name'"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 65 | static bool verifyManifest(xml::Element* el, SourcePathDiagnostics* diag) { |
| 66 | xml::Attribute* attr = el->findAttribute({}, u"package"); |
| 67 | if (!attr) { |
| 68 | diag->error(DiagMessage(el->lineNumber) << "<manifest> tag is missing 'package' attribute"); |
| 69 | return false; |
| 70 | } else if (ResourceUtils::isReference(attr->value)) { |
| 71 | diag->error(DiagMessage(el->lineNumber) |
| 72 | << "attribute 'package' in <manifest> tag must not be a reference"); |
| 73 | return false; |
| 74 | } else if (!util::isJavaPackageName(attr->value)) { |
| 75 | diag->error(DiagMessage(el->lineNumber) |
| 76 | << "attribute 'package' in <manifest> tag is not a valid Java package name: '" |
| 77 | << attr->value << "'"); |
| 78 | return false; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 79 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 83 | bool ManifestFixer::buildRules(xml::XmlActionExecutor* executor, IDiagnostics* diag) { |
| 84 | // First verify some options. |
| 85 | if (mOptions.renameManifestPackage) { |
| 86 | if (!util::isJavaPackageName(mOptions.renameManifestPackage.value())) { |
| 87 | diag->error(DiagMessage() << "invalid manifest package override '" |
| 88 | << mOptions.renameManifestPackage.value() << "'"); |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (mOptions.renameInstrumentationTargetPackage) { |
| 94 | if (!util::isJavaPackageName(mOptions.renameInstrumentationTargetPackage.value())) { |
| 95 | diag->error(DiagMessage() << "invalid instrumentation target package override '" |
| 96 | << mOptions.renameInstrumentationTargetPackage.value() << "'"); |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Common intent-filter actions. |
| 102 | xml::XmlNodeAction intentFilterAction; |
| 103 | intentFilterAction[u"action"]; |
| 104 | intentFilterAction[u"category"]; |
| 105 | intentFilterAction[u"data"]; |
| 106 | |
| 107 | // Common meta-data actions. |
| 108 | xml::XmlNodeAction metaDataAction; |
| 109 | |
| 110 | // Manifest actions. |
| 111 | xml::XmlNodeAction& manifestAction = (*executor)[u"manifest"]; |
| 112 | manifestAction.action(verifyManifest); |
| 113 | manifestAction.action([&](xml::Element* el) -> bool { |
| 114 | if (mOptions.versionNameDefault) { |
| 115 | if (el->findAttribute(xml::kSchemaAndroid, u"versionName") == nullptr) { |
| 116 | el->attributes.push_back(xml::Attribute{ |
| 117 | xml::kSchemaAndroid, |
| 118 | u"versionName", |
| 119 | mOptions.versionNameDefault.value() }); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | if (mOptions.versionCodeDefault) { |
| 124 | if (el->findAttribute(xml::kSchemaAndroid, u"versionCode") == nullptr) { |
| 125 | el->attributes.push_back(xml::Attribute{ |
| 126 | xml::kSchemaAndroid, |
| 127 | u"versionCode", |
| 128 | mOptions.versionCodeDefault.value() }); |
| 129 | } |
| 130 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 131 | return true; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 132 | }); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 133 | |
Adam Lesinski | 5ff3ad6 | 2016-04-13 20:30:45 -0700 | [diff] [blame] | 134 | // Meta tags. |
| 135 | manifestAction[u"eat-comment"]; |
| 136 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 137 | // Uses-sdk actions. |
| 138 | manifestAction[u"uses-sdk"].action([&](xml::Element* el) -> bool { |
| 139 | if (mOptions.minSdkVersionDefault && |
| 140 | el->findAttribute(xml::kSchemaAndroid, u"minSdkVersion") == nullptr) { |
| 141 | // There was no minSdkVersion defined and we have a default to assign. |
| 142 | el->attributes.push_back(xml::Attribute{ |
| 143 | xml::kSchemaAndroid, u"minSdkVersion", |
| 144 | mOptions.minSdkVersionDefault.value() }); |
| 145 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 146 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 147 | if (mOptions.targetSdkVersionDefault && |
| 148 | el->findAttribute(xml::kSchemaAndroid, u"targetSdkVersion") == nullptr) { |
| 149 | // There was no targetSdkVersion defined and we have a default to assign. |
| 150 | el->attributes.push_back(xml::Attribute{ |
| 151 | xml::kSchemaAndroid, u"targetSdkVersion", |
| 152 | mOptions.targetSdkVersionDefault.value() }); |
| 153 | } |
| 154 | return true; |
| 155 | }); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 156 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 157 | // Instrumentation actions. |
| 158 | manifestAction[u"instrumentation"].action([&](xml::Element* el) -> bool { |
| 159 | if (!mOptions.renameInstrumentationTargetPackage) { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | if (xml::Attribute* attr = el->findAttribute(xml::kSchemaAndroid, u"targetPackage")) { |
| 164 | attr->value = mOptions.renameInstrumentationTargetPackage.value(); |
| 165 | } |
| 166 | return true; |
| 167 | }); |
| 168 | |
Adam Lesinski | 5ff3ad6 | 2016-04-13 20:30:45 -0700 | [diff] [blame] | 169 | manifestAction[u"original-package"]; |
Adam Lesinski | c728c3d | 2016-04-06 17:40:25 -0700 | [diff] [blame] | 170 | manifestAction[u"protected-broadcast"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 171 | manifestAction[u"uses-permission"]; |
| 172 | manifestAction[u"permission"]; |
| 173 | manifestAction[u"permission-tree"]; |
| 174 | manifestAction[u"permission-group"]; |
| 175 | |
| 176 | manifestAction[u"uses-configuration"]; |
| 177 | manifestAction[u"uses-feature"]; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 178 | manifestAction[u"supports-screens"]; |
| 179 | manifestAction[u"compatible-screens"]; |
| 180 | manifestAction[u"supports-gl-texture"]; |
| 181 | |
| 182 | // Application actions. |
| 183 | xml::XmlNodeAction& applicationAction = (*executor)[u"manifest"][u"application"]; |
| 184 | applicationAction.action(optionalNameIsJavaClassName); |
| 185 | |
Adam Lesinski | fee32d4 | 2016-05-31 15:07:20 -0700 | [diff] [blame] | 186 | // Uses library actions. |
| 187 | applicationAction[u"uses-library"]; |
| 188 | |
Adam Lesinski | 5d84ad5 | 2016-06-23 13:18:16 -0700 | [diff] [blame] | 189 | // Meta-data. |
| 190 | applicationAction[u"meta-data"] = metaDataAction; |
| 191 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 192 | // Activity actions. |
| 193 | applicationAction[u"activity"].action(requiredNameIsJavaClassName); |
| 194 | applicationAction[u"activity"][u"intent-filter"] = intentFilterAction; |
| 195 | applicationAction[u"activity"][u"meta-data"] = metaDataAction; |
| 196 | |
| 197 | // Activity alias actions. |
| 198 | applicationAction[u"activity-alias"][u"intent-filter"] = intentFilterAction; |
| 199 | applicationAction[u"activity-alias"][u"meta-data"] = metaDataAction; |
| 200 | |
| 201 | // Service actions. |
| 202 | applicationAction[u"service"].action(requiredNameIsJavaClassName); |
| 203 | applicationAction[u"service"][u"intent-filter"] = intentFilterAction; |
| 204 | applicationAction[u"service"][u"meta-data"] = metaDataAction; |
| 205 | |
| 206 | // Receiver actions. |
| 207 | applicationAction[u"receiver"].action(requiredNameIsJavaClassName); |
| 208 | applicationAction[u"receiver"][u"intent-filter"] = intentFilterAction; |
| 209 | applicationAction[u"receiver"][u"meta-data"] = metaDataAction; |
| 210 | |
| 211 | // Provider actions. |
| 212 | applicationAction[u"provider"].action(requiredNameIsJavaClassName); |
| 213 | applicationAction[u"provider"][u"grant-uri-permissions"]; |
| 214 | applicationAction[u"provider"][u"meta-data"] = metaDataAction; |
| 215 | applicationAction[u"provider"][u"path-permissions"]; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 219 | class FullyQualifiedClassNameVisitor : public xml::Visitor { |
| 220 | public: |
| 221 | using xml::Visitor::visit; |
| 222 | |
Chih-Hung Hsieh | d53e3be | 2016-05-03 10:02:51 -0700 | [diff] [blame] | 223 | explicit FullyQualifiedClassNameVisitor(const StringPiece16& package) : mPackage(package) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void visit(xml::Element* el) override { |
| 227 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame^] | 228 | if (attr.namespaceUri == xml::kSchemaAndroid |
| 229 | && mClassAttributes.find(attr.name) != mClassAttributes.end()) { |
| 230 | if (Maybe<std::u16string> newValue = |
| 231 | util::getFullyQualifiedClassName(mPackage, attr.value)) { |
| 232 | attr.value = std::move(newValue.value()); |
| 233 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | // Super implementation to iterate over the children. |
| 238 | xml::Visitor::visit(el); |
| 239 | } |
| 240 | |
| 241 | private: |
| 242 | StringPiece16 mPackage; |
Adam Lesinski | 71965e8 | 2016-07-07 17:12:12 -0700 | [diff] [blame^] | 243 | std::unordered_set<StringPiece16> mClassAttributes = { u"name" }; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 246 | static bool renameManifestPackage(const StringPiece16& packageOverride, xml::Element* manifestEl) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 247 | xml::Attribute* attr = manifestEl->findAttribute({}, u"package"); |
| 248 | |
| 249 | // We've already verified that the manifest element is present, with a package name specified. |
| 250 | assert(attr); |
| 251 | |
| 252 | std::u16string originalPackage = std::move(attr->value); |
| 253 | attr->value = packageOverride.toString(); |
| 254 | |
| 255 | FullyQualifiedClassNameVisitor visitor(originalPackage); |
| 256 | manifestEl->accept(&visitor); |
| 257 | return true; |
| 258 | } |
| 259 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 260 | bool ManifestFixer::consume(IAaptContext* context, xml::XmlResource* doc) { |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 261 | xml::Element* root = xml::findRootElement(doc->root.get()); |
| 262 | if (!root || !root->namespaceUri.empty() || root->name != u"manifest") { |
| 263 | context->getDiagnostics()->error(DiagMessage(doc->file.source) |
| 264 | << "root tag must be <manifest>"); |
| 265 | return false; |
| 266 | } |
| 267 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 268 | if ((mOptions.minSdkVersionDefault || mOptions.targetSdkVersionDefault) |
| 269 | && root->findChild({}, u"uses-sdk") == nullptr) { |
| 270 | // Auto insert a <uses-sdk> element. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 271 | std::unique_ptr<xml::Element> usesSdk = util::make_unique<xml::Element>(); |
| 272 | usesSdk->name = u"uses-sdk"; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 273 | root->addChild(std::move(usesSdk)); |
| 274 | } |
| 275 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 276 | xml::XmlActionExecutor executor; |
| 277 | if (!buildRules(&executor, context->getDiagnostics())) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | if (!executor.execute(xml::XmlActionExecutorPolicy::Whitelist, context->getDiagnostics(), |
| 282 | doc)) { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | if (mOptions.renameManifestPackage) { |
| 287 | // Rename manifest package outside of the XmlActionExecutor. |
| 288 | // We need to extract the old package name and FullyQualify all class names. |
| 289 | if (!renameManifestPackage(mOptions.renameManifestPackage.value(), root)) { |
| 290 | return false; |
| 291 | } |
| 292 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | |
| 296 | } // namespace aapt |