summaryrefslogtreecommitdiff
path: root/libs/androidfw/include
diff options
context:
space:
mode:
authorRyan Mitchell <rtmitchell@google.com>2020-03-11 10:26:08 -0700
committerRyan Mitchell <rtmitchell@google.com>2020-03-19 18:33:55 -0700
commitef40d2e832e900aba6dbcf8217903d7dbe689ca0 (patch)
tree96e56c54d587a36b13889d3eac4d89682000a625 /libs/androidfw/include
parentad2d924b3490c01168ad1dfeb2fa05ebc61b8ef2 (diff)
Refactor ApkAsset loading APIs
To add the partner requested ResourcesProvider#loadFromDir APIs, this change adds format type integer that allows us to reduce the number of ApkAssets loading overrides. This change also adds hidden offset and length based ResourcesProvider APIs that could not make R. Bug: 142716192 Test: atest FrameworksResourceLoaderTests Change-Id: I926fde257cae701901dcd4ca408024feae8c90a6 Merged-In: I926fde257cae701901dcd4ca408024feae8c90a6
Diffstat (limited to 'libs/androidfw/include')
-rw-r--r--libs/androidfw/include/androidfw/ApkAssets.h91
-rw-r--r--libs/androidfw/include/androidfw/Asset.h24
-rw-r--r--libs/androidfw/include/androidfw/LoadedArsc.h20
3 files changed, 81 insertions, 54 deletions
diff --git a/libs/androidfw/include/androidfw/ApkAssets.h b/libs/androidfw/include/androidfw/ApkAssets.h
index af802b0e50b9..643dc5c861f7 100644
--- a/libs/androidfw/include/androidfw/ApkAssets.h
+++ b/libs/androidfw/include/androidfw/ApkAssets.h
@@ -37,49 +37,46 @@ class LoadedIdmap;
// Holds an APK.
class ApkAssets {
- public:
- // Creates an ApkAssets.
- // If `system` is true, the package is marked as a system package, and allows some functions to
- // filter out this package when computing what configurations/resources are available.
- static std::unique_ptr<const ApkAssets> Load(const std::string& path, bool system = false,
- bool for_loader = false);
-
- // Creates an ApkAssets, but forces any package with ID 0x7f to be loaded as a shared library.
- // If `system` is true, the package is marked as a system package, and allows some functions to
- // filter out this package when computing what configurations/resources are available.
- static std::unique_ptr<const ApkAssets> LoadAsSharedLibrary(const std::string& path,
- bool system = false);
+ // This means the data extends to the end of the file.
+ static constexpr off64_t kUnknownLength = -1;
- // Creates an ApkAssets from an IDMAP, which contains the original APK path, and the overlay
- // data.
- // If `system` is true, the package is marked as a system package, and allows some functions to
- // filter out this package when computing what configurations/resources are available.
- static std::unique_ptr<const ApkAssets> LoadOverlay(const std::string& idmap_path,
- bool system = false);
+ public:
+ // Creates an ApkAssets from the zip path.
+ static std::unique_ptr<const ApkAssets> Load(const std::string& path,
+ package_property_t flags = 0U);
// Creates an ApkAssets from the given file descriptor, and takes ownership of the file
// descriptor. The `friendly_name` is some name that will be used to identify the source of
// this ApkAssets in log messages and other debug scenarios.
- // If `system` is true, the package is marked as a system package, and allows some functions to
- // filter out this package when computing what configurations/resources are available.
- // If `force_shared_lib` is true, any package with ID 0x7f is loaded as a shared library.
+ // If `length` equals kUnknownLength, offset must equal 0; otherwise, the apk data will be read
+ // using the `offset` into the file descriptor and will be `length` bytes long.
static std::unique_ptr<const ApkAssets> LoadFromFd(base::unique_fd fd,
- const std::string& friendly_name, bool system,
- bool force_shared_lib,
- bool for_loader = false);
+ const std::string& friendly_name,
+ package_property_t flags = 0U,
+ off64_t offset = 0,
+ off64_t length = kUnknownLength);
+
+ // Creates an ApkAssets from the given path which points to a resources.arsc.
+ static std::unique_ptr<const ApkAssets> LoadTable(const std::string& path,
+ package_property_t flags = 0U);
+
+ // Creates an ApkAssets from the given file descriptor which points to an resources.arsc, and
+ // takes ownership of the file descriptor.
+ // If `length` equals kUnknownLength, offset must equal 0; otherwise, the .arsc data will be read
+ // using the `offset` into the file descriptor and will be `length` bytes long.
+ static std::unique_ptr<const ApkAssets> LoadTableFromFd(base::unique_fd fd,
+ const std::string& friendly_name,
+ package_property_t flags = 0U,
+ off64_t offset = 0,
+ off64_t length = kUnknownLength);
- // Creates an empty wrapper ApkAssets from the given path which points to an .arsc.
- static std::unique_ptr<const ApkAssets> LoadArsc(const std::string& path,
- bool for_loader = false);
-
- // Creates an empty wrapper ApkAssets from the given file descriptor which points to an .arsc,
- // Takes ownership of the file descriptor.
- static std::unique_ptr<const ApkAssets> LoadArsc(base::unique_fd fd,
- const std::string& friendly_name,
- bool for_loader = false);
+ // Creates an ApkAssets from an IDMAP, which contains the original APK path, and the overlay
+ // data.
+ static std::unique_ptr<const ApkAssets> LoadOverlay(const std::string& idmap_path,
+ package_property_t flags = 0U);
// Creates a totally empty ApkAssets with no resources table and no file entries.
- static std::unique_ptr<const ApkAssets> LoadEmpty(bool for_loader = false);
+ static std::unique_ptr<const ApkAssets> LoadEmpty(package_property_t flags = 0U);
std::unique_ptr<Asset> Open(const std::string& path,
Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const;
@@ -105,28 +102,38 @@ class ApkAssets {
}
inline bool IsOverlay() const {
- return (property_flags_ & PROPERTY_OVERLAY) != 0;
+ return loaded_idmap_ != nullptr;
}
bool IsUpToDate() const;
- // Creates an Asset from any file on the file system.
+ // Creates an Asset from a file on disk.
static std::unique_ptr<Asset> CreateAssetFromFile(const std::string& path);
+ // Creates an Asset from a file descriptor.
+ //
+ // The asset takes ownership of the file descriptor. If `length` equals kUnknownLength, offset
+ // must equal 0; otherwise, the asset data will be read using the `offset` into the file
+ // descriptor and will be `length` bytes long.
+ static std::unique_ptr<Asset> CreateAssetFromFd(base::unique_fd fd,
+ const char* path,
+ off64_t offset = 0,
+ off64_t length = kUnknownLength);
private:
DISALLOW_COPY_AND_ASSIGN(ApkAssets);
- static std::unique_ptr<const ApkAssets> LoadImpl(base::unique_fd fd, const std::string& path,
+ static std::unique_ptr<const ApkAssets> LoadImpl(ZipArchiveHandle unmanaged_handle,
+ const std::string& path,
std::unique_ptr<Asset> idmap_asset,
- std::unique_ptr<const LoadedIdmap> loaded_idmap,
+ std::unique_ptr<const LoadedIdmap> idmap,
package_property_t property_flags);
- static std::unique_ptr<const ApkAssets> LoadArscImpl(base::unique_fd fd,
- const std::string& path,
- package_property_t property_flags);
+ static std::unique_ptr<const ApkAssets> LoadTableImpl(std::unique_ptr<Asset> resources_asset,
+ const std::string& path,
+ package_property_t property_flags);
ApkAssets(ZipArchiveHandle unmanaged_handle,
- const std::string& path,
+ std::string path,
time_t last_mod_time,
package_property_t property_flags);
diff --git a/libs/androidfw/include/androidfw/Asset.h b/libs/androidfw/include/androidfw/Asset.h
index 053dbb7864c6..75761747a5b4 100644
--- a/libs/androidfw/include/androidfw/Asset.h
+++ b/libs/androidfw/include/androidfw/Asset.h
@@ -26,6 +26,7 @@
#include <memory>
+#include <android-base/unique_fd.h>
#include <utils/Compat.h>
#include <utils/Errors.h>
#include <utils/String8.h>
@@ -202,8 +203,14 @@ private:
*/
static Asset* createFromUncompressedMap(FileMap* dataMap, AccessMode mode);
+ /*
+ * Create the asset from a memory-mapped file segment.
+ *
+ * The asset takes ownership of the FileMap and the file descriptor "fd". The file descriptor is
+ * used to request new file descriptors using "openFileDescriptor".
+ */
static std::unique_ptr<Asset> createFromUncompressedMap(std::unique_ptr<FileMap> dataMap,
- AccessMode mode);
+ base::unique_fd fd, AccessMode mode);
/*
* Create the asset from a memory-mapped file segment with compressed
@@ -256,9 +263,9 @@ public:
/*
* Use a memory-mapped region.
*
- * On success, the object takes ownership of "dataMap".
+ * On success, the object takes ownership of "dataMap" and "fd".
*/
- status_t openChunk(FileMap* dataMap);
+ status_t openChunk(FileMap* dataMap, base::unique_fd fd);
/*
* Standard Asset interfaces.
@@ -273,11 +280,12 @@ public:
virtual bool isAllocated(void) const { return mBuf != NULL; }
private:
- off64_t mStart; // absolute file offset of start of chunk
- off64_t mLength; // length of the chunk
- off64_t mOffset; // current local offset, 0 == mStart
- FILE* mFp; // for read/seek
- char* mFileName; // for opening
+ off64_t mStart; // absolute file offset of start of chunk
+ off64_t mLength; // length of the chunk
+ off64_t mOffset; // current local offset, 0 == mStart
+ FILE* mFp; // for read/seek
+ char* mFileName; // for opening
+ base::unique_fd mFd; // for opening file descriptors
/*
* To support getBuffer() we either need to read the entire thing into
diff --git a/libs/androidfw/include/androidfw/LoadedArsc.h b/libs/androidfw/include/androidfw/LoadedArsc.h
index b5d3a1fc6c1f..89ff9f52125d 100644
--- a/libs/androidfw/include/androidfw/LoadedArsc.h
+++ b/libs/androidfw/include/androidfw/LoadedArsc.h
@@ -69,12 +69,24 @@ struct TypeSpec {
}
};
+// Flags that change the behavior of loaded packages.
+// Keep in sync with f/b/android/content/res/ApkAssets.java
using package_property_t = uint32_t;
enum : package_property_t {
- PROPERTY_DYNAMIC = 1,
- PROPERTY_LOADER = 2,
- PROPERTY_OVERLAY = 4,
- PROPERTY_SYSTEM = 8,
+ // The package contains framework resource values specified by the system.
+ // This allows some functions to filter out this package when computing
+ // what configurations/resources are available.
+ PROPERTY_SYSTEM = 1U << 0U,
+
+ // The package is a shared library or has a package id of 7f and is loaded as a shared library by
+ // force.
+ PROPERTY_DYNAMIC = 1U << 1U,
+
+ // The package has been loaded dynamically using a ResourcesProvider.
+ PROPERTY_LOADER = 1U << 2U,
+
+ // The package is a RRO.
+ PROPERTY_OVERLAY = 1U << 3U,
};
// TypeSpecPtr points to a block of memory that holds a TypeSpec struct, followed by an array of