| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 17 | #include "link/Linkers.h" | 
|  | 18 |  | 
|  | 19 | #include <algorithm> | 
|  | 20 | #include <iterator> | 
|  | 21 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 22 | #include "android-base/logging.h" | 
|  | 23 |  | 
|  | 24 | #include "ResourceTable.h" | 
|  | 25 |  | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | namespace aapt { | 
|  | 27 |  | 
|  | 28 | template <typename InputContainer, typename OutputIterator, typename Predicate> | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 29 | OutputIterator move_if(InputContainer& input_container, OutputIterator result, Predicate pred) { | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | const auto last = input_container.end(); | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 31 | auto new_end = std::find_if(input_container.begin(), input_container.end(), pred); | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 32 | if (new_end == last) { | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | return result; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | } | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 35 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 36 | *result = std::move(*new_end); | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 37 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 38 | auto first = new_end; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | ++first; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 40 |  | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 41 | for (; first != last; ++first) { | 
|  | 42 | if (bool(pred(*first))) { | 
|  | 43 | // We want to move this guy | 
|  | 44 | *result = std::move(*first); | 
|  | 45 | ++result; | 
|  | 46 | } else { | 
|  | 47 | // We want to keep this guy, but we will need to move it up the list to | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 48 | // replace missing items. | 
|  | 49 | *new_end = std::move(*first); | 
|  | 50 | ++new_end; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 51 | } | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 54 | input_container.erase(new_end, last); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | return result; | 
| Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 58 | bool PrivateAttributeMover::Consume(IAaptContext* context, ResourceTable* table) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | for (auto& package : table->packages) { | 
| Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 60 | ResourceTableType* type = package->FindTypeWithDefaultName(ResourceType::kAttr); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | if (!type) { | 
|  | 62 | continue; | 
|  | 63 | } | 
|  | 64 |  | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 65 | if (type->visibility_level != Visibility::Level::kPublic) { | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | // No public attributes, so we can safely leave these private attributes | 
|  | 67 | // where they are. | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 68 | continue; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 71 | std::vector<std::unique_ptr<ResourceEntry>> private_attr_entries; | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 |  | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 73 | move_if(type->entries, std::back_inserter(private_attr_entries), | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 74 | [](const std::unique_ptr<ResourceEntry>& entry) -> bool { | 
| Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 75 | return entry->visibility.level != Visibility::Level::kPublic; | 
| Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | }); | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 77 |  | 
|  | 78 | if (private_attr_entries.empty()) { | 
|  | 79 | // No private attributes. | 
|  | 80 | continue; | 
|  | 81 | } | 
|  | 82 |  | 
| Iurii Makhno | f0c5ff4 | 2022-02-22 13:31:02 +0000 | [diff] [blame] | 83 | auto attr_private_type = ResourceNamedTypeWithDefaultName(ResourceType::kAttrPrivate); | 
|  | 84 | ResourceTableType* priv_attr_type = package->FindOrCreateType(attr_private_type); | 
| Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 85 | CHECK(priv_attr_type->entries.empty()); | 
|  | 86 | priv_attr_type->entries = std::move(private_attr_entries); | 
| Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 87 | } | 
|  | 88 | return true; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | }  // namespace aapt |