• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #ifndef IDMAP2_INCLUDE_IDMAP2_RESOURCECONTAINER_H_
18 #define IDMAP2_INCLUDE_IDMAP2_RESOURCECONTAINER_H_
19 
20 #include <memory>
21 #include <string>
22 #include <variant>
23 #include <vector>
24 
25 #include "idmap2/Policies.h"
26 #include "idmap2/ResourceUtils.h"
27 
28 namespace android::idmap2 {
29 
30 struct ResourceContainer {
31   WARN_UNUSED virtual Result<uint32_t> GetCrc() const = 0;
32   WARN_UNUSED virtual const std::string& GetPath() const = 0;
33   WARN_UNUSED virtual Result<std::string> GetResourceName(ResourceId id) const = 0;
34 
35   virtual ~ResourceContainer() = default;
36 };
37 
38 struct TargetResourceContainer : public ResourceContainer {
39   static Result<std::unique_ptr<TargetResourceContainer>> FromPath(std::string path);
40 
41   WARN_UNUSED virtual Result<bool> DefinesOverlayable() const = 0;
42   WARN_UNUSED virtual Result<const android::OverlayableInfo*> GetOverlayableInfo(
43       ResourceId id) const = 0;
44   WARN_UNUSED virtual Result<ResourceId> GetResourceId(const std::string& name) const = 0;
45 
46   ~TargetResourceContainer() override = default;
47 };
48 
49 struct OverlayData {
50   struct ResourceIdValue {
51     // The overlay resource id.
52     ResourceId overlay_id;
53 
54     // Whether or not references to the overlay resource id should be rewritten to its corresponding
55     // target id during resource resolution.
56     bool rewrite_id;
57   };
58 
59   struct Value {
60     std::string resource_name;
61     std::variant<ResourceIdValue, TargetValueWithConfig> value;
62   };
63 
64   struct InlineStringPoolData {
65     // The binary data of the android::ResStringPool string pool.
66     std::unique_ptr<uint8_t[]> data;
67 
68     // The length of the binary data.
69     uint32_t data_length;
70 
71     // The offset added to TargetValue#data_value (the index of the string in the inline string
72     // pool) in order to prevent the indices of the overlay resource table string pool from
73     // colliding with the inline string pool indices.
74     uint32_t string_pool_offset;
75   };
76 
77   // The overlay's mapping of target resource name to overlaid value. Use a vector to enforce that
78   // the overlay pairs are inserted into the ResourceMapping in the specified ordered.
79   std::vector<Value> pairs;
80 
81   // If the overlay maps a target resource to a string literal (not a string resource), then the
82   // this field contains information about the string pool in which the string literal resides so it
83   // can be inlined into an idmap.
84   std::optional<InlineStringPoolData> string_pool_data;
85 };
86 
87 struct OverlayResourceContainer : public ResourceContainer {
88   static Result<std::unique_ptr<OverlayResourceContainer>> FromPath(std::string path);
89 
90   WARN_UNUSED virtual Result<OverlayManifestInfo> FindOverlayInfo(
91       const std::string& name) const = 0;
92   WARN_UNUSED virtual Result<OverlayData> GetOverlayData(const OverlayManifestInfo& info) const = 0;
93 
94   ~OverlayResourceContainer() override = default;
95 };
96 
97 }  // namespace android::idmap2
98 
99 #endif  // IDMAP2_INCLUDE_IDMAP2_RESOURCECONTAINER_H_
100