Home | History | Annotate | Download | only in androidfw
      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/LoadedArsc.h"
     28 #include "androidfw/misc.h"
     29 
     30 namespace android {
     31 
     32 class LoadedIdmap;
     33 
     34 // Holds an APK.
     35 class ApkAssets {
     36  public:
     37   // Creates an ApkAssets.
     38   // If `system` is true, the package is marked as a system package, and allows some functions to
     39   // filter out this package when computing what configurations/resources are available.
     40   static std::unique_ptr<const ApkAssets> Load(const std::string& path, bool system = false);
     41 
     42   // Creates an ApkAssets, but forces any package with ID 0x7f to be loaded as a shared library.
     43   // If `system` is true, the package is marked as a system package, and allows some functions to
     44   // filter out this package when computing what configurations/resources are available.
     45   static std::unique_ptr<const ApkAssets> LoadAsSharedLibrary(const std::string& path,
     46                                                               bool system = false);
     47 
     48   // Creates an ApkAssets from an IDMAP, which contains the original APK path, and the overlay
     49   // data.
     50   // If `system` is true, the package is marked as a system package, and allows some functions to
     51   // filter out this package when computing what configurations/resources are available.
     52   static std::unique_ptr<const ApkAssets> LoadOverlay(const std::string& idmap_path,
     53                                                       bool system = false);
     54 
     55   // Creates an ApkAssets from the given file descriptor, and takes ownership of the file
     56   // descriptor. The `friendly_name` is some name that will be used to identify the source of
     57   // this ApkAssets in log messages and other debug scenarios.
     58   // If `system` is true, the package is marked as a system package, and allows some functions to
     59   // filter out this package when computing what configurations/resources are available.
     60   // If `force_shared_lib` is true, any package with ID 0x7f is loaded as a shared library.
     61   static std::unique_ptr<const ApkAssets> LoadFromFd(base::unique_fd fd,
     62                                                      const std::string& friendly_name, bool system,
     63                                                      bool force_shared_lib);
     64 
     65   std::unique_ptr<Asset> Open(const std::string& path,
     66                               Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const;
     67 
     68   bool ForEachFile(const std::string& path,
     69                    const std::function<void(const StringPiece&, FileType)>& f) const;
     70 
     71   inline const std::string& GetPath() const {
     72     return path_;
     73   }
     74 
     75   // This is never nullptr.
     76   inline const LoadedArsc* GetLoadedArsc() const {
     77     return loaded_arsc_.get();
     78   }
     79 
     80  private:
     81   DISALLOW_COPY_AND_ASSIGN(ApkAssets);
     82 
     83   static std::unique_ptr<const ApkAssets> LoadImpl(base::unique_fd fd, const std::string& path,
     84                                                    std::unique_ptr<Asset> idmap_asset,
     85                                                    std::unique_ptr<const LoadedIdmap> loaded_idmap,
     86                                                    bool system, bool load_as_shared_library);
     87 
     88   // Creates an Asset from any file on the file system.
     89   static std::unique_ptr<Asset> CreateAssetFromFile(const std::string& path);
     90 
     91   ApkAssets(void* unmanaged_handle, const std::string& path);
     92 
     93   using ZipArchivePtr = std::unique_ptr<void, void(*)(void*)>;
     94 
     95   ZipArchivePtr zip_handle_;
     96   const std::string path_;
     97   std::unique_ptr<Asset> resources_asset_;
     98   std::unique_ptr<Asset> idmap_asset_;
     99   std::unique_ptr<const LoadedArsc> loaded_arsc_;
    100 };
    101 
    102 }  // namespace android
    103 
    104 #endif /* APKASSETS_H_ */
    105