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 <memory> 21 #include <string> 22 23 #include "android-base/macros.h" 24 #include "android-base/unique_fd.h" 25 26 #include "androidfw/Asset.h" 27 #include "androidfw/AssetsProvider.h" 28 #include "androidfw/Idmap.h" 29 #include "androidfw/LoadedArsc.h" 30 #include "androidfw/misc.h" 31 32 namespace android { 33 34 // Holds an APK. 35 class ApkAssets { 36 public: 37 // Creates an ApkAssets from a path on device. 38 static std::unique_ptr<ApkAssets> Load(const std::string& path, 39 package_property_t flags = 0U); 40 41 // Creates an ApkAssets from an open file descriptor. 42 static std::unique_ptr<ApkAssets> LoadFromFd(base::unique_fd fd, 43 const std::string& debug_name, 44 package_property_t flags = 0U, 45 off64_t offset = 0, 46 off64_t len = AssetsProvider::kUnknownLength); 47 48 // Creates an ApkAssets from an AssetProvider. 49 // The ApkAssets will take care of destroying the AssetsProvider when it is destroyed. 50 static std::unique_ptr<ApkAssets> Load(std::unique_ptr<AssetsProvider> assets, 51 package_property_t flags = 0U); 52 53 // Creates an ApkAssets from the given asset file representing a resources.arsc. 54 static std::unique_ptr<ApkAssets> LoadTable(std::unique_ptr<Asset> resources_asset, 55 std::unique_ptr<AssetsProvider> assets, 56 package_property_t flags = 0U); 57 58 // Creates an ApkAssets from an IDMAP, which contains the original APK path, and the overlay 59 // data. 60 static std::unique_ptr<ApkAssets> LoadOverlay(const std::string& idmap_path, 61 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 std::unique_ptr<ApkAssets> 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 std::unique_ptr<ApkAssets> 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 ApkAssets(std::unique_ptr<Asset> resources_asset, 110 std::unique_ptr<LoadedArsc> loaded_arsc, 111 std::unique_ptr<AssetsProvider> assets, 112 package_property_t property_flags, 113 std::unique_ptr<Asset> idmap_asset, 114 std::unique_ptr<LoadedIdmap> loaded_idmap); 115 116 std::unique_ptr<Asset> resources_asset_; 117 std::unique_ptr<LoadedArsc> loaded_arsc_; 118 119 std::unique_ptr<AssetsProvider> assets_provider_; 120 package_property_t property_flags_ = 0U; 121 122 std::unique_ptr<Asset> idmap_asset_; 123 std::unique_ptr<LoadedIdmap> loaded_idmap_; 124 }; 125 126 } // namespace android 127 128 #endif // APKASSETS_H_