• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 APKASSETS_H_
18 #define APKASSETS_H_
19 
20 #include <utils/RefBase.h>
21 
22 #include <memory>
23 #include <string>
24 
25 #include "android-base/macros.h"
26 #include "android-base/unique_fd.h"
27 #include "androidfw/Asset.h"
28 #include "androidfw/AssetsProvider.h"
29 #include "androidfw/Idmap.h"
30 #include "androidfw/LoadedArsc.h"
31 #include "androidfw/misc.h"
32 
33 namespace android {
34 
35 class ApkAssets;
36 
37 using ApkAssetsPtr = sp<ApkAssets>;
38 
39 // Holds an APK.
40 class ApkAssets : public RefBase {
41  public:
42   // Creates an ApkAssets from a path on device.
43   static ApkAssetsPtr Load(const std::string& path, package_property_t flags = 0U);
44 
45   // Creates an ApkAssets from an open file descriptor.
46   static ApkAssetsPtr LoadFromFd(base::unique_fd fd, const std::string& debug_name,
47                                  package_property_t flags = 0U, off64_t offset = 0,
48                                  off64_t len = AssetsProvider::kUnknownLength);
49 
50   // Creates an ApkAssets from an AssetProvider.
51   // The ApkAssets will take care of destroying the AssetsProvider when it is destroyed.
52   static ApkAssetsPtr Load(std::unique_ptr<AssetsProvider> assets, package_property_t flags = 0U);
53 
54   // Creates an ApkAssets from the given asset file representing a resources.arsc.
55   static ApkAssetsPtr LoadTable(std::unique_ptr<Asset> resources_asset,
56                                 std::unique_ptr<AssetsProvider> assets,
57                                 package_property_t flags = 0U);
58 
59   // Creates an ApkAssets from an IDMAP, which contains the original APK path, and the overlay
60   // data.
61   static ApkAssetsPtr LoadOverlay(const std::string& idmap_path, package_property_t flags = 0U);
62 
63   // Path to the contents of the ApkAssets on disk. The path could represent an APk, a directory,
64   // or some other file type.
65   std::optional<std::string_view> GetPath() const;
66 
67   const std::string& GetDebugName() const;
68 
GetAssetsProvider()69   const AssetsProvider* GetAssetsProvider() const {
70     return assets_provider_.get();
71   }
72 
73   // This is never nullptr.
GetLoadedArsc()74   const LoadedArsc* GetLoadedArsc() const {
75     return loaded_arsc_.get();
76   }
77 
GetLoadedIdmap()78   const LoadedIdmap* GetLoadedIdmap() const {
79     return loaded_idmap_.get();
80   }
81 
IsLoader()82   bool IsLoader() const {
83     return (property_flags_ & PROPERTY_LOADER) != 0;
84   }
85 
IsOverlay()86   bool IsOverlay() const {
87     return loaded_idmap_ != nullptr;
88   }
89 
90   // Returns whether the resources.arsc is allocated in RAM (not mmapped).
IsTableAllocated()91   bool IsTableAllocated() const {
92     return resources_asset_ != nullptr && resources_asset_->isAllocated();
93   }
94 
95   bool IsUpToDate() const;
96 
97  private:
98   static ApkAssetsPtr LoadImpl(std::unique_ptr<AssetsProvider> assets,
99                                package_property_t property_flags,
100                                std::unique_ptr<Asset> idmap_asset,
101                                std::unique_ptr<LoadedIdmap> loaded_idmap);
102 
103   static ApkAssetsPtr LoadImpl(std::unique_ptr<Asset> resources_asset,
104                                std::unique_ptr<AssetsProvider> assets,
105                                package_property_t property_flags,
106                                std::unique_ptr<Asset> idmap_asset,
107                                std::unique_ptr<LoadedIdmap> loaded_idmap);
108 
109   // Allows us to make it possible to call make_shared from inside the class but still keeps the
110   // ctor 'private' for all means and purposes.
111   struct PrivateConstructorUtil {
112     explicit PrivateConstructorUtil() = default;
113   };
114 
115  public:
116   ApkAssets(PrivateConstructorUtil, std::unique_ptr<Asset> resources_asset,
117             std::unique_ptr<LoadedArsc> loaded_arsc, std::unique_ptr<AssetsProvider> assets,
118             package_property_t property_flags, std::unique_ptr<Asset> idmap_asset,
119             std::unique_ptr<LoadedIdmap> loaded_idmap);
120 
121   std::unique_ptr<Asset> resources_asset_;
122   std::unique_ptr<LoadedArsc> loaded_arsc_;
123 
124   std::unique_ptr<AssetsProvider> assets_provider_;
125   package_property_t property_flags_ = 0U;
126 
127   std::unique_ptr<Asset> idmap_asset_;
128   std::unique_ptr<LoadedIdmap> loaded_idmap_;
129 };
130 
131 } // namespace android
132 
133 #endif // APKASSETS_H_