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 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 17 | #include "link/ManifestFixer.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <unordered_set> |
| 20 | |
| 21 | #include "android-base/logging.h" |
| 22 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 23 | #include "ResourceUtils.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 24 | #include "util/Util.h" |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 25 | #include "xml/XmlActionExecutor.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 26 | #include "xml/XmlDom.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 27 | |
| 28 | namespace aapt { |
| 29 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 30 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | * This is how PackageManager builds class names from AndroidManifest.xml |
| 32 | * entries. |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 33 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | static bool NameIsJavaClassName(xml::Element* el, xml::Attribute* attr, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 35 | SourcePathDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 36 | // We allow unqualified class names (ie: .HelloActivity) |
| 37 | // Since we don't know the package name, we can just make a fake one here and |
| 38 | // the test will be identical as long as the real package name is valid too. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | Maybe<std::string> fully_qualified_class_name = |
| 40 | util::GetFullyQualifiedClassName("a", attr->value); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 41 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | StringPiece qualified_class_name = fully_qualified_class_name |
| 43 | ? fully_qualified_class_name.value() |
| 44 | : attr->value; |
Adam Lesinski | d0f116b | 2016-07-08 15:00:32 -0700 | [diff] [blame] | 45 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 46 | if (!util::IsJavaClassName(qualified_class_name)) { |
| 47 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 48 | << "attribute 'android:name' in <" << el->name |
| 49 | << "> tag must be a valid Java class name"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 50 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 55 | static bool OptionalNameIsJavaClassName(xml::Element* el, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 56 | SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 58 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | static bool RequiredNameIsJavaClassName(xml::Element* el, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 64 | SourcePathDiagnostics* diag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | if (xml::Attribute* attr = el->FindAttribute(xml::kSchemaAndroid, "name")) { |
| 66 | return NameIsJavaClassName(el, attr, diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | << "<" << el->name << "> is missing attribute 'android:name'"); |
| 70 | return false; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | static bool VerifyManifest(xml::Element* el, SourcePathDiagnostics* diag) { |
| 74 | xml::Attribute* attr = el->FindAttribute({}, "package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | if (!attr) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 77 | << "<manifest> tag is missing 'package' attribute"); |
| 78 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | } else if (ResourceUtils::IsReference(attr->value)) { |
| 80 | diag->Error( |
| 81 | DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 82 | << "attribute 'package' in <manifest> tag must not be a reference"); |
| 83 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | } else if (!util::IsJavaPackageName(attr->value)) { |
| 85 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 86 | << "attribute 'package' in <manifest> tag is not a valid Java " |
| 87 | "package name: '" |
| 88 | << attr->value << "'"); |
| 89 | return false; |
| 90 | } |
| 91 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 94 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | * The coreApp attribute in <manifest> is not a regular AAPT attribute, so type |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | * checking on it is manual. |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 97 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | static bool FixCoreAppAttribute(xml::Element* el, SourcePathDiagnostics* diag) { |
| 99 | if (xml::Attribute* attr = el->FindAttribute("", "coreApp")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | std::unique_ptr<BinaryPrimitive> result = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 101 | ResourceUtils::TryParseBool(attr->value); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | if (!result) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 103 | diag->Error(DiagMessage(el->line_number) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | << "attribute coreApp must be a boolean"); |
| 105 | return false; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 106 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 107 | attr->compiled_value = std::move(result); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | } |
| 109 | return true; |
Adam Lesinski | 6b17d2c | 2016-08-10 11:37:06 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 112 | bool ManifestFixer::BuildRules(xml::XmlActionExecutor* executor, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 113 | IDiagnostics* diag) { |
| 114 | // First verify some options. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | if (options_.rename_manifest_package) { |
| 116 | if (!util::IsJavaPackageName(options_.rename_manifest_package.value())) { |
| 117 | diag->Error(DiagMessage() << "invalid manifest package override '" |
| 118 | << options_.rename_manifest_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | << "'"); |
| 120 | return false; |
| 121 | } |
| 122 | } |
| 123 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 124 | if (options_.rename_instrumentation_target_package) { |
| 125 | if (!util::IsJavaPackageName( |
| 126 | options_.rename_instrumentation_target_package.value())) { |
| 127 | diag->Error(DiagMessage() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 128 | << "invalid instrumentation target package override '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 129 | << options_.rename_instrumentation_target_package.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 130 | << "'"); |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Common intent-filter actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 136 | xml::XmlNodeAction intent_filter_action; |
| 137 | intent_filter_action["action"]; |
| 138 | intent_filter_action["category"]; |
| 139 | intent_filter_action["data"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | |
| 141 | // Common meta-data actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 142 | xml::XmlNodeAction meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 143 | |
| 144 | // Manifest actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | xml::XmlNodeAction& manifest_action = (*executor)["manifest"]; |
| 146 | manifest_action.Action(VerifyManifest); |
| 147 | manifest_action.Action(FixCoreAppAttribute); |
| 148 | manifest_action.Action([&](xml::Element* el) -> bool { |
| 149 | if (options_.version_name_default) { |
| 150 | if (el->FindAttribute(xml::kSchemaAndroid, "versionName") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 151 | el->attributes.push_back( |
| 152 | xml::Attribute{xml::kSchemaAndroid, "versionName", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 153 | options_.version_name_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 154 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 157 | if (options_.version_code_default) { |
| 158 | if (el->FindAttribute(xml::kSchemaAndroid, "versionCode") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 159 | el->attributes.push_back( |
| 160 | xml::Attribute{xml::kSchemaAndroid, "versionCode", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 161 | options_.version_code_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 162 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 163 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 164 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 165 | }); |
| 166 | |
| 167 | // Meta tags. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 168 | manifest_action["eat-comment"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 169 | |
| 170 | // Uses-sdk actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | manifest_action["uses-sdk"].Action([&](xml::Element* el) -> bool { |
| 172 | if (options_.min_sdk_version_default && |
| 173 | el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 174 | // There was no minSdkVersion defined and we have a default to assign. |
| 175 | el->attributes.push_back( |
| 176 | xml::Attribute{xml::kSchemaAndroid, "minSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 177 | options_.min_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | if (options_.target_sdk_version_default && |
| 181 | el->FindAttribute(xml::kSchemaAndroid, "targetSdkVersion") == nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 182 | // There was no targetSdkVersion defined and we have a default to assign. |
| 183 | el->attributes.push_back( |
| 184 | xml::Attribute{xml::kSchemaAndroid, "targetSdkVersion", |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 185 | options_.target_sdk_version_default.value()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 186 | } |
| 187 | return true; |
| 188 | }); |
| 189 | |
| 190 | // Instrumentation actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 191 | manifest_action["instrumentation"].Action([&](xml::Element* el) -> bool { |
| 192 | if (!options_.rename_instrumentation_target_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | if (xml::Attribute* attr = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 197 | el->FindAttribute(xml::kSchemaAndroid, "targetPackage")) { |
| 198 | attr->value = options_.rename_instrumentation_target_package.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 199 | } |
| 200 | return true; |
| 201 | }); |
| 202 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 203 | manifest_action["original-package"]; |
| 204 | manifest_action["protected-broadcast"]; |
| 205 | manifest_action["uses-permission"]; |
| 206 | manifest_action["permission"]; |
| 207 | manifest_action["permission-tree"]; |
| 208 | manifest_action["permission-group"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | manifest_action["uses-configuration"]; |
| 211 | manifest_action["uses-feature"]; |
| 212 | manifest_action["supports-screens"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 213 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | manifest_action["compatible-screens"]; |
| 215 | manifest_action["compatible-screens"]["screen"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 216 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 217 | manifest_action["supports-gl-texture"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | |
| 219 | // Application actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 220 | xml::XmlNodeAction& application_action = manifest_action["application"]; |
| 221 | application_action.Action(OptionalNameIsJavaClassName); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 222 | |
| 223 | // Uses library actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 224 | application_action["uses-library"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 225 | |
| 226 | // Meta-data. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 227 | application_action["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 228 | |
| 229 | // Activity actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 230 | application_action["activity"].Action(RequiredNameIsJavaClassName); |
| 231 | application_action["activity"]["intent-filter"] = intent_filter_action; |
| 232 | application_action["activity"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 233 | |
| 234 | // Activity alias actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 235 | application_action["activity-alias"]["intent-filter"] = intent_filter_action; |
| 236 | application_action["activity-alias"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 237 | |
| 238 | // Service actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 239 | application_action["service"].Action(RequiredNameIsJavaClassName); |
| 240 | application_action["service"]["intent-filter"] = intent_filter_action; |
| 241 | application_action["service"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 242 | |
| 243 | // Receiver actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 244 | application_action["receiver"].Action(RequiredNameIsJavaClassName); |
| 245 | application_action["receiver"]["intent-filter"] = intent_filter_action; |
| 246 | application_action["receiver"]["meta-data"] = meta_data_action; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 247 | |
| 248 | // Provider actions. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 249 | application_action["provider"].Action(RequiredNameIsJavaClassName); |
| 250 | application_action["provider"]["intent-filter"] = intent_filter_action; |
| 251 | application_action["provider"]["meta-data"] = meta_data_action; |
| 252 | application_action["provider"]["grant-uri-permissions"]; |
| 253 | application_action["provider"]["path-permissions"]; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 254 | |
| 255 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 258 | class FullyQualifiedClassNameVisitor : public xml::Visitor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 259 | public: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | using xml::Visitor::Visit; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 261 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 262 | explicit FullyQualifiedClassNameVisitor(const StringPiece& package) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 263 | : package_(package) {} |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 264 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 265 | void Visit(xml::Element* el) override { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 266 | for (xml::Attribute& attr : el->attributes) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 267 | if (attr.namespace_uri == xml::kSchemaAndroid && |
| 268 | class_attributes_.find(attr.name) != class_attributes_.end()) { |
| 269 | if (Maybe<std::string> new_value = |
| 270 | util::GetFullyQualifiedClassName(package_, attr.value)) { |
| 271 | attr.value = std::move(new_value.value()); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 272 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 273 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 276 | // Super implementation to iterate over the children. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 277 | xml::Visitor::Visit(el); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 281 | StringPiece package_; |
| 282 | std::unordered_set<StringPiece> class_attributes_ = {"name"}; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 283 | }; |
| 284 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 285 | static bool RenameManifestPackage(const StringPiece& package_override, |
| 286 | xml::Element* manifest_el) { |
| 287 | xml::Attribute* attr = manifest_el->FindAttribute({}, "package"); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 288 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 289 | // We've already verified that the manifest element is present, with a package |
| 290 | // name specified. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 291 | CHECK(attr != nullptr); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 292 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 293 | std::string original_package = std::move(attr->value); |
| 294 | attr->value = package_override.ToString(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | FullyQualifiedClassNameVisitor visitor(original_package); |
| 297 | manifest_el->Accept(&visitor); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 298 | return true; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 301 | bool ManifestFixer::Consume(IAaptContext* context, xml::XmlResource* doc) { |
| 302 | xml::Element* root = xml::FindRootElement(doc->root.get()); |
| 303 | if (!root || !root->namespace_uri.empty() || root->name != "manifest") { |
| 304 | context->GetDiagnostics()->Error(DiagMessage(doc->file.source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | << "root tag must be <manifest>"); |
| 306 | return false; |
| 307 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 308 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | if ((options_.min_sdk_version_default || |
| 310 | options_.target_sdk_version_default) && |
| 311 | root->FindChild({}, "uses-sdk") == nullptr) { |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 312 | // Auto insert a <uses-sdk> element. This must be inserted before the |
| 313 | // <application> tag. The device runtime PackageParser will make SDK version |
| 314 | // decisions while parsing <application>. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | std::unique_ptr<xml::Element> uses_sdk = util::make_unique<xml::Element>(); |
| 316 | uses_sdk->name = "uses-sdk"; |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 317 | root->InsertChild(0, std::move(uses_sdk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 318 | } |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 319 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 320 | xml::XmlActionExecutor executor; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 321 | if (!BuildRules(&executor, context->GetDiagnostics())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 322 | return false; |
| 323 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 325 | if (!executor.Execute(xml::XmlActionExecutorPolicy::kWhitelist, |
| 326 | context->GetDiagnostics(), doc)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 327 | return false; |
| 328 | } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 330 | if (options_.rename_manifest_package) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 331 | // Rename manifest package outside of the XmlActionExecutor. |
Adam Lesinski | e343eb1 | 2016-10-27 16:31:58 -0700 | [diff] [blame] | 332 | // We need to extract the old package name and FullyQualify all class |
| 333 | // names. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 334 | if (!RenameManifestPackage(options_.rename_manifest_package.value(), |
| 335 | root)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 336 | return false; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 337 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 338 | } |
| 339 | return true; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 342 | } // namespace aapt |